rect.h
6.33 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#ifndef STIM_RECT_H
#define STIM_RECT_H
//enable CUDA_CALLABLE macro
#include <stim/cuda/cudatools/callable.h>
#include <stim/math/plane.h>
#include <stim/math/vector.h>
#include <stim/math/triangle.h>
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <assert.h>
namespace stim{
//template for a rectangle class in ND space
template <typename T>
class rect : plane <T>
{
/*
^ O
|
|
Y P
|
|
O---------X--------->
*/
private:
stim::vec<T> X;
stim::vec<T> Y;
CUDA_CALLABLE void scale(T factor){
X *= factor;
Y *= factor;
}
public:
using stim::plane<T>::n;
using stim::plane<T>::P;
using stim::plane<T>::N;
using stim::plane<T>::U;
using stim::plane<T>::rotate;
///base constructor.
CUDA_CALLABLE rect()
: plane<T>()
{
init();
}
///create a rectangle given a size and position in Z space.
///@param size: size of the rectangle in ND space.
///@param z_pos z coordinate of the rectangle.
CUDA_CALLABLE rect(T size, T z_pos = (T)0)
: plane<T>(z_pos)
{
init(); //use the default setup
scale(size); //scale the rectangle
}
///create a rectangle from a center point, normal
///@param c: x,y,z location of the center.
///@param n: x,y,z direction of the normal.
CUDA_CALLABLE rect(vec<T> c, vec<T> n = vec<T>(0, 0, 1))
: plane<T>(n, c)
{
init(); //start with the default setting
normal(n); //orient
}
///create a rectangle from a center point, normal, and size
///@param c: x,y,z location of the center.
///@param s: size of the rectangle.
///@param n: x,y,z direction of the normal.
CUDA_CALLABLE rect(vec<T> c, T s, vec<T> n = vec<T>(0, 0, 1))
: plane<T>(n, c)
{
init(); //start with the default setting
scale(s);
}
///creates a rectangle from a centerpoint and an X and Y direction vectors.
///@param center: x,y,z location of the center.
///@param directionX: u,v,w direction of the X vector.
///@param directionY: u,v,w direction of the Y vector.
CUDA_CALLABLE rect(vec<T> center, vec<T> directionX, vec<T> directionY )
: plane<T>((directionX.cross(directionY)).norm(),center)
{
X = directionX;
Y = directionY;
}
///creates a rectangle from a size, centerpoint, X, and Y direction vectors.
///@param size of the rectangle in ND space.
///@param center: x,y,z location of the center.
///@param directionX: u,v,w direction of the X vector.
///@param directionY: u,v,w direction of the Y vector.
CUDA_CALLABLE rect(T size, vec<T> center, vec<T> directionX, vec<T> directionY )
: plane<T>((directionX.cross(directionY)).norm(),center)
{
X = directionX;
Y = directionY;
scale(size);
}
///creates a rectangle from a size, centerpoint, X, and Y direction vectors.
///@param size of the rectangle in ND space, size[0] = size in X, size[1] = size in Y.
///@param center: x,y,z location of the center.
///@param directionX: u,v,w direction of the X vector.
///@param directionY: u,v,w direction of the Y vector.
CUDA_CALLABLE rect(vec<T> size, vec<T> center, vec<T> directionX, vec<T> directionY)
: plane<T>((directionX.cross(directionY)).norm(), center)
{
X = directionX;
Y = directionY;
scale(size[0], size[1]);
}
///scales a rectangle in ND space.
///@param factor1: size of the scale in the X-direction.
///@param factor2: size of the scale in the Y-direction.
CUDA_CALLABLE void scale(T factor1, T factor2)
{
X *= factor1;
Y *= factor2;
}
///@param n; vector with the normal.
///Orients the rectangle along the normal n.
CUDA_CALLABLE void normal(vec<T> n)
{
//orient the rectangle along the specified normal
rotate(n, X, Y);
}
///general init method that sets a general rectangle.
CUDA_CALLABLE void init()
{
X = vec<T>(1, 0, 0);
Y = vec<T>(0, 1, 0);
}
//boolean comparison
bool operator==(const rect<T> & rhs)
{
if(P == rhs.P && X == rhs.X && Y == rhs.Y)
return true;
else
return false;
}
//get the world space value given the planar coordinates a, b in [0, 1]
CUDA_CALLABLE stim::vec<T> p(T a, T b)
{
stim::vec<T> result;
//given the two parameters a, b = [0 1], returns the position in world space
vec<T> A = this->P - X * (T)0.5 - Y * (T)0.5;
result = A + X * a + Y * b;
return result;
}
//parenthesis operator returns the world space given rectangular coordinates a and b in [0 1]
CUDA_CALLABLE stim::vec<T> operator()(T a, T b)
{
return p(a, b);
}
std::string str()
{
std::stringstream ss;
vec<T> A = P - X * (T)0.5 - Y * (T)0.5;
ss<<std::left<<"B="<<std::setfill('-')<<std::setw(20)<<A + Y<<">"<<"C="<<A + Y + X<<std::endl;
ss<<std::setfill(' ')<<std::setw(23)<<"|"<<"|"<<std::endl<<std::setw(23)<<"|"<<"|"<<std::endl;
ss<<std::left<<"A="<<std::setfill('-')<<std::setw(20)<<A<<">"<<"D="<<A + X;
return ss.str();
}
///multiplication operator scales the rectangle by a value rhs.
CUDA_CALLABLE rect<T> operator*(T rhs)
{
//scales the plane by a scalar value
//create the new rectangle
rect<T> result = *this;
result.scale(rhs);
return result;
}
///computes the distance between the specified point and this rectangle.
///@param p: x, y, z coordinates of the point to calculate distance to.
CUDA_CALLABLE T dist(vec<T> p)
{
//compute the distance between a point and this rect
vec<T> A = P - X * (T)0.5 - Y * (T)0.5;
//first break the rect up into two triangles
triangle<T> T0(A, A+X, A+Y);
triangle<T> T1(A+X+Y, A+X, A+Y);
T d0 = T0.dist(p);
T d1 = T1.dist(p);
if(d0 < d1)
return d0;
else
return d1;
}
CUDA_CALLABLE T center(vec<T> p)
{
this->P = p;
}
///Returns the maximum distance of the rectangle from a point p to the sides of the rectangle.
///@param p: x, y, z point.
CUDA_CALLABLE T dist_max(vec<T> p)
{
vec<T> A = P - X * (T)0.5 - Y * (T)0.5;
T da = (A - p).len();
T db = (A+X - p).len();
T dc = (A+Y - p).len();
T dd = (A+X+Y - p).len();
return std::max( da, std::max(db, std::max(dc, dd) ) );
}
};
} //end namespace rts
template <typename T, int N>
std::ostream& operator<<(std::ostream& os, stim::rect<T> R)
{
os<<R.str();
return os;
}
#endif