Blame view

stim/math/rect.h 6.36 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
31
32
  		|                   

  		|                   

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

  	*/

  

  private:

  

5eeaf941   Pavel Govyadinov   changer to the ba...
33
34
  	stim::vec<T> X;

  	stim::vec<T> Y;

81e0d221   David Mayerich   separated executa...
35
  

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

81e0d221   David Mayerich   separated executa...
37
  

81e0d221   David Mayerich   separated executa...
38
39
40
  

  public:

  

d03b807e   Pavel Govyadinov   fixed all the bug...
41
42
43
44
45
46
  	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...
47
  	///base constructor.

2137d771   Pavel Govyadinov   modified plane an...
48
49
50
  	CUDA_CALLABLE rect()

  	 : plane<T>()

  	{

81e0d221   David Mayerich   separated executa...
51
52
53
  		init();

  	}

  

3f15dade   Pavel Govyadinov   changed the plane...
54
55
56
  	///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...
57
58
59
  	CUDA_CALLABLE rect(T size, T z_pos = (T)0)

  	 : plane<T>(z_pos)

  	{

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

  		scale(size);	//scale the rectangle

81e0d221   David Mayerich   separated executa...
62
63
  	}

  

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

3f15dade   Pavel Govyadinov   changed the plane...
65
66
67
  	///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.

2137d771   Pavel Govyadinov   modified plane an...
68
  	CUDA_CALLABLE rect(vec<T> c, vec<T> n = vec<T>(0, 0, 1))

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

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

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

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

  	}

81e0d221   David Mayerich   separated executa...
74
  

3f15dade   Pavel Govyadinov   changed the plane...
75
76
77
78
  	///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.

2137d771   Pavel Govyadinov   modified plane an...
79
  	CUDA_CALLABLE rect(vec<T> c, T s, vec<T> n = vec<T>(0, 0, 1))

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

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

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

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

591f04ec   Pavel Govyadinov   fixed a very subt...
84
85
  		center(c);

  		rotate(n, X, Y);

3f15dade   Pavel Govyadinov   changed the plane...
86
87
88
89
90
91
  	}

  

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

5eeaf941   Pavel Govyadinov   changer to the ba...
92
  	CUDA_CALLABLE rect(vec<T> center, vec<T> directionX, vec<T> directionY )

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

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

4cefeb6d   Pavel Govyadinov   Changes to the re...
95
96
97
98
  		X = directionX;

  		Y = directionY;

  	}

  

3f15dade   Pavel Govyadinov   changed the plane...
99
100
101
102
103
  	///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.

5eeaf941   Pavel Govyadinov   changer to the ba...
104
  	CUDA_CALLABLE rect(T size, vec<T> center, vec<T> directionX, vec<T> directionY )

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

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

13c2a7d4   Pavel Govyadinov   some changes to t...
107
108
  		X = directionX;

  		Y = directionY;

5f81932b   David Mayerich   restored Pavel's ...
109
110
  		scale(size);

  	}

3f15dade   Pavel Govyadinov   changed the plane...
111
112
113
114
115
116
  	

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

d03b807e   Pavel Govyadinov   fixed all the bug...
117
118
  	CUDA_CALLABLE rect(vec<T> size, vec<T> center, vec<T> directionX, vec<T> directionY)

  		: plane<T>((directionX.cross(directionY)).norm(), center)

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

5f81932b   David Mayerich   restored Pavel's ...
120
121
122
  		X = directionX;

  		Y = directionY;

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

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

3f15dade   Pavel Govyadinov   changed the plane...
124
  

5b6c317a   Pavel Govyadinov   implemented the c...
125
126
127
128
129
  	CUDA_CALLABLE void scale(T factor){

  		X *= factor;

  		Y *= factor;

  	}

  

3f15dade   Pavel Govyadinov   changed the plane...
130
131
132
  	///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...
133
134
  	CUDA_CALLABLE void scale(T factor1, T factor2)

  	{

4cefeb6d   Pavel Govyadinov   Changes to the re...
135
136
137
  		X *= factor1;

  		Y *= factor2;

  	}

81e0d221   David Mayerich   separated executa...
138
  

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

  	///Orients the rectangle along the normal n.

