#ifndef RTS_TRIANGLE_H #define RTS_TRIANGLE_H //enable CUDA_CALLABLE macro #include #include #include namespace stim{ template struct triangle { /* A------>B | / | / | / | / | / | / C */ private: vec A; vec B; vec C; CUDA_CALLABLE vec _p(T s, T t) { //This function returns the point specified by p = A + s(B-A) + t(C-A) vec E0 = B-A; vec E1 = C-A; return A + s*E0 + t*E1; } public: CUDA_CALLABLE triangle() { } CUDA_CALLABLE triangle(vec a, vec b, vec c) { A = a; B = b; C = c; } CUDA_CALLABLE stim::vec operator()(T s, T t) { return _p(s, t); } CUDA_CALLABLE vec nearest(vec p) { //comptue the distance between a point and this triangle // This code is adapted from: http://www.geometrictools.com/Documentation/DistancePoint3Triangle3.pdf vec E0 = B-A; vec E1 = C-A; vec D = A - p; 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) { vec n = nearest(p); return (p - n).len(); } }; } #endif