#[repr(C)]pub struct Segment {
pub a: Point<f32>,
pub b: Point<f32>,
}Expand description
A line segment shape.
A segment is the simplest 1D shape, defined by two endpoints. It represents a straight line between two points with no thickness or volume.
§Structure
- a: The first endpoint
- b: The second endpoint
- Direction: Points from
atowardb
§Properties
- 1-dimensional: Has length but no width or volume
- Convex: Always convex
- No volume: Mass properties are zero
- Simple: Very fast collision detection
§Use Cases
Segments are commonly used for:
- Thin objects: Ropes, wires, laser beams
- Skeletal animation: Bone connections
- Path representation: Straight-line paths
- Geometry building block: Part of polylines and meshes
- Testing: Simple shape for debugging
§Note
For shapes with thickness, consider using Capsule instead,
which is a segment with a radius (rounded cylinder).
§Example
use parry3d::shape::Segment;
use nalgebra::Point3;
// Create a horizontal segment of length 5
let a = Point3::origin();
let b = Point3::new(5.0, 0.0, 0.0);
let segment = Segment::new(a, b);
assert_eq!(segment.length(), 5.0);
assert_eq!(segment.a, a);
assert_eq!(segment.b, b);Fields§
§a: Point<f32>The first endpoint of the segment.
b: Point<f32>The second endpoint of the segment.
Implementations§
Source§impl Segment
impl Segment
Sourcepub fn bounding_sphere(&self, pos: &Isometry<f32>) -> BoundingSphere
pub fn bounding_sphere(&self, pos: &Isometry<f32>) -> BoundingSphere
Computes the world-space bounding sphere of this segment, transformed by pos.
Sourcepub fn local_bounding_sphere(&self) -> BoundingSphere
pub fn local_bounding_sphere(&self) -> BoundingSphere
Computes the local-space bounding sphere of this segment.
Source§impl Segment
impl Segment
Sourcepub fn canonical_split(
&self,
axis: usize,
bias: f32,
epsilon: f32,
) -> SplitResult<Self>
pub fn canonical_split( &self, axis: usize, bias: f32, epsilon: f32, ) -> SplitResult<Self>
Splits this segment along the given canonical axis.
This will split the segment by a plane with a normal with it’s axis-th component set to 1.
The splitting plane is shifted wrt. the origin by the bias (i.e. it passes through the point
equal to normal * bias).
§Result
Returns the result of the split. The first shape returned is the piece lying on the negative half-space delimited by the splitting plane. The second shape returned is the piece lying on the positive half-space delimited by the splitting plane.
Sourcepub fn local_split(
&self,
local_axis: &UnitVector<f32>,
bias: f32,
epsilon: f32,
) -> SplitResult<Self>
pub fn local_split( &self, local_axis: &UnitVector<f32>, bias: f32, epsilon: f32, ) -> SplitResult<Self>
Splits this segment by a plane identified by its normal local_axis and
the bias (i.e. the plane passes through the point equal to normal * bias).
Sourcepub fn local_split_and_get_intersection(
&self,
local_axis: &UnitVector<f32>,
bias: f32,
epsilon: f32,
) -> (SplitResult<Self>, Option<(Point<f32>, f32)>)
pub fn local_split_and_get_intersection( &self, local_axis: &UnitVector<f32>, bias: f32, epsilon: f32, ) -> (SplitResult<Self>, Option<(Point<f32>, f32)>)
Split a segment with a plane.
This returns the result of the splitting operation, as well as
the intersection point (and barycentric coordinate of this point)
with the plane. The intersection point is None if the plane is
parallel or near-parallel to the segment.
Source§impl Segment
impl Segment
Sourcepub fn from_array(arr: &[Point<f32>; 2]) -> &Segment
pub fn from_array(arr: &[Point<f32>; 2]) -> &Segment
Creates a segment reference from an array of two points.
This is a zero-cost conversion using memory transmutation.
§Example
use parry3d::shape::Segment;
use nalgebra::Point3;
let points = [Point3::origin(), Point3::new(1.0, 0.0, 0.0)];
let segment = Segment::from_array(&points);
assert_eq!(segment.a, points[0]);
assert_eq!(segment.b, points[1]);Sourcepub fn scaled(self, scale: &Vector<f32>) -> Self
pub fn scaled(self, scale: &Vector<f32>) -> Self
Computes a scaled version of this segment.
Each endpoint is scaled component-wise by the scale vector.
§Arguments
scale- The scaling factors for each axis
§Example
use parry3d::shape::Segment;
use nalgebra::{Point3, Vector3};
let segment = Segment::new(
Point3::new(1.0, 2.0, 3.0),
Point3::new(4.0, 5.0, 6.0)
);
let scaled = segment.scaled(&Vector3::new(2.0, 2.0, 2.0));
assert_eq!(scaled.a, Point3::new(2.0, 4.0, 6.0));
assert_eq!(scaled.b, Point3::new(8.0, 10.0, 12.0));Sourcepub fn scaled_direction(&self) -> Vector<f32>
pub fn scaled_direction(&self) -> Vector<f32>
Returns the direction vector of this segment scaled by its length.
This is equivalent to b - a and points from a toward b.
The magnitude equals the segment length.
§Example
use parry3d::shape::Segment;
use nalgebra::{Point3, Vector3};
let segment = Segment::new(
Point3::origin(),
Point3::new(3.0, 4.0, 0.0)
);
let dir = segment.scaled_direction();
assert_eq!(dir, Vector3::new(3.0, 4.0, 0.0));
assert_eq!(dir.norm(), 5.0); // Length of the segmentSourcepub fn length(&self) -> f32
pub fn length(&self) -> f32
Returns the length of this segment.
§Example
use parry3d::shape::Segment;
use nalgebra::Point3;
// 3-4-5 right triangle
let segment = Segment::new(
Point3::origin(),
Point3::new(3.0, 4.0, 0.0)
);
assert_eq!(segment.length(), 5.0);Sourcepub fn swap(&mut self)
pub fn swap(&mut self)
Swaps the two endpoints of this segment.
After swapping, a becomes b and b becomes a.
§Example
use parry3d::shape::Segment;
use nalgebra::Point3;
let mut segment = Segment::new(
Point3::new(1.0, 0.0, 0.0),
Point3::new(5.0, 0.0, 0.0)
);
segment.swap();
assert_eq!(segment.a, Point3::new(5.0, 0.0, 0.0));
assert_eq!(segment.b, Point3::new(1.0, 0.0, 0.0));Sourcepub fn direction(&self) -> Option<Unit<Vector<f32>>>
pub fn direction(&self) -> Option<Unit<Vector<f32>>>
Returns the unit direction vector of this segment.
Points from a toward b with length 1.0.
§Returns
Some(direction)- The normalized direction if the segment has non-zero lengthNone- If both endpoints are equal (degenerate segment)
§Example
use parry3d::shape::Segment;
use nalgebra::{Point3, Vector3};
let segment = Segment::new(
Point3::origin(),
Point3::new(3.0, 4.0, 0.0)
);
if let Some(dir) = segment.direction() {
// Direction is normalized
assert!((dir.norm() - 1.0).abs() < 1e-6);
// Points from a to b
assert_eq!(*dir, Vector3::new(0.6, 0.8, 0.0));
}
// Degenerate segment (zero length)
let degenerate = Segment::new(Point3::origin(), Point3::origin());
assert!(degenerate.direction().is_none());Sourcepub fn scaled_normal(&self) -> Vector<f32>
pub fn scaled_normal(&self) -> Vector<f32>
In 2D, the not-normalized counterclockwise normal of this segment.
Sourcepub fn normal(&self) -> Option<Unit<Vector<f32>>>
pub fn normal(&self) -> Option<Unit<Vector<f32>>>
In 2D, the normalized counterclockwise normal of this segment.
Sourcepub fn transformed(&self, m: &Isometry<f32>) -> Self
pub fn transformed(&self, m: &Isometry<f32>) -> Self
Applies the isometry m to the vertices of this segment and returns the resulting segment.
Sourcepub fn point_at(&self, location: &SegmentPointLocation) -> Point<f32>
pub fn point_at(&self, location: &SegmentPointLocation) -> Point<f32>
Computes the point at the given location.
Trait Implementations§
Source§impl From<Segment> for PolygonalFeature
impl From<Segment> for PolygonalFeature
Source§impl PointQuery for Segment
impl PointQuery for Segment
Source§fn project_local_point(&self, pt: &Point<f32>, solid: bool) -> PointProjection
fn project_local_point(&self, pt: &Point<f32>, solid: bool) -> PointProjection
self. Read moreSource§fn project_local_point_and_get_feature(
&self,
pt: &Point<f32>,
) -> (PointProjection, FeatureId)
fn project_local_point_and_get_feature( &self, pt: &Point<f32>, ) -> (PointProjection, FeatureId)
self and returns the id of the
feature the point was projected on.Source§fn project_local_point_with_max_dist(
&self,
pt: &Point<f32>,
solid: bool,
max_dist: f32,
) -> Option<PointProjection>
fn project_local_point_with_max_dist( &self, pt: &Point<f32>, solid: bool, max_dist: f32, ) -> Option<PointProjection>
Source§fn project_point_with_max_dist(
&self,
m: &Isometry<f32>,
pt: &Point<f32>,
solid: bool,
max_dist: f32,
) -> Option<PointProjection>
fn project_point_with_max_dist( &self, m: &Isometry<f32>, pt: &Point<f32>, solid: bool, max_dist: f32, ) -> Option<PointProjection>
self transformed by m, unless the projection lies further than the given max distance.Source§fn distance_to_local_point(&self, pt: &Point<f32>, solid: bool) -> f32
fn distance_to_local_point(&self, pt: &Point<f32>, solid: bool) -> f32
self.Source§fn contains_local_point(&self, pt: &Point<f32>) -> bool
fn contains_local_point(&self, pt: &Point<f32>) -> bool
self.Source§fn project_point(
&self,
m: &Isometry<f32>,
pt: &Point<f32>,
solid: bool,
) -> PointProjection
fn project_point( &self, m: &Isometry<f32>, pt: &Point<f32>, solid: bool, ) -> PointProjection
self transformed by m.Source§fn distance_to_point(
&self,
m: &Isometry<f32>,
pt: &Point<f32>,
solid: bool,
) -> f32
fn distance_to_point( &self, m: &Isometry<f32>, pt: &Point<f32>, solid: bool, ) -> f32
self transformed by m.Source§fn project_point_and_get_feature(
&self,
m: &Isometry<f32>,
pt: &Point<f32>,
) -> (PointProjection, FeatureId)
fn project_point_and_get_feature( &self, m: &Isometry<f32>, pt: &Point<f32>, ) -> (PointProjection, FeatureId)
self transformed by m and returns the id of the
feature the point was projected on.Source§impl PointQueryWithLocation for Segment
impl PointQueryWithLocation for Segment
Source§type Location = SegmentPointLocation
type Location = SegmentPointLocation
Source§fn project_local_point_and_get_location(
&self,
pt: &Point<f32>,
_: bool,
) -> (PointProjection, Self::Location)
fn project_local_point_and_get_location( &self, pt: &Point<f32>, _: bool, ) -> (PointProjection, Self::Location)
self.Source§fn project_point_and_get_location(
&self,
m: &Isometry<f32>,
pt: &Point<f32>,
solid: bool,
) -> (PointProjection, Self::Location)
fn project_point_and_get_location( &self, m: &Isometry<f32>, pt: &Point<f32>, solid: bool, ) -> (PointProjection, Self::Location)
self transformed by m.Source§fn project_local_point_and_get_location_with_max_dist(
&self,
pt: &Point<f32>,
solid: bool,
max_dist: f32,
) -> Option<(PointProjection, Self::Location)>
fn project_local_point_and_get_location_with_max_dist( &self, pt: &Point<f32>, solid: bool, max_dist: f32, ) -> Option<(PointProjection, Self::Location)>
self, with a maximum projection distance.Source§impl PolygonalFeatureMap for Segment
impl PolygonalFeatureMap for Segment
Source§fn local_support_feature(
&self,
_: &Unit<Vector<f32>>,
out_feature: &mut PolygonalFeature,
)
fn local_support_feature( &self, _: &Unit<Vector<f32>>, out_feature: &mut PolygonalFeature, )
self towards the dir.Source§fn is_convex_polyhedron(&self) -> bool
fn is_convex_polyhedron(&self) -> bool
ConvexPolyhedron?Source§impl RayCast for Segment
impl RayCast for Segment
Source§fn cast_local_ray_and_get_normal(
&self,
ray: &Ray,
max_time_of_impact: f32,
solid: bool,
) -> Option<RayIntersection>
fn cast_local_ray_and_get_normal( &self, ray: &Ray, max_time_of_impact: f32, solid: bool, ) -> Option<RayIntersection>
Source§fn cast_local_ray(
&self,
ray: &Ray,
max_time_of_impact: f32,
solid: bool,
) -> Option<f32>
fn cast_local_ray( &self, ray: &Ray, max_time_of_impact: f32, solid: bool, ) -> Option<f32>
Source§fn intersects_local_ray(&self, ray: &Ray, max_time_of_impact: f32) -> bool
fn intersects_local_ray(&self, ray: &Ray, max_time_of_impact: f32) -> bool
Source§fn cast_ray(
&self,
m: &Isometry<f32>,
ray: &Ray,
max_time_of_impact: f32,
solid: bool,
) -> Option<f32>
fn cast_ray( &self, m: &Isometry<f32>, ray: &Ray, max_time_of_impact: f32, solid: bool, ) -> Option<f32>
Source§fn cast_ray_and_get_normal(
&self,
m: &Isometry<f32>,
ray: &Ray,
max_time_of_impact: f32,
solid: bool,
) -> Option<RayIntersection>
fn cast_ray_and_get_normal( &self, m: &Isometry<f32>, ray: &Ray, max_time_of_impact: f32, solid: bool, ) -> Option<RayIntersection>
Source§impl Shape for Segment
impl Shape for Segment
Source§fn scale_dyn(
&self,
scale: &Vector<f32>,
_num_subdivisions: u32,
) -> Option<Box<dyn Shape>>
fn scale_dyn( &self, scale: &Vector<f32>, _num_subdivisions: u32, ) -> Option<Box<dyn Shape>>
scale into a boxed trait-object. Read moreSource§fn compute_local_aabb(&self) -> Aabb
fn compute_local_aabb(&self) -> Aabb
Aabb of this shape.Source§fn compute_local_bounding_sphere(&self) -> BoundingSphere
fn compute_local_bounding_sphere(&self) -> BoundingSphere
Source§fn compute_aabb(&self, position: &Isometry<f32>) -> Aabb
fn compute_aabb(&self, position: &Isometry<f32>) -> Aabb
Aabb of this shape with the given position.Source§fn mass_properties(&self, _density: f32) -> MassProperties
fn mass_properties(&self, _density: f32) -> MassProperties
fn ccd_thickness(&self) -> f32
fn ccd_angular_thickness(&self) -> f32
Source§fn shape_type(&self) -> ShapeType
fn shape_type(&self) -> ShapeType
Source§fn as_typed_shape(&self) -> TypedShape<'_>
fn as_typed_shape(&self) -> TypedShape<'_>
Source§fn as_support_map(&self) -> Option<&dyn SupportMap>
fn as_support_map(&self) -> Option<&dyn SupportMap>
Source§fn as_polygonal_feature_map(&self) -> Option<(&dyn PolygonalFeatureMap, f32)>
fn as_polygonal_feature_map(&self) -> Option<(&dyn PolygonalFeatureMap, f32)>
Source§fn feature_normal_at_point(
&self,
feature: FeatureId,
_point: &Point<f32>,
) -> Option<Unit<Vector<f32>>>
fn feature_normal_at_point( &self, feature: FeatureId, _point: &Point<f32>, ) -> Option<Unit<Vector<f32>>>
Source§fn clone_box(&self) -> Box<dyn Shape>
fn clone_box(&self) -> Box<dyn Shape>
clone_dynSource§fn compute_bounding_sphere(&self, position: &Isometry<f32>) -> BoundingSphere
fn compute_bounding_sphere(&self, position: &Isometry<f32>) -> BoundingSphere
fn as_composite_shape(&self) -> Option<&dyn CompositeShape>
Source§impl SupportMap for Segment
impl SupportMap for Segment
Source§fn local_support_point(&self, dir: &Vector<f32>) -> Point<f32>
fn local_support_point(&self, dir: &Vector<f32>) -> Point<f32>
impl Copy for Segment
impl StructuralPartialEq for Segment
Auto Trait Implementations§
impl Freeze for Segment
impl RefUnwindSafe for Segment
impl Send for Segment
impl Sync for Segment
impl Unpin for Segment
impl UnwindSafe for Segment
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>, which can then be
downcast into Box<dyn ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>, which can then be further
downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.Source§impl<T> DowncastSend for T
impl<T> DowncastSend for T
Source§impl<T> DowncastSync for T
impl<T> DowncastSync for T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.