1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2004 Robert Osfield
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.
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.
14#ifndef OSG_LINESEGMENT
15#define OSG_LINESEGMENT 1
18#include <osg/BoundingBox>
19#include <osg/BoundingSphere>
23/** LineSegment class for representing a line segment. */
24class OSG_EXPORT LineSegment : public Referenced
28 typedef Vec3d vec_type;
29 typedef vec_type::value_type value_type;
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) {}
35 LineSegment& operator = (const LineSegment& seg) { _s = seg._s; _e = seg._e; return *this; }
37 inline void set(const vec_type& s,const vec_type& e) { _s=s; _e=e; }
39 inline vec_type& start() { return _s; }
40 inline const vec_type& start() const { return _s; }
42 inline vec_type& end() { return _e; }
43 inline const vec_type& end() const { return _e; }
45 inline bool valid() const { return _s.valid() && _e.valid() && _s!=_e; }
48 /** return true if segment intersects BoundingBox. */
49 bool intersect(const BoundingBox& bb) const;
51 /** return true if segment intersects BoundingBox and
52 * 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.
54 bool intersectAndComputeRatios(const BoundingBox& bb, float& ratioFromStartToEnd1, float& ratioFromStartToEnd2) const;
56 /** return true if segment intersects BoundingBox and
57 * 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.
59 bool intersectAndComputeRatios(const BoundingBox& bb, double& ratioFromStartToEnd1, double& ratioFromStartToEnd2) const;
62 /** return true if segment intersects BoundingSphere. */
63 bool intersect(const BoundingSphere& bs) const;
65 /** return true if segment intersects BoundingSphere and
66 * 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.
68 bool intersectAndComputeRatios(const BoundingSphere& bs, float& ratioFromStartToEnd1, float& ratioFromStartToEnd2) const;
70 /** return true if segment intersects BoundingSphere and
71 * 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.
73 bool intersectAndComputeRatios(const BoundingSphere& bs,double& ratioFromStartToEnd1, double& ratioFromStartToEnd2) const;
75 /** return true if segment intersects triangle and
76 * set float ratios where the ratio is 0.0 at the segment start point, and 1.0 at the segment end point.
78 bool intersect(const Vec3f& v1,const Vec3f& v2,const Vec3f& v3,float& ratioFromStartToEnd);
80 /** return true if segment intersects triangle and
81 * set double ratios where the ratio is 0.0 at the segment start point, and 1.0 at the segment end point.
83 bool intersect(const Vec3d& v1,const Vec3d& v2,const Vec3d& v3,double& ratioFromStartToEnd);
86 /** post multiply a segment by matrix.*/
87 inline void mult(const LineSegment& seg,const Matrix& m) { _s = seg._s*m; _e = seg._e*m; }
89 /** pre multiply a segment by matrix.*/
90 inline void mult(const Matrix& m,const LineSegment& seg) { _s = m*seg._s; _e = m*seg._e; }
94 virtual ~LineSegment();
96 static bool intersectAndClip(vec_type& s,vec_type& e,const BoundingBox& bb);