pub use crate::geometries::subdivision::{
catmull_clark as subdivide, Cage, SubdivisionOptions as Options,
};
pub fn wanted(prim: &super::parse::UsdPrim) -> bool {
!matches!(
prim.value("subdivisionScheme").and_then(|v| v.as_str()),
Some("none")
)
}
pub fn options_of(prim: &super::parse::UsdPrim) -> Options {
let corners = prim
.value("cornerIndices")
.map(|v| v.flat_u32())
.unwrap_or_default();
let indices = prim
.value("creaseIndices")
.map(|v| v.flat_u32())
.unwrap_or_default();
let lengths = prim
.value("creaseLengths")
.map(|v| v.flat_u32())
.unwrap_or_default();
let mut creases = Vec::new();
let mut at = 0usize;
for length in &lengths {
let length = *length as usize;
if at + length > indices.len() {
break;
}
for i in at..at + length.saturating_sub(1) {
creases.push((indices[i], indices[i + 1]));
}
at += length;
}
Options {
corners,
creases,
sharp_boundary: !matches!(
prim.value("interpolateBoundary").and_then(|v| v.as_str()),
Some("none")
),
}
}
#[cfg(test)]
mod tests {
use super::*;
fn cube() -> Cage {
Cage {
positions: vec![
[-1.0, -1.0, -1.0],
[1.0, -1.0, -1.0],
[1.0, 1.0, -1.0],
[-1.0, 1.0, -1.0],
[-1.0, -1.0, 1.0],
[1.0, -1.0, 1.0],
[1.0, 1.0, 1.0],
[-1.0, 1.0, 1.0],
],
counts: vec![4; 6],
indices: vec![
0, 3, 2, 1, 4, 5, 6, 7, 0, 1, 5, 4, 2, 3, 7, 6, 0, 4, 7, 3, 1, 2, 6, 5, ],
}
}
#[test]
fn a_cube_refines_into_quads() {
let out = subdivide(cube(), 1, &Options::default());
assert_eq!(out.counts.len(), 24);
assert!(out.counts.iter().all(|c| *c == 4), "all quads");
assert_eq!(out.positions.len(), 8 + 12 + 6);
}
#[test]
fn the_corners_move_inward() {
let out = subdivide(cube(), 1, &Options::default());
let corner = out.positions[0];
let distance = (corner[0] * corner[0] + corner[1] * corner[1] + corner[2] * corner[2]).sqrt();
let original = 3f32.sqrt();
assert!(
distance < original * 0.9,
"corner stayed at {distance}, was {original}"
);
assert!((corner[0].abs() - 5.0 / 9.0).abs() < 1e-5, "{corner:?}");
}
#[test]
fn levels_compound() {
let one = subdivide(cube(), 1, &Options::default());
let two = subdivide(cube(), 2, &Options::default());
assert_eq!(two.counts.len(), one.counts.len() * 4);
}
#[test]
fn a_corner_is_held() {
let out = subdivide(
cube(),
1,
&Options {
corners: vec![0],
..Default::default()
},
);
assert_eq!(out.positions[0], [-1.0, -1.0, -1.0]);
assert_ne!(out.positions[1], [1.0, -1.0, -1.0]);
}
#[test]
fn a_held_boundary_keeps_its_edge() {
let quad = Cage {
positions: vec![
[0.0, 0.0, 0.0],
[1.0, 0.0, 0.0],
[1.0, 1.0, 0.0],
[0.0, 1.0, 0.0],
],
counts: vec![4],
indices: vec![0, 1, 2, 3],
};
let out = subdivide(
quad,
1,
&Options {
sharp_boundary: true,
..Default::default()
},
);
assert_eq!(out.positions[0], [0.0, 0.0, 0.0]);
assert_eq!(out.positions[2], [1.0, 1.0, 0.0]);
assert!(out.positions.contains(&[0.5, 0.0, 0.0]));
}
#[test]
fn level_zero_changes_nothing() {
let out = subdivide(cube(), 0, &Options::default());
assert_eq!(out.positions.len(), 8);
assert_eq!(out.counts.len(), 6);
}
#[test]
fn silence_means_subdivide() {
use super::super::parse::parse;
let layer = parse(
r#"#usda 1.0
def Mesh "Quiet"
{
int[] faceVertexCounts = [4]
int[] faceVertexIndices = [0, 1, 2, 3]
point3f[] points = [(0, 0, 0), (1, 0, 0), (1, 1, 0), (0, 1, 0)]
}
def Mesh "Polygonal"
{
uniform token subdivisionScheme = "none"
int[] faceVertexCounts = [4]
int[] faceVertexIndices = [0, 1, 2, 3]
point3f[] points = [(0, 0, 0), (1, 0, 0), (1, 1, 0), (0, 1, 0)]
}
"#,
)
.unwrap();
assert!(wanted(layer.prim_at("/Quiet").unwrap()), "the default is catmullClark");
assert!(!wanted(layer.prim_at("/Polygonal").unwrap()));
}
#[test]
fn creases_are_read_as_runs() {
use super::super::parse::parse;
let layer = parse(
r#"#usda 1.0
def Mesh "M"
{
int[] creaseIndices = [0, 1, 2, 4, 5]
int[] creaseLengths = [3, 2]
float[] creaseSharpnesses = [10, 10]
int[] cornerIndices = [7]
int[] faceVertexCounts = [4]
int[] faceVertexIndices = [0, 1, 2, 3]
point3f[] points = [(0, 0, 0), (1, 0, 0), (1, 1, 0), (0, 1, 0)]
}
"#,
)
.unwrap();
let options = options_of(layer.prim_at("/M").unwrap());
assert_eq!(options.creases, vec![(0, 1), (1, 2), (4, 5)]);
assert_eq!(options.corners, vec![7]);
assert!(options.sharp_boundary, "edgeAndCorner is the default");
}
#[test]
fn a_loaded_mesh_refines_when_asked() {
use super::super::parse::parse;
use super::super::scene::{mesh_geometry_at, mesh_geometry};
let layer = parse(
r#"#usda 1.0
def Mesh "Cube"
{
int[] faceVertexCounts = [4, 4, 4, 4, 4, 4]
int[] faceVertexIndices = [
0, 3, 2, 1,
4, 5, 6, 7,
0, 1, 5, 4,
2, 3, 7, 6,
0, 4, 7, 3,
1, 2, 6, 5
]
point3f[] points = [
(-1, -1, -1), (1, -1, -1), (1, 1, -1), (-1, 1, -1),
(-1, -1, 1), (1, -1, 1), (1, 1, 1), (-1, 1, 1)
]
}
"#,
)
.unwrap();
let cube = layer.prim_at("/Cube").unwrap();
let cage = mesh_geometry(cube).expect("the cage");
assert_eq!(cage.get_attribute("position").unwrap().count(), 8);
let refined = mesh_geometry_at(cube, 2).expect("refined");
assert!(
refined.get_attribute("position").unwrap().count() > 8 * 4,
"two levels should be much denser, got {}",
refined.get_attribute("position").unwrap().count()
);
let corner = &refined.get_attribute("position").unwrap().array[0..3];
let distance = (corner[0].powi(2) + corner[1].powi(2) + corner[2].powi(2)).sqrt();
assert!(distance < 3f32.sqrt() * 0.9, "corner did not move: {distance}");
let layer = parse(
r#"#usda 1.0
def Mesh "Flat"
{
uniform token subdivisionScheme = "none"
int[] faceVertexCounts = [4]
int[] faceVertexIndices = [0, 1, 2, 3]
point3f[] points = [(0, 0, 0), (1, 0, 0), (1, 1, 0), (0, 1, 0)]
}
"#,
)
.unwrap();
let flat = layer.prim_at("/Flat").unwrap();
assert_eq!(
mesh_geometry_at(flat, 3).unwrap().get_attribute("position").unwrap().count(),
mesh_geometry(flat).unwrap().get_attribute("position").unwrap().count()
);
}
#[test]
fn nonsense_is_survivable() {
let empty = subdivide(
Cage {
positions: Vec::new(),
counts: Vec::new(),
indices: Vec::new(),
},
2,
&Options::default(),
);
assert!(empty.positions.is_empty());
let short = subdivide(
Cage {
positions: vec![[0.0; 3]; 3],
counts: vec![4, 4],
indices: vec![0, 1, 2],
},
1,
&Options::default(),
);
assert!(short.counts.len() <= 4);
}
}