2137d771   Pavel Govyadinov   modified plane an...
141
142
143
144
  	CUDA_CALLABLE void normal(vec<T> n)

  	{	

  		//orient the rectangle along the specified normal

  		rotate(n, X, Y);

3f15dade   Pavel Govyadinov   changed the plane...
145
146
147
  	}

  

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

2137d771   Pavel Govyadinov   modified plane an...
148
149
  	CUDA_CALLABLE void init()

  	{

3f15dade   Pavel Govyadinov   changed the plane...
150
151
152
153
  		X = vec<T>(1, 0, 0);

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

  	}

  

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

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

81e0d221   David Mayerich   separated executa...
156
  	{

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

81e0d221   David Mayerich   separated executa...
158
159
160
161
162
  			return true;

  		else

  			return false;

  	}

  

81e0d221   David Mayerich   separated executa...
163
  

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

5eeaf941   Pavel Govyadinov   changer to the ba...
165
  	CUDA_CALLABLE stim::vec<T> p(T a, T b)

81e0d221   David Mayerich   separated executa...
166
  	{

5eeaf941   Pavel Govyadinov   changer to the ba...
167
  		stim::vec<T> result;

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

d03b807e   Pavel Govyadinov   fixed all the bug...
169
  		vec<T> A = this->P - X * (T)0.5 - Y * (T)0.5;

81e0d221   David Mayerich   separated executa...
170
171
172
173
174
  		result = A + X * a + Y * b;

  

  		return result;

  	}

  

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

5eeaf941   Pavel Govyadinov   changer to the ba...
176
  	CUDA_CALLABLE stim::vec<T> operator()(T a, T b)

81e0d221   David Mayerich   separated executa...
177
178
179
180
181
182
183
  	{

  		return p(a, b);

  	}

  

  	std::string str()

  	{

  		std::stringstream ss;

2137d771   Pavel Govyadinov   modified plane an...
184
  		vec<T> A = P - X * (T)0.5 - Y * (T)0.5;

81e0d221   David Mayerich   separated executa...
185
186
187
188
  		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;

  

2137d771   Pavel Govyadinov   modified plane an...
189
          	return ss.str();

81e0d221   David Mayerich   separated executa...
190
191
192
  

  	}

  

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

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

81e0d221   David Mayerich   separated executa...
195
196
197
198
  	{

  		//scales the plane by a scalar value

  

  		//create the new rectangle

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

81e0d221   David Mayerich   separated executa...
200
201
202
203
204
205
  		result.scale(rhs);

  

  		return result;

  

  	}

  

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

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

5eeaf941   Pavel Govyadinov   changer to the ba...
208
  	CUDA_CALLABLE T dist(vec<T> p)

81e0d221   David Mayerich   separated executa...
209
210
211
  	{

          //compute the distance between a point and this rect

  

2137d771   Pavel Govyadinov   modified plane an...
212
  		vec<T> A = P - X * (T)0.5 - Y * (T)0.5;

81e0d221   David Mayerich   separated executa...
213
  

2137d771   Pavel Govyadinov   modified plane an...
214
215
216
  		//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...
217
218
  

  

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

  		T d1 = T1.dist(p);

81e0d221   David Mayerich   separated executa...
221
  

2137d771   Pavel Govyadinov   modified plane an...
222
223
224
225
  		if(d0 < d1)

  		    return d0;

  		else

  		    return d1;

81e0d221   David Mayerich   separated executa...
226
227
  	}

  

3f15dade   Pavel Govyadinov   changed the plane...
228
229
  	CUDA_CALLABLE T center(vec<T> p)

  	{

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

3f15dade   Pavel Govyadinov   changed the plane...
231
232
233
234
  	}

  

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

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

5eeaf941   Pavel Govyadinov   changer to the ba...
235
  	CUDA_CALLABLE T dist_max(vec<T> p)

81e0d221   David Mayerich   separated executa...
236
  	{

2137d771   Pavel Govyadinov   modified plane an...
237
238
239
240
241
  		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();

81e0d221   David Mayerich   separated executa...
242
  

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

81e0d221   David Mayerich   separated executa...
244
245
246
247
248
249
  	}

  };

  

  }	//end namespace rts

  

  template <typename T, int N>

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

81e0d221   David Mayerich   separated executa...
251
252
253
254
255
256
257
  {

      os<<R.str();

      return os;

  }

  

  

  #endif