Created
October 13, 2021 05:13
-
-
Save unitycoder/f0518ddbbf40a1b4e2d274cfd02c7df7 to your computer and use it in GitHub Desktop.
How do I get the cross sectional surface area of my geometry mesh in meters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // https://forum.unity.com/threads/how-do-i-get-the-cross-sectional-surface-area-of-my-geometry-mesh-in-meters.461278/#post-7567135 | |
| private static Dictionary<GameObject, Vector3[]> vertecesDict = new Dictionary<GameObject, Vector3[]>(); | |
| public static float CrossSectionArea(GameObject g, Vector3 normal) | |
| { | |
| Vector3[] verteces; | |
| if (!vertecesDict.TryGetValue(g, out verteces)) | |
| { | |
| MeshFilter[] meshes = g.GetComponentsInChildren<MeshFilter>(); | |
| HashSet<Vector3> sVerteces = new HashSet<Vector3>(); | |
| foreach (MeshFilter m in meshes) | |
| { | |
| foreach (Vector3 v3 in m.mesh.vertices) | |
| { | |
| sVerteces.Add(m.transform.TransformPoint(v3)); | |
| } | |
| } | |
| verteces = new Vector3[sVerteces.Count]; | |
| sVerteces.CopyTo(verteces); | |
| vertecesDict.Add(g, verteces); | |
| } | |
| normal = normal.normalized; | |
| Vector3 perp1 = Perp(normal).normalized; | |
| Vector3 perp2 = Vector3.Cross(normal, perp1); | |
| Vector2[] inThePlane = new Vector2[verteces.Length]; | |
| for (int i = 0; i < verteces.Length; i++) | |
| { | |
| inThePlane[i] = new Vector2(Vector3.Dot(perp1, verteces[i]), Vector3.Dot(perp2, verteces[i])); | |
| } | |
| return HullArea(convexHull(inThePlane)); | |
| } | |
| public static Vector3 Perp(Vector3 v3) | |
| { | |
| return v3.z < v3.x ? new Vector3(v3.y, -v3.x, 0) : new Vector3(0, -v3.z, v3.y); | |
| } | |
| public static float HullArea(Vector2[] hull) | |
| { | |
| float sum = 0f; | |
| for (int i = 1; i < hull.Length-1; i++) | |
| { | |
| sum += TriangleArea(hull[0], hull[i], hull[i+1]); | |
| } | |
| return sum; | |
| } | |
| public static float TriangleArea(Vector2 v0, Vector2 v1, Vector2 v2) | |
| { | |
| return Mathf.Abs((v1.x- v0.x) * (v2.y - v0.y) - (v2.x - v0.x) * (v1.y - v0.y)) * .5f; | |
| } | |
| // To find orientation of ordered triplet (p, q, r). | |
| // The function returns following values | |
| // 0 --> p, q and r are collinear | |
| // 1 --> Clockwise | |
| // 2 --> Counterclockwise | |
| public static float orientation(Vector2 p, Vector2 q, Vector2 r) | |
| { | |
| float val = (q.y - p.y) * (r.x - q.x) - | |
| (q.x - p.x) * (r.y - q.y); | |
| if (val == 0) return 0; // collinear | |
| return (val > 0) ? 1 : 2; // clock or counterclock wise | |
| } | |
| // Gets convex hull of a set of Vector2s. From https://www.geeksforgeeks.org/convex-hull-set-1-jarviss-algorithm-or-wrapping/ | |
| public static Vector2[] convexHull(Vector2[] Vector2s) | |
| { | |
| int n = Vector2s.Length; | |
| // There must be at least 3 Vector2s | |
| if (n < 3 /*|| n - SameCount(Vector2s) < 3*/) return Vector2s; | |
| // Initialize Result | |
| List<Vector2> hull = new List<Vector2>(); | |
| // Find the leftmost Vector2 | |
| int l = 0; | |
| for (int i = 1; i < n; i++) | |
| if (Vector2s[i].x < Vector2s[l].x) | |
| l = i; | |
| // Start from leftmost Vector2, keep moving | |
| // counterclockwise until reach the start Vector2 | |
| // again. This loop runs O(h) times where h is | |
| // number of Vector2s in result or output. | |
| int p = l, q; | |
| do | |
| { | |
| // Add current Vector2 to result | |
| hull.Add(Vector2s[p]); | |
| // Search for a Vector2 'q' such that | |
| // orientation(p, q, x) is counterclockwise | |
| // for all Vector2s 'x'. The idea is to keep | |
| // track of last visited most counterclock- | |
| // wise Vector2 in q. If any Vector2 'i' is more | |
| // counterclock-wise than q, then update q. | |
| q = (p + 1) % n; | |
| for (int i = 0; i < n; i++) | |
| { | |
| // If i is more counterclockwise than | |
| // current q, then update q | |
| if (orientation(Vector2s[p], Vector2s[i], Vector2s[q]) | |
| == 2) | |
| q = i; | |
| } | |
| // Now q is the most counterclockwise with | |
| // respect to p. Set p as q for next iteration, | |
| // so that q is added to result 'hull' | |
| p = q; | |
| } while (p != l); // While we don't come to first | |
| // Vector2 | |
| return hull.ToArray(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment