MCPcopy Create free account
hub / github.com/Kitware/VTK / PointInTriangle

Method PointInTriangle

Common/DataModel/vtkTriangle.cxx:1567–1613  ·  view source on GitHub ↗

------------------------------------------------------------------------------ Given a point x, determine whether it is inside (within the tolerance squared, tol2) the triangle defined by the three coordinate values p1, p2, p3. Method is via comparing dot products. (Note: in current implementation the tolerance only works in the neighborhood of the three vertices of the triangle.

Source from the content-addressed store, hash-verified

1565// (Note: in current implementation the tolerance only works in the
1566// neighborhood of the three vertices of the triangle.
1567int vtkTriangle::PointInTriangle(
1568 const double x[3], const double p1[3], const double p2[3], const double p3[3], double tol2)
1569{
1570 double x1[3], x2[3], x3[3], v13[3], v21[3], v32[3];
1571 double n1[3], n2[3], n3[3];
1572
1573 // Compute appropriate vectors
1574 //
1575 for (int i = 0; i < 3; i++)
1576 {
1577 x1[i] = x[i] - p1[i];
1578 x2[i] = x[i] - p2[i];
1579 x3[i] = x[i] - p3[i];
1580 v13[i] = p1[i] - p3[i];
1581 v21[i] = p2[i] - p1[i];
1582 v32[i] = p3[i] - p2[i];
1583 }
1584
1585 // See whether intersection point is within tolerance of a vertex.
1586 //
1587 if ((x1[0] * x1[0] + x1[1] * x1[1] + x1[2] * x1[2]) <= tol2 ||
1588 (x2[0] * x2[0] + x2[1] * x2[1] + x2[2] * x2[2]) <= tol2 ||
1589 (x3[0] * x3[0] + x3[1] * x3[1] + x3[2] * x3[2]) <= tol2)
1590 {
1591 return 1;
1592 }
1593
1594 // If not near a vertex, check whether point is inside of triangular face.
1595 //
1596 // Obtain normal off of triangular face
1597 //
1598 vtkMath::Cross(x1, v13, n1);
1599 vtkMath::Cross(x2, v21, n2);
1600 vtkMath::Cross(x3, v32, n3);
1601
1602 // Check whether ALL the three normals go in same direction
1603 //
1604 if ((vtkMath::Dot(n1, n2) >= 0.0) && (vtkMath::Dot(n2, n3) >= 0.0) &&
1605 (vtkMath::Dot(n1, n3) >= 0.0))
1606 {
1607 return 1;
1608 }
1609 else
1610 {
1611 return 0;
1612 }
1613}
1614
1615//------------------------------------------------------------------------------
1616double vtkTriangle::GetParametricDistance(const double pcoords[3])

Callers

nothing calls this directly

Calls 2

DotFunction · 0.70
CrossFunction · 0.50

Tested by

no test coverage detected