1 #ifndef TAG_INTERSECTION_H_
2 #define TAG_INTERSECTION_H_
6 #include <TooN/helpers.h>
20 template<
typename A,
typename B,
typename C,
typename D,
typename ABase,
typename BBase,
typename CBase,
typename DBase>
21 inline bool intersect_plane_line(
const TooN::Vector<3,A, ABase> & normal,
const double d,
const TooN::Vector<3,B, BBase> & p1,
const TooN::Vector<3,C, CBase> & p2, TooN::Vector<3,D, DBase> & i){
22 const double EPSILON = 0.000001;
24 TooN::Vector<3> dir = p2 - p1;
25 double c = (normal * dir);
26 if( fabs(c) < EPSILON )
28 double t = (d - normal * p1) / c;
36 const TooN::Vector<3> & vert0,
const TooN::Vector<3> & vert1,
const TooN::Vector<3> & vert2,
37 double & t,
double & u,
double & v)
39 const double EPSILON = 0.000001;
44 TooN::Vector<3> edge1 = vert1 - vert0;
45 TooN::Vector<3> edge2 = vert2 - vert0;
48 TooN::Vector<3> pvec = dir ^ edge2;
53 if (det > -EPSILON && det < EPSILON)
58 TooN::Vector<3> tvec = orig - vert0;
61 u = (tvec * pvec) * inv_det;
62 if (u < 0.0 || u > 1.0)
66 TooN::Vector<3> qvec = tvec ^ edge1;
69 v = (dir * qvec) * inv_det;
70 if (v < 0.0 || u + v > 1.0)
74 t = (edge2 * qvec) * inv_det;
81 const TooN::Vector<3> & vert0,
const TooN::Vector<3> & vert1,
const TooN::Vector<3> & vert2,
82 double & t,
double & u,
double & v)
84 const double EPSILON = 0.000001;
89 TooN::Vector<3> edge1 = vert1 - vert0;
90 TooN::Vector<3> edge2 = vert2 - vert0;
93 TooN::Vector<3> pvec = dir ^ edge2;
102 TooN::Vector<3> tvec = orig - vert0;
106 if (u < 0.0 || u > det)
110 TooN::Vector<3> qvec = tvec ^ edge1;
114 if (v < 0.0 || u + v > det)
128 inline bool intersect_triangles(
const TooN::Vector<3> & v1,
const TooN::Vector<3> & v2,
const TooN::Vector<3> & v3,
129 const TooN::Vector<3> & w1,
const TooN::Vector<3> & w2,
const TooN::Vector<3> & w3,
130 TooN::Vector<3> & p1, TooN::Vector<3> & p2 ){
131 const double EPSILON = 0.000001;
133 const TooN::Vector<3> * tv[3];
134 tv[0] = &v1; tv[1] = &v2; tv[2] = &v3;
135 const TooN::Vector<3> * tw[3];
136 tw[0] = &w1; tw[1] = &w2; tw[2] = &w3;
139 TooN::Vector<3> nv = (v2 - v1) ^ ( v3 - v1 );
142 double t1w = nv * w1 - dv;
143 double t2w = nv * w2 - dv;
144 double t3w = nv * w3 - dv;
146 if( (t1w < -EPSILON && t2w < -EPSILON && t3w < -EPSILON) ||
147 (t1w > EPSILON && t2w > EPSILON && t3w > EPSILON) ) {
152 TooN::Vector<3> nw = (w2 - w1) ^ ( w3 - w1 );
155 double t1v = nw * v1 - dw;
156 double t2v = nw * v2 - dw;
157 double t3v = nw * v3 - dw;
159 if( (t1v < -EPSILON && t2v < -EPSILON && t3v < -EPSILON) ||
160 (t1v > EPSILON && t2v > EPSILON && t3v > EPSILON) ) {
165 TooN::Vector<3> d = nv ^ nw;
175 else if( t1v * t3v > 0 )
181 else if( t1w * t3w > 0 )
188 TooN::Matrix<4,3> intersections;
195 TooN::Vector<4> proj = intersections * d;
198 int minIndex, maxIndex;
199 if( proj[0] < proj[1] ){
200 if( proj[2] < proj[3] ){
201 if( proj[0] > proj[2]){
206 if( proj[1] < proj[3] ){
212 if( proj[0] > proj[3]){
217 if( proj[1] < proj[2] ){
224 if( proj[2] < proj[3] ){
225 if( proj[1] > proj[2]){
230 if( proj[0] < proj[3] ){
236 if( proj[1] > proj[3]){
241 if( proj[0] < proj[2] ){
249 if( proj[minIndex] > proj[maxIndex] )
252 p1 = intersections[minIndex];
253 p2 = intersections[maxIndex];