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 { impl FinderPattern {
pub fn new(posX: f32, posY: f32, estimatedModuleSize: f32) -> Self { pub fn new(posX: f32, posY: f32, estimatedModuleSize: f32) -> Self {
Self::private_new(posX, posY, estimatedModuleSize, 1) 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 * 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 * @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 // Find distances between pattern centers
let zeroOneDistance = Point::distance( let zeroOneDistance = Point::distance(patterns[0].into(), patterns[1].into());
patterns[0].to_rxing_result_point(), let oneTwoDistance = Point::distance(patterns[1].into(), patterns[2].into());
patterns[1].to_rxing_result_point(), let zeroTwoDistance = Point::distance(patterns[0].into(), patterns[2].into());
);
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;
// Assume one closest to other two is B; A and C will just be guesses at first // Assume one closest to other two is B; A and C will just be guesses at first
let (mut pointA, pointB, mut pointC) =
if oneTwoDistance >= zeroOneDistance && oneTwoDistance >= zeroTwoDistance { if oneTwoDistance >= zeroOneDistance && oneTwoDistance >= zeroTwoDistance {
pointB = patterns[0]; (patterns[1], patterns[0], patterns[2])
pointA = patterns[1];
pointC = patterns[2];
} else if zeroTwoDistance >= oneTwoDistance && zeroTwoDistance >= zeroOneDistance { } else if zeroTwoDistance >= oneTwoDistance && zeroTwoDistance >= zeroOneDistance {
pointB = patterns[1]; (patterns[0], patterns[1], patterns[2])
pointA = patterns[0];
pointC = patterns[2];
} else { } else {
pointB = patterns[2]; (patterns[0], patterns[2], patterns[1])
pointA = patterns[0]; };
pointC = patterns[1];
}
// Use cross product to figure out whether A and C are correct or flipped. // 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 // 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 // we want for A, B, C. If it's negative, then we've got it flipped around and
// should swap A and C. // 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); std::mem::swap(&mut pointA, &mut pointC);
} }
let pa = pointA; patterns[0] = pointA;
let pb = pointB; patterns[1] = pointB;
let pc = pointC; patterns[2] = pointC;
patterns[0] = pa;
patterns[1] = pb;
patterns[2] = pc;
} }
/** /**
* Returns the z component of the cross product between vectors BC and BA. * 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 { fn crossProductZ(a: Point, b: Point, c: Point) -> f32 {
let bX = pointB.getX(); ((c.x - b.x) * (a.y - b.y)) - ((c.y - b.y) * (a.x - b.x))
let bY = pointB.getY();
((pointC.getX() - bX) * (pointA.getY() - bY)) - ((pointC.getY() - bY) * (pointA.getX() - bX))
} }