Blame view

stim/math/circle.h 4.65 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
  
26aee9ed   Pavel Govyadinov   changed circle cl...
38
39
40
41
42
43
44
  	///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
45
  
e21d1051   David Mayerich   cylinder can buil...
46
  	///create a circle given a size and position in Z space.
26aee9ed   Pavel Govyadinov   changed circle cl...
47
48
49
  	///@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...
50
  	circle(T radius, T z_pos = (T)0) : plane<T>(z_pos)
26aee9ed   Pavel Govyadinov   changed circle cl...
51
  	{
e21d1051   David Mayerich   cylinder can buil...
52
53
54
55
  		//center(stim::vec3<T>(0, 0, z_pos));
  		//scale(size);
  		//init();
  		R = radius;
26aee9ed   Pavel Govyadinov   changed circle cl...
56
57
58
59
60
61
  	}
  
  	///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...
62
  	circle(vec3<T> c, vec3<T> n = vec3<T>(0,0,1)) : plane<T>(n, c)
26aee9ed   Pavel Govyadinov   changed circle cl...
63
  	{
e21d1051   David Mayerich   cylinder can buil...
64
65
66
67
  		//center(c);
  		//normal(n);
  		//init();
  		R = (T)1;
26aee9ed   Pavel Govyadinov   changed circle cl...
68
69
  	}
  
4252d827   David Mayerich   ivote3 fixes and ...
70
  	/*///create a rectangle from a center point, normal, and size
26aee9ed   Pavel Govyadinov   changed circle cl...
71
72
73
74
  	///@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...
75
  	circle(vec3<T> c, T s, vec3<T> n = vec3<T>(0,0,1)) : plane<T>()
26aee9ed   Pavel Govyadinov   changed circle cl...
76
77
78
79
80
81
  	{
  		init();
  		center(c);
  		rotate(n, U, Y);
  		scale(s);
  	}
4252d827   David Mayerich   ivote3 fixes and ...
82
  	*/
26aee9ed   Pavel Govyadinov   changed circle cl...
83
84
85
86
87
88
89
  
  	///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...
90
  	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...
91
  	{
e21d1051   David Mayerich   cylinder can buil...
92
93
  		P = c;
  		N = n;
26aee9ed   Pavel Govyadinov   changed circle cl...
94
  		setU(u);
e21d1051   David Mayerich   cylinder can buil...
95
96
97
  		R = r;
  		//init();
  		//setU(u);
0311ab81   Pavel Govyadinov   fixed some issues...
98
  //		U = u;
e21d1051   David Mayerich   cylinder can buil...
99
100
101
  		//center(c);
  		//normal(n);
  		//scale(s);
26aee9ed   Pavel Govyadinov   changed circle cl...
102
103
104
105
106
107
108
  	}
  
  	///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...
109
110
111
  		//U *= factor;
  		//Y *= factor;
  		R *= factor;
26aee9ed   Pavel Govyadinov   changed circle cl...
112
113
114
115
116
  	}
  
  	///sets the normal for the cirlce
  	///@param n: x,y,z direction of the normal.
  	CUDA_CALLABLE void
e21d1051   David Mayerich   cylinder can buil...
117
118
  	normal(vec3<T> n){
  		rotate(n);
26aee9ed   Pavel Govyadinov   changed circle cl...
119
  	}
e24ed5a6   Pavel Govyadinov   circle added
120
  
26aee9ed   Pavel Govyadinov   changed circle cl...
121
122
  	///sets the center of the circle.
  	///@param n: x,y,z location of the center.
308a743c   David Mayerich   fixed class compa...
123
124
  	CUDA_CALLABLE void
  	center(vec3<T> p){
e21d1051   David Mayerich   cylinder can buil...
125
  		P = p;
26aee9ed   Pavel Govyadinov   changed circle cl...
126
127
128
129
130
131
132
133
134
135
136
137
  	}
  
  	///boolean comparison
  	bool
  	operator==(const circle<T> & rhs)
  	{
  		if(P == rhs.P && U == rhs.U && Y == rhs.Y)
  			return true;
  		else
  			return false;
  	}
  
e21d1051   David Mayerich   cylinder can buil...
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
  	//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...
156
  	///get the world space value given the planar coordinates a, b in [0, 1]
e21d1051   David Mayerich   cylinder can buil...
157
  	/*CUDA_CALLABLE stim::vec3<T> p(T a, T b)
26aee9ed   Pavel Govyadinov   changed circle cl...
158
  	{
308a743c   David Mayerich   fixed class compa...
159
  		stim::vec3<T> result;
26aee9ed   Pavel Govyadinov   changed circle cl...
160
  
308a743c   David Mayerich   fixed class compa...
161
  		vec3<T> A = this->P - this->U * (T)0.5 - Y * (T)0.5;
26aee9ed   Pavel Govyadinov   changed circle cl...
162
163
  		result = A + this->U * a + Y * b;
  		return result;
e21d1051   David Mayerich   cylinder can buil...
164
  	}*/
26aee9ed   Pavel Govyadinov   changed circle cl...
165
166
  
  	///parenthesis operator returns the world space given rectangular coordinates a and b in [0 1]
e21d1051   David Mayerich   cylinder can buil...
167
  	CUDA_CALLABLE stim::vec3<T> operator()(T r, T theta)
26aee9ed   Pavel Govyadinov   changed circle cl...
168
  	{
e21d1051   David Mayerich   cylinder can buil...
169
170
171
172
173
174
  		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...
175
176
177
178
179
  	}
  
  	///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...
180
181
182
183
184
185
  	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...
186
187
188
  		return result;
  	}	
  	
e21d1051   David Mayerich   cylinder can buil...
189
  	
e24ed5a6   Pavel Govyadinov   circle added
190
  
0311ab81   Pavel Govyadinov   fixed some issues...
191
192
193
  	std::string str() const
  	{
  		std::stringstream ss;
e21d1051   David Mayerich   cylinder can buil...
194
  		ss << "r = "<<R<<"  (P=" << P.str() << ", N=" << N.str() << ", U=" << U.str() << ")";
0311ab81   Pavel Govyadinov   fixed some issues...
195
196
197
  		return ss.str();
  	}
  
26aee9ed   Pavel Govyadinov   changed circle cl...
198
  };
e24ed5a6   Pavel Govyadinov   circle added
199
  }
e24ed5a6   Pavel Govyadinov   circle added
200
  #endif