GteSegment.h
Go to the documentation of this file.
1 // David Eberly, Geometric Tools, Redmond WA 98052
2 // Copyright (c) 1998-2017
3 // Distributed under the Boost Software License, Version 1.0.
4 // http://www.boost.org/LICENSE_1_0.txt
5 // http://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
6 // File Version: 3.0.0 (2016/06/19)
7 
8 #pragma once
9 
10 #include <Mathematics/GteVector.h>
11 
12 // The segment is represented by (1-t)*P0 + t*P1, where P0 and P1 are the
13 // endpoints of the segment and 0 <= t <= 1. Some algorithms prefer a
14 // centered representation that is similar to how oriented bounding boxes are
15 // defined. This representation is C + s*D, where C = (P0 + P1)/2 is the
16 // center of the segment, D = (P1 - P0)/|P1 - P0| is a unit-length direction
17 // vector for the segment, and |t| <= e. The value e = |P1 - P0|/2 is the
18 // extent (or radius or half-length) of the segment.
19 
20 namespace gte
21 {
22 
23 template <int N, typename Real>
24 class Segment
25 {
26 public:
27  // Construction and destruction. The default constructor sets p0 to
28  // (-1,0,...,0) and p1 to (1,0,...,0). NOTE: If you set p0 and p1;
29  // compute C, D, and e; and then recompute q0 = C-e*D and q1 = C+e*D,
30  // numerical round-off errors can lead to q0 not exactly equal to p0
31  // and q1 not exactly equal to p1.
32  Segment();
33  Segment(Vector<N, Real> const& p0, Vector<N, Real> const& p1);
34  Segment(std::array<Vector<N, Real>, 2> const& inP);
35  Segment(Vector<N, Real> const& center, Vector<N, Real> const& direction,
36  Real extent);
37 
38  // Manipulation via the centered form.
39  void SetCenteredForm(Vector<N, Real> const& center,
40  Vector<N, Real> const& direction, Real extent);
41 
42  void GetCenteredForm(Vector<N, Real>& center, Vector<N, Real>& direction,
43  Real& extent) const;
44 
45  // Public member access.
46  std::array<Vector<N, Real>, 2> p;
47 
48 public:
49  // Comparisons to support sorted containers.
50  bool operator==(Segment const& segment) const;
51  bool operator!=(Segment const& segment) const;
52  bool operator< (Segment const& segment) const;
53  bool operator<=(Segment const& segment) const;
54  bool operator> (Segment const& segment) const;
55  bool operator>=(Segment const& segment) const;
56 };
57 
58 // Template aliases for convenience.
59 template <typename Real>
61 
62 template <typename Real>
64 
65 
66 template <int N, typename Real>
68 {
69  p[1].MakeUnit(0);
70  p[0] = -p[1];
71 }
72 
73 template <int N, typename Real>
75  Vector<N, Real> const& p1)
76 {
77  p[0] = p0;
78  p[1] = p1;
79 }
80 
81 template <int N, typename Real>
83 {
84  p = inP;
85 }
86 
87 template <int N, typename Real>
89  Vector<N, Real> const& direction, Real extent)
90 {
91  SetCenteredForm(center, direction, extent);
92 }
93 
94 template <int N, typename Real>
96  Vector<N, Real> const& direction, Real extent)
97 {
98  p[0] = center - extent * direction;
99  p[1] = center + extent * direction;
100 }
101 
102 template <int N, typename Real>
104  Vector<N, Real>& direction, Real& extent) const
105 {
106  center = ((Real)0.5)*(p[0] + p[1]);
107  direction = p[1] - p[0];
108  extent = ((Real)0.5)*Normalize(direction);
109 }
110 
111 template <int N, typename Real>
112 bool Segment<N, Real>::operator==(Segment const& segment) const
113 {
114  return p == segment.p;
115 }
116 
117 template <int N, typename Real>
118 bool Segment<N, Real>::operator!=(Segment const& segment) const
119 {
120  return !operator==(segment);
121 }
122 
123 template <int N, typename Real>
124 bool Segment<N, Real>::operator<(Segment const& segment) const
125 {
126  return p < segment.p;
127 }
128 
129 template <int N, typename Real>
130 bool Segment<N, Real>::operator<=(Segment const& segment) const
131 {
132  return operator<(segment) || operator==(segment);
133 }
134 
135 template <int N, typename Real>
136 bool Segment<N, Real>::operator>(Segment const& segment) const
137 {
138  return !operator<=(segment);
139 }
140 
141 template <int N, typename Real>
142 bool Segment<N, Real>::operator>=(Segment const& segment) const
143 {
144  return !operator<(segment);
145 }
146 
147 
148 }
std::array< Vector< N, Real >, 2 > p
Definition: GteSegment.h:46
bool operator>=(Segment const &segment) const
Definition: GteSegment.h:142
bool operator==(Segment const &segment) const
Definition: GteSegment.h:112
bool operator<=(Segment const &segment) const
Definition: GteSegment.h:130
bool operator<(Segment const &segment) const
Definition: GteSegment.h:124
Real Normalize(GVector< Real > &v, bool robust=false)
Definition: GteGVector.h:454
GLenum array
Definition: glext.h:6669
bool operator>(Segment const &segment) const
Definition: GteSegment.h:136
void SetCenteredForm(Vector< N, Real > const &center, Vector< N, Real > const &direction, Real extent)
Definition: GteSegment.h:95
GLfloat GLfloat p
Definition: glext.h:11668
bool operator!=(Segment const &segment) const
Definition: GteSegment.h:118
void GetCenteredForm(Vector< N, Real > &center, Vector< N, Real > &direction, Real &extent) const
Definition: GteSegment.h:103


geometric_tools_engine
Author(s): Yijiang Huang
autogenerated on Thu Jul 18 2019 04:00:01