1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
use num_traits::Float;
use std::ops::*;
use std::iter::Sum;
use vec::repr_c::{
Vec3 as CVec3,
Vec4 as CVec4,
};
macro_rules! bezier_impl_any {
($Bezier:ident $Point:ident) => {
impl<T> $Bezier<T> {
pub fn normalized_tangent(self, t: T) -> $Point<T> where T: Float + Sum {
self.evaluate_derivative(t).normalized()
}
pub fn length_by_discretization(self, step_count: u32) -> T
where T: Float + AddAssign + Sum
{
let mut length = T::zero();
let mut prev_point = self.evaluate(T::zero());
for i in 1..step_count+2 {
let t = T::from(i).unwrap()/(T::from(step_count).unwrap()+T::one());
let next_point = self.evaluate(t);
length += (next_point - prev_point).magnitude();
prev_point = next_point;
}
length
}
}
}
}
macro_rules! bezier_impl_quadratic {
($(#[$attrs:meta])* $QuadraticBezier:ident $Point:ident $LineSegment:ident) => {
$(#[$attrs])*
#[derive(Debug, Default, Copy, Clone, Hash, PartialEq, Eq, )]
#[cfg_attr(feature="serde", derive(Serialize, Deserialize))]
pub struct $QuadraticBezier<T>(pub $Point<T>, pub $Point<T>, pub $Point<T>);
impl<T: Float> $QuadraticBezier<T> {
pub fn evaluate(self, t: T) -> $Point<T> {
let l = T::one();
let two = l+l;
self.0*(l-t)*(l-t) + self.1*two*(l-t)*t + self.2*t*t
}
pub fn evaluate_derivative(self, t: T) -> $Point<T> {
let l = T::one();
let n = l+l;
(self.1-self.0)*(l-t)*n + (self.2-self.1)*t*n
}
pub fn from_line_segment(line: $LineSegment<T>) -> Self {
$QuadraticBezier(line.a, line.a, line.b)
}
pub fn matrix() -> Mat3<T> {
let zero = T::zero();
let one = T::one();
let two = one+one;
Mat3 {
rows: CVec3::new(
Vec3::new( one, zero, zero),
Vec3::new(-two, two, zero),
Vec3::new( one, -two, one),
)
}
}
pub fn split(self, t: T) -> (Self, Self) {
let l = T::one();
let two = l+l;
let first = $QuadraticBezier(
self.0,
self.1*t - self.0*(t-l),
self.2*t*t - self.1*two*t*(t-l) + self.0*(t-l)*(t-l),
);
let second = $QuadraticBezier(
self.2*t*t - self.1*two*t*(t-l) + self.0*(t-l)*(t-l),
self.2*t - self.1*(t-l),
self.2,
);
(first, second)
}
}
impl<T> From<Vec3<$Point<T>>> for $QuadraticBezier<T> {
fn from(v: Vec3<$Point<T>>) -> Self {
$QuadraticBezier(v.x, v.y, v.z)
}
}
impl<T> From<$QuadraticBezier<T>> for Vec3<$Point<T>> {
fn from(v: $QuadraticBezier<T>) -> Self {
Vec3::new(v.0, v.1, v.2)
}
}
bezier_impl_any!($QuadraticBezier $Point);
}
}
macro_rules! bezier_impl_cubic {
($(#[$attrs:meta])* $CubicBezier:ident $Point:ident $LineSegment:ident) => {
$(#[$attrs])*
#[derive(Debug, Default, Copy, Clone, Hash, PartialEq, Eq, )]
#[cfg_attr(feature="serde", derive(Serialize, Deserialize))]
pub struct $CubicBezier<T>(pub $Point<T>, pub $Point<T>, pub $Point<T>, pub $Point<T>);
impl<T: Float> $CubicBezier<T> {
pub fn evaluate(self, t: T) -> $Point<T> {
let l = T::one();
let three = l+l+l;
self.0*(l-t)*(l-t)*(l-t) + self.1*three*(l-t)*(l-t)*t + self.2*three*(l-t)*t*t + self.3*t*t*t
}
pub fn evaluate_derivative(self, t: T) -> $Point<T> {
let l = T::one();
let n = l+l+l;
let two = l+l;
(self.1-self.0)*(l-t)*(l-t)*n + (self.2-self.1)*two*(l-t)*t*n + (self.3-self.2)*t*t*n
}
pub fn from_line_segment(line: $LineSegment<T>) -> Self {
$CubicBezier(line.a, line.a, line.b, line.b)
}
pub fn matrix() -> Mat4<T> {
let zero = T::zero();
let one = T::one();
let three = one+one+one;
let six = three + three;
Mat4 {
rows: CVec4::new(
Vec4::new( one, zero, zero, zero),
Vec4::new(-three, three, zero, zero),
Vec4::new( three, -six, three, zero),
Vec4::new(-one, three, -three, one),
)
}
}
pub fn split(self, t: T) -> (Self, Self) {
let l = T::one();
let two = l+l;
let three = l+l+l;
let first = $CubicBezier(
self.0,
self.1*t - self.0*(t-l),
self.2*t*t - self.1*two*t*(t-l) + self.0*(t-l)*(t-l),
self.3*t*t*t - self.2*three*t*t*(t-l) + self.1*three*t*(t-l)*(t-l) - self.0*(t-l)*(t-l)*(t-l),
);
let second = $CubicBezier(
self.3*t*t*t - self.2*three*t*t*(t-l) + self.1*three*t*(t-l)*(t-l) - self.0*(t-l)*(t-l)*(t-l),
self.3*t*t - self.2*two*t*(t-l) + self.1*(t-l)*(t-l),
self.3*t - self.2*(t-l),
self.3,
);
(first, second)
}
}
impl<T> From<Vec4<$Point<T>>> for $CubicBezier<T> {
fn from(v: Vec4<$Point<T>>) -> Self {
$CubicBezier(v.x, v.y, v.z, v.w)
}
}
impl<T> From<$CubicBezier<T>> for Vec4<$Point<T>> {
fn from(v: $CubicBezier<T>) -> Self {
Vec4::new(v.0, v.1, v.2, v.3)
}
}
bezier_impl_any!($CubicBezier $Point);
}
}
macro_rules! impl_all_beziers {
() => {
bezier_impl_quadratic!{
QuadraticBezier2 Vec2 LineSegment2
}
bezier_impl_quadratic!{
QuadraticBezier3 Vec3 LineSegment3
}
bezier_impl_cubic!{
CubicBezier2 Vec2 LineSegment2
}
bezier_impl_cubic!{
CubicBezier3 Vec3 LineSegment3
}
};
}
#[cfg(all(nightly, feature="repr_simd"))]
pub mod repr_simd {
use super::*;
use vec::repr_simd::{Vec3, Vec4, Vec2};
use mat::repr_simd::row_major::{Mat3, Mat4};
use geom::repr_simd::{LineSegment2, LineSegment3};
impl_all_beziers!{}
}
pub mod repr_c {
use super::*;
use vec::repr_c::{Vec3, Vec4, Vec2};
use mat::repr_c::row_major::{Mat3, Mat4};
use geom::repr_c::{LineSegment2, LineSegment3};
impl_all_beziers!{}
}
pub use self::repr_c::*;