| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | #include <osgVolume/Property> |
|---|
| 15 | #include <osgVolume/VolumeTile> |
|---|
| 16 | |
|---|
| 17 | using namespace osgVolume; |
|---|
| 18 | |
|---|
| 19 | Property::Property() |
|---|
| 20 | { |
|---|
| 21 | } |
|---|
| 22 | |
|---|
| 23 | Property::Property(const Property& property,const osg::CopyOp& copyop): |
|---|
| 24 | osg::Object(property,copyop) |
|---|
| 25 | { |
|---|
| 26 | } |
|---|
| 27 | |
|---|
| 28 | Property::~Property() |
|---|
| 29 | { |
|---|
| 30 | } |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | CompositeProperty::CompositeProperty() |
|---|
| 37 | { |
|---|
| 38 | } |
|---|
| 39 | |
|---|
| 40 | CompositeProperty::CompositeProperty(const CompositeProperty& compositeProperty, const osg::CopyOp& copyop): |
|---|
| 41 | Property(compositeProperty,copyop) |
|---|
| 42 | { |
|---|
| 43 | } |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | void CompositeProperty::clear() |
|---|
| 47 | { |
|---|
| 48 | _properties.clear(); |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | SwitchProperty::SwitchProperty() |
|---|
| 56 | { |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | SwitchProperty::SwitchProperty(const SwitchProperty& switchProperty, const osg::CopyOp& copyop): |
|---|
| 60 | CompositeProperty(switchProperty,copyop), |
|---|
| 61 | _activeProperty(switchProperty._activeProperty) |
|---|
| 62 | { |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | TransferFunctionProperty::TransferFunctionProperty(osg::TransferFunction* tf): |
|---|
| 71 | _tf(tf) |
|---|
| 72 | { |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | TransferFunctionProperty::TransferFunctionProperty(const TransferFunctionProperty& tfp, const osg::CopyOp& copyop): |
|---|
| 76 | Property(tfp,copyop), |
|---|
| 77 | _tf(tfp._tf) |
|---|
| 78 | { |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | |
|---|
| 82 | |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | ScalarProperty::ScalarProperty() |
|---|
| 86 | { |
|---|
| 87 | _uniform = new osg::Uniform; |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | ScalarProperty::ScalarProperty(const std::string& scalarName, float value) |
|---|
| 91 | { |
|---|
| 92 | setName(scalarName); |
|---|
| 93 | _uniform = new osg::Uniform(scalarName.c_str(), value); |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | ScalarProperty::ScalarProperty(const ScalarProperty& sp, const osg::CopyOp& copyop): |
|---|
| 97 | Property(sp,copyop) |
|---|
| 98 | { |
|---|
| 99 | _uniform = new osg::Uniform(sp._uniform->getName().c_str(), getValue()); |
|---|
| 100 | |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | |
|---|
| 106 | |
|---|
| 107 | IsoSurfaceProperty::IsoSurfaceProperty(float value): |
|---|
| 108 | ScalarProperty("IsoSurfaceValue",value) |
|---|
| 109 | { |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | IsoSurfaceProperty::IsoSurfaceProperty(const IsoSurfaceProperty& isp,const osg::CopyOp& copyop): |
|---|
| 113 | ScalarProperty(isp, copyop) |
|---|
| 114 | { |
|---|
| 115 | } |
|---|
| 116 | |
|---|
| 117 | |
|---|
| 118 | |
|---|
| 119 | |
|---|
| 120 | |
|---|
| 121 | |
|---|
| 122 | AlphaFuncProperty::AlphaFuncProperty(float value): |
|---|
| 123 | ScalarProperty("AlphaFuncValue",value) |
|---|
| 124 | { |
|---|
| 125 | _alphaFunc = new osg::AlphaFunc(osg::AlphaFunc::GREATER, value); |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | AlphaFuncProperty::AlphaFuncProperty(const AlphaFuncProperty& afp,const osg::CopyOp& copyop): |
|---|
| 129 | ScalarProperty(afp, copyop) |
|---|
| 130 | { |
|---|
| 131 | _alphaFunc = new osg::AlphaFunc(osg::AlphaFunc::GREATER, getValue()); |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | void AlphaFuncProperty::setValue(float v) |
|---|
| 135 | { |
|---|
| 136 | _uniform->set(v); |
|---|
| 137 | _alphaFunc->setReferenceValue(v); |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | |
|---|
| 141 | |
|---|
| 142 | |
|---|
| 143 | |
|---|
| 144 | MaximumIntensityProjectionProperty::MaximumIntensityProjectionProperty() |
|---|
| 145 | { |
|---|
| 146 | } |
|---|
| 147 | |
|---|
| 148 | MaximumIntensityProjectionProperty::MaximumIntensityProjectionProperty(const MaximumIntensityProjectionProperty& isp,const osg::CopyOp& copyop): |
|---|
| 149 | Property(isp, copyop) |
|---|
| 150 | { |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | |
|---|
| 154 | |
|---|
| 155 | |
|---|
| 156 | |
|---|
| 157 | LightingProperty::LightingProperty() |
|---|
| 158 | { |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | LightingProperty::LightingProperty(const LightingProperty& isp,const osg::CopyOp& copyop): |
|---|
| 162 | Property(isp, copyop) |
|---|
| 163 | { |
|---|
| 164 | } |
|---|
| 165 | |
|---|
| 166 | |
|---|
| 167 | |
|---|
| 168 | |
|---|
| 169 | |
|---|
| 170 | |
|---|
| 171 | SampleDensityProperty::SampleDensityProperty(float value): |
|---|
| 172 | ScalarProperty("SampleDensityValue",value) |
|---|
| 173 | { |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | SampleDensityProperty::SampleDensityProperty(const SampleDensityProperty& isp,const osg::CopyOp& copyop): |
|---|
| 177 | ScalarProperty(isp, copyop) |
|---|
| 178 | { |
|---|
| 179 | } |
|---|
| 180 | |
|---|
| 181 | |
|---|
| 182 | |
|---|
| 183 | |
|---|
| 184 | |
|---|
| 185 | |
|---|
| 186 | SampleDensityWhenMovingProperty::SampleDensityWhenMovingProperty(float value): |
|---|
| 187 | ScalarProperty("SampleDensityValue",value) |
|---|
| 188 | { |
|---|
| 189 | } |
|---|
| 190 | |
|---|
| 191 | SampleDensityWhenMovingProperty::SampleDensityWhenMovingProperty(const SampleDensityWhenMovingProperty& isp,const osg::CopyOp& copyop): |
|---|
| 192 | ScalarProperty(isp, copyop) |
|---|
| 193 | { |
|---|
| 194 | } |
|---|
| 195 | |
|---|
| 196 | |
|---|
| 197 | |
|---|
| 198 | |
|---|
| 199 | |
|---|
| 200 | |
|---|
| 201 | TransparencyProperty::TransparencyProperty(float value): |
|---|
| 202 | ScalarProperty("TransparencyValue",value) |
|---|
| 203 | { |
|---|
| 204 | } |
|---|
| 205 | |
|---|
| 206 | TransparencyProperty::TransparencyProperty(const TransparencyProperty& isp,const osg::CopyOp& copyop): |
|---|
| 207 | ScalarProperty(isp, copyop) |
|---|
| 208 | { |
|---|
| 209 | } |
|---|
| 210 | |
|---|
| 211 | |
|---|
| 212 | |
|---|
| 213 | |
|---|
| 214 | |
|---|
| 215 | PropertyVisitor::PropertyVisitor(bool traverseOnlyActiveChildren): |
|---|
| 216 | _traverseOnlyActiveChildren(traverseOnlyActiveChildren) |
|---|
| 217 | { |
|---|
| 218 | } |
|---|
| 219 | |
|---|
| 220 | void PropertyVisitor::apply(CompositeProperty& cp) |
|---|
| 221 | { |
|---|
| 222 | for(unsigned int i=0; i<cp.getNumProperties(); ++i) |
|---|
| 223 | { |
|---|
| 224 | cp.getProperty(i)->accept(*this); |
|---|
| 225 | } |
|---|
| 226 | } |
|---|
| 227 | |
|---|
| 228 | void PropertyVisitor::apply(SwitchProperty& sp) |
|---|
| 229 | { |
|---|
| 230 | if (_traverseOnlyActiveChildren) |
|---|
| 231 | { |
|---|
| 232 | if (sp.getActiveProperty()>=0 && sp.getActiveProperty()<static_cast<int>(sp.getNumProperties())) |
|---|
| 233 | { |
|---|
| 234 | sp.getProperty(sp.getActiveProperty())->accept(*this); |
|---|
| 235 | } |
|---|
| 236 | } |
|---|
| 237 | else |
|---|
| 238 | { |
|---|
| 239 | for(unsigned int i=0; i<sp.getNumProperties(); ++i) |
|---|
| 240 | { |
|---|
| 241 | sp.getProperty(i)->accept(*this); |
|---|
| 242 | } |
|---|
| 243 | } |
|---|
| 244 | } |
|---|
| 245 | |
|---|
| 246 | |
|---|
| 247 | |
|---|
| 248 | |
|---|
| 249 | |
|---|
| 250 | |
|---|
| 251 | CollectPropertiesVisitor::CollectPropertiesVisitor(bool traverseOnlyActiveChildren): |
|---|
| 252 | PropertyVisitor(traverseOnlyActiveChildren) |
|---|
| 253 | { |
|---|
| 254 | } |
|---|
| 255 | |
|---|
| 256 | void CollectPropertiesVisitor::apply(Property&) {} |
|---|
| 257 | void CollectPropertiesVisitor::apply(TransferFunctionProperty& tf) { _tfProperty = &tf; } |
|---|
| 258 | void CollectPropertiesVisitor::apply(ScalarProperty&) {} |
|---|
| 259 | void CollectPropertiesVisitor::apply(IsoSurfaceProperty& iso) { _isoProperty = &iso; } |
|---|
| 260 | void CollectPropertiesVisitor::apply(AlphaFuncProperty& af) { _afProperty = ⁡ } |
|---|
| 261 | void CollectPropertiesVisitor::apply(MaximumIntensityProjectionProperty& mip) { _mipProperty = &mip; } |
|---|
| 262 | void CollectPropertiesVisitor::apply(LightingProperty& lp) { _lightingProperty = &lp; } |
|---|
| 263 | void CollectPropertiesVisitor::apply(SampleDensityProperty& sdp) { _sampleDensityProperty = &sdp; } |
|---|
| 264 | void CollectPropertiesVisitor::apply(SampleDensityWhenMovingProperty& sdp) { _sampleDensityWhenMovingProperty = &sdp; } |
|---|
| 265 | void CollectPropertiesVisitor::apply(TransparencyProperty& tp) { _transparencyProperty = &tp; } |
|---|
| 266 | |
|---|
| 267 | |
|---|
| 268 | class CycleSwitchVisitor : public osgVolume::PropertyVisitor |
|---|
| 269 | { |
|---|
| 270 | public: |
|---|
| 271 | |
|---|
| 272 | CycleSwitchVisitor(int delta): |
|---|
| 273 | PropertyVisitor(false), |
|---|
| 274 | _delta(delta), |
|---|
| 275 | _switchModified(true) {} |
|---|
| 276 | |
|---|
| 277 | virtual void apply(SwitchProperty& sp) |
|---|
| 278 | { |
|---|
| 279 | if (sp.getNumProperties()>=2) |
|---|
| 280 | { |
|---|
| 281 | if (_delta>0) |
|---|
| 282 | { |
|---|
| 283 | int newValue = sp.getActiveProperty()+_delta; |
|---|
| 284 | if (newValue<static_cast<int>(sp.getNumProperties())) |
|---|
| 285 | { |
|---|
| 286 | sp.setActiveProperty(newValue); |
|---|
| 287 | } |
|---|
| 288 | else |
|---|
| 289 | { |
|---|
| 290 | sp.setActiveProperty(0); |
|---|
| 291 | } |
|---|
| 292 | |
|---|
| 293 | _switchModified = true; |
|---|
| 294 | } |
|---|
| 295 | else |
|---|
| 296 | { |
|---|
| 297 | int newValue = sp.getActiveProperty()+_delta; |
|---|
| 298 | if (newValue>=0) |
|---|
| 299 | { |
|---|
| 300 | sp.setActiveProperty(newValue); |
|---|
| 301 | } |
|---|
| 302 | else |
|---|
| 303 | { |
|---|
| 304 | sp.setActiveProperty(sp.getNumProperties()-1); |
|---|
| 305 | } |
|---|
| 306 | |
|---|
| 307 | _switchModified = true; |
|---|
| 308 | } |
|---|
| 309 | } |
|---|
| 310 | |
|---|
| 311 | PropertyVisitor::apply(sp); |
|---|
| 312 | } |
|---|
| 313 | |
|---|
| 314 | int _delta; |
|---|
| 315 | bool _switchModified; |
|---|
| 316 | }; |
|---|
| 317 | |
|---|
| 318 | |
|---|
| 319 | |
|---|
| 320 | |
|---|
| 321 | |
|---|
| 322 | PropertyAdjustmentCallback::PropertyAdjustmentCallback(): |
|---|
| 323 | _cyleForwardKey('v'), |
|---|
| 324 | _cyleBackwardKey('V'), |
|---|
| 325 | _transparencyKey('t'), |
|---|
| 326 | _alphaFuncKey('a'), |
|---|
| 327 | _sampleDensityKey('d'), |
|---|
| 328 | _updateTransparency(false), |
|---|
| 329 | _updateAlphaCutOff(false), |
|---|
| 330 | _updateSampleDensity(false) |
|---|
| 331 | { |
|---|
| 332 | } |
|---|
| 333 | |
|---|
| 334 | PropertyAdjustmentCallback::PropertyAdjustmentCallback(const PropertyAdjustmentCallback& pac,const osg::CopyOp&): |
|---|
| 335 | _cyleForwardKey(pac._cyleForwardKey), |
|---|
| 336 | _cyleBackwardKey(pac._cyleBackwardKey), |
|---|
| 337 | _transparencyKey(pac._transparencyKey), |
|---|
| 338 | _alphaFuncKey(pac._alphaFuncKey), |
|---|
| 339 | _sampleDensityKey(pac._sampleDensityKey), |
|---|
| 340 | _updateTransparency(false), |
|---|
| 341 | _updateAlphaCutOff(false), |
|---|
| 342 | _updateSampleDensity(false) |
|---|
| 343 | { |
|---|
| 344 | } |
|---|
| 345 | |
|---|
| 346 | bool PropertyAdjustmentCallback::handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter&, osg::Object* object, osg::NodeVisitor*) |
|---|
| 347 | { |
|---|
| 348 | osgVolume::VolumeTile* tile = dynamic_cast<osgVolume::VolumeTile*>(object); |
|---|
| 349 | osgVolume::Layer* layer = tile ? tile->getLayer() : 0; |
|---|
| 350 | osgVolume::Property* property = layer ? layer->getProperty() : 0; |
|---|
| 351 | if (!property) return false; |
|---|
| 352 | |
|---|
| 353 | osgVolume::CollectPropertiesVisitor cpv; |
|---|
| 354 | property->accept(cpv); |
|---|
| 355 | |
|---|
| 356 | bool passOnUpdates = false; |
|---|
| 357 | |
|---|
| 358 | switch(ea.getEventType()) |
|---|
| 359 | { |
|---|
| 360 | case(osgGA::GUIEventAdapter::MOVE): |
|---|
| 361 | case(osgGA::GUIEventAdapter::DRAG): |
|---|
| 362 | { |
|---|
| 363 | passOnUpdates = true; |
|---|
| 364 | } |
|---|
| 365 | case(osgGA::GUIEventAdapter::KEYDOWN): |
|---|
| 366 | { |
|---|
| 367 | if (ea.getKey()==_cyleForwardKey || ea.getKey()==_cyleBackwardKey) |
|---|
| 368 | { |
|---|
| 369 | CycleSwitchVisitor csv( (ea.getKey()==_cyleForwardKey) ? 1 : -1); |
|---|
| 370 | property->accept(csv); |
|---|
| 371 | if (csv._switchModified) |
|---|
| 372 | { |
|---|
| 373 | tile->setDirty(true); |
|---|
| 374 | tile->init(); |
|---|
| 375 | } |
|---|
| 376 | } |
|---|
| 377 | else if (ea.getKey()==_transparencyKey) _updateTransparency = passOnUpdates = true; |
|---|
| 378 | else if (ea.getKey()==_alphaFuncKey) _updateAlphaCutOff = passOnUpdates = true; |
|---|
| 379 | else if (ea.getKey()==_sampleDensityKey) _updateSampleDensity = passOnUpdates = true; |
|---|
| 380 | break; |
|---|
| 381 | } |
|---|
| 382 | case(osgGA::GUIEventAdapter::KEYUP): |
|---|
| 383 | { |
|---|
| 384 | if (ea.getKey()==_transparencyKey) _updateTransparency = false; |
|---|
| 385 | else if (ea.getKey()==_alphaFuncKey) _updateAlphaCutOff = false; |
|---|
| 386 | else if (ea.getKey()==_sampleDensityKey) _updateSampleDensity = false; |
|---|
| 387 | break; |
|---|
| 388 | } |
|---|
| 389 | default: |
|---|
| 390 | break; |
|---|
| 391 | } |
|---|
| 392 | |
|---|
| 393 | if (passOnUpdates) |
|---|
| 394 | { |
|---|
| 395 | float v = (ea.getY()-ea.getYmin())/(ea.getYmax()-ea.getYmin()); |
|---|
| 396 | if (ea.getMouseYOrientation()==osgGA::GUIEventAdapter::Y_INCREASING_DOWNWARDS) v = 1.0f-v; |
|---|
| 397 | |
|---|
| 398 | float v2 = v*v; |
|---|
| 399 | float v4 = v2*v2; |
|---|
| 400 | |
|---|
| 401 | if (_updateAlphaCutOff && cpv._isoProperty.valid()) |
|---|
| 402 | { |
|---|
| 403 | OSG_INFO<<"Setting isoProperty to "<<v<<std::endl; |
|---|
| 404 | cpv._isoProperty->setValue(v); |
|---|
| 405 | } |
|---|
| 406 | |
|---|
| 407 | if (_updateAlphaCutOff && cpv._afProperty.valid()) |
|---|
| 408 | { |
|---|
| 409 | OSG_INFO<<"Setting afProperty to "<<v2<<std::endl; |
|---|
| 410 | cpv._afProperty->setValue(v2); |
|---|
| 411 | } |
|---|
| 412 | |
|---|
| 413 | if (_updateTransparency && cpv._transparencyProperty.valid()) |
|---|
| 414 | { |
|---|
| 415 | OSG_INFO<<"Setting transparency to "<<v2<<std::endl; |
|---|
| 416 | cpv._transparencyProperty->setValue(1.0f-v2); |
|---|
| 417 | } |
|---|
| 418 | |
|---|
| 419 | if (_updateSampleDensity && cpv._sampleDensityProperty.valid()) |
|---|
| 420 | { |
|---|
| 421 | OSG_INFO<<"Setting sample density to "<<v4<<std::endl; |
|---|
| 422 | cpv._sampleDensityProperty->setValue(v4); |
|---|
| 423 | } |
|---|
| 424 | if (_updateSampleDensity && cpv._sampleDensityWhenMovingProperty.valid()) |
|---|
| 425 | { |
|---|
| 426 | OSG_INFO<<"Setting sample density when moving to "<<v4<<std::endl; |
|---|
| 427 | cpv._sampleDensityWhenMovingProperty->setValue(v4); |
|---|
| 428 | } |
|---|
| 429 | } |
|---|
| 430 | |
|---|
| 431 | |
|---|
| 432 | return false; |
|---|
| 433 | } |
|---|