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 14 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
121 changes: 121 additions & 0 deletions src/GShark/Geometry/Mesh.cs
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,127 @@ private FaceData CountFaceEdges()
}




// Checks if point is in or out a triangle (boolean result, In - True , Out - False)
private bool IsPointInTriangle(Point3 projection, Point3[] trianglePoints)
{
//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));
}

// Function that finds the closest point to a triangle (given the 3 vertices as points)
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 segment_AB = new Line(trianglePoints[0], trianglePoints[1]);
Line segment_BC = new Line(trianglePoints[1], trianglePoints[2]);
Line segment_CA = new Line(trianglePoints[2], trianglePoints[0]);
List<Line> segments = new List<Line>() { segment_AB, segment_BC, segment_CA };
List<Point3> edgesClosestPoints = new List<Point3>();
foreach (Line segment in segments)
{
Point3 segmentClosestPoint = segment.ClosestPoint(point);
edgesClosestPoints.Add(segmentClosestPoint);
}
return point.CloudClosestPoint(edgesClosestPoints);
}

public Point3 ClosestPoint(Point3 point)
{
List<MeshVertex> meshVertices = Vertices;

//All faces check
Sophielelerre marked this conversation as resolved.
Show resolved Hide resolved
System.Collections.Generic.IEnumerable<MeshFace> AllFaces = this.Faces;

var CloserPointToFace_List = new List<Point3>();

foreach (MeshFace face in AllFaces)
{
// Retrives the Vertices associated with each face
List<MeshVertex> GSVertices = face.AdjacentVertices();
// Converts from MeshVertex to Point3
List<Point3> FacePoints = GSVertices.ConvertAll(v => (Point3)v);
Sophielelerre marked this conversation as resolved.
Show resolved Hide resolved

if (GSVertices.Count == 3)
{
var trianglePoints = new Point3[] { FacePoints[0], FacePoints[1], FacePoints[2] };
Point3 ClosestPoint0 = ClosestPointToTriangle(trianglePoints, point);
CloserPointToFace_List.Add(ClosestPoint0);
Sophielelerre marked this conversation as resolved.
Show resolved Hide resolved
}
else if (GSVertices.Count == 4)
{
var trianglePoints1 = new Point3[] { FacePoints[0], FacePoints[1], FacePoints[2] };
var trianglePoints2 = new Point3[] { FacePoints[1], FacePoints[2], FacePoints[3] };
var trianglePoints3 = new Point3[] { FacePoints[0], FacePoints[1], FacePoints[3] };
var trianglePoints4 = new Point3[] { FacePoints[0], FacePoints[2], FacePoints[3] };

var ClosestPoint1 = ClosestPointToTriangle(trianglePoints1, point);
var ClosestPoint2 = ClosestPointToTriangle(trianglePoints2, point);
var ClosestPoint3 = ClosestPointToTriangle(trianglePoints3, point);
var ClosestPoint4 = ClosestPointToTriangle(trianglePoints4, point);

var TrianglesCP = new Point3[4] { ClosestPoint1, ClosestPoint2, ClosestPoint3, ClosestPoint4 };
Point3 ClosestPoint = point.CloudClosestPoint(TrianglesCP);

CloserPointToFace_List.Add(ClosestPoint);
Sophielelerre marked this conversation as resolved.
Show resolved Hide resolved
}
else
{
//throw new Exception("Ngon detected");
Sophielelerre marked this conversation as resolved.
Show resolved Hide resolved
// Stellate method (from Ngon to triangles)
Sophielelerre marked this conversation as resolved.
Show resolved Hide resolved
List<Point3> GSVerticesPoints = GSVertices.ConvertAll(v => (Point3)v);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as before: no need to convert. Let's not convert and then see what G-Shark people think about it.

Point3 ngonCentre = Point3.AveragePoint(GSVerticesPoints);

var TrianglesCP = new List<Point3>();

for (int i = 0; i < GSVertices.Count; i++)
{
Point3[] trianglePoints = new Point3[] { ngonCentre, GSVertices[i], GSVertices[(i + 1)% GSVertices.Count] };
Point3 ClosestPointToNgon = ClosestPointToTriangle(trianglePoints, point);
TrianglesCP.Add(ClosestPointToNgon);
}
Point3 ClosestPoint = point.CloudClosestPoint(TrianglesCP);
CloserPointToFace_List.Add(ClosestPoint);
}
}
Point3 MeshClosestPoint = point.CloudClosestPoint(CloserPointToFace_List);
return MeshClosestPoint;
Sophielelerre marked this conversation as resolved.
Show resolved Hide resolved
}

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



// Function to find mid point from cloud
public static Point3 AveragePoint(List<Point3> points)
{
Point3 pointTotSum = Point3.Origin;
for (int i = 0; i < points.Count(); i++)
{
pointTotSum += points[i];
}
Point3 mid = pointTotSum / points.Count();
return mid;
}

// Function that finds point in a cloud of points to a target point
public Point3 CloudClosestPoint(IEnumerable<Point3> cloud)
Sophielelerre marked this conversation as resolved.
Show resolved Hide resolved
{
double minDistance = double.MaxValue;
Point3 closestPoint = new Point3();
foreach (Point3 point in cloud)
{
double distanceToTarget = point.DistanceTo(this);
if (distanceToTarget < minDistance)
{
minDistance = distanceToTarget;
closestPoint = point;
}
}
return closestPoint;
}

}
}