pybevy_math 0.2.1

Math types (Vec2, Vec3, Vec4, Quat, Mat3, Mat4) for PyBevy
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
use bevy::math::{BVec3, Dir3, Vec3, Vec3Swizzles};
use pybevy_core::{FromBorrowedStorage, ValueStorage};
use pyo3::{
    basic::CompareOp,
    exceptions::{PyTypeError, PyValueError},
    prelude::*,
};

use super::{vec2::PyVec2, vec4::PyVec4};

#[pyclass(name = "Vec3")]
#[derive(Debug, Clone)]
pub struct PyVec3 {
    pub(crate) storage: ValueStorage<Vec3>,
}

impl From<PyVec3> for Vec3 {
    #[inline(always)]
    fn from(py_vec: PyVec3) -> Self {
        py_vec.storage.get().unwrap()
    }
}

impl From<&PyVec3> for Vec3 {
    #[inline(always)]
    fn from(py_vec: &PyVec3) -> Self {
        py_vec.storage.get().unwrap()
    }
}

impl From<Vec3> for PyVec3 {
    #[inline(always)]
    fn from(vec: Vec3) -> Self {
        PyVec3::from_vec3(vec)
    }
}

impl TryFrom<PyVec3> for Dir3 {
    type Error = PyErr;

    fn try_from(py_vec: PyVec3) -> Result<Self, Self::Error> {
        Self::try_from(&py_vec)
    }
}

impl TryFrom<&PyVec3> for Dir3 {
    type Error = PyErr;

    fn try_from(py_vec: &PyVec3) -> Result<Self, Self::Error> {
        let vec: Vec3 = py_vec.into();
        Dir3::try_from(vec).map_err(|err| PyValueError::new_err(err.to_string()))
    }
}

impl FromBorrowedStorage<ValueStorage<Vec3>> for PyVec3 {
    fn from_borrowed(storage: ValueStorage<Vec3>) -> Self {
        PyVec3 { storage }
    }
}

impl PyVec3 {
    #[inline(always)]
    pub fn from_vec3(vec: Vec3) -> Self {
        PyVec3 {
            storage: ValueStorage::owned(vec),
        }
    }

    #[inline(always)]
    pub fn into_vec3(self) -> Vec3 {
        self.into()
    }

    #[inline(always)]
    pub const fn vec3(vec: Vec3) -> Self {
        PyVec3 {
            storage: ValueStorage::owned(vec),
        }
    }

    #[inline(always)]
    fn as_ref(&self) -> PyResult<&Vec3> {
        Ok(self.storage.as_ref()?)
    }

    #[inline(always)]
    fn as_mut(&mut self) -> PyResult<&mut Vec3> {
        Ok(self.storage.as_mut()?)
    }

    #[inline(always)]
    pub fn get(&self) -> Vec3 {
        self.storage.get().unwrap()
    }

    pub const ZERO: PyVec3 = PyVec3::vec3(Vec3::ZERO);
    pub const ONE: PyVec3 = PyVec3::vec3(Vec3::ONE);
    pub const NEG_ONE: PyVec3 = PyVec3::vec3(Vec3::NEG_ONE);
    pub const MIN: PyVec3 = PyVec3::vec3(Vec3::MIN);
    pub const MAX: PyVec3 = PyVec3::vec3(Vec3::MAX);
    pub const NAN: PyVec3 = PyVec3::vec3(Vec3::NAN);
    pub const INFINITY: PyVec3 = PyVec3::vec3(Vec3::INFINITY);
    pub const NEG_INFINITY: PyVec3 = PyVec3::vec3(Vec3::NEG_INFINITY);
    pub const X: PyVec3 = PyVec3::vec3(Vec3::X);
    pub const Y: PyVec3 = PyVec3::vec3(Vec3::Y);
    pub const Z: PyVec3 = PyVec3::vec3(Vec3::Z);
    pub const NEG_X: PyVec3 = PyVec3::vec3(Vec3::NEG_X);
    pub const NEG_Y: PyVec3 = PyVec3::vec3(Vec3::NEG_Y);
    pub const NEG_Z: PyVec3 = PyVec3::vec3(Vec3::NEG_Z);
}

