From 93f3166c570a6502d1a629827cabcf28206b0118 Mon Sep 17 00:00:00 2001 From: Franco Cipollone Date: Wed, 13 Nov 2024 12:38:49 +0000 Subject: [PATCH] Improvements to obj creation. Signed-off-by: Franco Cipollone --- maliput/src/utility/mod.rs | 22 +++++++++----- maliput/tests/obj_file_description_test.rs | 34 +++++++++++++++++++++- 2 files changed, 48 insertions(+), 8 deletions(-) diff --git a/maliput/src/utility/mod.rs b/maliput/src/utility/mod.rs index e0125af..c51e314 100644 --- a/maliput/src/utility/mod.rs +++ b/maliput/src/utility/mod.rs @@ -30,7 +30,7 @@ use crate::api::RoadNetwork; use std::error::Error; -use std::fs::{create_dir, read_to_string, remove_file}; +use std::fs::{create_dir, create_dir_all, read_to_string, remove_file}; use std::path::{Path, PathBuf}; pub type ObjFeatures = maliput_sys::utility::ffi::Features; @@ -53,18 +53,27 @@ pub type ObjFeatures = maliput_sys::utility::ffi::Features; pub fn generate_obj_file( road_network: &RoadNetwork, dirpath: impl AsRef, - fileroot: impl AsRef, + fileroot: &String, obj_features: &ObjFeatures, ) -> Result> { - let complete_file_path = dirpath.as_ref().join(fileroot.as_ref().with_extension("obj")); + // Saves the complete path to the generated Wavefront file. + let future_obj_file_path = dirpath.as_ref().join(fileroot.clone() + ".obj"); let dirpath = to_string(dirpath)?; - let fileroot = to_string(fileroot)?; + // Creates dirpath if does not exist. + if !Path::new(&dirpath).exists() { + let _ = create_dir_all(&dirpath); + } let raw_rn = road_network.rn.as_ref(); if let Some(raw_rn) = raw_rn { unsafe { - maliput_sys::utility::ffi::Utility_GenerateObjFile(raw_rn, &dirpath, &fileroot, obj_features); + maliput_sys::utility::ffi::Utility_GenerateObjFile(raw_rn, &dirpath, fileroot, obj_features); + } + // Verify if the file was created. + if future_obj_file_path.is_file() && future_obj_file_path.with_extension("mtl").is_file() { + Ok(future_obj_file_path) + } else { + Result::Err(Box::from("Failed to generate the Wavefront files.")) } - Ok(complete_file_path) } else { Result::Err(Box::from("RoadNetwork is empty.")) } @@ -89,7 +98,6 @@ pub fn get_obj_description_from_road_network( if !output_directory.exists() { let _ = create_dir(&output_directory); } - let output_directory = to_string(output_directory)?; let file_name = String::from("road_network"); let path_to_obj_file = generate_obj_file(road_network, &output_directory, &file_name, obj_features)?; let obj_description = read_to_string(&path_to_obj_file)?; diff --git a/maliput/tests/obj_file_description_test.rs b/maliput/tests/obj_file_description_test.rs index a628815..77254d2 100644 --- a/maliput/tests/obj_file_description_test.rs +++ b/maliput/tests/obj_file_description_test.rs @@ -1,6 +1,38 @@ mod common; -use maliput::utility::{get_obj_description_from_road_network, ObjFeatures}; +use maliput::utility::{generate_obj_file, get_obj_description_from_road_network, ObjFeatures}; + +#[test] +fn generate_obj_file_test() { + let road_network = common::create_t_shape_road_network(); + let features = ObjFeatures { + max_grid_unit: 1.0, + min_grid_resolution: 5.0, + draw_stripes: true, + draw_arrows: true, + draw_lane_haze: true, + draw_branch_points: true, + draw_elevation_bounds: true, + off_grid_mesh_generation: false, + simplify_mesh_threshold: 0., + stripe_width: 0.25, + stripe_elevation: 0.05, + arrow_elevation: 0.05, + lane_haze_elevation: 0.02, + branch_point_elevation: 0.5, + branch_point_height: 0.5, + origin: [0.; 3], + highlighted_segments: Vec::new(), + }; + let dirpath = std::env::temp_dir().join("maliput"); + let fileroot: String = String::from("my_test.random"); + let obj_file_path = generate_obj_file(&road_network, dirpath, &fileroot, &features); + assert!(obj_file_path.is_ok()); + let obj_file_path = obj_file_path.unwrap(); + assert!(obj_file_path.is_file()); + let mtl_file_path = obj_file_path.with_extension("mtl"); + assert!(mtl_file_path.is_file()); +} #[test] fn get_obj_description_from_road_network_test() {