- Home /
How to Find Edge Vertices of a 2D mesh,How to Find Edge Vertices of a 2D mesh
I want to only Red point please help me,
Answer by Bunny83 · Nov 14, 2020 at 12:11 PM
I've posted an answer to a similar question over here. Does that help? Keep in mind this approach does only work when you have shared vertices all across the mesh. So any UV seam is considered an outer edge as well.
Answer by Llama_w_2Ls · Nov 14, 2020 at 12:54 PM
Ive done lots of 3D mesh problems like this in WPF so let me take a crack.
First, if you had an array of the points/vertices, alongside the triangle indicies, you can create a list of edges like this: (pseudocode warning..)
public class MainClass
{
private void GetEdges()
{
Point[] points = mesh.Points; // The mesh's vertices
int[] indicies = mesh.TriangleIndices; // The mesh's triangle indicies
List<Edge> edges = new List<Edge>();
// for every two triangle indicies
for (int i = 0; i < indicies.Length; i++)
{
// if point a != point b (meaning there is an edge)
if (points[indicies[i]] != points[indicies[i + 1]])
{
// Create a new edge with the corresponding points
// and add it to edge list
Edge edge = new Edge(points[indicies[i]], points[indicies[i + 1]]);
edges.Add(edge);
}
}
}
}
public class Edge
{
public Point A;
public Point B;
public Edge(Point a, Point b)
{
A = a;
B = b;
}
// Creates an object that represents
// a line from A to B (an edge)
}
With the edges, knowing that you only want the edges which dont go diagonally into the mesh, you can do a basic check with the points of each edge to check that the x or y of both points in the edge are the same, meaning it isn't slanted, but rather straight, either on the x-axis or y-axis. For example:
private void GetStraightEdges(List<Edge> edges)
{
List<Edge> straightEdges = new List<Edge>();
foreach (Edge edge in edges)
{
if (edge.A.X == edge.B.X ||
edge.A.Y == edge.B.Y)
{
// edge is straight and not diagonal
straightEdges.Add(edge);
}
else
{
// edge is diagonal and can be discarded
}
}
}
Finally, you wish to only find the red points on each straight edge, so you can simply create a new point array containing those points (as you already have the right edges for the job)
private List<Point> GetPoints(List<Edge> straightEdges)
{
List<Point> Points = new List<Point>();
foreach (Edge edge in straightEdges)
{
Points.Add(edge.A);
Points.Add(edge.B);
}
return Points;
}
And this method returns the list of red points in your diagram from that mesh. You can do whatever you want with that list of points, like circling them, or allowing them to be edited, im guessing. You do need the original collection of vertices and triangle indicies from the original mesh to do this though, which Unity provides for each mesh you create. Hopefully this helps @Hellojeska
Full Code:
public class MainClass
{
private void GetEdges()
{
Point[] points = mesh.Points; // The mesh's vertices
int[] indicies = mesh.TriangleIndices; // The mesh's triangle indicies
List<Edge> edges = new List<Edge>();
// for every two triangle indicies
for (int i = 0; i < indicies.Length; i++)
{
// if point a != point b (meaning there is an edge)
if (points[indicies[i]] != points[indicies[i + 1]])
{
// Create a new edge with the corresponding points
// and add it to edge list
Edge edge = new Edge(points[indicies[i]], points[indicies[i + 1]]);
edges.Add(edge);
}
}
GetStraightEdges(edges);
}
private void GetStraightEdges(List<Edge> edges)
{
List<Edge> straightEdges = new List<Edge>();
foreach (Edge edge in edges)
{
if (edge.A.X == edge.B.X ||
edge.A.Y == edge.B.Y)
{
// edge is straight and not diagonal
straightEdges.Add(edge);
}
else
{
// edge is diagonal and can be discarded
}
}
List<Point> redPoints = GetPoints(straightEdges);
}
private List<Point> GetPoints(List<Edge> straightEdges)
{
List<Point> Points = new List<Point>();
foreach (Edge edge in straightEdges)
{
Points.Add(edge.A);
Points.Add(edge.B);
}
return Points;
}
}
public class Edge
{
public Point A;
public Point B;
public Edge(Point a, Point b)
{
A = a;
B = b;
}
// Creates an object that represents
// a line from A to B (an edge)
}
Your answer
Follow this Question
Related Questions
Create edges for vertices(JS) 1 Answer
how to Find the vertices of each edge on mesh 4 Answers
Getting mesh length (x,z) 1 Answer
Making Different vertices,triangle,uv maps in the same mesh 1 Answer
Access a game's 3D data during runtime 0 Answers