Ejemplo n.º 1
0
            public void Build(ToolpathSet toolpaths, Interval1d zrange, double cellSize = 2.0)
            {
                segments = new DVector <Segment2d>();
                grid     = new SegmentHashGrid2d <int>(cellSize, -1);

                Action <LinearToolpath3 <PrintVertex> > processPathF = (polyPath) => {
                    if (polyPath.Type != ToolpathTypes.Deposition || polyPath.IsPlanar == false)
                    {
                        return;
                    }
                    if ((polyPath.TypeModifiers & FillTypeFlags.OutermostShell) == 0)
                    {
                        return;
                    }
                    Vector3d v0 = polyPath.Start.Position;
                    Vector3d v1 = polyPath.End.Position;
                    if (zrange.Contains(v0.z) == false || zrange.Contains(v1.z) == false)
                    {
                        return;
                    }
                    append_path(polyPath, cellSize);
                };

                process_linear_paths(toolpaths, processPathF);
            }
Ejemplo n.º 2
0
        public TemporalPathHash()
        {
            Segments = new DVector <Segment2d>();
            Times    = new DVector <int>();

            Hash = new SegmentHashGrid2d <int>(HashBucketSize, -1);
        }
        private void validate_path_caches()
        {
            if (path_cache_valid)
            {
                return;
            }

            double    maxLen    = 2.5f;
            double    maxLenSqr = maxLen * maxLen;
            Segment2d invalid   = new Segment2d(Vector2d.MaxValue, Vector2d.MaxValue);

            below_grid   = new SegmentHashGrid2d <Segment2d>(3 * maxLen, invalid);
            current_grid = new SegmentHashGrid2d <Segment2d>(3 * maxLen, invalid);

            Action <LinearToolpath3 <PrintVertex> > pathFuncF = (polyPath) => {
                if (polyPath.Type != ToolpathTypes.Deposition)
                {
                    return;
                }

                Vector3d v0          = polyPath.Start.Position;
                byte     layer_alpha = LayerFilterF(v0);
                if (layer_alpha == 0)
                {
                    return;
                }
                bool is_below = (layer_alpha < 255);
                var  grid     = (is_below) ? below_grid : current_grid;

                int N = polyPath.VertexCount;
                for (int k = 0; k < N - 1; ++k)
                {
                    Vector2d a  = polyPath[k].Position.xy;
                    Vector2d b  = polyPath[k + 1].Position.xy;
                    double   d2 = a.DistanceSquared(b);
                    if (d2 < maxLenSqr)
                    {
                        Segment2d s = new Segment2d(a, b);
                        grid.InsertSegment(s, s.Center, s.Extent);
                        continue;
                    }
                    int      subdivs = (int)(d2 / maxLenSqr);
                    Vector2d prev    = a;
                    for (int i = 1; i <= subdivs; ++i)
                    {
                        double    t    = (double)i / (double)subdivs;
                        Vector2d  next = Vector2d.Lerp(a, b, t);
                        Segment2d s    = new Segment2d(prev, next);
                        grid.InsertSegment(s, s.Center, s.Extent);
                        prev = next;
                    }
                }
            };

            ProcessLinearPaths(Paths, pathFuncF);

            path_cache_valid = true;
        }
