diff --git a/maliput/src/api/mod.rs b/maliput/src/api/mod.rs index 5727ec4..2898e90 100644 --- a/maliput/src/api/mod.rs +++ b/maliput/src/api/mod.rs @@ -200,7 +200,7 @@ impl<'a> RoadGeometry<'a> { /// println!("num_junctions: {}", road_geometry.num_junctions()); /// ``` pub struct RoadNetwork { - rn: cxx::UniquePtr, + pub(crate) rn: cxx::UniquePtr, } impl RoadNetwork { diff --git a/maliput/src/utility/mod.rs b/maliput/src/utility/mod.rs index d939df9..884a654 100644 --- a/maliput/src/utility/mod.rs +++ b/maliput/src/utility/mod.rs @@ -28,48 +28,60 @@ // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -use maliput_sys::api::ffi::RoadNetwork; +use crate::api::RoadNetwork; use std::error::Error; -use std::fs::{read_to_string, remove_file}; -use std::path::Path; +use std::fs::{create_dir, read_to_string, remove_file}; +use std::path::{Path, PathBuf}; -type Features = maliput_sys::utility::ffi::Features; +pub type Features = maliput_sys::utility::ffi::Features; -// Generate a mesh obj file from the road network. +/// Generates a Wavefront and a Material file from the `road_network`. These are written under the +/// `dirpath` directory as `fileroot.obj` and `fileroot.mtl`. +/// +/// Fails if `dirpath` doesn't exist, or if the files can't be created. pub fn generate_obj_file(road_network: &RoadNetwork, dirpath: &String, fileroot: &String, features: &Features) { unsafe { - maliput_sys::utility::ffi::Utility_GenerateObjFile(road_network, dirpath, fileroot, features); + maliput_sys::utility::ffi::Utility_GenerateObjFile( + road_network.rn.as_ref().map_or(std::ptr::null(), |ref_data| { + ref_data as *const maliput_sys::utility::ffi::RoadNetwork + }), + dirpath, + fileroot, + features, + ); } } pub fn get_obj_description_from_road_network( road_network: &RoadNetwork, - output_directory: String, + features: &Features, ) -> Result> { + let output_directory = std::env::temp_dir().join("maliput"); + if !output_directory.exists() { + let _ = create_dir(&output_directory); + } + let output_directory = path_to_string(output_directory)?; let file_name = String::from("road_network"); - let features = Features { - 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(), - }; generate_obj_file(road_network, &output_directory, &file_name, &features); - let full_path = Path::new(&output_directory); - let full_path = full_path.join(file_name + ".obj"); - let obj_description = read_to_string(&full_path)?; - let _ = remove_file(full_path); + let output_directory = Path::new(&output_directory); + let obj_full_path = output_directory.join(create_file_name(file_name.as_str(), "obj")); + let obj_description = read_to_string(&obj_full_path)?; + remove_file(obj_full_path)?; + let mtl_full_path = output_directory.join(create_file_name(file_name.as_str(), "mtl")); + remove_file(mtl_full_path)?; Ok(obj_description) } + +/// Converts a `PathBuf` to a `String` object. +fn path_to_string(path: PathBuf) -> Result { + Ok(path + .as_os_str() + .to_str() + .ok_or("Failed to get output directory.")? + .to_string()) +} + +/// Returns a `String` concatenating the `extension` to the `file_name`. +fn create_file_name(file_name: &str, extension: &str) -> String { + format!("{}.{}", file_name, extension) +} diff --git a/maliput/tests/obj_file_description_test.rs b/maliput/tests/obj_file_description_test.rs new file mode 100644 index 0000000..b290e4b --- /dev/null +++ b/maliput/tests/obj_file_description_test.rs @@ -0,0 +1,31 @@ +mod common; + +use maliput::utility::{get_obj_description_from_road_network, Features}; + +#[test] +fn get_obj_description_from_road_network_test() { + let road_network = common::create_t_shape_road_network(); + let features = Features { + 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 obj_description = get_obj_description_from_road_network(&road_network, &features); + assert!(obj_description.is_ok()); + let obj_description = obj_description.unwrap(); + assert_ne!(obj_description.len(), 0); +}