Skip to content

Commit

Permalink
Merge pull request #1 from adamnemecek/master
Browse files Browse the repository at this point in the history
use self where possible
  • Loading branch information
mrDIMAS committed Sep 20, 2020
2 parents a1a3fdd + ca4ddf4 commit 2f041d9
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 44 deletions.
72 changes: 34 additions & 38 deletions src/convex_shape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,9 @@ impl Visit for Axis {
id.visit(name, visitor)?;
if visitor.is_reading() {
*self = match id {
0 => Axis::X,
1 => Axis::Y,
2 => Axis::Z,
0 => Self::X,
1 => Self::Y,
2 => Self::Z,
_ => return Err(VisitError::User("Invalid axis".to_owned()))
}
}
Expand All @@ -246,11 +246,7 @@ pub struct CapsuleShape {

impl CircumRadius for CapsuleShape {
fn circumradius(&self) -> f32 {
if self.radius > self.height {
self.radius
} else {
self.height
}
self.radius.max(self.height)
}
}

Expand Down Expand Up @@ -394,47 +390,47 @@ macro_rules! define_is_as {
impl CircumRadius for ConvexShape {
fn circumradius(&self) -> f32 {
match self {
ConvexShape::Dummy => 0.0,
ConvexShape::Box(box_shape) => box_shape.circumradius(),
ConvexShape::Sphere(sphere) => sphere.circumradius(),
ConvexShape::Capsule(capsule) => capsule.circumradius(),
ConvexShape::Triangle(triangle) => triangle.circumradius(),
ConvexShape::PointCloud(point_cloud) => point_cloud.circumradius(),
Self::Dummy => 0.0,
Self::Box(box_shape) => box_shape.circumradius(),
Self::Sphere(sphere) => sphere.circumradius(),
Self::Capsule(capsule) => capsule.circumradius(),
Self::Triangle(triangle) => triangle.circumradius(),
Self::PointCloud(point_cloud) => point_cloud.circumradius(),
}
}
}

impl ConvexShape {
pub fn get_farthest_point(&self, position: Vec3, direction: Vec3) -> Vec3 {
position + match self {
ConvexShape::Dummy => Vec3::ZERO,
ConvexShape::Box(box_shape) => box_shape.get_farthest_point(direction),
ConvexShape::Sphere(sphere) => sphere.get_farthest_point(direction),
ConvexShape::Capsule(capsule) => capsule.get_farthest_point(direction),
ConvexShape::Triangle(triangle) => triangle.get_farthest_point(direction),
ConvexShape::PointCloud(point_cloud) => point_cloud.get_farthest_point(direction),
Self::Dummy => Vec3::ZERO,
Self::Box(box_shape) => box_shape.get_farthest_point(direction),
Self::Sphere(sphere) => sphere.get_farthest_point(direction),
Self::Capsule(capsule) => capsule.get_farthest_point(direction),
Self::Triangle(triangle) => triangle.get_farthest_point(direction),
Self::PointCloud(point_cloud) => point_cloud.get_farthest_point(direction),
}
}

pub fn id(&self) -> i32 {
match self {
ConvexShape::Dummy => 0,
ConvexShape::Box(_) => 1,
ConvexShape::Sphere(_) => 2,
ConvexShape::Capsule(_) => 3,
ConvexShape::Triangle(_) => 4,
ConvexShape::PointCloud(_) => 5,
Self::Dummy => 0,
Self::Box(_) => 1,
Self::Sphere(_) => 2,
Self::Capsule(_) => 3,
Self::Triangle(_) => 4,
Self::PointCloud(_) => 5,
}
}

pub fn new(id: i32) -> Result<Self, String> {
match id {
0 => Ok(ConvexShape::Dummy),
1 => Ok(ConvexShape::Box(Default::default())),
2 => Ok(ConvexShape::Sphere(Default::default())),
3 => Ok(ConvexShape::Capsule(Default::default())),
4 => Ok(ConvexShape::Triangle(Default::default())),
5 => Ok(ConvexShape::PointCloud(Default::default())),
0 => Ok(Self::Dummy),
1 => Ok(Self::Box(Default::default())),
2 => Ok(Self::Sphere(Default::default())),
3 => Ok(Self::Capsule(Default::default())),
4 => Ok(Self::Triangle(Default::default())),
5 => Ok(Self::PointCloud(Default::default())),
_ => Err("Invalid shape id!".to_owned())
}
}
Expand All @@ -449,12 +445,12 @@ impl ConvexShape {
impl Visit for ConvexShape {
fn visit(&mut self, name: &str, visitor: &mut Visitor) -> VisitResult {
match self {
ConvexShape::Dummy => Ok(()),
ConvexShape::Box(box_shape) => box_shape.visit(name, visitor),
ConvexShape::Sphere(sphere) => sphere.visit(name, visitor),
ConvexShape::Capsule(capsule) => capsule.visit(name, visitor),
ConvexShape::Triangle(triangle) => triangle.visit(name, visitor),
ConvexShape::PointCloud(point_cloud) => point_cloud.visit(name, visitor),
Self::Dummy => Ok(()),
Self::Box(box_shape) => box_shape.visit(name, visitor),
Self::Sphere(sphere) => sphere.visit(name, visitor),
Self::Capsule(capsule) => capsule.visit(name, visitor),
Self::Triangle(triangle) => triangle.visit(name, visitor),
Self::PointCloud(point_cloud) => point_cloud.visit(name, visitor),
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ impl Clone for Physics {
}

impl Physics {
pub fn new() -> Physics {
Physics {
pub fn new() -> Self {
Self {
bodies: Pool::new(),
static_geoms: Pool::new(),
query_buffer: Default::default(),
Expand Down
4 changes: 2 additions & 2 deletions src/rigid_body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ impl Clone for RigidBody {
}

impl RigidBody {
pub fn new(shape: ConvexShape) -> RigidBody {
RigidBody {
pub fn new(shape: ConvexShape) -> Self {
Self {
position: Vec3::ZERO,
last_position: Vec3::ZERO,
acceleration: Vec3::ZERO,
Expand Down
4 changes: 2 additions & 2 deletions src/static_geometry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ pub struct StaticGeometry {
impl StaticGeometry {
pub const OCTREE_THRESHOLD: usize = 64;

pub fn new(triangles: Vec<StaticTriangle>) -> StaticGeometry {
pub fn new(triangles: Vec<StaticTriangle>) -> Self {
let raw_triangles: Vec<[Vec3; 3]> = triangles.iter().map(|st| st.points).collect();

StaticGeometry {
Self {
octree: Octree::new(&raw_triangles, Self::OCTREE_THRESHOLD),
triangles
}
Expand Down

0 comments on commit 2f041d9

Please sign in to comment.