IDS peak ICV v1.3.0
Loading...
Searching...
No Matches
PolygonF.cs
Go to the documentation of this file.
1using System.Runtime.InteropServices;
4using Microsoft.Win32.SafeHandles;
5
7
17public class PolygonF : IDisposable, IHasSafeHandle
18{
19 internal readonly Handle _handle;
20
25 public PolygonF()
26 {
27 _handle = new Handle();
28 }
29
37 public PolygonF(PointF[] points)
38 {
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 })
41 .ToArray();
42
43 _handle = new Handle(cPoints);
44 }
45
46 internal PolygonF(Handle handle)
47 {
48 _handle = handle;
49 }
50
59 public bool IsClosed
60 {
61 get
62 {
63 var isClosed = false;
64 DllHelper.ExecuteAndMapReturnCodes(() =>
65 CInterface.peak_icv_Polygon_IsClosed(_handle, out isClosed));
66 return isClosed;
67 }
68 }
69
74 public PointF[] Points
75 {
76 get
77 {
78 nuint numPoints = 0;
79 DllHelper.ExecuteAndMapReturnCodes(() =>
80 CInterface.peak_icv_Polygon_GetPoints_GetCount(_handle, out numPoints));
81
82 if (numPoints == 0)
83 {
84 return Array.Empty<PointF>();
85 }
86
87 var cPoints = new Backend.Types.peak_icv_point_f[numPoints];
88 var cPointsHandle = GCHandle.Alloc(cPoints, GCHandleType.Pinned);
89
90 try
91 {
92 DllHelper.ExecuteAndMapReturnCodes(() =>
93 CInterface.peak_icv_Polygon_GetPoints(_handle, cPointsHandle.AddrOfPinnedObject(),
94 numPoints * (nuint) Marshal.SizeOf(typeof(Backend.Types.peak_icv_point_f))));
95 }
96 finally
97 {
98 cPointsHandle.Free();
99 }
100
101 return cPoints.Select(cCoordinate => new PointF(cCoordinate.x, cCoordinate.y)).ToArray();
102 }
103 }
104
109 public static IEqualityComparer<PolygonF> Comparer { get; } = new PolygonFEqualityComparer();
110
115 public void Dispose()
116 {
117 _handle.Dispose();
118 GC.SuppressFinalize(this);
119 }
120
121 SafeHandle IHasSafeHandle.Handle => _handle;
122
130 public override string ToString()
131 {
132 PointF[] points = Points;
133 return "(" + string.Join(", ", points.Select(p => "(" + p + ")")) + ")";
134 }
135
136 private sealed class PolygonFEqualityComparer : IEqualityComparer<PolygonF>
137 {
138 public bool Equals(PolygonF x, PolygonF y)
139 {
140 if (ReferenceEquals(x, y))
141 {
142 return true;
143 }
144
145 if (ReferenceEquals(x, null))
146 {
147 return false;
148 }
149
150 if (ReferenceEquals(y, null))
151 {
152 return false;
153 }
154
155 return x.Points.SequenceEqual(y.Points);
156 }
157
158 public int GetHashCode(PolygonF obj)
159 {
160 return obj.Points.Aggregate(17, (current, point) =>
161 current * 31 + (point?.GetHashCode() ?? 0));
162 }
163 }
164
165 internal sealed class Handle : SafeHandleZeroOrMinusOneIsInvalid
166 {
167 internal Handle() : base(true)
168 {
169 DllHelper.ExecuteAndMapReturnCodes(() => CInterface.peak_icv_Polygon_Create(out handle));
170 }
171
172 internal Handle(Backend.Types.peak_icv_point_f[] cPoints) : base(true)
173 {
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));
176 }
177
178 protected override bool ReleaseHandle()
179 {
180 return CInterface.peak_icv_Polygon_Destroy(handle) ==
181 Backend.Types.peak_icv_status.PEAK_ICV_STATUS_SUCCESS;
182 }
183 }
184
185 internal static class CInterface
186 {
187 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
188 public static extern Backend.Types.peak_icv_status peak_icv_Polygon_Create(out IntPtr polygon_handle);
189
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);
194
195 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
196 public static extern Backend.Types.peak_icv_status peak_icv_Polygon_Destroy(IntPtr polygon_handle);
197
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);
201
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);
206
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);
210 }
211}
Represents a 2D point with floating-point coordinates.
Definition Point.cs:126
A polygon is a two-dimensional geometric shape that is made up of a finite number of straight line se...
Definition PolygonF.cs:18
static IEqualityComparer< PolygonF > Comparer
Gets a default equality comparer for comparing PolygonF data values.
Definition PolygonF.cs:109
PointF[] Points
Gets the points from a polygon.
Definition PolygonF.cs:75
bool IsClosed
Gets information about whether a polygon is closed.
Definition PolygonF.cs:60
void Dispose()
Releases the resources used by the PolygonF class.
Definition PolygonF.cs:115
PolygonF(PointF[] points)
Initializes a new instance of the PolygonF class with specified floating-point points.
Definition PolygonF.cs:37
PolygonF()
Initializes a new empty instance of the PolygonF class.
Definition PolygonF.cs:25
override string ToString()
Converts the PolygonF to a string representation.
Definition PolygonF.cs:130