voro_rs 0.3.0

A Rust binding for voro++ library
Documentation
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
//! Wall and related classes.

#[cxx::bridge(namespace = "voro")]
pub mod ffi {
    unsafe extern "C++" {
        include!("voro_rs/src/boilerplate.hh");

        type voronoicell = crate::cell::ffi::voronoicell;
        type voronoicell_neighbor =
            crate::cell::ffi::voronoicell_neighbor;

        type wall_sphere;
        #[rust_name = "new_wall_sphere"]
        fn construct(
            xc_: f64,
            yc_: f64,
            zc_: f64,
            rc_: f64,
            w_id_: i32,
        ) -> UniquePtr<wall_sphere>;
        #[rust_name = "clone_wall_sphere"]
        fn clone_wall(
            value: &UniquePtr<wall_sphere>,
        ) -> UniquePtr<wall_sphere>;
        fn point_inside(
            self: Pin<&mut wall_sphere>,
            x: f64,
            y: f64,
            z: f64,
        ) -> bool;
        #[rust_name = "cut_cell_0"]
        fn cut_cell(
            self: Pin<&mut wall_sphere>,
            c: Pin<&mut voronoicell>,
            x: f64,
            y: f64,
            z: f64,
        ) -> bool;
        #[rust_name = "cut_cell_1"]
        fn cut_cell(
            self: Pin<&mut wall_sphere>,
            c: Pin<&mut voronoicell_neighbor>,
            x: f64,
            y: f64,
            z: f64,
        ) -> bool;

        type wall_plane;
        #[rust_name = "new_wall_plane"]
        fn construct(
            xc_: f64,
            yc_: f64,
            zc_: f64,
            rc_: f64,
            w_id_: i32,
        ) -> UniquePtr<wall_plane>;
        #[rust_name = "clone_wall_plane"]
        fn clone_wall(
            value: &UniquePtr<wall_plane>,
        ) -> UniquePtr<wall_plane>;
        fn point_inside(
            self: Pin<&mut wall_plane>,
            x: f64,
            y: f64,
            z: f64,
        ) -> bool;
        #[rust_name = "cut_cell_0"]
        fn cut_cell(
            self: Pin<&mut wall_plane>,
            c: Pin<&mut voronoicell>,
            x: f64,
            y: f64,
            z: f64,
        ) -> bool;
        #[rust_name = "cut_cell_1"]
        fn cut_cell(
            self: Pin<&mut wall_plane>,
            c: Pin<&mut voronoicell_neighbor>,
            x: f64,
            y: f64,
            z: f64,
        ) -> bool;

        type wall_cylinder;
        #[rust_name = "new_wall_cylinder"]
        fn construct(
            xc_: f64,
            yc_: f64,
            zc_: f64,
            xa_: f64,
            ya_: f64,
            za_: f64,
            rc_: f64,
            w_id_: i32,
        ) -> UniquePtr<wall_cylinder>;
        #[rust_name = "clone_wall_cylinder"]
        fn clone_wall(
            value: &UniquePtr<wall_cylinder>,
        ) -> UniquePtr<wall_cylinder>;
        fn point_inside(
            self: Pin<&mut wall_cylinder>,
            x: f64,
            y: f64,
            z: f64,
        ) -> bool;
        #[rust_name = "cut_cell_0"]
        fn cut_cell(
            self: Pin<&mut wall_cylinder>,
            c: Pin<&mut voronoicell>,
            x: f64,
            y: f64,
            z: f64,
        ) -> bool;
        #[rust_name = "cut_cell_1"]
        fn cut_cell(
            self: Pin<&mut wall_cylinder>,
            c: Pin<&mut voronoicell_neighbor>,
            x: f64,
            y: f64,
            z: f64,
        ) -> bool;

        type wall_cone;
        #[rust_name = "new_wall_cone"]
        fn construct(
            xc_: f64,
            yc_: f64,
            zc_: f64,
            xa_: f64,
            ya_: f64,
            za_: f64,
            ang: f64,
            w_id_: i32,
        ) -> UniquePtr<wall_cone>;
        #[rust_name = "clone_wall_cone"]
        fn clone_wall(
            value: &UniquePtr<wall_cone>,
        ) -> UniquePtr<wall_cone>;
        fn point_inside(
            self: Pin<&mut wall_cone>,
            x: f64,
            y: f64,
            z: f64,
        ) -> bool;
        #[rust_name = "cut_cell_0"]
        fn cut_cell(
            self: Pin<&mut wall_cone>,
            c: Pin<&mut voronoicell>,
            x: f64,
            y: f64,
            z: f64,
        ) -> bool;
        #[rust_name = "cut_cell_1"]
        fn cut_cell(
            self: Pin<&mut wall_cone>,
            c: Pin<&mut voronoicell_neighbor>,
            x: f64,
            y: f64,
            z: f64,
        ) -> bool;

        type wall;
        fn wall_sphere_to_wall<'a>(
            w: Pin<&'a mut wall_sphere>,
        ) -> Pin<&'a mut wall>;
        fn wall_plane_to_wall<'a>(
            w: Pin<&'a mut wall_plane>,
        ) -> Pin<&'a mut wall>;
        fn wall_cylinder_to_wall<'a>(
            w: Pin<&'a mut wall_cylinder>,
        ) -> Pin<&'a mut wall>;
        fn wall_cone_to_wall<'a>(
            w: Pin<&'a mut wall_cone>,
        ) -> Pin<&'a mut wall>;
    }
}

