mirror of
https://github.com/bherbruck/solidcraft.git
synced 2026-11-18 22:50:56 +00:00
f2051e5a3a
* Viewport: zebra bars along a direction with a white share, a map coloured by vertex values, and two-sided PBR materials
- Zebra { stripes, direction, width }: the reflected bars run along `direction` (the stripe follows
the reflected ray across them) and each is white for `width` of its period (0.05..0.95). The
engine passes +Z and 0.5, so SolidCraft's picture does not change; the straight-ahead white of
#2153 stays.
- Values { low, high }: a mesh coloured by a value at each vertex on the curvature colours' ramp
(low flat, high tight); SceneMesh::values carries them. A triangle vertex record grows one word
(TRI_SIZE 32): the value's bits after the palette index (TRI_COLOR_AT 24, TRI_VALUE_AT 28),
NO_VALUE for none; BlockBuilder::triangle_values and GpuScene::{tri,open_tri,glass_tri}_value
write them. Built triangles read it in vs_tri; display-plan vertices have their own entry,
vs_tri_planned, with none. A face without values in Values is coloured as the curvature map at
1 mm.
- Material::two_sided: fs_pbr lights a face's back as its front where the eye sees the back (an
open sheet's other side). Off by default, so solid bodies render as before.
Tests: the zebra's bars along X turn the stripes a quarter turn on a sphere against upright bars,
and a white share of 0.8 shows more white than 0.2 (gpu capture); a quad with values -1..1 ramps
flat, middle, tight; a PBR back face black one-sided and lit two-sided; a triangle's values after
its colour and NO_VALUE without. Tests that read a vertex's colour read it at 24..28.
Not built or run locally (CI is the check).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* Viewport analysis: rustfmt
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* Viewport analysis tests: the 32-byte vertex where tests counted 28, and the analysis pictures read where the camera draws
- The tests that counted triangle bytes by 28 (the cut preview's x-ray tool, the origin planes'
x-ray, the render preview's geometry size message) count by TRI_SIZE: the vertex record is 32
bytes since the value word (192 MiB for 2^21 triangles, 3 x 6 vertices a plane).
- The analysis shader's literal list loses the +Z bar direction, which now comes from the uniform.
- The analysis captures found the quad and the sphere by an assumed framing (40 mm on 200 px);
the front camera at distance 62.5 shows more, so the values test read background at its
sample points and the white-share test counted the light background as white. They now place
their samples with the camera's own projection (Camera::to_screen) and count on the sphere only.
Not built or run locally (CI is the check).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
---------
Co-authored-by: jyviko <2501648+jyviko@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
190 lines
7.8 KiB
Rust
190 lines
7.8 KiB
Rust
//! The surface analysis hooks a surface modeller drives, drawn headless with the viewport's
|
|
//! pipelines: the zebra's bars along a direction of the caller's (+Z stands them upright, as the
|
|
//! picture always was; +X lays them down, turning the stripes a quarter turn) and its white
|
|
//! share; and a mesh coloured by its vertices' values. Skipped, with the reason printed, on a
|
|
//! machine without a GPU adapter (`SOLIDCRAFT_REQUIRE_GPU=1` makes that a failure).
|
|
|
|
use solidcore_record::Vec3;
|
|
use solidcraft_color::AnalysisColors;
|
|
use solidcraft_engine::geom::Mesh;
|
|
use solidcraft_viewport::{AnalysisMode, Camera, Rgb, Scene, SceneAnalysis, SceneMesh, StandardView};
|
|
use solidcraft_viewport_gpu::GpuError;
|
|
use solidcraft_viewport_gpu::capture::{self, Capturer};
|
|
use std::sync::Arc;
|
|
|
|
fn capturer() -> Option<&'static Capturer> {
|
|
match capture::shared(solidcraft_gpu::env::policy().unwrap_or_else(|e| panic!("{e}"))) {
|
|
Ok(c) => Some(c),
|
|
Err(e @ GpuError::NoAdapter(_)) if std::env::var_os("SOLIDCRAFT_REQUIRE_GPU").is_none() => {
|
|
eprintln!("skipping: {e}");
|
|
None
|
|
}
|
|
Err(e) => panic!("{e}"),
|
|
}
|
|
}
|
|
|
|
const SIDE: u32 = 200;
|
|
|
|
/// A sphere of radius 15 about the origin, 48 rings of 96 quads.
|
|
fn sphere() -> Mesh {
|
|
let (rings, around) = (48u32, 96u32);
|
|
let mut positions = Vec::new();
|
|
let mut normals = Vec::new();
|
|
for i in 0..=rings {
|
|
let lat = std::f64::consts::PI * (f64::from(i) / f64::from(rings) - 0.5);
|
|
for j in 0..=around {
|
|
let lon = std::f64::consts::TAU * f64::from(j) / f64::from(around);
|
|
let n = Vec3::new(lat.cos() * lon.cos(), lat.cos() * lon.sin(), lat.sin());
|
|
normals.push(n);
|
|
positions.push(Vec3::new(15.0 * n.x, 15.0 * n.y, 15.0 * n.z));
|
|
}
|
|
}
|
|
let at = |i: u32, j: u32| i * (around + 1) + j;
|
|
let mut triangles = Vec::new();
|
|
for i in 0..rings {
|
|
for j in 0..around {
|
|
triangles.push([at(i, j), at(i, j + 1), at(i + 1, j + 1)]);
|
|
triangles.push([at(i, j), at(i + 1, j + 1), at(i + 1, j)]);
|
|
}
|
|
}
|
|
let tri_face = vec![0; triangles.len()];
|
|
Mesh { positions, normals, triangles, tri_face, ..Default::default() }
|
|
}
|
|
|
|
/// The design's radius the pictures are framed for.
|
|
const RADIUS: f64 = 40.0;
|
|
|
|
/// The front view the pictures are taken from.
|
|
fn camera() -> Camera {
|
|
let mut cam = Camera { distance: 62.5, fov: 0.0, ..Default::default() };
|
|
cam.set_view(StandardView::Front);
|
|
cam
|
|
}
|
|
|
|
/// Where world point `p` lands in the picture, in pixels (the camera's own projection: the
|
|
/// tests find what they look at where it is drawn, not by an assumed framing).
|
|
fn screen(p: Vec3) -> (f64, f64) {
|
|
let (x, y, _) = camera().to_screen(p, f64::from(SIDE), f64::from(SIDE), RADIUS).unwrap();
|
|
(x, y)
|
|
}
|
|
|
|
/// The sphere's centre and radius in the picture, in pixels.
|
|
fn disc() -> (f64, f64, f64) {
|
|
let (cx, cy) = screen(Vec3::new(0.0, 0.0, 0.0));
|
|
let (rx, _) = screen(Vec3::new(15.0, 0.0, 0.0));
|
|
(cx, cy, (rx - cx).abs())
|
|
}
|
|
|
|
/// `mesh` (with `values` at its positions) shaded by `mode`, seen from the front.
|
|
fn picture(c: &Capturer, mesh: Mesh, values: Option<Vec<f32>>, mode: AnalysisMode) -> Vec<u8> {
|
|
let body = SceneMesh { values: values.map(Arc::new), ..SceneMesh::solid(Arc::new(mesh), Rgb(160, 165, 175)) };
|
|
let scene =
|
|
Scene { meshes: vec![body], radius: RADIUS, analysis: Some(SceneAnalysis { mode, colors: AnalysisColors::DEFAULT }), ..Default::default() };
|
|
c.rgba(&scene, &camera(), SIDE, SIDE).unwrap()
|
|
}
|
|
|
|
fn pixel(img: &[u8], x: u32, y: u32) -> [u8; 4] {
|
|
let at = ((y * SIDE + x) * 4) as usize;
|
|
img.get(at..at + 4).and_then(|p| p.try_into().ok()).unwrap_or_default()
|
|
}
|
|
|
|
/// 1 for a white stripe's pixel, 0 for a black one's, None between.
|
|
fn shade(p: [u8; 4]) -> Option<u8> {
|
|
if p[0] > 225 && p[1] > 225 && p[2] > 225 {
|
|
Some(1)
|
|
} else if p[0] < 40 && p[1] < 40 && p[2] < 40 {
|
|
Some(0)
|
|
} else {
|
|
None
|
|
}
|
|
}
|
|
|
|
/// How often the stripes change along the sphere's middle row (`across`) or column, within it.
|
|
fn changes(img: &[u8], across: bool) -> usize {
|
|
let (cx, cy, r) = disc();
|
|
let reach = 0.9 * r;
|
|
let mut last = None;
|
|
let mut count = 0;
|
|
for k in 0..(2.0 * reach) as u32 {
|
|
let at = f64::from(k) - reach;
|
|
let (x, y) = if across { (cx + at, cy) } else { (cx, cy + at) };
|
|
let p = pixel(img, x.round() as u32, y.round() as u32);
|
|
if let Some(s) = shade(p) {
|
|
if last.is_some_and(|l| l != s) {
|
|
count += 1;
|
|
}
|
|
last = Some(s);
|
|
}
|
|
}
|
|
count
|
|
}
|
|
|
|
fn zebra(direction: Vec3, width: f64) -> AnalysisMode {
|
|
AnalysisMode::Zebra { stripes: 8.0, direction, width }
|
|
}
|
|
|
|
/// Upright bars (+Z, the picture as it always was) stripe the sphere across the view and not
|
|
/// down its middle; bars along X turn the stripes a quarter turn.
|
|
#[test]
|
|
fn the_bars_direction_turns_the_stripes() {
|
|
let Some(c) = capturer() else { return };
|
|
let upright = picture(c, sphere(), None, zebra(Vec3::new(0.0, 0.0, 1.0), 0.5));
|
|
let (row, column) = (changes(&upright, true), changes(&upright, false));
|
|
assert!(row >= 4 && row > 2 * column, "upright bars: {row} changes across, {column} down");
|
|
let lying = picture(c, sphere(), None, zebra(Vec3::new(1.0, 0.0, 0.0), 0.5));
|
|
let (row, column) = (changes(&lying, true), changes(&lying, false));
|
|
assert!(column >= 4 && column > 2 * row, "bars along X: {row} changes across, {column} down");
|
|
}
|
|
|
|
/// The white share of a period: 0.8 shows more white than 0.2.
|
|
#[test]
|
|
fn the_white_share_widens_the_white_stripes() {
|
|
let Some(c) = capturer() else { return };
|
|
// White pixels on the sphere only: the background is no stripe (its light part counted
|
|
// as white before).
|
|
let (cx, cy, r) = disc();
|
|
let white = |width: f64| {
|
|
let img = picture(c, sphere(), None, zebra(Vec3::new(0.0, 0.0, 1.0), width));
|
|
let mut count = 0;
|
|
for y in 0..SIDE {
|
|
for x in 0..SIDE {
|
|
let (dx, dy) = (f64::from(x) - cx, f64::from(y) - cy);
|
|
if dx * dx + dy * dy < 0.81 * r * r && shade(pixel(&img, x, y)) == Some(1) {
|
|
count += 1;
|
|
}
|
|
}
|
|
}
|
|
count
|
|
};
|
|
let (narrow, wide) = (white(0.2), white(0.8));
|
|
assert!(wide > 2 * narrow, "white pixels: {narrow} at 0.2, {wide} at 0.8");
|
|
}
|
|
|
|
/// A 30 mm square facing the view, its values -1 at the left, 1 at the right (0 down the
|
|
/// middle), coloured from -1 to 1: flat's colour at the left, the middle colour in the middle,
|
|
/// tight's at the right.
|
|
#[test]
|
|
fn values_ramp_through_the_curvature_colours() {
|
|
let Some(c) = capturer() else { return };
|
|
let quad = Mesh {
|
|
positions: vec![Vec3::new(-15.0, 0.0, -15.0), Vec3::new(15.0, 0.0, -15.0), Vec3::new(15.0, 0.0, 15.0), Vec3::new(-15.0, 0.0, 15.0)],
|
|
normals: vec![Vec3::new(0.0, -1.0, 0.0); 4],
|
|
triangles: vec![[0, 1, 2], [0, 2, 3]],
|
|
tri_face: vec![0, 0],
|
|
..Default::default()
|
|
};
|
|
let img = picture(c, quad, Some(vec![-1.0, 1.0, 1.0, -1.0]), AnalysisMode::Values { low: -1.0, high: 1.0 });
|
|
let [flat, middle, tight] = AnalysisColors::DEFAULT.curvature.map(|c| c.map(|v| (v * 255.0).round() as u8));
|
|
let near = |p: [u8; 4], c: [u8; 3]| (0..3).all(|k| (i32::from(p[k]) - i32::from(c[k])).abs() < 60);
|
|
// A tenth of the way in from each side (values -0.8 and 0.8: a tenth of the ramp from its
|
|
// ends), and the middle, where the camera draws them.
|
|
let at = |x: f64| {
|
|
let (px, py) = screen(Vec3::new(x, 0.0, 0.0));
|
|
pixel(&img, px.round() as u32, py.round() as u32)
|
|
};
|
|
let (left, mid, right) = (at(-12.0), at(0.0), at(12.0));
|
|
assert!(near(left, flat), "left {left:?}, flat {flat:?}");
|
|
assert!(near(mid, middle), "middle {mid:?}, {middle:?}");
|
|
assert!(near(right, tight), "right {right:?}, tight {tight:?}");
|
|
}
|