use crate::entities::{Entity, EntityCommon};
use crate::types::{BoundingBox3D, Color, Handle, LineWeight, Transparency, Vector3};
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct XLine {
pub common: EntityCommon,
pub base_point: Vector3,
pub direction: Vector3,
}
impl XLine {
pub fn new(base_point: Vector3, direction: Vector3) -> Self {
Self {
common: EntityCommon::default(),
base_point,
direction: direction.normalize(),
}
}
pub fn from_points(point1: Vector3, point2: Vector3) -> Self {
let direction = (point2 - point1).normalize();
Self::new(point1, direction)
}
pub fn horizontal(through: Vector3) -> Self {
Self::new(through, Vector3::UNIT_X)
}
pub fn vertical(through: Vector3) -> Self {
Self::new(through, Vector3::UNIT_Y)
}
pub fn along_z(through: Vector3) -> Self {
Self::new(through, Vector3::UNIT_Z)
}
pub fn at_angle(through: Vector3, angle_radians: f64) -> Self {
let direction = Vector3::new(angle_radians.cos(), angle_radians.sin(), 0.0);
Self::new(through, direction)
}
pub fn point_at(&self, t: f64) -> Vector3 {
self.base_point + self.direction * t
}
pub fn closest_point(&self, point: Vector3) -> Vector3 {
let v = point - self.base_point;
let t = v.dot(&self.direction);
self.point_at(t)
}
pub fn distance_to_point(&self, point: Vector3) -> f64 {
let closest = self.closest_point(point);
closest.distance(&point)
}
pub fn contains_point(&self, point: Vector3, tolerance: f64) -> bool {
self.distance_to_point(point) <= tolerance
}
pub fn angle_xy(&self) -> f64 {
self.direction.y.atan2(self.direction.x)
}
pub fn angle_from_xy(&self) -> f64 {
let xy_length = (self.direction.x.powi(2) + self.direction.y.powi(2)).sqrt();
self.direction.z.atan2(xy_length)
}
pub fn is_parallel_to(&self, other: &XLine, tolerance: f64) -> bool {
let cross = self.direction.cross(&other.direction);
cross.length() <= tolerance
}
pub fn is_perpendicular_to(&self, other: &XLine, tolerance: f64) -> bool {
self.direction.dot(&other.direction).abs() <= tolerance
}
pub fn intersection(&self, other: &XLine, tolerance: f64) -> Option<Vector3> {
let cross = self.direction.cross(&other.direction);
if cross.length() <= tolerance {
return None;
}
let w = self.base_point - other.base_point;
let a = self.direction.dot(&self.direction);
let b = self.direction.dot(&other.direction);
let c = other.direction.dot(&other.direction);
let d = self.direction.dot(&w);
let e = other.direction.dot(&w);
let denom = a * c - b * b;
if denom.abs() <= tolerance {
return None;
}
let t1 = (b * e - c * d) / denom;
let t2 = (a * e - b * d) / denom;
let p1 = self.point_at(t1);
let p2 = other.point_at(t2);
if p1.distance(&p2) <= tolerance {
Some(p1)
} else {
None
}
}
pub fn set_direction(&mut self, direction: Vector3) {
self.direction = direction.normalize();
}
pub fn with_layer(mut self, layer: impl Into<String>) -> Self {
self.common.layer = layer.into();
self
}
pub fn with_color(mut self, color: Color) -> Self {
self.common.color = color;
self
}
}
impl Default for XLine {
fn default() -> Self {
Self::new(Vector3::ZERO, Vector3::UNIT_X)
}
}
impl Entity for XLine {
fn handle(&self) -> Handle {
self.common.handle
}
fn set_handle(&mut self, handle: Handle) {
self.common.handle = handle;
}
fn layer(&self) -> &str {
&self.common.layer
}
fn set_layer(&mut self, layer: String) {
self.common.layer = layer;
}
fn color(&self) -> Color {
self.common.color
}
fn set_color(&mut self, color: Color) {
self.common.color = color;
}
fn line_weight(&self) -> LineWeight {
self.common.line_weight
}
fn set_line_weight(&mut self, weight: LineWeight) {
self.common.line_weight = weight;
}
fn transparency(&self) -> Transparency {
self.common.transparency
}
fn set_transparency(&mut self, transparency: Transparency) {
self.common.transparency = transparency;
}
fn is_invisible(&self) -> bool {
self.common.invisible
}
fn set_invisible(&mut self, invisible: bool) {
self.common.invisible = invisible;
}
fn bounding_box(&self) -> BoundingBox3D {
let far_positive = self.point_at(1e10);
let far_negative = self.point_at(-1e10);
BoundingBox3D::from_points(&[far_negative, far_positive]).unwrap_or_default()
}
fn translate(&mut self, offset: Vector3) {
super::translate::translate_xline(self, offset);
}
fn entity_type(&self) -> &'static str {
"XLINE"
}
fn apply_transform(&mut self, transform: &crate::types::Transform) {
super::transform::transform_xline(self, transform);
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_xline_creation() {
let xline = XLine::new(
Vector3::new(1.0, 2.0, 3.0),
Vector3::new(2.0, 0.0, 0.0),
);
assert_eq!(xline.base_point, Vector3::new(1.0, 2.0, 3.0));
assert!((xline.direction.length() - 1.0).abs() < 1e-10);
assert!((xline.direction.x - 1.0).abs() < 1e-10);
}
#[test]
fn test_xline_from_points() {
let xline = XLine::from_points(
Vector3::new(0.0, 0.0, 0.0),
Vector3::new(10.0, 0.0, 0.0),
);
assert_eq!(xline.base_point, Vector3::ZERO);
assert!((xline.direction.x - 1.0).abs() < 1e-10);
}
#[test]
fn test_xline_point_at() {
let xline = XLine::horizontal(Vector3::ZERO);
let point_pos = xline.point_at(5.0);
assert_eq!(point_pos, Vector3::new(5.0, 0.0, 0.0));
let point_neg = xline.point_at(-5.0);
assert_eq!(point_neg, Vector3::new(-5.0, 0.0, 0.0));
}
#[test]
fn test_xline_closest_point() {
let xline = XLine::horizontal(Vector3::ZERO);
let closest = xline.closest_point(Vector3::new(5.0, 3.0, 0.0));
assert_eq!(closest, Vector3::new(5.0, 0.0, 0.0));
let closest = xline.closest_point(Vector3::new(-5.0, 3.0, 0.0));
assert_eq!(closest, Vector3::new(-5.0, 0.0, 0.0));
}
#[test]
fn test_xline_distance_to_point() {
let xline = XLine::horizontal(Vector3::ZERO);
let distance = xline.distance_to_point(Vector3::new(5.0, 3.0, 4.0));
assert!((distance - 5.0).abs() < 1e-10);
}
#[test]
fn test_xline_parallel() {
let xline1 = XLine::horizontal(Vector3::ZERO);
let xline2 = XLine::horizontal(Vector3::new(0.0, 5.0, 0.0));
assert!(xline1.is_parallel_to(&xline2, 1e-10));
}
#[test]
fn test_xline_perpendicular() {
let xline1 = XLine::horizontal(Vector3::ZERO);
let xline2 = XLine::vertical(Vector3::ZERO);
assert!(xline1.is_perpendicular_to(&xline2, 1e-10));
}
#[test]
fn test_xline_intersection() {
let xline1 = XLine::horizontal(Vector3::ZERO);
let xline2 = XLine::vertical(Vector3::new(5.0, 0.0, 0.0));
let intersection = xline1.intersection(&xline2, 1e-10);
assert!(intersection.is_some());
let point = intersection.unwrap();
assert!((point.x - 5.0).abs() < 1e-10);
assert!(point.y.abs() < 1e-10);
}
#[test]
fn test_xline_no_intersection_parallel() {
let xline1 = XLine::horizontal(Vector3::ZERO);
let xline2 = XLine::horizontal(Vector3::new(0.0, 5.0, 0.0));
let intersection = xline1.intersection(&xline2, 1e-10);
assert!(intersection.is_none());
}
#[test]
fn test_xline_translate() {
let mut xline = XLine::horizontal(Vector3::ZERO);
xline.translate(Vector3::new(1.0, 2.0, 3.0));
assert_eq!(xline.base_point, Vector3::new(1.0, 2.0, 3.0));
assert_eq!(xline.direction, Vector3::UNIT_X);
}
#[test]
fn test_xline_at_angle() {
let xline = XLine::at_angle(Vector3::ZERO, std::f64::consts::FRAC_PI_4);
assert!((xline.direction.x - xline.direction.y).abs() < 1e-10);
}
}