Blame view

stim/math/circle.h 3.85 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>
26aee9ed   Pavel Govyadinov   changed circle cl...
8
  #include <assert.h>
e24ed5a6   Pavel Govyadinov   circle added
9
  #include <algorithm>
26aee9ed   Pavel Govyadinov   changed circle cl...
10
  #include <iostream>
e24ed5a6   Pavel Govyadinov   circle added
11
  
26aee9ed   Pavel Govyadinov   changed circle cl...
12
  namespace stim{
e24ed5a6   Pavel Govyadinov   circle added
13
  
26aee9ed   Pavel Govyadinov   changed circle cl...
14
15
  template <typename T>
  class circle : plane<T>
e24ed5a6   Pavel Govyadinov   circle added
16
  {
e24ed5a6   Pavel Govyadinov   circle added
17
  
26aee9ed   Pavel Govyadinov   changed circle cl...
18
19
  private:
  	
308a743c   David Mayerich   fixed class compa...
20
  	stim::vec3<T> Y;
e24ed5a6   Pavel Govyadinov   circle added
21
  
26aee9ed   Pavel Govyadinov   changed circle cl...
22
23
24
25
26
  	CUDA_CALLABLE void
  	init()
  	{
  		Y = U.cross(N).norm();
  	}
e24ed5a6   Pavel Govyadinov   circle added
27
  
26aee9ed   Pavel Govyadinov   changed circle cl...
28
29
30
31
32
33
34
  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
35
  
26aee9ed   Pavel Govyadinov   changed circle cl...
36
37
38
39
40
41
42
  	///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
43
  
26aee9ed   Pavel Govyadinov   changed circle cl...
44
45
46
47
48
49
50
  	///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.
  	CUDA_CALLABLE
  	circle(T size, T z_pos = (T)0) : plane<T>()
  	{
  		init();
308a743c   David Mayerich   fixed class compa...
51
  		center(stim::vec3<T>(0,0,z_pos));
26aee9ed   Pavel Govyadinov   changed circle cl...
52
53
54
55
56
57
58
  		scale(size);
  	}
  
  	///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
308a743c   David Mayerich   fixed class compa...
59
  	circle(vec3<T> c, vec3<T> n = vec3<T>(0,0,1)) : plane<T>()
26aee9ed   Pavel Govyadinov   changed circle cl...
60
61
62
63
64
65
66
67
68
69
70
  	{
  		center(c);
  		normal(n);
  		init();
  	}
  
  	///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.
  	CUDA_CALLABLE 
308a743c   David Mayerich   fixed class compa...
71
  	circle(vec3<T> c, T s, vec3<T> n = vec3<T>(0,0,1)) : plane<T>()
26aee9ed   Pavel Govyadinov   changed circle cl...
72
73
74
75
76
77
78
79
80
81
82
83
84
  	{
  		init();
  		center(c);
  		rotate(n, U, Y);
  		scale(s);
  	}
  
  	///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
308a743c   David Mayerich   fixed class compa...
85
  	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...
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
  	{
  		init();
  		setU(u);
  		center(c);
  		normal(n);
  		scale(s);
  	}
  
  	///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)
  	{
  		U *= factor;
  		Y *= factor;
  	}
  
  	///sets the normal for the cirlce
  	///@param n: x,y,z direction of the normal.
  	CUDA_CALLABLE void
308a743c   David Mayerich   fixed class compa...
106
  	normal(vec3<T> n)
26aee9ed   Pavel Govyadinov   changed circle cl...
107
108
109
  	{
  		rotate(n, Y);
  	}
e24ed5a6   Pavel Govyadinov   circle added
110
  
26aee9ed   Pavel Govyadinov   changed circle cl...
111
112
  	///sets the center of the circle.
  	///@param n: x,y,z location of the center.
308a743c   David Mayerich   fixed class compa...
113
114
  	CUDA_CALLABLE void
  	center(vec3<T> p){
26aee9ed   Pavel Govyadinov   changed circle cl...
115
116
117
118
119
120
121
122
123
124
125
126
127
128
  		this->P = p;
  	}
  
  	///boolean comparison
  	bool
  	operator==(const circle<T> & rhs)
  	{
  		if(P == rhs.P && U == rhs.U && Y == rhs.Y)
  			return true;
  		else
  			return false;
  	}
  
  	///get the world space value given the planar coordinates a, b in [0, 1]
308a743c   David Mayerich   fixed class compa...
129
  	CUDA_CALLABLE stim::vec3<T> p(T a, T b)
26aee9ed   Pavel Govyadinov   changed circle cl...
130
  	{
308a743c   David Mayerich   fixed class compa...
131
  		stim::vec3<T> result;
26aee9ed   Pavel Govyadinov   changed circle cl...
132
  
308a743c   David Mayerich   fixed class compa...
133
  		vec3<T> A = this->P - this->U * (T)0.5 - Y * (T)0.5;
26aee9ed   Pavel Govyadinov   changed circle cl...
134
135
136
137
138
  		result = A + this->U * a + Y * b;
  		return result;
  	}
  
  	///parenthesis operator returns the world space given rectangular coordinates a and b in [0 1]
308a743c   David Mayerich   fixed class compa...
139
  	CUDA_CALLABLE stim::vec3<T> operator()(T a, T b)
26aee9ed   Pavel Govyadinov   changed circle cl...
140
141
142
143
144
145
146
  	{
  		return p(a,b);
  	}
  
  	///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.
308a743c   David Mayerich   fixed class compa...
147
  	std::vector<stim::vec3<T> >
26aee9ed   Pavel Govyadinov   changed circle cl...
148
149
  	getPoints(int n)
  	{
308a743c   David Mayerich   fixed class compa...
150
151
  		std::vector<stim::vec3<T> > result;
  		stim::vec3<T> point;
26aee9ed   Pavel Govyadinov   changed circle cl...
152
153
154
  		T x,y;
  		float step = 360.0/(float) n;
  		for(float j = 0; j <= 360.0; j += step)
e24ed5a6   Pavel Govyadinov   circle added
155
  		{
26aee9ed   Pavel Govyadinov   changed circle cl...
156
157
158
  			y = 0.5*cos(j*2.0*M_PI/360.0)+0.5;
  			x = 0.5*sin(j*2.0*M_PI/360.0)+0.5;
  			result.push_back(p(x,y));
e24ed5a6   Pavel Govyadinov   circle added
159
  		}
26aee9ed   Pavel Govyadinov   changed circle cl...
160
161
162
163
164
165
  		return result;
  	}	
  	
  	///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.
308a743c   David Mayerich   fixed class compa...
166
  	stim::vec3<T>
26aee9ed   Pavel Govyadinov   changed circle cl...
167
168
169
170
171
172
173
  	p(T theta)
  	{
  		T x,y;
  		y = 0.5*cos(theta*2.0*M_PI/360.0)+0.5;
  		x = 0.5*sin(theta*2.0*M_PI/360.0)+0.5;
  		return p(x,y);
  	}
e24ed5a6   Pavel Govyadinov   circle added
174
  
26aee9ed   Pavel Govyadinov   changed circle cl...
175
  };
e24ed5a6   Pavel Govyadinov   circle added
176
  }
e24ed5a6   Pavel Govyadinov   circle added
177
  #endif