Skip to content

Commit

Permalink
Adds testing for get_obj_description_from_road_network method.
Browse files Browse the repository at this point in the history
Signed-off-by: Santiago Lopez <[email protected]>
  • Loading branch information
Santoi committed Oct 10, 2024
1 parent 822a777 commit 193fd4a
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 31 deletions.
2 changes: 1 addition & 1 deletion maliput/src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ impl<'a> RoadGeometry<'a> {
/// println!("num_junctions: {}", road_geometry.num_junctions());
/// ```
pub struct RoadNetwork {
rn: cxx::UniquePtr<maliput_sys::api::ffi::RoadNetwork>,
pub(crate) rn: cxx::UniquePtr<maliput_sys::api::ffi::RoadNetwork>,
}

impl RoadNetwork {
Expand Down
72 changes: 42 additions & 30 deletions maliput/src/utility/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Box<dyn Error>> {
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<String, &'static str> {
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)
}
31 changes: 31 additions & 0 deletions maliput/tests/obj_file_description_test.rs
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit 193fd4a

Please sign in to comment.