#[pymethods]
impl PyVec3 {
    #[staticmethod]
    #[pyo3(name = "ZERO")]
    pub fn zero() -> Self {
        Self::vec3(Vec3::ZERO)
    }
    #[staticmethod]
    #[pyo3(name = "ONE")]
    pub fn one() -> Self {
        Self::vec3(Vec3::ONE)
    }
    #[staticmethod]
    #[pyo3(name = "NEG_ONE")]
    pub fn neg_one() -> Self {
        Self::vec3(Vec3::NEG_ONE)
    }
    #[staticmethod]
    #[pyo3(name = "MIN")]
    pub fn min_value() -> Self {
        Self::vec3(Vec3::MIN)
    }
    #[staticmethod]
    #[pyo3(name = "MAX")]
    pub fn max_value() -> Self {
        Self::vec3(Vec3::MAX)
    }
    #[staticmethod]
    #[pyo3(name = "NAN")]
    pub fn nan() -> Self {
        Self::vec3(Vec3::NAN)
    }
    #[staticmethod]
    #[pyo3(name = "INFINITY")]
    pub fn infinity() -> Self {
        Self::vec3(Vec3::INFINITY)
    }
    #[staticmethod]
    #[pyo3(name = "NEG_INFINITY")]
    pub fn neg_infinity() -> Self {
        Self::vec3(Vec3::NEG_INFINITY)
    }
    #[staticmethod]
    #[pyo3(name = "X")]
    pub fn unit_x() -> Self {
        Self::vec3(Vec3::X)
    }
    #[staticmethod]
    #[pyo3(name = "Y")]
    pub fn unit_y() -> Self {
        Self::vec3(Vec3::Y)
    }
    #[staticmethod]
    #[pyo3(name = "Z")]
    pub fn unit_z() -> Self {
        Self::vec3(Vec3::Z)
    }
    #[staticmethod]
    #[pyo3(name = "NEG_X")]
    pub fn neg_x() -> Self {
        Self::vec3(Vec3::NEG_X)
    }
    #[staticmethod]
    #[pyo3(name = "NEG_Y")]
    pub fn neg_y() -> Self {
        Self::vec3(Vec3::NEG_Y)
    }
    #[staticmethod]
    #[pyo3(name = "NEG_Z")]
    pub fn neg_z() -> Self {
        Self::vec3(Vec3::NEG_Z)
    }

    #[new]
    pub fn new(x: f32, y: f32, z: f32) -> Self {
        PyVec3::vec3(Vec3::new(x, y, z))
    }

    #[staticmethod]
    pub fn splat(value: f32) -> Self {
        PyVec3::vec3(Vec3::splat(value))
    }

    #[getter]
    pub fn x(&self) -> PyResult<f32> {
        Ok(self.as_ref()?.x)
    }

    #[setter]
    pub fn set_x(&mut self, value: f32) -> PyResult<()> {
        self.as_mut()?.x = value;
        Ok(())
    }

    #[getter]
    pub fn y(&self) -> PyResult<f32> {
        Ok(self.as_ref()?.y)
    }

    #[setter]
    pub fn set_y(&mut self, value: f32) -> PyResult<()> {
        self.as_mut()?.y = value;
        Ok(())
    }

    #[getter]
    pub fn z(&self) -> PyResult<f32> {
        Ok(self.as_ref()?.z)
    }

    #[setter]
    pub fn set_z(&mut self, value: f32) -> PyResult<()> {
        self.as_mut()?.z = value;
        Ok(())
    }

    pub fn dot(&self, other: &PyVec3) -> PyResult<f32> {
        Ok(self.as_ref()?.dot(*other.as_ref()?))
    }

    pub fn cross(&self, other: &PyVec3) -> PyResult<Self> {
        Ok(PyVec3::from_vec3(self.as_ref()?.cross(*other.as_ref()?)))
    }

    pub fn length(&self) -> PyResult<f32> {
        Ok(self.as_ref()?.length())
    }

    pub fn length_squared(&self) -> PyResult<f32> {
        Ok(self.as_ref()?.length_squared())
    }

