TrinityCore
G3D::Capsule Class Reference

`#include <Capsule.h>`

## Public Member Functions

Capsule ()

Capsule (class BinaryInput &b)

Capsule (const Vector3 &_p1, const Vector3 &_p2, float _r)

void serialize (class BinaryOutput &b) const

void deserialize (class BinaryInput &b)

Line axis () const

Vector3 point (int i) const

float height () const

Vector3 center () const

void getReferenceFrame (class CoordinateFrame &cframe) const

bool contains (const Vector3 &p) const

float volume () const

float area () const

void getBounds (AABox &out) const

void getRandomSurfacePoint (Vector3 &P, Vector3 &N) const

Vector3 randomInteriorPoint () const

Vector3 p1

Vector3 p2

## Detailed Description

A shape formed by extruding a sphere along a line segment.

## Constructor & Destructor Documentation

 G3D::Capsule::Capsule ( )

Uninitialized

29  {
30 }
 G3D::Capsule::Capsule ( class BinaryInput & b )
24  {
25  deserialize(b);
26 }
void deserialize(class BinaryInput &b)
Definition: Capsule.cpp:45

Here is the call graph for this function:

 G3D::Capsule::Capsule ( const Vector3 & _p1, const Vector3 & _p2, float _r )
34  : p1(_p1), p2(_p2), _radius(_r) {
35 }
Vector3 p1
Definition: Capsule.h:29
Vector3 p2
Definition: Capsule.h:30
Definition: Capsule.h:32

## Member Function Documentation

 float G3D::Capsule::area ( ) const
67  {
68
69  return
70  // Sphere area
71  pow(_radius, 2) * 4 * (float)pi() +
72
73  // Cylinder area
74  (float)twoPi() * _radius * (p1 - p2).magnitude();
75 }
Vector3 p1
Definition: Capsule.h:29
Vector3 p2
Definition: Capsule.h:30
double pi()
Definition: g3dmath.h:147
Definition: Capsule.h:32
double twoPi()
Definition: g3dmath.h:159
G3D::Quat pow(const G3D::Quat &q, double x)
Definition: Quat.h:761

Here is the call graph for this function:

 Line G3D::Capsule::axis ( ) const

The line down the center of the capsule

52  {
53  return Line::fromTwoPoints(p1, p2);
54 }
Vector3 p1
Definition: Capsule.h:29
Vector3 p2
Definition: Capsule.h:30
static Line fromTwoPoints(const Vector3 &point1, const Vector3 &point2)
Definition: Line.h:52

Here is the call graph for this function:

 Vector3 G3D::Capsule::center ( ) const
inline
62  {
63  return (p1 + p2) / 2.0;
64  }
Vector3 p1
Definition: Capsule.h:29
Vector3 p2
Definition: Capsule.h:30

Here is the caller graph for this function:

 bool G3D::Capsule::contains ( const Vector3 & p ) const

Returns true if the point is inside the capsule or on its surface.

86  {
88 }
Vector3 p1
Definition: Capsule.h:29
Vector3 p2
Definition: Capsule.h:30
static LineSegment fromTwoPoints(const Point3 &point1, const Point3 &point2)
Definition: LineSegment.h:47
Definition: Capsule.h:46
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:

 void G3D::Capsule::deserialize ( class BinaryInput & b )
45  {
46  p1.deserialize(b);
47  p2.deserialize(b);
49 }
Vector3 p1
Definition: Capsule.h:29
Vector3 p2
Definition: Capsule.h:30
Definition: Capsule.h:32
void deserialize(class BinaryInput &b)
Definition: Vector3.cpp:190

Here is the call graph for this function:

Here is the caller graph for this function:

 void G3D::Capsule::getBounds ( AABox & out ) const

Get axis aligned bounding box

78  {
79  Vector3 min = p1.min(p2) - (Vector3(1, 1, 1) * _radius);
80  Vector3 max = p1.max(p2) + (Vector3(1, 1, 1) * _radius);
81
82  out = AABox(min, max);
83 }
Vector3 __fastcall max(const Vector3 &v) const
Definition: Vector3.h:794
Vector3 p1
Definition: Capsule.h:29
Vector3 p2
Definition: Capsule.h:30
Vector3 __fastcall min(const Vector3 &v) const
Definition: Vector3.h:789
T max(const T &x, const T &y)
Definition: g3dmath.h:320
T min(const T &x, const T &y)
Definition: g3dmath.h:305
Definition: Capsule.h:32

Here is the call graph for this function:

 void G3D::Capsule::getRandomSurfacePoint ( Vector3 & P, Vector3 & N ) const

Random world space point with outward facing normal.

91  {
92  float h = height();
94
95  // Create a random point on a standard capsule and then rotate to the global frame.
96
97  // Relative areas
98  float capRelArea = sqrt(r) / 2.0f;
99  float sideRelArea = r * h;
100
101  float r1 = uniformRandom(0, capRelArea * 2 + sideRelArea);
102
103  if (r1 < capRelArea * 2) {
104
105  // Select a point uniformly at random on a sphere
106  N = Sphere(Vector3::zero(), 1).randomSurfacePoint();
107  p = N * r;
108  p.y += sign(p.y) * h / 2.0f;
109  } else {
110  // Side
111  float a = uniformRandom(0, (float)twoPi());
112  N.x = cos(a);
113  N.y = 0;
114  N.z = sin(a);
115  p.x = N.x * r;
116  p.z = N.y * r;
117  p.y = uniformRandom(-h / 2.0f, h / 2.0f);
118  }
119
120  // Transform to world space
121  CoordinateFrame cframe;
122  getReferenceFrame(cframe);
123
124  p = cframe.pointToWorldSpace(p);
125  N = cframe.normalToWorldSpace(N);
126 }
float uniformRandom(float low=0.0f, float hi=1.0f)
Definition: g3dmath.h:694
static const Vector3 & zero()
Definition: Vector3.cpp:119
float height() const
Definition: Capsule.h:58
Definition: Capsule.h:46
double twoPi()
Definition: g3dmath.h:159
double sign(double fValue)
Definition: g3dmath.h:669
void getReferenceFrame(class CoordinateFrame &cframe) const
Definition: Capsule.cpp:129

