Moved the bound tests around. Added one for Aabb3

This commit is contained in:
Dzmitry Malyshau 2015-03-13 11:41:31 +03:00
parent d3d7241c86
commit 4d71e8aad9
4 changed files with 45 additions and 55 deletions

View file

@ -19,9 +19,10 @@ use cgmath::{Aabb, Aabb2, Aabb3};
use cgmath::{Point2, Point3};
use cgmath::{Vector2, Vector3};
use cgmath::{Ray, Intersect};
use cgmath::{Plane, Bound, Relation};
#[test]
fn test_aabb() {
fn test_general() {
let aabb = Aabb2::new(Point2::new(-20is, 30is), Point2::new(10is, -10is));
assert_eq!(aabb.min(), &Point2::new(-20is, -10is));
assert_eq!(aabb.max(), &Point2::new(10is, 30is));
@ -65,7 +66,7 @@ fn test_aabb() {
}
#[test]
fn test_aabb_ray_intersect() {
fn test_ray_intersect() {
let aabb = Aabb2::new(Point2::new(-5.0f32, 5.0), Point2::new(5.0, 10.0));
let ray1 = Ray::new(Point2::new(0.0f32, 0.0), Vector2::new(0.0, 1.0));
let ray2 = Ray::new(Point2::new(-10.0f32, 0.0), Vector2::new(2.5, 1.0));
@ -77,3 +78,14 @@ fn test_aabb_ray_intersect() {
assert_eq!((ray3, aabb).intersection(), None);
assert_eq!((ray4, aabb).intersection(), Some(Point2::new(5.0, 9.0)));
}
#[test]
fn test_bound() {
let aabb = Aabb3::new(Point3::new(-5.0f32, 5.0, 0.0), Point3::new(5.0, 10.0, 1.0));
let plane1 = Plane::from_point_normal(Point3::new(0f32, 0.0, 0.0), Vector3::new(0f32, 0.0, 1.0));
let plane2 = Plane::from_point_normal(Point3::new(-5.0f32, 4.0, 0.0), Vector3::new(0f32, 1.0, 0.0));
let plane3 = Plane::from_point_normal(Point3::new(6.0f32, 0.0, 0.0), Vector3::new(1f32, 0.0, 0.0));
assert_eq!(aabb.relate(&plane1), Relation::Cross);
assert_eq!(aabb.relate(&plane2), Relation::In);
assert_eq!(aabb.relate(&plane3), Relation::Out);
}

View file

@ -1,51 +0,0 @@
// Copyright 2013-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::{Aabb3, Bound, Plane, Point, Point3, Relation, Sphere, Vector, vec3};
#[test]
fn point() {
let point = Point3::new(1f32, 2.0, 3.0);
let normal = vec3(0f32, -0.8, -0.36);
let plane = Plane::from_point_normal(point, normal);
assert_eq!(point.relate(&plane), Relation::Cross);
assert_eq!(point.add_v(&normal).relate(&plane), Relation::In);
assert_eq!(point.add_v(&normal.mul_s(-1.0)).relate(&plane), Relation::Out);
}
#[test]
fn sphere() {
let point = Point3::new(1f32, 2.0, 3.0);
let sphere = Sphere { center: point, radius: 1.0 };
let normal = vec3(0f32, 0.0, 1.0);
assert_eq!(sphere.relate(
&Plane::from_point_normal(point, normal)
), Relation::Cross);
assert_eq!(sphere.relate(
&Plane::from_point_normal(point.add_v(&normal.mul_s(-3.0)), normal),
), Relation::In);
assert_eq!(sphere.relate(
&Plane::from_point_normal(point.add_v(&normal.mul_s(3.0)), normal),
), Relation::Out);
}
#[test]
fn aabb() {
//TODO
}

View file

@ -16,11 +16,23 @@
extern crate cgmath;
use cgmath::Point3;
use cgmath::ApproxEq;
use cgmath::{Point, Point3, Vector, Vector3};
use cgmath::{Bound, Relation, Plane};
use cgmath::{ApproxEq};
#[test]
fn test_homogeneous() {
let p = Point3::new(1.0f64, 2.0f64, 3.0f64);
assert!(p.approx_eq( &Point3::from_homogeneous( &p.to_homogeneous() ) ));
}
#[test]
fn test_bound() {
let point = Point3::new(1f32, 2.0, 3.0);
let normal = Vector3::new(0f32, -0.8, -0.36);
let plane = Plane::from_point_normal(point, normal);
assert_eq!(point.relate(&plane), Relation::Cross);
assert_eq!(point.add_v(&normal).relate(&plane), Relation::In);
assert_eq!(point.add_v(&normal.mul_s(-1.0)).relate(&plane), Relation::Out);
}

View file

@ -31,3 +31,20 @@ fn test_intersection() {
assert_eq!((sphere,r2).intersection(), Some(Point3::new(1f64, 0f64, 0f64)));
assert_eq!((sphere,r3).intersection(), None);
}
#[test]
fn test_bound() {
let point = Point3::new(1f32, 2.0, 3.0);
let sphere = Sphere { center: point, radius: 1.0 };
let normal = vec3(0f32, 0.0, 1.0);
assert_eq!(sphere.relate(
&Plane::from_point_normal(point, normal)
), Relation::Cross);
assert_eq!(sphere.relate(
&Plane::from_point_normal(point.add_v(&normal.mul_s(-3.0)), normal),
), Relation::In);
assert_eq!(sphere.relate(
&Plane::from_point_normal(point.add_v(&normal.mul_s(3.0)), normal),
), Relation::Out);
}