Exemple #1
0
        public virtual void Update()
        {
            base.begin_update();

            if (MeshSource == null)
            {
                throw new Exception("TrimMeshFromCurveOp: must set valid MeshSource to compute!");
            }
            if (CurveSource == null)
            {
                throw new Exception("TrimMeshFromCurveOp: must set valid CurveSource to compute!");
            }

            IMesh meshIn = MeshSource.GetIMesh();
            //ISpatial spatialIn = MeshSource.GetSpatial();

            DCurve3 curve = new DCurve3(CurveSource.GetICurve());

            TrimmedMesh = new DMesh3(meshIn, MeshHints.None);

            AxisAlignedBox3d bounds = TrimmedMesh.CachedBounds;
            Vector3d         seed   = bounds.Center + bounds.Extents.y * Vector3d.AxisY;

            MeshTrimLoop trim = new MeshTrimLoop(TrimmedMesh, curve, seed, null);

            trim.Trim();

            ApplyModifiers(TrimmedMesh);

            base.complete_update();
        }
Exemple #2
0
        void CreateNewPreview(bool bIsCurve)
        {
            preview      = null;
            curvePreview = null;

            if (bIsCurve)
            {
                curvePreview = new CurveRevolvePreview();
                curvePreview.Create(scene.TransparentNewSOMaterial, scene.RootGameObject, -1);
                sceneCurve = new DCurve3();
                curvePreview.RevolveCurve = sceneCurve;
                axisCurve = new DCurve3();
                curvePreview.AxisCurve = axisCurve;
                curvePreview.Visible   = false;
            }
            else
            {
                preview = new RevolvePreview();
                preview.Create(scene.TransparentNewSOMaterial, scene.RootGameObject, -1);
                sceneCurve      = new DCurve3();
                preview.Curve   = sceneCurve;
                preview.Axis    = scene.SceneFrame;
                preview.Visible = false;
            }

            // need to do this if we switch between revolve-around-object and revolve-around-curve
            if (curveSO != null)
            {
                update_curve_vertices(curveSO);
            }
        }
Exemple #3
0
        // figuring out a decent line width is tricky. Want to be responsive to camera
        //  pos, so line doesn't get super-thick when zoomed in. So we want to measure
        //  screen-space radius. But off-screen vertices are a problem. So, only consider
        //  vertices within a level, pointing-forward view cone (can't be actual view cone
        //  because then line thickness changes as you turn head!).
        //
        //  Also sub-sample verts for efficiency. Probably we don't need to do this
        //  every frame...but how to distribute?
        //
        //  ***returns 0*** if we couldn't find any points in view cone
        public static float EstimateStableCurveWidth(FScene scene,
                                                     Frame3f curveFrameS, DCurve3 curve, float fVisualAngleDeg)
        {
            // do computations in Scene coords..."safest"?

            Vector3f camPos     = scene.ActiveCamera.GetPosition();
            Vector3f camForward = scene.ActiveCamera.Forward();

            // use level-forward
            camForward[1] = 0; camForward.Normalize();

            camPos     = scene.ToSceneP(camPos);
            camForward = scene.ToSceneN(camForward);

            const float ViewConeDotThresh = 0.707106f;   // 45 degrees
            int         nSubSampleInc     = Math.Max(2, curve.VertexCount / 10);

            float rSum = 0; int iSum = 0;

            for (int k = 0; k < curve.VertexCount; k += nSubSampleInc)
            {
                Vector3f vS = (Vector3f)curve.GetVertex(k);
                vS = curveFrameS.FromFrameP(vS);
                Vector3f dv = (vS - camPos).Normalized;
                if (dv.Dot(camForward) < ViewConeDotThresh)
                {
                    continue;
                }

                float r = VRUtil.GetVRRadiusForVisualAngle(vS, camPos, fVisualAngleDeg);
                rSum += r; iSum++;
            }
            return((rSum == 0) ? 0 :  scene.ToWorldDimension(rSum / (float)iSum));
        }
