openscenegraph
osgVolume/Locator
Go to the documentation of this file.
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/observer_ptr>
21#include <osg/Matrixd>
22#include <osg/TexGen>
23#include <osg/MatrixTransform>
24
25#include <vector>
26
27namespace osgVolume {
28
29class OSGVOLUME_EXPORT Locator : public osg::Object
30{
31 public:
32
33 Locator() {}
34
35 Locator(const osg::Matrixd& transform) { setTransform(transform); }
36
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) {}
41
42 META_Object(osgVolume, Locator);
43
44 /** Set the transformation from local coordinates to model coordinates.*/
45 void setTransform(const osg::Matrixd& transform) { _transform = transform; _inverse.invert(_transform); locatorModified(); }
46
47 /** Set the transformation from local coordinates to model coordinates.*/
48 const osg::Matrixd& getTransform() const { return _transform; }
49
50 /** Set the extents of the local coords.*/
51 void setTransformAsExtents(double minX, double minY, double maxX, double maxY, double minZ, double maxZ);
52
53
54 virtual bool convertLocalToModel(const osg::Vec3d& /*local*/, osg::Vec3d& /*world*/) const;
55
56 virtual bool convertModelToLocal(const osg::Vec3d& /*world*/, osg::Vec3d& /*local*/) const;
57
58 static bool convertLocalCoordBetween(const Locator& source, const osg::Vec3d& sourceNDC,
59 const Locator& destination, osg::Vec3d& destinationNDC)
60 {
61 osg::Vec3d model;
62 if (!source.convertLocalToModel(sourceNDC, model)) return false;
63 if (!destination.convertModelToLocal(model, destinationNDC)) return false;
64 return true;
65 }
66
67 bool computeLocalBounds(osg::Vec3d& bottomLeft, osg::Vec3d& topRight) const;
68 bool computeLocalBounds(Locator& source, osg::Vec3d& bottomLeft, osg::Vec3d& topRight) const;
69
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;
72
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;
75
76
77 /** Callback interface for enabling the monitoring of changes to the Locator.*/
78 class LocatorCallback : virtual public osg::Object
79 {
80 public:
81 LocatorCallback() {}
82 LocatorCallback(const LocatorCallback& rhs, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY): osg::Object(rhs,copyop) {}
83 META_Object(osgVolume, LocatorCallback);
84
85 virtual void locatorModified(Locator* /*locator*/) {};
86
87 protected:
88 virtual ~LocatorCallback() {}
89 };
90
91 void addCallback(LocatorCallback* callback);
92 template<class T> void addCallback(const osg::ref_ptr<T>& callback) { addCallback(callback.get()); }
93
94
95 void removeCallback(LocatorCallback* callback);
96
97 typedef std::vector< osg::ref_ptr<LocatorCallback> > LocatorCallbacks;
98 LocatorCallbacks& getLocatorCallbacks() { return _locatorCallbacks; }
99 const LocatorCallbacks& getLocatorCallbacks() const { return _locatorCallbacks; }
100
101 protected:
102
103 void locatorModified();
104 osg::Matrixd _transform;
105 osg::Matrixd _inverse;
106
107 LocatorCallbacks _locatorCallbacks;
108};
109
110class OSGVOLUME_EXPORT TransformLocatorCallback : public Locator::LocatorCallback
111{
112 public:
113
114 TransformLocatorCallback(osg::MatrixTransform* transform);
115
116 void locatorModified(Locator* locator);
117
118 protected:
119
120 osg::observer_ptr<osg::MatrixTransform> _transform;
121};
122
123
124class OSGVOLUME_EXPORT TexGenLocatorCallback : public Locator::LocatorCallback
125{
126 public:
127
128 TexGenLocatorCallback(osg::TexGen* texgen, Locator* geometryLocator, Locator* imageLocator);
129
130 void locatorModified(Locator*);
131
132 protected:
133
134 osg::observer_ptr<osg::TexGen> _texgen;
135 osg::observer_ptr<osgVolume::Locator> _geometryLocator;
136 osg::observer_ptr<osgVolume::Locator> _imageLocator;
137};
138
139
140}
141
142#endif