| 1 |
{% import "macros.rs.tera" as macros %} |
| 2 |
{% import "coresimd.rs.tera" as coresimd %} |
| 3 |
{% import "neon.rs.tera" as neon %} |
| 4 |
{% import "sse2.rs.tera" as sse2 %} |
| 5 |
{% import "wasm32.rs.tera" as wasm32 %} |
| 6 |
|
| 7 |
// Generated from {{template_path}} template. Edit the template, not the generated file. |
| 8 |
|
| 9 |
{% if scalar_t == "f32" %} |
| 10 |
{% set vecn_t = "Vec" ~ dim %} |
| 11 |
{% if dim == 3 and is_align %} |
| 12 |
{% set self_t = "Mat3A" %} |
| 13 |
{% set col_t = "Vec3A" %} |
| 14 |
{% else %} |
| 15 |
{% set self_t = "Mat" ~ dim %} |
| 16 |
{% set col_t = vecn_t %} |
| 17 |
{% endif %} |
| 18 |
{% set quat_t = "Quat" %} |
| 19 |
{% set affine2_t = "Affine2" %} |
| 20 |
{% set affine3_t = "Affine3A" %} |
| 21 |
{% set vec2_t = "Vec2" %} |
| 22 |
{% set vec3_t = "Vec3" %} |
| 23 |
{% set vec4_t = "Vec4" %} |
| 24 |
{% set mat2_t = "Mat2" %} |
| 25 |
{% set mat3_t = "Mat3" %} |
| 26 |
{% set mat4_t = "Mat4" %} |
| 27 |
{% elif scalar_t == "f64" %} |
| 28 |
{% set vecn_t = "DVec" ~ dim %} |
| 29 |
{% set self_t = "DMat" ~ dim %} |
| 30 |
{% set col_t = vecn_t %} |
| 31 |
{% set quat_t = "DQuat" %} |
| 32 |
{% set affine2_t = "DAffine2" %} |
| 33 |
{% set affine3_t = "DAffine3" %} |
| 34 |
{% set vec2_t = "DVec2" %} |
| 35 |
{% set vec3_t = "DVec3" %} |
| 36 |
{% set vec4_t = "DVec4" %} |
| 37 |
{% set mat2_t = "DMat2" %} |
| 38 |
{% set mat3_t = "DMat3" %} |
| 39 |
{% set mat4_t = "DMat4" %} |
| 40 |
{% endif %} |
| 41 |
|
| 42 |
{% if self_t == "Mat2" %} |
| 43 |
{% if not is_scalar %} |
| 44 |
{% set is_simd = true %} |
| 45 |
{% if is_sse2 %} |
| 46 |
{% set simd_t = "__m128" %} |
| 47 |
{% elif is_wasm32 %} |
| 48 |
{% set simd_t = "v128" %} |
| 49 |
{% elif is_coresimd %} |
| 50 |
{% set simd_t = "f32x4" %} |
| 51 |
{% elif is_neon %} |
| 52 |
{% set simd_t = "float32x4_t" %} |
| 53 |
{% endif %} |
| 54 |
{% endif %} |
| 55 |
{% endif %} |
| 56 |
|
| 57 |
{% set size = dim * dim %} |
| 58 |
{% set nxn = dim ~ "x" ~ dim %} |
| 59 |
|
| 60 |
{% set components = ["x", "y", "z", "w"] | slice(end = dim) %} |
| 61 |
{% set axes = ["x_axis", "y_axis", "z_axis", "w_axis"] | slice(end = dim) %} |
| 62 |
{% set dimension_in_full = ["zero", "one", "two", "three", "four"] | nth(n = dim) %} |
| 63 |
|
| 64 |
use crate::{ |
| 65 |
{% if scalar_t == "f32" %} |
| 66 |
DMat{{ dim }}, |
| 67 |
{% elif scalar_t == "f64" %} |
| 68 |
Mat{{ dim }}, |
| 69 |
{% endif %} |
| 70 |
{% if dim == 2 %} |
| 71 |
{{ mat3_t }}, {{ vec2_t }}, |
| 72 |
{% if scalar_t == "f32" %} |
| 73 |
Mat3A, |
| 74 |
{% endif %} |
| 75 |
{% elif dim == 3 %} |
| 76 |
EulerRot, euler::{FromEuler, ToEuler}, |
| 77 |
{{ mat2_t }}, {{ mat4_t }}, {{ quat_t }}, {{ vec2_t }}, {{ col_t }}, |
| 78 |
{% if is_align %} |
| 79 |
{{ mat3_t }}, {{ vec3_t }}, |
| 80 |
{% elif scalar_t == "f32" %} |
| 81 |
Mat3A, Vec3A, |
| 82 |
{% endif %} |
| 83 |
{% elif dim == 4 %} |
| 84 |
EulerRot, euler::{FromEuler, ToEuler}, |
| 85 |
{{ mat3_t }}, {{ quat_t }}, {{ vec3_t }}, {{ col_t }}, |
| 86 |
{% if scalar_t == "f32" %} |
| 87 |
Mat3A, Vec3A, |
| 88 |
{% endif %} |
| 89 |
{% endif %} |
| 90 |
{% if self_t == "Mat4" %} |
| 91 |
{% if is_sse2 %} |
| 92 |
sse2::*, |
| 93 |
{% elif is_wasm32 %} |
| 94 |
wasm32::*, |
| 95 |
{% elif is_coresimd %} |
| 96 |
coresimd::*, |
| 97 |
{% elif is_neon %} |
| 98 |
neon::*, |
| 99 |
{% endif %} |
| 100 |
{% endif %} |
| 101 |
{{ scalar_t }}::math, |
| 102 |
swizzles::*, |
| 103 |
}; |
| 104 |
use core::fmt; |
| 105 |
use core::iter::{Product, Sum}; |
| 106 |
use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign}; |
| 107 |
|
| 108 |
{% if is_sse2 %} |
| 109 |
#[cfg(target_arch = "x86")] |
| 110 |
use core::arch::x86::*; |
| 111 |
#[cfg(target_arch = "x86_64")] |
| 112 |
use core::arch::x86_64::*; |
| 113 |
{% elif is_wasm32 %} |
| 114 |
use core::arch::wasm32::*; |
| 115 |
{% elif is_coresimd %} |
| 116 |
use core::simd::*; |
| 117 |
{% elif is_neon %} |
| 118 |
use core::arch::aarch64::*; |
| 119 |
{% endif %} |
| 120 |
|
| 121 |
{% if not is_coresimd %} |
| 122 |
#[cfg(feature = "zerocopy")] |
| 123 |
use zerocopy_derive::*; |
| 124 |
{% endif %} |
| 125 |
|
| 126 |
{% set is_sse2_or_neon = is_sse2 or is_neon %} |
| 127 |
{% if self_t == "Mat2" and is_sse2_or_neon %} |
| 128 |
#[repr(C)] |
| 129 |
union UnionCast { |
| 130 |
a: [f32; 4], |
| 131 |
v: {{ self_t }} |
| 132 |
} |
| 133 |
{% endif %} |
| 134 |
|
| 135 |
/// Creates a {{ nxn }} matrix from {{ dimension_in_full }} column vectors. |
| 136 |
#[inline(always)] |
| 137 |
#[must_use] |
| 138 |
pub const fn {{ self_t | lower }}( |
| 139 |
{% for axis in axes %} |
| 140 |
{{ axis }}: {{ col_t }}, |
| 141 |
{% endfor %} |
| 142 |
) -> {{ self_t }} { |
| 143 |
{{ self_t }}::from_cols({{ axes | join(sep=",") }}) |
| 144 |
} |
| 145 |
|
| 146 |
/// A {{ nxn }} column major matrix. |
| 147 |
{%- if dim == 3 %} |
| 148 |
/// |
| 149 |
/// This 3x3 matrix type features convenience methods for creating and using linear and |
| 150 |
/// affine transformations. If you are primarily dealing with 2D affine transformations the |
| 151 |
/// [`{{ affine2_t }}`](crate::{{ affine2_t }}) type is much faster and more space efficient than |
| 152 |
/// using a 3x3 matrix. |
| 153 |
/// |
| 154 |
/// Linear transformations including 3D rotation and scale can be created using methods |
| 155 |
/// such as [`Self::from_diagonal()`], [`Self::from_quat()`], [`Self::from_axis_angle()`], |
| 156 |
/// [`Self::from_rotation_x()`], [`Self::from_rotation_y()`], or |
| 157 |
/// [`Self::from_rotation_z()`]. |
| 158 |
/// |
| 159 |
/// The resulting matrices can be use to transform 3D vectors using regular vector |
| 160 |
/// multiplication. |
| 161 |
/// |
| 162 |
/// Affine transformations including 2D translation, rotation and scale can be created |
| 163 |
/// using methods such as [`Self::from_translation()`], [`Self::from_angle()`], |
| 164 |
/// [`Self::from_scale()`] and [`Self::from_scale_angle_translation()`]. |
| 165 |
/// |
| 166 |
/// The [`Self::transform_point2()`] and [`Self::transform_vector2()`] convenience methods |
| 167 |
/// are provided for performing affine transforms on 2D vectors and points. These multiply |
| 168 |
/// 2D inputs as 3D vectors with an implicit `z` value of `1` for points and `0` for |
| 169 |
/// vectors respectively. These methods assume that `Self` contains a valid affine |
| 170 |
/// transform. |
| 171 |
{%- elif dim == 4 %} |
| 172 |
/// |
| 173 |
/// This 4x4 matrix type features convenience methods for creating and using affine transforms and |
| 174 |
/// perspective projections. If you are primarily dealing with 3D affine transformations |
| 175 |
/// considering using [`{{affine3_t}}`](crate::{{ affine3_t }}) which is faster than a 4x4 matrix |
| 176 |
/// for some affine operations. |
| 177 |
/// |
| 178 |
/// Affine transformations including 3D translation, rotation and scale can be created |
| 179 |
/// using methods such as [`Self::from_translation()`], [`Self::from_quat()`], |
| 180 |
/// [`Self::from_scale()`] and [`Self::from_scale_rotation_translation()`]. |
| 181 |
/// |
| 182 |
/// Orthographic projections can be created using the methods [`Self::orthographic_lh()`] for |
| 183 |
/// left-handed coordinate systems and [`Self::orthographic_rh()`] for right-handed |
| 184 |
/// systems. The resulting matrix is also an affine transformation. |
| 185 |
/// |
| 186 |
/// The [`Self::transform_point3()`] and [`Self::transform_vector3()`] convenience methods |
| 187 |
/// are provided for performing affine transformations on 3D vectors and points. These |
| 188 |
/// multiply 3D inputs as 4D vectors with an implicit `w` value of `1` for points and `0` |
| 189 |
/// for vectors respectively. These methods assume that `Self` contains a valid affine |
| 190 |
/// transform. |
| 191 |
/// |
| 192 |
/// Perspective projections can be created using methods such as |
| 193 |
/// [`Self::perspective_lh()`], [`Self::perspective_infinite_lh()`] and |
| 194 |
/// [`Self::perspective_infinite_reverse_lh()`] for left-handed co-ordinate systems and |
| 195 |
/// [`Self::perspective_rh()`], [`Self::perspective_infinite_rh()`] and |
| 196 |
/// [`Self::perspective_infinite_reverse_rh()`] for right-handed co-ordinate systems. |
| 197 |
/// |
| 198 |
/// The resulting perspective project can be use to transform 3D vectors as points with |
| 199 |
/// perspective correction using the [`Self::project_point3()`] convenience method. |
| 200 |
{%- endif %} |
| 201 |
{%- if is_simd %} |
| 202 |
/// |
| 203 |
/// SIMD vector types are used for storage on supported platforms. |
| 204 |
/// |
| 205 |
/// This type is 16 byte aligned. |
| 206 |
{%- endif %} |
| 207 |
#[derive(Clone, Copy)] |
| 208 |
{%- if self_t == "Mat3A" and is_scalar %} |
| 209 |
#[cfg_attr(feature = "bytemuck", derive(bytemuck::AnyBitPattern))] |
| 210 |
{%- if not is_coresimd %} |
| 211 |
#[cfg_attr(feature = "zerocopy", derive(FromBytes, Immutable, KnownLayout))] |
| 212 |
{%- endif %} |
| 213 |
{%- else %} |
| 214 |
#[cfg_attr(feature = "bytemuck", derive(bytemuck::Pod, bytemuck::Zeroable))] |
| 215 |
{%- if not is_coresimd %} |
| 216 |
#[cfg_attr(feature = "zerocopy", derive(FromBytes, Immutable, IntoBytes, KnownLayout))] |
| 217 |
{%- endif %} |
| 218 |
{%- endif %} |
| 219 |
{%- if self_t == "Mat4" and is_scalar %} |
| 220 |
#[cfg_attr(any(not(feature = "scalar-math"), feature = "cuda"), repr(align(16)))] |
| 221 |
{%- elif self_t == "Mat2" and is_scalar %} |
| 222 |
#[cfg_attr(not(feature = "scalar-math"), repr(align(16)))] |
| 223 |
#[cfg_attr(feature = "cuda", repr(align(8)))] |
| 224 |
{%- elif self_t == "DMat2" or self_t == "DMat4" %} |
| 225 |
#[cfg_attr(feature = "cuda", repr(align(16)))] |
| 226 |
{%- endif %} |
| 227 |
{%- if self_t == "Mat2" and not is_scalar %} |
| 228 |
#[repr(transparent)] |
| 229 |
pub struct {{ self_t }}(pub(crate) {{ simd_t }}); |
| 230 |
{%- else %} |
| 231 |
#[repr(C)] |
| 232 |
pub struct {{ self_t }} |
| 233 |
{ |
| 234 |
{% for axis in axes %} |
| 235 |
pub {{ axis }}: {{ col_t }}, |
| 236 |
{%- endfor %} |
| 237 |
} |
| 238 |
{% endif %} |
| 239 |
|
| 240 |
impl {{ self_t }} { |
| 241 |
/// A {{ nxn }} matrix with all elements set to `0.0`. |
| 242 |
pub const ZERO: Self = Self::from_cols( |
| 243 |
{% for axis in axes %} |
| 244 |
{{ col_t }}::ZERO, |
| 245 |
{%- endfor %} |
| 246 |
); |
| 247 |
|
| 248 |
/// A {{ nxn }} identity matrix, where all diagonal elements are `1`, and all off-diagonal elements are `0`. |
| 249 |
pub const IDENTITY: Self = Self::from_cols( |
| 250 |
{% for i in range(end = dim) %} |
| 251 |
{{ col_t }}::{{ components[i] | upper }}, |
| 252 |
{%- endfor %} |
| 253 |
); |
| 254 |
|
| 255 |
/// All NAN:s. |
| 256 |
pub const NAN: Self = Self::from_cols( |
| 257 |
{% for axis in axes %} |
| 258 |
{{ col_t }}::NAN, |
| 259 |
{%- endfor %} |
| 260 |
); |
| 261 |
|
| 262 |
#[allow(clippy::too_many_arguments)] |
| 263 |
#[inline(always)] |
| 264 |
#[must_use] |
| 265 |
const fn new( |
| 266 |
{% for i in range(end = dim) %} |
| 267 |
{%- for j in range(end = dim) %} |
| 268 |
m{{ i }}{{ j }}: {{ scalar_t }}, |
| 269 |
{%- endfor %} |
| 270 |
{%- endfor %} |
| 271 |
) -> Self { |
| 272 |
{% if self_t == "Mat2" and is_sse2 %} |
| 273 |
unsafe { UnionCast { a: [m00, m01, m10, m11] }.v } |
| 274 |
{% elif self_t == "Mat2" and is_wasm32 %} |
| 275 |
Self(f32x4(m00, m01, m10, m11)) |
| 276 |
{% elif self_t == "Mat2" and is_coresimd %} |
| 277 |
Self(f32x4::from_array([m00, m01, m10, m11])) |
| 278 |
{% elif self_t == "Mat2" and is_neon %} |
| 279 |
unsafe { UnionCast { a: [m00, m01, m10, m11] }.v } |
| 280 |
{% else %} |
| 281 |
Self { |
| 282 |
{% for i in range(end = dim) %} |
| 283 |
{{ axes[i] }}: {{ col_t}}::new( |
| 284 |
{% for j in range(end = dim) %} |
| 285 |
m{{ i }}{{ j }}, |
| 286 |
{% endfor %} |
| 287 |
), |
| 288 |
{%- endfor %} |
| 289 |
} |
| 290 |
{% endif %} |
| 291 |
} |
| 292 |
|
| 293 |
/// Creates a {{ nxn }} matrix from {{ dimension_in_full }} column vectors. |
| 294 |
#[inline(always)] |
| 295 |
#[must_use] |
| 296 |
pub const fn from_cols( |
| 297 |
{% for axis in axes %} |
| 298 |
{{ axis }}: {{ col_t }}, |
| 299 |
{% endfor %} |
| 300 |
) -> Self { |
| 301 |
{% if self_t == "Mat2" and is_sse2 %} |
| 302 |
unsafe { UnionCast { a: [x_axis.x, x_axis.y, y_axis.x, y_axis.y] }.v } |
| 303 |
{% elif self_t == "Mat2" and is_wasm32 %} |
| 304 |
Self(f32x4(x_axis.x, x_axis.y, y_axis.x, y_axis.y)) |
| 305 |
{% elif self_t == "Mat2" and is_coresimd %} |
| 306 |
Self(f32x4::from_array([x_axis.x, x_axis.y, y_axis.x, y_axis.y])) |
| 307 |
{% elif self_t == "Mat2" and is_neon %} |
| 308 |
unsafe { UnionCast { a: [x_axis.x, x_axis.y, y_axis.x, y_axis.y] }.v } |
| 309 |
{% else %} |
| 310 |
Self { |
| 311 |
{% for axis in axes %} |
| 312 |
{{ axis }}, |
| 313 |
{%- endfor %} |
| 314 |
} |
| 315 |
{% endif %} |
| 316 |
} |
| 317 |
|
| 318 |
/// Creates a {{ nxn }} matrix from a `[{{ scalar_t }}; {{ size }}]` array stored in column major order. |
| 319 |
/// If your data is stored in row major you will need to `transpose` the returned |
| 320 |
/// matrix. |
| 321 |
#[inline] |
| 322 |
#[must_use] |
| 323 |
pub const fn from_cols_array(m: &[{{ scalar_t }}; {{ size }}]) -> Self { |
| 324 |
{% if self_t == "Mat2" and is_coresimd %} |
| 325 |
Self(f32x4::from_array(*m)) |
| 326 |
{% else %} |
| 327 |
Self::new( |
| 328 |
{% for i in range(end = size) %} |
| 329 |
m[{{ i }}], |
| 330 |
{%- endfor %} |
| 331 |
) |
| 332 |
{% endif %} |
| 333 |
} |
| 334 |
|
| 335 |
/// Creates a `[{{ scalar_t }}; {{ size }}]` array storing data in column major order. |
| 336 |
/// If you require data in row major order `transpose` the matrix first. |
| 337 |
#[inline] |
| 338 |
#[must_use] |
| 339 |
pub const fn to_cols_array(&self) -> [{{ scalar_t }}; {{ size }}] { |
| 340 |
{% if self_t == "Mat2" and not is_scalar %} |
| 341 |
unsafe { *(self as *const Self as *const [f32; 4]) } |
| 342 |
{% else %} |
| 343 |
{% if dim >= 2 and not is_scalar %} |
| 344 |
{% for axis in axes %} |
| 345 |
let [{% for c in components %} {{ axis }}_{{ c }}, {% endfor %}] = self.{{ axis }}.to_array(); |
| 346 |
{%- endfor %} |
| 347 |
{% endif %} |
| 348 |
|
| 349 |
[ |
| 350 |
{% for axis in axes %} |
| 351 |
{% for c in components %} |
| 352 |
{% if dim >= 2 and not is_scalar %} |
| 353 |
{{ axis }}_{{ c }}, |
| 354 |
{% else %} |
| 355 |
self.{{ axis }}.{{ c }}, |
| 356 |
{% endif %} |
| 357 |
{%- endfor %} |
| 358 |
{%- endfor %} |
| 359 |
] |
| 360 |
{% endif %} |
| 361 |
} |
| 362 |
|
| 363 |
/// Creates a {{ nxn }} matrix from a `[[{{ scalar_t }}; {{ dim }}]; {{ dim }}]` {{ dim }}D array stored in column major order. |
| 364 |
/// If your data is in row major order you will need to `transpose` the returned |
| 365 |
/// matrix. |
| 366 |
#[inline] |
| 367 |
#[must_use] |
| 368 |
pub const fn from_cols_array_2d(m: &[[{{ scalar_t }}; {{ dim }}]; {{ dim }}]) -> Self { |
| 369 |
Self::from_cols( |
| 370 |
{% for i in range(end = dim) %} |
| 371 |
{{ col_t }}::from_array(m[{{ i }}]), |
| 372 |
{%- endfor %} |
| 373 |
) |
| 374 |
} |
| 375 |
|
| 376 |
/// Creates a `[[{{ scalar_t }}; {{ dim }}]; {{ dim }}]` {{ dim }}D array storing data in column major order. |
| 377 |
/// If you require data in row major order `transpose` the matrix first. |
| 378 |
#[inline] |
| 379 |
#[must_use] |
| 380 |
pub const fn to_cols_array_2d(&self) -> [[{{ scalar_t }}; {{ dim }}]; {{ dim }}] { |
| 381 |
{% if self_t == "Mat2" and not is_scalar %} |
| 382 |
unsafe { *(self as *const Self as *const [[f32; 2]; 2]) } |
| 383 |
{% else %} |
| 384 |
[ |
| 385 |
{% for axis in axes %} |
| 386 |
self.{{ axis }}.to_array(), |
| 387 |
{%- endfor %} |
| 388 |
] |
| 389 |
{% endif %} |
| 390 |
} |
| 391 |
|
| 392 |
/// Creates a {{ nxn }} matrix with its diagonal set to `diagonal` and all other entries set to 0. |
| 393 |
#[doc(alias = "scale")] |
| 394 |
#[inline] |
| 395 |
#[must_use] |
| 396 |
pub const fn from_diagonal(diagonal: {{ vecn_t }}) -> Self { |
| 397 |
{% if self_t == "Mat4" and not is_scalar %} |
| 398 |
// diagonal.x, diagonal.y etc can't be done in a const-context |
| 399 |
let [x, y, z, w] = diagonal.to_array(); |
| 400 |
Self::new( |
| 401 |
{% for i in range(end = dim) %} |
| 402 |
{% for j in range(end = dim) %} |
| 403 |
{% if i == j %} |
| 404 |
{{ components[i] }}, |
| 405 |
{% else %} |
| 406 |
0.0, |
| 407 |
{% endif %} |
| 408 |
{%- endfor %} |
| 409 |
{%- endfor %} |
| 410 |
) |
| 411 |
{% else %} |
| 412 |
Self::new( |
| 413 |
{% for i in range(end = dim) %} |
| 414 |
{% for j in range(end = dim) %} |
| 415 |
{% if i == j %} |
| 416 |
diagonal.{{ components[i] }}, |
| 417 |
{% else %} |
| 418 |
0.0, |
| 419 |
{% endif %} |
| 420 |
{%- endfor %} |
| 421 |
{%- endfor %} |
| 422 |
) |
| 423 |
{% endif %} |
| 424 |
} |
| 425 |
|
| 426 |
{% if dim == 2 %} |
| 427 |
/// Creates a {{ nxn }} matrix containing the combining non-uniform `scale` and rotation of |
| 428 |
/// `angle` (in radians). |
| 429 |
#[inline] |
| 430 |
#[must_use] |
| 431 |
pub fn from_scale_angle(scale: {{ col_t }}, angle: {{ scalar_t }}) -> Self { |
| 432 |
let (sin, cos) = math::sin_cos(angle); |
| 433 |
Self::new(cos * scale.x, sin * scale.x, -sin * scale.y, cos * scale.y) |
| 434 |
} |
| 435 |
|
| 436 |
/// Creates a {{ nxn }} matrix containing a rotation of `angle` (in radians). |
| 437 |
#[inline] |
| 438 |
#[must_use] |
| 439 |
pub fn from_angle(angle: {{ scalar_t }}) -> Self { |
| 440 |
let (sin, cos) = math::sin_cos(angle); |
| 441 |
Self::new(cos, sin, -sin, cos) |
| 442 |
} |
| 443 |
|
| 444 |
/// Creates a {{ nxn }} matrix from a 3x3 matrix, discarding the 2nd row and column. |
| 445 |
#[inline] |
| 446 |
#[must_use] |
| 447 |
pub fn from_mat3(m: {{ mat3_t }}) -> Self { |
| 448 |
Self::from_cols(m.x_axis.xy(), m.y_axis.xy()) |
| 449 |
} |
| 450 |
|
| 451 |
{{ macros::impl_mat3_minor(mat3_t=mat3_t) }} |
| 452 |
|
| 453 |
{% if scalar_t == "f32" %} |
| 454 |
/// Creates a {{ nxn }} matrix from a 3x3 matrix, discarding the 2nd row and column. |
| 455 |
#[inline] |
| 456 |
#[must_use] |
| 457 |
pub fn from_mat3a(m: Mat3A) -> Self { |
| 458 |
{# TODO: SIMD optimise #} |
| 459 |
Self::from_cols(m.x_axis.xy(), m.y_axis.xy()) |
| 460 |
} |
| 461 |
|
| 462 |
{{ macros::impl_mat3_minor(mat3_t="Mat3A", align="a") }} |
| 463 |
{% endif %} |
| 464 |
|
| 465 |
{% elif dim == 3 %} |
| 466 |
/// Creates a 3x3 matrix from a 4x4 matrix, discarding the 4th row and column. |
| 467 |
#[inline] |
| 468 |
#[must_use] |
| 469 |
pub fn from_mat4(m: {{ mat4_t }}) -> Self { |
| 470 |
Self::from_cols( |
| 471 |
{{ col_t }}::from_vec4(m.x_axis), |
| 472 |
{{ col_t }}::from_vec4(m.y_axis), |
| 473 |
{{ col_t }}::from_vec4(m.z_axis), |
| 474 |
) |
| 475 |
} |
| 476 |
|
| 477 |
{{ macros::impl_mat4_minor(mat4_t=mat4_t, vec3_t=col_t) }} |
| 478 |
|
| 479 |
/// Creates a 3D rotation matrix from the given quaternion. |
| 480 |
/// |
| 481 |
/// # Panics |
| 482 |
/// |
| 483 |
/// Will panic if `rotation` is not normalized when `glam_assert` is enabled. |
| 484 |
#[inline] |
| 485 |
#[must_use] |
| 486 |
pub fn from_quat(rotation: {{ quat_t }}) -> Self { |
| 487 |
glam_assert!(rotation.is_normalized()); |
| 488 |
|
| 489 |
let x2 = rotation.x + rotation.x; |
| 490 |
let y2 = rotation.y + rotation.y; |
| 491 |
let z2 = rotation.z + rotation.z; |
| 492 |
let xx = rotation.x * x2; |
| 493 |
let xy = rotation.x * y2; |
| 494 |
let xz = rotation.x * z2; |
| 495 |
let yy = rotation.y * y2; |
| 496 |
let yz = rotation.y * z2; |
| 497 |
let zz = rotation.z * z2; |
| 498 |
let wx = rotation.w * x2; |
| 499 |
let wy = rotation.w * y2; |
| 500 |
let wz = rotation.w * z2; |
| 501 |
|
| 502 |
Self::from_cols( |
| 503 |
{{ col_t }}::new(1.0 - (yy + zz), xy + wz, xz - wy), |
| 504 |
{{ col_t }}::new(xy - wz, 1.0 - (xx + zz), yz + wx), |
| 505 |
{{ col_t }}::new(xz + wy, yz - wx, 1.0 - (xx + yy)), |
| 506 |
) |
| 507 |
} |
| 508 |
|
| 509 |
/// Creates a 3D rotation matrix from a normalized rotation `axis` and `angle` (in |
| 510 |
/// radians). |
| 511 |
/// |
| 512 |
/// # Panics |
| 513 |
/// |
| 514 |
/// Will panic if `axis` is not normalized when `glam_assert` is enabled. |
| 515 |
#[inline] |
| 516 |
#[must_use] |
| 517 |
pub fn from_axis_angle(axis: {{ vec3_t }}, angle: {{ scalar_t }}) -> Self { |
| 518 |
{# TODO: make common with dim == 4 #} |
| 519 |
glam_assert!(axis.is_normalized()); |
| 520 |
|
| 521 |
let (sin, cos) = math::sin_cos(angle); |
| 522 |
let (xsin, ysin, zsin) = axis.mul(sin).into(); |
| 523 |
let (x, y, z) = axis.into(); |
| 524 |
let (x2, y2, z2) = axis.mul(axis).into(); |
| 525 |
let omc = 1.0 - cos; |
| 526 |
let xyomc = x * y * omc; |
| 527 |
let xzomc = x * z * omc; |
| 528 |
let yzomc = y * z * omc; |
| 529 |
Self::from_cols( |
| 530 |
{{ col_t }}::new(x2 * omc + cos, xyomc + zsin, xzomc - ysin), |
| 531 |
{{ col_t }}::new(xyomc - zsin, y2 * omc + cos, yzomc + xsin), |
| 532 |
{{ col_t }}::new(xzomc + ysin, yzomc - xsin, z2 * omc + cos), |
| 533 |
) |
| 534 |
} |
| 535 |
|
| 536 |
/// Creates a 3D rotation matrix from the given euler rotation sequence and the angles (in |
| 537 |
/// radians). |
| 538 |
#[inline] |
| 539 |
#[must_use] |
| 540 |
pub fn from_euler(order: EulerRot, a: {{ scalar_t }}, b: {{ scalar_t }}, c: {{ scalar_t }}) -> Self { |
| 541 |
Self::from_euler_angles(order, a, b, c) |
| 542 |
} |
| 543 |
|
| 544 |
/// Extract Euler angles with the given Euler rotation order. |
| 545 |
/// |
| 546 |
/// Note if the input matrix contains scales, shears, or other non-rotation transformations then |
| 547 |
/// the resulting Euler angles will be ill-defined. |
| 548 |
/// |
| 549 |
/// # Panics |
| 550 |
/// |
| 551 |
/// Will panic if any input matrix column is not normalized when `glam_assert` is enabled. |
| 552 |
#[inline] |
| 553 |
#[must_use] |
| 554 |
pub fn to_euler(&self, order: EulerRot) -> ({{ scalar_t }}, {{ scalar_t }}, {{ scalar_t }}) { |
| 555 |
glam_assert!(self.x_axis.is_normalized() |
| 556 |
&& self.y_axis.is_normalized() |
| 557 |
&& self.z_axis.is_normalized()); |
| 558 |
self.to_euler_angles(order) |
| 559 |
} |
| 560 |
|
| 561 |
/// Creates a 3D rotation matrix from `angle` (in radians) around the x axis. |
| 562 |
#[inline] |
| 563 |
#[must_use] |
| 564 |
pub fn from_rotation_x(angle: {{ scalar_t }}) -> Self { |
| 565 |
let (sina, cosa) = math::sin_cos(angle); |
| 566 |
Self::from_cols( |
| 567 |
{{ col_t }}::X, |
| 568 |
{{ col_t }}::new(0.0, cosa, sina), |
| 569 |
{{ col_t }}::new(0.0, -sina, cosa), |
| 570 |
) |
| 571 |
} |
| 572 |
|
| 573 |
/// Creates a 3D rotation matrix from `angle` (in radians) around the y axis. |
| 574 |
#[inline] |
| 575 |
#[must_use] |
| 576 |
pub fn from_rotation_y(angle: {{ scalar_t }}) -> Self { |
| 577 |
let (sina, cosa) = math::sin_cos(angle); |
| 578 |
Self::from_cols( |
| 579 |
{{ col_t }}::new(cosa, 0.0, -sina), |
| 580 |
{{ col_t }}::Y, |
| 581 |
{{ col_t }}::new(sina, 0.0, cosa), |
| 582 |
) |
| 583 |
} |
| 584 |
|
| 585 |
/// Creates a 3D rotation matrix from `angle` (in radians) around the z axis. |
| 586 |
#[inline] |
| 587 |
#[must_use] |
| 588 |
pub fn from_rotation_z(angle: {{ scalar_t }}) -> Self { |
| 589 |
let (sina, cosa) = math::sin_cos(angle); |
| 590 |
Self::from_cols( |
| 591 |
{{ col_t }}::new(cosa, sina, 0.0), |
| 592 |
{{ col_t }}::new(-sina, cosa, 0.0), |
| 593 |
{{ col_t }}::Z, |
| 594 |
) |
| 595 |
} |
| 596 |
|
| 597 |
/// Creates an affine transformation matrix from the given 2D `translation`. |
| 598 |
/// |
| 599 |
/// The resulting matrix can be used to transform 2D points and vectors. See |
| 600 |
/// [`Self::transform_point2()`] and [`Self::transform_vector2()`]. |
| 601 |
#[inline] |
| 602 |
#[must_use] |
| 603 |
pub fn from_translation(translation: {{ vec2_t }}) -> Self { |
| 604 |
Self::from_cols( |
| 605 |
{{ col_t }}::X, |
| 606 |
{{ col_t }}::Y, |
| 607 |
{{ col_t }}::new(translation.x, translation.y, 1.0)) |
| 608 |
} |
| 609 |
|
| 610 |
/// Creates an affine transformation matrix from the given 2D rotation `angle` (in |
| 611 |
/// radians). |
| 612 |
/// |
| 613 |
/// The resulting matrix can be used to transform 2D points and vectors. See |
| 614 |
/// [`Self::transform_point2()`] and [`Self::transform_vector2()`]. |
| 615 |
#[inline] |
| 616 |
#[must_use] |
| 617 |
pub fn from_angle(angle: {{ scalar_t }}) -> Self { |
| 618 |
let (sin, cos) = math::sin_cos(angle); |
| 619 |
Self::from_cols( |
| 620 |
{{ col_t }}::new(cos, sin, 0.0), |
| 621 |
{{ col_t }}::new(-sin, cos, 0.0), |
| 622 |
{{ col_t }}::Z, |
| 623 |
) |
| 624 |
} |
| 625 |
|
| 626 |
/// Creates an affine transformation matrix from the given 2D `scale`, rotation `angle` (in |
| 627 |
/// radians) and `translation`. |
| 628 |
/// |
| 629 |
/// The resulting matrix can be used to transform 2D points and vectors. See |
| 630 |
/// [`Self::transform_point2()`] and [`Self::transform_vector2()`]. |
| 631 |
#[inline] |
| 632 |
#[must_use] |
| 633 |
pub fn from_scale_angle_translation(scale: {{ vec2_t }}, angle: {{ scalar_t }}, translation: {{ vec2_t }}) -> Self { |
| 634 |
let (sin, cos) = math::sin_cos(angle); |
| 635 |
Self::from_cols( |
| 636 |
{{ col_t }}::new(cos * scale.x, sin * scale.x, 0.0), |
| 637 |
{{ col_t }}::new(-sin * scale.y, cos * scale.y, 0.0), |
| 638 |
{{ col_t }}::new(translation.x, translation.y, 1.0), |
| 639 |
) |
| 640 |
} |
| 641 |
|
| 642 |
/// Creates an affine transformation matrix from the given non-uniform 2D `scale`. |
| 643 |
/// |
| 644 |
/// The resulting matrix can be used to transform 2D points and vectors. See |
| 645 |
/// [`Self::transform_point2()`] and [`Self::transform_vector2()`]. |
| 646 |
/// |
| 647 |
/// # Panics |
| 648 |
/// |
| 649 |
/// Will panic if all elements of `scale` are zero when `glam_assert` is enabled. |
| 650 |
#[inline] |
| 651 |
#[must_use] |
| 652 |
pub fn from_scale(scale: {{ vec2_t }}) -> Self { |
| 653 |
// Do not panic as long as any component is non-zero |
| 654 |
glam_assert!(scale.cmpne({{ vec2_t }}::ZERO).any()); |
| 655 |
|
| 656 |
Self::from_cols( |
| 657 |
{{ col_t }}::new(scale.x, 0.0, 0.0), |
| 658 |
{{ col_t }}::new(0.0, scale.y, 0.0), |
| 659 |
{{ col_t }}::Z, |
| 660 |
) |
| 661 |
} |
| 662 |
|
| 663 |
/// Creates an affine transformation matrix from the given 2x2 matrix. |
| 664 |
/// |
| 665 |
/// The resulting matrix can be used to transform 2D points and vectors. See |
| 666 |
/// [`Self::transform_point2()`] and [`Self::transform_vector2()`]. |
| 667 |
#[inline] |
| 668 |
pub fn from_mat2(m: {{ mat2_t }}) -> Self { |
| 669 |
Self::from_cols((m.x_axis, 0.0).into(), (m.y_axis, 0.0).into(), {{ col_t }}::Z) |
| 670 |
} |
| 671 |
|
| 672 |
{% elif dim == 4 %} |
| 673 |
#[inline] |
| 674 |
#[must_use] |
| 675 |
fn quat_to_axes(rotation: {{ quat_t }}) -> ({{ col_t }}, {{ col_t }}, {{ col_t }}) { |
| 676 |
glam_assert!(rotation.is_normalized()); |
| 677 |
|
| 678 |
let (x, y, z, w) = rotation.into(); |
| 679 |
let x2 = x + x; |
| 680 |
let y2 = y + y; |
| 681 |
let z2 = z + z; |
| 682 |
let xx = x * x2; |
| 683 |
let xy = x * y2; |
| 684 |
let xz = x * z2; |
| 685 |
let yy = y * y2; |
| 686 |
let yz = y * z2; |
| 687 |
let zz = z * z2; |
| 688 |
let wx = w * x2; |
| 689 |
let wy = w * y2; |
| 690 |
let wz = w * z2; |
| 691 |
|
| 692 |
let x_axis = {{ col_t }}::new(1.0 - (yy + zz), xy + wz, xz - wy, 0.0); |
| 693 |
let y_axis = {{ col_t }}::new(xy - wz, 1.0 - (xx + zz), yz + wx, 0.0); |
| 694 |
let z_axis = {{ col_t }}::new(xz + wy, yz - wx, 1.0 - (xx + yy), 0.0); |
| 695 |
(x_axis, y_axis, z_axis) |
| 696 |
} |
| 697 |
|
| 698 |
/// Creates an affine transformation matrix from the given 3D `scale`, `rotation` and |
| 699 |
/// `translation`. |
| 700 |
/// |
| 701 |
/// The resulting matrix can be used to transform 3D points and vectors. See |
| 702 |
/// [`Self::transform_point3()`] and [`Self::transform_vector3()`]. |
| 703 |
/// |
| 704 |
/// # Panics |
| 705 |
/// |
| 706 |
/// Will panic if `rotation` is not normalized when `glam_assert` is enabled. |
| 707 |
#[inline] |
| 708 |
#[must_use] |
| 709 |
pub fn from_scale_rotation_translation( |
| 710 |
scale: {{ vec3_t }}, |
| 711 |
rotation: {{ quat_t }}, |
| 712 |
translation: {{ vec3_t }}, |
| 713 |
) -> Self { |
| 714 |
let (x_axis, y_axis, z_axis) = Self::quat_to_axes(rotation); |
| 715 |
Self::from_cols( |
| 716 |
x_axis.mul(scale.x), |
| 717 |
y_axis.mul(scale.y), |
| 718 |
z_axis.mul(scale.z), |
| 719 |
{{ col_t }}::from((translation, 1.0)), |
| 720 |
) |
| 721 |
} |
| 722 |
|
| 723 |
/// Creates an affine transformation matrix from the given 3D `translation`. |
| 724 |
/// |
| 725 |
/// The resulting matrix can be used to transform 3D points and vectors. See |
| 726 |
/// [`Self::transform_point3()`] and [`Self::transform_vector3()`]. |
| 727 |
/// |
| 728 |
/// # Panics |
| 729 |
/// |
| 730 |
/// Will panic if `rotation` is not normalized when `glam_assert` is enabled. |
| 731 |
#[inline] |
| 732 |
#[must_use] |
| 733 |
pub fn from_rotation_translation(rotation: {{ quat_t }}, translation: {{ vec3_t }}) -> Self { |
| 734 |
let (x_axis, y_axis, z_axis) = Self::quat_to_axes(rotation); |
| 735 |
Self::from_cols(x_axis, y_axis, z_axis, {{ col_t }}::from((translation, 1.0))) |
| 736 |
} |
| 737 |
|
| 738 |
/// Extracts `scale`, `rotation` and `translation` from `self`. The input matrix is |
| 739 |
/// expected to be a 3D affine transformation matrix otherwise the output will be invalid. |
| 740 |
/// |
| 741 |
/// # Panics |
| 742 |
/// |
| 743 |
/// Will panic if the determinant of `self` is zero or if the resulting scale vector |
| 744 |
/// contains any zero elements when `glam_assert` is enabled. |
| 745 |
#[inline] |
| 746 |
#[must_use] |
| 747 |
pub fn to_scale_rotation_translation(&self) -> ({{ vec3_t }}, {{ quat_t }}, {{ vec3_t }}) { |
| 748 |
let det = self.determinant(); |
| 749 |
glam_assert!(det != 0.0); |
| 750 |
|
| 751 |
let scale = {{ vec3_t }}::new( |
| 752 |
self.x_axis.length() * math::signum(det), |
| 753 |
self.y_axis.length(), |
| 754 |
self.z_axis.length(), |
| 755 |
); |
| 756 |
|
| 757 |
glam_assert!(scale.cmpne({{ vec3_t }}::ZERO).all()); |
| 758 |
|
| 759 |
let inv_scale = scale.recip(); |
| 760 |
|
| 761 |
let rotation = {{ quat_t }}::from_rotation_axes( |
| 762 |
self.x_axis.mul(inv_scale.x).xyz(), |
| 763 |
self.y_axis.mul(inv_scale.y).xyz(), |
| 764 |
self.z_axis.mul(inv_scale.z).xyz(), |
| 765 |
); |
| 766 |
|
| 767 |
let translation = self.w_axis.xyz(); |
| 768 |
|
| 769 |
(scale, rotation, translation) |
| 770 |
} |
| 771 |
|
| 772 |
/// Creates an affine transformation matrix from the given `rotation` quaternion. |
| 773 |
/// |
| 774 |
/// The resulting matrix can be used to transform 3D points and vectors. See |
| 775 |
/// [`Self::transform_point3()`] and [`Self::transform_vector3()`]. |
| 776 |
/// |
| 777 |
/// # Panics |
| 778 |
/// |
| 779 |
/// Will panic if `rotation` is not normalized when `glam_assert` is enabled. |
| 780 |
#[inline] |
| 781 |
#[must_use] |
| 782 |
pub fn from_quat(rotation: {{ quat_t }}) -> Self { |
| 783 |
let (x_axis, y_axis, z_axis) = Self::quat_to_axes(rotation); |
| 784 |
Self::from_cols(x_axis, y_axis, z_axis, {{ col_t }}::W) |
| 785 |
} |
| 786 |
|
| 787 |
/// Creates an affine transformation matrix from the given 3x3 linear transformation |
| 788 |
/// matrix. |
| 789 |
/// |
| 790 |
/// The resulting matrix can be used to transform 3D points and vectors. See |
| 791 |
/// [`Self::transform_point3()`] and [`Self::transform_vector3()`]. |
| 792 |
#[inline] |
| 793 |
#[must_use] |
| 794 |
pub fn from_mat3(m: {{ mat3_t }}) -> Self { |
| 795 |
Self::from_cols( |
| 796 |
{{ col_t }}::from((m.x_axis, 0.0)), |
| 797 |
{{ col_t }}::from((m.y_axis, 0.0)), |
| 798 |
{{ col_t }}::from((m.z_axis, 0.0)), |
| 799 |
{{ col_t }}::W, |
| 800 |
) |
| 801 |
} |
| 802 |
|
| 803 |
/// Creates an affine transformation matrics from a 3x3 matrix (expressing scale, shear and |
| 804 |
/// rotation) and a translation vector. |
| 805 |
/// |
| 806 |
/// Equivalent to `{{ self_t }}::from_translation(translation) * {{ self_t }}::from_mat3(mat3)` |
| 807 |
#[inline] |
| 808 |
#[must_use] |
| 809 |
pub fn from_mat3_translation(mat3: {{ mat3_t }}, translation: {{ vec3_t }}) -> Self { |
| 810 |
Self::from_cols( |
| 811 |
{{ col_t }}::from((mat3.x_axis, 0.0)), |
| 812 |
{{ col_t }}::from((mat3.y_axis, 0.0)), |
| 813 |
{{ col_t }}::from((mat3.z_axis, 0.0)), |
| 814 |
{{ col_t }}::from((translation, 1.0)), |
| 815 |
) |
| 816 |
} |
| 817 |
|
| 818 |
{% if scalar_t == "f32" %} |
| 819 |
/// Creates an affine transformation matrix from the given 3x3 linear transformation |
| 820 |
/// matrix. |
| 821 |
/// |
| 822 |
/// The resulting matrix can be used to transform 3D points and vectors. See |
| 823 |
/// [`Self::transform_point3()`] and [`Self::transform_vector3()`]. |
| 824 |
#[inline] |
| 825 |
#[must_use] |
| 826 |
pub fn from_mat3a(m: Mat3A) -> Self { |
| 827 |
Self::from_cols( |
| 828 |
Vec4::from((m.x_axis, 0.0)), |
| 829 |
Vec4::from((m.y_axis, 0.0)), |
| 830 |
Vec4::from((m.z_axis, 0.0)), |
| 831 |
Vec4::W, |
| 832 |
) |
| 833 |
} |
| 834 |
{% endif %} |
| 835 |
|
| 836 |
/// Creates an affine transformation matrix from the given 3D `translation`. |
| 837 |
/// |
| 838 |
/// The resulting matrix can be used to transform 3D points and vectors. See |
| 839 |
/// [`Self::transform_point3()`] and [`Self::transform_vector3()`]. |
| 840 |
#[inline] |
| 841 |
#[must_use] |
| 842 |
pub fn from_translation(translation: {{ vec3_t }}) -> Self { |
| 843 |
Self::from_cols( |
| 844 |
{{ col_t }}::X, |
| 845 |
{{ col_t }}::Y, |
| 846 |
{{ col_t }}::Z, |
| 847 |
{{ col_t }}::new(translation.x, translation.y, translation.z, 1.0), |
| 848 |
) |
| 849 |
} |
| 850 |
|
| 851 |
/// Creates an affine transformation matrix containing a 3D rotation around a normalized |
| 852 |
/// rotation `axis` of `angle` (in radians). |
| 853 |
/// |
| 854 |
/// The resulting matrix can be used to transform 3D points and vectors. See |
| 855 |
/// [`Self::transform_point3()`] and [`Self::transform_vector3()`]. |
| 856 |
/// |
| 857 |
/// # Panics |
| 858 |
/// |
| 859 |
/// Will panic if `axis` is not normalized when `glam_assert` is enabled. |
| 860 |
#[inline] |
| 861 |
#[must_use] |
| 862 |
pub fn from_axis_angle(axis: {{ vec3_t }}, angle: {{ scalar_t }}) -> Self { |
| 863 |
{# TODO: make common with dim == 3 #} |
| 864 |
glam_assert!(axis.is_normalized()); |
| 865 |
|
| 866 |
let (sin, cos) = math::sin_cos(angle); |
| 867 |
let axis_sin = axis.mul(sin); |
| 868 |
let axis_sq = axis.mul(axis); |
| 869 |
let omc = 1.0 - cos; |
| 870 |
let xyomc = axis.x * axis.y * omc; |
| 871 |
let xzomc = axis.x * axis.z * omc; |
| 872 |
let yzomc = axis.y * axis.z * omc; |
| 873 |
Self::from_cols( |
| 874 |
{{ col_t }}::new( |
| 875 |
axis_sq.x * omc + cos, |
| 876 |
xyomc + axis_sin.z, |
| 877 |
xzomc - axis_sin.y, |
| 878 |
0.0, |
| 879 |
), |
| 880 |
{{ col_t }}::new( |
| 881 |
xyomc - axis_sin.z, |
| 882 |
axis_sq.y * omc + cos, |
| 883 |
yzomc + axis_sin.x, |
| 884 |
0.0, |
| 885 |
), |
| 886 |
{{ col_t }}::new( |
| 887 |
xzomc + axis_sin.y, |
| 888 |
yzomc - axis_sin.x, |
| 889 |
axis_sq.z * omc + cos, |
| 890 |
0.0, |
| 891 |
), |
| 892 |
{{ col_t }}::W, |
| 893 |
) |
| 894 |
} |
| 895 |
|
| 896 |
/// Creates a affine transformation matrix containing a rotation from the given euler |
| 897 |
/// rotation sequence and angles (in radians). |
| 898 |
/// |
| 899 |
/// The resulting matrix can be used to transform 3D points and vectors. See |
| 900 |
/// [`Self::transform_point3()`] and [`Self::transform_vector3()`]. |
| 901 |
#[inline] |
| 902 |
#[must_use] |
| 903 |
pub fn from_euler(order: EulerRot, a: {{ scalar_t }}, b: {{ scalar_t }}, c: {{ scalar_t }}) -> Self { |
| 904 |
Self::from_euler_angles(order, a, b, c) |
| 905 |
} |
| 906 |
|
| 907 |
/// Extract Euler angles with the given Euler rotation order. |
| 908 |
/// |
| 909 |
/// Note if the upper 3x3 matrix contain scales, shears, or other non-rotation transformations |
| 910 |
/// then the resulting Euler angles will be ill-defined. |
| 911 |
/// |
| 912 |
/// # Panics |
| 913 |
/// |
| 914 |
/// Will panic if any column of the upper 3x3 rotation matrix is not normalized when |
| 915 |
/// `glam_assert` is enabled. |
| 916 |
#[inline] |
| 917 |
#[must_use] |
| 918 |
pub fn to_euler(&self, order: EulerRot) -> ({{ scalar_t }}, {{ scalar_t }}, {{ scalar_t }}) { |
| 919 |
glam_assert!(self.x_axis.xyz().is_normalized() |
| 920 |
&& self.y_axis.xyz().is_normalized() |
| 921 |
&& self.z_axis.xyz().is_normalized()); |
| 922 |
self.to_euler_angles(order) |
| 923 |
} |
| 924 |
|
| 925 |
/// Creates an affine transformation matrix containing a 3D rotation around the x axis of |
| 926 |
/// `angle` (in radians). |
| 927 |
/// |
| 928 |
/// The resulting matrix can be used to transform 3D points and vectors. See |
| 929 |
/// [`Self::transform_point3()`] and [`Self::transform_vector3()`]. |
| 930 |
#[inline] |
| 931 |
#[must_use] |
| 932 |
pub fn from_rotation_x(angle: {{ scalar_t }}) -> Self { |
| 933 |
let (sina, cosa) = math::sin_cos(angle); |
| 934 |
Self::from_cols( |
| 935 |
{{ col_t }}::X, |
| 936 |
{{ col_t }}::new(0.0, cosa, sina, 0.0), |
| 937 |
{{ col_t }}::new(0.0, -sina, cosa, 0.0), |
| 938 |
{{ col_t }}::W, |
| 939 |
) |
| 940 |
} |
| 941 |
|
| 942 |
/// Creates an affine transformation matrix containing a 3D rotation around the y axis of |
| 943 |
/// `angle` (in radians). |
| 944 |
/// |
| 945 |
/// The resulting matrix can be used to transform 3D points and vectors. See |
| 946 |
/// [`Self::transform_point3()`] and [`Self::transform_vector3()`]. |
| 947 |
#[inline] |
| 948 |
#[must_use] |
| 949 |
pub fn from_rotation_y(angle: {{ scalar_t }}) -> Self { |
| 950 |
let (sina, cosa) = math::sin_cos(angle); |
| 951 |
Self::from_cols( |
| 952 |
{{ col_t }}::new(cosa, 0.0, -sina, 0.0), |
| 953 |
{{ col_t }}::Y, |
| 954 |
{{ col_t }}::new(sina, 0.0, cosa, 0.0), |
| 955 |
{{ col_t }}::W, |
| 956 |
) |
| 957 |
} |
| 958 |
|
| 959 |
/// Creates an affine transformation matrix containing a 3D rotation around the z axis of |
| 960 |
/// `angle` (in radians). |
| 961 |
/// |
| 962 |
/// The resulting matrix can be used to transform 3D points and vectors. See |
| 963 |
/// [`Self::transform_point3()`] and [`Self::transform_vector3()`]. |
| 964 |
#[inline] |
| 965 |
#[must_use] |
| 966 |
pub fn from_rotation_z(angle: {{ scalar_t }}) -> Self { |
| 967 |
let (sina, cosa) = math::sin_cos(angle); |
| 968 |
Self::from_cols( |
| 969 |
{{ col_t }}::new(cosa, sina, 0.0, 0.0), |
| 970 |
{{ col_t }}::new(-sina, cosa, 0.0, 0.0), |
| 971 |
{{ col_t }}::Z, |
| 972 |
{{ col_t }}::W, |
| 973 |
) |
| 974 |
} |
| 975 |
|
| 976 |
/// Creates an affine transformation matrix containing the given 3D non-uniform `scale`. |
| 977 |
/// |
| 978 |
/// The resulting matrix can be used to transform 3D points and vectors. See |
| 979 |
/// [`Self::transform_point3()`] and [`Self::transform_vector3()`]. |
| 980 |
/// |
| 981 |
/// # Panics |
| 982 |
/// |
| 983 |
/// Will panic if all elements of `scale` are zero when `glam_assert` is enabled. |
| 984 |
#[inline] |
| 985 |
#[must_use] |
| 986 |
pub fn from_scale(scale: {{ vec3_t }}) -> Self { |
| 987 |
// Do not panic as long as any component is non-zero |
| 988 |
glam_assert!(scale.cmpne({{ vec3_t }}::ZERO).any()); |
| 989 |
|
| 990 |
Self::from_cols( |
| 991 |
{{ col_t }}::new(scale.x, 0.0, 0.0, 0.0), |
| 992 |
{{ col_t }}::new(0.0, scale.y, 0.0, 0.0), |
| 993 |
{{ col_t }}::new(0.0, 0.0, scale.z, 0.0), |
| 994 |
{{ col_t }}::W, |
| 995 |
) |
| 996 |
} |
| 997 |
{% endif %} |
| 998 |
|
| 999 |
/// Creates a {{ nxn }} matrix from the first {{ size }} values in `slice`. |
| 1000 |
/// |
| 1001 |
/// # Panics |
| 1002 |
/// |
| 1003 |
/// Panics if `slice` is less than {{ size }} elements long. |
| 1004 |
#[inline] |
| 1005 |
#[must_use] |
| 1006 |
pub const fn from_cols_slice(slice: &[{{ scalar_t }}]) -> Self { |
| 1007 |
Self::new( |
| 1008 |
{% for i in range(end = size) %} |
| 1009 |
slice[{{ i }}], |
| 1010 |
{%- endfor %} |
| 1011 |
) |
| 1012 |
} |
| 1013 |
|
| 1014 |
/// Writes the columns of `self` to the first {{ size }} elements in `slice`. |
| 1015 |
/// |
| 1016 |
/// # Panics |
| 1017 |
/// |
| 1018 |
/// Panics if `slice` is less than {{ size }} elements long. |
| 1019 |
#[inline] |
| 1020 |
pub fn write_cols_to_slice(self, slice: &mut [{{ scalar_t }}]) { |
| 1021 |
{% for i in range(end = dim) %} |
| 1022 |
{%- for j in range(end = dim) %} |
| 1023 |
slice[{{ i * dim + j }}] = self.{{ axes[i] }}.{{ components[j] }}; |
| 1024 |
{%- endfor %} |
| 1025 |
{%- endfor %} |
| 1026 |
} |
| 1027 |
|
| 1028 |
/// Returns the matrix column for the given `index`. |
| 1029 |
/// |
| 1030 |
/// # Panics |
| 1031 |
/// |
| 1032 |
/// Panics if `index` is greater than {{ dim - 1 }}. |
| 1033 |
#[inline] |
| 1034 |
#[must_use] |
| 1035 |
pub fn col(&self, index: usize) -> {{ col_t }} { |
| 1036 |
match index { |
| 1037 |
{% for axis in axes %} |
| 1038 |
{{ loop.index0 }} => self.{{ axis }}, |
| 1039 |
{%- endfor %} |
| 1040 |
_ => panic!("index out of bounds"), |
| 1041 |
} |
| 1042 |
} |
| 1043 |
|
| 1044 |
/// Returns a mutable reference to the matrix column for the given `index`. |
| 1045 |
/// |
| 1046 |
/// # Panics |
| 1047 |
/// |
| 1048 |
/// Panics if `index` is greater than {{ dim - 1 }}. |
| 1049 |
#[inline] |
| 1050 |
pub fn col_mut(&mut self, index: usize) -> &mut {{ col_t }} { |
| 1051 |
match index { |
| 1052 |
{% for axis in axes %} |
| 1053 |
{{ loop.index0 }} => &mut self.{{ axis }}, |
| 1054 |
{%- endfor %} |
| 1055 |
_ => panic!("index out of bounds"), |
| 1056 |
} |
| 1057 |
} |
| 1058 |
|
| 1059 |
/// Returns the matrix row for the given `index`. |
| 1060 |
/// |
| 1061 |
/// # Panics |
| 1062 |
/// |
| 1063 |
/// Panics if `index` is greater than {{ dim - 1 }}. |
| 1064 |
#[inline] |
| 1065 |
#[must_use] |
| 1066 |
pub fn row(&self, index: usize) -> {{ col_t }} { |
| 1067 |
match index { |
| 1068 |
{% for i in range(end=dim) %} |
| 1069 |
{{ i }} => {{ col_t }}::new( |
| 1070 |
{% for axis in axes %} |
| 1071 |
self.{{ axis }}.{{ components[i] }}, |
| 1072 |
{%- endfor %} |
| 1073 |
), |
| 1074 |
{%- endfor %} |
| 1075 |
_ => panic!("index out of bounds"), |
| 1076 |
} |
| 1077 |
} |
| 1078 |
|
| 1079 |
/// Returns `true` if, and only if, all elements are finite. |
| 1080 |
/// If any element is either `NaN`, positive or negative infinity, this will return `false`. |
| 1081 |
#[inline] |
| 1082 |
#[must_use] |
| 1083 |
pub fn is_finite(&self) -> bool { |
| 1084 |
{% for axis in axes %} |
| 1085 |
self.{{ axis }}.is_finite() {% if not loop.last %} && {% endif %} |
| 1086 |
{% endfor %} |
| 1087 |
} |
| 1088 |
|
| 1089 |
/// Returns `true` if any elements are `NaN`. |
| 1090 |
#[inline] |
| 1091 |
#[must_use] |
| 1092 |
pub fn is_nan(&self) -> bool { |
| 1093 |
{% for axis in axes %} |
| 1094 |
self.{{ axis }}.is_nan() {% if not loop.last %} || {% endif %} |
| 1095 |
{% endfor %} |
| 1096 |
} |
| 1097 |
|
| 1098 |
/// Returns the transpose of `self`. |
| 1099 |
#[inline] |
| 1100 |
#[must_use] |
| 1101 |
pub fn transpose(&self) -> Self { |
| 1102 |
{% if self_t == "Mat2" and is_sse2 %} |
| 1103 |
Self(unsafe { _mm_shuffle_ps(self.0, self.0, 0b11_01_10_00) }) |
| 1104 |
{% elif self_t == "Mat2" and is_wasm32 %} |
| 1105 |
Self(i32x4_shuffle::<0, 2, 5, 7>(self.0, self.0)) |
| 1106 |
{% elif self_t == "Mat2" and is_coresimd %} |
| 1107 |
Self(simd_swizzle!(self.0, [0, 2, 1, 3])) |
| 1108 |
{% elif self_t == "Mat2" and is_neon %} |
| 1109 |
Self(unsafe { |
| 1110 |
vsetq_lane_f32( |
| 1111 |
vgetq_lane_f32(self.0, 2), |
| 1112 |
vsetq_lane_f32(vgetq_lane_f32(self.0, 1), self.0, 2), |
| 1113 |
1, |
| 1114 |
) |
| 1115 |
}) |
| 1116 |
{% elif self_t == "Mat3A" and is_sse2 %} |
| 1117 |
unsafe { |
| 1118 |
let tmp0 = _mm_shuffle_ps(self.x_axis.0, self.y_axis.0, 0b01_00_01_00); |
| 1119 |
let tmp1 = _mm_shuffle_ps(self.x_axis.0, self.y_axis.0, 0b11_10_11_10); |
| 1120 |
|
| 1121 |
Self { |
| 1122 |
x_axis: Vec3A(_mm_shuffle_ps(tmp0, self.z_axis.0, 0b00_00_10_00)), |
| 1123 |
y_axis: Vec3A(_mm_shuffle_ps(tmp0, self.z_axis.0, 0b01_01_11_01)), |
| 1124 |
z_axis: Vec3A(_mm_shuffle_ps(tmp1, self.z_axis.0, 0b10_10_10_00)), |
| 1125 |
} |
| 1126 |
} |
| 1127 |
{% elif self_t == "Mat3A" and is_wasm32 %} |
| 1128 |
let tmp0 = i32x4_shuffle::<0, 1, 4, 5>(self.x_axis.0, self.y_axis.0); |
| 1129 |
let tmp1 = i32x4_shuffle::<2, 3, 6, 7>(self.x_axis.0, self.y_axis.0); |
| 1130 |
|
| 1131 |
Self { |
| 1132 |
x_axis: Vec3A(i32x4_shuffle::<0, 2, 4, 4>(tmp0, self.z_axis.0)), |
| 1133 |
y_axis: Vec3A(i32x4_shuffle::<1, 3, 5, 5>(tmp0, self.z_axis.0)), |
| 1134 |
z_axis: Vec3A(i32x4_shuffle::<0, 2, 6, 6>(tmp1, self.z_axis.0)), |
| 1135 |
} |
| 1136 |
{% elif self_t == "Mat3A" and is_coresimd %} |
| 1137 |
let tmp0 = simd_swizzle!( |
| 1138 |
self.x_axis.0, |
| 1139 |
self.y_axis.0, |
| 1140 |
[0, 1, 4, 5] |
| 1141 |
); |
| 1142 |
let tmp1 = simd_swizzle!( |
| 1143 |
self.x_axis.0, |
| 1144 |
self.y_axis.0, |
| 1145 |
[2, 3, 6, 7] |
| 1146 |
); |
| 1147 |
|
| 1148 |
Self { |
| 1149 |
x_axis: Vec3A(simd_swizzle!( |
| 1150 |
tmp0, |
| 1151 |
self.z_axis.0, |
| 1152 |
[0, 2, 4, 4] |
| 1153 |
)), |
| 1154 |
y_axis: Vec3A(simd_swizzle!( |
| 1155 |
tmp0, |
| 1156 |
self.z_axis.0, |
| 1157 |
[1, 3, 5, 5] |
| 1158 |
)), |
| 1159 |
z_axis: Vec3A(simd_swizzle!( |
| 1160 |
tmp1, |
| 1161 |
self.z_axis.0, |
| 1162 |
[0, 2, 6, 6] |
| 1163 |
)), |
| 1164 |
} |
| 1165 |
{% elif self_t == "Mat3A" and is_neon %} |
| 1166 |
let x = self.x_axis.0; |
| 1167 |
let y = self.y_axis.0; |
| 1168 |
let z = self.z_axis.0; |
| 1169 |
unsafe { |
| 1170 |
let tmp0 = vreinterpretq_f32_u64(vsetq_lane_u64( |
| 1171 |
vgetq_lane_u64(vreinterpretq_u64_f32(y), 0), |
| 1172 |
vreinterpretq_u64_f32(x), |
| 1173 |
1, |
| 1174 |
)); |
| 1175 |
let tmp1 = vreinterpretq_f32_u64(vzip2q_u64( |
| 1176 |
vreinterpretq_u64_f32(x), |
| 1177 |
vreinterpretq_u64_f32(y), |
| 1178 |
)); |
| 1179 |
Mat3A::from_cols( |
| 1180 |
Vec3A::from(vsetq_lane_f32(vgetq_lane_f32(z, 0), vuzp1q_f32(tmp0, z), 3)), |
| 1181 |
Vec3A::from(vuzp2q_f32(tmp0, vdupq_laneq_f32(z, 1))), |
| 1182 |
Vec3A::from(vsetq_lane_f32(vgetq_lane_f32(z, 2), vuzp1q_f32(tmp1, z), 2)), |
| 1183 |
) |
| 1184 |
} |
| 1185 |
{% elif self_t == "Mat4" and is_sse2 %} |
| 1186 |
unsafe { |
| 1187 |
// Based on https://github.com/microsoft/DirectXMath `XMMatrixTranspose` |
| 1188 |
let tmp0 = _mm_shuffle_ps(self.x_axis.0, self.y_axis.0, 0b01_00_01_00); |
| 1189 |
let tmp1 = _mm_shuffle_ps(self.x_axis.0, self.y_axis.0, 0b11_10_11_10); |
| 1190 |
let tmp2 = _mm_shuffle_ps(self.z_axis.0, self.w_axis.0, 0b01_00_01_00); |
| 1191 |
let tmp3 = _mm_shuffle_ps(self.z_axis.0, self.w_axis.0, 0b11_10_11_10); |
| 1192 |
|
| 1193 |
Self { |
| 1194 |
x_axis: Vec4(_mm_shuffle_ps(tmp0, tmp2, 0b10_00_10_00)), |
| 1195 |
y_axis: Vec4(_mm_shuffle_ps(tmp0, tmp2, 0b11_01_11_01)), |
| 1196 |
z_axis: Vec4(_mm_shuffle_ps(tmp1, tmp3, 0b10_00_10_00)), |
| 1197 |
w_axis: Vec4(_mm_shuffle_ps(tmp1, tmp3, 0b11_01_11_01)), |
| 1198 |
} |
| 1199 |
} |
| 1200 |
{% elif self_t == "Mat4" and is_wasm32 %} |
| 1201 |
// Based on https://github.com/microsoft/DirectXMath `XMMatrixTranspose` |
| 1202 |
let tmp0 = i32x4_shuffle::<0, 1, 4, 5>(self.x_axis.0, self.y_axis.0); |
| 1203 |
let tmp1 = i32x4_shuffle::<2, 3, 6, 7>(self.x_axis.0, self.y_axis.0); |
| 1204 |
let tmp2 = i32x4_shuffle::<0, 1, 4, 5>(self.z_axis.0, self.w_axis.0); |
| 1205 |
let tmp3 = i32x4_shuffle::<2, 3, 6, 7>(self.z_axis.0, self.w_axis.0); |
| 1206 |
|
| 1207 |
Self { |
| 1208 |
x_axis: Vec4(i32x4_shuffle::<0, 2, 4, 6>(tmp0, tmp2)), |
| 1209 |
y_axis: Vec4(i32x4_shuffle::<1, 3, 5, 7>(tmp0, tmp2)), |
| 1210 |
z_axis: Vec4(i32x4_shuffle::<0, 2, 4, 6>(tmp1, tmp3)), |
| 1211 |
w_axis: Vec4(i32x4_shuffle::<1, 3, 5, 7>(tmp1, tmp3)), |
| 1212 |
} |
| 1213 |
{% elif self_t == "Mat4" and is_coresimd %} |
| 1214 |
// Based on https://github.com/microsoft/DirectXMath `XMMatrixTranspose` |
| 1215 |
let tmp0 = simd_swizzle!( |
| 1216 |
self.x_axis.0, |
| 1217 |
self.y_axis.0, |
| 1218 |
[0, 1, 4, 5] |
| 1219 |
); |
| 1220 |
let tmp1 = simd_swizzle!( |
| 1221 |
self.x_axis.0, |
| 1222 |
self.y_axis.0, |
| 1223 |
[2, 3, 6, 7] |
| 1224 |
); |
| 1225 |
let tmp2 = simd_swizzle!( |
| 1226 |
self.z_axis.0, |
| 1227 |
self.w_axis.0, |
| 1228 |
[0, 1, 4, 5] |
| 1229 |
); |
| 1230 |
let tmp3 = simd_swizzle!( |
| 1231 |
self.z_axis.0, |
| 1232 |
self.w_axis.0, |
| 1233 |
[2, 3, 6, 7] |
| 1234 |
); |
| 1235 |
|
| 1236 |
Self { |
| 1237 |
x_axis: Vec4(simd_swizzle!(tmp0, tmp2, [0, 2, 4, 6])), |
| 1238 |
y_axis: Vec4(simd_swizzle!(tmp0, tmp2, [1, 3, 5, 7])), |
| 1239 |
z_axis: Vec4(simd_swizzle!(tmp1, tmp3, [0, 2, 4, 6])), |
| 1240 |
w_axis: Vec4(simd_swizzle!(tmp1, tmp3, [1, 3, 5, 7])), |
| 1241 |
} |
| 1242 |
{% else %} |
| 1243 |
Self { |
| 1244 |
{% for i in range(end = dim) %} |
| 1245 |
{{ axes[i] }}: {{ col_t }}::new( |
| 1246 |
{% for j in range(end = dim) %} |
| 1247 |
self.{{ axes[j] }}.{{ components[i] }}, |
| 1248 |
{% endfor %} |
| 1249 |
), |
| 1250 |
{%- endfor %} |
| 1251 |
} |
| 1252 |
{% endif %} |
| 1253 |
} |
| 1254 |
|
| 1255 |
/// Returns the determinant of `self`. |
| 1256 |
{%- if dim < 4 %} |
| 1257 |
#[inline] |
| 1258 |
{%- endif %} |
| 1259 |
#[must_use] |
| 1260 |
pub fn determinant(&self) -> {{ scalar_t }} { |
| 1261 |
{% if self_t == "Mat2" and is_sse2 %} |
| 1262 |
unsafe { |
| 1263 |
let abcd = self.0; |
| 1264 |
let dcba = _mm_shuffle_ps(abcd, abcd, 0b00_01_10_11); |
| 1265 |
let prod = _mm_mul_ps(abcd, dcba); |
| 1266 |
let det = _mm_sub_ps(prod, _mm_shuffle_ps(prod, prod, 0b01_01_01_01)); |
| 1267 |
_mm_cvtss_f32(det) |
| 1268 |
} |
| 1269 |
{% elif self_t == "Mat2" and is_wasm32 %} |
| 1270 |
let abcd = self.0; |
| 1271 |
let dcba = i32x4_shuffle::<3, 2, 5, 4>(abcd, abcd); |
| 1272 |
let prod = f32x4_mul(abcd, dcba); |
| 1273 |
let det = f32x4_sub(prod, i32x4_shuffle::<1, 1, 5, 5>(prod, prod)); |
| 1274 |
f32x4_extract_lane::<0>(det) |
| 1275 |
{% elif self_t == "Mat2" and is_coresimd %} |
| 1276 |
let abcd = self.0; |
| 1277 |
let dcba = simd_swizzle!(abcd, [3, 2, 1, 0]); |
| 1278 |
let prod = abcd * dcba; |
| 1279 |
let det = prod - simd_swizzle!(prod, [1, 1, 1, 1]); |
| 1280 |
det[0] |
| 1281 |
{% elif self_t == "Mat2" and is_neon %} |
| 1282 |
unsafe { |
| 1283 |
let abcd = self.0; |
| 1284 |
let badc = vrev64q_f32(abcd); |
| 1285 |
let dcba = vextq_f32(badc, badc, 2); |
| 1286 |
let prod = vmulq_f32(abcd, dcba); |
| 1287 |
let det = vsubq_f32(prod, vdupq_laneq_f32(prod, 1)); |
| 1288 |
vgetq_lane_f32(det, 0) |
| 1289 |
} |
| 1290 |
{% elif self_t == "Mat4" and is_sse2 %} |
| 1291 |
unsafe { |
| 1292 |
// Based on https://github.com/g-truc/glm `glm_mat4_determinant_lowp` |
| 1293 |
let swp2a = _mm_shuffle_ps(self.z_axis.0, self.z_axis.0, 0b00_01_01_10); |
| 1294 |
let swp3a = _mm_shuffle_ps(self.w_axis.0, self.w_axis.0, 0b11_10_11_11); |
| 1295 |
let swp2b = _mm_shuffle_ps(self.z_axis.0, self.z_axis.0, 0b11_10_11_11); |
| 1296 |
let swp3b = _mm_shuffle_ps(self.w_axis.0, self.w_axis.0, 0b00_01_01_10); |
| 1297 |
let swp2c = _mm_shuffle_ps(self.z_axis.0, self.z_axis.0, 0b00_00_01_10); |
| 1298 |
let swp3c = _mm_shuffle_ps(self.w_axis.0, self.w_axis.0, 0b01_10_00_00); |
| 1299 |
|
| 1300 |
let mula = _mm_mul_ps(swp2a, swp3a); |
| 1301 |
let mulb = _mm_mul_ps(swp2b, swp3b); |
| 1302 |
let mulc = _mm_mul_ps(swp2c, swp3c); |
| 1303 |
let sube = _mm_sub_ps(mula, mulb); |
| 1304 |
let subf = _mm_sub_ps(_mm_movehl_ps(mulc, mulc), mulc); |
| 1305 |
|
| 1306 |
let subfaca = _mm_shuffle_ps(sube, sube, 0b10_01_00_00); |
| 1307 |
let swpfaca = _mm_shuffle_ps(self.y_axis.0, self.y_axis.0, 0b00_00_00_01); |
| 1308 |
let mulfaca = _mm_mul_ps(swpfaca, subfaca); |
| 1309 |
|
| 1310 |
let subtmpb = _mm_shuffle_ps(sube, subf, 0b00_00_11_01); |
| 1311 |
let subfacb = _mm_shuffle_ps(subtmpb, subtmpb, 0b11_01_01_00); |
| 1312 |
let swpfacb = _mm_shuffle_ps(self.y_axis.0, self.y_axis.0, 0b01_01_10_10); |
| 1313 |
let mulfacb = _mm_mul_ps(swpfacb, subfacb); |
| 1314 |
|
| 1315 |
let subres = _mm_sub_ps(mulfaca, mulfacb); |
| 1316 |
let subtmpc = _mm_shuffle_ps(sube, subf, 0b01_00_10_10); |
| 1317 |
let subfacc = _mm_shuffle_ps(subtmpc, subtmpc, 0b11_11_10_00); |
| 1318 |
let swpfacc = _mm_shuffle_ps(self.y_axis.0, self.y_axis.0, 0b10_11_11_11); |
| 1319 |
let mulfacc = _mm_mul_ps(swpfacc, subfacc); |
| 1320 |
|
| 1321 |
let addres = _mm_add_ps(subres, mulfacc); |
| 1322 |
let detcof = _mm_mul_ps(addres, _mm_setr_ps(1.0, -1.0, 1.0, -1.0)); |
| 1323 |
|
| 1324 |
dot4(self.x_axis.0, detcof) |
| 1325 |
} |
| 1326 |
{% elif self_t == "Mat4" and is_wasm32 %} |
| 1327 |
// Based on https://github.com/g-truc/glm `glm_mat4_determinant` |
| 1328 |
let swp2a = i32x4_shuffle::<2, 1, 1, 0>(self.z_axis.0, self.z_axis.0); |
| 1329 |
let swp3a = i32x4_shuffle::<3, 3, 2, 3>(self.w_axis.0, self.w_axis.0); |
| 1330 |
let swp2b = i32x4_shuffle::<3, 3, 2, 3>(self.z_axis.0, self.z_axis.0); |
| 1331 |
let swp3b = i32x4_shuffle::<2, 1, 1, 0>(self.w_axis.0, self.w_axis.0); |
| 1332 |
let swp2c = i32x4_shuffle::<2, 1, 0, 0>(self.z_axis.0, self.z_axis.0); |
| 1333 |
let swp3c = i32x4_shuffle::<0, 0, 2, 1>(self.w_axis.0, self.w_axis.0); |
| 1334 |
|
| 1335 |
let mula = f32x4_mul(swp2a, swp3a); |
| 1336 |
let mulb = f32x4_mul(swp2b, swp3b); |
| 1337 |
let mulc = f32x4_mul(swp2c, swp3c); |
| 1338 |
let sube = f32x4_sub(mula, mulb); |
| 1339 |
let subf = f32x4_sub(i32x4_shuffle::<6, 7, 2, 3>(mulc, mulc), mulc); |
| 1340 |
|
| 1341 |
let subfaca = i32x4_shuffle::<0, 0, 1, 2>(sube, sube); |
| 1342 |
let swpfaca = i32x4_shuffle::<1, 0, 0, 0>(self.y_axis.0, self.y_axis.0); |
| 1343 |
let mulfaca = f32x4_mul(swpfaca, subfaca); |
| 1344 |
|
| 1345 |
let subtmpb = i32x4_shuffle::<1, 3, 4, 4>(sube, subf); |
| 1346 |
let subfacb = i32x4_shuffle::<0, 1, 1, 3>(subtmpb, subtmpb); |
| 1347 |
let swpfacb = i32x4_shuffle::<2, 2, 1, 1>(self.y_axis.0, self.y_axis.0); |
| 1348 |
let mulfacb = f32x4_mul(swpfacb, subfacb); |
| 1349 |
|
| 1350 |
let subres = f32x4_sub(mulfaca, mulfacb); |
| 1351 |
let subtmpc = i32x4_shuffle::<2, 2, 4, 5>(sube, subf); |
| 1352 |
let subfacc = i32x4_shuffle::<0, 2, 3, 3>(subtmpc, subtmpc); |
| 1353 |
let swpfacc = i32x4_shuffle::<3, 3, 3, 2>(self.y_axis.0, self.y_axis.0); |
| 1354 |
let mulfacc = f32x4_mul(swpfacc, subfacc); |
| 1355 |
|
| 1356 |
let addres = f32x4_add(subres, mulfacc); |
| 1357 |
let detcof = f32x4_mul(addres, f32x4(1.0, -1.0, 1.0, -1.0)); |
| 1358 |
|
| 1359 |
dot4(self.x_axis.0, detcof) |
| 1360 |
{% elif self_t == "Mat4" and is_coresimd %} |
| 1361 |
// Based on https://github.com/g-truc/glm `glm_mat4_determinant` |
| 1362 |
let swp2a = simd_swizzle!(self.z_axis.0, [2, 1, 1, 0]); |
| 1363 |
let swp3a = simd_swizzle!(self.w_axis.0, [3, 3, 2, 3]); |
| 1364 |
let swp2b = simd_swizzle!(self.z_axis.0, [3, 3, 2, 3]); |
| 1365 |
let swp3b = simd_swizzle!(self.w_axis.0, [2, 1, 1, 0]); |
| 1366 |
let swp2c = simd_swizzle!(self.z_axis.0, [2, 1, 0, 0]); |
| 1367 |
let swp3c = simd_swizzle!(self.w_axis.0, [0, 0, 2, 1]); |
| 1368 |
|
| 1369 |
let mula = swp2a * swp3a; |
| 1370 |
let mulb = swp2b * swp3b; |
| 1371 |
let mulc = swp2c * swp3c; |
| 1372 |
let sube = mula - mulb; |
| 1373 |
let subf = simd_swizzle!(mulc, [2, 3, 2, 3]) - mulc; |
| 1374 |
|
| 1375 |
let subfaca = simd_swizzle!(sube, [0, 0, 1, 2]); |
| 1376 |
let swpfaca = simd_swizzle!(self.y_axis.0, [1, 0, 0, 0]); |
| 1377 |
let mulfaca = swpfaca * subfaca; |
| 1378 |
|
| 1379 |
let subtmpb = simd_swizzle!(sube, subf, [1, 3, 4, 4]); |
| 1380 |
let subfacb = simd_swizzle!(subtmpb, [0, 1, 1, 3]); |
| 1381 |
let swpfacb = simd_swizzle!(self.y_axis.0, [2, 2, 1, 1]); |
| 1382 |
let mulfacb = swpfacb * subfacb; |
| 1383 |
|
| 1384 |
let subres = mulfaca - mulfacb; |
| 1385 |
let subtmpc = simd_swizzle!(sube, subf, [2, 2, 4, 5]); |
| 1386 |
let subfacc = simd_swizzle!(subtmpc, [0, 2, 3, 3]); |
| 1387 |
let swpfacc = simd_swizzle!(self.y_axis.0, [3, 3, 3, 2]); |
| 1388 |
let mulfacc = swpfacc * subfacc; |
| 1389 |
|
| 1390 |
let addres = subres + mulfacc; |
| 1391 |
let detcof = addres * f32x4::from_array([1.0, -1.0, 1.0, -1.0]); |
| 1392 |
|
| 1393 |
dot4(self.x_axis.0, detcof) |
| 1394 |
{# |
| 1395 |
// neon implementation is slower than scalar |
| 1396 |
// {% elif self_t == "Mat4" and is_neon %} |
| 1397 |
// unsafe { |
| 1398 |
// let swizz2110 = |x| { |
| 1399 |
// let x = vuzp1q_f32(x, vdupq_laneq_f32(x, 1)); |
| 1400 |
// vextq_f32(x, x, 1) |
| 1401 |
// }; |
| 1402 |
// let swizz3323 = |x| { |
| 1403 |
// let xy = vgetq_lane_f32(x, 3); |
| 1404 |
// vsetq_lane_f32(xy, vsetq_lane_f32(xy, x, 0), 1) |
| 1405 |
// }; |
| 1406 |
// let swizz2100 = |x| { |
| 1407 |
// let y = vuzp1q_f32(x, x); |
| 1408 |
// vuzp1q_f32(vextq_f32(x, y, 3), y) |
| 1409 |
// }; |
| 1410 |
// let swizz0021 = |x| vtrn1q_f32(x, vzip1q_f32(x, x)); |
| 1411 |
// // let swizz6723 = |x, y| { |
| 1412 |
// // vsetq_lane_f64(vgetq_lane_f64(y, 1), 0) |
| 1413 |
// // }; |
| 1414 |
// let swizz2323 = |x| vreinterpretq_f32_f64(vdupq_laneq_f64(vreinterpretq_f64_f32(x), 1)); |
| 1415 |
// let swizz0012 = |x| vzip1q_f32(x, vuzp1q_f32(x, x)); |
| 1416 |
// let swizz1000 = |x| vsetq_lane_f32(vgetq_lane_f32(x, 1), vdupq_laneq_f32(x, 0), 0); |
| 1417 |
// let swizz1344 = |x, y| vuzp2q_f32(x, vdupq_laneq_f32(y, 0)); |
| 1418 |
// let swizz0113 = |x| vsetq_lane_f32(vgetq_lane_f32(x, 1), x, 2); |
| 1419 |
// let swizz2211 = |x| { |
| 1420 |
// let x = vsetq_lane_f32(vgetq_lane_f32(x, 1), x, 3); |
| 1421 |
// vzip2q_f32(x, x) |
| 1422 |
// }; |
| 1423 |
// let swizz2245 = |x, y| vextq_f32(vtrn1q_f32(x, x), y, 1); |
| 1424 |
// let swizz0233 = |x| vuzp1q_f32(x, vdupq_laneq_f32(x, 3)); |
| 1425 |
// let swizz3332 = |x| vsetq_lane_f32(vgetq_lane_f32(x, 2), vdupq_laneq_f32(x, 3), 3); |
| 1426 |
|
| 1427 |
// // Based on https://github.com/g-truc/glm `glm_mat4_determinant` |
| 1428 |
// let swp2a = swizz2110(self.z_axis.0); |
| 1429 |
// let swp3a = swizz3323(self.w_axis.0); |
| 1430 |
// let swp2b = swizz3323(self.z_axis.0); |
| 1431 |
// let swp3b = swizz2110(self.w_axis.0); |
| 1432 |
// let swp2c = swizz2100(self.z_axis.0); |
| 1433 |
// let swp3c = swizz0021(self.w_axis.0); |
| 1434 |
|
| 1435 |
// let mula = vmulq_f32(swp2a, swp3a); |
| 1436 |
// let mulb = vmulq_f32(swp2b, swp3b); |
| 1437 |
// let mulc = vmulq_f32(swp2c, swp3c); |
| 1438 |
// let sube = vsubq_f32(mula, mulb); |
| 1439 |
// let subf = vsubq_f32(swizz2323(mulc), mulc); |
| 1440 |
|
| 1441 |
// let subfaca = swizz0012(sube); |
| 1442 |
// let swpfaca = swizz1000(self.y_axis.0); |
| 1443 |
// let mulfaca = vmulq_f32(swpfaca, subfaca); |
| 1444 |
|
| 1445 |
// let subtmpb = swizz1344(sube, subf); |
| 1446 |
// let subfacb = swizz0113(subtmpb); |
| 1447 |
// let swpfacb = swizz2211(self.y_axis.0); |
| 1448 |
// let mulfacb = vmulq_f32(swpfacb, subfacb); |
| 1449 |
|
| 1450 |
// let subres = vsubq_f32(mulfaca, mulfacb); |
| 1451 |
// let subtmpc = swizz2245(sube, subf); |
| 1452 |
// let subfacc = swizz0233(subtmpc); |
| 1453 |
// let swpfacc = swizz3332(self.y_axis.0); |
| 1454 |
// let mulfacc = vmulq_f32(swpfacc, subfacc); |
| 1455 |
|
| 1456 |
// let addres = vaddq_f32(subres, mulfacc); |
| 1457 |
// const COF: float32x4_t = Vec4::new(1.0, -1.0, 1.0, -1.0).0; |
| 1458 |
// let detcof = vmulq_f32(addres, COF); |
| 1459 |
|
| 1460 |
// dot4(self.x_axis.0, detcof) |
| 1461 |
// } |
| 1462 |
#} |
| 1463 |
{% elif dim == 2 %} |
| 1464 |
self.x_axis.x * self.y_axis.y - self.x_axis.y * self.y_axis.x |
| 1465 |
{% elif dim == 3 %} |
| 1466 |
self.z_axis.dot(self.x_axis.cross(self.y_axis)) |
| 1467 |
{% elif dim == 4 %} |
| 1468 |
let (m00, m01, m02, m03) = self.x_axis.into(); |
| 1469 |
let (m10, m11, m12, m13) = self.y_axis.into(); |
| 1470 |
let (m20, m21, m22, m23) = self.z_axis.into(); |
| 1471 |
let (m30, m31, m32, m33) = self.w_axis.into(); |
| 1472 |
|
| 1473 |
let a2323 = m22 * m33 - m23 * m32; |
| 1474 |
let a1323 = m21 * m33 - m23 * m31; |
| 1475 |
let a1223 = m21 * m32 - m22 * m31; |
| 1476 |
let a0323 = m20 * m33 - m23 * m30; |
| 1477 |
let a0223 = m20 * m32 - m22 * m30; |
| 1478 |
let a0123 = m20 * m31 - m21 * m30; |
| 1479 |
|
| 1480 |
m00 * (m11 * a2323 - m12 * a1323 + m13 * a1223) |
| 1481 |
- m01 * (m10 * a2323 - m12 * a0323 + m13 * a0223) |
| 1482 |
+ m02 * (m10 * a1323 - m11 * a0323 + m13 * a0123) |
| 1483 |
- m03 * (m10 * a1223 - m11 * a0223 + m12 * a0123) |
| 1484 |
{% endif %} |
| 1485 |
} |
| 1486 |
|
| 1487 |
/// Returns the inverse of `self`. |
| 1488 |
/// |
| 1489 |
/// If the matrix is not invertible the returned matrix will be invalid. |
| 1490 |
/// |
| 1491 |
/// # Panics |
| 1492 |
/// |
| 1493 |
/// Will panic if the determinant of `self` is zero when `glam_assert` is enabled. |
| 1494 |
{%- if dim < 4 %} |
| 1495 |
#[inline] |
| 1496 |
{%- endif %} |
| 1497 |
#[must_use] |
| 1498 |
pub fn inverse(&self) -> Self { |
| 1499 |
{% if self_t == "Mat2" and is_sse2 %} |
| 1500 |
unsafe { |
| 1501 |
const SIGN: __m128 = crate::sse2::m128_from_f32x4([1.0, -1.0, -1.0, 1.0]); |
| 1502 |
let abcd = self.0; |
| 1503 |
let dcba = _mm_shuffle_ps(abcd, abcd, 0b00_01_10_11); |
| 1504 |
let prod = _mm_mul_ps(abcd, dcba); |
| 1505 |
let sub = _mm_sub_ps(prod, _mm_shuffle_ps(prod, prod, 0b01_01_01_01)); |
| 1506 |
let det = _mm_shuffle_ps(sub, sub, 0b00_00_00_00); |
| 1507 |
let tmp = _mm_div_ps(SIGN, det); |
| 1508 |
glam_assert!(Mat2(tmp).is_finite()); |
| 1509 |
let dbca = _mm_shuffle_ps(abcd, abcd, 0b00_10_01_11); |
| 1510 |
Self(_mm_mul_ps(dbca, tmp)) |
| 1511 |
} |
| 1512 |
{% elif self_t == "Mat2" and is_wasm32 %} |
| 1513 |
const SIGN: v128 = crate::wasm32::v128_from_f32x4([1.0, -1.0, -1.0, 1.0]); |
| 1514 |
let abcd = self.0; |
| 1515 |
let dcba = i32x4_shuffle::<3, 2, 5, 4>(abcd, abcd); |
| 1516 |
let prod = f32x4_mul(abcd, dcba); |
| 1517 |
let sub = f32x4_sub(prod, i32x4_shuffle::<1, 1, 5, 5>(prod, prod)); |
| 1518 |
let det = i32x4_shuffle::<0, 0, 4, 4>(sub, sub); |
| 1519 |
let tmp = f32x4_div(SIGN, det); |
| 1520 |
glam_assert!(Mat2(tmp).is_finite()); |
| 1521 |
let dbca = i32x4_shuffle::<3, 1, 6, 4>(abcd, abcd); |
| 1522 |
Self(f32x4_mul(dbca, tmp)) |
| 1523 |
{% elif self_t == "Mat2" and is_coresimd %} |
| 1524 |
const SIGN: f32x4 = f32x4::from_array([1.0, -1.0, -1.0, 1.0]); |
| 1525 |
let abcd = self.0; |
| 1526 |
let dcba = simd_swizzle!(abcd, [3, 2, 1, 0]); |
| 1527 |
let prod = abcd * dcba; |
| 1528 |
let sub = prod - simd_swizzle!(prod, [1, 1, 1, 1]); |
| 1529 |
let det = simd_swizzle!(sub, [0, 0, 0, 0]); |
| 1530 |
let tmp = SIGN / det; |
| 1531 |
glam_assert!(Mat2(tmp).is_finite()); |
| 1532 |
let dbca = simd_swizzle!(abcd, [3, 1, 2, 0]); |
| 1533 |
Self(dbca.mul(tmp)) |
| 1534 |
{% elif self_t == "Mat2" and is_neon %} |
| 1535 |
unsafe { |
| 1536 |
const SIGN: float32x4_t = crate::neon::f32x4_from_array([1.0, -1.0, -1.0, 1.0]); |
| 1537 |
let abcd = self.0; |
| 1538 |
let badc = vrev64q_f32(abcd); |
| 1539 |
let dcba = vextq_f32(badc, badc, 2); |
| 1540 |
let prod = vmulq_f32(abcd, dcba); |
| 1541 |
let sub = vsubq_f32(prod, vdupq_laneq_f32(prod, 1)); |
| 1542 |
let det = vdupq_laneq_f32(sub, 0); |
| 1543 |
let tmp = vdivq_f32(SIGN, det); |
| 1544 |
glam_assert!(Mat2(tmp).is_finite()); |
| 1545 |
//let dbca = simd_swizzle!(abcd, [3, 1, 2, 0]); |
| 1546 |
let dbca = vsetq_lane_f32( |
| 1547 |
vgetq_lane_f32(abcd, 0), |
| 1548 |
vsetq_lane_f32(vgetq_lane_f32(abcd, 3), abcd, 0), |
| 1549 |
3, |
| 1550 |
); |
| 1551 |
Self(vmulq_f32(dbca, tmp)) |
| 1552 |
} |
| 1553 |
{% elif self_t == "Mat4" and is_sse2 %} |
| 1554 |
{{ sse2::impl_mat4_inverse() }} |
| 1555 |
{% elif self_t == "Mat4" and is_wasm32 %} |
| 1556 |
{{ wasm32::impl_mat4_inverse() }} |
| 1557 |
{% elif self_t == "Mat4" and is_coresimd %} |
| 1558 |
{{ coresimd::impl_mat4_inverse() }} |
| 1559 |
{% elif self_t == "Mat4" and is_neon %} |
| 1560 |
{{ neon::impl_mat4_inverse() }} |
| 1561 |
{% elif dim == 2 %} |
| 1562 |
let inv_det = { |
| 1563 |
let det = self.determinant(); |
| 1564 |
glam_assert!(det != 0.0); |
| 1565 |
det.recip() |
| 1566 |
}; |
| 1567 |
Self::new( |
| 1568 |
self.y_axis.y * inv_det, |
| 1569 |
self.x_axis.y * -inv_det, |
| 1570 |
self.y_axis.x * -inv_det, |
| 1571 |
self.x_axis.x * inv_det, |
| 1572 |
) |
| 1573 |
{% elif dim == 3 %} |
| 1574 |
let tmp0 = self.y_axis.cross(self.z_axis); |
| 1575 |
let tmp1 = self.z_axis.cross(self.x_axis); |
| 1576 |
let tmp2 = self.x_axis.cross(self.y_axis); |
| 1577 |
let det = self.z_axis.dot(tmp2); |
| 1578 |
glam_assert!(det != 0.0); |
| 1579 |
let inv_det = {{ col_t }}::splat(det.recip()); |
| 1580 |
Self::from_cols(tmp0.mul(inv_det), tmp1.mul(inv_det), tmp2.mul(inv_det)).transpose() |
| 1581 |
{% elif dim == 4 %} |
| 1582 |
let (m00, m01, m02, m03) = self.x_axis.into(); |
| 1583 |
let (m10, m11, m12, m13) = self.y_axis.into(); |
| 1584 |
let (m20, m21, m22, m23) = self.z_axis.into(); |
| 1585 |
let (m30, m31, m32, m33) = self.w_axis.into(); |
| 1586 |
|
| 1587 |
let coef00 = m22 * m33 - m32 * m23; |
| 1588 |
let coef02 = m12 * m33 - m32 * m13; |
| 1589 |
let coef03 = m12 * m23 - m22 * m13; |
| 1590 |
|
| 1591 |
let coef04 = m21 * m33 - m31 * m23; |
| 1592 |
let coef06 = m11 * m33 - m31 * m13; |
| 1593 |
let coef07 = m11 * m23 - m21 * m13; |
| 1594 |
|
| 1595 |
let coef08 = m21 * m32 - m31 * m22; |
| 1596 |
let coef10 = m11 * m32 - m31 * m12; |
| 1597 |
let coef11 = m11 * m22 - m21 * m12; |
| 1598 |
|
| 1599 |
let coef12 = m20 * m33 - m30 * m23; |
| 1600 |
let coef14 = m10 * m33 - m30 * m13; |
| 1601 |
let coef15 = m10 * m23 - m20 * m13; |
| 1602 |
|
| 1603 |
let coef16 = m20 * m32 - m30 * m22; |
| 1604 |
let coef18 = m10 * m32 - m30 * m12; |
| 1605 |
let coef19 = m10 * m22 - m20 * m12; |
| 1606 |
|
| 1607 |
let coef20 = m20 * m31 - m30 * m21; |
| 1608 |
let coef22 = m10 * m31 - m30 * m11; |
| 1609 |
let coef23 = m10 * m21 - m20 * m11; |
| 1610 |
|
| 1611 |
let fac0 = {{ col_t }}::new(coef00, coef00, coef02, coef03); |
| 1612 |
let fac1 = {{ col_t }}::new(coef04, coef04, coef06, coef07); |
| 1613 |
let fac2 = {{ col_t }}::new(coef08, coef08, coef10, coef11); |
| 1614 |
let fac3 = {{ col_t }}::new(coef12, coef12, coef14, coef15); |
| 1615 |
let fac4 = {{ col_t }}::new(coef16, coef16, coef18, coef19); |
| 1616 |
let fac5 = {{ col_t }}::new(coef20, coef20, coef22, coef23); |
| 1617 |
|
| 1618 |
let vec0 = {{ col_t }}::new(m10, m00, m00, m00); |
| 1619 |
let vec1 = {{ col_t }}::new(m11, m01, m01, m01); |
| 1620 |
let vec2 = {{ col_t }}::new(m12, m02, m02, m02); |
| 1621 |
let vec3 = {{ col_t }}::new(m13, m03, m03, m03); |
| 1622 |
|
| 1623 |
let inv0 = vec1.mul(fac0).sub(vec2.mul(fac1)).add(vec3.mul(fac2)); |
| 1624 |
let inv1 = vec0.mul(fac0).sub(vec2.mul(fac3)).add(vec3.mul(fac4)); |
| 1625 |
let inv2 = vec0.mul(fac1).sub(vec1.mul(fac3)).add(vec3.mul(fac5)); |
| 1626 |
let inv3 = vec0.mul(fac2).sub(vec1.mul(fac4)).add(vec2.mul(fac5)); |
| 1627 |
|
| 1628 |
let sign_a = {{ col_t }}::new(1.0, -1.0, 1.0, -1.0); |
| 1629 |
let sign_b = {{ col_t }}::new(-1.0, 1.0, -1.0, 1.0); |
| 1630 |
|
| 1631 |
let inverse = Self::from_cols( |
| 1632 |
inv0.mul(sign_a), |
| 1633 |
inv1.mul(sign_b), |
| 1634 |
inv2.mul(sign_a), |
| 1635 |
inv3.mul(sign_b), |
| 1636 |
); |
| 1637 |
|
| 1638 |
let col0 = {{ col_t }}::new( |
| 1639 |
inverse.x_axis.x, |
| 1640 |
inverse.y_axis.x, |
| 1641 |
inverse.z_axis.x, |
| 1642 |
inverse.w_axis.x, |
| 1643 |
); |
| 1644 |
|
| 1645 |
let dot0 = self.x_axis.mul(col0); |
| 1646 |
let dot1 = dot0.x + dot0.y + dot0.z + dot0.w; |
| 1647 |
|
| 1648 |
glam_assert!(dot1 != 0.0); |
| 1649 |
|
| 1650 |
let rcp_det = dot1.recip(); |
| 1651 |
inverse.mul(rcp_det) |
| 1652 |
{% endif %} |
| 1653 |
} |
| 1654 |
|
| 1655 |
{% if dim == 3 %} |
| 1656 |
/// Transforms the given 2D vector as a point. |
| 1657 |
/// |
| 1658 |
/// This is the equivalent of multiplying `rhs` as a 3D vector where `z` is `1`. |
| 1659 |
/// |
| 1660 |
/// This method assumes that `self` contains a valid affine transform. |
| 1661 |
/// |
| 1662 |
/// # Panics |
| 1663 |
/// |
| 1664 |
/// Will panic if the 2nd row of `self` is not `(0, 0, 1)` when `glam_assert` is enabled. |
| 1665 |
#[inline] |
| 1666 |
#[must_use] |
| 1667 |
pub fn transform_point2(&self, rhs: {{ vec2_t }}) -> {{ vec2_t }} { |
| 1668 |
glam_assert!(self.row(2).abs_diff_eq({{ col_t }}::Z, 1e-6)); |
| 1669 |
{{ mat2_t }}::from_cols(self.x_axis.xy(), self.y_axis.xy()) * rhs + self.z_axis.xy() |
| 1670 |
} |
| 1671 |
|
| 1672 |
/// Rotates the given 2D vector. |
| 1673 |
/// |
| 1674 |
/// This is the equivalent of multiplying `rhs` as a 3D vector where `z` is `0`. |
| 1675 |
/// |
| 1676 |
/// This method assumes that `self` contains a valid affine transform. |
| 1677 |
/// |
| 1678 |
/// # Panics |
| 1679 |
/// |
| 1680 |
/// Will panic if the 2nd row of `self` is not `(0, 0, 1)` when `glam_assert` is enabled. |
| 1681 |
#[inline] |
| 1682 |
#[must_use] |
| 1683 |
pub fn transform_vector2(&self, rhs: {{ vec2_t }}) -> {{ vec2_t }} { |
| 1684 |
glam_assert!(self.row(2).abs_diff_eq({{ col_t }}::Z, 1e-6)); |
| 1685 |
{{ mat2_t }}::from_cols(self.x_axis.xy(), self.y_axis.xy()) * rhs |
| 1686 |
} |
| 1687 |
|
| 1688 |
/// Creates a left-handed view matrix using a facing direction and an up direction. |
| 1689 |
/// |
| 1690 |
/// For a view coordinate system with `+X=right`, `+Y=up` and `+Z=forward`. |
| 1691 |
/// |
| 1692 |
/// # Panics |
| 1693 |
/// |
| 1694 |
/// Will panic if `dir` or `up` are not normalized when `glam_assert` is enabled. |
| 1695 |
#[inline] |
| 1696 |
#[must_use] |
| 1697 |
pub fn look_to_lh(dir: {{ vec3_t }}, up: {{ vec3_t }}) -> Self { |
| 1698 |
Self::look_to_rh(-dir, up) |
| 1699 |
} |
| 1700 |
|
| 1701 |
/// Creates a right-handed view matrix using a facing direction and an up direction. |
| 1702 |
/// |
| 1703 |
/// For a view coordinate system with `+X=right`, `+Y=up` and `+Z=back`. |
| 1704 |
/// |
| 1705 |
/// # Panics |
| 1706 |
/// |
| 1707 |
/// Will panic if `dir` or `up` are not normalized when `glam_assert` is enabled. |
| 1708 |
#[inline] |
| 1709 |
#[must_use] |
| 1710 |
pub fn look_to_rh(dir: {{ vec3_t }}, up: {{ vec3_t }}) -> Self { |
| 1711 |
glam_assert!(dir.is_normalized()); |
| 1712 |
glam_assert!(up.is_normalized()); |
| 1713 |
let f = dir; |
| 1714 |
let s = f.cross(up).normalize(); |
| 1715 |
let u = s.cross(f); |
| 1716 |
|
| 1717 |
Self::from_cols( |
| 1718 |
{{ col_t }}::new(s.x, u.x, -f.x), |
| 1719 |
{{ col_t }}::new(s.y, u.y, -f.y), |
| 1720 |
{{ col_t }}::new(s.z, u.z, -f.z), |
| 1721 |
) |
| 1722 |
} |
| 1723 |
|
| 1724 |
/// Creates a left-handed view matrix using a camera position, a focal point and an up |
| 1725 |
/// direction. |
| 1726 |
/// |
| 1727 |
/// For a view coordinate system with `+X=right`, `+Y=up` and `+Z=forward`. |
| 1728 |
/// |
| 1729 |
/// # Panics |
| 1730 |
/// |
| 1731 |
/// Will panic if `up` is not normalized when `glam_assert` is enabled. |
| 1732 |
#[inline] |
| 1733 |
#[must_use] |
| 1734 |
pub fn look_at_lh(eye: {{ vec3_t }}, center: {{ vec3_t }}, up: {{ vec3_t }}) -> Self { |
| 1735 |
Self::look_to_lh(center.sub(eye).normalize(), up) |
| 1736 |
} |
| 1737 |
|
| 1738 |
/// Creates a right-handed view matrix using a camera position, a focal point and an up |
| 1739 |
/// direction. |
| 1740 |
/// |
| 1741 |
/// For a view coordinate system with `+X=right`, `+Y=up` and `+Z=back`. |
| 1742 |
/// |
| 1743 |
/// # Panics |
| 1744 |
/// |
| 1745 |
/// Will panic if `up` is not normalized when `glam_assert` is enabled. |
| 1746 |
#[inline] |
| 1747 |
pub fn look_at_rh(eye: {{ vec3_t }}, center: {{ vec3_t }}, up: {{ vec3_t }}) -> Self { |
| 1748 |
Self::look_to_rh(center.sub(eye).normalize(), up) |
| 1749 |
} |
| 1750 |
|
| 1751 |
{% elif dim == 4 %} |
| 1752 |
/// Creates a left-handed view matrix using a camera position, a facing direction and an up |
| 1753 |
/// direction |
| 1754 |
/// |
| 1755 |
/// For a view coordinate system with `+X=right`, `+Y=up` and `+Z=forward`. |
| 1756 |
/// |
| 1757 |
/// # Panics |
| 1758 |
/// |
| 1759 |
/// Will panic if `dir` or `up` are not normalized when `glam_assert` is enabled. |
| 1760 |
#[inline] |
| 1761 |
#[must_use] |
| 1762 |
pub fn look_to_lh(eye: {{ vec3_t }}, dir: {{ vec3_t }}, up: {{ vec3_t }}) -> Self { |
| 1763 |
Self::look_to_rh(eye, -dir, up) |
| 1764 |
} |
| 1765 |
|
| 1766 |
/// Creates a right-handed view matrix using a camera position, a facing direction, and an up |
| 1767 |
/// direction. |
| 1768 |
/// |
| 1769 |
/// For a view coordinate system with `+X=right`, `+Y=up` and `+Z=back`. |
| 1770 |
/// |
| 1771 |
/// # Panics |
| 1772 |
/// |
| 1773 |
/// Will panic if `dir` or `up` are not normalized when `glam_assert` is enabled. |
| 1774 |
#[inline] |
| 1775 |
#[must_use] |
| 1776 |
pub fn look_to_rh(eye: {{ vec3_t }}, dir: {{ vec3_t }}, up: {{ vec3_t }}) -> Self { |
| 1777 |
glam_assert!(dir.is_normalized()); |
| 1778 |
glam_assert!(up.is_normalized()); |
| 1779 |
let f = dir; |
| 1780 |
let s = f.cross(up).normalize(); |
| 1781 |
let u = s.cross(f); |
| 1782 |
|
| 1783 |
Self::from_cols( |
| 1784 |
{{ col_t }}::new(s.x, u.x, -f.x, 0.0), |
| 1785 |
{{ col_t }}::new(s.y, u.y, -f.y, 0.0), |
| 1786 |
{{ col_t }}::new(s.z, u.z, -f.z, 0.0), |
| 1787 |
{{ col_t }}::new(-eye.dot(s), -eye.dot(u), eye.dot(f), 1.0), |
| 1788 |
) |
| 1789 |
} |
| 1790 |
|
| 1791 |
/// Creates a left-handed view matrix using a camera position, a focal points and an up |
| 1792 |
/// direction. |
| 1793 |
/// |
| 1794 |
/// For a view coordinate system with `+X=right`, `+Y=up` and `+Z=forward`. |
| 1795 |
/// |
| 1796 |
/// # Panics |
| 1797 |
/// |
| 1798 |
/// Will panic if `up` is not normalized when `glam_assert` is enabled. |
| 1799 |
#[inline] |
| 1800 |
#[must_use] |
| 1801 |
pub fn look_at_lh(eye: {{ vec3_t }}, center: {{ vec3_t }}, up: {{ vec3_t }}) -> Self { |
| 1802 |
Self::look_to_lh(eye, center.sub(eye).normalize(), up) |
| 1803 |
} |
| 1804 |
|
| 1805 |
/// Creates a right-handed view matrix using a camera position, a focal point, and an up |
| 1806 |
/// direction. |
| 1807 |
/// |
| 1808 |
/// For a view coordinate system with `+X=right`, `+Y=up` and `+Z=back`. |
| 1809 |
/// |
| 1810 |
/// # Panics |
| 1811 |
/// |
| 1812 |
/// Will panic if `up` is not normalized when `glam_assert` is enabled. |
| 1813 |
#[inline] |
| 1814 |
pub fn look_at_rh(eye: {{ vec3_t }}, center: {{ vec3_t }}, up: {{ vec3_t }}) -> Self { |
| 1815 |
Self::look_to_rh(eye, center.sub(eye).normalize(), up) |
| 1816 |
} |
| 1817 |
|
| 1818 |
/// Creates a right-handed perspective projection matrix with [-1,1] depth range. |
| 1819 |
/// |
| 1820 |
/// This is the same as the OpenGL `glFrustum` function. |
| 1821 |
/// |
| 1822 |
/// See <https://registry.khronos.org/OpenGL-Refpages/gl2.1/xhtml/glFrustum.xml> |
| 1823 |
#[inline] |
| 1824 |
#[must_use] |
| 1825 |
pub fn frustum_rh_gl( |
| 1826 |
left: {{ scalar_t }}, |
| 1827 |
right: {{ scalar_t }}, |
| 1828 |
bottom: {{ scalar_t }}, |
| 1829 |
top: {{ scalar_t }}, |
| 1830 |
z_near: {{ scalar_t }}, |
| 1831 |
z_far: {{ scalar_t }}, |
| 1832 |
) -> Self { |
| 1833 |
let inv_width = 1.0 / (right - left); |
| 1834 |
let inv_height = 1.0 / (top - bottom); |
| 1835 |
let inv_depth = 1.0 / (z_far - z_near); |
| 1836 |
let a = (right + left) * inv_width; |
| 1837 |
let b = (top + bottom) * inv_height; |
| 1838 |
let c = -(z_far + z_near) * inv_depth; |
| 1839 |
let d = -(2.0 * z_far * z_near) * inv_depth; |
| 1840 |
let two_z_near = 2.0 * z_near; |
| 1841 |
Self::from_cols( |
| 1842 |
{{ col_t }}::new(two_z_near * inv_width, 0.0, 0.0, 0.0), |
| 1843 |
{{ col_t }}::new(0.0, two_z_near * inv_height, 0.0, 0.0), |
| 1844 |
{{ col_t }}::new(a, b, c, -1.0), |
| 1845 |
{{ col_t }}::new(0.0, 0.0, d, 0.0), |
| 1846 |
) |
| 1847 |
} |
| 1848 |
|
| 1849 |
/// Creates a left-handed perspective projection matrix with `[0,1]` depth range. |
| 1850 |
/// |
| 1851 |
/// # Panics |
| 1852 |
/// |
| 1853 |
/// Will panic if `z_near` or `z_far` are less than or equal to zero when `glam_assert` is |
| 1854 |
/// enabled. |
| 1855 |
#[inline] |
| 1856 |
#[must_use] |
| 1857 |
pub fn frustum_lh( |
| 1858 |
left: {{ scalar_t }}, |
| 1859 |
right: {{ scalar_t }}, |
| 1860 |
bottom: {{ scalar_t }}, |
| 1861 |
top: {{ scalar_t }}, |
| 1862 |
z_near: {{ scalar_t }}, |
| 1863 |
z_far: {{ scalar_t }}, |
| 1864 |
) -> Self { |
| 1865 |
glam_assert!(z_near > 0.0 && z_far > 0.0); |
| 1866 |
let inv_width = 1.0 / (right - left); |
| 1867 |
let inv_height = 1.0 / (top - bottom); |
| 1868 |
let inv_depth = 1.0 / (z_far - z_near); |
| 1869 |
let a = (right + left) * inv_width; |
| 1870 |
let b = (top + bottom) * inv_height; |
| 1871 |
let c = z_far * inv_depth; |
| 1872 |
let d = -(z_far * z_near) * inv_depth; |
| 1873 |
let two_z_near = 2.0 * z_near; |
| 1874 |
Self::from_cols( |
| 1875 |
{{ col_t }}::new(two_z_near * inv_width, 0.0, 0.0, 0.0), |
| 1876 |
{{ col_t }}::new(0.0, two_z_near * inv_height, 0.0, 0.0), |
| 1877 |
{{ col_t }}::new(a, b, c, 1.0), |
| 1878 |
{{ col_t }}::new(0.0, 0.0, d, 0.0), |
| 1879 |
) |
| 1880 |
} |
| 1881 |
|
| 1882 |
/// Creates a right-handed perspective projection matrix with `[0,1]` depth range. |
| 1883 |
/// |
| 1884 |
/// # Panics |
| 1885 |
/// |
| 1886 |
/// Will panic if `z_near` or `z_far` are less than or equal to zero when `glam_assert` is |
| 1887 |
/// enabled. |
| 1888 |
#[inline] |
| 1889 |
#[must_use] |
| 1890 |
pub fn frustum_rh( |
| 1891 |
left: {{ scalar_t }}, |
| 1892 |
right: {{ scalar_t }}, |
| 1893 |
bottom: {{ scalar_t }}, |
| 1894 |
top: {{ scalar_t }}, |
| 1895 |
z_near: {{ scalar_t }}, |
| 1896 |
z_far: {{ scalar_t }}, |
| 1897 |
) -> Self { |
| 1898 |
glam_assert!(z_near > 0.0 && z_far > 0.0); |
| 1899 |
let inv_width = 1.0 / (right - left); |
| 1900 |
let inv_height = 1.0 / (top - bottom); |
| 1901 |
let inv_depth = 1.0 / (z_far - z_near); |
| 1902 |
let a = (right + left) * inv_width; |
| 1903 |
let b = (top + bottom) * inv_height; |
| 1904 |
let c = -z_far * inv_depth; |
| 1905 |
let d = -(z_far * z_near) * inv_depth; |
| 1906 |
let two_z_near = 2.0 * z_near; |
| 1907 |
Self::from_cols( |
| 1908 |
{{ col_t }}::new(two_z_near * inv_width, 0.0, 0.0, 0.0), |
| 1909 |
{{ col_t }}::new(0.0, two_z_near * inv_height, 0.0, 0.0), |
| 1910 |
{{ col_t }}::new(a, b, c, -1.0), |
| 1911 |
{{ col_t }}::new(0.0, 0.0, d, 0.0), |
| 1912 |
) |
| 1913 |
} |
| 1914 |
|
| 1915 |
/// Creates a right-handed perspective projection matrix with `[-1,1]` depth range. |
| 1916 |
/// |
| 1917 |
/// Useful to map the standard right-handed coordinate system into what OpenGL expects. |
| 1918 |
/// |
| 1919 |
/// This is the same as the OpenGL `gluPerspective` function. |
| 1920 |
/// See <https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/gluPerspective.xml> |
| 1921 |
#[inline] |
| 1922 |
#[must_use] |
| 1923 |
pub fn perspective_rh_gl( |
| 1924 |
fov_y_radians: {{ scalar_t }}, |
| 1925 |
aspect_ratio: {{ scalar_t }}, |
| 1926 |
z_near: {{ scalar_t }}, |
| 1927 |
z_far: {{ scalar_t }}, |
| 1928 |
) -> Self { |
| 1929 |
let inv_length = 1.0 / (z_near - z_far); |
| 1930 |
let f = 1.0 / math::tan(0.5 * fov_y_radians); |
| 1931 |
let a = f / aspect_ratio; |
| 1932 |
let b = (z_near + z_far) * inv_length; |
| 1933 |
let c = (2.0 * z_near * z_far) * inv_length; |
| 1934 |
Self::from_cols( |
| 1935 |
{{ col_t }}::new(a, 0.0, 0.0, 0.0), |
| 1936 |
{{ col_t }}::new(0.0, f, 0.0, 0.0), |
| 1937 |
{{ col_t }}::new(0.0, 0.0, b, -1.0), |
| 1938 |
{{ col_t }}::new(0.0, 0.0, c, 0.0), |
| 1939 |
) |
| 1940 |
} |
| 1941 |
|
| 1942 |
/// Creates a left-handed perspective projection matrix with `[0,1]` depth range. |
| 1943 |
/// |
| 1944 |
/// Useful to map the standard left-handed coordinate system into what WebGPU/Metal/Direct3D expect. |
| 1945 |
/// |
| 1946 |
/// # Panics |
| 1947 |
/// |
| 1948 |
/// Will panic if `z_near` or `z_far` are less than or equal to zero when `glam_assert` is |
| 1949 |
/// enabled. |
| 1950 |
#[inline] |
| 1951 |
#[must_use] |
| 1952 |
pub fn perspective_lh(fov_y_radians: {{ scalar_t }}, aspect_ratio: {{ scalar_t }}, z_near: {{ scalar_t }}, z_far: {{ scalar_t }}) -> Self { |
| 1953 |
glam_assert!(z_near > 0.0 && z_far > 0.0); |
| 1954 |
let (sin_fov, cos_fov) = math::sin_cos(0.5 * fov_y_radians); |
| 1955 |
let h = cos_fov / sin_fov; |
| 1956 |
let w = h / aspect_ratio; |
| 1957 |
let r = z_far / (z_far - z_near); |
| 1958 |
Self::from_cols( |
| 1959 |
{{ col_t }}::new(w, 0.0, 0.0, 0.0), |
| 1960 |
{{ col_t }}::new(0.0, h, 0.0, 0.0), |
| 1961 |
{{ col_t }}::new(0.0, 0.0, r, 1.0), |
| 1962 |
{{ col_t }}::new(0.0, 0.0, -r * z_near, 0.0), |
| 1963 |
) |
| 1964 |
} |
| 1965 |
|
| 1966 |
/// Creates a right-handed perspective projection matrix with `[0,1]` depth range. |
| 1967 |
/// |
| 1968 |
/// Useful to map the standard right-handed coordinate system into what WebGPU/Metal/Direct3D expect. |
| 1969 |
/// |
| 1970 |
/// # Panics |
| 1971 |
/// |
| 1972 |
/// Will panic if `z_near` or `z_far` are less than or equal to zero when `glam_assert` is |
| 1973 |
/// enabled. |
| 1974 |
#[inline] |
| 1975 |
#[must_use] |
| 1976 |
pub fn perspective_rh(fov_y_radians: {{ scalar_t }}, aspect_ratio: {{ scalar_t }}, z_near: {{ scalar_t }}, z_far: {{ scalar_t }}) -> Self { |
| 1977 |
glam_assert!(z_near > 0.0 && z_far > 0.0); |
| 1978 |
let (sin_fov, cos_fov) = math::sin_cos(0.5 * fov_y_radians); |
| 1979 |
let h = cos_fov / sin_fov; |
| 1980 |
let w = h / aspect_ratio; |
| 1981 |
let r = z_far / (z_near - z_far); |
| 1982 |
Self::from_cols( |
| 1983 |
{{ col_t }}::new(w, 0.0, 0.0, 0.0), |
| 1984 |
{{ col_t }}::new(0.0, h, 0.0, 0.0), |
| 1985 |
{{ col_t }}::new(0.0, 0.0, r, -1.0), |
| 1986 |
{{ col_t }}::new(0.0, 0.0, r * z_near, 0.0), |
| 1987 |
) |
| 1988 |
} |
| 1989 |
|
| 1990 |
/// Creates an infinite left-handed perspective projection matrix with `[0,1]` depth range. |
| 1991 |
/// |
| 1992 |
/// Like `perspective_lh`, but with an infinite value for `z_far`. |
| 1993 |
/// The result is that points near `z_near` are mapped to depth `0`, and as they move towards infinity the depth approaches `1`. |
| 1994 |
/// |
| 1995 |
/// # Panics |
| 1996 |
/// |
| 1997 |
/// Will panic if `z_near` or `z_far` are less than or equal to zero when `glam_assert` is |
| 1998 |
/// enabled. |
| 1999 |
#[inline] |
| 2000 |
#[must_use] |
| 2001 |
pub fn perspective_infinite_lh(fov_y_radians: {{ scalar_t }}, aspect_ratio: {{ scalar_t }}, z_near: {{ scalar_t }}) -> Self { |
| 2002 |
glam_assert!(z_near > 0.0); |
| 2003 |
let (sin_fov, cos_fov) = math::sin_cos(0.5 * fov_y_radians); |
| 2004 |
let h = cos_fov / sin_fov; |
| 2005 |
let w = h / aspect_ratio; |
| 2006 |
Self::from_cols( |
| 2007 |
{{ col_t }}::new(w, 0.0, 0.0, 0.0), |
| 2008 |
{{ col_t }}::new(0.0, h, 0.0, 0.0), |
| 2009 |
{{ col_t }}::new(0.0, 0.0, 1.0, 1.0), |
| 2010 |
{{ col_t }}::new(0.0, 0.0, -z_near, 0.0), |
| 2011 |
) |
| 2012 |
} |
| 2013 |
|
| 2014 |
/// Creates an infinite reverse left-handed perspective projection matrix with `[0,1]` depth range. |
| 2015 |
/// |
| 2016 |
/// Similar to `perspective_infinite_lh`, but maps `Z = z_near` to a depth of `1` and `Z = infinity` to a depth of `0`. |
| 2017 |
/// |
| 2018 |
/// # Panics |
| 2019 |
/// |
| 2020 |
/// Will panic if `z_near` is less than or equal to zero when `glam_assert` is enabled. |
| 2021 |
#[inline] |
| 2022 |
#[must_use] |
| 2023 |
pub fn perspective_infinite_reverse_lh( |
| 2024 |
fov_y_radians: {{ scalar_t }}, |
| 2025 |
aspect_ratio: {{ scalar_t }}, |
| 2026 |
z_near: {{ scalar_t }}, |
| 2027 |
) -> Self { |
| 2028 |
glam_assert!(z_near > 0.0); |
| 2029 |
let (sin_fov, cos_fov) = math::sin_cos(0.5 * fov_y_radians); |
| 2030 |
let h = cos_fov / sin_fov; |
| 2031 |
let w = h / aspect_ratio; |
| 2032 |
Self::from_cols( |
| 2033 |
{{ col_t }}::new(w, 0.0, 0.0, 0.0), |
| 2034 |
{{ col_t }}::new(0.0, h, 0.0, 0.0), |
| 2035 |
{{ col_t }}::new(0.0, 0.0, 0.0, 1.0), |
| 2036 |
{{ col_t }}::new(0.0, 0.0, z_near, 0.0), |
| 2037 |
) |
| 2038 |
} |
| 2039 |
|
| 2040 |
/// Creates an infinite right-handed perspective projection matrix with `[0,1]` depth range. |
| 2041 |
/// |
| 2042 |
/// Like `perspective_rh`, but with an infinite value for `z_far`. |
| 2043 |
/// The result is that points near `z_near` are mapped to depth `0`, and as they move towards infinity the depth approaches `1`. |
| 2044 |
/// |
| 2045 |
/// # Panics |
| 2046 |
/// |
| 2047 |
/// Will panic if `z_near` or `z_far` are less than or equal to zero when `glam_assert` is |
| 2048 |
/// enabled. |
| 2049 |
#[inline] |
| 2050 |
#[must_use] |
| 2051 |
pub fn perspective_infinite_rh(fov_y_radians: {{ scalar_t }}, aspect_ratio: {{ scalar_t }}, z_near: {{ scalar_t }}) -> Self { |
| 2052 |
glam_assert!(z_near > 0.0); |
| 2053 |
let f = 1.0 / math::tan(0.5 * fov_y_radians); |
| 2054 |
Self::from_cols( |
| 2055 |
{{ col_t }}::new(f / aspect_ratio, 0.0, 0.0, 0.0), |
| 2056 |
{{ col_t }}::new(0.0, f, 0.0, 0.0), |
| 2057 |
{{ col_t }}::new(0.0, 0.0, -1.0, -1.0), |
| 2058 |
{{ col_t }}::new(0.0, 0.0, -z_near, 0.0), |
| 2059 |
) |
| 2060 |
} |
| 2061 |
|
| 2062 |
/// Creates an infinite reverse right-handed perspective projection matrix with `[0,1]` depth range. |
| 2063 |
/// |
| 2064 |
/// Similar to `perspective_infinite_rh`, but maps `Z = z_near` to a depth of `1` and `Z = infinity` to a depth of `0`. |
| 2065 |
/// |
| 2066 |
/// # Panics |
| 2067 |
/// |
| 2068 |
/// Will panic if `z_near` is less than or equal to zero when `glam_assert` is enabled. |
| 2069 |
#[inline] |
| 2070 |
#[must_use] |
| 2071 |
pub fn perspective_infinite_reverse_rh( |
| 2072 |
fov_y_radians: {{ scalar_t }}, |
| 2073 |
aspect_ratio: {{ scalar_t }}, |
| 2074 |
z_near: {{ scalar_t }}, |
| 2075 |
) -> Self { |
| 2076 |
glam_assert!(z_near > 0.0); |
| 2077 |
let f = 1.0 / math::tan(0.5 * fov_y_radians); |
| 2078 |
Self::from_cols( |
| 2079 |
{{ col_t }}::new(f / aspect_ratio, 0.0, 0.0, 0.0), |
| 2080 |
{{ col_t }}::new(0.0, f, 0.0, 0.0), |
| 2081 |
{{ col_t }}::new(0.0, 0.0, 0.0, -1.0), |
| 2082 |
{{ col_t }}::new(0.0, 0.0, z_near, 0.0), |
| 2083 |
) |
| 2084 |
} |
| 2085 |
|
| 2086 |
/// Creates a right-handed orthographic projection matrix with `[-1,1]` depth |
| 2087 |
/// range. This is the same as the OpenGL `glOrtho` function in OpenGL. |
| 2088 |
/// See |
| 2089 |
/// <https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glOrtho.xml> |
| 2090 |
/// |
| 2091 |
/// Useful to map a right-handed coordinate system to the normalized device coordinates that OpenGL expects. |
| 2092 |
#[inline] |
| 2093 |
#[must_use] |
| 2094 |
pub fn orthographic_rh_gl( |
| 2095 |
left: {{ scalar_t }}, |
| 2096 |
right: {{ scalar_t }}, |
| 2097 |
bottom: {{ scalar_t }}, |
| 2098 |
top: {{ scalar_t }}, |
| 2099 |
near: {{ scalar_t }}, |
| 2100 |
far: {{ scalar_t }}, |
| 2101 |
) -> Self { |
| 2102 |
let a = 2.0 / (right - left); |
| 2103 |
let b = 2.0 / (top - bottom); |
| 2104 |
let c = -2.0 / (far - near); |
| 2105 |
let tx = -(right + left) / (right - left); |
| 2106 |
let ty = -(top + bottom) / (top - bottom); |
| 2107 |
let tz = -(far + near) / (far - near); |
| 2108 |
|
| 2109 |
Self::from_cols( |
| 2110 |
{{ col_t }}::new(a, 0.0, 0.0, 0.0), |
| 2111 |
{{ col_t }}::new(0.0, b, 0.0, 0.0), |
| 2112 |
{{ col_t }}::new(0.0, 0.0, c, 0.0), |
| 2113 |
{{ col_t }}::new(tx, ty, tz, 1.0), |
| 2114 |
) |
| 2115 |
} |
| 2116 |
|
| 2117 |
/// Creates a left-handed orthographic projection matrix with `[0,1]` depth range. |
| 2118 |
/// |
| 2119 |
/// Useful to map a left-handed coordinate system to the normalized device coordinates that WebGPU/Direct3D/Metal expect. |
| 2120 |
#[inline] |
| 2121 |
#[must_use] |
| 2122 |
pub fn orthographic_lh( |
| 2123 |
left: {{ scalar_t }}, |
| 2124 |
right: {{ scalar_t }}, |
| 2125 |
bottom: {{ scalar_t }}, |
| 2126 |
top: {{ scalar_t }}, |
| 2127 |
near: {{ scalar_t }}, |
| 2128 |
far: {{ scalar_t }}, |
| 2129 |
) -> Self { |
| 2130 |
let rcp_width = 1.0 / (right - left); |
| 2131 |
let rcp_height = 1.0 / (top - bottom); |
| 2132 |
let r = 1.0 / (far - near); |
| 2133 |
Self::from_cols( |
| 2134 |
{{ col_t }}::new(rcp_width + rcp_width, 0.0, 0.0, 0.0), |
| 2135 |
{{ col_t }}::new(0.0, rcp_height + rcp_height, 0.0, 0.0), |
| 2136 |
{{ col_t }}::new(0.0, 0.0, r, 0.0), |
| 2137 |
{{ col_t }}::new( |
| 2138 |
-(left + right) * rcp_width, |
| 2139 |
-(top + bottom) * rcp_height, |
| 2140 |
-r * near, |
| 2141 |
1.0, |
| 2142 |
), |
| 2143 |
) |
| 2144 |
} |
| 2145 |
|
| 2146 |
/// Creates a right-handed orthographic projection matrix with `[0,1]` depth range. |
| 2147 |
/// |
| 2148 |
/// Useful to map a right-handed coordinate system to the normalized device coordinates that WebGPU/Direct3D/Metal expect. |
| 2149 |
#[inline] |
| 2150 |
#[must_use] |
| 2151 |
pub fn orthographic_rh( |
| 2152 |
left: {{ scalar_t }}, |
| 2153 |
right: {{ scalar_t }}, |
| 2154 |
bottom: {{ scalar_t }}, |
| 2155 |
top: {{ scalar_t }}, |
| 2156 |
near: {{ scalar_t }}, |
| 2157 |
far: {{ scalar_t }}, |
| 2158 |
) -> Self { |
| 2159 |
let rcp_width = 1.0 / (right - left); |
| 2160 |
let rcp_height = 1.0 / (top - bottom); |
| 2161 |
let r = 1.0 / (near - far); |
| 2162 |
Self::from_cols( |
| 2163 |
{{ col_t }}::new(rcp_width + rcp_width, 0.0, 0.0, 0.0), |
| 2164 |
{{ col_t }}::new(0.0, rcp_height + rcp_height, 0.0, 0.0), |
| 2165 |
{{ col_t }}::new(0.0, 0.0, r, 0.0), |
| 2166 |
{{ col_t }}::new( |
| 2167 |
-(left + right) * rcp_width, |
| 2168 |
-(top + bottom) * rcp_height, |
| 2169 |
r * near, |
| 2170 |
1.0, |
| 2171 |
), |
| 2172 |
) |
| 2173 |
} |
| 2174 |
|
| 2175 |
/// Transforms the given 3D vector as a point, applying perspective correction. |
| 2176 |
/// |
| 2177 |
/// This is the equivalent of multiplying the 3D vector as a 4D vector where `w` is `1.0`. |
| 2178 |
/// The perspective divide is performed meaning the resulting 3D vector is divided by `w`. |
| 2179 |
/// |
| 2180 |
/// This method assumes that `self` contains a projective transform. |
| 2181 |
#[inline] |
| 2182 |
#[must_use] |
| 2183 |
pub fn project_point3(&self, rhs: {{ vec3_t }}) -> {{ vec3_t }} { |
| 2184 |
let mut res = self.x_axis.mul(rhs.x); |
| 2185 |
res = self.y_axis.mul(rhs.y).add(res); |
| 2186 |
res = self.z_axis.mul(rhs.z).add(res); |
| 2187 |
res = self.w_axis.add(res); |
| 2188 |
res = res.div(res.w); |
| 2189 |
res.xyz() |
| 2190 |
} |
| 2191 |
|
| 2192 |
/// Transforms the given 3D vector as a point. |
| 2193 |
/// |
| 2194 |
/// This is the equivalent of multiplying the 3D vector as a 4D vector where `w` is |
| 2195 |
/// `1.0`. |
| 2196 |
/// |
| 2197 |
/// This method assumes that `self` contains a valid affine transform. It does not perform |
| 2198 |
/// a perspective divide, if `self` contains a perspective transform, or if you are unsure, |
| 2199 |
/// the [`Self::project_point3()`] method should be used instead. |
| 2200 |
/// |
| 2201 |
/// # Panics |
| 2202 |
/// |
| 2203 |
/// Will panic if the 3rd row of `self` is not `(0, 0, 0, 1)` when `glam_assert` is enabled. |
| 2204 |
#[inline] |
| 2205 |
#[must_use] |
| 2206 |
pub fn transform_point3(&self, rhs: {{ vec3_t }}) -> {{ vec3_t }} { |
| 2207 |
glam_assert!(self.row(3).abs_diff_eq({{ vec4_t }}::W, 1e-6)); |
| 2208 |
let mut res = self.x_axis.mul(rhs.x); |
| 2209 |
res = self.y_axis.mul(rhs.y).add(res); |
| 2210 |
res = self.z_axis.mul(rhs.z).add(res); |
| 2211 |
res = self.w_axis.add(res); |
| 2212 |
res.xyz() |
| 2213 |
} |
| 2214 |
|
| 2215 |
/// Transforms the give 3D vector as a direction. |
| 2216 |
/// |
| 2217 |
/// This is the equivalent of multiplying the 3D vector as a 4D vector where `w` is |
| 2218 |
/// `0.0`. |
| 2219 |
/// |
| 2220 |
/// This method assumes that `self` contains a valid affine transform. |
| 2221 |
/// |
| 2222 |
/// # Panics |
| 2223 |
/// |
| 2224 |
/// Will panic if the 3rd row of `self` is not `(0, 0, 0, 1)` when `glam_assert` is enabled. |
| 2225 |
#[inline] |
| 2226 |
#[must_use] |
| 2227 |
pub fn transform_vector3(&self, rhs: {{ vec3_t }}) -> {{ vec3_t }} { |
| 2228 |
glam_assert!(self.row(3).abs_diff_eq({{ vec4_t }}::W, 1e-6)); |
| 2229 |
let mut res = self.x_axis.mul(rhs.x); |
| 2230 |
res = self.y_axis.mul(rhs.y).add(res); |
| 2231 |
res = self.z_axis.mul(rhs.z).add(res); |
| 2232 |
res.xyz() |
| 2233 |
} |
| 2234 |
|
| 2235 |
{% endif %} |
| 2236 |
|
| 2237 |
{% if self_t == "Mat4" %} |
| 2238 |
/// Transforms the given [`Vec3A`] as a 3D point, applying perspective correction. |
| 2239 |
/// |
| 2240 |
/// This is the equivalent of multiplying the [`Vec3A`] as a 4D vector where `w` is `1.0`. |
| 2241 |
/// The perspective divide is performed meaning the resulting 3D vector is divided by `w`. |
| 2242 |
/// |
| 2243 |
/// This method assumes that `self` contains a projective transform. |
| 2244 |
#[inline] |
| 2245 |
#[must_use] |
| 2246 |
pub fn project_point3a(&self, rhs: Vec3A) -> Vec3A { |
| 2247 |
{% if is_scalar %} |
| 2248 |
self.project_point3(rhs.into()).into() |
| 2249 |
{% else %} |
| 2250 |
let mut res = self.x_axis.mul(rhs.xxxx()); |
| 2251 |
res = self.y_axis.mul(rhs.yyyy()).add(res); |
| 2252 |
res = self.z_axis.mul(rhs.zzzz()).add(res); |
| 2253 |
res = self.w_axis.add(res); |
| 2254 |
res = res.div(res.wwww()); |
| 2255 |
Vec3A::from_vec4(res) |
| 2256 |
{% endif %} |
| 2257 |
} |
| 2258 |
|
| 2259 |
/// Transforms the given [`Vec3A`] as 3D point. |
| 2260 |
/// |
| 2261 |
/// This is the equivalent of multiplying the [`Vec3A`] as a 4D vector where `w` is `1.0`. |
| 2262 |
#[inline] |
| 2263 |
#[must_use] |
| 2264 |
pub fn transform_point3a(&self, rhs: Vec3A) -> Vec3A { |
| 2265 |
{% if is_scalar %} |
| 2266 |
self.transform_point3(rhs.into()).into() |
| 2267 |
{% else %} |
| 2268 |
glam_assert!(self.row(3).abs_diff_eq({{ vec4_t }}::W, 1e-6)); |
| 2269 |
let mut res = self.x_axis.mul(rhs.xxxx()); |
| 2270 |
res = self.y_axis.mul(rhs.yyyy()).add(res); |
| 2271 |
res = self.z_axis.mul(rhs.zzzz()).add(res); |
| 2272 |
res = self.w_axis.add(res); |
| 2273 |
Vec3A::from_vec4(res) |
| 2274 |
{% endif %} |
| 2275 |
} |
| 2276 |
|
| 2277 |
/// Transforms the give [`Vec3A`] as 3D vector. |
| 2278 |
/// |
| 2279 |
/// This is the equivalent of multiplying the [`Vec3A`] as a 4D vector where `w` is `0.0`. |
| 2280 |
#[inline] |
| 2281 |
#[must_use] |
| 2282 |
pub fn transform_vector3a(&self, rhs: Vec3A) -> Vec3A { |
| 2283 |
{% if is_scalar %} |
| 2284 |
self.transform_vector3(rhs.into()).into() |
| 2285 |
{% else %} |
| 2286 |
glam_assert!(self.row(3).abs_diff_eq({{ vec4_t }}::W, 1e-6)); |
| 2287 |
let mut res = self.x_axis.mul(rhs.xxxx()); |
| 2288 |
res = self.y_axis.mul(rhs.yyyy()).add(res); |
| 2289 |
res = self.z_axis.mul(rhs.zzzz()).add(res); |
| 2290 |
Vec3A::from_vec4(res) |
| 2291 |
{% endif %} |
| 2292 |
} |
| 2293 |
{% endif %} |
| 2294 |
|
| 2295 |
/// Transforms a {{ dim }}D vector. |
| 2296 |
#[inline] |
| 2297 |
#[must_use] |
| 2298 |
pub fn mul_vec{{ dim }}(&self, rhs: {{ vecn_t }}) -> {{ vecn_t }} { |
| 2299 |
{% if self_t == "Mat2" and is_sse2 %} |
| 2300 |
unsafe { |
| 2301 |
use core::mem::MaybeUninit; |
| 2302 |
use crate::Align16; |
| 2303 |
let abcd = self.0; |
| 2304 |
let xxyy = _mm_set_ps(rhs.y, rhs.y, rhs.x, rhs.x); |
| 2305 |
let axbxcydy = _mm_mul_ps(abcd, xxyy); |
| 2306 |
let cydyaxbx = _mm_shuffle_ps(axbxcydy, axbxcydy, 0b01_00_11_10); |
| 2307 |
let result = _mm_add_ps(axbxcydy, cydyaxbx); |
| 2308 |
let mut out: MaybeUninit<Align16<Vec2>> = MaybeUninit::uninit(); |
| 2309 |
_mm_store_ps(out.as_mut_ptr().cast(), result); |
| 2310 |
out.assume_init().0 |
| 2311 |
} |
| 2312 |
{% elif self_t == "Mat2" and is_wasm32 %} |
| 2313 |
use core::mem::MaybeUninit; |
| 2314 |
let abcd = self.0; |
| 2315 |
let xxyy = f32x4(rhs.x, rhs.x, rhs.y, rhs.y); |
| 2316 |
let axbxcydy = f32x4_mul(abcd, xxyy); |
| 2317 |
let cydyaxbx = i32x4_shuffle::<2, 3, 4, 5>(axbxcydy, axbxcydy); |
| 2318 |
let result = f32x4_add(axbxcydy, cydyaxbx); |
| 2319 |
let mut out: MaybeUninit<v128> = MaybeUninit::uninit(); |
| 2320 |
unsafe { |
| 2321 |
v128_store(out.as_mut_ptr(), result); |
| 2322 |
*(&out.assume_init() as *const v128 as *const Vec2) |
| 2323 |
} |
| 2324 |
{% elif self_t == "Mat2" and is_coresimd %} |
| 2325 |
let abcd = self.0; |
| 2326 |
let xxyy = f32x4::from_array([rhs.x, rhs.x, rhs.y, rhs.y]); |
| 2327 |
let axbxcydy = abcd.mul(xxyy); |
| 2328 |
let cydyaxbx = simd_swizzle!(axbxcydy, [2, 3, 0, 1]); |
| 2329 |
let result = axbxcydy.add(cydyaxbx); |
| 2330 |
unsafe { *(&result as *const f32x4 as *const Vec2) } |
| 2331 |
{% elif self_t == "Mat2" and is_neon %} |
| 2332 |
unsafe { |
| 2333 |
let abcd = self.0; |
| 2334 |
let xxyy = vld1q_f32([rhs.x, rhs.x, rhs.y, rhs.y].as_ptr()); |
| 2335 |
let axbxcydy = vmulq_f32(abcd, xxyy); |
| 2336 |
// let cydyaxbx = simd_swizzle!(axbxcydy, [2, 3, 0, 1]); |
| 2337 |
let cydyaxbx = vextq_f32(axbxcydy, axbxcydy, 2); |
| 2338 |
let result = vaddq_f32(axbxcydy, cydyaxbx); |
| 2339 |
*(&result as *const float32x4_t as *const Vec2) |
| 2340 |
} |
| 2341 |
{% elif dim == 2 %} |
| 2342 |
#[allow(clippy::suspicious_operation_groupings)] |
| 2343 |
{{ col_t }}::new( |
| 2344 |
(self.x_axis.x * rhs.x) + (self.y_axis.x * rhs.y), |
| 2345 |
(self.x_axis.y * rhs.x) + (self.y_axis.y * rhs.y), |
| 2346 |
) |
| 2347 |
{% elif self_t == "Mat3A" %} |
| 2348 |
{# use the Vec3A implementation #} |
| 2349 |
self.mul_vec3a(rhs.into()).into() |
| 2350 |
{% elif dim == 3 %} |
| 2351 |
let mut res = self.x_axis.mul(rhs.x); |
| 2352 |
res = res.add(self.y_axis.mul(rhs.y)); |
| 2353 |
res = res.add(self.z_axis.mul(rhs.z)); |
| 2354 |
res |
| 2355 |
{% elif dim == 4 %} |
| 2356 |
{% if is_scalar %} |
| 2357 |
let mut res = self.x_axis.mul(rhs.x); |
| 2358 |
res = res.add(self.y_axis.mul(rhs.y)); |
| 2359 |
res = res.add(self.z_axis.mul(rhs.z)); |
| 2360 |
res = res.add(self.w_axis.mul(rhs.w)); |
| 2361 |
res |
| 2362 |
{% else %} |
| 2363 |
{# use swizzles if simd #} |
| 2364 |
let mut res = self.x_axis.mul(rhs.xxxx()); |
| 2365 |
res = res.add(self.y_axis.mul(rhs.yyyy())); |
| 2366 |
res = res.add(self.z_axis.mul(rhs.zzzz())); |
| 2367 |
res = res.add(self.w_axis.mul(rhs.wwww())); |
| 2368 |
res |
| 2369 |
{% endif %} |
| 2370 |
{% endif %} |
| 2371 |
} |
| 2372 |
|
| 2373 |
{% if self_t == "Mat3" %} |
| 2374 |
/// Transforms a [`Vec3A`]. |
| 2375 |
#[inline] |
| 2376 |
#[must_use] |
| 2377 |
pub fn mul_vec3a(&self, rhs: Vec3A) -> Vec3A { |
| 2378 |
self.mul_vec3(rhs.into()).into() |
| 2379 |
} |
| 2380 |
{% elif self_t == "Mat3A" %} |
| 2381 |
/// Transforms a [`Vec3A`]. |
| 2382 |
#[inline] |
| 2383 |
#[must_use] |
| 2384 |
pub fn mul_vec3a(&self, rhs: Vec3A) -> Vec3A { |
| 2385 |
let mut res = self.x_axis.mul(rhs.xxx()); |
| 2386 |
res = res.add(self.y_axis.mul(rhs.yyy())); |
| 2387 |
res = res.add(self.z_axis.mul(rhs.zzz())); |
| 2388 |
res |
| 2389 |
} |
| 2390 |
{% endif %} |
| 2391 |
|
| 2392 |
/// Multiplies two {{ nxn }} matrices. |
| 2393 |
#[inline] |
| 2394 |
#[must_use] |
| 2395 |
pub fn mul_mat{{ dim }}(&self, rhs: &Self) -> Self { |
| 2396 |
self.mul(rhs) |
| 2397 |
} |
| 2398 |
|
| 2399 |
/// Adds two {{ nxn }} matrices. |
| 2400 |
#[inline] |
| 2401 |
#[must_use] |
| 2402 |
pub fn add_mat{{ dim }}(&self, rhs: &Self) -> Self { |
| 2403 |
self.add(rhs) |
| 2404 |
} |
| 2405 |
|
| 2406 |
/// Subtracts two {{ nxn }} matrices. |
| 2407 |
#[inline] |
| 2408 |
#[must_use] |
| 2409 |
pub fn sub_mat{{ dim }}(&self, rhs: &Self) -> Self { |
| 2410 |
self.sub(rhs) |
| 2411 |
} |
| 2412 |
|
| 2413 |
/// Multiplies a {{ nxn }} matrix by a scalar. |
| 2414 |
#[inline] |
| 2415 |
#[must_use] |
| 2416 |
pub fn mul_scalar(&self, rhs: {{ scalar_t }}) -> Self { |
| 2417 |
{% if self_t == "Mat2" and is_sse2 %} |
| 2418 |
Self(unsafe { _mm_mul_ps(self.0, _mm_set_ps1(rhs)) }) |
| 2419 |
{% elif self_t == "Mat2" and is_wasm32 %} |
| 2420 |
Self(f32x4_mul(self.0, f32x4_splat(rhs))) |
| 2421 |
{% elif self_t == "Mat2" and is_coresimd %} |
| 2422 |
Self(self.0 * f32x4::splat(rhs)) |
| 2423 |
{% elif self_t == "Mat2" and is_neon %} |
| 2424 |
Self(unsafe { vmulq_f32(self.0, vld1q_dup_f32(&rhs)) }) |
| 2425 |
{% else %} |
| 2426 |
Self::from_cols( |
| 2427 |
{% for axis in axes %} |
| 2428 |
self.{{ axis }}.mul(rhs), |
| 2429 |
{%- endfor %} |
| 2430 |
) |
| 2431 |
{% endif %} |
| 2432 |
} |
| 2433 |
|
| 2434 |
/// Divides a {{ nxn }} matrix by a scalar. |
| 2435 |
#[inline] |
| 2436 |
#[must_use] |
| 2437 |
pub fn div_scalar(&self, rhs: {{ scalar_t }}) -> Self { |
| 2438 |
{% if self_t == "Mat2" and is_sse2 %} |
| 2439 |
Self(unsafe { _mm_div_ps(self.0, _mm_set_ps1(rhs)) }) |
| 2440 |
{% elif self_t == "Mat2" and is_wasm32 %} |
| 2441 |
Self(f32x4_div(self.0, f32x4_splat(rhs))) |
| 2442 |
{% elif self_t == "Mat2" and is_coresimd %} |
| 2443 |
Self(self.0 / f32x4::splat(rhs)) |
| 2444 |
{% elif self_t == "Mat2" and is_neon %} |
| 2445 |
Self(unsafe { vdivq_f32(self.0, vld1q_dup_f32(&rhs)) }) |
| 2446 |
{% else %} |
| 2447 |
let rhs = {{ col_t }}::splat(rhs); |
| 2448 |
Self::from_cols( |
| 2449 |
{% for axis in axes %} |
| 2450 |
self.{{ axis }}.div(rhs), |
| 2451 |
{%- endfor %} |
| 2452 |
) |
| 2453 |
{% endif %} |
| 2454 |
} |
| 2455 |
|
| 2456 |
/// Returns true if the absolute difference of all elements between `self` and `rhs` |
| 2457 |
/// is less than or equal to `max_abs_diff`. |
| 2458 |
/// |
| 2459 |
/// This can be used to compare if two matrices contain similar elements. It works best |
| 2460 |
/// when comparing with a known value. The `max_abs_diff` that should be used used |
| 2461 |
/// depends on the values being compared against. |
| 2462 |
/// |
| 2463 |
/// For more see |
| 2464 |
/// [comparing floating point numbers](https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/). |
| 2465 |
#[inline] |
| 2466 |
#[must_use] |
| 2467 |
pub fn abs_diff_eq(&self, rhs: Self, max_abs_diff: {{ scalar_t }}) -> bool { |
| 2468 |
{% for axis in axes %} |
| 2469 |
self.{{ axis }}.abs_diff_eq(rhs.{{ axis }}, max_abs_diff) |
| 2470 |
{% if not loop.last %} && {% endif %} |
| 2471 |
{% endfor %} |
| 2472 |
} |
| 2473 |
|
| 2474 |
/// Takes the absolute value of each element in `self` |
| 2475 |
#[inline] |
| 2476 |
#[must_use] |
| 2477 |
pub fn abs(&self) -> Self { |
| 2478 |
Self::from_cols( |
| 2479 |
{% for axis in axes %} |
| 2480 |
self.{{ axis }}.abs(), |
| 2481 |
{% endfor %} |
| 2482 |
) |
| 2483 |
} |
| 2484 |
|
| 2485 |
{% if scalar_t == "f32" %} |
| 2486 |
#[inline] |
| 2487 |
pub fn as_dmat{{ dim }}(&self) -> DMat{{ dim }} { |
| 2488 |
DMat{{ dim }}::from_cols( |
| 2489 |
{% for axis in axes %} |
| 2490 |
self.{{ axis }}.as_dvec{{ dim }}(), |
| 2491 |
{% endfor %} |
| 2492 |
) |
| 2493 |
} |
| 2494 |
{% elif scalar_t == "f64" %} |
| 2495 |
#[inline] |
| 2496 |
pub fn as_mat{{ dim }}(&self) -> Mat{{ dim }} { |
| 2497 |
Mat{{ dim }}::from_cols( |
| 2498 |
{% for axis in axes %} |
| 2499 |
self.{{ axis }}.as_vec{{ dim }}(), |
| 2500 |
{% endfor %} |
| 2501 |
) |
| 2502 |
} |
| 2503 |
{% endif %} |
| 2504 |
} |
| 2505 |
|
| 2506 |
impl Default for {{ self_t }} { |
| 2507 |
#[inline] |
| 2508 |
fn default() -> Self { |
| 2509 |
Self::IDENTITY |
| 2510 |
} |
| 2511 |
} |
| 2512 |
|
| 2513 |
impl Add for {{ self_t }} { |
| 2514 |
type Output = Self; |
| 2515 |
#[inline] |
| 2516 |
fn add(self, rhs: Self) -> Self { |
| 2517 |
{% if self_t == "Mat2" and is_sse2 %} |
| 2518 |
Self(unsafe { _mm_add_ps(self.0, rhs.0) }) |
| 2519 |
{% elif self_t == "Mat2" and is_wasm32 %} |
| 2520 |
Self(f32x4_add(self.0, rhs.0)) |
| 2521 |
{% elif self_t == "Mat2" and is_coresimd %} |
| 2522 |
Self(self.0 + rhs.0) |
| 2523 |
{% elif self_t == "Mat2" and is_neon %} |
| 2524 |
Self(unsafe { vaddq_f32(self.0, rhs.0) }) |
| 2525 |
{% else %} |
| 2526 |
Self::from_cols( |
| 2527 |
{% for axis in axes %} |
| 2528 |
self.{{ axis }}.add(rhs.{{ axis }}), |
| 2529 |
{%- endfor %} |
| 2530 |
) |
| 2531 |
{% endif %} |
| 2532 |
} |
| 2533 |
} |
| 2534 |
|
| 2535 |
{{ macros::impl_ref_binop(imp="Add", self_t=self_t, rhs_t=self_t, output_t=self_t) }} |
| 2536 |
|
| 2537 |
{{ macros::impl_op_assign(imp="Add", self_t=self_t, rhs_t=self_t) }} |
| 2538 |
|
| 2539 |
impl Sub for {{ self_t }} { |
| 2540 |
type Output = Self; |
| 2541 |
#[inline] |
| 2542 |
fn sub(self, rhs: Self) -> Self { |
| 2543 |
{% if self_t == "Mat2" and is_sse2 %} |
| 2544 |
Self(unsafe { _mm_sub_ps(self.0, rhs.0) }) |
| 2545 |
{% elif self_t == "Mat2" and is_wasm32 %} |
| 2546 |
Self(f32x4_sub(self.0, rhs.0)) |
| 2547 |
{% elif self_t == "Mat2" and is_coresimd %} |
| 2548 |
Self(self.0 - rhs.0) |
| 2549 |
{% elif self_t == "Mat2" and is_neon %} |
| 2550 |
Self(unsafe { vsubq_f32(self.0, rhs.0) }) |
| 2551 |
{% else %} |
| 2552 |
Self::from_cols( |
| 2553 |
{% for axis in axes %} |
| 2554 |
self.{{ axis }}.sub(rhs.{{ axis }}), |
| 2555 |
{%- endfor %} |
| 2556 |
) |
| 2557 |
{% endif %} |
| 2558 |
} |
| 2559 |
} |
| 2560 |
|
| 2561 |
{{ macros::impl_ref_binop(imp="Sub", self_t=self_t, rhs_t=self_t, output_t=self_t) }} |
| 2562 |
|
| 2563 |
{{ macros::impl_op_assign(imp="Sub", self_t=self_t, rhs_t=self_t) }} |
| 2564 |
|
| 2565 |
impl Neg for {{ self_t }} { |
| 2566 |
type Output = Self; |
| 2567 |
#[inline] |
| 2568 |
fn neg(self) -> Self::Output { |
| 2569 |
{% if self_t == "Mat2" and is_sse2 %} |
| 2570 |
Self(unsafe { _mm_xor_ps(self.0, _mm_set1_ps(-0.0)) }) |
| 2571 |
{% elif self_t == "Mat2" and is_wasm32 %} |
| 2572 |
Self(f32x4_neg(self.0)) |
| 2573 |
{% elif self_t == "Mat2" and is_coresimd %} |
| 2574 |
Self(-self.0) |
| 2575 |
{% elif self_t == "Mat2" and is_neon %} |
| 2576 |
Self(unsafe { vnegq_f32(self.0) }) |
| 2577 |
{% else %} |
| 2578 |
Self::from_cols( |
| 2579 |
{% for axis in axes %} |
| 2580 |
self.{{ axis }}.neg(), |
| 2581 |
{%- endfor %} |
| 2582 |
) |
| 2583 |
{% endif %} |
| 2584 |
} |
| 2585 |
} |
| 2586 |
|
| 2587 |
{{ macros::impl_ref_unop(imp="Neg", self_t=self_t, output_t=self_t) }} |
| 2588 |
|
| 2589 |
impl Mul for {{ self_t }} { |
| 2590 |
type Output = Self; |
| 2591 |
#[inline] |
| 2592 |
fn mul(self, rhs: Self) -> Self { |
| 2593 |
{% if self_t == "Mat2" and is_sse2 %} |
| 2594 |
unsafe { |
| 2595 |
let abcd = self.0; |
| 2596 |
let rhs = rhs.0; |
| 2597 |
let xxyy0 = _mm_shuffle_ps(rhs, rhs, 0b01_01_00_00); |
| 2598 |
let xxyy1 = _mm_shuffle_ps(rhs, rhs, 0b11_11_10_10); |
| 2599 |
let axbxcydy0 = _mm_mul_ps(abcd, xxyy0); |
| 2600 |
let axbxcydy1 = _mm_mul_ps(abcd, xxyy1); |
| 2601 |
let cydyaxbx0 = _mm_shuffle_ps(axbxcydy0, axbxcydy0, 0b01_00_11_10); |
| 2602 |
let cydyaxbx1 = _mm_shuffle_ps(axbxcydy1, axbxcydy1, 0b01_00_11_10); |
| 2603 |
let result0 = _mm_add_ps(axbxcydy0, cydyaxbx0); |
| 2604 |
let result1 = _mm_add_ps(axbxcydy1, cydyaxbx1); |
| 2605 |
Self(_mm_shuffle_ps(result0, result1, 0b01_00_01_00)) |
| 2606 |
} |
| 2607 |
{% elif self_t == "Mat2" and is_wasm32 %} |
| 2608 |
let abcd = self.0; |
| 2609 |
let rhs = rhs.0; |
| 2610 |
let xxyy0 = i32x4_shuffle::<0, 0, 5, 5>(rhs, rhs); |
| 2611 |
let xxyy1 = i32x4_shuffle::<2, 2, 7, 7>(rhs, rhs); |
| 2612 |
let axbxcydy0 = f32x4_mul(abcd, xxyy0); |
| 2613 |
let axbxcydy1 = f32x4_mul(abcd, xxyy1); |
| 2614 |
let cydyaxbx0 = i32x4_shuffle::<2, 3, 4, 5>(axbxcydy0, axbxcydy0); |
| 2615 |
let cydyaxbx1 = i32x4_shuffle::<2, 3, 4, 5>(axbxcydy1, axbxcydy1); |
| 2616 |
let result0 = f32x4_add(axbxcydy0, cydyaxbx0); |
| 2617 |
let result1 = f32x4_add(axbxcydy1, cydyaxbx1); |
| 2618 |
Self(i32x4_shuffle::<0, 1, 4, 5>(result0, result1)) |
| 2619 |
{% elif self_t == "Mat2" and is_coresimd %} |
| 2620 |
let abcd = self.0; |
| 2621 |
let xxyy0 = simd_swizzle!(rhs.0, [0, 0, 1, 1]); |
| 2622 |
let xxyy1 = simd_swizzle!(rhs.0, [2, 2, 3, 3]); |
| 2623 |
let axbxcydy0 = abcd * xxyy0; |
| 2624 |
let axbxcydy1 = abcd * xxyy1; |
| 2625 |
let cydyaxbx0 = simd_swizzle!(axbxcydy0, [2, 3, 0, 1]); |
| 2626 |
let cydyaxbx1 = simd_swizzle!(axbxcydy1, [2, 3, 0, 1]); |
| 2627 |
let result0 = axbxcydy0 + cydyaxbx0; |
| 2628 |
let result1 = axbxcydy1 + cydyaxbx1; |
| 2629 |
Self(simd_swizzle!(result0, result1, [0, 1, 4, 5])) |
| 2630 |
{% elif self_t == "Mat2" and is_neon %} |
| 2631 |
unsafe { |
| 2632 |
let abcd = self.0; |
| 2633 |
let xxyy0 = vzip1q_f32(rhs.0, rhs.0); |
| 2634 |
let xxyy1 = vzip2q_f32(rhs.0, rhs.0); |
| 2635 |
let axbxcydy0 = vmulq_f32(abcd, xxyy0); |
| 2636 |
let axbxcydy1 = vmulq_f32(abcd, xxyy1); |
| 2637 |
let cydyaxbx0 = vextq_f32(axbxcydy0, axbxcydy0, 2); |
| 2638 |
let cydyaxbx1 = vextq_f32(axbxcydy1, axbxcydy1, 2); |
| 2639 |
let result0 = vaddq_f32(axbxcydy0, cydyaxbx0); |
| 2640 |
let result1 = vaddq_f32(axbxcydy1, cydyaxbx1); |
| 2641 |
Self(vreinterpretq_f32_u64( |
| 2642 |
vsetq_lane_u64(vgetq_lane_u64(vreinterpretq_u64_f32(result1), 0), |
| 2643 |
vreinterpretq_u64_f32(result0), 1))) |
| 2644 |
} |
| 2645 |
{% else %} |
| 2646 |
Self::from_cols( |
| 2647 |
{% for axis in axes %} |
| 2648 |
self.mul(rhs.{{ axis }}), |
| 2649 |
{%- endfor %} |
| 2650 |
) |
| 2651 |
{% endif %} |
| 2652 |
} |
| 2653 |
} |
| 2654 |
|
| 2655 |
{{ macros::impl_ref_binop(imp="Mul", self_t=self_t, rhs_t=self_t, output_t=self_t) }} |
| 2656 |
|
| 2657 |
{{ macros::impl_op_assign(imp="Mul", self_t=self_t, rhs_t=self_t) }} |
| 2658 |
|
| 2659 |
impl Mul<{{ col_t }}> for {{ self_t }} { |
| 2660 |
type Output = {{ col_t }}; |
| 2661 |
#[inline] |
| 2662 |
fn mul(self, rhs: {{ col_t }}) -> Self::Output { |
| 2663 |
{% if self_t == "Mat3A" %} |
| 2664 |
self.mul_vec3a(rhs) |
| 2665 |
{% else %} |
| 2666 |
self.mul_vec{{ dim }}(rhs) |
| 2667 |
{% endif %} |
| 2668 |
} |
| 2669 |
} |
| 2670 |
|
| 2671 |
{{ macros::impl_ref_binop(imp="Mul", self_t=self_t, rhs_t=col_t, output_t=col_t) }} |
| 2672 |
|
| 2673 |
impl Mul<{{ self_t }}> for {{ scalar_t }} { |
| 2674 |
type Output = {{ self_t }}; |
| 2675 |
#[inline] |
| 2676 |
fn mul(self, rhs: {{ self_t }}) -> Self::Output { |
| 2677 |
rhs.mul_scalar(self) |
| 2678 |
} |
| 2679 |
} |
| 2680 |
|
| 2681 |
{{ macros::impl_ref_binop(imp="Mul", self_t=scalar_t, rhs_t=self_t, output_t=self_t) }} |
| 2682 |
|
| 2683 |
impl Mul<{{ scalar_t }}> for {{ self_t }} { |
| 2684 |
type Output = Self; |
| 2685 |
#[inline] |
| 2686 |
fn mul(self, rhs: {{ scalar_t }}) -> Self { |
| 2687 |
self.mul_scalar(rhs) |
| 2688 |
} |
| 2689 |
} |
| 2690 |
|
| 2691 |
{{ macros::impl_ref_binop(imp="Mul", self_t=self_t, rhs_t=scalar_t, output_t=self_t) }} |
| 2692 |
|
| 2693 |
{{ macros::impl_op_assign(imp="Mul", self_t=self_t, rhs_t=scalar_t) }} |
| 2694 |
|
| 2695 |
impl Div<{{ self_t }}> for {{ scalar_t }} { |
| 2696 |
type Output = {{ self_t }}; |
| 2697 |
#[inline] |
| 2698 |
fn div(self, rhs: {{ self_t }}) -> Self::Output { |
| 2699 |
rhs.div_scalar(self) |
| 2700 |
} |
| 2701 |
} |
| 2702 |
|
| 2703 |
{{ macros::impl_ref_binop(imp="Div", self_t=scalar_t, rhs_t=self_t, output_t=self_t) }} |
| 2704 |
|
| 2705 |
impl Div<{{ scalar_t }}> for {{ self_t }} { |
| 2706 |
type Output = Self; |
| 2707 |
#[inline] |
| 2708 |
fn div(self, rhs: {{ scalar_t }}) -> Self { |
| 2709 |
self.div_scalar(rhs) |
| 2710 |
} |
| 2711 |
} |
| 2712 |
|
| 2713 |
{{ macros::impl_ref_binop(imp="Div", self_t=self_t, rhs_t=scalar_t, output_t=self_t) }} |
| 2714 |
|
| 2715 |
{{ macros::impl_op_assign(imp="Div", self_t=self_t, rhs_t=scalar_t) }} |
| 2716 |
|
| 2717 |
{% if self_t == "Mat3" %} |
| 2718 |
impl Mul<Vec3A> for Mat3 { |
| 2719 |
type Output = Vec3A; |
| 2720 |
#[inline] |
| 2721 |
fn mul(self, rhs: Vec3A) -> Vec3A { |
| 2722 |
self.mul_vec3a(rhs) |
| 2723 |
} |
| 2724 |
} |
| 2725 |
|
| 2726 |
{{ macros::impl_ref_binop(imp="Mul", self_t=self_t, rhs_t="Vec3A", output_t="Vec3A") }} |
| 2727 |
|
| 2728 |
impl From<Mat3A> for Mat3 { |
| 2729 |
#[inline] |
| 2730 |
fn from(m: Mat3A) -> Self { |
| 2731 |
Self { |
| 2732 |
x_axis: m.x_axis.into(), |
| 2733 |
y_axis: m.y_axis.into(), |
| 2734 |
z_axis: m.z_axis.into(), |
| 2735 |
} |
| 2736 |
} |
| 2737 |
} |
| 2738 |
{% elif self_t == "Mat3A" %} |
| 2739 |
impl Mul<Vec3> for Mat3A { |
| 2740 |
type Output = Vec3; |
| 2741 |
#[inline] |
| 2742 |
fn mul(self, rhs: Vec3) -> Vec3 { |
| 2743 |
self.mul_vec3a(rhs.into()).into() |
| 2744 |
} |
| 2745 |
} |
| 2746 |
|
| 2747 |
{{ macros::impl_ref_binop(imp="Mul", self_t=self_t, rhs_t="Vec3", output_t="Vec3") }} |
| 2748 |
|
| 2749 |
impl From<Mat3> for Mat3A { |
| 2750 |
#[inline] |
| 2751 |
fn from(m: Mat3) -> Self { |
| 2752 |
Self { |
| 2753 |
x_axis: m.x_axis.into(), |
| 2754 |
y_axis: m.y_axis.into(), |
| 2755 |
z_axis: m.z_axis.into(), |
| 2756 |
} |
| 2757 |
} |
| 2758 |
} |
| 2759 |
{% endif %} |
| 2760 |
|
| 2761 |
impl Sum<Self> for {{ self_t }} { |
| 2762 |
fn sum<I>(iter: I) -> Self |
| 2763 |
where |
| 2764 |
I: Iterator<Item = Self>, |
| 2765 |
{ |
| 2766 |
iter.fold(Self::ZERO, Self::add) |
| 2767 |
} |
| 2768 |
} |
| 2769 |
|
| 2770 |
impl<'a> Sum<&'a Self> for {{ self_t }} { |
| 2771 |
fn sum<I>(iter: I) -> Self |
| 2772 |
where |
| 2773 |
I: Iterator<Item = &'a Self>, |
| 2774 |
{ |
| 2775 |
iter.fold(Self::ZERO, |a, &b| Self::add(a, b)) |
| 2776 |
} |
| 2777 |
} |
| 2778 |
|
| 2779 |
impl Product for {{ self_t }} { |
| 2780 |
fn product<I>(iter: I) -> Self |
| 2781 |
where |
| 2782 |
I: Iterator<Item = Self>, |
| 2783 |
{ |
| 2784 |
iter.fold(Self::IDENTITY, Self::mul) |
| 2785 |
} |
| 2786 |
} |
| 2787 |
|
| 2788 |
impl<'a> Product<&'a Self> for {{ self_t }} { |
| 2789 |
fn product<I>(iter: I) -> Self |
| 2790 |
where |
| 2791 |
I: Iterator<Item = &'a Self>, |
| 2792 |
{ |
| 2793 |
iter.fold(Self::IDENTITY, |a, &b| Self::mul(a, b)) |
| 2794 |
} |
| 2795 |
} |
| 2796 |
|
| 2797 |
impl PartialEq for {{ self_t }} { |
| 2798 |
#[inline] |
| 2799 |
fn eq(&self, rhs: &Self) -> bool { |
| 2800 |
{% for axis in axes %} |
| 2801 |
self.{{ axis }}.eq(&rhs.{{ axis }}) {% if not loop.last %} && {% endif %} |
| 2802 |
{% endfor %} |
| 2803 |
} |
| 2804 |
} |
| 2805 |
|
| 2806 |
{% if not is_align %} |
| 2807 |
impl AsRef<[{{ scalar_t }}; {{ size }}]> for {{ self_t }} { |
| 2808 |
#[inline] |
| 2809 |
fn as_ref(&self) -> &[{{ scalar_t }}; {{ size }}] { |
| 2810 |
unsafe { &*(self as *const Self as *const [{{ scalar_t }}; {{ size }}]) } |
| 2811 |
} |
| 2812 |
} |
| 2813 |
|
| 2814 |
impl AsMut<[{{ scalar_t }}; {{ size }}]> for {{ self_t }} { |
| 2815 |
#[inline] |
| 2816 |
fn as_mut(&mut self) -> &mut [{{ scalar_t }}; {{ size }}] { |
| 2817 |
unsafe { &mut *(self as *mut Self as *mut [{{ scalar_t }}; {{ size }}]) } |
| 2818 |
} |
| 2819 |
} |
| 2820 |
{% endif %} |
| 2821 |
|
| 2822 |
{% if self_t == "Mat2" and not is_scalar %} |
| 2823 |
impl core::ops::Deref for Mat2 { |
| 2824 |
type Target = crate::deref::Cols2<Vec2>; |
| 2825 |
#[inline] |
| 2826 |
fn deref(&self) -> &Self::Target { |
| 2827 |
unsafe { &*(self as *const Self as *const Self::Target) } |
| 2828 |
} |
| 2829 |
} |
| 2830 |
|
| 2831 |
impl core::ops::DerefMut for Mat2 { |
| 2832 |
#[inline] |
| 2833 |
fn deref_mut(&mut self) -> &mut Self::Target { |
| 2834 |
unsafe { &mut *(self as *mut Self as *mut Self::Target) } |
| 2835 |
} |
| 2836 |
} |
| 2837 |
{% endif %} |
| 2838 |
|
| 2839 |
impl fmt::Debug for {{ self_t }} { |
| 2840 |
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 2841 |
fmt.debug_struct(stringify!({{ self_t }})) |
| 2842 |
{% for axis in axes %} |
| 2843 |
.field("{{ axis }}", &self.{{ axis }}) |
| 2844 |
{% endfor %} |
| 2845 |
.finish() |
| 2846 |
} |
| 2847 |
} |
| 2848 |
|
| 2849 |
impl fmt::Display for {{ self_t }} { |
| 2850 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 2851 |
if let Some(p) = f.precision() { |
| 2852 |
{% if dim == 2 %} |
| 2853 |
write!(f, "[{:.*}, {:.*}]", p, self.x_axis, p, self.y_axis) |
| 2854 |
{% elif dim == 3 %} |
| 2855 |
write!(f, "[{:.*}, {:.*}, {:.*}]", p, self.x_axis, p, self.y_axis, p, self.z_axis) |
| 2856 |
{% elif dim == 4 %} |
| 2857 |
write!(f, "[{:.*}, {:.*}, {:.*}, {:.*}]", p, self.x_axis, p, self.y_axis, p, self.z_axis, p, self.w_axis) |
| 2858 |
{% endif %} |
| 2859 |
} else { |
| 2860 |
{% if dim == 2 %} |
| 2861 |
write!(f, "[{}, {}]", self.x_axis, self.y_axis) |
| 2862 |
{% elif dim == 3 %} |
| 2863 |
write!(f, "[{}, {}, {}]", self.x_axis, self.y_axis, self.z_axis) |
| 2864 |
{% elif dim == 4 %} |
| 2865 |
write!(f, "[{}, {}, {}, {}]", self.x_axis, self.y_axis, self.z_axis, self.w_axis) |
| 2866 |
{% endif %} |
| 2867 |
} |
| 2868 |
} |
| 2869 |
} |
| 2870 |
|