public void AlgorithmAABB() { var dataCloud = mConvexHull3.DataCloud; var points = mConvexHull3.Vertices; MinMaxPair X = new MinMaxPair(); MinMaxPair Y = new MinMaxPair(); MinMaxPair Z = new MinMaxPair(); foreach (int point in points) { Vector3 dataCloudPoint = dataCloud[point]; X.CheckAgainst(dataCloudPoint.x); Y.CheckAgainst(dataCloudPoint.y); Z.CheckAgainst(dataCloudPoint.z); } mVolume = X.Delta * Y.Delta * Z.Delta; mBox.Extents = new Vector3(X.Delta, Y.Delta, Z.Delta); mBox.Corner = new Vector3(X.Min, Y.Min, Z.Min); mBox.X = Vector3.right; mBox.Y = Vector3.up; mBox.Z = Vector3.forward; }
/// <summary> /// Function returns for given axis min vertex index and breadth along it. /// </summary> public int GetAlongAxisData(Vector3 axis, out MinMaxPair breadthData) { Pair <int, float> minVertex = new Pair <int, float>(-1, float.MaxValue); breadthData = new MinMaxPair(); Vector3 centroid = Centroid; for (int i = 0; i < mDataCloud.Count; i++) { Vector3 point = mDataCloud[i]; Vector3 direction = point - centroid; float distance = Vector3.Dot(direction, axis); breadthData.CheckAgainst(distance); if (ExtMathf.Less(distance, minVertex.Second)) { minVertex.First = i; minVertex.Second = distance; } } return(minVertex.First); }
/// <summary> /// Function for each axis returns it's min vertex index and breadth along it. /// </summary> public void GetAlongAxesData( Vector3 xAxis, Vector3 yAxis, Vector3 zAxis, out Pair <int, MinMaxPair> xAxisData, out Pair <int, MinMaxPair> yAxisData, out Pair <int, MinMaxPair> zAxisData ) { Pair <int, float> minVertexX = new Pair <int, float>(0, float.MaxValue); Pair <int, float> minVertexY = new Pair <int, float>(0, float.MaxValue); Pair <int, float> minVertexZ = new Pair <int, float>(0, float.MaxValue); MinMaxPair breadthX = new MinMaxPair(); MinMaxPair breadthY = new MinMaxPair(); MinMaxPair breadthZ = new MinMaxPair(); Vector3 centroid = Centroid; for (int i = 0; i < mDataCloud.Count; i++) { Vector3 point = mDataCloud[i]; Vector3 direction = point - centroid; float xDistance = Vector3.Dot(direction, xAxis); float yDistance = Vector3.Dot(direction, yAxis); float zDistance = Vector3.Dot(direction, zAxis); breadthX.CheckAgainst(xDistance); breadthY.CheckAgainst(yDistance); breadthZ.CheckAgainst(zDistance); if (ExtMathf.Less(xDistance, minVertexX.Second)) { minVertexX.First = i; minVertexX.Second = xDistance; } if (ExtMathf.Less(yDistance, minVertexY.Second)) { minVertexY.First = i; minVertexY.Second = yDistance; } if (ExtMathf.Less(zDistance, minVertexZ.Second)) { minVertexZ.First = i; minVertexZ.Second = zDistance; } } xAxisData = new Pair <int, MinMaxPair>(minVertexX.First, breadthX); yAxisData = new Pair <int, MinMaxPair>(minVertexY.First, breadthY); zAxisData = new Pair <int, MinMaxPair>(minVertexZ.First, breadthZ); }
public void AlgorithmRotatingCalipers() { var edges = mConvexHull2.Edges; var dataCloud = mConvexHull2.DataCloud; var vertices = mConvexHull2.Vertices; if (edges.Count < 3) { Debugger.Get.Log("Insufficient points to form a triangle.", DebugOption.SBB2); return; } mArea = float.MaxValue; foreach (Edge edge in edges) { Vector3 edgeDirection = edge.V[1] - edge.V[0]; float edgeAngle = Mathf.Atan2(edgeDirection.y, edgeDirection.x); // x = min, y = max MinMaxPair X = new MinMaxPair(); MinMaxPair Y = new MinMaxPair(); foreach (int p in vertices) { Vector3 rotatedPoint = ExtMathf.Rotation2D(dataCloud[p], -edgeAngle); X.CheckAgainst(rotatedPoint.x); Y.CheckAgainst(rotatedPoint.y); } float newArea = X.Delta * Y.Delta; if (newArea < mArea) { mArea = newArea; mRectangle.Extents = new Vector2(X.Delta, Y.Delta); mRectangle.Corner = ExtMathf.Rotation2D(new Vector3(X.Min, Y.Min), edgeAngle); mRectangle.X = ExtMathf.Rotation2D(Vector3.right, edgeAngle).normalized; mRectangle.Y = ExtMathf.Rotation2D(Vector3.up, edgeAngle).normalized; } } }