示例#1
0
 /// <summary>
 /// Finds the Segment from the DCurve3 closes to the position
 /// </summary>
 /// <param name="curve">DCurve3</param>
 /// <param name="position">Vector3d</param>
 /// <returns>Integer Sgement index</returns>
 public static int NearestSegment(this DCurve3 curve, Vector3d position)
 {
     _ = curve.DistanceSquared(position, out int iSeg, out double tangent);
     return(iSeg);
 }
        protected override void Update_GenerateMap()
        {
            base.begin_update();

            if (MeshSource == null)
            {
                throw new Exception("EnclosedRegionOffsetOp: must set valid MeshSource to compute!");
            }
            if (MeshSource.HasSpatial == false)
            {
                throw new Exception("EnclosedRegionOffsetOp: MeshSource must have spatial data structure!");
            }

            IMesh imesh = MeshSource.GetIMesh();

            if (imesh.HasVertexNormals == false)
            {
                throw new Exception("EnclosedRegionOffsetOp: input mesh does not have surface normals...");
            }
            if (imesh is DMesh3 == false)
            {
                throw new Exception("EnclosedRegionOffsetOp: in current implementation, input mesh must be a DMesh3. Ugh.");
            }
            DMesh3   mesh    = imesh as DMesh3;
            ISpatial spatial = MeshSource.GetSpatial();

            DCurve3           curve = new DCurve3(CurveSource.GetICurve());
            MeshFacesFromLoop loop  = new MeshFacesFromLoop(mesh, curve, spatial);

            // [RMS] this is all f'n ugly!

            MeshVertexSelection selection = new MeshVertexSelection(mesh);

            selection.SelectTriangleVertices(loop.InteriorTriangles);


            // [TODO] do this inline w/ loop below? but then no maxdist!
            Dictionary <int, double> dists = new Dictionary <int, double>();
            double max_dist = 0;

            foreach (int vid in selection)
            {
                Vector3d v = mesh.GetVertex(vid);
                int      inearseg; double nearsegt;
                double   min_dist_sqr = curve.DistanceSquared(v, out inearseg, out nearsegt);
                min_dist_sqr = Math.Sqrt(min_dist_sqr);
                max_dist     = Math.Max(min_dist_sqr, max_dist);
                dists[vid]   = min_dist_sqr;
            }


            lock (Displacement) {
                Displacement.Clear();
                Displacement.Resize(mesh.MaxVertexID);

                // todo: can do this in parallel...
                foreach (int vid in selection)
                {
                    //Vector3d v = mesh.GetVertex(vid);

                    // [TODO]...
                    double dist    = max_dist - dists[vid];
                    double falloff = Falloff.FalloffT(dist / max_dist);

                    Vector3d n = mesh.GetVertexNormal(vid);
                    n = n - n.Dot(normal) * normal;
                    n.Normalize();

                    Displacement[vid] = falloff * offset_distance * n;
                }
            }

            // smooth it?

            base.complete_update();
        }