use crate::core::geo::geometry::Geometry;
use crate::core::geo::point2d::{Point2D, create_from_xy_point};
use crate::core::geo::xy_encoding_utils::XYEncodingUtils;
use crate::core::geo::xy_geometry::XYGeometry;
use crate::core::util::bit_util::BitUtil;
use crate::core::util::core_helper::CoreHelper;
use crate::core::util::error::lucene_error::Result;
#[derive(Clone, Copy, Debug)]
pub struct XYPoint {
x: f32,
y: f32,
}
impl XYPoint {
pub fn new(x: f32, y: f32) -> Result<Self> {
Ok(Self {
x: XYEncodingUtils::check_val(x)?,
y: XYEncodingUtils::check_val(y)?,
})
}
pub fn get_x(&self) -> f32 {
self.x
}
pub fn get_y(&self) -> f32 {
self.y
}
}
impl Geometry for XYPoint {
type Component2D = Point2D;
fn to_component2d(&self) -> Result<Self::Component2D> {
Ok(create_from_xy_point(self))
}
}
impl XYGeometry for XYPoint {}
impl PartialEq for XYPoint {
fn eq(&self, other: &Self) -> bool {
CoreHelper::compare_f32(self.x, other.x).is_eq()
&& CoreHelper::compare_f32(self.y, other.y).is_eq()
}
}
impl Eq for XYPoint {}
impl std::hash::Hash for XYPoint {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
(BitUtil::float_to_int_bits(self.x) as u32).hash(state);
(BitUtil::float_to_int_bits(self.y) as u32).hash(state);
}
}
impl std::fmt::Display for XYPoint {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "XYPoint({},{})", self.x, self.y)
}
}