| 1 | /** |
|---|
| 2 | @file Line.h |
|---|
| 3 | |
|---|
| 4 | Line class |
|---|
| 5 | |
|---|
| 6 | @maintainer Morgan McGuire, matrix@graphics3d.com |
|---|
| 7 | |
|---|
| 8 | @created 2001-06-02 |
|---|
| 9 | @edited 2006-02-28 |
|---|
| 10 | */ |
|---|
| 11 | |
|---|
| 12 | #ifndef G3D_LINE_H |
|---|
| 13 | #define G3D_LINE_H |
|---|
| 14 | |
|---|
| 15 | #include "G3D/platform.h" |
|---|
| 16 | #include "G3D/Vector3.h" |
|---|
| 17 | |
|---|
| 18 | namespace G3D { |
|---|
| 19 | |
|---|
| 20 | class Plane; |
|---|
| 21 | |
|---|
| 22 | /** |
|---|
| 23 | An infinite 3D line. |
|---|
| 24 | */ |
|---|
| 25 | class Line { |
|---|
| 26 | protected: |
|---|
| 27 | |
|---|
| 28 | Vector3 _point; |
|---|
| 29 | Vector3 _direction; |
|---|
| 30 | |
|---|
| 31 | Line(const Vector3& point, const Vector3& direction) { |
|---|
| 32 | _point = point; |
|---|
| 33 | _direction = direction.direction(); |
|---|
| 34 | } |
|---|
| 35 | |
|---|
| 36 | public: |
|---|
| 37 | |
|---|
| 38 | /** Undefined (provided for creating Array<Line> only) */ |
|---|
| 39 | inline Line() {} |
|---|
| 40 | |
|---|
| 41 | virtual ~Line() {} |
|---|
| 42 | |
|---|
| 43 | /** |
|---|
| 44 | Constructs a line from two (not equal) points. |
|---|
| 45 | */ |
|---|
| 46 | static Line fromTwoPoints(const Vector3 &point1, const Vector3 &point2) { |
|---|
| 47 | return Line(point1, point2 - point1); |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | /** |
|---|
| 51 | Creates a line from a point and a (nonzero) direction. |
|---|
| 52 | */ |
|---|
| 53 | static Line fromPointAndDirection(const Vector3& point, const Vector3& direction) { |
|---|
| 54 | return Line(point, direction); |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | /** |
|---|
| 58 | Returns the closest point on the line to point. |
|---|
| 59 | */ |
|---|
| 60 | Vector3 closestPoint(const Vector3& pt) const; |
|---|
| 61 | |
|---|
| 62 | /** |
|---|
| 63 | Returns the distance between point and the line |
|---|
| 64 | */ |
|---|
| 65 | double distance(const Vector3& point) const { |
|---|
| 66 | return (closestPoint(point) - point).magnitude(); |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | /** Returns a point on the line */ |
|---|
| 70 | Vector3 point() const; |
|---|
| 71 | |
|---|
| 72 | /** Returns the direction (or negative direction) of the line */ |
|---|
| 73 | Vector3 direction() const; |
|---|
| 74 | |
|---|
| 75 | /** |
|---|
| 76 | Returns the point where the line and plane intersect. If there |
|---|
| 77 | is no intersection, returns a point at infinity. |
|---|
| 78 | */ |
|---|
| 79 | Vector3 intersection(const Plane &plane) const; |
|---|
| 80 | }; |
|---|
| 81 | |
|---|
| 82 | };// namespace |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | #endif |
|---|