Exemplo n.º 1
0
        /// <summary>
        /// 指定されたバイナリヒープリストへ値を追加する
        /// </summary>
        /// <param name="list">追加先リスト、バイナリヒープになっている必要がある</param>
        /// <param name="value">追加値</param>
        /// <param name="comparer">比較インターフェース</param>
        public static void PushHeap(FList <T> list, T value, IComparer <T> comparer)
        {
            list.Add(value);
            var last = list.Count;

            if (2 <= last)
            {
                --last;
                PushHeapByIndex(list, last, 0, list[last], comparer);
            }
        }
Exemplo n.º 2
0
        public static void triangulate <T>(Project <T> project, FList <T> poly, FList <TriIdx> result)
        {
            var N = poly.Count;

            result.Clear();
            if (N < 3)
            {
                return;
            }

            result.Capacity = poly.Count - 2;

            if (N == 3)
            {
                result.Add(new TriIdx(0, 1, 2));
                return;
            }

            var vinfo = new vertex_info[N];

            vinfo[0] = new vertex_info(project(poly[0]), 0);
            for (int i = 1; i < N - 1; ++i)
            {
                vinfo[i]          = new vertex_info(project(poly[i]), i);
                vinfo[i].prev     = vinfo[i - 1];
                vinfo[i - 1].next = vinfo[i];
            }
            vinfo[N - 1]      = new vertex_info(project(poly[N - 1]), N - 1);
            vinfo[N - 1].prev = vinfo[N - 2];
            vinfo[N - 1].next = vinfo[0];
            vinfo[0].prev     = vinfo[N - 1];
            vinfo[N - 2].next = vinfo[N - 1];

            for (int i = 0; i < N; ++i)
            {
                vinfo[i].recompute();
            }

            var begin = vinfo[0];

            removeDegeneracies(ref begin, result);

            doTriangulate(begin, result);
        }
Exemplo n.º 3
0
        static int removeDegeneracies(ref vertex_info begin, FList <TriIdx> result)
        {
            vertex_info v;
            vertex_info n;
            int         count  = 0;
            int         remain = 0;

            v = begin;
            do
            {
                v = v.next;
                ++remain;
            } while (v != begin);

            v = begin;
            do
            {
                if (remain < 4)
                {
                    break;
                }

                bool remove = false;
                if (v.p == v.next.p)
                {
                    remove = true;
                }
                else if (v.p == v.next.next.p)
                {
                    if (v.next.p == v.next.next.next.p)
                    {
                        // a 'z' in the loop: z (a) b a b c . remove a-b-a . z (a) a b c . remove a-a-b (next loop) . z a b c
                        // z --(a)-- b
                        //         /
                        //        /
                        //      a -- b -- d
                        remove = true;
                    }
                    else
                    {
                        // a 'shard' in the loop: z (a) b a c d . remove a-b-a . z (a) a b c d . remove a-a-b (next loop) . z a b c d
                        // z --(a)-- b
                        //         /
                        //        /
                        //      a -- c -- d
                        // n.b. can only do this if the shard is pointing out of the polygon. i.e. b is outside z-a-c
                        remove = !internalToAngle(v.next.next.next, v, v.prev, v.next.p);
                    }
                }

                if (remove)
                {
                    result.Add(new TriIdx(v.idx, v.next.idx, v.next.next.idx));
                    n = v.next;
                    if (n == begin)
                    {
                        begin = n.next;
                    }
                    n.remove();
                    count++;
                    remain--;
                }
                else
                {
                    v = v.next;
                }
            } while (v != begin);

            return(count);
        }
