Added preliminary support for writing VolumeSettings to disk from within Present3D in response to pressing and releasing 'Ctrl-S'.

git-svn-id: http://svn.openscenegraph.org/osg/OpenSceneGraph/trunk@14363 16af8721-9629-0410-8352-f15c8da7e697
This commit is contained in:
Robert Osfield 2014-07-04 16:45:05 +00:00
parent 70f9bdc6e1
commit f2d11bb46e
7 changed files with 238 additions and 69 deletions

View File

@ -292,11 +292,11 @@ public:
useTabbedDragger(false),
useTrackballDragger(false),
region_in_pixel_coords(false),
alphaValue("1.0"),
cutoffValue("0.1"),
exteriorTransparencyFactorValue(""),
sampleDensityValue("0.005"),
sampleRatioValue("1.0"),
alphaValue(),
cutoffValue(),
exteriorTransparencyFactorValue(),
sampleDensityValue(),
sampleRatioValue(),
colorSpaceOperation(osg::NO_COLOR_SPACE_OPERATION),
colorModulate(1.0f,1.0f,1.0f,1.0f),
technique(osgVolume::VolumeSettings::RayTraced)

View File

@ -40,6 +40,7 @@ class SampleDensityProperty;
class SampleDensityWhenMovingProperty;
class TransparencyProperty;
class ExteriorTransparencyFactorProperty;
class VolumeSettings;
class OSGVOLUME_EXPORT PropertyVisitor
{
@ -49,24 +50,24 @@ class OSGVOLUME_EXPORT PropertyVisitor
virtual ~PropertyVisitor() {}
virtual void apply(Property&) {}
virtual void apply(Property&);
virtual void apply(CompositeProperty&);
virtual void apply(SwitchProperty&);
virtual void apply(TransferFunctionProperty&) {}
virtual void apply(ScalarProperty&) {}
virtual void apply(IsoSurfaceProperty&) {}
virtual void apply(AlphaFuncProperty&) {}
virtual void apply(MaximumIntensityProjectionProperty&) {}
virtual void apply(LightingProperty&) {}
virtual void apply(SampleRatioProperty&) {}
virtual void apply(SampleRatioWhenMovingProperty&) {}
virtual void apply(SampleDensityProperty&) {}
virtual void apply(SampleDensityWhenMovingProperty&) {}
virtual void apply(TransparencyProperty&) {}
virtual void apply(ExteriorTransparencyFactorProperty&) {}
virtual void apply(TransferFunctionProperty&);
virtual void apply(ScalarProperty&);
virtual void apply(IsoSurfaceProperty&);
virtual void apply(AlphaFuncProperty&);
virtual void apply(MaximumIntensityProjectionProperty&);
virtual void apply(LightingProperty&);
virtual void apply(SampleRatioProperty&);
virtual void apply(SampleRatioWhenMovingProperty&);
virtual void apply(SampleDensityProperty&);
virtual void apply(SampleDensityWhenMovingProperty&);
virtual void apply(TransparencyProperty&);
virtual void apply(ExteriorTransparencyFactorProperty&);
virtual void apply(VolumeSettings&);
bool _traverseOnlyActiveChildren;
};
@ -82,6 +83,7 @@ class OSGVOLUME_EXPORT Property : public osg::Object
META_Object(osgVolume, Property);
virtual void accept(PropertyVisitor& pv) { pv.apply(*this); }
virtual void traverse(PropertyVisitor& pv) {}
protected:
@ -101,6 +103,16 @@ class OSGVOLUME_EXPORT CompositeProperty : public Property
virtual void accept(PropertyVisitor& pv) { pv.apply(*this); }
virtual void traverse(PropertyVisitor& pv)
{
for(Properties::iterator itr = _properties.begin();
itr != _properties.end();
++itr)
{
(*itr)->accept(pv);
}
}
void clear();
typedef std::vector< osg::ref_ptr<Property> > Properties;
@ -139,6 +151,22 @@ class OSGVOLUME_EXPORT SwitchProperty : public CompositeProperty
virtual void accept(PropertyVisitor& pv) { pv.apply(*this); }
virtual void traverse(PropertyVisitor& pv)
{
if (pv._traverseOnlyActiveChildren)
{
if (_activeProperty>=0 && static_cast<unsigned int>(_activeProperty)<=getNumProperties())
{
_properties[_activeProperty]->accept(pv);
}
}
else
{
CompositeProperty::traverse(pv);
}
}
/** Set which child property is active.
* -1 disables all children.*/
void setActiveProperty(int i) { _activeProperty = i; }
@ -411,7 +439,6 @@ class OSGVOLUME_EXPORT CollectPropertiesVisitor : public osgVolume::PropertyVisi
CollectPropertiesVisitor(bool traverseOnlyActiveChildren=true);
virtual void apply(Property&);
virtual void apply(TransferFunctionProperty&);
virtual void apply(ScalarProperty&);
virtual void apply(IsoSurfaceProperty& iso);

