quad.h
3.73 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
#ifndef RTS_QUAD_H
#define RTS_QUAD_H
//enable CUDA_CALLABLE macro
#include <stim/cuda/callable.h>
#include <stim/math/vector.h>
#include <stim/math/triangle.h>
#include <stim/math/quaternion.h>
#include <iostream>
#include <iomanip>
#include <algorithm>
namespace stim{
//template for a quadangle class in ND space
template <class T, int N = 3>
struct quad
{
/*
B------------------>C
^ ^
| |
Y |
| |
| |
A---------X-------->O
*/
/*T A[N];
T B[N];
T C[N];*/
rts::vec<T, N> A;
rts::vec<T, N> X;
rts::vec<T, N> Y;
CUDA_CALLABLE quad()
{
}
CUDA_CALLABLE quad(vec<T, N> a, vec<T, N> b, vec<T, N> c)
{
A = a;
Y = b - a;
X = c - a - Y;
}
/*******************************************************************
Constructor - create a quad from a position, normal, and rotation
*******************************************************************/
CUDA_CALLABLE quad(rts::vec<T, N> c, rts::vec<T, N> normal, T width, T height, T theta)
{
//compute the X direction - start along world-space X
Y = rts::vec<T, N>(0, 1, 0);
if(Y == normal)
Y = rts::vec<T, N>(0, 0, 1);
X = Y.cross(normal).norm();
std::cout<<X<<std::endl;
//rotate the X axis by theta radians
rts::quaternion<T> q;
q.CreateRotation(theta, normal);
X = q.toMatrix3() * X;
Y = normal.cross(X);
//normalize everything
X = X.norm();
Y = Y.norm();
//scale to match the quad width and height
X = X * width;
Y = Y * height;
//set the corner of the plane
A = c - X * 0.5f - Y * 0.5f;
std::cout<<X<<std::endl;
}
//boolean comparison
bool operator==(const quad<T, N> & rhs)
{
if(A == rhs.A && X == rhs.X && Y == rhs.Y)
return true;
else
return false;
}
/*******************************************
Return the normal for the quad
*******************************************/
CUDA_CALLABLE rts::vec<T, N> n()
{
return (X.cross(Y)).norm();
}
CUDA_CALLABLE rts::vec<T, N> p(T a, T b)
{
rts::vec<T, N> result;
//given the two parameters a, b = [0 1], returns the position in world space
result = A + X * a + Y * b;
return result;
}
CUDA_CALLABLE rts::vec<T, N> operator()(T a, T b)
{
return p(a, b);
}
std::string str()
{
std::stringstream ss;
ss<<std::left<<"B="<<setfill('-')<<setw(20)<<A + Y<<">"<<"C="<<A + Y + X<<std::endl;
ss<<setfill(' ')<<setw(23)<<"|"<<"|"<<std::endl<<setw(23)<<"|"<<"|"<<std::endl;
ss<<std::left<<"A="<<setfill('-')<<setw(20)<<A<<">"<<"D="<<A + X;
return ss.str();
}
CUDA_CALLABLE quad<T, N> operator*(T rhs)
{
//scales the plane by a scalar value
//compute the center point
rts::vec<T, N> c = A + X*0.5f + Y*0.5f;
//create the new quadangle
quad<T, N> result;
result.X = X * rhs;
result.Y = Y * rhs;
result.A = c - result.X*0.5f - result.Y*0.5f;
return result;
}
CUDA_CALLABLE T dist(vec<T, N> p)
{
//compute the distance between a point and this quad
//first break the quad up into two triangles
triangle<T, N> T0(A, A+X, A+Y);
triangle<T, N> 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 dist_max(vec<T, N> p)
{
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, rts::quad<T, N> R)
{
os<<R.str();
return os;
}
#endif