Partially encapsulate primitives by adding constructors

This commit is contained in:
Mikolaj Wielgus 2026-06-07 23:53:22 +02:00
parent b134f7eef2
commit e92ee1d697
5 changed files with 58 additions and 78 deletions

View File

@ -60,15 +60,11 @@ impl Layout {
}
pub fn insert_seg(&mut self, spec: SegSpec) -> SegId {
self.insert_seg_raw(Seg {
spec,
endpoints: [
self.joint(spec.endjoints[0]).spec.position,
self.joint(spec.endjoints[1]).spec.position,
],
layer: self.joint(spec.endjoints[0]).spec.layer,
net: self.joint(spec.endjoints[0]).spec.net,
})
let endjoints = [
&self.joints[spec.endjoints[0].index()],
&self.joints[spec.endjoints[1].index()],
];
self.insert_seg_raw(Seg::new(spec, endjoints))
}
pub fn insert_seg_raw(&mut self, seg: Seg) -> SegId {
@ -104,15 +100,11 @@ impl Layout {
}
pub fn insert_via(&mut self, spec: ViaSpec) -> ViaId {
let joints = [self.joint(spec.endjoints[0]), self.joint(spec.endjoints[1])];
self.insert_via_raw(Via {
spec,
position: joints[0].spec.position,
min_layer: std::cmp::min(joints[0].spec.layer, joints[1].spec.layer),
max_layer: std::cmp::max(joints[0].spec.layer, joints[1].spec.layer),
net: joints[0].spec.net,
})
let endjoints = [
&self.joints[spec.endjoints[0].index()],
&self.joints[spec.endjoints[1].index()],
];
self.insert_via_raw(Via::new(spec, endjoints))
}
pub fn insert_via_raw(&mut self, via: Via) -> ViaId {

View File

@ -7,7 +7,7 @@ use rstar::primitives::GeomWithData;
use crate::layout::{
Layout,
primitives::{
Joint, JointId, JointSpec, Poly, PolyId, SegId, SegSpec, ViaId, ViaSpec,
Joint, JointId, JointSpec, Poly, PolyId, Seg, SegId, SegSpec, Via, ViaId, ViaSpec,
},
};
@ -69,14 +69,13 @@ impl Layout {
.remove(&GeomWithData::new(old_seg.bbox().rtree_rectangle(), id));
let endjoint_ids = old_seg.spec.endjoints;
let endjoint_specs = [
self.joints[endjoint_ids[0].index()].spec,
self.joints[endjoint_ids[1].index()].spec,
let endjoints = [
&self.joints[endjoint_ids[0].index()],
&self.joints[endjoint_ids[1].index()],
];
let spec = old_seg.spec;
self.segs.modify(id.index(), |seg| {
seg.endpoints = [endjoint_specs[0].position, endjoint_specs[1].position];
seg.layer = endjoint_specs[0].layer;
seg.net = endjoint_specs[0].net;
*seg = Seg::new(spec, endjoints);
});
let new_seg = &self.segs[id.index()];
@ -107,15 +106,13 @@ impl Layout {
.remove(&GeomWithData::new(old_via.bbox().rtree_rectangle(), id));
let endjoint_ids = old_via.spec.endjoints;
let endjoint_specs = [
self.joints[endjoint_ids[0].index()].spec,
self.joints[endjoint_ids[1].index()].spec,
let endjoints = [
&self.joints[endjoint_ids[0].index()],
&self.joints[endjoint_ids[1].index()],
];
let spec = old_via.spec;
self.vias.modify(id.index(), |via| {
via.position = endjoint_specs[0].position;
via.min_layer = std::cmp::min(endjoint_specs[0].layer, endjoint_specs[1].layer);
via.max_layer = std::cmp::max(endjoint_specs[0].layer, endjoint_specs[1].layer);
via.net = endjoint_specs[0].net;
*via = Via::new(spec, endjoints);
});
let new_via = &self.vias[id.index()];

View File

@ -9,7 +9,7 @@ use crate::layout::compounds::{ComponentId, NetId, PinId};
use crate::vector::Vector2;
use crate::{Rect3, Vector3, layout::LayerId};
use super::JointId;
use super::{Joint, JointId};
#[derive(
Clone,
@ -53,6 +53,16 @@ pub struct Via {
}
impl Via {
pub fn new(spec: ViaSpec, endjoints: [&Joint; 2]) -> Self {
Self {
spec,
position: endjoints[0].spec.position,
min_layer: std::cmp::min(endjoints[0].spec.layer, endjoints[1].spec.layer),
max_layer: std::cmp::max(endjoints[0].spec.layer, endjoints[1].spec.layer),
net: endjoints[0].spec.net,
}
}
pub fn contains_point2(&self, point: Vector2<i64>) -> bool {
(point.x - self.position.x).pow(2) as u64 + (point.y - self.position.y).pow(2) as u64
<= self.spec.radius.pow(2)

View File

@ -245,11 +245,7 @@ impl NavmesherBoard {
pub fn insert_joint(&mut self, spec: JointSpec) -> JointId {
let layer = spec.layer;
let obstacle = Self::joint_bounding_octagon(&Joint {
spec,
segs: Vec::new(),
vias: Vec::new(),
});
let obstacle = Self::joint_bounding_octagon(&Joint::new(spec));
let joint_id = self.board.insert_joint(spec);
self.joint_multiobstacles.insert(
joint_id.index(),

View File

@ -12,7 +12,7 @@ use specctra::{
use crate::{
board::{Board, LayerDesc, LayerSide, LayerType},
layout::primitives::{JointSpec, Poly, PolySpec, Seg, SegSpec},
layout::primitives::{JointSpec, Poly, PolySpec, SegSpec},
layout::{
LayerId,
compounds::{ComponentId, NetId, PinId, PinSpec},
@ -383,23 +383,18 @@ impl Board {
flip: bool,
coordinate_scale: f64,
) {
board.insert_poly({
let spec = PolySpec {
vertices: vec![
Self::pos(place, pin_pos, x1, y1, flip, coordinate_scale),
Self::pos(place, pin_pos, x2, y1, flip, coordinate_scale),
Self::pos(place, pin_pos, x2, y2, flip, coordinate_scale),
Self::pos(place, pin_pos, x1, y2, flip, coordinate_scale),
],
layer,
net,
component,
pin,
};
let centroid = Vector2::<i64>::poly_centroid(&spec.vertices);
Poly { spec, centroid }
});
board.insert_poly(Poly::new(PolySpec {
vertices: vec![
Self::pos(place, pin_pos, x1, y1, flip, coordinate_scale),
Self::pos(place, pin_pos, x2, y1, flip, coordinate_scale),
Self::pos(place, pin_pos, x2, y2, flip, coordinate_scale),
Self::pos(place, pin_pos, x1, y2, flip, coordinate_scale),
],
layer,
net,
component,
pin,
}));
}
fn place_path(
@ -448,16 +443,11 @@ impl Board {
pin,
});
let _ = board.insert_seg_raw(Seg {
spec: SegSpec {
endjoints: [prev_joint, joint],
half_width: Self::scale_size(width / 2.0, coordinate_scale),
component,
pin,
},
endpoints: [prev_pos, pos],
layer,
net,
let _ = board.insert_seg(SegSpec {
endjoints: [prev_joint, joint],
half_width: Self::scale_size(width / 2.0, coordinate_scale),
component,
pin,
});
prev_pos = pos;
@ -482,18 +472,13 @@ impl Board {
.iter()
.map(|coord| Self::pos(place, pin_pos, coord.x, coord.y, flip, coordinate_scale))
.collect();
board.insert_poly({
let spec = PolySpec {
vertices,
layer,
net,
component,
pin,
};
let centroid = Vector2::<i64>::poly_centroid(&spec.vertices);
Poly { spec, centroid }
});
board.insert_poly(Poly::new(PolySpec {
vertices,
layer,
net,
component,
pin,
}));
}
fn layer(_board: &Board, layers: &[Layer], name: &str, front: bool) -> LayerId {