use crate::prelude::{VoroCell, VoroCellNbr, VoroCellSgl};
use cxx::UniquePtr;

type DVec3 = [f64; 3];

/// `wall_sphere` class in voro++.
///
/// A class representing a spherical wall object.
pub struct WallSphere {
    pub(crate) inner: UniquePtr<ffi::wall_sphere>,
}

impl WallSphere {
    /// Constructs a spherical wall object.
    ///
    /// * `c`: a position vector for the sphere's center.
    /// * `r`: the radius of the sphere.
    pub fn new(c: DVec3, r: f64) -> Self {
        Self::new_with_id(c, r, -99)
    }

    /// Constructs a spherical wall object.
    ///
    /// * `c`: a position vector for the sphere's center.
    /// * `r`: the radius of the sphere.
    /// * `id`: an ID number to associate with the wall for neighbor tracking.
    pub fn new_with_id(c: DVec3, r: f64, id: i32) -> Self {
        Self {
            inner: ffi::new_wall_sphere(
                c[0], c[1], c[2], r, id,
            ),
        }
    }
}

/// `wall_plane` class in voro++.
///
/// A class representing a plane wall object.
pub struct WallPlane {
    pub(crate) inner: UniquePtr<ffi::wall_plane>,
}

impl WallPlane {
    /// Constructs a plane wall object.
    ///
    /// * `c`: a normal vector to the plane.
    /// * `a`: a displacement along the normal vector.
    pub fn new(c: DVec3, a: f64) -> Self {
        Self::new_with_id(c, a, -99)
    }

    /// Constructs a plane wall object.
    ///
    /// * `c`: a normal vector to the plane.
    /// * `a`: a displacement along the normal vector.
    /// * `id`: an ID number to associate with the wall for neighbor tracking.
    pub fn new_with_id(c: DVec3, a: f64, id: i32) -> Self {
        Self {
            inner: ffi::new_wall_plane(
                c[0], c[1], c[2], a, id,
            ),
        }
    }
}

/// `wall_cylinder` class in voro++.
///
/// A class representing a cylindrical wall object.
pub struct WallCylinder {
    pub(crate) inner: UniquePtr<ffi::wall_cylinder>,
}

impl WallCylinder {
    /// Constructs a cylinder wall object.
    ///
    /// * `c`: a point on the axis of the cylinder.
    /// * `a`: a vector pointing along the direction of the cylinder.
    /// * `r`: the radius of the cylinder.
    pub fn new(c: DVec3, a: DVec3, r: f64) -> Self {
        Self::new_with_id(c, a, r, -99)
    }

    /// Constructs a cylinder wall object.
    ///
    /// * `c`: a point on the axis of the cylinder.
    /// * `a`: a vector pointing along the direction of the cylinder.
    /// * `r`: the radius of the cylinder.
    /// * `id`: an ID number to associate with the wall for neighbor tracking.
    pub fn new_with_id(
        c: DVec3,
        a: DVec3,
        r: f64,
        id: i32,
    ) -> Self {
        Self {
            inner: ffi::new_wall_cylinder(
                c[0], c[1], c[2], a[0], a[1], a[2], r, id,
            ),
        }
    }
}

/// `wall_cone` class in voro++.
///
/// A class representing a conical wall object.
pub struct WallCone {
    pub(crate) inner: UniquePtr<ffi::wall_cone>,
}

impl WallCone {
    /// Constructs a cone wall object.
    ///
    /// * `c`: the apex of the cone.
    /// * `a`: a vector pointing along the axis of the cone.
    /// * `ang`: the angle (in radians) of the cone, measured from the axis.
    pub fn new(c: DVec3, a: DVec3, ang: f64) -> Self {
        Self::new_with_id(c, a, ang, -99)
    }

