Blame view

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