Improved Plane, fixed Frustum computation from Matrix4, implemented Frustum contains() tests.
This commit is contained in:
parent
715f3b7157
commit
fa3aacafcf
4 changed files with 95 additions and 17 deletions
|
@ -16,14 +16,15 @@
|
|||
//! View frustum for visibility determination
|
||||
|
||||
use array::Array2;
|
||||
use bound::*;
|
||||
use matrix::Matrix4;
|
||||
use num::BaseFloat;
|
||||
use plane::Plane;
|
||||
use point::Point3;
|
||||
use vector::{Vector, EuclideanVector};
|
||||
|
||||
#[derive(Copy, Clone, PartialEq, RustcEncodable, RustcDecodable)]
|
||||
pub struct Frustum<S> {
|
||||
#[derive(Copy, Clone, Debug, PartialEq, RustcEncodable, RustcDecodable)]
|
||||
pub struct Frustum<S: BaseFloat> {
|
||||
pub left: Plane<S>,
|
||||
pub right: Plane<S>,
|
||||
pub bottom: Plane<S>,
|
||||
|
@ -34,7 +35,7 @@ pub struct Frustum<S> {
|
|||
|
||||
impl<S: BaseFloat + 'static>
|
||||
Frustum<S> {
|
||||
/// Constructs a frustum
|
||||
/// Construct a frustum.
|
||||
pub fn new(left: Plane<S>, right: Plane<S>,
|
||||
bottom: Plane<S>, top: Plane<S>,
|
||||
near: Plane<S>, far: Plane<S>) -> Frustum<S> {
|
||||
|
@ -48,14 +49,38 @@ Frustum<S> {
|
|||
}
|
||||
}
|
||||
|
||||
/// Extracts frustum planes from a projection matrix
|
||||
pub fn from_matrix4(mat: Matrix4<S>) -> Frustum<S> {
|
||||
Frustum::new(Plane::from_vector4(mat.row(3).add_v(&mat.row(0)).normalize()),
|
||||
Plane::from_vector4(mat.row(3).sub_v(&mat.row(0)).normalize()),
|
||||
Plane::from_vector4(mat.row(3).add_v(&mat.row(1)).normalize()),
|
||||
Plane::from_vector4(mat.row(3).sub_v(&mat.row(1)).normalize()),
|
||||
Plane::from_vector4(mat.row(3).add_v(&mat.row(2)).normalize()),
|
||||
Plane::from_vector4(mat.row(3).sub_v(&mat.row(2)).normalize()))
|
||||
/// Extract frustum planes from a projection matrix.
|
||||
pub fn from_matrix4(mat: Matrix4<S>) -> Option<Frustum<S>> {
|
||||
Some(Frustum::new(
|
||||
match Plane::from_vector4_alt(mat.row(3).add_v(&mat.row(0))).normalize()
|
||||
{ Some(p) => p, None => return None },
|
||||
match Plane::from_vector4_alt(mat.row(3).sub_v(&mat.row(0))).normalize()
|
||||
{ Some(p) => p, None => return None },
|
||||
match Plane::from_vector4_alt(mat.row(3).add_v(&mat.row(1))).normalize()
|
||||
{ Some(p) => p, None => return None },
|
||||
match Plane::from_vector4_alt(mat.row(3).sub_v(&mat.row(1))).normalize()
|
||||
{ Some(p) => p, None => return None },
|
||||
match Plane::from_vector4_alt(mat.row(3).add_v(&mat.row(2))).normalize()
|
||||
{ Some(p) => p, None => return None },
|
||||
match Plane::from_vector4_alt(mat.row(3).sub_v(&mat.row(2))).normalize()
|
||||
{ Some(p) => p, None => return None }
|
||||
))
|
||||
}
|
||||
|
||||
/// Find the spatial relation of a bound inside this frustum.
|
||||
pub fn contains<B: Bound<S>>(&self, bound: &B) -> Relation {
|
||||
[&self.left, &self.right, &self.top, &self.bottom, &self.near, &self.far]
|
||||
.iter().fold(Relation::In, |cur, p| {
|
||||
let r = bound.relate(p);
|
||||
// if we see Cross, then the result is Cross
|
||||
// if we see In, then we keep the old result
|
||||
// otherwise, take the current result
|
||||
if cur == Relation::Cross || r == Relation::In {
|
||||
cur
|
||||
}else {
|
||||
r
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
|
21
src/plane.rs
21
src/plane.rs
|
@ -17,7 +17,7 @@ use std::fmt;
|
|||
|
||||
use approx::ApproxEq;
|
||||
use intersect::Intersect;
|
||||
use num::{BaseFloat, Zero, zero};
|
||||
use num::{BaseFloat, one, Zero, zero};
|
||||
use point::{Point, Point3};
|
||||
use ray::Ray3;
|
||||
use vector::{Vector3, Vector4};
|
||||
|
@ -65,9 +65,13 @@ impl<S: BaseFloat> Plane<S> {
|
|||
|
||||
/// Construct a plane from the components of a four-dimensional vector
|
||||
pub fn from_vector4(v: Vector4<S>) -> Plane<S> {
|
||||
match v {
|
||||
Vector4 { x, y, z, w } => Plane { n: Vector3::new(x, y, z), d: w },
|
||||
}
|
||||
Plane { n: Vector3::new(v.x, v.y, v.z), d: v.w }
|
||||
}
|
||||
|
||||
/// Construct a plane from the components of a four-dimensional vector
|
||||
/// Assuming alternative representation: `A*x + B*y + C*z + D = 0`
|
||||
pub fn from_vector4_alt(v: Vector4<S>) -> Plane<S> {
|
||||
Plane { n: Vector3::new(v.x, v.y, v.z), d: -v.w }
|
||||
}
|
||||
|
||||
/// Constructs a plane that passes through the the three points `a`, `b` and `c`
|
||||
|
@ -94,6 +98,15 @@ impl<S: BaseFloat> Plane<S> {
|
|||
pub fn from_point_normal(p: Point3<S>, n: Vector3<S>) -> Plane<S> {
|
||||
Plane { n: n, d: p.dot(&n) }
|
||||
}
|
||||
|
||||
/// Normalize a plane.
|
||||
pub fn normalize(&self) -> Option<Plane<S>> {
|
||||
if self.n.approx_eq(&zero()) { None }
|
||||
else {
|
||||
let denom = one::<S>() / self.n.length();
|
||||
Some(Plane::new(self.n.mul_s(denom), self.d*denom))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<S: BaseFloat> Intersect<Option<Point3<S>>> for (Plane<S>, Ray3<S>) {
|
||||
|
|
|
@ -97,7 +97,7 @@ impl<S: BaseFloat, A: Angle<S>> PerspectiveFov<S, A> {
|
|||
impl<S: BaseFloat + 'static, A: Angle<S>> Projection<S> for PerspectiveFov<S, A> {
|
||||
fn to_frustum(&self) -> Frustum<S> {
|
||||
// TODO: Could this be faster?
|
||||
Frustum::from_matrix4(self.to_matrix4())
|
||||
Frustum::from_matrix4(self.to_matrix4()).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -153,7 +153,7 @@ pub struct Perspective<S> {
|
|||
impl<S: BaseFloat + 'static> Projection<S> for Perspective<S> {
|
||||
fn to_frustum(&self) -> Frustum<S> {
|
||||
// TODO: Could this be faster?
|
||||
Frustum::from_matrix4(self.to_matrix4())
|
||||
Frustum::from_matrix4(self.to_matrix4()).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
40
tests/frustum.rs
Normal file
40
tests/frustum.rs
Normal file
|
@ -0,0 +1,40 @@
|
|||
// Copyright 2015 The CGMath Developers. For a full listing of the authors,
|
||||
// refer to the AUTHORS file at the top-level directory of this distribution.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
extern crate cgmath;
|
||||
|
||||
use cgmath::{PerspectiveFov, Point3, Projection, Relation, Sphere, rad};
|
||||
|
||||
#[test]
|
||||
fn test_contains() {
|
||||
let frustum = PerspectiveFov {
|
||||
fovy: rad(1f32),
|
||||
aspect: 1f32,
|
||||
near: 1f32,
|
||||
far: 10f32,
|
||||
}.to_frustum();
|
||||
assert_eq!(frustum.contains(&Sphere {
|
||||
center: Point3::new(0f32, 0f32, -5f32),
|
||||
radius: 1f32,
|
||||
}), Relation::In);
|
||||
assert_eq!(frustum.contains(&Sphere {
|
||||
center: Point3::new(0f32, 3f32, -5f32),
|
||||
radius: 1f32,
|
||||
}), Relation::Cross);
|
||||
assert_eq!(frustum.contains(&Sphere {
|
||||
center: Point3::new(0f32, 0f32, 5f32),
|
||||
radius: 1f32,
|
||||
}), Relation::Out);
|
||||
}
|
Loading…
Reference in a new issue