    /// Constructs a cone wall object.
    ///
    /// * `c`: the apex of the cone.
    /// * `a`: a vector pointing along the axis of the cone.
    /// * `ang`: the angle (in radians) of the cone, measured from the axis.
    /// * `id`: an ID number to associate with the wall for neighbor tracking.
    pub fn new_with_id(
        c: DVec3,
        a: DVec3,
        ang: f64,
        id: i32,
    ) -> Self {
        Self {
            inner: ffi::new_wall_cone(
                c[0], c[1], c[2], a[0], a[1], a[2], ang, id,
            ),
        }
    }
}

impl Clone for WallSphere {
    fn clone(&self) -> Self {
        Self {
            inner: ffi::clone_wall_sphere(&self.inner),
        }
    }
}

impl Clone for WallPlane {
    fn clone(&self) -> Self {
        Self {
            inner: ffi::clone_wall_plane(&self.inner),
        }
    }
}

impl Clone for WallCylinder {
    fn clone(&self) -> Self {
        Self {
            inner: ffi::clone_wall_cylinder(&self.inner),
        }
    }
}

impl Clone for WallCone {
    fn clone(&self) -> Self {
        Self {
            inner: ffi::clone_wall_cone(&self.inner),
        }
    }
}

/// A part of trait `Wall` whose parameter does not depends on cell type.
pub trait Wall0 {
    /// Tests to see whether a point is inside the sphere wall object.
    ///
    /// * `x,y,z`: the vector to test.
    ///
    /// Return true if the point is inside, false if the point is outside.
    fn point_inside(&mut self, xyz: DVec3) -> bool;
}

impl Wall0 for WallSphere {
    fn point_inside(&mut self, xyz: DVec3) -> bool {
        self.inner
            .pin_mut()
            .point_inside(xyz[0], xyz[1], xyz[2])
    }
}

impl Wall0 for WallPlane {
    fn point_inside(&mut self, xyz: DVec3) -> bool {
        self.inner
            .pin_mut()
            .point_inside(xyz[0], xyz[1], xyz[2])
    }
}

impl Wall0 for WallCylinder {
    fn point_inside(&mut self, xyz: DVec3) -> bool {
        self.inner
            .pin_mut()
            .point_inside(xyz[0], xyz[1], xyz[2])
    }
}

impl Wall0 for WallCone {
    fn point_inside(&mut self, xyz: DVec3) -> bool {
        self.inner
            .pin_mut()
            .point_inside(xyz[0], xyz[1], xyz[2])
    }
}

/// A part of trait `Wall` whose parameter depends on cell type.
pub trait Wall1<T: VoroCell> {
    /// Cuts a cell by the sphere wall object. The spherical wall is approximated by
    /// a single plane applied at the point on the sphere which is closest to the center
    /// of the cell. This works well for particle arrangements that are packed against
    /// the wall, but loses accuracy for sparse particle distributions.
    ///
    /// * `cell`: the Voronoi cell to be cut.
    /// * `xyz`: the location of the Voronoi cell.
    ///
    /// Return true if the cell still exists, false if the cell is deleted.
    fn cut_cell(
        &mut self,
        cell: &mut T,
        xyz: DVec3,
    ) -> bool;
}

impl Wall1<VoroCellSgl> for WallSphere {
    fn cut_cell(
        &mut self,
        cell: &mut VoroCellSgl,
        xyz: DVec3,
    ) -> bool {
        self.inner.pin_mut().cut_cell_0(
            cell.inner.pin_mut(),
            xyz[0],
            xyz[1],
            xyz[2],
        )
    }
}

impl Wall1<VoroCellNbr> for WallSphere {
    fn cut_cell(
        &mut self,
        cell: &mut VoroCellNbr,
        xyz: DVec3,
    ) -> bool {
        self.inner.pin_mut().cut_cell_1(
            cell.inner.pin_mut(),
            xyz[0],
            xyz[1],
            xyz[2],
        )
    }
}

impl Wall1<VoroCellSgl> for WallPlane {
    fn cut_cell(
        &mut self,
        cell: &mut VoroCellSgl,
        xyz: DVec3,
    ) -> bool {
        self.inner.pin_mut().cut_cell_0(
            cell.inner.pin_mut(),
            xyz[0],
            xyz[1],
            xyz[2],
        )
    }
}

