Blame view

stim/math/circle.h 5.35 KB
e24ed5a6   Pavel Govyadinov   circle added
1
2
3
  #ifndef STIM_CIRCLE_H
  #define STIM_CIRCLE_H
  
e24ed5a6   Pavel Govyadinov   circle added
4
  #include <stim/cuda/cudatools/callable.h>
26aee9ed   Pavel Govyadinov   changed circle cl...
5
  #include <stim/math/plane.h>
e24ed5a6   Pavel Govyadinov   circle added
6
7
  #include <stim/math/vector.h>
  #include <stim/math/triangle.h>
98eecaa9   David Mayerich   VS and win32 updates
8
  #include <stim/math/constants.h>
26aee9ed   Pavel Govyadinov   changed circle cl...
9
  #include <assert.h>
e24ed5a6   Pavel Govyadinov   circle added
10
  #include <algorithm>
26aee9ed   Pavel Govyadinov   changed circle cl...
11
  #include <iostream>
e24ed5a6   Pavel Govyadinov   circle added
12
  
26aee9ed   Pavel Govyadinov   changed circle cl...
13
  namespace stim{
e24ed5a6   Pavel Govyadinov   circle added
14
  
26aee9ed   Pavel Govyadinov   changed circle cl...
15
16
  template <typename T>
  class circle : plane<T>
e24ed5a6   Pavel Govyadinov   circle added
17
  {
e24ed5a6   Pavel Govyadinov   circle added
18
  
26aee9ed   Pavel Govyadinov   changed circle cl...
19
20
  private:
  	
e21d1051   David Mayerich   cylinder can buil...
21
22
  	//stim::vec3<T> Y;
  	T R;								//radius of the circle
e24ed5a6   Pavel Govyadinov   circle added
23
  
e21d1051   David Mayerich   cylinder can buil...
24
  	/*CUDA_CALLABLE void
26aee9ed   Pavel Govyadinov   changed circle cl...
25
26
27
  	init()
  	{
  		Y = U.cross(N).norm();
e21d1051   David Mayerich   cylinder can buil...
28
  	}*/
e24ed5a6   Pavel Govyadinov   circle added
29
  
26aee9ed   Pavel Govyadinov   changed circle cl...
30
31
32
33
34
35
36
  public:
  	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;
  	using stim::plane<T>::setU;
e24ed5a6   Pavel Govyadinov   circle added
37
  
ee17b90b   Jiaming Guo   make it compatibl...
38
39
  	using stim::plane<T>::init;
  
26aee9ed   Pavel Govyadinov   changed circle cl...
40
41
42
43
44
45
46
  	///base constructor
  	///@param th value of the angle of the starting point from 0 to 360.
  	CUDA_CALLABLE
  	circle() : plane<T>()
  	{
  		init();
  	}
e24ed5a6   Pavel Govyadinov   circle added
47
  
e21d1051   David Mayerich   cylinder can buil...
48
  	///create a circle given a size and position in Z space.
26aee9ed   Pavel Govyadinov   changed circle cl...
49
50
51
  	///@param size: size of the rectangle in ND space.
  	///@param z_pos z coordinate of the rectangle.
  	CUDA_CALLABLE
e21d1051   David Mayerich   cylinder can buil...
52
  	circle(T radius, T z_pos = (T)0) : plane<T>(z_pos)
26aee9ed   Pavel Govyadinov   changed circle cl...
53
  	{
e21d1051   David Mayerich   cylinder can buil...
54
55
56
57
  		//center(stim::vec3<T>(0, 0, z_pos));
  		//scale(size);
  		//init();
  		R = radius;
26aee9ed   Pavel Govyadinov   changed circle cl...
58
59
60
61
62
63
  	}
  
  	///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.	
  	CUDA_CALLABLE
e21d1051   David Mayerich   cylinder can buil...
64
  	circle(vec3<T> c, vec3<T> n = vec3<T>(0,0,1)) : plane<T>(n, c)
26aee9ed   Pavel Govyadinov   changed circle cl...
65
  	{
e21d1051   David Mayerich   cylinder can buil...
66
67
68
69
  		//center(c);
  		//normal(n);
  		//init();
  		R = (T)1;
26aee9ed   Pavel Govyadinov   changed circle cl...
70
71
  	}
  
4252d827   David Mayerich   ivote3 fixes and ...
72
  	/*///create a rectangle from a center point, normal, and size
26aee9ed   Pavel Govyadinov   changed circle cl...
73
74
75
76
  	///@param c: x,y,z location of the center.
  	///@param s: size of the rectangle.
  	///@param n: x,y,z direction of the normal.
  	CUDA_CALLABLE 
308a743c   David Mayerich   fixed class compa...
77
  	circle(vec3<T> c, T s, vec3<T> n = vec3<T>(0,0,1)) : plane<T>()
26aee9ed   Pavel Govyadinov   changed circle cl...
78
79
80
81
82
83
  	{
  		init();
  		center(c);
  		rotate(n, U, Y);
  		scale(s);
  	}
4252d827   David Mayerich   ivote3 fixes and ...
84
  	*/
26aee9ed   Pavel Govyadinov   changed circle cl...
85
86
87
88
89
90
91
  
  	///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.
  	///@param u: x,y,z direction for the zero vector (from where the rotation starts)
  	CUDA_CALLABLE
e21d1051   David Mayerich   cylinder can buil...
92
  	circle(vec3<T> c, T r, vec3<T> n = vec3<T>(0,0,1), vec3<T> u = vec3<T>(1, 0, 0)) : plane<T>()
26aee9ed   Pavel Govyadinov   changed circle cl...
93
  	{
e21d1051   David Mayerich   cylinder can buil...
94
95
  		P = c;
  		N = n;
26aee9ed   Pavel Govyadinov   changed circle cl...
96
  		setU(u);
e21d1051   David Mayerich   cylinder can buil...
97
98
99
  		R = r;
  		//init();
  		//setU(u);
0311ab81   Pavel Govyadinov   fixed some issues...
100
  //		U = u;
e21d1051   David Mayerich   cylinder can buil...
101
102
103
  		//center(c);
  		//normal(n);
  		//scale(s);
26aee9ed   Pavel Govyadinov   changed circle cl...
104
105
106
107
108
109
110
  	}
  
  	///scales the circle by a certain factor
  	///@param factor: the factor by which the dimensions of the shape are scaled.
  	CUDA_CALLABLE
  	void scale(T factor)
  	{
e21d1051   David Mayerich   cylinder can buil...
111
112
113
  		//U *= factor;
  		//Y *= factor;
  		R *= factor;
26aee9ed   Pavel Govyadinov   changed circle cl...
114
115
  	}
  
6b317c71   Jiaming Guo   add function to r...
116
117
118
119
120
121
122
123
  	///set the radius of circle to a certain value 
  	///@param value: the new radius of the circle
  	CUDA_CALLABLE
  	void set_R(T value) 
  	{
  		R = value;
  	}
  
26aee9ed   Pavel Govyadinov   changed circle cl...
124
125
126
  	///sets the normal for the cirlce
  	///@param n: x,y,z direction of the normal.
  	CUDA_CALLABLE void
e21d1051   David Mayerich   cylinder can buil...
127
128
  	normal(vec3<T> n){
  		rotate(n);
26aee9ed   Pavel Govyadinov   changed circle cl...
129
  	}
e24ed5a6   Pavel Govyadinov   circle added
130
  
26aee9ed   Pavel Govyadinov   changed circle cl...
131
132
  	///sets the center of the circle.
  	///@param n: x,y,z location of the center.
308a743c   David Mayerich   fixed class compa...
133
134
  	CUDA_CALLABLE void
  	center(vec3<T> p){
e21d1051   David Mayerich   cylinder can buil...
135
  		P = p;
26aee9ed   Pavel Govyadinov   changed circle cl...
136
137
138
139
140
141
  	}
  
  	///boolean comparison
  	bool
  	operator==(const circle<T> & rhs)
  	{
ee17b90b   Jiaming Guo   make it compatibl...
142
  		if(P == rhs.P && U == rhs.U)
26aee9ed   Pavel Govyadinov   changed circle cl...
143
144
145
146
147
  			return true;
  		else
  			return false;
  	}
  
e21d1051   David Mayerich   cylinder can buil...
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
  	//returns the point in world space corresponding to the polar coordinate (r, theta)
  	CUDA_CALLABLE stim::vec3<T>
  		p(T r, T theta) {
  		T u = r * cos(theta);				//calculate the coordinates in the planar space defined by the circle
  		T v = r * sin(theta);
  
  		vec3<T> V = U.cross(N);				//calculate the orthogonal vector V
  		return P + U * u + V * v;			//calculate the cartesian coordinate of the specified point
  	}
  
  	//returns the point in world space corresponding to the value theta at radius R
  	CUDA_CALLABLE stim::vec3<T>
  		p(T theta) {
  		return p(R, theta);
  	}
  
  	//get the world space value given the polar coordinates (r, theta)
  
26aee9ed   Pavel Govyadinov   changed circle cl...
166
  	///get the world space value given the planar coordinates a, b in [0, 1]
e21d1051   David Mayerich   cylinder can buil...
167
  	/*CUDA_CALLABLE stim::vec3<T> p(T a, T b)
26aee9ed   Pavel Govyadinov   changed circle cl...
168
  	{
308a743c   David Mayerich   fixed class compa...
169
  		stim::vec3<T> result;
26aee9ed   Pavel Govyadinov   changed circle cl...
170
  
308a743c   David Mayerich   fixed class compa...
171
  		vec3<T> A = this->P - this->U * (T)0.5 - Y * (T)0.5;
26aee9ed   Pavel Govyadinov   changed circle cl...
172
173
  		result = A + this->U * a + Y * b;
  		return result;
e21d1051   David Mayerich   cylinder can buil...
174
  	}*/
26aee9ed   Pavel Govyadinov   changed circle cl...
175
176
  
  	///parenthesis operator returns the world space given rectangular coordinates a and b in [0 1]
e21d1051   David Mayerich   cylinder can buil...
177
  	CUDA_CALLABLE stim::vec3<T> operator()(T r, T theta)
26aee9ed   Pavel Govyadinov   changed circle cl...
178
  	{
e21d1051   David Mayerich   cylinder can buil...
179
180
181
182
183
184
  		return p(r, theta);
  	}
  
  	//parenthesis operator returns the world space coordinate at the edge of the circle given theta
  	CUDA_CALLABLE stim::vec3<T> operator()(T theta) {
  		return p(theta);
26aee9ed   Pavel Govyadinov   changed circle cl...
185
186
187
188
189
  	}
  
  	///returns a vector with the points on the initialized circle.
  	///connecting the points results in a circle.
  	///@param n: integer for the number of points representing the circle.
e21d1051   David Mayerich   cylinder can buil...
190
191
192
193
194
195
  	std::vector< stim::vec3<T> > points(unsigned n) {
  		std::vector< stim::vec3<T> > result(n);				//initialize a vector of n points
  
  		float dt = stim::TAU / n;
  		for (unsigned i = 0; i < n; i++)
  			result[i] = p(i * dt);							//calculate a point on the edge of the circle
26aee9ed   Pavel Govyadinov   changed circle cl...
196
197
  		return result;
  	}	
e24ed5a6   Pavel Govyadinov   circle added
198
  
30b20d4f   Jiaming Guo   make it render be...
199
200
201
202
203
204
205
206
207
208
209
210
211
  	///returns a vector with the points on the initialized circle
  	///connecting the points results in a circle
  	///@param n: integer for the number of points representing the circle
  	///the only difference between points and glpoints is that the first point appears twice in the returning lists
  	std::vector< stim::vec3<T> > glpoints(unsigned n) {
  		std::vector< stim::vec3<T> > result(n + 1);
  		float dt = stim::TAU / n;
  		for (unsigned i = 0; i < n; i++)
  			result[i] = p(i * dt);
  		result[n] = p(0);									//close the circle!
  		return result;
  	}
  	
0311ab81   Pavel Govyadinov   fixed some issues...
212
213
214
  	std::string str() const
  	{
  		std::stringstream ss;
e21d1051   David Mayerich   cylinder can buil...
215
  		ss << "r = "<<R<<"  (P=" << P.str() << ", N=" << N.str() << ", U=" << U.str() << ")";
0311ab81   Pavel Govyadinov   fixed some issues...
216
217
218
  		return ss.str();
  	}
  
26aee9ed   Pavel Govyadinov   changed circle cl...
219
  };
e24ed5a6   Pavel Govyadinov   circle added
220
  }
e24ed5a6   Pavel Govyadinov   circle added
221
  #endif