Exemple #4
0
        void draw_lines(LineSet lines, Vector3f cameraPos)
        {
            GL.Begin(GL.LINES);
            GL.Color(lines.Color);
            int NS = lines.Segments.Count;

            for (int k = 0; k < NS; ++k)
            {
                Segment3d seg = lines.Segments[k];
                GL.Vertex((Vector3)seg.P0);
                GL.Vertex((Vector3)seg.P1);
            }
            GL.End();

            int NC = lines.Curves.Count;

            for (int k = 0; k < NC; ++k)
            {
                DCurve3 c = lines.Curves[k];
                GL.Begin(GL.LINE_STRIP);
                GL.Color(lines.Color);
                int NV = c.VertexCount;
                for (int i = 0; i < NV; ++i)
                {
                    GL.Vertex((Vector3)c[i]);
                }
                if (c.Closed)
                {
                    GL.Vertex((Vector3)c[0]);
                }
                GL.End();
            }
        }
Exemple #5
0
        /// <summary>
        /// Request a copy of the internal curve from a background thread.
        /// If curve is dirty, this will block until it is computed in PreRender (which will then signal this thread).
        /// </summary>
        public DCurve3 RequestCurveCopyFromBGThread()
        {
            if (FPlatform.InMainThread())
            {
                throw new Exception("ThreadSafePolyCurveSO.RequestCurveCopyFromBGThread: called from main thread!");
            }

            bool wait_for_compute = false;

            lock (curve_copy_lock) {
                if (curve_copy == null || this.curve.Timestamp != curve_timestamp || target_model_modified)
                {
                    wait_for_compute = true;
                }
            }
            if (wait_for_compute)
            {
                waiter.WaitOne();
            }

            DCurve3 result;

            lock (curve_copy_lock) {
                result           = new DCurve3(curve_copy);
                result.Timestamp = curve_copy.Timestamp;
            }
            return(result);
        }
Exemple #6
0
        public override void PreRender()
        {
            base.PreRender();

            if (this.curve.Timestamp != curve_timestamp || target_model_modified)
            {
                // lock copy-curve while we rebuild it
                lock (curve_copy_lock) {
                    curve_copy = new DCurve3(this.curve);

                    if (TransformMode == OutputCurveTransform.ToTargetSO && TargetModelSO == null)
                    {
                        throw new InvalidOperationException("ThreadSafePolyCurveSO.PreRender: no TargetSO but target mode requested!");
                    }

                    for (int k = 0; k < curve_copy.VertexCount; ++k)
                    {
                        Vector3f f = (Vector3f)curve_copy[k];
                        f = SceneTransforms.ObjectToSceneP(this, f);
                        if (TransformMode == OutputCurveTransform.ToTargetSO)
                        {
                            f = SceneTransforms.SceneToObjectP(TargetModelSO, f);
                        }
                        curve_copy[k] = f;
                    }
                    curve_copy.Timestamp = this.curve.Timestamp;
                }
                waiter.Set();  // signal any threads waiting on this update

                curve_timestamp       = this.curve.Timestamp;
                target_model_modified = false;

                on_curve_validated();
            }
        }
Exemple #7
0
        /// <summary>
        /// Creates ag3.DCurve from a geometry
        /// </summary>
        /// <param name="curve"> this curve</param>
        /// <param name="geom"> the OGR geometry to ue as ths source</param>
        /// <param name="crs"> the crs to u for the DCurve3 DEFAULT map default projections or EPG:4326 if none</param>
        /// <returns></returns>
        public static DCurve3 FromGeometry(this DCurve3 curve, Geometry geom, SpatialReference crs = null)
        {
            if (geom.GetSpatialReference() == null)
            {
                if (crs != null)
                {
                    geom.AssignSpatialReference(crs);
                }
                else
                {
                    geom.AssignSpatialReference(AppState.instance.projectCrs);
                }
            }
            if (geom.TransformTo(AppState.instance.mapProj) != 0)
            {
                throw new NotSupportedException("projection failed");
            }
            if (geom.Transform(AppState.instance.mapTrans) != 0)
            {
                throw new NotSupportedException("axis change failed");
            }
            int n = geom.GetPointCount();

            Vector3d[] ls = new Vector3d[n];
            for (int i = 0; i < n; i++)
            {
                double[] argout = new double[3];
                geom.GetPoint(i, argout);
                ls[i] = new Vector3d(argout);
            }
            curve.ClearVertices();
            curve.SetVertices(ls);
            curve.Closed = geom.IsRing();
            return(curve);
        }
