diff --git a/src/multi/qrcode/detector/multi_finder_pattern_finder.rs b/src/multi/qrcode/detector/multi_finder_pattern_finder.rs index d2505ef..9e5da69 100644 --- a/src/multi/qrcode/detector/multi_finder_pattern_finder.rs +++ b/src/multi/qrcode/detector/multi_finder_pattern_finder.rs @@ -20,7 +20,6 @@ use crate::{ common::{BitMatrix, Result}, qrcode::detector::{FinderPattern, FinderPatternFinder, FinderPatternInfo}, result_point_utils, DecodeHintType, DecodingHintDictionary, Exceptions, Point, PointCallback, - ResultPoint, }; // max. legal count of modules per QR code edge (177) @@ -175,18 +174,10 @@ impl<'a> MultiFinderPatternFinder<'_> { // Calculate the distances: a = topleft-bottomleft, b=topleft-topright, c = diagonal let info = FinderPatternInfo::new(test); - let dA = Point::distance( - info.getTopLeft().to_rxing_result_point(), - info.getBottomLeft().to_rxing_result_point(), - ); - let dC = Point::distance( - info.getTopRight().to_rxing_result_point(), - info.getBottomLeft().to_rxing_result_point(), - ); - let dB = Point::distance( - info.getTopLeft().to_rxing_result_point(), - info.getTopRight().to_rxing_result_point(), - ); + let dA = Point::distance(info.getTopLeft().into(), info.getBottomLeft().into()); + let dC = + Point::distance(info.getTopRight().into(), info.getBottomLeft().into()); + let dB = Point::distance(info.getTopLeft().into(), info.getTopRight().into()); // Check the sizes let estimatedModuleCount = (dA + dB) / (p1.getEstimatedModuleSize() * 2.0); diff --git a/src/qrcode/detector/alignment_pattern.rs b/src/qrcode/detector/alignment_pattern.rs index 6f9727c..e429600 100644 --- a/src/qrcode/detector/alignment_pattern.rs +++ b/src/qrcode/detector/alignment_pattern.rs @@ -16,7 +16,7 @@ //Point -use crate::{point, Point, ResultPoint}; +use crate::{point, Point}; /** *
Encapsulates an alignment pattern, which are the smaller square patterns found in
@@ -36,17 +36,9 @@ impl From<&AlignmentPattern> for Point {
}
}
-impl ResultPoint for AlignmentPattern {
- fn getX(&self) -> f32 {
- self.point.x
- }
-
- fn getY(&self) -> f32 {
- self.point.y
- }
-
- fn to_rxing_result_point(&self) -> Point {
- self.point
+impl From
Encapsulates a finder pattern, which are the three square patterns found in @@ -27,21 +27,7 @@ use crate::{point, Point, ResultPoint}; pub struct FinderPattern { estimatedModuleSize: f32, count: usize, - point: Point, -} - -impl ResultPoint for FinderPattern { - fn getX(&self) -> f32 { - self.point.x - } - - fn getY(&self) -> f32 { - self.point.y - } - - fn to_rxing_result_point(&self) -> Point { - self.point - } + pub(crate) point: Point, } impl From<&FinderPattern> for Point { @@ -82,7 +68,7 @@ impl FinderPattern { * position and size -- meaning, it is at nearly the same center with nearly the same size.
*/ pub fn aboutEquals(&self, moduleSize: f32, i: f32, j: f32) -> bool { - if (i - self.getY()).abs() <= moduleSize && (j - self.getX()).abs() <= moduleSize { + if (i - self.point.y).abs() <= moduleSize && (j - self.point.x).abs() <= moduleSize { let moduleSizeDiff = (moduleSize - self.estimatedModuleSize).abs(); moduleSizeDiff <= 1.0 || moduleSizeDiff <= self.estimatedModuleSize } else { @@ -97,8 +83,8 @@ impl FinderPattern { */ pub fn combineEstimate(&self, i: f32, j: f32, newModuleSize: f32) -> FinderPattern { let combinedCount = self.count as f32 + 1.0; - let combinedX = (self.count as f32 * self.getX() + j) / combinedCount; - let combinedY = (self.count as f32 * self.getY() + i) / combinedCount; + let combinedX = (self.count as f32 * self.point.x + j) / combinedCount; + let combinedY = (self.count as f32 * self.point.y + i) / combinedCount; let combinedModuleSize = (self.count as f32 * self.estimatedModuleSize + newModuleSize) / combinedCount; FinderPattern::private_new( diff --git a/src/qrcode/detector/finder_pattern_finder.rs b/src/qrcode/detector/finder_pattern_finder.rs index 0b40745..dc12d5e 100755 --- a/src/qrcode/detector/finder_pattern_finder.rs +++ b/src/qrcode/detector/finder_pattern_finder.rs @@ -14,10 +14,12 @@ * limitations under the License. */ +use std::ops::Div; + use crate::{ common::{BitMatrix, Result}, - result_point_utils, DecodeHintType, DecodeHintValue, DecodingHintDictionary, Exceptions, - PointCallback, ResultPoint, + result_point_utils, DecodeHintType, DecodeHintValue, DecodingHintDictionary, Exceptions, Point, + PointCallback, }; use super::{FinderPattern, FinderPatternInfo}; @@ -633,9 +635,11 @@ impl<'a> FinderPatternFinder<'_> { // difference in the x / y coordinates of the two centers. // This is the case where you find top left last. self.hasSkipped = true; - return (((fnp.getX() - center.getX()).abs() - - (fnp.getY() - center.getY()).abs()) - / 2.0) + + return (Point::from(fnp) - Point::from(center)) + .abs() + .fold(|x, y| x - y) + .div(2.0) .floor() as u32; } else { firstConfirmedCenter.replace(center); @@ -679,10 +683,7 @@ impl<'a> FinderPatternFinder<'_> { * Get square of distance between a and b. */ fn squaredDistance(a: &FinderPattern, b: &FinderPattern) -> f64 { - let x = a.getX() as f64 - b.getX() as f64; - let y = a.getY() as f64 - b.getY() as f64; - - x * x + y * y + Point::from(a).squaredDistance(Point::from(b)) as f64 } /** diff --git a/src/qrcode/detector/qrcode_detector.rs b/src/qrcode/detector/qrcode_detector.rs index 029e28c..e5cfeb0 100644 --- a/src/qrcode/detector/qrcode_detector.rs +++ b/src/qrcode/detector/qrcode_detector.rs @@ -21,7 +21,6 @@ use crate::{ point, qrcode::decoder::Version, DecodeHintType, DecodeHintValue, DecodingHintDictionary, Exceptions, Point, PointCallback, - ResultPoint, }; use super::{ @@ -114,16 +113,16 @@ impl<'a> Detector<'_> { // Anything above version 1 has an alignment pattern if !provisionalVersion.getAlignmentPatternCenters().is_empty() { // Guess where a "bottom right" finder pattern would have been - let bottomRightX = topRight.getX() - topLeft.getX() + bottomLeft.getX(); - let bottomRightY = topRight.getY() - topLeft.getY() + bottomLeft.getY(); + let bottomRightX = topRight.point.x - topLeft.point.x + bottomLeft.point.x; + let bottomRightY = topRight.point.y - topLeft.point.y + bottomLeft.point.y; // Estimate that alignment pattern is closer by 3 modules // from "bottom right" to known top left location let correctionToTopLeft = 1.0 - (3.0 / modulesBetweenFPCenters as f32); let estAlignmentX = - (topLeft.getX() + correctionToTopLeft * (bottomRightX - topLeft.getX())) as u32; + (topLeft.point.x + correctionToTopLeft * (bottomRightX - topLeft.point.x)) as u32; let estAlignmentY = - (topLeft.getY() + correctionToTopLeft * (bottomRightY - topLeft.getY())) as u32; + (topLeft.point.y + correctionToTopLeft * (bottomRightY - topLeft.point.y)) as u32; // Kind of arbitrary -- expand search radius before giving up let mut i = 4; @@ -151,16 +150,16 @@ impl<'a> Detector<'_> { let bits = Detector::sampleGrid(self.image, &transform, dimension)?; let mut points = vec![ - bottomLeft.to_rxing_result_point(), - topLeft.to_rxing_result_point(), - topRight.to_rxing_result_point(), + Point::from(bottomLeft), + Point::from(topLeft), + Point::from(topRight), ]; if alignmentPattern.is_some() { points.push( alignmentPattern .ok_or(Exceptions::NotFoundException(None))? - .to_rxing_result_point(), + .into(), ) } diff --git a/src/rxing_result_point.rs b/src/rxing_result_point.rs index 81d1aa1..534ea96 100644 --- a/src/rxing_result_point.rs +++ b/src/rxing_result_point.rs @@ -182,10 +182,23 @@ impl Point { f32::max(self.x.abs(), self.y.abs()) } + pub fn squaredDistance(self, p: Self) -> f32 { + let diff = self - p; + diff.x * diff.x + diff.y * diff.y + } + pub fn distance(self, p: Self) -> f32 { (self - p).length() } + pub fn abs(self) -> Self { + Self::new(self.x.abs(), self.y.abs()) + } + + pub fn fold U>(self, f: F) -> U { + f(self.x, self.y) + } + /// Calculate a floating point pixel coordinate representing the 'center' of the pixel. /// This is sort of the inverse operation of the PointI(PointF) conversion constructor. /// See also the documentation of the GridSampler API.