    pub fn normalize(&self) -> PyResult<Self> {
        Ok(PyVec3::from_vec3(self.as_ref()?.normalize()))
    }

    pub fn xy(&self) -> PyResult<PyVec2> {
        Ok(self.as_ref()?.xy().into())
    }

    pub fn xz(&self) -> PyResult<PyVec2> {
        Ok(self.as_ref()?.xz().into())
    }

    pub fn yz(&self) -> PyResult<PyVec2> {
        Ok(self.as_ref()?.yz().into())
    }

    pub fn yx(&self) -> PyResult<PyVec2> {
        Ok(self.as_ref()?.yx().into())
    }

    pub fn zx(&self) -> PyResult<PyVec2> {
        Ok(self.as_ref()?.zx().into())
    }

    pub fn zy(&self) -> PyResult<PyVec2> {
        Ok(self.as_ref()?.zy().into())
    }

    pub fn xx(&self) -> PyResult<PyVec2> {
        Ok(self.as_ref()?.xx().into())
    }

    pub fn yy(&self) -> PyResult<PyVec2> {
        Ok(self.as_ref()?.yy().into())
    }

    pub fn zz(&self) -> PyResult<PyVec2> {
        Ok(self.as_ref()?.zz().into())
    }

    pub fn with_x(&self, x: f32) -> PyResult<PyVec3> {
        Ok(PyVec3::from_vec3(self.as_ref()?.with_x(x)))
    }

    pub fn with_y(&self, y: f32) -> PyResult<PyVec3> {
        Ok(PyVec3::from_vec3(self.as_ref()?.with_y(y)))
    }

    pub fn with_z(&self, z: f32) -> PyResult<PyVec3> {
        Ok(PyVec3::from_vec3(self.as_ref()?.with_z(z)))
    }

    pub fn truncate(&self) -> PyResult<PyVec2> {
        Ok(self.as_ref()?.truncate().into())
    }

    pub fn extend(&self, w: f32) -> PyResult<PyVec4> {
        Ok(self.as_ref()?.extend(w).into())
    }

    #[staticmethod]
    pub fn from_array(a: (f32, f32, f32)) -> PyVec3 {
        PyVec3::from_vec3(Vec3::from_array([a.0, a.1, a.2]))
    }

    pub fn to_array(&self) -> PyResult<(f32, f32, f32)> {
        let arr = self.as_ref()?.to_array();
        Ok((arr[0], arr[1], arr[2]))
    }

    pub fn abs(&self) -> PyResult<PyVec3> {
        Ok(PyVec3::from_vec3(self.as_ref()?.abs()))
    }

    pub fn signum(&self) -> PyResult<PyVec3> {
        Ok(PyVec3::from_vec3(self.as_ref()?.signum()))
    }

    pub fn copysign(&self, rhs: &PyVec3) -> PyResult<PyVec3> {
        Ok(PyVec3::from_vec3(self.as_ref()?.copysign(*rhs.as_ref()?)))
    }

    pub fn is_finite(&self) -> PyResult<bool> {
        Ok(self.as_ref()?.is_finite())
    }

    pub fn is_nan(&self) -> PyResult<bool> {
        Ok(self.as_ref()?.is_nan())
    }

    pub fn round(&self) -> PyResult<PyVec3> {
        Ok(PyVec3::from_vec3(self.as_ref()?.round()))
    }

    pub fn trunc(&self) -> PyResult<PyVec3> {
        Ok(PyVec3::from_vec3(self.as_ref()?.trunc()))
    }

    pub fn fract(&self) -> PyResult<PyVec3> {
        Ok(PyVec3::from_vec3(self.as_ref()?.fract()))
    }

    pub fn fract_gl(&self) -> PyResult<PyVec3> {
        Ok(PyVec3::from_vec3(self.as_ref()?.fract_gl()))
    }

    pub fn exp(&self) -> PyResult<PyVec3> {
        Ok(PyVec3::from_vec3(self.as_ref()?.exp()))
    }

    pub fn powf(&self, n: f32) -> PyResult<PyVec3> {
        Ok(PyVec3::from_vec3(self.as_ref()?.powf(n)))
    }

