Blame view

stim/math/plane.h 4.01 KB
d06d3dbd   Pavel Govyadinov   stable, pre-major...
1
2
  #ifndef STIM_PLANE_H
  #define STIM_PLANE_H
559d0fcb   David Mayerich   wave interactions...
3
4
  
  #include <iostream>
308a743c   David Mayerich   fixed class compa...
5
  #include <stim/math/vec3.h>
d06d3dbd   Pavel Govyadinov   stable, pre-major...
6
7
  #include <stim/cuda/cudatools/callable.h>
  #include <stim/math/quaternion.h>
559d0fcb   David Mayerich   wave interactions...
8
9
  
  
d06d3dbd   Pavel Govyadinov   stable, pre-major...
10
11
12
  namespace stim
  {
  template<typename T> class plane;
559d0fcb   David Mayerich   wave interactions...
13
14
  }
  
d06d3dbd   Pavel Govyadinov   stable, pre-major...
15
16
17
18
19
20
21
22
23
24
  template<typename T>
  CUDA_CALLABLE stim::plane<T> operator-(stim::plane<T> v);
  
  namespace stim
  {
  
  template <typename T>
  class plane
  {
  	protected:
308a743c   David Mayerich   fixed class compa...
25
26
27
  		stim::vec3<T> P;
  		stim::vec3<T> N;
  		stim::vec3<T> U;
d06d3dbd   Pavel Govyadinov   stable, pre-major...
28
29
30
31
32
  
  		///Initializes the plane with standard coordinates.
  		///
  		CUDA_CALLABLE void init()
  		{
308a743c   David Mayerich   fixed class compa...
33
34
35
  			P = stim::vec3<T>(0, 0, 0);
  			N = stim::vec3<T>(0, 0, 1);
  			U = stim::vec3<T>(1, 0, 0);
d06d3dbd   Pavel Govyadinov   stable, pre-major...
36
37
38
  		}
  
  	public:
559d0fcb   David Mayerich   wave interactions...
39
  	
d06d3dbd   Pavel Govyadinov   stable, pre-major...
40
41
42
43
44
  		CUDA_CALLABLE plane()
  		{
  			init();
  		}
  
308a743c   David Mayerich   fixed class compa...
45
  		CUDA_CALLABLE plane(vec3<T> n, vec3<T> p = vec3<T>(0, 0, 0))
d06d3dbd   Pavel Govyadinov   stable, pre-major...
46
47
48
49
50
51
52
53
54
55
56
57
58
  		{
  			init();
  			P = p;
  			rotate(n.norm());
  		}
  
  		CUDA_CALLABLE plane(T z_pos)
  		{
  			init();
  			P[2] = z_pos;
  		}
  
  		//create a plane from three points (a triangle)
308a743c   David Mayerich   fixed class compa...
59
  		CUDA_CALLABLE plane(vec3<T> a, vec3<T> b, vec3<T> c)
d06d3dbd   Pavel Govyadinov   stable, pre-major...
60
61
62
  		{
  			init();
  			P = c;
308a743c   David Mayerich   fixed class compa...
63
  			stim::vec3<T> n = (c - a).cross(b - a);
d06d3dbd   Pavel Govyadinov   stable, pre-major...
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
  			try
  			{
  				if(n.len() != 0)
  				{
  					rotate(n.norm());
  				} else {
  				 	throw 42;
  				}
  			}
  			catch(int i)
  			{
  				std::cerr << "No plane can be creates as all points a,b,c lie on a straight line" << std::endl;
  			}  
  		}
  	
  		template< typename U >
  		CUDA_CALLABLE operator plane<U>()
  		{
  			plane<U> result(N, P);
  			return result;
  
  		}
  
308a743c   David Mayerich   fixed class compa...
87
  		CUDA_CALLABLE vec3<T> n()
d06d3dbd   Pavel Govyadinov   stable, pre-major...
88
89
90
91
  		{
  			return N;
  		}
  
308a743c   David Mayerich   fixed class compa...
92
  		CUDA_CALLABLE vec3<T> p()
d06d3dbd   Pavel Govyadinov   stable, pre-major...
93
94
95
96
  		{
  			return P;
  		}
  
308a743c   David Mayerich   fixed class compa...
97
  		CUDA_CALLABLE vec3<T> u()
d06d3dbd   Pavel Govyadinov   stable, pre-major...
98
99
100
101
102
103
104
105
106
107
108
109
  		{
  			return U;
  		}
  
  		///flip the plane front-to-back
  		CUDA_CALLABLE plane<T> flip(){
  			plane<T> result = *this;
  			result.N = -result.N;
  			return result;
  		}
  
  		//determines how a vector v intersects the plane (1 = intersects front, 0 = within plane,     -1 = intersects back)
308a743c   David Mayerich   fixed class compa...
110
  		CUDA_CALLABLE int face(vec3<T> v){
d06d3dbd   Pavel Govyadinov   stable, pre-major...
111
112
113
114
115
116
117
118
119
120
121
122
123
  			
  			T dprod = v.dot(N);             //get the dot product between v and N
  
  			//conditional returns the appropriate value
  			if(dprod < 0)
  				return 1;
  			else if(dprod > 0)
  				return -1;
  			else
  				return 0;
  		}
  
  		//determine on which side of the plane a point lies (1 = front, 0 = on the plane, -1 = bac    k)
308a743c   David Mayerich   fixed class compa...
124
  		CUDA_CALLABLE int side(vec3<T> p){
d06d3dbd   Pavel Govyadinov   stable, pre-major...
125
  
308a743c   David Mayerich   fixed class compa...
126
  			vec3<T> v = p - P;    //get the vector from P to the query point p
d06d3dbd   Pavel Govyadinov   stable, pre-major...
127
128
129
130
131
  
  			return face(v);
  		}
  
  		//compute the component of v that is perpendicular to the plane
308a743c   David Mayerich   fixed class compa...
132
  		CUDA_CALLABLE vec3<T> perpendicular(vec3<T> v){
d06d3dbd   Pavel Govyadinov   stable, pre-major...
133
134
135
136
  			return N * v.dot(N);
  		}
  
  		//compute the projection of v in the plane
308a743c   David Mayerich   fixed class compa...
137
  		CUDA_CALLABLE vec3<T> parallel(vec3<T> v){
d06d3dbd   Pavel Govyadinov   stable, pre-major...
138
139
140
  			return v - perpendicular(v);
  		}
  
308a743c   David Mayerich   fixed class compa...
141
  		CUDA_CALLABLE void setU(vec3<T> v)
d06d3dbd   Pavel Govyadinov   stable, pre-major...
142
143
144
145
  		{
  			U = (parallel(v.norm())).norm();		
  		}
  
308a743c   David Mayerich   fixed class compa...
146
  		CUDA_CALLABLE void decompose(vec3<T> v, vec3<T>& para, vec3<T>& perp){
d06d3dbd   Pavel Govyadinov   stable, pre-major...
147
148
149
150
151
  			perp = N * v.dot(N);
  			para = v - perp;
  		}
  
  		//get both the parallel and perpendicular components of a vector v w.r.t. the plane
308a743c   David Mayerich   fixed class compa...
152
  		CUDA_CALLABLE void project(vec3<T> v, vec3<T> &v_par, vec3<T> &v_perp){
d06d3dbd   Pavel Govyadinov   stable, pre-major...
153
154
155
156
157
158
  
  			v_perp = v.dot(N);
  			v_par = v - v_perp;
  		}
  
  		//compute the reflection of v off of the plane
308a743c   David Mayerich   fixed class compa...
159
  		CUDA_CALLABLE vec3<T> reflect(vec3<T> v){
d06d3dbd   Pavel Govyadinov   stable, pre-major...
160
161
  
  			//compute the reflection using N_prime as the plane normal
308a743c   David Mayerich   fixed class compa...
162
163
  			vec3<T> par = parallel(v);
  			vec3<T> r = (-v) + par * 2;
d06d3dbd   Pavel Govyadinov   stable, pre-major...
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
  			return r;
  
  		}
  
  		CUDA_CALLABLE stim::plane<T> operator-()
  		{
  			stim::plane<T> p = *this;
  
  			//negate the normal vector
  			p.N = -p.N;
  			return p;
  		}
  
  		//output a string
  		std::string str(){
  			std::stringstream ss;
  			ss<<"P: "<<P<<std::endl;
  			ss<<"N: "<<N<<std::endl;
  			ss<<"U: "<<U;
  			return ss.str();
  		}
  
  
308a743c   David Mayerich   fixed class compa...
187
  		CUDA_CALLABLE void rotate(vec3<T> n)
d06d3dbd   Pavel Govyadinov   stable, pre-major...
188
189
190
  		{
  			quaternion<T> q;
  			q.CreateRotation(N, n);
83c3121c   David Mayerich   NaN value for |Ax...
191
192
193
  			matrix_sq<T, 3> M = q.toMatrix3();
  			N = M * N;
  			U = M * U;
d06d3dbd   Pavel Govyadinov   stable, pre-major...
194
195
196
  
  		}
  
308a743c   David Mayerich   fixed class compa...
197
  		CUDA_CALLABLE void rotate(vec3<T> n, vec3<T> &Y)
26aee9ed   Pavel Govyadinov   changed circle cl...
198
199
200
201
202
203
204
205
206
207
  		{
  			quaternion<T> q;
  			q.CreateRotation(N, n);
  			
  			N = q.toMatrix3() * N;
  			U = q.toMatrix3() * U;
  			Y = q.toMatrix3() * Y;
  
  		}
  
308a743c   David Mayerich   fixed class compa...
208
  		CUDA_CALLABLE void rotate(vec3<T> n, vec3<T> &X, vec3<T> &Y)
d06d3dbd   Pavel Govyadinov   stable, pre-major...
209
210
211
212
213
214
215
216
217
218
  		{
  			quaternion<T> q;
  			q.CreateRotation(N, n);
  			
  			N = q.toMatrix3() * N;
  			U = q.toMatrix3() * U;
  			X = q.toMatrix3() * X;
  			Y = q.toMatrix3() * Y;
  
  		}
559d0fcb   David Mayerich   wave interactions...
219
220
  
  };
d06d3dbd   Pavel Govyadinov   stable, pre-major...
221
222
  		
  		
559d0fcb   David Mayerich   wave interactions...
223
  }
8a86bd56   David Mayerich   changed rts names...
224
  #endif