groan_rs 0.11.3

Gromacs Analysis Library for Rust
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
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
// Released under MIT License.
// Copyright (c) 2023-2025 Ladislav Bartos

//! Implementation of shapes for geometry selection.

use dyn_clone::DynClone;

use crate::structures::{dimension::Dimension, simbox::SimBox, vector3d::Vector3D};

/// Structure describing a sphere for geometry selections.
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
pub struct Sphere {
    /// Coordinates of the center of the sphere.
    position: Vector3D,
    /// Radius of the sphere.
    radius: f32,
}

/// Structure describing a rectangular box for geometry selections.
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
pub struct Rectangular {
    /// Coordinates of the box origin.
    position: Vector3D,
    /// Length of the box side along the x-axis.
    x: f32,
    /// Length of the box side along the y-axis.
    y: f32,
    /// Length of the box side along the z-axis.
    z: f32,
}

/// Structure describing a cylinder for geometry selections.
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
pub struct Cylinder {
    /// Coordinates of the cylinder base center.
    position: Vector3D,
    /// Radius of the cylinder.
    radius: f32,
    /// Height of the cylinder.
    height: f32,
    /// Orientation of the cylinder in space.
    orientation: Dimension,
    /// Plane in which the cylinder base is placed.
    plane: Dimension,
}

/// Structure describing a triangular prism for geometry selections.
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
pub struct TriangularPrism {
    /// Coordinates of the vertices of the base.
    base1: Vector3D,
    base2: Vector3D,
    base3: Vector3D,
    /// Height of the prism.
    height: f32,
    /// Orientation of the triangular prism in space.
    orientation: Dimension,
    /// Plane in which the triangular prism base is placed.
    plane: Dimension,
}

/// Any structure implementing this trait can be used for geometry selection.
pub trait Shape: DynClone {
    /// Returns `true` if target point is inside the `Shape`. Else returns `false`.
    fn inside(&self, point: &Vector3D, simbox: &SimBox) -> bool;
}

dyn_clone::clone_trait_object!(Shape);

impl Sphere {
    /// Construct a new Sphere.
    ///
    /// ## Arguments
    /// - `position` - Coordinates of the center of the sphere.
    /// - `radius` - Radius of the sphere.
    ///
    /// ## Example
    /// ```no_run
    /// # use groan_rs::prelude::*;
    /// #
    /// // constructs a sphere with radius 2.0 nm
    /// let sphere = Sphere::new(
    ///     Vector3D::new(1.0, 2.0, 3.0), // position of the sphere center
    ///     2.0,                           // radius of the sphere
    /// );
    /// ```
    pub fn new(position: Vector3D, radius: f32) -> Self {
        Sphere { position, radius }
    }

    /// Get position of the center of the sphere.
    pub fn get_position(&self) -> &Vector3D {
        &self.position
    }

    /// Get radius of the sphere.
    pub fn get_radius(&self) -> f32 {
        self.radius
    }
}

impl Shape for Sphere {
    /// Check if point is inside the sphere. Takes PBC into consideration.
    ///
    /// For PBC-unsafe variant, see [Sphere::inside_naive].
    fn inside(&self, point: &Vector3D, simbox: &SimBox) -> bool {
        point.distance(&self.position, Dimension::XYZ, simbox) < self.radius
    }
}

impl Rectangular {
    /// Construct a new Rectangular box.
    ///
    /// ## Arguments
    /// - `position` - Coordinates of the box origin.
    /// - `x` - Length of the box side along the x-axis.
    /// - `y` - Length of the box side along the y-axis.
    /// - `z` - Length of the box side along the z-axis.
    ///
    /// ## Example
    /// ```no_run
    /// # use groan_rs::prelude::*;
    /// #
    /// // constructs a rectangular box of specified size
    /// let rect = Rectangular::new(
    ///     Vector3D::new(1.0, 2.0, 3.0), // position of box origin
    ///     1.0,                          // size of the box along x-dimension
    ///     2.0,                          // size of the box along y-dimension
    ///     3.0,                          // size of the box along z-dimension
    /// );
    /// ```
    pub fn new(position: Vector3D, x: f32, y: f32, z: f32) -> Self {
        Rectangular { position, x, y, z }
    }

    /// Get coordinate of the box origin.
    pub fn get_position(&self) -> &Vector3D {
        &self.position
    }

    /// Get length of the box side along the x-axis.
    pub fn get_x(&self) -> f32 {
        self.x
    }

    /// Get length of the box side along the y-axis.
    pub fn get_y(&self) -> f32 {
        self.y
    }

    /// Get length of the box side along the z-axis.
    pub fn get_z(&self) -> f32 {
        self.z
    }
}

