Skip to main content

opensubdiv_rs/far/
topology_descriptor.rs

1//! Borrowed mesh-topology description (port of
2//! `opensubdiv/far/topologyDescriptor.h`).
3
4use crate::Index;
5
6/// Description of one face-varying channel for a [`TopologyDescriptor`]
7/// (`Far::TopologyDescriptor::FVarChannel`).
8///
9/// A face-varying channel assigns one *value index* to every face-vertex
10/// (corner) of the mesh, allowing data such as UVs to be discontinuous
11/// ("seamed") across edges: two faces sharing an edge may refer to different
12/// values at the shared vertices.
13#[derive(Debug, Clone, Copy, Default)]
14pub struct FVarChannelDescriptor<'a> {
15    /// Number of distinct values in the channel.
16    pub num_values: usize,
17    /// One value index per face-vertex, flattened in the same order as
18    /// [`TopologyDescriptor::vert_indices_per_face`].
19    pub value_indices: &'a [Index],
20}
21
22impl<'a> FVarChannelDescriptor<'a> {
23    /// Describe a channel with `num_values` distinct values, indexed once per
24    /// face-vertex by `value_indices`.
25    pub fn new(num_values: usize, value_indices: &'a [Index]) -> Self {
26        Self {
27            num_values,
28            value_indices,
29        }
30    }
31}
32
33/// A simple reference to raw topology data for use with
34/// [`TopologyRefinerFactory`](super::TopologyRefinerFactory) — the port of
35/// `Far::TopologyDescriptor`.
36///
37/// All slices are borrowed from the caller; nothing is copied until the
38/// factory builds the refiner. Only `num_vertices`, `num_verts_per_face` and
39/// `vert_indices_per_face` are required; creases, corners and holes are
40/// optional.
41#[derive(Debug, Clone, Copy, Default)]
42pub struct TopologyDescriptor<'a> {
43    /// Number of vertices in the mesh.
44    pub num_vertices: usize,
45    /// Number of vertices of each face.
46    pub num_verts_per_face: &'a [usize],
47    /// Flattened, per-face vertex indices, in winding order.
48    pub vert_indices_per_face: &'a [Index],
49
50    /// Pairs of vertex indices identifying creased edges.
51    pub crease_vertex_index_pairs: &'a [[Index; 2]],
52    /// Sharpness of each creased edge (parallel to
53    /// `crease_vertex_index_pairs`).
54    pub crease_weights: &'a [f32],
55
56    /// Indices of sharpened (corner) vertices.
57    pub corner_vertex_indices: &'a [Index],
58    /// Sharpness of each corner vertex (parallel to
59    /// `corner_vertex_indices`).
60    pub corner_weights: &'a [f32],
61
62    /// Indices of faces tagged as holes.
63    pub hole_indices: &'a [Index],
64
65    /// Face-varying channels (UVs, per-corner colors, …).
66    pub fvar_channels: &'a [FVarChannelDescriptor<'a>],
67}
68
69impl<'a> TopologyDescriptor<'a> {
70    /// Create a descriptor from the required topology: the number of
71    /// vertices, the size of each face, and the flattened face-vertex list.
72    pub fn new(
73        num_vertices: usize,
74        num_verts_per_face: &'a [usize],
75        vert_indices_per_face: &'a [Index],
76    ) -> Self {
77        TopologyDescriptor {
78            num_vertices,
79            num_verts_per_face,
80            vert_indices_per_face,
81            ..Default::default()
82        }
83    }
84
85    /// Builder-style setter for creased edges.
86    pub fn with_creases(mut self, vertex_pairs: &'a [[Index; 2]], weights: &'a [f32]) -> Self {
87        self.crease_vertex_index_pairs = vertex_pairs;
88        self.crease_weights = weights;
89        self
90    }
91
92    /// Builder-style setter for corner vertices.
93    pub fn with_corners(mut self, vertices: &'a [Index], weights: &'a [f32]) -> Self {
94        self.corner_vertex_indices = vertices;
95        self.corner_weights = weights;
96        self
97    }
98
99    /// Builder-style setter for hole faces.
100    pub fn with_holes(mut self, hole_indices: &'a [Index]) -> Self {
101        self.hole_indices = hole_indices;
102        self
103    }
104
105    /// Builder-style setter for face-varying channels.
106    pub fn with_fvar_channels(mut self, channels: &'a [FVarChannelDescriptor<'a>]) -> Self {
107        self.fvar_channels = channels;
108        self
109    }
110}