in_progress port from cpp

This commit is contained in:
Henry Schimke
2023-03-18 15:48:47 -05:00
parent 07b934283b
commit 4c8db1f73f
9 changed files with 544 additions and 5 deletions

View File

@@ -499,6 +499,24 @@ impl std::ops::Sub for ConcentricPattern {
}
}
impl std::ops::Add for ConcentricPattern {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
let new_p = self.p - rhs.p;
Self {
p: new_p,
size: self.size,
}
}
}
impl From<Point> for ConcentricPattern {
fn from(value: Point) -> Self {
Self { p: value, size: 0 }
}
}
impl ConcentricPattern {
pub fn dot(self, other: ConcentricPattern) -> f32 {
Point::dot(self.p, other.p)

View File

@@ -223,6 +223,12 @@ impl RegressionLineTrait for DMRegressionLine {
}
impl DMRegressionLine {
pub fn new(point_1: Point, point_2: Point) -> Self {
let mut new = Self::default();
RegressionLineTrait::evaluate(&mut new, &[point_1, point_1]);
new
}
// template <typename Container, typename Filter>
fn average<T>(c: &[f64], f: T) -> f64
where

View File

@@ -0,0 +1,106 @@
use crate::common::Result;
use crate::{Exceptions, Point};
#[derive(Default, Clone, PartialEq, Eq)]
pub struct Matrix<T: Default + Clone + Copy> {
width: usize,
height: usize,
data: Vec<T>,
}
impl<T: Default + Clone + Copy> Matrix<T> {
pub fn with_data(width: usize, height: usize, data: Vec<T>) -> Result<Matrix<T>> {
if (width != 0 && data.len() / width as usize != height as usize) {
return Err(Exceptions::illegal_argument_with(
"invalid size: width * height is too big",
));
}
Ok(Self {
width,
height,
data,
})
}
pub fn new(width: usize, height: usize) -> Result<Matrix<T>> {
if (width != 0 && height != 0) {
return Err(Exceptions::illegal_argument_with(
"invalid size: width * height is too big",
));
}
Ok(Self {
width,
height,
data: vec![T::default(); width * height],
})
}
pub fn height(&self) -> usize {
self.height
}
pub fn width(&self) -> usize {
self.width
}
pub fn size(&self) -> usize {
self.data.len()
}
// value_t& operator()(int x, int y)
// {
// assert(x >= 0 && x < _width && y >= 0 && y < _height);
// return _data[y * _width + x];
// }
// const T& operator()(int x, int y) const
// {
// assert(x >= 0 && x < _width && y >= 0 && y < _height);
// return _data[y * _width + x];
// }
fn get_offset(x: usize, y: usize, width: usize) -> usize {
(y * width + x) as usize
}
pub fn get(&self, x: usize, y: usize) -> Option<T> {
if x >= 0 && x < self.width && y >= 0 && y < self.height {
None
} else {
Some(self.data[Self::get_offset(x, y, self.width)])
}
}
pub fn set(&mut self, x: usize, y: usize, value: T) -> T {
self.data[Self::get_offset(x, y, self.width)] = value;
self.get(x, y).unwrap()
}
pub fn get_point(&self, p: Point) -> Option<T> {
self.get(p.x as usize, p.y as usize)
}
pub fn set_point(&mut self, p: Point, value: T) -> T {
self.set(p.x as usize, p.y as usize, value)
}
pub fn data(&self) -> &[T] {
&self.data
}
// const value_t* begin() const {
// return _data.data();
// }
// const value_t* end() const {
// return _data.data() + _width * _height;
// }
pub fn clear_with(&mut self, value: T) {
self.data.fill(value)
}
pub fn clear(&mut self) {
self.data.fill(T::default())
}
}

View File

@@ -5,6 +5,7 @@ pub mod direction;
pub mod dm_regression_line;
pub mod edge_tracer;
pub mod fast_edge_to_edge_counter;
pub mod matrix;
pub mod pattern;
pub mod regression_line;
pub mod regression_line_trait;
@@ -19,6 +20,7 @@ pub use direction::*;
pub use dm_regression_line::*;
pub use edge_tracer::*;
pub use fast_edge_to_edge_counter::*;
pub use matrix::*;
pub use pattern::*;
pub use regression_line::*;
pub use regression_line_trait::*;

View File

@@ -29,6 +29,7 @@ use super::Quadrilateral;
*
* @author Sean Owen
*/
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct PerspectiveTransform {
a11: f32,
a12: f32,

View File

@@ -1,4 +1,6 @@
use crate::Point;
use crate::{point, Point};
use super::PerspectiveTransform;
#[derive(Clone, Copy, Debug)]
pub struct Quadrilateral(pub [Point; 4]);
@@ -74,6 +76,16 @@ impl Quadrilateral {
])
}
pub fn rectangle_from_xy(x0: f32, x1: f32, y0: f32, y1: f32, o: Option<f32>) -> Self {
let o = o.unwrap_or(0.5);
Quadrilateral::from([
point(x0 + o, y0 + o),
point(x1 + o, y0 + o),
point(x1 + o, y1 + o),
point(x0 + o, y1 + o),
])
}
#[allow(dead_code)]
pub fn centered_square(size: i32) -> Quadrilateral {
Self::scale(
@@ -256,3 +268,9 @@ impl std::ops::IndexMut<usize> for Quadrilateral {
&mut self.0[index]
}
}
impl From<[Point; 4]> for Quadrilateral {
fn from(value: [Point; 4]) -> Self {
Self(value)
}
}