From 9889555b02f1d1db312d9a3d04bb22969fc0dc34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vuka=C5=A1in=20Stepanovi=C4=87?= Date: Thu, 16 Feb 2023 15:19:35 +0000 Subject: [PATCH] refactor ResultPoint trait bounds into Into bounds --- src/qrcode/detector/alignment_pattern.rs | 21 +++++----- src/qrcode/detector/qrcode_detector.rs | 50 ++++++++++++------------ 2 files changed, 38 insertions(+), 33 deletions(-) diff --git a/src/qrcode/detector/alignment_pattern.rs b/src/qrcode/detector/alignment_pattern.rs index ba913c2..6f9727c 100644 --- a/src/qrcode/detector/alignment_pattern.rs +++ b/src/qrcode/detector/alignment_pattern.rs @@ -16,7 +16,7 @@ //Point -use crate::{Point, ResultPoint}; +use crate::{point, Point, ResultPoint}; /** *

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)] pub struct AlignmentPattern { estimatedModuleSize: f32, - point: (f32, f32), + point: Point, +} + +impl From<&AlignmentPattern> for Point { + fn from(value: &AlignmentPattern) -> Self { + value.point + } } impl ResultPoint for AlignmentPattern { fn getX(&self) -> f32 { - self.point.0 + self.point.x } fn getY(&self) -> f32 { - self.point.1 + self.point.y } fn to_rxing_result_point(&self) -> Point { - Point { - x: self.point.0, - y: self.point.1, - } + self.point } } @@ -51,7 +54,7 @@ impl AlignmentPattern { pub fn new(posX: f32, posY: f32, estimatedModuleSize: f32) -> Self { Self { estimatedModuleSize, - point: (posX, posY), + point: point(posX, posY), } } diff --git a/src/qrcode/detector/qrcode_detector.rs b/src/qrcode/detector/qrcode_detector.rs index 6d18a9d..0c21cde 100644 --- a/src/qrcode/detector/qrcode_detector.rs +++ b/src/qrcode/detector/qrcode_detector.rs @@ -167,13 +167,18 @@ impl<'a> Detector<'_> { Ok(QRCodeDetectorResult::new(bits, points)) } - fn createTransform( - topLeft: &T, - topRight: &T, - bottomLeft: &T, - alignmentPattern: Option<&X>, + fn createTransform, X: Into>( + topLeft: T, + topRight: T, + bottomLeft: T, + alignmentPattern: Option, dimension: u32, ) -> Option { + let topLeft: Point = topLeft.into(); + let topRight: Point = topRight.into(); + let bottomLeft: Point = bottomLeft.into(); + let alignmentPattern: Option = alignmentPattern.map(Into::into); + let dimMinusThree = dimension as f32 - 3.5; let bottomRightX: f32; let bottomRightY: f32; @@ -226,22 +231,16 @@ impl<'a> Detector<'_> { *

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.

*/ - fn computeDimension( - topLeft: &T, - topRight: &T, - bottomLeft: &T, + fn computeDimension>( + topLeft: T, + topRight: T, + bottomLeft: T, moduleSize: f32, ) -> Result { - let tltrCentersDimension = (Point::distance( - topLeft.to_rxing_result_point(), - topRight.to_rxing_result_point(), - ) / moduleSize) - .round() as i32; - let tlblCentersDimension = (Point::distance( - topLeft.to_rxing_result_point(), - bottomLeft.to_rxing_result_point(), - ) / moduleSize) - .round() as i32; + let tltrCentersDimension = + (Point::distance(topLeft.into(), topRight.into()) / moduleSize).round() as i32; + let tlblCentersDimension = + (Point::distance(topLeft.into(), bottomLeft.into()) / moduleSize).round() as i32; let mut dimension = ((tltrCentersDimension + tlblCentersDimension) / 2) + 7; match dimension & 0x03 { 0 => dimension += 1, @@ -261,11 +260,11 @@ impl<'a> Detector<'_> { * @param bottomLeft detected bottom-left finder pattern center * @return estimated module size */ - pub fn calculateModuleSize( + pub fn calculateModuleSize>( &self, - topLeft: &T, - topRight: &T, - bottomLeft: &T, + topLeft: T, + topRight: T, + bottomLeft: T, ) -> f32 { // Take the average (self.calculateModuleSizeOneWay(topLeft, topRight) @@ -278,7 +277,10 @@ impl<'a> Detector<'_> { * {@link #sizeOfBlackWhiteBlackRunBothWays(int, int, int, int)} to figure the * width of each, measuring along the axis between their centers.

*/ - fn calculateModuleSizeOneWay(&self, pattern: &T, otherPattern: &T) -> f32 { + fn calculateModuleSizeOneWay>(&self, pattern: T, otherPattern: T) -> f32 { + let pattern: Point = pattern.into(); + let otherPattern: Point = otherPattern.into(); + let moduleSizeEst1 = self.sizeOfBlackWhiteBlackRunBothWays( pattern.getX().floor() as u32, pattern.getY().floor() as u32,