MeshLib C++ Docs
Loading...
Searching...
No Matches
MRBezier.h
Go to the documentation of this file.
1#pragma once
2
3#include "MRMeshFwd.h"
4#include "MRVectorTraits.h"
5#include <array>
6
7namespace MR
8{
9
11template <typename V>
13{
15 using T = typename VTraits::BaseType;
16 static constexpr int elements = VTraits::size;
17
19 V p[4];
20
22 V getPoint( T t ) const;
23
25 static std::array<T, 4> getWeights( T t );
26};
27
28template <typename V>
30{
31 // De Casteljau's algorithm
32 V q[3];
33 for ( int i = 0; i < 3; ++i )
34 q[i] = lerp( p[i], p[i+1], t );
35
36 V r[2];
37 for ( int i = 0; i < 2; ++i )
38 r[i] = lerp( q[i], q[i+1], t );
39
40 return lerp( r[0], r[1], t );
41}
42
43template <typename V>
44inline auto CubicBezierCurve<V>::getWeights( T t ) -> std::array<T, 4>
45{
46 T s = 1 - t;
47 std::array<T, 4> w =
48 {
49 s * s * s,
50 3 * s * s * t,
51 3 * s * t * t
52 };
53 w[3] = 1 - w[0] - w[1] - w[2];
54 return w;
55}
56
57} //namespace MR
constexpr auto lerp(V v0, V v1, T t) noexcept
Linear interpolation: returns v0 when t==0 and v1 when t==1.
Definition MRMesh/MRMeshFwd.h:677
Cubic Bezier curve.
Definition MRBezier.h:13
V p[4]
4 control points
Definition MRBezier.h:19
static std::array< T, 4 > getWeights(T t)
computes weights of every control point for given parameter value, the sum of all weights is equal to...
Definition MRBezier.h:44
typename VTraits::BaseType T
Definition MRBezier.h:15
V getPoint(T t) const
computes point on the curve from parameter value
Definition MRBezier.h:29
static constexpr int elements
Definition MRBezier.h:16
Definition MRMesh/MRVectorTraits.h:15
static constexpr int size
Definition MRMesh/MRVectorTraits.h:19
T BaseType
Definition MRMesh/MRVectorTraits.h:18