public static void TestExtMath() { float a = 1f; float b = 1f + 1e-6f; float c = 1f + 1e-4f; Debug.Assert(ExtMathf.Equal(a, b)); Debug.Assert(!ExtMathf.Equal(a, c)); Debug.Assert(ExtMathf.Greater(c, a)); Debug.Assert(!ExtMathf.Greater(b, a)); Debug.Assert(ExtMathf.Less(a, c)); Debug.Assert(!ExtMathf.Less(a, b)); Vector3 A = new Vector3(-1, 0, -1); Vector3 B = new Vector3(1, 0, 1); Vector3 C = new Vector3(0, 1, 0); Vector3 D = new Vector3(0, (1f / 3f), 0); Debug.Assert(ExtMathf.Centroid(A, B, C) == D); Vector3 xAxis = new Vector3(Random.Range(-1f, 1f), Random.Range(-1f, 1f), Random.Range(-1f, 1f)).normalized; Vector3 yAxis; Vector3 zAxis; ExtMathf.AxesFromAxis(xAxis, out yAxis, out zAxis); Debug.Assert(ExtMathf.Equal(Vector3.Cross(yAxis, zAxis).normalized, xAxis)); Debug.Assert(ExtMathf.Equal(Vector3.Cross(zAxis, xAxis).normalized, yAxis)); Debug.Assert(ExtMathf.Equal(Vector3.Cross(xAxis, yAxis).normalized, zAxis)); }
/// <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> /// Returns relative position of point 'V' to this Edge, assuming space to be 2D. /// +1 - point lies above the line. /// 0 - point lies on the line. /// -1 - point lies below the line. /// </summary> public int CalculateRelative2DPosition(Vector3 P) { float d = (P.x - V[0].x) * (V[1].y - V[0].y) - (P.y - V[0].y) * (V[1].x - V[0].x); return(ExtMathf.Greater(d, 0f) ? 1 : (ExtMathf.Less(d, 0f) ? -1 : 0)); }
/// <summary> /// Returns relative position of point 'V' to this Triangle. /// +1 - point lies in front of the triangle. /// 0 - point lies on the triangle. /// -1 - point lies behind the triangle. /// </summary> public int CalculateRelativePosition(Vector3 P) { float distance = CalculateDistance(P); return(ExtMathf.Greater(distance, 0f) ? 1 : (ExtMathf.Less(distance, 0f) ? -1 : 0)); }
/// <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); }