CVB++ 15.1
Loading...
Searching...
No Matches
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 , preAlign_(false)
95 {
96 SetMode(mode);
97 }
98
99 ~AlignmentConfiguration() = default;
101 AlignmentConfiguration &operator=(const AlignmentConfiguration &) = delete;
103 AlignmentConfiguration &operator=(AlignmentConfiguration &&) = default;
104
106
110 size_t MaxIterations() const noexcept
111 {
112 return maxIterations_;
113 }
114
116
120 void SetMaxIterations(size_t maxIterations) noexcept
121 {
122 maxIterations_ = maxIterations;
123 }
124
126
133 double Tolerance() const noexcept
134 {
135 return tolerance_;
136 }
137
139
143 void SetTolerance(double tolerance) noexcept
144 {
145 tolerance_ = tolerance;
146 }
147
149
156 double MinImprovement() const noexcept
157 {
158 return minImprovement_;
159 }
160
162
166 void SetMinImprovement(double minImprovement) noexcept
167 {
168 minImprovement_ = minImprovement;
169 }
170
173
178 double Correspondence() const noexcept
179 {
180 return correspondence_;
181 }
182
185
190 void SetCorrespondence(double correspondence) noexcept
191 {
192 correspondence_ = correspondence;
193 }
194
196
200 PreAlignmentMode Mode() const noexcept
201 {
202 return preAlign_ ? PreAlignmentMode::On : PreAlignmentMode::Off;
203 }
204
206
210 void SetMode(PreAlignmentMode mode) noexcept
211 {
212 preAlign_ = mode == PreAlignmentMode::On ? true : false;
213 }
214
215 private:
216 size_t maxIterations_;
217 double tolerance_;
218 double minImprovement_;
219 double correspondence_;
220 bool preAlign_;
221 };
222
224
226 class AlignmentResult2D
227 {
228 public:
229 AlignmentResult2D(const AlignmentResult2D &) noexcept = default;
230 AlignmentResult2D(AlignmentResult2D &&) noexcept = default;
231
232 AlignmentResult2D &operator=(const AlignmentResult2D &) = default;
233 AlignmentResult2D &operator=(AlignmentResult2D &&) = default;
234
235 ~AlignmentResult2D() = default;
236
238
242 const Matrix2D &Rotation() const noexcept
243 {
244 return rotation_;
245 }
246
248
252 const Vector2D<double> &Translation() const noexcept
253 {
254 return translation_;
255 }
256
258
262 double MeanDistance() const noexcept
263 {
264 return meanDistance_;
265 }
266
268
272 size_t NumIterations() const noexcept
273 {
274 return numIterations_;
275 }
276
277 private:
278 AlignmentResult2D() noexcept {};
279
280 Matrix2D rotation_;
281 Point2D<double> translation_;
282 double meanDistance_{0};
283 size_t numIterations_{0};
284
285 friend AlignmentResult2D AlignCurve(const std::vector<Point2D<double>> &, const std::vector<Point2D<double>> &,
286 const AlignmentConfiguration &);
287 };
288
291
307 inline AlignmentResult2D AlignCurve(const std::vector<Point2D<double>> &curve,
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([&]() {
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 {
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:68
A set of parameters that stores the result of alignment operation.
Definition curve.hpp:227
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:133
PreAlignmentMode Mode() const noexcept
Gets the pre-align mode.
Definition curve.hpp:200
void SetTolerance(double tolerance) noexcept
Sets the value for early stopping criteria.
Definition curve.hpp:143
size_t MaxIterations() const noexcept
Gets the maximum iterations of ICP.
Definition curve.hpp:110
void SetMode(PreAlignmentMode mode) noexcept
Sets the pre-align mode.
Definition curve.hpp:210
void SetCorrespondence(double correspondence) noexcept
Sets the minimum value for correspondence of two points to be considered for alignment.
Definition curve.hpp:190
double MinImprovement() const noexcept
Gets the value for early stopping criteria.
Definition curve.hpp:156
void SetMinImprovement(double minImprovement) noexcept
Sets the value for early stopping criteria.
Definition curve.hpp:166
void SetMaxIterations(size_t maxIterations) noexcept
Sets the maximum iterations of ICP.
Definition curve.hpp:120
double Correspondence() const noexcept
Gets the minimum value for correspondence of two points to be considered for alignment.
Definition curve.hpp:178
A set of parameters that stores the result of alignment operation.
Definition curve.hpp:227
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
size_t NumIterations() const noexcept
Gets the number of iterations needed for alignment.
Definition curve.hpp:272
const Matrix2D & Rotation() const noexcept
Gets the estimated rotation.
Definition curve.hpp:242
double MeanDistance() const noexcept
Gets the mean distance between reference and aligned curve.
Definition curve.hpp:262
const Vector2D< double > & Translation() const noexcept
Gets the estimated translation.
Definition curve.hpp:252
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: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: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:502
Namespace for the Foundation package.
Definition decl_metric_aqs12_calibration_piece.hpp:11
Root namespace for the Image Manager interface.
Definition version.hpp:11
Point2D< T > Vector2D
Alias for Point2D.
Definition point_2d.hpp:379