    pub fn recip(&self) -> PyResult<PyVec3> {
        Ok(PyVec3::from_vec3(self.as_ref()?.recip()))
    }

    pub fn lerp(&self, rhs: &PyVec3, s: f32) -> PyResult<PyVec3> {
        Ok(PyVec3::from_vec3(self.as_ref()?.lerp(*rhs.as_ref()?, s)))
    }

    pub fn move_towards(&self, rhs: &PyVec3, d: f32) -> PyResult<PyVec3> {
        Ok(PyVec3::from_vec3(
            self.as_ref()?.move_towards(*rhs.as_ref()?, d),
        ))
    }

    pub fn midpoint(&self, rhs: &PyVec3) -> PyResult<PyVec3> {
        Ok(PyVec3::from_vec3(self.as_ref()?.midpoint(*rhs.as_ref()?)))
    }

    pub fn project_onto(&self, rhs: &PyVec3) -> PyResult<PyVec3> {
        Ok(PyVec3::from_vec3(
            self.as_ref()?.project_onto(*rhs.as_ref()?),
        ))
    }

    pub fn reject_from(&self, rhs: &PyVec3) -> PyResult<PyVec3> {
        Ok(PyVec3::from_vec3(
            self.as_ref()?.reject_from(*rhs.as_ref()?),
        ))
    }

    pub fn normalize_or_zero(&self) -> PyResult<PyVec3> {
        Ok(PyVec3::from_vec3(self.as_ref()?.normalize_or_zero()))
    }

    pub fn try_normalize(&self) -> PyResult<Option<PyVec3>> {
        Ok(self.as_ref()?.try_normalize().map(PyVec3::from_vec3))
    }

    pub fn distance(&self, rhs: &PyVec3) -> PyResult<f32> {
        Ok(self.as_ref()?.distance(*rhs.as_ref()?))
    }

    pub fn distance_squared(&self, rhs: &PyVec3) -> PyResult<f32> {
        Ok(self.as_ref()?.distance_squared(*rhs.as_ref()?))
    }

    pub fn min(&self, rhs: &PyVec3) -> PyResult<PyVec3> {
        Ok(PyVec3::from_vec3(self.as_ref()?.min(*rhs.as_ref()?)))
    }

    pub fn max(&self, rhs: &PyVec3) -> PyResult<PyVec3> {
        Ok(PyVec3::from_vec3(self.as_ref()?.max(*rhs.as_ref()?)))
    }

    pub fn clamp(&self, min: &PyVec3, max: &PyVec3) -> PyResult<PyVec3> {
        Ok(PyVec3::from_vec3(
            self.as_ref()?.clamp(*min.as_ref()?, *max.as_ref()?),
        ))
    }

    pub fn element_sum(&self) -> PyResult<f32> {
        Ok(self.as_ref()?.element_sum())
    }

    pub fn element_product(&self) -> PyResult<f32> {
        Ok(self.as_ref()?.element_product())
    }

    fn __add__(&self, other: &Bound<'_, PyAny>) -> PyResult<PyVec3> {
        let self_vec = *self.as_ref()?;
        if let Ok(scalar) = other.extract::<f32>() {
            Ok(PyVec3::from_vec3(self_vec + scalar))
        } else if let Ok(other_vec) = other.extract::<PyVec3>() {
            Ok(PyVec3::from_vec3(self_vec + *other_vec.as_ref()?))
        } else {
            Err(PyTypeError::new_err("Unsupported operand type for +"))
        }
    }

    fn __sub__(&self, other: &Bound<'_, PyAny>) -> PyResult<PyVec3> {
        let self_vec = *self.as_ref()?;
        if let Ok(scalar) = other.extract::<f32>() {
            Ok(PyVec3::from_vec3(self_vec - scalar))
        } else if let Ok(other_vec) = other.extract::<PyVec3>() {
            Ok(PyVec3::from_vec3(self_vec - *other_vec.as_ref()?))
        } else {
            Err(PyTypeError::new_err("Unsupported operand type for -"))
        }
    }

