1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
//! Wrapper enum for 3D primitives

use cgmath::{BaseFloat, Point3, Vector3};
use cgmath::prelude::*;

use {Aabb3, Ray3};
use prelude::*;
use primitive::{Capsule, ConvexPolyhedron, Cube, Cuboid, Cylinder, Particle3, Quad, Sphere};

/// Wrapper enum for 3D primitives, that also implements the `Primitive` trait, making it easier
/// to use many different primitives in algorithms.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum Primitive3<S>
where
    S: BaseFloat,
{
    /// Particle
    Particle(Particle3<S>),
    /// Rectangular plane
    Quad(Quad<S>),
    /// Sphere
    Sphere(Sphere<S>),
    /// Cuboid
    Cuboid(Cuboid<S>),
    /// Cube
    Cube(Cube<S>),
    /// Cylinder
    Cylinder(Cylinder<S>),
    /// Capsule
    Capsule(Capsule<S>),
    /// Convex polyhedron with any number of vertices/faces
    ConvexPolyhedron(ConvexPolyhedron<S>),
}

impl<S> From<Particle3<S>> for Primitive3<S>
where
    S: BaseFloat,
{
    fn from(particle: Particle3<S>) -> Primitive3<S> {
        Primitive3::Particle(particle)
    }
}

impl<S> From<Quad<S>> for Primitive3<S>
where
    S: BaseFloat,
{
    fn from(quad: Quad<S>) -> Self {
        Primitive3::Quad(quad)
    }
}

impl<S> From<Sphere<S>> for Primitive3<S>
where
    S: BaseFloat,
{
    fn from(sphere: Sphere<S>) -> Primitive3<S> {
        Primitive3::Sphere(sphere)
    }
}

impl<S> From<Cube<S>> for Primitive3<S>
where
    S: BaseFloat,
{
    fn from(cuboid: Cube<S>) -> Primitive3<S> {
        Primitive3::Cube(cuboid)
    }
}

impl<S> From<Cuboid<S>> for Primitive3<S>
where
    S: BaseFloat,
{
    fn from(cuboid: Cuboid<S>) -> Primitive3<S> {
        Primitive3::Cuboid(cuboid)
    }
}

impl<S> From<Cylinder<S>> for Primitive3<S>
where
    S: BaseFloat,
{
    fn from(cylinder: Cylinder<S>) -> Primitive3<S> {
        Primitive3::Cylinder(cylinder)
    }
}

impl<S> From<Capsule<S>> for Primitive3<S>
where
    S: BaseFloat,
{
    fn from(capsule: Capsule<S>) -> Primitive3<S> {
        Primitive3::Capsule(capsule)
    }
}

impl<S> From<ConvexPolyhedron<S>> for Primitive3<S>
where
    S: BaseFloat,
{
    fn from(polyhedron: ConvexPolyhedron<S>) -> Primitive3<S> {
        Primitive3::ConvexPolyhedron(polyhedron)
    }
}

impl<S> ComputeBound<Aabb3<S>> for Primitive3<S>
where
    S: BaseFloat,
{
    fn compute_bound(&self) -> Aabb3<S> {
        match *self {
            Primitive3::Particle(_) => Aabb3::zero(),
            Primitive3::Quad(ref quad) => quad.compute_bound(),
            Primitive3::Cuboid(ref cuboid) => cuboid.compute_bound(),
            Primitive3::Cube(ref cuboid) => cuboid.compute_bound(),
            Primitive3::Sphere(ref sphere) => sphere.compute_bound(),
            Primitive3::Cylinder(ref cylinder) => cylinder.compute_bound(),
            Primitive3::Capsule(ref capsule) => capsule.compute_bound(),
            Primitive3::ConvexPolyhedron(ref polyhedron) => polyhedron.compute_bound(),
        }
    }
}

