Commit d06d3dbd9dcc66207034c717ea2965dd3e295c53

Authored by Pavel Govyadinov
1 parent 035d798f

stable, pre-major changes

Showing 3 changed files with 339 additions and 255 deletions   Show diff stats
stim/gl/gl_spider.h
@@ -81,8 +81,8 @@ class gl_spider : public virtual gl_texture<T> @@ -81,8 +81,8 @@ class gl_spider : public virtual gl_texture<T>
81 int numSamplesPos; 81 int numSamplesPos;
82 int numSamplesMag; 82 int numSamplesMag;
83 83
84 -// float stepsize = 4.0; //Step size.  
85 - float stepsize = 3.0; //Step size. 84 + float stepsize = 5.0; //Step size.
  85 +// float stepsize = 3.0; //Step size.
86 int current_cost; //variable to store the cost of the current step. 86 int current_cost; //variable to store the cost of the current step.
87 87
88 88
@@ -140,11 +140,12 @@ class gl_spider : public virtual gl_texture<T> @@ -140,11 +140,12 @@ class gl_spider : public virtual gl_texture<T>
140 setMatrix(); //create the transformation matrix. 140 setMatrix(); //create the transformation matrix.
141 glCallList(dList+1); //move the templates to p, d, m. 141 glCallList(dList+1); //move the templates to p, d, m.
142 int best = getCost(ptexbufferID, numSamplesPos); //find min cost. 142 int best = getCost(ptexbufferID, numSamplesPos); //find min cost.
  143 + std::cerr << best << std::endl;
143 stim::vec<float> next( //find next position. 144 stim::vec<float> next( //find next position.
144 - pV[best][0],  
145 - pV[best][1],  
146 - pV[best][2],  
147 - 1); 145 + pV[best][0],
  146 + pV[best][1],
  147 + pV[best][2],
  148 + 1);
