Blame view

stim/math/triangle.h 4.14 KB
81e0d221   David Mayerich   separated executa...
1
2
3
4
  #ifndef RTS_TRIANGLE_H

  #define RTS_TRIANGLE_H

  

  //enable CUDA_CALLABLE macro

7d1d153a   Pavel Govyadinov   fixed the include...
5
  #include <stim/cuda/cudatools/callable.h>

cce7daf9   Pavel Govyadinov   added glObj.h to ...
6
  #include <stim/math/vector.h>

81e0d221   David Mayerich   separated executa...
7
8
9
10
  #include <iostream>

  

  namespace stim{

  

5eeaf941   Pavel Govyadinov   changer to the ba...
11
  template <class T>

81e0d221   David Mayerich   separated executa...
12
13
14
15
16
17
18
19
20
21
22
23
24
25
  struct triangle

  {

      /*

          A------>B

          |      /

          |     /

          |    /

          |   /

          |  /

          | /

          C

      */

      private:

  

5eeaf941   Pavel Govyadinov   changer to the ba...
26
27
28
      vec<T> A;

      vec<T> B;

      vec<T> C;

81e0d221   David Mayerich   separated executa...
29
  

5eeaf941   Pavel Govyadinov   changer to the ba...
30
      CUDA_CALLABLE vec<T> _p(T s, T t)

81e0d221   David Mayerich   separated executa...
31
32
      {

          //This function returns the point specified by p = A + s(B-A) + t(C-A)

5eeaf941   Pavel Govyadinov   changer to the ba...
33
34
          vec<T> E0 = B-A;

          vec<T> E1 = C-A;

81e0d221   David Mayerich   separated executa...
35
36
37
38
39
40
41
42
43
44
45
46
47
48
  

          return A + s*E0 + t*E1;

      }

  

  

      public:

  

  

  

      CUDA_CALLABLE triangle()

  	{

  

  	}

  

5eeaf941   Pavel Govyadinov   changer to the ba...
49
  	CUDA_CALLABLE triangle(vec<T> a, vec<T> b, vec<T> c)

81e0d221   David Mayerich   separated executa...
50
51
52
53
54
55
  	{

  		A = a;

  		B = b;

  		C = c;

  	}

  

5eeaf941   Pavel Govyadinov   changer to the ba...
56
  	CUDA_CALLABLE stim::vec<T> operator()(T s, T t)

81e0d221   David Mayerich   separated executa...
57
58
59
60
  	{

          return _p(s, t);

  	}

  

5eeaf941   Pavel Govyadinov   changer to the ba...
61
  	CUDA_CALLABLE vec<T> nearest(vec<T> p)

81e0d221   David Mayerich   separated executa...
62
63
64
65
  	{

          //comptue the distance between a point and this triangle

          //  This code is adapted from: http://www.geometrictools.com/Documentation/DistancePoint3Triangle3.pdf

  

5eeaf941   Pavel Govyadinov   changer to the ba...
66
67
68
          vec<T> E0 = B-A;

          vec<T> E1 = C-A;

          vec<T> D = A - p;

81e0d221   David Mayerich   separated executa...
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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
  

          T a = E0.dot(E0);

          T b = E0.dot(E1);

          T c = E1.dot(E1);

          T d = E0.dot(D);

          T e = E1.dot(D);

          //T f = D.dot(D);

  

          T det = a*c - b*b;

          T s = b*e - c*d;

          T t = b*d - a*e;

  

          /*std::cout<<"E0: "<<E0<<std::endl;

          std::cout<<"E1: "<<E1<<std::endl;

          std::cout<<"a: "<<a<<std::endl;

          std::cout<<"b: "<<b<<std::endl;

          std::cout<<"c: "<<c<<std::endl;

          std::cout<<"d: "<<d<<std::endl;

          std::cout<<"e: "<<e<<std::endl;

          std::cout<<"f: "<<f<<std::endl;

          std::cout<<"det: "<<det<<std::endl;

          std::cout<<"s: "<<s<<std::endl;

          std::cout<<"t: "<<t<<std::endl;*/

  

  

          if( s+t <= det)

          {

              if(s < 0)

              {

                  if(t < 0)

                  {

                      //region 4

                      //std::cout<<"Region 4"<<std::endl;

                      s = 0;

                      t = 0;

                      //done?

                  }

                  else

                  {

                      //region 3

                      //std::cout<<"Region 3"<<std::endl;

                      s=0;

                      t = ( e >= 0 ? 0 : ( -e >= c ? 1 : -e/c ) );

                      //done

                  }

              }

              else if(t < 0)

              {

                  //region 5

                  //std::cout<<"Region 5"<<std::endl;

                  s = ( d >= 0 ? 0 : ( -d >= a ? 1 : -d/a ) );

                  t = 0;

                  //done

              }

              else

              {

                  //region 0

                  //std::cout<<"Region 0"<<std::endl;

                  T invDet = (T)1.0/det;

                  s *= invDet;

                  t *= invDet;

                  //done

              }

          }

          else

          {

              if(s < 0)

              {

                  //region 2

                  //std::cout<<"Region 2"<<std::endl;

                  s = 0;

                  t = 1;

                  //done?

  

              }

              else if(t < 0)

              {

                  //region 6

                  //std::cout<<"Region 6"<<std::endl;

                  s = 1;

                  t = 0;

                  //done?

              }

              else

              {

                  //region 1

                  //std::cout<<"Region 1"<<std::endl;

                  T numer = c + e - b - d;

                  if( numer <= 0 )

                      s = 0;

                  else

                  {

                      T denom = a - 2 * b + c;

                      s = ( numer >= denom ? 1 : numer/denom );

                  }

                  t = 1 - s;

                  //done

              }

          }

  

          //std::cout<<"s: "<<s<<std::endl;

          //std::cout<<"t: "<<t<<std::endl;

  

          //std::cout<<"p: "<<_p(s, t)<<std::endl;

  

  		return _p(s, t);

  

  	}

  

5eeaf941   Pavel Govyadinov   changer to the ba...
178
  	CUDA_CALLABLE T dist(vec<T> p)

81e0d221   David Mayerich   separated executa...
179
  	{

5eeaf941   Pavel Govyadinov   changer to the ba...
180
          vec<T> n = nearest(p);

81e0d221   David Mayerich   separated executa...
181
182
183
184
185
186
187
188
  

          return (p - n).len();

  	}

  };

  

  }

  

  #endif