Here is the call graph for this function:

 void G3D::Capsule::getReferenceFrame ( class CoordinateFrame & cframe ) const

Get a reference frame in which the center of mass is the origin and Y is the axis of the capsule.

129  {
130  cframe.translation = center();
131
132  Vector3 Y = (p1 - p2).direction();
133  Vector3 X = (abs(Y.dot(Vector3::unitX())) > 0.9) ? Vector3::unitY() : Vector3::unitX();
134  Vector3 Z = X.cross(Y).direction();
135  X = Y.cross(Z);
136  cframe.rotation.setColumn(0, X);
137  cframe.rotation.setColumn(1, Y);
138  cframe.rotation.setColumn(2, Z);
139 }
#define Z
Definition: CollisionDetection.cpp:2283
Vector3 center() const
Definition: Capsule.h:62
#define X
Definition: CollisionDetection.cpp:2281
double abs(double fValue)
Definition: g3dmath.h:617
Vector3 p1
Definition: Capsule.h:29
Vector3 p2
Definition: Capsule.h:30
static const Vector3 & unitX()
Definition: Vector3.cpp:121
static const Vector3 & unitY()
Definition: Vector3.cpp:122
#define Y
Definition: CollisionDetection.cpp:2282

Here is the call graph for this function:

Here is the caller graph for this function:

 float G3D::Capsule::height ( ) const
inline

Distance between the sphere centers. The total extent of the cylinder is 2r + h.

58  {
59  return (p1 - p2).magnitude();
60  }
Vector3 p1
Definition: Capsule.h:29
Vector3 p2
Definition: Capsule.h:30

Here is the caller graph for this function:

 Vector3 G3D::Capsule::point ( int i ) const
inline

Argument may be 0 or 1

51  {
52  debugAssert(i == 0 || i == 1);
53  return (i == 0) ? p1 : p2;
54  }
Vector3 p1
Definition: Capsule.h:29
Vector3 p2
Definition: Capsule.h:30
#define debugAssert(exp)
Definition: debugAssert.h:160

Here is the caller graph for this function:

inline
46  {
48  }
Definition: Capsule.h:32

Here is the caller graph for this function:

 Vector3 G3D::Capsule::randomInteriorPoint ( ) const

Point selected uniformly at random over the volume.

142  {
143  float h = height();
145
146  // Create a random point in a standard capsule and then rotate to the global frame.
147
148  Vector3 p;
149
150  float hemiVolume = (float)pi() * (r*r*r) * 4 / 6.0f;
151  float cylVolume = (float)pi() * square(r) * h;
152
153  float r1 = uniformRandom(0, 2.0f * hemiVolume + cylVolume);
154
155  if (r1 < 2.0 * hemiVolume) {
156
157  p = Sphere(Vector3::zero(), r).randomInteriorPoint();
158
159  p.y += sign(p.y) * h / 2.0f;
160
161  } else {
162
163  // Select a point uniformly at random on a disk
164  float a = uniformRandom(0, (float)twoPi());
165  float r2 = sqrt(uniformRandom(0, 1)) * r;
166
167  p = Vector3(cos(a) * r2,
168  uniformRandom(-h / 2.0f, h / 2.0f),
169  sin(a) * r2);
170  }
171
172  // Transform to world space
173  CoordinateFrame cframe;
174  getReferenceFrame(cframe);
175
176  return cframe.pointToWorldSpace(p);
177 }
double pi()
Definition: g3dmath.h:147
float uniformRandom(float low=0.0f, float hi=1.0f)
Definition: g3dmath.h:694
static const Vector3 & zero()
Definition: Vector3.cpp:119
float height() const
Definition: Capsule.h:58
Definition: Capsule.h:46
double square(double fValue)
Definition: g3dmath.h:698
double twoPi()
Definition: g3dmath.h:159
double sign(double fValue)
Definition: g3dmath.h:669
void getReferenceFrame(class CoordinateFrame &cframe) const
Definition: Capsule.cpp:129

Here is the call graph for this function:

 void G3D::Capsule::serialize ( class BinaryOutput & b ) const
38  {
39  p1.serialize(b);
40  p2.serialize(b);
42 }
Vector3 p1
Definition: Capsule.h:29
Vector3 p2
Definition: Capsule.h:30
Definition: Capsule.h:32
void serialize(class BinaryOutput &b) const
Definition: Vector3.cpp:219

Here is the call graph for this function:

 float G3D::Capsule::volume ( ) const
57  {
58  return
59  // Sphere volume
60  pow(_radius, 3) * (float)pi() * 4 / 3 +
61
62  // Cylinder volume
63  pow(_radius, 2) * (p1 - p2).magnitude();
64 }
Vector3 p1
Definition: Capsule.h:29
Vector3 p2
Definition: Capsule.h:30
double pi()
Definition: g3dmath.h:147
Definition: Capsule.h:32
G3D::Quat pow(const G3D::Quat &q, double x)
Definition: Quat.h:761

Here is the call graph for this function: