2007-02-11 18:33:59 +08:00
|
|
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
|
|
|
|
*
|
|
|
|
* This library is open source and may be redistributed and/or modified under
|
|
|
|
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
|
|
|
|
* (at your option) any later version. The full license is in LICENSE file
|
|
|
|
* included with this distribution, and on the openscenegraph.org website.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* OpenSceneGraph Public License for more details.
|
|
|
|
*/
|
|
|
|
//osgManipulator - Copyright (C) 2007 Fugro-Jason B.V.
|
|
|
|
|
|
|
|
#ifndef OSGMANIPULATOR_PROJECTOR
|
|
|
|
#define OSGMANIPULATOR_PROJECTOR 1
|
|
|
|
|
|
|
|
#include <osgManipulator/Export>
|
|
|
|
|
|
|
|
#include <osg/LineSegment>
|
|
|
|
#include <osgUtil/SceneView>
|
|
|
|
|
2007-02-12 05:12:34 +08:00
|
|
|
#include <osgManipulator/Dragger>
|
|
|
|
|
2007-02-11 18:33:59 +08:00
|
|
|
namespace osgManipulator {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Base class for Projectors. Projectors maps 2D cursor motions to 3D motions.
|
|
|
|
*/
|
|
|
|
class OSGMANIPULATOR_EXPORT Projector : public osg::Referenced
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
Projector();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculates the object/world coordinates (projectedPoint) of a window
|
2008-11-21 19:09:11 +08:00
|
|
|
* coordinate (pointToProject) when projected onto some shape or
|
|
|
|
* geometry (implemented in derived classes). SceneView in used for i
|
|
|
|
* projecting window coordinates into object coordinates and vice versa.
|
2007-02-11 18:33:59 +08:00
|
|
|
* Returns true on successful projection.
|
|
|
|
*/
|
2008-11-21 19:23:21 +08:00
|
|
|
virtual bool project(const PointerInfo& pi, osg::Vec3d& projectedPoint) const = 0;
|
2007-02-11 18:33:59 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the matrix for transforming the projector's local coordinate
|
|
|
|
* system to the world/object coordinate system.
|
|
|
|
*/
|
|
|
|
void setLocalToWorld(const osg::Matrix& localToWorld)
|
|
|
|
{
|
|
|
|
_localToWorld = localToWorld;
|
|
|
|
_worldToLocalDirty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the matrix for transforming the projector's local coordinate
|
|
|
|
* system to the world/object coordinate system.
|
|
|
|
*/
|
|
|
|
inline const osg::Matrix& getLocalToWorld() const { return _localToWorld; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the matrix for transforming the world/object coordinate
|
|
|
|
* system to the command's local coordinate system.
|
|
|
|
*/
|
|
|
|
inline const osg::Matrix& getWorldToLocal() const
|
|
|
|
{
|
|
|
|
if (_worldToLocalDirty)
|
|
|
|
{
|
|
|
|
_worldToLocal.invert(_localToWorld);
|
|
|
|
_worldToLocalDirty = false;
|
|
|
|
}
|
|
|
|
return _worldToLocal;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
virtual ~Projector();
|
|
|
|
|
|
|
|
osg::Matrix _localToWorld;
|
|
|
|
mutable osg::Matrix _worldToLocal;
|
|
|
|
|
|
|
|
mutable bool _worldToLocalDirty;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* LineProjector projects points onto the closest point on the given line.
|
|
|
|
*/
|
|
|
|
class OSGMANIPULATOR_EXPORT LineProjector : public Projector
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
LineProjector();
|
|
|
|
|
2008-02-18 22:51:05 +08:00
|
|
|
LineProjector(const osg::LineSegment::vec_type& s, const osg::LineSegment::vec_type& e);
|
2007-02-11 18:33:59 +08:00
|
|
|
|
2008-02-18 22:51:05 +08:00
|
|
|
inline void setLine(const osg::LineSegment::vec_type& s, const osg::LineSegment::vec_type& e) { _line->start() = s; _line->end() = e; }
|
|
|
|
|
|
|
|
inline const osg::LineSegment::vec_type& getLineStart() const { return _line->start(); }
|
|
|
|
inline osg::LineSegment::vec_type& getLineStart() { return _line->start(); }
|
|
|
|
|
|
|
|
inline const osg::LineSegment::vec_type& getLineEnd() const { return _line->end(); }
|
|
|
|
inline osg::LineSegment::vec_type& getLineEnd() { return _line->end(); }
|
2007-02-11 18:33:59 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculates the object coordinates (projectedPoint) of a window
|
2008-11-21 19:09:11 +08:00
|
|
|
* coordinate (pointToProject) when projected onto the given line.
|
2007-02-11 18:33:59 +08:00
|
|
|
* Returns true on successful projection.
|
|
|
|
*/
|
2008-11-21 19:23:21 +08:00
|
|
|
virtual bool project(const PointerInfo& pi, osg::Vec3d& projectedPoint) const;
|
2007-02-11 18:33:59 +08:00
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
virtual ~LineProjector();
|
|
|
|
|
|
|
|
osg::ref_ptr<osg::LineSegment> _line;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* PlaneProjector projects points onto the given line.
|
|
|
|
*/
|
|
|
|
class OSGMANIPULATOR_EXPORT PlaneProjector : public Projector
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
PlaneProjector();
|
|
|
|
|
|
|
|
PlaneProjector(const osg::Plane& plane);
|
|
|
|
|
|
|
|
inline void setPlane(const osg::Plane& plane) { _plane = plane; }
|
|
|
|
inline const osg::Plane& getPlane() const { return _plane; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculates the object coordinates (projectedPoint) of a window
|
2008-11-21 19:09:11 +08:00
|
|
|
* coordinate (pointToProject) when projected onto the given plane.
|
2007-02-11 18:33:59 +08:00
|
|
|
* Returns true on successful projection.
|
|
|
|
*/
|
2008-11-21 19:23:21 +08:00
|
|
|
virtual bool project(const PointerInfo& pi, osg::Vec3d& projectedPoint) const;
|
2007-02-11 18:33:59 +08:00
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
virtual ~PlaneProjector();
|
|
|
|
|
|
|
|
osg::Plane _plane;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SphereProjector projects points onto the given sphere.
|
|
|
|
*/
|
|
|
|
class OSGMANIPULATOR_EXPORT SphereProjector : public Projector
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
SphereProjector();
|
|
|
|
|
2007-02-11 20:22:58 +08:00
|
|
|
SphereProjector(osg::Sphere* sphere);
|
2007-02-11 18:33:59 +08:00
|
|
|
|
2007-02-11 20:22:58 +08:00
|
|
|
inline void setSphere(osg::Sphere* sphere) { _sphere = sphere; }
|
|
|
|
inline const osg::Sphere* getSphere() const { return _sphere.get(); }
|
2007-02-11 18:33:59 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculates the object coordinates (projectedPoint) of a window
|
2008-11-21 19:09:11 +08:00
|
|
|
* coordinate (pointToProject) when projected onto the given sphere.
|
2007-02-11 18:33:59 +08:00
|
|
|
* Returns true on successful projection.
|
|
|
|
*/
|
2008-11-21 19:23:21 +08:00
|
|
|
virtual bool project(const PointerInfo& pi, osg::Vec3d& projectedPoint) const;
|
2007-02-11 18:33:59 +08:00
|
|
|
|
|
|
|
/**
|
2008-11-21 19:09:11 +08:00
|
|
|
* Returns true is the point is in front of the cylinder given the eye
|
|
|
|
* direction.
|
|
|
|
*/
|
2007-02-26 21:01:17 +08:00
|
|
|
bool isPointInFront(const PointerInfo& pi, const osg::Matrix& localToWorld) const;
|
2007-02-11 18:33:59 +08:00
|
|
|
|
|
|
|
void setFront(bool front) { _front = front; }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
virtual ~SphereProjector();
|
|
|
|
|
|
|
|
osg::ref_ptr<osg::Sphere> _sphere;
|
|
|
|
bool _front;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SpherePlaneProjector projects points onto a sphere, failing which it project
|
|
|
|
* onto a plane oriented to the viewing direction.
|
|
|
|
*/
|
|
|
|
class OSGMANIPULATOR_EXPORT SpherePlaneProjector : public SphereProjector
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
SpherePlaneProjector();
|
|
|
|
|
2007-02-11 20:22:58 +08:00
|
|
|
SpherePlaneProjector(osg::Sphere* sphere);
|
2007-02-11 18:33:59 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculates the object coordinates (projectedPoint) of a window
|
2008-11-21 19:09:11 +08:00
|
|
|
* coordinate (pointToProject) when projected onto the given sphere.
|
2007-02-11 18:33:59 +08:00
|
|
|
* Returns true on successful projection.
|
|
|
|
*/
|
2008-11-21 19:23:21 +08:00
|
|
|
virtual bool project(const PointerInfo& pi, osg::Vec3d& projectedPoint) const;
|
2007-02-11 18:33:59 +08:00
|
|
|
|
|
|
|
/**
|
2008-11-21 19:09:11 +08:00
|
|
|
* Returns true if the previous projection was on the sphere and false
|
|
|
|
* if the projection was on the plane.
|
|
|
|
*/
|
2007-02-11 18:33:59 +08:00
|
|
|
bool isProjectionOnSphere() const { return _onSphere; }
|
|
|
|
|
2008-11-21 19:23:21 +08:00
|
|
|
osg::Quat getRotation(const osg::Vec3d& p1, bool p1OnSphere,
|
|
|
|
const osg::Vec3d& p2, bool p2OnSphere,
|
2007-02-11 18:33:59 +08:00
|
|
|
float radialFactor = 0.0f) const;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
virtual ~SpherePlaneProjector();
|
|
|
|
|
|
|
|
mutable osg::Plane _plane;
|
|
|
|
mutable bool _onSphere;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* CylinderProjector projects points onto the given cylinder.
|
|
|
|
*/
|
|
|
|
class OSGMANIPULATOR_EXPORT CylinderProjector : public Projector
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
CylinderProjector();
|
|
|
|
|
2007-02-11 20:22:58 +08:00
|
|
|
CylinderProjector(osg::Cylinder* cylinder);
|
2007-02-11 18:33:59 +08:00
|
|
|
|
2007-02-11 20:22:58 +08:00
|
|
|
inline void setCylinder(osg::Cylinder* cylinder)
|
2007-02-11 18:33:59 +08:00
|
|
|
{
|
2007-02-11 20:22:58 +08:00
|
|
|
_cylinder = cylinder;
|
2008-11-21 19:23:21 +08:00
|
|
|
_cylinderAxis = osg::Vec3d(0.0,0.0,1.0) * osg::Matrix(cylinder->getRotation());
|
2007-02-11 18:33:59 +08:00
|
|
|
_cylinderAxis.normalize();
|
|
|
|
}
|
2007-02-11 20:22:58 +08:00
|
|
|
inline const osg::Cylinder* getCylinder() const { return _cylinder.get(); }
|
2007-02-11 18:33:59 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculates the object coordinates (projectedPoint) of a window
|
2008-11-21 19:09:11 +08:00
|
|
|
* coordinate (pointToProject) when projected onto the given plane.
|
2007-02-11 18:33:59 +08:00
|
|
|
* Returns true on successful projection.
|
|
|
|
*/
|
2008-11-21 19:23:21 +08:00
|
|
|
virtual bool project(const PointerInfo& pi, osg::Vec3d& projectedPoint) const;
|
2007-02-11 18:33:59 +08:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2008-11-21 19:09:11 +08:00
|
|
|
* Returns true is the point is in front of the cylinder given the eye
|
|
|
|
* direction.
|
|
|
|
*/
|
2007-02-26 21:01:17 +08:00
|
|
|
bool isPointInFront(const PointerInfo& pi, const osg::Matrix& localToWorld) const;
|
2007-02-11 18:33:59 +08:00
|
|
|
|
|
|
|
void setFront(bool front) { _front = front; }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
virtual ~CylinderProjector();
|
|
|
|
|
|
|
|
osg::ref_ptr<osg::Cylinder> _cylinder;
|
2008-11-21 19:23:21 +08:00
|
|
|
osg::Vec3d _cylinderAxis;
|
2007-02-11 18:33:59 +08:00
|
|
|
bool _front;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2012-03-09 18:20:23 +08:00
|
|
|
* CylinderPlaneProjector projects a point onto a plane relative to the
|
|
|
|
* given cylinder. For most cases, the plane will be parallel to the
|
|
|
|
* cylinder axis oriented towards the eyepoint. When the eyepoint and
|
|
|
|
* cylinder axis are close to parallel, then it will project onto a plane
|
|
|
|
* perpendicular to the cylinder.
|
2007-02-11 18:33:59 +08:00
|
|
|
*/
|
|
|
|
class OSGMANIPULATOR_EXPORT CylinderPlaneProjector : public CylinderProjector
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
CylinderPlaneProjector();
|
|
|
|
|
2007-02-11 20:22:58 +08:00
|
|
|
CylinderPlaneProjector(osg::Cylinder* cylinder);
|
2007-02-11 18:33:59 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculates the object coordinates (projectedPoint) of a window
|
2008-11-21 19:09:11 +08:00
|
|
|
* coordinate (pointToProject) when projected onto the given plane.
|
2007-02-11 18:33:59 +08:00
|
|
|
* Returns true on successful projection.
|
2012-03-09 18:20:23 +08:00
|
|
|
* \param[in] pi Incoming intersection information
|
|
|
|
* \param[out] projectedPoint Point located on the given plane
|
|
|
|
* \return bool Whether the projection onto the plane was successful.
|
2007-02-11 18:33:59 +08:00
|
|
|
*/
|
2008-11-21 19:23:21 +08:00
|
|
|
virtual bool project(const PointerInfo& pi, osg::Vec3d& projectedPoint) const;
|
2007-02-11 18:33:59 +08:00
|
|
|
|
|
|
|
/**
|
2012-03-09 18:20:23 +08:00
|
|
|
* Generates a rotation about the cylinder axis based upon the incoming
|
|
|
|
* projected points on the plane computed from project().
|
|
|
|
* \param[in] p1 Initial projection point
|
|
|
|
* \param[in] p2 Second projection point
|
|
|
|
* \return osg::Quat Rotation about cylinder axis
|
2008-11-21 19:09:11 +08:00
|
|
|
*/
|
2012-03-09 18:20:23 +08:00
|
|
|
osg::Quat getRotation(const osg::Vec3d& p1, const osg::Vec3d& p2) const;
|
2007-02-11 18:33:59 +08:00
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
virtual ~CylinderPlaneProjector();
|
|
|
|
|
|
|
|
mutable osg::Plane _plane;
|
2008-11-21 19:23:21 +08:00
|
|
|
mutable osg::Vec3d _planeLineStart, _planeLineEnd;
|
2012-03-06 18:08:49 +08:00
|
|
|
mutable bool _parallelPlane;
|
2007-02-11 18:33:59 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|