-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Polyhedron.cs
280 lines (247 loc) · 8.89 KB
/
Polyhedron.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
using System.Diagnostics.CodeAnalysis;
using System.Drawing;
using System.Numerics;
namespace X10D.Drawing;
/// <summary>
/// Represents a 3D polyhedron composed of single-precision floating-point points.
/// </summary>
public class Polyhedron : IEquatable<Polyhedron>
{
private readonly List<Vector3> _vertices = new();
/// <summary>
/// Initializes a new instance of the <see cref="Polyhedron" /> class.
/// </summary>
public Polyhedron()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="Polyhedron" /> class by copying the specified polyhedron.
/// </summary>
/// <exception cref="ArgumentNullException"><paramref name="polyhedron" /> is <see langword="null" />.</exception>
public Polyhedron(Polyhedron polyhedron)
: this(polyhedron?._vertices ?? throw new ArgumentNullException(nameof(polyhedron)))
{
}
/// <summary>
/// Initializes a new instance of the <see cref="Polyhedron" /> class by constructing it from the specified vertices.
/// </summary>
/// <param name="vertices">An enumerable collection of vertices from which the polyhedron should be constructed.</param>
/// <exception cref="ArgumentNullException"><paramref name="vertices" /> is <see langword="null" />.</exception>
public Polyhedron(IEnumerable<Vector3> vertices)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(vertices);
#else
if (vertices is null)
{
throw new ArgumentNullException(nameof(vertices));
}
#endif
_vertices = new List<Vector3>(vertices);
}
/// <summary>
/// Gets an empty polyhedron. That is, a polygon with no vertices.
/// </summary>
/// <value>An empty polyhedron.</value>
public static Polyhedron Empty
{
get => new();
}
/// <summary>
/// Returns a value indicating whether this polyhedron is convex.
/// </summary>
/// <value><see langword="true" /> if this polyhedron is convex; otherwise, <see langword="false" />.</value>
public bool IsConvex
{
get
{
if (_vertices.Count < 4)
{
return false;
}
Vector3[] vertices = _vertices.ToArray();
int n = vertices.Length;
for (var i = 0; i < n; i++)
{
int j = (i + 1) % n;
int k = (i + 2) % n;
if (Vector3.Cross(vertices[j] - vertices[i], vertices[k] - vertices[j]).LengthSquared() < 1e-6f)
{
return false;
}
}
return true;
}
}
/// <summary>
/// Gets the number of vertices in this polyhedron.
/// </summary>
/// <value>An <see cref="int" /> value, representing the number of vertices in this polyhedron.</value>
public int VertexCount
{
get => _vertices.Count;
}
/// <summary>
/// Gets a read-only view of the vertices in this polyhedron.
/// </summary>
/// <value>
/// A <see cref="IReadOnlyList{T}" /> of <see cref="Vector3" /> values, representing the vertices of this polyhedron.
/// </value>
public IReadOnlyList<Vector3> Vertices
{
get => _vertices.AsReadOnly();
}
/// <summary>
/// Returns a value indicating whether two instances of <see cref="Polyhedron" /> are equal.
/// </summary>
/// <param name="left">The first instance.</param>
/// <param name="right">The second instance.</param>
/// <returns>
/// <see langword="true" /> if <paramref name="left" /> and <paramref name="right" /> are considered equal; otherwise,
/// <see langword="false" />.
/// </returns>
public static bool operator ==(Polyhedron? left, Polyhedron? right)
{
return Equals(left, right);
}
/// <summary>
/// Returns a value indicating whether two instances of <see cref="Polyhedron" /> are not equal.
/// </summary>
/// <param name="left">The first instance.</param>
/// <param name="right">The second instance.</param>
/// <returns>
/// <see langword="true" /> if <paramref name="left" /> and <paramref name="right" /> are considered not equal; otherwise,
/// <see langword="false" />.
/// </returns>
public static bool operator !=(Polyhedron? left, Polyhedron? right)
{
return !(left == right);
}
/// <summary>
/// Implicitly converts a <see cref="Polygon" /> to a <see cref="Polyhedron" />.
/// </summary>
/// <param name="polygon">The polyhedron to convert.</param>
/// <returns>
/// The converted polyhedron, or <see langword="null" /> if <paramref name="polygon" /> is <see langword="null" />.
/// </returns>
[return: NotNullIfNotNull("polygon")]
public static implicit operator Polyhedron?(Polygon? polygon)
{
return polygon is null ? null : FromPolygon(polygon);
}
/// <summary>
/// Implicitly converts a <see cref="PolygonF" /> to a <see cref="Polyhedron" />.
/// </summary>
/// <param name="polygon">The polyhedron to convert.</param>
/// <returns>
/// The converted polyhedron, or <see langword="null" /> if <paramref name="polygon" /> is <see langword="null" />.
/// </returns>
[return: NotNullIfNotNull("polygon")]
public static implicit operator Polyhedron?(PolygonF? polygon)
{
return polygon is null ? null : FromPolygonF(polygon);
}
/// <summary>
/// Converts a <see cref="Polygon" /> to a <see cref="Polyhedron" />.
/// </summary>
/// <param name="polygon">The polyhedron to convert.</param>
/// <returns>The converted polyhedron.</returns>
/// <exception cref="ArgumentNullException"><paramref name="polygon" /> is <see langword="null" />.</exception>
public static Polyhedron FromPolygon(Polygon polygon)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(polygon);
#else
if (polygon is null)
{
throw new ArgumentNullException(nameof(polygon));
}
#endif
var vertices = new List<Vector3>();
foreach (Point vertex in polygon.Vertices)
{
vertices.Add(new Vector3(vertex.X, vertex.Y, 0));
}
return new Polyhedron(vertices);
}
/// <summary>
/// Converts a <see cref="PolygonF" /> to a <see cref="Polyhedron" />.
/// </summary>
/// <param name="polygon">The polyhedron to convert.</param>
/// <returns>The converted polyhedron.</returns>
/// <exception cref="ArgumentNullException"><paramref name="polygon" /> is <see langword="null" />.</exception>
public static Polyhedron FromPolygonF(PolygonF polygon)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(polygon);
#else
if (polygon is null)
{
throw new ArgumentNullException(nameof(polygon));
}
#endif
var vertices = new List<Vector3>();
foreach (PointF vertex in polygon.Vertices)
{
vertices.Add(new Vector3(vertex.X, vertex.Y, 0));
}
return new Polyhedron(vertices);
}
/// <summary>
/// Adds a vertex to this polyhedron.
/// </summary>
/// <param name="vertex">The vertex to add.</param>
public void AddVertex(Vector3 vertex)
{
_vertices.Add(vertex);
}
/// <summary>
/// Adds a collection of vertices to this polyhedron.
/// </summary>
/// <param name="vertices">An enumerable collection of vertices to add.</param>
/// <exception cref="ArgumentNullException"><paramref name="vertices" /> is <see langword="null" />.</exception>
public void AddVertices(IEnumerable<Vector3> vertices)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(vertices);
#else
if (vertices is null)
{
throw new ArgumentNullException(nameof(vertices));
}
#endif
foreach (Vector3 vertex in vertices)
{
AddVertex(vertex);
}
}
/// <summary>
/// Clears all vertices from this polyhedron.
/// </summary>
public void ClearVertices()
{
_vertices.Clear();
}
/// <inheritdoc />
public override bool Equals(object? obj)
{
return obj is Polyhedron polyhedron && Equals(polyhedron);
}
/// <summary>
/// Returns a value indicating whether this instance and another instance are equal.
/// </summary>
/// <param name="other">The instance with which to compare.</param>
/// <returns>
/// <see langword="true" /> if this instance and <paramref name="other" /> are considered equal; otherwise,
/// <see langword="false" />.
/// </returns>
public bool Equals(Polyhedron? other)
{
return other is not null && _vertices.SequenceEqual(other._vertices);
}
/// <inheritdoc />
public override int GetHashCode()
{
return _vertices.Aggregate(0, HashCode.Combine);
}
}