CVB++ 14.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
37 inline std::vector<Point2D<double>> ResampleCurve(const std::vector<Point2D<double>> &curve, int resampledCurveLength)
38 {
39 std::vector<Point2D<double>> resampledCurve(resampledCurveLength);
40 Internal::DoResCall([&]() {
41 return CVB_CALL_CAPI(CVFResampleCurve2D(
42 reinterpret_cast<const CExports::CVIPointD *>(curve.data()),
43 static_cast<CExports::cvbdim_t>(curve.size()),
44 static_cast<CExports::cvbdim_t>(resampledCurve.size()),
45 reinterpret_cast<CExports::CVIPointD *>(resampledCurve.data()));
46 }));
47 return resampledCurve;
48 }
49
52 {
56 Off,
60 On
61 };
62
64
67 {
68 public:
69 AlignmentConfiguration() noexcept
70 : maxIterations_(50)
71 , tolerance_(1e-6)
72 , minImprovement_(1e-3)
73 , correspondence_(.9)
74 , preAlign_(false)
75 {
76 }
77
79
87 AlignmentConfiguration(size_t maxIterations, double tolerance, double minImprovement, double correspondence,
88 PreAlignmentMode mode) noexcept
89 : maxIterations_(maxIterations)
90 , tolerance_(tolerance)
91 , minImprovement_(minImprovement)
92 , correspondence_(correspondence)
93 {
94 SetMode(mode);
95 }
96
97 AlignmentConfiguration(const AlignmentConfiguration &) noexcept = default;
99
100 AlignmentConfiguration &operator=(const AlignmentConfiguration &) = default;
101 AlignmentConfiguration &operator=(AlignmentConfiguration &&) = default;
102
104
108 size_t MaxIterations() const noexcept
109 {
110 return maxIterations_;
111 }
112
114
118 void SetMaxIterations(size_t maxIterations) noexcept
119 {
120 maxIterations_ = maxIterations;
121 }
122
124
131 double Tolerance() const noexcept
132 {
133 return tolerance_;
134 }
135
137
141 void SetTolerance(double tolerance) noexcept
142 {
143 tolerance_ = tolerance;
144 }
145
147
154 double MinImprovement() const noexcept
155 {
156 return minImprovement_;
157 }
158
160
164 void SetMinImprovement(double minImprovement) noexcept
165 {
166 minImprovement_ = minImprovement;
167 }
168
171
176 double Correspondence() const noexcept
177 {
178 return correspondence_;
179 }
180
183
188 void SetCorrespondence(double correspondence) noexcept
189 {
190 correspondence_ = correspondence;
191 }
192
194
198 PreAlignmentMode Mode() const noexcept
199 {
200 return preAlign_ ? PreAlignmentMode::On : PreAlignmentMode::Off;
201 }
202
204
208 void SetMode(PreAlignmentMode mode) noexcept
209 {
210 preAlign_ = mode == PreAlignmentMode::On ? true : false;
211 }
212
213 private:
214 size_t maxIterations_;
215 double tolerance_;
216 double minImprovement_;
217 double correspondence_;
218 bool preAlign_;
219 };
220
222
225 {
226 public:
227 AlignmentResult2D(const AlignmentResult2D &) noexcept = default;
228 AlignmentResult2D(AlignmentResult2D &&) noexcept = default;
229
230 AlignmentResult2D &operator=(const AlignmentResult2D &) = default;
231 AlignmentResult2D &operator=(AlignmentResult2D &&) = default;
232
234
238 const Matrix2D &Rotation() const noexcept
239 {
240 return rotation_;
241 }
242
244
248 const Vector2D<double> &Translation() const noexcept
249 {
250 return translation_;
251 }
252
255
260 double MeanDistance() const noexcept
261 {
262 return meanDistance_;
263 }
264
266
270 size_t NumIterations() const noexcept
271 {
272 return numIterations_;
273 }
274
275 private:
276 AlignmentResult2D() noexcept
277 : meanDistance_(0)
278 , numIterations_(0){};
279
280 Matrix2D rotation_;
281 Point2D<double> translation_;
282 double meanDistance_;
283 size_t numIterations_;
284
286 const AlignmentConfiguration &);
287 };
288
291
308 const std::vector<Point2D<double>> &curveToBeAligned,
310 {
311 AlignmentResult2D result;
312 Internal::DoResCall([&]() {
313 return CVB_CALL_CAPI(CVFAlignCurves2D(reinterpret_cast<const CExports::CVIPointD *>(curve.data()),
314 static_cast<CExports::cvbdim_t>(curve.size()),
315 reinterpret_cast<const CExports::CVIPointD *>(curveToBeAligned.data()),
316 static_cast<CExports::cvbdim_t>(curveToBeAligned.size()),
317 reinterpret_cast<const CExports::CVFAlignmentConfiguration &>(config),
318 reinterpret_cast<CExports::CVFAlignment2DResult &>(result)));
319 });
320 return result;
321 }
322
325
339 const std::vector<Point2D<double>> &points)
340 {
341 std::vector<double> differences(points.size());
342 Internal::DoResCall([&]() {
343 return CVB_CALL_CAPI(
344 CVFCalculateSignedDifferences(reinterpret_cast<const CExports::CVIPointD *>(curve.data()),
345 static_cast<CExports::cvbdim_t>(curve.size()),
346 reinterpret_cast<const CExports::CVIPointD *>(points.data()),
347 static_cast<CExports::cvbdim_t>(points.size()), differences.data()));
348 });
349 return differences;
350 }
351
353
376 const Line2D &line)
377 {
378 std::vector<Point2D<double>> intersectionPoints(curve.size());
379 auto intersectionPointsCalculatedLength = intersectionPoints.size();
380 auto normal = line.Normal();
381 Internal::DoResCall([&]() {
382 return CVB_CALL_CAPI(CVFIntersectCurve2DWithLine(
383 reinterpret_cast<const CExports::CVIPointD *>(curve.data()),
384 static_cast<CExports::cvbdim_t>(curve.size()), *reinterpret_cast<const CExports::CVIPointD *>(&normal),
385 line.Distance(), static_cast<CExports::cvbdim_t>(intersectionPoints.size()),
386 reinterpret_cast<CExports::CVIPointD *>(intersectionPoints.data()),
387 reinterpret_cast<CExports::cvbdim_t &>(intersectionPointsCalculatedLength)));
388 });
389 intersectionPoints.resize(intersectionPointsCalculatedLength);
390 return intersectionPoints;
391 }
392
394
419 {
420 double enclosedArea = 0;
421 Internal::DoResCall([&]() {
422 return CVB_CALL_CAPI(CVFCalculateEnclosedArea(reinterpret_cast<const CExports::CVIPointD *>(curve.data()),
423 static_cast<CExports::cvbdim_t>(curve.size()), &enclosedArea));
424 });
425 return enclosedArea;
426 }
427
429
443 const std::vector<Point2D<double>> &points)
444 {
445 Matrix2D distance;
446 Internal::DoResCall([&]() {
447 return CVB_CALL_CAPI(CVFCalculateQuadraticDifferencesToClosestPoint(
448 reinterpret_cast<const CExports::CVIPointD *>(curve.data()),
449 static_cast<CExports::cvbdim_t>(curve.size()),
450 reinterpret_cast<const CExports::CVIPointD *>(points.data()),
451 static_cast<CExports::cvbdim_t>(points.size()), reinterpret_cast<double *>(&distance)));
452 });
453 return distance;
454 }
455
458 {
462 AsIs,
466 Best
467 };
468
470
503 const std::vector<Point2D<double>> &curveRHS,
504 const StartSelectionMode &mode)
505 {
506 std::vector<Point2D<double>> mergedCurve(curveLHS.size() + curveRHS.size());
507 int mergedLength = 0;
508 Internal::DoResCall([&]() {
509 return CVB_CALL_CAPI(CVFMerge2Curves(reinterpret_cast<const CExports::CVIPointD *>(curveLHS.data()),
510 static_cast<CExports::cvbdim_t>(curveLHS.size()),
511 reinterpret_cast<const CExports::CVIPointD *>(curveRHS.data()),
512 static_cast<CExports::cvbdim_t>(curveRHS.size()),
513 mode == StartSelectionMode::AsIs ? false : true,
514 static_cast<CExports::cvbdim_t>(mergedCurve.size()),
515 reinterpret_cast<CExports::CVIPointD *>(mergedCurve.data()),
516 reinterpret_cast<CExports::cvbdim_t &>(mergedLength)));
517 });
518 if (mergedLength != static_cast<int>(mergedCurve.size()))
519 mergedCurve.resize(mergedLength);
520 return mergedCurve;
521 }
522 } // namespace Curve
523
524 using Curve::AlignCurve;
525 using Curve::AlignmentConfiguration;
526 using Curve::AlignmentResult2D;
535
536 } // namespace Foundation
537 CVB_END_INLINE_NS
538} // namespace Cvb
539
540#endif
A set of parameters that configures an alignment operation.
Definition: curve.hpp:67
AlignmentConfiguration(size_t maxIterations, double tolerance, double minImprovement, double correspondence, PreAlignmentMode mode) noexcept
Constructor for an alignment configuration.
Definition: curve.hpp:87
double Tolerance() const noexcept
Gets the value for early stopping criteria.
Definition: curve.hpp:131
PreAlignmentMode Mode() const noexcept
Gets the pre-align mode.
Definition: curve.hpp:198
void SetTolerance(double tolerance) noexcept
Sets the value for early stopping criteria.
Definition: curve.hpp:141
size_t MaxIterations() const noexcept
Gets the maximum iterations of ICP.
Definition: curve.hpp:108
void SetMode(PreAlignmentMode mode) noexcept
Sets the pre-align mode.
Definition: curve.hpp:208
void SetCorrespondence(double correspondence) noexcept
Sets the minimum value for correspondence of two points to be considered for alignment.
Definition: curve.hpp:188
double MinImprovement() const noexcept
Gets the value for early stopping criteria.
Definition: curve.hpp:154
void SetMinImprovement(double minImprovement) noexcept
Sets the value for early stopping criteria.
Definition: curve.hpp:164
void SetMaxIterations(size_t maxIterations) noexcept
Sets the maximum iterations of ICP.
Definition: curve.hpp:118
double Correspondence() const noexcept
Gets the minimum value for correspondence of two points to be considered for alignment.
Definition: curve.hpp:176
A set of parameters that stores the result of alignment operation.
Definition: curve.hpp:225
size_t NumIterations() const noexcept
Gets the number of iterations needed for alignment.
Definition: curve.hpp:270
const Matrix2D & Rotation() const noexcept
Gets the estimated rotation.
Definition: curve.hpp:238
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:307
double MeanDistance() const noexcept
Gets the minimum value for correspondence of two points to be considered for alignment.
Definition: curve.hpp:260
const Vector2D< double > & Translation() const noexcept
Gets the estimated translation.
Definition: curve.hpp:248
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:73
Point2D< double > Normal() const noexcept
Gets the normal vector of the line.
Definition: line_2d.hpp:52
Double precision 2x2 matrix class.
Definition: matrix_2d.hpp:16
double CalculateEnclosedArea(const std::vector< Point2D< double > > &curve)
Calculates the enclosed area of the given curve.
Definition: curve.hpp:418
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:442
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:338
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:307
std::vector< Point2D< double > > IntersectWithLine(const std::vector< Point2D< double > > &curve, const Line2D &line)
Intersects a 2D curve with a line.
Definition: curve.hpp:375
StartSelectionMode
Decides which start point should be used for the new curve.
Definition: curve.hpp:458
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:52
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:502
Root namespace for the Image Manager interface.
Definition: c_barcode.h:24