Get / Calculate edges of a 3D object - C #

I need to get all vertices in the edge position of a determinate object 3D (in this case a path).

See the image:

My question is, how can I get only the edges vertices, like the image?

1 Answer

1

You can use a [convex-hull-algorithm][1] to find the outer edge of a point set. There are a few to pick from, but here’s an example of Jarvis march:

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public class FindConvexHull : MonoBehaviour 
{
	public List<Vector2> points;
	public float gizmoSize = 0.1f;

	private void OnDrawGizmos()
	{
		Gizmos.color = Color.white;
		foreach(var point in points)
			Gizmos.DrawSphere(point, gizmoSize);


		var hull = ConvexHull(points);
		if(hull != null)
		{
			Gizmos.color = Color.green;
			for (int i = 0; i < hull.Count - 1; i++) 
			{
				Gizmos.DrawLine(hull*, hull[i+1]);*
  •  	}*
    
  •  	Gizmos.DrawLine(hull[0], hull[hull.Count - 1]);*
    
  •  }*
    
  • }*

  • private List ConvexHull(List points)*

  • {*

  •  List<Vector2> hull = new List<Vector2>();*
    
  •  if(points.Count < 3)*
    
  •  	return hull;*
    
  •  int leftMost = 0;*
    
  •  for (int i = 0; i < points.Count; i++)* 
    
  •  {*
    

_ if(points*.x < points[leftMost].x)_
_
leftMost = i;_
_
}*_

* int pointA = leftMost;*
* int pointB;*
* do*
* {*
* hull.Add(points[pointA]);*
* pointB = (pointA + 1) % points.Count;*

* for (int i = 0; i < points.Count; i++)*
* {*
_ if(GetTurn(points[pointA], points*, points[pointB]) == 2)
pointB = i;
}*_

* pointA = pointB;*
* }*
* while (pointA != leftMost);*

* return hull;*
* }*

* private float GetTurn(Vector2 a, Vector2 b, Vector2 c)*
* {*
_ var val = (b.y - a.y) * (c.x - b.x) - (b.x - a.x) * (c.y - b.y);_

* if (val == 0)*
* return 0;*

* if (val > 0)*
* return 1;*

* else*
* return 2;*
* }*
}

[1]: Gift wrapping algorithm - Wikipedia

Wait. I'm confused. They all have the same name. You want to make sure you did not get the same one by it's location? If that is the case, then you may need to write some kind of custom comparison for the list. You can try using lamda.transform.position.Equals( temp.transform.position) but that is not always accurate. Google the C# list class for doing custom comparisons. How did you create these objects? You're job would be a lot easier if you gave them all unique names, which is easy to do if you are instantiating them.