### Blame view

stim/math/triangle.h 4.14 KB
 ```1 2 3 4``` `````` #ifndef RTS_TRIANGLE_H #define RTS_TRIANGLE_H //enable CUDA_CALLABLE macro `````` `5` `````` #include `````` `6` `````` #include `````` ```7 8 9 10``` `````` #include namespace stim{ `````` `11` `````` template `````` ```12 13 14 15 16 17 18 19 20 21 22 23 24 25``` `````` struct triangle { /* A------>B | / | / | / | / | / | / C */ private: `````` ```26 27 28``` `````` vec A; vec B; vec C; `````` `29` `````` `````` `30` `````` CUDA_CALLABLE vec _p(T s, T t) `````` ```31 32``` `````` { //This function returns the point specified by p = A + s(B-A) + t(C-A) `````` ```33 34``` `````` vec E0 = B-A; vec E1 = C-A; `````` ```35 36 37 38 39 40 41 42 43 44 45 46 47 48``` `````` return A + s*E0 + t*E1; } public: CUDA_CALLABLE triangle() { } `````` `49` `````` CUDA_CALLABLE triangle(vec a, vec b, vec c) `````` ```50 51 52 53 54 55``` `````` { A = a; B = b; C = c; } `````` `56` `````` CUDA_CALLABLE stim::vec operator()(T s, T t) `````` ```57 58 59 60``` `````` { return _p(s, t); } `````` `61` `````` CUDA_CALLABLE vec nearest(vec p) `````` ```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 `````` ```66 67 68``` `````` vec E0 = B-A; vec E1 = C-A; vec D = A - p; `````` ```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: "<= 0 ? 0 : ( -e >= c ? 1 : -e/c ) ); //done } } else if(t < 0) { //region 5 //std::cout<<"Region 5"<= 0 ? 0 : ( -d >= a ? 1 : -d/a ) ); t = 0; //done } else { //region 0 //std::cout<<"Region 0"<= denom ? 1 : numer/denom ); } t = 1 - s; //done } } //std::cout<<"s: "< p) `````` `179` `````` { `````` `180` `````` vec n = nearest(p); `````` ```181 182 183 184 185 186 187 188``` `````` return (p - n).len(); } }; } #endif ``````