use serde_json::{Map, Value};
use super::xform::{self, IDENTITY, M4};
use super::{Ctx, MAX_DEPTH, Merged, Stats, Up, Y_TO_Z, Z_TO_Y, content, fetch, reference};
use crate::document::Document;
use crate::formats::Input;
use crate::{ErrorKind, Result};
pub(super) const MAX_TILES: usize = 1024;
struct Tile {
json: Value,
transform: M4,
add: bool,
error: f64,
path: Vec<usize>,
}
impl Tile {
fn new(mut json: Value, parent: &M4, parent_adds: bool, path: Vec<usize>) -> Result<Self> {
ensure!(
json.get("implicitTiling").is_none() && json.pointer("/extensions/3DTILES_implicit_tiling").is_none(),
Unsupported,
"3D Tiles implicit tiling isn't read"
);
let transform = match json.get("transform") {
None | Some(Value::Null) => *parent,
Some(t) => {
let m: Vec<f64> = t
.as_array()
.into_iter()
.flatten()
.filter_map(Value::as_f64)
.filter(|x| x.is_finite())
.collect();
ensure!(
m.len() == 16 && t.as_array().is_some_and(|t| t.len() == 16),
Corrupt,
"3D Tiles tile transform isn't sixteen numbers"
);
let mut flat = [0.0; 16];
flat.copy_from_slice(&m);
xform::mul(parent, &xform::from_column_major(&flat))
}
};
let add = match json.get("refine").and_then(Value::as_str) {
Some(r) if r.eq_ignore_ascii_case("ADD") => true,
Some(r) if r.eq_ignore_ascii_case("REPLACE") => false,
_ => parent_adds,
};
let error = json
.get("geometricError")
.and_then(Value::as_f64)
.filter(|e| e.is_finite())
.unwrap_or(0.0);
let _ = json.get_mut("children").filter(|c| !c.is_array()).map(Value::take);
Ok(Self {
json,
transform,
add,
error,
path,
})
}
fn children(&mut self) -> Vec<Value> {
match self.json.get_mut("children").map(Value::take) {
Some(Value::Array(list)) => list,
_ => Vec::new(),
}
}
fn child_count(&self) -> usize {
self.json.get("children").and_then(Value::as_array).map_or(0, Vec::len)
}
fn uris(&self) -> Vec<&str> {
fn uri(c: &Value) -> Option<&str> {
c.get("uri").or_else(|| c.get("url"))?.as_str()
}
let one = self.json.get("content").and_then(uri);
let more = self
.json
.get("contents")
.and_then(Value::as_array)
.into_iter()
.flatten()
.filter_map(uri);
one.into_iter().chain(more).collect()
}
}
pub(super) fn read(ctx: &Ctx<'_>, json: &Map<String, Value>, stats: &mut Stats) -> Result<Document> {
ensure!(
ctx.depth < MAX_DEPTH,
LimitExceeded,
"3D Tiles tilesets nest more than {MAX_DEPTH} deep"
);
let asset = json.get("asset");
if stats.version.is_none() {
stats.version = asset
.and_then(|a| a.get("version"))
.and_then(Value::as_str)
.map(str::to_owned);
}
let up = match asset.and_then(|a| a.get("gltfUpAxis")).and_then(Value::as_str) {
Some(a) if a.eq_ignore_ascii_case("X") => Up::X,
Some(a) if a.eq_ignore_ascii_case("Z") => Up::Z,
Some(a) if a.eq_ignore_ascii_case("Y") => Up::Y,
_ => ctx.up,
};
ensure!(
json.get("extensions")
.and_then(|e| e.get("3DTILES_implicit_tiling"))
.is_none(),
Unsupported,
"3D Tiles implicit tiling isn't read"
);
let Some(root) = json.get("root").filter(|r| r.is_object()) else {
bail!(Corrupt, "3D Tiles tileset has no root tile");
};
let budget = MAX_TILES.saturating_sub(stats.tiles).max(1);
let mut open = vec![Tile::new(root.clone(), &IDENTITY, false, Vec::new())?];
let mut drawn: Vec<Tile> = Vec::new();
while !open.is_empty() {
let coarsest = (0..open.len())
.max_by(|&a, &b| open[a].error.total_cmp(&open[b].error))
.unwrap_or(0);
let mut tile = open.swap_remove(coarsest);
let kids = tile.child_count();
if kids == 0 {
drawn.push(tile);
continue;
}
if drawn.len() + open.len() + kids + usize::from(tile.add) > budget {
stats.capped = true;
drawn.push(tile);
continue;
}
for (i, child) in tile.children().into_iter().enumerate() {
let mut path = tile.path.clone();
path.push(i);
open.push(Tile::new(child, &tile.transform, tile.add, path)?);
}
if tile.add {
drawn.push(tile);
}
}
drawn.sort_by(|a, b| a.path.cmp(&b.path));
let mut out = Merged::new("tileset");
let mut read = 0;
let before = stats.missing.len();
for tile in &drawn {
let place = xform::mul(&Z_TO_Y, &xform::mul(&tile.transform, &Y_TO_Z));
for uri in tile.uris() {
let found = reference(ctx, uri).and_then(|path| Ok((fetch(ctx, &path, uri)?, path)));
let (bytes, path) = match found {
Ok(found) => found,
Err(e) if matches!(e.kind(), ErrorKind::MissingAsset | ErrorKind::Unsupported) => {
stats.missing.push(uri.to_owned());
continue;
}
Err(e) => return Err(e),
};
let sub = Ctx {
input: Input {
limit: ctx.input.limit,
..Input::shared(&bytes, &path)
},
up,
depth: ctx.depth + 1,
..*ctx
};
let listed = stats.missing.len();
let doc = match content(&sub, &bytes, stats) {
Ok(doc) => doc,
Err(e) if e.kind() == ErrorKind::MissingAsset => {
if stats.missing.len() == listed {
stats.missing.push(uri.to_owned());
}
continue;
}
Err(e) => return Err(e.at(&path)),
};
out.add(doc, &place, Some(uri), ctx.input.limit)?;
read += 1;
}
}
if read == 0 && stats.missing.len() > before {
let missing = &stats.missing[before..];
return Err(crate::Error::missing(format!(
"none of the 3D Tiles tileset's content is among the assets: {:?}{}",
missing[0],
if missing.len() > 1 {
format!(" and {} more", missing.len() - 1)
} else {
String::new()
}
)));
}
stats.kind("tileset");
Ok(out.doc)
}