-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathrect.h
162 lines (137 loc) · 3.48 KB
/
rect.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#ifndef CUBE_H
#define CUBE_H
#include <vector>
#include "point.h"
template <size_t DIMENSION>
class Rect
{
public:
typedef typename Point<DIMENSION>::Vector Vector;
Rect()
{
}
Rect(const Vector &bottomLeft, const Vector &topRight)
: mBottomLeft(bottomLeft)
, mTopRight(topRight)
{
}
Vector& bottomLeft()
{
return mBottomLeft;
}
Vector& topRight()
{
return mTopRight;
}
Vector center() const
{
return (mBottomLeft + mTopRight) / 2;
}
float maxSize() const
{
float maxSize = 0;
for (size_t dim = 0; dim < DIMENSION; dim++)
{
maxSize = std::max(maxSize, mTopRight(dim) - mBottomLeft(dim));
}
return maxSize;
}
float minSize() const
{
float minSize = std::numeric_limits<float>::max();
for (size_t dim = 0; dim < DIMENSION; dim++)
{
minSize = std::min(minSize, mTopRight(dim) - mBottomLeft(dim));
}
return minSize;
}
float width() const
{
return mTopRight(0) - mBottomLeft(0);
}
float height() const
{
return mTopRight(1) - mBottomLeft(1);
}
float depth() const
{
return mTopRight(2) - mBottomLeft(2);
}
void getVertices(std::vector<Vector> &vertices) const
{
size_t numVertices = 1 << DIMENSION;
vertices.resize(numVertices);
for (size_t i = 0; i < numVertices; i++)
{
for (size_t dim = 0; dim < DIMENSION; dim++)
{
int signal = (((i & (1 << dim)) >> dim) << 1) - 1;
if (signal > 0)
{
vertices[i](dim) = mTopRight(dim);
}
else
{
vertices[i](dim) = mBottomLeft(dim);
}
}
}
}
/**
* @brief Find the closest point in the rect edges to point
* @param point
* Point to which the closest point will be found
* @return
* The closest point in the rect edges
*/
Vector closestPointToPoint(const Vector &point) const
{
Vector closestPoint;
for (size_t dim = 0; dim < DIMENSION; dim++)
{
if (point(dim) <= mBottomLeft(dim))
{
closestPoint(dim) = mBottomLeft(dim);
}
else if (point(dim) >= mTopRight(dim))
{
closestPoint(dim) = mTopRight(dim);
}
else
{
closestPoint(dim) = point(dim);
}
}
return closestPoint;
}
float distanceToPoint(const Vector &point) const
{
return (point - closestPointToPoint(point)).norm();
}
bool containsPoint(const Vector &point) const
{
for (size_t dim = 0; dim < DIMENSION; dim++)
{
float min = std::min(mBottomLeft(dim), mTopRight(dim));
float max = std::max(mBottomLeft(dim), mTopRight(dim));
if (!(point(dim) >= min && point(dim) <= max))
{
return false;
}
}
return true;
}
bool operator==(const Rect &other) const
{
return mBottomLeft == other.mBottomLeft &&
mTopRight == other.mTopRight;
}
private:
Vector mBottomLeft;
Vector mTopRight;
};
template class Rect<2>;
template class Rect<3>;
typedef Rect<2> Rect2d;
typedef Rect<3> Rect3d;
#endif // CUBE_H