Skip to content

Commit

Permalink
More Numbers + l2math fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
AtomicGamer9523 committed Dec 1, 2023
1 parent 4ed8f21 commit f56fb45
Show file tree
Hide file tree
Showing 19 changed files with 984 additions and 315 deletions.
6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,7 @@ license = "MIT"

[profile.release]
opt-level = 3
debug = false
panic = "abort"
# debug = false
debug = true
# panic = "abort"
panic = "unwind"
2 changes: 1 addition & 1 deletion libs/l2math/bindings/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! For Documentation please refer [LLM](../llm)
//! For Documentation please refer [L2Math](../l2math)
#![cfg_attr(feature = "no_std", no_std)]

Expand Down
19 changes: 19 additions & 0 deletions libs/libodo/dualnum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -454,3 +454,22 @@ impl<T> core::ops::IndexMut<usize> for &mut DualNum<T> {
&mut self.0[i]
}
}

impl<T> core::ops::Mul<Vec2D> for DualNum<T> {
type Output = crate::Vec2DDual<T>;
#[inline]
fn mul(self, rhs: Vec2D) -> Self::Output {
let size = self.size();
let mut x_array = Array::with_capacity(size);
let mut y_array = Array::with_capacity(size);
for i in 0..size {
x_array[i] = self.0[i] * rhs.x();
y_array[i] = self.0[i] * rhs.y();
}
debug_assert!(x_array.len() <= 4);
debug_assert!(y_array.len() <= 4);
let x = Self::constructor(x_array);
let y = Self::constructor(y_array);
crate::Vec2DDual::new(x, y)
}
}
4 changes: 2 additions & 2 deletions libs/libodo/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ extern crate alloc;

mod dualnum;
// mod rotation2d;
// mod vec2ddual;
mod vec2ddual;

pub use dualnum::DualNum;
// pub use rotation2d::Rotation2D;
// pub use vec2ddual::Vec2DDual;
pub use vec2ddual::Vec2DDual;
106 changes: 75 additions & 31 deletions libs/libodo/vec2ddual.rs
Original file line number Diff line number Diff line change
@@ -1,40 +1,84 @@
#[allow(unused_imports)]
use libtrig::prelude::*;
use libtrig::Vec2D;
use libtrig::*;

use crate::DualNum;