    fn __mul__(&self, other: &Bound<'_, PyAny>) -> PyResult<PyVec3> {
        let self_vec = *self.as_ref()?;
        if let Ok(scalar) = other.extract::<f32>() {
            Ok(PyVec3::from_vec3(self_vec * scalar))
        } else if let Ok(other_vec) = other.extract::<PyVec3>() {
            Ok(PyVec3::from_vec3(self_vec * *other_vec.as_ref()?))
        } else {
            Err(PyTypeError::new_err("Unsupported operand type for *"))
        }
    }

    fn __div__(&self, other: &Bound<'_, PyAny>) -> PyResult<PyVec3> {
        let self_vec = *self.as_ref()?;
        if let Ok(scalar) = other.extract::<f32>() {
            Ok(PyVec3::from_vec3(self_vec / scalar))
        } else if let Ok(other_vec) = other.extract::<PyVec3>() {
            Ok(PyVec3::from_vec3(self_vec / *other_vec.as_ref()?))
        } else {
            Err(PyTypeError::new_err("Unsupported operand type for /"))
        }
    }

    fn __truediv__(&self, other: &Bound<'_, PyAny>) -> PyResult<PyVec3> {
        self.__div__(other)
    }

    fn __neg__(&self) -> PyResult<PyVec3> {
        Ok(PyVec3::from_vec3(-*self.as_ref()?))
    }

    fn __rmul__(&self, other: &Bound<'_, PyAny>) -> PyResult<PyVec3> {
        let self_vec = *self.as_ref()?;
        if let Ok(scalar) = other.extract::<f32>() {
            Ok(PyVec3::from_vec3(scalar * self_vec))
        } else if let Ok(other_vec) = other.extract::<PyVec3>() {
            Ok(PyVec3::from_vec3(*other_vec.as_ref()? * self_vec))
        } else {
            Err(PyTypeError::new_err("Unsupported operand type for *"))
        }
    }

    fn __repr__(&self) -> PyResult<String> {
        let v = *self.as_ref()?;
        Ok(format!("Vec3({}, {}, {})", v.x, v.y, v.z))
    }

    fn __richcmp__(&self, other: &Bound<'_, PyAny>, op: CompareOp) -> PyResult<bool> {
        if let Ok(other_vec) = other.extract::<PyVec3>() {
            let self_vec = *self.as_ref()?;
            let other_val = *other_vec.as_ref()?;
            match op {
                CompareOp::Eq => Ok(self_vec == other_val),
                CompareOp::Ne => Ok(self_vec != other_val),
                _ => Err(PyTypeError::new_err("Unsupported comparison operation")),
            }
        } else if let Ok(tuple) = other.extract::<(f32, f32, f32)>() {
            match op {
                CompareOp::Eq => Ok(self.as_tuple()? == tuple),
                CompareOp::Ne => Ok(self.as_tuple()? != tuple),
                _ => Err(PyTypeError::new_err("Unsupported comparison operation")),
            }
        } else {
            Err(PyTypeError::new_err(
                "Can only compare Vec3 with another Vec3 or a tuple of three floats",
            ))
        }
    }

    fn as_tuple(&self) -> PyResult<(f32, f32, f32)> {
        let v = *self.as_ref()?;
        Ok((v.x, v.y, v.z))
    }

    pub fn is_normalized(&self) -> PyResult<bool> {
        Ok(self.as_ref()?.is_normalized())
    }

    pub fn floor(&self) -> PyResult<PyVec3> {
        Ok(PyVec3::from_vec3(self.as_ref()?.floor()))
    }

    pub fn ceil(&self) -> PyResult<PyVec3> {
        Ok(PyVec3::from_vec3(self.as_ref()?.ceil()))
    }

    pub fn zxy(&self) -> PyResult<PyVec3> {
        Ok(PyVec3::from_vec3(self.as_ref()?.zxy()))
    }

    pub fn xyz(&self) -> PyResult<PyVec3> {
        Ok(PyVec3::from_vec3(self.as_ref()?.xyz()))
    }

    pub fn xzy(&self) -> PyResult<PyVec3> {
        Ok(PyVec3::from_vec3(self.as_ref()?.xzy()))
    }

    pub fn yxz(&self) -> PyResult<PyVec3> {
        Ok(PyVec3::from_vec3(self.as_ref()?.yxz()))
    }

