use crate::{Error, Path64, PathD, PathKind, Paths64, PathsD, validate_path_d, validate_path64};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "snake_case"))]
#[repr(u8)]
pub enum ClipType {
Intersection = 1,
Union = 2,
Difference = 3,
Xor = 4,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "snake_case"))]
#[repr(u8)]
pub enum FillRule {
EvenOdd = 0,
NonZero = 1,
Positive = 2,
Negative = 3,
}
#[derive(Clone, Copy, Debug)]
pub struct BooleanRequest<'a> {
pub subjects: &'a [Path64],
pub clips: &'a [Path64],
pub clip_type: ClipType,
pub fill_rule: FillRule,
}
impl<'a> BooleanRequest<'a> {
#[must_use]
pub const fn new(
subjects: &'a [Path64],
clips: &'a [Path64],
clip_type: ClipType,
fill_rule: FillRule,
) -> Self {
Self { subjects, clips, clip_type, fill_rule }
}
}
#[derive(Clone, Copy, Debug)]
pub struct BooleanRequestD<'a> {
pub subjects: &'a [PathD],
pub clips: &'a [PathD],
pub clip_type: ClipType,
pub fill_rule: FillRule,
}
impl<'a> BooleanRequestD<'a> {
#[must_use]
pub const fn new(
subjects: &'a [PathD],
clips: &'a [PathD],
clip_type: ClipType,
fill_rule: FillRule,
) -> Self {
Self { subjects, clips, clip_type, fill_rule }
}
}
pub fn validate_request(request: &BooleanRequest<'_>) -> Result<(), Error> {
for path in request.subjects.iter().chain(request.clips) {
validate_path64(path, PathKind::Closed)?;
}
Ok(())
}
pub fn validate_request_d(request: &BooleanRequestD<'_>) -> Result<(), Error> {
for path in request.subjects.iter().chain(request.clips) {
validate_path_d(path, PathKind::Closed)?;
}
Ok(())
}
pub fn boolean_op(request: BooleanRequest<'_>) -> Result<Paths64, Error> {
validate_request(&request)?;
crate::boolean::boolean_op64(request)
}
pub fn boolean_op_d(request: BooleanRequestD<'_>) -> Result<PathsD, Error> {
validate_request_d(&request)?;
crate::boolean::boolean_op_d(request)
}
pub fn intersection(subjects: &[Path64], clips: &[Path64]) -> Result<Paths64, Error> {
boolean_op(BooleanRequest::new(subjects, clips, ClipType::Intersection, FillRule::EvenOdd))
}
pub fn union(subjects: &[Path64], clips: &[Path64]) -> Result<Paths64, Error> {
boolean_op(BooleanRequest::new(subjects, clips, ClipType::Union, FillRule::EvenOdd))
}
pub fn difference(subjects: &[Path64], clips: &[Path64]) -> Result<Paths64, Error> {
boolean_op(BooleanRequest::new(subjects, clips, ClipType::Difference, FillRule::EvenOdd))
}
pub fn xor(subjects: &[Path64], clips: &[Path64]) -> Result<Paths64, Error> {
boolean_op(BooleanRequest::new(subjects, clips, ClipType::Xor, FillRule::EvenOdd))
}
pub fn intersection_d(subjects: &[PathD], clips: &[PathD]) -> Result<PathsD, Error> {
boolean_op_d(BooleanRequestD::new(subjects, clips, ClipType::Intersection, FillRule::EvenOdd))
}
pub fn union_d(subjects: &[PathD], clips: &[PathD]) -> Result<PathsD, Error> {
boolean_op_d(BooleanRequestD::new(subjects, clips, ClipType::Union, FillRule::EvenOdd))
}
pub fn difference_d(subjects: &[PathD], clips: &[PathD]) -> Result<PathsD, Error> {
boolean_op_d(BooleanRequestD::new(subjects, clips, ClipType::Difference, FillRule::EvenOdd))
}
pub fn xor_d(subjects: &[PathD], clips: &[PathD]) -> Result<PathsD, Error> {
boolean_op_d(BooleanRequestD::new(subjects, clips, ClipType::Xor, FillRule::EvenOdd))
}
pub fn intersection_path(subject: &Path64, clip: &Path64) -> Result<Paths64, Error> {
intersection(std::slice::from_ref(subject), std::slice::from_ref(clip))
}
pub fn union_path(subject: &Path64, clip: &Path64) -> Result<Paths64, Error> {
union(std::slice::from_ref(subject), std::slice::from_ref(clip))
}
pub fn difference_path(subject: &Path64, clip: &Path64) -> Result<Paths64, Error> {
difference(std::slice::from_ref(subject), std::slice::from_ref(clip))
}
pub fn xor_path(subject: &Path64, clip: &Path64) -> Result<Paths64, Error> {
xor(std::slice::from_ref(subject), std::slice::from_ref(clip))
}
pub fn intersection_path_d(subject: &PathD, clip: &PathD) -> Result<PathsD, Error> {
intersection_d(std::slice::from_ref(subject), std::slice::from_ref(clip))
}
pub fn union_path_d(subject: &PathD, clip: &PathD) -> Result<PathsD, Error> {
union_d(std::slice::from_ref(subject), std::slice::from_ref(clip))
}
pub fn difference_path_d(subject: &PathD, clip: &PathD) -> Result<PathsD, Error> {
difference_d(std::slice::from_ref(subject), std::slice::from_ref(clip))
}
pub fn xor_path_d(subject: &PathD, clip: &PathD) -> Result<PathsD, Error> {
xor_d(std::slice::from_ref(subject), std::slice::from_ref(clip))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn exposes_stable_operation_values() {
assert_eq!(ClipType::Intersection as u8, 1);
assert_eq!(ClipType::Union as u8, 2);
assert_eq!(ClipType::Difference as u8, 3);
assert_eq!(ClipType::Xor as u8, 4);
assert_eq!(FillRule::EvenOdd as u8, 0);
assert_eq!(FillRule::NonZero as u8, 1);
assert_eq!(FillRule::Positive as u8, 2);
assert_eq!(FillRule::Negative as u8, 3);
}
#[test]
fn constructors_preserve_borrowed_inputs_and_options() {
let integer_subject = vec![crate::Point64::new(0, 0)];
let integer_clip = vec![crate::Point64::new(1, 1)];
let integer_request = BooleanRequest::new(
std::slice::from_ref(&integer_subject),
std::slice::from_ref(&integer_clip),
ClipType::Difference,
FillRule::Positive,
);
assert!(std::ptr::eq(integer_request.subjects[0].as_ptr(), integer_subject.as_ptr()));
assert!(std::ptr::eq(integer_request.clips[0].as_ptr(), integer_clip.as_ptr()));
assert_eq!(integer_request.clip_type, ClipType::Difference);
assert_eq!(integer_request.fill_rule, FillRule::Positive);
let double_subject = vec![crate::PointD::new(0.0, 0.0)];
let double_clip = vec![crate::PointD::new(1.0, 1.0)];
let double_request = BooleanRequestD::new(
std::slice::from_ref(&double_subject),
std::slice::from_ref(&double_clip),
ClipType::Xor,
FillRule::Negative,
);
assert!(std::ptr::eq(double_request.subjects[0].as_ptr(), double_subject.as_ptr()));
assert!(std::ptr::eq(double_request.clips[0].as_ptr(), double_clip.as_ptr()));
assert_eq!(double_request.clip_type, ClipType::Xor);
assert_eq!(double_request.fill_rule, FillRule::Negative);
}
#[test]
fn validates_then_executes_union_with_empty_clip() {
let square =
vec![crate::Point64::new(0, 0), crate::Point64::new(1, 0), crate::Point64::new(1, 1)];
let request = BooleanRequest {
subjects: &[square],
clips: &[],
clip_type: ClipType::Union,
fill_rule: FillRule::NonZero,
};
assert_eq!(validate_request(&request), Ok(()));
assert_eq!(boolean_op(request).expect("valid request").len(), 1);
}
#[test]
fn rejects_invalid_boolean_input_before_kernel() {
let request = BooleanRequest {
subjects: &[vec![crate::Point64::new(0, 0)]],
clips: &[],
clip_type: ClipType::Intersection,
fill_rule: FillRule::EvenOdd,
};
assert!(matches!(boolean_op(request), Err(Error::InvalidPath { .. })));
}
#[test]
fn validates_double_requests() {
let square = vec![
crate::PointD::new(0.0, 0.0),
crate::PointD::new(1.0, 0.0),
crate::PointD::new(1.0, 1.0),
];
let request = BooleanRequestD {
subjects: &[square],
clips: &[],
clip_type: ClipType::Union,
fill_rule: FillRule::NonZero,
};
assert!(validate_request_d(&request).is_ok());
assert_eq!(crate::boolean_op_d(request).expect("valid request").len(), 1);
let bad = BooleanRequestD {
subjects: &[vec![crate::PointD::new(0.0, 0.0), crate::PointD::new(f64::NAN, 0.0)]],
clips: &[],
clip_type: ClipType::Union,
fill_rule: FillRule::EvenOdd,
};
assert!(matches!(validate_request_d(&bad), Err(Error::InvalidPath { .. })));
}
#[test]
#[allow(clippy::cast_precision_loss)]
fn convenience_operations_use_even_odd_semantics() {
let subject = vec![
crate::Point64::new(0, 0),
crate::Point64::new(10, 0),
crate::Point64::new(10, 10),
crate::Point64::new(0, 10),
];
let clip = vec![
crate::Point64::new(5, 0),
crate::Point64::new(15, 0),
crate::Point64::new(15, 10),
crate::Point64::new(5, 10),
];
let subjects = [subject];
let clips = [clip];
assert_eq!(intersection(&subjects, &clips).expect("intersection").len(), 1);
assert_eq!(union(&subjects, &clips).expect("union").len(), 1);
assert_eq!(difference(&subjects, &clips).expect("difference").len(), 1);
assert_eq!(xor(&subjects, &clips).expect("xor").len(), 2);
let subjects_d = subjects
.iter()
.map(|path| {
path.iter()
.map(|point| crate::PointD::new(point.x as f64, point.y as f64))
.collect()
})
.collect::<Vec<PathD>>();
let clips_d = clips
.iter()
.map(|path| {
path.iter()
.map(|point| crate::PointD::new(point.x as f64, point.y as f64))
.collect()
})
.collect::<Vec<PathD>>();
assert_eq!(intersection_d(&subjects_d, &clips_d).expect("intersection_d").len(), 1);
assert_eq!(union_d(&subjects_d, &clips_d).expect("union_d").len(), 1);
assert_eq!(difference_d(&subjects_d, &clips_d).expect("difference_d").len(), 1);
assert_eq!(xor_d(&subjects_d, &clips_d).expect("xor_d").len(), 2);
}
#[test]
fn single_ring_helpers_match_collection_helpers() {
let integer_subject = vec![
crate::Point64::new(0, 0),
crate::Point64::new(10, 0),
crate::Point64::new(10, 10),
crate::Point64::new(0, 10),
];
let integer_clip = vec![
crate::Point64::new(5, 0),
crate::Point64::new(15, 0),
crate::Point64::new(15, 10),
crate::Point64::new(5, 10),
];
let integer_subjects = std::slice::from_ref(&integer_subject);
let integer_clips = std::slice::from_ref(&integer_clip);
assert_eq!(
intersection_path(&integer_subject, &integer_clip),
intersection(integer_subjects, integer_clips)
);
assert_eq!(
union_path(&integer_subject, &integer_clip),
union(integer_subjects, integer_clips)
);
assert_eq!(
difference_path(&integer_subject, &integer_clip),
difference(integer_subjects, integer_clips)
);
assert_eq!(xor_path(&integer_subject, &integer_clip), xor(integer_subjects, integer_clips));
let double_subject = vec![
crate::PointD::new(0.0, 0.0),
crate::PointD::new(10.0, 0.0),
crate::PointD::new(10.0, 10.0),
crate::PointD::new(0.0, 10.0),
];
let double_clip = vec![
crate::PointD::new(5.0, 0.0),
crate::PointD::new(15.0, 0.0),
crate::PointD::new(15.0, 10.0),
crate::PointD::new(5.0, 10.0),
];
let double_subjects = std::slice::from_ref(&double_subject);
let double_clips = std::slice::from_ref(&double_clip);
assert_eq!(
intersection_path_d(&double_subject, &double_clip),
intersection_d(double_subjects, double_clips)
);
assert_eq!(
union_path_d(&double_subject, &double_clip),
union_d(double_subjects, double_clips)
);
assert_eq!(
difference_path_d(&double_subject, &double_clip),
difference_d(double_subjects, double_clips)
);
assert_eq!(xor_path_d(&double_subject, &double_clip), xor_d(double_subjects, double_clips));
}
}