| 1 | /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2009 Robert Osfield |
|---|
| 2 | * |
|---|
| 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. |
|---|
| 7 | * |
|---|
| 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. |
|---|
| 12 | */ |
|---|
| 13 | |
|---|
| 14 | #ifndef OSGVOLUME_LOCATOR |
|---|
| 15 | #define OSGVOLUME_LOCATOR 1 |
|---|
| 16 | |
|---|
| 17 | #include <osgVolume/Export> |
|---|
| 18 | |
|---|
| 19 | #include <osg/Object> |
|---|
| 20 | #include <osg/Matrixd> |
|---|
| 21 | |
|---|
| 22 | #include <vector> |
|---|
| 23 | |
|---|
| 24 | namespace osgVolume { |
|---|
| 25 | |
|---|
| 26 | class OSGVOLUME_EXPORT Locator : public osg::Object |
|---|
| 27 | { |
|---|
| 28 | public: |
|---|
| 29 | |
|---|
| 30 | Locator() {} |
|---|
| 31 | |
|---|
| 32 | Locator(const osg::Matrixd& transform) { setTransform(transform); } |
|---|
| 33 | |
|---|
| 34 | /** Copy constructor using CopyOp to manage deep vs shallow copy.*/ |
|---|
| 35 | Locator(const Locator& locator,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY): |
|---|
| 36 | osg::Object(locator, copyop), |
|---|
| 37 | _transform(locator._transform) {} |
|---|
| 38 | |
|---|
| 39 | META_Object(osgVolume, Locator); |
|---|
| 40 | |
|---|
| 41 | /** Set the transformation from local coordinates to model coordinates.*/ |
|---|
| 42 | void setTransform(const osg::Matrixd& transform) { _transform = transform; _inverse.invert(_transform); locatorModified(); } |
|---|
| 43 | |
|---|
| 44 | /** Set the transformation from local coordinates to model coordinates.*/ |
|---|
| 45 | const osg::Matrixd& getTransform() const { return _transform; } |
|---|
| 46 | |
|---|
| 47 | /** Set the extents of the local coords.*/ |
|---|
| 48 | void setTransformAsExtents(double minX, double minY, double maxX, double maxY, double minZ, double maxZ); |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | virtual bool convertLocalToModel(const osg::Vec3d& /*local*/, osg::Vec3d& /*world*/) const; |
|---|
| 52 | |
|---|
| 53 | virtual bool convertModelToLocal(const osg::Vec3d& /*world*/, osg::Vec3d& /*local*/) const; |
|---|
| 54 | |
|---|
| 55 | static bool convertLocalCoordBetween(const Locator& source, const osg::Vec3d& sourceNDC, |
|---|
| 56 | const Locator& destination, osg::Vec3d& destinationNDC) |
|---|
| 57 | { |
|---|
| 58 | osg::Vec3d model; |
|---|
| 59 | if (!source.convertLocalToModel(sourceNDC, model)) return false; |
|---|
| 60 | if (!destination.convertModelToLocal(model, destinationNDC)) return false; |
|---|
| 61 | return true; |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | bool computeLocalBounds(osg::Vec3d& bottomLeft, osg::Vec3d& topRight) const; |
|---|
| 65 | bool computeLocalBounds(Locator& source, osg::Vec3d& bottomLeft, osg::Vec3d& topRight) const; |
|---|
| 66 | |
|---|
| 67 | |
|---|
| 68 | /** Callback interface for enabling the monitoring of changes to the Locator.*/ |
|---|
| 69 | class LocatorCallback : virtual public osg::Object |
|---|
| 70 | { |
|---|
| 71 | public: |
|---|
| 72 | LocatorCallback() {} |
|---|
| 73 | LocatorCallback(const LocatorCallback& rhs, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY): osg::Object(rhs,copyop) {} |
|---|
| 74 | META_Object(osgVolume, LocatorCallback); |
|---|
| 75 | |
|---|
| 76 | virtual void locatorModified(Locator* locator) {}; |
|---|
| 77 | |
|---|
| 78 | protected: |
|---|
| 79 | virtual ~LocatorCallback() {} |
|---|
| 80 | }; |
|---|
| 81 | |
|---|
| 82 | void addCallback(LocatorCallback* callback); |
|---|
| 83 | void removeCallback(LocatorCallback* callback); |
|---|
| 84 | |
|---|
| 85 | typedef std::vector< osg::ref_ptr<LocatorCallback> > LocatorCallbacks; |
|---|
| 86 | LocatorCallbacks& getLocatorCallbacks() { return _locatorCallbacks; } |
|---|
| 87 | const LocatorCallbacks& getLocatorCallbacks() const { return _locatorCallbacks; } |
|---|
| 88 | |
|---|
| 89 | protected: |
|---|
| 90 | |
|---|
| 91 | void locatorModified(); |
|---|
| 92 | osg::Matrixd _transform; |
|---|
| 93 | osg::Matrixd _inverse; |
|---|
| 94 | |
|---|
| 95 | LocatorCallbacks _locatorCallbacks; |
|---|
| 96 | }; |
|---|
| 97 | |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | #endif |
|---|