use crate::core::geo::geometry::Geometry;
use crate::core::geo::rectangle2d::{Rectangle2D, create_from_xy_rectangle};
use crate::core::geo::xy_geometry::XYGeometry;
use crate::core::util::CoreHelper;
use crate::core::util::bit_util::BitUtil;
use crate::core::util::error::lucene_error::LuceneError;
use crate::core::util::error::lucene_error::Result;
use std::fmt;
use std::fmt::{Display, Formatter};
#[derive(Debug, Clone)]
pub struct XYRectangle {
pub min_x: f32,
pub max_x: f32,
pub min_y: f32,
pub max_y: f32,
}
impl XYRectangle {
pub fn new(min_x: f32, max_x: f32, min_y: f32, max_y: f32) -> Result<Self> {
if min_x > max_x {
return Err(LuceneError::illegal_argument(format!(
"minX must be lower than maxX, got {min_x} > {max_x}"
)));
}
if min_y > max_y {
return Err(LuceneError::illegal_argument(format!(
"minY must be lower than maxY, got {min_y} > {max_y}"
)));
}
Ok(Self {
min_x: Self::check_val(min_x)?,
max_x: Self::check_val(max_x)?,
min_y: Self::check_val(min_y)?,
max_y: Self::check_val(max_y)?,
})
}
pub fn from_point_distance(x: f32, y: f32, radius: f32) -> Result<Self> {
Self::check_val(x)?;
Self::check_val(y)?;
if radius < 0.0 {
return Err(LuceneError::illegal_argument(format!(
"radius must be bigger than 0, got {radius}"
)));
}
if !radius.is_finite() {
return Err(LuceneError::illegal_argument(format!(
"radius must be finite, got {radius}"
)));
}
let distance_box = radius.next_up();
let min_x = CoreHelper::max_f32(-f32::MAX, x - distance_box);
let max_x = CoreHelper::min_f32(f32::MAX, x + distance_box);
let min_y = CoreHelper::max_f32(-f32::MAX, y - distance_box);
let max_y = CoreHelper::min_f32(f32::MAX, y + distance_box);
Self::new(min_x, max_x, min_y, max_y)
}
fn check_val(v: f32) -> Result<f32> {
if !v.is_finite() {
return Err(LuceneError::illegal_argument(format!(
"invalid value {v}: must be finite"
)));
}
Ok(v)
}
}
impl Eq for XYRectangle {}
impl PartialEq for XYRectangle {
fn eq(&self, other: &Self) -> bool {
CoreHelper::compare_f32(self.min_x, other.min_x).is_eq()
&& CoreHelper::compare_f32(self.min_y, other.min_y).is_eq()
&& CoreHelper::compare_f32(self.max_x, other.max_x).is_eq()
&& CoreHelper::compare_f32(self.max_y, other.max_y).is_eq()
}
}
impl std::hash::Hash for XYRectangle {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
(BitUtil::float_to_int_bits(self.min_x) as u32).hash(state);
(BitUtil::float_to_int_bits(self.min_y) as u32).hash(state);
(BitUtil::float_to_int_bits(self.max_x) as u32).hash(state);
(BitUtil::float_to_int_bits(self.max_y) as u32).hash(state);
}
}
impl Geometry for XYRectangle {
type Component2D = Rectangle2D;
fn to_component2d(&self) -> Result<Self::Component2D> {
Ok(create_from_xy_rectangle(self))
}
}
impl XYGeometry for XYRectangle {}
impl Display for XYRectangle {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(
f,
"XYRectangle(x={} TO {} y={} TO {})",
self.min_x, self.max_x, self.min_y, self.max_y
)
}
}