topola/src/layout/layout.rs

255 lines
7.8 KiB
Rust

use std::collections::HashMap;
use geo::Point;
use petgraph::stable_graph::StableDiGraph;
use rstar::AABB;
use crate::{
drawing::{
band::BandIndex,
bend::LooseBendWeight,
dot::{DotIndex, FixedDotIndex, FixedDotWeight, LooseDotIndex, LooseDotWeight},
graph::{GetLayer, GetMaybeNet, PrimitiveIndex, PrimitiveWeight, Retag},
primitive::{GetJoints, GetOtherJoint},
rules::RulesTrait,
seg::{
FixedSegIndex, FixedSegWeight, LoneLooseSegIndex, LoneLooseSegWeight, SeqLooseSegIndex,
SeqLooseSegWeight,
},
segbend::Segbend,
wraparoundable::WraparoundableIndex,
Drawing, Infringement, LayoutException,
},
geometry::{
compound::CompoundManagerTrait, poly::PolyShape, primitive::PrimitiveShapeTrait,
shape::ShapeTrait, BendWeightTrait, DotWeightTrait, GenericNode, Geometry, GeometryLabel,
GetWidth, SegWeightTrait,
},
graph::{GenericIndex, GetNodeIndex},
layout::zone::{GetMaybeApex, MakePolyShape, PourZoneIndex, SolidZoneIndex, Zone, ZoneWeight},
math::Circle,
};
pub type NodeIndex = GenericNode<PrimitiveIndex, GenericIndex<ZoneWeight>>;
#[derive(Debug)]
pub struct Layout<R: RulesTrait> {
drawing: Drawing<ZoneWeight, R>,
}
impl<R: RulesTrait> Layout<R> {
pub fn new(drawing: Drawing<ZoneWeight, R>) -> Self {
Self { drawing }
}
pub fn remove_band(&mut self, band: BandIndex) {
self.drawing.remove_band(band);
}
pub fn remove_segbend(&mut self, segbend: &Segbend, face: LooseDotIndex) {
self.drawing.remove_segbend(segbend, face)
}
pub fn insert_segbend(
&mut self,
from: DotIndex,
around: WraparoundableIndex,
dot_weight: LooseDotWeight,
seg_weight: SeqLooseSegWeight,
bend_weight: LooseBendWeight,
cw: bool,
) -> Result<Segbend, LayoutException> {
self.drawing
.insert_segbend(from, around, dot_weight, seg_weight, bend_weight, cw)
}
pub fn add_fixed_dot(&mut self, weight: FixedDotWeight) -> Result<FixedDotIndex, Infringement> {
self.drawing.add_fixed_dot(weight)
}
pub fn add_zone_fixed_dot(
&mut self,
weight: FixedDotWeight,
zone: GenericIndex<ZoneWeight>,
) -> Result<FixedDotIndex, Infringement> {
let maybe_dot = self.drawing.add_fixed_dot(weight);
if let Ok(dot) = maybe_dot {
self.drawing.add_to_compound(dot, zone);
}
maybe_dot
}
pub fn add_fixed_seg(
&mut self,
from: FixedDotIndex,
to: FixedDotIndex,
weight: FixedSegWeight,
) -> Result<FixedSegIndex, Infringement> {
self.drawing.add_fixed_seg(from, to, weight)
}
pub fn add_zone_fixed_seg(
&mut self,
from: FixedDotIndex,
to: FixedDotIndex,
weight: FixedSegWeight,
zone: GenericIndex<ZoneWeight>,
) -> Result<FixedSegIndex, Infringement> {
let maybe_seg = self.add_fixed_seg(from, to, weight);
if let Ok(seg) = maybe_seg {
self.drawing.add_to_compound(seg, zone);
}
maybe_seg
}
pub fn add_lone_loose_seg(
&mut self,
from: FixedDotIndex,
to: FixedDotIndex,
weight: LoneLooseSegWeight,
) -> Result<LoneLooseSegIndex, Infringement> {
self.drawing.add_lone_loose_seg(from, to, weight)
}
pub fn add_seq_loose_seg(
&mut self,
from: DotIndex,
to: LooseDotIndex,
weight: SeqLooseSegWeight,
) -> Result<SeqLooseSegIndex, Infringement> {
self.drawing.add_seq_loose_seg(from, to, weight)
}
pub fn move_dot(&mut self, dot: DotIndex, to: Point) -> Result<(), Infringement> {
self.drawing.move_dot(dot, to)
}
pub fn add_zone(&mut self, weight: ZoneWeight) -> GenericIndex<ZoneWeight> {
self.drawing.add_compound(weight)
}
pub fn zones<W: 'static>(
&self,
node: GenericIndex<W>,
) -> impl Iterator<Item = GenericIndex<ZoneWeight>> + '_ {
self.drawing.compounds(node)
}
pub fn band_length(&self, band: BandIndex) -> f64 {
match band {
BandIndex::Straight(seg) => self.drawing.geometry().seg_shape(seg.into()).length(),
BandIndex::Bended(mut start_seg) => {
let mut length = self.drawing.geometry().seg_shape(start_seg.into()).length();
let mut start_dot = self.drawing.primitive(start_seg).joints().1;
let bend = self.drawing.primitive(start_dot).bend();
length += self.drawing.geometry().bend_shape(bend.into()).length();
let mut prev_dot = self.drawing.primitive(bend).other_joint(start_dot.into());
let mut seg = self.drawing.primitive(prev_dot).seg().unwrap();
length += self.drawing.geometry().seg_shape(seg.into()).length();
while let DotIndex::Loose(dot) =
self.drawing.primitive(seg).other_joint(prev_dot.into())
{
let bend = self.drawing.primitive(dot).bend();
length += self.drawing.geometry().bend_shape(bend.into()).length();
prev_dot = self.drawing.primitive(bend).other_joint(dot);
seg = self.drawing.primitive(prev_dot).seg().unwrap();
length += self.drawing.geometry().seg_shape(seg.into()).length();
}
length
}
}
}
pub fn zone_nodes(&self) -> impl Iterator<Item = GenericIndex<ZoneWeight>> + '_ {
self.drawing.rtree().iter().filter_map(|wrapper| {
if let NodeIndex::Compound(zone) = wrapper.data {
Some(zone)
} else {
None
}
})
}
pub fn layer_zone_nodes(
&self,
layer: u64,
) -> impl Iterator<Item = GenericIndex<ZoneWeight>> + '_ {
self.drawing
.rtree()
.locate_in_envelope_intersecting(&AABB::from_corners(
[-f64::INFINITY, -f64::INFINITY, layer as f64],
[f64::INFINITY, f64::INFINITY, layer as f64],
))
.filter_map(|wrapper| {
if let NodeIndex::Compound(zone) = wrapper.data {
Some(zone)
} else {
None
}
})
}
pub fn zone_members(
&self,
zone: GenericIndex<ZoneWeight>,
) -> impl Iterator<Item = PrimitiveIndex> + '_ {
self.drawing
.geometry()
.compound_members(GenericIndex::new(zone.node_index()))
}
pub fn drawing(&self) -> &Drawing<ZoneWeight, R> {
&self.drawing
}
pub fn rules(&self) -> &R {
self.drawing.rules()
}
pub fn rules_mut(&mut self) -> &mut R {
self.drawing.rules_mut()
}
pub fn zone(&self, index: GenericIndex<ZoneWeight>) -> Zone<R> {
Zone::new(index, self)
}
}
/*impl<R: RulesTrait> CompoundManagerTrait<ZoneWeight, GenericIndex<ZoneWeight>> for Layout<R> {
fn add_compound(&mut self, weight: ZoneWeight) -> GenericIndex<ZoneWeight> {
self.drawing.add_compound(weight)
}
fn remove_compound(&mut self, compound: GenericIndex<ZoneWeight>) {
self.drawing.remove_compound(compound);
}
fn add_to_compound<W>(
&mut self,
primitive: GenericIndex<W>,
compound: GenericIndex<ZoneWeight>,
) {
self.drawing.add_to_compound(primitive, compound);
}
fn compound_weight(&self, compound: GenericIndex<ZoneWeight>) -> ZoneWeight {
self.drawing.compound_weight(compound)
}
fn compounds<W>(
&self,
node: GenericIndex<W>,
) -> impl Iterator<Item = GenericIndex<ZoneWeight>> {
self.drawing.compounds(node)
}
}*/