Commit 2137d77110928caec404f27178976bd2eca5d5ab

Authored by Pavel Govyadinov
1 parent 59360849

modified plane and rect in order have an inheritance scheme plane -> rect.

Showing 3 changed files with 328 additions and 58 deletions   Show diff stats
stim/math/plane.h
... ... @@ -31,6 +31,12 @@ template <typename T> class plane
31 31 init();
32 32 }
33 33  
  34 + CUDA_CALLABLE plane(vec<T> p)
  35 + {
  36 + init();
  37 + P = p;
  38 + }
  39 +
34 40 CUDA_CALLABLE plane(vec<T> n, vec<T> p = vec<T>(0, 0, 0))
35 41 {
36 42 init();
... ... @@ -125,6 +131,11 @@ template &lt;typename T&gt; class plane
125 131 return v - perpendicular(v);
126 132 }
127 133  
  134 + CUDA_CALLABLE void setU(vec<T> v)
  135 + {
  136 + U = (parallel(v.norm())).norm();
  137 + }
  138 +
128 139 CUDA_CALLABLE void decompose(vec<T> v, vec<T>& para, vec<T>& perp){
129 140 perp = N * v.dot(N);
130 141 para = v - perp;
... ... @@ -176,6 +187,18 @@ template &lt;typename T&gt; class plane
176 187  
177 188 }
178 189  
  190 + CUDA_CALLABLE void rotate(vec<T> n, vec<T> &X, vec<T> &Y)
  191 + {
  192 + quaternion<T> q;
  193 + q.CreateRotation(N, n);
  194 +
  195 + N = q.toMatrix3() * N;
  196 + U = q.toMatrix3() * U;
  197 + X = q.toMatrix3() * X;
  198 + Y = q.toMatrix3() * Y;
  199 +
  200 + }
  201 +
179 202 };
180 203  
181 204  
... ...
stim/math/rect.h
1   -#ifndef RTS_RECT_H
2   -#define RTS_RECT_H
  1 +#ifndef STIM_RECT_H
  2 +#define STIM_RECT_H
3 3  
4 4 //enable CUDA_CALLABLE macro
5 5 #include <stim/cuda/cudatools/callable.h>
  6 +#include <stim/math/plane.h>
