Vector3 OnMeshTriangle(int i, MPath path, out int triangleIndex) { RaycastHit hit; // Debug.Log ("At point " + i + " normals " + path.line.GetNormals ().Count + " path.line " + path.Count); var normal = i < path.Count ? path.GetNormal(i) : path.GetNormal(0); var position = i < path.Count ? path.GetPosition(i) : path.GetPosition(0); Ray ray = new Ray(position, -normal); if (manifold.gameObject.GetComponent <MeshCollider> ().Raycast(ray, out hit, 0.5f)) { triangleIndex = hit.triangleIndex; return(hit.point); } else { Debug.LogWarning("Warning in OnMesh: Raycast not working, returning closest Point instead"); triangleIndex = -1; return(GameObject.FindGameObjectWithTag("Sphere").GetComponent <MeshCollider> ().ClosestPoint(position)); } }
public Homotopy newHomotopy(MPath path1, MPath midPath) { Homotopy hom = new Homotopy(path1, midPath, homotopyMaterial); Debug.Log(midPath.ToString()); Debug.Log(midPath.Count.ToString()); var color = path1.GetColor(); midPath.SetColor(color); color.a = color.a / 3f; path1.SetColor(color); // for (int i = 0; i < Statics.numHomotopyLines; i++) { // GameObject lineObj = new GameObject (); // var line = lineObj.AddComponent<LineRenderer> (); // line.positionCount = hom.counter; // line.startWidth = Statics.lineThickness / 3f; // line.endWidth = Statics.lineThickness / 3f; // line.numCornerVertices = 0; // line.GetComponent<Renderer> ().material = pathMaterial; // line.GetComponent<Renderer> ().material.SetColor ("_Color", Statics.homotopyColor); //// hom.homotopyLines.Add (line); // } for (int i = 0; i < path1.Count; i++) { midPath.SetPosition(i, path1.GetPosition(i)); if (path1.hasNormals) { midPath.SetNormal(i, path1.GetNormal(i)); } } midPath.SetMesh(); // int counter = 1; // float sum = 0f; // var length = midPath.Length (); // for (int i = 0; i < count - 1; i++) { // if (sum > counter * (length / (Statics.numHomotopyLines + 1))) { // var line = hom.homotopyLines [counter - 1]; // line.SetPosition (0, midpath.GetPosition (i)); // counter++; // } // sum += Vector3.Distance (midpath.GetPosition (i), midpath.GetPosition (i + 1)); // } return(hom); }
public IEnumerator Cut(MPath path) { while (buildingGraph) { yield return(new WaitForSeconds(0.01f)); } DebugTime("Total Cut"); Vector3 normal = path.GetNormal(0); path.InsertPositionAt(0, path.dotFrom.transform.position); path.InsertNormalAt(0, normal); path.AddPosition(path.dotTo.transform.position); path.AddNormal(normal); DebugTime("Identify Cuts"); mesh = manifold.gameObject.GetComponent <MeshFilter> ().mesh; var vertices = mesh.vertices.ToList(); meshTriangles = mesh.triangles.ToList(); meshNormals = mesh.normals.ToList(); // Misc.DebugSphere (vertices[1299], Color.blue, "1299"); // Misc.DebugSphere (vertices[1334], Color.blue, "1334"); // Misc.DebugSphere (vertices[1335], Color.blue, "1335"); // DebugAtTriangle (2596, Color.white, "2596"); // DebugAtTriangle (2597, Color.white, "2597"); var cutPointIdentifier = new CutPointIdentifier(g, manifold); cutPointIdentifier.IdentifyCuts(path); var cutPoints = cutPointIdentifier.cutPoints; var cutEdges = cutPointIdentifier.cutEdges; var cutPointType = cutPointIdentifier.cutPointType; var cutTriangles = cutPointIdentifier.cutTriangles; var cutAlongEdges = cutPointIdentifier.cutAlongEdges; var cutNodes = cutPointIdentifier.cutNodes; DebugTime("Identify Cuts"); Debug.Log("Identification done, resulting in " + cutPoints.Count + " cut Points."); List <HashSet <int> > bounds; List <HashSet <int> > meshVertexPres; GetNewMeshVertices(vertices, cutNodes, cutAlongEdges, cutEdges, out bounds, out meshVertexPres); Debug.Log("GetBounds done, resulting in " + bounds.Count + " bounds."); int numComponents = meshVertexPres.Count; DebugTime("Set up Polygons"); var polyAtTriangle = new Dictionary <int, List <MPolygon> > (); foreach (var i in cutTriangles.Keys) { var index = cutTriangles[i]; polyAtTriangle[index] = new List <MPolygon> (); var list = new MPolygon(); list.Add(meshTriangles[index * 3]); list.Add(meshTriangles[index * 3 + 1]); list.Add(meshTriangles[index * 3 + 2]); polyAtTriangle[index].Add(list); } DebugTime("Set up Polygons"); // foreach (var item in cutTriangles.Keys) { // DebugAtTriangle (cutTriangles[item], Color.red, item + ""); // } DebugTime("Cut up Polygons"); oldVertexNumber = vertices.Count; var polygonCutter = new PolygonCutter(meshTriangles, meshNormals, vertices, polyAtTriangle); var sidesOfCut = polygonCutter.Cut(cutPoints, cutEdges, cutNodes, cutTriangles); List <Vector3> normals = polygonCutter.normals; var cutPointsToVertices = polygonCutter.cutPointVertices; DebugTime("Cut up Polygons"); // for (int i = 0; i < cutPoints.Count; i++) { // Vector3 cp = cutPoints[i]; // Misc.DebugSphere (cp, Color.red, i + "cp"); // } DebugTime("Triangulate"); var to = new TriangleOrdering(vertices, polyAtTriangle, bounds, cutNodes, cutPointsToVertices, sidesOfCut); Pair <HashSet <int> > sideBounds; var polygons = to.orderTriangles(out sideBounds); DebugTime("Triangulate"); Debug.Log("Triangulation done, resulting in " + polygons.Count + " lists of polygons."); // foreach (var item in sidesOfCut.left) { // DebugAtPolygon (vertices, item, Color.green); // } // foreach (var item in sidesOfCut.right) { // DebugAtPolygon (vertices, item, Color.red); // } var newMeshVertexPres = new List <HashSet <int> > (); newMeshVertexPres.Add(new HashSet <int> ()); newMeshVertexPres.Add(new HashSet <int> ()); foreach (var bound in sideBounds.left) { newMeshVertexPres[0].UnionWith(meshVertexPres[bound]); } foreach (var bound in sideBounds.right) { newMeshVertexPres[1].UnionWith(meshVertexPres[bound]); } numComponents = 2; if (newMeshVertexPres[0].SetEquals(newMeshVertexPres[1])) { numComponents = 1; polygons[0].AddRange(polygons[1]); } DebugTime("GetTriangles"); List <List <int> > meshts; List <List <Vector3> > meshvs, meshns; SetUpNewMeshes(vertices, cutTriangles, newMeshVertexPres, out meshts, out meshvs, out meshns); DebugTime("Set Normals and Triangles"); DebugTime("Set up manifolds"); SetUpManifolds(vertices, meshts, meshvs, meshns, normals, polygons, cutPoints, numComponents); SetSwitchedManifolds(); DebugTime("Set up manifolds"); //SLOW DebugTime("Total Cut"); }