Debian Codegraph @

third_party/rust/euclid/src/translation.rs

raw
1 // Copyright 2018 The Servo Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3 //
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
9
10 use crate::num::*;
11 use crate::UnknownUnit;
12 use crate::{point2, point3, vec2, vec3, Box2D, Box3D, Rect, Size2D};
13 use crate::{Point2D, Point3D, Transform2D, Transform3D, Vector2D, Vector3D};
14
15 use core::cmp::{Eq, PartialEq};
16 use core::fmt;
17 use core::hash::Hash;
18 use core::marker::PhantomData;
19 use core::ops::{Add, AddAssign, Neg, Sub, SubAssign};
20
21 #[cfg(feature = "bytemuck")]
22 use bytemuck::{Pod, Zeroable};
23 use num_traits::NumCast;
24 #[cfg(feature = "serde")]
25 use serde::{Deserialize, Serialize};
26
27 /// A 2d transformation from a space to another that can only express translations.
28 ///
29 /// The main benefit of this type over a Vector2D is the ability to cast
30 /// between a source and a destination spaces.
31 ///
32 /// Example:
33 ///
34 /// ```
35 /// use euclid::{Translation2D, Point2D, point2};
36 /// struct ParentSpace;
37 /// struct ChildSpace;
38 /// type ScrollOffset = Translation2D<i32, ParentSpace, ChildSpace>;
39 /// type ParentPoint = Point2D<i32, ParentSpace>;
40 /// type ChildPoint = Point2D<i32, ChildSpace>;
41 ///
42 /// let scrolling = ScrollOffset::new(0, 100);
43 /// let p1: ParentPoint = point2(0, 0);
44 /// let p2: ChildPoint = scrolling.transform_point(p1);
45 /// ```
46 ///
47 #[repr(C)]
48 #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
49 #[cfg_attr(
50 feature = "serde",
51 serde(bound(
52 serialize = "T: serde::Serialize",
53 deserialize = "T: serde::Deserialize<'de>"
54 ))
55 )]
56 pub struct Translation2D<T, Src, Dst> {
57 pub x: T,
58 pub y: T,
59 #[doc(hidden)]
60 pub _unit: PhantomData<(Src, Dst)>,
61 }
62
63 #[cfg(feature = "arbitrary")]
64 impl<'a, T, Src, Dst> arbitrary::Arbitrary<'a> for Translation2D<T, Src, Dst>
65 where
66 T: arbitrary::Arbitrary<'a>,
67 {
68 fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
69 let (x, y) = arbitrary::Arbitrary::arbitrary(u)?;
70 Ok(Translation2D {
71 x,
72 y,
73 _unit: PhantomData,
74 })
75 }
76 }
77
78 impl<T: Copy, Src, Dst> Copy for Translation2D<T, Src, Dst> {}
79
80 impl<T: Clone, Src, Dst> Clone for Translation2D<T, Src, Dst> {
81 fn clone(&self) -> Self {
82 Translation2D {
83 x: self.x.clone(),
84 y: self.y.clone(),
85 _unit: PhantomData,
86 }
87 }
88 }
89
90 impl<T, Src, Dst> Eq for Translation2D<T, Src, Dst> where T: Eq {}
91
92 impl<T, Src, Dst> PartialEq for Translation2D<T, Src, Dst>
93 where
94 T: PartialEq,
95 {
96 fn eq(&self, other: &Self) -> bool {
97 self.x == other.x && self.y == other.y
98 }
99 }
100
101 impl<T, Src, Dst> Hash for Translation2D<T, Src, Dst>
102 where
103 T: Hash,
104 {
105 fn hash<H: core::hash::Hasher>(&self, h: &mut H) {
106 self.x.hash(h);
107 self.y.hash(h);
108 }
109 }
110
111 impl<T, Src, Dst> Translation2D<T, Src, Dst> {
112 #[inline]
113 pub const fn new(x: T, y: T) -> Self {
114 Translation2D {
115 x,
116 y,
117 _unit: PhantomData,
118 }
119 }
120
121 #[inline]
122 pub fn splat(v: T) -> Self
123 where
124 T: Clone,
125 {
126 Translation2D {
127 x: v.clone(),
128 y: v,
129 _unit: PhantomData,
130 }
131 }
132
133 /// Creates no-op translation (both `x` and `y` is `zero()`).
134 #[inline]
135 pub fn identity() -> Self
136 where
137 T: Zero,
138 {
139 Self::new(T::zero(), T::zero())
140 }
141
142 /// Check if translation does nothing (both x and y is `zero()`).
143 ///
144 /// ```rust
145 /// use euclid::default::Translation2D;
146 ///
147 /// assert_eq!(Translation2D::<f32>::identity().is_identity(), true);
148 /// assert_eq!(Translation2D::new(0, 0).is_identity(), true);
149 /// assert_eq!(Translation2D::new(1, 0).is_identity(), false);
150 /// assert_eq!(Translation2D::new(0, 1).is_identity(), false);
151 /// ```
152 #[inline]
153 pub fn is_identity(&self) -> bool
154 where
155 T: Zero + PartialEq,
156 {
157 let _0 = T::zero();
158 self.x == _0 && self.y == _0
159 }
160
161 /// No-op, just cast the unit.
162 #[inline]
163 pub fn transform_size(&self, s: Size2D<T, Src>) -> Size2D<T, Dst> {
164 Size2D::new(s.width, s.height)
165 }
166 }
167
168 impl<T: Copy, Src, Dst> Translation2D<T, Src, Dst> {
169 /// Cast into a 2D vector.
170 #[inline]
171 pub fn to_vector(&self) -> Vector2D<T, Src> {
172 vec2(self.x, self.y)
173 }
174
175 /// Cast into an array with x and y.
176 #[inline]
177 pub fn to_array(&self) -> [T; 2] {
178 [self.x, self.y]
179 }
180
181 /// Cast into a tuple with x and y.
182 #[inline]
183 pub fn to_tuple(&self) -> (T, T) {
184 (self.x, self.y)
185 }
186
187 /// Drop the units, preserving only the numeric value.
188 #[inline]
189 pub fn to_untyped(&self) -> Translation2D<T, UnknownUnit, UnknownUnit> {
190 Translation2D {
191 x: self.x,
192 y: self.y,
193 _unit: PhantomData,
194 }
195 }
196
197 /// Tag a unitless value with units.
198 #[inline]
199 pub fn from_untyped(t: &Translation2D<T, UnknownUnit, UnknownUnit>) -> Self {
200 Translation2D {
201 x: t.x,
202 y: t.y,
203 _unit: PhantomData,
204 }
205 }
206
207 /// Returns the matrix representation of this translation.
208 #[inline]
209 pub fn to_transform(&self) -> Transform2D<T, Src, Dst>
210 where
211 T: Zero + One,
212 {
213 (*self).into()
214 }
215
216 /// Translate a point and cast its unit.
217 #[inline]
218 pub fn transform_point(&self, p: Point2D<T, Src>) -> Point2D<T::Output, Dst>
219 where
220 T: Add,
221 {
222 point2(p.x + self.x, p.y + self.y)
223 }
224
225 /// Translate a rectangle and cast its unit.
226 #[inline]
227 pub fn transform_rect(&self, r: &Rect<T, Src>) -> Rect<T::Output, Dst>
228 where
229 T: Add<Output = T>,
230 {
231 Rect {
232 origin: self.transform_point(r.origin),
233 size: self.transform_size(r.size),
234 }
235 }
236
237 /// Translate a 2D box and cast its unit.
238 #[inline]
239 pub fn transform_box(&self, r: &Box2D<T, Src>) -> Box2D<T::Output, Dst>
240 where
241 T: Add,
242 {
243 Box2D {
244 min: self.transform_point(r.min),
245 max: self.transform_point(r.max),
246 }
247 }
248
249 /// Return the inverse transformation.
250 #[inline]
251 pub fn inverse(&self) -> Translation2D<T::Output, Dst, Src>
252 where
253 T: Neg,
254 {
255 Translation2D::new(-self.x, -self.y)
256 }
257 }
258
259 impl<T: NumCast + Copy, Src, Dst> Translation2D<T, Src, Dst> {
260 /// Cast from one numeric representation to another, preserving the units.
261 ///
262 /// When casting from floating vector to integer coordinates, the decimals are truncated
263 /// as one would expect from a simple cast, but this behavior does not always make sense
264 /// geometrically. Consider using `round()`, `ceil()` or `floor()` before casting.
265 #[inline]
266 pub fn cast<NewT: NumCast>(self) -> Translation2D<NewT, Src, Dst> {
267 self.try_cast().unwrap()
268 }
269
270 /// Fallible cast from one numeric representation to another, preserving the units.
271 ///
272 /// When casting from floating vector to integer coordinates, the decimals are truncated
273 /// as one would expect from a simple cast, but this behavior does not always make sense
274 /// geometrically. Consider using `round()`, `ceil()` or `floor()` before casting.
275 pub fn try_cast<NewT: NumCast>(self) -> Option<Translation2D<NewT, Src, Dst>> {
276 match (NumCast::from(self.x), NumCast::from(self.y)) {
277 (Some(x), Some(y)) => Some(Translation2D::new(x, y)),
278 _ => None,
279 }
280 }
281
282 // Convenience functions for common casts.
283
284 /// Cast into an `f32` vector.
285 #[inline]
286 pub fn to_f32(self) -> Translation2D<f32, Src, Dst> {
287 self.cast()
288 }
289
290 /// Cast into an `f64` vector.
291 #[inline]
292 pub fn to_f64(self) -> Translation2D<f64, Src, Dst> {
293 self.cast()
294 }
295
296 /// Cast into an `usize` vector, truncating decimals if any.
297 ///
298 /// When casting from floating vector vectors, it is worth considering whether
299 /// to `round()`, `ceil()` or `floor()` before the cast in order to obtain
300 /// the desired conversion behavior.
301 #[inline]
302 pub fn to_usize(self) -> Translation2D<usize, Src, Dst> {
303 self.cast()
304 }
305
306 /// Cast into an `u32` vector, truncating decimals if any.
307 ///
308 /// When casting from floating vector vectors, it is worth considering whether
309 /// to `round()`, `ceil()` or `floor()` before the cast in order to obtain
310 /// the desired conversion behavior.
311 #[inline]
312 pub fn to_u32(self) -> Translation2D<u32, Src, Dst> {
313 self.cast()
314 }
315
316 /// Cast into an i32 vector, truncating decimals if any.
317 ///
318 /// When casting from floating vector vectors, it is worth considering whether
319 /// to `round()`, `ceil()` or `floor()` before the cast in order to obtain
320 /// the desired conversion behavior.
321 #[inline]
322 pub fn to_i32(self) -> Translation2D<i32, Src, Dst> {
323 self.cast()
324 }
325
326 /// Cast into an i64 vector, truncating decimals if any.
327 ///
328 /// When casting from floating vector vectors, it is worth considering whether
329 /// to `round()`, `ceil()` or `floor()` before the cast in order to obtain
330 /// the desired conversion behavior.
331 #[inline]
332 pub fn to_i64(self) -> Translation2D<i64, Src, Dst> {
333 self.cast()
334 }
335 }
336
337 #[cfg(feature = "bytemuck")]
338 unsafe impl<T: Zeroable, Src, Dst> Zeroable for Translation2D<T, Src, Dst> {}
339
340 #[cfg(feature = "bytemuck")]
341 unsafe impl<T: Pod, Src: 'static, Dst: 'static> Pod for Translation2D<T, Src, Dst> {}
342
343 impl<T: Add, Src, Dst1, Dst2> Add<Translation2D<T, Dst1, Dst2>> for Translation2D<T, Src, Dst1> {
344 type Output = Translation2D<T::Output, Src, Dst2>;
345
346 fn add(self, other: Translation2D<T, Dst1, Dst2>) -> Self::Output {
347 Translation2D::new(self.x + other.x, self.y + other.y)
348 }
349 }
350
351 impl<T: AddAssign, Src, Dst> AddAssign<Translation2D<T, Dst, Dst>> for Translation2D<T, Src, Dst> {
352 fn add_assign(&mut self, other: Translation2D<T, Dst, Dst>) {
353 self.x += other.x;
354 self.y += other.y;
355 }
356 }
357
358 impl<T: Sub, Src, Dst1, Dst2> Sub<Translation2D<T, Dst1, Dst2>> for Translation2D<T, Src, Dst2> {
359 type Output = Translation2D<T::Output, Src, Dst1>;
360
361 fn sub(self, other: Translation2D<T, Dst1, Dst2>) -> Self::Output {
362 Translation2D::new(self.x - other.x, self.y - other.y)
363 }
364 }
365
366 impl<T: SubAssign, Src, Dst> SubAssign<Translation2D<T, Dst, Dst>> for Translation2D<T, Src, Dst> {
367 fn sub_assign(&mut self, other: Translation2D<T, Dst, Dst>) {
368 self.x -= other.x;
369 self.y -= other.y;
370 }
371 }
372
373 impl<T, Src, Dst> From<Vector2D<T, Src>> for Translation2D<T, Src, Dst> {
374 fn from(v: Vector2D<T, Src>) -> Self {
375 Translation2D::new(v.x, v.y)
376 }
377 }
378
379 impl<T, Src, Dst> From<Translation2D<T, Src, Dst>> for Vector2D<T, Src> {
380 fn from(t: Translation2D<T, Src, Dst>) -> Self {
381 vec2(t.x, t.y)
382 }
383 }
384
385 impl<T, Src, Dst> From<Translation2D<T, Src, Dst>> for Transform2D<T, Src, Dst>
386 where
387 T: Zero + One,
388 {
389 fn from(t: Translation2D<T, Src, Dst>) -> Self {
390 Transform2D::translation(t.x, t.y)
391 }
392 }
393
394 impl<T, Src, Dst> Default for Translation2D<T, Src, Dst>
395 where
396 T: Zero,
397 {
398 fn default() -> Self {
399 Self::identity()
400 }
401 }
402
403 impl<T: fmt::Debug, Src, Dst> fmt::Debug for Translation2D<T, Src, Dst> {
404 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
405 write!(f, "Translation({:?},{:?})", self.x, self.y)
406 }
407 }
408
409 /// A 3d transformation from a space to another that can only express translations.
410 ///
411 /// The main benefit of this type over a Vector3D is the ability to cast
412 /// between a source and a destination spaces.
413 #[repr(C)]
414 pub struct Translation3D<T, Src, Dst> {
415 pub x: T,
416 pub y: T,
417 pub z: T,
418 #[doc(hidden)]
419 pub _unit: PhantomData<(Src, Dst)>,
420 }
421
422 impl<T: Copy, Src, Dst> Copy for Translation3D<T, Src, Dst> {}
423
424 impl<T: Clone, Src, Dst> Clone for Translation3D<T, Src, Dst> {
425 fn clone(&self) -> Self {
426 Translation3D {
427 x: self.x.clone(),
428 y: self.y.clone(),
429 z: self.z.clone(),
430 _unit: PhantomData,
431 }
432 }
433 }
434
435 #[cfg(feature = "serde")]
436 impl<'de, T, Src, Dst> serde::Deserialize<'de> for Translation3D<T, Src, Dst>
437 where
438 T: serde::Deserialize<'de>,
439 {
440 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
441 where
442 D: serde::Deserializer<'de>,
443 {
444 let (x, y, z) = serde::Deserialize::deserialize(deserializer)?;
445 Ok(Translation3D {
446 x,
447 y,
448 z,
449 _unit: PhantomData,
450 })
451 }
452 }
453
454 #[cfg(feature = "serde")]
455 impl<T, Src, Dst> serde::Serialize for Translation3D<T, Src, Dst>
456 where
457 T: serde::Serialize,
458 {
459 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
460 where
461 S: serde::Serializer,
462 {
463 (&self.x, &self.y, &self.z).serialize(serializer)
464 }
465 }
466
467 impl<T, Src, Dst> Eq for Translation3D<T, Src, Dst> where T: Eq {}
468
469 impl<T, Src, Dst> PartialEq for Translation3D<T, Src, Dst>
470 where
471 T: PartialEq,
472 {
473 fn eq(&self, other: &Self) -> bool {
474 self.x == other.x && self.y == other.y && self.z == other.z
475 }
476 }
477
478 impl<T, Src, Dst> Hash for Translation3D<T, Src, Dst>
479 where
480 T: Hash,
481 {
482 fn hash<H: core::hash::Hasher>(&self, h: &mut H) {
483 self.x.hash(h);
484 self.y.hash(h);
485 self.z.hash(h);
486 }
487 }
488
489 impl<T, Src, Dst> Translation3D<T, Src, Dst> {
490 #[inline]
491 pub const fn new(x: T, y: T, z: T) -> Self {
492 Translation3D {
493 x,
494 y,
495 z,
496 _unit: PhantomData,
497 }
498 }
499
500 #[inline]
501 pub fn splat(v: T) -> Self
502 where
503 T: Clone,
504 {
505 Translation3D {
506 x: v.clone(),
507 y: v.clone(),
508 z: v,
509 _unit: PhantomData,
510 }
511 }
512
513 /// Creates no-op translation (`x`, `y` and `z` is `zero()`).
514 #[inline]
515 pub fn identity() -> Self
516 where
517 T: Zero,
518 {
519 Translation3D::new(T::zero(), T::zero(), T::zero())
520 }
521
522 /// Check if translation does nothing (`x`, `y` and `z` is `zero()`).
523 ///
524 /// ```rust
525 /// use euclid::default::Translation3D;
526 ///
527 /// assert_eq!(Translation3D::<f32>::identity().is_identity(), true);
528 /// assert_eq!(Translation3D::new(0, 0, 0).is_identity(), true);
529 /// assert_eq!(Translation3D::new(1, 0, 0).is_identity(), false);
530 /// assert_eq!(Translation3D::new(0, 1, 0).is_identity(), false);
531 /// assert_eq!(Translation3D::new(0, 0, 1).is_identity(), false);
532 /// ```
533 #[inline]
534 pub fn is_identity(&self) -> bool
535 where
536 T: Zero + PartialEq,
537 {
538 let _0 = T::zero();
539 self.x == _0 && self.y == _0 && self.z == _0
540 }
541
542 /// No-op, just cast the unit.
543 #[inline]
544 pub fn transform_size(self, s: Size2D<T, Src>) -> Size2D<T, Dst> {
545 Size2D::new(s.width, s.height)
546 }
547 }
548
549 impl<T: Copy, Src, Dst> Translation3D<T, Src, Dst> {
550 /// Cast into a 3D vector.
551 #[inline]
552 pub fn to_vector(&self) -> Vector3D<T, Src> {
553 vec3(self.x, self.y, self.z)
554 }
555
556 /// Cast into an array with x, y and z.
557 #[inline]
558 pub fn to_array(&self) -> [T; 3] {
559 [self.x, self.y, self.z]
560 }
561
562 /// Cast into a tuple with x, y and z.
563 #[inline]
564 pub fn to_tuple(&self) -> (T, T, T) {
565 (self.x, self.y, self.z)
566 }
567
568 /// Drop the units, preserving only the numeric value.
569 #[inline]
570 pub fn to_untyped(&self) -> Translation3D<T, UnknownUnit, UnknownUnit> {
571 Translation3D {
572 x: self.x,
573 y: self.y,
574 z: self.z,
575 _unit: PhantomData,
576 }
577 }
578
579 /// Tag a unitless value with units.
580 #[inline]
581 pub fn from_untyped(t: &Translation3D<T, UnknownUnit, UnknownUnit>) -> Self {
582 Translation3D {
583 x: t.x,
584 y: t.y,
585 z: t.z,
586 _unit: PhantomData,
587 }
588 }
589
590 /// Returns the matrix representation of this translation.
591 #[inline]
592 pub fn to_transform(&self) -> Transform3D<T, Src, Dst>
593 where
594 T: Zero + One,
595 {
596 (*self).into()
597 }
598
599 /// Translate a point and cast its unit.
600 #[inline]
601 pub fn transform_point3d(&self, p: &Point3D<T, Src>) -> Point3D<T::Output, Dst>
602 where
603 T: Add,
604 {
605 point3(p.x + self.x, p.y + self.y, p.z + self.z)
606 }
607
608 /// Translate a point and cast its unit.
609 #[inline]
610 pub fn transform_point2d(&self, p: &Point2D<T, Src>) -> Point2D<T::Output, Dst>
611 where
612 T: Add,
613 {
614 point2(p.x + self.x, p.y + self.y)
615 }
616
617 /// Translate a 2D box and cast its unit.
618 #[inline]
619 pub fn transform_box2d(&self, b: &Box2D<T, Src>) -> Box2D<T::Output, Dst>
620 where
621 T: Add,
622 {
623 Box2D {
624 min: self.transform_point2d(&b.min),
625 max: self.transform_point2d(&b.max),
626 }
627 }
628
629 /// Translate a 3D box and cast its unit.
630 #[inline]
631 pub fn transform_box3d(&self, b: &Box3D<T, Src>) -> Box3D<T::Output, Dst>
632 where
633 T: Add,
634 {
635 Box3D {
636 min: self.transform_point3d(&b.min),
637 max: self.transform_point3d(&b.max),
638 }
639 }
640
641 /// Translate a rectangle and cast its unit.
642 #[inline]
643 pub fn transform_rect(&self, r: &Rect<T, Src>) -> Rect<T, Dst>
644 where
645 T: Add<Output = T>,
646 {
647 Rect {
648 origin: self.transform_point2d(&r.origin),
649 size: self.transform_size(r.size),
650 }
651 }
652
653 /// Return the inverse transformation.
654 #[inline]
655 pub fn inverse(&self) -> Translation3D<T::Output, Dst, Src>
656 where
657 T: Neg,
658 {
659 Translation3D::new(-self.x, -self.y, -self.z)
660 }
661 }
662
663 impl<T: NumCast + Copy, Src, Dst> Translation3D<T, Src, Dst> {
664 /// Cast from one numeric representation to another, preserving the units.
665 ///
666 /// When casting from floating vector to integer coordinates, the decimals are truncated
667 /// as one would expect from a simple cast, but this behavior does not always make sense
668 /// geometrically. Consider using `round()`, `ceil()` or `floor()` before casting.
669 #[inline]
670 pub fn cast<NewT: NumCast>(self) -> Translation3D<NewT, Src, Dst> {
671 self.try_cast().unwrap()
672 }
673
674 /// Fallible cast from one numeric representation to another, preserving the units.
675 ///
676 /// When casting from floating vector to integer coordinates, the decimals are truncated
677 /// as one would expect from a simple cast, but this behavior does not always make sense
678 /// geometrically. Consider using `round()`, `ceil()` or `floor()` before casting.
679 pub fn try_cast<NewT: NumCast>(self) -> Option<Translation3D<NewT, Src, Dst>> {
680 match (
681 NumCast::from(self.x),
682 NumCast::from(self.y),
683 NumCast::from(self.z),
684 ) {
685 (Some(x), Some(y), Some(z)) => Some(Translation3D::new(x, y, z)),
686 _ => None,
687 }
688 }
689
690 // Convenience functions for common casts.
691
692 /// Cast into an `f32` vector.
693 #[inline]
694 pub fn to_f32(self) -> Translation3D<f32, Src, Dst> {
695 self.cast()
696 }
697
698 /// Cast into an `f64` vector.
699 #[inline]
700 pub fn to_f64(self) -> Translation3D<f64, Src, Dst> {
701 self.cast()
702 }
703
704 /// Cast into an `usize` vector, truncating decimals if any.
705 ///
706 /// When casting from floating vector vectors, it is worth considering whether
707 /// to `round()`, `ceil()` or `floor()` before the cast in order to obtain
708 /// the desired conversion behavior.
709 #[inline]
710 pub fn to_usize(self) -> Translation3D<usize, Src, Dst> {
711 self.cast()
712 }
713
714 /// Cast into an `u32` vector, truncating decimals if any.
715 ///
716 /// When casting from floating vector vectors, it is worth considering whether
717 /// to `round()`, `ceil()` or `floor()` before the cast in order to obtain
718 /// the desired conversion behavior.
719 #[inline]
720 pub fn to_u32(self) -> Translation3D<u32, Src, Dst> {
721 self.cast()
722 }
723
724 /// Cast into an i32 vector, truncating decimals if any.
725 ///
726 /// When casting from floating vector vectors, it is worth considering whether
727 /// to `round()`, `ceil()` or `floor()` before the cast in order to obtain
728 /// the desired conversion behavior.
729 #[inline]
730 pub fn to_i32(self) -> Translation3D<i32, Src, Dst> {
731 self.cast()
732 }
733
734 /// Cast into an i64 vector, truncating decimals if any.
735 ///
736 /// When casting from floating vector vectors, it is worth considering whether
737 /// to `round()`, `ceil()` or `floor()` before the cast in order to obtain
738 /// the desired conversion behavior.
739 #[inline]
740 pub fn to_i64(self) -> Translation3D<i64, Src, Dst> {
741 self.cast()
742 }
743 }
744
745 #[cfg(feature = "bytemuck")]
746 unsafe impl<T: Zeroable, Src, Dst> Zeroable for Translation3D<T, Src, Dst> {}
747
748 #[cfg(feature = "bytemuck")]
749 unsafe impl<T: Pod, Src: 'static, Dst: 'static> Pod for Translation3D<T, Src, Dst> {}
750
751 impl<T: Add, Src, Dst1, Dst2> Add<Translation3D<T, Dst1, Dst2>> for Translation3D<T, Src, Dst1> {
752 type Output = Translation3D<T::Output, Src, Dst2>;
753
754 fn add(self, other: Translation3D<T, Dst1, Dst2>) -> Self::Output {
755 Translation3D::new(self.x + other.x, self.y + other.y, self.z + other.z)
756 }
757 }
758
759 impl<T: AddAssign, Src, Dst> AddAssign<Translation3D<T, Dst, Dst>> for Translation3D<T, Src, Dst> {
760 fn add_assign(&mut self, other: Translation3D<T, Dst, Dst>) {
761 self.x += other.x;
762 self.y += other.y;
763 self.z += other.z;
764 }
765 }
766
767 impl<T: Sub, Src, Dst1, Dst2> Sub<Translation3D<T, Dst1, Dst2>> for Translation3D<T, Src, Dst2> {
768 type Output = Translation3D<T::Output, Src, Dst1>;
769
770 fn sub(self, other: Translation3D<T, Dst1, Dst2>) -> Self::Output {
771 Translation3D::new(self.x - other.x, self.y - other.y, self.z - other.z)
772 }
773 }
774
775 impl<T: SubAssign, Src, Dst> SubAssign<Translation3D<T, Dst, Dst>> for Translation3D<T, Src, Dst> {
776 fn sub_assign(&mut self, other: Translation3D<T, Dst, Dst>) {
777 self.x -= other.x;
778 self.y -= other.y;
779 self.z -= other.z;
780 }
781 }
782
783 impl<T, Src, Dst> From<Vector3D<T, Src>> for Translation3D<T, Src, Dst> {
784 fn from(v: Vector3D<T, Src>) -> Self {
785 Translation3D::new(v.x, v.y, v.z)
786 }
787 }
788
789 impl<T, Src, Dst> From<Translation3D<T, Src, Dst>> for Vector3D<T, Src> {
790 fn from(t: Translation3D<T, Src, Dst>) -> Self {
791 vec3(t.x, t.y, t.z)
792 }
793 }
794
795 impl<T, Src, Dst> From<Translation3D<T, Src, Dst>> for Transform3D<T, Src, Dst>
796 where
797 T: Zero + One,
798 {
799 fn from(t: Translation3D<T, Src, Dst>) -> Self {
800 Transform3D::translation(t.x, t.y, t.z)
801 }
802 }
803
804 impl<T, Src, Dst> Default for Translation3D<T, Src, Dst>
805 where
806 T: Zero,
807 {
808 fn default() -> Self {
809 Self::identity()
810 }
811 }
812
813 impl<T: fmt::Debug, Src, Dst> fmt::Debug for Translation3D<T, Src, Dst> {
814 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
815 write!(f, "Translation({:?},{:?},{:?})", self.x, self.y, self.z)
816 }
817 }
818
819 #[cfg(test)]
820 mod _2d {
821 #[test]
822 fn simple() {
823 use crate::{rect, Rect, Translation2D};
824
825 struct A;
826 struct B;
827
828 type Translation = Translation2D<i32, A, B>;
829 type SrcRect = Rect<i32, A>;
830 type DstRect = Rect<i32, B>;
831
832 let tx = Translation::new(10, -10);
833 let r1: SrcRect = rect(10, 20, 30, 40);
834 let r2: DstRect = tx.transform_rect(&r1);
835 assert_eq!(r2, rect(20, 10, 30, 40));
836
837 let inv_tx = tx.inverse();
838 assert_eq!(inv_tx.transform_rect(&r2), r1);
839
840 assert!((tx + inv_tx).is_identity());
841 }
842
843 /// Operation tests
844 mod ops {
845 use crate::default::Translation2D;
846
847 #[test]
848 fn test_add() {
849 let t1 = Translation2D::new(1.0, 2.0);
850 let t2 = Translation2D::new(3.0, 4.0);
851 assert_eq!(t1 + t2, Translation2D::new(4.0, 6.0));
852
853 let t1 = Translation2D::new(1.0, 2.0);
854 let t2 = Translation2D::new(0.0, 0.0);
855 assert_eq!(t1 + t2, Translation2D::new(1.0, 2.0));
856
857 let t1 = Translation2D::new(1.0, 2.0);
858 let t2 = Translation2D::new(-3.0, -4.0);
859 assert_eq!(t1 + t2, Translation2D::new(-2.0, -2.0));
860
861 let t1 = Translation2D::new(0.0, 0.0);
862 let t2 = Translation2D::new(0.0, 0.0);
863 assert_eq!(t1 + t2, Translation2D::new(0.0, 0.0));
864 }
865
866 #[test]
867 pub fn test_add_assign() {
868 let mut t = Translation2D::new(1.0, 2.0);
869 t += Translation2D::new(3.0, 4.0);
870 assert_eq!(t, Translation2D::new(4.0, 6.0));
871
872 let mut t = Translation2D::new(1.0, 2.0);
873 t += Translation2D::new(0.0, 0.0);
874 assert_eq!(t, Translation2D::new(1.0, 2.0));
875
876 let mut t = Translation2D::new(1.0, 2.0);
877 t += Translation2D::new(-3.0, -4.0);
878 assert_eq!(t, Translation2D::new(-2.0, -2.0));
879
880 let mut t = Translation2D::new(0.0, 0.0);
881 t += Translation2D::new(0.0, 0.0);
882 assert_eq!(t, Translation2D::new(0.0, 0.0));
883 }
884
885 #[test]
886 pub fn test_sub() {
887 let t1 = Translation2D::new(1.0, 2.0);
888 let t2 = Translation2D::new(3.0, 4.0);
889 assert_eq!(t1 - t2, Translation2D::new(-2.0, -2.0));
890
891 let t1 = Translation2D::new(1.0, 2.0);
892 let t2 = Translation2D::new(0.0, 0.0);
893 assert_eq!(t1 - t2, Translation2D::new(1.0, 2.0));
894
895 let t1 = Translation2D::new(1.0, 2.0);
896 let t2 = Translation2D::new(-3.0, -4.0);
897 assert_eq!(t1 - t2, Translation2D::new(4.0, 6.0));
898
899 let t1 = Translation2D::new(0.0, 0.0);
900 let t2 = Translation2D::new(0.0, 0.0);
901 assert_eq!(t1 - t2, Translation2D::new(0.0, 0.0));
902 }
903
904 #[test]
905 pub fn test_sub_assign() {
906 let mut t = Translation2D::new(1.0, 2.0);
907 t -= Translation2D::new(3.0, 4.0);
908 assert_eq!(t, Translation2D::new(-2.0, -2.0));
909
910 let mut t = Translation2D::new(1.0, 2.0);
911 t -= Translation2D::new(0.0, 0.0);
912 assert_eq!(t, Translation2D::new(1.0, 2.0));
913
914 let mut t = Translation2D::new(1.0, 2.0);
915 t -= Translation2D::new(-3.0, -4.0);
916 assert_eq!(t, Translation2D::new(4.0, 6.0));
917
918 let mut t = Translation2D::new(0.0, 0.0);
919 t -= Translation2D::new(0.0, 0.0);
920 assert_eq!(t, Translation2D::new(0.0, 0.0));
921 }
922 }
923 }
924
925 #[cfg(test)]
926 mod _3d {
927 #[test]
928 fn simple() {
929 use crate::{point3, Point3D, Translation3D};
930
931 struct A;
932 struct B;
933
934 type Translation = Translation3D<i32, A, B>;
935 type SrcPoint = Point3D<i32, A>;
936 type DstPoint = Point3D<i32, B>;
937
938 let tx = Translation::new(10, -10, 100);
939 let p1: SrcPoint = point3(10, 20, 30);
940 let p2: DstPoint = tx.transform_point3d(&p1);
941 assert_eq!(p2, point3(20, 10, 130));
942
943 let inv_tx = tx.inverse();
944 assert_eq!(inv_tx.transform_point3d(&p2), p1);
945
946 assert!((tx + inv_tx).is_identity());
947 }
948
949 /// Operation tests
950 mod ops {
951 use crate::default::Translation3D;
952
953 #[test]
954 pub fn test_add() {
955 let t1 = Translation3D::new(1.0, 2.0, 3.0);
956 let t2 = Translation3D::new(4.0, 5.0, 6.0);
957 assert_eq!(t1 + t2, Translation3D::new(5.0, 7.0, 9.0));
958
959 let t1 = Translation3D::new(1.0, 2.0, 3.0);
960 let t2 = Translation3D::new(0.0, 0.0, 0.0);
961 assert_eq!(t1 + t2, Translation3D::new(1.0, 2.0, 3.0));
962
963 let t1 = Translation3D::new(1.0, 2.0, 3.0);
964 let t2 = Translation3D::new(-4.0, -5.0, -6.0);
965 assert_eq!(t1 + t2, Translation3D::new(-3.0, -3.0, -3.0));
966
967 let t1 = Translation3D::new(0.0, 0.0, 0.0);
968 let t2 = Translation3D::new(0.0, 0.0, 0.0);
969 assert_eq!(t1 + t2, Translation3D::new(0.0, 0.0, 0.0));
970 }
971
972 #[test]
973 pub fn test_add_assign() {
974 let mut t = Translation3D::new(1.0, 2.0, 3.0);
975 t += Translation3D::new(4.0, 5.0, 6.0);
976 assert_eq!(t, Translation3D::new(5.0, 7.0, 9.0));
977
978 let mut t = Translation3D::new(1.0, 2.0, 3.0);
979 t += Translation3D::new(0.0, 0.0, 0.0);
980 assert_eq!(t, Translation3D::new(1.0, 2.0, 3.0));
981
982 let mut t = Translation3D::new(1.0, 2.0, 3.0);
983 t += Translation3D::new(-4.0, -5.0, -6.0);
984 assert_eq!(t, Translation3D::new(-3.0, -3.0, -3.0));
985
986 let mut t = Translation3D::new(0.0, 0.0, 0.0);
987 t += Translation3D::new(0.0, 0.0, 0.0);
988 assert_eq!(t, Translation3D::new(0.0, 0.0, 0.0));
989 }
990
991 #[test]
992 pub fn test_sub() {
993 let t1 = Translation3D::new(1.0, 2.0, 3.0);
994 let t2 = Translation3D::new(4.0, 5.0, 6.0);
995 assert_eq!(t1 - t2, Translation3D::new(-3.0, -3.0, -3.0));
996
997 let t1 = Translation3D::new(1.0, 2.0, 3.0);
998 let t2 = Translation3D::new(0.0, 0.0, 0.0);
999 assert_eq!(t1 - t2, Translation3D::new(1.0, 2.0, 3.0));
1000
1001 let t1 = Translation3D::new(1.0, 2.0, 3.0);
1002 let t2 = Translation3D::new(-4.0, -5.0, -6.0);
1003 assert_eq!(t1 - t2, Translation3D::new(5.0, 7.0, 9.0));
1004
1005 let t1 = Translation3D::new(0.0, 0.0, 0.0);
1006 let t2 = Translation3D::new(0.0, 0.0, 0.0);
1007 assert_eq!(t1 - t2, Translation3D::new(0.0, 0.0, 0.0));
1008 }
1009
1010 #[test]
1011 pub fn test_sub_assign() {
1012 let mut t = Translation3D::new(1.0, 2.0, 3.0);
1013 t -= Translation3D::new(4.0, 5.0, 6.0);
1014 assert_eq!(t, Translation3D::new(-3.0, -3.0, -3.0));
1015
1016 let mut t = Translation3D::new(1.0, 2.0, 3.0);
1017 t -= Translation3D::new(0.0, 0.0, 0.0);
1018 assert_eq!(t, Translation3D::new(1.0, 2.0, 3.0));
1019
1020 let mut t = Translation3D::new(1.0, 2.0, 3.0);
1021 t -= Translation3D::new(-4.0, -5.0, -6.0);
1022 assert_eq!(t, Translation3D::new(5.0, 7.0, 9.0));
1023
1024 let mut t = Translation3D::new(0.0, 0.0, 0.0);
1025 t -= Translation3D::new(0.0, 0.0, 0.0);
1026 assert_eq!(t, Translation3D::new(0.0, 0.0, 0.0));
1027 }
1028 }
1029 }