use crate::dllink::Dllink;
use crate::point::Point;
use crate::rdllist::RDllist;
pub fn rpolygon_cut_convex<T>(
pointset: &[Point<T, T>],
is_anticlockwise: bool,
) -> Vec<Vec<Point<T, T>>>
where
T: Clone
+ Copy
+ PartialOrd
+ Ord
+ std::ops::Sub<Output = T>
+ std::ops::Mul<Output = T>
+ num_traits::Zero
+ num_traits::Num,
{
let mut lst: Vec<Point<T, T>> = pointset.to_vec();
let mut rdll = RDllist::new(lst.len(), false);
let first_ptr: *mut Dllink<usize> = rdll.get_mut(0);
let index_lists = rpolygon_cut_convex_recur(first_ptr, &mut lst, is_anticlockwise, &mut rdll);
index_lists
.into_iter()
.map(|indices| indices.into_iter().map(|i| lst[i]).collect())
.collect()
}
pub fn rpolygon_cut_explicit<T>(
pointset: &[Point<T, T>],
is_anticlockwise: bool,
) -> Vec<Vec<Point<T, T>>>
where
T: Clone
+ Copy
+ PartialOrd
+ Ord
+ std::ops::Sub<Output = T>
+ std::ops::Mul<Output = T>
+ num_traits::Zero
+ num_traits::Num,
{
let mut lst: Vec<Point<T, T>> = pointset.to_vec();
let mut rdll = RDllist::new(lst.len(), false);
let first_ptr: *mut Dllink<usize> = rdll.get_mut(0);
let index_lists = rpolygon_cut_explicit_recur(first_ptr, &mut lst, is_anticlockwise, &mut rdll);
index_lists
.into_iter()
.map(|indices| indices.into_iter().map(|i| lst[i]).collect())
.collect()
}
pub fn rpolygon_cut_implicit<T>(
pointset: &[Point<T, T>],
is_anticlockwise: bool,
) -> Vec<Vec<Point<T, T>>>
where
T: Clone
+ Copy
+ PartialOrd
+ Ord
+ std::ops::Sub<Output = T>
+ std::ops::Mul<Output = T>
+ num_traits::Zero
+ num_traits::Num,
{
let mut lst: Vec<Point<T, T>> = pointset.to_vec();
let mut rdll = RDllist::new(lst.len(), false);
let first_ptr: *mut Dllink<usize> = rdll.get_mut(0);
let index_lists = rpolygon_cut_implicit_recur(first_ptr, &mut lst, is_anticlockwise, &mut rdll);
index_lists
.into_iter()
.map(|indices| indices.into_iter().map(|i| lst[i]).collect())
.collect()
}
pub fn rpolygon_cut_rectangle<T>(
pointset: &[Point<T, T>],
is_anticlockwise: bool,
) -> Vec<Vec<Point<T, T>>>
where
T: Clone
+ Copy
+ PartialOrd
+ Ord
+ std::ops::Sub<Output = T>
+ std::ops::Mul<Output = T>
+ num_traits::Zero
+ num_traits::Num,
{
let l1 = rpolygon_cut_implicit(pointset, is_anticlockwise);
let mut res = Vec::new();
for piece in l1 {
let l2 = rpolygon_cut_explicit(&piece, is_anticlockwise);
res.extend(l2);
}
res
}
#[allow(unused_variables)]
fn rpolygon_cut_convex_recur<T>(
v1: *mut Dllink<usize>,
lst: &mut [Point<T, T>],
is_anticlockwise: bool,
rdll: &mut RDllist,
) -> Vec<Vec<usize>>
where
T: Clone
+ Copy
+ PartialOrd
+ Ord
+ std::ops::Sub<Output = T>
+ std::ops::Mul<Output = T>
+ num_traits::Zero
+ num_traits::Num,
{
let mut indices = Vec::new();
unsafe {
let mut current = v1;
loop {
indices.push((*current).data);
current = (*current).next;
if current == v1 {
break;
}
}
}
vec![indices]
}
#[allow(unused_variables)]
fn rpolygon_cut_explicit_recur<T>(
v1: *mut Dllink<usize>,
lst: &mut [Point<T, T>],
is_anticlockwise: bool,
rdll: &mut RDllist,
) -> Vec<Vec<usize>>
where
T: Clone
+ Copy
+ PartialOrd
+ Ord
+ std::ops::Sub<Output = T>
+ std::ops::Mul<Output = T>
+ num_traits::Zero
+ num_traits::Num,
{
let mut indices = Vec::new();
unsafe {
let mut current = v1;
loop {
indices.push((*current).data);
current = (*current).next;
if current == v1 {
break;
}
}
}
vec![indices]
}
#[allow(unused_variables)]
fn rpolygon_cut_implicit_recur<T>(
v1: *mut Dllink<usize>,
lst: &mut [Point<T, T>],
is_anticlockwise: bool,
rdll: &mut RDllist,
) -> Vec<Vec<usize>>
where
T: Clone
+ Copy
+ PartialOrd
+ Ord
+ std::ops::Sub<Output = T>
+ std::ops::Mul<Output = T>
+ num_traits::Zero
+ num_traits::Num,
{
let mut indices = Vec::new();
unsafe {
let mut current = v1;
loop {
indices.push((*current).data);
current = (*current).next;
if current == v1 {
break;
}
}
}
vec![indices]
}