Blame view

stim/math/rect.h 6.38 KB
2137d771   Pavel Govyadinov   modified plane an...
1
2
  #ifndef STIM_RECT_H

  #define STIM_RECT_H

81e0d221   David Mayerich   separated executa...
3
  

d03b807e   Pavel Govyadinov   fixed all the bug...
4
  

81e0d221   David Mayerich   separated executa...
5
  //enable CUDA_CALLABLE macro

7d1d153a   Pavel Govyadinov   fixed the include...
6
  #include <stim/cuda/cudatools/callable.h>

2137d771   Pavel Govyadinov   modified plane an...
7
  #include <stim/math/plane.h>

cce7daf9   Pavel Govyadinov   added glObj.h to ...
8
9
  #include <stim/math/vector.h>

  #include <stim/math/triangle.h>

81e0d221   David Mayerich   separated executa...
10
11
12
  #include <iostream>

  #include <iomanip>

  #include <algorithm>

d03b807e   Pavel Govyadinov   fixed all the bug...
13
  #include <assert.h>

81e0d221   David Mayerich   separated executa...
14
15
16
17
  

  namespace stim{

  

  //template for a rectangle class in ND space

2137d771   Pavel Govyadinov   modified plane an...
18
  template <typename T>

d03b807e   Pavel Govyadinov   fixed all the bug...
19
  class rect : plane <T>

81e0d221   David Mayerich   separated executa...
20
21
22
23
24
  {

  	/*

  		^                   O

  		|                   

  		|                   

2137d771   Pavel Govyadinov   modified plane an...
25
  		Y         P         

81e0d221   David Mayerich   separated executa...
26
27
28
29
30
  		|                   

  		|                   

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

  	*/

  

8309b07a   David Mayerich   fixed some vec3 e...
31
  protected:

81e0d221   David Mayerich   separated executa...
32
  

8309b07a   David Mayerich   fixed some vec3 e...
33
34
  	stim::vec3<T> X;

  	stim::vec3<T> Y;

81e0d221   David Mayerich   separated executa...
35
36
37
  

  public:

  

d03b807e   Pavel Govyadinov   fixed all the bug...
38
39
40
41
42
43
  	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;

  

3f15dade   Pavel Govyadinov   changed the plane...
44
  	///base constructor.

2137d771   Pavel Govyadinov   modified plane an...
45
46
47
  	CUDA_CALLABLE rect()

  	 : plane<T>()

  	{

81e0d221   David Mayerich   separated executa...
48
49
50
  		init();

  	}

  

3f15dade   Pavel Govyadinov   changed the plane...
51
52
53
  	///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.

2137d771   Pavel Govyadinov   modified plane an...
54
55
56
  	CUDA_CALLABLE rect(T size, T z_pos = (T)0)

  	 : plane<T>(z_pos)

  	{

81e0d221   David Mayerich   separated executa...
57
58
  		init();			//use the default setup

  		scale(size);	//scale the rectangle

81e0d221   David Mayerich   separated executa...
59
60
  	}

  

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

3f15dade   Pavel Govyadinov   changed the plane...
62
63
64
  	///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.

8309b07a   David Mayerich   fixed some vec3 e...
65
  	CUDA_CALLABLE rect(vec3<T> c, vec3<T> n = vec3<T>(0, 0, 1))

591f04ec   Pavel Govyadinov   fixed a very subt...
66
  		: plane<T>()

2137d771   Pavel Govyadinov   modified plane an...
67
  	{

4cefeb6d   Pavel Govyadinov   Changes to the re...
68
  		init();			//start with the default setting

4cefeb6d   Pavel Govyadinov   Changes to the re...
69
70
  		normal(n);		//orient

  	}

81e0d221   David Mayerich   separated executa...
71
  

3f15dade   Pavel Govyadinov   changed the plane...
72
73
74
75
  	///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.

8309b07a   David Mayerich   fixed some vec3 e...
76
  	CUDA_CALLABLE rect(vec3<T> c, T s, vec3<T> n = vec3<T>(0, 0, 1))

591f04ec   Pavel Govyadinov   fixed a very subt...
77
  		: plane<T>()

2137d771   Pavel Govyadinov   modified plane an...
78
  	{

3f15dade   Pavel Govyadinov   changed the plane...
79
  		init();			//start with the default setting

3f15dade   Pavel Govyadinov   changed the plane...
80
  		scale(s);

591f04ec   Pavel Govyadinov   fixed a very subt...
81
82
  		center(c);

  		rotate(n, X, Y);

3f15dade   Pavel Govyadinov   changed the plane...
83
84
85
86
87
88
  	}

  

  	///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.

8309b07a   David Mayerich   fixed some vec3 e...
89
  	CUDA_CALLABLE rect(vec3<T> center, vec3<T> directionX, vec3<T> directionY )

d03b807e   Pavel Govyadinov   fixed all the bug...
90
  		 : plane<T>((directionX.cross(directionY)).norm(),center)

4cefeb6d   Pavel Govyadinov   Changes to the re...
91
  	{

4cefeb6d   Pavel Govyadinov   Changes to the re...
92
93
94
95
  		X = directionX;

  		Y = directionY;

  	}

  

3f15dade   Pavel Govyadinov   changed the plane...
96
97
98
99
100
  	///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.

8309b07a   David Mayerich   fixed some vec3 e...
101
  	CUDA_CALLABLE rect(T size, vec3<T> center, vec3<T> directionX, vec3<T> directionY )

d03b807e   Pavel Govyadinov   fixed all the bug...
102
  		: plane<T>((directionX.cross(directionY)).norm(),center)

13c2a7d4   Pavel Govyadinov   some changes to t...
103
  	{	

13c2a7d4   Pavel Govyadinov   some changes to t...
104
105
  		X = directionX;

  		Y = directionY;

5f81932b   David Mayerich   restored Pavel's ...
106
107
  		scale(size);

  	}

3f15dade   Pavel Govyadinov   changed the plane...
108
109
110
111
112
113
  	

  	///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.

8309b07a   David Mayerich   fixed some vec3 e...
114
  	CUDA_CALLABLE rect(vec3<T> size, vec3<T> center, vec3<T> directionX, vec3<T> directionY)

d03b807e   Pavel Govyadinov   fixed all the bug...
115
  		: plane<T>((directionX.cross(directionY)).norm(), center)

5f81932b   David Mayerich   restored Pavel's ...
116
  	{	

5f81932b   David Mayerich   restored Pavel's ...
117
118
119
  		X = directionX;

  		Y = directionY;

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

13c2a7d4   Pavel Govyadinov   some changes to t...
120
  	}

3f15dade   Pavel Govyadinov   changed the plane...
121
  

5b6c317a   Pavel Govyadinov   implemented the c...
122
123
124
125
126
  	CUDA_CALLABLE void scale(T factor){

  		X *= factor;

  		Y *= factor;

  	}

  

3f15dade   Pavel Govyadinov   changed the plane...
127
128
129
  	///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.	

2137d771   Pavel Govyadinov   modified plane an...
130
131
  	CUDA_CALLABLE void scale(T factor1, T factor2)

  	{

4cefeb6d   Pavel Govyadinov   Changes to the re...
132
133
134
  		X *= factor1;

  		Y *= factor2;

  	}

81e0d221   David Mayerich   separated executa...
135
  

3f15dade   Pavel Govyadinov   changed the plane...
136
137
  	///@param n; vector with the normal.

  	///Orients the rectangle along the normal n.

8309b07a   David Mayerich   fixed some vec3 e...
138
  	CUDA_CALLABLE void normal(vec3<T> n)

2137d771   Pavel Govyadinov   modified plane an...
139
140
141
  	{	

  		//orient the rectangle along the specified normal

  		rotate(n, X, Y);

3f15dade   Pavel Govyadinov   changed the plane...
142
143
144
  	}

  

  	///general init method that sets a general rectangle.

2137d771   Pavel Govyadinov   modified plane an...
145
146
  	CUDA_CALLABLE void init()

  	{

8309b07a   David Mayerich   fixed some vec3 e...
147
148
  		X = vec3<T>(1, 0, 0);

  		Y = vec3<T>(0, 1, 0);

3f15dade   Pavel Govyadinov   changed the plane...
149
150
  	}

  

81e0d221   David Mayerich   separated executa...
151
  	//boolean comparison

5eeaf941   Pavel Govyadinov   changer to the ba...
152
  	bool operator==(const rect<T> & rhs)

81e0d221   David Mayerich   separated executa...
153
  	{

2137d771   Pavel Govyadinov   modified plane an...
154
  		if(P == rhs.P && X == rhs.X && Y == rhs.Y)

81e0d221   David Mayerich   separated executa...
155
156
157
158
159
  			return true;

  		else

  			return false;

  	}

  

81e0d221   David Mayerich   separated executa...
160
  

eeebe223   David Mayerich   added comments to...
161
  	//get the world space value given the planar coordinates a, b in [0, 1]

8309b07a   David Mayerich   fixed some vec3 e...
162
  	CUDA_CALLABLE stim::vec3<T> p(T a, T b)

81e0d221   David Mayerich   separated executa...
163
  	{

8309b07a   David Mayerich   fixed some vec3 e...
164
  		stim::vec3<T> result;

81e0d221   David Mayerich   separated executa...
165
  		//given the two parameters a, b = [0 1], returns the position in world space

8309b07a   David Mayerich   fixed some vec3 e...
166
  		vec3<T> A = this->P - X * (T)0.5 - Y * (T)0.5;

81e0d221   David Mayerich   separated executa...
167
168
169
170
171
  		result = A + X * a + Y * b;

  

  		return result;

  	}

  

eeebe223   David Mayerich   added comments to...
172
  	//parenthesis operator returns the world space given rectangular coordinates a and b in [0 1]

8309b07a   David Mayerich   fixed some vec3 e...
173
  	CUDA_CALLABLE stim::vec3<T> operator()(T a, T b)

81e0d221   David Mayerich   separated executa...
174
175
176
177
178
179
180
  	{

  		return p(a, b);

  	}

  

  	std::string str()

  	{

  		std::stringstream ss;

8309b07a   David Mayerich   fixed some vec3 e...
181
  		vec3<T> A = P - X * (T)0.5 - Y * (T)0.5;

81e0d221   David Mayerich   separated executa...
182
183
184
185
  		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;

  

8309b07a   David Mayerich   fixed some vec3 e...
186
          return ss.str();

81e0d221   David Mayerich   separated executa...
187
188
189
  

  	}

  

3f15dade   Pavel Govyadinov   changed the plane...
190
  	///multiplication operator scales the rectangle by a value rhs.

5eeaf941   Pavel Govyadinov   changer to the ba...
191
  	CUDA_CALLABLE rect<T> operator*(T rhs)

81e0d221   David Mayerich   separated executa...
192
193
194
195
  	{

  		//scales the plane by a scalar value

  

  		//create the new rectangle

5eeaf941   Pavel Govyadinov   changer to the ba...
196
  		rect<T> result = *this;

81e0d221   David Mayerich   separated executa...
197
198
199
200
201
202
  		result.scale(rhs);

  

  		return result;

  

  	}

  

3f15dade   Pavel Govyadinov   changed the plane...
203
204
  	///computes the distance between the specified point and this rectangle.

  	///@param p: x, y, z coordinates of the point to calculate distance to.

8309b07a   David Mayerich   fixed some vec3 e...
205
  	CUDA_CALLABLE T dist(vec3<T> p)

81e0d221   David Mayerich   separated executa...
206
207
208
  	{

          //compute the distance between a point and this rect

  

8309b07a   David Mayerich   fixed some vec3 e...
209
  		vec3<T> A = P - X * (T)0.5 - Y * (T)0.5;

81e0d221   David Mayerich   separated executa...
210
  

2137d771   Pavel Govyadinov   modified plane an...
211
212
213
  		//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);

81e0d221   David Mayerich   separated executa...
214
215
  

  

2137d771   Pavel Govyadinov   modified plane an...
216
217
  		T d0 = T0.dist(p);

  		T d1 = T1.dist(p);

81e0d221   David Mayerich   separated executa...
218
  

2137d771   Pavel Govyadinov   modified plane an...
219
220
221
222
  		if(d0 < d1)

  		    return d0;

  		else

  		    return d1;

81e0d221   David Mayerich   separated executa...
223
224
  	}

  

8309b07a   David Mayerich   fixed some vec3 e...
225
  	CUDA_CALLABLE T center(vec3<T> p)

3f15dade   Pavel Govyadinov   changed the plane...
226
  	{

d03b807e   Pavel Govyadinov   fixed all the bug...
227
  		this->P = p;

3f15dade   Pavel Govyadinov   changed the plane...
228
229
230
231
  	}

  

  	///Returns the maximum distance of the rectangle from a point p to the sides of the rectangle.

  	///@param p: x, y, z point.

8309b07a   David Mayerich   fixed some vec3 e...
232
  	CUDA_CALLABLE T dist_max(vec3<T> p)

81e0d221   David Mayerich   separated executa...
233
  	{

8309b07a   David Mayerich   fixed some vec3 e...
234
  		vec3<T> A = P - X * (T)0.5 - Y * (T)0.5;

2137d771   Pavel Govyadinov   modified plane an...
235
236
237
238
  		T da = (A - p).len();

  		T db = (A+X - p).len();

  		T dc = (A+Y - p).len();

  		T dd = (A+X+Y - p).len();

81e0d221   David Mayerich   separated executa...
239
  

2137d771   Pavel Govyadinov   modified plane an...
240
  		return std::max( da, std::max(db, std::max(dc, dd) ) );

81e0d221   David Mayerich   separated executa...
241
242
243
244
245
246
  	}

  };

  

  }	//end namespace rts

  

  template <typename T, int N>

5eeaf941   Pavel Govyadinov   changer to the ba...
247
  std::ostream& operator<<(std::ostream& os, stim::rect<T> R)

81e0d221   David Mayerich   separated executa...
248
249
250
251
252
253
254
  {

      os<<R.str();

      return os;

  }

  

  

  #endif