IDS peak ICV v1.3.0
Loading...
Searching...
No Matches
PointCloudXYZ.cs
Go to the documentation of this file.
1using System.Runtime.InteropServices;
6
8
15public class PointCloudXYZ : PointCloudT<PointXYZ>
16{
27 public PointCloudXYZ(XYZImage image)
28 : base(CreateFromXYZImage(image))
29 {
30 }
31
49 public PointCloudXYZ(UndistortedImage undistortedDepthMap)
50 : base(CreateFromUndistortedImage(undistortedDepthMap))
51 {
52 }
53
75 public PointCloudXYZ(string filePath)
76 : base(LoadFromFile(filePath))
77 {
78 }
79
87 public PointCloudXYZ(PointXYZ[] points)
88 : base(CreateFromPoints(points))
89 {
90 _points = points;
91 }
92
93 private PointCloudXYZ()
94 {
95 }
96
97 private PointCloudXYZ(Handle handle) : base(handle)
98 {
99 }
100
103 public override PointXYZ[] Points
104 {
105 get
106 {
107 if (_points != null)
108 {
109 return _points;
110 }
111
112 nuint numPoints = 0;
113 DllHelper.ExecuteAndMapReturnCodes(() =>
114 CInterface.peak_icv_PointCloud_GetPoints_GetCount(_handle, out numPoints));
115
116 if (numPoints == 0)
117 {
118 _points = Array.Empty<PointXYZ>();
119 return _points;
120 }
121
122 var pointsC = new Backend.Types.peak_icv_point_xyz[numPoints];
123 var handle = GCHandle.Alloc(pointsC, GCHandleType.Pinned);
124
125 try
126 {
127 IntPtr ptr = handle.AddrOfPinnedObject();
128
129 DllHelper.ExecuteAndMapReturnCodes(() =>
130 CInterface.peak_icv_PointCloud_GetPoints(_handle,
131 ptr,
132 (nuint) Marshal.SizeOf<Backend.Types.peak_icv_point_xyz>() * numPoints));
133 }
134 finally
135 {
136 handle.Free();
137 }
138
139
140 _points = pointsC
141 .Select(np => new PointXYZ(np.x, np.y, np.z))
142 .ToArray();
143 return _points;
144 }
145 }
146
151 public static IEqualityComparer<PointCloudXYZ> Comparer { get; } = new PointCloudEqualityComparer();
152
153 private static Handle CreateFromUndistortedImage(UndistortedImage undistortedDepthMap)
154 {
155 using var xyz = new XYZImage(undistortedDepthMap);
156 return CreateFromXYZImage(xyz);
157 }
158
170 {
171 var pointCloud = new PointCloudXYZ();
172 var cMatrix = matrix.ToCType();
173 DllHelper.ExecuteAndMapReturnCodes(() =>
174 CInterface.peak_icv_PointCloud_Transform(_handle, cMatrix, ref pointCloud._handle));
175 return pointCloud;
176 }
177
189 {
190 var pointCloud = new PointCloudXYZ();
191 DllHelper.ExecuteAndMapReturnCodes(() =>
192 CInterface.peak_icv_PointCloud_TransformToWorkspace(_handle, extrinsicParameters.ToCType(),
193 (nuint) Marshal.SizeOf<Backend.Types.peak_icv_extrinsic_parameters>(),
194 ref pointCloud._handle));
195 return pointCloud;
196 }
197
198 private static Handle CreateFromXYZImage(XYZImage image)
199 {
200 var handle = new Handle();
201 DllHelper.ExecuteAndMapReturnCodes(() =>
202 CInterface.peak_icv_PointCloud_CreateFromXYZImage(out handle, image._handle));
203 return handle;
204 }
205
206 private static Handle LoadFromFile(string filePath)
207 {
208 var handle = new Handle();
209 DllHelper.ExecuteAndMapReturnCodes(() =>
210 CInterface.peak_icv_PointCloud_CreateFromFile(out handle,
211 Backend.Types.peak_icv_point_type.PEAK_ICV_POINT_TYPE_XYZ, Utils.ToUtf8(filePath)));
212 return handle;
213 }
214
215 private static Handle CreateFromPoints(PointXYZ[] points)
216 {
217 Backend.Types.peak_icv_point_xyz[] pointsC = points.Select(point =>
218 new Backend.Types.peak_icv_point_xyz
219 {
220 x = point.X,
221 y = point.Y,
222 z = point.Z
223 }).ToArray();
224
225 var handle = new Handle();
226 DllHelper.ExecuteAndMapReturnCodes(() =>
227 CInterface.peak_icv_PointCloud_CreateFromPoints(
228 out handle, pointsC, (nuint) pointsC.Length,
229 Backend.Types.peak_icv_point_type.PEAK_ICV_POINT_TYPE_XYZ));
230 return handle;
231 }
232
233 private sealed class PointCloudEqualityComparer : IEqualityComparer<PointCloudXYZ>
234 {
235 public bool Equals(PointCloudXYZ x, PointCloudXYZ y)
236 {
237 if (ReferenceEquals(x, y))
238 {
239 return true;
240 }
241
242 if (ReferenceEquals(x, null))
243 {
244 return false;
245 }
246
247 if (ReferenceEquals(y, null))
248 {
249 return false;
250 }
251
252 return x.Points.SequenceEqual(y.Points, PointXYZ.Comparer);
253 }
254
255 public int GetHashCode(PointCloudXYZ obj)
256 {
257 PointXYZ[] points = obj.Points;
258 var count = points.Count();
259
260 return (
261 count,
262 PointXYZ.Comparer.GetHashCode(points.First()),
263 PointXYZ.Comparer.GetHashCode(points.ElementAt(count / 2)),
264 PointXYZ.Comparer.GetHashCode(points.Last())
265 ).GetHashCode();
266 }
267 }
268}
Represents the extrinsic parameters for camera calibration, including rotation, translation and the t...
A point consists of x, y and z coordinate.
Definition PointXYZ.cs:11
static IEqualityComparer< PointXYZ > Comparer
Gets a default equality comparer for comparing PointXYZ data values.
Definition PointXYZ.cs:51
Represents a point cloud consisting of points with x, y, and z coordinates.
PointCloudXYZ Transform(TransformationMatrix3D matrix)
Applies a transformation to the point cloud.
PointCloudXYZ(string filePath)
Constructs a point cloud by loading it from the specified file.
PointCloudXYZ TransformToWorkspace(ExtrinsicParameters extrinsicParameters)
Transforms the point cloud to the workspace coordinate system using the extrinsic parameters.
PointCloudXYZ(XYZImage image)
Constructs a point cloud from the specified XYZ image.
static IEqualityComparer< PointCloudXYZ > Comparer
Gets a default equality comparer for comparing PointCloudXYZ data values.
PointCloudXYZ(UndistortedImage undistortedDepthMap)
Constructs a point cloud from the specified undistorted depth map.
PointCloudXYZ(PointXYZ[] points)
Constructs a point cloud from the specified collection of points.
A transformation matrix is a square matrix that, when multiplied by the coordinates of a geometric ob...
Represents an image that has undergone undistortion.
Represents a three-channel image encoding cartesian coordinates (x, y, z) of a 3D point.
Definition XYZImage.cs:27