Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added test function MeshClosestPoint #1

Merged
merged 19 commits into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 116 additions & 0 deletions src/GShark/Geometry/Mesh.cs
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,122 @@ private FaceData CountFaceEdges()
return data;
}

/// <summary>
/// Checks if point is in or out a triangle.
/// </summary>
/// <param name="projection">Point to check.</param>
/// <param name="trianglePoints">Triangle vertices.</param>
/// <returns>Boolean result (In - True , Out - False).</returns>
/// <exception cref="ArgumentOutOfRangeException">Thrown when trianglePoints is not 3 points.</exception>
private bool IsPointInTriangle(Point3 projection, Point3[] trianglePoints)
{
if (trianglePoints.Length != 3)
throw new ArgumentOutOfRangeException($"{nameof(trianglePoints)} must be 3.");

//edges vectors of the triangle
Vector3 v0 = new Vector3(trianglePoints[1] - trianglePoints[0]);
Vector3 v1 = new Vector3(trianglePoints[2] - trianglePoints[1]);
Vector3 v2 = new Vector3(trianglePoints[0] - trianglePoints[2]);

var edges_vectors = new Vector3[] { v0, v1, v2 };

// normal to the triangle
Vector3 n = Vector3.CrossProduct(v0, v1);

// Creates a list to store the dot products of the vectors
// Negative dot product means that the point is ouside of the triangle edges
List<double> dotProducts = new List<double>();

for (int i = 0; i < 3; i++)
{
Vector3 w = new Vector3(projection - trianglePoints[i]);
Vector3 t = Vector3.CrossProduct(edges_vectors[i], w);
double dotProduct = n * t;
dotProducts.Add(dotProduct);
}

return ((dotProducts[0] > 0) && (dotProducts[1] > 0) && (dotProducts[2] > 0));
}


/// <summary>
/// Finds the closest point to a triangle.
/// </summary>
/// <param name="trianglePoints">Triangle vertices.</param>
/// <param name="point">Test point.</param>
/// <returns>Closest point to the triangle.</returns>
/// <exception cref="ArgumentOutOfRangeException">Thrown when trianglePoints is not 3 points.</exception>
private Point3 ClosestPointToTriangle(Point3[] trianglePoints, Point3 point)
{
if (trianglePoints.Length != 3)
throw new ArgumentOutOfRangeException($"{nameof(trianglePoints)} must be 3.");
Sophielelerre marked this conversation as resolved.
Show resolved Hide resolved
// Creates Plane from 3 points
// TODO: checks if the triangle degenerate into a line or a point before this
Plane facePlane = new Plane(trianglePoints[0], trianglePoints[1], trianglePoints[2]);
// Find projection of point of the plane
Point3 closestPoint = facePlane.ClosestPoint(point, out _);
// if the projection of the point is inside the triangle, we return the closest point
if (IsPointInTriangle(closestPoint, trianglePoints))
return closestPoint;

// If not, we create a line for each triangle edges
// and then find the closest point on each edge
Line segmentAB = new Line(trianglePoints[0], trianglePoints[1]);
Line segmentBC = new Line(trianglePoints[1], trianglePoints[2]);
Line segmentCA = new Line(trianglePoints[2], trianglePoints[0]);
List<Line> segments = new List<Line>() { segmentAB, segmentBC, segmentCA };
List<Point3> edgesClosestPoints = new List<Point3>();
foreach (Line segment in segments)
{
Point3 segmentClosestPoint = segment.ClosestPoint(point);
edgesClosestPoints.Add(segmentClosestPoint);
}
return Point3.CloudClosestPoint(edgesClosestPoints, point);
}

/// <summary>
/// Finds the closest point to a mesh.
/// </summary>
/// <param name="point">Test point.</param>
/// <returns>Mesh Closest Point.</returns>
/// <remark>Method might not work with concave Ngon faces.</remark>
public Point3 ClosestPoint(Point3 point)
{
List<MeshVertex> meshVertices = Vertices;
// All faces check
IEnumerable<MeshFace> allFaces = this.Faces;
var closerPointToFaceList = new List<Point3>();

foreach (MeshFace face in allFaces)
{
// Retrives the vertices associated with each face
List<MeshVertex> faceVertices = face.AdjacentVertices();

if (faceVertices.Count == 3)
{
var trianglePoints = new Point3[] { faceVertices[0], faceVertices[1], faceVertices[2] };
Point3 closestPoint0 = ClosestPointToTriangle(trianglePoints, point);
closerPointToFaceList.Add(closestPoint0);
}
else
{
// Ngon triangulation using the vertices' centroid and consecutive vertices to create the triangles
Point3 ngonCentre = Point3.AveragePoint(faceVertices);
var TrianglesCP = new List<Point3>();

for (int i = 0; i < faceVertices .Count; i++)
{
Point3[] trianglePoints = new Point3[] { ngonCentre, faceVertices[i], faceVertices[(i + 1)% faceVertices.Count] };
Point3 ClosestPointToNgon = ClosestPointToTriangle(trianglePoints, point);
TrianglesCP.Add(ClosestPointToNgon);
}
Point3 ClosestPoint = Point3.CloudClosestPoint(TrianglesCP, point);
closerPointToFaceList.Add(ClosestPoint);
}
}
Point3 meshClosestPoint = Point3.CloudClosestPoint(closerPointToFaceList, point);
return new Point3(meshClosestPoint);
}

/// <summary>
/// Type of mesh (Triangular, Quad, Ngon or Error).
Expand Down
41 changes: 40 additions & 1 deletion src/GShark/Geometry/Point3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,46 @@ public int InPolygon(Polygon polygon)
}
return inside ? 1 : -1;
}


/// <summary>
/// Finds average point from a cloud of points.
/// </summary>
/// <param name="points"></param>
/// <returns>The average point.</returns>
public static Point3 AveragePoint (IEnumerable<Point3> points)
{
Point3 pointTotSum = Point3.Origin;
foreach (Point3 point in points)
{
pointTotSum += point;
}
Point3 average = pointTotSum / points.Count();
return average;
}

/// <summary>
/// Finds closest point to a test point from a cloud of points.
/// </summary>
/// <param name="cloud">Cloud of points from which the closest is chosen.</param>
/// <param name="testPoint"></param>
/// <returns>Closest point.</returns>
/// <remark>Returns the same object within the cloud without copying it.</remark>
public static Point3 CloudClosestPoint (IEnumerable<Point3> cloud, Point3 testPoint)
{
double minDistance = double.MaxValue;
Point3 closestPoint = new Point3();
foreach (Point3 point in cloud)
{
double distanceToTarget = point.DistanceTo(testPoint);
if (distanceToTarget < minDistance)
{
minDistance = distanceToTarget;
closestPoint = point;
}
}
return closestPoint;
}

}
}