Skip to content

Commit

Permalink
Improvements to obj creation.
Browse files Browse the repository at this point in the history
Signed-off-by: Franco Cipollone <[email protected]>
  • Loading branch information
francocipollone committed Nov 13, 2024
1 parent fa4391a commit 93f3166
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 8 deletions.
22 changes: 15 additions & 7 deletions maliput/src/utility/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -53,18 +53,27 @@ pub type ObjFeatures = maliput_sys::utility::ffi::Features;
pub fn generate_obj_file(
road_network: &RoadNetwork,
dirpath: impl AsRef<Path>,
fileroot: impl AsRef<Path>,
fileroot: &String,
obj_features: &ObjFeatures,
) -> Result<PathBuf, Box<dyn Error>> {
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."))
}
Expand All @@ -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)?;
Expand Down
34 changes: 33 additions & 1 deletion maliput/tests/obj_file_description_test.rs
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down

0 comments on commit 93f3166

Please sign in to comment.