View File

@ -31,6 +31,7 @@ class OSGVOLUME_EXPORT VolumeSettings : public Property
META_Object(osgVolume, VolumeSettings);
virtual void accept(PropertyVisitor& pv);
virtual void traverse(PropertyVisitor& pv);
enum Technique
{

View File

@ -1432,10 +1432,19 @@ void ReaderWriterP3DXML::parseVolume(osgPresentation::SlideShowConstructor& cons
if (getProperty(cur, "vs", vs) || getProperty(cur, "VolumeSettings", vs))
{
volumeData.volumeSettings = osgDB::readFile<osgVolume::VolumeSettings>(vs);
OSG_NOTICE<<"VolumeSetting read "<<vs<<" "<<volumeData.volumeSettings.get()<<std::endl;
if (volumeData.volumeSettings.valid())
{
OSG_NOTICE<<"VolumeSetting read "<<vs<<" "<<volumeData.volumeSettings.get()<<std::endl;
volumeData.volumeSettings->setName(vs);
OSG_NOTICE<<" assigned name to VS "<<volumeData.volumeSettings->getName()<<std::endl;
}
}
if (!volumeData.volumeSettings) volumeData.volumeSettings = new osgVolume::VolumeSettings;
if (!volumeData.volumeSettings)
{
OSG_NOTICE<<"VolumeSetting fallback has been created"<<std::endl;
volumeData.volumeSettings = new osgVolume::VolumeSettings;
}
// check the rendering technique/shading model to use
std::string technique;

View File

@ -2510,6 +2510,82 @@ protected:
std::string _source;
};
struct CollectVolumeSettingsVisitor : public osgVolume::PropertyVisitor
{
CollectVolumeSettingsVisitor():
osgVolume::PropertyVisitor(false) {}
virtual void apply(osgVolume::VolumeSettings& vs)
{
_vsList.push_back(&vs);
}
typedef std::vector< osg::ref_ptr<osgVolume::VolumeSettings> > VolumeSettingsList;
VolumeSettingsList _vsList;
};
struct VolumeSettingsCallback : public osgGA::GUIEventHandler
{
VolumeSettingsCallback():
_saveKey(19), // Ctril-S
_editKey(05) // Ctrl-E
{
}
int _saveKey;
int _editKey;
bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa, osg::Object* object, osg::NodeVisitor* nv)
{
if (ea.getHandled()) return false;
osgVolume::VolumeTile* tile = dynamic_cast<osgVolume::VolumeTile*>(object);
if (!tile)
{
OSG_NOTICE<<"Warning: VolumeSettingsCallback assigned to a node other than VolumeTile, cannot operate edit/save."<<std::endl;
return false;
}
if (ea.getEventType()==osgGA::GUIEventAdapter::KEYUP)
{
if (ea.getKey()==_saveKey)
{
CollectVolumeSettingsVisitor cvsv;
tile->getLayer()->getProperty()->accept(cvsv);
for(CollectVolumeSettingsVisitor::VolumeSettingsList::iterator itr = cvsv._vsList.begin();
itr != cvsv._vsList.end();
++itr)
{
osgVolume::VolumeSettings* vs = itr->get();
std::string filename = vs->getName();
if (!filename.empty())
{
OSG_NOTICE<<"Save VolumeSettings "<<vs<<" to filename "<<filename<<std::endl;
osgDB::writeObjectFile(*vs, filename);
}
else
{
OSG_NOTICE<<"VolumeSettings "<<vs<<" with blank filename, saving to 'no_filename_vs.osgt'"<<std::endl;
osgDB::writeObjectFile(*vs, "no_filename_vs.osgt");
}
}
return true;
}
if (ea.getKey()==_editKey)
{
OSG_NOTICE<<"Need to edit VolumeSettings "<<std::endl;
return true;
}
}
return false;
}
};
void SlideShowConstructor::setUpVolumeScalarProperty(osgVolume::VolumeTile* tile, osgVolume::ScalarProperty* property, const std::string& source)
{
if (!source.empty())
@ -2726,17 +2802,33 @@ void SlideShowConstructor::addVolume(const std::string& filename, const Position
tile->setLayer(layer.get());
osg::ref_ptr<osgVolume::CompositeProperty> groupPropetry = new osgVolume::CompositeProperty;
osg::ref_ptr<osgVolume::SwitchProperty> sp = new osgVolume::SwitchProperty;
sp->setActiveProperty(0);
groupPropetry->addProperty(sp.get());
osg::ref_ptr<osgVolume::VolumeSettings> vs = volumeData.volumeSettings;
osg::ref_ptr<osgVolume::AlphaFuncProperty> ap = new osgVolume::AlphaFuncProperty(0.1f);
osg::ref_ptr<osgVolume::AlphaFuncProperty> ap = vs.valid() ? vs->getCutoffProperty() : new osgVolume::AlphaFuncProperty(0.1f);
setUpVolumeScalarProperty(tile.get(), ap.get(), volumeData.cutoffValue);
osg::ref_ptr<osgVolume::TransparencyProperty> tp = new osgVolume::TransparencyProperty(1.0f);
osg::ref_ptr<osgVolume::TransparencyProperty> tp = vs.valid() ? vs->getTransparencyProperty() : new osgVolume::TransparencyProperty(1.0f);
setUpVolumeScalarProperty(tile.get(), tp.get(), volumeData.alphaValue);
osg::ref_ptr<osgVolume::SampleRatioProperty> sr = vs.valid() ? vs->getSampleRatioProperty() : new osgVolume::SampleRatioProperty(1.0);
setUpVolumeScalarProperty(tile.get(), sr.get(), volumeData.sampleRatioValue);
osg::ref_ptr<osgVolume::SampleRatioWhenMovingProperty> srm = vs.valid() ? vs->getSampleRatioWhenMovingProperty() : 0;
if (!volumeData.sampleRatioWhenMovingValue.empty())
{
srm = new osgVolume::SampleRatioWhenMovingProperty(0.5);
setUpVolumeScalarProperty(tile.get(), srm.get(), volumeData.sampleRatioWhenMovingValue);
}
// part of hull implementation.
osg::ref_ptr<osgVolume::ExteriorTransparencyFactorProperty> etfp;
if (!volumeData.exteriorTransparencyFactorValue.empty())
{
@ -2744,6 +2836,7 @@ void SlideShowConstructor::addVolume(const std::string& filename, const Position
setUpVolumeScalarProperty(tile.get(), etfp.get(), volumeData.exteriorTransparencyFactorValue);
}
// deprecated, used by old RayTracedTechnique
osg::ref_ptr<osgVolume::SampleDensityProperty> sd = new osgVolume::SampleDensityProperty(0.005);
setUpVolumeScalarProperty(tile.get(), sd.get(), volumeData.sampleDensityValue);
@ -2754,30 +2847,67 @@ void SlideShowConstructor::addVolume(const std::string& filename, const Position
setUpVolumeScalarProperty(tile.get(), sdm.get(), volumeData.sampleDensityWhenMovingValue);
}
osg::ref_ptr<osgVolume::SampleRatioProperty> sr = new osgVolume::SampleRatioProperty(1.0);
setUpVolumeScalarProperty(tile.get(), sr.get(), volumeData.sampleRatioValue);
osg::ref_ptr<osgVolume::SampleRatioWhenMovingProperty> srm;
if (!volumeData.sampleRatioWhenMovingValue.empty())
{
srm = new osgVolume::SampleRatioWhenMovingProperty(0.5);
setUpVolumeScalarProperty(tile.get(), srm.get(), volumeData.sampleRatioWhenMovingValue);
}
osg::ref_ptr<osgVolume::TransferFunctionProperty> tfp = volumeData.transferFunction.valid() ? new osgVolume::TransferFunctionProperty(volumeData.transferFunction.get()) : 0;
if (volumeData.volumeSettings.valid())
{
groupPropetry->addProperty(volumeData.volumeSettings.get());
}
else
{
if (ap.valid()) groupPropetry->addProperty(ap.get());
if (tp.valid()) groupPropetry->addProperty(tp.get());
if (sr.valid()) groupPropetry->addProperty(sr.get());
if (srm.valid()) groupPropetry->addProperty(srm.get());
}
if (sd.valid()) groupPropetry->addProperty(sd.get());
if (sdm.valid()) groupPropetry->addProperty(sdm.get());
if (tfp.valid()) groupPropetry->addProperty(tfp.get());
if (etfp.valid()) groupPropetry->addProperty(etfp.get());
#if 1
{
// Standard
osgVolume::CompositeProperty* cp = new osgVolume::CompositeProperty;
sp->addProperty(cp);
}
{
// Light
sp->addProperty(new osgVolume::LightingProperty);
}
{
// Isosurface
osgVolume::IsoSurfaceProperty* isp = new osgVolume::IsoSurfaceProperty(0.1);
setUpVolumeScalarProperty(tile.get(), isp, volumeData.alphaValue);
sp->addProperty(isp);
}
{
// MaximumIntensityProjection
sp->addProperty(new osgVolume::MaximumIntensityProjectionProperty);
}
switch(volumeData.shadingModel)
{
case(osgVolume::VolumeSettings::Standard): sp->setActiveProperty(0); break;
case(osgVolume::VolumeSettings::Light): sp->setActiveProperty(1); break;
case(osgVolume::VolumeSettings::Isosurface): sp->setActiveProperty(2); break;
case(osgVolume::VolumeSettings::MaximumIntensityProjection): sp->setActiveProperty(3); break;
}
#else
{
// Standard
osgVolume::CompositeProperty* cp = new osgVolume::CompositeProperty;
cp->addProperty(ap.get());
cp->addProperty(tp.get());
if (sd.valid()) cp->addProperty(sd.get());
if (sdm.valid()) cp->addProperty(sdm.get());
if (sr.valid()) cp->addProperty(sr.get());
if (srm.valid()) cp->addProperty(srm.get());
if (tfp.valid()) cp->addProperty(tfp.get());
if (etfp.valid()) cp->addProperty(etfp.get());
sp->addProperty(cp);
}
@ -2841,9 +2971,13 @@ void SlideShowConstructor::addVolume(const std::string& filename, const Position
case(osgVolume::VolumeSettings::Isosurface): sp->setActiveProperty(2); break;
case(osgVolume::VolumeSettings::MaximumIntensityProjection): sp->setActiveProperty(3); break;
}
#endif
#if 1
layer->addProperty(groupPropetry.get());
#else
layer->addProperty(sp.get());
#endif
switch(volumeData.technique)
{
case(osgVolume::VolumeSettings::FixedFunction):
@ -2914,6 +3048,8 @@ void SlideShowConstructor::addVolume(const std::string& filename, const Position
model = group.get();
}
tile->addEventCallback(new VolumeSettingsCallback());
ModelData modelData;
addModel(model.get(), positionData, modelData, scriptData);
}

View File

@ -14,9 +14,11 @@
#include <osgVolume/Property>
#include <osgVolume/VolumeTile>
#include <osgVolume/RayTracedTechnique>
#include <osgVolume/VolumeSettings>
using namespace osgVolume;
Property::Property()
{
}
@ -261,31 +263,22 @@ PropertyVisitor::PropertyVisitor(bool traverseOnlyActiveChildren):
{
}
void PropertyVisitor::apply(CompositeProperty& cp)
{
for(unsigned int i=0; i<cp.getNumProperties(); ++i)
{
cp.getProperty(i)->accept(*this);
}
}
void PropertyVisitor::apply(SwitchProperty& sp)
{
if (_traverseOnlyActiveChildren)
{
if (sp.getActiveProperty()>=0 && sp.getActiveProperty()<static_cast<int>(sp.getNumProperties()))
{
sp.getProperty(sp.getActiveProperty())->accept(*this);
}
}
else
{
for(unsigned int i=0; i<sp.getNumProperties(); ++i)
{
sp.getProperty(i)->accept(*this);
}
}
}
void PropertyVisitor::apply(Property& p) { p.traverse(*this); }
void PropertyVisitor::apply(CompositeProperty& p) { apply(static_cast<Property&>(p)); }
void PropertyVisitor::apply(SwitchProperty& p) { apply(static_cast<Property&>(p)); }
void PropertyVisitor::apply(TransferFunctionProperty& p) { apply(static_cast<Property&>(p)); }
void PropertyVisitor::apply(ScalarProperty& p) { apply(static_cast<Property&>(p)); }
void PropertyVisitor::apply(IsoSurfaceProperty& p) { apply(static_cast<Property&>(p)); }
void PropertyVisitor::apply(AlphaFuncProperty& p) { apply(static_cast<Property&>(p)); }
void PropertyVisitor::apply(MaximumIntensityProjectionProperty& p) { apply(static_cast<Property&>(p)); }
void PropertyVisitor::apply(LightingProperty& p) { apply(static_cast<Property&>(p)); }
void PropertyVisitor::apply(SampleRatioProperty& p) { apply(static_cast<Property&>(p)); }
void PropertyVisitor::apply(SampleRatioWhenMovingProperty& p) { apply(static_cast<Property&>(p)); }
void PropertyVisitor::apply(SampleDensityProperty& p) { apply(static_cast<Property&>(p)); }
void PropertyVisitor::apply(SampleDensityWhenMovingProperty& p) { apply(static_cast<Property&>(p)); }
void PropertyVisitor::apply(TransparencyProperty& p) { apply(static_cast<Property&>(p)); }
void PropertyVisitor::apply(ExteriorTransparencyFactorProperty& p) { apply(static_cast<Property&>(p)); }
void PropertyVisitor::apply(VolumeSettings& p) { apply(static_cast<Property&>(p)); }
/////////////////////////////////////////////////////////////////////////////
@ -297,7 +290,6 @@ CollectPropertiesVisitor::CollectPropertiesVisitor(bool traverseOnlyActiveChildr
{
}
void CollectPropertiesVisitor::apply(Property&) {}
void CollectPropertiesVisitor::apply(TransferFunctionProperty& tf) { _tfProperty = &tf; }
void CollectPropertiesVisitor::apply(ScalarProperty&) {}
void CollectPropertiesVisitor::apply(IsoSurfaceProperty& iso) { _isoProperty = &iso; }
@ -311,7 +303,6 @@ void CollectPropertiesVisitor::apply(SampleRatioWhenMovingProperty& srp) { _samp
void CollectPropertiesVisitor::apply(TransparencyProperty& tp) { _transparencyProperty = &tp; }
void CollectPropertiesVisitor::apply(ExteriorTransparencyFactorProperty& etfp) { _exteriorTransparencyFactorProperty = &etfp; }
class CycleSwitchVisitor : public osgVolume::PropertyVisitor
{
public:

View File

@ -37,6 +37,11 @@ VolumeSettings::VolumeSettings(const VolumeSettings& vs,const osg::CopyOp& copyo
}
void VolumeSettings::accept(PropertyVisitor& pv)
{
pv.apply(*this);
}
void VolumeSettings::traverse(PropertyVisitor& pv)
{
_sampleRatioProperty->accept(pv);
_sampleRatioWhenMovingProperty->accept(pv);