-
-
Notifications
You must be signed in to change notification settings - Fork 233
/
Copy pathPathOffsetter.cs
159 lines (147 loc) · 7.55 KB
/
PathOffsetter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
using System.Collections.Generic;
using ProceduralToolkit.ClipperLib;
using UnityEngine;
namespace ProceduralToolkit
{
/// <summary>
/// ClipperOffset wrapper
/// </summary>
public class PathOffsetter
{
/// <summary>
/// Maximum distance in multiples of delta that vertices can be offset from their original positions before squaring is applied.
/// Only relevant when JoinType = jtMiter.
/// </summary>
public double arcTolerance
{
get => clipperOffset.ArcTolerance;
set => clipperOffset.ArcTolerance = value;
}
/// <summary>
/// Maximum acceptable imprecision when arcs are approximated in an offsetting operation.
/// Only relevant when JoinType = jtRound and/or EndType = etRound.
/// </summary>
public double miterLimit
{
get => clipperOffset.MiterLimit;
set => clipperOffset.MiterLimit = value;
}
private readonly ClipperOffset clipperOffset;
/// <summary>
/// Constructs a new PathOffsetter
/// </summary>
/// <param name="miterLimit">
/// Maximum distance in multiples of delta that vertices can be offset from their original positions before squaring is applied.
/// Only relevant when JoinType = jtMiter.
/// </param>
/// <param name="arcTolerance">
/// Maximum acceptable imprecision when arcs are approximated in an offsetting operation.
/// Only relevant when JoinType = jtRound and/or EndType = etRound.
/// </param>
public PathOffsetter(double miterLimit = 2.0, double arcTolerance = 0.25)
{
clipperOffset = new ClipperOffset(miterLimit, arcTolerance);
}
/// <summary>
/// Adds a path to a ClipperOffset object in preparation for offsetting.
/// </summary>
/// <param name="path"> Vertices of the path. </param>
/// <param name="joinType"> See http://www.angusj.com/delphi/clipper/documentation/Docs/Units/ClipperLib/Types/JoinType.htm </param>
/// <param name="endType"> See http://www.angusj.com/delphi/clipper/documentation/Docs/Units/ClipperLib/Types/EndType.htm </param>
public void AddPath(IReadOnlyList<Vector2> path, JoinType joinType = JoinType.jtMiter, EndType endType = EndType.etClosedPolygon)
{
clipperOffset.AddPath(ClipperUtility.ToIntPath(path), joinType, endType);
}
/// <summary>
/// Adds a path to a ClipperOffset object in preparation for offsetting.
/// </summary>
/// <param name="path"> Vertices of the path. </param>
/// <param name="joinType"> See http://www.angusj.com/delphi/clipper/documentation/Docs/Units/ClipperLib/Types/JoinType.htm </param>
/// <param name="endType"> See http://www.angusj.com/delphi/clipper/documentation/Docs/Units/ClipperLib/Types/EndType.htm </param>
public void AddPath(List<Vector2> path, JoinType joinType = JoinType.jtMiter, EndType endType = EndType.etClosedPolygon)
{
clipperOffset.AddPath(ClipperUtility.ToIntPath(path), joinType, endType);
}
/// <summary>
/// Adds a path to a ClipperOffset object in preparation for offsetting.
/// </summary>
/// <param name="path"> Vertices of the path. </param>
/// <param name="joinType"> See http://www.angusj.com/delphi/clipper/documentation/Docs/Units/ClipperLib/Types/JoinType.htm </param>
/// <param name="endType"> See http://www.angusj.com/delphi/clipper/documentation/Docs/Units/ClipperLib/Types/EndType.htm </param>
public void AddPath(List<IntPoint> path, JoinType joinType = JoinType.jtMiter, EndType endType = EndType.etClosedPolygon)
{
clipperOffset.AddPath(path, joinType, endType);
}
/// <summary>
/// Adds paths to a ClipperOffset object in preparation for offsetting.
/// </summary>
/// <param name="paths"> List of paths. </param>
/// <param name="joinType"> See http://www.angusj.com/delphi/clipper/documentation/Docs/Units/ClipperLib/Types/JoinType.htm </param>
/// <param name="endType"> See http://www.angusj.com/delphi/clipper/documentation/Docs/Units/ClipperLib/Types/EndType.htm </param>
public void AddPaths(List<List<Vector2>> paths, JoinType joinType = JoinType.jtMiter, EndType endType = EndType.etClosedPolygon)
{
clipperOffset.AddPaths(ClipperUtility.ToIntPaths(paths), joinType, endType);
}
/// <summary>
/// Adds paths to a ClipperOffset object in preparation for offsetting.
/// </summary>
/// <param name="paths"> List of paths. </param>
/// <param name="joinType"> See http://www.angusj.com/delphi/clipper/documentation/Docs/Units/ClipperLib/Types/JoinType.htm </param>
/// <param name="endType"> See http://www.angusj.com/delphi/clipper/documentation/Docs/Units/ClipperLib/Types/EndType.htm </param>
public void AddPaths(List<List<IntPoint>> paths, JoinType joinType = JoinType.jtMiter, EndType endType = EndType.etClosedPolygon)
{
clipperOffset.AddPaths(paths, joinType, endType);
}
/// <summary>
/// Performs the offset operation.
/// Can be called multiple times, offsetting the same paths by different amounts (ie using different deltas).
/// </summary>
/// <param name="output">The List that will receive the result of the offset operation.</param>
/// <param name="delta">
/// The amount to which the supplied paths will be offset.
/// Positive values expand polygons and negative values shrink them.
/// Scaled by <see cref="ClipperUtility.ClipperScale"/>.
/// </param>
public void Offset(ref List<List<Vector2>> output, double delta)
{
var intOutput = new List<List<IntPoint>>();
clipperOffset.Execute(ref intOutput, delta*ClipperUtility.ClipperScale);
ClipperUtility.ToVector2Paths(intOutput, ref output);
}
/// <summary>
/// Performs the offset operation.
/// Can be called multiple times, offsetting the same paths by different amounts (ie using different deltas).
/// </summary>
/// <param name="output">The List that will receive the result of the offset operation.</param>
/// <param name="delta">
/// The amount to which the supplied paths will be offset.
/// Positive values expand polygons and negative values shrink them.
/// Not scaled.
/// </param>
public void Offset(ref List<List<IntPoint>> output, double delta)
{
clipperOffset.Execute(ref output, delta);
}
/// <summary>
/// Performs the offset operation.
/// Can be called multiple times, offsetting the same paths by different amounts (ie using different deltas).
/// </summary>
/// <param name="output">The PolyTree that will receive the result of the offset operation.</param>
/// <param name="delta">
/// The amount to which the supplied paths will be offset.
/// Positive values expand polygons and negative values shrink them.
/// Not scaled.
/// </param>
public void Offset(ref PolyTree output, double delta)
{
clipperOffset.Execute(ref output, delta);
}
/// <summary>
/// Clears all paths from the ClipperOffset object, allowing new paths to be assigned.
/// </summary>
public void Clear()
{
clipperOffset.Clear();
}
}
}