1 /* 2 Copyright (c) 2013-2021 Timur Gafarov 3 4 Boost Software License - Version 1.0 - August 17th, 2003 5 6 Permission is hereby granted, free of charge, to any person or organization 7 obtaining a copy of the software and accompanying documentation covered by 8 this license (the "Software") to use, reproduce, display, distribute, 9 execute, and transmit the Software, and to prepare derivative works of the 10 Software, and to permit third-parties to whom the Software is furnished to 11 do so, all subject to the following: 12 13 The copyright notices in the Software and this entire statement, including 14 the above license grant, this restriction and the following disclaimer, 15 must be included in all copies of the Software, in whole or in part, and 16 all derivative works of the Software, unless such copies or derivative 17 works are solely in the form of machine-executable object code generated by 18 a source language processor. 19 20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 DEALINGS IN THE SOFTWARE. 27 */ 28 29 /** 30 * Bézier interpolation functions 31 * 32 * Copyright: Timur Gafarov 2013-2021. 33 * License: $(LINK2 boost.org/LICENSE_1_0.txt, Boost License 1.0). 34 * Authors: Timur Gafarov 35 */ 36 module dlib.math.interpolation.bezier; 37 38 import dlib.math.vector; 39 40 /** 41 * Computes cubic Bézier curve 42 */ 43 T bezierCubic(T) (T A, T B, T C, T D, T t) 44 { 45 T s = cast(T)1.0 - t; 46 T s2 = s * s; 47 T s3 = s2 * s; 48 return s3 * A + 49 3.0 * t * s2 * B + 50 3.0 * t * t * s * C + 51 t * t * t * D; 52 } 53 54 /// ditto 55 alias bezier = bezierCubic; 56 57 /** 58 * Computes cubic Bézier curve tangent 59 */ 60 T bezierCubicTangent(T)(T a, T b, T c, T d, T t) 61 { 62 T c1 = (d - (3.0 * c) + (3.0 * b) - a); 63 T c2 = ((3.0 * c) - (6.0 * b) + (3.0 * a)); 64 T c3 = ((3.0 * b) - (3.0 * a)); 65 return ((3.0 * c1 * t * t) + (2.0 * c2 * t) + c3); 66 } 67 68 /// ditto 69 alias bezierTangent = bezierCubicTangent; 70 71 /** 72 * Computes cubic Bézier curve in 2D space 73 */ 74 Vector!(T,2) bezierVector2(T)( 75 Vector!(T,2) a, 76 Vector!(T,2) b, 77 Vector!(T,2) c, 78 Vector!(T,2) d, 79 T t) 80 { 81 return Vector!(T,2) 82 ( 83 bezier(a.x, b.x, c.x, d.x, t), 84 bezier(a.y, b.y, c.y, d.y, t) 85 ); 86 } 87 88 /** 89 * Computes cubic Bézier curve in 3D space 90 */ 91 Vector!(T,3) bezierVector3(T)( 92 Vector!(T,3) a, 93 Vector!(T,3) b, 94 Vector!(T,3) c, 95 Vector!(T,3) d, 96 T t) 97 { 98 return Vector!(T,3) 99 ( 100 bezier(a.x, b.x, c.x, d.x, t), 101 bezier(a.y, b.y, c.y, d.y, t), 102 bezier(a.z, b.z, c.z, d.z, t) 103 ); 104 } 105 106 /** 107 * Computes cubic Bézier curve tangent in 2D space 108 */ 109 Vector!(T,2) bezierTangentVector2(T)( 110 Vector!(T,2) a, 111 Vector!(T,2) b, 112 Vector!(T,2) c, 113 Vector!(T,2) d, 114 T t) 115 { 116 return Vector!(T,2) 117 ( 118 bezierTangent(a.x, b.x, c.x, d.x, t), 119 bezierTangent(a.y, b.y, c.y, d.y, t) 120 ); 121 } 122 123 /** 124 * Computes cubic Bézier curve tangent in 3D space 125 */ 126 Vector!(T,3) bezierTangentVector3(T)( 127 Vector!(T,3) a, 128 Vector!(T,3) b, 129 Vector!(T,3) c, 130 Vector!(T,3) d, 131 T t) 132 { 133 return Vector!(T,3) 134 ( 135 bezierTangent(a.x, b.x, c.x, d.x, t), 136 bezierTangent(a.y, b.y, c.y, d.y, t), 137 bezierTangent(a.z, b.z, c.z, d.z, t) 138 ); 139 }