refactor ResultPoint trait bounds into Into<Point> bounds

This commit is contained in:
Vukašin Stepanović
2023-02-16 15:19:35 +00:00
parent 00f007f14c
commit 9889555b02
2 changed files with 38 additions and 33 deletions

View File

@@ -16,7 +16,7 @@
//Point //Point
use crate::{Point, ResultPoint}; use crate::{point, Point, ResultPoint};
/** /**
* <p>Encapsulates an alignment pattern, which are the smaller square patterns found in * <p>Encapsulates an alignment pattern, which are the smaller square patterns found in
@@ -27,23 +27,26 @@ use crate::{Point, ResultPoint};
#[derive(Debug, Clone, Copy, PartialEq)] #[derive(Debug, Clone, Copy, PartialEq)]
pub struct AlignmentPattern { pub struct AlignmentPattern {
estimatedModuleSize: f32, estimatedModuleSize: f32,
point: (f32, f32), point: Point,
}
impl From<&AlignmentPattern> for Point {
fn from(value: &AlignmentPattern) -> Self {
value.point
}
} }
impl ResultPoint for AlignmentPattern { impl ResultPoint for AlignmentPattern {
fn getX(&self) -> f32 { fn getX(&self) -> f32 {
self.point.0 self.point.x
} }
fn getY(&self) -> f32 { fn getY(&self) -> f32 {
self.point.1 self.point.y
} }
fn to_rxing_result_point(&self) -> Point { fn to_rxing_result_point(&self) -> Point {
Point { self.point
x: self.point.0,
y: self.point.1,
}
} }
} }
@@ -51,7 +54,7 @@ impl AlignmentPattern {
pub fn new(posX: f32, posY: f32, estimatedModuleSize: f32) -> Self { pub fn new(posX: f32, posY: f32, estimatedModuleSize: f32) -> Self {
Self { Self {
estimatedModuleSize, estimatedModuleSize,
point: (posX, posY), point: point(posX, posY),
} }
} }

View File

@@ -167,13 +167,18 @@ impl<'a> Detector<'_> {
Ok(QRCodeDetectorResult::new(bits, points)) Ok(QRCodeDetectorResult::new(bits, points))
} }
fn createTransform<T: ResultPoint, X: ResultPoint>( fn createTransform<T: Into<Point>, X: Into<Point>>(
topLeft: &T, topLeft: T,
topRight: &T, topRight: T,
bottomLeft: &T, bottomLeft: T,
alignmentPattern: Option<&X>, alignmentPattern: Option<X>,
dimension: u32, dimension: u32,
) -> Option<PerspectiveTransform> { ) -> Option<PerspectiveTransform> {
let topLeft: Point = topLeft.into();
let topRight: Point = topRight.into();
let bottomLeft: Point = bottomLeft.into();
let alignmentPattern: Option<Point> = alignmentPattern.map(Into::into);
let dimMinusThree = dimension as f32 - 3.5; let dimMinusThree = dimension as f32 - 3.5;
let bottomRightX: f32; let bottomRightX: f32;
let bottomRightY: f32; let bottomRightY: f32;
@@ -226,22 +231,16 @@ impl<'a> Detector<'_> {
* <p>Computes the dimension (number of modules on a size) of the QR Code based on the position * <p>Computes the dimension (number of modules on a size) of the QR Code based on the position
* of the finder patterns and estimated module size.</p> * of the finder patterns and estimated module size.</p>
*/ */
fn computeDimension<T: ResultPoint>( fn computeDimension<T: Into<Point>>(
topLeft: &T, topLeft: T,
topRight: &T, topRight: T,
bottomLeft: &T, bottomLeft: T,
moduleSize: f32, moduleSize: f32,
) -> Result<u32> { ) -> Result<u32> {
let tltrCentersDimension = (Point::distance( let tltrCentersDimension =
topLeft.to_rxing_result_point(), (Point::distance(topLeft.into(), topRight.into()) / moduleSize).round() as i32;
topRight.to_rxing_result_point(), let tlblCentersDimension =
) / moduleSize) (Point::distance(topLeft.into(), bottomLeft.into()) / moduleSize).round() as i32;
.round() as i32;
let tlblCentersDimension = (Point::distance(
topLeft.to_rxing_result_point(),
bottomLeft.to_rxing_result_point(),
) / moduleSize)
.round() as i32;
let mut dimension = ((tltrCentersDimension + tlblCentersDimension) / 2) + 7; let mut dimension = ((tltrCentersDimension + tlblCentersDimension) / 2) + 7;
match dimension & 0x03 { match dimension & 0x03 {
0 => dimension += 1, 0 => dimension += 1,
@@ -261,11 +260,11 @@ impl<'a> Detector<'_> {
* @param bottomLeft detected bottom-left finder pattern center * @param bottomLeft detected bottom-left finder pattern center
* @return estimated module size * @return estimated module size
*/ */
pub fn calculateModuleSize<T: ResultPoint>( pub fn calculateModuleSize<T: Into<Point>>(
&self, &self,
topLeft: &T, topLeft: T,
topRight: &T, topRight: T,
bottomLeft: &T, bottomLeft: T,
) -> f32 { ) -> f32 {
// Take the average // Take the average
(self.calculateModuleSizeOneWay(topLeft, topRight) (self.calculateModuleSizeOneWay(topLeft, topRight)
@@ -278,7 +277,10 @@ impl<'a> Detector<'_> {
* {@link #sizeOfBlackWhiteBlackRunBothWays(int, int, int, int)} to figure the * {@link #sizeOfBlackWhiteBlackRunBothWays(int, int, int, int)} to figure the
* width of each, measuring along the axis between their centers.</p> * width of each, measuring along the axis between their centers.</p>
*/ */
fn calculateModuleSizeOneWay<T: ResultPoint>(&self, pattern: &T, otherPattern: &T) -> f32 { fn calculateModuleSizeOneWay<T: Into<Point>>(&self, pattern: T, otherPattern: T) -> f32 {
let pattern: Point = pattern.into();
let otherPattern: Point = otherPattern.into();
let moduleSizeEst1 = self.sizeOfBlackWhiteBlackRunBothWays( let moduleSizeEst1 = self.sizeOfBlackWhiteBlackRunBothWays(
pattern.getX().floor() as u32, pattern.getX().floor() as u32,
pattern.getY().floor() as u32, pattern.getY().floor() as u32,