Bezier 0.4.0
Fast and lightweight class for using the Bezier curves of any order in C++
Loading...
Searching...
No Matches
bezier.h
1/*
2 * Copyright 2019 Mirko Kokot
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef BEZIER_H
18#define BEZIER_H
19
20#include <memory>
21#include <mutex>
22#include <optional>
23
24#include "declarations.h"
25
26namespace Bezier
27{
36class Curve
37{
38public:
39 ~Curve() = default;
40
46 Curve(Eigen::MatrixX2d points);
47
53 Curve(const PointVector& points);
54
55 Curve(const Curve& curve);
56 Curve(Curve&& curve) noexcept;
57 Curve& operator=(const Curve& curve);
58 Curve& operator=(Curve&& curve) noexcept;
59
64 unsigned order() const;
65
71
78 Point controlPoint(unsigned idx) const;
79
84 std::pair<Point, Point> endPoints() const;
85
92
98 PointVector polyline(double flatness) const;
99
106
112 ParamVector polylineParams(double flatness) const;
113
118 double length() const;
119
126 double length(double t) const;
127
135 double length(double t1, double t2) const;
136
145 double step(double t, double ds) const;
146
151 void reverse();
152
160 void setControlPoint(unsigned idx, const Point& point);
161
169
178
184 Point valueAt(double t) const;
185
191 Eigen::MatrixX2d valueAt(const ParamVector& t_vector) const;
192
198 double curvatureAt(double t) const;
199
205 double curvatureDerivativeAt(double t) const;
206
214 Vector tangentAt(double t) const;
215
222 Vector normalAt(double t) const;
223
230 const Curve& derivative() const;
231
239 const Curve& derivative(unsigned n) const;
240
246 Vector derivativeAt(double t) const;
247
254 Vector derivativeAt(unsigned n, double t) const;
255
261
267
273
279 std::vector<Curve> splitCurve(const ParamVector& t_vector) const;
280
286 std::pair<Curve, Curve> splitCurve(double t = 0.5) const;
287
295 PointVector intersections(const Curve& curve) const;
296
302 double projectPoint(const Point& point) const;
303
309 double distance(const Point& point) const;
310
320 void applyContinuity(const Curve& curve, const std::vector<double>& beta_coeffs);
321
329 static Curve offsetCurve(const Curve& curve, double offset, unsigned order = 0);
330
339 static Curve joinCurves(const Curve& curve1, const Curve& curve2, unsigned order = 0);
340
348 static Curve fromPolyline(const PointVector& polyline, unsigned order = 0);
349
350private:
352 void swap(Curve& other) noexcept;
353
355 unsigned N_{};
357 Eigen::MatrixX2d control_points_;
358
359 struct Cache
360 {
361 Cache() = default;
362 Cache(const Cache& other); // deep-copies derivative; rest are value types
363 Cache(Cache&&) = default;
364 Cache& operator=(const Cache& other);
365 Cache& operator=(Cache&&) = default;
366
367 std::unique_ptr<const Curve> derivative;
368 std::optional<ParamVector> roots;
369 std::optional<BoundingBox> bounding_box;
370 std::optional<PointVector> polyline;
371 std::optional<ParamVector> polyline_t;
372 double polyline_flatness{};
373 std::optional<Eigen::VectorXd> projection_polynomial_const;
374 std::optional<Eigen::MatrixX2d> projection_polynomial_der;
375 std::optional<Eigen::VectorXd> chebyshev_polynomial;
377 void clear();
378 };
379 mutable Cache cache_;
380 mutable std::recursive_mutex cache_mutex_;
381};
382
383} // namespace Bezier
384
385#endif // BEZIER_H
A Bezier curve class.
Definition bezier.h:37
void applyContinuity(const Curve &curve, const std::vector< double > &beta_coeffs)
Apply geometric continuity based on another curve.
Eigen::MatrixX2d valueAt(const ParamVector &t_vector) const
Get the point vector on curve for given parameters.
Point controlPoint(unsigned idx) const
Get the control point at index idx.
double curvatureAt(double t) const
Get curvature of the curve for a given t.
Point valueAt(double t) const
Get the point on curve for a given t.
static Curve fromPolyline(const PointVector &polyline, unsigned order=0)
Fit a Bezier curve to an ordered polyline.
double distance(const Point &point) const
Get distance of the point to the curve.
Curve(const PointVector &points)
Create the Bezier curve.
ParamVector polylineParams(double flatness) const
Get curve parameters corresponding to polyline points.
PointVector intersections(const Curve &curve) const
Get the points of intersection with another curve.
void reverse()
Reverse order of control points.
const Curve & derivative(unsigned n) const
Get the nth derivative of a curve.
Curve(Eigen::MatrixX2d points)
Create the Bezier curve.
static Curve joinCurves(const Curve &curve1, const Curve &curve2, unsigned order=0)
Fit a single Bezier curve through two curves joined end to end.
PointVector polyline(double flatness) const
Get a polyline representation of the curve as a vector of points on curve.
void setControlPoint(unsigned idx, const Point &point)
Set the new coordinates to a control point.
Vector normalAt(double t) const
Get the unit normal of the curve for a given t.
PointVector polyline() const
Get a polyline representation of the curve as a vector of points on curve.
PointVector controlPoints() const
Get a vector of control points.
Vector derivativeAt(double t) const
Get value of a derivative for a given t.
double length() const
Compute exact arc length using Chebyshev polynomials.
void raiseOrder()
Raise the curve order by 1.
ParamVector extrema() const
Get all extrema of the curve.
BoundingBox boundingBox() const
Get the bounding box of curve.
ParamVector roots() const
Get roots of the curve on both axes.
double projectPoint(const Point &point) const
Get the parameter t where curve is closest to given point.
const Curve & derivative() const
Get the derivative of a curve.
Vector derivativeAt(unsigned n, double t) const
Get value of an nth derivative for a given t.
double length(double t1, double t2) const
Compute exact arc length using Chebyshev polynomials.
std::pair< Point, Point > endPoints() const
Get first and last control points.
ParamVector polylineParams() const
Get curve parameters corresponding to polyline points.
double length(double t) const
Compute exact arc length using Chebyshev polynomials.
std::vector< Curve > splitCurve(const ParamVector &t_vector) const
Split the curve into subcurves at multiple parameters.
void lowerOrder()
Lower the curve order by 1.
static Curve offsetCurve(const Curve &curve, double offset, unsigned order=0)
Fit a Bezier approximation of the curve offset by a given distance.
double step(double t, double ds) const
Compute parameter t which is ds distance from given t.
std::pair< Curve, Curve > splitCurve(double t=0.5) const
Split the curve into two subcurves.
unsigned order() const
Get order of the curve (Nth order curve is described with N+1 points)
double curvatureDerivativeAt(double t) const
Get curvature derivative of the curve for a given t.
Vector tangentAt(double t) const
Get the unit tangent of the curve for a given t.
Definition bezier.h:27
Eigen::Vector2d Point
Point in xy plane.
Definition declarations.h:40
std::vector< double > ParamVector
A vector of curve parameters.
Definition declarations.h:50
std::vector< Point > PointVector
A vector of Points.
Definition declarations.h:45
Eigen::AlignedBox2d BoundingBox
Bounding box class.
Definition declarations.h:60
Eigen::Vector2d Vector
A Vector in xy plane.
Definition declarations.h:55