Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add _get_shoelace_area rust #1558

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions rust/src/annotation.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (C) 2024 Intel Corporation
//
// SPDX-License-Identifier: MIT

fn get_shoelace_area(points: Vec<(f64, f64)>) -> f64 {
let n = points.len();
// Not a polygon
if n < 3 {
return 0.0;
}
let mut area = 0.0;
for i in 0..n {
let (x1, y1) = points[i];
let (x2, y2) = points[(i + 1) % n]; // Next vertex, wrapping around using modulo
area += x1 * y2 - y1 * x2;
}
area.abs() / 2.0
}
2 changes: 2 additions & 0 deletions rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use pyo3::prelude::*;
use crate::coco_page_mapper::CocoPageMapper;
use crate::datum_page_mapper::DatumPageMapper;
use crate::json_section_page_mapper::JsonSectionPageMapper;
use crate::annotations::

/// Datumaro Rust API
#[pymodule]
Expand All @@ -21,6 +22,7 @@ fn rust_api(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
m.add_class::<CocoPageMapper>()?;
m.add_class::<DatumPageMapper>()?;
m.add_class::<JsonSectionPageMapper>()?;
m.add_function(wrap_pyfunction!(get_shoelace_area, m)?)?;

Ok(())
}
17 changes: 17 additions & 0 deletions rust/tests/annotations.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright (C) 2024 Intel Corporation
//
// SPDX-License-Identifier: MIT

use datumaro_rust_api::annotations::get_shoelace_area;

#[test]
fn test_get_shoelace_area() {
// Define a polygon example
let points = vec![(0.0, 0.0), (4.0, 0.0), (4.0, 3.0), (0.0, 3.0)];

// Calculate the shoelace area
let area = get_shoelace_area(&points);

// Assert that the calculated area matches the expected area (12.0 in this case)
assert_eq!(area, 12.0);
}
30 changes: 16 additions & 14 deletions src/datumaro/components/annotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

from datumaro.components.media import Image
from datumaro.util.attrs_util import default_if_none, not_empty

from datumaro.rust_api import get_shoelace_area

class AnnotationType(IntEnum):
unknown = 0
Expand Down Expand Up @@ -840,21 +840,23 @@ def __eq__(self, other):
inter_area = self_polygon.intersection(other_polygon).area
return abs(self_polygon.area - inter_area) < CHECK_POLYGON_EQ_EPSILONE

def _get_shoelace_area(self):
points = self.get_points()
n = len(points)
# Not a polygon
if n < 3:
return 0

area = 0.0
for i in range(n):
x1, y1 = points[i]
x2, y2 = points[(i + 1) % n] # Next vertex, wrapping around using modulo
area += x1 * y2 - y1 * x2
# def _get_shoelace_area(self):
# points = self.get_points()
# n = len(points)
# # Not a polygon
# if n < 3:
# return 0

return abs(area) / 2.0
# area = 0.0
# for i in range(n):
# x1, y1 = points[i]
# x2, y2 = points[(i + 1) % n] # Next vertex, wrapping around using modulo
# area += x1 * y2 - y1 * x2

# return abs(area) / 2.0
def _get_shoelace_area(self):
points = self.get_points()
return get_shoelace_area(points)

@attrs(slots=True, init=False, order=False)
class Bbox(_Shape):
Expand Down
Loading