Projector 9.25 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
/* -*-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>

#include <osgManipulator/Dragger>

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
         * 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.
         * Returns true on successful projection.
         */
        virtual bool project(const PointerInfo& pi, osg::Vec3d& projectedPoint) const = 0;

        /**
         * 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();

        LineProjector(const osg::LineSegment::vec_type& s, const osg::LineSegment::vec_type& e);

        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(); }

        /**
         * Calculates the object coordinates (projectedPoint) of a window 
         * coordinate (pointToProject) when projected onto the given line. 
         * Returns true on successful projection.
         */
        virtual bool project(const PointerInfo& pi, osg::Vec3d& projectedPoint) const;

    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 
         * coordinate (pointToProject) when projected onto the given plane. 
         * Returns true on successful projection.
         */
        virtual bool project(const PointerInfo& pi, osg::Vec3d& projectedPoint) const;

    protected:

        virtual ~PlaneProjector();

        osg::Plane _plane;
};

/**
 * SphereProjector projects points onto the given sphere.
 */
class OSGMANIPULATOR_EXPORT SphereProjector : public Projector
{
    public:

        SphereProjector();

        SphereProjector(osg::Sphere* sphere);

        inline void setSphere(osg::Sphere* sphere) { _sphere = sphere; }
        inline const osg::Sphere* getSphere() const { return _sphere.get(); }

        /**
         * Calculates the object coordinates (projectedPoint) of a window 
         * coordinate (pointToProject) when projected onto the given sphere. 
         * Returns true on successful projection.
         */
        virtual bool project(const PointerInfo& pi, osg::Vec3d& projectedPoint) const;

        /**
         * Returns true is the point is in front of the cylinder given the eye
         * direction.
         */
        bool isPointInFront(const PointerInfo& pi, const osg::Matrix& localToWorld) const;

        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();

        SpherePlaneProjector(osg::Sphere* sphere);

        /**
         * Calculates the object coordinates (projectedPoint) of a window 
         * coordinate (pointToProject) when projected onto the given sphere. 
         * Returns true on successful projection.
         */
        virtual bool project(const PointerInfo& pi, osg::Vec3d& projectedPoint) const;

        /**
         * Returns true if the previous projection was on the sphere and false 
         * if the projection was on the plane.
         */
        bool isProjectionOnSphere() const { return _onSphere; }

        osg::Quat getRotation(const osg::Vec3d& p1, bool p1OnSphere, 
                              const osg::Vec3d& p2, bool p2OnSphere,
                              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();

        CylinderProjector(osg::Cylinder* cylinder);

        inline void setCylinder(osg::Cylinder* cylinder)
        {
            _cylinder = cylinder;
            _cylinderAxis = osg::Vec3d(0.0,0.0,1.0) * osg::Matrix(cylinder->getRotation());
            _cylinderAxis.normalize();
        }
        inline const osg::Cylinder* getCylinder() const { return _cylinder.get(); }

        /**
         * Calculates the object coordinates (projectedPoint) of a window 
         * coordinate (pointToProject) when projected onto the given plane. 
         * Returns true on successful projection.
         */
        virtual bool project(const PointerInfo& pi, osg::Vec3d& projectedPoint) const;

        
        /**
         * Returns true is the point is in front of the cylinder given the eye 
         * direction.
         */
        bool isPointInFront(const PointerInfo& pi, const osg::Matrix& localToWorld) const;

        void setFront(bool front) { _front = front; }

    protected:

        virtual ~CylinderProjector();

        osg::ref_ptr<osg::Cylinder> _cylinder;
        osg::Vec3d                  _cylinderAxis;
        bool                        _front;
};

/**
 * CylinderPlaneProjector projects points onto the given cylinder.
 */
class OSGMANIPULATOR_EXPORT CylinderPlaneProjector : public CylinderProjector
{
    public:

        CylinderPlaneProjector();

        CylinderPlaneProjector(osg::Cylinder* cylinder);

        /**
         * Calculates the object coordinates (projectedPoint) of a window 
         * coordinate (pointToProject) when projected onto the given plane. 
         * Returns true on successful projection.
         */
        virtual bool project(const PointerInfo& pi, osg::Vec3d& projectedPoint) const;

        /**
         * Returns true if the previous projection was on the cylinder and 
         * false if the projection was on the plane.
         */
        bool isProjectionOnCylinder() const { return _onCylinder; }

        osg::Quat getRotation(const osg::Vec3d& p1, bool p1OnCyl, const osg::Vec3d& p2, bool p2OnCyl) const;

    protected:

        virtual ~CylinderPlaneProjector();

        mutable osg::Plane _plane;
        mutable bool       _onCylinder;
        mutable osg::Vec3d _planeLineStart, _planeLineEnd;
};

}

#endif