148 next = cT*next; //find next position. 149 next = cT*next; //find next position.
149 setPosition( 150 setPosition(
150 next[0]*S[0]*R[0], 151 next[0]*S[0]*R[0],
@@ -727,7 +728,7 @@ class gl_spider : public virtual gl_texture&lt;T&gt; @@ -727,7 +728,7 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
727 ///Best results if samples is can create a perfect root. 728 ///Best results if samples is can create a perfect root.
728 ///Default Constructor 729 ///Default Constructor
729 gl_spider 730 gl_spider
730 - (int samples = 1089, int samplespos = 400,int samplesmag = 144) 731 + (int samples = 1089, int samplespos = 441,int samplesmag = 144)
731 { 732 {
732 p = vec<float>(0.0, 0.0, 0.0); 733 p = vec<float>(0.0, 0.0, 0.0);
733 d = vec<float>(0.0, 0.0, 1.0); 734 d = vec<float>(0.0, 0.0, 1.0);
@@ -750,7 +751,7 @@ class gl_spider : public virtual gl_texture&lt;T&gt; @@ -750,7 +751,7 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
750 ///@param int samples, number of templates this spider is going to use. 751 ///@param int samples, number of templates this spider is going to use.
751 gl_spider 752 gl_spider
752 (float pos_x, float pos_y, float pos_z, float dir_x, float dir_y, float dir_z, 753 (float pos_x, float pos_y, float pos_z, float dir_x, float dir_y, float dir_z,
753 - float mag_x, int numsamples = 1089, int numsamplespos = 400, int numsamplesmag =144) 754 + float mag_x, int numsamples = 1089, int numsamplespos = 441, int numsamplesmag =144)
754 { 755 {
755 p = vec<float>(pos_x, pos_y, pos_z); 756 p = vec<float>(pos_x, pos_y, pos_z);
756 d = vec<float>(dir_x, dir_y, dir_z); 757 d = vec<float>(dir_x, dir_y, dir_z);
@@ -768,7 +769,7 @@ class gl_spider : public virtual gl_texture&lt;T&gt; @@ -768,7 +769,7 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
768 ///@param float mag, size of the vector. 769 ///@param float mag, size of the vector.
769 ///@param int samples, number of templates this spider is going to use. 770 ///@param int samples, number of templates this spider is going to use.
770 gl_spider 771 gl_spider
771 - (stim::vec<float> pos, stim::vec<float> dir, float mag, int samples = 1089, int samplesPos = 400, int samplesMag = 144) 772 + (stim::vec<float> pos, stim::vec<float> dir, float mag, int samples = 1089, int samplesPos = 441, int samplesMag = 144)
772 { 773 {
773 p = pos; 774 p = pos;
774 d = dir; 775 d = dir;
@@ -815,13 +816,13 @@ class gl_spider : public virtual gl_texture&lt;T&gt; @@ -815,13 +816,13 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
815 glListBase(dList); 816 glListBase(dList);
816 Bind(texbufferID, fboID, numSamples); 817 Bind(texbufferID, fboID, numSamples);
817 genDirectionVectors(5*M_PI/4); 818 genDirectionVectors(5*M_PI/4);
818 -// Unbind();  
819 - Bind(ptexbufferID, bfboID, numSamplesPos); 819 + Unbind();
  820 + Bind(ptexbufferID, pfboID, numSamplesPos);
820 genPositionVectors(); 821 genPositionVectors();
821 -// Unbind(); 822 + Unbind();
822 Bind(mtexbufferID, mfboID, numSamplesMag); 823 Bind(mtexbufferID, mfboID, numSamplesMag);
823 genMagnitudeVectors(); 824 genMagnitudeVectors();
824 -// Unbind(); 825 + Unbind();
825 Bind(btexbufferID, bfboID, 27); 826 Bind(btexbufferID, bfboID, 27);
826 DrawCylinder(); 827 DrawCylinder();
827 Unbind(); 828 Unbind();
@@ -1150,13 +1151,13 @@ class gl_spider : public virtual gl_texture&lt;T&gt; @@ -1150,13 +1151,13 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
1150 start = std::clock(); 1151 start = std::clock();
1151 #endif 1152 #endif
1152 findOptimalDirection(); 1153 findOptimalDirection();
1153 - //Unbind(); 1154 + Unbind();
1154 Bind(ptexbufferID, pfboID, numSamplesPos); 1155 Bind(ptexbufferID, pfboID, numSamplesPos);
1155 findOptimalPosition(); 1156 findOptimalPosition();
1156 - //Unbind(); 1157 + Unbind();
1157 Bind(mtexbufferID, mfboID, numSamplesMag); 1158 Bind(mtexbufferID, mfboID, numSamplesMag);
1158 findOptimalScale(); 1159 findOptimalScale();
1159 - //Unbind(); 1160 + Unbind();
1160 CHECK_OPENGL_ERROR 1161 CHECK_OPENGL_ERROR
1161 1162
1162 #ifdef TESTING 1163 #ifdef TESTING
@@ -1566,7 +1567,9 @@ class gl_spider : public virtual gl_texture&lt;T&gt; @@ -1566,7 +1567,9 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
1566 } 1567 }
1567 else { 1568 else {
1568 cL.push_back(stim::vec<float>(p[0], p[1],p[2])); 1569 cL.push_back(stim::vec<float>(p[0], p[1],p[2]));
1569 - cM.push_back(m[0]); 1570 + cM.push_back(stim::vec<float>(m[0], m[0]));
  1571 +// cM.push_back(m[0]);
  1572 +
1570 sk.TexCoord(m[0]); 1573 sk.TexCoord(m[0]);
1571 sk.Vertex(p[0], p[1], p[2]); 1574 sk.Vertex(p[0], p[1], p[2]);
1572 Bind(btexbufferID, bfboID, 27); 1575 Bind(btexbufferID, bfboID, 27);
1 -#ifndef RTS_PLANE_H  
2 -#define RTS_PLANE_H 1 +#ifndef STIM_PLANE_H
  2 +#define STIM_PLANE_H
3 3
4 #include <iostream> 4 #include <iostream>
5 #include <stim/math/vector.h> 5 #include <stim/math/vector.h>
6 -#include "rts/cuda/callable.h" 6 +#include <stim/cuda/cudatools/callable.h>
  7 +#include <stim/math/quaternion.h>
7 8
8 9
9 -namespace stim{  
10 -template <typename T, int D> class plane; 10 +namespace stim
  11 +{
  12 +template<typename T> class plane;
11 } 13 }
12 14
13 -template <typename T, int D>  
14 -CUDA_CALLABLE stim::plane<T, D> operator-(stim::plane<T, D> v);  
15 -  
16 -namespace stim{  
17 -  
18 -template <class T, int D = 3>  
19 -class plane{  
20 -  
21 - //a plane is defined by a point and a normal  
22 -  
23 -private:  
24 -  
25 - vec<T, D> P; //point on the plane  
26 - vec<T, D> N; //plane normal  
27 -  
28 - CUDA_CALLABLE void init(){  
29 - P = vec<T, D>(0, 0, 0);  
30 - N = vec<T, D>(0, 0, 1);  
31 - }  
32 -  
33 -  
34 -public:  
35 -  
36 - //default constructor  
37 - CUDA_CALLABLE plane(){  
38 - init();  
39 - } 15 +template<typename T>
  16 +CUDA_CALLABLE stim::plane<T> operator-(stim::plane<T> v);
  17 +
  18 +namespace stim
  19 +{
  20 +
  21 +template <typename T>
  22 +class plane
  23 +{
  24 + protected:
  25 + stim::vec<T> P;
  26 + stim::vec<T> N;
  27 + stim::vec<T> U;
  28 +
  29 + ///Initializes the plane with standard coordinates.
  30 + ///
  31 + CUDA_CALLABLE void init()
  32 + {
  33 + P = stim::vec<T>(0, 0, 0);
  34 + N = stim::vec<T>(0, 0, 1);
  35 + U = stim::vec<T>(1, 0, 0);
  36 + }
  37 +
  38 + public:
40 39
41 - CUDA_CALLABLE plane(vec<T, D> n, vec<T, D> p = vec<T, D>(0, 0, 0)){  
42 - P = p;  
43 - N = n.norm();  
44 - }  
45 -  
46 - CUDA_CALLABLE plane(T z_pos){  
47 - init();  
48 - P[2] = z_pos;  
49 - }  
50 -  
51 - //create a plane from three points (a triangle)  
52 - CUDA_CALLABLE plane(vec<T, D> a, vec<T, D> b, vec<T, D> c){  
53 - P = c;  
54 - N = (c - a).cross(b - a);  
55 - if(N.len() == 0) //handle the degenerate case when two vectors are the same, N = 0  
56 - N = 0;  
57 - else  
58 - N = N.norm();  
59 - }  
60 -  
61 - template< typename U >  
62 - CUDA_CALLABLE operator plane<U, D>(){  
63 -  
64 - plane<U, D> result(N, P);  
65 - return result;  
66 - }  
67 -  
68 - CUDA_CALLABLE vec<T, D> norm(){  
69 - return N;  
70 - }  
71 -  
72 - CUDA_CALLABLE vec<T, D> p(){  
73 - return P;  
74 - }  
75 -  
76 - //flip the plane front-to-back  
77 - CUDA_CALLABLE plane<T, D> flip(){  
78 - plane<T, D> result = *this;  
79 - result.N = -result.N;  
80 - return result;  
81 - }  
82 -  
83 - //determines how a vector v intersects the plane (1 = intersects front, 0 = within plane, -1 = intersects back)  
84 - CUDA_CALLABLE int face(vec<T, D> v){  
85 -  
86 - T dprod = v.dot(N); //get the dot product between v and N  
87 -  
88 - //conditional returns the appropriate value  
89 - if(dprod < 0)  
90 - return 1;  
91 - else if(dprod > 0)  
92 - return -1;  
93 - else  
94 - return 0;  
95 - }  
96 -  
97 - //determine on which side of the plane a point lies (1 = front, 0 = on the plane, -1 = back)  
98 - CUDA_CALLABLE int side(vec<T, D> p){  
99 -  
100 - vec<T, D> v = p - P; //get the vector from P to the query point p  
101 -  
102 - return face(v);  
103 - }  
104 -  
105 - //compute the component of v that is perpendicular to the plane  
106 - CUDA_CALLABLE vec<T, D> perpendicular(vec<T, D> v){  
107 - return N * v.dot(N);  
108 - }  
109 -  
110 - //compute the projection of v in the plane  
111 - CUDA_CALLABLE vec<T, D> parallel(vec<T, D> v){  
112 - return v - perpendicular(v);  
113 - }  
114 -  
115 - CUDA_CALLABLE void decompose(vec<T, D> v, vec<T, D>& para, vec<T, D>& perp){  
116 - perp = N * v.dot(N);  
117 - para = v - perp;  
118 - }  
119 -  
120 - //get both the parallel and perpendicular components of a vector v w.r.t. the plane  
121 - CUDA_CALLABLE void project(vec<T, D> v, vec<T, D> &v_par, vec<T, D> &v_perp){  
122 -  
123 - v_perp = v.dot(N);  
124 - v_par = v - v_perp;  
125 - }  
126 -  
127 - //compute the reflection of v off of the plane  
128 - CUDA_CALLABLE vec<T, D> reflect(vec<T, D> v){  
129 -  
130 - //compute the reflection using N_prime as the plane normal  
131 - vec<T, D> par = parallel(v);  
132 - vec<T, D> r = (-v) + par * 2;  
133 -  
134 - /*std::cout<<"----------------REFLECT-----------------------------"<<std::endl;  
135 - std::cout<<str()<<std::endl;  
136 - std::cout<<"v: "<<v<<std::endl;  
137 - std::cout<<"r: "<<r<<std::endl;  
138 - std::cout<<"Perpendicular: "<<perpendicular(v)<<std::endl;  
139 - std::cout<<"Parallel: "<<par<<std::endl;*/  
140 - return r;  
141 -  
142 - }  
143 -  
144 - CUDA_CALLABLE rts::plane<T, D> operator-()  
145 - {  
146 - rts::plane<T, D> p = *this;  
147 -  
148 - //negate the normal vector  
149 - p.N = -p.N;  
150 -  
151 - return p;  
152 - }  
153 -  
154 - //output a string  
155 - std::string str(){  
156 - std::stringstream ss;  
157 - ss<<"P: "<<P<<std::endl;  
158 - ss<<"N: "<<N;  
159 - return ss.str();  
160 - }  
161 -  
162 - ///////Friendship  
163 - //friend CUDA_CALLABLE rts::plane<T, D> operator- <> (rts::plane<T, D> v);  
164 -  
165 - 40 + CUDA_CALLABLE plane()
  41 + {
  42 + init();
  43 + }
  44 +
  45 + CUDA_CALLABLE plane(vec<T> n, vec<T> p = vec<T>(0, 0, 0))
  46 + {
  47 + init();
  48 + P = p;
  49 + rotate(n.norm());
  50 + }
  51 +
  52 + CUDA_CALLABLE plane(T z_pos)
  53 + {
  54 + init();
  55 + P[2] = z_pos;
  56 + }
  57 +
  58 + //create a plane from three points (a triangle)
  59 + CUDA_CALLABLE plane(vec<T> a, vec<T> b, vec<T> c)
  60 + {
  61 + init();
  62 + P = c;
  63 + stim::vec<T> n = (c - a).cross(b - a);
  64 + try
  65 + {
  66 + if(n.len() != 0)
  67 + {
  68 + rotate(n.norm());
  69 + } else {
  70 + throw 42;
  71 + }
  72 + }
  73 + catch(int i)
  74 + {
  75 + std::cerr << "No plane can be creates as all points a,b,c lie on a straight line" << std::endl;
  76 + }
  77 + }
  78 +
  79 + template< typename U >
  80 + CUDA_CALLABLE operator plane<U>()
  81 + {
  82 + plane<U> result(N, P);
  83 + return result;
  84 +
  85 + }
  86 +
  87 + CUDA_CALLABLE vec<T> n()
  88 + {
  89 + return N;
  90 + }
  91 +
  92 + CUDA_CALLABLE vec<T> p()
  93 + {
  94 + return P;
  95 + }
  96 +
  97 + CUDA_CALLABLE vec<T> u()
  98 + {
  99 + return U;
  100 + }
  101 +
  102 + ///flip the plane front-to-back
  103 + CUDA_CALLABLE plane<T> flip(){
  104 + plane<T> result = *this;
  105 + result.N = -result.N;
  106 + return result;
  107 + }
  108 +
  109 + //determines how a vector v intersects the plane (1 = intersects front, 0 = within plane, -1 = intersects back)
  110 + CUDA_CALLABLE int face(vec<T> v){
  111 +
  112 + T dprod = v.dot(N); //get the dot product between v and N
  113 +
  114 + //conditional returns the appropriate value
  115 + if(dprod < 0)
  116 + return 1;
  117 + else if(dprod > 0)
  118 + return -1;
  119 + else
  120 + return 0;
  121 + }
  122 +
  123 + //determine on which side of the plane a point lies (1 = front, 0 = on the plane, -1 = bac k)
  124 + CUDA_CALLABLE int side(vec<T> p){
  125 +
  126 + vec<T> v = p - P; //get the vector from P to the query point p
  127 +
  128 + return face(v);
  129 + }
  130 +
  131 + //compute the component of v that is perpendicular to the plane
  132 + CUDA_CALLABLE vec<T> perpendicular(vec<T> v){
  133 + return N * v.dot(N);
  134 + }
  135 +
  136 + //compute the projection of v in the plane
  137 + CUDA_CALLABLE vec<T> parallel(vec<T> v){
  138 + return v - perpendicular(v);
  139 + }
  140 +
  141 + CUDA_CALLABLE void setU(vec<T> v)
  142 + {
  143 + U = (parallel(v.norm())).norm();
  144 + }
  145 +
  146 + CUDA_CALLABLE void decompose(vec<T> v, vec<T>& para, vec<T>& perp){
  147 + perp = N * v.dot(N);
  148 + para = v - perp;
  149 + }
  150 +
  151 + //get both the parallel and perpendicular components of a vector v w.r.t. the plane
  152 + CUDA_CALLABLE void project(vec<T> v, vec<T> &v_par, vec<T> &v_perp){
  153 +
  154 + v_perp = v.dot(N);
  155 + v_par = v - v_perp;
  156 + }
  157 +
  158 + //compute the reflection of v off of the plane
  159 + CUDA_CALLABLE vec<T> reflect(vec<T> v){
  160 +
  161 + //compute the reflection using N_prime as the plane normal
  162 + vec<T> par = parallel(v);
  163 + vec<T> r = (-v) + par * 2;
  164 + return r;
  165 +
  166 + }
  167 +
  168 + CUDA_CALLABLE stim::plane<T> operator-()
  169 + {
  170 + stim::plane<T> p = *this;
  171 +
  172 + //negate the normal vector
  173 + p.N = -p.N;
  174 + return p;
  175 + }
  176 +
  177 + //output a string
  178 + std::string str(){
  179 + std::stringstream ss;
  180 + ss<<"P: "<<P<<std::endl;
  181 + ss<<"N: "<<N<<std::endl;
  182 + ss<<"U: "<<U;
  183 + return ss.str();
  184 + }
  185 +
  186 +
  187 + CUDA_CALLABLE void rotate(vec<T> n)
  188 + {
  189 + quaternion<T> q;
  190 + q.CreateRotation(N, n);
  191 +
  192 + N = q.toMatrix3() * N;
  193 + U = q.toMatrix3() * U;
  194 +
  195 + }
  196 +
  197 + CUDA_CALLABLE void rotate(vec<T> n, vec<T> &X, vec<T> &Y)
  198 + {
  199 + quaternion<T> q;
  200 + q.CreateRotation(N, n);
  201 +
  202 + N = q.toMatrix3() * N;
  203 + U = q.toMatrix3() * U;
  204 + X = q.toMatrix3() * X;
  205 + Y = q.toMatrix3() * Y;
  206 +
  207 + }
166 208
167 }; 209 };
168 - 210 +
  211 +
169 } 212 }
170 -  
171 -//arithmetic operators  
172 -  
173 -//negative operator flips the plane (front to back)  
174 -//template <typename T, int D>  
175 -  
176 -  
177 -  
178 -  
179 #endif 213 #endif
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 //enable CUDA_CALLABLE macro
5 #include <stim/cuda/cudatools/callable.h> 6 #include <stim/cuda/cudatools/callable.h>
  7 +#include <stim/math/plane.h>
6 #include <stim/math/vector.h> 8 #include <stim/math/vector.h>
7 #include <stim/math/triangle.h> 9 #include <stim/math/triangle.h>
8 -#include <stim/math/quaternion.h>  
9 #include <iostream> 10 #include <iostream>
10 #include <iomanip> 11 #include <iomanip>
11 #include <algorithm> 12 #include <algorithm>
  13 +#include <assert.h>
12 14
13 namespace stim{ 15 namespace stim{
14 16
15 //template for a rectangle class in ND space 17 //template for a rectangle class in ND space
16 -template <class T>  
17 -struct rect 18 +template <typename T>
  19 +class rect : plane <T>
18 { 20 {
19 /* 21 /*
20 ^ O 22 ^ O
21 | 23 |
22 | 24 |
23 - Y C 25 + Y P
24 | 26 |
25 | 27 |
26 O---------X---------> 28 O---------X--------->
@@ -28,106 +30,143 @@ struct rect @@ -28,106 +30,143 @@ struct rect
28 30
29 private: 31 private:
30 32
31 - stim::vec<T> C;  
32 stim::vec<T> X; 33 stim::vec<T> X;
33 stim::vec<T> Y; 34 stim::vec<T> Y;
34 35
35 - CUDA_CALLABLE void scale(T factor){  
36 - X *= factor;  
37 - Y *= factor;  
38 - }  
39 36
40 37
41 - CUDA_CALLABLE void normal(vec<T> n){ //orient the rectangle along the specified normal  
42 -  
43 - n = n.norm(); //normalize, just in case  
44 - vec<T> n_current = X.cross(Y).norm(); //compute the current normal  
45 - quaternion<T> q; //create a quaternion  
46 - q.CreateRotation(n_current, n); //initialize a rotation from n_current to n  
47 -  
48 - //apply the quaternion to the vectors and position  
49 - X = q.toMatrix3() * X;  
50 - Y = q.toMatrix3() * Y;  
51 - }  
52 -  
53 - CUDA_CALLABLE void init(){  
54 - C = vec<T>(0, 0, 0);  
55 - X = vec<T>(1, 0, 0);  
56 - Y = vec<T>(0, 1, 0);  
57 - }  
58 38
59 public: 39 public:
60 40
61 - CUDA_CALLABLE rect(){ 41 + using stim::plane<T>::n;
  42 + using stim::plane<T>::P;
  43 + using stim::plane<T>::N;
  44 + using stim::plane<T>::U;
  45 + using stim::plane<T>::rotate;
  46 +
  47 + ///base constructor.
  48 + CUDA_CALLABLE rect()
  49 + : plane<T>()
  50 + {
62 init(); 51 init();
63 } 52 }
64 53
65 - //create a rectangle given a size and position  
66 - CUDA_CALLABLE rect(T size, T z_pos = (T)0){ 54 + ///create a rectangle given a size and position in Z space.
  55 + ///@param size: size of the rectangle in ND space.
  56 + ///@param z_pos z coordinate of the rectangle.
  57 + CUDA_CALLABLE rect(T size, T z_pos = (T)0)
  58 + : plane<T>(z_pos)
  59 + {
67 init(); //use the default setup 60 init(); //use the default setup
68 scale(size); //scale the rectangle 61 scale(size); //scale the rectangle
69 - C[2] = z_pos;  
70 } 62 }
71 63
72 64
73 - //create a rectangle from a center point, normal, and size  
74 - CUDA_CALLABLE rect(vec<T> c, vec<T> n = vec<T>(0, 0, 1)){ 65 + ///create a rectangle from a center point, normal
  66 + ///@param c: x,y,z location of the center.
  67 + ///@param n: x,y,z direction of the normal.
  68 + CUDA_CALLABLE rect(vec<T> c, vec<T> n = vec<T>(0, 0, 1))
  69 + : plane<T>()
  70 + {
75 init(); //start with the default setting 71 init(); //start with the default setting
76 - C = c;  
77 normal(n); //orient 72 normal(n); //orient
78 } 73 }
79 74
  75 + ///create a rectangle from a center point, normal, and size
  76 + ///@param c: x,y,z location of the center.
  77 + ///@param s: size of the rectangle.
  78 + ///@param n: x,y,z direction of the normal.
  79 + CUDA_CALLABLE rect(vec<T> c, T s, vec<T> n = vec<T>(0, 0, 1))
  80 + : plane<T>()
  81 + {
  82 + init(); //start with the default setting
  83 + scale(s);
  84 + center(c);
  85 + rotate(n, X, Y);
  86 + }
  87 +
  88 + ///creates a rectangle from a centerpoint and an X and Y direction vectors.
  89 + ///@param center: x,y,z location of the center.
  90 + ///@param directionX: u,v,w direction of the X vector.
  91 + ///@param directionY: u,v,w direction of the Y vector.
80 CUDA_CALLABLE rect(vec<T> center, vec<T> directionX, vec<T> directionY ) 92 CUDA_CALLABLE rect(vec<T> center, vec<T> directionX, vec<T> directionY )
  93 + : plane<T>((directionX.cross(directionY)).norm(),center)
81 { 94 {
82 - C = center;  
83 X = directionX; 95 X = directionX;
84 Y = directionY; 96 Y = directionY;
85 } 97 }
86 98
  99 + ///creates a rectangle from a size, centerpoint, X, and Y direction vectors.
  100 + ///@param size of the rectangle in ND space.
  101 + ///@param center: x,y,z location of the center.
  102 + ///@param directionX: u,v,w direction of the X vector.
  103 + ///@param directionY: u,v,w direction of the Y vector.
87 CUDA_CALLABLE rect(T size, vec<T> center, vec<T> directionX, vec<T> directionY ) 104 CUDA_CALLABLE rect(T size, vec<T> center, vec<T> directionX, vec<T> directionY )
  105 + : plane<T>((directionX.cross(directionY)).norm(),center)
88 { 106 {
89 - C = center;  
90 X = directionX; 107 X = directionX;
91 Y = directionY; 108 Y = directionY;
92 scale(size); 109 scale(size);
93 } 110 }
94 -  
95 - CUDA_CALLABLE rect(vec<T> size, vec<T> center, vec<T> directionX, vec<T> directionY ) 111 +
  112 + ///creates a rectangle from a size, centerpoint, X, and Y direction vectors.
  113 + ///@param size of the rectangle in ND space, size[0] = size in X, size[1] = size in Y.
  114 + ///@param center: x,y,z location of the center.
  115 + ///@param directionX: u,v,w direction of the X vector.
  116 + ///@param directionY: u,v,w direction of the Y vector.
  117 + CUDA_CALLABLE rect(vec<T> size, vec<T> center, vec<T> directionX, vec<T> directionY)
  118 + : plane<T>((directionX.cross(directionY)).norm(), center)
96 { 119 {
97 - C = center;  
98 X = directionX; 120 X = directionX;
99 Y = directionY; 121 Y = directionY;
100 scale(size[0], size[1]); 122 scale(size[0], size[1]);
101 } 123 }
102 -  
103 - CUDA_CALLABLE void scale(T factor1, T factor2){ 124 +
  125 + CUDA_CALLABLE void scale(T factor){
  126 + X *= factor;
  127 + Y *= factor;
  128 + }
  129 +
  130 + ///scales a rectangle in ND space.
  131 + ///@param factor1: size of the scale in the X-direction.
  132 + ///@param factor2: size of the scale in the Y-direction.
  133 + CUDA_CALLABLE void scale(T factor1, T factor2)
  134 + {
104 X *= factor1; 135 X *= factor1;
105 Y *= factor2; 136 Y *= factor2;
106 } 137 }
107 138
  139 + ///@param n; vector with the normal.
  140 + ///Orients the rectangle along the normal n.
  141 + CUDA_CALLABLE void normal(vec<T> n)
  142 + {
  143 + //orient the rectangle along the specified normal
  144 + rotate(n, X, Y);
  145 + }
  146 +
  147 + ///general init method that sets a general rectangle.
  148 + CUDA_CALLABLE void init()
  149 + {
  150 + X = vec<T>(1, 0, 0);
  151 + Y = vec<T>(0, 1, 0);
  152 + }
  153 +
108 //boolean comparison 154 //boolean comparison
109 bool operator==(const rect<T> & rhs) 155 bool operator==(const rect<T> & rhs)
110 { 156 {
111 - if(C == rhs.C && X == rhs.X && Y == rhs.Y) 157 + if(P == rhs.P && X == rhs.X && Y == rhs.Y)
112 return true; 158 return true;
113 else 159 else
114 return false; 160 return false;
115 } 161 }
116 162
117 - /*******************************************  
118 - Return the normal for the rect  
119 - *******************************************/  
120 - CUDA_CALLABLE stim::vec<T> n()  
121 - {  
122 - return (X.cross(Y)).norm();  
123 - }  
124 163
125 //get the world space value given the planar coordinates a, b in [0, 1] 164 //get the world space value given the planar coordinates a, b in [0, 1]
126 CUDA_CALLABLE stim::vec<T> p(T a, T b) 165 CUDA_CALLABLE stim::vec<T> p(T a, T b)
127 { 166 {
128 stim::vec<T> result; 167 stim::vec<T> result;
129 //given the two parameters a, b = [0 1], returns the position in world space 168 //given the two parameters a, b = [0 1], returns the position in world space
130 - vec<T> A = C - X * (T)0.5 - Y * (T)0.5; 169 + vec<T> A = this->P - X * (T)0.5 - Y * (T)0.5;
131 result = A + X * a + Y * b; 170 result = A + X * a + Y * b;
132 171
133 return result; 172 return result;
@@ -142,16 +181,16 @@ public: @@ -142,16 +181,16 @@ public:
142 std::string str() 181 std::string str()
143 { 182 {
144 std::stringstream ss; 183 std::stringstream ss;
145 - vec<T> A = C - X * (T)0.5 - Y * (T)0.5; 184 + vec<T> A = P - X * (T)0.5 - Y * (T)0.5;
146 ss<<std::left<<"B="<<std::setfill('-')<<std::setw(20)<<A + Y<<">"<<"C="<<A + Y + X<<std::endl; 185 ss<<std::left<<"B="<<std::setfill('-')<<std::setw(20)<<A + Y<<">"<<"C="<<A + Y + X<<std::endl;
147 ss<<std::setfill(' ')<<std::setw(23)<<"|"<<"|"<<std::endl<<std::setw(23)<<"|"<<"|"<<std::endl; 186 ss<<std::setfill(' ')<<std::setw(23)<<"|"<<"|"<<std::endl<<std::setw(23)<<"|"<<"|"<<std::endl;
148 ss<<std::left<<"A="<<std::setfill('-')<<std::setw(20)<<A<<">"<<"D="<<A + X; 187 ss<<std::left<<"A="<<std::setfill('-')<<std::setw(20)<<A<<">"<<"D="<<A + X;
149 188
150 - return ss.str(); 189 + return ss.str();
151 190
152 } 191 }
153 192
154 - //scales the rectangle by a value rhs 193 + ///multiplication operator scales the rectangle by a value rhs.
155 CUDA_CALLABLE rect<T> operator*(T rhs) 194 CUDA_CALLABLE rect<T> operator*(T rhs)
156 { 195 {
157 //scales the plane by a scalar value 196 //scales the plane by a scalar value
@@ -164,36 +203,44 @@ public: @@ -164,36 +203,44 @@ public:
164 203
165 } 204 }
166 205
167 - //computes the distance between the specified point and this rectangle 206 + ///computes the distance between the specified point and this rectangle.
  207 + ///@param p: x, y, z coordinates of the point to calculate distance to.
168 CUDA_CALLABLE T dist(vec<T> p) 208 CUDA_CALLABLE T dist(vec<T> p)
169 { 209 {
170 //compute the distance between a point and this rect 210 //compute the distance between a point and this rect
171 211
172 - vec<T> A = C - X * (T)0.5 - Y * (T)0.5; 212 + vec<T> A = P - X * (T)0.5 - Y * (T)0.5;
173 213
174 - //first break the rect up into two triangles  
175 - triangle<T> T0(A, A+X, A+Y);  
176 - triangle<T> T1(A+X+Y, A+X, A+Y); 214 + //first break the rect up into two triangles
  215 + triangle<T> T0(A, A+X, A+Y);
  216 + triangle<T> T1(A+X+Y, A+X, A+Y);
177 217
178 218
179 - T d0 = T0.dist(p);  
180 - T d1 = T1.dist(p); 219 + T d0 = T0.dist(p);
  220 + T d1 = T1.dist(p);
181 221
182 - if(d0 < d1)  
183 - return d0;  
184 - else  
185 - return d1; 222 + if(d0 < d1)
  223 + return d0;
  224 + else
  225 + return d1;
  226 + }
  227 +
  228 + CUDA_CALLABLE T center(vec<T> p)
  229 + {
  230 + this->P = p;
186 } 231 }
187 232
  233 + ///Returns the maximum distance of the rectangle from a point p to the sides of the rectangle.
  234 + ///@param p: x, y, z point.
188 CUDA_CALLABLE T dist_max(vec<T> p) 235 CUDA_CALLABLE T dist_max(vec<T> p)
189 { 236 {
190 - vec<T> A = C - X * (T)0.5 - Y * (T)0.5;  
191 - T da = (A - p).len();  
192 - T db = (A+X - p).len();  
193 - T dc = (A+Y - p).len();  
194 - T dd = (A+X+Y - p).len(); 237 + vec<T> A = P - X * (T)0.5 - Y * (T)0.5;
  238 + T da = (A - p).len();
  239 + T db = (A+X - p).len();
  240 + T dc = (A+Y - p).len();
  241 + T dd = (A+X+Y - p).len();
195 242
196 - return std::max( da, std::max(db, std::max(dc, dd) ) ); 243 + return std::max( da, std::max(db, std::max(dc, dd) ) );
197 } 244 }
198 }; 245 };
199 246