geom3d/surface/
bezier.rs

1use super::Surface;
2use crate::basis::bernstein;
3use crate::{Float, Grid, Point3, Point4};
4
5#[derive(Debug)]
6pub struct BezierSurface<P> {
7    pub control_points: Grid<P>,
8}
9
10impl<P: Clone> BezierSurface<P> {
11    pub fn new(control_points: Grid<P>) -> Self {
12        assert!(control_points.rows() > 1 && control_points.cols() > 1);
13        Self { control_points }
14    }
15}
16
17impl Surface for BezierSurface<Point3> {
18    fn get_point(&self, u: Float, v: Float) -> Point3 {
19        let (n, m) = self.control_points.size();
20        let basis_u = bernstein(n, u); // n rows
21        let basis_v = bernstein(m, v); // m cols
22        let mut point = Point3::ZERO;
23        for i in 0..n {
24            for j in 0..m {
25                let p = self.control_points[i][j];
26                point += basis_u[i] * basis_v[j] * p;
27            }
28        }
29        point
30    }
31}
32
33/// Rational bezier surface, point (x,y,z) with weight w is (wx,wy,wz,w)
34impl Surface for BezierSurface<Point4> {
35    fn get_point(&self, u: Float, v: Float) -> Point3 {
36        let (n, m) = self.control_points.size();
37        let basis_u = bernstein(n, u); // n rows
38        let basis_v = bernstein(m, v); // m cols
39        let mut point = Point4::ZERO;
40        for i in 0..n {
41            for j in 0..m {
42                let p = self.control_points[i][j];
43                point += basis_u[i] * basis_v[j] * p;
44            }
45        }
46        (1.0 / point.w) * point.truncate()
47    }
48}