forked from 1b-agent/opencadstudio-upstream
Point clouds: drawn from their scans, placed, cropped and attached as the reference does
Scans (.rcs) and projects (.rcp) are read and their points drawn as screen-sized dots, coloured by the cloud's stylization. The cloud's placement carries its scale and rotation; its extents and crops stay in the scan's coordinates. A missing scan shows the extents with the saved path. -POINTCLOUDATTACH / POINTCLOUDATTACH, POINTCLOUDCROP and the POINTCLOUD* settings follow the reference's prompts and messages, the Properties list is the reference's, and a locked cloud is left alone by edits. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
07f25f4bca
commit
e9c4e501db
58 files changed
+2711
-237
No files matched your search
Vendored
+6
-1
@@ -105,11 +105,16 @@ pub fn visualization_section(entity: &EntityType) -> Option<PropSection> {
|
||||
| EntityType::Seqend(_)
|
||||
| EntityType::Leader(_)
|
||||
| EntityType::Wipeout(_)
|
||||
// The reference lists no 3D group for an underlay.
|
||||
// The reference lists no 3D group for an underlay; a point
|
||||
// cloud's is its own (stylization and colour scheme).
|
||||
| EntityType::Underlay(_)
|
||||
| EntityType::Unknown(_)
|
||||
// Non-plotting drawing-view border: never rendered, no properties.
|
||||
| EntityType::ViewBorder(_)
|
||||
) || matches!(
|
||||
entity,
|
||||
EntityType::Extended(extended)
|
||||
if matches!(extended.data, codec::entities::ExtendedEntityData::PointCloudEx(_))
|
||||
) {
|
||||
return None;
|
||||
}
|
||||
|
||||
@@ -564,6 +564,20 @@ fn point_cloud_wires(
|
||||
let RenderObject::Lines(body_points) = rendered.object else {
|
||||
return None;
|
||||
};
|
||||
// A cloud whose scan file is found draws its points instead of its
|
||||
// extents box; the box stays behind, unseen, to pick the cloud, with its
|
||||
// faces so a click among the points selects it too.
|
||||
let mut shown = true;
|
||||
let (mut pick_tris, mut pick_tris_low) = (Vec::new(), Vec::new());
|
||||
if let codec::entities::ExtendedEntityData::PointCloudEx(data) = &extended.data {
|
||||
shown = crate::scene::model::point_cloud::resolve_source(document, data).is_none();
|
||||
let c = codec::entities::point_cloud_ex_corners(data).map(|c| [c.x, c.y, c.z]);
|
||||
let mut tris = Vec::with_capacity(36);
|
||||
for [a, b, d, e] in [[0, 1, 3, 2], [4, 5, 7, 6], [0, 1, 4, 5], [2, 3, 6, 7], [0, 2, 4, 6], [1, 3, 5, 7]] {
|
||||
tris.extend([c[a], c[b], c[e], c[a], c[e], c[d]]);
|
||||
}
|
||||
(pick_tris, pick_tris_low) = points_to_ds(tris);
|
||||
}
|
||||
let (points, points_low) = points_to_ds(body_points);
|
||||
let mut wires = vec![WireModel {
|
||||
bg_adapt: None,
|
||||
@@ -572,14 +586,14 @@ fn point_cloud_wires(
|
||||
pattern_stations: Vec::new(),
|
||||
world_width: 0.0,
|
||||
depth_override: None,
|
||||
display_visible: true,
|
||||
display_visible: shown,
|
||||
snap_only: false,
|
||||
plot_visible: true,
|
||||
plot_visible: shown,
|
||||
fill_is_3d: false,
|
||||
fill_is_2d_solid: false,
|
||||
render_instance: None,
|
||||
pick_tris: Vec::new(),
|
||||
pick_tris_low: Vec::new(),
|
||||
pick_tris,
|
||||
pick_tris_low,
|
||||
dash_from_start: false,
|
||||
dash_align_end: None,
|
||||
text_verts: Vec::new(),
|
||||
|
||||
+106
-7
@@ -2186,6 +2186,11 @@ pub struct Scene {
|
||||
frozen_hatch_cache: RefCell<HashMap<(Handle, String, u64), (u64, u64, Arc<Vec<HatchModel>>)>>,
|
||||
frozen_wipeout_cache: RefCell<HashMap<(Handle, String, u64), (u64, Arc<Vec<HatchModel>>)>>,
|
||||
frozen_image_cache: RefCell<HashMap<(Handle, u64), (u64, Arc<Vec<ImageModel>>)>>,
|
||||
/// Point clouds shown by the model content, keyed by target block and
|
||||
/// frozen-layer signature, per geometry epoch.
|
||||
point_cloud_cache: RefCell<
|
||||
HashMap<(Handle, u64), (u64, Arc<crate::scene::model::point_cloud::PointCloudSet>)>,
|
||||
>,
|
||||
frozen_mesh_cache: RefCell<HashMap<(Handle, String, u64), (u64, Arc<Vec<MeshLodSet>>)>>,
|
||||
/// Viewports that carry layer color/alpha/linetype/lineweight overrides.
|
||||
/// Cached per geometry epoch so ordinary viewports can share render data.
|
||||
@@ -2648,6 +2653,7 @@ impl Scene {
|
||||
frozen_hatch_cache: RefCell::new(HashMap::default()),
|
||||
frozen_wipeout_cache: RefCell::new(HashMap::default()),
|
||||
frozen_image_cache: RefCell::new(HashMap::default()),
|
||||
point_cloud_cache: RefCell::new(HashMap::default()),
|
||||
frozen_mesh_cache: RefCell::new(HashMap::default()),
|
||||
viewport_style_override_cache: RefCell::new(None),
|
||||
insert_hatch_cache: RefCell::new(None),
|
||||
@@ -8322,6 +8328,85 @@ impl Scene {
|
||||
arc
|
||||
}
|
||||
|
||||
/// Point clouds of the model content, with a content viewport's frozen
|
||||
/// layers removed. The paper sheet shows none. The set is rebuilt per
|
||||
/// geometry epoch; unchanged clouds keep their placed points (and GPU
|
||||
/// buffers) by key.
|
||||
pub(super) fn point_clouds_for_viewport(
|
||||
&self,
|
||||
frozen: &HashSet<Handle>,
|
||||
paper_sheet: bool,
|
||||
) -> Arc<crate::scene::model::point_cloud::PointCloudSet> {
|
||||
use crate::scene::model::point_cloud;
|
||||
static EMPTY: std::sync::OnceLock<Arc<point_cloud::PointCloudSet>> =
|
||||
std::sync::OnceLock::new();
|
||||
let empty = || Arc::clone(EMPTY.get_or_init(Default::default));
|
||||
if paper_sheet {
|
||||
return empty();
|
||||
}
|
||||
let target_block = self.content_render_block_handle();
|
||||
let key = (target_block, Self::frozen_layers_sig(frozen));
|
||||
if let Some((epoch, arc)) = self.point_cloud_cache.borrow().get(&key) {
|
||||
if *epoch == self.geometry_epoch {
|
||||
return Arc::clone(arc);
|
||||
}
|
||||
}
|
||||
let arc = if point_cloud::any_definition(&self.document) {
|
||||
let depths = rustc_hash::FxHashMap::default();
|
||||
let graph = render_graph::RenderSceneGraph::new(
|
||||
&self.document,
|
||||
(!frozen.is_empty()).then_some(frozen),
|
||||
None,
|
||||
true,
|
||||
&depths,
|
||||
);
|
||||
let mut clouds = Vec::new();
|
||||
graph.walk_root(
|
||||
self.render_scene_root(target_block),
|
||||
|entity, context| {
|
||||
context.is_instanced() || !self.entity_temporarily_hidden(entity.common().handle)
|
||||
},
|
||||
|entity, context| {
|
||||
let EntityType::Extended(extended) = entity else {
|
||||
return;
|
||||
};
|
||||
let codec::entities::ExtendedEntityData::PointCloudEx(data) = &extended.data
|
||||
else {
|
||||
return;
|
||||
};
|
||||
let transform = context.is_instanced().then_some(&context.transform);
|
||||
// Object Color stylization draws the cloud in its own colour.
|
||||
let common = entity.common();
|
||||
let color = if matches!(common.color, codec::types::Color::ByLayer) {
|
||||
self.document
|
||||
.layers
|
||||
.get(&common.layer)
|
||||
.map(|layer| layer.color.clone())
|
||||
.unwrap_or(common.color.clone())
|
||||
} else {
|
||||
common.color.clone()
|
||||
};
|
||||
let object_color = color.rgb().map_or([255; 3], |(r, g, b)| [r, g, b]);
|
||||
clouds.extend(point_cloud::placed(&self.document, data, object_color, transform));
|
||||
},
|
||||
);
|
||||
if clouds.is_empty() {
|
||||
empty()
|
||||
} else {
|
||||
Arc::new(point_cloud::PointCloudSet {
|
||||
point_size: point_cloud::point_size(&self.document),
|
||||
clouds,
|
||||
})
|
||||
}
|
||||
} else {
|
||||
empty()
|
||||
};
|
||||
self.point_cloud_cache
|
||||
.borrow_mut()
|
||||
.insert(key, (self.geometry_epoch, Arc::clone(&arc)));
|
||||
arc
|
||||
}
|
||||
|
||||
/// Image / OLE models for a content viewport, with its frozen layers removed.
|
||||
pub(super) fn images_for_viewport(
|
||||
&self,
|
||||
@@ -8392,14 +8477,19 @@ impl Scene {
|
||||
arc
|
||||
}
|
||||
|
||||
/// True when `handle`'s entity sits on a locked layer. Locked objects stay
|
||||
/// visible, snappable and selectable, but mutation paths must skip them.
|
||||
/// True when `handle`'s entity sits on a locked layer, or is a point cloud
|
||||
/// locked on its own. Locked objects stay visible, snappable and
|
||||
/// selectable, but mutation paths must skip them.
|
||||
pub fn is_layer_locked(&self, handle: Handle) -> bool {
|
||||
self.document
|
||||
.get_entity(handle)
|
||||
.map(|e| e.common().layer.clone())
|
||||
.and_then(|name| self.document.layers.get(&name).map(|l| l.is_locked()))
|
||||
.unwrap_or(false)
|
||||
let Some(entity) = self.document.get_entity(handle) else {
|
||||
return false;
|
||||
};
|
||||
is_locked_point_cloud(entity)
|
||||
|| self
|
||||
.document
|
||||
.layers
|
||||
.get(&entity.common().layer)
|
||||
.is_some_and(|l| l.is_locked())
|
||||
}
|
||||
|
||||
/// The name of the locked layer `handle` sits on, if any (for messages).
|
||||
@@ -13817,3 +13907,12 @@ mod update_entities_batch_tests {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A point cloud whose Locked property is set: edits leave it alone.
|
||||
pub(crate) fn is_locked_point_cloud(entity: &EntityType) -> bool {
|
||||
matches!(
|
||||
entity,
|
||||
EntityType::Extended(extended)
|
||||
if matches!(&extended.data, codec::entities::ExtendedEntityData::PointCloudEx(data) if data.locked)
|
||||
)
|
||||
}
|
||||
@@ -9,6 +9,7 @@ pub mod ole_pres;
|
||||
pub mod pdf_layers;
|
||||
pub mod pdf_raster;
|
||||
pub mod pdf_vector;
|
||||
pub mod point_cloud;
|
||||
pub mod underlay_vector;
|
||||
pub mod mesh_model;
|
||||
pub mod solid_model;
|
||||
|
||||
@@ -0,0 +1,522 @@
|
||||
// Point cloud scans drawn as points: the scan files a PointCloudEx entity
|
||||
// names are decoded once per file version, and the points it shows (placed
|
||||
// in the drawing, crops applied, coloured) are kept per placement so a
|
||||
// re-tessellate or a new render set reuses them.
|
||||
|
||||
mod rcp;
|
||||
mod rcs;
|
||||
|
||||
use codec::entities::{PointCloudExCrop, PointCloudExData};
|
||||
use codec::objects::{ClassObjectData, ObjectType, PointCloudDefinition};
|
||||
use codec::types::{Transform, Vector3};
|
||||
use codec::CadDocument;
|
||||
use rustc_hash::FxHashMap;
|
||||
use std::hash::{Hash, Hasher};
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::{Arc, Mutex, OnceLock, Weak};
|
||||
use std::time::SystemTime;
|
||||
|
||||
/// A scan file's points, in the file's own coordinates (the frame a
|
||||
/// PointCloudEx places with its origin and axes).
|
||||
pub struct CloudPoints {
|
||||
/// Unique per decoded file version: keys the placed point sets.
|
||||
id: u64,
|
||||
pub positions: Vec<[f64; 3]>,
|
||||
pub colors: Vec<[u8; 3]>,
|
||||
pub intensity: Vec<u8>,
|
||||
pub has_rgb: bool,
|
||||
pub has_intensity: bool,
|
||||
}
|
||||
|
||||
/// Scan points by file path and modification time. A file that fails to
|
||||
/// decode is remembered too, so it is not re-read on every tessellate.
|
||||
pub fn load(path: &Path) -> Option<Arc<CloudPoints>> {
|
||||
type Cache = Mutex<FxHashMap<PathBuf, (SystemTime, Option<Arc<CloudPoints>>)>>;
|
||||
static CACHE: OnceLock<Cache> = OnceLock::new();
|
||||
let modified = std::fs::metadata(path).and_then(|m| m.modified()).ok()?;
|
||||
let mut cache = CACHE.get_or_init(Default::default).lock().ok()?;
|
||||
if let Some((stamp, cloud)) = cache.get(path) {
|
||||
if *stamp == modified {
|
||||
return cloud.clone();
|
||||
}
|
||||
}
|
||||
let started = crate::perf::enabled().then(iced::time::Instant::now);
|
||||
let cloud = decode(path).map(Arc::new);
|
||||
crate::perf::record(format_args!(
|
||||
"[perf] point-cloud-load {:>7.1}ms points={} {}",
|
||||
crate::perf::elapsed_ms(started),
|
||||
cloud.as_ref().map_or(0, |cloud| cloud.positions.len()),
|
||||
path.display(),
|
||||
));
|
||||
cache.insert(path.to_path_buf(), (modified, cloud.clone()));
|
||||
cloud
|
||||
}
|
||||
|
||||
fn decode(path: &Path) -> Option<CloudPoints> {
|
||||
static NEXT_ID: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(1);
|
||||
let bytes = std::fs::read(path).ok()?;
|
||||
let is_project = path
|
||||
.extension()
|
||||
.is_some_and(|e| e.eq_ignore_ascii_case("rcp"));
|
||||
// A project lists its scans with their own transforms; a scan file
|
||||
// carries the same transform in its header.
|
||||
let scans: Vec<(rcs::Scan, [[f64; 3]; 3])> = if is_project {
|
||||
rcp::scans(&bytes, path)?
|
||||
.into_iter()
|
||||
.filter_map(|entry| {
|
||||
let scan = rcs::decode(&std::fs::read(&entry.path).ok()?)?;
|
||||
Some((scan, [entry.translation, entry.rotation, entry.scale]))
|
||||
})
|
||||
.collect()
|
||||
} else {
|
||||
let scan = rcs::decode(&bytes)?;
|
||||
let transform = [scan.translation, scan.rotation, scan.scale];
|
||||
vec![(scan, transform)]
|
||||
};
|
||||
let mut cloud = CloudPoints {
|
||||
id: NEXT_ID.fetch_add(1, std::sync::atomic::Ordering::Relaxed),
|
||||
positions: Vec::new(),
|
||||
colors: Vec::new(),
|
||||
intensity: Vec::new(),
|
||||
has_rgb: scans.iter().any(|(scan, _)| scan.has_rgb),
|
||||
has_intensity: scans.iter().any(|(scan, _)| scan.has_intensity),
|
||||
};
|
||||
for (scan, [translation, rotation, scale]) in scans {
|
||||
let rotate = euler(rotation);
|
||||
cloud.positions.extend(scan.local.iter().map(|p| {
|
||||
let s = [p[0] * scale[0], p[1] * scale[1], p[2] * scale[2]];
|
||||
let r = rotate.map(|row| row[0] * s[0] + row[1] * s[1] + row[2] * s[2]);
|
||||
[r[0] + translation[0], r[1] + translation[1], r[2] + translation[2]]
|
||||
}));
|
||||
if scan.has_rgb || !cloud.has_rgb {
|
||||
cloud.colors.extend_from_slice(&scan.colors);
|
||||
} else {
|
||||
// A scan without colours among coloured ones shows its intensity.
|
||||
cloud.colors.extend(scan.intensity.iter().map(|&i| [i; 3]));
|
||||
}
|
||||
cloud.intensity.extend_from_slice(&scan.intensity);
|
||||
}
|
||||
Some(cloud)
|
||||
}
|
||||
|
||||
/// Rotation matrix of a scan's rotation angles (radians, applied about X,
|
||||
/// then Y, then Z).
|
||||
// ponytail: the angle convention is unverified — every sample scan has a
|
||||
// zero rotation; check against a rotated scan when one turns up.
|
||||
fn euler([x, y, z]: [f64; 3]) -> [[f64; 3]; 3] {
|
||||
let (sx, cx) = x.sin_cos();
|
||||
let (sy, cy) = y.sin_cos();
|
||||
let (sz, cz) = z.sin_cos();
|
||||
[
|
||||
[cy * cz, sx * sy * cz - cx * sz, cx * sy * cz + sx * sz],
|
||||
[cy * sz, sx * sy * sz + cx * cz, cx * sy * sz - sx * cz],
|
||||
[-sy, sx * cy, cx * cy],
|
||||
]
|
||||
}
|
||||
|
||||
/// The point cloud definition a PointCloudEx names.
|
||||
pub(crate) fn definition<'a>(
|
||||
document: &'a CadDocument,
|
||||
data: &PointCloudExData,
|
||||
) -> Option<&'a PointCloudDefinition> {
|
||||
match document.objects.get(&data.definition_handle)? {
|
||||
ObjectType::ClassObject(object) => match &object.data {
|
||||
ClassObjectData::PointCloudDefinitionEx(definition)
|
||||
| ClassObjectData::PointCloudDefinition(definition) => Some(definition),
|
||||
_ => None,
|
||||
},
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
type Sources = Mutex<FxHashMap<String, PathBuf>>;
|
||||
|
||||
fn sources() -> &'static Sources {
|
||||
static SOURCES: OnceLock<Sources> = OnceLock::new();
|
||||
SOURCES.get_or_init(Default::default)
|
||||
}
|
||||
|
||||
/// Where a stored (relative or moved) scan path was found for an opened
|
||||
/// drawing — the definition names the stored path.
|
||||
// ponytail: keyed by the stored path alone, like underlay sources; two open
|
||||
// drawings naming the same relative path in different folders share one.
|
||||
pub(crate) fn register_source(stored: &str, resolved: PathBuf) {
|
||||
if let Ok(mut map) = sources().lock() {
|
||||
map.insert(stored.to_string(), resolved);
|
||||
}
|
||||
}
|
||||
|
||||
/// The scan file a PointCloudEx shows, when it exists: the stored path as
|
||||
/// is, else where it was found next to the drawing, else relative to the
|
||||
/// working folder.
|
||||
pub(crate) fn resolve_source(document: &CadDocument, data: &PointCloudExData) -> Option<PathBuf> {
|
||||
let stored = definition(document, data)?.source_filename.trim();
|
||||
if stored.is_empty() {
|
||||
return None;
|
||||
}
|
||||
let path = PathBuf::from(stored.replace('\\', "/"));
|
||||
if path.is_absolute() && path.is_file() {
|
||||
return Some(path);
|
||||
}
|
||||
if let Some(found) = sources().lock().ok().and_then(|map| map.get(stored).cloned()) {
|
||||
if found.is_file() {
|
||||
return Some(found);
|
||||
}
|
||||
}
|
||||
path.is_file().then_some(path)
|
||||
}
|
||||
|
||||
/// One drawn point: position as two f32 (high + low, like every other
|
||||
/// renderer vertex) and its colour.
|
||||
#[repr(C)]
|
||||
#[derive(Copy, Clone, Debug, bytemuck::Pod, bytemuck::Zeroable)]
|
||||
pub struct PointInstance {
|
||||
pub pos: [f32; 3],
|
||||
pub pos_low: [f32; 3],
|
||||
pub color: [u8; 4],
|
||||
}
|
||||
|
||||
/// The points one cloud shows, placed in the drawing.
|
||||
#[derive(Debug)]
|
||||
pub struct PlacedCloud {
|
||||
/// Identity of the source file version, placement and crops: equal keys
|
||||
/// hold equal points, so GPU buffers are reused by key.
|
||||
pub key: u64,
|
||||
pub instances: Vec<PointInstance>,
|
||||
}
|
||||
|
||||
/// Every cloud a view shows, with the POINTCLOUDPOINTSIZE they draw at.
|
||||
#[derive(Debug, Default)]
|
||||
pub struct PointCloudSet {
|
||||
pub point_size: f32,
|
||||
pub clouds: Vec<Arc<PlacedCloud>>,
|
||||
}
|
||||
|
||||
/// POINTCLOUDPOINTSIZE in pixels (1–10, default 2).
|
||||
pub(crate) fn point_size(document: &CadDocument) -> f32 {
|
||||
crate::io::drawing_variable(document, "POINTCLOUDPOINTSIZE")
|
||||
.and_then(|v| v.trim().parse::<i16>().ok())
|
||||
.unwrap_or(2)
|
||||
.clamp(1, 10) as f32
|
||||
}
|
||||
|
||||
/// Whether the drawing defines any point cloud, so drawings without one
|
||||
/// skip the render walk.
|
||||
pub(crate) fn any_definition(document: &CadDocument) -> bool {
|
||||
document.objects.values().any(|object| {
|
||||
matches!(
|
||||
object,
|
||||
ObjectType::ClassObject(object)
|
||||
if matches!(object.data, ClassObjectData::PointCloudDefinitionEx(_)
|
||||
| ClassObjectData::PointCloudDefinition(_))
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
/// The points `data` shows, placed through its origin and axes and then
|
||||
/// `transform` (a block instance), with its crops applied. `None` when the
|
||||
/// scan file is missing or unreadable.
|
||||
pub(crate) fn placed(
|
||||
document: &CadDocument,
|
||||
data: &PointCloudExData,
|
||||
object_color: [u8; 3],
|
||||
transform: Option<&Transform>,
|
||||
) -> Option<Arc<PlacedCloud>> {
|
||||
let cloud = load(&resolve_source(document, data)?)?;
|
||||
// The points shown: POINTCLOUDDENSITY per cent of POINTCLOUDPOINTMAX,
|
||||
// every n-th point of a larger cloud.
|
||||
let shown = |name| setting(name).map_or(0, |s| setting_value(document, s));
|
||||
let limit = (shown("POINTCLOUDPOINTMAX") * shown("POINTCLOUDDENSITY") / 100).max(1) as usize;
|
||||
let step = cloud.positions.len().div_ceil(limit).max(1);
|
||||
let style = Stylization::of(document, data, object_color);
|
||||
let mut origin = data.ucs_origin;
|
||||
let mut axes = [data.ucs_x_direction, data.ucs_y_direction, data.ucs_z_direction];
|
||||
if let Some(transform) = transform {
|
||||
let placed = transform.apply(origin);
|
||||
axes = axes.map(|axis| transform.apply(origin + axis) - placed);
|
||||
origin = placed;
|
||||
}
|
||||
let crops: Vec<&PointCloudExCrop> = if data.show_cropping {
|
||||
data.croppings
|
||||
.iter()
|
||||
.filter(|crop| match crop.crop_type {
|
||||
1 | 2 => crop.points.len() >= 3,
|
||||
3 => crop.points.len() >= 2,
|
||||
_ => false,
|
||||
})
|
||||
.collect()
|
||||
} else {
|
||||
Vec::new()
|
||||
};
|
||||
|
||||
let mut h = rustc_hash::FxHasher::default();
|
||||
cloud.id.hash(&mut h);
|
||||
let bits = |v: &Vector3| [v.x, v.y, v.z].map(f64::to_bits);
|
||||
bits(&origin).hash(&mut h);
|
||||
axes.iter().map(bits).for_each(|v| v.hash(&mut h));
|
||||
for crop in &crops {
|
||||
let frame = [crop.plane, crop.x_direction, crop.y_direction];
|
||||
frame.iter().chain(&crop.points).map(bits).for_each(|v| v.hash(&mut h));
|
||||
(crop.inside, crop.inverted).hash(&mut h);
|
||||
}
|
||||
step.hash(&mut h);
|
||||
style.hash(&mut h);
|
||||
let key = h.finish();
|
||||
|
||||
type Placed = Mutex<FxHashMap<u64, Weak<PlacedCloud>>>;
|
||||
static PLACED: OnceLock<Placed> = OnceLock::new();
|
||||
let placed_cache = PLACED.get_or_init(Default::default);
|
||||
if let Some(hit) = placed_cache.lock().ok()?.get(&key).and_then(Weak::upgrade) {
|
||||
return Some(hit);
|
||||
}
|
||||
|
||||
let started = crate::perf::enabled().then(iced::time::Instant::now);
|
||||
let world = |p: &[f64; 3]| origin + axes[0] * p[0] + axes[1] * p[1] + axes[2] * p[2];
|
||||
// Elevation runs over the cloud's own height unless a fixed range is set.
|
||||
let (low, high) = if data.elevation_apply_to_fixed_range {
|
||||
(data.elevation_min, data.elevation_max)
|
||||
} else {
|
||||
cloud.positions.iter().fold((f64::MAX, f64::MIN), |(lo, hi), p| {
|
||||
let z = world(p).z;
|
||||
(lo.min(z), hi.max(z))
|
||||
})
|
||||
};
|
||||
let color = |i: usize, z: f64| -> [u8; 4] {
|
||||
let [r, g, b] = match &style {
|
||||
Stylization::Object(c) => *c,
|
||||
Stylization::Ramp { colors, gradient, intensity: true, range } => {
|
||||
let percent = f64::from(cloud.intensity.get(i).copied().unwrap_or(0)) * 100.0 / 255.0;
|
||||
let (from, to) = (range.0 as f64, range.1 as f64);
|
||||
ramp(colors, (percent - from) / (to - from).max(1e-9), *gradient)
|
||||
}
|
||||
Stylization::Ramp { colors, gradient, .. } => {
|
||||
ramp(colors, (z - low) / (high - low).max(1e-9), *gradient)
|
||||
}
|
||||
Stylization::Scan => {
|
||||
if cloud.has_rgb {
|
||||
cloud.colors[i]
|
||||
} else if cloud.has_intensity {
|
||||
[cloud.intensity[i]; 3]
|
||||
} else {
|
||||
object_color
|
||||
}
|
||||
}
|
||||
};
|
||||
[r, g, b, 255]
|
||||
};
|
||||
let mut instances = Vec::with_capacity(cloud.positions.len() / step + 1);
|
||||
for (i, p) in cloud.positions.iter().enumerate().step_by(step) {
|
||||
if !crops.iter().all(|crop| keeps(crop, p)) {
|
||||
continue;
|
||||
}
|
||||
let w = world(p);
|
||||
let pos = [w.x as f32, w.y as f32, w.z as f32];
|
||||
instances.push(PointInstance {
|
||||
pos,
|
||||
pos_low: [
|
||||
(w.x - pos[0] as f64) as f32,
|
||||
(w.y - pos[1] as f64) as f32,
|
||||
(w.z - pos[2] as f64) as f32,
|
||||
],
|
||||
color: color(i, w.z),
|
||||
});
|
||||
}
|
||||
crate::perf::record(format_args!(
|
||||
"[perf] point-cloud-place {:>7.1}ms shown={}",
|
||||
crate::perf::elapsed_ms(started),
|
||||
instances.len(),
|
||||
));
|
||||
let placed = Arc::new(PlacedCloud { key, instances });
|
||||
let mut cache = placed_cache.lock().ok()?;
|
||||
cache.retain(|_, weak| weak.strong_count() > 0);
|
||||
cache.insert(key, Arc::downgrade(&placed));
|
||||
Some(placed)
|
||||
}
|
||||
|
||||
/// Whether a crop keeps point `p` (file coordinates). The crop is a prism
|
||||
/// through the plane at `plane` spanned by its two directions; its polygon
|
||||
/// is in that plane's coordinates. `inside` keeps the prism's interior and
|
||||
/// `inverted` flips it.
|
||||
fn keeps(crop: &PointCloudExCrop, p: &[f64; 3]) -> bool {
|
||||
let d = Vector3::new(p[0], p[1], p[2]) - crop.plane;
|
||||
let (x, y) = (d.dot(&crop.x_direction), d.dot(&crop.y_direction));
|
||||
let points = &crop.points;
|
||||
// A circle: its centre and a point on it.
|
||||
if crop.crop_type == 3 {
|
||||
let (c, e) = (points[0], points[1]);
|
||||
let r2 = (e.x - c.x).powi(2) + (e.y - c.y).powi(2);
|
||||
let inside = (x - c.x).powi(2) + (y - c.y).powi(2) <= r2;
|
||||
return (inside == crop.inside) != crop.inverted;
|
||||
}
|
||||
let mut inside = false;
|
||||
let mut j = points.len() - 1;
|
||||
for i in 0..points.len() {
|
||||
let (a, b) = (points[i], points[j]);
|
||||
if (a.y > y) != (b.y > y) && x < (b.x - a.x) * (y - a.y) / (b.y - a.y) + a.x {
|
||||
inside = !inside;
|
||||
}
|
||||
j = i;
|
||||
}
|
||||
(inside == crop.inside) != crop.inverted
|
||||
}
|
||||
|
||||
/// How a cloud's points are coloured (its stylization).
|
||||
#[derive(Hash)]
|
||||
enum Stylization {
|
||||
/// The scan's own colours (intensity as grey when it has none).
|
||||
Scan,
|
||||
/// The cloud's object colour.
|
||||
Object([u8; 3]),
|
||||
/// A colour scheme over intensity (per cent, in the range given) or
|
||||
/// elevation, blended or in bands.
|
||||
Ramp { colors: Vec<[u8; 3]>, gradient: bool, intensity: bool, range: (i64, i64) },
|
||||
}
|
||||
|
||||
impl Stylization {
|
||||
fn of(document: &CadDocument, data: &PointCloudExData, object_color: [u8; 3]) -> Self {
|
||||
let (scheme, intensity, gradient) = match data.stylization_type {
|
||||
2 => return Self::Object(object_color),
|
||||
5 => (&data.intensity_color_scheme, true, data.intensity_as_gradient),
|
||||
4 => (&data.current_color_scheme, false, data.elevation_as_gradient),
|
||||
// Normals and classification have no data here: scan colours.
|
||||
_ => return Self::Scan,
|
||||
};
|
||||
let colors = ramp_colors(document, scheme, if intensity { "Spectrum" } else { "Earth" });
|
||||
let range = if intensity && data.intensity_max > data.intensity_min {
|
||||
(i64::from(data.intensity_min), i64::from(data.intensity_max))
|
||||
} else {
|
||||
(0, 100)
|
||||
};
|
||||
Self::Ramp { colors, gradient, intensity, range: (range.0, range.1) }
|
||||
}
|
||||
}
|
||||
|
||||
/// The colour schemes a new drawing's colour map carries, low to high.
|
||||
const BUILTIN_RAMPS: [(&str, &[u32]); 7] = [
|
||||
("Hydro", &[0x00bfff, 0x007ca5, 0x005f7f, 0x00394c, 0x00264c, 0x00134c, 0x00004c, 0x000026]),
|
||||
("Grayscale", &[0xffffff, 0xcccccc, 0x999999, 0x666666, 0x333333, 0x000000]),
|
||||
("Earth", &[0xffffc8, 0xf0d796, 0xe1af64, 0xd28732, 0xc35f00]),
|
||||
("Blues", &[0x0000ff, 0x0000a5, 0x00007f, 0x00004c, 0x000026]),
|
||||
("Greens", &[0x00ff00, 0x00a500, 0x007f00, 0x004c00, 0x002600]),
|
||||
("Spectrum", &[0xff00ff, 0x0000ff, 0x00ffff, 0x00ff00, 0xffff00, 0xff0000]),
|
||||
("Reds", &[0xff0000, 0xa50000, 0x7f0000, 0x4c0000, 0x260000]),
|
||||
];
|
||||
|
||||
fn rgb(color: u32) -> [u8; 3] {
|
||||
[(color >> 16) as u8, (color >> 8) as u8, color as u8]
|
||||
}
|
||||
|
||||
/// A scheme's colours: from the drawing's colour map when it has the scheme,
|
||||
/// else the built-in one of that name (or `fallback` for an unset scheme).
|
||||
fn ramp_colors(document: &CadDocument, id: &str, fallback: &str) -> Vec<[u8; 3]> {
|
||||
for object in document.objects.values() {
|
||||
let ObjectType::ClassObject(object) = object else {
|
||||
continue;
|
||||
};
|
||||
let ClassObjectData::PointCloudColorMap(map) = &object.data else {
|
||||
continue;
|
||||
};
|
||||
if let Some(found) = map.color_ramps.iter().find(|r| !id.is_empty() && r.id.eq_ignore_ascii_case(id)) {
|
||||
let colors: Vec<[u8; 3]> =
|
||||
found.colors.iter().filter(|c| c.visible).map(|c| rgb(c.color as u32)).collect();
|
||||
if !colors.is_empty() {
|
||||
return colors;
|
||||
}
|
||||
}
|
||||
}
|
||||
let name = crate::entities::extended::POINT_CLOUD_RAMPS
|
||||
.iter()
|
||||
.find(|(_, guid)| guid.eq_ignore_ascii_case(id))
|
||||
.map_or(fallback, |(name, _)| name);
|
||||
BUILTIN_RAMPS
|
||||
.iter()
|
||||
.find(|(ramp, _)| *ramp == name)
|
||||
.map_or_else(|| vec![[255; 3]], |(_, colors)| colors.iter().map(|c| rgb(*c)).collect())
|
||||
}
|
||||
|
||||
/// The colour at `t` (0–1, clamped) along a scheme: blended between its
|
||||
/// colours, or the band `t` falls in.
|
||||
fn ramp(colors: &[[u8; 3]], t: f64, gradient: bool) -> [u8; 3] {
|
||||
let t = if t.is_finite() { t.clamp(0.0, 1.0) } else { 0.0 };
|
||||
if colors.len() < 2 {
|
||||
return colors.first().copied().unwrap_or([255; 3]);
|
||||
}
|
||||
if !gradient {
|
||||
return colors[((t * colors.len() as f64) as usize).min(colors.len() - 1)];
|
||||
}
|
||||
let x = t * (colors.len() - 1) as f64;
|
||||
let k = (x as usize).min(colors.len() - 2);
|
||||
let f = x - k as f64;
|
||||
let (a, b) = (colors[k], colors[k + 1]);
|
||||
[0, 1, 2].map(|c| (f64::from(a[c]) + (f64::from(b[c]) - f64::from(a[c])) * f).round() as u8)
|
||||
}
|
||||
|
||||
/// A point cloud system variable: its default and the integers it takes.
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub(crate) struct Setting {
|
||||
pub name: &'static str,
|
||||
pub default: i64,
|
||||
min: i64,
|
||||
max: i64,
|
||||
}
|
||||
|
||||
/// The reference's defaults and ranges.
|
||||
const SETTINGS: [Setting; 15] = [
|
||||
Setting { name: "POINTCLOUDDENSITY", default: 15, min: 1, max: 100 },
|
||||
Setting { name: "POINTCLOUDRTDENSITY", default: 5, min: 1, max: 100 },
|
||||
Setting { name: "POINTCLOUDPOINTSIZE", default: 2, min: 1, max: 10 },
|
||||
Setting { name: "POINTCLOUDLOD", default: 10, min: 1, max: 10 },
|
||||
Setting { name: "POINTCLOUDLOCK", default: 0, min: 0, max: 1 },
|
||||
Setting { name: "POINTCLOUDAUTOUPDATE", default: 0, min: 0, max: 1 },
|
||||
Setting { name: "POINTCLOUDVISRETAIN", default: 1, min: 0, max: 1 },
|
||||
Setting { name: "POINTCLOUDSHADING", default: 0, min: 0, max: 1 },
|
||||
Setting { name: "POINTCLOUD2DVSDISPLAY", default: 0, min: 0, max: 1 },
|
||||
Setting { name: "POINTCLOUDLIGHTSOURCE", default: 0, min: 0, max: 1 },
|
||||
Setting { name: "POINTCLOUDLIGHTING", default: 0, min: 0, max: 2 },
|
||||
Setting { name: "POINTCLOUDBOUNDARY", default: 1, min: 0, max: 2 },
|
||||
Setting { name: "POINTCLOUDCACHESIZE", default: 512, min: 0, max: 32000 },
|
||||
Setting { name: "POINTCLOUDPOINTMAX", default: 10_000_000, min: 1_000_000, max: 25_000_000 },
|
||||
Setting { name: "POINTCLOUDPOINTMAXLEGACY", default: 1_500_000, min: 1_500_000, max: 10_000_000 },
|
||||
];
|
||||
|
||||
/// How a value was refused: the lines to show, and whether the prompt asks
|
||||
/// again (a small range) or the command ends (the point and cache limits).
|
||||
pub(crate) struct Refusal {
|
||||
pub lines: Vec<String>,
|
||||
pub ask_again: bool,
|
||||
}
|
||||
|
||||
impl Setting {
|
||||
pub(crate) fn check(&self, text: &str) -> Result<i64, Refusal> {
|
||||
let Ok(value) = text.trim().parse::<i64>() else {
|
||||
return Err(Refusal { lines: vec!["Requires an integer value.".to_string()], ask_again: true });
|
||||
};
|
||||
if (self.min..=self.max).contains(&value) {
|
||||
return Ok(value);
|
||||
}
|
||||
Err(if self.max > 100 {
|
||||
Refusal {
|
||||
lines: vec![format!("Cannot set {} to that value.", self.name), "*Invalid*".to_string()],
|
||||
ask_again: false,
|
||||
}
|
||||
} else if self.min == 0 && self.max == 1 {
|
||||
Refusal { lines: vec!["Requires 0 or 1 only.".to_string()], ask_again: true }
|
||||
} else {
|
||||
Refusal {
|
||||
lines: vec![format!("Requires an integer between {} and {}.", self.min, self.max)],
|
||||
ask_again: true,
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn setting(name: &str) -> Option<Setting> {
|
||||
SETTINGS.iter().copied().find(|setting| setting.name.eq_ignore_ascii_case(name))
|
||||
}
|
||||
|
||||
/// A setting's value in the drawing, or its default.
|
||||
pub(crate) fn setting_value(document: &CadDocument, setting: Setting) -> i64 {
|
||||
crate::io::drawing_variable(document, setting.name)
|
||||
.and_then(|value| value.trim().parse::<i64>().ok())
|
||||
.unwrap_or(setting.default)
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
// Scan project (.rcp) reader: a zip holding one XML whose
|
||||
// `<VoxelTreeRunTime>` entries list the project's scans — each with its
|
||||
// transform (Translation / Rotation / Scale), visibility and the .rcs path
|
||||
// (`RelativePath` against the project's folder, `Path` absolute).
|
||||
|
||||
use std::io::{Cursor, Read};
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
/// One visible scan of the project.
|
||||
pub struct ScanEntry {
|
||||
pub path: PathBuf,
|
||||
pub translation: [f64; 3],
|
||||
pub rotation: [f64; 3],
|
||||
pub scale: [f64; 3],
|
||||
}
|
||||
|
||||
pub fn scans(bytes: &[u8], project: &Path) -> Option<Vec<ScanEntry>> {
|
||||
let mut archive = zip::ZipArchive::new(Cursor::new(bytes)).ok()?;
|
||||
let folder = project.parent().unwrap_or(Path::new(""));
|
||||
for i in 0..archive.len() {
|
||||
let file = archive.by_index(i).ok()?;
|
||||
if !file.name().to_ascii_lowercase().ends_with(".xml") {
|
||||
continue;
|
||||
}
|
||||
let mut text = String::new();
|
||||
// The project XML is small; the cap keeps a crafted archive in check.
|
||||
if file.take(64 << 20).read_to_string(&mut text).is_err() {
|
||||
continue;
|
||||
}
|
||||
let Ok(doc) = roxmltree::Document::parse(text.trim_start_matches('\u{feff}')) else {
|
||||
continue;
|
||||
};
|
||||
let nodes: Vec<_> = doc
|
||||
.descendants()
|
||||
.filter(|n| n.has_tag_name("VoxelTreeRunTime"))
|
||||
.collect();
|
||||
if nodes.is_empty() {
|
||||
continue;
|
||||
}
|
||||
return Some(nodes.into_iter().filter_map(|node| entry(node, folder)).collect());
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
fn child<'a, 'i>(node: roxmltree::Node<'a, 'i>, tag: &str) -> Option<roxmltree::Node<'a, 'i>> {
|
||||
node.children().find(|n| n.has_tag_name(tag))
|
||||
}
|
||||
|
||||
fn value<'a>(node: roxmltree::Node<'a, '_>, tag: &str) -> Option<&'a str> {
|
||||
child(node, tag)?.attribute("Value")
|
||||
}
|
||||
|
||||
fn xyz(node: roxmltree::Node, tag: &str, default: f64) -> [f64; 3] {
|
||||
let n = child(node, tag);
|
||||
["x", "y", "z"].map(|a| {
|
||||
n.and_then(|n| n.attribute(a))
|
||||
.and_then(|v| v.parse::<f64>().ok())
|
||||
.filter(|v| v.is_finite())
|
||||
.unwrap_or(default)
|
||||
})
|
||||
}
|
||||
|
||||
fn entry(node: roxmltree::Node, folder: &Path) -> Option<ScanEntry> {
|
||||
if value(node, "Visible") == Some("0") {
|
||||
return None;
|
||||
}
|
||||
let relative = value(node, "RelativePath")
|
||||
.filter(|p| !p.is_empty())
|
||||
.map(|p| folder.join(p.replace('\\', "/")))
|
||||
.filter(|p| p.is_file());
|
||||
let path = relative.or_else(|| {
|
||||
value(node, "Path")
|
||||
.map(PathBuf::from)
|
||||
.filter(|p| p.is_file())
|
||||
})?;
|
||||
Some(ScanEntry {
|
||||
path,
|
||||
translation: xyz(node, "Translation", 0.0),
|
||||
rotation: xyz(node, "Rotation", 0.0),
|
||||
scale: xyz(node, "Scale", 1.0),
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
// Indexed scan (.rcs, "ADOCT" version 3) point reader.
|
||||
//
|
||||
// Layout (little-endian):
|
||||
// 0x10 3 × f64 scan translation T
|
||||
// 0x28 3 × f64 scan rotation
|
||||
// 0x40 3 × f64 scan scale
|
||||
// 0xE0 3 × u8 has RGB, has normals, has intensity
|
||||
// 0x129 u32 section count, then 24-byte entries at 0x12D:
|
||||
// u64 type, u64 offset, u64 size
|
||||
// Section 2 is the node directory: u64 node count, then 368-byte records
|
||||
// (cube min at +0, levels +96, point count +100, block size +104, per-level
|
||||
// cell counts +108 and point counts +236). Section 1 holds the node blocks
|
||||
// back to back: the node's octree index (u32 entries), then 16-byte point
|
||||
// records — a u64 with three 18-bit millimetre offsets from the node cube's
|
||||
// min corner, the intensity byte, then blue, green, red.
|
||||
// The octree index is only needed for spatial queries: every record carries
|
||||
// its own position.
|
||||
|
||||
/// One scan, in its own (untransformed) frame.
|
||||
pub struct Scan {
|
||||
/// Point positions before the scan transform.
|
||||
pub local: Vec<[f64; 3]>,
|
||||
pub colors: Vec<[u8; 3]>,
|
||||
pub intensity: Vec<u8>,
|
||||
pub has_rgb: bool,
|
||||
pub has_intensity: bool,
|
||||
pub translation: [f64; 3],
|
||||
pub rotation: [f64; 3],
|
||||
pub scale: [f64; 3],
|
||||
}
|
||||
|
||||
const NODE_RECORD: usize = 368;
|
||||
|
||||
fn u32_at(b: &[u8], at: usize) -> Option<u32> {
|
||||
Some(u32::from_le_bytes(b.get(at..at.checked_add(4)?)?.try_into().ok()?))
|
||||
}
|
||||
|
||||
fn u64_at(b: &[u8], at: usize) -> Option<u64> {
|
||||
Some(u64::from_le_bytes(b.get(at..at.checked_add(8)?)?.try_into().ok()?))
|
||||
}
|
||||
|
||||
fn f64_at(b: &[u8], at: usize) -> Option<f64> {
|
||||
u64_at(b, at).map(f64::from_bits).filter(|v| v.is_finite())
|
||||
}
|
||||
|
||||
fn vec3_at(b: &[u8], at: usize) -> Option<[f64; 3]> {
|
||||
Some([f64_at(b, at)?, f64_at(b, at + 8)?, f64_at(b, at + 16)?])
|
||||
}
|
||||
|
||||
pub fn decode(b: &[u8]) -> Option<Scan> {
|
||||
if b.get(0..5)? != b"ADOCT" {
|
||||
return None;
|
||||
}
|
||||
let translation = vec3_at(b, 0x10)?;
|
||||
let rotation = vec3_at(b, 0x28)?;
|
||||
let scale = vec3_at(b, 0x40)?;
|
||||
let has_rgb = *b.get(0xE0)? != 0;
|
||||
let has_intensity = *b.get(0xE2)? != 0;
|
||||
|
||||
let sections = u32_at(b, 0x129)? as usize;
|
||||
let (mut directory, mut data) = (None, None);
|
||||
for k in 0..sections.min(64) {
|
||||
let at = 0x12D + k * 24;
|
||||
let offset = usize::try_from(u64_at(b, at + 8)?).ok()?;
|
||||
match u64_at(b, at)? {
|
||||
1 => data = Some(offset),
|
||||
2 => directory = Some(offset),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
let (directory, mut block) = (directory?, data?);
|
||||
let nodes = usize::try_from(u64_at(b, directory)?).ok()?;
|
||||
// Every node needs its directory record; a count past the file is corrupt.
|
||||
if nodes.checked_mul(NODE_RECORD)? > b.len() {
|
||||
return None;
|
||||
}
|
||||
|
||||
let mut scan = Scan {
|
||||
local: Vec::new(),
|
||||
colors: Vec::new(),
|
||||
intensity: Vec::new(),
|
||||
has_rgb,
|
||||
has_intensity,
|
||||
translation,
|
||||
rotation,
|
||||
scale,
|
||||
};
|
||||
for node in 0..nodes {
|
||||
let record = directory + 8 + node * NODE_RECORD;
|
||||
let cube_min = vec3_at(b, record)?;
|
||||
let points = u32_at(b, record + 100)? as usize;
|
||||
let size = u32_at(b, record + 104)? as usize;
|
||||
let mut entries = points;
|
||||
for level in 0..32 {
|
||||
entries = entries.checked_add(u32_at(b, record + 108 + level * 4)? as usize)?;
|
||||
}
|
||||
if entries.checked_mul(4)?.checked_add(points.checked_mul(16)?)? != size {
|
||||
return None;
|
||||
}
|
||||
let records = block + entries * 4;
|
||||
let end = block.checked_add(size)?;
|
||||
let bytes = b.get(records..end)?;
|
||||
scan.local.reserve(points);
|
||||
scan.colors.reserve(points);
|
||||
scan.intensity.reserve(points);
|
||||
for rec in bytes.chunks_exact(16) {
|
||||
let v = u64::from_le_bytes(rec[0..8].try_into().ok()?);
|
||||
let q = |shift: u32| ((v >> shift) & 0x3FFFF) as f64 * 1e-3;
|
||||
scan.local.push([cube_min[0] + q(0), cube_min[1] + q(18), cube_min[2] + q(36)]);
|
||||
scan.intensity.push(rec[8]);
|
||||
scan.colors.push([rec[11], rec[10], rec[9]]);
|
||||
}
|
||||
block = end;
|
||||
}
|
||||
Some(scan)
|
||||
}
|
||||
@@ -26,6 +26,7 @@ pub mod gpu_upload;
|
||||
pub mod hatch_gpu;
|
||||
pub mod wipeout_gpu;
|
||||
pub mod image_gpu;
|
||||
pub mod point_cloud_gpu;
|
||||
pub mod mesh_gpu;
|
||||
pub mod text_gpu;
|
||||
pub mod uniforms;
|
||||
@@ -136,6 +137,7 @@ pub struct Pipeline {
|
||||
/// private backends behind one upload/LOD/draw lifecycle.
|
||||
hatch_gpu: hatch_gpu::HatchGpu,
|
||||
image_pipeline: wgpu::RenderPipeline,
|
||||
point_cloud_pipeline: wgpu::RenderPipeline,
|
||||
/// SDF text-quad pipeline (Phase 2b): draws per-glyph quads sampling the
|
||||
/// shared glyph atlas. Fed only when `OCS_TEXT_SDF` is set (else no verts).
|
||||
text_pipeline: wgpu::RenderPipeline,
|
||||
@@ -313,6 +315,7 @@ pub struct Pipeline {
|
||||
/// viewport rect. Recomputed by `compute_wipeout_lod`.
|
||||
wipeout_skip_flags: Vec<bool>,
|
||||
gpu_images: Vec<ImageGpu>,
|
||||
gpu_point_clouds: point_cloud_gpu::PointCloudGpu,
|
||||
/// Batched mesh geometry — every solid's LOD0 concatenated into a few large
|
||||
/// buffers so the whole set draws in a handful of calls instead of one per
|
||||
/// solid. Hover / selection never re-pack it.
|
||||
@@ -344,6 +347,8 @@ pub struct Pipeline {
|
||||
pub cached_preview_hatch_source: Option<std::sync::Arc<Vec<HatchModel>>>,
|
||||
pub cached_wipeout_source: Option<std::sync::Arc<Vec<HatchModel>>>,
|
||||
pub cached_image_source: Option<std::sync::Arc<Vec<ImageModel>>>,
|
||||
pub cached_point_cloud_source:
|
||||
Option<std::sync::Arc<crate::scene::model::point_cloud::PointCloudSet>>,
|
||||
pub cached_text_source: Option<std::sync::Arc<Vec<text_gpu::TextVertex>>>,
|
||||
pub cached_annotation_highlight_source: Option<std::sync::Arc<Vec<WireModel>>>,
|
||||
pub cached_mesh_source: Option<std::sync::Arc<Vec<MeshLodSet>>>,
|
||||
@@ -2109,6 +2114,61 @@ impl Pipeline {
|
||||
cache: None,
|
||||
});
|
||||
|
||||
// ── Point cloud sprites ────────────────────────────────────────────
|
||||
let point_cloud_bgl1 = point_cloud_gpu::params_layout(device);
|
||||
let point_cloud_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
||||
label: Some("point_cloud.pipeline_layout"),
|
||||
bind_group_layouts: &[&frame_bgl, &point_cloud_bgl1].map(Some),
|
||||
immediate_size: 0,
|
||||
});
|
||||
let point_cloud_shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
|
||||
label: Some("point_cloud.shader"),
|
||||
source: wgpu::ShaderSource::Wgsl(std::borrow::Cow::Borrowed(include_str!(concat!(
|
||||
env!("CARGO_MANIFEST_DIR"),
|
||||
"/src/shaders/point_cloud.wgsl"
|
||||
)))),
|
||||
});
|
||||
let point_cloud_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
|
||||
label: Some("point_cloud.pipeline"),
|
||||
layout: Some(&point_cloud_layout),
|
||||
vertex: wgpu::VertexState {
|
||||
module: &point_cloud_shader,
|
||||
entry_point: Some("vs_main"),
|
||||
buffers: &[point_cloud_gpu::instance_layout()],
|
||||
compilation_options: wgpu::PipelineCompilationOptions::default(),
|
||||
},
|
||||
primitive: wgpu::PrimitiveState {
|
||||
topology: wgpu::PrimitiveTopology::TriangleList,
|
||||
cull_mode: None,
|
||||
..Default::default()
|
||||
},
|
||||
depth_stencil: Some(wgpu::DepthStencilState {
|
||||
format: wgpu::TextureFormat::Depth24PlusStencil8,
|
||||
depth_write_enabled: Some(true),
|
||||
depth_compare: Some(wgpu::CompareFunction::LessEqual),
|
||||
stencil: content_stencil.clone(),
|
||||
bias: wgpu::DepthBiasState::default(),
|
||||
}),
|
||||
multisample: wgpu::MultisampleState {
|
||||
count: MSAA_SAMPLES,
|
||||
mask: !0,
|
||||
alpha_to_coverage_enabled: false,
|
||||
},
|
||||
fragment: Some(wgpu::FragmentState {
|
||||
module: &point_cloud_shader,
|
||||
entry_point: Some("fs_main"),
|
||||
targets: &[Some(wgpu::ColorTargetState {
|
||||
format,
|
||||
blend: None,
|
||||
write_mask: wgpu::ColorWrites::ALL,
|
||||
})],
|
||||
compilation_options: wgpu::PipelineCompilationOptions::default(),
|
||||
}),
|
||||
multiview_mask: None,
|
||||
cache: None,
|
||||
});
|
||||
let gpu_point_clouds = point_cloud_gpu::PointCloudGpu::new(device, &point_cloud_bgl1);
|
||||
|
||||
// ── Text (SDF glyph quads) ─────────────────────────────────────────
|
||||
let text_atlas_bgl = text_gpu::TextAtlasGpu::bind_group_layout(device);
|
||||
let (
|
||||
@@ -2294,6 +2354,7 @@ impl Pipeline {
|
||||
wipeout_pipeline,
|
||||
hatch_gpu,
|
||||
image_pipeline,
|
||||
point_cloud_pipeline,
|
||||
text_pipeline,
|
||||
block_text_pipeline,
|
||||
text_highlight_pipeline,
|
||||
@@ -2385,6 +2446,7 @@ impl Pipeline {
|
||||
gpu_wipeouts: vec![],
|
||||
wipeout_skip_flags: vec![],
|
||||
gpu_images: vec![],
|
||||
gpu_point_clouds,
|
||||
gpu_mesh_batch: vec![],
|
||||
gpu_mesh_dynamic: vec![],
|
||||
mesh_disabled_chunks: rustc_hash::FxHashSet::default(),
|
||||
@@ -2400,6 +2462,7 @@ impl Pipeline {
|
||||
cached_preview_hatch_source: None,
|
||||
cached_wipeout_source: None,
|
||||
cached_image_source: None,
|
||||
cached_point_cloud_source: None,
|
||||
cached_text_source: None,
|
||||
cached_annotation_highlight_source: None,
|
||||
cached_mesh_source: None,
|
||||
@@ -3776,6 +3839,15 @@ analytic={:.1} regular={:.1} blocks={:.1}",
|
||||
self.gpu_images = ImageGpu::from_models(device, queue, images, &self.image_bgl1);
|
||||
}
|
||||
|
||||
pub fn upload_point_clouds(
|
||||
&mut self,
|
||||
device: &wgpu::Device,
|
||||
queue: &wgpu::Queue,
|
||||
set: &crate::scene::model::point_cloud::PointCloudSet,
|
||||
) {
|
||||
self.gpu_point_clouds.upload(device, queue, set);
|
||||
}
|
||||
|
||||
/// Upload the frame's SDF text-quad vertices, and (re)build the GPU glyph
|
||||
/// atlas from the shared CPU atlas when it grew (new glyphs baked by the
|
||||
/// text collector). `verts` empty (flag off) leaves nothing to draw.
|
||||
@@ -4189,6 +4261,38 @@ analytic={:.1} regular={:.1} blocks={:.1}",
|
||||
}
|
||||
}
|
||||
|
||||
// ── Pass 3: point clouds ──────────────────────────────────────────
|
||||
if !self.gpu_point_clouds.is_empty() {
|
||||
let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
|
||||
label: Some("point_cloud.render_pass"),
|
||||
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
|
||||
view: msaa,
|
||||
depth_slice: None,
|
||||
resolve_target: None,
|
||||
ops: wgpu::Operations {
|
||||
load: wgpu::LoadOp::Load,
|
||||
store: wgpu::StoreOp::Store,
|
||||
},
|
||||
})],
|
||||
depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment {
|
||||
view: &self.depth_view,
|
||||
depth_ops: Some(wgpu::Operations {
|
||||
load: wgpu::LoadOp::Load,
|
||||
store: wgpu::StoreOp::Store,
|
||||
}),
|
||||
stencil_ops: Some(wgpu::Operations { load: wgpu::LoadOp::Load, store: wgpu::StoreOp::Store }),
|
||||
}),
|
||||
timestamp_writes: None,
|
||||
occlusion_query_set: None,
|
||||
multiview_mask: None,
|
||||
});
|
||||
pass.set_viewport(raster.x, raster.y, raster.width, raster.height, 0.0, 1.0);
|
||||
pass.set_pipeline(&self.point_cloud_pipeline);
|
||||
pass.set_bind_group(0, &self.uniform_bind_group, &[]);
|
||||
pass.set_stencil_reference(stencil_ref);
|
||||
self.gpu_point_clouds.draw(&mut pass);
|
||||
}
|
||||
|
||||
// ── Pass 4: solid meshes (batched) ────────────────────────────────
|
||||
if !self.gpu_mesh_batch.is_empty() || !self.gpu_mesh_dynamic.is_empty() {
|
||||
let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
|
||||
@@ -5043,6 +5147,7 @@ analytic={:.1} regular={:.1} blocks={:.1}",
|
||||
self.cached_preview_hatch_source = None;
|
||||
self.cached_wipeout_source = None;
|
||||
self.cached_image_source = None;
|
||||
self.cached_point_cloud_source = None;
|
||||
self.cached_text_source = None;
|
||||
self.cached_mesh_source = None;
|
||||
self.cached_face3d_source = None;
|
||||
|
||||
@@ -0,0 +1,120 @@
|
||||
// Point cloud GPU buffers — one instance buffer per placed cloud, drawn as
|
||||
// screen-space square sprites (6 vertices per instance, no vertex buffer).
|
||||
//
|
||||
// Group 1 binding 0 — PointParams (sprite size in pixels, 16 bytes).
|
||||
//
|
||||
// Buffers are keyed by `PlacedCloud::key`: a new render set re-uploads only
|
||||
// the clouds whose points changed; the rest keep their buffers.
|
||||
|
||||
use crate::scene::model::point_cloud::{PointCloudSet, PointInstance};
|
||||
use iced::wgpu;
|
||||
|
||||
pub fn instance_layout<'a>() -> wgpu::VertexBufferLayout<'a> {
|
||||
const ATTRS: &[wgpu::VertexAttribute] = &[
|
||||
wgpu::VertexAttribute {
|
||||
offset: std::mem::offset_of!(PointInstance, pos) as u64,
|
||||
shader_location: 0,
|
||||
format: wgpu::VertexFormat::Float32x3,
|
||||
},
|
||||
wgpu::VertexAttribute {
|
||||
offset: std::mem::offset_of!(PointInstance, pos_low) as u64,
|
||||
shader_location: 1,
|
||||
format: wgpu::VertexFormat::Float32x3,
|
||||
},
|
||||
wgpu::VertexAttribute {
|
||||
offset: std::mem::offset_of!(PointInstance, color) as u64,
|
||||
shader_location: 2,
|
||||
format: wgpu::VertexFormat::Unorm8x4,
|
||||
},
|
||||
];
|
||||
wgpu::VertexBufferLayout {
|
||||
array_stride: std::mem::size_of::<PointInstance>() as u64,
|
||||
step_mode: wgpu::VertexStepMode::Instance,
|
||||
attributes: ATTRS,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn params_layout(device: &wgpu::Device) -> wgpu::BindGroupLayout {
|
||||
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
||||
label: Some("point_cloud.bgl1"),
|
||||
entries: &[wgpu::BindGroupLayoutEntry {
|
||||
binding: 0,
|
||||
visibility: wgpu::ShaderStages::VERTEX,
|
||||
ty: wgpu::BindingType::Buffer {
|
||||
ty: wgpu::BufferBindingType::Uniform,
|
||||
has_dynamic_offset: false,
|
||||
min_binding_size: None,
|
||||
},
|
||||
count: None,
|
||||
}],
|
||||
})
|
||||
}
|
||||
|
||||
struct CloudBuffer {
|
||||
key: u64,
|
||||
buffer: wgpu::Buffer,
|
||||
count: u32,
|
||||
}
|
||||
|
||||
/// The resident point clouds of one viewport pipeline.
|
||||
pub struct PointCloudGpu {
|
||||
clouds: Vec<CloudBuffer>,
|
||||
params: wgpu::Buffer,
|
||||
bind_group: wgpu::BindGroup,
|
||||
point_size: f32,
|
||||
}
|
||||
|
||||
impl PointCloudGpu {
|
||||
pub fn new(device: &wgpu::Device, layout: &wgpu::BindGroupLayout) -> Self {
|
||||
let params = device.create_buffer(&wgpu::BufferDescriptor {
|
||||
label: Some("point_cloud.params"),
|
||||
size: 16,
|
||||
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
|
||||
mapped_at_creation: false,
|
||||
});
|
||||
let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
|
||||
label: Some("point_cloud.bind_group1"),
|
||||
layout,
|
||||
entries: &[wgpu::BindGroupEntry { binding: 0, resource: params.as_entire_binding() }],
|
||||
});
|
||||
Self { clouds: Vec::new(), params, bind_group, point_size: f32::NAN }
|
||||
}
|
||||
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.clouds.is_empty()
|
||||
}
|
||||
|
||||
/// Make `set` resident, keeping the buffers of clouds it already had.
|
||||
pub fn upload(&mut self, device: &wgpu::Device, queue: &wgpu::Queue, set: &PointCloudSet) {
|
||||
let mut old = std::mem::take(&mut self.clouds);
|
||||
for cloud in set.clouds.iter().filter(|cloud| !cloud.instances.is_empty()) {
|
||||
if let Some(at) = old.iter().position(|resident| resident.key == cloud.key) {
|
||||
self.clouds.push(old.swap_remove(at));
|
||||
continue;
|
||||
}
|
||||
self.clouds.push(CloudBuffer {
|
||||
key: cloud.key,
|
||||
buffer: super::gpu_upload::upload_buffer(
|
||||
device,
|
||||
queue,
|
||||
"point_cloud.instances",
|
||||
&cloud.instances,
|
||||
wgpu::BufferUsages::VERTEX,
|
||||
),
|
||||
count: cloud.instances.len() as u32,
|
||||
});
|
||||
}
|
||||
if self.point_size != set.point_size {
|
||||
self.point_size = set.point_size;
|
||||
queue.write_buffer(&self.params, 0, bytemuck::cast_slice(&[set.point_size, 0.0, 0.0, 0.0]));
|
||||
}
|
||||
}
|
||||
|
||||
pub fn draw<'a>(&'a self, pass: &mut wgpu::RenderPass<'a>) {
|
||||
pass.set_bind_group(1, &self.bind_group, &[]);
|
||||
for cloud in &self.clouds {
|
||||
pass.set_vertex_buffer(0, cloud.buffer.slice(..));
|
||||
pass.draw(0..6, 0..cloud.count);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -165,6 +165,8 @@ pub struct ViewportData {
|
||||
/// Wipeout fills — rendered in a separate pass AFTER wires.
|
||||
pub(in crate::scene) wipeout_hatches: Arc<Vec<HatchModel>>,
|
||||
pub(in crate::scene) images: Arc<Vec<ImageModel>>,
|
||||
/// Point cloud sprites; empty on the paper sheet.
|
||||
pub(in crate::scene) point_clouds: Arc<crate::scene::model::point_cloud::PointCloudSet>,
|
||||
pub(in crate::scene) meshes: Arc<Vec<MeshLodSet>>,
|
||||
pub(in crate::scene) background_image:
|
||||
Option<crate::scene::model::image_model::DecodedImage>,
|
||||
@@ -554,6 +556,14 @@ impl shader::Primitive for Primitive {
|
||||
inner.upload_images(device, queue, &vp.images[..]);
|
||||
inner.cached_image_source = Some(Arc::clone(&vp.images));
|
||||
}
|
||||
if inner
|
||||
.cached_point_cloud_source
|
||||
.as_ref()
|
||||
.map_or(true, |source| !Arc::ptr_eq(source, &vp.point_clouds))
|
||||
{
|
||||
inner.upload_point_clouds(device, queue, &vp.point_clouds);
|
||||
inner.cached_point_cloud_source = Some(Arc::clone(&vp.point_clouds));
|
||||
}
|
||||
if inner
|
||||
.cached_text_source
|
||||
.as_ref()
|
||||
@@ -1411,6 +1421,12 @@ fn render_signature(vp: &ViewportData, placement: &PhysicalViewport) -> u64 {
|
||||
std::sync::Arc::as_ptr(&vp.meshes) as usize
|
||||
}
|
||||
.hash(&mut h);
|
||||
if vp.point_clouds.clouds.is_empty() {
|
||||
std::ptr::null()
|
||||
} else {
|
||||
std::sync::Arc::as_ptr(&vp.point_clouds)
|
||||
}
|
||||
.hash(&mut h);
|
||||
vp.geometry_epoch.hash(&mut h);
|
||||
vp.selection_generation.hash(&mut h);
|
||||
vp.selected_sig.hash(&mut h);
|
||||
@@ -4720,6 +4736,7 @@ impl Scene {
|
||||
} else {
|
||||
self.meshes_for_viewport(inst.handle, &vp_frozen)
|
||||
};
|
||||
let point_clouds = self.point_clouds_for_viewport(&vp_frozen, inst.paper_sheet);
|
||||
|
||||
// SDF text quads (behind OCS_TEXT_SDF). The glyph quads ride on each
|
||||
// entity's own wire (produced by the tessellator, transformed for
|
||||
@@ -4819,6 +4836,7 @@ impl Scene {
|
||||
hatches,
|
||||
wipeout_hatches,
|
||||
images,
|
||||
point_clouds,
|
||||
meshes,
|
||||
background_image: display.background.image.clone(),
|
||||
environment_image: display.background.environment.clone(),
|
||||
|
||||
Reference in new issue
Block a user