impl Wall1<VoroCellNbr> for WallPlane {
    fn cut_cell(
        &mut self,
        cell: &mut VoroCellNbr,
        xyz: DVec3,
    ) -> bool {
        self.inner.pin_mut().cut_cell_1(
            cell.inner.pin_mut(),
            xyz[0],
            xyz[1],
            xyz[2],
        )
    }
}

impl Wall1<VoroCellSgl> for WallCylinder {
    fn cut_cell(
        &mut self,
        cell: &mut VoroCellSgl,
        xyz: DVec3,
    ) -> bool {
        self.inner.pin_mut().cut_cell_0(
            cell.inner.pin_mut(),
            xyz[0],
            xyz[1],
            xyz[2],
        )
    }
}

impl Wall1<VoroCellNbr> for WallCylinder {
    fn cut_cell(
        &mut self,
        cell: &mut VoroCellNbr,
        xyz: DVec3,
    ) -> bool {
        self.inner.pin_mut().cut_cell_1(
            cell.inner.pin_mut(),
            xyz[0],
            xyz[1],
            xyz[2],
        )
    }
}

impl Wall1<VoroCellSgl> for WallCone {
    fn cut_cell(
        &mut self,
        cell: &mut VoroCellSgl,
        xyz: DVec3,
    ) -> bool {
        self.inner.pin_mut().cut_cell_0(
            cell.inner.pin_mut(),
            xyz[0],
            xyz[1],
            xyz[2],
        )
    }
}

impl Wall1<VoroCellNbr> for WallCone {
    fn cut_cell(
        &mut self,
        cell: &mut VoroCellNbr,
        xyz: DVec3,
    ) -> bool {
        self.inner.pin_mut().cut_cell_1(
            cell.inner.pin_mut(),
            xyz[0],
            xyz[1],
            xyz[2],
        )
    }
}

/// `wall` abstract class in voro++.
///
/// This is a trait for a generic wall object. A wall object
/// can be specified by deriving a new struct from this and specifying the
/// functions.
pub trait Wall:
    Wall0 + Wall1<VoroCellSgl> + Wall1<VoroCellNbr>
{
}
impl Wall for WallSphere {}
impl Wall for WallPlane {}
impl Wall for WallCylinder {}
impl Wall for WallCone {}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::prelude::{VoroCell, VoroCellSgl};

    #[test]
    fn basic_test() {
        let mut w0 =
            WallSphere::new([10.0, 0.0, 0.0], 10.0);
        let mut c0 = VoroCellSgl::new(
            [-1.0, -1.0, -1.0],
            [1.0, 1.0, 1.0],
        );

        assert_eq!(c0.volume(), 8.0);

        let mut c1 = c0.clone();
        assert_eq!(c1.volume(), 8.0);

        let b = w0.cut_cell(&mut c1, [0.0, 0.0, 0.0]);
        assert_eq!(c0.volume(), 8.0);
        assert_eq!(c1.volume(), 4.0);
        assert_eq!(b, true);

        let mut w1 = w0.clone();
        let c = w1.cut_cell(&mut c0, [0.0, 0.0, 0.0]);
        assert_eq!(c0.volume(), 4.0);
        assert_eq!(c, true);
    }

    #[test]
    fn test_nbr() {
        let mut w0 =
            WallSphere::new([10.0, 0.0, 0.0], 10.0);
        let mut c0 = VoroCellNbr::new(
            [-1.0, -1.0, -1.0],
            [1.0, 1.0, 1.0],
        );

        w0.cut_cell(&mut c0, [0.0, 0.0, 0.0]);
        assert_eq!(c0.volume(), 4.0);
    }

    #[test]
    fn overload_test() {
        let mut w0 =
            WallSphere::new([10.0, 0.0, 0.0], 10.0);
        let mut c0 = VoroCellSgl::new(
            [-1.0, -1.0, -1.0],
            [1.0, 1.0, 1.0],
        );
        let mut c1 = VoroCellSgl::new(
            [-1.0, -1.0, -1.0],
            [1.0, 1.0, 1.0],
        );

        w0.cut_cell(&mut c0, [0.0, 0.0, 0.0]);
        w0.cut_cell(&mut c1, [0.0, 0.0, 0.0]);
    }

    #[test]
    fn all_types() {
        WallPlane::new([10.0, 0.0, 0.0], 10.0);
        WallPlane::new([1.0, 0.0, 0.0], 10.0);
        WallCylinder::new(
            [0.0, 0.0, 0.0],
            [1.0, 0.0, 0.0],
            10.0,
        );
        WallCone::new(
            [0.0, 0.0, 0.0],
            [1.0, 0.0, 0.0],
            1.0,
        );
    }
}