//The length of this edge public float Length() { //The edge points TO a vertex MyVector3 p2 = v.position; MyVector3 p1 = prevEdge.v.position; float length = MyVector3.Distance(p1, p2); return(length); }
// // Calculate length of curve // //Get the length of the curve with a naive method where we divide the //curve into straight lines and then measure the length of each line //tEnd is 1 if we want to get the length of the entire curve public static float GetLength_Naive(_Curve curve, int steps, float tEnd) { //Split the ruve into positions with some steps resolution List <MyVector3> CurvePoints = SplitCurve(curve, steps, tEnd); //Calculate the length by measuring the length of each step float length = 0f; for (int i = 1; i < CurvePoints.Count; i++) { float thisStepLength = MyVector3.Distance(CurvePoints[i - 1], CurvePoints[i]); length += thisStepLength; } return(length); }
//Cut a triangle where two vertices are inside and the other vertex is outside //Make sure they are sorted clockwise: O1-O2-I1 //F means that this vertex is outside the plane private static void CutTriangleTwoOutside(MyMeshVertex O1, MyMeshVertex O2, MyMeshVertex I1, HalfEdgeData3 newMeshO, HalfEdgeData3 newMeshI, HashSet <HalfEdge3> newEdgesI, HashSet <HalfEdge3> newEdgesO, Plane3 cutPlane) { //Cut the triangle by using edge-plane intersection //Triangles in Unity are ordered clockwise, so form edges that intersects with the plane: Edge3 e_O2I1 = new Edge3(O2.position, I1.position); //Edge3 e_F1F2 = new Edge3(F1, F2); //Not needed because never intersects with the plane Edge3 e_I1O1 = new Edge3(I1.position, O1.position); //The positions of the intersection vertices MyVector3 pos_O2I1 = _Intersections.GetLinePlaneIntersectionPoint(cutPlane, e_O2I1); MyVector3 pos_I1O1 = _Intersections.GetLinePlaneIntersectionPoint(cutPlane, e_I1O1); //The normals of the intersection vertices float percentageBetween_O2I1 = MyVector3.Distance(O2.position, pos_O2I1) / MyVector3.Distance(O2.position, I1.position); float percentageBetween_I1O1 = MyVector3.Distance(I1.position, pos_I1O1) / MyVector3.Distance(I1.position, O1.position); MyVector3 normal_O2I1 = _Interpolation.Lerp(O2.normal, I1.normal, percentageBetween_O2I1); MyVector3 normal_I1O1 = _Interpolation.Lerp(I1.normal, O1.normal, percentageBetween_I1O1); //MyVector3 normal_F2B1 = Vector3.Slerp(F2.normal.ToVector3(), B1.normal.ToVector3(), percentageBetween_F2B1).ToMyVector3(); //MyVector3 normal_B1F1 = Vector3.Slerp(B1.normal.ToVector3(), F1.normal.ToVector3(), percentageBetween_B1F1).ToMyVector3(); normal_O2I1 = MyVector3.Normalize(normal_O2I1); normal_I1O1 = MyVector3.Normalize(normal_I1O1); //The intersection vertices MyMeshVertex v_O2I1 = new MyMeshVertex(pos_O2I1, normal_O2I1); MyMeshVertex v_I1O1 = new MyMeshVertex(pos_I1O1, normal_I1O1); //Form 3 new triangles //Outside AddTriangleToMesh(v_O2I1, v_I1O1, O2, newMeshO, newEdgesO); AddTriangleToMesh(O2, v_I1O1, O1, newMeshO, null); //Inside AddTriangleToMesh(v_I1O1, v_O2I1, I1, newMeshI, newEdgesI); }
// // Help method to calculate the accumulated total distances along the curve // by walking along it by using constant t-steps // public static List <float> GetAccumulatedDistances(Curve curve, int steps = 20) { //Step 1. Find positions on the curve by using the bad t-value List <MyVector3> positionsOnCurve = SplitCurve(curve, steps, tEnd: 1f); //Step 2. Calculate the cumulative distances along the curve for each position along the curve //we just calculated List <float> accumulatedDistances = new List <float>(); float totalDistance = 0f; accumulatedDistances.Add(totalDistance); for (int i = 1; i < positionsOnCurve.Count; i++) { totalDistance += MyVector3.Distance(positionsOnCurve[i], positionsOnCurve[i - 1]); accumulatedDistances.Add(totalDistance); } return(accumulatedDistances); }