Blame view

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