pub struct Vec2DDual<T: Float> {
/// Dual version of a 2D vector.
pub struct Vec2DDual<T> {
/// The x component of the vector.
pub x: DualNum<T>,
/// The y component of the vector.
pub y: DualNum<T>,
}

impl Vec2DDual<Float64> {
// pub const fn new(v: Vec2D, n: Int) -> Self {
// Self {
// x: DualNum::constant(v.x(), n as usize),
// y: DualNum::constant(v.y(), n as usize),
// }
// }

// operator fun plus(v: Vector2d) = Vector2dDual(x + v.x, y + v.y)
// operator fun plus(v: Vector2dDual<Param>) = Vector2dDual(x + v.x, y + v.y)
// operator fun minus(v: Vector2dDual<Param>) = Vector2dDual(x - v.x, y - v.y)
// operator fun unaryMinus() = Vector2dDual(-x, -y)
//
// operator fun div(z: Double) = Vector2dDual(x / z, y / z)
//
// infix fun dot(v: Vector2dDual<Param>) = x * v.x + y * v.y
// fun sqrNorm() = this dot this
// fun norm() = sqrNorm().sqrt()
//
// fun bind() = Vector2dDual(x, y)
//
// fun <NewParam> reparam(oldParam: DualNum<NewParam>) =
// Vector2dDual(x.reparam(oldParam), y.reparam(oldParam))
//
// fun drop(n: Int) = Vector2dDual(x.drop(n), y.drop(n))
// fun value() = Vector2d(x.value(), y.value())
//
// // precondition: this is normalized
// fun angleCast() = Rotation2dDual(x, y)
impl<T> Vec2DDual<T> {
/// Construct a new Vec2DDual.
#[inline]
#[must_use = "This function creates a new Vec2DDual"]
pub const fn new(x: DualNum<T>, y: DualNum<T>) -> Self {
Self { x, y }
}
/// Construct a new Vec2DDual from a Vec2D.
#[inline]
#[must_use = "This function creates a new Vec2DDual"]
pub fn constant(v: Vec2D, n: usize) -> Self {
Self::new(DualNum::constant(v.x(), n), DualNum::constant(v.y(), n))
}
}

impl<T> core::ops::Add<Vec2D> for Vec2DDual<T> {
type Output = Self;
#[inline]
#[must_use = "This function creates a new Vec2DDual"]
fn add(self, rhs: Vec2D) -> Self::Output {
Self::new(self.x + rhs.x(), self.y + rhs.y())
}
}

impl<T> core::ops::Add<Vec2DDual<T>> for Vec2DDual<T> {
type Output = Self;
#[inline]
#[must_use = "This function creates a new Vec2DDual"]
fn add(self, rhs: Vec2DDual<T>) -> Self::Output {
Self::new(self.x + rhs.x, self.y + rhs.y)
}
}

impl<T> core::ops::Sub<Vec2D> for Vec2DDual<T> {
type Output = Self;
#[inline]
#[must_use = "This function creates a new Vec2DDual"]
fn sub(self, rhs: Vec2D) -> Self::Output {
Self::new(self.x - rhs.x(), self.y - rhs.y())
}
}

impl<T> core::ops::Sub<Vec2DDual<T>> for Vec2DDual<T> {
type Output = Self;
#[inline]
#[must_use = "This function creates a new Vec2DDual"]
fn sub(self, rhs: Vec2DDual<T>) -> Self::Output {
Self::new(self.x - rhs.x, self.y - rhs.y)
}
}

impl<T> core::ops::Neg for Vec2DDual<T> {
type Output = Self;
#[inline]
#[must_use = "This function creates a new Vec2DDual"]
fn neg(self) -> Self::Output {
Self::new(-self.x, -self.y)
}
}

impl<T> core::ops::Div<Float64> for Vec2DDual<T> {
type Output = Self;
#[inline]
#[must_use = "This function creates a new Vec2DDual"]
fn div(self, rhs: Float64) -> Self::Output {
Self::new(self.x / rhs, self.y / rhs)
}
}

// impl Dot for
2 changes: 1 addition & 1 deletion libs/libtrig/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

## Trigonometry functionality (angles, vectors, etc.)

Uses [`llm`](../llm/) for math functions.
Uses [`l2math`](../l2math/) for math functions.

It is primarily intended to be used by other libraries that require trigonometry functionality. Such as [`libodo`](../libodo/) for odometry, and [`libpath`](../libpath/) for pathing.
1 change: 1 addition & 0 deletions libs/libtrig/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![no_std]
#![cfg_attr(feature = "unstable", feature(const_fn_floating_point_arithmetic))]
#![cfg_attr(feature = "unstable", feature(const_mut_refs))]
#![cfg_attr(feature = "unstable", feature(const_mut_refs))]
#![warn(missing_docs, unused, clippy::all, unsafe_code)]
#![doc = include_str!("./README.md")]

Expand Down
8 changes: 6 additions & 2 deletions libs/libtrig/morenums/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,25 @@ mod _u2;
#[path = "./u3/mod.rs"]
mod _u3;

/// A simple type alias for a bit.
#[allow(non_camel_case_types)]
pub type bit = bool;

pub use _u2::u2;
pub use _u3::u3;

impl From<u2> for u3 {
#[must_use]
#[inline(always)]
fn from(u: u2) -> Self {
u3::new(u.0, u.1, false)
u3::fromu2(u)
}
}

impl From<u3> for u2 {
#[must_use]
#[inline(always)]
fn from(u: u3) -> Self {
u2::new(u.0, u.1)
u2::fromu3(u)
}
}
32 changes: 30 additions & 2 deletions libs/libtrig/morenums/u2/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ impl core::default::Default for u2 {
}
}

impl core::cmp::Eq for u2 {}

impl From<u2> for u8 {
/// Convert a `u2` to a `u8`.
#[must_use]
Expand Down Expand Up @@ -143,3 +141,33 @@ impl From<u2> for crate::Float64 {
u8::from(u) as crate::Float64
}
}

impl core::ops::AddAssign for u2 {
#[inline]
fn add_assign(&mut self, rhs: Self) {
*self = *self + rhs;
}
}

impl core::ops::SubAssign for u2 {
#[inline]
fn sub_assign(&mut self, rhs: Self) {
*self = *self - rhs;
}
}

impl core::ops::MulAssign for u2 {
#[inline]
fn mul_assign(&mut self, rhs: Self) {
*self = *self * rhs;
}
}

impl core::ops::DivAssign for u2 {
#[inline]
fn div_assign(&mut self, rhs: Self) {
*self = *self / rhs;
}
}

impl crate::traits::Number for u2 {}
Loading

0 comments on commit f56fb45

Please sign in to comment.