Blame view

stim/math/rect.h 4.89 KB
81e0d221   David Mayerich   separated executa...
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
  #ifndef RTS_RECT_H

  #define RTS_RECT_H

  

  //enable CUDA_CALLABLE macro

  #include "../cuda/callable.h"

  #include "../math/vector.h"

  #include "../math/triangle.h"

  #include "../math/quaternion.h"

  #include <iostream>

  #include <iomanip>

  #include <algorithm>

  

  namespace stim{

  

  //template for a rectangle class in ND space

  template <class T, int N = 3>

  struct rect

  {

  	/*

  		^                   O

  		|                   

  		|                   

  		Y         C         

  		|                   

  		|                   

  		O---------X--------->

  	*/

  

  private:

  

  	stim::vec<T, N> C;

  	stim::vec<T, N> X;

  	stim::vec<T, N> Y;

  

  	CUDA_CALLABLE void scale(T factor){

  		X *= factor;

  		Y *= factor;

  	}

4cefeb6d   Pavel Govyadinov   Changes to the re...
39
  	

81e0d221   David Mayerich   separated executa...
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
  

  	CUDA_CALLABLE void normal(vec<T, N> n){		//orient the rectangle along the specified normal

  

  		n = n.norm();								//normalize, just in case

  		vec<T, N> n_current = X.cross(Y).norm();	//compute the current normal

  		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(){

  		C = vec<T, N>(0, 0, 0);

  		X = vec<T, N>(1, 0, 0);

  		Y = vec<T, N>(0, 1, 0);

  	}

  

  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;

  	}

  

eeebe223   David Mayerich   added comments to...
72
  	//create a rectangle from a center point, normal, and size

81e0d221   David Mayerich   separated executa...
73
74
75
76
77
  	CUDA_CALLABLE rect(T size, vec<T, N> c, vec<T, N> n = vec<T, N>(0, 0, 1)){

  		init();			//start with the default setting

  		C = c;

  		scale(size);	//scale the rectangle

  		normal(n);		//orient

81e0d221   David Mayerich   separated executa...
78
  	}

4cefeb6d   Pavel Govyadinov   Changes to the re...
79
80
81
82
83
84
85
86
  	

  	//create a rectangle from a center point, normal, and size

  	CUDA_CALLABLE rect(vec<T,2> size, vec<T, N> c, vec<T, N> n = vec<T, N>(0, 0, 1)){

  		init();			//start with the default setting

  		C = c;

  		scale(size);	//scale the rectangle

  		normal(n);		//orient

  	}

81e0d221   David Mayerich   separated executa...
87
  

4cefeb6d   Pavel Govyadinov   Changes to the re...
88
89
90
91
92
93
94
  	CUDA_CALLABLE rect(vec<T, N> center, vec<T, N> directionX, vec<T, N> directionY )

  	{

  		C = center;

  		X = directionX;

  		Y = directionY;

  	}

  

13c2a7d4   Pavel Govyadinov   some changes to t...
95
96
97
98
99
100
101
  	CUDA_CALLABLE rect(vec<T,N> mag, vec<T, N> center, vec<T, N> directionX, vec<T, N> directionY )

  	{	

  		C = center;

  		X = directionX;

  		Y = directionY;

  		scale(mag[0], mag[1]);

  	}

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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
  

  	//boolean comparison

  	bool operator==(const rect<T, N> & rhs)

  	{

  		if(C == rhs.C && X == rhs.X && Y == rhs.Y)

  			return true;

  		else

  			return false;

  	}

  

  	/*******************************************

  	Return the normal for the rect

  	*******************************************/

  	CUDA_CALLABLE stim::vec<T, N> n()

  	{

          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]

81e0d221   David Mayerich   separated executa...
126
127
128
129
130
131
132
133
134
135
  	CUDA_CALLABLE stim::vec<T, N> p(T a, T b)

  	{

  		stim::vec<T, N> result;

  		//given the two parameters a, b = [0 1], returns the position in world space

  		vec<T, N> A = C - X * (T)0.5 - Y * (T)0.5;

  		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]

81e0d221   David Mayerich   separated executa...
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
  	CUDA_CALLABLE stim::vec<T, N> operator()(T a, T b)

  	{

  		return p(a, b);

  	}

  

  	std::string str()

  	{

  		std::stringstream ss;

  		vec<T, N> A = C - 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();

  

  	}

  

eeebe223   David Mayerich   added comments to...
154
  	//scales the rectangle by a value rhs

81e0d221   David Mayerich   separated executa...
155
156
157
158
159
160
161
162
163
164
165
166
  	CUDA_CALLABLE rect<T, N> operator*(T rhs)

  	{

  		//scales the plane by a scalar value

  

  		//create the new rectangle

  		rect<T, N> result = *this;

  		result.scale(rhs);

  

  		return result;

  

  	}

  

eeebe223   David Mayerich   added comments to...
167
  	//computes the distance between the specified point and this rectangle

81e0d221   David Mayerich   separated executa...
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
  	CUDA_CALLABLE T dist(vec<T, N> p)

  	{

          //compute the distance between a point and this rect

  

  		vec<T, N> A = C - X * (T)0.5 - Y * (T)0.5;

  

          //first break the rect 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)

  	{

  		vec<T, N> A = C - 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, N> R)

  {

      os<<R.str();

      return os;

  }

  

  

  #endif