sort_by_perimeters_owned

Function sort_by_perimeters_owned 

Source
pub fn sort_by_perimeters_owned<T>(
    contours: Vec<Contour<T>>,
) -> Vec<(Contour<T>, f64)>
where T: Num + NumCast + Copy + PartialEq + Eq + AsPrimitive<f64>,
Expand description

Calculates the perimeter of each contour and sorts them in descending order.

This function takes a vector of Contour<T> objects, computes the perimeter for each one, and returns a new vector of tuples, where each tuple contains the original contour and its calculated perimeter as an f64. The returned vector is sorted based on the perimeter in descending order.

For performance, this function takes ownership of the input vector and uses an unstable sort. The perimeter is calculated as the sum of Euclidean distances between consecutive points, closing the loop by including the distance between the last and first point.

§Type Parameters

  • T: The numeric type of the point coordinates within the contour. It must be a type that can be losslessly converted to f64 for distance calculations, such as i32 or u32.

§Arguments

  • contours: A Vec<Contour<T>> which will be consumed by the function.

§Returns

A Vec<(Contour<T>, f64)> sorted by the perimeter in descending order. Contours with 0 or 1 point will have a perimeter of 0.0.

§Examples

use imageproc::contours::{Contour, BorderType};
use imageproc::point::Point;
use image_debug_utils::contours::sort_by_perimeters_owned;

let c1 = Contour {
    parent: None,
    border_type: BorderType::Outer,
    points: vec![Point::new(0,0), Point::new(10,0), Point::new(10,10), Point::new(0,10)]
}; // Perimeter 40.0

let c2 = Contour {
    parent: None,
    border_type: BorderType::Outer,
    points: vec![Point::new(0,0), Point::new(10,0)]
}; // Perimeter 20.0 (10 + 10 back to start)

let contours = vec![c2.clone(), c1.clone()];

let sorted = sort_by_perimeters_owned(contours);
assert_eq!(sorted[0].1, 40.0);
assert_eq!(sorted[1].1, 20.0);