impl Shape for Rectangular {
    /// Check if point is inside the rectangular box. Takes PBC into consideration.
    ///
    /// For PBC-unsafe variant, see [Rectangular::inside_naive].
    fn inside(&self, point: &Vector3D, simbox: &SimBox) -> bool {
        let mut dx = point.distance(&self.position, Dimension::X, simbox);
        if dx < 0.0 {
            dx += simbox.x;
        }
        let mut dy = point.distance(&self.position, Dimension::Y, simbox);
        if dy < 0.0 {
            dy += simbox.y;
        }
        let mut dz = point.distance(&self.position, Dimension::Z, simbox);
        if dz < 0.0 {
            dz += simbox.z;
        }

        dx <= self.x && dy <= self.y && dz <= self.z
    }
}

impl Cylinder {
    /// Construct a new Cylinder.
    ///
    /// ## Arguments
    /// - `position` - Coordinates of the center of the cylinder's base.
    /// - `orientation` - Orientation of the cylinder in space. Only X, Y, and Z are supported.
    /// - `radius` - Radius of the cylinder.
    /// - `height` - Height of the cylinder along the main axis of the cylinder.
    ///
    /// ## Panics
    /// Panics if `orientation` is not `Dimension::X`, `Dimension::Y`, nor `Dimension::Z`.
    ///
    /// ## Example
    /// ```no_run
    /// # use groan_rs::prelude::*;
    /// #
    /// // constructs a cylinder oriented along the z-dimension of the simulation box
    /// let cylinder = Cylinder::new(
    ///     Vector3D::new(1.0, 2.0, 3.0), // position
    ///     1.5,                          // radius
    ///     2.0,                          // height
    ///     Dimension::Z,                 // orientation
    /// );
    /// ```
    pub fn new(position: Vector3D, radius: f32, height: f32, orientation: Dimension) -> Self {
        let plane = match orientation {
            Dimension::X => Dimension::YZ,
            Dimension::Y => Dimension::XZ,
            Dimension::Z => Dimension::XY,
            d => panic!(
                "FATAL GROAN ERROR | Cylinder::new | Unsupported orientation dimension '{}'.",
                d
            ),
        };

        Cylinder {
            position,
            radius,
            height,
            orientation,
            plane,
        }
    }

    /// Get position of the center of the cylinder's base.
    pub fn get_position(&self) -> &Vector3D {
        &self.position
    }

    /// Get radius of the cylinder.
    pub fn get_radius(&self) -> f32 {
        self.radius
    }

    /// Get height of the cylinder
    pub fn get_height(&self) -> f32 {
        self.height
    }

    /// Get orientation of the cylinder
    pub fn get_orientation(&self) -> Dimension {
        self.orientation
    }
}

impl Shape for Cylinder {
    /// Check if point is inside the cylinder. Takes PBC into consideration.
    ///
    /// For PBC-unsafe variant, see [Cylinder::inside_naive].
    fn inside(&self, point: &Vector3D, simbox: &SimBox) -> bool {
        let mut distance_axis = point.distance(&self.position, self.orientation, simbox);

        if distance_axis < 0.0 {
            match self.orientation {
                Dimension::X => distance_axis += simbox.x,
                Dimension::Y => distance_axis += simbox.y,
                Dimension::Z => distance_axis += simbox.z,
                d => panic!("FATAL GROAN ERROR | Cylinder::inside | Orientation dimension '{}' should never occur in a cylinder shape.", d),
            }
        }

        if distance_axis > self.height
            || point.distance(&self.position, self.plane, simbox) > self.radius
        {
            return false;
        }

        true
    }
}

