Blame view

stim/math/plane.h 3.51 KB
559d0fcb   David Mayerich   wave interactions...
1
2
3
4
  #ifndef RTS_PLANE_H
  #define RTS_PLANE_H
  
  #include <iostream>
cce7daf9   Pavel Govyadinov   added glObj.h to ...
5
  #include <stim/math/vector.h>
559d0fcb   David Mayerich   wave interactions...
6
7
8
  #include "rts/cuda/callable.h"
  
  
8a86bd56   David Mayerich   changed rts names...
9
  namespace stim{
559d0fcb   David Mayerich   wave interactions...
10
11
12
13
  template <typename T, int D> class plane;
  }
  
  template <typename T, int D>
8a86bd56   David Mayerich   changed rts names...
14
  CUDA_CALLABLE stim::plane<T, D> operator-(stim::plane<T, D> v);
559d0fcb   David Mayerich   wave interactions...
15
  
8a86bd56   David Mayerich   changed rts names...
16
  namespace stim{
559d0fcb   David Mayerich   wave interactions...
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
  
  template <class T, int D = 3>
  class plane{
  
  	//a plane is defined by a point and a normal
  
  private:
  
  	vec<T, D> P;	//point on the plane
  	vec<T, D> N;	//plane normal
  
  	CUDA_CALLABLE void init(){
  		P = vec<T, D>(0, 0, 0);
  		N = vec<T, D>(0, 0, 1);
  	}
  
  
  public:
  
  	//default constructor
  	CUDA_CALLABLE plane(){
  		init();
  	}
  	
  	CUDA_CALLABLE plane(vec<T, D> n, vec<T, D> p = vec<T, D>(0, 0, 0)){
  		P = p;
  		N = n.norm();
  	}
  
  	CUDA_CALLABLE plane(T z_pos){
  		init();
  		P[2] = z_pos;
  	}
  
  	//create a plane from three points (a triangle)
  	CUDA_CALLABLE plane(vec<T, D> a, vec<T, D> b, vec<T, D> c){
  		P = c;
  		N = (c - a).cross(b - a);
  		if(N.len() == 0)	//handle the degenerate case when two vectors are the same, N = 0
  			N = 0;
  		else
  			N = N.norm();
  	}
  
ecfd14df   David Mayerich   implemented D fie...
61
62
63
64
65
66
67
  	template< typename U >
  	CUDA_CALLABLE operator plane<U, D>(){
  
  		plane<U, D> result(N, P);
  		return result;
  	}
  
559d0fcb   David Mayerich   wave interactions...
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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
  	CUDA_CALLABLE vec<T, D> norm(){
  		return N;
  	}
  
  	CUDA_CALLABLE vec<T, D> p(){
  		return P;
  	}
  
  	//flip the plane front-to-back
  	CUDA_CALLABLE plane<T, D> flip(){
  		plane<T, D> 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, D> 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 = back)
  	CUDA_CALLABLE int side(vec<T, D> p){
  
  		vec<T, D> 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, D> perpendicular(vec<T, D> v){
  		return N * v.dot(N);
  	}
  
  	//compute the projection of v in the plane
  	CUDA_CALLABLE vec<T, D> parallel(vec<T, D> v){
  		return v - perpendicular(v);
  	}
  
ecfd14df   David Mayerich   implemented D fie...
115
116
117
118
119
  	CUDA_CALLABLE void decompose(vec<T, D> v, vec<T, D>& para, vec<T, D>& perp){
  		perp = N * v.dot(N);
  		para = v - perp;
  	}
  
559d0fcb   David Mayerich   wave interactions...
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
  	//get both the parallel and perpendicular components of a vector v w.r.t. the plane
  	CUDA_CALLABLE void project(vec<T, D> v, vec<T, D> &v_par, vec<T, D> &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, D> reflect(vec<T, D> v){
  
  		//compute the reflection using N_prime as the plane normal
  		vec<T, D> par = parallel(v);
  		vec<T, D> r = (-v) + par * 2;
  
  		/*std::cout<<"----------------REFLECT-----------------------------"<<std::endl;
  		std::cout<<str()<<std::endl;
  		std::cout<<"v: "<<v<<std::endl;
  		std::cout<<"r: "<<r<<std::endl;
  		std::cout<<"Perpendicular: "<<perpendicular(v)<<std::endl;
  		std::cout<<"Parallel: "<<par<<std::endl;*/
  		return r;
  
  	}
  
ecfd14df   David Mayerich   implemented D fie...
144
145
146
147
148
149
150
151
152
153
  	CUDA_CALLABLE rts::plane<T, D> operator-()
  	{
  		rts::plane<T, D> p = *this;
  
  		//negate the normal vector
  		p.N = -p.N;
  
  		return p;
  	}
  
559d0fcb   David Mayerich   wave interactions...
154
155
156
157
158
159
160
161
162
  	//output a string
  	std::string str(){
  		std::stringstream ss;
  		ss<<"P: "<<P<<std::endl;
  		ss<<"N: "<<N;
  		return ss.str();
  	}
  
  	///////Friendship
ecfd14df   David Mayerich   implemented D fie...
163
  	//friend CUDA_CALLABLE rts::plane<T, D> operator- <> (rts::plane<T, D> v);
559d0fcb   David Mayerich   wave interactions...
164
165
166
167
168
169
170
171
172
173
  
  
  
  };
  
  }
  
  //arithmetic operators
  
  //negative operator flips the plane (front to back)
ecfd14df   David Mayerich   implemented D fie...
174
  //template <typename T, int D>
559d0fcb   David Mayerich   wave interactions...
175
  
559d0fcb   David Mayerich   wave interactions...
176
177
178
  
  
  
8a86bd56   David Mayerich   changed rts names...
179
  #endif