From 9aa7010ded5a32b818f0cc5d77c45e0402e377b5 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Tue, 7 Jun 2011 20:24:49 +0000 Subject: [PATCH] Added support for reading .osg, .osgb, .osgx, .osgt and .ive models that contain volumes from within the volume tag. --- src/osgPresentation/SlideShowConstructor.cpp | 272 ++++++++++++------- 1 file changed, 170 insertions(+), 102 deletions(-) diff --git a/src/osgPresentation/SlideShowConstructor.cpp b/src/osgPresentation/SlideShowConstructor.cpp index 77e500f37..ca35f4100 100644 --- a/src/osgPresentation/SlideShowConstructor.cpp +++ b/src/osgPresentation/SlideShowConstructor.cpp @@ -1753,130 +1753,198 @@ void SlideShowConstructor::addVolume(const std::string& filename, const Position // osg::Object::DataVariance defaultMatrixDataVariance = osg::Object::DYNAMIC; // STATIC std::string foundFile = filename; - - osgDB::FileType fileType = osgDB::fileType(foundFile); - if (fileType == osgDB::FILE_NOT_FOUND) - { - foundFile = findFileAndRecordPath(foundFile); - fileType = osgDB::fileType(foundFile); - } - osg::ref_ptr image; - if (fileType == osgDB::DIRECTORY) + osg::ref_ptr volume; + osg::ref_ptr tile; + osg::ref_ptr layer; + + // check for wild cards + if (filename.find('*')!=std::string::npos) { - image = osgDB::readImageFile(foundFile+".dicom", _options.get()); - } - else if (fileType == osgDB::REGULAR_FILE) - { - image = osgDB::readImageFile( foundFile, _options.get() ); + osgDB::DirectoryContents filenames = osgDB::expandWildcardsInFilename(filename); + if (filenames.empty()) return; + + // make sure images are in alphabetical order. + std::sort(filenames.begin(), filenames.end()); + + typedef std::vector< osg::ref_ptr > Images; + Images images; + for(osgDB::DirectoryContents::iterator itr = filenames.begin(); + itr != filenames.end(); + ++itr) + { + osg::ref_ptr loadedImage = osgDB::readImageFile(*itr); + if (loadedImage.valid()) + { + OSG_NOTICE<<"Image loaded "<<*itr< obj = osgDB::readObjectFile(foundFile); + image = dynamic_cast(obj.get()); + volume = dynamic_cast(obj.get()); + } + else + { + image = osgDB::readImageFile( foundFile, _options.get() ); + } + } + else + { + // not found image, so fallback to plguins/callbacks to find the model. + image = osgDB::readImageFile( filename, _options.get() ); + if (image) recordOptionsFilePath(_options.get() ); + } } + + if (!image && !volume) return; - if (!image) return; - - osg::ref_ptr details = dynamic_cast(image->getUserData()); - osg::ref_ptr matrix = details ? details->getMatrix() : dynamic_cast(image->getUserData()); - - osg::ref_ptr volume = new osgVolume::Volume; - osg::ref_ptr tile = new osgVolume::VolumeTile; - volume->addChild(tile.get()); - - osg::ref_ptr layer = new osgVolume::ImageLayer(image.get()); - if (details) + if (volume.valid()) { - layer->setTexelOffset(details->getTexelOffset()); - layer->setTexelScale(details->getTexelScale()); + if (!tile) + { + if (volume->getNumChildren()>0) + { + tile = dynamic_cast(volume->getChild(0)); + } + } } - layer->rescaleToZeroToOneRange(); - - if (matrix.valid()) + else { - layer->setLocator(new osgVolume::Locator(*matrix)); - osg::Matrix tm = osg::Matrix::scale(volumeData.region[3]-volumeData.region[0], volumeData.region[4]-volumeData.region[1], volumeData.region[5]-volumeData.region[2]) * - osg::Matrix::translate(volumeData.region[0],volumeData.region[1],volumeData.region[2]); - tile->setLocator(new osgVolume::Locator(tm * (*matrix))); + volume = new osgVolume::Volume; } - - tile->setLayer(layer.get()); - - osgVolume::SwitchProperty* sp = new osgVolume::SwitchProperty; - sp->setActiveProperty(0); - - osgVolume::AlphaFuncProperty* ap = new osgVolume::AlphaFuncProperty(volumeData.cutoffValue); - osgVolume::TransparencyProperty* tp = new osgVolume::TransparencyProperty(volumeData.alphaValue); - osgVolume::SampleDensityProperty* sd = new osgVolume::SampleDensityProperty(volumeData.sampleDensityValue); - osgVolume::SampleDensityWhenMovingProperty* sdm = (volumeData.sampleDensityWhenMovingValue > 0.0f) ? (new osgVolume::SampleDensityWhenMovingProperty(volumeData.sampleDensityWhenMovingValue)) : 0; - osgVolume::TransferFunctionProperty* tfp = volumeData.transferFunction.valid() ? new osgVolume::TransferFunctionProperty(volumeData.transferFunction.get()) : 0; - + if (tile.valid()) { - // Standard - osgVolume::CompositeProperty* cp = new osgVolume::CompositeProperty; - cp->addProperty(ap); - cp->addProperty(sd); - cp->addProperty(tp); - if (sdm) cp->addProperty(sdm); - if (tfp) cp->addProperty(tfp); - - sp->addProperty(cp); + layer = dynamic_cast(tile->getLayer()); + image = layer.valid() ? layer->getImage() : 0; } - + else { - // Light - osgVolume::CompositeProperty* cp = new osgVolume::CompositeProperty; - cp->addProperty(ap); - cp->addProperty(sd); - cp->addProperty(tp); - cp->addProperty(new osgVolume::LightingProperty); - if (sdm) cp->addProperty(sdm); - if (tfp) cp->addProperty(tfp); + if (!image) return; - sp->addProperty(cp); + tile = new osgVolume::VolumeTile; + volume->addChild(tile.get()); } + if (!layer) { - // Isosurface - osgVolume::CompositeProperty* cp = new osgVolume::CompositeProperty; - cp->addProperty(sd); - cp->addProperty(tp); - cp->addProperty(new osgVolume::IsoSurfaceProperty(volumeData.cutoffValue)); - if (sdm) cp->addProperty(sdm); - if (tfp) cp->addProperty(tfp); + if (!image) return; - sp->addProperty(cp); + osg::ref_ptr details = dynamic_cast(image->getUserData()); + osg::ref_ptr matrix = details ? details->getMatrix() : dynamic_cast(image->getUserData()); + + osg::ref_ptr layer = new osgVolume::ImageLayer(image.get()); + if (details) + { + layer->setTexelOffset(details->getTexelOffset()); + layer->setTexelScale(details->getTexelScale()); + } + layer->rescaleToZeroToOneRange(); + + if (matrix.valid()) + { + layer->setLocator(new osgVolume::Locator(*matrix)); + osg::Matrix tm = osg::Matrix::scale(volumeData.region[3]-volumeData.region[0], volumeData.region[4]-volumeData.region[1], volumeData.region[5]-volumeData.region[2]) * + osg::Matrix::translate(volumeData.region[0],volumeData.region[1],volumeData.region[2]); + tile->setLocator(new osgVolume::Locator(tm * (*matrix))); + } + + + tile->setLayer(layer.get()); + + osgVolume::SwitchProperty* sp = new osgVolume::SwitchProperty; + sp->setActiveProperty(0); + + osgVolume::AlphaFuncProperty* ap = new osgVolume::AlphaFuncProperty(volumeData.cutoffValue); + osgVolume::TransparencyProperty* tp = new osgVolume::TransparencyProperty(volumeData.alphaValue); + osgVolume::SampleDensityProperty* sd = new osgVolume::SampleDensityProperty(volumeData.sampleDensityValue); + osgVolume::SampleDensityWhenMovingProperty* sdm = (volumeData.sampleDensityWhenMovingValue > 0.0f) ? (new osgVolume::SampleDensityWhenMovingProperty(volumeData.sampleDensityWhenMovingValue)) : 0; + osgVolume::TransferFunctionProperty* tfp = volumeData.transferFunction.valid() ? new osgVolume::TransferFunctionProperty(volumeData.transferFunction.get()) : 0; + + { + // Standard + osgVolume::CompositeProperty* cp = new osgVolume::CompositeProperty; + cp->addProperty(ap); + cp->addProperty(sd); + cp->addProperty(tp); + if (sdm) cp->addProperty(sdm); + if (tfp) cp->addProperty(tfp); + + sp->addProperty(cp); + } + + { + // Light + osgVolume::CompositeProperty* cp = new osgVolume::CompositeProperty; + cp->addProperty(ap); + cp->addProperty(sd); + cp->addProperty(tp); + cp->addProperty(new osgVolume::LightingProperty); + if (sdm) cp->addProperty(sdm); + if (tfp) cp->addProperty(tfp); + + sp->addProperty(cp); + } + + { + // Isosurface + osgVolume::CompositeProperty* cp = new osgVolume::CompositeProperty; + cp->addProperty(sd); + cp->addProperty(tp); + cp->addProperty(new osgVolume::IsoSurfaceProperty(volumeData.cutoffValue)); + if (sdm) cp->addProperty(sdm); + if (tfp) cp->addProperty(tfp); + + sp->addProperty(cp); + } + + { + // MaximumIntensityProjection + osgVolume::CompositeProperty* cp = new osgVolume::CompositeProperty; + cp->addProperty(ap); + cp->addProperty(sd); + cp->addProperty(tp); + cp->addProperty(new osgVolume::MaximumIntensityProjectionProperty); + if (sdm) cp->addProperty(sdm); + if (tfp) cp->addProperty(tfp); + + sp->addProperty(cp); + } + + switch(volumeData.shadingModel) + { + case(VolumeData::Standard): sp->setActiveProperty(0); break; + case(VolumeData::Light): sp->setActiveProperty(1); break; + case(VolumeData::Isosurface): sp->setActiveProperty(2); break; + case(VolumeData::MaximumIntensityProjection): sp->setActiveProperty(3); break; + } + + layer->addProperty(sp); + tile->setVolumeTechnique(new osgVolume::RayTracedTechnique); + tile->setEventCallback(new osgVolume::PropertyAdjustmentCallback()); } - { - // MaximumIntensityProjection - osgVolume::CompositeProperty* cp = new osgVolume::CompositeProperty; - cp->addProperty(ap); - cp->addProperty(sd); - cp->addProperty(tp); - cp->addProperty(new osgVolume::MaximumIntensityProjectionProperty); - if (sdm) cp->addProperty(sdm); - if (tfp) cp->addProperty(tfp); - - sp->addProperty(cp); - } - - switch(volumeData.shadingModel) - { - case(VolumeData::Standard): sp->setActiveProperty(0); break; - case(VolumeData::Light): sp->setActiveProperty(1); break; - case(VolumeData::Isosurface): sp->setActiveProperty(2); break; - case(VolumeData::MaximumIntensityProjection): sp->setActiveProperty(3); break; - } - - layer->addProperty(sp); - tile->setVolumeTechnique(new osgVolume::RayTracedTechnique); - tile->setEventCallback(new osgVolume::PropertyAdjustmentCallback()); - - osg::ref_ptr model = volume.get(); if (volumeData.useTabbedDragger || volumeData.useTrackballDragger)