Polytope

Struct Polytope 

Source
pub struct Polytope<V> { /* private fields */ }
Expand description

A data structure that represents a polytope — a flat sided multi-dimensional object where the vertices are connected by edges, edges by faces, faces by cells, etc.

Vertex type is represented by the type parameter V and it must be implemented by the client depending on the problem at hand. For geometrical applications, it would make sense to represent a vertex as an n-dimensional vector, where n is equal to the dimension of the polytope, but this representation is not forced by the library.

The rest of the elements (edges, faces, cells, etc.) are represented as the list of indices of the lower dimensional bounding elements. For example, an edge bounded by the 4th and 5th vertices is represented as [4, 5].

§Examples

The following code creates a 0-dimensional polytope (i.e. a point).

use polytope::Polytope;
let point = Polytope::<String>::new("".to_string());
assert_eq!(point.dimension(), 0);
assert_eq!(point.vertices().len(), 1);

Given a point, one can create a line, a rectangle and a prism using extrude():

use polytope::Polytope;
let pull_in = |v: &String| v.clone() + "-";
let push_out = |v: &String| v.clone() + "+";
let point = Polytope::<String>::new("".to_string());
let line = point.extrude(&pull_in, &push_out);
let rectangle = line.extrude(&pull_in, &push_out);
let prism = rectangle.extrude(&pull_in, &push_out);
assert_eq!(prism.dimension(), 3);
assert_eq!(prism.vertices().len(), 8);
assert_eq!(prism.elements(0).len(), 12);
assert_eq!(prism.elements(1).len(), 6);
assert_eq!(prism.elements(2).len(), 1);

…or, given a rectangle, one can create double pyramid using cone():

use polytope::Polytope;
let pull_in = |v: &String| v.clone() + "-";
let push_out = |v: &String| v.clone() + "+";
let point = Polytope::<String>::new("".to_string());
let line = point.extrude(&pull_in, &push_out);
let rectangle = line.extrude(&pull_in, &push_out);
let pyramid = rectangle.cone("0000".to_string(), pull_in, push_out);
assert_eq!(pyramid.dimension(), 3);
assert_eq!(pyramid.vertices().len(), 9);
assert_eq!(pyramid.elements(0).len(), 16);
assert_eq!(pyramid.elements(1).len(), 10);
assert_eq!(pyramid.elements(2).len(), 2);

Implementations§

Source§

impl<V> Polytope<V>

Source

pub fn new(vertex: V) -> Self

Constructs a 0-dimensional polytope with one vertex.

Source

pub fn dimension(&self) -> usize

Returns the dimension of the polytope.

This is equal to the dimension of the highest dimensional element in the list of elements.

Source

pub fn vertices(&self) -> &[V]

Lends the vertex list.

Source

pub fn elements(&self, dimension: usize) -> &[Element]

Lends the element list for the given dimension.

Source

pub fn extrude<F1, F2>(&self, pull_in: F1, push_out: F2) -> Self
where F1: Fn(&V) -> V, F2: Fn(&V) -> V,

Extrudes the polytope into the higher dimension.

Two replicas of the polytope is created by applying the functions pull_in and push_out to the vertices. Then, the replicas are linked via higher dimensional elements (vertices via edges, edges via faces, etc.).

This can be used to construct lines out of vertices, rectangle out of lines, etc.

Source

pub fn cone<F1, F2>(&self, apex: V, pull_in: F1, push_out: F2) -> Self
where F1: Fn(&V) -> V, F2: Fn(&V) -> V,

Constructs a higher dimensional double cone out of the polytope.

Two replicas of the polytope is created by applying the functions pull_in and push_out to the vertices. Then, the replicas are linked to the given apex via higher dimensional elements.

This can be used to construct double triangle, double pyramid, double cone, etc.

Trait Implementations§

Source§

impl<V: Debug> Debug for Polytope<V>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<V> Freeze for Polytope<V>

§

impl<V> RefUnwindSafe for Polytope<V>
where V: RefUnwindSafe,

§

impl<V> Send for Polytope<V>
where V: Send,

§

impl<V> Sync for Polytope<V>
where V: Sync,

§

impl<V> Unpin for Polytope<V>

§

impl<V> UnwindSafe for Polytope<V>
where V: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.