impl TriangularPrism {
    /// Construct a new Triangular Prism.
    ///
    /// ## Arguments
    /// - `base1`, `base2`, `base3` - Coordinates of the vertices of the prism's base.
    /// - `height` - Perpendicular distance between the two bases of the prism.
    ///
    /// ## Panics
    /// Panics if the base of the prism is *not* entirely localized in the XY, XZ, or YZ plane.
    /// Panics if the base can't be constructed.
    ///
    /// ## Example
    /// ```no_run
    /// # use groan_rs::prelude::*;
    /// #
    /// // constructs a triangular prism oriented along the x-dimension of the simulation box
    /// let prism = TriangularPrism::new(
    ///     Vector3D::new(1.0, 2.0, 3.0),  // first vertex of the base
    ///     Vector3D::new(1.0, 4.5, 5.3),  // second vertex of the base
    ///     Vector3D::new(1.0, 2.3, 1.5),  // third vertex of the base
    ///     4.2                            // height of the prism
    /// );
    /// ```
    ///
    /// ## Notes
    /// - Periodic boundary conditions are not applied to the base of the triangular prism.
    ///   In other words, if you define the base of a triangular prism with a point
    ///   that is outside of the box boundaries, you will only select atoms located in the area
    ///   enclosed by the base itself, not by the periodic image of the base.
    ///
    /// ```text
    ///     ╔════════════════╗
    ///     ║                ║
    ///     ║       A        ║
    ///     ║    ###         ║
    ///     ║#######         ║
    ///  C  ║#####B       C'!║
    ///     ║                ║
    ///     ║                ║
    ///     ╚════════════════╝
    ///
    /// ```
    ///   `A`, `B`, `C` specify vertices of the base, `#` roughly specifies areas that are considered
    ///   to be inside the base. `C'` specifies periodic image of `C`.
    ///   Areas marked with `!` are *NOT* included in the prism, even though they could be if we considered PBC.
    ///
    /// - The above-described behavior may change in future versions of the `groan_rs` library!
    ///
    /// - Since the upper base of the prism is defined relative to the bottom base (using the `height` attribute),
    ///   periodic boundary conditions *ARE* used in this case. In other words,
    ///   if you define a prism with a height that would position the upper base outside of the box,
    ///   the atoms will be selected based on the periodic image of the upper base.
    ///
    /// ```text
    ///     ╔════════════════╗
    ///     ║      #######   ║
    ///     ║      #######   ║
    ///     ║      A#####B   ║
    ///     ║                ║
    ///     ║                ║
    ///     ║      #######   ║
    ///     ║      #######   ║
    ///     ╚════════════════╝
    ///
    /// ```
    ///   (The vertex `C` is not seen.)
    pub fn new(base1: Vector3D, base2: Vector3D, base3: Vector3D, height: f32) -> Self {
        let bases = [
            (base1.x, base2.x, base3.x, Dimension::X, Dimension::YZ),
            (base1.y, base2.y, base3.y, Dimension::Y, Dimension::XZ),
            (base1.z, base2.z, base3.z, Dimension::Z, Dimension::XY),
        ];

        let mut orientation = None;
        let mut plane = Dimension::None;

        for (val1, val2, val3, current_orientation, current_plane) in bases.iter() {
            if val1 == val2 && val2 == val3 {
                if orientation.is_some() {
                    panic!("FATAL GROAN ERROR | TriangularPrism::new | Base of the requested TriangularPrism can not be constructed.");
                }
                orientation = Some(*current_orientation);
                plane = *current_plane;
            }
        }

        if orientation.is_none() {
            panic!("FATAL GROAN ERROR | TriangularPrism::new | Base of the requested TriangularPrism does not lie in xy, xz, nor yz plane.");
        }

        // determine position of the bases

        TriangularPrism {
            base1,
            base2,
            base3,
            height,
            orientation: orientation.unwrap(),
            plane,
        }
    }

    /// Get coordinates of the first vertex of the base.
    pub fn get_base1(&self) -> &Vector3D {
        &self.base1
    }

    /// Get coordinates of the second vertex of the base.
    pub fn get_base2(&self) -> &Vector3D {
        &self.base2
    }

    /// Get coordinates of the third vertex of the base.
    pub fn get_base3(&self) -> &Vector3D {
        &self.base3
    }

    /// Get height of the triangular prism.
    pub fn get_height(&self) -> f32 {
        self.height
    }

    /// Get orientation of the triangular prism.
    pub fn get_orientation(&self) -> Dimension {
        self.orientation
    }

    /// Get plane of the base of the triangular prism.
    pub fn get_plane(&self) -> Dimension {
        self.plane
    }

    fn sign(point1: &Vector3D, point2: &Vector3D, point3: &Vector3D, plane: Dimension) -> f32 {
        match plane {
            Dimension::XY => {
                (point1.x - point3.x) * (point2.y - point3.y)
                    - (point2.x - point3.x) * (point1.y - point3.y)
            }
            Dimension::XZ => {
                (point1.x - point3.x) * (point2.z - point3.z)
                    - (point2.x - point3.x) * (point1.z - point3.z)
            }
            Dimension::YZ => {
                (point1.y - point3.y) * (point2.z - point3.z)
                    - (point2.y - point3.y) * (point1.z - point3.z)
            }
            _ => panic!(
                "FATAL GROAN ERROR | TriangularPrism::sign | This dimension should never occur."
            ),
        }
    }
}

impl Shape for TriangularPrism {
    /// Check if point is inside the triangular prism.
    ///
    /// Adapted from `https://stackoverflow.com/questions/2049582/how-to-determine-if-a-point-is-in-a-2d-triangle`
    fn inside(&self, point: &Vector3D, simbox: &SimBox) -> bool {
        let mut distance_from_base =
            point.distance(self.get_base1(), self.get_orientation(), simbox);

        if distance_from_base < 0.0 {
            match self.get_orientation() {
                Dimension::X => distance_from_base += simbox.x,
                Dimension::Y => distance_from_base += simbox.y,
                Dimension::Z => distance_from_base += simbox.z,
                d => panic!("FATAL GROAN ERROR | TriangularPrism::inside | Orientation dimension '{}' should never occur in a triangular prism.", d),
            }
        }

        if distance_from_base >= self.get_height() {
            return false;
        }

        let d1 = TriangularPrism::sign(point, self.get_base1(), self.get_base2(), self.get_plane());
        let d2 = TriangularPrism::sign(point, self.get_base2(), self.get_base3(), self.get_plane());
        let d3 = TriangularPrism::sign(point, self.get_base3(), self.get_base1(), self.get_plane());

        let has_neg = (d1 < 0.0) || (d2 < 0.0) || (d3 < 0.0);
        let has_pos = (d1 > 0.0) || (d2 > 0.0) || (d3 > 0.0);

        !(has_neg && has_pos)
    }
}

