Skip to content

Commit

Permalink
Fix rkyv feature.
Browse files Browse the repository at this point in the history
  • Loading branch information
n3vu0r committed Aug 16, 2023
1 parent c48e6ec commit 5883bec
Show file tree
Hide file tree
Showing 12 changed files with 221 additions and 43 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Virtual Trackball Orbiting via the Exponential Map
[Documentation]: https://docs.rs/trackball/badge.svg
[Downloads]: https://img.shields.io/crates/d/trackball.svg
[Version]: https://img.shields.io/crates/v/trackball.svg
[Rust]: https://img.shields.io/badge/rust-stable-brightgreen.svg
[Rust]: https://img.shields.io/badge/rust-v1.70-brightgreen.svg
[License]: https://img.shields.io/badge/License-MIT%20OR%20Apache--2.0-blue.svg

This is an alternative trackball technique using exponential map and parallel transport to
Expand Down
6 changes: 6 additions & 0 deletions RELEASES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# Version 0.11.2 (2023-08-16)

* Make some types `Copy`.
* Fix `rkyv` by implementing `Archive` for `Self`.
* Enable `rkyv` for `docsrs` again.

# Version 0.11.1 (2023-08-09)

* Fix scale boundary in `Scope::scale()` mode.
Expand Down
34 changes: 29 additions & 5 deletions src/bound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,8 @@ use nalgebra::{Isometry3, Point3, RealField, UnitQuaternion, Vector3};
/// Orthogonal boundary conditions implementing [`Clamp`].
///
/// Implements [`Default`] and can be created with `Bound::default()`.
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(
feature = "rkyv",
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
)]
pub struct Bound<N: Copy + RealField> {
/// Isometry in world space of bound inversely transforming target and eye positions.
pub transform: Isometry3<N>,
Expand Down Expand Up @@ -124,3 +120,31 @@ impl<N: Copy + RealField> Clamp<N> for Bound<N> {
None
}
}

#[cfg(feature = "rkyv")]
impl<N: Copy + RealField> rkyv::Archive for Bound<N> {
type Archived = Self;
type Resolver = ();

#[inline]
#[allow(unsafe_code)]
unsafe fn resolve(&self, _: usize, _: Self::Resolver, out: *mut Self::Archived) {
out.write(rkyv::to_archived!(*self as Self));
}
}

#[cfg(feature = "rkyv")]
impl<Ser: rkyv::Fallible + ?Sized, N: Copy + RealField> rkyv::Serialize<Ser> for Bound<N> {
#[inline]
fn serialize(&self, _: &mut Ser) -> Result<Self::Resolver, Ser::Error> {
Ok(())
}
}