    pub fn yzx(&self) -> PyResult<PyVec3> {
        Ok(PyVec3::from_vec3(self.as_ref()?.yzx()))
    }

    pub fn zyx(&self) -> PyResult<PyVec3> {
        Ok(PyVec3::from_vec3(self.as_ref()?.zyx()))
    }

    pub fn xxx(&self) -> PyResult<PyVec3> {
        Ok(PyVec3::from_vec3(self.as_ref()?.xxx()))
    }

    pub fn xxy(&self) -> PyResult<PyVec3> {
        Ok(PyVec3::from_vec3(self.as_ref()?.xxy()))
    }

    pub fn xxz(&self) -> PyResult<PyVec3> {
        Ok(PyVec3::from_vec3(self.as_ref()?.xxz()))
    }

    pub fn xyx(&self) -> PyResult<PyVec3> {
        Ok(PyVec3::from_vec3(self.as_ref()?.xyx()))
    }

    pub fn xyy(&self) -> PyResult<PyVec3> {
        Ok(PyVec3::from_vec3(self.as_ref()?.xyy()))
    }

    pub fn xzx(&self) -> PyResult<PyVec3> {
        Ok(PyVec3::from_vec3(self.as_ref()?.xzx()))
    }

    pub fn xzz(&self) -> PyResult<PyVec3> {
        Ok(PyVec3::from_vec3(self.as_ref()?.xzz()))
    }

    pub fn yxx(&self) -> PyResult<PyVec3> {
        Ok(PyVec3::from_vec3(self.as_ref()?.yxx()))
    }

    pub fn yxy(&self) -> PyResult<PyVec3> {
        Ok(PyVec3::from_vec3(self.as_ref()?.yxy()))
    }

    pub fn yyx(&self) -> PyResult<PyVec3> {
        Ok(PyVec3::from_vec3(self.as_ref()?.yyx()))
    }

    pub fn yyy(&self) -> PyResult<PyVec3> {
        Ok(PyVec3::from_vec3(self.as_ref()?.yyy()))
    }

    pub fn yyz(&self) -> PyResult<PyVec3> {
        Ok(PyVec3::from_vec3(self.as_ref()?.yyz()))
    }

    pub fn yzy(&self) -> PyResult<PyVec3> {
        Ok(PyVec3::from_vec3(self.as_ref()?.yzy()))
    }

    pub fn yzz(&self) -> PyResult<PyVec3> {
        Ok(PyVec3::from_vec3(self.as_ref()?.yzz()))
    }

    pub fn zxx(&self) -> PyResult<PyVec3> {
        Ok(PyVec3::from_vec3(self.as_ref()?.zxx()))
    }

    pub fn zxz(&self) -> PyResult<PyVec3> {
        Ok(PyVec3::from_vec3(self.as_ref()?.zxz()))
    }

    pub fn zyy(&self) -> PyResult<PyVec3> {
        Ok(PyVec3::from_vec3(self.as_ref()?.zyy()))
    }

    pub fn zyz(&self) -> PyResult<PyVec3> {
        Ok(PyVec3::from_vec3(self.as_ref()?.zyz()))
    }

    pub fn zzx(&self) -> PyResult<PyVec3> {
        Ok(PyVec3::from_vec3(self.as_ref()?.zzx()))
    }

    pub fn zzy(&self) -> PyResult<PyVec3> {
        Ok(PyVec3::from_vec3(self.as_ref()?.zzy()))
    }

    pub fn zzz(&self) -> PyResult<PyVec3> {
        Ok(PyVec3::from_vec3(self.as_ref()?.zzz()))
    }

    pub fn min_element(&self) -> PyResult<f32> {
        Ok(self.as_ref()?.min_element())
    }

    pub fn max_element(&self) -> PyResult<f32> {
        Ok(self.as_ref()?.max_element())
    }

    pub fn angle_between(&self, other: &PyVec3) -> PyResult<f32> {
        Ok(self.as_ref()?.angle_between(*other.as_ref()?))
    }

    pub fn any_orthogonal_vector(&self) -> PyResult<PyVec3> {
        Ok(self.as_ref()?.any_orthogonal_vector().into())
    }