Exemple #8
0
        public PolyCurveSO Create(SOMaterial defaultMaterial)
        {
            if (curve == null)
            {
                LineGenerator gen = new LineGenerator()
                {
                    Start = Vector3.zero, End = 10.0f * Vector3.up, StepSize = 0.1f
                };
                gen.Generate();
                curve = new DCurve3();
                gen.Make(curve);
            }

            // assumes type identifier is something like BlahBlahSO
            root = new GameObject(UniqueNames.GetNext(Type.identifier.Remove(Type.identifier.Length - 2)));

            if (EnableLineRenderer)
            {
                LineRenderer ren = root.AddComponent <LineRenderer>();
                ren.startWidth    = ren.endWidth = 0.05f;
                ren.useWorldSpace = false;
            }

            AssignSOMaterial(defaultMaterial);       // need to do this to setup BaseSO material stack
            Material useMaterial = CurrentMaterial;

            Create_internal(useMaterial);

            UpdateGeometry();

            increment_timestamp();

            return(this);
        }
Exemple #9
0
        public static void TestOffsetAnimation()
        {
            Window window = new Window("TestFill");

            window.SetDefaultSize(600, 600);
            window.SetPosition(WindowPosition.Center);

            DMesh3            mesh  = StandardMeshReader.ReadMesh("c:\\scratch\\remesh.obj");
            MeshBoundaryLoops loops = new MeshBoundaryLoops(mesh);
            DCurve3           curve = loops[0].ToCurve();
            Polygon2d         poly  = new Polygon2d();

            foreach (Vector3d v in curve.Vertices)
            {
                poly.AppendVertex(v.xy);
            }
            Outer = new GeneralPolygon2d(poly);

            DebugViewCanvas view = new DebugViewCanvas();

            view.AddPolygon(Outer, Colorf.Black);


            DGraph2 graph = TopoOffset2d.QuickCompute(Outer, AnimOffset, AnimSpacing);

            view.AddGraph(graph, Colorf.Red);

            window.Add(view);
            window.ShowAll();

            Active = view;
        }
Exemple #10
0
        void enable_circle_indicator(bool enable)
        {
            if (enable == false && circle_indicator == null)
            {
                return;
            }
            if (enable && circle_indicator == null)
            {
                LineSet lines = new LineSet();
                lines.UseFixedNormal = true;
                lines.FixedNormal    = Vector3f.AxisY;
                DCurve3 curve = new DCurve3(Polygon2d.MakeCircle(gizmoInitialRadius, 64), 0, 2);
                lines.Curves.Add(curve);
                lines.Width     = 1.0f;
                lines.WidthType = LineWidthType.Pixel;
                lines.Segments.Add(
                    new Segment3d(Vector3d.Zero, gizmoInitialRadius * diagonals[nRotationAxis]));
                lines.Color      = Colorf.DimGrey;
                circle_indicator = new fLineSetGameObject(new GameObject(), lines, "circle");
                circle_indicator.SetLayer(FPlatform.WidgetOverlayLayer, true);

                circle_indicator.SetLocalRotation(Quaternionf.FromTo(Vector3f.AxisY, Frame3f.Identity.GetAxis(nRotationAxis)));
                RootGameObject.AddChild(circle_indicator, false);
            }
            circle_indicator.SetVisible(enable);
        }
Exemple #11
0
 protected void GetPlanarPolys(PlanarRegion r, List <Polygon2d> polys)
 {
     try {
         MeshRegionBoundaryLoops loops = new MeshRegionBoundaryLoops(r.Mesh, r.Triangles);
         foreach (var loop in loops)
         {
             DCurve3   curve = loop.ToCurve();
             Polygon2d poly  = new Polygon2d();
             int       NV    = curve.VertexCount;
             for (int k = 0; k < NV; k++)
             {
                 poly.AppendVertex(curve[k].xy);
             }
             polys.Add(poly);
         }
     } catch (Exception) {
         // add each triangle as a polygon and let clipper sort it out
         Vector3d v0 = Vector3d.Zero, v1 = Vector3d.Zero, v2 = Vector3d.Zero;
         foreach (int tid in r.Triangles)
         {
             r.Mesh.GetTriVertices(tid, ref v0, ref v1, ref v2);
             Polygon2d p = new Polygon2d();
             p.AppendVertex(v0.xy); p.AppendVertex(v1.xy); p.AppendVertex(v2.xy);
             polys.Add(p);
         }
     }
 }
