use color::{AlphaColor, Srgb};
use glam::DVec3;
use ranim_core::Extract;
use ranim_core::anchor::{Aabb, Locate};
use ranim_core::core_item::CoreItem;
use ranim_core::core_item::vitem::Basis2d;
use ranim_core::{color, glam};
use ranim_core::traits::{
Opacity, RotateTransform, ScaleTransform, ShiftTransform, StrokeColor, With,
};
use crate::vitem::geometry::EllipticArc;
use crate::vitem::{DEFAULT_STROKE_WIDTH, VItem};
use ranim_core::anchor::AabbPoint;
#[derive(Clone, Debug, ranim_macros::Interpolatable)]
pub struct Arc {
pub basis: Basis2d,
pub center: DVec3,
pub radius: f64,
pub angle: f64,
pub stroke_rgba: AlphaColor<Srgb>,
pub stroke_width: f32,
}
impl Arc {
pub fn new(angle: f64, radius: f64) -> Self {
Self {
basis: Basis2d::default(),
center: DVec3::ZERO,
radius,
angle,
stroke_rgba: AlphaColor::WHITE,
stroke_width: DEFAULT_STROKE_WIDTH,
}
}
pub fn scale(&mut self, scale: f64) -> &mut Self {
self.scale_by_anchor(scale, AabbPoint::CENTER)
}
pub fn scale_by_anchor<T>(&mut self, scale: f64, anchor: T) -> &mut Self
where
T: Locate<Self>,
{
let anchor = anchor.locate(self);
self.radius *= scale;
self.center
.shift(-anchor)
.scale(DVec3::splat(scale))
.shift(anchor);
self
}
pub fn start(&self) -> DVec3 {
self.center + self.radius * self.basis.u()
}
pub fn end(&self) -> DVec3 {
let u = self.angle.cos() * self.basis.u();
let v = self.angle.sin() * self.basis.v();
self.center + self.radius * (u + v)
}
}
impl Aabb for Arc {
fn aabb(&self) -> [DVec3; 2] {
VItem::from(self.clone()).aabb()
}
}
impl ShiftTransform for Arc {
fn shift(&mut self, shift: DVec3) -> &mut Self {
self.center.shift(shift);
self
}
}
impl RotateTransform for Arc {
fn rotate_on_axis(&mut self, axis: DVec3, angle: f64) -> &mut Self {
self.center.rotate_on_axis(axis, angle);
self.basis.rotate_on_axis(axis, angle);
self
}
}
impl Opacity for Arc {
fn set_opacity(&mut self, opacity: f32) -> &mut Self {
self.stroke_rgba = self.stroke_rgba.with_alpha(opacity);
self
}
}
impl StrokeColor for Arc {
fn stroke_color(&self) -> AlphaColor<Srgb> {
self.stroke_rgba
}
fn set_stroke_color(&mut self, color: AlphaColor<Srgb>) -> &mut Self {
self.stroke_rgba = color;
self
}
fn set_stroke_opacity(&mut self, opacity: f32) -> &mut Self {
self.stroke_rgba = self.stroke_rgba.with_alpha(opacity);
self
}
}
impl From<Arc> for VItem {
fn from(value: Arc) -> Self {
EllipticArc::from(value).into()
}
}
impl Extract for Arc {
type Target = CoreItem;
fn extract_into(&self, buf: &mut Vec<Self::Target>) {
VItem::from(self.clone()).extract_into(buf);
}
}
#[derive(Clone, Debug, ranim_macros::Interpolatable)]
pub struct ArcBetweenPoints {
pub basis: Basis2d,
pub start: DVec3,
pub end: DVec3,
pub angle: f64,
pub stroke_rgba: AlphaColor<Srgb>,
pub stroke_width: f32,
}
impl ArcBetweenPoints {
pub fn new(start: DVec3, end: DVec3, angle: f64) -> Self {
Self {
basis: Basis2d::default(),
start,
end,
angle,
stroke_rgba: AlphaColor::WHITE,
stroke_width: DEFAULT_STROKE_WIDTH,
}
}
pub fn scale(&mut self, scale: f64) -> &mut Self {
self.scale_at(scale, AabbPoint::CENTER)
}
pub fn scale_at<T>(&mut self, scale: f64, anchor: T) -> &mut Self
where
T: Locate<Self>,
{
let point = anchor.locate(self);
self.start
.shift(-point)
.scale(DVec3::splat(scale))
.shift(point);
self.end
.shift(-point)
.scale(DVec3::splat(scale))
.shift(point);
self
}
}
impl Aabb for ArcBetweenPoints {
fn aabb(&self) -> [DVec3; 2] {
Arc::from(self.clone()).aabb()
}
}
impl ShiftTransform for ArcBetweenPoints {
fn shift(&mut self, shift: DVec3) -> &mut Self {
self.start.shift(shift);
self.end.shift(shift);
self
}
}
impl RotateTransform for ArcBetweenPoints {
fn rotate_on_axis(&mut self, axis: DVec3, angle: f64) -> &mut Self {
self.start.rotate_on_axis(axis, angle);
self.end.rotate_on_axis(axis, angle);
self.basis.rotate_on_axis(axis, angle);
self
}
}
impl Opacity for ArcBetweenPoints {
fn set_opacity(&mut self, opacity: f32) -> &mut Self {
self.stroke_rgba = self.stroke_rgba.with_alpha(opacity);
self
}
}
impl StrokeColor for ArcBetweenPoints {
fn stroke_color(&self) -> AlphaColor<Srgb> {
self.stroke_rgba
}
fn set_stroke_color(&mut self, color: AlphaColor<Srgb>) -> &mut Self {
self.stroke_rgba = color;
self
}
fn set_stroke_opacity(&mut self, opacity: f32) -> &mut Self {
self.stroke_rgba = self.stroke_rgba.with_alpha(opacity);
self
}
}
impl From<ArcBetweenPoints> for Arc {
fn from(value: ArcBetweenPoints) -> Arc {
let ArcBetweenPoints {
basis: proj,
start,
end,
angle,
stroke_rgba,
stroke_width,
} = value;
let radius = (start.distance(end) / 2.0) / (angle / 2.0).sin();
Arc {
basis: proj,
angle,
radius,
center: DVec3::ZERO,
stroke_rgba,
stroke_width,
}
.with(|arc| {
let cur_start = arc.start();
let v1 = arc.end() - arc.start();
let v2 = end - start;
let rot_angle = v1.angle_between(v2);
let mut rot_axis = v1.cross(v2);
if rot_axis.length_squared() <= f64::EPSILON {
rot_axis = DVec3::NEG_Z;
}
rot_axis = rot_axis.normalize();
arc.shift(start - cur_start);
arc.shift(-start);
arc.rotate_on_axis(rot_axis, rot_angle);
arc.shift(start);
})
}
}
impl From<ArcBetweenPoints> for VItem {
fn from(value: ArcBetweenPoints) -> Self {
Arc::from(value).into()
}
}
impl Extract for ArcBetweenPoints {
type Target = CoreItem;
fn extract_into(&self, buf: &mut Vec<Self::Target>) {
Arc::from(self.clone()).extract_into(buf);
}
}
#[cfg(test)]
mod tests {
use std::f64::consts::PI;
use assert_float_eq::assert_float_absolute_eq;
use glam::dvec3;
use ranim_core::traits::ShiftTransformExt;
use crate::vitem::geometry::anchor::Origin;
use super::*;
#[test]
fn test_arc() {
let arc = Arc::new(PI / 2.0, 2.0);
assert_float_absolute_eq!(
arc.start().distance_squared(dvec3(2.0, 0.0, 0.0)),
0.0,
1e-10
);
assert_float_absolute_eq!(arc.end().distance_squared(dvec3(0.0, 2.0, 0.0)), 0.0, 1e-10);
let arc_between_points =
ArcBetweenPoints::new(dvec3(2.0, 0.0, 0.0), dvec3(0.0, 2.0, 0.0), PI / 2.0);
let arc_between_points = Arc::from(arc_between_points);
assert_float_absolute_eq!(
arc.center.distance_squared(arc_between_points.center),
0.0,
1e-10
);
assert_float_absolute_eq!(arc.radius - arc_between_points.radius, 0.0, 1e-10);
assert_float_absolute_eq!(arc.angle - arc_between_points.angle, 0.0, 1e-10);
let arc_between_points =
ArcBetweenPoints::new(dvec3(0.0, 2.0, 0.0), dvec3(2.0, 0.0, 0.0), PI / 2.0);
let arc_between_points = Arc::from(arc_between_points);
let arc = Arc::new(PI / 2.0, 2.0).with(|arc| {
arc.with_origin(Origin, |x| {
x.rotate_on_axis(DVec3::NEG_Z, PI);
})
.shift(dvec3(2.0, 2.0, 0.0));
});
assert_float_absolute_eq!(
arc.center.distance_squared(arc_between_points.center),
0.0,
1e-10
);
assert_float_absolute_eq!(arc.radius - arc_between_points.radius, 0.0, 1e-10);
assert_float_absolute_eq!(arc.angle - arc_between_points.angle, 0.0, 1e-10);
}
}