opensubdiv_rs/far/ptex.rs
1//! Ptex face indexing (port of `opensubdiv/far/ptexIndices.h`).
2
3use super::topology_refiner::TopologyRefiner;
4use crate::Index;
5
6/// Mapping between base-level faces and *ptex* face indices
7/// (`Far::PtexIndices`).
8///
9/// The ptex convention parameterizes the subdivision surface by the
10/// scheme's regular face domain: a *regular* base face (a quadrilateral
11/// for the quad-split schemes, a triangle for Loop) maps to a single ptex
12/// face, while an N-sided base face of a quad-split scheme maps to N ptex
13/// faces — one per corner, corresponding to its N quadrilateral child
14/// faces at refinement level 1.
15#[derive(Debug, Clone)]
16pub struct PtexIndices {
17 /// Ptex index of the first ptex face of each base face (+ total).
18 offsets: Vec<u32>,
19 /// Reverse map: base face of each ptex face.
20 base_faces: Vec<Index>,
21 /// Reverse map: corner of the base face each ptex face covers
22 /// (always 0 for quad base faces).
23 corners: Vec<u16>,
24}
25
26impl PtexIndices {
27 /// Compute the ptex-face indexing for the base level of `refiner`
28 /// (`PtexIndices::PtexIndices`).
29 pub fn new(refiner: &TopologyRefiner) -> Self {
30 let base = refiner.level(0);
31 let regular_size = refiner.scheme_type().regular_face_size();
32 let ptex_count = |f: usize| {
33 let size = base.face_vertices(f).len();
34 if size == regular_size {
35 1
36 } else {
37 size
38 }
39 };
40 let num_ptex = (0..base.num_faces()).map(ptex_count).sum();
41 let mut offsets = Vec::with_capacity(base.num_faces() + 1);
42 let mut base_faces = Vec::with_capacity(num_ptex);
43 let mut corners = Vec::with_capacity(num_ptex);
44 offsets.push(0);
45 for f in 0..base.num_faces() {
46 for k in 0..ptex_count(f) {
47 base_faces.push(f as Index);
48 corners.push(k as u16);
49 }
50 offsets.push(base_faces.len() as u32);
51 }
52 Self {
53 offsets,
54 base_faces,
55 corners,
56 }
57 }
58
59 /// The total number of ptex faces (`GetNumFaces`).
60 pub fn num_faces(&self) -> usize {
61 self.base_faces.len()
62 }
63
64 /// The ptex index of the first ptex face of base face `face`
65 /// (`GetFaceId`).
66 pub fn face_id(&self, face: usize) -> Index {
67 self.offsets[face]
68 }
69
70 /// The number of ptex faces of base face `face`: 1 for regular faces
71 /// (quads, or triangles under Loop), N for N-gons.
72 pub fn face_ptex_count(&self, face: usize) -> usize {
73 (self.offsets[face + 1] - self.offsets[face]) as usize
74 }
75
76 /// The base face covered by ptex face `ptex_face`.
77 pub fn base_face(&self, ptex_face: usize) -> Index {
78 self.base_faces[ptex_face]
79 }
80
81 /// The corner of the base face covered by ptex face `ptex_face` (always
82 /// 0 for regular base faces, which map to a single ptex face).
83 pub fn base_face_corner(&self, ptex_face: usize) -> usize {
84 self.corners[ptex_face] as usize
85 }
86}