6 7 #include <stim/math/vector.h>
7 8 #include <stim/math/triangle.h>
8   -#include <stim/math/quaternion.h>
9 9 #include <iostream>
10 10 #include <iomanip>
11 11 #include <algorithm>
... ... @@ -13,14 +13,14 @@
13 13 namespace stim{
14 14  
15 15 //template for a rectangle class in ND space
16   -template <class T>
17   -struct rect
  16 +template <typename T>
  17 +class rect : public plane <T>
18 18 {
19 19 /*
20 20 ^ O
21 21 |
22 22 |
23   - Y C
  23 + Y P
24 24 |
25 25 |
26 26 O---------X--------->
... ... @@ -28,7 +28,6 @@ struct rect
28 28  
29 29 private:
30 30  
31   - stim::vec<T> C;
32 31 stim::vec<T> X;
33 32 stim::vec<T> Y;
34 33  
... ... @@ -42,26 +41,30 @@ private:
42 41 public:
43 42  
44 43 ///base constructor.
45   - CUDA_CALLABLE rect(){
  44 + CUDA_CALLABLE rect()
  45 + : plane<T>()
  46 + {
46 47 init();
47 48 }
48 49  
49 50 ///create a rectangle given a size and position in Z space.
50 51 ///@param size: size of the rectangle in ND space.
51 52 ///@param z_pos z coordinate of the rectangle.
52   - CUDA_CALLABLE rect(T size, T z_pos = (T)0){
  53 + CUDA_CALLABLE rect(T size, T z_pos = (T)0)
  54 + : plane<T>(z_pos)
  55 + {
53 56 init(); //use the default setup
54 57 scale(size); //scale the rectangle
55   - C[2] = z_pos;
56 58 }
57 59  
58 60  
59 61 ///create a rectangle from a center point, normal
60 62 ///@param c: x,y,z location of the center.
61 63 ///@param n: x,y,z direction of the normal.
62   - CUDA_CALLABLE rect(vec<T> c, vec<T> n = vec<T>(0, 0, 1)){
  64 + CUDA_CALLABLE rect(vec<T> c, vec<T> n = vec<T>(0, 0, 1))
  65 + : plane<T>(n, c)
  66 + {
63 67 init(); //start with the default setting
64   - C = c;
65 68 normal(n); //orient
66 69 }
67 70  
... ... @@ -69,11 +72,11 @@ public:
69 72 ///@param c: x,y,z location of the center.
70 73 ///@param s: size of the rectangle.
71 74 ///@param n: x,y,z direction of the normal.
72   - CUDA_CALLABLE rect(vec<T> c, T s, vec<T> n = vec<T>(0, 0, 1)){
  75 + CUDA_CALLABLE rect(vec<T> c, T s, vec<T> n = vec<T>(0, 0, 1))
  76 + : plane<T>(n, c)
  77 + {
73 78 init(); //start with the default setting
74   - C = c;
75 79 scale(s);
76   - normal(n); //orient
77 80 }
78 81  
79 82 ///creates a rectangle from a centerpoint and an X and Y direction vectors.
... ... @@ -81,8 +84,8 @@ public:
81 84 ///@param directionX: u,v,w direction of the X vector.
82 85 ///@param directionY: u,v,w direction of the Y vector.
83 86 CUDA_CALLABLE rect(vec<T> center, vec<T> directionX, vec<T> directionY )
  87 + : plane<T>(p)
84 88 {
85   - C = center;
86 89 X = directionX;
87 90 Y = directionY;
88 91 }
... ... @@ -93,8 +96,8 @@ public:
93 96 ///@param directionX: u,v,w direction of the X vector.
94 97 ///@param directionY: u,v,w direction of the Y vector.
95 98 CUDA_CALLABLE rect(T size, vec<T> center, vec<T> directionX, vec<T> directionY )
  99 + : plane<T>(p)
96 100 {
97   - C = center;
98 101 X = directionX;
99 102 Y = directionY;
100 103 scale(size);
... ... @@ -106,8 +109,8 @@ public:
106 109 ///@param directionX: u,v,w direction of the X vector.
107 110 ///@param directionY: u,v,w direction of the Y vector.
108 111 CUDA_CALLABLE rect(vec<T> size, vec<T> center, vec<T> directionX, vec<T> directionY )
  112 + : plane<T>(p)
109 113 {
110   - C = center;
111 114 X = directionX;
112 115 Y = directionY;
113 116 scale(size[0], size[1]);
... ... @@ -116,28 +119,24 @@ public:
116 119 ///scales a rectangle in ND space.
117 120 ///@param factor1: size of the scale in the X-direction.
118 121 ///@param factor2: size of the scale in the Y-direction.
119   - CUDA_CALLABLE void scale(T factor1, T factor2){
  122 + CUDA_CALLABLE void scale(T factor1, T factor2)
  123 + {
120 124 X *= factor1;
121 125 Y *= factor2;
122 126 }
123 127  
124 128 ///@param n; vector with the normal.
125 129 ///Orients the rectangle along the normal n.
126   - CUDA_CALLABLE void normal(vec<T> n){ //orient the rectangle along the specified normal
127   -
128   - n = n.norm(); //normalize, just in case
129   - vec<T> n_current = X.cross(Y).norm(); //compute the current normal
130   - quaternion<T> q; //create a quaternion
131   - q.CreateRotation(n_current, n); //initialize a rotation from n_current to n
132   -
133   - //apply the quaternion to the vectors and position
134   - X = q.toMatrix3() * X;
135   - Y = q.toMatrix3() * Y;
  130 + CUDA_CALLABLE void normal(vec<T> n)
  131 + {
  132 + //orient the rectangle along the specified normal
  133 + rotate(n, X, Y);
136 134 }
137 135  
138 136 ///general init method that sets a general rectangle.
139   - CUDA_CALLABLE void init(){
140   - C = vec<T>(0, 0, 0);
  137 + CUDA_CALLABLE void init()
  138 + {
  139 + P = vec<T>(0, 0, 0);
141 140 X = vec<T>(1, 0, 0);
142 141 Y = vec<T>(0, 1, 0);
143 142 }
... ... @@ -145,26 +144,19 @@ public:
145 144 //boolean comparison
146 145 bool operator==(const rect<T> & rhs)
147 146 {
148   - if(C == rhs.C && X == rhs.X && Y == rhs.Y)
  147 + if(P == rhs.P && X == rhs.X && Y == rhs.Y)
149 148 return true;
150 149 else
151 150 return false;
152 151 }
153 152  
154   - /*******************************************
155   - Return the normal for the rect
156   - *******************************************/
157   - CUDA_CALLABLE stim::vec<T> n()
158   - {
159   - return (X.cross(Y)).norm();
160   - }
161 153  
162 154 //get the world space value given the planar coordinates a, b in [0, 1]
163 155 CUDA_CALLABLE stim::vec<T> p(T a, T b)
164 156 {
165 157 stim::vec<T> result;
166 158 //given the two parameters a, b = [0 1], returns the position in world space
167   - vec<T> A = C - X * (T)0.5 - Y * (T)0.5;
  159 + vec<T> A = P - X * (T)0.5 - Y * (T)0.5;
168 160 result = A + X * a + Y * b;
169 161  
170 162 return result;
... ... @@ -179,12 +171,12 @@ public:
179 171 std::string str()
180 172 {
181 173 std::stringstream ss;
182   - vec<T> A = C - X * (T)0.5 - Y * (T)0.5;
  174 + vec<T> A = P - X * (T)0.5 - Y * (T)0.5;
183 175 ss<<std::left<<"B="<<std::setfill('-')<<std::setw(20)<<A + Y<<">"<<"C="<<A + Y + X<<std::endl;
184 176 ss<<std::setfill(' ')<<std::setw(23)<<"|"<<"|"<<std::endl<<std::setw(23)<<"|"<<"|"<<std::endl;
185 177 ss<<std::left<<"A="<<std::setfill('-')<<std::setw(20)<<A<<">"<<"D="<<A + X;
186 178  
187   - return ss.str();
  179 + return ss.str();
188 180  
189 181 }
190 182  
... ... @@ -207,20 +199,20 @@ public:
207 199 {
208 200 //compute the distance between a point and this rect
209 201  
210   - vec<T> A = C - X * (T)0.5 - Y * (T)0.5;
  202 + vec<T> A = P - X * (T)0.5 - Y * (T)0.5;
211 203  
212   - //first break the rect up into two triangles
213   - triangle<T> T0(A, A+X, A+Y);
214   - triangle<T> T1(A+X+Y, A+X, A+Y);
  204 + //first break the rect up into two triangles
  205 + triangle<T> T0(A, A+X, A+Y);
  206 + triangle<T> T1(A+X+Y, A+X, A+Y);
215 207  
216 208  
217   - T d0 = T0.dist(p);
218   - T d1 = T1.dist(p);
  209 + T d0 = T0.dist(p);
  210 + T d1 = T1.dist(p);
219 211  
220   - if(d0 < d1)
221   - return d0;
222   - else
223   - return d1;
  212 + if(d0 < d1)
  213 + return d0;
  214 + else
  215 + return d1;
224 216 }
225 217  
226 218 CUDA_CALLABLE T center(vec<T> p)
... ... @@ -232,13 +224,13 @@ public:
232 224 ///@param p: x, y, z point.
233 225 CUDA_CALLABLE T dist_max(vec<T> p)
234 226 {
235   - vec<T> A = C - X * (T)0.5 - Y * (T)0.5;
236   - T da = (A - p).len();
237   - T db = (A+X - p).len();
238   - T dc = (A+Y - p).len();
239   - T dd = (A+X+Y - p).len();
  227 + vec<T> A = P - X * (T)0.5 - Y * (T)0.5;
  228 + T da = (A - p).len();
  229 + T db = (A+X - p).len();
  230 + T dc = (A+Y - p).len();
  231 + T dd = (A+X+Y - p).len();
240 232  
241   - return std::max( da, std::max(db, std::max(dc, dd) ) );
  233 + return std::max( da, std::max(db, std::max(dc, dd) ) );
242 234 }
243 235 };
244 236  
... ...
stim/math/rect_old.h 0 → 100644
  1 +#ifndef RTS_RECT_H
  2 +#define RTS_RECT_H
  3 +
  4 +//enable CUDA_CALLABLE macro
  5 +#include <stim/cuda/cudatools/callable.h>
  6 +#include <stim/math/vector.h>
  7 +#include <stim/math/triangle.h>
  8 +#include <stim/math/quaternion.h>
  9 +#include <iostream>
  10 +#include <iomanip>
  11 +#include <algorithm>
  12 +
  13 +namespace stim{
  14 +
  15 +//template for a rectangle class in ND space
  16 +template <class T>
  17 +struct rect
  18 +{
  19 + /*
  20 + ^ O
  21 + |
  22 + |
  23 + Y C
  24 + |
  25 + |
  26 + O---------X--------->
  27 + */
  28 +
  29 +private:
  30 +
  31 + stim::vec<T> C;
  32 + stim::vec<T> X;
  33 + stim::vec<T> Y;
  34 +
  35 + CUDA_CALLABLE void scale(T factor){
  36 + X *= factor;
  37 + Y *= factor;
  38 + }
  39 +
  40 +
  41 +
  42 +public:
  43 +
  44 + ///base constructor.
  45 + CUDA_CALLABLE rect(){
  46 + init();
  47 + }
  48 +
  49 + ///create a rectangle given a size and position in Z space.
  50 + ///@param size: size of the rectangle in ND space.
  51 + ///@param z_pos z coordinate of the rectangle.
  52 + CUDA_CALLABLE rect(T size, T z_pos = (T)0){
  53 + init(); //use the default setup
  54 + scale(size); //scale the rectangle
  55 + C[2] = z_pos;
  56 + }
  57 +
  58 +
  59 + ///create a rectangle from a center point, normal
  60 + ///@param c: x,y,z location of the center.
  61 + ///@param n: x,y,z direction of the normal.
  62 + CUDA_CALLABLE rect(vec<T> c, vec<T> n = vec<T>(0, 0, 1)){
  63 + init(); //start with the default setting
  64 + C = c;
  65 + normal(n); //orient
  66 + }
  67 +
  68 + ///create a rectangle from a center point, normal, and size
  69 + ///@param c: x,y,z location of the center.
  70 + ///@param s: size of the rectangle.
  71 + ///@param n: x,y,z direction of the normal.
  72 + CUDA_CALLABLE rect(vec<T> c, T s, vec<T> n = vec<T>(0, 0, 1)){
  73 + init(); //start with the default setting
  74 + C = c;
  75 + scale(s);
  76 + normal(n); //orient
  77 + }
  78 +
  79 + ///creates a rectangle from a centerpoint and an X and Y direction vectors.
  80 + ///@param center: x,y,z location of the center.
  81 + ///@param directionX: u,v,w direction of the X vector.
  82 + ///@param directionY: u,v,w direction of the Y vector.
  83 + CUDA_CALLABLE rect(vec<T> center, vec<T> directionX, vec<T> directionY )
  84 + {
  85 + C = center;
  86 + X = directionX;
  87 + Y = directionY;
  88 + }
  89 +
  90 + ///creates a rectangle from a size, centerpoint, X, and Y direction vectors.
  91 + ///@param size of the rectangle in ND space.
  92 + ///@param center: x,y,z location of the center.
  93 + ///@param directionX: u,v,w direction of the X vector.
  94 + ///@param directionY: u,v,w direction of the Y vector.
  95 + CUDA_CALLABLE rect(T size, vec<T> center, vec<T> directionX, vec<T> directionY )
  96 + {
  97 + C = center;
  98 + X = directionX;
  99 + Y = directionY;
  100 + scale(size);
  101 + }
  102 +
  103 + ///creates a rectangle from a size, centerpoint, X, and Y direction vectors.
  104 + ///@param size of the rectangle in ND space, size[0] = size in X, size[1] = size in Y.
  105 + ///@param center: x,y,z location of the center.
  106 + ///@param directionX: u,v,w direction of the X vector.
  107 + ///@param directionY: u,v,w direction of the Y vector.
  108 + CUDA_CALLABLE rect(vec<T> size, vec<T> center, vec<T> directionX, vec<T> directionY )
  109 + {
  110 + C = center;
  111 + X = directionX;
  112 + Y = directionY;
  113 + scale(size[0], size[1]);
  114 + }
  115 +
  116 + ///scales a rectangle in ND space.
  117 + ///@param factor1: size of the scale in the X-direction.
  118 + ///@param factor2: size of the scale in the Y-direction.
  119 + CUDA_CALLABLE void scale(T factor1, T factor2){
  120 + X *= factor1;
  121 + Y *= factor2;
  122 + }
  123 +
  124 + ///@param n; vector with the normal.
  125 + ///Orients the rectangle along the normal n.
  126 + CUDA_CALLABLE void normal(vec<T> n){ //orient the rectangle along the specified normal
  127 +
  128 + n = n.norm(); //normalize, just in case
  129 + vec<T> n_current = X.cross(Y).norm(); //compute the current normal
  130 + quaternion<T> q; //create a quaternion
  131 + q.CreateRotation(n_current, n); //initialize a rotation from n_current to n
  132 +
  133 + //apply the quaternion to the vectors and position
  134 + X = q.toMatrix3() * X;
  135 + Y = q.toMatrix3() * Y;
  136 + }
  137 +
  138 + ///general init method that sets a general rectangle.
  139 + CUDA_CALLABLE void init(){
  140 + C = vec<T>(0, 0, 0);
  141 + X = vec<T>(1, 0, 0);
  142 + Y = vec<T>(0, 1, 0);
  143 + }
  144 +
  145 + //boolean comparison
  146 + bool operator==(const rect<T> & rhs)
  147 + {
  148 + if(C == rhs.C && X == rhs.X && Y == rhs.Y)
  149 + return true;
  150 + else
  151 + return false;
  152 + }
  153 +
  154 + /*******************************************
  155 + Return the normal for the rect
  156 + *******************************************/
  157 + CUDA_CALLABLE stim::vec<T> n()
  158 + {
  159 + return (X.cross(Y)).norm();
  160 + }
  161 +
  162 + //get the world space value given the planar coordinates a, b in [0, 1]
  163 + CUDA_CALLABLE stim::vec<T> p(T a, T b)
  164 + {
  165 + stim::vec<T> result;
  166 + //given the two parameters a, b = [0 1], returns the position in world space
  167 + vec<T> A = C - X * (T)0.5 - Y * (T)0.5;
  168 + result = A + X * a + Y * b;
  169 +
  170 + return result;
  171 + }
  172 +
  173 + //parenthesis operator returns the world space given rectangular coordinates a and b in [0 1]
  174 + CUDA_CALLABLE stim::vec<T> operator()(T a, T b)
  175 + {
  176 + return p(a, b);
  177 + }
  178 +
  179 + std::string str()
  180 + {
  181 + std::stringstream ss;
  182 + vec<T> A = C - X * (T)0.5 - Y * (T)0.5;
  183 + ss<<std::left<<"B="<<std::setfill('-')<<std::setw(20)<<A + Y<<">"<<"C="<<A + Y + X<<std::endl;
  184 + ss<<std::setfill(' ')<<std::setw(23)<<"|"<<"|"<<std::endl<<std::setw(23)<<"|"<<"|"<<std::endl;
  185 + ss<<std::left<<"A="<<std::setfill('-')<<std::setw(20)<<A<<">"<<"D="<<A + X;
  186 +
  187 + return ss.str();
  188 +
  189 + }
  190 +
  191 + ///multiplication operator scales the rectangle by a value rhs.
  192 + CUDA_CALLABLE rect<T> operator*(T rhs)
  193 + {
  194 + //scales the plane by a scalar value
  195 +
  196 + //create the new rectangle
  197 + rect<T> result = *this;
  198 + result.scale(rhs);
  199 +
  200 + return result;
  201 +
  202 + }
  203 +
  204 + ///computes the distance between the specified point and this rectangle.
  205 + ///@param p: x, y, z coordinates of the point to calculate distance to.
  206 + CUDA_CALLABLE T dist(vec<T> p)
  207 + {
  208 + //compute the distance between a point and this rect
  209 +
  210 + vec<T> A = C - X * (T)0.5 - Y * (T)0.5;
  211 +
  212 + //first break the rect up into two triangles
  213 + triangle<T> T0(A, A+X, A+Y);
  214 + triangle<T> T1(A+X+Y, A+X, A+Y);
  215 +
  216 +
  217 + T d0 = T0.dist(p);
  218 + T d1 = T1.dist(p);
  219 +
  220 + if(d0 < d1)
  221 + return d0;
  222 + else
  223 + return d1;
  224 + }
  225 +
  226 + CUDA_CALLABLE T center(vec<T> p)
  227 + {
  228 + C = p;
  229 + }
  230 +
  231 + ///Returns the maximum distance of the rectangle from a point p to the sides of the rectangle.
  232 + ///@param p: x, y, z point.
  233 + CUDA_CALLABLE T dist_max(vec<T> p)
  234 + {
  235 + vec<T> A = C - X * (T)0.5 - Y * (T)0.5;
  236 + T da = (A - p).len();
  237 + T db = (A+X - p).len();
  238 + T dc = (A+Y - p).len();
  239 + T dd = (A+X+Y - p).len();
  240 +
  241 + return std::max( da, std::max(db, std::max(dc, dd) ) );
  242 + }
  243 +};
  244 +
  245 +} //end namespace rts
  246 +
  247 +template <typename T, int N>
  248 +std::ostream& operator<<(std::ostream& os, stim::rect<T> R)
  249 +{
  250 + os<<R.str();
  251 + return os;
  252 +}
  253 +
  254 +
  255 +#endif
... ...