1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2009 Robert Osfield
3 * This library is open source and may be redistributed and/or modified under
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version. The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * OpenSceneGraph Public License for more details.
14#ifndef OSGVOLUME_LOCATOR
15#define OSGVOLUME_LOCATOR 1
17#include <osgVolume/Export>
20#include <osg/observer_ptr>
23#include <osg/MatrixTransform>
29class OSGVOLUME_EXPORT Locator : public osg::Object
35 Locator(const osg::Matrixd& transform) { setTransform(transform); }
37 /** Copy constructor using CopyOp to manage deep vs shallow copy.*/
38 Locator(const Locator& locator,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY):
39 osg::Object(locator, copyop),
40 _transform(locator._transform) {}
42 META_Object(osgVolume, Locator);
44 /** Set the transformation from local coordinates to model coordinates.*/
45 void setTransform(const osg::Matrixd& transform) { _transform = transform; _inverse.invert(_transform); locatorModified(); }
47 /** Set the transformation from local coordinates to model coordinates.*/
48 const osg::Matrixd& getTransform() const { return _transform; }
50 /** Set the extents of the local coords.*/
51 void setTransformAsExtents(double minX, double minY, double maxX, double maxY, double minZ, double maxZ);
54 virtual bool convertLocalToModel(const osg::Vec3d& /*local*/, osg::Vec3d& /*world*/) const;
56 virtual bool convertModelToLocal(const osg::Vec3d& /*world*/, osg::Vec3d& /*local*/) const;
58 static bool convertLocalCoordBetween(const Locator& source, const osg::Vec3d& sourceNDC,
59 const Locator& destination, osg::Vec3d& destinationNDC)
62 if (!source.convertLocalToModel(sourceNDC, model)) return false;
63 if (!destination.convertModelToLocal(model, destinationNDC)) return false;
67 bool computeLocalBounds(osg::Vec3d& bottomLeft, osg::Vec3d& topRight) const;
68 bool computeLocalBounds(Locator& source, osg::Vec3d& bottomLeft, osg::Vec3d& topRight) const;
70 /** Return true if the axis of the Locator are inverted requiring the faces of any cubes used from rendering to be flipped to ensure the correct front/back face is used.*/
71 bool inverted() const;
73 /** apply the appropriate FrontFace setting to provided StateSet to ensure that the rendering of hull of the volume is the correct orientation.*/
74 void applyAppropriateFrontFace(osg::StateSet* ss) const;
77 /** Callback interface for enabling the monitoring of changes to the Locator.*/
78 class LocatorCallback : virtual public osg::Object
82 LocatorCallback(const LocatorCallback& rhs, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY): osg::Object(rhs,copyop) {}
83 META_Object(osgVolume, LocatorCallback);
85 virtual void locatorModified(Locator* /*locator*/) {};
88 virtual ~LocatorCallback() {}
91 void addCallback(LocatorCallback* callback);
92 template<class T> void addCallback(const osg::ref_ptr<T>& callback) { addCallback(callback.get()); }
95 void removeCallback(LocatorCallback* callback);
97 typedef std::vector< osg::ref_ptr<LocatorCallback> > LocatorCallbacks;
98 LocatorCallbacks& getLocatorCallbacks() { return _locatorCallbacks; }
99 const LocatorCallbacks& getLocatorCallbacks() const { return _locatorCallbacks; }
103 void locatorModified();
104 osg::Matrixd _transform;
105 osg::Matrixd _inverse;
107 LocatorCallbacks _locatorCallbacks;
110class OSGVOLUME_EXPORT TransformLocatorCallback : public Locator::LocatorCallback
114 TransformLocatorCallback(osg::MatrixTransform* transform);
116 void locatorModified(Locator* locator);
120 osg::observer_ptr<osg::MatrixTransform> _transform;
124class OSGVOLUME_EXPORT TexGenLocatorCallback : public Locator::LocatorCallback
128 TexGenLocatorCallback(osg::TexGen* texgen, Locator* geometryLocator, Locator* imageLocator);
130 void locatorModified(Locator*);
134 osg::observer_ptr<osg::TexGen> _texgen;
135 osg::observer_ptr<osgVolume::Locator> _geometryLocator;
136 osg::observer_ptr<osgVolume::Locator> _imageLocator;