Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve RXingResultPoint usage #13

Merged
merged 19 commits into from
Feb 17, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
remove impl ResultPoint for FinderPattern & AlignmentPattern
Asha20 committed Feb 16, 2023
commit 1322f7defac2150c61285ab733666c6c445abd7c
17 changes: 4 additions & 13 deletions src/multi/qrcode/detector/multi_finder_pattern_finder.rs
Original file line number Diff line number Diff line change
@@ -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);
22 changes: 7 additions & 15 deletions src/qrcode/detector/alignment_pattern.rs
Original file line number Diff line number Diff line change
@@ -16,7 +16,7 @@

//Point

use crate::{point, Point, ResultPoint};
use crate::{point, Point};

/**
* <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 {
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<AlignmentPattern> for Point {
fn from(value: AlignmentPattern) -> Self {
value.point
}
}

@@ -63,7 +55,7 @@ impl AlignmentPattern {
* 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 {
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();
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.
*/
pub fn combineEstimate(&self, i: f32, j: f32, newModuleSize: f32) -> AlignmentPattern {
let combinedX = (self.getX() + j) / 2.0;
let combinedY = (self.getY() + i) / 2.0;
let combinedX = (self.point.x + j) / 2.0;
let combinedY = (self.point.y + i) / 2.0;
let combinedModuleSize = (self.estimatedModuleSize + newModuleSize) / 2.0;
AlignmentPattern::new(combinedX, combinedY, combinedModuleSize)
}
24 changes: 5 additions & 19 deletions src/qrcode/detector/finder_pattern.rs
Original file line number Diff line number Diff line change
@@ -14,7 +14,7 @@
* 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
@@ -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.</p>
*/
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(
19 changes: 10 additions & 9 deletions src/qrcode/detector/finder_pattern_finder.rs
Original file line number Diff line number Diff line change
@@ -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
}

/**
17 changes: 8 additions & 9 deletions src/qrcode/detector/qrcode_detector.rs
Original file line number Diff line number Diff line change
@@ -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(),
)
}

13 changes: 13 additions & 0 deletions src/rxing_result_point.rs
Original file line number Diff line number Diff line change
@@ -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, 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.
/// This is sort of the inverse operation of the PointI(PointF) conversion constructor.
/// See also the documentation of the GridSampler API.