Blame view

math/quad.h 4.33 KB
a47a23a9   dmayerich   added ENVI functions
1
2
3
4
  #ifndef RTS_RECT_H
  #define RTS_RECT_H
  
  //enable CUDA_CALLABLE macro
cc8715ef   dmayerich   organization
5
6
7
  #include "rts/cuda/callable.h"
  #include "rts/math/vector.h"
  #include "rts/math/point.h"
0174d823   dmayerich   bug fixes and exits
8
  #include "rts/math/triangle.h"
7006df5f   David Mayerich   reformat of direc...
9
  #include "rts/math/quaternion.h"
a47a23a9   dmayerich   added ENVI functions
10
  #include <iostream>
57729e5b   David Mayerich   changed directory...
11
  #include <algorithm>
a47a23a9   dmayerich   added ENVI functions
12
13
14
  
  namespace rts{
  
7006df5f   David Mayerich   reformat of direc...
15
  //template for a quadangle class in ND space
a47a23a9   dmayerich   added ENVI functions
16
  template <class T, int N>
7006df5f   David Mayerich   reformat of direc...
17
  struct quad
a47a23a9   dmayerich   added ENVI functions
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
  {
  	/*
  		C------------------>O
  		^                   ^
  		|                   |
  		Y                   |
  		|                   |
  		|                   |
  		A---------X-------->B
  	*/
  
  	/*T A[N];
  	T B[N];
  	T C[N];*/
  
7006df5f   David Mayerich   reformat of direc...
33
34
35
  	rts::point<T, N> A;
  	rts::vector<T, N> X;
  	rts::vector<T, N> Y;
a47a23a9   dmayerich   added ENVI functions
36
37
  
  
7006df5f   David Mayerich   reformat of direc...
38
  	CUDA_CALLABLE quad()
a47a23a9   dmayerich   added ENVI functions
39
40
41
42
  	{
  
  	}
  
7006df5f   David Mayerich   reformat of direc...
43
  	CUDA_CALLABLE quad(point<T, N> a, point<T, N> b, point<T, N> c)
a47a23a9   dmayerich   added ENVI functions
44
45
46
47
48
49
50
51
  	{
  
  		A = a;
  		X = b - a;
  		Y = c - a;
  
  	}
  
7006df5f   David Mayerich   reformat of direc...
52
53
54
55
      /****************************************************************
      Constructor - create a quad from two points and a normal
      ****************************************************************/
  	CUDA_CALLABLE quad(rts::point<T, N> pMin, rts::point<T, N> pMax, rts::vector<T, N> normal)
a47a23a9   dmayerich   added ENVI functions
56
57
  	{
  
7006df5f   David Mayerich   reformat of direc...
58
          //assign the corner point
a47a23a9   dmayerich   added ENVI functions
59
60
          A = pMin;
  
57729e5b   David Mayerich   changed directory...
61
          //compute the vector from pMin to pMax
7006df5f   David Mayerich   reformat of direc...
62
          rts::vector<T, 3> v0;
a47a23a9   dmayerich   added ENVI functions
63
64
65
          v0 = pMax - pMin;
  
          //compute the cross product of A and the plane normal
7006df5f   David Mayerich   reformat of direc...
66
          rts::vector<T, 3> v1;
a47a23a9   dmayerich   added ENVI functions
67
68
69
          v1 = v0.cross(normal);
  
  
7006df5f   David Mayerich   reformat of direc...
70
71
          //calculate point B
          rts::point<T, 3> B;
7731cf24   David Mayerich   fixed precision p...
72
          B = A + v0 * 0.5f + v1 * 0.5f;
a47a23a9   dmayerich   added ENVI functions
73
74
  
          //calculate rtsPoint C
7006df5f   David Mayerich   reformat of direc...
75
          rts::point<T, 3> C;
7731cf24   David Mayerich   fixed precision p...
76
          C = A  + v0 * 0.5f - v1 * 0.5f;
a47a23a9   dmayerich   added ENVI functions
77
78
79
80
  
          //calculate X and Y
          X = B - A;
          Y = C - A;
7006df5f   David Mayerich   reformat of direc...
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
  	}
  
  	/*******************************************************************
  	Constructor - create a quad from a position, normal, and rotation
  	*******************************************************************/
  	CUDA_CALLABLE quad(rts::point<T, N> c, rts::vector<T, N> normal, T width, T height, T theta)
  	{
  
          //compute the X direction - start along world-space X
          Y = rts::vector<T, N>(0, 1, 0);
          if(Y == normal)
              Y = rts::vector<T, N>(0, 0, 1);
  
          X = Y.cross(normal).norm();
  
          std::cout<<X<<std::endl;
  
          //rotate the X axis by theta radians
          rts::quaternion<T> q;
          q.CreateRotation(theta, normal);
          X = q.toMatrix3() * X;
          Y = normal.cross(X);
a47a23a9   dmayerich   added ENVI functions
103
  
7006df5f   David Mayerich   reformat of direc...
104
105
106
          //normalize everything
          X = X.norm();
          Y = Y.norm();
a47a23a9   dmayerich   added ENVI functions
107
  
7006df5f   David Mayerich   reformat of direc...
108
109
110
          //scale to match the quad width and height
          X = X * width;
          Y = Y * height;
a47a23a9   dmayerich   added ENVI functions
111
  
7006df5f   David Mayerich   reformat of direc...
112
          //set the corner of the plane
7731cf24   David Mayerich   fixed precision p...
113
          A = c - X * 0.5f - Y * 0.5f;
a47a23a9   dmayerich   added ENVI functions
114
  
7006df5f   David Mayerich   reformat of direc...
115
          std::cout<<X<<std::endl;
a47a23a9   dmayerich   added ENVI functions
116
117
  	}
  
7006df5f   David Mayerich   reformat of direc...
118
119
120
121
  	/*******************************************
  	Return the normal for the quad
  	*******************************************/
  	CUDA_CALLABLE rts::vector<T, N> n()
a47a23a9   dmayerich   added ENVI functions
122
  	{
7006df5f   David Mayerich   reformat of direc...
123
124
125
126
127
128
          return (X.cross(Y)).norm();
  	}
  
  	CUDA_CALLABLE rts::point<T, N> p(T a, T b)
  	{
  		rts::point<T, N> result;
a47a23a9   dmayerich   added ENVI functions
129
130
  		//given the two parameters a, b = [0 1], returns the position in world space
  		result = A + X * a + Y * b;
cc8715ef   dmayerich   organization
131
  
a47a23a9   dmayerich   added ENVI functions
132
133
134
  		return result;
  	}
  
7006df5f   David Mayerich   reformat of direc...
135
  	CUDA_CALLABLE rts::point<T, N> operator()(T a, T b)
a47a23a9   dmayerich   added ENVI functions
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
  	{
  		return p(a, b);
  	}
  
  	std::string toStr()
  	{
  		std::stringstream ss;
  
  		ss<<"A = "<<A<<std::endl;
  		ss<<"B = "<<A + X<<std::endl;
  		ss<<"C = "<<A + X + Y<<std::endl;
  		ss<<"D = "<<A + Y<<std::endl;
  
          return ss.str();
  
  	}
  
7006df5f   David Mayerich   reformat of direc...
153
  	CUDA_CALLABLE quad<T, N> operator*(T rhs)
a47a23a9   dmayerich   added ENVI functions
154
155
156
  	{
  		//scales the plane by a scalar value
  
7006df5f   David Mayerich   reformat of direc...
157
  		//compute the center point
7731cf24   David Mayerich   fixed precision p...
158
  		rts::point<T, N> c = A + X*0.5f + Y*0.5f;
a47a23a9   dmayerich   added ENVI functions
159
  
7006df5f   David Mayerich   reformat of direc...
160
161
  		//create the new quadangle
  		quad<T, N> result;
a47a23a9   dmayerich   added ENVI functions
162
163
  		result.X = X * rhs;
  		result.Y = Y * rhs;
7731cf24   David Mayerich   fixed precision p...
164
  		result.A = c - result.X*0.5f - result.Y*0.5f;
a47a23a9   dmayerich   added ENVI functions
165
166
167
168
  
  		return result;
  
  	}
0174d823   dmayerich   bug fixes and exits
169
  
7006df5f   David Mayerich   reformat of direc...
170
  	CUDA_CALLABLE T dist(point<T, N> p)
0174d823   dmayerich   bug fixes and exits
171
172
173
174
  	{
          //compute the distance between a point and this quad
  
          //first break the quad up into two triangles
7006df5f   David Mayerich   reformat of direc...
175
176
          triangle<T, N> T0(A, A+X, A+Y);
          triangle<T, N> T1(A+X+Y, A+X, A+Y);
0174d823   dmayerich   bug fixes and exits
177
178
179
180
181
182
183
184
185
186
  
  
          ptype d0 = T0.dist(p);
          ptype d1 = T1.dist(p);
  
          if(d0 < d1)
              return d0;
          else
              return d1;
  	}
7006df5f   David Mayerich   reformat of direc...
187
188
189
190
191
192
193
194
  
  	CUDA_CALLABLE T dist_max(point<T, N> p)
  	{
          T da = (A - p).len();
          T db = (A+X - p).len();
          T dc = (A+Y - p).len();
          T dd = (A+X+Y - p).len();
  
7731cf24   David Mayerich   fixed precision p...
195
          return fmax( da, fmax(db, fmax(dc, dd) ) );
7006df5f   David Mayerich   reformat of direc...
196
  	}
a47a23a9   dmayerich   added ENVI functions
197
198
199
200
201
  };
  
  }	//end namespace rts
  
  template <typename T, int N>
7006df5f   David Mayerich   reformat of direc...
202
  std::ostream& operator<<(std::ostream& os, rts::quad<T, N> R)
a47a23a9   dmayerich   added ENVI functions
203
204
205
206
207
208
  {
      os<<R.toStr();
      return os;
  }
  
  
cc8715ef   dmayerich   organization
209
  #endif