From 71128a81bc2a586bfdd85a96acda5ecac09cd959 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vuka=C5=A1in=20Stepanovi=C4=87?= Date: Thu, 16 Feb 2023 09:37:21 +0000 Subject: [PATCH] remove MathUtils::round() --- src/aztec/detector.rs | 36 ++++++-------- src/common/detector/MathUtils.rs | 47 ++++++------------- .../detector/white_rectangle_detector.rs | 8 ++-- src/qrcode/detector/qrcode_detector.rs | 9 ++-- 4 files changed, 35 insertions(+), 65 deletions(-) diff --git a/src/aztec/detector.rs b/src/aztec/detector.rs index fdba244..6d44eea 100644 --- a/src/aztec/detector.rs +++ b/src/aztec/detector.rs @@ -18,7 +18,7 @@ use std::fmt; use crate::{ common::{ - detector::{MathUtils, WhiteRectangleDetector}, + detector::WhiteRectangleDetector, reedsolomon::{self, ReedSolomonDecoder}, BitMatrix, DefaultGridSampler, GridSampler, Result, }, @@ -403,12 +403,10 @@ impl<'a> Detector<'_> { // } //Compute the center of the rectangle - let mut cx = MathUtils::round( - (point_a.getX() + point_d.getX() + point_b.getX() + point_c.getX()) / 4.0f32, - ); - let mut cy = MathUtils::round( - (point_a.getY() + point_d.getY() + point_b.getY() + point_c.getY()) / 4.0f32, - ); + let mut cx = ((point_a.getX() + point_d.getX() + point_b.getX() + point_c.getX()) / 4.0) + .round() as i32; + let mut cy = ((point_a.getY() + point_d.getY() + point_b.getY() + point_c.getY()) / 4.0) + .round() as i32; // Redetermine the white rectangle starting from previously computed center. // This will ensure that we end up with a white rectangle in center bull's eye @@ -455,12 +453,10 @@ impl<'a> Detector<'_> { // } // Recompute the center of the rectangle - cx = MathUtils::round( - (point_a.getX() + point_d.getX() + point_b.getX() + point_c.getX()) / 4.0f32, - ); - cy = MathUtils::round( - (point_a.getY() + point_d.getY() + point_b.getY() + point_c.getY()) / 4.0f32, - ); + cx = ((point_a.getX() + point_d.getX() + point_b.getX() + point_c.getX()) / 4.0).round() + as i32; + cy = ((point_a.getY() + point_d.getY() + point_b.getY() + point_c.getY()) / 4.0).round() + as i32; AztecPoint::new(cx, cy) } @@ -541,8 +537,8 @@ impl<'a> Detector<'_> { for i in 0..size { // for (int i = 0; i < size; i++) { if self.image.get( - MathUtils::round(px + i as f32 * dx) as u32, - MathUtils::round(py + i as f32 * dy) as u32, + (px + i as f32 * dx).round() as u32, + (py + i as f32 * dy).round() as u32, ) { result |= 1 << (size - i - 1); } @@ -629,11 +625,7 @@ impl<'a> Detector<'_> { for _i in 0..i_max { // for (int i = 0; i < iMax; i++) { - if self - .image - .get(MathUtils::round(px) as u32, MathUtils::round(py) as u32) - != color_model - { + if self.image.get(px.round() as u32, py.round() as u32) != color_model { error += 1; } px += dx; @@ -710,8 +702,8 @@ impl<'a> Detector<'_> { } fn is_valid(&self, point: Point) -> bool { - let x = MathUtils::round(point.getX()); - let y = MathUtils::round(point.getY()); + let x = point.getX().round() as i32; + let y = point.getY().round() as i32; self.is_valid_points(x, y) } diff --git a/src/common/detector/MathUtils.rs b/src/common/detector/MathUtils.rs index 3f1d1dc..621c929 100644 --- a/src/common/detector/MathUtils.rs +++ b/src/common/detector/MathUtils.rs @@ -14,27 +14,12 @@ * limitations under the License. */ -use std::{f32, i32, ops::Add}; +use std::ops::Add; /** * General math-related and numeric utility functions. */ -/** - * Ends up being a bit faster than {@link Math#round(float)}. This merely rounds its - * argument to the nearest int, where x.5 rounds up to x+1. Semantics of this shortcut - * differ slightly from {@link Math#round(float)} in that half rounds down for negative - * values. -2.5 rounds to -3, not -2. For purposes here it makes no difference. - * - * @param d real value to round - * @return nearest {@code int} - */ -#[inline(always)] -pub fn round(d: f32) -> i32 { - // (d + (if d < 0.0f32 { -0.5f32 } else { 0.5f32 })) as i32 - d.round() as i32 -} - // /** // * @param aX point A x coordinate // * @param aY point A y coordinate @@ -77,33 +62,31 @@ where #[cfg(test)] mod tests { - use crate::common::detector::MathUtils; - // static EPSILON: f32 = 1.0E-8f32; #[test] fn testRound() { - assert_eq!(-1, MathUtils::round(-1.0f32)); - assert_eq!(0, MathUtils::round(0.0f32)); - assert_eq!(1, MathUtils::round(1.0f32)); + assert_eq!(-1, (-1.0f32).round() as i32); + assert_eq!(0, (0.0f32).round() as i32); + assert_eq!(1, (1.0f32).round() as i32); - assert_eq!(2, MathUtils::round(1.9f32)); - assert_eq!(2, MathUtils::round(2.1f32)); + assert_eq!(2, (1.9f32).round() as i32); + assert_eq!(2, (2.1f32).round() as i32); - assert_eq!(3, MathUtils::round(2.5f32)); + assert_eq!(3, (2.5f32).round() as i32); - assert_eq!(-2, MathUtils::round(-1.9f32)); - assert_eq!(-2, MathUtils::round(-2.1f32)); + assert_eq!(-2, (-1.9f32).round() as i32); + assert_eq!(-2, (-2.1f32).round() as i32); - assert_eq!(-3, MathUtils::round(-2.5f32)); // This differs from Math.round() + assert_eq!(-3, (-2.5f32).round() as i32); // This differs from Math.round() - assert_eq!(i32::MAX, MathUtils::round(i32::MAX as f32)); - assert_eq!(i32::MIN, MathUtils::round(i32::MIN as f32)); + assert_eq!(i32::MAX, (i32::MAX as f32).round() as i32); + assert_eq!(i32::MIN, (i32::MIN as f32).round() as i32); - assert_eq!(i32::MAX, MathUtils::round(f32::MAX)); - assert_eq!(i32::MIN, MathUtils::round(f32::NEG_INFINITY)); + assert_eq!(i32::MAX, (f32::MAX).round() as i32); + assert_eq!(i32::MIN, (f32::NEG_INFINITY).round() as i32); - assert_eq!(0, MathUtils::round(f32::NAN)); + assert_eq!(0, (f32::NAN).round() as i32); } // #[test] diff --git a/src/common/detector/white_rectangle_detector.rs b/src/common/detector/white_rectangle_detector.rs index 7cdb660..cb8df39 100644 --- a/src/common/detector/white_rectangle_detector.rs +++ b/src/common/detector/white_rectangle_detector.rs @@ -21,8 +21,6 @@ use crate::{ Exceptions, Point, ResultPoint, }; -use super::MathUtils; - /** *

* Detects a candidate barcode-like rectangular region within an image. It @@ -293,13 +291,13 @@ impl<'a> WhiteRectangleDetector<'_> { let a = Point::new(a_x, a_y); let b = Point::new(b_x, b_y); - let dist = MathUtils::round(a.distance(b)); + let dist = a.distance(b).round() as i32; let x_step: f32 = (b_x - a_x) / dist as f32; let y_step: f32 = (b_y - a_y) / dist as f32; for i in 0..dist { - let x = MathUtils::round(a_x + i as f32 * x_step); - let y = MathUtils::round(a_y + i as f32 * y_step); + let x = (a_x + i as f32 * x_step).round() as i32; + let y = (a_y + i as f32 * y_step).round() as i32; if self.image.get(x as u32, y as u32) { return Some(Point::new(x as f32, y as f32)); } diff --git a/src/qrcode/detector/qrcode_detector.rs b/src/qrcode/detector/qrcode_detector.rs index 7016528..eda48de 100644 --- a/src/qrcode/detector/qrcode_detector.rs +++ b/src/qrcode/detector/qrcode_detector.rs @@ -17,10 +17,7 @@ use std::collections::HashMap; use crate::{ - common::{ - detector::MathUtils, BitMatrix, DefaultGridSampler, GridSampler, PerspectiveTransform, - Result, - }, + common::{BitMatrix, DefaultGridSampler, GridSampler, PerspectiveTransform, Result}, qrcode::decoder::Version, result_point_utils, DecodeHintType, DecodeHintValue, DecodingHintDictionary, Exceptions, Point, PointCallback, ResultPoint, @@ -235,9 +232,9 @@ impl<'a> Detector<'_> { moduleSize: f32, ) -> Result { let tltrCentersDimension = - MathUtils::round(result_point_utils::distance(topLeft, topRight) / moduleSize); + (result_point_utils::distance(topLeft, topRight) / moduleSize).round() as i32; let tlblCentersDimension = - MathUtils::round(result_point_utils::distance(topLeft, bottomLeft) / moduleSize); + (result_point_utils::distance(topLeft, bottomLeft) / moduleSize).round() as i32; let mut dimension = ((tltrCentersDimension + tlblCentersDimension) / 2) + 7; match dimension & 0x03 { 0 => dimension += 1,