Skip to content

Commit

Permalink
add perimeter and v3 : magnitude -> mag
Browse files Browse the repository at this point in the history
  • Loading branch information
Uriopass committed Jan 11, 2023
1 parent dc1ed8b commit df05f21
Show file tree
Hide file tree
Showing 12 changed files with 49 additions and 46 deletions.
12 changes: 4 additions & 8 deletions egregoria/src/engine_interaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,9 @@ pub enum WorldCommand {
size: u32,
spacing: f32,
},
MoveZonePoint {
UpdateZone {
building: BuildingID,
i: usize,
pos: Vec2,
zone: Polygon,
},
ResetSave,
SetGameTime(GameTime),
Expand Down Expand Up @@ -317,13 +316,10 @@ impl WorldCommand {
.resources
.insert::<EgregoriaOptions>(EgregoriaOptions::clone(opts));
}
MoveZonePoint { building, i, pos } => {
UpdateZone { building, ref zone } => {
let mut map = goria.map_mut();

map.update_zone(building, |z| {
let Some(p) = z.0.get_mut(i) else { return; };
*p = pos;
});
map.update_zone(building, move |z| *z = zone.clone());
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion egregoria/src/map/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ impl Map {
Some((to, r))
}

pub fn update_zone(&mut self, id: BuildingID, f: impl Fn(&mut Polygon)) {
pub fn update_zone(&mut self, id: BuildingID, f: impl FnOnce(&mut Polygon)) {
info!("update_zone {:?}", id);

let Some(b) = self.buildings.get_mut(id) else { return; };
Expand Down
2 changes: 1 addition & 1 deletion egregoria/src/map/objects/turn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ impl Turn {
let ang = src_dir.angle(dst_dir);

let dist =
(pos_dst - pos_src).magnitude() * (TURN_ANG_ADD + ang.abs() * TURN_ANG_MUL) * TURN_MUL;
(pos_dst - pos_src).magn() * (TURN_ANG_ADD + ang.abs() * TURN_ANG_MUL) * TURN_MUL;

let derivative_src = src_dir * dist;
let derivative_dst = dst_dir * dist;
Expand Down
2 changes: 1 addition & 1 deletion egregoria/src/transportation/train.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ pub fn locomotive_decision(
.get_point()
.and_then(|x| {
let d = x - trans.position;
if d.magnitude2() < 0.5 {
if d.mag2() < 0.5 {
return None;
}
d.try_normalize()
Expand Down
2 changes: 1 addition & 1 deletion geom/src/line3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ impl Line3 {

let proj1 = diff2.dot(r);

let d = proj1 / r.magnitude2();
let d = proj1 / r.mag2();
self.src + r * d
}

Expand Down
13 changes: 13 additions & 0 deletions geom/src/polygon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,19 @@ impl Polygon {
s.abs() * 0.5
}

pub fn perimeter(&self) -> f32 {
let mut s = 0.0;
for i in 0..self.0.len() {
let src = unsafe { self.0.get_unchecked(i) };
let dst = unsafe {
self.0
.get_unchecked(if i + 1 == self.0.len() { 0 } else { i + 1 })
};
s += (dst - src).mag();
}
s
}

#[inline]
/// Checks if the polygon contains a point.
/// Assumes a bbox check has already been performed.
Expand Down
10 changes: 5 additions & 5 deletions geom/src/polyline3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,19 +88,19 @@ impl PolyLine3 {
None => unsafe { unreachable_unchecked() },
};
self.check_empty();
self.l -= (v - self.last()).magnitude();
self.l -= (v - self.last()).magn();
v
}

pub fn push(&mut self, item: Vec3) {
self.l += (self.last() - item).magnitude();
self.l += (self.last() - item).magn();
self.points.push(item);
}

pub fn pop_first(&mut self) -> Vec3 {
let v = self.points.remove(0);
self.check_empty();
self.l -= (self.first() - v).magnitude();
self.l -= (self.first() - v).magn();
v
}

Expand Down Expand Up @@ -286,7 +286,7 @@ impl PolyLine3 {
[x] => Self::new(vec![x]),
[f, l] => {
let v = l - f;
let m = v.magnitude();
let m = v.magn();
dst = dst.min(m);

Self::new(vec![f + v * (dst / m), l])
Expand Down Expand Up @@ -449,5 +449,5 @@ impl<'a> PointsAlongs<'a> {
}

fn length(v: &[Vec3]) -> f32 {
v.windows(2).map(|x| (x[1] - x[0]).magnitude()).sum()
v.windows(2).map(|x| (x[1] - x[0]).magn()).sum()
}
4 changes: 2 additions & 2 deletions geom/src/segment3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl Segment3 {
} else if proj2 <= 0.0 {
self.dst
} else {
let lol = proj1 / diff.magnitude2();
let lol = proj1 / diff.mag2();
self.src + diff * lol
}
}
Expand Down Expand Up @@ -54,7 +54,7 @@ impl Segment3 {
}

pub fn scale(&mut self, scale: f32) -> &mut Self {
self.resize(self.vec().magnitude() * scale)
self.resize(self.vec().magn() * scale)
}

pub fn vec(&self) -> Vec3 {
Expand Down
22 changes: 11 additions & 11 deletions geom/src/v3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -416,12 +416,12 @@ impl Vec3 {
}

#[inline]
pub fn magnitude(self) -> f32 {
self.magnitude2().sqrt()
pub fn magn(self) -> f32 {
self.mag2().sqrt()
}

#[inline]
pub fn magnitude2(self) -> f32 {
pub fn mag2(self) -> f32 {
self.dot(self)
}

Expand Down Expand Up @@ -500,12 +500,12 @@ impl Vec3 {

#[inline]
pub fn distance2(self, rhs: Self) -> f32 {
(self - rhs).magnitude2()
(self - rhs).mag2()
}

#[inline]
pub fn distance(self, rhs: Self) -> f32 {
(self - rhs).magnitude()
(self - rhs).magn()
}

#[inline]
Expand All @@ -515,7 +515,7 @@ impl Vec3 {

#[inline]
pub fn try_normalize(self) -> Option<Vec3> {
let m = self.magnitude();
let m = self.magn();
if m > 0.0 {
Some(self / m)
} else {
Expand All @@ -525,13 +525,13 @@ impl Vec3 {

#[inline]
pub fn normalize(self) -> Vec3 {
let m = self.magnitude();
let m = self.magn();
self / m
}

#[inline]
pub fn try_normalize_to(self, v: f32) -> Option<Vec3> {
let m = self.magnitude();
let m = self.magn();
if m > 0.0 {
Some(self * (v / m))
} else {
Expand All @@ -541,13 +541,13 @@ impl Vec3 {

#[inline]
pub fn normalize_to(self, v: f32) -> Vec3 {
let m = self.magnitude();
let m = self.magn();
self * (v / m)
}

#[inline]
pub fn dir_dist(self) -> Option<(Vec3, f32)> {
let m = self.magnitude();
let m = self.magn();
if m > 0.0 {
Some((self / m, m))
} else {
Expand Down Expand Up @@ -575,7 +575,7 @@ impl Vec3 {

#[inline]
pub fn cap_magnitude(self, max: f32) -> Vec3 {
let m = self.magnitude();
let m = self.magn();
if m > max {
self * max / m
} else {
Expand Down
21 changes: 8 additions & 13 deletions native_app/src/gui/zoneedit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,12 @@ pub(crate) fn zoneedit(goria: &Egregoria, uiworld: &mut UiWorld) {
.collect();

let area = newpoly.area();
let perimeter = newpoly.perimeter();

let mut invalidmsg = format!("");

let bid = comp.building;
const MAX_ZONE_AREA: f32 = 75000.0;
const MAX_ZONE_AREA: f32 = 100000.0;
if area > MAX_ZONE_AREA {
invalidmsg = format!("Area too big ({} > {MAX_ZONE_AREA})", area);
} else {
Expand Down Expand Up @@ -105,22 +106,16 @@ pub(crate) fn zoneedit(goria: &Egregoria, uiworld: &mut UiWorld) {
draw.circle(p.z(1.0), 5.0).color(base_col);
}

if let Some(offset) = state.offset {
if state.offset.is_some() {
if inp.act.contains(&InputAction::Select) {
inspected.dontclear = true;
}
if !inp.act.contains(&InputAction::Select) {
if let Some(unproj) = inp.unprojected {
if isvalid {
let unproj = unproj.xy();
let newpos = unproj - offset;

commands.push(WorldCommand::MoveZonePoint {
building: comp.building,
i: state.i,
pos: newpos,
});
}
if isvalid {
commands.push(WorldCommand::UpdateZone {
building: comp.building,
zone: newpoly,
});
}
state.offset = None;
}
Expand Down
3 changes: 1 addition & 2 deletions native_app/src/rendering/camera_handler_3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,7 @@ impl CameraHandler3D {
};
}

lerpp!(self.camera.pos, self.targetpos, 8.0, |v: Vec3| v
.magnitude2());
lerpp!(self.camera.pos, self.targetpos, 8.0, |v: Vec3| v.mag2());
lerpp!(self.camera.yaw, self.targetyaw, 16.0, |x: Radians| x
.0
.abs());
Expand Down
2 changes: 1 addition & 1 deletion native_app/src/rendering/map_mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ impl MapBuilders {
let from = lanes[id.src].get_inter_node_pos(inter_id).up(0.01);
let to = lanes[id.dst].get_inter_node_pos(inter_id).up(0.01);

let l = (to - from).magnitude();
let l = (to - from).magn();

if l < walking_w {
continue;
Expand Down

0 comments on commit df05f21

Please sign in to comment.