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

Convex hull example #256

Merged
merged 23 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
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
103 changes: 0 additions & 103 deletions crates/parry2d/examples/common_macroquad.rs

This file was deleted.

7 changes: 6 additions & 1 deletion crates/parry2d/examples/common_macroquad2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,12 @@ pub fn easy_draw_text(text: &str) {
#[allow(dead_code)]
pub fn lissajous_2d(t: f32) -> Vec2 {
// Some hardcoded parameters to have a pleasing lissajous trajectory.
let (a, b, delta_x, delta_y) = (3.0, 2.0, FRAC_PI_2, FRAC_PI_4);
lissajous_2d_with_params(t, 3.0, 2.0, FRAC_PI_2, FRAC_PI_4)
}

#[allow(dead_code)]
pub fn lissajous_2d_with_params(t: f32, a: f32, b: f32, delta_x: f32, delta_y: f32) -> Vec2 {
// Some hardcoded parameters to have a pleasing lissajous trajectory.

let x = (a * t + delta_x).sin();
let y = (b * t + delta_y).sin();
Expand Down
4 changes: 2 additions & 2 deletions crates/parry2d/examples/convex_hull2d.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
mod common_macroquad;
mod common_macroquad2d;

use std::f32::consts::{FRAC_PI_2, FRAC_PI_4};

use common_macroquad::{draw_point, draw_polygon, lissajous_2d_with_params, na_from_mquad};
use common_macroquad2d::{draw_point, draw_polygon, lissajous_2d_with_params, na_from_mquad};
use macroquad::prelude::*;
use nalgebra::Point2;
use parry2d::transformation;
Expand Down
5 changes: 2 additions & 3 deletions crates/parry2d/examples/point_in_poly2d.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
mod common_macroquad;

use common_macroquad::{draw_point, draw_polygon};
mod common_macroquad2d;
use common_macroquad2d::{draw_point, draw_polygon};
use macroquad::prelude::*;
use nalgebra::{Point2, UnitComplex, Vector2};
use parry2d::utils::point_in_poly2d;
Expand Down
5 changes: 2 additions & 3 deletions crates/parry2d/examples/polygons_intersection2d.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
mod common_macroquad;

use common_macroquad::draw_polygon;
mod common_macroquad2d;
use common_macroquad2d::draw_polygon;
use macroquad::prelude::*;
use nalgebra::{Point2, UnitComplex, Vector2};
use parry2d::shape::Ball;
Expand Down
5 changes: 2 additions & 3 deletions crates/parry2d/examples/project_point2d.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
mod common_macroquad;

use common_macroquad::{draw_line_2d, draw_trimesh2, lissajous_2d, mquad_from_na, na_from_mquad};
mod common_macroquad2d;
use common_macroquad2d::{draw_line_2d, draw_trimesh2, lissajous_2d, mquad_from_na, na_from_mquad};
use macroquad::prelude::*;
use nalgebra::{Point3, UnitComplex, Vector2};
use parry2d::math::{Isometry, Translation};
Expand Down
5 changes: 2 additions & 3 deletions crates/parry2d/examples/raycasts_animated.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
mod common_macroquad;

use common_macroquad::draw_point;
mod common_macroquad2d;
use common_macroquad2d::draw_point;
use macroquad::prelude::*;
use nalgebra::{Isometry2, Point2, UnitComplex, Vector2};
use parry2d::math::Isometry;
Expand Down
14 changes: 12 additions & 2 deletions crates/parry3d/examples/common_macroquad3d.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#[allow(unused, dead_code)]
use std::f32::consts::{FRAC_PI_2, FRAC_PI_4, FRAC_PI_6};

use macroquad::{
Expand Down Expand Up @@ -31,8 +30,19 @@ pub fn na_from_mquad(a: Vec3) -> Point3<Real> {
#[allow(dead_code)]
pub fn lissajous_3d(t: f32) -> Vec3 {
// Some hardcoded parameters to have a pleasing lissajous trajectory.
let (a, b, c, delta_x, delta_y, delta_z) = (3.0, 2.0, 1.0, FRAC_PI_2, FRAC_PI_4, FRAC_PI_6);
lissajous_3d_with_params(t, 3.0, 2.0, 1.0, FRAC_PI_2, FRAC_PI_4, FRAC_PI_6)
}

#[allow(dead_code)]
pub fn lissajous_3d_with_params(
t: f32,
a: f32,
b: f32,
c: f32,
delta_x: f32,
delta_y: f32,
delta_z: f32,
) -> Vec3 {
let x = (a * t + delta_x).sin();
let y = (b * t + delta_y).sin();
let z = (c * t + delta_z).sin();
Expand Down
4 changes: 2 additions & 2 deletions crates/parry3d/examples/convex_hull3d.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
mod common_macroquad;
mod common_macroquad3d;

use std::f32::consts::{FRAC_PI_2, FRAC_PI_4, FRAC_PI_6};

use common_macroquad::{
use common_macroquad3d::{
lissajous_3d_with_params, mquad_from_na, mquad_mesh_from_points, na_from_mquad,
};
use macroquad::prelude::*;
Expand Down
4 changes: 2 additions & 2 deletions crates/parry3d/examples/plane_intersection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use nalgebra::{UnitVector3, Vector3};
use parry3d::query::IntersectResult;
use parry3d::shape::{Cuboid, TriMesh};

mod common_macroquad;
use common_macroquad::*;
mod common_macroquad3d;
use common_macroquad3d::*;

#[macroquad::main("parry3d::query::PlaneIntersection")]
async fn main() {
Expand Down
13 changes: 4 additions & 9 deletions crates/parry3d/examples/project_point3d.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use macroquad::prelude::*;
use nalgebra::Vector3;
use parry3d::math::Isometry;
use parry3d::query::PointQuery;
use parry3d::shape::{Cuboid, TriMesh, TriMeshFlags};

mod common_macroquad;
use common_macroquad::*;
mod common_macroquad3d;
use common_macroquad3d::*;

#[macroquad::main("parry3d::query::PlaneIntersection")]
async fn main() {
Expand All @@ -25,14 +24,10 @@ async fn main() {
let slow_elapsed_time = elapsed_time / 3.0;

let point_to_project = lissajous_3d(slow_elapsed_time);
let projected_point = trimesh.project_point(
&Isometry::identity(),
&na_from_mquad(point_to_project),
true,
);
let projected_point = trimesh.project_local_point(&na_from_mquad(point_to_project), true);

let slow_elapsed_time = slow_elapsed_time * 0.7;
// Going 3d!
// Setup 3D camera.
set_camera(&Camera3D {
position: Vec3::new(
slow_elapsed_time.sin() * 3.0,
Expand Down
51 changes: 22 additions & 29 deletions src/transformation/to_trimesh/cuboid_to_trimesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,14 @@ use crate::math::{Point, Real};
use crate::shape::Cuboid;
use crate::transformation::utils;

#[cfg(feature = "dim3")]
use na::Point3 as PointDim;
#[cfg(feature = "dim3")]
use na::{self, Point3};

#[cfg(feature = "dim2")]
use na::Point2 as PointDim;
#[cfg(feature = "dim2")]
use na::{self, Point2};
Vrixyz marked this conversation as resolved.
Show resolved Hide resolved

impl Aabb {
/// Discretize the boundary of this Aabb as a triangle-mesh.
pub fn to_trimesh(&self) -> (Vec<PointDim<Real>>, Vec<[u32; 3]>) {
pub fn to_trimesh(&self) -> (Vec<Point<Real>>, Vec<[u32; 3]>) {
let center = self.center();
let half_extents = self.half_extents();
let mut cube_mesh = Cuboid::new(half_extents).to_trimesh();
Expand All @@ -26,31 +21,29 @@ impl Aabb {

impl Cuboid {
/// Discretize the boundary of this cuboid as a triangle-mesh.
pub fn to_trimesh(&self) -> (Vec<PointDim<Real>>, Vec<[u32; 3]>) {
pub fn to_trimesh(&self) -> (Vec<Point<Real>>, Vec<[u32; 3]>) {
let (vtx, idx) = unit_cuboid();
(utils::scaled(vtx, self.half_extents * 2.0), idx)
}
}

/**
* Generates a cuboid shape with a split index buffer.
*
* The cuboid is centered at the origin, and has its half extents set to 0.5.
*/
fn unit_cuboid() -> (Vec<PointDim<Real>>, Vec<[u32; 3]>) {
/// Generates a cuboid shape with a split index buffer.
///
/// The cuboid is centered at the origin, and has its half extents set to 0.5.
fn unit_cuboid() -> (Vec<Point<Real>>, Vec<[u32; 3]>) {
#[cfg(feature = "dim3")]
return {
{
let mut coords = Vec::with_capacity(8);
let mut faces = Vec::with_capacity(12);

coords.push(Point3::new(-0.5, -0.5, 0.5));
coords.push(Point3::new(-0.5, -0.5, -0.5));
coords.push(Point3::new(0.5, -0.5, -0.5));
coords.push(Point3::new(0.5, -0.5, 0.5));
coords.push(Point3::new(-0.5, 0.5, 0.5));
coords.push(Point3::new(-0.5, 0.5, -0.5));
coords.push(Point3::new(0.5, 0.5, -0.5));
coords.push(Point3::new(0.5, 0.5, 0.5));
coords.push(Point::new(-0.5, -0.5, 0.5));
coords.push(Point::new(-0.5, -0.5, -0.5));
coords.push(Point::new(0.5, -0.5, -0.5));
coords.push(Point::new(0.5, -0.5, 0.5));
coords.push(Point::new(-0.5, 0.5, 0.5));
coords.push(Point::new(-0.5, 0.5, -0.5));
coords.push(Point::new(0.5, 0.5, -0.5));
coords.push(Point::new(0.5, 0.5, 0.5));

faces.push([4, 5, 0]);
faces.push([5, 1, 0]);
Expand All @@ -71,20 +64,20 @@ fn unit_cuboid() -> (Vec<PointDim<Real>>, Vec<[u32; 3]>) {
faces.push([4, 7, 5]);

(coords, faces)
};
}
#[cfg(feature = "dim2")]
return {
{
let mut coords = Vec::with_capacity(8);
let mut faces = Vec::with_capacity(12);

coords.push(Point2::new(-0.5, -0.5));
coords.push(Point2::new(0.5, -0.5));
coords.push(Point2::new(-0.5, 0.5));
coords.push(Point2::new(0.5, 0.5));
coords.push(Point::new(-0.5, -0.5));
coords.push(Point::new(0.5, -0.5));
coords.push(Point::new(-0.5, 0.5));
coords.push(Point::new(0.5, 0.5));

faces.push([0, 1, 2]);
faces.push([2, 1, 3]);

(coords, faces)
};
}
}
Loading
You are viewing a condensed version of this merge commit. You can view the full changes here.