splines
splines - popular parametric splines for Gleam.
Splines are often used in graphics, simulations and games. They can be used to approximate curved figures, define paths for objects to move along, or shape the behavior of kinematics.
splines defines several popular spline kinds:
- B-splines, often used for smooth paths that require strong continuity
- Beziér splines, often used for curved figures
- Catmull-Rom splines, often used for smooth paths that pass through the knots
Other splines may be added in the future.
Types
A uniform cubic 2D Beziér Spline.
pub type Bezier2D =
Spline(bezier.Bezier(vec2.Vec2(Float)), vec2.Vec2(Float))
A uniform cubic 3D Beziér Spline.
pub type Bezier3D =
Spline(bezier.Bezier(vec3.Vec3(Float)), vec3.Vec3(Float))
A uniform cubic 2D Catmull-Rom Spline.
pub type CatmullRom2D =
Spline(
catmull_rom.CatmullRom(vec2.Vec2(Float)),
vec2.Vec2(Float),
)
A uniform cubic 3D Catmull-Rom Spline.
pub type CatmullRom3D =
Spline(
catmull_rom.CatmullRom(vec3.Vec3(Float)),
vec3.Vec3(Float),
)
Values
pub fn basis_2d(
points: List(vec2.Vec2(Float)),
) -> Result(
Spline(b.BSpline(vec2.Vec2(Float)), vec2.Vec2(Float)),
Nil,
)
Constructs a sequence of cubic B-spline curves in 2d.
The list of points must be at least of length 4.
pub fn basis_3d(
points: List(vec3.Vec3(Float)),
) -> Result(
Spline(b.BSpline(vec3.Vec3(Float)), vec3.Vec3(Float)),
Nil,
)
Constructs a sequence of cubic B-spline curves in 3d.
The list of points must be at least of length 4.
pub fn bezier_2d(
points: List(vec2.Vec2(Float)),
) -> Result(
Spline(bezier.Bezier(vec2.Vec2(Float)), vec2.Vec2(Float)),
Nil,
)
Constructs a sequence of cubic Beziér curves in 3d, where every segment has two control points and the curves are C0 continuous (share knots).
The list of points must be at least of length 4, and have multiples of 3 beyond the first 4.
pub fn bezier_3d(
points: List(vec3.Vec3(Float)),
) -> Result(
Spline(bezier.Bezier(vec3.Vec3(Float)), vec3.Vec3(Float)),
Nil,
)
Constructs a sequence of cubic Beziér curves in 3d, where every segment has two control points and the curves are C0 continuous (share knots).
The list of points must be at least of length 4, and have multiples of 3 beyond the first 4.
pub fn catmull_rom_2d(
points: List(vec2.Vec2(Float)),
) -> Result(
Spline(
catmull_rom.CatmullRom(vec2.Vec2(Float)),
vec2.Vec2(Float),
),
Nil,
)
Constructs a sequence of Catmull-Rom curves in 2d, where the curve passes through all but the first and last points in the sequence.
The list of points must be at least of length 4.
pub fn catmull_rom_3d(
points: List(vec3.Vec3(Float)),
) -> Result(
Spline(
catmull_rom.CatmullRom(vec3.Vec3(Float)),
vec3.Vec3(Float),
),
Nil,
)
Constructs a sequence of Catmull-Rom curves in 3d, where the curve passes through all but the first and last points in the sequence.
The list of points must be at least of length 4.
pub fn length(spline: Spline(a, p)) -> Int
Returns the number of individual curves composing the spline.
pub fn sample(spline: Spline(a, p), t: Float) -> p
Samples a generic spline at time t. The behavior of sampling
depends on the number of component curves:
- If
0 <= t <= length(spline), the integer part oftis used to select the curve and then subtracted fromtto scale it to within[0,1]. - If
tis less than0.0, the first curve is sampled. - If
tis greater than the number of curves, the last curve is sampled andlength(spline) - 1is subtracted fromtto scale for the last curve’s offset.
Example:
Assume a spline s with three component curves (indexed 0, 1, 2).
sample(s, -0.75)will sample curve0at-0.75(out-of-bounds before).sample(s, 0.33)will sample curve0at0.33.sample(s, 1.2)will sample curve1at0.2.sample(s, 2.5)will sample curve2at0.5.sample(s, 3.79)will sample curve2at1.79(out-of-bounds after).