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
/*
Copyright 2020 Martin Buck

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the Software
is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall
be included all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

//@todo implement as many traits as possible (see point_cloud_3d for reference)

//! PointCloud3Df32, a collection of positions within 3D space stored lossy as f32 vector for easier usage during rendering

use crate::*;

use std::marker::PhantomData;

//------------------------------------------------------------------------------

#[derive(Debug, PartialEq, PartialOrd, Clone, Default)]
/// PointCloud3Df32, a collection of positions within 3D space stored lossy as f32 vector for easier usage during rendering
pub struct PointCloud3Df32<P>
where
    P: IsBuildable3D,
{
    pub data: Vec<f32>,
    _phantom: PhantomData<P>,
}

impl<P> PointCloud3Df32<P>
where
    P: IsBuildable3D,
{
    /// Creates a new, empty point cloud
    pub fn new() -> PointCloud3Df32<P> {
        PointCloud3Df32 {
            data: Vec::new(),
            _phantom: PhantomData::default(),
        }
    }
    /// Creates a new, empty point cloud with capacity for n points
    pub fn with_capacity(n: usize) -> PointCloud3Df32<P> {
        PointCloud3Df32 {
            data: Vec::with_capacity(3 * n),
            _phantom: PhantomData::default(),
        }
    }
}

//------------------------------------------------------------------------------

impl<P> IsPushable<P> for PointCloud3Df32<P>
where
    P: IsBuildable3D,
{
    fn push(&mut self, p: P) {
        self.data.push(p.x() as f32);
        self.data.push(p.y() as f32);
        self.data.push(p.z() as f32);
    }
    fn reserve(&mut self, n: usize) {
        self.data.reserve(3 * n)
    }
}

impl<P> IsDataContainer<P> for PointCloud3Df32<P>
where
    P: IsBuildable3D,
{
    fn reserve_d(&mut self, n: usize) {
        self.data.reserve(3 * n);
    }

    fn len_d(&self) -> usize {
        self.data.len() / 3
    }

    fn push_d(&mut self, p: P) {
        self.data.push(p.x() as f32);
        self.data.push(p.y() as f32);
        self.data.push(p.z() as f32);
    }

    fn get_d(&self, index: usize) -> P {
        P::new(
            self.data[3 * index + 0] as f64,
            self.data[3 * index + 1] as f64,
            self.data[3 * index + 2] as f64,
        )
    }

    fn set_d(&mut self, index: usize, p: P) {
        self.data[3 * index + 0] = p.x() as f32;
        self.data[3 * index + 1] = p.y() as f32;
        self.data[3 * index + 2] = p.z() as f32;
    }
}

impl<P> IsMovable3D for PointCloud3Df32<P>
where
    P: IsBuildable3D,
{
    fn move_by(&mut self, x: f64, y: f64, z: f64) {
        for index in 0..self.data.len() / 3 {
            self.data[3 * index + 0] += x as f32;
            self.data[3 * index + 1] += y as f32;
            self.data[3 * index + 2] += z as f32;
        }
    }
}

impl<P> HasBoundingBox3DMaybe for PointCloud3Df32<P>
where
    P: IsBuildable3D,
{
    fn bounding_box_maybe(&self) -> Result<BoundingBox3D> {
        let d = &self.data;
        let n_p = d.len() / 3;

        if n_p <= 2 {
            return Err(ErrorKind::TooFewPoints);
        }

        let mut minx = d[0];
        let mut miny = d[1];
        let mut minz = d[2];
        let mut maxx = d[0];
        let mut maxy = d[1];
        let mut maxz = d[2];

        for i in 1..n_p {
            let [x, y, z] = [d[3 * i + 0], d[3 * i + 1], d[3 * i + 2]];
            if x < minx {
                minx = x;
            }
            if y < miny {
                miny = y;
            }
            if z < minz {
                minz = z;
            }
            if x > maxx {
                maxx = x;
            }
            if y > maxy {
                maxy = y;
            }
            if z > maxz {
                maxz = z;
            }
        }

        BoundingBox3D::new(
            &Point3D {
                x: minx as f64,
                y: miny as f64,
                z: minz as f64,
            },
            &Point3D {
                x: maxx as f64,
                y: maxy as f64,
                z: maxz as f64,
            },
        )
    }
}

impl<P> Into<Vec<f32>> for PointCloud3Df32<P>
where
    P: IsBuildable3D,
{
    fn into(self) -> Vec<f32> {
        self.data
    }
}

impl<P> From<Vec<f32>> for PointCloud3Df32<P>
where
    P: IsBuildable3D,
{
    fn from(data: Vec<f32>) -> Self {
        Self {
            data,
            _phantom: PhantomData::default(),
        }
    }
}