public static List <GeneralPolygon2d> ComputeOffsetPolygon(GeneralPolygon2d poly, double fOffset, bool bMiter = false) { double nIntScale = GetIntScale(poly); CPolygonList clipper_polys = new CPolygonList(); clipper_polys.Add(ClipperUtil.ConvertToClipper(poly.Outer, nIntScale)); foreach (Polygon2d hole in poly.Holes) { clipper_polys.Add(ClipperUtil.ConvertToClipper(hole, nIntScale)); } CPolygonList dilate_solution = new CPolygonList(); try { ClipperOffset co = new ClipperOffset(); if (bMiter) { co.AddPaths(clipper_polys, JoinType.jtMiter, EndType.etClosedPolygon); } else { co.AddPaths(clipper_polys, JoinType.jtRound, EndType.etClosedPolygon); } co.Execute(ref dilate_solution, fOffset * nIntScale); } catch /*( Exception e )*/ { //System.Diagnostics.Debug.WriteLine("ClipperUtil.ComputeOffsetPolygon: Clipper threw exception: " + e.Message); return(new List <GeneralPolygon2d>()); } List <GeneralPolygon2d> polys = ClipperUtil.ConvertFromClipper(dilate_solution, nIntScale); return(polys); }
private static IPolygon Clopening(IPolygon p, Box2 boundingBox, double offset) { if (p.IsEmpty) { return(Polygon.Empty); } double scale = Math.Max(boundingBox.Width, boundingBox.Height); var fixedPointRange = new Box2(boundingBox.MinCorner, new Vector2(scale, scale)); var fixedP = ConvertToFixedPoint(p, fixedPointRange); var fixedScaleOffset = offset * _fixedPointRange / scale; try { var offsetter = new ClipperOffset(); offsetter.AddPaths(fixedP, JoinType.jtMiter, EndType.etClosedPolygon); var fixedIntermediate = new ClipperPolygon(); offsetter.Execute(ref fixedIntermediate, fixedScaleOffset); offsetter.Clear(); offsetter.AddPaths(fixedIntermediate, JoinType.jtMiter, EndType.etClosedPolygon); var fixedAnswer = new ClipperPolygon(); offsetter.Execute(ref fixedAnswer, -fixedScaleOffset); return(ConvertToFloatingPoint(fixedAnswer, fixedPointRange)); } catch (Exception e) { Console.WriteLine("EXCEPTION: {0}", e); return(p); } }
private void InsetPath() { var path = this.Children.OfType <IPathObject>().FirstOrDefault(); if (path == null) { // clear our existing data VertexSource = new VertexStorage(); return; } var aPolys = path.VertexSource.CreatePolygons(); var offseter = new ClipperOffset(); offseter.AddPaths(aPolys, InflatePathObject3D.GetJoinType(OuterStyle), EndType.etClosedPolygon); var outerLoops = new List <List <IntPoint> >(); offseter.Execute(ref outerLoops, OutlineWidth * Ratio * 1000); Clipper.CleanPolygons(outerLoops); offseter.AddPaths(aPolys, InflatePathObject3D.GetJoinType(InnerStyle), EndType.etClosedPolygon); var innerLoops = new List <List <IntPoint> >(); offseter.Execute(ref innerLoops, -OutlineWidth * (1 - Ratio) * 1000); Clipper.CleanPolygons(innerLoops); var allLoops = outerLoops; allLoops.AddRange(innerLoops); VertexSource = allLoops.CreateVertexStorage(); (VertexSource as VertexStorage).Add(0, 0, ShapePath.FlagsAndCommand.Stop); }
private IEnumerable <HPGLLine> OffsetLine(double offset, HPGLLine line) { var newlines = new List <HPGLLine> { line }; var co = new ClipperOffset(); var solution = new List <List <IntPoint> >(); var solution2 = new List <List <IntPoint> >(); solution.Add(line.Commands.Select(x => new IntPoint(_scale * x.PointFrom.X0, _scale * x.PointFrom.Y0)).ToList()); co.AddPaths(solution, JoinType.jtRound, EndType.etClosedPolygon); co.Execute(ref solution2, offset); var existingline = line; foreach (var polygon in solution2) { var newcmds = new List <HPGLCommand>(); HPGLCommand last = null; foreach (var pt in polygon) { var from = new Point3D { X = pt.X / _scale, Y = pt.Y / _scale }; var hpgl = new HPGLCommand { PointFrom = from, CommandType = HPGLCommand.HPGLCommandType.PenDown }; newcmds.Add(hpgl); if (last != null) { last.PointTo = from; } last = hpgl; } last.PointTo = newcmds.First().PointFrom; if (existingline == null) { // add new line existingline = new HPGLLine { PreCommands = new List <HPGLCommand> { new HPGLCommand { CommandType = HPGLCommand.HPGLCommandType.PenUp } }, PostCommands = new List <HPGLCommand>(), ParentLine = line.ParentLine }; newlines.Add(existingline); } existingline.Commands = newcmds; existingline.PreCommands.Last(l => l.IsPenCommand).PointTo = newcmds.First().PointFrom; existingline = null; } return(newlines); }
private static List <Polygon> OffsetViaClipper(IEnumerable <Polygon> polygons, double offset, bool notMiter, double tolerance, double deltaAngle) { var allPolygons = polygons.SelectMany(polygon => polygon.AllPolygons).ToList(); if (double.IsNaN(tolerance) || tolerance.IsNegligible()) { var totalLength = allPolygons.Sum(loop => loop.Perimeter); tolerance = totalLength * 0.001; } var joinType = notMiter ? (double.IsNaN(deltaAngle) ? JoinType.jtSquare : JoinType.jtRound) : JoinType.jtMiter; //Convert Points (TVGL) to IntPoints (Clipper) var clipperSubject = allPolygons.Select(loop => loop.Vertices.Select(point => new IntPoint(point.X * scale, point.Y * scale)).ToList()).ToList(); //Setup Clipper var clip = new ClipperOffset(2, tolerance * scale); clip.AddPaths(clipperSubject, joinType, EndType.etClosedPolygon); //Begin an evaluation var clipperSolution = new List <List <IntPoint> >(); clip.Execute(clipperSolution, offset * scale); //Convert back to points and return solution var solution = clipperSolution.Select(clipperPath => new Polygon(clipperPath.Select(point => new Vector2(point.X / scale, point.Y / scale)))); return(solution.CreateShallowPolygonTrees(true)); }
public static IPolygon Offset(this IPolygon p, double offset) { if (p.IsEmpty) { return(Polygon.Empty); } var boundingBox = p.BoundingBox().Offset(Math.Max(offset, 0)); double scale = Math.Max(boundingBox.Width, boundingBox.Height); var fixedPointRange = new Box2(boundingBox.MinCorner, new Vector2(scale, scale)); var fixedP = ConvertToFixedPoint(p, fixedPointRange); try { var offsetter = new ClipperOffset(); offsetter.AddPaths(fixedP, JoinType.jtMiter, EndType.etClosedPolygon); var fixedAnswer = new ClipperPolygon(); offsetter.Execute(ref fixedAnswer, offset * _fixedPointRange / scale); return(ConvertToFloatingPoint(fixedAnswer, fixedPointRange)); } catch (Exception e) { Console.WriteLine("EXCEPTION: {0}", e); return(p); } }
/// <summary> /// Creates a set of polygonal regions offset from a given set of Clipper polygons /// </summary> /// <param name="paths">Hierarchy of Clipper polygons to be offset</param> /// <param name="innerOffset">Padding distance from the edge of the given paths region</param> /// <param name="outerOffset">The outer boundary distance of the offset region</param> /// <returns></returns> public static List <List <IntPoint> > GenerateOffsetRegionFromRoadPaths( List <List <IntPoint> > paths, float innerOffset, float outerOffset) { var solution = new List <List <IntPoint> >(); if (outerOffset < 0f || outerOffset < innerOffset) { return(solution); } var clipperOffset = new ClipperOffset(); clipperOffset.AddPaths(paths, JoinType.jtMiter, EndType.etClosedPolygon); var innerOffsetRegions = new List <List <IntPoint> >(); clipperOffset.Execute(ref innerOffsetRegions, innerOffset * UpScaleFactor); var outerOffsetRegions = new List <List <IntPoint> >(); clipperOffset.Execute(ref outerOffsetRegions, outerOffset * UpScaleFactor); var clipper = new Clipper(); clipper.AddPaths(outerOffsetRegions, PolyType.ptSubject, true); clipper.AddPaths(innerOffsetRegions, PolyType.ptClip, true); clipper.Execute(ClipType.ctXor, solution, PolyFillType.pftEvenOdd, PolyFillType.pftEvenOdd); return(solution); }
/// <summary> Creates skeleton lines connected to outer walls and transit polygons. </summary> private static List <Wall> CreateConnectedAndTransit(InDoorGeneratorSettings settings, List <SkeletonEdge> walls, out List <List <IntPoint> > transitPolygons) { var offset = new ClipperOffset(); var connected = new List <Wall>(); var paths = new List <List <IntPoint> >(); var skeleton = settings.Skeleton; foreach (var wall in walls) { var start = new IntPoint(wall.Start.X * Scale, wall.Start.Y * Scale); var end = new IntPoint(wall.End.X * Scale, wall.End.Y * Scale); if (!wall.IsOuter) { paths.Add(new List <IntPoint> { start, end }); } else if (wall.IsSkeleton && (skeleton.Distances[wall.Start] < settings.HalfTransitAreaWidth || skeleton.Distances[wall.End] < settings.HalfTransitAreaWidth)) { var undesired = new Wall(wall.Start, wall.End, false); // TODO do I need this check here? //if (!connected.Contains(undesired)) connected.Add(undesired); } } transitPolygons = new List <List <IntPoint> >(1); offset.AddPaths(paths, JoinType.jtMiter, EndType.etClosedLine); offset.Execute(ref transitPolygons, settings.HalfTransitAreaWidth * Scale); return(connected); }
public static IEnumerable <IPolyLine2D> BuildOffset(IEnumerable <IPolygon2D> pgons, double delta, JoinType joinType, EndType endType, double mitterLimit, double maxDiscretizationAngle = 5) { bool containsCircArc = false; var polygons = new List <List <IntPoint> >(pgons.Count()); foreach (var polygon in pgons) { var p = CreatePolygon(polygon); polygons.Add(p); } if (joinType == JoinType.jtRound || endType == EndType.etOpenRound) { containsCircArc = true; } var co = new ClipperOffset(mitterLimit); co.AddPaths(polygons, joinType, endType); var solution = new List <List <IntPoint> >(); co.Execute(ref solution, delta * ClipperScale); foreach (var poly in solution) { yield return(CreatePolyline(poly, containsCircArc, maxDiscretizationAngle + 1)); } }
public static IEnumerable <IPolyLine2D> BuildOffset(IEnumerable <IPolyLine2D> polylines, double delta, JoinType joinType, EndType endType, double mitterLimit, bool sort = false, double maxDiscretizationAngle = 5) { PolyLine2DDiscretizator discretizator = new PolyLine2DDiscretizator { NumberOfTiles = 1, LengthOfTile = double.MaxValue, Angle = maxDiscretizationAngle }; bool containsCircArc = joinType == JoinType.jtRound || endType == EndType.etOpenRound; var polygons = new List <List <IntPoint> >(polylines.Count()); foreach (var polyline in polylines) { if (!containsCircArc) { containsCircArc |= polyline.Segments.FirstOrDefault(s => s is CircularArcSegment2D) != null; } var polygon = CreatePolygon(polyline, discretizator, true); polygons.Add(polygon); } var co = new ClipperOffset(mitterLimit); co.AddPaths(polygons, joinType, endType); var solution = new List <List <IntPoint> >(); co.Execute(ref solution, delta * ClipperScale); foreach (var polygon in solution) { yield return(CreatePolyline(polygon, containsCircArc, maxDiscretizationAngle + 1)); } }
public static List <Polygon> OffsetLinesViaClipper(this IEnumerable <IEnumerable <Vector2> > polylines, double offset, double tolerance) { if (double.IsNaN(tolerance) || tolerance.IsNegligible()) { tolerance = Constants.BaseTolerance; } //Convert Points (TVGL) to IntPoints (Clipper) var clipperSubject = polylines.Select(line => line.Select(point => new IntPoint(point.X * scale, point.Y * scale)).ToList()).ToList(); //Setup Clipper var clip = new ClipperOffset(2, tolerance * scale); clip.AddPaths(clipperSubject, JoinType.jtSquare, EndType.etOpenSquare); //Begin an evaluation var clipperSolution = new List <List <IntPoint> >(); clip.Execute(clipperSolution, offset * scale); //Convert back to points and return solution var solution = clipperSolution.Select(clipperPath => new Polygon(clipperPath.Select(point => new Vector2(point.X / scale, point.Y / scale)))); return(solution.CreateShallowPolygonTrees(true)); }
public void GenerateCollision() { //List<List<Vector2>> polygons = new List<List<Vector2>>(); List <List <Vector2> > unitedPolygons = new List <List <Vector2> >(); Clipper clipper = new Clipper(); int scalingFactor = 10000; for (int x = 0; x < layer.chunkSize_x; x++) { for (int y = 0; y < layer.chunkSize_y; y++) { if (grid[x, y] != null && grid[x, y].colider) { Path newPoligons = new Path(4); newPoligons.Add(new IntPoint(Mathf.Floor((0 + x) * scalingFactor), Mathf.Floor((0 + y) * scalingFactor))); newPoligons.Add(new IntPoint(Mathf.Floor((0 + x) * scalingFactor), Mathf.Floor((1 + y) * scalingFactor))); newPoligons.Add(new IntPoint(Mathf.Floor((1 + x) * scalingFactor), Mathf.Floor((1 + y) * scalingFactor))); newPoligons.Add(new IntPoint(Mathf.Floor((1 + x) * scalingFactor), Mathf.Floor((0 + y) * scalingFactor))); clipper.AddPath(newPoligons, PolyType.ptSubject, true); } } } Paths solution = new Paths(); clipper.Execute(ClipType.ctUnion, solution); ClipperOffset offset = new ClipperOffset(); offset.AddPaths(solution, JoinType.jtMiter, EndType.etClosedPolygon); offset.Execute(ref solution, 5f); foreach (Path path in solution) { List <Vector2> unitedPolygon = new List <Vector2>(); foreach (IntPoint point in path) { unitedPolygon.Add(new Vector2(point.X / (float)scalingFactor, point.Y / (float)scalingFactor)); } unitedPolygons.Add(unitedPolygon); } unitedPolygons = TileMap.RemoveClosePointsInPolygons(unitedPolygons); PolygonCollider2D collider = layer.map.gameObject.GetComponent <PolygonCollider2D>(); collider.pathCount = unitedPolygons.Count; for (int i = 0; i < unitedPolygons.Count; i++) { Vector2[] points = unitedPolygons[i].ToArray(); collider.SetPath(i, points); } }
//this function takes a list of polygons as a parameter, this list of polygons represent all the polygons that constitute collision in your level. public List <List <Vector2> > UniteCollisionPolygons(List <List <Vector2> > polygons) { //this is going to be the result of the method List <List <Vector2> > unitedPolygons = new List <List <Vector2> >(); Clipper clipper = new Clipper(); //clipper only works with ints, so if we're working with floats, we need to multiply all our floats by //a scaling factor, and when we're done, divide by the same scaling factor again int scalingFactor = 10000; //this loop will convert our List<List<Vector2>> to what Clipper works with, which is "Path" and "IntPoint" //and then add all the Paths to the clipper object so we can process them for (int i = 0; i < polygons.Count; i++) { Path allPolygonsPath = new Path(polygons[i].Count); for (int j = 0; j < polygons[i].Count; j++) { allPolygonsPath.Add(new IntPoint(Mathf.Floor(polygons[i][j].x * scalingFactor), Mathf.Floor(polygons[i][j].y * scalingFactor))); //print(new IntPoint(Mathf.Floor(polygons[i][j].x * scalingFactor), Mathf.Floor(polygons[i][j].y * scalingFactor)).X + ", " + new IntPoint(Mathf.Floor(polygons[i][j].x * scalingFactor), Mathf.Floor(polygons[i][j].y * scalingFactor)).Y); } clipper.AddPath(allPolygonsPath, PolyType.ptSubject, true); } //this will be the result Paths solution = new Paths(); //having added all the Paths added to the clipper object, we tell clipper to execute an union clipper.Execute(ClipType.ctUnion, solution); //the union may not end perfectly, so we're gonna do an offset in our polygons, that is, expand them outside a little bit ClipperOffset offset = new ClipperOffset(); offset.AddPaths(solution, JoinType.jtMiter, EndType.etClosedPolygon); //5 is the amount of offset offset.Execute(ref solution, 0.1f); //now we just need to convert it into a List<List<Vector2>> while removing the scaling foreach (Path path in solution) { List <Vector2> unitedPolygon = new List <Vector2>(); foreach (IntPoint point in path) { unitedPolygon.Add(new Vector2(point.X / (float)scalingFactor, point.Y / (float)scalingFactor)); } unitedPolygons.Add(unitedPolygon); } //this removes some redundant vertices in the polygons when they are too close from each other //may be useful to clean things up a little if your initial collisions don't match perfectly from tile to tile unitedPolygons = RemoveClosePointsInPolygons(unitedPolygons); //everything done return(unitedPolygons); }
public static void generateOutlineSegments() { Logger.logProgress("Generating outline segments"); //Check if there should be at least one shell if (Global.Values.shellThickness < 1) { return; } //We need to got through every layercomponent //for (ushort i = 0; i < Global.Values.layerCount; i++) foreach (LayerComponent layer in Global.Values.layerComponentList) { Logger.logProgress("Outline: " + layer.layerNumber); //And every island inside the list foreach (Island island in layer.islandList) { if (island.outlinePolygons.Count < 1) { continue; } //The first outline will be one that is half an extrusion thinner than the sliced outline, ths is sothat the dimensions //do not change once extruded Polygons outline = new Polygons(); ClipperOffset offset = new ClipperOffset(); offset.AddPaths(island.outlinePolygons, JoinType.jtMiter, EndType.etClosedPolygon); offset.Execute(ref outline, -Global.Values.nozzleWidth / 2); for (ushort j = 0; j < Global.Values.shellThickness; j++) { //Place the newly created outline in its own segment LayerSegment outlineSegment = new LayerSegment(SegmentType.OutlineSegment); outlineSegment.segmentType = SegmentType.OutlineSegment; outlineSegment.segmentSpeed = layer.layerSpeed; outlineSegment.outlinePolygons = new Polygons(outline); island.segmentList.Add(outlineSegment); var distance = (-Global.Values.nozzleWidth / 2) - Global.Values.nozzleWidth * (j + 1); //We now shrink the outline with one extrusion width for the next shell if any offset = new ClipperOffset(); offset.AddPaths(island.outlinePolygons, JoinType.jtMiter, EndType.etClosedPolygon); offset.Execute(ref outline, distance); } //We now need to store the smallest outline as the new layer outline for infill trimming purposes //the current outline though is just half an extrusion width to small offset = new ClipperOffset(); offset.AddPaths(outline, JoinType.jtMiter, EndType.etClosedPolygon); offset.Execute(ref island.outlinePolygons, Global.Values.nozzleWidth); } } }
/// <summary> /// Gets a parallel <see cref="Loxy"/> with offset /// </summary> /// <param name="JT">Jointype</param> /// <param name="ET">Endtype</param> /// <param name="Offset">Offset</param> /// <returns></returns> public Loxy GetOffset(JoinType JT, EndType ET, double Offset) { ClipperOffset co = new ClipperOffset(); List <List <IntPoint> > Solution = new List <List <IntPoint> >(); co.AddPaths(ToClipperPoly(this), (ClipperLib.JoinType)JT, (ClipperLib.EndType)ET); co.Execute(ref Solution, (long)(ToInt * Offset)); return(FromClipperLoxy(Solution)); }
static public ClipperPolygons Buffer(this ClipperPolygons polygons, double distance) { ClipperPolygons result = new ClipperPolygons(); ClipperOffset co = new ClipperOffset(); co.AddPaths(polygons, JoinType.jtRound, EndType.etClosedPolygon); co.Execute(ref result, distance * Acc); return result; }
public static Polygons Offset(this Polygons polygons, int distance) { ClipperOffset offseter = new ClipperOffset(); offseter.AddPaths(polygons, JoinType.jtMiter, EndType.etClosedPolygon); Paths solution = new Polygons(); offseter.Execute(ref solution, distance); return(solution); }
/// <summary> /// Simplifies an list INTPolygons using expand and shrink technique. /// </summary> /// <param name="polygons">The INTPolygons.</param> /// <param name="value">The value used for expand and shrink.</param> /// <returns>INTPolygons.</returns> public INTPolygons SimplifyINTPolygons(INTPolygons polygons, double value) { double simplificationFactor = Math.Pow(10.0, this.PolygonalBooleanPrecision) * UnitConversion.Convert(value, Length_Unit_Types.FEET, UnitType); ClipperOffset clipperOffset = new ClipperOffset(); clipperOffset.AddPaths(polygons, ClipperLib.JoinType.jtMiter, EndType.etClosedPolygon); INTPolygons shrink = new INTPolygons(); clipperOffset.Execute(ref shrink, -simplificationFactor); //expanding to return the polygons to their original position clipperOffset.Clear(); clipperOffset.AddPaths(shrink, ClipperLib.JoinType.jtMiter, EndType.etClosedPolygon); INTPolygons expand = new INTPolygons(); clipperOffset.Execute(ref expand, simplificationFactor); shrink = null; clipperOffset = null; return(expand); }
public static void calculateSupportSegments() { Logger.logProgress("Calculating support segments"); if (!Global.Values.shouldSupportMaterial || Global.Values.supportMaterialDesnity <= 0) { return; } //To calculate the support segments we will keep a union of the above layers and perform a difference with them and the current layer Polygons topUnion = new Polygons(); for (int i = Global.Values.layerCount - 1; i > -1; i--) { Polygons layerPolygons = new Polygons(); foreach (Island island in Global.Values.layerComponentList[i].islandList) { Polygons offsetResult = new Polygons(); ClipperOffset offset = new ClipperOffset(); offset.AddPaths(island.outlinePolygons, JoinType.jtMiter, EndType.etClosedPolygon); offset.Execute(ref offsetResult, Global.Values.shellThickness * Global.Values.nozzleWidth); Clipper clipper = new Clipper(); clipper.AddPaths(offsetResult, PolyType.ptClip, true); clipper.AddPaths(layerPolygons, PolyType.ptSubject, true); clipper.Execute(ClipType.ctUnion, layerPolygons); } Polygons supportOutline = new Polygons(); Clipper supportClipper = new Clipper(); supportClipper.AddPaths(topUnion, PolyType.ptSubject, true); supportClipper.AddPaths(layerPolygons, PolyType.ptClip, true); supportClipper.Execute(ClipType.ctDifference, supportOutline); //We should just offset the support slightly so that it does not touch the rest of the model ClipperOffset clipperOffset = new ClipperOffset(); clipperOffset.AddPaths(supportOutline, JoinType.jtMiter, EndType.etClosedPolygon); clipperOffset.Execute(ref supportOutline, -Global.Values.nozzleWidth); Island supportIsland = new Island(); LayerSegment segment = new LayerSegment(SegmentType.SupportSegment); segment.segmentSpeed = Global.Values.layerComponentList[i].supportSpeed; segment.outlinePolygons = supportOutline; supportIsland.outlinePolygons = supportOutline; supportIsland.segmentList.Add(segment); Global.Values.layerComponentList[i].islandList.Add(supportIsland); Clipper unionClipper = new Clipper(); unionClipper.AddPaths(topUnion, PolyType.ptClip, true); unionClipper.AddPaths(layerPolygons, PolyType.ptSubject, true); unionClipper.Execute(ClipType.ctUnion, topUnion); } }
/* * * Geometry features (Offset) * */ public Polygon OffsetPolygon(float offset) { // Calculate Polygon-Clipper scale. float maximum = Mathf.Max(bounds.width, bounds.height) + offset * 2.0f + offset; float scale = (float)Int32.MaxValue / maximum; // Convert to Clipper. Paths paths = new Paths(); { Path path = new Path(); EnumeratePoints((Vector2 eachPoint) => { path.Add(new IntPoint(eachPoint.x * scale, eachPoint.y * scale)); }); paths.Add(path); } foreach (Polygon eachPolygon in polygons) { Path path = new Path(); eachPolygon.EnumeratePoints((Vector2 eachPoint) => { path.Add(new IntPoint(eachPoint.x * scale, eachPoint.y * scale)); }); paths.Add(path); } // Clipper offset. Paths solutionPaths = new Paths(); ClipperOffset clipperOffset = new ClipperOffset(); clipperOffset.AddPaths(paths, JoinType.jtMiter, EndType.etClosedPolygon); clipperOffset.Execute(ref solutionPaths, (double)offset * scale); // Convert from Cipper. Polygon offsetPolygon = null; for (int index = 0; index < solutionPaths.Count; index++) { Path eachSolutionPath = solutionPaths[index]; Polygon eachSolutionPolygon = PolygonFromClipperPath(eachSolutionPath, scale); if (index == 0) { offsetPolygon = Polygon.PolygonWithPoints(eachSolutionPolygon.points); // Copy } else { offsetPolygon.AddPolygon(eachSolutionPolygon); } } // Back to Polygon. return(offsetPolygon); }
public override List <List <IntPoint> > GetCopyOfClipperPolygon(double relativeWidth, List <List <IntPoint> > outerPolygon) { var delta = (-2 * relativeWidth) * ClosedSymbolBase.ClipperScalingInt; var clipper = new ClipperOffset(); clipper.AddPaths(outerPolygon, JoinType.jtMiter, EndType.etClosedPolygon); var result = new List <List <IntPoint> >(); clipper.Execute(ref result, delta); return(result); }
List <List <IntPoint> > Offset(List <List <IntPoint> > polygons, double delta) { var result = new List <List <IntPoint> >(); var clipper = new ClipperOffset(); clipper.AddPaths(polygons, JoinType.jtSquare, EndType.etClosedPolygon); clipper.Execute(ref result, delta); return(result); }
private static Vector2[][] GenerateOpaqueOutline(Sprite sprite, float detail, byte alphaTolerance) { Vector2[][] paths = GenerateTransparentOutline(sprite, 1, alphaTolerance, true); List <List <IntPoint> > intPointList = ConvertToIntPointList(paths, detail); List <List <IntPoint> > offsetIntPointList = new List <List <IntPoint> >(); ClipperOffset offset = new ClipperOffset(); offset.AddPaths(intPointList, JoinType.jtMiter, EndType.etClosedPolygon); offset.Execute(ref offsetIntPointList, -32);//TODO: magic number! return(ConvertToVector2Array(offsetIntPointList)); }
public Paths offset(int distance) { Paths solution2 = new Paths(); ClipperOffset co = new ClipperOffset(); co.AddPaths(polygons, JoinType.jtRound, EndType.etClosedPolygon); //偏置时,偏置量需要先扩大Scale倍 //co.MiterLimit = 2.0; co.Execute(ref solution2, distance); return(solution2); }
public static List <List <Vector2> > Outline(List <Vector2> polygon, FillMode fillMode, bool closed, StrokeStyle strokeStyle, float strokeWidth, out PolyTree tree) { List <List <Vector2> > simplified = Clipper.SimplifyPolygon(polygon, fillMode.ToPolyFillType()); Offsetter.Clear(); Offsetter.MiterLimit = strokeStyle.MiterLimit; Offsetter.AddPaths(simplified, (JoinType)strokeStyle.LineJoin, closed ? EndType.etClosedLine : strokeStyle.CapStyle.ToEndType()); tree = new PolyTree(); Offsetter.Execute(ref tree, strokeWidth / 2); return(Clipper.ClosedPathsFromPolyTree(tree)); }
public Polygons Offset(Polygons polygons, double distance) { var offseter = new ClipperOffset(); offseter.AddPaths(polygons, JoinType.jtRound, EndType.etClosedPolygon); var solution = new Polygons(); offseter.Execute(ref solution, distance); return(solution); }
private void AddCharacterMeshes(string currentText, TypeFacePrinter printer) { int newIndex = asyncMeshGroups.Count; StyledTypeFace typeFace = printer.TypeFaceStyle; for (int i = 0; i < currentText.Length; i++) { string letter = currentText[i].ToString(); TypeFacePrinter letterPrinter = new TypeFacePrinter(letter, typeFace); if (CharacterHasMesh(letterPrinter, letter)) { #if true Mesh textMesh = VertexSourceToMesh.Extrude(letterPrinter, unscaledLetterHeight / 2); #else Mesh textMesh = VertexSourceToMesh.Extrude(letterPrinter, unscaledLetterHeight / 2); // this is the code to make rounded tops // convert the letterPrinter to clipper polygons List <List <IntPoint> > insetPoly = VertexSourceToPolygon.CreatePolygons(letterPrinter); // inset them ClipperOffset clipper = new ClipperOffset(); clipper.AddPaths(insetPoly, JoinType.jtMiter, EndType.etClosedPolygon); List <List <IntPoint> > solution = new List <List <IntPoint> >(); clipper.Execute(solution, 5.0); // convert them back into a vertex source // merge both the inset and original vertex sources together // convert the new vertex source into a mesh (trianglulate them) // offset the inner loop in z // create the polygons from the inner loop to a center point so that there is the rest of an approximation of the bubble // make the mesh for the bottom // add the top and bottom together // done #endif asyncMeshGroups.Add(new MeshGroup(textMesh)); PlatingMeshGroupData newMeshInfo = new PlatingMeshGroupData(); newMeshInfo.spacing = printer.GetOffsetLeftOfCharacterIndex(i); asyncPlatingDatas.Add(newMeshInfo); asyncMeshGroupTransforms.Add(ScaleRotateTranslate.Identity()); PlatingHelper.CreateITraceableForMeshGroup(asyncPlatingDatas, asyncMeshGroups, newIndex, null); ScaleRotateTranslate moved = asyncMeshGroupTransforms[newIndex]; moved.translation *= Matrix4X4.CreateTranslation(new Vector3(0, 0, unscaledLetterHeight / 2)); asyncMeshGroupTransforms[newIndex] = moved; newIndex++; } processingProgressControl.PercentComplete = ((i + 1) * 95 / currentText.Length); } }
/// <summary> /// Compute offset polygon from all input polys (ie separate islands may merge) /// </summary> public static List <GeneralPolygon2d> ComputeOffsetPolygon(IEnumerable <GeneralPolygon2d> polys, double fOffset, bool bMiter = false, double minArea = -1) { double nIntScale = GetIntScale(polys); if (minArea < 0) { minArea = DiscardMinArea; } ClipperOffset co = new ClipperOffset(); PolyTree tree = new PolyTree(); try { foreach (GeneralPolygon2d poly in polys) { CPolygonList clipper_poly = ConvertToClipper(poly, nIntScale); if (bMiter) { co.AddPaths(clipper_poly, JoinType.jtMiter, EndType.etClosedPolygon); } else { co.AddPaths(clipper_poly, JoinType.jtRound, EndType.etClosedPolygon); } } co.Execute(ref tree, fOffset * nIntScale); List <GeneralPolygon2d> result = new List <GeneralPolygon2d>(); for (int ci = 0; ci < tree.ChildCount; ++ci) { Convert(tree.Childs[ci], result, nIntScale, minArea); } return(result); } catch /*(Exception e)*/ { //System.Diagnostics.Debug.WriteLine("ClipperUtil.ComputeOffsetPolygon: Clipper threw exception: " + e.Message); return(new List <GeneralPolygon2d>()); } }
public static PolygonList OffsetPolygon(PolygonList poly, float absoluteDistance, JoinType joinType = JoinType.jtMiter, EndType endType = EndType.etClosedPolygon) { ClipperOffset offset = new ClipperOffset(); offset.AddPaths(poly, joinType, endType); List <List <IntPoint> > solution = new List <List <IntPoint> >(); offset.Execute(ref solution, absoluteDistance * Vector.Scale); return(solution); }
private Polygons GetOffsetPolugon(Polygons source, double offset) { if (offset != 0) { Polygons solution2 = new Polygons(); ClipperOffset co = new ClipperOffset(); co.AddPaths(source, JoinType.jtRound, EndType.etClosedPolygon); co.Execute(ref solution2, offset); return(solution2); } return(new Polygons(source)); }
public static Polygons Offset(this Polygons polygons, int distance) { ClipperOffset offseter = new ClipperOffset(); offseter.AddPaths(polygons, JoinType.jtMiter, EndType.etClosedPolygon); Paths solution = new Polygons(); offseter.Execute(ref solution, distance); return solution; }