TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
G3D::LineSegment Class Reference

#include <LineSegment.h>

Public Member Functions

 LineSegment ()
 
 LineSegment (class BinaryInput &b)
 
void serialize (class BinaryOutput &b) const
 
void deserialize (class BinaryInput &b)
 
virtual ~LineSegment ()
 
Point3 point (int i) const
 
Point3 midpoint () const
 
float length () const
 
Point3 closestPoint (const Point3 &point) const
 
double distance (const Point3 &p) const
 
double distanceSquared (const Point3 &p) const
 
bool intersectsSolidSphere (const class Sphere &s) const
 
Point3 randomPoint () const
 

Static Public Member Functions

static LineSegment fromTwoPoints (const Point3 &point1, const Point3 &point2)
 

Protected Member Functions

 LineSegment (const Point3 &__point, const Vector3 &_direction)
 

Protected Attributes

Point3 _point
 
Vector3 direction
 

Detailed Description

An finite segment of an infinite 3D line.

Constructor & Destructor Documentation

G3D::LineSegment::LineSegment ( const Point3 __point,
const Vector3 _direction 
)
inlineprotected
29  : _point(__point), direction(_direction) {
30  }
Point3 _point
Definition: LineSegment.h:24
Vector3 direction
Definition: LineSegment.h:27
G3D::LineSegment::LineSegment ( )
inline
Point3 _point
Definition: LineSegment.h:24
static const Vector3 & zero()
Definition: Vector3.cpp:119
Vector3 direction
Definition: LineSegment.h:27

+ Here is the caller graph for this function:

G3D::LineSegment::LineSegment ( class BinaryInput b)
82  {
83  deserialize(b);
84 }
void deserialize(class BinaryInput &b)
Definition: LineSegment.cpp:93

+ Here is the call graph for this function:

virtual G3D::LineSegment::~LineSegment ( )
inlinevirtual
42 {}

Member Function Documentation

Vector3 G3D::LineSegment::closestPoint ( const Point3 point) const

Returns the closest point on the line segment to point.

18  {
19 
20  // The vector from the end of the capsule to the point in question.
21  Vector3 v(p - _point);
22 
23  // Projection of v onto the line segment scaled by
24  // the length of direction.
25  float t = direction.dot(v);
26 
27  // Avoid some square roots. Derivation:
28  // t/direction.length() <= direction.length()
29  // t <= direction.squaredLength()
30 
31  if ((t >= 0) && (t <= direction.squaredMagnitude())) {
32 
33  // The point falls within the segment. Normalize direction,
34  // divide t by the length of direction.
35  return _point + direction * t / direction.squaredMagnitude();
36 
37  } else {
38 
39  // The point does not fall within the segment; see which end is closer.
40 
41  // Distance from 0, squared
42  float d0Squared = v.squaredMagnitude();
43 
44  // Distance from 1, squared
45  float d1Squared = (v - direction).squaredMagnitude();
46 
47  if (d0Squared < d1Squared) {
48 
49  // Point 0 is closer
50  return _point;
51 
52  } else {
53 
54  // Point 1 is closer
55  return _point + direction;
56 
57  }
58  }
59 
60 }
float squaredMagnitude() const
Definition: Vector3.h:736
Point3 _point
Definition: LineSegment.h:24
float __fastcall dot(const Vector3 &rkVector) const
Definition: Vector3.h:771
Vector3 direction
Definition: LineSegment.h:27

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void G3D::LineSegment::deserialize ( class BinaryInput b)
93  {
96 }
Point3 _point
Definition: LineSegment.h:24
void deserialize(class BinaryInput &b)
Definition: Vector3.cpp:190
Vector3 direction
Definition: LineSegment.h:27

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

double G3D::LineSegment::distance ( const Point3 p) const
inline

Returns the distance between point and the line

70  {
71  return (closestPoint(p) - p).magnitude();
72  }
Point3 closestPoint(const Point3 &point) const
Definition: LineSegment.cpp:18

+ Here is the call graph for this function:

double G3D::LineSegment::distanceSquared ( const Point3 p) const
inline
74  {
75  return (closestPoint(p) - p).squaredMagnitude();
76  }
Point3 closestPoint(const Point3 &point) const
Definition: LineSegment.cpp:18

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static LineSegment G3D::LineSegment::fromTwoPoints ( const Point3 point1,
const Point3 point2 
)
inlinestatic

Constructs a line from two (not equal) points.

47  {
48  return LineSegment(point1, point2 - point1);
49  }
LineSegment()
Definition: LineSegment.h:34

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

bool G3D::LineSegment::intersectsSolidSphere ( const class Sphere s) const

Returns true if some part of this segment is inside the sphere

77  {
78  return distanceSquared(s.center) <= square(s.radius);
79 }
double distanceSquared(const Point3 &p) const
Definition: LineSegment.h:74
double square(double fValue)
Definition: g3dmath.h:698

+ Here is the call graph for this function:

float G3D::LineSegment::length ( ) const
inline
58  {
59  return direction.magnitude();
60  }
float magnitude() const
Definition: Vector3.h:746
Vector3 direction
Definition: LineSegment.h:27

+ Here is the call graph for this function:

Point3 G3D::LineSegment::midpoint ( ) const
inline
54  {
55  return _point + direction * 0.5f;
56  }
Point3 _point
Definition: LineSegment.h:24
Vector3 direction
Definition: LineSegment.h:27
Vector3 G3D::LineSegment::point ( int  i) const

Call with 0 or 1

62  {
63  switch (i) {
64  case 0:
65  return _point;
66 
67  case 1:
68  return _point + direction;
69 
70  default:
71  debugAssertM(i == 0 || i == 1, "Argument to point must be 0 or 1");
72  return _point;
73  }
74 }
Point3 _point
Definition: LineSegment.h:24
#define debugAssertM(exp, message)
Definition: debugAssert.h:161
Vector3 direction
Definition: LineSegment.h:27
Vector3 G3D::LineSegment::randomPoint ( ) const
99  {
100  return _point + uniformRandom(0, 1) * direction;
101 }
Point3 _point
Definition: LineSegment.h:24
float uniformRandom(float low=0.0f, float hi=1.0f)
Definition: g3dmath.h:694
Vector3 direction
Definition: LineSegment.h:27

+ Here is the call graph for this function:

void G3D::LineSegment::serialize ( class BinaryOutput b) const
87  {
88  _point.serialize(b);
90 }
Point3 _point
Definition: LineSegment.h:24
void serialize(class BinaryOutput &b) const
Definition: Vector3.cpp:219
Vector3 direction
Definition: LineSegment.h:27

+ Here is the call graph for this function:

Member Data Documentation

Point3 G3D::LineSegment::_point
protected
Vector3 G3D::LineSegment::direction
protected

Not normalized


The documentation for this class was generated from the following files: