/** * Test if a given 2D point (x, y) is inside a triangle of this TIN (given * by index). If so, return true and give the elevation value by reference. */ bool vtTin::TestTriangle(int tri, const DPoint2 &p, float &fAltitude) const { // get points const int v0 = m_tri[tri*3]; const int v1 = m_tri[tri*3+1]; const int v2 = m_tri[tri*3+2]; const DPoint2 &p1 = m_vert[v0]; const DPoint2 &p2 = m_vert[v1]; const DPoint2 &p3 = m_vert[v2]; // First try to identify which triangle if (PointInTriangle(p, p1, p2, p3)) { double bary[3]; if (BarycentricCoords(p1, p2, p3, p, bary)) { // compute barycentric combination of function values at vertices const double val = bary[0] * m_z[v0] + bary[1] * m_z[v1] + bary[2] * m_z[v2]; fAltitude = (float) val; return true; } } return false; }
// Returns true iff the point is inside or on the exterior of the triangle and not equal to one of the triangle vertices. bool point_triangle_intersection(ofVec3f &t1, ofVec3f &t2, ofVec3f &t3, ofVec3f &point) { if(point == t1 || point == t2 || point == t3) { return false; } float c1, c2, c3; BarycentricCoords(t1, t2, t3, point, c1, c2, c3); if(0 < c1 && c1 < 1 && 0 < c2 && c2 < 1 && 0 < c3 && c3 < 1) { return true; } }