Skip to content

Commit

Permalink
feat(engine): introduce GLTF module and refactor related components (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
miseyu authored Nov 26, 2024
1 parent d4463e7 commit 985f1ee
Show file tree
Hide file tree
Showing 20 changed files with 194 additions and 293 deletions.
30 changes: 29 additions & 1 deletion engine/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions engine/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ reearth-flow-action-wasm-processor = { path = "runtime/action-wasm-processor" }
reearth-flow-common = { path = "runtime/common" }
reearth-flow-eval-expr = { path = "runtime/eval-expr" }
reearth-flow-geometry = { path = "runtime/geometry" }
reearth-flow-gltf = { path = "runtime/gltf" }
reearth-flow-macros = { path = "runtime/macros" }
reearth-flow-runner = { path = "runtime/runner" }
reearth-flow-runtime = { path = "runtime/runtime" }
Expand Down
1 change: 1 addition & 0 deletions engine/runtime/action-sink/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ reearth-flow-action-log.workspace = true
reearth-flow-common.workspace = true
reearth-flow-eval-expr.workspace = true
reearth-flow-geometry.workspace = true
reearth-flow-gltf.workspace = true
reearth-flow-runtime.workspace = true
reearth-flow-state.workspace = true
reearth-flow-storage.workspace = true
Expand Down
3 changes: 0 additions & 3 deletions engine/runtime/action-sink/src/file/cesium3dtiles.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
mod gltf;
mod material;
pub(crate) mod metadata;
pub(crate) mod sink;
mod slice;
mod tiling;
15 changes: 7 additions & 8 deletions engine/runtime/action-sink/src/file/cesium3dtiles/sink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,25 @@ use nusamai_citygml::schema::Schema;
use nusamai_projection::cartesian::geodetic_to_geocentric;
use rayon::prelude::*;
use reearth_flow_common::{
gltf::calculate_normal,
texture::{apply_downsample_factor, get_texture_downsample_scale_of_polygon},
uri::Uri,
};
use reearth_flow_gltf::calculate_normal;
use reearth_flow_runtime::event::EventHub;
use reearth_flow_runtime::executor_operation::{ExecutorContext, NodeContext};
use reearth_flow_runtime::node::{Port, Sink, SinkFactory, DEFAULT_PORT};
use reearth_flow_runtime::{errors::BoxedError, executor_operation::Context};
use reearth_flow_types::geometry as geometry_types;
use reearth_flow_types::Expr;
use reearth_flow_types::Feature;
use reearth_flow_types::{geometry as geometry_types, material};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use serde_json::Value as JsonValue;
use tempfile::tempdir;
use url::Url;

use super::gltf::write_gltf_glb;
use super::tiling::{TileContent, TileTree};
use super::{
gltf, material, metadata,
slice::{slice_to_tiles, SlicedFeature},
tiling,
};
Expand Down Expand Up @@ -434,9 +432,9 @@ fn tile_writing_stage(
};

let mut vertices: IndexSet<[u32; 9], RandomState> = IndexSet::default(); // [x, y, z, u, v, feature_id]
let mut primitives: gltf::Primitives = Default::default();
let mut primitives: reearth_flow_gltf::Primitives = Default::default();

let mut metadata_encoder = metadata::MetadataEncoder::new(schema);
let mut metadata_encoder = reearth_flow_gltf::MetadataEncoder::new(schema);

let packer = Mutex::new(AtlasPacker::default());

Expand Down Expand Up @@ -743,14 +741,15 @@ fn tile_writing_stage(
let writer = BufWriter::new(&mut buffer);
let content_path = content.content_path.clone();
contents.lock().unwrap().push(content);
write_gltf_glb(
reearth_flow_gltf::write_gltf_glb(
writer,
translation,
vertices,
primitives,
features.len(),
metadata_encoder,
)?;
)
.map_err(crate::errors::SinkError::cesium3dtiles_writer)?;

let storage = ctx
.storage_resolver
Expand Down
13 changes: 8 additions & 5 deletions engine/runtime/action-sink/src/file/cesium3dtiles/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ use std::collections::{HashMap, HashSet};
use flatgeom::{MultiPolygon, Polygon, Polygon2, Polygon3};
use indexmap::IndexSet;
use itertools::Itertools;
use reearth_flow_types::{AttributeValue, Feature, GeometryType};
use reearth_flow_types::{
material::{self, Material},
AttributeValue, Feature, GeometryType,
};
use serde::{Deserialize, Serialize};

use super::{material::Material, material::Texture, tiling, tiling::zxy_from_lng_lat};
use super::{tiling, tiling::zxy_from_lng_lat};

pub type TileZXYName = (u8, u32, u32);

Expand Down Expand Up @@ -40,7 +43,7 @@ pub fn slice_to_tiles<E>(

let mut sliced_tiles: HashMap<(u8, u32, u32), SlicedFeature> = HashMap::new();
let mut materials: IndexSet<Material> = IndexSet::new();
let default_material = reearth_flow_types::Material::default();
let default_material = reearth_flow_types::material::X3DMaterial::default();

let (lng_center, lat_center, approx_dx, approx_dy, approx_dh) = {
let vertice = city_gml.max_min_vertice();
Expand Down Expand Up @@ -99,8 +102,8 @@ pub fn slice_to_tiles<E>(
let orig_tex = poly_tex.and_then(|idx| city_gml.textures.get(idx as usize));
Material {
base_color: orig_mat.diffuse_color.into(),
base_texture: orig_tex.map(|tex| Texture {
uri: tex.uri.clone().into(),
base_texture: orig_tex.map(|tex| material::Texture {
uri: tex.uri.clone(),
}),
}
} else {
Expand Down
3 changes: 0 additions & 3 deletions engine/runtime/action-wasm-processor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,3 @@ tokio.workspace = true
tracing.workspace = true
wasmer.workspace = true
wasmer-wasix.workspace = true

[dev-dependencies]
bytes.workspace = true
1 change: 0 additions & 1 deletion engine/runtime/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ pub mod csv;
pub mod dir;
pub mod fs;
pub mod future;
pub mod gltf;
pub mod image;
pub mod json;
pub mod serde;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
mod helper;

fn main() {
helper::execute("data-convert/08-ubld/workflow.yml");
helper::execute("data-convert/07-brid-tun-cons/workflow.yml");
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,43 +14,14 @@ with:
extractDmGeometryAsXmlFragment: false
outputPath:
graphs:
- !include ../../../graphs/attribute_reader.yml
- !include ../../../graphs/folder_and_file_path_reader.yml
- id: 49aefc85-168d-4190-b7e2-dc0d58ee44e3
name: entry_point
nodes:
- id: d5fa7732-ef20-45ec-a331-e6d57571354b
name: AttributeReader
name: FolderAndfilePathReader
type: subGraph
subGraphId: 64931277-3a82-4a1c-88bc-9b54fe172518

- id: e1a0388f-78a3-4aec-b3e1-d6ffc9065dc9
name: AttributeMapperFilePath
type: action
action: AttributeMapper
with:
mappers:
- attribute: code
valueAttribute: meshCode
- attribute: type
valueAttribute: package
- attribute: maxLod
valueAttribute: maxLod
- attribute: length
expr: |
env.get("__value")["package"].len()
- attribute: file
expr: |
file::extract_filename(env.get("__value").cityGmlPath)
- id: c8d0f9dd-8f3d-46e0-8608-dfa22801a3d2
name: FileWriterTsvFilePath
type: action
action: FileWriter
with:
format: tsv
output: |
file::join_path(env.get("outputPath"), "maxLod.tsv")
subGraphId: c6863b71-953b-4d15-af56-396fc93fc617

- id: 254d6287-7649-4647-9ab5-0c1c423f356a
name: FeatureReaderByCityGml
Expand Down Expand Up @@ -97,20 +68,10 @@ graphs:
file::join_path(env.get("outputPath"), "ubld_lod1-4")
edges:
- id: 7b81f501-3f07-4cec-bf9b-9cefcebdf47d
from: d5fa7732-ef20-45ec-a331-e6d57571354b
to: e1a0388f-78a3-4aec-b3e1-d6ffc9065dc9
fromPort: filePath
toPort: default
- id: 284b02d9-8051-4614-a03c-583274c700f8
from: e1a0388f-78a3-4aec-b3e1-d6ffc9065dc9
to: c8d0f9dd-8f3d-46e0-8608-dfa22801a3d2
fromPort: default
toPort: default
- id: a4751655-5956-4e27-a976-e35f8914ad31
from: d5fa7732-ef20-45ec-a331-e6d57571354b
to: 254d6287-7649-4647-9ab5-0c1c423f356a
fromPort: filePath
fromPort: default
toPort: default
- id: 86ec56d6-cefb-48ca-a72d-0c57a33d198a
from: 254d6287-7649-4647-9ab5-0c1c423f356a
Expand Down
37 changes: 37 additions & 0 deletions engine/runtime/gltf/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
[package]
description = "Re:Earth Flow GLTF library"
name = "reearth-flow-gltf"

authors.workspace = true
edition.workspace = true
homepage.workspace = true
license.workspace = true
repository.workspace = true
rust-version.workspace = true
version.workspace = true

[dependencies]
reearth-flow-common.workspace = true
reearth-flow-eval-expr.workspace = true
reearth-flow-geometry.workspace = true
reearth-flow-runtime.workspace = true
reearth-flow-storage.workspace = true
reearth-flow-types.workspace = true

nusamai-citygml.workspace = true
nusamai-gltf.workspace = true

ahash.workspace = true
byteorder.workspace = true
chrono.workspace = true
indexmap.workspace = true
itertools.workspace = true
once_cell.workspace = true
regex.workspace = true
schemars.workspace = true
serde.workspace = true
serde_json.workspace = true
tempfile.workspace = true
thiserror.workspace = true
tokio.workspace = true
tracing.workspace = true
21 changes: 21 additions & 0 deletions engine/runtime/gltf/src/errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use thiserror::Error;

#[derive(Error, Debug)]
pub enum Error {
#[error("Gltf Metadata error: {0}")]
Metadata(String),
#[error("Gltf writer error: {0}")]
Writer(String),
}

pub type Result<T, E = Error> = std::result::Result<T, E>;

impl Error {
pub fn metadata<T: ToString>(message: T) -> Self {
Self::Metadata(message.to_string())
}

pub fn writer<T: ToString>(message: T) -> Self {
Self::Writer(message.to_string())
}
}
8 changes: 8 additions & 0 deletions engine/runtime/gltf/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
pub(crate) mod errors;
pub(crate) mod metadata;
pub(crate) mod utils;
pub(crate) mod writer;

pub use metadata::*;
pub use utils::*;
pub use writer::*;
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ impl<'a> MetadataEncoder<'a> {
&mut self,
typename: &str,
attributes: &HashMap<String, AttributeValue>,
) -> Result<usize, ()> {
) -> crate::errors::Result<usize> {
let Some(TypeDef::Feature(feature_def)) = self.original_schema.types.get(typename) else {
return Err(());
return Err(crate::errors::Error::metadata("Feature type not found"));
};

let typename = typename.replace(':', "_");
Expand Down Expand Up @@ -145,7 +145,10 @@ impl From<&FeatureTypeDef> for Class {
}

impl Class {
fn add_feature(&mut self, attributes: &HashMap<String, AttributeValue>) -> Result<usize, ()> {
fn add_feature(
&mut self,
attributes: &HashMap<String, AttributeValue>,
) -> crate::errors::Result<usize> {
// Encode id
if let Some(id) = attributes.get("gmlId") {
if let Some(prop) = self.properties.get_mut("id") {
Expand Down
File renamed without changes.
Loading

0 comments on commit 985f1ee

Please sign in to comment.