dumbmath/aabb.rs
1// Copyright 2015 Nicholas Bishop
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::f32;
16use vec3f::Vec3f;
17
18pub struct Aabb3f {
19 pub min: Vec3f,
20 pub max: Vec3f
21}
22
23impl Aabb3f {
24 /// Create an empty Aabb3f with min initialized to +infinity and
25 /// max is initialized to -infinity.
26 pub fn new() -> Aabb3f {
27 Aabb3f {
28 min: Vec3f::from_scalar(f32::INFINITY),
29 max: Vec3f::from_scalar(f32::NEG_INFINITY)
30 }
31 }
32
33 pub fn from_point(point: Vec3f) -> Aabb3f {
34 Aabb3f {
35 min: point,
36 max: point
37 }
38 }
39
40 /// True if the point intersects the box
41 pub fn contains_point(&self, point: Vec3f) -> bool {
42 (self.min.x <= point.x &&
43 self.min.y <= point.y &&
44 self.min.z <= point.z &&
45
46 self.max.x >= point.x &&
47 self.max.y >= point.y &&
48 self.max.z >= point.z)
49 }
50}
51
52#[test]
53fn test_aabb3f_contains_point() {
54 use vec3f::ZERO_3F;
55 assert!(!Aabb3f::new().contains_point(ZERO_3F));
56 assert!(Aabb3f::from_point(ZERO_3F).contains_point(ZERO_3F));
57}