Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example convex_decomposition #266

Merged
merged 35 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from 34 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
44c81cc
example for projection (+ bug detected)
Vrixyz Jul 31, 2024
9526e75
simpler project example
Vrixyz Jul 31, 2024
113046d
use lissajous trajectory + small polish pass
Vrixyz Jul 31, 2024
ed6a112
zoom in
Vrixyz Aug 14, 2024
8c09f1a
Merge branch 'master' into projection_example
Vrixyz Aug 14, 2024
b181c65
scrape plane intersection
Vrixyz Aug 14, 2024
2a68514
expose cuboid::to_trimesh for parry2d + add 2d example for projectpoint
Vrixyz Aug 14, 2024
065f6b6
fix local point inside the shape
Vrixyz Aug 14, 2024
174f012
common macroquad code in their example
Vrixyz Aug 14, 2024
3955d44
use a shaded cube for project_point2d too.
Vrixyz Aug 14, 2024
4bbe298
lighter cube
Vrixyz Sep 3, 2024
2dcadc9
fix plane intersection code with common code
Vrixyz Sep 3, 2024
d61b05b
removed a few unused variables
Vrixyz Sep 3, 2024
8ab7b5c
2d examples use common macroquad code
Vrixyz Sep 3, 2024
8057422
add example for convex hull
Vrixyz Sep 3, 2024
feda1a6
3d example for convex hull
Vrixyz Sep 3, 2024
39d7f8e
3d example for convex hull
Vrixyz Sep 3, 2024
aa711e6
add example for aabb2d
Vrixyz Sep 3, 2024
335639f
example for aabb in 3d
Vrixyz Sep 3, 2024
35c557f
remove a few unused code
Vrixyz Sep 3, 2024
1bf510f
bounding sphere example using macroquad
Vrixyz Sep 4, 2024
359b76b
add a note about float imprecisions
Vrixyz Sep 4, 2024
607ac1f
simpler aabb 3d function
Vrixyz Sep 4, 2024
cafcad4
Merge branch 'example_aabb' into example_bounding_sphere
Vrixyz Sep 4, 2024
cfd62f4
3d example for bounding spheres
Vrixyz Sep 4, 2024
1b4c792
experiments on convex_decomposition ; infinite loop
Vrixyz Sep 11, 2024
674a8f9
non-crashing version
Vrixyz Sep 11, 2024
cb7c924
Merge branch 'master' into example_decomposition
Vrixyz Nov 19, 2024
92998ca
fix incorrect merge + remove moved files
Vrixyz Nov 19, 2024
aab99b8
add convex_decomposition to scraped examples
Vrixyz Nov 19, 2024
dad2f58
Squashed commit of the following:
Vrixyz Nov 25, 2024
4e9ad1d
fix clippy
Vrixyz Nov 27, 2024
5e1b244
rename macroquad examples
Vrixyz Nov 27, 2024
e425dca
decompose rabbit + add warning against running in debug
Vrixyz Dec 2, 2024
ad0abfd
apply PR suggestions
Vrixyz Dec 6, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 test_points = grid_points();
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 @@ -206,3 +206,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
98 changes: 98 additions & 0 deletions crates/parry3d/examples/convex_decomposition.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
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...");
Vrixyz marked this conversation as resolved.
Show resolved Hide resolved
#[cfg(debug_assertions)]
{
macroquad::text::draw_text(
"Running in debug mode is way slower than with `--release`.",
Vrixyz marked this conversation as resolved.
Show resolved Hide resolved
10.0,
48.0 + 48.0,
30.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();

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()
});

let shapes_count = trimesh_convex_compound.shapes().len() as u32;
for (i, s) in trimesh_convex_compound.shapes().iter().enumerate() {
let trimesh_convex = s.1.as_convex_polyhedron().unwrap().to_trimesh();
Vrixyz marked this conversation as resolved.
Show resolved Hide resolved

/*
* Display 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,
),
);
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