Skip to main content

opensubdiv_rs/sdc/
scheme.rs

1//! Scheme-specific subdivision and limit masks.
2//!
3//! Port of `opensubdiv/sdc/scheme.h` and the scheme specializations in
4//! `bilinearScheme.h`, `catmarkScheme.h` and `loopScheme.h`.
5//!
6//! A *mask* is the set of stencil weights used to compute a refined (child)
7//! vertex — or a limit point — from vertices in its parent neighborhood:
8//!
9//! * **face-vertex** masks weight the vertices of a parent face,
10//! * **edge-vertex** masks weight the two end vertices of a parent edge plus
11//!   one weight per incident face,
12//! * **vertex-vertex** masks weight the parent vertex itself, the vertex at
13//!   the opposite end of each incident edge, and one weight per incident
14//!   face.
15//!
16//! As in OpenSubdiv, per-face weights are flagged as being either for the
17//! *face centers* (the child vertex at the face's center — Catmark) or for
18//! the vertex of the face *opposite* the subject component (Loop edge masks,
19//! Catmark limit masks). See [`EdgeVertexMask::face_weights_for_face_centers`].
20
21use super::crease::{Crease, Rule};
22use super::options::{Options, TriangleSubdivision};
23use super::types::SchemeType;
24
25/// The per-face weight the Catmark "smooth triangle" rule gives an incident
26/// triangle in an edge-vertex mask (`CATMARK_SMOOTH_TRI_EDGE_WEIGHT`), in
27/// place of the standard 1/4.
28const CATMARK_SMOOTH_TRI_EDGE_WEIGHT: f32 = 0.470;
29
30/// Neighborhood description of an edge, as needed to compute its child
31/// vertex mask.
32#[derive(Debug, Clone, Copy)]
33pub struct EdgeNeighborhood {
34    /// Effective sharpness of the edge (boundary sharpening already applied).
35    pub sharpness: f32,
36    /// Number of faces incident the edge (1 for a boundary edge, 2 for a
37    /// manifold interior edge).
38    pub num_faces: usize,
39    /// Number of vertices of each of the first two incident faces
40    /// (`GetNumVerticesPerFace`), in the order of the mask's face weights.
41    /// Only consulted by the Catmark scheme under
42    /// [`TriangleSubdivision::Smooth`], and only for edges with exactly two
43    /// incident faces; entries without a face are ignored and may be zero.
44    pub face_vertex_counts: [usize; 2],
45}
46
47/// Neighborhood description of a vertex, as needed to compute its child
48/// vertex mask (and its limit mask).
49#[derive(Debug, Clone, Copy)]
50pub struct VertexNeighborhood<'a> {
51    /// Effective sharpness of the vertex at the parent level.
52    pub sharpness: f32,
53    /// Sharpness the vertex will have at the child level.
54    pub child_sharpness: f32,
55    /// Effective sharpness of each incident edge at the parent level.
56    pub edge_sharpness: &'a [f32],
57    /// Sharpness each incident edge will have at the child level (same
58    /// order). May be empty when computing limit masks.
59    pub child_edge_sharpness: &'a [f32],
60    /// Number of incident faces.
61    pub num_faces: usize,
62}
63
64/// Mask for a child vertex of a face: a uniform average of the face's
65/// vertices for all schemes that generate face child-vertices.
66#[derive(Debug, Clone, Copy, Default)]
67pub struct FaceVertexMask {
68    /// The single weight applied to every vertex of the parent face.
69    pub vertex_weight: f32,
70}
71
72/// Mask for the child vertex of an edge.
73#[derive(Debug, Clone, Default)]
74pub struct EdgeVertexMask {
75    /// Weights for the two end vertices of the edge.
76    pub vertex_weights: [f32; 2],
77    /// One weight per incident face (empty when the mask is a pure crease).
78    pub face_weights: Vec<f32>,
79    /// When true (Catmark/Bilinear), `face_weights[i]` applies to the *child
80    /// vertex at the center* of incident face `i`. When false (Loop),
81    /// `face_weights[i]` applies to the vertex of incident face `i` opposite
82    /// the edge.
83    pub face_weights_for_face_centers: bool,
84}
85
86/// Mask for the child vertex of a vertex, or for its limit point.
87#[derive(Debug, Clone, Default)]
88pub struct VertexVertexMask {
89    /// Weight applied to the parent vertex itself.
90    pub vertex_weight: f32,
91    /// One weight per incident edge, applied to the vertex at the *opposite
92    /// end* of that edge.
93    pub edge_weights: Vec<f32>,
94    /// One weight per incident face. For refinement masks these apply to the
95    /// *child vertex at the center* of the face
96    /// (`face_weights_for_face_centers == true`); for Catmark limit masks
97    /// they apply to the vertex of the (quad) face *opposite* the subject
98    /// vertex.
99    pub face_weights: Vec<f32>,
100    /// See [`EdgeVertexMask::face_weights_for_face_centers`].
101    pub face_weights_for_face_centers: bool,
102}
103
104impl VertexVertexMask {
105    fn clear_for(&mut self, num_edges: usize, num_faces: usize) {
106        self.vertex_weight = 0.0;
107        self.edge_weights.clear();
108        self.edge_weights.resize(num_edges, 0.0);
109        self.face_weights.clear();
110        self.face_weights.resize(num_faces, 0.0);
111        self.face_weights_for_face_centers = true;
112    }
113}
114
115/// Scheme-specific application of the subdivision rules
116/// (`Sdc::Scheme<SCHEME_TYPE>`).
117#[derive(Debug, Clone, Copy)]
118pub struct Scheme {
119    scheme_type: SchemeType,
120    crease: Crease,
121    triangle_subdivision: TriangleSubdivision,
122}
123
124impl Scheme {
125    /// Create the rules for `scheme_type` under the given subdivision
126    /// `options`.
127    pub fn new(scheme_type: SchemeType, options: Options) -> Self {
128        Self {
129            scheme_type,
130            crease: Crease::new(options),
131            triangle_subdivision: options.triangle_subdivision,
132        }
133    }
134
135    /// The subdivision scheme these rules apply.
136    pub fn scheme_type(&self) -> SchemeType {
137        self.scheme_type
138    }
139
140    /// The triangle-subdivision rule these rules apply (only meaningful for
141    /// the Catmark scheme).
142    pub fn triangle_subdivision(&self) -> TriangleSubdivision {
143        self.triangle_subdivision
144    }
145
146    /// The creasing queries derived from this scheme's options.
147    pub fn crease(&self) -> &Crease {
148        &self.crease
149    }
150
151    // ------------------------------------------------------------------
152    //  Face-vertex masks
153    // ------------------------------------------------------------------
154
155    /// Mask for the child vertex of a face with `num_vertices` vertices
156    /// (`Scheme::ComputeFaceVertexMask`). Identical for all schemes that
157    /// split faces to quads: the centroid of the face.
158    pub fn compute_face_vertex_mask(&self, num_vertices: usize) -> FaceVertexMask {
159        FaceVertexMask {
160            vertex_weight: 1.0 / num_vertices as f32,
161        }
162    }
163
164    // ------------------------------------------------------------------
165    //  Edge-vertex masks
166    // ------------------------------------------------------------------
167
168    /// Mask for the child vertex of an edge (`Scheme::ComputeEdgeVertexMask`).
169    pub fn compute_edge_vertex_mask(&self, edge: &EdgeNeighborhood, mask: &mut EdgeVertexMask) {
170        mask.face_weights.clear();
171        mask.face_weights_for_face_centers = !matches!(self.scheme_type, SchemeType::Loop);
172
173        if self.scheme_type == SchemeType::Bilinear {
174            mask.vertex_weights = [0.5, 0.5];
175            return;
176        }
177
178        // The transitional crease weight blends the sharp (crease) mask with
179        // the smooth mask for semi-sharp edges:
180        let crease_weight = edge.sharpness.clamp(0.0, 1.0);
181
182        let smooth_supported = match self.scheme_type {
183            SchemeType::Catmark => edge.num_faces > 0,
184            SchemeType::Loop => edge.num_faces == 2,
185            SchemeType::Bilinear => unreachable!(),
186        };
187
188        if crease_weight >= 1.0 || !smooth_supported {
189            mask.vertex_weights = [0.5, 0.5];
190            return;
191        }
192
193        let smooth_weight = 1.0 - crease_weight;
194        match self.scheme_type {
195            SchemeType::Catmark => {
196                // Smooth mask: 1/4 to each end vertex, the remaining 1/2
197                // distributed over the incident faces' child vertices —
198                // unless the smooth-triangle rule adjusts the split.
199                let (v_weight, f_weight) = self.catmark_smooth_edge_weights(edge);
200                let vw = crease_weight * 0.5 + smooth_weight * v_weight;
201                mask.vertex_weights = [vw, vw];
202                mask.face_weights
203                    .resize(edge.num_faces, smooth_weight * f_weight);
204            }
205            SchemeType::Loop => {
206                // Smooth mask: 3/8 to each end vertex, 1/8 to the vertex
207                // opposite the edge in each of the two incident triangles.
208                let vw = crease_weight * 0.5 + smooth_weight * 0.375;
209                mask.vertex_weights = [vw, vw];
210                mask.face_weights.resize(2, smooth_weight * 0.125);
211            }
212            SchemeType::Bilinear => unreachable!(),
213        }
214    }
215
216    /// The (end-vertex, per-face) weights of the smooth Catmark edge-vertex
217    /// mask (`Scheme<SCHEME_CATMARK>::assignSmoothMaskForEdge`).
218    ///
219    /// Under [`TriangleSubdivision::Smooth`], an interior edge with a
220    /// triangle on either side uses the "smooth triangle" adjustment of
221    /// Catmull and Clark's original paper: each incident triangle
222    /// contributes [`CATMARK_SMOOTH_TRI_EDGE_WEIGHT`] instead of 1/4, the two
223    /// face weights are averaged, and the end vertices share the remainder.
224    /// This removes the pinching plain Catmark produces at triangles inside
225    /// a quad mesh. The order of operations mirrors OpenSubdiv (and Hbr) so
226    /// the weights match bit for bit.
227    fn catmark_smooth_edge_weights(&self, edge: &EdgeNeighborhood) -> (f32, f32) {
228        if self.triangle_subdivision == TriangleSubdivision::Smooth && edge.num_faces == 2 {
229            let [face0_is_tri, face1_is_tri] = edge.face_vertex_counts.map(|n| n == 3);
230            if face0_is_tri || face1_is_tri {
231                let tri_weight = |is_tri: bool| {
232                    if is_tri {
233                        CATMARK_SMOOTH_TRI_EDGE_WEIGHT
234                    } else {
235                        0.25
236                    }
237                };
238                let f_weight = 0.5 * (tri_weight(face0_is_tri) + tri_weight(face1_is_tri));
239                let v_weight = 0.5 * (1.0 - 2.0 * f_weight);
240                return (v_weight, f_weight);
241            }
242        }
243        (0.25, 0.5 / edge.num_faces as f32)
244    }
245
246    // ------------------------------------------------------------------
247    //  Vertex-vertex masks
248    // ------------------------------------------------------------------
249
250    /// Mask for the child vertex of a vertex
251    /// (`Scheme::ComputeVertexVertexMask`).
252    ///
253    /// Handles the smooth/dart, crease and corner rules, including the
254    /// fractional blending of masks across a refinement step for semi-sharp
255    /// features.
256    pub fn compute_vertex_vertex_mask(
257        &self,
258        vertex: &VertexNeighborhood<'_>,
259        mask: &mut VertexVertexMask,
260    ) {
261        let num_edges = vertex.edge_sharpness.len();
262        mask.clear_for(num_edges, vertex.num_faces);
263
264        if self.scheme_type == SchemeType::Bilinear {
265            mask.vertex_weight = 1.0;
266            return;
267        }
268
269        let sharp_edges_parent = count_sharp(vertex.edge_sharpness);
270        let parent_rule = self
271            .crease
272            .determine_vertex_vertex_rule(vertex.sharpness, sharp_edges_parent);
273
274        // Fast path: no sharp features at all.
275        if parent_rule == Rule::Smooth || parent_rule == Rule::Dart {
276            self.assign_smooth_mask_for_vertex(vertex, 1.0, mask);
277            return;
278        }
279
280        let sharp_edges_child = count_sharp(vertex.child_edge_sharpness);
281        let child_rule = self
282            .crease
283            .determine_vertex_vertex_rule(vertex.child_sharpness, sharp_edges_child);
284
285        if parent_rule == child_rule {
286            self.assign_rule_mask(parent_rule, vertex, ByLevel::Parent, 1.0, mask);
287            return;
288        }
289
290        // Transitional (semi-sharp) case: blend the parent-rule mask with the
291        // child-rule mask by the fractional weight.
292        let frac = self.crease.compute_fractional_weight_at_vertex(
293            vertex.sharpness,
294            vertex.child_sharpness,
295            vertex.edge_sharpness,
296            vertex.child_edge_sharpness,
297        );
298        self.assign_rule_mask(parent_rule, vertex, ByLevel::Parent, frac, mask);
299        self.assign_rule_mask(child_rule, vertex, ByLevel::Child, 1.0 - frac, mask);
300    }
301
302    fn assign_rule_mask(
303        &self,
304        rule: Rule,
305        vertex: &VertexNeighborhood<'_>,
306        level: ByLevel,
307        scale: f32,
308        mask: &mut VertexVertexMask,
309    ) {
310        match rule {
311            Rule::Smooth | Rule::Dart => self.assign_smooth_mask_for_vertex(vertex, scale, mask),
312            Rule::Crease => {
313                let sharpness = match level {
314                    ByLevel::Parent => vertex.edge_sharpness,
315                    ByLevel::Child => vertex.child_edge_sharpness,
316                };
317                assign_crease_mask_for_vertex(sharpness, scale, mask);
318            }
319            Rule::Corner => mask.vertex_weight += scale,
320            Rule::Unknown => unreachable!(),
321        }
322    }
323
324    fn assign_smooth_mask_for_vertex(
325        &self,
326        vertex: &VertexNeighborhood<'_>,
327        scale: f32,
328        mask: &mut VertexVertexMask,
329    ) {
330        let valence = vertex.edge_sharpness.len();
331        if valence == 0 {
332            mask.vertex_weight += scale;
333            return;
334        }
335        match self.scheme_type {
336            SchemeType::Catmark => {
337                // V' = (n-2)/n V + 1/n^2 Sum(E_i) + 1/n^2 Sum(F_i)
338                // with E_i the opposite end vertices of the incident edges
339                // and F_i the child vertices of the incident faces.
340                let n = valence as f32;
341                let e_weight = 1.0 / (n * n);
342                let f_weight = e_weight;
343                let v_weight =
344                    1.0 - (valence as f32) * e_weight - (vertex.num_faces as f32) * f_weight;
345                mask.vertex_weight += scale * v_weight;
346                for w in mask.edge_weights.iter_mut() {
347                    *w += scale * e_weight;
348                }
349                for w in mask.face_weights.iter_mut() {
350                    *w += scale * f_weight;
351                }
352            }
353            SchemeType::Loop => {
354                // V' = (1 - n*beta) V + beta Sum(E_i), with Warren/Loop's
355                // beta = (5/8 - (3/8 + 1/4 cos(2 pi / n))^2) / n.
356                let beta = loop_beta(valence);
357                mask.vertex_weight += scale * (1.0 - valence as f32 * beta);
358                for w in mask.edge_weights.iter_mut() {
359                    *w += scale * beta;
360                }
361            }
362            SchemeType::Bilinear => unreachable!(),
363        }
364    }
365
366    // ------------------------------------------------------------------
367    //  Limit masks
368    // ------------------------------------------------------------------
369
370    /// Mask evaluating the *limit position* of a vertex on the subdivision
371    /// surface (`Scheme::ComputeVertexLimitMask`).
372    ///
373    /// For Catmark, the per-face weights apply to the vertex of each incident
374    /// (quad) face diagonally *opposite* the subject vertex
375    /// (`face_weights_for_face_centers == false`); the caller must therefore
376    /// only invoke this on levels where all incident faces are quads (i.e.
377    /// after at least one refinement for meshes with non-quad faces).
378    pub fn compute_vertex_limit_mask(
379        &self,
380        vertex: &VertexNeighborhood<'_>,
381        mask: &mut VertexVertexMask,
382    ) {
383        let num_edges = vertex.edge_sharpness.len();
384        mask.clear_for(num_edges, vertex.num_faces);
385        mask.face_weights_for_face_centers = false;
386
387        if self.scheme_type == SchemeType::Bilinear || num_edges == 0 {
388            mask.vertex_weight = 1.0;
389            mask.face_weights.clear();
390            mask.face_weights.resize(vertex.num_faces, 0.0);
391            return;
392        }
393
394        let sharp_edges = count_sharp(vertex.edge_sharpness);
395        let rule = self
396            .crease
397            .determine_vertex_vertex_rule(vertex.sharpness, sharp_edges);
398
399        match rule {
400            Rule::Smooth | Rule::Dart => match self.scheme_type {
401                SchemeType::Catmark => {
402                    // L = (n^2 V + 4 Sum(E_i) + Sum(D_i)) / (n (n + 5)),
403                    // D_i the diagonally opposite vertex of incident quad i.
404                    let n = num_edges as f32;
405                    let f_weight = 1.0 / (n * (n + 5.0));
406                    let e_weight = 4.0 * f_weight;
407                    mask.vertex_weight = 1.0 - n * e_weight - vertex.num_faces as f32 * f_weight;
408                    mask.edge_weights.fill(e_weight);
409                    mask.face_weights.fill(f_weight);
410                }
411                SchemeType::Loop => {
412                    // L = (omega V + Sum(E_i)) / (omega + n), omega = 3/(8 beta).
413                    let beta = loop_beta(num_edges);
414                    let omega = 3.0 / (8.0 * beta);
415                    let e_weight = 1.0 / (omega + num_edges as f32);
416                    mask.vertex_weight = omega * e_weight;
417                    mask.edge_weights.fill(e_weight);
418                }
419                SchemeType::Bilinear => unreachable!(),
420            },
421            Rule::Crease => {
422                // The boundary/crease limit curve is a cubic B-spline:
423                // L = 2/3 V + 1/6 E_a + 1/6 E_b along the two sharp edges.
424                mask.vertex_weight = 2.0 / 3.0;
425                let mut assigned = 0;
426                for (i, &s) in vertex.edge_sharpness.iter().enumerate() {
427                    if Crease::is_sharp(s) && assigned < 2 {
428                        mask.edge_weights[i] = 1.0 / 6.0;
429                        assigned += 1;
430                    }
431                }
432            }
433            Rule::Corner => {
434                mask.vertex_weight = 1.0;
435            }
436            Rule::Unknown => unreachable!(),
437        }
438    }
439}
440
441#[derive(Clone, Copy)]
442enum ByLevel {
443    Parent,
444    Child,
445}
446
447fn count_sharp(sharpness: &[f32]) -> usize {
448    sharpness.iter().filter(|&&s| Crease::is_sharp(s)).count()
449}
450
451fn assign_crease_mask_for_vertex(edge_sharpness: &[f32], scale: f32, mask: &mut VertexVertexMask) {
452    // V' = 3/4 V + 1/8 (E_a + E_b) along the two sharpest edges.
453    mask.vertex_weight += scale * 0.75;
454    let mut assigned = 0;
455    for (i, &s) in edge_sharpness.iter().enumerate() {
456        if Crease::is_sharp(s) && assigned < 2 {
457            mask.edge_weights[i] += scale * 0.125;
458            assigned += 1;
459        }
460    }
461}
462
463/// Loop subdivision's `beta(n)` — the per-edge weight of the smooth
464/// vertex-vertex mask (`Sdc::Scheme<SCHEME_LOOP>`).
465fn loop_beta(valence: usize) -> f32 {
466    let inv_valence = 1.0 / valence as f32;
467    let t = 0.375 + 0.25 * (std::f32::consts::TAU * inv_valence).cos();
468    (0.625 - t * t) * inv_valence
469}
470
471#[cfg(test)]
472mod tests {
473    use super::*;
474
475    fn scheme(t: SchemeType) -> Scheme {
476        Scheme::new(t, Options::default())
477    }
478
479    #[test]
480    fn catmark_smooth_edge_mask() {
481        let s = scheme(SchemeType::Catmark);
482        let mut mask = EdgeVertexMask::default();
483        s.compute_edge_vertex_mask(
484            &EdgeNeighborhood {
485                sharpness: 0.0,
486                num_faces: 2,
487                face_vertex_counts: [4, 4],
488            },
489            &mut mask,
490        );
491        assert_eq!(mask.vertex_weights, [0.25, 0.25]);
492        assert_eq!(mask.face_weights, vec![0.25, 0.25]);
493        assert!(mask.face_weights_for_face_centers);
494    }
495
496    #[test]
497    fn loop_smooth_edge_mask() {
498        let s = scheme(SchemeType::Loop);
499        let mut mask = EdgeVertexMask::default();
500        s.compute_edge_vertex_mask(
501            &EdgeNeighborhood {
502                sharpness: 0.0,
503                num_faces: 2,
504                face_vertex_counts: [4, 4],
505            },
506            &mut mask,
507        );
508        assert_eq!(mask.vertex_weights, [0.375, 0.375]);
509        assert_eq!(mask.face_weights, vec![0.125, 0.125]);
510        assert!(!mask.face_weights_for_face_centers);
511    }
512
513    #[test]
514    fn semi_sharp_edge_mask_blends() {
515        let s = scheme(SchemeType::Catmark);
516        let mut mask = EdgeVertexMask::default();
517        s.compute_edge_vertex_mask(
518            &EdgeNeighborhood {
519                sharpness: 0.5,
520                num_faces: 2,
521                face_vertex_counts: [4, 4],
522            },
523            &mut mask,
524        );
525        // 0.5 * (0.5, 0.5) + 0.5 * (0.25, 0.25, f: 0.25, 0.25)
526        assert_eq!(mask.vertex_weights, [0.375, 0.375]);
527        assert_eq!(mask.face_weights, vec![0.125, 0.125]);
528    }
529
530    fn catmark_edge_mask(options: Options, edge: EdgeNeighborhood) -> EdgeVertexMask {
531        let mut mask = EdgeVertexMask::default();
532        Scheme::new(SchemeType::Catmark, options).compute_edge_vertex_mask(&edge, &mut mask);
533        mask
534    }
535
536    #[test]
537    fn smooth_triangle_rule_adjusts_edge_between_quad_and_triangle() {
538        let smooth = Options::default().with_triangle_subdivision(TriangleSubdivision::Smooth);
539        let mask = catmark_edge_mask(
540            smooth,
541            EdgeNeighborhood {
542                sharpness: 0.0,
543                num_faces: 2,
544                face_vertex_counts: [4, 3],
545            },
546        );
547        // f = (1/4 + 0.470) / 2 = 0.36; v = (1 - 2 f) / 2 = 0.14.
548        let f = 0.5 * (0.25 + 0.470);
549        let v = 0.5 * (1.0 - 2.0 * f);
550        assert_eq!(mask.vertex_weights, [v, v]);
551        assert_eq!(mask.face_weights, vec![f, f]);
552        assert!(mask.face_weights_for_face_centers);
553
554        // The triangle may sit on either side of the edge.
555        let flipped = catmark_edge_mask(
556            smooth,
557            EdgeNeighborhood {
558                sharpness: 0.0,
559                num_faces: 2,
560                face_vertex_counts: [3, 4],
561            },
562        );
563        assert_eq!(flipped.vertex_weights, mask.vertex_weights);
564        assert_eq!(flipped.face_weights, mask.face_weights);
565
566        // Two triangles: each face takes the full 0.470.
567        let both = catmark_edge_mask(
568            smooth,
569            EdgeNeighborhood {
570                sharpness: 0.0,
571                num_faces: 2,
572                face_vertex_counts: [3, 3],
573            },
574        );
575        let v = 0.5 * (1.0 - 2.0 * 0.470);
576        assert_eq!(both.vertex_weights, [v, v]);
577        assert_eq!(both.face_weights, vec![0.470, 0.470]);
578    }
579
580    #[test]
581    fn smooth_triangle_rule_leaves_other_edges_alone() {
582        let smooth = Options::default().with_triangle_subdivision(TriangleSubdivision::Smooth);
583
584        // No triangle: the standard weights.
585        let quads = catmark_edge_mask(
586            smooth,
587            EdgeNeighborhood {
588                sharpness: 0.0,
589                num_faces: 2,
590                face_vertex_counts: [4, 4],
591            },
592        );
593        assert_eq!(quads.vertex_weights, [0.25, 0.25]);
594        assert_eq!(quads.face_weights, vec![0.25, 0.25]);
595
596        // A smooth boundary edge of a triangle (one incident face): the
597        // rule only applies to edges with two faces.
598        let boundary = catmark_edge_mask(
599            smooth,
600            EdgeNeighborhood {
601                sharpness: 0.0,
602                num_faces: 1,
603                face_vertex_counts: [3, 0],
604            },
605        );
606        assert_eq!(boundary.vertex_weights, [0.25, 0.25]);
607        assert_eq!(boundary.face_weights, vec![0.5]);
608
609        // Under the default rule, triangles get the standard weights.
610        let catmark = catmark_edge_mask(
611            Options::default(),
612            EdgeNeighborhood {
613                sharpness: 0.0,
614                num_faces: 2,
615                face_vertex_counts: [4, 3],
616            },
617        );
618        assert_eq!(catmark.vertex_weights, [0.25, 0.25]);
619        assert_eq!(catmark.face_weights, vec![0.25, 0.25]);
620
621        // Loop ignores the option entirely.
622        let mut mask = EdgeVertexMask::default();
623        Scheme::new(SchemeType::Loop, smooth).compute_edge_vertex_mask(
624            &EdgeNeighborhood {
625                sharpness: 0.0,
626                num_faces: 2,
627                face_vertex_counts: [3, 3],
628            },
629            &mut mask,
630        );
631        assert_eq!(mask.vertex_weights, [0.375, 0.375]);
632        assert_eq!(mask.face_weights, vec![0.125, 0.125]);
633    }
634
635    #[test]
636    fn smooth_triangle_rule_blends_with_semi_sharp_crease() {
637        let smooth = Options::default().with_triangle_subdivision(TriangleSubdivision::Smooth);
638        let mask = catmark_edge_mask(
639            smooth,
640            EdgeNeighborhood {
641                sharpness: 0.5,
642                num_faces: 2,
643                face_vertex_counts: [4, 3],
644            },
645        );
646        // 0.5 * (0.5, 0.5) + 0.5 * (0.14, 0.14, f: 0.36, 0.36)
647        let f = 0.5 * (0.25 + 0.470);
648        let v = 0.5 * (1.0 - 2.0 * f);
649        assert_eq!(mask.vertex_weights, [0.5 * 0.5 + 0.5 * v; 2]);
650        assert_eq!(mask.face_weights, vec![0.5 * f; 2]);
651    }
652
653    #[test]
654    fn catmark_smooth_vertex_mask_valence_4() {
655        let s = scheme(SchemeType::Catmark);
656        let mut mask = VertexVertexMask::default();
657        let es = [0.0f32; 4];
658        s.compute_vertex_vertex_mask(
659            &VertexNeighborhood {
660                sharpness: 0.0,
661                child_sharpness: 0.0,
662                edge_sharpness: &es,
663                child_edge_sharpness: &es,
664                num_faces: 4,
665            },
666            &mut mask,
667        );
668        assert!((mask.vertex_weight - 0.5).abs() < 1e-6);
669        assert_eq!(mask.edge_weights, vec![0.0625; 4]);
670        assert_eq!(mask.face_weights, vec![0.0625; 4]);
671    }
672
673    #[test]
674    fn loop_regular_limit_mask() {
675        let s = scheme(SchemeType::Loop);
676        let mut mask = VertexVertexMask::default();
677        let es = [0.0f32; 6];
678        s.compute_vertex_limit_mask(
679            &VertexNeighborhood {
680                sharpness: 0.0,
681                child_sharpness: 0.0,
682                edge_sharpness: &es,
683                child_edge_sharpness: &es,
684                num_faces: 6,
685            },
686            &mut mask,
687        );
688        // Regular Loop limit stencil: 1/2 center, 1/12 per neighbor.
689        assert!((mask.vertex_weight - 0.5).abs() < 1e-5);
690        for w in &mask.edge_weights {
691            assert!((w - 1.0 / 12.0).abs() < 1e-5);
692        }
693    }
694
695    #[test]
696    fn catmark_regular_limit_mask_is_bspline() {
697        let s = scheme(SchemeType::Catmark);
698        let mut mask = VertexVertexMask::default();
699        let es = [0.0f32; 4];
700        s.compute_vertex_limit_mask(
701            &VertexNeighborhood {
702                sharpness: 0.0,
703                child_sharpness: 0.0,
704                edge_sharpness: &es,
705                child_edge_sharpness: &es,
706                num_faces: 4,
707            },
708            &mut mask,
709        );
710        // Bicubic B-spline evaluation at a regular vertex:
711        // 16/36 center, 4/36 edge neighbors, 1/36 diagonal neighbors.
712        assert!((mask.vertex_weight - 16.0 / 36.0).abs() < 1e-6);
713        assert_eq!(mask.edge_weights, vec![4.0 / 36.0; 4]);
714        assert_eq!(mask.face_weights, vec![1.0 / 36.0; 4]);
715    }
716}