/// Trait implemented by structures that can be used for geometry selection without PBC.
pub trait NaiveShape: DynClone {
    /// Returns `true` if target point is inside the `NaiveShape`. **Does not take periodic boundary conditions into consideration.**
    fn inside_naive(&self, point: &Vector3D) -> bool;
}

impl NaiveShape for Sphere {
    /// Check if point is inside the sphere. **Does not take PBC into consideration.**
    ///
    /// For PBC-safe variant, see [Sphere::inside].
    fn inside_naive(&self, point: &Vector3D) -> bool {
        point.distance_naive(&self.position, Dimension::XYZ) < self.radius
    }
}

impl NaiveShape for Cylinder {
    /// Check if point is inside the cylinder. **Does not take PBC into consideration.**
    ///
    /// For PBC-safe variant, see [Cylinder::inside].
    fn inside_naive(&self, point: &Vector3D) -> bool {
        let dist = point.distance_naive(&self.position, self.orientation);
        dist >= 0.0
            && dist < self.height
            && point.distance_naive(&self.position, self.plane) < self.radius
    }
}

impl NaiveShape for Rectangular {
    /// Check if point is inside the rectangular box.
    ///
    /// For PBC-safe variant, see [Rectangular::inside].
    fn inside_naive(&self, point: &Vector3D) -> bool {
        let dx = point.distance_naive(&self.position, Dimension::X);
        let dy = point.distance_naive(&self.position, Dimension::Y);
        let dz = point.distance_naive(&self.position, Dimension::Z);

        dx >= 0.0 && dx <= self.x && dy >= 0.0 && dy <= self.y && dz >= 0.0 && dz <= self.z
    }
}

dyn_clone::clone_trait_object!(NaiveShape);

#[cfg(test)]
mod tests_sphere {
    use super::*;
    use float_cmp::assert_approx_eq;
    use rand::RngExt;

    #[test]
    fn new() {
        let sphere = Sphere::new([1.0, 2.0, 3.0].into(), 1.5);

        assert_approx_eq!(f32, sphere.get_position().x, 1.0);
        assert_approx_eq!(f32, sphere.get_position().y, 2.0);
        assert_approx_eq!(f32, sphere.get_position().z, 3.0);
        assert_approx_eq!(f32, sphere.get_radius(), 1.5);
    }

    #[test]
    fn inside_nopbc() {
        let sphere = Sphere::new([1.0, 2.0, 3.0].into(), 1.5);

        let point = Vector3D::new(2.0, 2.5, 2.4);
        let simbox = SimBox::from([5.0, 5.0, 5.0]);

        assert!(sphere.inside(&point, &simbox));
    }

    #[test]
    fn inside_pbc() {
        let sphere = Sphere::new([1.0, 2.0, 4.5].into(), 1.5);

        let point = Vector3D::new(4.8, 2.1, 0.3);
        let simbox = SimBox::from([5.0, 5.0, 5.0]);

        assert!(sphere.inside(&point, &simbox));
    }

    #[test]
    fn not_inside() {
        let sphere = Sphere::new([1.0, 2.0, 4.5].into(), 1.5);

        let point = Vector3D::new(4.0, 2.1, 0.3);
        let simbox = SimBox::from([5.0, 5.0, 5.0]);

        assert!(!sphere.inside(&point, &simbox));
    }

    #[test]
    fn inside_random() {
        let sphere_center = Vector3D::new(1.0, 2.0, 3.0);
        let sphere_radius = 2.5;

        let sphere = Sphere::new(sphere_center.clone(), sphere_radius);
        let simbox = SimBox::from([5.0, 5.0, 5.0]);
        let mut rng = rand::rng();

        for _ in 0..100 {
            let x = rng.random_range(0.0..5.0);
            let y = rng.random_range(0.0..5.0);
            let z = rng.random_range(0.0..5.0);

            let point = Vector3D::new(x, y, z);

            assert_eq!(
                sphere.inside(&point, &simbox),
                point.distance(&sphere_center, Dimension::XYZ, &simbox) < sphere_radius
            );
        }
    }

    #[test]
    fn inside_naive() {
        let sphere = Sphere::new([1.0, 2.0, 3.0].into(), 1.5);
        let point = Vector3D::new(2.0, 2.5, 2.4);
        assert!(sphere.inside_naive(&point));
    }

    #[test]
    fn not_inside_naive() {
        let sphere = Sphere::new([1.0, 2.0, 4.5].into(), 1.5);
        let point = Vector3D::new(4.8, 2.1, 0.3);
        assert!(!sphere.inside_naive(&point));

        let sphere = Sphere::new([1.0, 2.0, 4.5].into(), 1.5);
        let point = Vector3D::new(4.0, 2.1, 0.3);
        assert!(!sphere.inside_naive(&point));
    }