Exemple #12
0
        public override List <ISnapPoint> GeneratePoints(SceneObject so)
        {
            List <ISnapPoint> v = new List <ISnapPoint>();

            if (so is PolyCurveSO)
            {
                PolyCurveSO curveSO = so as PolyCurveSO;
                DCurve3     curve   = curveSO.Curve;

                Frame3f f = Frame3f.Identity;

                Frame3f startFrame = new Frame3f((Vector3f)curve.Start, -(Vector3f)curve.Tangent(0), 1);
                v.Add(new SOFrameSnapPoint(curveSO)
                {
                    frame = f.FromFrame(startFrame)
                });
                Frame3f endFrame = new Frame3f((Vector3f)curve.End, (Vector3f)curve.Tangent(curve.VertexCount - 1), 1);
                v.Add(new SOFrameSnapPoint(curveSO)
                {
                    frame = f.FromFrame(endFrame)
                });
            }

            if (base.EnableGeometry)
            {
                base.build_geometry(so, v);
            }

            return(v);
        }
 public ImplicitCurve3d(DCurve3 curve, double radius)
 {
     Curve  = curve;
     Radius = radius;
     Box    = curve.GetBoundingBox();
     Box.Expand(Radius);
     spatial = new DCurve3BoxTree(curve);
 }
Exemple #14
0
        void draw_quads(LineSet lines, Vector3f cameraPos)
        {
            GL.Begin(GL.QUADS);
            GL.Color(lines.Color);

            int NS = lines.Segments.Count;

            for (int k = 0; k < NS; ++k)
            {
                Segment3d seg = lines.Segments[k];
                Vector3f  start = (Vector3f)seg.P0, end = (Vector3f)seg.P1;
                if (lines.UseFixedNormal)
                {
                    draw_quad_fixednormal(ref start, ref end, ref lines.FixedNormal, lines.Width);
                }
                else
                {
                    draw_quad_viewalign(ref start, ref end, ref cameraPos, lines.Width);
                }
            }

            int NC = lines.Curves.Count;

            for (int k = 0; k < NC; ++k)
            {
                DCurve3  c    = lines.Curves[k];
                int      NV   = c.VertexCount;
                Vector3f prev = (Vector3f)c[0];
                for (int i = 1; i < NV; ++i)
                {
                    Vector3f cur = (Vector3f)c[i];
                    if (lines.UseFixedNormal)
                    {
                        draw_quad_fixednormal(ref prev, ref cur, ref lines.FixedNormal, lines.Width);
                    }
                    else
                    {
                        draw_quad_viewalign(ref prev, ref cur, ref cameraPos, lines.Width);
                    }
                    prev = cur;
                }
                if (c.Closed)
                {
                    Vector3f cur = (Vector3f)c[0];
                    if (lines.UseFixedNormal)
                    {
                        draw_quad_fixednormal(ref prev, ref cur, ref lines.FixedNormal, lines.Width);
                    }
                    else
                    {
                        draw_quad_viewalign(ref prev, ref cur, ref cameraPos, lines.Width);
                    }
                }
            }

            GL.End();
        }
Exemple #15
0
        public DCurve3 RequestCurveCopyFromMainThread()
        {
            DCurve3 result;

            lock (curve_copy_lock) {
                result = new DCurve3(curve_copy);
            }
            return(result);
        }
