forked from 1b-agent/opencadstudio-upstream
Merge PR #1104: improve snapping and drawing input handling
This commit is contained in:
commit
73a02b06e2
5 files changed
+1160
-16
No files matched your search
@@ -568,6 +568,20 @@ struct MeshSurfaceParams {
|
||||
flags: [u32; 4],
|
||||
}
|
||||
|
||||
/// Downscale a decoded RGBA buffer so neither dimension exceeds `limit`,
|
||||
/// preserving aspect ratio (scaled by whichever dimension overshoots more).
|
||||
/// `None` only for genuinely malformed input (`rgba`'s length doesn't match
|
||||
/// `width * height * 4`) — the caller falls back to the 1x1 material color
|
||||
/// in that case, same as it already does for a missing image entirely.
|
||||
fn downscale_rgba_to_limit(width: u32, height: u32, rgba: &[u8], limit: u32) -> Option<(u32, u32, Vec<u8>)> {
|
||||
let buffer = image::RgbaImage::from_raw(width, height, rgba.to_vec())?;
|
||||
let scale = (limit as f64 / width.max(height) as f64).min(1.0);
|
||||
let new_width = ((width as f64 * scale).round() as u32).clamp(1, limit);
|
||||
let new_height = ((height as f64 * scale).round() as u32).clamp(1, limit);
|
||||
let resized = image::imageops::resize(&buffer, new_width, new_height, image::imageops::FilterType::Triangle);
|
||||
Some((new_width, new_height, resized.into_raw()))
|
||||
}
|
||||
|
||||
fn upload_rgba_texture(
|
||||
device: &wgpu::Device,
|
||||
queue: &wgpu::Queue,
|
||||
@@ -576,9 +590,27 @@ fn upload_rgba_texture(
|
||||
fallback: [u8; 4],
|
||||
srgb: bool,
|
||||
) -> wgpu::TextureView {
|
||||
let (width, height, pixels) = image.map_or((1, 1, fallback.as_slice()), |image| {
|
||||
(image.width, image.height, image.rgba.as_slice())
|
||||
});
|
||||
// A material map's decoded dimensions come straight from the source file
|
||||
// (`material_model::load_map_image`), uncapped — an oversized image would
|
||||
// otherwise fail `wgpu` texture-size validation and leave the material
|
||||
// unusable (audit: raster-image rendering already tiles to this same
|
||||
// limit in `image_gpu.rs`; a mesh material sampled by UV can't use that
|
||||
// per-quad tiling trick, so downscale instead of tiling).
|
||||
let limit = device.limits().max_texture_dimension_2d.max(1);
|
||||
let resized;
|
||||
let (width, height, pixels): (u32, u32, &[u8]) = match image {
|
||||
Some(image) if image.width > limit || image.height > limit => {
|
||||
match downscale_rgba_to_limit(image.width, image.height, &image.rgba, limit) {
|
||||
Some((w, h, buf)) => {
|
||||
resized = buf;
|
||||
(w, h, resized.as_slice())
|
||||
}
|
||||
None => (1, 1, fallback.as_slice()),
|
||||
}
|
||||
}
|
||||
Some(image) => (image.width, image.height, image.rgba.as_slice()),
|
||||
None => (1, 1, fallback.as_slice()),
|
||||
};
|
||||
let texture = device.create_texture(&wgpu::TextureDescriptor {
|
||||
label: Some(label),
|
||||
size: wgpu::Extent3d {
|
||||
@@ -2426,3 +2458,40 @@ pub fn build_mesh_batch_filtered(
|
||||
}
|
||||
(chunks, total_tris)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod texture_limit_tests {
|
||||
use super::downscale_rgba_to_limit;
|
||||
|
||||
#[test]
|
||||
fn downscale_keeps_both_dimensions_within_the_limit() {
|
||||
// A wide-and-short image: the limit is only crossed by width, but
|
||||
// both dimensions must come out <= limit and aspect ratio preserved.
|
||||
let (w, h) = (500u32, 10u32);
|
||||
let rgba = vec![0u8; (w * h * 4) as usize];
|
||||
let (new_w, new_h, buf) = downscale_rgba_to_limit(w, h, &rgba, 200).expect("well-formed input must resize");
|
||||
assert!(new_w <= 200 && new_h <= 200, "both dimensions must respect the limit, got {new_w}x{new_h}");
|
||||
assert_eq!(buf.len(), (new_w * new_h * 4) as usize, "the returned buffer must match its reported dimensions");
|
||||
// Aspect ratio 50:1 should be preserved within rounding.
|
||||
let ratio = new_w as f64 / new_h as f64;
|
||||
assert!((ratio - 50.0).abs() < 1.0, "expected aspect ratio near 50:1, got {ratio}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn downscale_handles_a_square_image_over_the_limit() {
|
||||
let (w, h) = (300u32, 300u32);
|
||||
let rgba = vec![255u8; (w * h * 4) as usize];
|
||||
let (new_w, new_h, buf) = downscale_rgba_to_limit(w, h, &rgba, 128).expect("well-formed input must resize");
|
||||
assert_eq!(new_w, 128);
|
||||
assert_eq!(new_h, 128);
|
||||
assert_eq!(buf.len(), (128 * 128 * 4) as usize);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn downscale_rejects_a_buffer_whose_length_does_not_match_its_dimensions() {
|
||||
// Corrupt/mismatched input (audit's "validate decoded byte length" ask):
|
||||
// must fail cleanly, not panic or silently misread the buffer.
|
||||
let rgba = vec![0u8; 10];
|
||||
assert!(downscale_rgba_to_limit(1000, 1000, &rgba, 512).is_none());
|
||||
}
|
||||
}
|
||||
+121
-4
@@ -7,7 +7,7 @@
|
||||
//! producing their normal wire, hatch, image, wipeout, or mesh model.
|
||||
|
||||
use acadrust::entities::Insert;
|
||||
use acadrust::types::{Color, Transform, Vector3};
|
||||
use acadrust::types::{Color, Matrix3, Matrix4, Transform, Vector3};
|
||||
use acadrust::{CadDocument, EntityType, Handle};
|
||||
use rustc_hash::{FxHashMap, FxHashSet};
|
||||
|
||||
@@ -1042,11 +1042,34 @@ pub fn document_block_uses(document: &CadDocument) -> Vec<BlockUse> {
|
||||
uses
|
||||
}
|
||||
|
||||
/// A MINSERT's row/column counts are parsed `u16`s whose unchecked product
|
||||
/// (`Insert::instance_count`) would otherwise drive this function's
|
||||
/// allocation and a full per-instance render-graph walk after it — a corrupt
|
||||
/// or adversarial 65535x65535 pair requests over four billion instances (tens
|
||||
/// of GiB) long before any GPU-side chunking limit could help. The primary
|
||||
/// defense is the load-time filter in `io::is_entity_corrupt` (kept in sync
|
||||
/// with this constant), which drops such an insert before it ever reaches a
|
||||
/// `Scene`; this is the failsafe for an insert built or edited some other way
|
||||
/// (API/scripting) that skips that path.
|
||||
const MAX_MINSERT_INSTANCES: usize = 20_000;
|
||||
|
||||
pub fn array_offsets(insert: &Insert) -> Vec<[f64; 3]> {
|
||||
if !insert.is_minsert() {
|
||||
return vec![[0.0; 3]];
|
||||
}
|
||||
let mut offsets = Vec::with_capacity(insert.instance_count());
|
||||
let instance_count = insert.instance_count();
|
||||
if instance_count > MAX_MINSERT_INSTANCES {
|
||||
eprintln!(
|
||||
"MINSERT '{}' requests {} instances ({}x{}), exceeding the {} budget; rendering a single instance instead",
|
||||
insert.block_name,
|
||||
instance_count,
|
||||
insert.row_count,
|
||||
insert.column_count,
|
||||
MAX_MINSERT_INSTANCES
|
||||
);
|
||||
return vec![[0.0; 3]];
|
||||
}
|
||||
let mut offsets = Vec::with_capacity(instance_count);
|
||||
for row in 0..insert.row_count {
|
||||
for column in 0..insert.column_count {
|
||||
offsets.push([
|
||||
@@ -1070,8 +1093,20 @@ pub fn insert_instance_transform(
|
||||
if offset == [0.0; 3] {
|
||||
transform
|
||||
} else {
|
||||
Transform::from_translation(Vector3::new(offset[0], offset[1], offset[2]))
|
||||
.then(&transform)
|
||||
// Row/column spacing is defined in the insert's rotated/OCS-oriented
|
||||
// space but is NOT subject to the block's x/y/z scale — this matches
|
||||
// the pinned CAD model's own `Insert::array_points`, which rotates
|
||||
// the offset but never scales it. Composing the raw offset ahead of
|
||||
// the full (scaled) transform, as this used to do, let the block
|
||||
// scale multiply the spacing too (e.g. an x-scale-2 insert with
|
||||
// column spacing 10 would render 20 units between columns). Instead,
|
||||
// orient the offset by rotation + OCS only, then translate the
|
||||
// already-composed (scaled) instance transform by that world-space
|
||||
// vector, applied after — never scaled.
|
||||
let orientation = Transform::from_matrix(Matrix4::rotation_z(insert.rotation))
|
||||
.then(&Transform::from_matrix(Matrix4::from_matrix3(Matrix3::arbitrary_axis(insert.normal))));
|
||||
let world_offset = orientation.apply_rotation(Vector3::new(offset[0], offset[1], offset[2]));
|
||||
transform.then(&Transform::from_translation(world_offset))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1163,4 +1198,86 @@ mod tests {
|
||||
assert!((from_insert.x - 12.0).abs() < 1.0e-9);
|
||||
assert!((applied.x - 11.0).abs() < 1.0e-9);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn array_offsets_clamps_pathological_minsert_counts() {
|
||||
// A corrupt or adversarial row/column pair (parsed as u16, so this is
|
||||
// reachable from a malformed file) must not drive a multi-billion-
|
||||
// element allocation; the primary defense is the load-time filter in
|
||||
// `io::is_entity_corrupt`, this is the render-graph-side failsafe.
|
||||
let mut insert = Insert::new("BLOCK", Vector3::ZERO);
|
||||
insert.row_count = u16::MAX;
|
||||
insert.column_count = u16::MAX;
|
||||
insert.row_spacing = 1.0;
|
||||
insert.column_spacing = 1.0;
|
||||
let offsets = array_offsets(&insert);
|
||||
assert_eq!(offsets, vec![[0.0; 3]], "pathological instance count must clamp to a single instance");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn array_offsets_is_unaffected_for_a_reasonable_minsert() {
|
||||
let mut insert = Insert::new("BLOCK", Vector3::ZERO);
|
||||
insert.row_count = 3;
|
||||
insert.column_count = 4;
|
||||
insert.row_spacing = 5.0;
|
||||
insert.column_spacing = 2.0;
|
||||
let offsets = array_offsets(&insert);
|
||||
assert_eq!(offsets.len(), 12);
|
||||
assert_eq!(offsets[0], [0.0, 0.0, 0.0]);
|
||||
assert_eq!(offsets[1], [2.0, 0.0, 0.0]);
|
||||
assert_eq!(offsets[4], [0.0, 5.0, 0.0]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn insert_instance_transform_does_not_scale_the_row_column_spacing() {
|
||||
// github discussion-derived report: an INSERT with x-scale 2 and
|
||||
// column spacing 10 was rendering 20 drawing units between columns
|
||||
// instead of 10, because the offset translation was composed ahead
|
||||
// of (and thus multiplied by) the block scale.
|
||||
let document = CadDocument::new();
|
||||
let mut insert = Insert::new("BLOCK", Vector3::new(100.0, 0.0, 0.0));
|
||||
insert.set_x_scale(2.0);
|
||||
insert.row_count = 1;
|
||||
insert.column_count = 2;
|
||||
insert.column_spacing = 10.0;
|
||||
|
||||
let transform = insert_instance_transform(
|
||||
&document,
|
||||
&insert,
|
||||
[10.0, 0.0, 0.0],
|
||||
1.0,
|
||||
BlockScalePolicy::Applied,
|
||||
);
|
||||
// The block's own origin (local point 0,0,0) placed at this instance
|
||||
// must land 10 units from the insert point, not 20.
|
||||
let world = transform.apply(Vector3::ZERO);
|
||||
assert!((world.x - 110.0).abs() < 1.0e-9, "expected spacing 10, got offset {}", world.x - 100.0);
|
||||
|
||||
// Block-local geometry must still pick up the block scale normally.
|
||||
let block_point = transform.apply(Vector3::new(1.0, 0.0, 0.0));
|
||||
assert!((block_point.x - 112.0).abs() < 1.0e-9, "block-local geometry should still be scaled by x_scale 2");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn insert_instance_transform_rotates_the_spacing_with_the_insert() {
|
||||
// Matches the pinned CAD model's own `Insert::array_points`: spacing
|
||||
// is rotated with the insert but never scaled.
|
||||
let document = CadDocument::new();
|
||||
let mut insert = Insert::new("BLOCK", Vector3::ZERO);
|
||||
insert.rotation = std::f64::consts::FRAC_PI_2;
|
||||
insert.row_count = 1;
|
||||
insert.column_count = 2;
|
||||
insert.column_spacing = 10.0;
|
||||
|
||||
let transform = insert_instance_transform(
|
||||
&document,
|
||||
&insert,
|
||||
[10.0, 0.0, 0.0],
|
||||
1.0,
|
||||
BlockScalePolicy::Applied,
|
||||
);
|
||||
let world = transform.apply(Vector3::ZERO);
|
||||
assert!(world.x.abs() < 1.0e-9, "a 90deg rotation should turn column spacing into Y, got x={}", world.x);
|
||||
assert!((world.y - 10.0).abs() < 1.0e-9, "expected the spacing rotated onto Y, got y={}", world.y);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user