17public class PolygonF : IDisposable, IHasSafeHandle
19 internal readonly Handle _handle;
27 _handle =
new Handle();
39 Backend.Types.peak_icv_point_f[] cPoints = points
40 .Select(coordinate =>
new Backend.Types.peak_icv_point_f { x = coordinate.X, y = coordinate.Y })
43 _handle =
new Handle(cPoints);
64 DllHelper.ExecuteAndMapReturnCodes(() =>
65 CInterface.peak_icv_Polygon_IsClosed(_handle, out isClosed));
79 DllHelper.ExecuteAndMapReturnCodes(() =>
80 CInterface.peak_icv_Polygon_GetPoints_GetCount(_handle, out numPoints));
84 return Array.Empty<
PointF>();
87 var cPoints =
new Backend.Types.peak_icv_point_f[numPoints];
88 var cPointsHandle = GCHandle.Alloc(cPoints, GCHandleType.Pinned);
92 DllHelper.ExecuteAndMapReturnCodes(() =>
93 CInterface.peak_icv_Polygon_GetPoints(_handle, cPointsHandle.AddrOfPinnedObject(),
94 numPoints * (nuint) Marshal.SizeOf(typeof(Backend.Types.peak_icv_point_f))));
101 return cPoints.Select(cCoordinate =>
new PointF(cCoordinate.x, cCoordinate.y)).ToArray();
109 public static IEqualityComparer<PolygonF>
Comparer {
get; } =
new PolygonFEqualityComparer();
118 GC.SuppressFinalize(
this);
121 SafeHandle IHasSafeHandle.Handle => _handle;
133 return "(" +
string.Join(
", ", points.Select(p =>
"(" + p +
")")) +
")";
136 private sealed class PolygonFEqualityComparer : IEqualityComparer<PolygonF>
140 if (ReferenceEquals(x, y))
145 if (ReferenceEquals(x,
null))
150 if (ReferenceEquals(y,
null))
158 public int GetHashCode(
PolygonF obj)
160 return obj.Points.Aggregate(17, (current, point) =>
161 current * 31 + (point?.GetHashCode() ?? 0));
165 internal sealed class Handle : SafeHandleZeroOrMinusOneIsInvalid
167 internal Handle() : base(true)
169 DllHelper.ExecuteAndMapReturnCodes(() => CInterface.peak_icv_Polygon_Create(out handle));
172 internal Handle(Backend.Types.peak_icv_point_f[] cPoints) : base(true)
174 DllHelper.ExecuteAndMapReturnCodes(() => CInterface.peak_icv_Polygon_CreateFromPoints(out handle,
175 cPoints, (uint) cPoints.Length, Backend.Types.peak_icv_point_type.PEAK_ICV_POINT_TYPE_XY));
178 protected override bool ReleaseHandle()
180 return CInterface.peak_icv_Polygon_Destroy(handle) ==
181 Backend.Types.peak_icv_status.PEAK_ICV_STATUS_SUCCESS;
185 internal static class CInterface
187 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
188 public static extern Backend.Types.peak_icv_status peak_icv_Polygon_Create(out IntPtr polygon_handle);
190 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
191 public static extern Backend.Types.peak_icv_status peak_icv_Polygon_CreateFromPoints(
192 out IntPtr polygon_handle, Backend.Types.peak_icv_point_f[] points, nuint num_points,
193 Backend.Types.peak_icv_point_type point_type);
195 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
196 public static extern Backend.Types.peak_icv_status peak_icv_Polygon_Destroy(IntPtr polygon_handle);
198 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
199 public static extern Backend.Types.peak_icv_status peak_icv_Polygon_IsClosed(Handle polygon_handle,
200 [MarshalAs(UnmanagedType.I1)] out
bool is_closed);
202 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
203 public static extern Backend.Types.peak_icv_status peak_icv_Polygon_GetPoints_GetCount(
204 Handle polygon_handle,
205 out nuint num_points);
207 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
208 public static extern Backend.Types.peak_icv_status peak_icv_Polygon_GetPoints(Handle polygon_handle,
209 IntPtr points, nuint byte_size_points);