MCPcopy Create free account
hub / github.com/assaultcube/AC / intersecttriangle

Function intersecttriangle

source/src/weapon.cpp:382–402  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

380}
381
382inline bool intersecttriangle(const vec &from, const vec &dir, const vec &v0, const vec &v1, const vec &v2, vec *end, float *_t) // precise but rather expensive, based on Moeller–Trumbore intersection algorithm
383{
384 const float EPSILON = 0.00001f;
385 vec edge1 = v1; edge1.sub(v0); // edge1 = v1 - v0
386 vec edge2 = v2; edge2.sub(v0); // edge2 = v2 - v0
387 vec pvec; pvec.cross(dir, edge2); // pvec = dir x edge2
388 float det = edge1.dot(pvec); // det = edge1 * pvec
389 if(fabs(det) < EPSILON) return false;
390 float invdet = 1.0f / det;
391 vec tvec = from; tvec.sub(v0); // tvec = from - v0
392 float u = invdet * tvec.dot(pvec); // u = tvec * pvec / det
393 if(u < 0.0f || u > 1.0f) return false;
394 vec qvec; qvec.cross(tvec, edge1); // qvec = tvec x edge1
395 float v = invdet * dir.dot(qvec); // v = dir * qvec / det
396 if(v < 0.0f || u + v > 1.0f) return false;
397 float t = invdet * edge2.dot(qvec); // t = edge2 * qvec / det
398 if(t < EPSILON) return false;
399 if(_t) *_t = t; // 0..1 if intersection is between from and to
400 if(end) *end = dir, end->mul(t).add(from); // calculate point of intersection
401 return true;
402}
403
404inline bool intersecttriangle2(const vec &from, const vec &to, const vec &v0, const vec &v1, const vec &v2, vec *end, float *_t)
405{

Callers 2

intersecttriangle2Function · 0.85
intersectgeometryFunction · 0.85

Calls 3

mulMethod · 0.80
dotMethod · 0.45
addMethod · 0.45

Tested by

no test coverage detected