use std::f64::consts::{PI, TAU};
use ranim_core::{
Extract,
anchor::{Aabb, AabbPoint, Locate},
color,
core_item::CoreItem,
glam::{DVec2, DVec3, dvec2, dvec3},
traits::{Discard, RotateTransform, ScaleTransform, ShiftTransform, ShiftTransformExt},
};
use color::{AlphaColor, Srgb};
use itertools::Itertools;
use crate::vitem::{DEFAULT_STROKE_WIDTH, VItem, geometry::Circle};
use ranim_core::core_item::vitem::Basis2d;
use ranim_core::traits::{Alignable, FillColor, Opacity, StrokeColor, StrokeWidth, With};
#[derive(Clone, Debug, ranim_macros::Interpolatable)]
pub struct Square {
pub basis: Basis2d,
pub center: DVec3,
pub size: f64,
pub stroke_rgba: AlphaColor<Srgb>,
pub stroke_width: f32,
pub fill_rgba: AlphaColor<Srgb>,
}
impl Square {
pub fn new(size: f64) -> Self {
Self {
basis: Basis2d::default(),
center: dvec3(0.0, 0.0, 0.0),
size,
stroke_rgba: AlphaColor::WHITE,
stroke_width: DEFAULT_STROKE_WIDTH,
fill_rgba: AlphaColor::TRANSPARENT,
}
}
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 anchor = anchor.locate(self);
self.size *= scale;
self.center
.shift(-anchor)
.scale(DVec3::splat(scale))
.shift(anchor);
self
}
}
impl Aabb for Square {
fn aabb(&self) -> [DVec3; 2] {
let (u, v) = self.basis.uv();
[
self.center + self.size / 2.0 * (u + v),
self.center - self.size / 2.0 * (u + v),
]
.aabb()
}
}
impl ShiftTransform for Square {
fn shift(&mut self, shift: DVec3) -> &mut Self {
self.center.shift(shift);
self
}
}
impl RotateTransform for Square {
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 Alignable for Square {
fn is_aligned(&self, _other: &Self) -> bool {
true
}
fn align_with(&mut self, _other: &mut Self) {}
}
impl Opacity for Square {
fn set_opacity(&mut self, opacity: f32) -> &mut Self {
self.stroke_rgba = self.stroke_rgba.with_alpha(opacity);
self.fill_rgba = self.fill_rgba.with_alpha(opacity);
self
}
}
impl StrokeColor for Square {
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 FillColor for Square {
fn fill_color(&self) -> AlphaColor<Srgb> {
self.fill_rgba
}
fn set_fill_color(&mut self, color: AlphaColor<Srgb>) -> &mut Self {
self.fill_rgba = color;
self
}
fn set_fill_opacity(&mut self, opacity: f32) -> &mut Self {
self.fill_rgba = self.fill_rgba.with_alpha(opacity);
self
}
}
impl Extract for Square {
type Target = CoreItem;
fn extract_into(&self, buf: &mut Vec<Self::Target>) {
VItem::from(self.clone()).extract_into(buf);
}
}
impl From<Square> for Rectangle {
fn from(value: Square) -> Self {
let Square {
basis,
center,
size: width,
stroke_rgba,
stroke_width,
fill_rgba,
} = value;
let (u, v) = basis.uv();
let p0 = center - width / 2.0 * u - width / 2.0 * v;
Rectangle {
basis,
p0,
size: dvec2(width, width),
stroke_rgba,
stroke_width,
fill_rgba,
}
}
}
impl From<Square> for RegularPolygon {
fn from(value: Square) -> Self {
RegularPolygon::new(4, value.size / 2.0 * 2.0f64.sqrt()).with(|x| {
x.basis = value.basis;
x.stroke_rgba = value.stroke_rgba;
x.stroke_width = value.stroke_width;
x.fill_rgba = value.fill_rgba;
})
}
}
impl From<Square> for Polygon {
fn from(value: Square) -> Self {
Rectangle::from(value).into()
}
}
impl From<Square> for VItem {
fn from(value: Square) -> Self {
Rectangle::from(value).into()
}
}
#[derive(Clone, Debug, ranim_macros::Interpolatable)]
pub struct Rectangle {
pub basis: Basis2d,
pub p0: DVec3,
pub size: DVec2,
pub stroke_rgba: AlphaColor<Srgb>,
pub stroke_width: f32,
pub fill_rgba: AlphaColor<Srgb>,
}
impl Rectangle {
pub fn new(width: f64, height: f64) -> Self {
let half_width = width / 2.0;
let half_height = height / 2.0;
let p0 = dvec3(-half_width, -half_height, 0.0);
let size = dvec2(width, height);
Self::from_min_size(p0, size)
}
pub fn from_min_size(p0: DVec3, size: DVec2) -> Self {
Self {
basis: Basis2d::default(),
p0,
size,
stroke_rgba: AlphaColor::WHITE,
stroke_width: DEFAULT_STROKE_WIDTH,
fill_rgba: AlphaColor::TRANSPARENT,
}
}
pub fn width(&self) -> f64 {
self.size.x.abs()
}
pub fn height(&self) -> f64 {
self.size.y.abs()
}
}
impl Aabb for Rectangle {
fn aabb(&self) -> [DVec3; 2] {
let (u, v) = self.basis.uv();
let p1 = self.p0;
let p2 = self.p0 + self.size.x * u + self.size.y * v;
[p1, p2].aabb()
}
}
impl ShiftTransform for Rectangle {
fn shift(&mut self, shift: DVec3) -> &mut Self {
self.p0.shift(shift);
self
}
}
impl RotateTransform for Rectangle {
fn rotate_on_axis(&mut self, axis: DVec3, angle: f64) -> &mut Self {
self.p0.rotate_on_axis(axis, angle);
self.basis.rotate_on_axis(axis, angle);
self
}
}
impl ScaleTransform for Rectangle {
fn scale(&mut self, scale: DVec3) -> &mut Self {
self.p0.scale(scale);
let (u, v) = self.basis.uv();
let scale_u = scale.dot(u);
let scale_v = scale.dot(v);
self.size *= dvec2(scale_u, scale_v);
self
}
}
impl Opacity for Rectangle {
fn set_opacity(&mut self, opacity: f32) -> &mut Self {
self.stroke_rgba = self.stroke_rgba.with_alpha(opacity);
self.fill_rgba = self.fill_rgba.with_alpha(opacity);
self
}
}
impl Alignable for Rectangle {
fn align_with(&mut self, _other: &mut Self) {}
fn is_aligned(&self, _other: &Self) -> bool {
true
}
}
impl StrokeColor for Rectangle {
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 FillColor for Rectangle {
fn fill_color(&self) -> AlphaColor<Srgb> {
self.fill_rgba
}
fn set_fill_color(&mut self, color: AlphaColor<Srgb>) -> &mut Self {
self.fill_rgba = color;
self
}
fn set_fill_opacity(&mut self, opacity: f32) -> &mut Self {
self.fill_rgba = self.fill_rgba.with_alpha(opacity);
self
}
}
impl From<Rectangle> for Polygon {
fn from(value: Rectangle) -> Self {
let p0 = value.p0;
let (u, v) = value.basis.uv();
let DVec2 { x: w, y: h } = value.size;
let points = vec![p0, p0 + u * w, p0 + u * w + v * h, p0 + v * h];
Polygon {
basis: value.basis,
points,
stroke_rgba: value.stroke_rgba,
stroke_width: value.stroke_width,
fill_rgba: value.fill_rgba,
}
}
}
impl From<Rectangle> for VItem {
fn from(value: Rectangle) -> Self {
Polygon::from(value).into()
}
}
impl Extract for Rectangle {
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 Polygon {
pub basis: Basis2d,
pub points: Vec<DVec3>,
pub stroke_rgba: AlphaColor<Srgb>,
pub stroke_width: f32,
pub fill_rgba: AlphaColor<Srgb>,
}
impl Polygon {
pub fn new(points: Vec<DVec3>) -> Self {
Self {
basis: Basis2d::default(),
points,
stroke_rgba: AlphaColor::WHITE,
stroke_width: DEFAULT_STROKE_WIDTH,
fill_rgba: AlphaColor::TRANSPARENT,
}
}
}
impl Aabb for Polygon {
fn aabb(&self) -> [DVec3; 2] {
self.points.aabb()
}
}
impl ShiftTransform for Polygon {
fn shift(&mut self, shift: DVec3) -> &mut Self {
self.points.shift(shift);
self
}
}
impl RotateTransform for Polygon {
fn rotate_on_axis(&mut self, axis: DVec3, angle: f64) -> &mut Self {
self.points.rotate_on_axis(axis, angle);
self.basis.rotate_on_axis(axis, angle);
self
}
}
impl ScaleTransform for Polygon {
fn scale(&mut self, scale: DVec3) -> &mut Self {
self.points.scale(scale);
self
}
}
impl Alignable for Polygon {
fn is_aligned(&self, other: &Self) -> bool {
self.points.len() == other.points.len()
}
fn align_with(&mut self, other: &mut Self) {
if self.points.len() > other.points.len() {
return other.align_with(self);
}
self.points
.resize(other.points.len(), self.points.last().cloned().unwrap());
}
}
impl Opacity for Polygon {
fn set_opacity(&mut self, opacity: f32) -> &mut Self {
self.fill_rgba = self.fill_rgba.with_alpha(opacity);
self.stroke_rgba = self.stroke_rgba.with_alpha(opacity);
self
}
}
impl StrokeColor for Polygon {
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 FillColor for Polygon {
fn fill_color(&self) -> AlphaColor<Srgb> {
self.fill_rgba
}
fn set_fill_color(&mut self, color: AlphaColor<Srgb>) -> &mut Self {
self.fill_rgba = color;
self
}
fn set_fill_opacity(&mut self, opacity: f32) -> &mut Self {
self.fill_rgba = self.fill_rgba.with_alpha(opacity);
self
}
}
impl From<Polygon> for VItem {
fn from(value: Polygon) -> Self {
let Polygon {
mut points,
stroke_rgba,
stroke_width,
fill_rgba,
basis,
..
} = value;
assert!(points.len() > 2);
points.push(points[0]);
let anchors = points;
let handles = anchors
.iter()
.tuple_windows()
.map(|(&a, &b)| 0.5 * (a + b))
.collect::<Vec<_>>();
let vpoints = anchors.into_iter().interleave(handles).collect::<Vec<_>>();
VItem::from_vpoints(vpoints)
.with_basis(basis)
.with(|vitem| {
vitem
.set_fill_color(fill_rgba)
.set_stroke_color(stroke_rgba)
.set_stroke_width(stroke_width);
})
}
}
impl Extract for Polygon {
type Target = CoreItem;
fn extract_into(&self, buf: &mut Vec<Self::Target>) {
VItem::from(self.clone()).extract_into(buf);
}
}
#[derive(Debug, Clone, ranim_macros::Interpolatable)]
pub struct RegularPolygon {
pub basis: Basis2d,
pub center: DVec3,
pub sides: usize,
pub radius: f64,
pub stroke_rgba: AlphaColor<Srgb>,
pub stroke_width: f32,
pub fill_rgba: AlphaColor<Srgb>,
}
impl Alignable for RegularPolygon {
fn is_aligned(&self, _other: &Self) -> bool {
true
}
fn align_with(&mut self, _other: &mut Self) {}
}
impl RegularPolygon {
pub fn new(sides: usize, radius: f64) -> Self {
assert!(sides >= 3);
Self {
basis: Basis2d::default(),
center: DVec3::ZERO,
sides,
radius,
stroke_rgba: AlphaColor::WHITE,
stroke_width: DEFAULT_STROKE_WIDTH,
fill_rgba: AlphaColor::TRANSPARENT,
}
}
pub fn points(&self) -> Vec<DVec3> {
let &Self {
sides,
radius,
center,
..
} = self;
let u = self.basis.u();
let normal = self.basis.normal();
(0..sides)
.map(|i| TAU * (i as f64 / sides as f64))
.map(|angle| u.rotate_axis(normal, angle) * radius + center)
.collect()
}
pub fn outer_circle(&self) -> Circle {
Circle::new(self.radius).with(|x| x.move_to(self.center).discard())
}
pub fn inner_circle(&self) -> Circle {
Circle::new(self.radius * (PI / self.sides as f64).cos())
.with(|x| x.move_to(self.center).discard())
}
}
impl Aabb for RegularPolygon {
fn aabb(&self) -> [DVec3; 2] {
self.points().aabb()
}
}
impl ShiftTransform for RegularPolygon {
fn shift(&mut self, offset: DVec3) -> &mut Self {
self.center.shift(offset);
self
}
}
impl RotateTransform for RegularPolygon {
fn rotate_on_axis(&mut self, axis: DVec3, angle: f64) -> &mut Self {
self.basis.rotate_on_axis(axis, angle);
self.center.rotate_on_axis(axis, angle);
self
}
}
impl Opacity for RegularPolygon {
fn set_opacity(&mut self, opacity: f32) -> &mut Self {
self.fill_rgba = self.fill_rgba.with_alpha(opacity);
self.stroke_rgba = self.stroke_rgba.with_alpha(opacity);
self
}
}
impl FillColor for RegularPolygon {
fn fill_color(&self) -> AlphaColor<Srgb> {
self.fill_rgba
}
fn set_fill_color(&mut self, color: AlphaColor<Srgb>) -> &mut Self {
self.fill_rgba = color;
self
}
fn set_fill_opacity(&mut self, opacity: f32) -> &mut Self {
self.fill_rgba = self.fill_rgba.with_alpha(opacity);
self
}
}
impl StrokeColor for RegularPolygon {
fn stroke_color(&self) -> AlphaColor<Srgb> {
self.stroke_rgba
}
fn set_stroke_opacity(&mut self, opacity: f32) -> &mut Self {
self.stroke_rgba = self.stroke_rgba.with_alpha(opacity);
self
}
fn set_stroke_color(&mut self, color: AlphaColor<Srgb>) -> &mut Self {
self.stroke_rgba = color;
self
}
}
impl From<RegularPolygon> for Polygon {
fn from(value: RegularPolygon) -> Self {
Polygon::new(value.points()).with(|x| {
x.basis = value.basis;
x.fill_rgba = value.fill_rgba;
x.stroke_rgba = value.stroke_rgba;
x.stroke_width = value.stroke_width;
})
}
}
impl Extract for RegularPolygon {
type Target = CoreItem;
fn extract_into(&self, buf: &mut Vec<Self::Target>) {
Polygon::from(self.clone()).extract_into(buf);
}
}