impl<S> ComputeBound<::volume::Sphere<S>> for Primitive3<S>
where
    S: BaseFloat,
{
    fn compute_bound(&self) -> ::volume::Sphere<S> {
        match *self {
            Primitive3::Particle(_) => ::volume::Sphere {
                center: Point3::origin(),
                radius: S::zero(),
            },
            Primitive3::Quad(ref quad) => quad.compute_bound(),
            Primitive3::Cuboid(ref cuboid) => cuboid.compute_bound(),
            Primitive3::Cube(ref cuboid) => cuboid.compute_bound(),
            Primitive3::Sphere(ref sphere) => sphere.compute_bound(),
            Primitive3::Cylinder(ref cylinder) => cylinder.compute_bound(),
            Primitive3::Capsule(ref capsule) => capsule.compute_bound(),
            Primitive3::ConvexPolyhedron(ref polyhedron) => polyhedron.compute_bound(),
        }
    }
}

impl<S> Primitive for Primitive3<S>
where
    S: BaseFloat,
{
    type Point = Point3<S>;

    fn support_point<T>(&self, direction: &Vector3<S>, transform: &T) -> Point3<S>
    where
        T: Transform<Point3<S>>,
    {
        match *self {
            Primitive3::Particle(_) => transform.transform_point(Point3::origin()),
            Primitive3::Quad(ref quad) => quad.support_point(direction, transform),
            Primitive3::Sphere(ref sphere) => sphere.support_point(direction, transform),
            Primitive3::Cuboid(ref cuboid) => cuboid.support_point(direction, transform),
            Primitive3::Cube(ref cuboid) => cuboid.support_point(direction, transform),
            Primitive3::Cylinder(ref cylinder) => cylinder.support_point(direction, transform),
            Primitive3::Capsule(ref capsule) => capsule.support_point(direction, transform),
            Primitive3::ConvexPolyhedron(ref polyhedron) => {
                polyhedron.support_point(direction, transform)
            }
        }
    }
}

impl<S> DiscreteTransformed<Ray3<S>> for Primitive3<S>
where
    S: BaseFloat,
{
    type Point = Point3<S>;

    fn intersects_transformed<T>(&self, ray: &Ray3<S>, transform: &T) -> bool
    where
        T: Transform<Self::Point>,
    {
        match *self {
            Primitive3::Particle(ref particle) => particle.intersects_transformed(ray, transform),
            Primitive3::Quad(ref quad) => quad.intersects_transformed(ray, transform),
            Primitive3::Sphere(ref sphere) => sphere.intersects_transformed(ray, transform),
            Primitive3::Cuboid(ref cuboid) => cuboid.intersects_transformed(ray, transform),
            Primitive3::Cube(ref cuboid) => cuboid.intersects_transformed(ray, transform),
            Primitive3::Cylinder(ref cylinder) => cylinder.intersects_transformed(ray, transform),
            Primitive3::Capsule(ref capsule) => capsule.intersects_transformed(ray, transform),
            Primitive3::ConvexPolyhedron(ref polyhedron) => {
                polyhedron.intersects_transformed(ray, transform)
            }
        }
    }
}

impl<S> ContinuousTransformed<Ray3<S>> for Primitive3<S>
where
    S: BaseFloat,
{
    type Point = Point3<S>;
    type Result = Point3<S>;

    fn intersection_transformed<T>(&self, ray: &Ray3<S>, transform: &T) -> Option<Point3<S>>
    where
        T: Transform<Point3<S>>,
    {
        match *self {
            Primitive3::Particle(ref particle) => particle.intersection_transformed(ray, transform),
            Primitive3::Quad(ref quad) => quad.intersection_transformed(ray, transform),
            Primitive3::Sphere(ref sphere) => sphere.intersection_transformed(ray, transform),
            Primitive3::Cuboid(ref cuboid) => cuboid.intersection_transformed(ray, transform),
            Primitive3::Cube(ref cuboid) => cuboid.intersection_transformed(ray, transform),
            Primitive3::Cylinder(ref cylinder) => cylinder.intersection_transformed(ray, transform),
            Primitive3::Capsule(ref capsule) => capsule.intersection_transformed(ray, transform),
            Primitive3::ConvexPolyhedron(ref polyhedron) => {
                polyhedron.intersection_transformed(ray, transform)
            }
        }
    }
}