remove impl ResultPoint for FinderPattern & AlignmentPattern

This commit is contained in:
Vukašin Stepanović
2023-02-16 16:32:01 +00:00
parent 072d3d0ec7
commit 1322f7defa
6 changed files with 47 additions and 65 deletions

View File

@@ -20,7 +20,6 @@ use crate::{
common::{BitMatrix, Result}, common::{BitMatrix, Result},
qrcode::detector::{FinderPattern, FinderPatternFinder, FinderPatternInfo}, qrcode::detector::{FinderPattern, FinderPatternFinder, FinderPatternInfo},
result_point_utils, DecodeHintType, DecodingHintDictionary, Exceptions, Point, PointCallback, result_point_utils, DecodeHintType, DecodingHintDictionary, Exceptions, Point, PointCallback,
ResultPoint,
}; };
// max. legal count of modules per QR code edge (177) // 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 // Calculate the distances: a = topleft-bottomleft, b=topleft-topright, c = diagonal
let info = FinderPatternInfo::new(test); let info = FinderPatternInfo::new(test);
let dA = Point::distance( let dA = Point::distance(info.getTopLeft().into(), info.getBottomLeft().into());
info.getTopLeft().to_rxing_result_point(), let dC =
info.getBottomLeft().to_rxing_result_point(), Point::distance(info.getTopRight().into(), info.getBottomLeft().into());
); let dB = Point::distance(info.getTopLeft().into(), info.getTopRight().into());
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(),
);
// Check the sizes // Check the sizes
let estimatedModuleCount = (dA + dB) / (p1.getEstimatedModuleSize() * 2.0); let estimatedModuleCount = (dA + dB) / (p1.getEstimatedModuleSize() * 2.0);

View File

