mirror of
https://github.com/starovoid/rxing.git
synced 2026-07-27 12:52:34 +00:00
remove MathUtils::round()
This commit is contained in:
@@ -18,7 +18,7 @@ use std::fmt;
|
|||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
common::{
|
common::{
|
||||||
detector::{MathUtils, WhiteRectangleDetector},
|
detector::WhiteRectangleDetector,
|
||||||
reedsolomon::{self, ReedSolomonDecoder},
|
reedsolomon::{self, ReedSolomonDecoder},
|
||||||
BitMatrix, DefaultGridSampler, GridSampler, Result,
|
BitMatrix, DefaultGridSampler, GridSampler, Result,
|
||||||
},
|
},
|
||||||
@@ -403,12 +403,10 @@ impl<'a> Detector<'_> {
|
|||||||
// }
|
// }
|
||||||
|
|
||||||
//Compute the center of the rectangle
|
//Compute the center of the rectangle
|
||||||
let mut cx = MathUtils::round(
|
let mut cx = ((point_a.getX() + point_d.getX() + point_b.getX() + point_c.getX()) / 4.0)
|
||||||
(point_a.getX() + point_d.getX() + point_b.getX() + point_c.getX()) / 4.0f32,
|
.round() as i32;
|
||||||
);
|
let mut cy = ((point_a.getY() + point_d.getY() + point_b.getY() + point_c.getY()) / 4.0)
|
||||||
let mut cy = MathUtils::round(
|
.round() as i32;
|
||||||
(point_a.getY() + point_d.getY() + point_b.getY() + point_c.getY()) / 4.0f32,
|
|
||||||
);
|
|
||||||
|
|
||||||
// Redetermine the white rectangle starting from previously computed center.
|
// 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
|
// 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
|
// Recompute the center of the rectangle
|
||||||
cx = MathUtils::round(
|
cx = ((point_a.getX() + point_d.getX() + point_b.getX() + point_c.getX()) / 4.0).round()
|
||||||
(point_a.getX() + point_d.getX() + point_b.getX() + point_c.getX()) / 4.0f32,
|
as i32;
|
||||||
);
|
cy = ((point_a.getY() + point_d.getY() + point_b.getY() + point_c.getY()) / 4.0).round()
|
||||||
cy = MathUtils::round(
|
as i32;
|
||||||
(point_a.getY() + point_d.getY() + point_b.getY() + point_c.getY()) / 4.0f32,
|
|
||||||
);
|
|
||||||
|
|
||||||
AztecPoint::new(cx, cy)
|
AztecPoint::new(cx, cy)
|
||||||
}
|
}
|
||||||
@@ -541,8 +537,8 @@ impl<'a> Detector<'_> {
|
|||||||
for i in 0..size {
|
for i in 0..size {
|
||||||
// for (int i = 0; i < size; i++) {
|
// for (int i = 0; i < size; i++) {
|
||||||
if self.image.get(
|
if self.image.get(
|
||||||
MathUtils::round(px + i as f32 * dx) as u32,
|
(px + i as f32 * dx).round() as u32,
|
||||||
MathUtils::round(py + i as f32 * dy) as u32,
|
(py + i as f32 * dy).round() as u32,
|
||||||
) {
|
) {
|
||||||
result |= 1 << (size - i - 1);
|
result |= 1 << (size - i - 1);
|
||||||
}
|
}
|
||||||
@@ -629,11 +625,7 @@ impl<'a> Detector<'_> {
|
|||||||
for _i in 0..i_max {
|
for _i in 0..i_max {
|
||||||
// for (int i = 0; i < iMax; i++) {
|
// for (int i = 0; i < iMax; i++) {
|
||||||
|
|
||||||
if self
|
if self.image.get(px.round() as u32, py.round() as u32) != color_model {
|
||||||
.image
|
|
||||||
.get(MathUtils::round(px) as u32, MathUtils::round(py) as u32)
|
|
||||||
!= color_model
|
|
||||||
{
|
|
||||||
error += 1;
|
error += 1;
|
||||||
}
|
}
|
||||||
px += dx;
|
px += dx;
|
||||||
@@ -710,8 +702,8 @@ impl<'a> Detector<'_> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn is_valid(&self, point: Point) -> bool {
|
fn is_valid(&self, point: Point) -> bool {
|
||||||
let x = MathUtils::round(point.getX());
|
let x = point.getX().round() as i32;
|
||||||
let y = MathUtils::round(point.getY());
|
let y = point.getY().round() as i32;
|
||||||
self.is_valid_points(x, y)
|
self.is_valid_points(x, y)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -14,27 +14,12 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
use std::{f32, i32, ops::Add};
|
use std::ops::Add;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* General math-related and numeric utility functions.
|
* 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 aX point A x coordinate
|
||||||
// * @param aY point A y coordinate
|
// * @param aY point A y coordinate
|
||||||
@@ -77,33 +62,31 @@ where
|
|||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use crate::common::detector::MathUtils;
|
|
||||||
|
|
||||||
// static EPSILON: f32 = 1.0E-8f32;
|
// static EPSILON: f32 = 1.0E-8f32;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn testRound() {
|
fn testRound() {
|
||||||
assert_eq!(-1, MathUtils::round(-1.0f32));
|
assert_eq!(-1, (-1.0f32).round() as i32);
|
||||||
assert_eq!(0, MathUtils::round(0.0f32));
|
assert_eq!(0, (0.0f32).round() as i32);
|
||||||
assert_eq!(1, MathUtils::round(1.0f32));
|
assert_eq!(1, (1.0f32).round() as i32);
|
||||||
|
|
||||||
assert_eq!(2, MathUtils::round(1.9f32));
|
assert_eq!(2, (1.9f32).round() as i32);
|
||||||
assert_eq!(2, MathUtils::round(2.1f32));
|
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, (-1.9f32).round() as i32);
|
||||||
assert_eq!(-2, MathUtils::round(-2.1f32));
|
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::MAX, (i32::MAX as f32).round() as i32);
|
||||||
assert_eq!(i32::MIN, MathUtils::round(i32::MIN as f32));
|
assert_eq!(i32::MIN, (i32::MIN as f32).round() as i32);
|
||||||
|
|
||||||
assert_eq!(i32::MAX, MathUtils::round(f32::MAX));
|
assert_eq!(i32::MAX, (f32::MAX).round() as i32);
|
||||||
assert_eq!(i32::MIN, MathUtils::round(f32::NEG_INFINITY));
|
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]
|
// #[test]
|
||||||
|
|||||||
@@ -21,8 +21,6 @@ use crate::{
|
|||||||
Exceptions, Point, ResultPoint,
|
Exceptions, Point, ResultPoint,
|
||||||
};
|
};
|
||||||
|
|
||||||
use super::MathUtils;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>
|
* <p>
|
||||||
* Detects a candidate barcode-like rectangular region within an image. It
|
* 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 a = Point::new(a_x, a_y);
|
||||||
let b = Point::new(b_x, b_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 x_step: f32 = (b_x - a_x) / dist as f32;
|
||||||
let y_step: f32 = (b_y - a_y) / dist as f32;
|
let y_step: f32 = (b_y - a_y) / dist as f32;
|
||||||
|
|
||||||
for i in 0..dist {
|
for i in 0..dist {
|
||||||
let x = MathUtils::round(a_x + i as f32 * x_step);
|
let x = (a_x + i as f32 * x_step).round() as i32;
|
||||||
let y = MathUtils::round(a_y + i as f32 * y_step);
|
let y = (a_y + i as f32 * y_step).round() as i32;
|
||||||
if self.image.get(x as u32, y as u32) {
|
if self.image.get(x as u32, y as u32) {
|
||||||
return Some(Point::new(x as f32, y as f32));
|
return Some(Point::new(x as f32, y as f32));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,10 +17,7 @@
|
|||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
common::{
|
common::{BitMatrix, DefaultGridSampler, GridSampler, PerspectiveTransform, Result},
|
||||||
detector::MathUtils, BitMatrix, DefaultGridSampler, GridSampler, PerspectiveTransform,
|
|
||||||
Result,
|
|
||||||
},
|
|
||||||
qrcode::decoder::Version,
|
qrcode::decoder::Version,
|
||||||
result_point_utils, DecodeHintType, DecodeHintValue, DecodingHintDictionary, Exceptions, Point,
|
result_point_utils, DecodeHintType, DecodeHintValue, DecodingHintDictionary, Exceptions, Point,
|
||||||
PointCallback, ResultPoint,
|
PointCallback, ResultPoint,
|
||||||
@@ -235,9 +232,9 @@ impl<'a> Detector<'_> {
|
|||||||
moduleSize: f32,
|
moduleSize: f32,
|
||||||
) -> Result<u32> {
|
) -> Result<u32> {
|
||||||
let tltrCentersDimension =
|
let tltrCentersDimension =
|
||||||
MathUtils::round(result_point_utils::distance(topLeft, topRight) / moduleSize);
|
(result_point_utils::distance(topLeft, topRight) / moduleSize).round() as i32;
|
||||||
let tlblCentersDimension =
|
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;
|
let mut dimension = ((tltrCentersDimension + tlblCentersDimension) / 2) + 7;
|
||||||
match dimension & 0x03 {
|
match dimension & 0x03 {
|
||||||
0 => dimension += 1,
|
0 => dimension += 1,
|
||||||
|
|||||||
Reference in New Issue
Block a user