public CurveSample2d Sample(double f)
        {
            if (f <= 0)
            {
                return(new CurveSample2d(new Vector2d(positions[0]), tangent(0)));
            }

            int N = arc_len.Length;

            if (f >= arc_len[N - 1])
            {
                return(new CurveSample2d(new Vector2d(positions[N - 1]), tangent(N - 1)));
            }

            for (int k = 0; k < N; ++k)
            {
                if (f < arc_len[k])
                {
                    int a = k - 1;
                    int b = k;
                    if (arc_len[a] == arc_len[b])
                    {
                        return(new CurveSample2d(new Vector2d(positions[a]), tangent(a)));
                    }

                    double t = (f - arc_len[a]) / (arc_len[b] - arc_len[a]);
                    return(new CurveSample2d(
                               Vector2d.Lerp(positions[a], positions[b], t),
                               Vector2d.Lerp(tangent(a), tangent(b), t)));
                }
            }

            throw new ArgumentException("SampledArcLengthParam2d.Sample: somehow arc len is outside any possible range");
        }
        protected Vector2d bilerp(ref Vector2d v00, ref Vector2d v10, ref Vector2d v11, ref Vector2d v01, double tx, double ty)
        {
            Vector2d a = Vector2d.Lerp(ref v00, ref v01, ty);
            Vector2d b = Vector2d.Lerp(ref v10, ref v11, ty);

            return(Vector2d.Lerp(a, b, tx));
        }
Beispiel #3
0
        /// <summary>
        /// Find and remove any junction (ie valence>2) vertices of the graph.
        /// At a junction, the pair of best-aligned (ie straightest) edges are left
        /// connected, and all the other edges are disconnected
        ///
        /// [TODO] currently there is no DGraph2.SetEdge(), so the 'other' edges
        /// are deleted and new edges inserted. Hence, edge IDs are not preserved.
        /// </summary>
        public static int DisconnectJunctions(DGraph2 graph)
        {
            var junctions = new List <int>();

            // find all junctions
            foreach (int vid in graph.VertexIndices())
            {
                if (graph.IsJunctionVertex(vid))
                {
                    junctions.Add(vid);
                }
            }


            foreach (int vid in junctions)
            {
                Vector2d v         = graph.GetVertex(vid);
                int[]    nbr_verts = graph.VtxVerticesItr(vid).ToArray();

                // find best-aligned pair of edges connected to vid
                Index2i best_aligned = Index2i.Max; double max_angle = 0;
                for (int i = 0; i < nbr_verts.Length; ++i)
                {
                    for (int j = i + 1; j < nbr_verts.Length; ++j)
                    {
                        double angle = Vector2d.AngleD(
                            (graph.GetVertex(nbr_verts[i]) - v).Normalized,
                            (graph.GetVertex(nbr_verts[j]) - v).Normalized);
                        angle = Math.Abs(angle);
                        if (angle > max_angle)
                        {
                            max_angle    = angle;
                            best_aligned = new Index2i(nbr_verts[i], nbr_verts[j]);
                        }
                    }
                }

                // for nbr verts that are not part of the best_aligned edges,
                // we remove those edges and add a new one connected to a new vertex
                for (int k = 0; k < nbr_verts.Length; ++k)
                {
                    if (nbr_verts[k] == best_aligned.a || nbr_verts[k] == best_aligned.b)
                    {
                        continue;
                    }

                    int eid = graph.FindEdge(vid, nbr_verts[k]);
                    graph.RemoveEdge(eid, true);
                    if (graph.IsVertex(nbr_verts[k]))
                    {
                        var newpos = Vector2d.Lerp(graph.GetVertex(nbr_verts[k]), v, 0.99);
                        int newv   = graph.AppendVertex(newpos);
                        graph.AppendEdge(nbr_verts[k], newv);
                    }
                }
            }

            return(junctions.Count);
        }
Beispiel #4
0
        /// <summary>
        /// foreach edge [vid,b] connected to junction vertex vid, remove, add new vertex c,
        /// and then add new edge [b,c]. Optionally move c a bit back along edge from vid.
        /// </summary>
        public static void DisconnectJunction(DGraph2 graph, int vid, double shrinkFactor = 1.0)
        {
            Vector2d v = graph.GetVertex(vid);

            int[] nbr_verts = graph.VtxVerticesItr(vid).ToArray();
            for (int k = 0; k < nbr_verts.Length; ++k)
            {
                int eid = graph.FindEdge(vid, nbr_verts[k]);
                graph.RemoveEdge(eid, true);
                if (graph.IsVertex(nbr_verts[k]))
                {
                    Vector2d newpos = Vector2d.Lerp(graph.GetVertex(nbr_verts[k]), v, shrinkFactor);
                    int      newv   = graph.AppendVertex(newpos);
                    graph.AppendEdge(nbr_verts[k], newv);
                }
            }
        }