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 all commits
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
7 changes: 6 additions & 1 deletion crates/parry2d/examples/common_macroquad2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,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
57 changes: 40 additions & 17 deletions crates/parry2d/examples/convex_hull2d.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,44 @@
extern crate nalgebra as na;
mod common_macroquad2d;

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

use common_macroquad2d::{draw_point, draw_polygon, lissajous_2d_with_params, na_from_mquad};
use macroquad::prelude::*;
use nalgebra::Point2;
use parry2d::transformation;

fn main() {
let points = vec![
Point2::new(0.77705324, 0.05374551),
Point2::new(0.35096353, 0.9873069),
Point2::new(0.09537989, 0.44411153),
Point2::new(0.108208835, 0.72445065),
Point2::new(0.7661844, 0.86163324),
Point2::new(0.5185994, 0.66594696),
Point2::new(0.768981, 0.23657233),
Point2::new(0.058607936, 0.09037298),
Point2::new(0.8818559, 0.3804205),
Point2::new(0.9571466, 0.17664945),
];

let _ = transformation::convex_hull(&points[..]);
const RENDER_SCALE: f32 = 30.0;

#[macroquad::main("parry2d::utils::point_in_poly2d")]
async fn main() {
let count = 9;
let mut pts = vec![Point2::default(); count];

let render_pos = Point2::new(300.0, 300.0);

loop {
let elapsed_time = get_time() as f32;
let elapsed_time_slow = elapsed_time * 0.2;
clear_background(BLACK);

for (i, pt) in pts.iter_mut().enumerate() {
*pt = na_from_mquad(lissajous_2d_with_params(
(i * i) as f32 + elapsed_time_slow,
2.0 + i as f32 / 3.0,
(i as f32 / count as f32) + elapsed_time_slow.cos() * 0.1,
(elapsed_time_slow as f32 + i as f32).cos() * 0.1 + FRAC_PI_2,
FRAC_PI_4,
)) * 5f32;
draw_point(*pt, RENDER_SCALE, render_pos, RED);
}

/*
*
* Compute the convex hull.
*
*/
let convex_hull = transformation::convex_hull(&pts);
draw_polygon(&convex_hull, RENDER_SCALE, render_pos, WHITE);
next_frame().await
}
}
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
63 changes: 47 additions & 16 deletions crates/parry3d/examples/convex_hull3d.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,52 @@
extern crate nalgebra as na;
mod common_macroquad3d;

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

use common_macroquad3d::{
lissajous_3d_with_params, mquad_from_na, mquad_mesh_from_points, na_from_mquad,
};
use macroquad::prelude::*;
use nalgebra::Point3;
use parry3d::transformation;

fn main() {
let points = vec![
Point3::new(0.77705324, 0.05374551, 0.9822232),
Point3::new(0.35096353, 0.9873069, 0.28922123),
Point3::new(0.09537989, 0.44411153, 0.05486667),
Point3::new(0.108208835, 0.72445065, 0.6669141),
Point3::new(0.7661844, 0.86163324, 0.80507314),
Point3::new(0.5185994, 0.66594696, 0.072779536),
Point3::new(0.768981, 0.23657233, 0.44346774),
Point3::new(0.058607936, 0.09037298, 0.017009139),
Point3::new(0.8818559, 0.3804205, 0.25173646),
Point3::new(0.9571466, 0.17664945, 0.6029223),
];
#[macroquad::main("parry2d::utils::point_in_poly2d")]
async fn main() {
let count = 9;
let mut pts = vec![Point3::default(); count];

let camera_pos = Vec3::new(8.0, 8.0, 8.0);
loop {
let elapsed_time = get_time() as f32;
let elapsed_time_slow = elapsed_time * 0.2;
clear_background(BLACK);

let _ = transformation::convex_hull(&points[..]);
for (i, pt) in pts.iter_mut().enumerate() {
*pt = na_from_mquad(lissajous_3d_with_params(
(i * i) as f32 + elapsed_time_slow,
2.0 + i as f32 / 3.0,
1f32 + (i as f32).sin() * 0.2,
(i as f32 / count as f32) + elapsed_time_slow.cos() * 0.1,
(elapsed_time_slow as f32 + i as f32).cos() * 0.1 + FRAC_PI_2,
FRAC_PI_4,
FRAC_PI_6,
)) * 5f32;
draw_sphere(mquad_from_na(*pt), 0.1f32, None, RED);
}
// Initialize 3D camera.
set_camera(&Camera3D {
position: camera_pos,
up: Vec3::new(0f32, 1f32, 0f32),
target: Vec3::new(0.5f32, 0f32, 0.5f32),
..Default::default()
});
/*
*
* Compute the convex hull.
*
*/
let convex_hull = transformation::convex_hull(&pts);
let mesh = mquad_mesh_from_points(&convex_hull, Vec3::new(5.0, 10.0, 3.0), DARKGRAY);
draw_mesh(&mesh);
next_frame().await
}
}