#[cfg(feature = "rkyv")]
impl<De: rkyv::Fallible + ?Sized, N: Copy + RealField> rkyv::Deserialize<Self, De> for Bound<N> {
#[inline]
fn deserialize(&self, _: &mut De) -> Result<Self, De::Error> {
Ok(rkyv::from_archived!(*self))
}
}
8 changes: 4 additions & 4 deletions src/clamp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ pub trait Clamp<N: Copy + RealField>: Send + Sync + Debug + 'static {
let pitch_axis = frame.pitch_axis();
// Old target position in eye space.
let old_target = frame.target() - eye;
let mut min_delta = delta.clone();
let mut min_delta = *delta;
let mut loops = 0;
loop {
let frame = min_delta.transform(frame);
Expand Down Expand Up @@ -133,7 +133,7 @@ pub trait Clamp<N: Copy + RealField>: Send + Sync + Debug + 'static {
let old_rot_inverse = frame.view().rotation.inverse();
// Old eye position in camera space.
let old_eye = old_rot_inverse * (frame.eye() - target);
let mut min_delta = delta.clone();
let mut min_delta = *delta;
let mut loops = 0;
loop {
let mut bound = false;
Expand Down Expand Up @@ -181,7 +181,7 @@ pub trait Clamp<N: Copy + RealField>: Send + Sync + Debug + 'static {
let old_target = frame.target();
let old_rot_inverse = frame.view().rotation.inverse();
let old_eye = frame.eye();
let mut min_delta = delta.clone();
let mut min_delta = *delta;
let mut loops = 0;
loop {
let frame = min_delta.transform(old_frame);
Expand Down Expand Up @@ -212,7 +212,7 @@ pub trait Clamp<N: Copy + RealField>: Send + Sync + Debug + 'static {
}
&Delta::Scale { mut rat, pos } => {
let old_zat = frame.distance();
let mut min_delta = delta.clone();
let mut min_delta = *delta;
let mut loops = 0;
loop {
let frame = min_delta.transform(frame);
Expand Down
38 changes: 31 additions & 7 deletions src/delta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,8 @@ use nalgebra::{Point3, RealField, Unit, UnitQuaternion, Vector3};
use simba::scalar::SubsetOf;

/// Delta transform from initial to final [`Frame`].
#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(
feature = "rkyv",
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
)]
pub enum Delta<N: Copy + RealField> {
/// Yields frame as identity transform (default).
Frame,
Expand Down Expand Up @@ -54,7 +50,7 @@ impl<N: Copy + RealField> Delta<N> {
/// Transforms from initial to final frame.
#[must_use]
pub fn transform(&self, frame: &Frame<N>) -> Frame<N> {
let mut frame = frame.clone();
let mut frame = *frame;
match self {
Self::Frame => {}
Self::First {
Expand Down Expand Up @@ -104,7 +100,7 @@ impl<N: Copy + RealField> Delta<N> {
/// * `t`: The interpolation parameter between 0 and 1.
#[must_use]
pub fn lerp_slerp(&self, t: N) -> Self {
match self.clone() {
match *self {
Self::Frame => Self::Frame,
Self::First {
pitch,
Expand Down Expand Up @@ -161,3 +157,31 @@ impl<N: Copy + RealField> Default for Delta<N> {
Self::Frame
}
}

#[cfg(feature = "rkyv")]
impl<N: Copy + RealField> rkyv::Archive for Delta<N> {
type Archived = Self;
type Resolver = ();

#[inline]
#[allow(unsafe_code)]
unsafe fn resolve(&self, _: usize, _: Self::Resolver, out: *mut Self::Archived) {
out.write(rkyv::to_archived!(*self as Self));
}
}

#[cfg(feature = "rkyv")]
impl<Ser: rkyv::Fallible + ?Sized, N: Copy + RealField> rkyv::Serialize<Ser> for Delta<N> {
#[inline]
fn serialize(&self, _: &mut Ser) -> Result<Self::Resolver, Ser::Error> {
Ok(())
}
}

#[cfg(feature = "rkyv")]
impl<De: rkyv::Fallible + ?Sized, N: Copy + RealField> rkyv::Deserialize<Self, De> for Delta<N> {
#[inline]
fn deserialize(&self, _: &mut De) -> Result<Self, De::Error> {
Ok(rkyv::from_archived!(*self))
}
}
34 changes: 29 additions & 5 deletions src/fixed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,8 @@ use simba::scalar::SubsetOf;
/// * Implements [`Default`] and can be created with `Fixed::default()` returning
/// `Fixed::Ver(N::frac_pi_4())`.
/// * Implements `From<N>` and can be created with `N::into()` returning `Fixed::Ver()`.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(
feature = "rkyv",
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
)]
pub enum Fixed<N: Copy + RealField> {
/// Fixed horizontal field of view aka Vert- scaling.
Hor(N),
Expand Down Expand Up @@ -108,3 +104,31 @@ impl<N: Copy + RealField> Fixed<N> {
}
}
}

#[cfg(feature = "rkyv")]
impl<N: Copy + RealField> rkyv::Archive for Fixed<N> {
type Archived = Self;
type Resolver = ();

#[inline]
#[allow(unsafe_code)]
unsafe fn resolve(&self, _: usize, _: Self::Resolver, out: *mut Self::Archived) {
out.write(rkyv::to_archived!(*self as Self));
}
}

#[cfg(feature = "rkyv")]
impl<Ser: rkyv::Fallible + ?Sized, N: Copy + RealField> rkyv::Serialize<Ser> for Fixed<N> {
#[inline]
fn serialize(&self, _: &mut Ser) -> Result<Self::Resolver, Ser::Error> {
Ok(())
}
}

#[cfg(feature = "rkyv")]
impl<De: rkyv::Fallible + ?Sized, N: Copy + RealField> rkyv::Deserialize<Self, De> for Fixed<N> {
#[inline]
fn deserialize(&self, _: &mut De) -> Result<Self, De::Error> {
Ok(rkyv::from_archived!(*self))
}
}
34 changes: 29 additions & 5 deletions src/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,8 @@ use nalgebra::{Isometry3, Point3, RealField, Unit, UnitQuaternion, Vector3};
use simba::scalar::SubsetOf;

/// Frame wrt camera eye and target.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(
feature = "rkyv",
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
)]
pub struct Frame<N: Copy + RealField> {
/// Target position in world space.
pos: Point3<N>,
Expand Down Expand Up @@ -248,3 +244,31 @@ where
&& self.zat.ulps_eq(&other.zat, epsilon, max_ulps)
}
}

#[cfg(feature = "rkyv")]
impl<N: Copy + RealField> rkyv::Archive for Frame<N> {
type Archived = Self;
type Resolver = ();

#[inline]
#[allow(unsafe_code)]
unsafe fn resolve(&self, _: usize, _: Self::Resolver, out: *mut Self::Archived) {
out.write(rkyv::to_archived!(*self as Self));
}
}

#[cfg(feature = "rkyv")]
impl<Ser: rkyv::Fallible + ?Sized, N: Copy + RealField> rkyv::Serialize<Ser> for Frame<N> {
#[inline]
fn serialize(&self, _: &mut Ser) -> Result<Self::Resolver, Ser::Error> {
Ok(())
}
}

#[cfg(feature = "rkyv")]
impl<De: rkyv::Fallible + ?Sized, N: Copy + RealField> rkyv::Deserialize<Self, De> for Frame<N> {
#[inline]
fn deserialize(&self, _: &mut De) -> Result<Self, De::Error> {
Ok(rkyv::from_archived!(*self))
}
}
38 changes: 31 additions & 7 deletions src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,8 @@ use nalgebra::{convert, zero, Isometry3, Matrix4, Point2, Point3, RealField, Vec
use simba::scalar::SubsetOf;

/// Image as projection of [`Scope`] wrt [`Frame`].
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(
feature = "rkyv",
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
)]
pub struct Image<N: Copy + RealField> {
/// Current position in screen space of hovering input or pointing device.
pos: Point2<N>,
Expand Down Expand Up @@ -44,8 +40,8 @@ impl<N: Copy + RealField> Image<N> {
pos: Point2::origin(),
max,
upp: zero(),
frame: frame.clone(),
scope: scope.clone(),
frame: *frame,
scope: *scope,
view_iso: Isometry3::identity(),
view_mat: zero(),
proj_mat: zero(),
Expand Down Expand Up @@ -236,3 +232,31 @@ impl<N: Copy + RealField> Image<N> {
}
}
}

#[cfg(feature = "rkyv")]
impl<N: Copy + RealField> rkyv::Archive for Image<N> {
type Archived = Self;
type Resolver = ();

#[inline]
#[allow(unsafe_code)]
unsafe fn resolve(&self, _: usize, _: Self::Resolver, out: *mut Self::Archived) {
out.write(rkyv::to_archived!(*self as Self));
}
}

#[cfg(feature = "rkyv")]
impl<Ser: rkyv::Fallible + ?Sized, N: Copy + RealField> rkyv::Serialize<Ser> for Image<N> {
#[inline]
fn serialize(&self, _: &mut Ser) -> Result<Self::Resolver, Ser::Error> {
Ok(())
}
}

#[cfg(feature = "rkyv")]
impl<De: rkyv::Fallible + ?Sized, N: Copy + RealField> rkyv::Deserialize<Self, De> for Image<N> {
#[inline]
fn deserialize(&self, _: &mut De) -> Result<Self, De::Error> {
Ok(rkyv::from_archived!(*self))
}
}
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@
//! }
//! ```

#![cfg_attr(any(feature = "rkyv", feature = "cc"), deny(unsafe_code))]
#![cfg_attr(not(any(feature = "rkyv", feature = "cc")), forbid(unsafe_code))]
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
#![forbid(missing_docs)]
#![deny(rustdoc::broken_intra_doc_links)]
Expand Down
2 changes: 2 additions & 0 deletions src/orbit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ impl Orbit<f32> {
.vec
.map(|(ray, len)| ray.into_inner().push(len))
.unwrap_or_default();
#[allow(unsafe_code)]
unsafe {
trackball_orbit_f(
rot.as_vector_mut().as_mut_ptr(),
Expand Down Expand Up @@ -182,6 +183,7 @@ impl Orbit<f64> {
.vec
.map(|(ray, len)| ray.into_inner().push(len))
.unwrap_or_default();
#[allow(unsafe_code)]
unsafe {
trackball_orbit_d(
rot.as_vector_mut().as_mut_ptr(),
Expand Down
32 changes: 28 additions & 4 deletions src/plane.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ use simba::scalar::SubsetOf;
/// Realizes plane equation `a*x+b*y+c*z+d=0` with unit normal `[x, y, z]` and signed bias `d`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(
feature = "rkyv",
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
)]
#[repr(C)]
pub struct Plane<N: Copy + RealField> {
/// Plane unit normal.
Expand Down Expand Up @@ -212,3 +208,31 @@ where
&& self.bias.ulps_eq(&other.bias, epsilon, max_ulps)
}
}

#[cfg(feature = "rkyv")]
impl<N: Copy + RealField> rkyv::Archive for Plane<N> {
type Archived = Self;
type Resolver = ();

#[inline]
#[allow(unsafe_code)]
unsafe fn resolve(&self, _: usize, _: Self::Resolver, out: *mut Self::Archived) {
out.write(rkyv::to_archived!(*self as Self));
}
}

#[cfg(feature = "rkyv")]
impl<Ser: rkyv::Fallible + ?Sized, N: Copy + RealField> rkyv::Serialize<Ser> for Plane<N> {
#[inline]
fn serialize(&self, _: &mut Ser) -> Result<Self::Resolver, Ser::Error> {
Ok(())
}
}

#[cfg(feature = "rkyv")]
impl<De: rkyv::Fallible + ?Sized, N: Copy + RealField> rkyv::Deserialize<Self, De> for Plane<N> {
#[inline]
fn deserialize(&self, _: &mut De) -> Result<Self, De::Error> {
Ok(rkyv::from_archived!(*self))
}
}
Loading

0 comments on commit 5883bec

Please sign in to comment.