Exemple #16
0
 /// <summary>
 /// Creates g3.DCurve from Vector3[]
 /// </summary>
 /// <param name="curve">DCurve</param>
 /// <param name="verteces">Vextor3[]</param>
 /// <param name="bClosed">whether the line is closed</param>
 public static DCurve3 Vector3(this DCurve3 curve, Vector3[] verteces, bool bClosed)
 {
     curve.ClearVertices();
     curve.Closed = bClosed;
     foreach (Vector3 vertex in verteces)
     {
         curve.AppendVertex(vertex);
     }
     return(curve);
 }
        public static void compute_distance_image()
        {
            DMesh3            mesh  = StandardMeshReader.ReadMesh("c:\\scratch\\remesh.obj");
            MeshBoundaryLoops loops = new MeshBoundaryLoops(mesh);
            DCurve3           curve = loops[0].ToCurve();
            Polygon2d         poly  = new Polygon2d();

            foreach (Vector3d v in curve.Vertices)
            {
                poly.AppendVertex(v.xy);
            }
            int      N        = 1024;
            double   cellsize = poly.Bounds.MaxDim / (double)N;
            Vector2d o        = poly.Bounds.Min;

            o -= 4 * cellsize * Vector2d.One;
            N += 8;

            ShiftGridIndexer2 indexer = new ShiftGridIndexer2(poly.Bounds.Min, cellsize);

            double[] df   = new double[N * N];
            double   maxd = 0;

            for (int yi = 0; yi < N; ++yi)
            {
                for (int xi = 0; xi < N; ++xi)
                {
                    Vector2d p = indexer.FromGrid(new Vector2i(xi, yi));
                    double   d = Math.Sqrt(poly.DistanceSquared(p));
                    df[yi * N + xi] = d;
                    maxd            = Math.Max(d, maxd);
                }
            }

            SKBitmap bmp = new SKBitmap(N, N);

            for (int yi = 0; yi < N; ++yi)
            {
                for (int xi = 0; xi < N; ++xi)
                {
                    double d = df[yi * N + xi];
                    float  f = (float)(d / maxd);
                    byte   b = (byte)(int)(f * 255);
                    bmp.SetPixel(xi, yi, new SKColor(b, b, b));
                }
            }

            using (var image = SKImage.FromBitmap(bmp))
                using (var data = image.Encode(SKEncodedImageFormat.Png, 80)) {
                    // save the data to a stream
                    using (var stream = File.OpenWrite("c:\\scratch\\distances.png")) {
                        data.SaveTo(stream);
                    }
                }
        }
Exemple #18
0
        public static DCurve3 ExtractLoopV(IMesh mesh, int[] vertices)
        {
            DCurve3 curve = new DCurve3();

            for (int i = 0; i < vertices.Length; ++i)
            {
                curve.AppendVertex(mesh.GetVertex(vertices[i]));
            }
            curve.Closed = true;
            return(curve);
        }
Exemple #19
0
        public static Rhino.Geometry.PolylineCurve ToRhino(this DCurve3 _crv)
        {
            List <Rhino.Geometry.Point3d> pts = new List <Rhino.Geometry.Point3d>(_crv.VertexCount);

            for (int i = 0; i < _crv.VertexCount; i++)
            {
                pts.Add(_crv[i].ToRhinoPt());
            }

            return(new Rhino.Geometry.PolylineCurve(pts));
        }
Exemple #20
0
 public static void Store(DCurve3 curve, BinaryWriter writer)
 {
     writer.Write(curve.Closed);
     writer.Write(curve.VertexCount);
     for (int i = 0; i < curve.VertexCount; ++i)
     {
         writer.Write(curve[i].x);
         writer.Write(curve[i].y);
         writer.Write(curve[i].z);
     }
 }
Exemple #21
0
        public static void PreserveBoundaryLoops(MeshConstraints cons, NGonsCore.geometry3Sharp.mesh.DMesh3 mesh)
        {
            MeshBoundaryLoops loops = new MeshBoundaryLoops(mesh);

            foreach (EdgeLoop loop in loops)
            {
                DCurve3 loopC = MeshUtil.ExtractLoopV(mesh, loop.Vertices);
                DCurveProjectionTarget target = new DCurveProjectionTarget(loopC);
                ConstrainVtxLoopTo(cons, mesh, loop.Vertices, target);
            }
        }
Exemple #22
0
        public static DCurve3 ExtractLoopV(IMesh mesh, IEnumerable <int> vertices)
        {
            DCurve3 curve = new DCurve3();

            foreach (int vid in vertices)
            {
                curve.AppendVertex(mesh.GetVertex(vid));
            }
            curve.Closed = true;
            return(curve);
        }