Ejemplo n.º 4
0
        private void FilterSelfOverlaps(double overlapRadius, bool bResample = true)
        {
            // [RMS] this tolerance business is not workign properly right now. The problem is
            //  that decimator loses corners!

            // To simplify the computation we are going to resample the curve so that no adjacent
            // are within a given distance. Then we can use distance-to-segments, with the two adjacent
            // segments filtered out, to measure self-distance

            double dist_thresh      = overlapRadius;
            double sharp_thresh_deg = 45;

            //Profiler.Start("InitialResample");

            // resample graph. the degenerate-edge thing is necessary to
            // filter out tiny segments that are functionally sharp corners,
            // but geometrically are made of multiple angles < threshold
            // (maybe there is a better way to do this?)
            DGraph2Resampler r = new DGraph2Resampler(Graph);

            r.CollapseDegenerateEdges(overlapRadius / 10);
            if (bResample)
            {
                r.SplitToMaxEdgeLength(overlapRadius / 2);
                r.CollapseToMinEdgeLength(overlapRadius / 3);
            }
            r.CollapseDegenerateEdges(overlapRadius / 10);

            //Profiler.StopAndAccumulate("InitialResample");
            //Profiler.Start("SharpCorners");

            // find sharp corners
            List <int> sharp_corners = new List <int>();

            foreach (int vid in Graph.VertexIndices())
            {
                if (is_fixed_v(vid))
                {
                    continue;
                }
                double open_angle = Graph.OpeningAngle(vid);
                if (open_angle < sharp_thresh_deg)
                {
                    sharp_corners.Add(vid);
                }
            }

            // disconnect at sharp corners
            foreach (int vid in sharp_corners)
            {
                if (Graph.IsVertex(vid) == false)
                {
                    continue;
                }
                int      e0     = Graph.GetVtxEdges(vid)[0];
                Index2i  ev     = Graph.GetEdgeV(e0);
                int      otherv = (ev.a == vid) ? ev.b : ev.a;
                Vector2d newpos = Graph.GetVertex(vid);  //0.5 * (Graph.GetVertex(vid) + Graph.GetVertex(otherv));
                Graph.RemoveEdge(e0, false);
                int newvid = Graph.AppendVertex(newpos);
                Graph.AppendEdge(newvid, otherv);
            }

            //Profiler.StopAndAccumulate("SharpCorners");
            //Profiler.Start("HashTable");

            // build edge hash table  (cell size is just a ballpark guess here...)
            edge_hash = new SegmentHashGrid2d <int>(3 * overlapRadius, -1);
            foreach (int eid in Graph.EdgeIndices())
            {
                Segment2d seg = Graph.GetEdgeSegment(eid);
                edge_hash.InsertSegment(eid, seg.Center, seg.Extent);
            }

            if (CollisionGraph.EdgeCount > 0)
            {
                collision_edge_hash = new SegmentHashGrid2d <int>(3 * CollisionRadius, -1);
                foreach (int eid in CollisionGraph.EdgeIndices())
                {
                    Segment2d seg = CollisionGraph.GetEdgeSegment(eid);
                    collision_edge_hash.InsertSegment(eid, seg.Center, seg.Extent);
                }
            }

            //Profiler.StopAndAccumulate("HashTable");
            //Profiler.Start("Erode1");

            // Step 1: erode from boundary vertices
            List <int> boundaries = new List <int>();

            foreach (int vid in Graph.VertexIndices())
            {
                if (Graph.GetVtxEdgeCount(vid) == 1)
                {
                    boundaries.Add(vid);
                }
            }
            foreach (int vid in boundaries)
            {
                if (Graph.IsVertex(vid) == false)
                {
                    continue;
                }
                double dist           = MinSelfSegDistance(vid, 2 * dist_thresh);
                double collision_dist = MinCollisionConstraintDistance(vid, CollisionRadius);
                if (dist < dist_thresh || collision_dist < CollisionRadius)
                {
                    int eid = Graph.GetVtxEdges(vid)[0];
                    decimate_forward(vid, eid, dist_thresh);
                }
            }

            //Profiler.StopAndAccumulate("Erode1");
            //Profiler.Start("OpenAngleSort");

            //
            // Step 2: find any other possible self-overlaps and erode them.
            //

            // sort all vertices by opening angle. For any overlap, we can erode
            // on either side. Prefer to erode on side with higher curvature.
            List <Vector2d> remaining_v = new List <Vector2d>(Graph.MaxVertexID);

            foreach (int vid in Graph.VertexIndices())
            {
                if (is_fixed_v(vid))
                {
                    continue;
                }
                double open_angle = Graph.OpeningAngle(vid);
                if (open_angle == double.MaxValue)
                {
                    continue;
                }
                remaining_v.Add(new Vector2d(vid, open_angle));
            }
            remaining_v.Sort((a, b) => { return((a.y < b.y) ? -1 : (a.y > b.y ? 1 : 0)); });

            //Profiler.StopAndAccumulate("OpenAngleSort");
            //Profiler.Start("Erode2");

            // look for overlap vertices. When we find one, erode on both sides.
            foreach (Vector2d vinfo in remaining_v)
            {
                int vid = (int)vinfo.x;
                if (Graph.IsVertex(vid) == false)
                {
                    continue;
                }
                double dist = MinSelfSegDistance(vid, 2 * dist_thresh);
                if (dist < dist_thresh)
                {
                    List <int> nbrs = new List <int>(Graph.GetVtxEdges(vid));
                    foreach (int eid in nbrs)
                    {
                        if (Graph.IsEdge(eid))    // may have been decimated!
                        {
                            decimate_forward(vid, eid, dist_thresh);
                        }
                    }
                }
            }

            //Profiler.StopAndAccumulate("Erode2");
            //Profiler.Start("FlatCollapse");

            // get rid of extra vertices
            r.CollapseFlatVertices(FinalFlatCollapseAngleThreshDeg);

            //Profiler.StopAndAccumulate("FlatCollapse");
        }