| 1 | |
| 2 | // calculations inspired by |
| 3 | // https://github.com/AcademySoftwareFoundation/openexr/blob/master/OpenEXR/IlmImf/ImfTiledMisc.cpp |
| 4 | |
| 5 | //! Simple math utilities. |
| 6 | |
| 7 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::convertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/::TryFromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/TryFrom#; |
| 8 | use craterust-analyzer cargo exr 1.74.0 crate/::errorrust-analyzer cargo exr 1.74.0 error/::{i32_to_usizerust-analyzer cargo exr 1.74.0 error/i32_to_usize().}; |
| 9 | use craterust-analyzer cargo exr 1.74.0 crate/::errorrust-analyzer cargo exr 1.74.0 error/::Resultrust-analyzer cargo exr 1.74.0 error/Result#; |
| 10 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::opsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/::{Addrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Add#, Subrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Sub#, Divrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Div#, Mulrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Mul#}; |
| 11 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Debug#; |
| 12 | |
| 13 | /// Simple two-dimensional vector of any numerical type. |
| 14 | /// Supports only few mathematical operations |
| 15 | /// as this is used mainly as data struct. |
| 16 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, Hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/macros/Hash:, Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default:)] |
| 17 | pub struct Vec2rust-analyzer cargo exr 1.74.0 math/Vec2#<T> (pub T, pub T); |
| 18 | |
| 19 | impl<T> Vec2rust-analyzer cargo exr 1.74.0 math/Vec2#<T> { |
| 20 | |
| 21 | /// Returns the vector with the maximum of either coordinates. |
| 22 | pub fn maxrust-analyzer cargo exr 1.74.0 math/impl#[`Vec2<T>`]max().(self, other: Selfrust-analyzer cargo exr 1.74.0 math/impl#[`Vec2<T>`]) -> Selfrust-analyzer cargo exr 1.74.0 math/impl#[`Vec2<T>`] where T: Ordrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord# { |
| 23 | Vec2rust-analyzer cargo exr 1.74.0 math/Vec2#(self.0rust-analyzer cargo exr 1.74.0 math/Vec2#0..maxrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord#max().(other.0rust-analyzer cargo exr 1.74.0 math/Vec2#0.), self.1rust-analyzer cargo exr 1.74.0 math/Vec2#1..maxrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord#max().(other.1rust-analyzer cargo exr 1.74.0 math/Vec2#1.)) |
| 24 | } |
| 25 | |
| 26 | /// Returns the vector with the minimum of either coordinates. |
| 27 | pub fn minrust-analyzer cargo exr 1.74.0 math/impl#[`Vec2<T>`]min().(self, other: Selfrust-analyzer cargo exr 1.74.0 math/impl#[`Vec2<T>`]) -> Selfrust-analyzer cargo exr 1.74.0 math/impl#[`Vec2<T>`] where T: Ordrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord# { |
| 28 | Vec2rust-analyzer cargo exr 1.74.0 math/Vec2#(self.0rust-analyzer cargo exr 1.74.0 math/Vec2#0..minrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord#min().(other.0rust-analyzer cargo exr 1.74.0 math/Vec2#0.), self.1rust-analyzer cargo exr 1.74.0 math/Vec2#1..minrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord#min().(other.1rust-analyzer cargo exr 1.74.0 math/Vec2#1.)) |
| 29 | } |
| 30 | |
| 31 | /// Try to convert all components of this vector to a new type, |
| 32 | /// yielding either a vector of that new type, or an error. |
| 33 | pub fn try_fromrust-analyzer cargo exr 1.74.0 math/impl#[`Vec2<T>`]try_from().<S>(value: Vec2rust-analyzer cargo exr 1.74.0 math/Vec2#<S>) -> stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Selfrust-analyzer cargo exr 1.74.0 math/impl#[`Vec2<T>`], T::Errorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/TryFrom#Error#> where T: TryFromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/TryFrom#<S> { |
| 34 | let x = T::try_fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/TryFrom#try_from().(value.0rust-analyzer cargo exr 1.74.0 math/Vec2#0.)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 35 | let y = T::try_fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/TryFrom#try_from().(value.1rust-analyzer cargo exr 1.74.0 math/Vec2#1.)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 36 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Vec2rust-analyzer cargo exr 1.74.0 math/Vec2#(x, y)) |
| 37 | } |
| 38 | |
| 39 | |
| 40 | |
| 41 | /// Seeing this vector as a dimension or size (width and height), |
| 42 | /// this returns the area that this dimensions contains (`width * height`). |
| 43 | #[inline] pub fn arearust-analyzer cargo exr 1.74.0 math/impl#[`Vec2<T>`]area().(self) -> T where T: stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::opsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/::Mulrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Mul#<T, Outputrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Mul#Output# = T> { |
| 44 | self.0rust-analyzer cargo exr 1.74.0 math/Vec2#0. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Mul#mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Mul#mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Mul#mul().self.1rust-analyzer cargo exr 1.74.0 math/Vec2#1. |
| 45 | } |
| 46 | |
| 47 | /// The first component of this 2D vector. |
| 48 | #[inline] pub fn xrust-analyzer cargo exr 1.74.0 math/impl#[`Vec2<T>`]x().(self) -> T { self.0rust-analyzer cargo exr 1.74.0 math/Vec2#0. } |
| 49 | |
| 50 | /// The second component of this 2D vector. |
| 51 | #[inline] pub fn yrust-analyzer cargo exr 1.74.0 math/impl#[`Vec2<T>`]y().(self) -> T { self.1rust-analyzer cargo exr 1.74.0 math/Vec2#1. } |
| 52 | |
| 53 | /// The first component of this 2D vector. |
| 54 | #[inline] pub fn widthrust-analyzer cargo exr 1.74.0 math/impl#[`Vec2<T>`]width().(self) -> T { self.0rust-analyzer cargo exr 1.74.0 math/Vec2#0. } |
| 55 | |
| 56 | /// The second component of this 2D vector. |
| 57 | #[inline] pub fn heightrust-analyzer cargo exr 1.74.0 math/impl#[`Vec2<T>`]height().(self) -> T { self.1rust-analyzer cargo exr 1.74.0 math/Vec2#1. } |
| 58 | |
| 59 | // TODO use this! |
| 60 | /// Convert this two-dimensional coordinate to an index suited for one-dimensional flattened image arrays. |
| 61 | /// Works for images that store the pixels row by row, one after another, in a single array. |
| 62 | /// In debug mode, panics for an index out of bounds. |
| 63 | #[inline] pub fn flat_index_for_sizerust-analyzer cargo exr 1.74.0 math/impl#[`Vec2<T>`]flat_index_for_size().(self, resolution: Vec2rust-analyzer cargo exr 1.74.0 math/Vec2#<T>) -> T |
| 64 | where T: Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy# + Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Debug# + Ordrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord# + Mulrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Mul#<Outputrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Mul#Output#=T> + Addrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Add#<Outputrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Add#Output#=T> |
| 65 | { |
| 66 | debug_assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/debug_assert!!( |
| 67 | self.xrust-analyzer cargo exr 1.74.0 math/impl#[`Vec2<T>`]x().() <rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#lt(). resolution.widthrust-analyzer cargo exr 1.74.0 math/impl#[`Vec2<T>`]width().() && self.yrust-analyzer cargo exr 1.74.0 math/impl#[`Vec2<T>`]y().() <rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#lt(). resolution.heightrust-analyzer cargo exr 1.74.0 math/impl#[`Vec2<T>`]height().(), |
| 68 | "Vec2 index {:?} is invalid for resolution {:?}", self, resolution |
| 69 | ); |
| 70 | |
| 71 | let Vec2rust-analyzer cargo exr 1.74.0 math/Vec2#(x, y) = self; |
| 72 | y rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Mul#mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Mul#mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Mul#mul().resolution.widthrust-analyzer cargo exr 1.74.0 math/impl#[`Vec2<T>`]width().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Add#add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Add#add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Add#add().x |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | |
| 77 | |
| 78 | impl Vec2rust-analyzer cargo exr 1.74.0 math/Vec2#<i32> { |
| 79 | |
| 80 | /// Try to convert to [`Vec2<usize>`], returning an error on negative numbers. |
| 81 | pub fn to_usizerust-analyzer cargo exr 1.74.0 math/impl#[`Vec2<i32>`]to_usize().(self, error_message: &'static str) -> Resultrust-analyzer cargo exr 1.74.0 error/Result#<Vec2rust-analyzer cargo exr 1.74.0 math/Vec2#<usize>> { |
| 82 | let x = i32_to_usizerust-analyzer cargo exr 1.74.0 error/i32_to_usize().(self.0rust-analyzer cargo exr 1.74.0 math/Vec2#0., error_message)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 83 | let y = i32_to_usizerust-analyzer cargo exr 1.74.0 error/i32_to_usize().(self.1rust-analyzer cargo exr 1.74.0 math/Vec2#1., error_message)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 84 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Vec2rust-analyzer cargo exr 1.74.0 math/Vec2#(x, y)) |
| 85 | } |
| 86 | |
| 87 | } |
| 88 | |
| 89 | impl Vec2rust-analyzer cargo exr 1.74.0 math/Vec2#<usize> { |
| 90 | |
| 91 | /// Panics for too large values |
| 92 | pub fn to_i32rust-analyzer cargo exr 1.74.0 math/impl#[`Vec2<usize>`]to_i32().(self) -> Vec2rust-analyzer cargo exr 1.74.0 math/Vec2#<i32> { |
| 93 | let x = i32::try_fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/num/ptr_try_from_impls/impl#[i32][`TryFrom<usize>`]try_from().(self.0rust-analyzer cargo exr 1.74.0 math/Vec2#0.).expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("vector x coordinate too large"); |
| 94 | let y = i32::try_fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/num/ptr_try_from_impls/impl#[i32][`TryFrom<usize>`]try_from().(self.1rust-analyzer cargo exr 1.74.0 math/Vec2#1.).expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("vector y coordinate too large"); |
| 95 | Vec2rust-analyzer cargo exr 1.74.0 math/Vec2#(x, y) |
| 96 | } |
| 97 | |
| 98 | } |
| 99 | |
| 100 | |
| 101 | impl<T: stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::opsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/::Addrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Add#<T>> stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::opsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/::Addrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Add#<Vec2rust-analyzer cargo exr 1.74.0 math/Vec2#<T>> for Vec2rust-analyzer cargo exr 1.74.0 math/Vec2#<T> { |
| 102 | type Outputrust-analyzer cargo exr 1.74.0 math/impl#[`Vec2<T>`][`Add<Self>`]Output# = Vec2rust-analyzer cargo exr 1.74.0 math/Vec2#<T::Outputrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Add#Output#>; |
| 103 | fn addrust-analyzer cargo exr 1.74.0 math/impl#[`Vec2<T>`][`Add<Self>`]add().(self, other: Vec2rust-analyzer cargo exr 1.74.0 math/Vec2#<T>) -> Selfrust-analyzer cargo exr 1.74.0 math/impl#[`Vec2<T>`][`Add<Self>`]::Outputrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Add#Output# { |
| 104 | Vec2rust-analyzer cargo exr 1.74.0 math/Vec2#(self.0rust-analyzer cargo exr 1.74.0 math/Vec2#0. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Add#add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Add#add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Add#add().other.0rust-analyzer cargo exr 1.74.0 math/Vec2#0., self.1rust-analyzer cargo exr 1.74.0 math/Vec2#1. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Add#add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Add#add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Add#add().other.1rust-analyzer cargo exr 1.74.0 math/Vec2#1.) |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | impl<T: stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::opsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/::Subrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Sub#<T>> stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::opsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/::Subrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Sub#<Vec2rust-analyzer cargo exr 1.74.0 math/Vec2#<T>> for Vec2rust-analyzer cargo exr 1.74.0 math/Vec2#<T> { |
| 109 | type Outputrust-analyzer cargo exr 1.74.0 math/impl#[`Vec2<T>`][`Sub<Self>`]Output# = Vec2rust-analyzer cargo exr 1.74.0 math/Vec2#<T::Outputrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Sub#Output#>; |
| 110 | fn subrust-analyzer cargo exr 1.74.0 math/impl#[`Vec2<T>`][`Sub<Self>`]sub().(self, other: Vec2rust-analyzer cargo exr 1.74.0 math/Vec2#<T>) -> Selfrust-analyzer cargo exr 1.74.0 math/impl#[`Vec2<T>`][`Sub<Self>`]::Outputrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Sub#Output# { |
| 111 | Vec2rust-analyzer cargo exr 1.74.0 math/Vec2#(self.0rust-analyzer cargo exr 1.74.0 math/Vec2#0. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Sub#sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Sub#sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Sub#sub().other.0rust-analyzer cargo exr 1.74.0 math/Vec2#0., self.1rust-analyzer cargo exr 1.74.0 math/Vec2#1. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Sub#sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Sub#sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Sub#sub().other.1rust-analyzer cargo exr 1.74.0 math/Vec2#1.) |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | impl<T: stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::opsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/::Divrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Div#<T>> stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::opsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/::Divrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Div#<Vec2rust-analyzer cargo exr 1.74.0 math/Vec2#<T>> for Vec2rust-analyzer cargo exr 1.74.0 math/Vec2#<T> { |
| 116 | type Outputrust-analyzer cargo exr 1.74.0 math/impl#[`Vec2<T>`][`Div<Self>`]Output# = Vec2rust-analyzer cargo exr 1.74.0 math/Vec2#<T::Outputrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Div#Output#>; |
| 117 | fn divrust-analyzer cargo exr 1.74.0 math/impl#[`Vec2<T>`][`Div<Self>`]div().(self, other: Vec2rust-analyzer cargo exr 1.74.0 math/Vec2#<T>) -> Selfrust-analyzer cargo exr 1.74.0 math/impl#[`Vec2<T>`][`Div<Self>`]::Outputrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Div#Output# { |
| 118 | Vec2rust-analyzer cargo exr 1.74.0 math/Vec2#(self.0rust-analyzer cargo exr 1.74.0 math/Vec2#0. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Div#div()./rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Div#div(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Div#div().other.0rust-analyzer cargo exr 1.74.0 math/Vec2#0., self.1rust-analyzer cargo exr 1.74.0 math/Vec2#1. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Div#div()./rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Div#div(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Div#div().other.1rust-analyzer cargo exr 1.74.0 math/Vec2#1.) |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | impl<T: stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::opsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/::Mulrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Mul#<T>> stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::opsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/::Mulrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Mul#<Vec2rust-analyzer cargo exr 1.74.0 math/Vec2#<T>> for Vec2rust-analyzer cargo exr 1.74.0 math/Vec2#<T> { |
| 123 | type Outputrust-analyzer cargo exr 1.74.0 math/impl#[`Vec2<T>`][`Mul<Self>`]Output# = Vec2rust-analyzer cargo exr 1.74.0 math/Vec2#<T::Outputrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Mul#Output#>; |
| 124 | fn mulrust-analyzer cargo exr 1.74.0 math/impl#[`Vec2<T>`][`Mul<Self>`]mul().(self, other: Vec2rust-analyzer cargo exr 1.74.0 math/Vec2#<T>) -> Selfrust-analyzer cargo exr 1.74.0 math/impl#[`Vec2<T>`][`Mul<Self>`]::Outputrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Mul#Output# { |
| 125 | Vec2rust-analyzer cargo exr 1.74.0 math/Vec2#(self.0rust-analyzer cargo exr 1.74.0 math/Vec2#0. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Mul#mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Mul#mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Mul#mul().other.0rust-analyzer cargo exr 1.74.0 math/Vec2#0., self.1rust-analyzer cargo exr 1.74.0 math/Vec2#1. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Mul#mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Mul#mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Mul#mul().other.1rust-analyzer cargo exr 1.74.0 math/Vec2#1.) |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | impl<T> stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::opsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/::Negrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Neg# for Vec2rust-analyzer cargo exr 1.74.0 math/Vec2#<T> where T: stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::opsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/::Negrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Neg#<Outputrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Neg#Output#=T> { |
| 130 | type Outputrust-analyzer cargo exr 1.74.0 math/impl#[`Vec2<T>`][Neg]Output# = Vec2rust-analyzer cargo exr 1.74.0 math/Vec2#<T>; |
| 131 | fn negrust-analyzer cargo exr 1.74.0 math/impl#[`Vec2<T>`][Neg]neg().(self) -> Selfrust-analyzer cargo exr 1.74.0 math/impl#[`Vec2<T>`][Neg]::Outputrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Neg#Output# { Vec2rust-analyzer cargo exr 1.74.0 math/Vec2#(-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Neg#neg().self.0rust-analyzer cargo exr 1.74.0 math/Vec2#0., -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Neg#neg().self.1rust-analyzer cargo exr 1.74.0 math/Vec2#1.) } |
| 132 | } |
| 133 | |
| 134 | impl<T> Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<(T, T)> for Vec2rust-analyzer cargo exr 1.74.0 math/Vec2#<T> { |
| 135 | fn fromrust-analyzer cargo exr 1.74.0 math/impl#[`Vec2<T>`][`From<(T, T)>`]from().((x, y): (T, T)) -> Selfrust-analyzer cargo exr 1.74.0 math/impl#[`Vec2<T>`][`From<(T, T)>`] { Vec2rust-analyzer cargo exr 1.74.0 math/Vec2#(x, y) } |
| 136 | } |
| 137 | |
| 138 | impl<T> Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<Vec2rust-analyzer cargo exr 1.74.0 math/Vec2#<T>> for (T, T) { |
| 139 | fn fromrust-analyzer cargo exr 1.74.0 math/impl#[`(T, T)`][`From<Vec2<T>>`]from().(vec2: Vec2rust-analyzer cargo exr 1.74.0 math/Vec2#<T>) -> Selfrust-analyzer cargo exr 1.74.0 math/impl#[`(T, T)`][`From<Vec2<T>>`] { (vec2.0rust-analyzer cargo exr 1.74.0 math/Vec2#0., vec2.1rust-analyzer cargo exr 1.74.0 math/Vec2#1.) } |
| 140 | } |
| 141 | |
| 142 | /// Computes `floor(log(x)/log(2))`. Returns 0 where argument is 0. |
| 143 | // TODO does rust std not provide this? |
| 144 | pub(craterust-analyzer cargo exr 1.74.0 crate/) fn floor_log_2rust-analyzer cargo exr 1.74.0 math/floor_log_2().(mut number: u32) -> u32 { |
| 145 | let mut log = 0; |
| 146 | |
| 147 | // TODO check if this unrolls properly? |
| 148 | while number rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialOrd<Self>`]gt().1 { |
| 149 | log rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`AddAssign<Self>`]add_assign().1; |
| 150 | number rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`ShrAssign<i32>`]shr_assign().>>=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`ShrAssign<i32>`]shr_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`ShrAssign<i32>`]shr_assign().1; |
| 151 | } |
| 152 | |
| 153 | log |
| 154 | } |
| 155 | |
| 156 | |
| 157 | /// Computes `ceil(log(x)/log(2))`. Returns 0 where argument is 0. |
| 158 | // taken from https://github.com/openexr/openexr/blob/master/OpenEXR/IlmImf/ImfTiledMisc.cpp |
| 159 | // TODO does rust std not provide this? |
| 160 | pub(craterust-analyzer cargo exr 1.74.0 crate/) fn ceil_log_2rust-analyzer cargo exr 1.74.0 math/ceil_log_2().(mut number: u32) -> u32 { |
| 161 | let mut log = 0; |
| 162 | let mut round_up = 0; |
| 163 | |
| 164 | // TODO check if this unrolls properly |
| 165 | while number rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialOrd<Self>`]gt().1 { |
| 166 | if number rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitAnd<Self>`]bitand().&rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitAnd<Self>`]bitand(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitAnd<Self>`]bitand().1 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]ne().0 { |
| 167 | round_up = 1; |
| 168 | } |
| 169 | |
| 170 | log rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`AddAssign<Self>`]add_assign().1; |
| 171 | number rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`ShrAssign<i32>`]shr_assign().>>=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`ShrAssign<i32>`]shr_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`ShrAssign<i32>`]shr_assign().1; |
| 172 | } |
| 173 | |
| 174 | log rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Add<Self>`]add().round_up |
| 175 | } |
| 176 | |
| 177 | |
| 178 | /// Round up or down in specific calculations. |
| 179 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/macros/Hash:)] |
| 180 | pub enum RoundingModerust-analyzer cargo exr 1.74.0 math/RoundingMode# { |
| 181 | |
| 182 | /// Round down. |
| 183 | Downrust-analyzer cargo exr 1.74.0 math/RoundingMode#Down#, |
| 184 | |
| 185 | /// Round up. |
| 186 | Uprust-analyzer cargo exr 1.74.0 math/RoundingMode#Up#, |
| 187 | } |
| 188 | |
| 189 | impl RoundingModerust-analyzer cargo exr 1.74.0 math/RoundingMode# { |
| 190 | pub(craterust-analyzer cargo exr 1.74.0 crate/) fn log2rust-analyzer cargo exr 1.74.0 math/impl#[RoundingMode]log2().(self, number: u32) -> u32 { |
| 191 | match self { |
| 192 | RoundingModerust-analyzer cargo exr 1.74.0 math/RoundingMode#::Downrust-analyzer cargo exr 1.74.0 math/RoundingMode#Down# => selfrust-analyzer cargo exr 1.74.0 math/::floor_log_2rust-analyzer cargo exr 1.74.0 math/floor_log_2().(number), |
| 193 | RoundingModerust-analyzer cargo exr 1.74.0 math/RoundingMode#::Uprust-analyzer cargo exr 1.74.0 math/RoundingMode#Up# => selfrust-analyzer cargo exr 1.74.0 math/::ceil_log_2rust-analyzer cargo exr 1.74.0 math/ceil_log_2().(number), |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | /// Only works for positive numbers. |
| 198 | pub(craterust-analyzer cargo exr 1.74.0 crate/) fn dividerust-analyzer cargo exr 1.74.0 math/impl#[RoundingMode]divide().<T>(self, dividend: T, divisor: T) -> T |
| 199 | where T: Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy# + Addrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Add#<Outputrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Add#Output# = T> + Subrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Sub#<Outputrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Sub#Output# = T> + Divrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Div#<Outputrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Div#Output# = T> + Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<u8> + stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::cmprust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/::PartialOrdrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd# |
| 200 | { |
| 201 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!( |
| 202 | dividend >= T::fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#from().(0) && divisor >= T::fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#from().(1), |
| 203 | "division with rounding up only works for positive numbers" |
| 204 | ); |
| 205 | |
| 206 | match self { |
| 207 | RoundingModerust-analyzer cargo exr 1.74.0 math/RoundingMode#::Uprust-analyzer cargo exr 1.74.0 math/RoundingMode#Up# => (dividend rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Add#add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Add#add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Add#add().divisor rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Sub#sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Sub#sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Sub#sub().T::fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#from().(1_u8)) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Div#div()./rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Div#div(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Div#div().divisor, // only works for positive numbers |
| 208 | RoundingModerust-analyzer cargo exr 1.74.0 math/RoundingMode#::Downrust-analyzer cargo exr 1.74.0 math/RoundingMode#Down# => dividend rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Div#div()./rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Div#div(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Div#div().divisor, |
| 209 | } |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | // TODO log2 tests |