| 1 | /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2004 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 OSG_LINESEGMENT |
|---|
| 15 | #define OSG_LINESEGMENT 1 |
|---|
| 16 | |
|---|
| 17 | #include <osg/Matrix> |
|---|
| 18 | #include <osg/BoundingBox> |
|---|
| 19 | #include <osg/BoundingSphere> |
|---|
| 20 | |
|---|
| 21 | namespace osg { |
|---|
| 22 | |
|---|
| 23 | /** LineSegment class for representing a line segment. */ |
|---|
| 24 | class OSG_EXPORT LineSegment : public Referenced |
|---|
| 25 | { |
|---|
| 26 | public: |
|---|
| 27 | |
|---|
| 28 | typedef Vec3d vec_type; |
|---|
| 29 | typedef vec_type::value_type value_type; |
|---|
| 30 | |
|---|
| 31 | LineSegment() {}; |
|---|
| 32 | LineSegment(const LineSegment& seg) : Referenced(),_s(seg._s),_e(seg._e) {} |
|---|
| 33 | LineSegment(const vec_type& s,const vec_type& e) : _s(s),_e(e) {} |
|---|
| 34 | |
|---|
| 35 | LineSegment& operator = (const LineSegment& seg) { _s = seg._s; _e = seg._e; return *this; } |
|---|
| 36 | |
|---|
| 37 | inline void set(const vec_type& s,const vec_type& e) { _s=s; _e=e; } |
|---|
| 38 | |
|---|
| 39 | inline vec_type& start() { return _s; } |
|---|
| 40 | inline const vec_type& start() const { return _s; } |
|---|
| 41 | |
|---|
| 42 | inline vec_type& end() { return _e; } |
|---|
| 43 | inline const vec_type& end() const { return _e; } |
|---|
| 44 | |
|---|
| 45 | inline bool valid() const { return _s.valid() && _e.valid() && _s!=_e; } |
|---|
| 46 | |
|---|
| 47 | /** return true if segment intersects BoundingBox. */ |
|---|
| 48 | bool intersect(const BoundingBox& bb) const; |
|---|
| 49 | |
|---|
| 50 | /** return true if segment intersects BoundingBox |
|---|
| 51 | * and return the intersection ratios. |
|---|
| 52 | */ |
|---|
| 53 | bool intersect(const BoundingBox& bb,float& r1,float& r2) const; |
|---|
| 54 | |
|---|
| 55 | /** return true if segment intersects BoundingBox |
|---|
| 56 | * and return the intersection ratios. |
|---|
| 57 | */ |
|---|
| 58 | bool intersect(const BoundingBox& bb,double& r1,double& r2) const; |
|---|
| 59 | |
|---|
| 60 | /** return true if segment intersects BoundingSphere. */ |
|---|
| 61 | bool intersect(const BoundingSphere& bs) const; |
|---|
| 62 | |
|---|
| 63 | /** return true if segment intersects BoundingSphere and return the |
|---|
| 64 | * intersection ratio. |
|---|
| 65 | */ |
|---|
| 66 | bool intersect(const BoundingSphere& bs,float& r1,float& r2) const; |
|---|
| 67 | |
|---|
| 68 | /** return true if segment intersects BoundingSphere and return the |
|---|
| 69 | * intersection ratio. |
|---|
| 70 | */ |
|---|
| 71 | bool intersect(const BoundingSphere& bs,double& r1,double& r2) const; |
|---|
| 72 | |
|---|
| 73 | /** return true if segment intersects triangle |
|---|
| 74 | * and set ratio long segment. |
|---|
| 75 | */ |
|---|
| 76 | bool intersect(const Vec3f& v1,const Vec3f& v2,const Vec3f& v3,float& r); |
|---|
| 77 | |
|---|
| 78 | /** return true if segment intersects triangle |
|---|
| 79 | * and set ratio long segment. |
|---|
| 80 | */ |
|---|
| 81 | bool intersect(const Vec3d& v1,const Vec3d& v2,const Vec3d& v3,double& r); |
|---|
| 82 | |
|---|
| 83 | |
|---|
| 84 | /** post multiply a segment by matrix.*/ |
|---|
| 85 | inline void mult(const LineSegment& seg,const Matrix& m) { _s = seg._s*m; _e = seg._e*m; } |
|---|
| 86 | /** pre multiply a segment by matrix.*/ |
|---|
| 87 | inline void mult(const Matrix& m,const LineSegment& seg) { _s = m*seg._s; _e = m*seg._e; } |
|---|
| 88 | |
|---|
| 89 | protected: |
|---|
| 90 | |
|---|
| 91 | virtual ~LineSegment(); |
|---|
| 92 | |
|---|
| 93 | static bool intersectAndClip(vec_type& s,vec_type& e,const BoundingBox& bb); |
|---|
| 94 | |
|---|
| 95 | vec_type _s; |
|---|
| 96 | vec_type _e; |
|---|
| 97 | }; |
|---|
| 98 | |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | #endif |
|---|