@@ -16,7 +16,7 @@
//Point //Point
use crate::{point, Point, ResultPoint}; use crate::{point, Point};
/** /**
* <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
@@ -36,17 +36,9 @@ impl From<&AlignmentPattern> for Point {
} }
} }
impl ResultPoint for AlignmentPattern { impl From<AlignmentPattern> for Point {
fn getX(&self) -> f32 { fn from(value: AlignmentPattern) -> Self {
self.point.x value.point
}
fn getY(&self) -> f32 {
self.point.y
}
fn to_rxing_result_point(&self) -> Point {
self.point
} }
} }
@@ -63,7 +55,7 @@ impl AlignmentPattern {
* position and size -- meaning, it is at nearly the same center with nearly the same size.</p> * position and size -- meaning, it is at nearly the same center with nearly the same size.</p>
*/ */
pub fn aboutEquals(&self, moduleSize: f32, i: f32, j: f32) -> bool { 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(); let moduleSizeDiff = (moduleSize - self.estimatedModuleSize).abs();
return moduleSizeDiff <= 1.0 || moduleSizeDiff <= self.estimatedModuleSize; return moduleSizeDiff <= 1.0 || moduleSizeDiff <= self.estimatedModuleSize;
} }
@@ -75,8 +67,8 @@ impl AlignmentPattern {
* with a new estimate. It returns a new {@code FinderPattern} containing an average of the two. * with a new estimate. It returns a new {@code FinderPattern} containing an average of the two.
*/ */
pub fn combineEstimate(&self, i: f32, j: f32, newModuleSize: f32) -> AlignmentPattern { pub fn combineEstimate(&self, i: f32, j: f32, newModuleSize: f32) -> AlignmentPattern {
let combinedX = (self.getX() + j) / 2.0; let combinedX = (self.point.x + j) / 2.0;
let combinedY = (self.getY() + i) / 2.0; let combinedY = (self.point.y + i) / 2.0;
let combinedModuleSize = (self.estimatedModuleSize + newModuleSize) / 2.0; let combinedModuleSize = (self.estimatedModuleSize + newModuleSize) / 2.0;
AlignmentPattern::new(combinedX, combinedY, combinedModuleSize) AlignmentPattern::new(combinedX, combinedY, combinedModuleSize)
} }

View File

@@ -14,7 +14,7 @@
* limitations under the License. * limitations under the License.
*/ */
use crate::{point, Point, ResultPoint}; use crate::{point, Point};
/** /**
* <p>Encapsulates a finder pattern, which are the three square patterns found in * <p>Encapsulates a finder pattern, which are the three square patterns found in
@@ -27,21 +27,7 @@ use crate::{point, Point, ResultPoint};
pub struct FinderPattern { pub struct FinderPattern {
estimatedModuleSize: f32, estimatedModuleSize: f32,
count: usize, count: usize,
point: Point, pub(crate) 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
}
} }
impl From<&FinderPattern> for 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.</p> * position and size -- meaning, it is at nearly the same center with nearly the same size.</p>
*/ */
pub fn aboutEquals(&self, moduleSize: f32, i: f32, j: f32) -> bool { 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(); let moduleSizeDiff = (moduleSize - self.estimatedModuleSize).abs();
moduleSizeDiff <= 1.0 || moduleSizeDiff <= self.estimatedModuleSize moduleSizeDiff <= 1.0 || moduleSizeDiff <= self.estimatedModuleSize
} else { } else {
@@ -97,8 +83,8 @@ impl FinderPattern {
*/ */
pub fn combineEstimate(&self, i: f32, j: f32, newModuleSize: f32) -> FinderPattern { pub fn combineEstimate(&self, i: f32, j: f32, newModuleSize: f32) -> FinderPattern {
let combinedCount = self.count as f32 + 1.0; let combinedCount = self.count as f32 + 1.0;
let combinedX = (self.count as f32 * self.getX() + j) / combinedCount; let combinedX = (self.count as f32 * self.point.x + j) / combinedCount;
let combinedY = (self.count as f32 * self.getY() + i) / combinedCount; let combinedY = (self.count as f32 * self.point.y + i) / combinedCount;
let combinedModuleSize = let combinedModuleSize =
(self.count as f32 * self.estimatedModuleSize + newModuleSize) / combinedCount; (self.count as f32 * self.estimatedModuleSize + newModuleSize) / combinedCount;
FinderPattern::private_new( FinderPattern::private_new(

View File

@@ -14,10 +14,12 @@
* limitations under the License. * limitations under the License.
*/ */
use std::ops::Div;
use crate::{ use crate::{
common::{BitMatrix, Result}, common::{BitMatrix, Result},
result_point_utils, DecodeHintType, DecodeHintValue, DecodingHintDictionary, Exceptions, result_point_utils, DecodeHintType, DecodeHintValue, DecodingHintDictionary, Exceptions, Point,
PointCallback, ResultPoint, PointCallback,
}; };
use super::{FinderPattern, FinderPatternInfo}; use super::{FinderPattern, FinderPatternInfo};
@@ -633,9 +635,11 @@ impl<'a> FinderPatternFinder<'_> {
// difference in the x / y coordinates of the two centers. // difference in the x / y coordinates of the two centers.
// This is the case where you find top left last. // This is the case where you find top left last.
self.hasSkipped = true; self.hasSkipped = true;
return (((fnp.getX() - center.getX()).abs()
- (fnp.getY() - center.getY()).abs()) return (Point::from(fnp) - Point::from(center))
/ 2.0) .abs()
.fold(|x, y| x - y)
.div(2.0)
.floor() as u32; .floor() as u32;
} else { } else {
firstConfirmedCenter.replace(center); firstConfirmedCenter.replace(center);
@@ -679,10 +683,7 @@ impl<'a> FinderPatternFinder<'_> {
* Get square of distance between a and b. * Get square of distance between a and b.
*/ */
fn squaredDistance(a: &FinderPattern, b: &FinderPattern) -> f64 { fn squaredDistance(a: &FinderPattern, b: &FinderPattern) -> f64 {
let x = a.getX() as f64 - b.getX() as f64; Point::from(a).squaredDistance(Point::from(b)) as f64
let y = a.getY() as f64 - b.getY() as f64;
x * x + y * y
} }
/** /**

View File

@@ -21,7 +21,6 @@ use crate::{
point, point,
qrcode::decoder::Version, qrcode::decoder::Version,
DecodeHintType, DecodeHintValue, DecodingHintDictionary, Exceptions, Point, PointCallback, DecodeHintType, DecodeHintValue, DecodingHintDictionary, Exceptions, Point, PointCallback,
ResultPoint,
}; };
use super::{ use super::{
@@ -114,16 +113,16 @@ impl<'a> Detector<'_> {
// Anything above version 1 has an alignment pattern // Anything above version 1 has an alignment pattern
if !provisionalVersion.getAlignmentPatternCenters().is_empty() { if !provisionalVersion.getAlignmentPatternCenters().is_empty() {
// Guess where a "bottom right" finder pattern would have been // Guess where a "bottom right" finder pattern would have been
let bottomRightX = topRight.getX() - topLeft.getX() + bottomLeft.getX(); let bottomRightX = topRight.point.x - topLeft.point.x + bottomLeft.point.x;
let bottomRightY = topRight.getY() - topLeft.getY() + bottomLeft.getY(); let bottomRightY = topRight.point.y - topLeft.point.y + bottomLeft.point.y;
// Estimate that alignment pattern is closer by 3 modules // Estimate that alignment pattern is closer by 3 modules
// from "bottom right" to known top left location // from "bottom right" to known top left location
let correctionToTopLeft = 1.0 - (3.0 / modulesBetweenFPCenters as f32); let correctionToTopLeft = 1.0 - (3.0 / modulesBetweenFPCenters as f32);
let estAlignmentX = let estAlignmentX =
(topLeft.getX() + correctionToTopLeft * (bottomRightX - topLeft.getX())) as u32; (topLeft.point.x + correctionToTopLeft * (bottomRightX - topLeft.point.x)) as u32;
let estAlignmentY = 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 // Kind of arbitrary -- expand search radius before giving up
let mut i = 4; let mut i = 4;
@@ -151,16 +150,16 @@ impl<'a> Detector<'_> {
let bits = Detector::sampleGrid(self.image, &transform, dimension)?; let bits = Detector::sampleGrid(self.image, &transform, dimension)?;
let mut points = vec![ let mut points = vec![
bottomLeft.to_rxing_result_point(), Point::from(bottomLeft),
topLeft.to_rxing_result_point(), Point::from(topLeft),
topRight.to_rxing_result_point(), Point::from(topRight),
]; ];
if alignmentPattern.is_some() { if alignmentPattern.is_some() {
points.push( points.push(
alignmentPattern alignmentPattern
.ok_or(Exceptions::NotFoundException(None))? .ok_or(Exceptions::NotFoundException(None))?
.to_rxing_result_point(), .into(),
) )
} }

View File

@@ -182,10 +182,23 @@ impl Point {
f32::max(self.x.abs(), self.y.abs()) 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 { pub fn distance(self, p: Self) -> f32 {
(self - p).length() (self - p).length()
} }
pub fn abs(self) -> Self {
Self::new(self.x.abs(), self.y.abs())
}
pub fn fold<U, F: Fn(f32, f32) -> U>(self, f: F) -> U {
f(self.x, self.y)
}
/// Calculate a floating point pixel coordinate representing the 'center' of the pixel. /// 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. /// This is sort of the inverse operation of the PointI(PointF) conversion constructor.
/// See also the documentation of the GridSampler API. /// See also the documentation of the GridSampler API.