Skip to content

Commit

Permalink
max steepness as calculated by point generation is now used to check …
Browse files Browse the repository at this point in the history
…when building
  • Loading branch information
Uriopass committed Jan 3, 2024
1 parent 5868dba commit d9cddc5
Show file tree
Hide file tree
Showing 9 changed files with 380 additions and 151 deletions.
3 changes: 2 additions & 1 deletion geom/src/boldspline.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::Vec2;
use super::{Vec2, Vec3};
use crate::aabb::AABB;
use crate::{vec2, BoldLine, Circle, Intersect, Polygon, Segment, Shape, Spline, OBB};
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -82,6 +82,7 @@ impl Intersect<Polygon> for BoldSpline {
}
}

pub static mut DEBUG_POS: Vec<Vec3> = Vec::new();
pub static mut DEBUG_OBBS: Vec<OBB> = Vec::new();
pub static mut DEBUG_SPLINES: Vec<Spline> = Vec::new();

Expand Down
6 changes: 4 additions & 2 deletions geom/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ mod segment;
mod segment3;
pub mod skeleton;
mod sphere;
mod spline;
mod spline1;
mod spline3;
mod splines;
mod transform;
mod v2;
mod v3;
Expand Down Expand Up @@ -85,8 +86,9 @@ pub use ray3::*;
pub use segment::*;
pub use segment3::*;
pub use sphere::*;
pub use spline::*;
pub use spline1::*;
pub use spline3::*;
pub use splines::*;
pub use transform::*;
pub use v2::*;
pub use v3::*;
Expand Down
File renamed without changes.
178 changes: 178 additions & 0 deletions geom/src/spline1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
use serde::{Deserialize, Serialize};

#[derive(Copy, Clone, Debug, Serialize, Deserialize)]
pub struct Spline1 {
pub from: f32,
pub to: f32,
pub from_derivative: f32,
pub to_derivative: f32,
}

impl Default for Spline1 {
fn default() -> Self {
Self {
from: 0.0,
to: 0.0,
from_derivative: 0.0,
to_derivative: 0.0,
}
}
}

impl Spline1 {
#[inline]
pub fn get(&self, t: f32) -> f32 {
(1.0 - t).powi(3) * self.from
+ 3.0 * t * (1.0 - t).powi(2) * (self.from + self.from_derivative)
+ 3.0 * t.powi(2) * (1.0 - t) * (self.to - self.to_derivative)
+ t.powi(3) * self.to
}

#[inline]
pub fn derivative(&self, t: f32) -> f32 {
-3.0 * (t - 1.0).powi(2) * self.from
+ 3.0 * (t - 1.0) * (3.0 * t - 1.0) * (self.from + self.from_derivative)
+ 3.0 * t * (2.0 - 3.0 * t) * (self.to - self.to_derivative)
+ 3.0 * t.powi(2) * self.to
}

#[inline]
pub fn derivative_2(&self, t: f32) -> f32 {
6.0 * (1.0 - t) * self.from
+ (18.0 * t - 12.0) * (self.from + self.from_derivative)
+ (6.0 - 18.0 * t) * (self.to - self.to_derivative)
+ 6.0 * t * self.to
}

#[allow(non_snake_case)]
pub fn split_at(&self, t: f32) -> (Spline1, Spline1) {
// https://upload.wikimedia.org/wikipedia/commons/1/11/Bezier_rec.png
let mid = self.get(t);
let H = (self.to - self.to_derivative) * t + (self.from + self.from_derivative) * (1.0 - t);

let L2 = self.from + self.from_derivative * t;
let L3 = L2 + (H - L2) * t;

let from_spline = Spline1 {
from: self.from,
to: mid,
from_derivative: L2 - self.from,
to_derivative: mid - L3,
};

let R3 = self.to - self.to_derivative * (1.0 - t);
let R2 = R3 + (H - R3) * (1.0 - t);

let to_spline = Spline1 {
from: mid,
to: self.to,
from_derivative: R2 - mid,
to_derivative: self.to - R3,
};

(from_spline, to_spline)
}

pub fn smart_points(
&self,
detail: f32,
start: f32,
end: f32,
) -> impl Iterator<Item = f32> + '_ {
self.smart_points_t(detail, start, end)
.map(move |t| self.get(t))
}

pub fn smart_points_t(
&self,
detail: f32,
start: f32,
end: f32,
) -> impl Iterator<Item = f32> + '_ {
let detail = detail.abs();

std::iter::once(start)
.chain(SmartPoints1 {
spline: self,
t: start,
end,
detail,
})
.chain(std::iter::once(end))
}

pub fn into_smart_points_t(
self,
detail: f32,
start: f32,
end: f32,
) -> impl Iterator<Item = f32> {
let detail = detail.abs();

std::iter::once(start)
.chain(OwnedSmartPoints1 {
spline: self,
t: start,
end,
detail,
})
.chain(std::iter::once(end))
}

pub fn into_smart_points(self, detail: f32, start: f32, end: f32) -> impl Iterator<Item = f32> {
self.into_smart_points_t(detail, start, end)
.map(move |t| self.get(t))
}

pub fn points(&self, n: usize) -> impl Iterator<Item = f32> + '_ {
(0..n).map(move |i| {
let c = i as f32 / (n - 1) as f32;

self.get(c)
})
}

#[inline]
fn step(&self, t: f32, detail: f32) -> f32 {
let dot = self.derivative(t).abs();
(detail / dot).min(0.15)
}
}

pub struct SmartPoints1<'a> {
spline: &'a Spline1,
t: f32,
end: f32,
detail: f32,
}

impl<'a> Iterator for SmartPoints1<'a> {
type Item = f32;

fn next(&mut self) -> Option<Self::Item> {
self.t += self.spline.step(self.t, self.detail);
if self.t > self.end {
return None;
}
Some(self.t)
}
}

pub struct OwnedSmartPoints1 {
spline: Spline1,
t: f32,
end: f32,
detail: f32,
}

impl Iterator for OwnedSmartPoints1 {
type Item = f32;

fn next(&mut self) -> Option<Self::Item> {
self.t += self.spline.step(self.t, self.detail);
if self.t > self.end {
return None;
}
Some(self.t)
}
}
6 changes: 5 additions & 1 deletion native_app/src/game_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,9 @@ impl State {
let mut col = LinearColor::WHITE;
col.a = 0.2;
unsafe {
for v in &geom::DEBUG_POS {
immediate.circle(*v, 1.0).color(LinearColor::RED);
}
for v in &geom::DEBUG_OBBS {
immediate
.obb(*v, map.environment.height(v.center()).unwrap_or(0.0) + 8.0)
Expand All @@ -335,8 +338,9 @@ impl State {
)
.color(col);
}
//geom::DEBUG_OBBS.clear();
geom::DEBUG_OBBS.clear();
geom::DEBUG_SPLINES.clear();
geom::DEBUG_POS.clear();
}

immediate.apply(&mut self.immtess, ctx);
Expand Down
Loading

0 comments on commit d9cddc5

Please sign in to comment.