nalgebra/geometry/orthographic.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785
#[cfg(feature = "arbitrary")]
use quickcheck::{Arbitrary, Gen};
#[cfg(feature = "rand-no-std")]
use rand::{
distributions::{Distribution, Standard},
Rng,
};
#[cfg(feature = "serde-serialize-no-std")]
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::fmt;
use simba::scalar::RealField;
use crate::base::dimension::U3;
use crate::base::storage::Storage;
use crate::base::{Matrix4, Vector, Vector3};
use crate::geometry::{Point3, Projective3};
#[cfg(feature = "rkyv-serialize")]
use rkyv::bytecheck;
/// A 3D orthographic projection stored as a homogeneous 4x4 matrix.
#[repr(C)]
#[cfg_attr(
feature = "rkyv-serialize-no-std",
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize),
archive(
as = "Orthographic3<T::Archived>",
bound(archive = "
T: rkyv::Archive,
Matrix4<T>: rkyv::Archive<Archived = Matrix4<T::Archived>>
")
)
)]
#[cfg_attr(feature = "rkyv-serialize", derive(bytecheck::CheckBytes))]
#[derive(Copy, Clone)]
pub struct Orthographic3<T> {
matrix: Matrix4<T>,
}
impl<T: RealField> fmt::Debug for Orthographic3<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
self.matrix.fmt(f)
}
}
impl<T: RealField> PartialEq for Orthographic3<T> {
#[inline]
fn eq(&self, right: &Self) -> bool {
self.matrix == right.matrix
}
}
#[cfg(feature = "bytemuck")]
unsafe impl<T> bytemuck::Zeroable for Orthographic3<T>
where
T: RealField + bytemuck::Zeroable,
Matrix4<T>: bytemuck::Zeroable,
{
}
#[cfg(feature = "bytemuck")]
unsafe impl<T> bytemuck::Pod for Orthographic3<T>
where
T: RealField + bytemuck::Pod,
Matrix4<T>: bytemuck::Pod,
{
}
#[cfg(feature = "serde-serialize-no-std")]
impl<T: RealField + Serialize> Serialize for Orthographic3<T> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
self.matrix.serialize(serializer)
}
}
#[cfg(feature = "serde-serialize-no-std")]
impl<'a, T: RealField + Deserialize<'a>> Deserialize<'a> for Orthographic3<T> {
fn deserialize<Des>(deserializer: Des) -> Result<Self, Des::Error>
where
Des: Deserializer<'a>,
{
let matrix = Matrix4::<T>::deserialize(deserializer)?;
Ok(Self::from_matrix_unchecked(matrix))
}
}
impl<T> Orthographic3<T> {
/// Wraps the given matrix to interpret it as a 3D orthographic matrix.
///
/// It is not checked whether or not the given matrix actually represents an orthographic
/// projection.
///
/// # Example
/// ```
/// # use nalgebra::{Orthographic3, Point3, Matrix4};
/// let mat = Matrix4::new(
/// 2.0 / 9.0, 0.0, 0.0, -11.0 / 9.0,
/// 0.0, 2.0 / 18.0, 0.0, -22.0 / 18.0,
/// 0.0, 0.0, -2.0 / 999.9, -1000.1 / 999.9,
/// 0.0, 0.0, 0.0, 1.0
/// );
/// let proj = Orthographic3::from_matrix_unchecked(mat);
/// assert_eq!(proj, Orthographic3::new(1.0, 10.0, 2.0, 20.0, 0.1, 1000.0));
/// ```
#[inline]
pub const fn from_matrix_unchecked(matrix: Matrix4<T>) -> Self {
Self { matrix }
}
}
impl<T: RealField> Orthographic3<T> {
/// Creates a new orthographic projection matrix.
///
/// This follows the OpenGL convention, so this will flip the `z` axis.
///
/// # Example
/// ```
/// # #[macro_use] extern crate approx;
/// # use nalgebra::{Orthographic3, Point3};
/// let proj = Orthographic3::new(1.0, 10.0, 2.0, 20.0, 0.1, 1000.0);
/// // Check this projection actually transforms the view cuboid into the double-unit cube.
/// // See https://www.nalgebra.org/docs/user_guide/projections#orthographic-projection for more details.
/// let p1 = Point3::new(1.0, 2.0, -0.1);
/// let p2 = Point3::new(1.0, 2.0, -1000.0);
/// let p3 = Point3::new(1.0, 20.0, -0.1);
/// let p4 = Point3::new(1.0, 20.0, -1000.0);
/// let p5 = Point3::new(10.0, 2.0, -0.1);
/// let p6 = Point3::new(10.0, 2.0, -1000.0);
/// let p7 = Point3::new(10.0, 20.0, -0.1);
/// let p8 = Point3::new(10.0, 20.0, -1000.0);
///
/// assert_relative_eq!(proj.project_point(&p1), Point3::new(-1.0, -1.0, -1.0));
/// assert_relative_eq!(proj.project_point(&p2), Point3::new(-1.0, -1.0, 1.0));
/// assert_relative_eq!(proj.project_point(&p3), Point3::new(-1.0, 1.0, -1.0));
/// assert_relative_eq!(proj.project_point(&p4), Point3::new(-1.0, 1.0, 1.0));
/// assert_relative_eq!(proj.project_point(&p5), Point3::new( 1.0, -1.0, -1.0));
/// assert_relative_eq!(proj.project_point(&p6), Point3::new( 1.0, -1.0, 1.0));
/// assert_relative_eq!(proj.project_point(&p7), Point3::new( 1.0, 1.0, -1.0));
/// assert_relative_eq!(proj.project_point(&p8), Point3::new( 1.0, 1.0, 1.0));
///
/// // This also works with flipped axis. In other words, we allow that
/// // `left > right`, `bottom > top`, and/or `znear > zfar`.
/// let proj = Orthographic3::new(10.0, 1.0, 20.0, 2.0, 1000.0, 0.1);
///
/// assert_relative_eq!(proj.project_point(&p1), Point3::new( 1.0, 1.0, 1.0));
/// assert_relative_eq!(proj.project_point(&p2), Point3::new( 1.0, 1.0, -1.0));
/// assert_relative_eq!(proj.project_point(&p3), Point3::new( 1.0, -1.0, 1.0));
/// assert_relative_eq!(proj.project_point(&p4), Point3::new( 1.0, -1.0, -1.0));
/// assert_relative_eq!(proj.project_point(&p5), Point3::new(-1.0, 1.0, 1.0));
/// assert_relative_eq!(proj.project_point(&p6), Point3::new(-1.0, 1.0, -1.0));
/// assert_relative_eq!(proj.project_point(&p7), Point3::new(-1.0, -1.0, 1.0));
/// assert_relative_eq!(proj.project_point(&p8), Point3::new(-1.0, -1.0, -1.0));
/// ```
#[inline]
pub fn new(left: T, right: T, bottom: T, top: T, znear: T, zfar: T) -> Self {
let matrix = Matrix4::<T>::identity();
let mut res = Self::from_matrix_unchecked(matrix);
res.set_left_and_right(left, right);
res.set_bottom_and_top(bottom, top);
res.set_znear_and_zfar(znear, zfar);
res
}
/// Creates a new orthographic projection matrix from an aspect ratio and the vertical field of view.
#[inline]
pub fn from_fov(aspect: T, vfov: T, znear: T, zfar: T) -> Self {
assert!(
znear != zfar,
"The far plane must not be equal to the near plane."
);
assert!(
!relative_eq!(aspect, T::zero()),
"The aspect ratio must not be zero."
);
let half: T = crate::convert(0.5);
let width = zfar.clone() * (vfov * half.clone()).tan();
let height = width.clone() / aspect;
Self::new(
-width.clone() * half.clone(),
width * half.clone(),
-height.clone() * half.clone(),
height * half,
znear,
zfar,
)
}
/// Retrieves the inverse of the underlying homogeneous matrix.
///
/// # Example
/// ```
/// # #[macro_use] extern crate approx;
/// # use nalgebra::{Orthographic3, Point3, Matrix4};
/// let proj = Orthographic3::new(1.0, 10.0, 2.0, 20.0, 0.1, 1000.0);
/// let inv = proj.inverse();
///
/// assert_relative_eq!(inv * proj.as_matrix(), Matrix4::identity());
/// assert_relative_eq!(proj.as_matrix() * inv, Matrix4::identity());
///
/// let proj = Orthographic3::new(10.0, 1.0, 20.0, 2.0, 1000.0, 0.1);
/// let inv = proj.inverse();
/// assert_relative_eq!(inv * proj.as_matrix(), Matrix4::identity());
/// assert_relative_eq!(proj.as_matrix() * inv, Matrix4::identity());
/// ```
#[inline]
#[must_use]
pub fn inverse(&self) -> Matrix4<T> {
let mut res = self.clone().to_homogeneous();
let inv_m11 = T::one() / self.matrix[(0, 0)].clone();
let inv_m22 = T::one() / self.matrix[(1, 1)].clone();
let inv_m33 = T::one() / self.matrix[(2, 2)].clone();
res[(0, 0)] = inv_m11.clone();
res[(1, 1)] = inv_m22.clone();
res[(2, 2)] = inv_m33.clone();
res[(0, 3)] = -self.matrix[(0, 3)].clone() * inv_m11;
res[(1, 3)] = -self.matrix[(1, 3)].clone() * inv_m22;
res[(2, 3)] = -self.matrix[(2, 3)].clone() * inv_m33;
res
}
/// Computes the corresponding homogeneous matrix.
///
/// # Example
/// ```
/// # use nalgebra::{Orthographic3, Point3, Matrix4};
/// let proj = Orthographic3::new(1.0, 10.0, 2.0, 20.0, 0.1, 1000.0);
/// let expected = Matrix4::new(
/// 2.0 / 9.0, 0.0, 0.0, -11.0 / 9.0,
/// 0.0, 2.0 / 18.0, 0.0, -22.0 / 18.0,
/// 0.0, 0.0, -2.0 / 999.9, -1000.1 / 999.9,
/// 0.0, 0.0, 0.0, 1.0
/// );
/// assert_eq!(proj.to_homogeneous(), expected);
/// ```
#[inline]
#[must_use]
pub fn to_homogeneous(self) -> Matrix4<T> {
self.matrix
}
/// A reference to the underlying homogeneous transformation matrix.
///
/// # Example
/// ```
/// # use nalgebra::{Orthographic3, Point3, Matrix4};
/// let proj = Orthographic3::new(1.0, 10.0, 2.0, 20.0, 0.1, 1000.0);
/// let expected = Matrix4::new(
/// 2.0 / 9.0, 0.0, 0.0, -11.0 / 9.0,
/// 0.0, 2.0 / 18.0, 0.0, -22.0 / 18.0,
/// 0.0, 0.0, -2.0 / 999.9, -1000.1 / 999.9,
/// 0.0, 0.0, 0.0, 1.0
/// );
/// assert_eq!(*proj.as_matrix(), expected);
/// ```
#[inline]
#[must_use]
pub fn as_matrix(&self) -> &Matrix4<T> {
&self.matrix
}
/// A reference to this transformation seen as a `Projective3`.
///
/// # Example
/// ```
/// # use nalgebra::Orthographic3;
/// let proj = Orthographic3::new(1.0, 10.0, 2.0, 20.0, 0.1, 1000.0);
/// assert_eq!(proj.as_projective().to_homogeneous(), proj.to_homogeneous());
/// ```
#[inline]
#[must_use]
pub fn as_projective(&self) -> &Projective3<T> {
unsafe { &*(self as *const Orthographic3<T> as *const Projective3<T>) }
}
/// This transformation seen as a `Projective3`.
///
/// # Example
/// ```
/// # use nalgebra::Orthographic3;
/// let proj = Orthographic3::new(1.0, 10.0, 2.0, 20.0, 0.1, 1000.0);
/// assert_eq!(proj.to_projective().to_homogeneous(), proj.to_homogeneous());
/// ```
#[inline]
#[must_use]
pub fn to_projective(self) -> Projective3<T> {
Projective3::from_matrix_unchecked(self.matrix)
}
/// Retrieves the underlying homogeneous matrix.
///
/// # Example
/// ```
/// # #[macro_use] extern crate approx;
/// # use nalgebra::{Orthographic3, Point3, Matrix4};
/// let proj = Orthographic3::new(1.0, 10.0, 2.0, 20.0, 0.1, 1000.0);
/// let expected = Matrix4::new(
/// 2.0 / 9.0, 0.0, 0.0, -11.0 / 9.0,
/// 0.0, 2.0 / 18.0, 0.0, -22.0 / 18.0,
/// 0.0, 0.0, -2.0 / 999.9, -1000.1 / 999.9,
/// 0.0, 0.0, 0.0, 1.0
/// );
/// assert_eq!(proj.into_inner(), expected);
/// ```
#[inline]
pub fn into_inner(self) -> Matrix4<T> {
self.matrix
}
/// Retrieves the underlying homogeneous matrix.
/// Deprecated: Use [`Orthographic3::into_inner`] instead.
#[deprecated(note = "use `.into_inner()` instead")]
#[inline]
pub fn unwrap(self) -> Matrix4<T> {
self.matrix
}
/// The left offset of the view cuboid.
///
/// # Example
/// ```
/// # #[macro_use] extern crate approx;
/// # use nalgebra::Orthographic3;
/// let proj = Orthographic3::new(1.0, 10.0, 2.0, 20.0, 0.1, 1000.0);
/// assert_relative_eq!(proj.left(), 1.0, epsilon = 1.0e-6);
///
/// let proj = Orthographic3::new(10.0, 1.0, 20.0, 2.0, 1000.0, 0.1);
/// assert_relative_eq!(proj.left(), 10.0, epsilon = 1.0e-6);
/// ```
#[inline]
#[must_use]
pub fn left(&self) -> T {
(-T::one() - self.matrix[(0, 3)].clone()) / self.matrix[(0, 0)].clone()
}
/// The right offset of the view cuboid.
///
/// # Example
/// ```
/// # #[macro_use] extern crate approx;
/// # use nalgebra::Orthographic3;
/// let proj = Orthographic3::new(1.0, 10.0, 2.0, 20.0, 0.1, 1000.0);
/// assert_relative_eq!(proj.right(), 10.0, epsilon = 1.0e-6);
///
/// let proj = Orthographic3::new(10.0, 1.0, 20.0, 2.0, 1000.0, 0.1);
/// assert_relative_eq!(proj.right(), 1.0, epsilon = 1.0e-6);
/// ```
#[inline]
#[must_use]
pub fn right(&self) -> T {
(T::one() - self.matrix[(0, 3)].clone()) / self.matrix[(0, 0)].clone()
}
/// The bottom offset of the view cuboid.
///
/// # Example
/// ```
/// # #[macro_use] extern crate approx;
/// # use nalgebra::Orthographic3;
/// let proj = Orthographic3::new(1.0, 10.0, 2.0, 20.0, 0.1, 1000.0);
/// assert_relative_eq!(proj.bottom(), 2.0, epsilon = 1.0e-6);
///
/// let proj = Orthographic3::new(10.0, 1.0, 20.0, 2.0, 1000.0, 0.1);
/// assert_relative_eq!(proj.bottom(), 20.0, epsilon = 1.0e-6);
/// ```
#[inline]
#[must_use]
pub fn bottom(&self) -> T {
(-T::one() - self.matrix[(1, 3)].clone()) / self.matrix[(1, 1)].clone()
}
/// The top offset of the view cuboid.
///
/// # Example
/// ```
/// # #[macro_use] extern crate approx;
/// # use nalgebra::Orthographic3;
/// let proj = Orthographic3::new(1.0, 10.0, 2.0, 20.0, 0.1, 1000.0);
/// assert_relative_eq!(proj.top(), 20.0, epsilon = 1.0e-6);
///
/// let proj = Orthographic3::new(10.0, 1.0, 20.0, 2.0, 1000.0, 0.1);
/// assert_relative_eq!(proj.top(), 2.0, epsilon = 1.0e-6);
/// ```
#[inline]
#[must_use]
pub fn top(&self) -> T {
(T::one() - self.matrix[(1, 3)].clone()) / self.matrix[(1, 1)].clone()
}
/// The near plane offset of the view cuboid.
///
/// # Example
/// ```
/// # #[macro_use] extern crate approx;
/// # use nalgebra::Orthographic3;
/// let proj = Orthographic3::new(1.0, 10.0, 2.0, 20.0, 0.1, 1000.0);
/// assert_relative_eq!(proj.znear(), 0.1, epsilon = 1.0e-6);
///
/// let proj = Orthographic3::new(10.0, 1.0, 20.0, 2.0, 1000.0, 0.1);
/// assert_relative_eq!(proj.znear(), 1000.0, epsilon = 1.0e-6);
/// ```
#[inline]
#[must_use]
pub fn znear(&self) -> T {
(T::one() + self.matrix[(2, 3)].clone()) / self.matrix[(2, 2)].clone()
}
/// The far plane offset of the view cuboid.
///
/// # Example
/// ```
/// # #[macro_use] extern crate approx;
/// # use nalgebra::Orthographic3;
/// let proj = Orthographic3::new(1.0, 10.0, 2.0, 20.0, 0.1, 1000.0);
/// assert_relative_eq!(proj.zfar(), 1000.0, epsilon = 1.0e-6);
///
/// let proj = Orthographic3::new(10.0, 1.0, 20.0, 2.0, 1000.0, 0.1);
/// assert_relative_eq!(proj.zfar(), 0.1, epsilon = 1.0e-6);
/// ```
#[inline]
#[must_use]
pub fn zfar(&self) -> T {
(-T::one() + self.matrix[(2, 3)].clone()) / self.matrix[(2, 2)].clone()
}
// TODO: when we get specialization, specialize the Mul impl instead.
/// Projects a point. Faster than matrix multiplication.
///
/// # Example
/// ```
/// # #[macro_use] extern crate approx;
/// # use nalgebra::{Orthographic3, Point3};
/// let proj = Orthographic3::new(1.0, 10.0, 2.0, 20.0, 0.1, 1000.0);
///
/// let p1 = Point3::new(1.0, 2.0, -0.1);
/// let p2 = Point3::new(1.0, 2.0, -1000.0);
/// let p3 = Point3::new(1.0, 20.0, -0.1);
/// let p4 = Point3::new(1.0, 20.0, -1000.0);
/// let p5 = Point3::new(10.0, 2.0, -0.1);
/// let p6 = Point3::new(10.0, 2.0, -1000.0);
/// let p7 = Point3::new(10.0, 20.0, -0.1);
/// let p8 = Point3::new(10.0, 20.0, -1000.0);
///
/// assert_relative_eq!(proj.project_point(&p1), Point3::new(-1.0, -1.0, -1.0));
/// assert_relative_eq!(proj.project_point(&p2), Point3::new(-1.0, -1.0, 1.0));
/// assert_relative_eq!(proj.project_point(&p3), Point3::new(-1.0, 1.0, -1.0));
/// assert_relative_eq!(proj.project_point(&p4), Point3::new(-1.0, 1.0, 1.0));
/// assert_relative_eq!(proj.project_point(&p5), Point3::new( 1.0, -1.0, -1.0));
/// assert_relative_eq!(proj.project_point(&p6), Point3::new( 1.0, -1.0, 1.0));
/// assert_relative_eq!(proj.project_point(&p7), Point3::new( 1.0, 1.0, -1.0));
/// assert_relative_eq!(proj.project_point(&p8), Point3::new( 1.0, 1.0, 1.0));
/// ```
#[inline]
#[must_use]
pub fn project_point(&self, p: &Point3<T>) -> Point3<T> {
Point3::new(
self.matrix[(0, 0)].clone() * p[0].clone() + self.matrix[(0, 3)].clone(),
self.matrix[(1, 1)].clone() * p[1].clone() + self.matrix[(1, 3)].clone(),
self.matrix[(2, 2)].clone() * p[2].clone() + self.matrix[(2, 3)].clone(),
)
}
/// Un-projects a point. Faster than multiplication by the underlying matrix inverse.
///
/// # Example
/// ```
/// # #[macro_use] extern crate approx;
/// # use nalgebra::{Orthographic3, Point3};
/// let proj = Orthographic3::new(1.0, 10.0, 2.0, 20.0, 0.1, 1000.0);
///
/// let p1 = Point3::new(-1.0, -1.0, -1.0);
/// let p2 = Point3::new(-1.0, -1.0, 1.0);
/// let p3 = Point3::new(-1.0, 1.0, -1.0);
/// let p4 = Point3::new(-1.0, 1.0, 1.0);
/// let p5 = Point3::new( 1.0, -1.0, -1.0);
/// let p6 = Point3::new( 1.0, -1.0, 1.0);
/// let p7 = Point3::new( 1.0, 1.0, -1.0);
/// let p8 = Point3::new( 1.0, 1.0, 1.0);
///
/// assert_relative_eq!(proj.unproject_point(&p1), Point3::new(1.0, 2.0, -0.1), epsilon = 1.0e-6);
/// assert_relative_eq!(proj.unproject_point(&p2), Point3::new(1.0, 2.0, -1000.0), epsilon = 1.0e-6);
/// assert_relative_eq!(proj.unproject_point(&p3), Point3::new(1.0, 20.0, -0.1), epsilon = 1.0e-6);
/// assert_relative_eq!(proj.unproject_point(&p4), Point3::new(1.0, 20.0, -1000.0), epsilon = 1.0e-6);
/// assert_relative_eq!(proj.unproject_point(&p5), Point3::new(10.0, 2.0, -0.1), epsilon = 1.0e-6);
/// assert_relative_eq!(proj.unproject_point(&p6), Point3::new(10.0, 2.0, -1000.0), epsilon = 1.0e-6);
/// assert_relative_eq!(proj.unproject_point(&p7), Point3::new(10.0, 20.0, -0.1), epsilon = 1.0e-6);
/// assert_relative_eq!(proj.unproject_point(&p8), Point3::new(10.0, 20.0, -1000.0), epsilon = 1.0e-6);
/// ```
#[inline]
#[must_use]
pub fn unproject_point(&self, p: &Point3<T>) -> Point3<T> {
Point3::new(
(p[0].clone() - self.matrix[(0, 3)].clone()) / self.matrix[(0, 0)].clone(),
(p[1].clone() - self.matrix[(1, 3)].clone()) / self.matrix[(1, 1)].clone(),
(p[2].clone() - self.matrix[(2, 3)].clone()) / self.matrix[(2, 2)].clone(),
)
}
// TODO: when we get specialization, specialize the Mul impl instead.
/// Projects a vector. Faster than matrix multiplication.
///
/// Vectors are not affected by the translation part of the projection.
///
/// # Example
/// ```
/// # #[macro_use] extern crate approx;
/// # use nalgebra::{Orthographic3, Vector3};
/// let proj = Orthographic3::new(1.0, 10.0, 2.0, 20.0, 0.1, 1000.0);
///
/// let v1 = Vector3::x();
/// let v2 = Vector3::y();
/// let v3 = Vector3::z();
///
/// assert_relative_eq!(proj.project_vector(&v1), Vector3::x() * 2.0 / 9.0);
/// assert_relative_eq!(proj.project_vector(&v2), Vector3::y() * 2.0 / 18.0);
/// assert_relative_eq!(proj.project_vector(&v3), Vector3::z() * -2.0 / 999.9);
/// ```
#[inline]
#[must_use]
pub fn project_vector<SB>(&self, p: &Vector<T, U3, SB>) -> Vector3<T>
where
SB: Storage<T, U3>,
{
Vector3::new(
self.matrix[(0, 0)].clone() * p[0].clone(),
self.matrix[(1, 1)].clone() * p[1].clone(),
self.matrix[(2, 2)].clone() * p[2].clone(),
)
}
/// Sets the left offset of the view cuboid.
///
/// # Example
/// ```
/// # #[macro_use] extern crate approx;
/// # use nalgebra::Orthographic3;
/// let mut proj = Orthographic3::new(1.0, 10.0, 2.0, 20.0, 0.1, 1000.0);
/// proj.set_left(2.0);
/// assert_relative_eq!(proj.left(), 2.0, epsilon = 1.0e-6);
///
/// // It is OK to set a left offset greater than the current right offset.
/// proj.set_left(20.0);
/// assert_relative_eq!(proj.left(), 20.0, epsilon = 1.0e-6);
/// ```
#[inline]
pub fn set_left(&mut self, left: T) {
let right = self.right();
self.set_left_and_right(left, right);
}
/// Sets the right offset of the view cuboid.
///
/// # Example
/// ```
/// # #[macro_use] extern crate approx;
/// # use nalgebra::Orthographic3;
/// let mut proj = Orthographic3::new(1.0, 10.0, 2.0, 20.0, 0.1, 1000.0);
/// proj.set_right(15.0);
/// assert_relative_eq!(proj.right(), 15.0, epsilon = 1.0e-6);
///
/// // It is OK to set a right offset smaller than the current left offset.
/// proj.set_right(-3.0);
/// assert_relative_eq!(proj.right(), -3.0, epsilon = 1.0e-6);
/// ```
#[inline]
pub fn set_right(&mut self, right: T) {
let left = self.left();
self.set_left_and_right(left, right);
}
/// Sets the bottom offset of the view cuboid.
///
/// # Example
/// ```
/// # #[macro_use] extern crate approx;
/// # use nalgebra::Orthographic3;
/// let mut proj = Orthographic3::new(1.0, 10.0, 2.0, 20.0, 0.1, 1000.0);
/// proj.set_bottom(8.0);
/// assert_relative_eq!(proj.bottom(), 8.0, epsilon = 1.0e-6);
///
/// // It is OK to set a bottom offset greater than the current top offset.
/// proj.set_bottom(50.0);
/// assert_relative_eq!(proj.bottom(), 50.0, epsilon = 1.0e-6);
/// ```
#[inline]
pub fn set_bottom(&mut self, bottom: T) {
let top = self.top();
self.set_bottom_and_top(bottom, top);
}
/// Sets the top offset of the view cuboid.
///
/// # Example
/// ```
/// # #[macro_use] extern crate approx;
/// # use nalgebra::Orthographic3;
/// let mut proj = Orthographic3::new(1.0, 10.0, 2.0, 20.0, 0.1, 1000.0);
/// proj.set_top(15.0);
/// assert_relative_eq!(proj.top(), 15.0, epsilon = 1.0e-6);
///
/// // It is OK to set a top offset smaller than the current bottom offset.
/// proj.set_top(-3.0);
/// assert_relative_eq!(proj.top(), -3.0, epsilon = 1.0e-6);
/// ```
#[inline]
pub fn set_top(&mut self, top: T) {
let bottom = self.bottom();
self.set_bottom_and_top(bottom, top);
}
/// Sets the near plane offset of the view cuboid.
///
/// # Example
/// ```
/// # #[macro_use] extern crate approx;
/// # use nalgebra::Orthographic3;
/// let mut proj = Orthographic3::new(1.0, 10.0, 2.0, 20.0, 0.1, 1000.0);
/// proj.set_znear(8.0);
/// assert_relative_eq!(proj.znear(), 8.0, epsilon = 1.0e-6);
///
/// // It is OK to set a znear greater than the current zfar.
/// proj.set_znear(5000.0);
/// assert_relative_eq!(proj.znear(), 5000.0, epsilon = 1.0e-6);
/// ```
#[inline]
pub fn set_znear(&mut self, znear: T) {
let zfar = self.zfar();
self.set_znear_and_zfar(znear, zfar);
}
/// Sets the far plane offset of the view cuboid.
///
/// # Example
/// ```
/// # #[macro_use] extern crate approx;
/// # use nalgebra::Orthographic3;
/// let mut proj = Orthographic3::new(1.0, 10.0, 2.0, 20.0, 0.1, 1000.0);
/// proj.set_zfar(15.0);
/// assert_relative_eq!(proj.zfar(), 15.0, epsilon = 1.0e-6);
///
/// // It is OK to set a zfar smaller than the current znear.
/// proj.set_zfar(-3.0);
/// assert_relative_eq!(proj.zfar(), -3.0, epsilon = 1.0e-6);
/// ```
#[inline]
pub fn set_zfar(&mut self, zfar: T) {
let znear = self.znear();
self.set_znear_and_zfar(znear, zfar);
}
/// Sets the view cuboid offsets along the `x` axis.
///
/// # Example
/// ```
/// # #[macro_use] extern crate approx;
/// # use nalgebra::Orthographic3;
/// let mut proj = Orthographic3::new(1.0, 10.0, 2.0, 20.0, 0.1, 1000.0);
/// proj.set_left_and_right(7.0, 70.0);
/// assert_relative_eq!(proj.left(), 7.0, epsilon = 1.0e-6);
/// assert_relative_eq!(proj.right(), 70.0, epsilon = 1.0e-6);
///
/// // It is also OK to have `left > right`.
/// proj.set_left_and_right(70.0, 7.0);
/// assert_relative_eq!(proj.left(), 70.0, epsilon = 1.0e-6);
/// assert_relative_eq!(proj.right(), 7.0, epsilon = 1.0e-6);
/// ```
#[inline]
pub fn set_left_and_right(&mut self, left: T, right: T) {
assert!(
left != right,
"The left corner must not be equal to the right corner."
);
self.matrix[(0, 0)] = crate::convert::<_, T>(2.0) / (right.clone() - left.clone());
self.matrix[(0, 3)] = -(right.clone() + left.clone()) / (right - left);
}
/// Sets the view cuboid offsets along the `y` axis.
///
/// # Example
/// ```
/// # #[macro_use] extern crate approx;
/// # use nalgebra::Orthographic3;
/// let mut proj = Orthographic3::new(1.0, 10.0, 2.0, 20.0, 0.1, 1000.0);
/// proj.set_bottom_and_top(7.0, 70.0);
/// assert_relative_eq!(proj.bottom(), 7.0, epsilon = 1.0e-6);
/// assert_relative_eq!(proj.top(), 70.0, epsilon = 1.0e-6);
///
/// // It is also OK to have `bottom > top`.
/// proj.set_bottom_and_top(70.0, 7.0);
/// assert_relative_eq!(proj.bottom(), 70.0, epsilon = 1.0e-6);
/// assert_relative_eq!(proj.top(), 7.0, epsilon = 1.0e-6);
/// ```
#[inline]
pub fn set_bottom_and_top(&mut self, bottom: T, top: T) {
assert_ne!(
bottom, top,
"The top corner must not be equal to the bottom corner."
);
self.matrix[(1, 1)] = crate::convert::<_, T>(2.0) / (top.clone() - bottom.clone());
self.matrix[(1, 3)] = -(top.clone() + bottom.clone()) / (top - bottom);
}
/// Sets the near and far plane offsets of the view cuboid.
///
/// # Example
/// ```
/// # #[macro_use] extern crate approx;
/// # use nalgebra::Orthographic3;
/// let mut proj = Orthographic3::new(1.0, 10.0, 2.0, 20.0, 0.1, 1000.0);
/// proj.set_znear_and_zfar(50.0, 5000.0);
/// assert_relative_eq!(proj.znear(), 50.0, epsilon = 1.0e-6);
/// assert_relative_eq!(proj.zfar(), 5000.0, epsilon = 1.0e-6);
///
/// // It is also OK to have `znear > zfar`.
/// proj.set_znear_and_zfar(5000.0, 0.5);
/// assert_relative_eq!(proj.znear(), 5000.0, epsilon = 1.0e-6);
/// assert_relative_eq!(proj.zfar(), 0.5, epsilon = 1.0e-6);
/// ```
#[inline]
pub fn set_znear_and_zfar(&mut self, znear: T, zfar: T) {
assert!(
zfar != znear,
"The near-plane and far-plane must not be superimposed."
);
self.matrix[(2, 2)] = -crate::convert::<_, T>(2.0) / (zfar.clone() - znear.clone());
self.matrix[(2, 3)] = -(zfar.clone() + znear.clone()) / (zfar - znear);
}
}
#[cfg(feature = "rand-no-std")]
impl<T: RealField> Distribution<Orthographic3<T>> for Standard
where
Standard: Distribution<T>,
{
/// Generate an arbitrary random variate for testing purposes.
fn sample<R: Rng + ?Sized>(&self, r: &mut R) -> Orthographic3<T> {
use crate::base::helper;
let left = r.gen();
let right = helper::reject_rand(r, |x: &T| *x > left);
let bottom = r.gen();
let top = helper::reject_rand(r, |x: &T| *x > bottom);
let znear = r.gen();
let zfar = helper::reject_rand(r, |x: &T| *x > znear);
Orthographic3::new(left, right, bottom, top, znear, zfar)
}
}
#[cfg(feature = "arbitrary")]
impl<T: RealField + Arbitrary> Arbitrary for Orthographic3<T>
where
Matrix4<T>: Send,
{
fn arbitrary(g: &mut Gen) -> Self {
use crate::base::helper;
let left = Arbitrary::arbitrary(g);
let right = helper::reject(g, |x: &T| *x > left);
let bottom = Arbitrary::arbitrary(g);
let top = helper::reject(g, |x: &T| *x > bottom);
let znear = Arbitrary::arbitrary(g);
let zfar = helper::reject(g, |x: &T| *x > znear);
Self::new(left, right, bottom, top, znear, zfar)
}
}
impl<T: RealField> From<Orthographic3<T>> for Matrix4<T> {
#[inline]
fn from(orth: Orthographic3<T>) -> Self {
orth.into_inner()
}
}