remove indirection for RxingResultPoint

Previously, the struct was passed around everywhere as a
reference. It only holds two floats, so there's no real
need for indirection. Now it's being passed as a value.
This commit is contained in:
Vukašin Stepanović
2023-02-15 14:42:07 +00:00
parent 145cf704fe
commit 6b7099a03b
12 changed files with 229 additions and 267 deletions

View File

@@ -18,22 +18,26 @@ pub struct RXingResultPoint {
pub(crate) x: f32,
pub(crate) y: f32,
}
impl Hash for RXingResultPoint {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.x.to_string().hash(state);
self.y.to_string().hash(state);
}
}
impl PartialEq for RXingResultPoint {
fn eq(&self, other: &Self) -> bool {
self.x == other.x && self.y == other.y
}
}
impl Eq for RXingResultPoint {}
impl RXingResultPoint {
pub const fn new(x: f32, y: f32) -> Self {
Self { x, y }
}
pub const fn with_single(x: f32) -> Self {
Self { x, y: x }
}
@@ -47,12 +51,8 @@ impl std::ops::AddAssign for RXingResultPoint {
}
impl<'a> Sum<&'a RXingResultPoint> for RXingResultPoint {
fn sum<I: Iterator<Item = &'a RXingResultPoint>>(iter: I) -> Self {
let mut add = RXingResultPoint { x: 0.0, y: 0.0 };
for n in iter {
add += *n;
}
add
fn sum<I: Iterator<Item = &'a Self>>(iter: I) -> Self {
iter.fold(Self::default(), |acc, &p| acc + p)
}
}
@@ -65,7 +65,7 @@ impl ResultPoint for RXingResultPoint {
self.y
}
fn into_rxing_result_point(self) -> RXingResultPoint {
fn into_rxing_result_point(self) -> Self {
self
}
}
@@ -77,7 +77,7 @@ impl fmt::Display for RXingResultPoint {
}
impl std::ops::Sub<RXingResultPoint> for RXingResultPoint {
type Output = RXingResultPoint;
type Output = Self;
fn sub(self, rhs: Self) -> Self::Output {
Self {
@@ -87,19 +87,8 @@ impl std::ops::Sub<RXingResultPoint> for RXingResultPoint {
}
}
impl std::ops::Sub<&RXingResultPoint> for RXingResultPoint {
type Output = RXingResultPoint;
fn sub(self, rhs: &Self) -> Self::Output {
Self {
x: self.x - rhs.x,
y: self.y - rhs.y,
}
}
}
impl std::ops::Neg for RXingResultPoint {
type Output = RXingResultPoint;
type Output = Self;
fn neg(self) -> Self::Output {
Self {
@@ -110,9 +99,9 @@ impl std::ops::Neg for RXingResultPoint {
}
impl std::ops::Add<RXingResultPoint> for RXingResultPoint {
type Output = RXingResultPoint;
type Output = Self;
fn add(self, rhs: RXingResultPoint) -> Self::Output {
fn add(self, rhs: Self) -> Self::Output {
Self {
x: self.x + rhs.x,
y: self.y + rhs.y,
@@ -121,9 +110,9 @@ impl std::ops::Add<RXingResultPoint> for RXingResultPoint {
}
impl std::ops::Mul<RXingResultPoint> for RXingResultPoint {
type Output = RXingResultPoint;
type Output = Self;
fn mul(self, rhs: RXingResultPoint) -> Self::Output {
fn mul(self, rhs: Self) -> Self::Output {
Self {
x: self.x * rhs.x,
y: self.y * rhs.y,
@@ -132,7 +121,7 @@ impl std::ops::Mul<RXingResultPoint> for RXingResultPoint {
}
impl std::ops::Mul<f32> for RXingResultPoint {
type Output = RXingResultPoint;
type Output = Self;
fn mul(self, rhs: f32) -> Self::Output {
Self {
@@ -143,7 +132,7 @@ impl std::ops::Mul<f32> for RXingResultPoint {
}
impl std::ops::Mul<i32> for RXingResultPoint {
type Output = RXingResultPoint;
type Output = Self;
fn mul(self, rhs: i32) -> Self::Output {
Self {
@@ -157,7 +146,7 @@ impl std::ops::Mul<RXingResultPoint> for i32 {
type Output = RXingResultPoint;
fn mul(self, rhs: RXingResultPoint) -> Self::Output {
RXingResultPoint {
Self::Output {
x: rhs.x * self as f32,
y: rhs.y * self as f32,
}
@@ -168,29 +157,7 @@ impl std::ops::Mul<RXingResultPoint> for f32 {
type Output = RXingResultPoint;
fn mul(self, rhs: RXingResultPoint) -> Self::Output {
RXingResultPoint {
x: rhs.x * self,
y: rhs.y * self,
}
}
}
impl std::ops::Mul<&RXingResultPoint> for f32 {
type Output = RXingResultPoint;
fn mul(self, rhs: &RXingResultPoint) -> Self::Output {
RXingResultPoint {
x: rhs.x * self,
y: rhs.y * self,
}
}
}
impl std::ops::Mul<&mut RXingResultPoint> for f32 {
type Output = RXingResultPoint;
fn mul(self, rhs: &mut RXingResultPoint) -> Self::Output {
RXingResultPoint {
Self::Output {
x: rhs.x * self,
y: rhs.y * self,
}
@@ -209,66 +176,57 @@ impl std::ops::Div<f32> for RXingResultPoint {
}
impl RXingResultPoint {
pub fn dot(a: RXingResultPoint, b: RXingResultPoint) -> f32 {
a.x * b.x + a.y * b.y
pub fn dot(self, p: Self) -> f32 {
self.x * p.x + self.y * p.y
}
pub fn cross(a: &RXingResultPoint, b: &RXingResultPoint) -> f32 {
a.x * b.y - b.x * a.y
pub fn cross(self, p: Self) -> f32 {
self.x * p.y - p.x * self.y
}
/// L1 norm
pub fn sumAbsComponent(p: &RXingResultPoint) -> f32 {
(p.x).abs() + (p.y).abs()
pub fn sumAbsComponent(self) -> f32 {
self.x.abs() + self.y.abs()
}
/// L2 norm
pub fn length(p: RXingResultPoint) -> f32 {
(Self::dot(p, p)).sqrt()
pub fn length(self) -> f32 {
Self::dot(self, self).sqrt()
}
/// L-inf norm
pub fn maxAbsComponent(p: &RXingResultPoint) -> f32 {
let a = (p.x).abs();
let b = (p.y).abs();
if a > b {
a
} else {
b
}
// return std::cmp::max((p.x).abs(), (p.y).abs());
pub fn maxAbsComponent(self) -> f32 {
f32::max(self.x.abs(), self.y.abs())
}
pub fn distance(a: RXingResultPoint, b: RXingResultPoint) -> f32 {
Self::length(a - b)
pub fn distance(self, p: Self) -> f32 {
Self::length(self - p)
}
/// 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.
#[inline(always)]
pub fn centered(p: &RXingResultPoint) -> RXingResultPoint {
RXingResultPoint {
x: (p.x).floor() + 0.5,
y: (p.y).floor() + 0.5,
pub fn centered(self) -> Self {
Self {
x: self.x.floor() + 0.5,
y: self.y.floor() + 0.5,
}
}
pub fn normalized(d: RXingResultPoint) -> RXingResultPoint {
d / Self::length(d)
pub fn normalized(self) -> Self {
self / Self::length(self)
}
pub fn bresenhamDirection(d: &RXingResultPoint) -> RXingResultPoint {
*d / Self::maxAbsComponent(d)
pub fn bresenhamDirection(self) -> Self {
self / Self::maxAbsComponent(self)
}
pub fn mainDirection(d: RXingResultPoint) -> RXingResultPoint {
if (d.x).abs() > (d.y).abs() {
Self::new(d.x, 0.0)
pub fn mainDirection(self) -> Self {
if self.x.abs() > self.y.abs() {
Self::new(self.x, 0.0)
} else {
Self::new(0.0, d.y)
Self::new(0.0, self.y)
}
}
}