use std::sync::{Mutex, OnceLock, PoisonError};
use serde_json::{Map, Value, json};
use crate::document::{Builder, Document, Part};
use crate::source::SourceInfo;
use crate::{Context as _, ErrorKind, Result};
#[derive(Default)]
pub(crate) struct Cloud {
pub positions: Vec<[f32; 3]>,
pub colors: Vec<[u16; 4]>,
pub normals: Vec<[f32; 3]>,
pub columns: Vec<Column>,
pub origin: [f64; 3],
pub constant: Map<String, Value>,
}
pub(crate) struct Column {
pub name: String,
pub values: Vec<f32>,
}
impl Column {
#[cfg_attr(
not(any(feature = "pcd", feature = "xyz", feature = "e57")),
allow(dead_code, reason = "not every reader uses it")
)]
pub fn new(name: &str, values: Vec<f32>) -> Self {
Self {
name: attribute_name(name),
values,
}
}
}
pub(crate) fn attribute_name(name: &str) -> String {
let s: String = name
.trim()
.chars()
.map(|c| {
if c.is_ascii_alphanumeric() {
c.to_ascii_uppercase()
} else {
'_'
}
})
.collect();
if s.is_empty() { "VALUE".into() } else { s }
}
fn table() -> &'static [u16] {
static TABLE: OnceLock<Box<[u16]>> = OnceLock::new();
TABLE.get_or_init(|| {
(0..=u16::MAX)
.map(|v| {
let c = crate::mesh::srgb_to_linear(f32::from(v) / 65535.0);
#[allow(
clippy::cast_possible_truncation,
clippy::cast_sign_loss,
reason = "the value is in 0..=65535"
)]
let q = (c * 65535.0).round().clamp(0.0, 65535.0) as u16;
q
})
.collect()
})
}
#[inline]
pub(crate) fn linear16(rgb: [u16; 3]) -> [u16; 4] {
let t = table();
[
t[usize::from(rgb[0])],
t[usize::from(rgb[1])],
t[usize::from(rgb[2])],
u16::MAX,
]
}
#[cfg_attr(not(any(feature = "pcd")), allow(dead_code, reason = "not every reader uses it"))]
#[inline]
pub(crate) fn linear8(rgb: [u8; 3]) -> [u16; 4] {
linear16(rgb.map(|c| u16::from(c) * 257))
}
#[cfg_attr(
not(any(feature = "pcd", feature = "xyz", feature = "e57")),
allow(dead_code, reason = "not every reader uses it")
)]
#[inline]
pub(crate) fn linear_f(rgb: [f32; 3], alpha: f32) -> [u16; 4] {
let q = |c: f32| {
#[allow(
clippy::cast_possible_truncation,
clippy::cast_sign_loss,
reason = "clamped to 0..=65535 first"
)]
let v = (c.clamp(0.0, 1.0) * 65535.0).round() as u16;
v
};
let [r, g, b, _] = linear16(rgb.map(q));
[r, g, b, q(alpha)]
}
pub(crate) fn origin(min: [f64; 3], max: [f64; 3]) -> [f64; 3] {
std::array::from_fn(|k| {
let far = min[k].abs().max(max[k].abs());
if far.is_finite() && far > 1024.0 && min[k] <= max[k] {
f64::midpoint(min[k], max[k]).round()
} else {
0.0
}
})
}
#[cfg_attr(
not(any(feature = "las", feature = "xyz", feature = "e57")),
allow(dead_code, reason = "not every reader uses it")
)]
#[inline]
pub(crate) fn z_up(p: [f64; 3]) -> [f64; 3] {
[p[0], p[2], -p[1]]
}
fn retain<T>(v: &mut Vec<T>, keep: &[bool]) {
if v.len() == keep.len() {
let mut k = keep.iter();
v.retain(|_| k.next().copied().unwrap_or(false));
}
}
impl Cloud {
fn drop_broken(&mut self) {
let n = self.positions.len();
if self.positions.iter().all(|p| p.iter().all(|c| c.is_finite())) {
return;
}
let keep: Vec<bool> = self.positions.iter().map(|p| p.iter().all(|c| c.is_finite())).collect();
retain(&mut self.colors, &keep);
retain(&mut self.normals, &keep);
for c in &mut self.columns {
retain(&mut c.values, &keep);
}
retain(&mut self.positions, &keep);
debug_assert!(self.positions.len() < n);
}
pub fn document(
mut self,
info: &SourceInfo,
mut source: Map<String, Value>,
mut facts: Map<String, Value>,
) -> Document {
info.write(&mut source);
let source = Value::Object(source);
self.drop_broken();
let n = self.positions.len();
facts.insert("points".into(), json!(n));
if n == 0 {
let mut doc = Document::empty(facts);
doc.json["asset"]["extras"] = json!({ "source": source });
return doc;
}
let mut b = Builder::default();
let mut constant = std::mem::take(&mut self.constant);
let mut attributes = json!({ "POSITION": b.positions(std::mem::take(&mut self.positions)) });
if self.normals.len() == n {
for v in &mut self.normals {
let len = (v[0] * v[0] + v[1] * v[1] + v[2] * v[2]).sqrt();
*v = if len.is_finite() && len > 1e-12 {
v.map(|c| c / len)
} else {
[0.0, 1.0, 0.0]
};
}
let a = b.accessor(
Part::f32(std::mem::take(&mut self.normals).into_flattened()),
5126,
n,
"VEC3",
Some(34962),
);
attributes["NORMAL"] = json!(a);
}
if self.colors.len() == n {
let first = self.colors[0];
if self.colors.iter().all(|&c| c == first) {
constant.insert("COLOR_0".into(), json!(first.map(|c| f32::from(c) / 65535.0)));
} else {
let a = b.accessor(
Part::u16(std::mem::take(&mut self.colors).into_flattened()),
5123,
n,
"VEC4",
Some(34962),
);
b.accessors[a]["normalized"] = json!(true);
attributes["COLOR_0"] = json!(a);
}
}
for c in std::mem::take(&mut self.columns) {
let key = format!("_{}", c.name);
if c.values.len() != n || attributes.get(&key).is_some() || constant.contains_key(&key) {
continue;
}
let first = c.values[0];
if c.values.iter().all(|v| v.to_bits() == first.to_bits()) {
constant.insert(key, if first.is_finite() { json!(first) } else { Value::Null });
continue;
}
attributes[key] = json!(b.accessor(Part::f32(c.values), 5126, n, "SCALAR", Some(34962)));
}
let mut mesh = json!({ "primitives": [{ "attributes": attributes, "mode": 0 }] });
if !constant.is_empty() {
mesh["extras"] = json!({ "constant": constant });
}
let mut node = json!({ "mesh": 0 });
if self.origin.iter().any(|&c| c != 0.0 && c.is_finite()) {
node["translation"] = json!(self.origin.map(|c| if c.is_finite() { c } else { 0.0 }));
}
let mut json = Map::new();
let mut extras = Map::new();
extras.insert("source".into(), source);
json.insert("asset".into(), json!({ "extras": extras }));
json.insert("scene".into(), json!(0));
json.insert("scenes".into(), json!([{ "nodes": [0] }]));
json.insert("nodes".into(), json!([node]));
json.insert("meshes".into(), json!([mesh]));
b.finish(json, facts)
}
}
pub(crate) fn reserve<T>(v: &mut Vec<T>, n: usize, what: &str) -> Result<()> {
v.try_reserve(n).with_context(ErrorKind::LimitExceeded, || {
format!("not enough memory for the {what} points")
})
}
pub(crate) fn check_count(n: u64, max: usize, what: &str) -> Result<()> {
ensure!(
usize::try_from(n).is_ok_and(|n| n <= max),
LimitExceeded,
"{n} points in the {what} file, more than the {max} allowed (Keep::max_points)"
);
Ok(())
}
#[cfg_attr(
not(any(feature = "pcd", feature = "xyz", feature = "e57")),
allow(dead_code, reason = "not every reader uses it")
)]
pub(crate) fn map<I: Send, T: Send>(items: Vec<I>, threads: usize, f: impl Fn(I) -> T + Sync) -> Vec<T> {
let n = items.len();
let helpers = threads.min(n).saturating_sub(1);
let queue = Mutex::new(items.into_iter().enumerate());
let done: Mutex<Vec<Option<T>>> = Mutex::new((0..n).map(|_| None).collect());
let run = || {
loop {
let next = queue.lock().unwrap_or_else(PoisonError::into_inner).next();
let Some((k, item)) = next else { break };
let t = f(item);
done.lock().unwrap_or_else(PoisonError::into_inner)[k] = Some(t);
}
};
std::thread::scope(|s| {
for _ in 0..helpers {
if !crate::keep::spawn(s, run) {
break;
}
}
run();
});
done.into_inner()
.unwrap_or_else(PoisonError::into_inner)
.into_iter()
.flatten()
.collect()
}
#[cfg(test)]
pub(crate) fn corrupt(b: &mut [u8], rng: &mut crate::test_rng::Pcg32, k: usize) {
if b.is_empty() {
return;
}
for _ in 0..k {
let at = rng.below(u32::try_from(b.len()).unwrap_or(u32::MAX)) as usize;
b[at] = rng.next_u32().to_le_bytes()[0];
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn colors_go_linear_and_keep_their_ends() {
assert_eq!(linear8([0, 255, 0]), [0, 65535, 0, 65535]);
let mid = linear8([128, 128, 128])[0];
assert!((f32::from(mid) / 65535.0 - 0.2158).abs() < 0.001, "{mid}");
assert_eq!(linear_f([1.0, 0.0, 2.0], 0.5), [65535, 0, 65535, 32768]);
}
#[test]
fn the_origin_is_zero_near_zero_and_rounded_far_away() {
let o = origin([500_000.25, 10.0, -3000.0], [500_100.75, 20.0, -2000.0]);
assert_eq!(o, [500_051.0, 0.0, -2500.0]);
assert_eq!(origin([f64::NAN; 3], [f64::NAN; 3]), [0.0; 3]);
}
#[test]
fn constant_columns_and_broken_points_are_dropped() {
let cloud = Cloud {
positions: vec![[0.0, 0.0, 0.0], [f32::NAN, 0.0, 0.0], [1.0, 2.0, 3.0]],
colors: vec![[1, 2, 3, 4]; 3],
normals: vec![[0.0, 0.0, 2.0], [0.0; 3], [0.0; 3]],
columns: vec![
Column::new("intensity", vec![1.0, 2.0, 3.0]),
Column::new("class", vec![2.0, 9.0, 2.0]),
],
origin: [10.0, 0.0, 0.0],
constant: Map::new(),
};
let info = SourceInfo {
format: Some("test".into()),
..SourceInfo::default()
};
let doc = cloud.document(&info, Map::new(), Map::new());
assert_eq!(doc.source_info().format.as_deref(), Some("test"));
assert_eq!(doc.facts["points"], 2);
let mesh = &doc.json["meshes"][0];
let attrs = &mesh["primitives"][0]["attributes"];
assert!(attrs.get("_INTENSITY").is_some());
assert!(attrs.get("_CLASS").is_none());
assert!(attrs.get("COLOR_0").is_none());
assert_eq!(mesh["extras"]["constant"]["_CLASS"], 2.0);
assert_eq!(doc.json["nodes"][0]["translation"], json!([10.0, 0.0, 0.0]));
let m = doc.flatten(crate::document::Pose::Rest).unwrap().unwrap();
assert_eq!(m.prims.iter().map(|p| p.points.len()).sum::<usize>(), 2);
}
}