LineSegment 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. #ifndef OSG_LINESEGMENT
  14. #define OSG_LINESEGMENT 1
  15. #include <osg/Matrix>
  16. #include <osg/BoundingBox>
  17. #include <osg/BoundingSphere>
  18. namespace osg {
  19. /** LineSegment class for representing a line segment. */
  20. class OSG_EXPORT LineSegment : public Referenced
  21. {
  22. public:
  23. typedef Vec3d vec_type;
  24. typedef vec_type::value_type value_type;
  25. LineSegment() {};
  26. LineSegment(const LineSegment& seg) : Referenced(),_s(seg._s),_e(seg._e) {}
  27. LineSegment(const vec_type& s,const vec_type& e) : _s(s),_e(e) {}
  28. LineSegment& operator = (const LineSegment& seg) { _s = seg._s; _e = seg._e; return *this; }
  29. inline void set(const vec_type& s,const vec_type& e) { _s=s; _e=e; }
  30. inline vec_type& start() { return _s; }
  31. inline const vec_type& start() const { return _s; }
  32. inline vec_type& end() { return _e; }
  33. inline const vec_type& end() const { return _e; }
  34. inline bool valid() const { return _s.valid() && _e.valid() && _s!=_e; }
  35. /** return true if segment intersects BoundingBox. */
  36. bool intersect(const BoundingBox& bb) const;
  37. /** return true if segment intersects BoundingBox and
  38. * set float ratios for the first and second intersections, where the ratio is 0.0 at the segment start point, and 1.0 at the segment end point.
  39. */
  40. bool intersectAndComputeRatios(const BoundingBox& bb, float& ratioFromStartToEnd1, float& ratioFromStartToEnd2) const;
  41. /** return true if segment intersects BoundingBox and
  42. * set double ratios for the first and second intersections, where the ratio is 0.0 at the segment start point, and 1.0 at the segment end point.
  43. */
  44. bool intersectAndComputeRatios(const BoundingBox& bb, double& ratioFromStartToEnd1, double& ratioFromStartToEnd2) const;
  45. /** return true if segment intersects BoundingSphere. */
  46. bool intersect(const BoundingSphere& bs) const;
  47. /** return true if segment intersects BoundingSphere and
  48. * set float ratios for the first and second intersections, where the ratio is 0.0 at the segment start point, and 1.0 at the segment end point.
  49. */
  50. bool intersectAndComputeRatios(const BoundingSphere& bs, float& ratioFromStartToEnd1, float& ratioFromStartToEnd2) const;
  51. /** return true if segment intersects BoundingSphere and
  52. * set double ratios for the first and second intersections, where the ratio is 0.0 at the segment start point, and 1.0 at the segment end point.
  53. */
  54. bool intersectAndComputeRatios(const BoundingSphere& bs,double& ratioFromStartToEnd1, double& ratioFromStartToEnd2) const;
  55. /** return true if segment intersects triangle and
  56. * set float ratios where the ratio is 0.0 at the segment start point, and 1.0 at the segment end point.
  57. */
  58. bool intersect(const Vec3f& v1,const Vec3f& v2,const Vec3f& v3,float& ratioFromStartToEnd);
  59. /** return true if segment intersects triangle and
  60. * set double ratios where the ratio is 0.0 at the segment start point, and 1.0 at the segment end point.
  61. */
  62. bool intersect(const Vec3d& v1,const Vec3d& v2,const Vec3d& v3,double& ratioFromStartToEnd);
  63. /** post multiply a segment by matrix.*/
  64. inline void mult(const LineSegment& seg,const Matrix& m) { _s = seg._s*m; _e = seg._e*m; }
  65. /** pre multiply a segment by matrix.*/
  66. inline void mult(const Matrix& m,const LineSegment& seg) { _s = m*seg._s; _e = m*seg._e; }
  67. protected:
  68. virtual ~LineSegment();
  69. static bool intersectAndClip(vec_type& s,vec_type& e,const BoundingBox& bb);
  70. vec_type _s;
  71. vec_type _e;
  72. };
  73. }
  74. #endif