GteRectangle.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 // Points are R(s0,s1) = C + s0*A0 + s1*A1, where C is the center of the
13 // rectangle and A0 and A1 are unit-length and perpendicular axes. The
14 // parameters s0 and s1 are constrained by |s0| <= e0 and |s1| <= e1,
15 // where e0 > 0 and e1 > 0 are the extents of the rectangle.
16 
17 namespace gte
18 {
19 
20 template <int N, typename Real>
21 class Rectangle
22 {
23 public:
24  // Construction and destruction. The default constructor sets the origin
25  // to (0,...,0), axis A0 to (1,0,...,0), axis A1 to (0,1,0,...0), and both
26  // extents to 1.
27  Rectangle();
28  Rectangle(Vector<N, Real> const& inCenter,
29  std::array<Vector<N, Real>, 2> const& inAxis,
30  Vector<2, Real> const& inExtent);
31 
32  // Compute the vertices of the rectangle. If index i has the bit pattern
33  // i = b[1]b[0], then
34  // vertex[i] = center + sum_{d=0}^{1} sign[d] * extent[d] * axis[d]
35  // where sign[d] = 2*b[d] - 1.
36  void GetVertices(std::array<Vector<N, Real>, 4>& vertex) const;
37 
39  std::array<Vector<N, Real>, 2> axis;
41 
42 public:
43  // Comparisons to support sorted containers.
44  bool operator==(Rectangle const& rectangle) const;
45  bool operator!=(Rectangle const& rectangle) const;
46  bool operator< (Rectangle const& rectangle) const;
47  bool operator<=(Rectangle const& rectangle) const;
48  bool operator> (Rectangle const& rectangle) const;
49  bool operator>=(Rectangle const& rectangle) const;
50 };
51 
52 // Template alias for convenience.
53 template <typename Real>
55 
56 
57 template <int N, typename Real>
59 {
60  center.MakeZero();
61  for (int i = 0; i < 2; ++i)
62  {
63  axis[i].MakeUnit(i);
64  extent[i] = (Real)1;
65  }
66 }
67 
68 template <int N, typename Real>
70  std::array<Vector<N, Real>, 2> const& inAxis,
71  Vector<2, Real> const& inExtent)
72  :
73  center(inCenter),
74  axis(inAxis),
75  extent(inExtent)
76 {
77 }
78 
79 template <int N, typename Real>
81  std::array<Vector<N, Real>, 4>& vertex) const
82 {
83  Vector<N, Real> product0 = extent[0] * axis[0];
84  Vector<N, Real> product1 = extent[1] * axis[1];
85  Vector<N, Real> sum = product0 + product1;
86  Vector<N, Real> dif = product0 - product1;
87 
88  vertex[0] = center - sum;
89  vertex[1] = center + dif;
90  vertex[2] = center - dif;
91  vertex[3] = center + sum;
92 }
93 
94 template <int N, typename Real>
95 bool Rectangle<N, Real>::operator==(Rectangle const& rectangle) const
96 {
97  if (center != rectangle.center)
98  {
99  return false;
100  }
101 
102  for (int i = 0; i < 2; ++i)
103  {
104  if (axis[i] != rectangle.axis[i])
105  {
106  return false;
107  }
108  }
109 
110  for (int i = 0; i < 2; ++i)
111  {
112  if (extent[i] != rectangle.extent[i])
113  {
114  return false;
115  }
116  }
117 
118  return true;
119 }
120 
121 template <int N, typename Real>
122 bool Rectangle<N, Real>::operator!=(Rectangle const& rectangle) const
123 {
124  return !operator==(rectangle);
125 }
126 
127 template <int N, typename Real>
128 bool Rectangle<N, Real>::operator<(Rectangle const& rectangle) const
129 {
130  if (center < rectangle.center)
131  {
132  return true;
133  }
134 
135  if (center > rectangle.center)
136  {
137  return false;
138  }
139 
140  if (axis < rectangle.axis)
141  {
142  return true;
143  }
144 
145  if (axis > rectangle.axis)
146  {
147  return false;
148  }
149 
150  return extent < rectangle.extent;
151 }
152 
153 template <int N, typename Real>
154 bool Rectangle<N, Real>::operator<=(Rectangle const& rectangle) const
155 {
156  return operator<(rectangle) || operator==(rectangle);
157 }
158 
159 template <int N, typename Real>
160 bool Rectangle<N, Real>::operator>(Rectangle const& rectangle) const
161 {
162  return !operator<=(rectangle);
163 }
164 
165 template <int N, typename Real>
166 bool Rectangle<N, Real>::operator>=(Rectangle const& rectangle) const
167 {
168  return !operator<(rectangle);
169 }
170 
171 
172 }
Vector< 2, Real > extent
Definition: GteRectangle.h:40
Vector< N, Real > center
Definition: GteRectangle.h:38
std::array< Vector< N, Real >, 2 > axis
Definition: GteRectangle.h:39
bool operator<(Rectangle const &rectangle) const
Definition: GteRectangle.h:128
bool operator<=(Rectangle const &rectangle) const
Definition: GteRectangle.h:154
bool operator==(Rectangle const &rectangle) const
Definition: GteRectangle.h:95
void GetVertices(std::array< Vector< N, Real >, 4 > &vertex) const
Definition: GteRectangle.h:80
bool operator!=(Rectangle const &rectangle) const
Definition: GteRectangle.h:122
bool operator>(Rectangle const &rectangle) const
Definition: GteRectangle.h:160
GLenum array
Definition: glext.h:6669
bool operator>=(Rectangle const &rectangle) const
Definition: GteRectangle.h:166


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