    #[test]
    fn inside_random_naive() {
        let sphere_center = Vector3D::new(1.0, 2.0, 3.0);
        let sphere_radius = 2.5;

        let sphere = Sphere::new(sphere_center.clone(), sphere_radius);
        let mut rng = rand::rng();

        for _ in 0..100 {
            let x = rng.random_range(0.0..5.0);
            let y = rng.random_range(0.0..5.0);
            let z = rng.random_range(0.0..5.0);

            let point = Vector3D::new(x, y, z);

            assert_eq!(
                sphere.inside_naive(&point),
                point.distance_naive(&sphere_center, Dimension::XYZ) < sphere_radius
            );
        }
    }
}

#[cfg(test)]
mod tests_rectangular {
    use super::*;
    use float_cmp::assert_approx_eq;

    #[test]
    fn new() {
        let rect = Rectangular::new([1.0, 2.0, 3.0].into(), 2.0, 1.8, 0.7);

        assert_approx_eq!(f32, rect.get_position().x, 1.0);
        assert_approx_eq!(f32, rect.get_position().y, 2.0);
        assert_approx_eq!(f32, rect.get_position().z, 3.0);

        assert_approx_eq!(f32, rect.get_x(), 2.0);
        assert_approx_eq!(f32, rect.get_y(), 1.8);
        assert_approx_eq!(f32, rect.get_z(), 0.7);
    }

    #[test]
    fn inside_nopbc_1() {
        let rect = Rectangular::new([1.0, 2.0, 3.0].into(), 3.0, 2.0, 1.0);

        let point = Vector3D::new(3.1, 3.8, 3.9);
        let simbox = SimBox::from([10.0, 10.0, 10.0]);

        assert!(rect.inside(&point, &simbox));
        assert!(rect.inside_naive(&point));
    }

    #[test]
    fn inside_nopbc_2() {
        let rect = Rectangular::new([2.5, 3.1, 0.3].into(), 1.2, 1.3, 5.0);

        let point = Vector3D::new(2.6, 4.3, 4.9);
        let simbox = SimBox::from([10.0, 10.0, 10.0]);

        assert!(rect.inside(&point, &simbox));
        assert!(rect.inside_naive(&point));
    }

    #[test]
    fn not_inside_nopbc_1() {
        let rect = Rectangular::new([1.0, 2.0, 3.0].into(), 3.0, 2.0, 1.0);

        let point = Vector3D::new(4.1, 3.8, 3.9);
        let simbox = SimBox::from([10.0, 10.0, 10.0]);

        assert!(!rect.inside(&point, &simbox));
        assert!(!rect.inside_naive(&point));
    }

    #[test]
    fn not_inside_nopbc_2() {
        let rect = Rectangular::new([1.0, 2.0, 3.0].into(), 3.0, 2.0, 1.0);

        let point = Vector3D::new(2.1, 1.9, 3.9);
        let simbox = SimBox::from([10.0, 10.0, 10.0]);

        assert!(!rect.inside(&point, &simbox));
        assert!(!rect.inside_naive(&point));
    }

    #[test]
    fn not_inside_nopbc_3() {
        let rect = Rectangular::new([1.0, 2.0, 3.0].into(), 3.0, 2.0, 1.0);

        let point = Vector3D::new(2.1, 2.5, 4.1);
        let simbox = SimBox::from([10.0, 10.0, 10.0]);

        assert!(!rect.inside(&point, &simbox));
        assert!(!rect.inside_naive(&point));
    }

    #[test]
    fn inside_pbc_1() {
        let rect = Rectangular::new([1.0, 2.0, 3.0].into(), 4.0, 2.0, 1.5);

        let point = Vector3D::new(0.5, 3.8, 3.3);
        let simbox = SimBox::from([4.0, 4.0, 4.0]);

        assert!(rect.inside(&point, &simbox));
        assert!(!rect.inside_naive(&point));
    }

    #[test]
    fn inside_pbc_2() {
        let rect = Rectangular::new([1.0, 2.0, 3.0].into(), 1.0, 4.0, 1.5);

        let point = Vector3D::new(1.3, 1.2, 3.5);
        let simbox = SimBox::from([4.0, 4.0, 4.0]);

        assert!(rect.inside(&point, &simbox));
        assert!(!rect.inside_naive(&point));
    }

    #[test]
    fn inside_pbc_3() {
        let rect = Rectangular::new([1.0, 2.0, 3.0].into(), 1.0, 2.0, 1.5);

        let point = Vector3D::new(1.9, 2.2, 0.0);
        let simbox = SimBox::from([4.0, 4.0, 4.0]);

        assert!(rect.inside(&point, &simbox));
        assert!(!rect.inside_naive(&point));
    }
}

#[cfg(test)]
mod tests_cylinder {
    use super::*;
    use float_cmp::assert_approx_eq;

    #[test]
    fn new() {
        let cyl = Cylinder::new([1.0, 2.0, 3.0].into(), 0.8, 4.3, Dimension::Y);

        assert_approx_eq!(f32, cyl.get_position().x, 1.0);
        assert_approx_eq!(f32, cyl.get_position().y, 2.0);
        assert_approx_eq!(f32, cyl.get_position().z, 3.0);
        assert_eq!(cyl.get_orientation(), Dimension::Y);
        assert_approx_eq!(f32, cyl.get_radius(), 0.8);
        assert_approx_eq!(f32, cyl.get_height(), 4.3);
    }