    pub fn any_orthonormal_vector(&self) -> PyResult<PyVec3> {
        Ok(self.as_ref()?.any_orthonormal_vector().into())
    }

    pub fn any_orthonormal_pair(&self) -> PyResult<(PyVec3, PyVec3)> {
        let (v1, v2) = self.as_ref()?.any_orthonormal_pair();
        Ok((PyVec3::from_vec3(v1), PyVec3::from_vec3(v2)))
    }

    pub fn length_recip(&self) -> PyResult<f32> {
        Ok(self.as_ref()?.length_recip())
    }

    pub fn slerp(&self, rhs: &PyVec3, s: f32) -> PyResult<PyVec3> {
        Ok(PyVec3::from_vec3(self.as_ref()?.slerp(*rhs.as_ref()?, s)))
    }

    pub fn clamp_length(&self, min: f32, max: f32) -> PyResult<PyVec3> {
        Ok(PyVec3::from_vec3(self.as_ref()?.clamp_length(min, max)))
    }

    pub fn clamp_length_min(&self, min: f32) -> PyResult<PyVec3> {
        Ok(PyVec3::from_vec3(self.as_ref()?.clamp_length_min(min)))
    }

    pub fn clamp_length_max(&self, max: f32) -> PyResult<PyVec3> {
        Ok(PyVec3::from_vec3(self.as_ref()?.clamp_length_max(max)))
    }

    pub fn project_onto_normalized(&self, rhs: &PyVec3) -> PyResult<PyVec3> {
        Ok(PyVec3::from_vec3(
            self.as_ref()?.project_onto_normalized(*rhs.as_ref()?),
        ))
    }

    pub fn reject_from_normalized(&self, rhs: &PyVec3) -> PyResult<PyVec3> {
        Ok(PyVec3::from_vec3(
            self.as_ref()?.reject_from_normalized(*rhs.as_ref()?),
        ))
    }

    #[staticmethod]
    pub fn select(
        mask: (bool, bool, bool),
        if_true: &PyVec3,
        if_false: &PyVec3,
    ) -> PyResult<PyVec3> {
        let bmask = BVec3::new(mask.0, mask.1, mask.2);
        Ok(PyVec3::from_vec3(Vec3::select(
            bmask,
            *if_true.as_ref()?,
            *if_false.as_ref()?,
        )))
    }

    pub fn cmpeq(&self, rhs: &PyVec3) -> PyResult<(bool, bool, bool)> {
        let result = self.as_ref()?.cmpeq(*rhs.as_ref()?);
        Ok((result.x, result.y, result.z))
    }

    pub fn cmpne(&self, rhs: &PyVec3) -> PyResult<(bool, bool, bool)> {
        let result = self.as_ref()?.cmpne(*rhs.as_ref()?);
        Ok((result.x, result.y, result.z))
    }

    pub fn cmpge(&self, rhs: &PyVec3) -> PyResult<(bool, bool, bool)> {
        let result = self.as_ref()?.cmpge(*rhs.as_ref()?);
        Ok((result.x, result.y, result.z))
    }

    pub fn cmpgt(&self, rhs: &PyVec3) -> PyResult<(bool, bool, bool)> {
        let result = self.as_ref()?.cmpgt(*rhs.as_ref()?);
        Ok((result.x, result.y, result.z))
    }

    pub fn cmple(&self, rhs: &PyVec3) -> PyResult<(bool, bool, bool)> {
        let result = self.as_ref()?.cmple(*rhs.as_ref()?);
        Ok((result.x, result.y, result.z))
    }

    pub fn cmplt(&self, rhs: &PyVec3) -> PyResult<(bool, bool, bool)> {
        let result = self.as_ref()?.cmplt(*rhs.as_ref()?);
        Ok((result.x, result.y, result.z))
    }

    #[staticmethod]
    pub fn from_slice(slice: Vec<f32>) -> PyResult<PyVec3> {
        if slice.len() < 3 {
            return Err(PyValueError::new_err("Slice must have at least 3 elements"));
        }
        Ok(PyVec3::from_vec3(Vec3::from_slice(&slice)))
    }
}