Exemple #23
0
        /// <summary>
        /// Converts DCurve3 whihc is in Local Vector3d coordinates to Vector3[] World coordinates
        /// </summary>
        /// <param name="curve">input curve</param>
        /// <returns>Vector3[] in world coordinates</returns>
        public static Vector3[] ToWorld(this DCurve3 curve)
        {
            List <Vector3>  ret      = new List <Vector3>();
            List <Vector3d> vertexes = curve.Vertices as List <Vector3d>;

            for (int i = 0; i < curve.VertexCount; i++)
            {
                Vector3 local = (Vector3)vertexes[i];
                ret.Add(AppState.instance.map.transform.TransformVector(local));
            }
            return(ret.ToArray());
        }
Exemple #24
0
        public virtual SceneObject BuildSO(Func <DCurve3, SceneObject> SOBuilderF, SOMaterial material, float scale = 1.0f)
        {
            // create shifted curve
            Vector3d vCenter      = curve.GetBoundingBox().Center;
            DCurve3  shifted      = bake_transform(-vCenter);
            Frame3f  shiftedFrame = new Frame3f((Vector3f)vCenter, Quaternionf.Identity);

            SceneObject so = SOBuilderF(shifted);

            so.SetLocalFrame(shiftedFrame, CoordSpace.WorldCoords);

            return(so);
        }
Exemple #25
0
        public DCurve3 ExtractDCurve()
        {
            if (!curve_valid)
            {
                Update();
            }

            var result = Curve;

            Curve       = null;
            curve_valid = false;
            return(result);
        }
Exemple #26
0
        public static void Restore(DCurve3 curve, BinaryReader reader)
        {
            curve.Closed = reader.ReadBoolean();
            int count = reader.ReadInt32();

            for (int i = 0; i < count; ++i)
            {
                double x = reader.ReadDouble();
                double y = reader.ReadDouble();
                double z = reader.ReadDouble();
                curve.AppendVertex(new Vector3D(x, y, z));
            }
        }
        void project_to_target()
        {
            PolyCurveSO sourceSO = targets[0];
            DCurve3     curve    = sourceSO.Curve;
            int         N        = curve.VertexCount;

            for (int i = 0; i < N; ++i)
            {
                Vector3f v  = (Vector3f)curve[i];
                Vector3f vW = SceneTransforms.TransformTo(v, sourceSO, CoordSpace.ObjectCoords, CoordSpace.WorldCoords);
                vW       = (Vector3f)ProjectionTarget.Project(vW);
                curve[i] = SceneTransforms.TransformTo(vW, sourceSO, CoordSpace.WorldCoords, CoordSpace.ObjectCoords);
            }
        }
Exemple #28
0
        public void Create(SOMaterial useMaterial, fGameObject parent)
        {
            if (curve == null)
            {
                curve = new DCurve3();
            }

            meshObject = GameObjectFactory.CreateMeshGO("mesh_tube_preview");
            //meshObject.SetMesh(new fMesh())
            meshObject.SetMaterial(MaterialUtil.ToMaterialf(useMaterial));
            bUpdatePending = true;

            parent.AddChild(meshObject, false);
        }
Exemple #29
0
        public MeshFacesFromLoop(NGonsCore.geometry3Sharp.mesh.DMesh3 Mesh, DCurve3 SpaceCurve, ISpatial Spatial, int tSeed)
        {
            this.Mesh = Mesh;

            int N = SpaceCurve.VertexCount;

            InitialLoopT = new int[N];
            for (int i = 0; i < N; ++i)
            {
                InitialLoopT[i] = Spatial.FindNearestTriangle(SpaceCurve[i]);
            }

            find_path();
            find_interior_from_seed(tSeed);
        }
Exemple #30
0
        /// <summary>
        /// Estimates the 3D centroid of a DCurve
        /// </summary>
        /// <param name="curve">DCurve</param>
        /// <returns>Vector3[]</returns>
        public static Vector3d Center(this DCurve3 curve)
        {
            Vector3d center = Vector3d.Zero;
            int      len    = curve.SegmentCount;

            if (!curve.Closed)
            {
                len++;
            }
            for (int i = 0; i < len; i++)
            {
                center += curve.GetVertex(i);
            }
            center /= len;
            return(center);
        }