refactor orderBestPatterns to not use ResultPoint

This commit is contained in:
Vukašin Stepanović
2023-02-16 15:10:24 +00:00
parent 41150a8f8b
commit 00f007f14c
2 changed files with 31 additions and 43 deletions

View File

@@ -44,6 +44,18 @@ impl ResultPoint for FinderPattern {
}
}
impl From<&FinderPattern> for Point {
fn from(value: &FinderPattern) -> Self {
value.point
}
}
impl From<FinderPattern> for Point {
fn from(value: FinderPattern) -> Self {
value.point
}
}
impl FinderPattern {
pub fn new(posX: f32, posY: f32, estimatedModuleSize: f32) -> Self {
Self::private_new(posX, posY, estimatedModuleSize, 1)

View File

@@ -1,4 +1,4 @@
use crate::{Point, ResultPoint};
use crate::Point;
/**
* Orders an array of three Points in an order [A,B,C] such that AB is less than AC
@@ -6,62 +6,38 @@ use crate::{Point, ResultPoint};
*
* @param patterns array of three {@code Point} to order
*/
pub fn orderBestPatterns<T: ResultPoint + Copy + Clone>(patterns: &mut [T; 3]) {
pub fn orderBestPatterns<T: Into<Point>>(patterns: &mut [T; 3]) {
// Find distances between pattern centers
let zeroOneDistance = Point::distance(
patterns[0].to_rxing_result_point(),
patterns[1].to_rxing_result_point(),
);
let oneTwoDistance = Point::distance(
patterns[1].to_rxing_result_point(),
patterns[2].to_rxing_result_point(),
);
let zeroTwoDistance = Point::distance(
patterns[0].to_rxing_result_point(),
patterns[2].to_rxing_result_point(),
);
let mut pointA;
let pointB;
let mut pointC;
let zeroOneDistance = Point::distance(patterns[0].into(), patterns[1].into());
let oneTwoDistance = Point::distance(patterns[1].into(), patterns[2].into());
let zeroTwoDistance = Point::distance(patterns[0].into(), patterns[2].into());
// Assume one closest to other two is B; A and C will just be guesses at first
if oneTwoDistance >= zeroOneDistance && oneTwoDistance >= zeroTwoDistance {
pointB = patterns[0];
pointA = patterns[1];
pointC = patterns[2];
} else if zeroTwoDistance >= oneTwoDistance && zeroTwoDistance >= zeroOneDistance {
pointB = patterns[1];
pointA = patterns[0];
pointC = patterns[2];
} else {
pointB = patterns[2];
pointA = patterns[0];
pointC = patterns[1];
}
let (mut pointA, pointB, mut pointC) =
if oneTwoDistance >= zeroOneDistance && oneTwoDistance >= zeroTwoDistance {
(patterns[1], patterns[0], patterns[2])
} else if zeroTwoDistance >= oneTwoDistance && zeroTwoDistance >= zeroOneDistance {
(patterns[0], patterns[1], patterns[2])
} else {
(patterns[0], patterns[2], patterns[1])
};
// Use cross product to figure out whether A and C are correct or flipped.
// This asks whether BC x BA has a positive z component, which is the arrangement
// we want for A, B, C. If it's negative, then we've got it flipped around and
// should swap A and C.
if crossProductZ(pointA, pointB, pointC) < 0.0 {
if crossProductZ(pointA.into(), pointB.into(), pointC.into()) < 0.0 {
std::mem::swap(&mut pointA, &mut pointC);
}
let pa = pointA;
let pb = pointB;
let pc = pointC;
patterns[0] = pa;
patterns[1] = pb;
patterns[2] = pc;
patterns[0] = pointA;
patterns[1] = pointB;
patterns[2] = pointC;
}
/**
* Returns the z component of the cross product between vectors BC and BA.
*/
pub fn crossProductZ<T: ResultPoint>(pointA: T, pointB: T, pointC: T) -> f32 {
let bX = pointB.getX();
let bY = pointB.getY();
((pointC.getX() - bX) * (pointA.getY() - bY)) - ((pointC.getY() - bY) * (pointA.getX() - bX))
fn crossProductZ(a: Point, b: Point, c: Point) -> f32 {
((c.x - b.x) * (a.y - b.y)) - ((c.y - b.y) * (a.x - b.x))
}