CVB++ 15.0
curve.hpp
1#pragma once
2
3#if defined _WIN32
4
5# include "../_cexports/c_foundation.h"
6# include "cvb/area_2d.hpp"
7# include "cvb/line_2d.hpp"
8# include "cvb/matrix_2d.hpp"
9
10# include <algorithm>
11
12namespace Cvb
13{
14 CVB_BEGIN_INLINE_NS
15
16 namespace Foundation
17 {
19
23 namespace Curve
24 {
27
38 int resampledCurveLength)
39 {
40 std::vector<Point2D<double>> resampledCurve(resampledCurveLength);
41 Internal::DoResCall([&]() {
42 return CVB_CALL_CAPI(CVFResampleCurve2D(
43 reinterpret_cast<const CExports::CVIPointD *>(curve.data()),
44 static_cast<CExports::cvbdim_t>(curve.size()),
45 static_cast<CExports::cvbdim_t>(resampledCurve.size()),
46 reinterpret_cast<CExports::CVIPointD *>(resampledCurve.data()));
47 }));
48 return resampledCurve;
49 }
50
53 {
62 };
63
65
67 class AlignmentConfiguration
68 {
69 public:
70 AlignmentConfiguration() noexcept
71 : maxIterations_(50)
72 , tolerance_(1e-6)
73 , minImprovement_(1e-3)
74 , correspondence_(.9)
75 , preAlign_(false)
76 {
77 }
78
80
88 AlignmentConfiguration(size_t maxIterations, double tolerance, double minImprovement, double correspondence,
89 PreAlignmentMode mode) noexcept
90 : maxIterations_(maxIterations)
91 , tolerance_(tolerance)
92 , minImprovement_(minImprovement)
93 , correspondence_(correspondence)
94 {
95 SetMode(mode);
96 }
97
98 AlignmentConfiguration(const AlignmentConfiguration &) noexcept = default;
100
101 AlignmentConfiguration &operator=(const AlignmentConfiguration &) = default;
102 AlignmentConfiguration &operator=(AlignmentConfiguration &&) = default;
103
105
109 size_t MaxIterations() const noexcept
110 {
111 return maxIterations_;
112 }
113
115
119 void SetMaxIterations(size_t maxIterations) noexcept
120 {
121 maxIterations_ = maxIterations;
122 }
123
125
132 double Tolerance() const noexcept
133 {
134 return tolerance_;
135 }
136
138
142 void SetTolerance(double tolerance) noexcept
143 {
144 tolerance_ = tolerance;
145 }
146
148
155 double MinImprovement() const noexcept
156 {
157 return minImprovement_;
158 }
159
161
165 void SetMinImprovement(double minImprovement) noexcept
166 {
167 minImprovement_ = minImprovement;
168 }
169
172
177 double Correspondence() const noexcept
178 {
179 return correspondence_;
180 }
181
184
189 void SetCorrespondence(double correspondence) noexcept
190 {
191 correspondence_ = correspondence;
192 }
193
195
199 PreAlignmentMode Mode() const noexcept
200 {
201 return preAlign_ ? PreAlignmentMode::On : PreAlignmentMode::Off;
202 }
203
205
209 void SetMode(PreAlignmentMode mode) noexcept
210 {
211 preAlign_ = mode == PreAlignmentMode::On ? true : false;
212 }
213
214 private:
215 size_t maxIterations_;
216 double tolerance_;
217 double minImprovement_;
218 double correspondence_;
219 bool preAlign_;
220 };
221
223
225 class AlignmentResult2D
226 {
227 public:
228 AlignmentResult2D(const AlignmentResult2D &) noexcept = default;
229 AlignmentResult2D(AlignmentResult2D &&) noexcept = default;
230
231 AlignmentResult2D &operator=(const AlignmentResult2D &) = default;
232 AlignmentResult2D &operator=(AlignmentResult2D &&) = default;
233
235
239 const Matrix2D &Rotation() const noexcept
240 {
241 return rotation_;
242 }
243
245
249 const Vector2D<double> &Translation() const noexcept
250 {
251 return translation_;
252 }
253
255
259 double MeanDistance() const noexcept
260 {
261 return meanDistance_;
262 }
263
265
269 size_t NumIterations() const noexcept
270 {
271 return numIterations_;
272 }
273
274 private:
275 AlignmentResult2D() noexcept
276 : meanDistance_(0)
277 , numIterations_(0) {};
278
279 Matrix2D rotation_;
280 Point2D<double> translation_;
281 double meanDistance_;
282 size_t numIterations_;
283
285 const AlignmentConfiguration &);
286 };
287
290
306 inline AlignmentResult2D AlignCurve(const std::vector<Point2D<double>> &curve,
307 const std::vector<Point2D<double>> &curveToBeAligned,
309 {
310 AlignmentResult2D result;
311 Internal::DoResCall([&]() {
312 return CVB_CALL_CAPI(CVFAlignCurves2D(reinterpret_cast<const CExports::CVIPointD *>(curve.data()),
313 static_cast<CExports::cvbdim_t>(curve.size()),
314 reinterpret_cast<const CExports::CVIPointD *>(curveToBeAligned.data()),
315 static_cast<CExports::cvbdim_t>(curveToBeAligned.size()),
316 reinterpret_cast<const CExports::CVFAlignmentConfiguration &>(config),
317 reinterpret_cast<CExports::CVFAlignment2DResult &>(result)));
318 });
319 return result;
320 }
321
324
338 const std::vector<Point2D<double>> &points)
339 {
340 std::vector<double> differences(points.size());
341 Internal::DoResCall([&]() {
342 return CVB_CALL_CAPI(
343 CVFCalculateSignedDifferences(reinterpret_cast<const CExports::CVIPointD *>(curve.data()),
344 static_cast<CExports::cvbdim_t>(curve.size()),
345 reinterpret_cast<const CExports::CVIPointD *>(points.data()),
346 static_cast<CExports::cvbdim_t>(points.size()), differences.data()));
347 });
348 return differences;
349 }
350
352
375 const Line2D &line)
376 {
377 std::vector<Point2D<double>> intersectionPoints(curve.size());
378 auto intersectionPointsCalculatedLength = intersectionPoints.size();
379 auto normal = line.Normal();
380 Internal::DoResCall([&]() {
381 return CVB_CALL_CAPI(CVFIntersectCurve2DWithLine(
382 reinterpret_cast<const CExports::CVIPointD *>(curve.data()),
383 static_cast<CExports::cvbdim_t>(curve.size()), *reinterpret_cast<const CExports::CVIPointD *>(&normal),
384 line.Distance(), static_cast<CExports::cvbdim_t>(intersectionPoints.size()),
385 reinterpret_cast<CExports::CVIPointD *>(intersectionPoints.data()),
386 reinterpret_cast<CExports::cvbdim_t &>(intersectionPointsCalculatedLength)));
387 });
388 intersectionPoints.resize(intersectionPointsCalculatedLength);
389 return intersectionPoints;
390 }
391
393
418 {
419 double enclosedArea = 0;
420 Internal::DoResCall([&]() {
421 return CVB_CALL_CAPI(CVFCalculateEnclosedArea(reinterpret_cast<const CExports::CVIPointD *>(curve.data()),
422 static_cast<CExports::cvbdim_t>(curve.size()), &enclosedArea));
423 });
424 return enclosedArea;
425 }
426
428
442 const std::vector<Point2D<double>> &points)
443 {
444 Matrix2D distance;
445 Internal::DoResCall([&]() {
447 reinterpret_cast<const CExports::CVIPointD *>(curve.data()),
448 static_cast<CExports::cvbdim_t>(curve.size()),
449 reinterpret_cast<const CExports::CVIPointD *>(points.data()),
450 static_cast<CExports::cvbdim_t>(points.size()), reinterpret_cast<double *>(&distance)));
451 });
452 return distance;
453 }
454
457 {
466 };
467
469
502 const std::vector<Point2D<double>> &curveRHS,
503 const StartSelectionMode &mode)
504 {
505 std::vector<Point2D<double>> mergedCurve(curveLHS.size() + curveRHS.size());
506 int mergedLength = 0;
507 Internal::DoResCall([&]() {
508 return CVB_CALL_CAPI(CVFMerge2Curves(reinterpret_cast<const CExports::CVIPointD *>(curveLHS.data()),
509 static_cast<CExports::cvbdim_t>(curveLHS.size()),
510 reinterpret_cast<const CExports::CVIPointD *>(curveRHS.data()),
511 static_cast<CExports::cvbdim_t>(curveRHS.size()),
512 mode == StartSelectionMode::AsIs ? false : true,
513 static_cast<CExports::cvbdim_t>(mergedCurve.size()),
514 reinterpret_cast<CExports::CVIPointD *>(mergedCurve.data()),
515 reinterpret_cast<CExports::cvbdim_t &>(mergedLength)));
516 });
517 if (mergedLength != static_cast<int>(mergedCurve.size()))
518 mergedCurve.resize(mergedLength);
519 return mergedCurve;
520 }
521 } // namespace Curve
522
523 using Curve::AlignCurve;
524 using Curve::AlignmentConfiguration;
525 using Curve::AlignmentResult2D;
534
535 } // namespace Foundation
536 CVB_END_INLINE_NS
537} // namespace Cvb
538
539#endif
A set of parameters that configures an alignment operation.
Definition curve.hpp:68
AlignmentConfiguration(size_t maxIterations, double tolerance, double minImprovement, double correspondence, PreAlignmentMode mode) noexcept
Constructor for an alignment configuration.
Definition curve.hpp:88
double Tolerance() const noexcept
Gets the value for early stopping criteria.
Definition curve.hpp:132
PreAlignmentMode Mode() const noexcept
Gets the pre-align mode.
Definition curve.hpp:199
void SetTolerance(double tolerance) noexcept
Sets the value for early stopping criteria.
Definition curve.hpp:142
size_t MaxIterations() const noexcept
Gets the maximum iterations of ICP.
Definition curve.hpp:109
void SetMode(PreAlignmentMode mode) noexcept
Sets the pre-align mode.
Definition curve.hpp:209
void SetCorrespondence(double correspondence) noexcept
Sets the minimum value for correspondence of two points to be considered for alignment.
Definition curve.hpp:189
double MinImprovement() const noexcept
Gets the value for early stopping criteria.
Definition curve.hpp:155
void SetMinImprovement(double minImprovement) noexcept
Sets the value for early stopping criteria.
Definition curve.hpp:165
void SetMaxIterations(size_t maxIterations) noexcept
Sets the maximum iterations of ICP.
Definition curve.hpp:119
double Correspondence() const noexcept
Gets the minimum value for correspondence of two points to be considered for alignment.
Definition curve.hpp:177
A set of parameters that stores the result of alignment operation.
Definition curve.hpp:226
friend AlignmentResult2D AlignCurve(const std::vector< Point2D< double > > &, const std::vector< Point2D< double > > &, const AlignmentConfiguration &)
Aligns two 2D curves via an iterative closest point method considering consensus correspondence for b...
Definition curve.hpp:306
size_t NumIterations() const noexcept
Gets the number of iterations needed for alignment.
Definition curve.hpp:269
const Matrix2D & Rotation() const noexcept
Gets the estimated rotation.
Definition curve.hpp:239
double MeanDistance() const noexcept
Gets the mean distance between reference and aligned curve.
Definition curve.hpp:259
const Vector2D< double > & Translation() const noexcept
Gets the estimated translation.
Definition curve.hpp:249
Object representing an infinite line in 2 dimensional space.
Definition line_2d.hpp:16
double Distance() const noexcept
Gets the distance of the line from the origin.
Definition line_2d.hpp:77
Point2D< double > Normal() const noexcept
Gets the normal vector of the line.
Definition line_2d.hpp:56
Double precision 2x2 matrix class.
Definition matrix_2d.hpp:16
Multi-purpose 2D vector class.
Definition point_2d.hpp:20
cvbres_t CVFCalculateSignedDifferences(const CVIPointD *Curve, cvbdim_t CurveLength, const CVIPointD *Points, cvbdim_t PointsLength, double *SignedDifferences)
cvbres_t CVFResampleCurve2D(const CVIPointD *Curve, cvbdim_t CurveLength, cvbdim_t ResampledCurveLength, CVIPointD *ResampledCurve)
cvbres_t CVFCalculateEnclosedArea(const CVIPointD *Curve, cvbdim_t CurveLength, double *EnclosedArea)
cvbres_t CVFAlignCurves2D(const CVIPointD *Curve, cvbdim_t CurveLength, const CVIPointD *CurveToBeAligned, cvbdim_t CurveToBeAlignedLength, const CVFAlignmentConfiguration &Config, CVFAlignment2DResult &Result)
cvbres_t CVFCalculateQuadraticDifferencesToClosestPoint(const CVIPointD *Curve, cvbdim_t CurveLength, const CVIPointD *Points, cvbdim_t PointsLength, double *Differences)
cvbres_t CVFIntersectCurve2DWithLine(const CVIPointD *Curve, cvbdim_t CurveLength, CVIPointD Normal, double DistanceToOrigin, cvbdim_t IntersectionPointsLength, CVIPointD *IntersectionPoints, cvbdim_t &IntersectionPointsCalculatedLength)
cvbres_t CVFMerge2Curves(const CVIPointD *CurveLHS, cvbdim_t CurveLengthLHS, const CVIPointD *CurveRHS, cvbdim_t CurveLengthRHS, cvbbool_t sort, cvbdim_t CurveOutAllocatedLength, CVIPointD *CurveOut, cvbdim_t &CurveOutLength)
Namespace for collection of 2D curve functions from the Foundation package.
Definition curve.hpp:24
double CalculateEnclosedArea(const std::vector< Point2D< double > > &curve)
Calculates the enclosed area of the given curve.
Definition curve.hpp:417
Matrix2D CalculateQuadraticDifferencesToClosestPoint(const std::vector< Point2D< double > > &curve, const std::vector< Point2D< double > > &points)
Calculates the quadratic distance of the points to the closest point of the given curve.
Definition curve.hpp:441
std::vector< double > CalculateSignedDifferences(const std::vector< Point2D< double > > &curve, const std::vector< Point2D< double > > &points)
Calculates signed differences along the y axis of given points to curve (curve - points).
Definition curve.hpp:337
AlignmentResult2D AlignCurve(const std::vector< Point2D< double > > &curve, const std::vector< Point2D< double > > &curveToBeAligned, const AlignmentConfiguration &config=AlignmentConfiguration())
Aligns two 2D curves via an iterative closest point method considering consensus correspondence for b...
Definition curve.hpp:306
std::vector< Point2D< double > > IntersectWithLine(const std::vector< Point2D< double > > &curve, const Line2D &line)
Intersects a 2D curve with a line.
Definition curve.hpp:374
StartSelectionMode
Decides which start point should be used for the new curve.
Definition curve.hpp:457
std::vector< Point2D< double > > ResampleCurve(const std::vector< Point2D< double > > &curve, int resampledCurveLength)
Resamples a 2-dimensional curve via linear interpolation assuming that the "speed" of the time mappin...
Definition curve.hpp:37
PreAlignmentMode
Specifies if the pre-alignment is required.
Definition curve.hpp:53
std::vector< Point2D< double > > MergeTwoCurves(const std::vector< Point2D< double > > &curveLHS, const std::vector< Point2D< double > > &curveRHS, const StartSelectionMode &mode)
Merges two curves into one curve with interpolation.
Definition curve.hpp:501
Namespace for the Foundation package.
Definition decl_metric_aqs12_calibration_piece.hpp:11
Root namespace for the Image Manager interface.
Definition c_bayer_to_rgb.h:17
Point2D< T > Vector2D
Alias for Point2D.
Definition point_2d.hpp:379