geometry: rename `Shape*` to `PrimitiveShape*`

This commit is contained in:
Mikolaj Wielgus 2024-04-10 01:34:18 +00:00
parent 2d5998547a
commit 6932a698f6
13 changed files with 74 additions and 70 deletions

View File

@ -7,7 +7,7 @@ use std::{
use topola::{
drawing::{graph::MakePrimitive, primitive::MakeShape, zone::MakePolygon, Drawing},
dsn::{design::DsnDesign, rules::DsnRules},
geometry::shape::{BendShape, DotShape, SegShape, Shape},
geometry::primitive::{BendShape, DotShape, PrimitiveShape, SegShape},
math::Circle,
};

View File

@ -1,6 +1,6 @@
use egui::{emath::RectTransform, epaint, Color32, Pos2, Stroke, Ui};
use geo::{CoordsIter, Point, Polygon};
use topola::geometry::shape::Shape;
use topola::geometry::primitive::PrimitiveShape;
pub struct Painter<'a> {
ui: &'a mut Ui,
@ -12,15 +12,15 @@ impl<'a> Painter<'a> {
Self { ui, transform }
}
pub fn paint_shape(&mut self, shape: &Shape, color: Color32) {
pub fn paint_shape(&mut self, shape: &PrimitiveShape, color: Color32) {
let epaint_shape = match shape {
Shape::Dot(dot) => epaint::Shape::circle_filled(
PrimitiveShape::Dot(dot) => epaint::Shape::circle_filled(
self.transform
.transform_pos([dot.c.pos.x() as f32, -dot.c.pos.y() as f32].into()),
dot.c.r as f32 * self.transform.scale().x,
color,
),
Shape::Seg(seg) => epaint::Shape::line_segment(
PrimitiveShape::Seg(seg) => epaint::Shape::line_segment(
[
self.transform
.transform_pos([seg.from.x() as f32, -seg.from.y() as f32].into()),
@ -29,7 +29,7 @@ impl<'a> Painter<'a> {
],
Stroke::new(seg.width as f32 * self.transform.scale().x, color),
),
Shape::Bend(bend) => {
PrimitiveShape::Bend(bend) => {
let delta_from = bend.from - bend.c.pos;
let delta_to = bend.to - bend.c.pos;

View File

@ -21,7 +21,7 @@ use topola::drawing::seg::FixedSegWeight;
use topola::drawing::zone::MakePolygon;
use topola::drawing::{Drawing, Infringement, LayoutException};
use topola::dsn::design::DsnDesign;
use topola::geometry::shape::{Shape, ShapeTrait};
use topola::geometry::primitive::{PrimitiveShape, PrimitiveShapeTrait};
use topola::layout::connectivity::BandIndex;
use topola::layout::Layout;
use topola::mesh::{Mesh, MeshEdgeReference, VertexIndex};
@ -326,7 +326,7 @@ fn render_times(
maybe_band: Option<BandIndex>,
mut maybe_mesh: Option<Mesh>,
path: &[VertexIndex],
ghosts: &[Shape],
ghosts: &[PrimitiveShape],
highlighteds: &[PrimitiveIndex],
times: i64,
) {

View File

@ -2,7 +2,7 @@ use geo::{CoordsIter, Point, Polygon};
use pathfinder_canvas::{
vec2f, ArcDirection, Canvas, CanvasRenderingContext2D, ColorU, FillRule, Path2D, RectF,
};
use topola::geometry::shape::{Shape, ShapeTrait};
use topola::geometry::primitive::{PrimitiveShape, PrimitiveShapeTrait};
pub struct Painter<'a> {
canvas: &'a mut CanvasRenderingContext2D,
@ -13,12 +13,12 @@ impl<'a> Painter<'a> {
Self { canvas }
}
pub fn paint_shape(&mut self, shape: &Shape, color: ColorU, zoom: f32) {
pub fn paint_shape(&mut self, shape: &PrimitiveShape, color: ColorU, zoom: f32) {
self.canvas.set_stroke_style(color);
self.canvas.set_fill_style(color);
match shape {
Shape::Dot(dot) => {
PrimitiveShape::Dot(dot) => {
let mut path = Path2D::new();
path.ellipse(
vec2f(dot.c.pos.x() as f32, -dot.c.pos.y() as f32),
@ -29,14 +29,14 @@ impl<'a> Painter<'a> {
);
self.canvas.fill_path(path, FillRule::Winding);
}
Shape::Seg(seg) => {
PrimitiveShape::Seg(seg) => {
let mut path = Path2D::new();
path.move_to(vec2f(seg.from.x() as f32, -seg.from.y() as f32));
path.line_to(vec2f(seg.to.x() as f32, -seg.to.y() as f32));
self.canvas.set_line_width(seg.width as f32);
self.canvas.stroke_path(path);
}
Shape::Bend(bend) => {
PrimitiveShape::Bend(bend) => {
let delta1 = bend.from - bend.c.pos;
let delta2 = bend.to - bend.c.pos;
@ -56,7 +56,7 @@ impl<'a> Painter<'a> {
}
}
let envelope = ShapeTrait::envelope(shape, 0.0);
let envelope = PrimitiveShapeTrait::envelope(shape, 0.0);
// XXX: points represented as arrays can't be conveniently converted to vector types
let topleft = vec2f(envelope.lower()[0] as f32, -envelope.lower()[1] as f32);
let bottomright = vec2f(envelope.upper()[0] as f32, -envelope.upper()[1] as f32);

View File

@ -30,7 +30,7 @@ use crate::drawing::{
};
use crate::geometry::Node;
use crate::geometry::{
shape::{Shape, ShapeTrait},
primitive::{PrimitiveShape, PrimitiveShapeTrait},
with_rtree::GeometryWithRtree,
BendWeightTrait, DotWeightTrait, Geometry, GeometryLabel, GetOffset, GetPos, GetWidth,
SegWeightTrait,
@ -58,11 +58,11 @@ pub enum LayoutException {
// TODO add real error messages + these should eventually use Display
#[derive(Error, Debug, Clone, Copy)]
#[error("{0:?} infringes on {1:?}")]
pub struct Infringement(pub Shape, pub PrimitiveIndex);
pub struct Infringement(pub PrimitiveShape, pub PrimitiveIndex);
#[derive(Error, Debug, Clone, Copy)]
#[error("{0:?} collides with {1:?}")]
pub struct Collision(pub Shape, pub PrimitiveIndex);
pub struct Collision(pub PrimitiveShape, pub PrimitiveIndex);
#[derive(Error, Debug, Clone, Copy)]
#[error("{1:?} is already connected to net {0}")]

View File

@ -10,7 +10,7 @@ use crate::{
rules::GetConditions,
Drawing,
},
geometry::shape::{Shape, ShapeTrait},
geometry::primitive::{PrimitiveShape, PrimitiveShapeTrait},
math::{self, Circle, NoTangents},
};
@ -189,7 +189,7 @@ impl<'a, R: RulesTrait> Guide<'a, R> {
fn bend_circle(&self, bend: BendIndex, width: f64, guide_conditions: &Conditions) -> Circle {
let outer_circle = match bend.primitive(self.drawing).shape() {
Shape::Bend(shape) => shape.outer_circle(),
PrimitiveShape::Bend(shape) => shape.outer_circle(),
_ => unreachable!(),
};

View File

@ -2,7 +2,7 @@ use enum_dispatch::enum_dispatch;
use petgraph::stable_graph::NodeIndex;
use crate::geometry::{
shape::{Shape, ShapeTrait},
primitive::{PrimitiveShape, PrimitiveShapeTrait},
GetOffset, GetWidth,
};
use crate::graph::{GenericIndex, GetNodeIndex};
@ -35,7 +35,7 @@ pub trait GetWeight<W> {
#[enum_dispatch]
pub trait MakeShape {
fn shape(&self) -> Shape;
fn shape(&self) -> PrimitiveShape;
}
#[enum_dispatch]
@ -270,7 +270,7 @@ impl<'a, R: RulesTrait> FixedDot<'a, R> {
}
impl<'a, R: RulesTrait> MakeShape for FixedDot<'a, R> {
fn shape(&self) -> Shape {
fn shape(&self) -> PrimitiveShape {
self.drawing.geometry().dot_shape(self.index.into())
}
}
@ -316,7 +316,7 @@ impl<'a, R: RulesTrait> LooseDot<'a, R> {
}
impl<'a, R: RulesTrait> MakeShape for LooseDot<'a, R> {
fn shape(&self) -> Shape {
fn shape(&self) -> PrimitiveShape {
self.drawing.geometry().dot_shape(self.index.into())
}
}
@ -339,7 +339,7 @@ pub type FixedSeg<'a, R> = GenericPrimitive<'a, FixedSegWeight, R>;
impl_fixed_primitive!(FixedSeg, FixedSegWeight);
impl<'a, R: RulesTrait> MakeShape for FixedSeg<'a, R> {
fn shape(&self) -> Shape {
fn shape(&self) -> PrimitiveShape {
self.drawing.geometry().seg_shape(self.index.into())
}
}
@ -362,7 +362,7 @@ pub type LoneLooseSeg<'a, R> = GenericPrimitive<'a, LoneLooseSegWeight, R>;
impl_loose_primitive!(LoneLooseSeg, LoneLooseSegWeight);
impl<'a, R: RulesTrait> MakeShape for LoneLooseSeg<'a, R> {
fn shape(&self) -> Shape {
fn shape(&self) -> PrimitiveShape {
self.drawing.geometry().seg_shape(self.index.into())
}
}
@ -385,7 +385,7 @@ pub type SeqLooseSeg<'a, R> = GenericPrimitive<'a, SeqLooseSegWeight, R>;
impl_loose_primitive!(SeqLooseSeg, SeqLooseSegWeight);
impl<'a, R: RulesTrait> MakeShape for SeqLooseSeg<'a, R> {
fn shape(&self) -> Shape {
fn shape(&self) -> PrimitiveShape {
self.drawing.geometry().seg_shape(self.index.into())
}
}
@ -426,7 +426,7 @@ impl<'a, R: RulesTrait> GetBendIndex for FixedBend<'a, R> {
}
impl<'a, R: RulesTrait> MakeShape for FixedBend<'a, R> {
fn shape(&self) -> Shape {
fn shape(&self) -> PrimitiveShape {
self.drawing.geometry().bend_shape(self.index.into())
}
}
@ -464,7 +464,7 @@ impl<'a, R: RulesTrait> From<LooseBend<'a, R>> for BendIndex {
}
impl<'a, R: RulesTrait> MakeShape for LooseBend<'a, R> {
fn shape(&self) -> Shape {
fn shape(&self) -> PrimitiveShape {
self.drawing.geometry().bend_shape(self.index.into())
}
}

View File

@ -17,7 +17,7 @@ use crate::{
rules::RulesTrait,
seg::{FixedSegWeight, LoneLooseSegWeight, SegWeight, SeqLooseSegWeight},
},
geometry::shape::{BendShape, DotShape, SegShape, Shape},
geometry::primitive::{BendShape, DotShape, PrimitiveShape, SegShape},
graph::{GenericIndex, GetNodeIndex},
math::Circle,
};
@ -234,9 +234,9 @@ impl<
}
}
pub fn dot_shape(&self, dot: DI) -> Shape {
pub fn dot_shape(&self, dot: DI) -> PrimitiveShape {
let weight = self.dot_weight(dot);
Shape::Dot(DotShape {
PrimitiveShape::Dot(DotShape {
c: Circle {
pos: weight.pos(),
r: weight.width() / 2.0,
@ -244,19 +244,19 @@ impl<
})
}
pub fn seg_shape(&self, seg: SI) -> Shape {
pub fn seg_shape(&self, seg: SI) -> PrimitiveShape {
let (from, to) = self.seg_joints(seg);
Shape::Seg(SegShape {
PrimitiveShape::Seg(SegShape {
from: self.dot_weight(from).pos(),
to: self.dot_weight(to).pos(),
width: self.primitive_weight(seg.node_index()).width(),
})
}
pub fn bend_shape(&self, bend: BI) -> Shape {
pub fn bend_shape(&self, bend: BI) -> PrimitiveShape {
let (from, to) = self.bend_joints(bend);
let core_weight = self.core_weight(bend);
Shape::Bend(BendShape {
PrimitiveShape::Bend(BendShape {
from: self.dot_weight(from).pos(),
to: self.dot_weight(to).pos(),
c: Circle {

View File

@ -1,6 +1,6 @@
#[macro_use]
mod geometry;
pub mod shape;
pub mod primitive;
pub mod with_rtree;
pub use geometry::*;

View File

@ -5,11 +5,11 @@ use rstar::{RTreeObject, AABB};
use crate::math::{self, Circle};
#[enum_dispatch]
pub trait ShapeTrait {
pub trait PrimitiveShapeTrait {
fn priority(&self) -> u64;
fn inflate(&self, margin: f64) -> Shape;
fn inflate(&self, margin: f64) -> PrimitiveShape;
fn center(&self) -> Point;
fn intersects(&self, other: &Shape) -> bool;
fn intersects(&self, other: &PrimitiveShape) -> bool;
fn envelope(&self, margin: f64) -> AABB<[f64; 2]>;
fn width(&self) -> f64;
fn length(&self) -> f64;
@ -35,9 +35,9 @@ pub trait ShapeTrait {
}
}
#[enum_dispatch(ShapeTrait)]
#[enum_dispatch(PrimitiveShapeTrait)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Shape {
pub enum PrimitiveShape {
// Intentionally in different order to reorder `self.intersects(...)` properly.
Dot(DotShape),
Seg(SegShape),
@ -49,13 +49,13 @@ pub struct DotShape {
pub c: Circle,
}
impl ShapeTrait for DotShape {
impl PrimitiveShapeTrait for DotShape {
fn priority(&self) -> u64 {
3
}
fn inflate(&self, margin: f64) -> Shape {
Shape::Dot(DotShape {
fn inflate(&self, margin: f64) -> PrimitiveShape {
PrimitiveShape::Dot(DotShape {
c: Circle {
pos: self.c.pos,
r: self.c.r + margin,
@ -67,15 +67,19 @@ impl ShapeTrait for DotShape {
self.c.pos
}
fn intersects(&self, other: &Shape) -> bool {
fn intersects(&self, other: &PrimitiveShape) -> bool {
if self.priority() < other.priority() {
return other.intersects(&Shape::from(*self));
return other.intersects(&PrimitiveShape::from(*self));
}
match other {
Shape::Dot(other) => self.c.pos.euclidean_distance(&other.c.pos) < self.c.r + other.c.r,
Shape::Seg(other) => self.c.pos.euclidean_distance(&other.polygon()) < self.c.r,
Shape::Bend(other) => {
PrimitiveShape::Dot(other) => {
self.c.pos.euclidean_distance(&other.c.pos) < self.c.r + other.c.r
}
PrimitiveShape::Seg(other) => {
self.c.pos.euclidean_distance(&other.polygon()) < self.c.r
}
PrimitiveShape::Bend(other) => {
for point in math::intersect_circles(&self.c, &other.inner_circle()) {
if other.between_ends(point) {
return true;
@ -139,13 +143,13 @@ impl SegShape {
}
}
impl ShapeTrait for SegShape {
impl PrimitiveShapeTrait for SegShape {
fn priority(&self) -> u64 {
2
}
fn inflate(&self, margin: f64) -> Shape {
Shape::Seg(SegShape {
fn inflate(&self, margin: f64) -> PrimitiveShape {
PrimitiveShape::Seg(SegShape {
from: self.from,
to: self.to,
width: self.width + 2.0 * margin,
@ -156,15 +160,15 @@ impl ShapeTrait for SegShape {
(self.from + self.to) / 2.0
}
fn intersects(&self, other: &Shape) -> bool {
fn intersects(&self, other: &PrimitiveShape) -> bool {
if self.priority() < other.priority() {
return other.intersects(&Shape::from(*self));
return other.intersects(&PrimitiveShape::from(*self));
}
match other {
Shape::Dot(..) => unreachable!(),
Shape::Seg(other) => self.polygon().intersects(&other.polygon()),
Shape::Bend(other) => {
PrimitiveShape::Dot(..) => unreachable!(),
PrimitiveShape::Seg(other) => self.polygon().intersects(&other.polygon()),
PrimitiveShape::Bend(other) => {
for segment in self.polygon().exterior().lines() {
let inner_circle = other.inner_circle();
let outer_circle = other.outer_circle();
@ -248,13 +252,13 @@ impl BendShape {
}
}
impl ShapeTrait for BendShape {
impl PrimitiveShapeTrait for BendShape {
fn priority(&self) -> u64 {
1
}
fn inflate(&self, margin: f64) -> Shape {
Shape::Bend(BendShape {
fn inflate(&self, margin: f64) -> PrimitiveShape {
PrimitiveShape::Bend(BendShape {
from: self.from, // TODO: Is not inflated for now.
to: self.to, // TODO: Is not inflated for now.
c: Circle {
@ -270,14 +274,14 @@ impl ShapeTrait for BendShape {
self.c.pos + (sum / sum.euclidean_distance(&point! {x: 0.0, y: 0.0})) * self.c.r
}
fn intersects(&self, other: &Shape) -> bool {
fn intersects(&self, other: &PrimitiveShape) -> bool {
if self.priority() < other.priority() {
return other.intersects(&Shape::from(*self));
return other.intersects(&PrimitiveShape::from(*self));
}
match other {
Shape::Dot(..) | Shape::Seg(..) => unreachable!(),
Shape::Bend(other) => {
PrimitiveShape::Dot(..) | PrimitiveShape::Seg(..) => unreachable!(),
PrimitiveShape::Bend(other) => {
for point in math::intersect_circles(&self.inner_circle(), &other.inner_circle()) {
if self.between_ends(point) && other.between_ends(point) {
return true;
@ -334,9 +338,9 @@ impl ShapeTrait for BendShape {
}
}
impl RTreeObject for Shape {
impl RTreeObject for PrimitiveShape {
type Envelope = AABB<[f64; 2]>;
fn envelope(&self) -> Self::Envelope {
ShapeTrait::envelope(self, 0.0)
PrimitiveShapeTrait::envelope(self, 0.0)
}
}

View File

@ -8,7 +8,7 @@ use rstar::{primitives::GeomWithData, Envelope, RTree, RTreeObject, AABB};
use crate::{
drawing::graph::{GetLayer, Retag},
geometry::{
shape::{Shape, ShapeTrait},
primitive::{PrimitiveShape, PrimitiveShapeTrait},
BendWeightTrait, DotWeightTrait, Geometry, GeometryLabel, GetWidth, Node, SegWeightTrait,
},
graph::{GenericIndex, GetNodeIndex},
@ -331,7 +331,7 @@ impl<
BboxedIndex::new(Bbox::new(aabb), Node::Grouping(grouping))
}
fn shape(&self, primitive: PI) -> Shape {
fn shape(&self, primitive: PI) -> PrimitiveShape {
if let Ok(dot) = <PI as TryInto<DI>>::try_into(primitive) {
self.geometry.dot_shape(dot)
} else if let Ok(seg) = <PI as TryInto<SI>>::try_into(primitive) {

View File

@ -17,7 +17,7 @@ use crate::{
primitive::{GetCore, MakeShape, Primitive},
Drawing,
},
geometry::shape::ShapeTrait,
geometry::primitive::PrimitiveShapeTrait,
graph::GetNodeIndex,
triangulation::{GetVertexIndex, Triangulation},
};

View File

@ -12,7 +12,7 @@ use crate::drawing::{
primitive::MakeShape,
rules::RulesTrait,
};
use crate::geometry::shape::ShapeTrait;
use crate::geometry::primitive::PrimitiveShapeTrait;
use crate::layout::connectivity::BandIndex;
use crate::layout::Layout;