LineSegment 3.47 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
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2004 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.
*/

#ifndef OSG_LINESEGMENT
#define OSG_LINESEGMENT 1

#include <osg/Matrix>
#include <osg/BoundingBox>
#include <osg/BoundingSphere>

namespace osg {

/** LineSegment class for representing a line segment. */
class OSG_EXPORT LineSegment : public Referenced
{
    public:

        typedef Vec3d vec_type;
        typedef vec_type::value_type value_type;

        LineSegment() {};
        LineSegment(const LineSegment& seg) : Referenced(),_s(seg._s),_e(seg._e) {}
        LineSegment(const vec_type& s,const vec_type& e) : _s(s),_e(e) {}

        LineSegment& operator = (const LineSegment& seg) { _s = seg._s;  _e = seg._e; return *this; }

        inline void set(const vec_type& s,const vec_type& e) { _s=s; _e=e; }
        
        inline vec_type& start() { return _s; }
        inline const vec_type& start() const { return _s; }

        inline vec_type& end() { return _e; }
        inline const vec_type& end() const { return _e; }

        inline bool valid() const { return _s.valid() && _e.valid() && _s!=_e; }

        /** return true if segment intersects BoundingBox. */
        bool intersect(const BoundingBox& bb) const;

        /** return true if segment intersects BoundingBox
          * and return the intersection ratios.
        */
        bool intersect(const BoundingBox& bb,float& r1,float& r2) const;

        /** return true if segment intersects BoundingBox
          * and return the intersection ratios.
        */
        bool intersect(const BoundingBox& bb,double& r1,double& r2) const;

        /** return true if segment intersects BoundingSphere. */
        bool intersect(const BoundingSphere& bs) const;

        /** return true if segment intersects BoundingSphere and return the
          * intersection ratio.
        */
        bool intersect(const BoundingSphere& bs,float& r1,float& r2) const;
        
        /** return true if segment intersects BoundingSphere and return the
          * intersection ratio.
        */
        bool intersect(const BoundingSphere& bs,double& r1,double& r2) const;

        /** return true if segment intersects triangle
          * and set ratio long segment.
        */
        bool intersect(const Vec3f& v1,const Vec3f& v2,const Vec3f& v3,float& r);

        /** return true if segment intersects triangle
          * and set ratio long segment.
        */
        bool intersect(const Vec3d& v1,const Vec3d& v2,const Vec3d& v3,double& r);


        /** post multiply a segment by matrix.*/
        inline void mult(const LineSegment& seg,const Matrix& m) { _s = seg._s*m; _e = seg._e*m; }
        /** pre multiply a segment by matrix.*/
        inline void mult(const Matrix& m,const LineSegment& seg) { _s = m*seg._s; _e = m*seg._e; }

    protected:

        virtual ~LineSegment();
        
        static bool intersectAndClip(vec_type& s,vec_type& e,const BoundingBox& bb);

        vec_type _s;
        vec_type _e;
};

}

#endif