forked from Syomus/ProceduralToolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClipperUtility.cs
63 lines (54 loc) · 1.79 KB
/
ClipperUtility.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
using System.Collections.Generic;
using ProceduralToolkit.ClipperLib;
using UnityEngine;
namespace ProceduralToolkit
{
/// <summary>
/// Utility class for conversion of Clipper data from and to Unity format
/// </summary>
public class ClipperUtility
{
public const float ClipperScale = 100000;
public static List<IntPoint> ToIntPath(IReadOnlyList<Vector2> path)
{
var intPath = new List<IntPoint>(path.Count);
foreach (var vertex in path)
{
intPath.Add(ToIntPoint(vertex));
}
return intPath;
}
public static List<IntPoint> ToIntPath(List<Vector2> path)
{
return path.ConvertAll(ToIntPoint);
}
public static List<Vector2> ToVector2Path(List<IntPoint> intPath)
{
return intPath.ConvertAll(ToVector2);
}
public static List<List<IntPoint>> ToIntPaths(List<List<Vector2>> paths)
{
return paths.ConvertAll(ToIntPath);
}
public static List<List<Vector2>> ToVector2Paths(List<List<IntPoint>> intPaths)
{
return intPaths.ConvertAll(ToVector2Path);
}
public static void ToVector2Paths(List<List<IntPoint>> intPaths, ref List<List<Vector2>> paths)
{
paths.Clear();
foreach (List<IntPoint> intPath in intPaths)
{
paths.Add(ToVector2Path(intPath));
}
}
public static IntPoint ToIntPoint(Vector2 vector2)
{
return new IntPoint(vector2.x*ClipperScale, vector2.y*ClipperScale);
}
public static Vector2 ToVector2(IntPoint intPoint)
{
return new Vector2(intPoint.X/ClipperScale, intPoint.Y/ClipperScale);
}
}
}