2014-05-26 17:10:04 +00:00
|
|
|
// Copyright 2013-2014 The CGMath Developers. For a full listing of the authors,
|
2015-03-14 02:44:59 +00:00
|
|
|
// refer to the Cargo.toml file at the top-level directory of this distribution.
|
2014-05-13 02:33:47 +00:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
//! Line segments
|
|
|
|
|
2015-03-29 21:39:47 +00:00
|
|
|
use std::marker::PhantomData;
|
2015-04-05 01:19:11 +00:00
|
|
|
|
|
|
|
use rust_num::{Zero, zero, One, one};
|
|
|
|
|
|
|
|
use num::{BaseNum, BaseFloat};
|
2014-05-13 02:33:47 +00:00
|
|
|
use point::{Point, Point2, Point3};
|
2015-03-29 21:39:47 +00:00
|
|
|
use vector::{Vector, Vector2, Vector3};
|
2014-05-30 01:42:04 +00:00
|
|
|
use ray::{Ray2};
|
2014-05-13 02:33:47 +00:00
|
|
|
use intersect::Intersect;
|
|
|
|
|
2014-05-25 10:17:57 +00:00
|
|
|
/// A generic directed line segment from `origin` to `dest`.
|
2015-01-03 21:29:26 +00:00
|
|
|
#[derive(Copy, Clone, PartialEq, RustcEncodable, RustcDecodable)]
|
2015-03-29 21:39:47 +00:00
|
|
|
pub struct Line<S, V, P> {
|
2014-05-13 02:33:47 +00:00
|
|
|
pub origin: P,
|
|
|
|
pub dest: P,
|
2015-03-29 21:39:47 +00:00
|
|
|
phantom_s: PhantomData<S>,
|
|
|
|
phantom_v: PhantomData<V>
|
2014-05-13 02:33:47 +00:00
|
|
|
}
|
|
|
|
|
2015-03-29 21:39:47 +00:00
|
|
|
impl<S: BaseNum, V: Vector<S>, P: Point<S, V>> Line<S, V, P> {
|
|
|
|
pub fn new(origin: P, dest: P) -> Line<S, V, P> {
|
|
|
|
Line {
|
|
|
|
origin: origin,
|
|
|
|
dest: dest,
|
|
|
|
phantom_v: PhantomData,
|
|
|
|
phantom_s: PhantomData
|
|
|
|
}
|
2014-05-13 02:33:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-29 21:39:47 +00:00
|
|
|
pub type Line2<S> = Line<S, Vector2<S>, Point2<S>>;
|
|
|
|
pub type Line3<S> = Line<S, Vector3<S>, Point3<S>>;
|
2014-05-13 02:33:47 +00:00
|
|
|
|
2015-03-14 02:44:59 +00:00
|
|
|
/// Determines if an intersection between a ray and a line segment is found.
|
2014-05-30 01:42:04 +00:00
|
|
|
impl<S: BaseFloat> Intersect<Option<Point2<S>>> for (Ray2<S>, Line2<S>) {
|
2014-05-13 02:33:47 +00:00
|
|
|
fn intersection(&self) -> Option<Point2<S>> {
|
2015-03-14 02:44:18 +00:00
|
|
|
let (ref ray, ref line) = *self;
|
2014-05-13 02:33:47 +00:00
|
|
|
|
2015-03-14 02:44:18 +00:00
|
|
|
let p = ray.origin;
|
|
|
|
let q = line.origin;
|
|
|
|
let r = ray.direction;
|
|
|
|
let s = Vector2::new(line.dest.x - line.origin.x, line.dest.y - line.origin.y);
|
|
|
|
let zero: S = Zero::zero();
|
2014-05-13 02:33:47 +00:00
|
|
|
|
2015-03-14 02:44:18 +00:00
|
|
|
let cross_1 = r.perp_dot(&s);
|
|
|
|
let qmp = Vector2::new(q.x - p.x, q.y - p.y);
|
|
|
|
let cross_2 = qmp.perp_dot(&r);
|
2014-05-13 02:33:47 +00:00
|
|
|
|
2015-03-14 02:44:18 +00:00
|
|
|
if cross_1 == zero {
|
|
|
|
if cross_2 != zero {
|
|
|
|
// parallel
|
|
|
|
return None;
|
|
|
|
}
|
2014-05-30 01:42:04 +00:00
|
|
|
|
2015-03-14 02:44:18 +00:00
|
|
|
// collinear
|
|
|
|
let q2mp = Vector2::new(line.dest.x - p.x, line.dest.y - p.y);
|
|
|
|
let dot_1 = qmp.dot(&r);
|
|
|
|
let dot_2 = q2mp.dot(&r);
|
|
|
|
if (dot_1 <= zero && dot_2 >= zero) || (dot_1 >= zero && dot_2 <= zero) {
|
|
|
|
return Some(p);
|
|
|
|
}
|
|
|
|
else if dot_1 >= zero && dot_2 >= zero {
|
|
|
|
if dot_1 <= dot_2 {
|
|
|
|
return Some(q);
|
2014-05-13 02:33:47 +00:00
|
|
|
}
|
2015-03-14 02:44:18 +00:00
|
|
|
else {
|
|
|
|
return Some(line.dest);
|
|
|
|
}
|
|
|
|
}
|
2014-05-13 02:33:47 +00:00
|
|
|
|
2015-03-14 02:44:18 +00:00
|
|
|
// no overlap exists
|
|
|
|
return None;
|
|
|
|
}
|
2014-05-13 02:33:47 +00:00
|
|
|
|
2015-03-14 02:44:18 +00:00
|
|
|
let t = qmp.perp_dot(&s) / cross_1;
|
|
|
|
let u = cross_2 / cross_1;
|
2014-05-13 02:33:47 +00:00
|
|
|
|
2015-03-14 02:44:18 +00:00
|
|
|
if zero <= t && u >= zero && u <= One::one() {
|
|
|
|
return Some(Point2::new(p.x + t*r.x, p.y + t*r.y));
|
2014-05-13 02:33:47 +00:00
|
|
|
}
|
2015-03-14 02:44:18 +00:00
|
|
|
|
|
|
|
return None;
|
2014-05-13 02:33:47 +00:00
|
|
|
}
|
|
|
|
}
|