    #[test]
    fn inside_x_nopbc() {
        let cylinder = Cylinder::new([2.0, 1.0, 3.0].into(), 2.0, 4.0, Dimension::X);

        let point = Vector3D::new(4.2, 1.8, 2.2);
        let simbox = SimBox::from([10.0, 10.0, 10.0]);

        assert!(cylinder.inside(&point, &simbox));
        assert!(cylinder.inside_naive(&point));
    }
    #[test]
    fn not_inside_x_nopbc() {
        let cylinder = Cylinder::new([3.0, 3.0, 3.0].into(), 2.0, 4.0, Dimension::X);

        let point = Vector3D::new(2.9, 3.8, 2.2);
        let simbox = SimBox::from([10.0, 10.0, 10.0]);

        assert!(!cylinder.inside(&point, &simbox));
        assert!(!cylinder.inside_naive(&point));
    }

    #[test]
    fn not_inside_x_nopbc2() {
        let cylinder = Cylinder::new([3.0, 3.0, 3.0].into(), 2.0, 4.0, Dimension::X);

        let point = Vector3D::new(3.1, 4.6, 1.2);
        let simbox = SimBox::from([10.0, 10.0, 10.0]);

        assert!(!cylinder.inside(&point, &simbox));
        assert!(!cylinder.inside_naive(&point));
    }

    #[test]
    fn inside_x_pbc_1() {
        let cylinder = Cylinder::new([2.0, 1.0, 3.0].into(), 2.0, 3.0, Dimension::X);

        let point = Vector3D::new(0.3, 1.4, 2.2);
        let simbox = SimBox::from([4.0, 4.0, 4.0]);

        assert!(cylinder.inside(&point, &simbox));
        assert!(!cylinder.inside_naive(&point));
    }

    #[test]
    fn inside_x_pbc_2() {
        let cylinder = Cylinder::new([2.0, 1.0, 3.0].into(), 2.0, 3.0, Dimension::X);

        let point = Vector3D::new(2.4, 3.8, 2.8);
        let simbox = SimBox::from([4.0, 4.0, 4.0]);

        assert!(cylinder.inside(&point, &simbox));
        assert!(!cylinder.inside_naive(&point));
    }

    #[test]
    fn inside_y_nopbc() {
        let cylinder = Cylinder::new([3.0, 3.0, 3.0].into(), 4.0, 4.0, Dimension::Y);

        let point = Vector3D::new(5.2, 3.8, 3.2);
        let simbox = SimBox::from([10.0, 10.0, 10.0]);

        assert!(cylinder.inside(&point, &simbox));
        assert!(cylinder.inside_naive(&point));
    }

    #[test]
    fn not_inside_y_nopbc() {
        let cylinder = Cylinder::new([3.0, 3.0, 3.0].into(), 2.0, 4.0, Dimension::Y);

        let point = Vector3D::new(4.2, 7.3, 2.2);
        let simbox = SimBox::from([10.0, 10.0, 10.0]);

        assert!(!cylinder.inside(&point, &simbox));
        assert!(!cylinder.inside_naive(&point));
    }

    #[test]
    fn not_inside_y_nopbc2() {
        let cylinder = Cylinder::new([3.0, 3.0, 3.0].into(), 2.0, 4.0, Dimension::Y);

        let point = Vector3D::new(1.1, 5.4, 3.7);
        let simbox = SimBox::from([10.0, 10.0, 10.0]);

        assert!(!cylinder.inside(&point, &simbox));
        assert!(!cylinder.inside_naive(&point));
    }

    #[test]
    fn inside_y_pbc_1() {
        let cylinder = Cylinder::new([1.0, 2.0, 3.0].into(), 2.0, 3.0, Dimension::Y);

        let point = Vector3D::new(0.7, 0.8, 3.4);
        let simbox = SimBox::from([4.0, 4.0, 4.0]);

        assert!(cylinder.inside(&point, &simbox));
        assert!(!cylinder.inside_naive(&point));
    }

    #[test]
    fn inside_y_pbc_2() {
        let cylinder = Cylinder::new([1.0, 2.0, 3.5].into(), 2.0, 3.0, Dimension::Y);

        let point = Vector3D::new(3.5, 3.4, 0.0);
        let simbox = SimBox::from([4.0, 4.0, 4.0]);

        assert!(cylinder.inside(&point, &simbox));
        assert!(!cylinder.inside_naive(&point));
    }

    #[test]
    fn inside_z_nopbc() {
        let cylinder = Cylinder::new([3.0, 3.0, 3.0].into(), 2.0, 2.0, Dimension::Z);

        let point = Vector3D::new(4.0, 3.8, 4.8);
        let simbox = SimBox::from([10.0, 10.0, 10.0]);

        assert!(cylinder.inside(&point, &simbox));
        assert!(cylinder.inside_naive(&point));
    }

