Skip to content

Commit

Permalink
Example convex_decomposition (#266)
Browse files Browse the repository at this point in the history
  • Loading branch information
Vrixyz authored Dec 6, 2024
1 parent 5cd3877 commit 37663ce
Show file tree
Hide file tree
Showing 15 changed files with 134 additions and 12 deletions.
2 changes: 1 addition & 1 deletion crates/parry2d/examples/aabb2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use parry2d::shape::Ball;

const RENDER_SCALE: f32 = 30.0;

#[macroquad::main("parry2d::utils::point_in_poly2d")]
#[macroquad::main("aabb2d")]
async fn main() {
let render_pos = Vec2::new(300.0, 300.0);

Expand Down
2 changes: 1 addition & 1 deletion crates/parry2d/examples/bounding_sphere2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use parry2d::shape::Cuboid;

const RENDER_SCALE: f32 = 30.0;

#[macroquad::main("parry2d::utils::point_in_poly2d")]
#[macroquad::main("bounding_sphere2d")]
async fn main() {
let render_pos = Vec2::new(300.0, 300.0);

Expand Down
2 changes: 1 addition & 1 deletion crates/parry2d/examples/convex_hull2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use parry2d::transformation;

const RENDER_SCALE: f32 = 30.0;

#[macroquad::main("parry2d::utils::point_in_poly2d")]
#[macroquad::main("convex_hull2d")]
async fn main() {
let count = 9;
let mut pts = vec![Point2::default(); count];
Expand Down
2 changes: 1 addition & 1 deletion crates/parry2d/examples/point_in_poly2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use parry2d::utils::point_in_poly2d;

const RENDER_SCALE: f32 = 30.0;

#[macroquad::main("parry2d::utils::point_in_poly2d")]
#[macroquad::main("points_in_poly2d")]
async fn main() {
let mut spikes = spikes_polygon();
let mut squares = squares_polygon();
Expand Down
2 changes: 1 addition & 1 deletion crates/parry2d/examples/polygons_intersection2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use parry2d::transformation::polygons_intersection_points;

const RENDER_SCALE: f32 = 30.0;

#[macroquad::main("parry2d::utils::polygons_intersection_points")]
#[macroquad::main("polygons_intersection2d")]
async fn main() {
let spikes = spikes_polygon();
let mut animated_spikes = spikes.clone();
Expand Down
2 changes: 1 addition & 1 deletion crates/parry2d/examples/project_point2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use parry2d::math::{Isometry, Translation};
use parry2d::query::PointQuery;
use parry2d::shape::{Cuboid, TriMesh, TriMeshFlags};

#[macroquad::main("parry3d::query::PlaneIntersection")]
#[macroquad::main("project_point2d")]
async fn main() {
//
// This is useful to test for https://github.com/dimforge/parry/pull/248
Expand Down
2 changes: 1 addition & 1 deletion crates/parry2d/examples/raycasts_animated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use parry2d::shape::Cuboid;

const RENDER_SCALE: f32 = 30.0;

#[macroquad::main("parry2d::query::RayCast")]
#[macroquad::main("raycasts_animated")]
async fn main() {
let animation_scale = 1.4;
let animation_rotation = 0.04;
Expand Down
6 changes: 6 additions & 0 deletions crates/parry3d/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -208,3 +208,9 @@ doc-scrape-examples = true
name = "time_of_impact_query3d"
path = "examples/time_of_impact_query3d.rs"
doc-scrape-examples = true

[[example]]
name = "convex_decomposition"
path = "examples/convex_decomposition.rs"
doc-scrape-examples = true
required-features = ["wavefront"]
2 changes: 1 addition & 1 deletion crates/parry3d/examples/aabb3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use na::Isometry3;
use parry3d::bounding_volume::{Aabb, BoundingVolume};
use parry3d::shape::Ball;

#[macroquad::main("parry2d::utils::point_in_poly2d")]
#[macroquad::main("aabb3d")]
async fn main() {
let camera_pos = Vec3::new(8f32, 8f32, 12f32);

Expand Down
2 changes: 1 addition & 1 deletion crates/parry3d/examples/bounding_sphere3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use na::{Isometry3, Vector3};
use parry3d::bounding_volume::BoundingVolume;
use parry3d::shape::Cuboid;

#[macroquad::main("parry2d::utils::point_in_poly2d")]
#[macroquad::main("bounding_sphere3d")]
async fn main() {
let camera_pos = Vec3::new(8f32, 8f32, 12f32);

Expand Down
14 changes: 14 additions & 0 deletions crates/parry3d/examples/common_macroquad3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,20 @@ pub fn na_from_mquad(a: Vec3) -> Point3<Real> {
Point3::new(a.x, a.y, a.z)
}

/// Converts a hue (from 0..=1) to rgb
#[allow(dead_code)]
pub fn hue_to_rgb(h: f32) -> (f32, f32, f32) {
let kr = (5.0 + h * 6.0).rem_euclid(6.0);
let kg = (3.0 + h * 6.0).rem_euclid(6.0);
let kb = (1.0 + h * 6.0).rem_euclid(6.0);

let r = 1.0 - kr.min(4.0 - kr).min(1.0).max(0.0);
let g = 1.0 - kg.min(4.0 - kg).min(1.0).max(0.0);
let b = 1.0 - kb.min(4.0 - kb).min(1.0).max(0.0);

(r, g, b)
}

/// Returns [lissajous curve](https://en.wikipedia.org/wiki/Lissajous_curve) coordinates for time `t`.
///
/// This uses hardcoded parameters to have an arbitrary pleasing trajectory.
Expand Down
102 changes: 102 additions & 0 deletions crates/parry3d/examples/convex_decomposition.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
mod common_macroquad3d;

extern crate nalgebra as na;

use common_macroquad3d::{easy_draw_text, hue_to_rgb, mquad_mesh_from_points};
use macroquad::prelude::*;
use na::Point3;
use obj::{Obj, ObjData};
use parry3d::{
math::Real,
shape::{SharedShape, TriMesh, TriMeshFlags},
};

#[macroquad::main("convex_decomposition")]
async fn main() {
/*
* Initialize the shapes.
*/
let Obj {
data: ObjData {
position, objects, ..
},
..
} = Obj::load("assets/tests/low_poly_bunny.obj").unwrap();

let bunny_mesh = TriMesh::with_flags(
position
.iter()
.map(|v| Point3::new(v[0] as Real, v[1] as Real, v[2] as Real))
.collect::<Vec<_>>(),
objects[0].groups[0]
.polys
.iter()
.map(|p| [p.0[0].0 as u32, p.0[1].0 as u32, p.0[2].0 as u32])
.collect::<Vec<_>>(),
TriMeshFlags::all(),
)
.unwrap();
clear_background(BLACK);

easy_draw_text("Please wait while convex decomposition is being computed...");
#[cfg(debug_assertions)]
{
macroquad::text::draw_text(
"Running in debug mode is significantly slower than with `--release`.",
10.0,
48.0 + 48.0,
26.0,
RED,
);
}
next_frame().await;
let mesh_vertices = bunny_mesh.vertices();
let mesh_indices = bunny_mesh.indices();
let convex_mesh = SharedShape::convex_decomposition(&mesh_vertices, &mesh_indices);
let trimesh_convex_compound = convex_mesh.as_compound().unwrap();

let shapes_count = trimesh_convex_compound.shapes().len() as u32;
let mut meshes = Vec::new();
for (i, s) in trimesh_convex_compound.shapes().iter().enumerate() {
let trimesh_convex = s.1.as_convex_polyhedron().unwrap().to_trimesh();

/*
* Make render meshes out of the shapes.
*/
let (r, g, b) = hue_to_rgb(i as f32 / 6 as f32);
let mesh = mquad_mesh_from_points(
&trimesh_convex,
Vec3::new(1f32, 3f32, 3f32),
Color::from_rgba(
(r as f32 * 255.0) as u8,
(g as f32 * 255.0) as u8,
(b as f32 * 255.0) as u8,
255,
),
);
meshes.push(mesh);
}

loop {
clear_background(BLACK);
let elapsed_time = get_time() as f32;
let camera_pos = Vec3::new(
5.5f32 * elapsed_time.sin(),
3f32,
5.5f32 * elapsed_time.cos(),
);
// Initialize 3D camera.
set_camera(&Camera3D {
position: camera_pos,
up: Vec3::new(0f32, 1f32, 0f32),
target: Vec3::new(0f32, 1f32, 0f32),
..Default::default()
});
for mesh in &meshes {
draw_mesh(mesh);
}
set_default_camera();
easy_draw_text(&format!("Number of shapes: {}", shapes_count));
next_frame().await
}
}
2 changes: 1 addition & 1 deletion crates/parry3d/examples/convex_hull3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use macroquad::prelude::*;
use nalgebra::Point3;
use parry3d::transformation;

#[macroquad::main("parry2d::utils::point_in_poly2d")]
#[macroquad::main("convex_hull3d")]
async fn main() {
let count = 9;
let mut pts = vec![Point3::default(); count];
Expand Down
2 changes: 1 addition & 1 deletion crates/parry3d/examples/plane_intersection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use parry3d::shape::{Cuboid, TriMesh};
mod common_macroquad3d;
use common_macroquad3d::*;

#[macroquad::main("parry3d::query::PlaneIntersection")]
#[macroquad::main("plane_intersection")]
async fn main() {
let trimesh = Cuboid::new(Vector3::repeat(1.0)).to_trimesh();

Expand Down
2 changes: 1 addition & 1 deletion crates/parry3d/examples/project_point3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use parry3d::shape::{Cuboid, TriMesh, TriMeshFlags};
mod common_macroquad3d;
use common_macroquad3d::*;

#[macroquad::main("parry3d::query::PlaneIntersection")]
#[macroquad::main("project_point3d")]
async fn main() {
let trimesh = Cuboid::new(Vector3::new(0.2, 0.5, 1.0)).to_trimesh();

Expand Down

0 comments on commit 37663ce

Please sign in to comment.