Exemplo n.º 4
0
        /**
         * \brief Merge a set of holes into a polygon. (templated)
         *
         * Take a polygon loop and a collection of hole loops, and patch
         * the hole loops into the polygon loop, returning a vector of
         * vertices from the polygon and holes, which describes a new
         * polygon boundary with no holes. The new polygon boundary is
         * constructed via the addition of edges * joining the polygon
         * loop to the holes.
         *
         * This may be applied to arbitrary vertex data (generally
         * carve::geom3d::Vertex pointers), but a projection function must
         * be supplied to convert vertices to coordinates in 2-space, in
         * which the work is performed.
         *
         * @tparam project_t A functor which converts vertices to a 2d
         *                   projection.
         * @tparam vert_t    The vertex type.
         * @param project The projection functor.
         * @param f_loop The polygon loop into which holes are to be
         *               incorporated.
         * @param h_loops The set of hole loops to be incorporated.
         *
         * @return A vector of vertex pointers.
         */
        public static FList <T> incorporateHolesIntoPolygon <T>(Project <T> project, FList <T> f_loop, FList <FList <T> > h_loops)
        {
            var N = f_loop.Count;

            // work out how much space to reserve for the patched in holes.
            for (int i = h_loops.Count - 1; i != -1; i--)
            {
                N += 2 + h_loops[i].Count;
            }

            // this is the vector that we will build the result in.
            var current_f_loop = new FList <T>(N);

            current_f_loop.AddRange(f_loop);

            var h_loop_min_vertex = new FList <Iterator <T> >(h_loops.Count);

            // find the major axis for the holes - this is the axis that we
            // will sort on for finding vertices on the polygon to join
            // holes up to.
            //
            // it might also be nice to also look for whether it is better
            // to sort ascending or descending.
            //
            // another trick that could be used is to modify the projection
            // by 90 degree rotations or flipping about an axis. just as
            // long as we keep the carve::geom3d::Vector pointers for the
            // real data in sync, everything should be ok. then we wouldn't
            // need to accomodate axes or sort order in the main loop.

            // find the bounding box of all the holes.
            bool    first = true;
            element min_x = element.MaxValue, min_y = element.MaxValue, max_x = element.MinValue, max_y = element.MinValue;

            for (int i = h_loops.Count - 1; i != -1; i--)
            {
                var hole = h_loops[i];
                for (int j = hole.Count - 1; j != -1; j--)
                {
                    var curr = project(hole[j]);
                    if (first)
                    {
                        min_x = max_x = curr.X;
                        min_y = max_y = curr.Y;
                        first = false;
                    }
                    else
                    {
                        if (curr.X < min_x)
                        {
                            min_x = curr.X;
                        }
                        if (curr.Y < min_y)
                        {
                            min_y = curr.Y;
                        }
                        if (max_x < curr.X)
                        {
                            max_x = curr.X;
                        }
                        if (max_y < curr.Y)
                        {
                            max_y = curr.Y;
                        }
                    }
                }
            }

            // choose the axis for which the bbox is largest.
            int axis = (max_x - min_x) > (max_y - min_y) ? 0 : 1;

            // for each hole, find the minimum vertex in the chosen axis.
            for (int i = 0, n = h_loops.Count; i < n; i++)
            {
                var hole   = h_loops[i];
                var best_i = MinElementIndex(hole, new order_h_loops <T>(project, axis));
                h_loop_min_vertex.Add(new Iterator <T>(hole, best_i));
            }

            // sort the holes by the minimum vertex.
            h_loop_min_vertex.Sort(new order_h_loops_iterator <T>(project, axis));

            // now, for each hole, find a vertex in the current polygon loop that it can be joined to.
            for (int i = 0; i < h_loop_min_vertex.Count; ++i)
            {
                var N_f_loop = current_f_loop.Count;

                // the index of the vertex in the hole to connect.
                var h_loop_connect = h_loop_min_vertex[i].value;

                var hole_min = project(h_loop_connect);

                // we order polygon loop vertices that may be able to be connected
                // to the hole vertex by their distance to the hole vertex
                var f_loop_heap = new PriorityQueue <int>(new heap_ordering <T>(project, current_f_loop, h_loop_connect, axis), N);

                for (int j = 0; j < N_f_loop; ++j)
                {
                    // it is guaranteed that there exists a polygon vertex with
                    // coord < the min hole coord chosen, which can be joined to
                    // the min hole coord without crossing the polygon
                    // boundary. also, because we merge holes in ascending
                    // order, it is also true that this join can never cross
                    // another hole (and that doesn't need to be tested for).
                    if (project(current_f_loop[j])[axis] <= hole_min[axis])
                    {
                        f_loop_heap.Push(j);
                    }
                }

                // we are going to test each potential (according to the
                // previous test) polygon vertex as a candidate join. we order
                // by closeness to the hole vertex, so that the join we make
                // is as small as possible. to test, we need to check the
                // joining line segment does not cross any other line segment
                // in the current polygon loop (excluding those that have the
                // vertex that we are attempting to join with as an endpoint).
                var attachment_point = current_f_loop.Count;

                while (f_loop_heap.Count != 0)
                {
                    var curr = f_loop_heap.Pop();
                    // test the candidate join from current_f_loop[curr] to hole_min

                    if (!testCandidateAttachment(project, current_f_loop, curr, hole_min))
                    {
                        continue;
                    }

                    attachment_point = curr;
                    break;
                }

                if (attachment_point == current_f_loop.Count)
                {
                    throw new ApplicationException("didn't manage to link up hole!");
                }

                patchHoleIntoPolygon(current_f_loop, attachment_point, h_loop_min_vertex[i]);
            }

            return(current_f_loop);
        }
Exemplo n.º 5
0
        static bool doTriangulate(vertex_info begin, FList <TriIdx> result)
        {
            var vq = new EarQueue();

            var v      = begin;
            int remain = 0;

            do
            {
                if (v.isCandidate())
                {
                    vq.push(v);
                }
                v = v.next;
                remain++;
            } while (v != begin);

            while (remain > 3 && vq.size() != 0)
            {
                var v2 = vq.pop();
                if (!v2.isClipable())
                {
                    v2.failed = true;
                    continue;
                }

continue_clipping:
                var n = v2.next;
                var p = v2.prev;

                result.Add(new TriIdx(v2.prev.idx, v2.idx, v2.next.idx));

                v2.remove();
                if (v2 == begin)
                {
                    begin = v2.next;
                }

                if (--remain == 3)
                {
                    break;
                }

                vq.updateVertex(n);
                vq.updateVertex(p);

                if (n.score < p.score)
                {
                    var t = n;
                    n = p;
                    p = t;
                }

                if (n.score > 0.25 && n.isCandidate() && n.isClipable())
                {
                    vq.remove(n);
                    v2 = n;
                    goto continue_clipping;
                }

                if (p.score > 0.25 && p.isCandidate() && p.isClipable())
                {
                    vq.remove(p);
                    v2 = p;
                    goto continue_clipping;
                }
            }


            if (remain > 3)
            {
                remain -= removeDegeneracies(ref begin, result);

                if (remain > 3)
                {
                    return(splitAndResume(begin, result));
                }
            }

            if (remain == 3)
            {
                result.Add(new TriIdx(begin.idx, begin.next.idx, begin.next.next.idx));
            }

            var d = begin;

            do
            {
                var n = d.next;
                d = n;
            } while (d != begin);

            return(true);
        }