/// <summary> /// available after call to UpdateSection() /// </summary> public DMesh3 GetSectionMesh(double simplifyTol = 0.01) { DMesh3 mesh = new DMesh3(); if (localCurves.Loops == null) { return(mesh); } List <GeneralPolygon2d> solids = GetSolids(); foreach (GeneralPolygon2d poly in solids) { poly.Simplify(simplifyTol, simplifyTol / 10, true); TriangulatedPolygonGenerator gen = new TriangulatedPolygonGenerator() { Polygon = poly }; DMesh3 polyMesh = gen.Generate().MakeDMesh(); MeshTransforms.PerVertexTransform(polyMesh, (uv) => { return(frameL.FromPlaneUV((Vector2f)uv.xy, 2)); }); MeshEditor.Append(mesh, polyMesh); } if (OutputSpace != CoordSpace.ObjectCoords) { MeshTransforms.PerVertexTransform(mesh, (v) => { return(SceneTransforms.TransformTo((Vector3f)v, SO, CoordSpace.ObjectCoords, OutputSpace)); }); } return(mesh); }
void apply_scene_scale(TargetObject obj, Vector3f scale) { // construct scaled scene frame and update SO Frame3f f = obj.sceneFrame; f.Origin = scale * (f.Origin - sharedOriginS) + sharedOriginS; obj.curSceneFrame = f; obj.SO.SetLocalFrame(f, CoordSpace.SceneCoords); Frame3f fL = obj.SO.GetLocalFrame(CoordSpace.ObjectCoords); // transform is to map from original obj frame into scene, scale, and then map into scaled obj frame TransformSequence seq = new TransformSequence(); seq.AppendFromFrame(obj.objFrame); seq.AppendScale(scale, sharedOriginS); seq.AppendToFrame(fL); obj.SO.EditAndUpdateMesh((mesh) => { // restore original positions mesh.VerticesBuffer.copy(obj.InputMeshV); if (obj.InputMeshN != null && mesh.HasVertexNormals) { mesh.NormalsBuffer.copy(obj.InputMeshN); } // apply xform MeshTransforms.PerVertexTransform(mesh, seq); }, GeometryEditTypes.VertexDeformation); }
public virtual void Setup() { // push history stream, so that we can do undo/redo internal to tool, // that will not end up in external history push_history_stream(); if (OnApplyF == null) { OnApplyF = this.add_so_to_scene; } if (PreviewMaterial == null) { PreviewMaterial = SOMaterial.CreateMesh("tool_generated", Colorf.DimGrey); } if (ErrorMaterial == null) { ErrorMaterial = SOMaterial.CreateMesh("tool_generated_error", Colorf.VideoRed); } // clear selection here so that multi-select GroupSO goes away, otherwise // when we copy frmaes below, they are relative to that GroupSO, and things move inputSelection = new List <SceneObject>(Scene.Selected); set_allow_selection_changes(true); Scene.ClearSelection(); set_allow_selection_changes(false); MeshSourceOps = new List <ConstantMeshSourceOp>(); SceneMeshes = new List <DMesh3>(); foreach (var so in InputSOs) { var xform = SceneTransforms.ObjectToSceneXForm(so); DMesh3 sceneMesh = new DMesh3(so.Mesh); MeshTransforms.PerVertexTransform(sceneMesh, xform); SceneMeshes.Add(sceneMesh); MeshSourceOps.Add( new ConstantMeshSourceOp(sceneMesh, false, true)); } EditOp = edit_op_factory(MeshSourceOps.Cast <DMeshSourceOp>()); ComputeOp = new ThreadedMeshComputeOp() { MeshSource = EditOp }; PreviewSO = new DMeshSO() { EnableSpatial = EnablePreviewSpatial }; PreviewSO.Create(new DMesh3(), PreviewMaterial); Scene.AddSceneObject(PreviewSO); postprocess_target_objects(); base_initialize_parameters(); }
public void Calculate(DMesh3Builder mesh) { var icp = ICP(mesh); DMesh3 source = mesh.Meshes[1]; //Func<Vector3d, Vector3d> TransformVector = (x, f) => //{ // return (Vector3d) //}; Func <Vector3d, Vector3d> TransformF = (v1) => { return(v1 += icp.Translation); }; MeshTransforms.PerVertexTransform(source, TransformF); }
public virtual void Setup() { // push history stream, so that we can do undo/redo internal to tool, // that will not end up in external history push_history_stream(); if (OnApplyF == null) { OnApplyF = this.add_to_scene; } combineMesh = new DMesh3(); MeshEditor editor = new MeshEditor(combineMesh); foreach (var so in InputSOs) { DMesh3 inputMesh = so.Mesh; int[] mapV; if (editor.AppendMesh(so.Mesh, out mapV)) { MeshTransforms.PerVertexTransform(combineMesh, inputMesh, mapV, (v, old_id, new_id) => { return(SceneTransforms.ObjectToSceneP(so, v)); }); } } ; MeshSourceOp = new ConstantMeshSourceOp(combineMesh, true, true); SeparateOp = new SeparateSolidsOp() { MeshSource = MeshSourceOp }; ComputeOp = new ThreadedResultComputeOp <List <DMesh3> >() { ResultSource = SeparateOp }; if (HiddenPreviewMaterial == null) { HiddenPreviewMaterial = SOMaterial.CreateTransparent("remove_hidden_generated", new Colorf(Colorf.DimGrey, 0.5f)); } if (KeepPreviewMaterial == null) { KeepPreviewMaterial = SOMaterial.CreateFlatShaded("remove_keep_generated", Colorf.DimGrey); } }
public AppendInfo AppendConnectorTo(DMesh3 mesh, Vector3d translate) { validate_geometry(); AppendInfo info = new AppendInfo(); MeshEditor editor = new MeshEditor(mesh); int[] mapV; if (HasInner) { info.InnerGID = mesh.AllocateTriangleGroup(); editor.AppendMesh(InnerMesh, out mapV, info.InnerGID); info.InnerLoop = EdgeLoop.FromVertices(mesh, new MappedList(InnerLoop.Vertices, mapV)); MeshTransforms.PerVertexTransform(mesh, InnerMesh.VertexIndices(), (vid) => { return(mapV[vid]); }, (v, old_vid, new_vid) => { return(v + translate); }); } else { info.InnerGID = -1; info.InnerLoop = null; } info.OuterGID = mesh.AllocateTriangleGroup(); editor.AppendMesh(OuterMesh, out mapV, info.OuterGID); info.OuterLoop = EdgeLoop.FromVertices(mesh, new MappedList(OuterLoop.Vertices, mapV)); MeshTransforms.PerVertexTransform(mesh, OuterMesh.VertexIndices(), (vid) => { return(mapV[vid]); }, (v, old_vid, new_vid) => { return(v + translate); }); return(info); }
static void RunCalc() { // Target = Element you transform around. // Source = Element you want to transform. DMesh3Builder builder = new DMesh3Builder(); StandardMeshReader reader = new StandardMeshReader(); reader.MeshBuilder = builder; var Target = reader.Read(@"D:\Eksamen 4th semester\Pointclouds\triangle4.obj", new ReadOptions()); var Source = reader.Read(@"D:\Eksamen 4th semester\Pointclouds\triangle3.obj", new ReadOptions()); DMeshAABBTree3 tree = new DMeshAABBTree3(builder.Meshes[0], autoBuild: true); MeshICP calc = new MeshICP(builder.Meshes[1], tree); calc.Solve(); calc.Solve(bUpdate: true); Console.WriteLine(calc.Rotation); Console.WriteLine(calc.Translation); Console.WriteLine(); DMesh3 source = builder.Meshes[1]; DMesh3 target = builder.Meshes[0]; //foreach (var item in source.Vertices()) //{ // Console.WriteLine(item); //} //Console.WriteLine("--------------------"); Func <Vector3d, Vector3d> TransformF = (v1) => { return(v1 += calc.Translation); }; MeshTransforms.PerVertexTransform(source, TransformF); var tarver = target.Vertices(); List <Vector3d> tarlist = tarver.ToList(); trees = new KdTree <double, string>(3, new DoubleMath()); foreach (var item in tarlist) { trees.Add(new double[] { item.x, item.y, item.z }, item.xy.ToString()); } for (var findLimit = 0; findLimit <= tarlist.Count; findLimit++) { } double[][] tar = new double[tarlist.Count][]; int i = 0; foreach (var item in tarlist) { tar[i] = new double[] { item.x, item.y, 1 }; i++; } int k = 3; int numClasses = 300; Console.WriteLine("With k = 3"); List <int> res = new List <int>(); foreach (var item in target.Vertices()) { double[] src = new double[] { item.x, item.y, item.z }; int predicted = Classify(src, tar, numClasses, k); } }
public virtual void Setup() { // push history stream, so that we can do undo/redo internal to tool, // that will not end up in external history push_history_stream(); if (OnApplyF == null) { OnApplyF = this.add_so_to_scene; } if (PreviewMaterial == null) { PreviewMaterial = SOMaterial.CreateMesh("tool_generated", Colorf.DimGrey); } if (ErrorMaterial == null) { ErrorMaterial = SOMaterial.CreateMesh("tool_generated_error", Colorf.VideoRed); } // clear selection here so that multi-select GroupSO goes away, otherwise // when we copy frmaes below, they are relative to that GroupSO, and things move inputSelection = new List <SceneObject>(Scene.Selected); set_allow_selection_changes(true); Scene.ClearSelection(); set_allow_selection_changes(false); if (InputSOs.Count == 1 && ForceSceneSpaceComputation == false) { combineMesh = new DMesh3(InputSOs[0].Mesh); sceneToObjUnitScale = SceneTransforms.SceneToObject(InputSOs[0], 1.0f); } else { combineMesh = new DMesh3(); MeshEditor editor = new MeshEditor(combineMesh); foreach (var so in InputSOs) { TransformSequence xform = SceneTransforms.ObjectToSceneXForm(so); DMesh3 inputMesh = so.Mesh; int[] mapV; if (editor.AppendMesh(so.Mesh, out mapV)) { MeshTransforms.PerVertexTransform(combineMesh, inputMesh, mapV, (v, old_id, new_id) => { return(xform.TransformP(v)); }); } } ; sceneToObjUnitScale = 1.0; } MeshSourceOp = new ConstantMeshSourceOp(combineMesh, true, true); EditOp = edit_op_factory(MeshSourceOp); ComputeOp = new ThreadedMeshComputeOp() { MeshSource = EditOp }; PreviewSO = new DMeshSO() { EnableSpatial = EnablePreviewSpatial }; PreviewSO.Create(new DMesh3(), PreviewMaterial); if (InputSOs.Count == 1 && ForceSceneSpaceComputation == false) { PreviewSO.SetLocalFrame(InputSOs[0].GetLocalFrame(CoordSpace.ObjectCoords), CoordSpace.ObjectCoords); PreviewSO.SetLocalScale(InputSOs[0].GetLocalScale()); } Scene.AddSceneObject(PreviewSO); postprocess_target_objects(); base_initialize_parameters(); }
public bool Insert() { Func <int, bool> is_contained_v = (vid) => { Vector3d v = Mesh.GetVertex(vid); Vector2f vf2 = ProjectFrame.ToPlaneUV((Vector3f)v, 2); return(Polygon.Contains(vf2)); }; MeshVertexSelection vertexROI = new MeshVertexSelection(Mesh); Index3i seedT = Mesh.GetTriangle(SeedTriangle); // if a seed vert of seed triangle is containd in polygon, we will // flood-fill out from there, this gives a better ROI. // If not, we will try flood-fill from the seed triangles. List <int> seed_verts = new List <int>(); for (int j = 0; j < 3; ++j) { if (is_contained_v(seedT[j])) { seed_verts.Add(seedT[j]); } } if (seed_verts.Count == 0) { seed_verts.Add(seedT.a); seed_verts.Add(seedT.b); seed_verts.Add(seedT.c); } // flood-fill out from seed vertices until we have found all vertices // contained in polygon vertexROI.FloodFill(seed_verts.ToArray(), is_contained_v); // convert vertex ROI to face ROI MeshFaceSelection faceROI = new MeshFaceSelection(Mesh, vertexROI, 1); faceROI.ExpandToOneRingNeighbours(); faceROI.FillEars(true); // this might be a good idea... // construct submesh RegionOperator regionOp = new RegionOperator(Mesh, faceROI); DSubmesh3 roiSubmesh = regionOp.Region; DMesh3 roiMesh = roiSubmesh.SubMesh; // save 3D positions of unmodified mesh Vector3d[] initialPositions = new Vector3d[roiMesh.MaxVertexID]; // map roi mesh to plane MeshTransforms.PerVertexTransform(roiMesh, roiMesh.VertexIndices(), (v, vid) => { Vector2f uv = ProjectFrame.ToPlaneUV((Vector3f)v, 2); initialPositions[vid] = v; return(new Vector3d(uv.x, uv.y, 0)); }); // save a copy of 2D mesh and construct bvtree. we will use // this later to project back to 3d // [TODO] can we use a better spatial DS here, that takes advantage of 2D? DMesh3 projectMesh = new DMesh3(roiMesh); DMeshAABBTree3 projecter = new DMeshAABBTree3(projectMesh, true); MeshInsertUVPolyCurve insertUV = new MeshInsertUVPolyCurve(roiMesh, Polygon); //insertUV.Validate() bool bOK = insertUV.Apply(); if (!bOK) { throw new Exception("insertUV.Apply() failed"); } if (SimplifyInsertion) { insertUV.Simplify(); } int[] insertedPolyVerts = insertUV.CurveVertices; // grab inserted loop, assuming it worked EdgeLoop insertedLoop = null; if (insertUV.Loops.Count == 1) { insertedLoop = insertUV.Loops[0]; } // find interior triangles List <int> interiorT = new List <int>(); foreach (int tid in roiMesh.TriangleIndices()) { Vector3d centroid = roiMesh.GetTriCentroid(tid); if (Polygon.Contains(centroid.xy)) { interiorT.Add(tid); } } if (RemovePolygonInterior) { MeshEditor editor = new MeshEditor(roiMesh); editor.RemoveTriangles(interiorT, true); InteriorTriangles = null; } else { InteriorTriangles = interiorT.ToArray(); } // map back to 3d Vector3d a = Vector3d.Zero, b = Vector3d.Zero, c = Vector3d.Zero; foreach (int vid in roiMesh.VertexIndices()) { // [TODO] somehow re-use exact positions from regionOp maps? // construct new 3D pos w/ barycentric interpolation Vector3d v = roiMesh.GetVertex(vid); int tid = projecter.FindNearestTriangle(v); Index3i tri = projectMesh.GetTriangle(tid); projectMesh.GetTriVertices(tid, ref a, ref b, ref c); Vector3d bary = MathUtil.BarycentricCoords(ref v, ref a, ref b, ref c); Vector3d pos = bary.x * initialPositions[tri.a] + bary.y * initialPositions[tri.b] + bary.z * initialPositions[tri.c]; roiMesh.SetVertex(vid, pos); } bOK = BackPropagate(regionOp, insertedPolyVerts, insertedLoop); return(bOK); }