cgmath/src/sphere.rs

60 lines
1.9 KiB
Rust
Raw Normal View History

2014-05-26 17:10:04 +00:00
// Copyright 2013-2014 The CGMath Developers. For a full listing of the authors,
2013-09-05 07:19:31 +00:00
// 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.
//! Bounding sphere
use bound::*;
2013-11-07 02:08:37 +00:00
use intersect::Intersect;
use num::{BaseNum, BaseFloat, zero};
2013-11-07 02:08:37 +00:00
use point::{Point, Point3};
use plane::Plane;
2013-11-07 02:08:37 +00:00
use ray::Ray3;
use vector::Vector;
2015-01-03 21:29:26 +00:00
#[derive(Copy, Clone, PartialEq, RustcEncodable, RustcDecodable)]
2013-09-05 07:19:31 +00:00
pub struct Sphere<S> {
2014-04-01 11:00:17 +00:00
pub center: Point3<S>,
pub radius: S,
2013-09-05 07:19:31 +00:00
}
2013-11-07 02:08:37 +00:00
2014-05-26 17:10:04 +00:00
impl<S: BaseFloat> Intersect<Option<Point3<S>>> for (Sphere<S>, Ray3<S>) {
2013-11-07 02:08:37 +00:00
fn intersection(&self) -> Option<Point3<S>> {
match *self {
2013-11-09 01:15:51 +00:00
(ref s, ref r) => {
2013-11-07 02:08:37 +00:00
let l = s.center.sub_p(&r.origin);
let tca = l.dot(&r.direction);
2014-05-26 17:10:04 +00:00
if tca < zero() { return None; }
2013-11-07 02:08:37 +00:00
let d2 = l.dot(&l) - tca*tca;
2014-01-25 17:59:13 +00:00
if d2 > s.radius*s.radius { return None; }
2014-04-02 09:24:04 +00:00
let thc = (s.radius*s.radius - d2).sqrt();
2013-11-07 02:08:37 +00:00
Some(r.origin.add_v(&r.direction.mul_s(tca - thc)))
}
}
}
}
impl<S: BaseNum> Bound<S> for Sphere<S> {
fn relate(&self, plane: &Plane<S>) -> Relation {
let dist = self.center.dot(&plane.n) - plane.d;
if dist > self.radius {
Relation::In
}else if dist < - self.radius {
Relation::Out
}else {
Relation::Cross
}
}
}