    #[test]
    fn not_inside_z_nopbc() {
        let cylinder = Cylinder::new([3.0, 3.0, 3.0].into(), 2.0, 4.0, Dimension::Z);

        let point = Vector3D::new(4.0, 3.8, 7.2);
        let simbox = SimBox::from([10.0, 10.0, 10.0]);

        assert!(!cylinder.inside(&point, &simbox));
        assert!(!cylinder.inside_naive(&point));
    }

    #[test]
    fn not_inside_z_nopbc2() {
        let cylinder = Cylinder::new([3.0, 3.0, 3.0].into(), 2.0, 4.0, Dimension::Z);

        let point = Vector3D::new(4.9, 3.7, 6.8);
        let simbox = SimBox::from([10.0, 10.0, 10.0]);

        assert!(!cylinder.inside(&point, &simbox));
        assert!(!cylinder.inside_naive(&point));
    }

    #[test]
    fn inside_z_pbc_1() {
        let cylinder = Cylinder::new([1.0, 2.0, 3.0].into(), 2.0, 3.0, Dimension::Z);

        let point = Vector3D::new(1.4, 1.5, 1.5);
        let simbox = SimBox::from([4.0, 4.0, 4.0]);

        assert!(cylinder.inside(&point, &simbox));
        assert!(!cylinder.inside_naive(&point));
    }

    #[test]
    fn inside_z_pbc_2() {
        let cylinder = Cylinder::new([3.0, 2.0, 3.0].into(), 2.0, 3.0, Dimension::Z);

        let point = Vector3D::new(0.3, 1.5, 3.1);
        let simbox = SimBox::from([4.0, 4.0, 4.0]);

        assert!(cylinder.inside(&point, &simbox));
        assert!(!cylinder.inside_naive(&point));
    }
}

#[cfg(test)]
mod tests_triprism {
    use super::*;
    use float_cmp::assert_approx_eq;

    #[test]
    fn new_z() {
        let base1 = Vector3D::new(3.0, 4.0, 2.0);
        let base2 = Vector3D::new(7.0, 5.0, 2.0);
        let base3 = Vector3D::new(4.0, 3.0, 2.0);

        let prism = TriangularPrism::new(base1, base2, base3, 4.3);

        assert_approx_eq!(f32, prism.get_base1().x, 3.0);
        assert_approx_eq!(f32, prism.get_base1().y, 4.0);
        assert_approx_eq!(f32, prism.get_base1().z, 2.0);

        assert_approx_eq!(f32, prism.get_base2().x, 7.0);
        assert_approx_eq!(f32, prism.get_base2().y, 5.0);
        assert_approx_eq!(f32, prism.get_base2().z, 2.0);

        assert_approx_eq!(f32, prism.get_base3().x, 4.0);
        assert_approx_eq!(f32, prism.get_base3().y, 3.0);
        assert_approx_eq!(f32, prism.get_base3().z, 2.0);

        assert_approx_eq!(f32, prism.get_height(), 4.3);
        assert_eq!(prism.get_orientation(), Dimension::Z);
        assert_eq!(prism.get_plane(), Dimension::XY);
    }

    #[test]
    fn new_y() {
        let base1 = Vector3D::new(3.0, 3.0, 3.0);
        let base2 = Vector3D::new(7.0, 3.0, 2.0);
        let base3 = Vector3D::new(4.0, 3.0, 5.0);

        let prism = TriangularPrism::new(base1, base2, base3, 4.3);

        assert_eq!(prism.get_orientation(), Dimension::Y);
        assert_eq!(prism.get_plane(), Dimension::XZ);
    }

    #[test]
    fn new_x() {
        let base1 = Vector3D::new(5.0, 7.0, 3.0);
        let base2 = Vector3D::new(5.0, 0.0, 2.0);
        let base3 = Vector3D::new(5.0, 4.0, 5.0);

        let prism = TriangularPrism::new(base1, base2, base3, 4.3);

        assert_eq!(prism.get_orientation(), Dimension::X);
        assert_eq!(prism.get_plane(), Dimension::YZ);
    }

