81e0d221
David Mayerich
separated executa...
|
1
2
3
4
5
|
#ifndef RTS_RECT_H
#define RTS_RECT_H
//enable CUDA_CALLABLE macro
#include "../cuda/callable.h"
|
5eeaf941
Pavel Govyadinov
changer to the ba...
|
6
|
#include "../math/mathvec.h"
|
81e0d221
David Mayerich
separated executa...
|
7
8
9
10
11
12
13
14
15
|
#include "../math/triangle.h"
#include "../math/quaternion.h"
#include <iostream>
#include <iomanip>
#include <algorithm>
namespace stim{
//template for a rectangle class in ND space
|
5eeaf941
Pavel Govyadinov
changer to the ba...
|
16
|
template <class T>
|
81e0d221
David Mayerich
separated executa...
|
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
struct rect
{
/*
^ O
|
|
Y C
|
|
O---------X--------->
*/
private:
|
5eeaf941
Pavel Govyadinov
changer to the ba...
|
31
32
33
|
stim::vec<T> C;
stim::vec<T> X;
stim::vec<T> Y;
|
81e0d221
David Mayerich
separated executa...
|
34
35
36
37
38
|
CUDA_CALLABLE void scale(T factor){
X *= factor;
Y *= factor;
}
|
4cefeb6d
Pavel Govyadinov
Changes to the re...
|
39
|
|
81e0d221
David Mayerich
separated executa...
|
40
|
|
5eeaf941
Pavel Govyadinov
changer to the ba...
|
41
|
CUDA_CALLABLE void normal(vec<T> n){ //orient the rectangle along the specified normal
|
81e0d221
David Mayerich
separated executa...
|
42
43
|
n = n.norm(); //normalize, just in case
|
5eeaf941
Pavel Govyadinov
changer to the ba...
|
44
|
vec<T> n_current = X.cross(Y).norm(); //compute the current normal
|
81e0d221
David Mayerich
separated executa...
|
45
46
47
48
49
50
51
52
53
|
quaternion<T> q; //create a quaternion
q.CreateRotation(n_current, n); //initialize a rotation from n_current to n
//apply the quaternion to the vectors and position
X = q.toMatrix3() * X;
Y = q.toMatrix3() * Y;
}
CUDA_CALLABLE void init(){
|
5eeaf941
Pavel Govyadinov
changer to the ba...
|
54
55
56
|
C = vec<T>(0, 0, 0);
X = vec<T>(1, 0, 0);
Y = vec<T>(0, 1, 0);
|
81e0d221
David Mayerich
separated executa...
|
57
58
59
60
61
62
63
64
|
}
public:
CUDA_CALLABLE rect(){
init();
}
|
eeebe223
David Mayerich
added comments to...
|
65
|
//create a rectangle given a size and position
|
81e0d221
David Mayerich
separated executa...
|
66
67
68
69
70
71
|
CUDA_CALLABLE rect(T size, T z_pos = (T)0){
init(); //use the default setup
scale(size); //scale the rectangle
C[2] = z_pos;
}
|
4cefeb6d
Pavel Govyadinov
Changes to the re...
|
72
73
|
//create a rectangle from a center point, normal, and size
|
5eeaf941
Pavel Govyadinov
changer to the ba...
|
74
|
CUDA_CALLABLE rect(vec<T> c, vec<T> n = vec<T>(0, 0, 1)){
|
4cefeb6d
Pavel Govyadinov
Changes to the re...
|
75
76
|
init(); //start with the default setting
C = c;
|
4cefeb6d
Pavel Govyadinov
Changes to the re...
|
77
78
|
normal(n); //orient
}
|
81e0d221
David Mayerich
separated executa...
|
79
|
|
5eeaf941
Pavel Govyadinov
changer to the ba...
|
80
|
CUDA_CALLABLE rect(vec<T> center, vec<T> directionX, vec<T> directionY )
|
4cefeb6d
Pavel Govyadinov
Changes to the re...
|
81
82
83
84
85
86
|
{
C = center;
X = directionX;
Y = directionY;
}
|
5eeaf941
Pavel Govyadinov
changer to the ba...
|
87
|
CUDA_CALLABLE rect(T size, vec<T> center, vec<T> directionX, vec<T> directionY )
|
13c2a7d4
Pavel Govyadinov
some changes to t...
|
88
89
90
91
|
{
C = center;
X = directionX;
Y = directionY;
|
5f81932b
David Mayerich
restored Pavel's ...
|
92
93
94
|
scale(size);
}
|
5eeaf941
Pavel Govyadinov
changer to the ba...
|
95
|
CUDA_CALLABLE rect(vec<T> size, vec<T> center, vec<T> directionX, vec<T> directionY )
|
5f81932b
David Mayerich
restored Pavel's ...
|
96
97
98
99
100
|
{
C = center;
X = directionX;
Y = directionY;
scale(size[0], size[1]);
|
13c2a7d4
Pavel Govyadinov
some changes to t...
|
101
|
}
|
4cefeb6d
Pavel Govyadinov
Changes to the re...
|
102
103
104
105
106
|
CUDA_CALLABLE void scale(T factor1, T factor2){
X *= factor1;
Y *= factor2;
}
|
81e0d221
David Mayerich
separated executa...
|
107
108
|
//boolean comparison
|
5eeaf941
Pavel Govyadinov
changer to the ba...
|
109
|
bool operator==(const rect<T> & rhs)
|
81e0d221
David Mayerich
separated executa...
|
110
111
112
113
114
115
116
117
118
119
|
{
if(C == rhs.C && X == rhs.X && Y == rhs.Y)
return true;
else
return false;
}
/*******************************************
Return the normal for the rect
*******************************************/
|
5eeaf941
Pavel Govyadinov
changer to the ba...
|
120
|
CUDA_CALLABLE stim::vec<T> n()
|
81e0d221
David Mayerich
separated executa...
|
121
122
123
124
|
{
return (X.cross(Y)).norm();
}
|
eeebe223
David Mayerich
added comments to...
|
125
|
//get the world space value given the planar coordinates a, b in [0, 1]
|
5eeaf941
Pavel Govyadinov
changer to the ba...
|
126
|
CUDA_CALLABLE stim::vec<T> p(T a, T b)
|
81e0d221
David Mayerich
separated executa...
|
127
|
{
|
5eeaf941
Pavel Govyadinov
changer to the ba...
|
128
|
stim::vec<T> result;
|
81e0d221
David Mayerich
separated executa...
|
129
|
//given the two parameters a, b = [0 1], returns the position in world space
|
5eeaf941
Pavel Govyadinov
changer to the ba...
|
130
|
vec<T> A = C - X * (T)0.5 - Y * (T)0.5;
|
81e0d221
David Mayerich
separated executa...
|
131
132
133
134
135
|
result = A + X * a + Y * b;
return result;
}
|
eeebe223
David Mayerich
added comments to...
|
136
|
//parenthesis operator returns the world space given rectangular coordinates a and b in [0 1]
|
5eeaf941
Pavel Govyadinov
changer to the ba...
|
137
|
CUDA_CALLABLE stim::vec<T> operator()(T a, T b)
|
81e0d221
David Mayerich
separated executa...
|
138
139
140
141
142
143
144
|
{
return p(a, b);
}
std::string str()
{
std::stringstream ss;
|
5eeaf941
Pavel Govyadinov
changer to the ba...
|
145
|
vec<T> A = C - X * (T)0.5 - Y * (T)0.5;
|
81e0d221
David Mayerich
separated executa...
|
146
147
148
149
150
151
152
153
|
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();
}
|
eeebe223
David Mayerich
added comments to...
|
154
|
//scales the rectangle by a value rhs
|
5eeaf941
Pavel Govyadinov
changer to the ba...
|
155
|
CUDA_CALLABLE rect<T> operator*(T rhs)
|
81e0d221
David Mayerich
separated executa...
|
156
157
158
159
|
{
//scales the plane by a scalar value
//create the new rectangle
|
5eeaf941
Pavel Govyadinov
changer to the ba...
|
160
|
rect<T> result = *this;
|
81e0d221
David Mayerich
separated executa...
|
161
162
163
164
165
166
|
result.scale(rhs);
return result;
}
|
eeebe223
David Mayerich
added comments to...
|
167
|
//computes the distance between the specified point and this rectangle
|
5eeaf941
Pavel Govyadinov
changer to the ba...
|
168
|
CUDA_CALLABLE T dist(vec<T> p)
|
81e0d221
David Mayerich
separated executa...
|
169
170
171
|
{
//compute the distance between a point and this rect
|
5eeaf941
Pavel Govyadinov
changer to the ba...
|
172
|
vec<T> A = C - X * (T)0.5 - Y * (T)0.5;
|
81e0d221
David Mayerich
separated executa...
|
173
174
|
//first break the rect up into two triangles
|
5eeaf941
Pavel Govyadinov
changer to the ba...
|
175
176
|
triangle<T> T0(A, A+X, A+Y);
triangle<T> T1(A+X+Y, A+X, A+Y);
|
81e0d221
David Mayerich
separated executa...
|
177
178
179
180
181
182
183
184
185
186
187
|
T d0 = T0.dist(p);
T d1 = T1.dist(p);
if(d0 < d1)
return d0;
else
return d1;
}
|
5eeaf941
Pavel Govyadinov
changer to the ba...
|
188
|
CUDA_CALLABLE T dist_max(vec<T> p)
|
81e0d221
David Mayerich
separated executa...
|
189
|
{
|
5eeaf941
Pavel Govyadinov
changer to the ba...
|
190
|
vec<T> A = C - X * (T)0.5 - Y * (T)0.5;
|
81e0d221
David Mayerich
separated executa...
|
191
192
193
194
195
196
197
198
199
200
201
202
|
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>
|
5eeaf941
Pavel Govyadinov
changer to the ba...
|
203
|
std::ostream& operator<<(std::ostream& os, stim::rect<T> R)
|
81e0d221
David Mayerich
separated executa...
|
204
205
206
207
208
209
210
|
{
os<<R.str();
return os;
}
#endif
|