protected override void OnPointUpdated(ControlPoint pt, Frame3f prevFrameS, bool isFirst) { DMesh3 mesh = InputMeshSO.Mesh; DMeshAABBTree3 spatial = InputMeshSO.Spatial; Vector3f ptO = SceneTransforms.SceneToObjectP(InputMeshSO, pt.currentFrameS.Origin); Frame3f frameO = MeshQueries.NearestPointFrame(mesh, spatial, ptO, true); Vector3d dir = -frameO.Z; if (hole_direction != HoleDirections.Normal) { Vector3f axis = Vector3f.AxisX; if (hole_direction == HoleDirections.AxisY) { axis = Vector3f.AxisY; } else if (hole_direction == HoleDirections.AxisZ) { axis = Vector3f.AxisZ; } axis = SceneTransforms.SceneToObjectN(InputMeshSO, axis); dir = (dir.Dot(axis) < 0) ? -axis : axis; } //dir.Normalize(); LastUpdateRay = new Ray3d(frameO.Origin, dir); List <int> hitTris = new List <int>(); int hit_tris = spatial.FindAllHitTriangles(LastUpdateRay, hitTris); double max_t = 0; foreach (int tid in hitTris) { Vector3d n = mesh.GetTriNormal(tid); if (n.Dot(LastUpdateRay.Direction) < 0) { continue; } IntrRay3Triangle3 rayhit = MeshQueries.TriangleIntersection(InputMeshSO.Mesh, tid, LastUpdateRay); max_t = rayhit.RayParameter; break; } if (max_t <= 0) { return; } LastThroughDepth = max_t; update_current_hole_type(); }
public static void testInsertPolygon_PlanarProj() { double dscale = 1.0; DMesh3 mesh = TestUtil.LoadTestInputMesh("bunny_solid.obj"); dscale = 0.3; //DMesh3 mesh = TestUtil.LoadTestInputMesh("cylinder.obj"); //DMesh3 mesh = TestUtil.LoadTestInputMesh("cube.obj"); double size = mesh.CachedBounds.MaxDim; Vector3d c = mesh.CachedBounds.Center; Vector3d fw = c + mesh.CachedBounds.DiagonalLength * 2 * Vector3d.AxisZ; Ray3d ray = new Ray3d(fw, (c - fw).Normalized); // projection frame and polygon that lives in this frame Frame3f projectFrame = new Frame3f(ray.Origin, ray.Direction); Polygon2d circle = Polygon2d.MakeCircle(dscale * size * 0.1, 6); DMeshAABBTree3 spatial = new DMeshAABBTree3(mesh, true); List <int> hitTris = new List <int>(); spatial.FindAllHitTriangles(ray, hitTris); while (hitTris.Count != 2) { ray.Origin += 100 * MathUtil.Epsilon * Vector3d.One; hitTris.Clear(); spatial.FindAllHitTriangles(ray, hitTris); } // insert polygons but don't simplify the result DMesh3 noTrimMesh = new DMesh3(mesh); List <int[]> noTrimPolyVerts = new List <int[]>(); List <EdgeLoop> noTrimLoops = new List <EdgeLoop>(); foreach (int hit_tid in hitTris) { MeshInsertProjectedPolygon insert = new MeshInsertProjectedPolygon(noTrimMesh, circle, projectFrame, hit_tid); insert.SimplifyInsertion = false; if (insert.Insert()) { noTrimPolyVerts.Add(insert.InsertedPolygonVerts); noTrimLoops.Add(insert.InsertedLoop); } else { System.Console.WriteLine("testInsertPolygon_PlanarProj: no-trim Insert() failed"); } } TestUtil.WriteTestOutputMesh(noTrimMesh, "insert_polygon_notrim.obj"); // do different-vtx-count stitch if (noTrimLoops.Count == 2) { noTrimLoops[1].Reverse(); MeshStitchLoops stitcher = new MeshStitchLoops(noTrimMesh, noTrimLoops[0], noTrimLoops[1]); stitcher.TrustLoopOrientations = false; stitcher.AddKnownCorrespondences(noTrimPolyVerts[0], noTrimPolyVerts[1]); stitcher.Stitch(); } TestUtil.WriteTestOutputMesh(noTrimMesh, "insert_polygon_notrim_joined.obj"); // now do simplified version, which we can trivially stitch List <EdgeLoop> edgeLoops = new List <EdgeLoop>(); foreach (int hit_tid in hitTris) { MeshInsertProjectedPolygon insert = new MeshInsertProjectedPolygon(mesh, circle, projectFrame, hit_tid); if (insert.Insert()) { edgeLoops.Add(insert.InsertedLoop); } else { System.Console.WriteLine("testInsertPolygon_PlanarProj: Insert() failed"); } } //TestUtil.WriteTestOutputMesh(mesh, "insert_polygon_before_stitch.obj"); // do stitch if (edgeLoops.Count == 2) { MeshEditor editor = new MeshEditor(mesh); EdgeLoop l0 = edgeLoops[0]; EdgeLoop l1 = edgeLoops[1]; l1.Reverse(); editor.StitchLoop(l0.Vertices, l1.Vertices); } TestUtil.WriteTestOutputMesh(mesh, "insert_polygon_joined.obj"); }
protected override ICADObject OnHover(Vector3 mouse, Camera camera, UnityEngine.Matrix4x4 tf, ref double dist) { var tris = new List <int>(); var fullTf = tf * transform; var invFullTf = fullTf.inverse; var ray = camera.ScreenPointToRay(mouse); ray.origin = invFullTf.MultiplyPoint3x4(ray.origin); ray.direction = invFullTf.MultiplyVector(ray.direction); hitMesh.FindAllHitTriangles(new Ray3d(ray.origin.ToVector3d(), ray.direction.ToVector3d().Normalized), tris); double min = -1.0; int hoverV0 = -1; int hoverV1 = -1; foreach (var ti in tris) { var t = hitMesh.Mesh.GetTriangle(ti); for (int i = 0; i < 3; i++) { var v0 = fullTf.MultiplyPoint3x4(hitMesh.Mesh.GetVertex(t[i]).ToVector3()); if (!HoverPoint(mouse, camera, ref min, v0)) { continue; } hoverV0 = t[i]; hoverV1 = -1; } } if (hoverV0 != -1) { dist = min; return(new MeshVertexEntity(this, hoverV0)); } foreach (var ti in tris) { var t = hitMesh.Mesh.GetTriangle(ti); for (int i = 0; i < 3; i++) { var v0 = hitMesh.Mesh.GetVertex(t[i]).ToVector3(); var v1 = hitMesh.Mesh.GetVertex(t[(i + 1) % 3]).ToVector3(); if (useThreshold && !meshCheck.IsEdgeSharp(v0, v1, thresholdAngle)) { continue; } if (!HoverSegment(mouse, camera, ref min, fullTf.MultiplyPoint3x4(v0), fullTf.MultiplyPoint3x4(v1))) { continue; } hoverV0 = t[i]; hoverV1 = t[(i + 1) % 3]; } } if (hoverV0 != -1) { dist = min; return(new MeshEdgeEntity(this, hoverV0, hoverV1)); } return(null); }