    #[test]
    #[should_panic(
        expected = "FATAL GROAN ERROR | TriangularPrism::new | Base of the requested TriangularPrism does not lie in xy, xz, nor yz plane."
    )]
    fn invalid_base_orientation() {
        let base1 = Vector3D::new(3.0, 4.0, 2.0);
        let base2 = Vector3D::new(7.0, 5.0, 1.8);
        let base3 = Vector3D::new(4.0, 3.0, 2.0);

        let _ = TriangularPrism::new(base1, base2, base3, 4.3);
    }

    #[test]
    #[should_panic(
        expected = "FATAL GROAN ERROR | TriangularPrism::new | Base of the requested TriangularPrism can not be constructed."
    )]
    fn invalid_base() {
        let base1 = Vector3D::new(3.0, 4.0, 2.0);
        let base2 = Vector3D::new(7.0, 4.0, 2.0);
        let base3 = Vector3D::new(4.0, 4.0, 2.0);

        let _ = TriangularPrism::new(base1, base2, base3, 4.3);
    }

    #[test]
    fn inside_x_nopbc() {
        let base1 = Vector3D::new(5.0, 7.0, 3.0);
        let base2 = Vector3D::new(5.0, 0.0, 2.0);
        let base3 = Vector3D::new(5.0, 4.0, 5.0);

        let prism = TriangularPrism::new(base1, base2, base3, 4.3);

        let point = Vector3D::new(9.1, 4.8, 3.6);

        let simbox = SimBox::from([10.0, 10.0, 10.0]);

        assert!(prism.inside(&point, &simbox));
    }

    #[test]
    fn not_inside_x_nopbc() {
        let base1 = Vector3D::new(5.0, 7.0, 3.0);
        let base2 = Vector3D::new(5.0, 0.0, 2.0);
        let base3 = Vector3D::new(5.0, 4.0, 5.0);

        let prism = TriangularPrism::new(base1, base2, base3, 4.3);

        let point = Vector3D::new(9.7, 4.8, 3.6);

        let simbox = SimBox::from([10.0, 10.0, 10.0]);

        assert!(!prism.inside(&point, &simbox));
    }

    #[test]
    fn inside_x_pbc() {
        let base1 = Vector3D::new(5.0, 7.0, 3.0);
        let base2 = Vector3D::new(5.0, 0.0, 2.0);
        let base3 = Vector3D::new(5.0, 4.0, 5.0);

        let prism = TriangularPrism::new(base1, base2, base3, 4.3);

        let point = Vector3D::new(0.3, 4.8, 3.6);

        let simbox = SimBox::from([8.0, 8.0, 8.0]);

        assert!(prism.inside(&point, &simbox));
    }

    #[test]
    fn inside_y_nopbc() {
        let base1 = Vector3D::new(3.0, 3.0, 3.0);
        let base2 = Vector3D::new(7.0, 3.0, 2.0);
        let base3 = Vector3D::new(4.0, 3.0, 5.0);

        let prism = TriangularPrism::new(base1, base2, base3, 4.3);

        let point = Vector3D::new(4.8, 5.6, 3.6);

        let simbox = SimBox::from([10.0, 10.0, 10.0]);

        assert!(prism.inside(&point, &simbox));
    }

    #[test]
    fn not_inside_y_nopbc() {
        let base1 = Vector3D::new(3.0, 3.0, 3.0);
        let base2 = Vector3D::new(7.0, 3.0, 2.0);
        let base3 = Vector3D::new(4.0, 3.0, 5.0);

        let prism = TriangularPrism::new(base1, base2, base3, 4.3);

        let point = Vector3D::new(5.5, 5.6, 3.6);

        let simbox = SimBox::from([10.0, 10.0, 10.0]);

        assert!(!prism.inside(&point, &simbox));
    }

    #[test]
    fn inside_y_pbc() {
        let base1 = Vector3D::new(3.0, 3.0, 3.0);
        let base2 = Vector3D::new(7.0, 3.0, 2.0);
        let base3 = Vector3D::new(4.0, 3.0, 5.0);

        let prism = TriangularPrism::new(base1, base2, base3, 4.3);

        let point = Vector3D::new(4.8, 2.1, 3.6);

        let simbox = SimBox::from([10.0, 5.0, 10.0]);

        assert!(prism.inside(&point, &simbox));
    }

    #[test]
    fn inside_z_nopbc() {
        let base1 = Vector3D::new(3.0, 4.0, 2.0);
        let base2 = Vector3D::new(7.0, 5.0, 2.0);
        let base3 = Vector3D::new(4.0, 3.0, 2.0);

        let prism = TriangularPrism::new(base1, base2, base3, 4.3);

        let point = Vector3D::new(4.8, 3.6, 2.1);

        let simbox = SimBox::from([10.0, 10.0, 10.0]);

        assert!(prism.inside(&point, &simbox));
    }

    #[test]
    fn not_inside_z_nopbc() {
        let base1 = Vector3D::new(3.0, 4.0, 2.0);
        let base2 = Vector3D::new(7.0, 5.0, 2.0);
        let base3 = Vector3D::new(4.0, 3.0, 2.0);

        let prism = TriangularPrism::new(base1, base2, base3, 4.3);

        let point = Vector3D::new(4.8, 3.4, 2.1);

        let simbox = SimBox::from([10.0, 10.0, 10.0]);

        assert!(!prism.inside(&point, &simbox));
    }

    #[test]
    fn inside_z_pbc() {
        let base1 = Vector3D::new(3.0, 4.0, 8.0);
        let base2 = Vector3D::new(7.0, 5.0, 8.0);
        let base3 = Vector3D::new(4.0, 3.0, 8.0);

        let prism = TriangularPrism::new(base1, base2, base3, 4.3);

        let point = Vector3D::new(4.8, 3.6, 2.1);

        let simbox = SimBox::from([10.0, 10.0, 10.0]);

        assert!(prism.inside(&point, &simbox));
    }
}