// ╔════════════════════════════════════════════════════════════════╗ // ║ Copyright © 2025 NWH Coding d.o.o. All rights reserved. ║ // ║ Licensed under Unity Asset Store Terms of Service: ║ // ║ https://unity.com/legal/as-terms ║ // ║ Use permitted only in compliance with the License. ║ // ║ Distributed "AS IS", without warranty of any kind. ║ // ╚════════════════════════════════════════════════════════════════╝ #region using UnityEngine; #endregion namespace NWH.DWP2 { /// /// Utility class for geometric calculations used in buoyancy simulation. /// Provides methods for calculating centroids, areas, and surface-weighted centers of triangles and quadrilaterals. /// public static class GeometryUtils { /// /// Calculates the centroid of a quadrilateral defined by four points. /// /// First corner point. /// Second corner point. /// Third corner point. /// Fourth corner point. /// The centroid position of the quadrilateral. public static Vector3 CalculateCentroid(Vector3 pointA, Vector3 pointB, Vector3 pointC, Vector3 pointD) { Vector3 centroid = (pointA + pointB + pointC + pointD) / 4; return centroid; } /// /// Calculates the surface area of a quadrilateral by dividing it into two triangles. /// /// First corner point. /// Second corner point. /// Third corner point. /// Fourth corner point. /// Total surface area of the quadrilateral. public static float CalculateQuadrilateralArea(Vector3 pointA, Vector3 pointB, Vector3 pointC, Vector3 pointD) { // Calculate the area of the first triangle (pointA, pointB, pointC) float areaTriangle1 = CalculateTriangleArea(pointA, pointB, pointC); // Calculate the area of the second triangle (pointA, pointC, pointD) float areaTriangle2 = CalculateTriangleArea(pointA, pointC, pointD); // Return the sum of both triangle areas return areaTriangle1 + areaTriangle2; } /// /// Calculates the surface area of a triangle using the cross product method. /// /// First vertex of the triangle. /// Second vertex of the triangle. /// Third vertex of the triangle. /// Surface area of the triangle. public static float CalculateTriangleArea(Vector3 pointA, Vector3 pointB, Vector3 pointC) { Vector3 vectorAB = pointB - pointA; Vector3 vectorAC = pointC - pointA; // Calculate the cross product of vectorAB and vectorAC Vector3 crossProduct = Vector3.Cross(vectorAB, vectorAC); // Calculate the magnitude of the cross product and divide by 2 to get the area return crossProduct.magnitude / 2; } /// /// Calculates the surface-weighted center of a quadrilateral. /// The center is weighted by the area of the two triangles that make up the quadrilateral. /// /// First corner point. /// Second corner point. /// Third corner point. /// Fourth corner point. /// The surface-weighted center position. public static Vector3 CalculateSurfaceWeightedCenter(Vector3 pointA, Vector3 pointB, Vector3 pointC, Vector3 pointD) { // Calculate the area and centroid of the first triangle (pointA, pointB, pointC) float areaTriangle1 = CalculateTriangleArea(pointA, pointB, pointC); Vector3 centroidTriangle1 = CalculateCentroid(pointA, pointB, pointC); // Calculate the area and centroid of the second triangle (pointA, pointC, pointD) float areaTriangle2 = CalculateTriangleArea(pointA, pointC, pointD); Vector3 centroidTriangle2 = CalculateCentroid(pointA, pointC, pointD); // Calculate the surface-weighted center float totalArea = areaTriangle1 + areaTriangle2; Vector3 surfaceWeightedCenter = (centroidTriangle1 * areaTriangle1 + centroidTriangle2 * areaTriangle2) / totalArea; return surfaceWeightedCenter; } private static Vector3 CalculateCentroid(Vector3 pointA, Vector3 pointB, Vector3 pointC) { return (pointA + pointB + pointC) / 3; } } }