/ examples / bus.rs
bus.rs
  1  use kurbo::{Affine, BezPath};
  2  
  3  use linesweeper::topology::{Topology, WindingNumber};
  4  
  5  #[derive(Copy, Clone, Debug, PartialEq, Eq, Default)]
  6  struct Windings {
  7      main: i32,
  8      cutout: i32,
  9      modifier: i32,
 10  }
 11  
 12  impl std::ops::Add for Windings {
 13      type Output = Windings;
 14  
 15      fn add(self, rhs: Self) -> Self::Output {
 16          Self {
 17              main: self.main + rhs.main,
 18              cutout: self.cutout + rhs.cutout,
 19              modifier: self.modifier + rhs.modifier,
 20          }
 21      }
 22  }
 23  
 24  impl std::ops::AddAssign for Windings {
 25      fn add_assign(&mut self, rhs: Self) {
 26          *self = *self + rhs
 27      }
 28  }
 29  
 30  impl WindingNumber for Windings {
 31      type Tag = Tag;
 32  
 33      fn single(tag: Self::Tag, positive: bool) -> Self {
 34          let sign = if positive { 1 } else { -1 };
 35          match tag {
 36              Tag::Main => Self {
 37                  main: sign,
 38                  ..Default::default()
 39              },
 40              Tag::Cutout => Self {
 41                  cutout: sign,
 42                  ..Default::default()
 43              },
 44              Tag::Modifier => Self {
 45                  modifier: sign,
 46                  ..Default::default()
 47              },
 48          }
 49      }
 50  }
 51  
 52  #[derive(Copy, Clone, Debug, PartialEq, Eq)]
 53  enum Tag {
 54      Main,
 55      Cutout,
 56      Modifier,
 57  }
 58  
 59  pub fn main() -> anyhow::Result<()> {
 60      let bus = "M0,80 C0,35.8 35.8,0 80,0 L432,0 C476.2,0 512,35.8 512,80 L512,368 C512,394.2 499.4,417.4 480,432 L480,480 C480,497.7 465.7,512 448,512 L416,512 C398.3,512 384,497.7 384,480 L384,448 L128,448 L128,480 C128,497.7 113.7,512 96,512 L64,512 C46.3,512 32,497.7 32,480 L32,432 C12.6,417.4 0,394.2 0,368 L0,80 Z M129.9,152.2 L112,224 L400,224 L382.1,152.2 C378.5,138 365.7,128 351,128 L161,128 C146.3,128 133.5,138 130,152.2 Z M128,320 A32 32 0 1 0 64,320 A32 32 0 1 0 128,320 Z M416,352 A32 32 0 1 0 416,288 A32 32 0 1 0 416,352 Z";
 61      let cutout = "M320 512V266.8C288.1 221.6 235.5 192 176 192C78.8 192 0 270.8 0 368c0 59.5 29.6 112.1 74.8 144H320z";
 62      let modifier = "M144 512c79.5 0 144-64.5 144-144s-64.5-144-144-144S0 288.5 0 368s64.5 144 144 144zm67.3-164.7l-72 72c-6.2 6.2-16.4 6.2-22.6 0l-40-40c-6.2-6.2-6.2-16.4 0-22.6s16.4-6.2 22.6 0L128 385.4l60.7-60.7c6.2-6.2 16.4-6.2 22.6 0s6.2 16.4 0 22.6z";
 63  
 64      let bus = BezPath::from_svg(bus).unwrap();
 65      let cutout = Affine::translate((193.0, 0.0)) * BezPath::from_svg(cutout).unwrap();
 66      let modifier = Affine::translate((224.0, 0.0)) * BezPath::from_svg(modifier).unwrap();
 67  
 68      let eps = 1e-5;
 69      let top = Topology::<Windings>::from_paths(
 70          [
 71              (&bus, Tag::Main),
 72              (&cutout, Tag::Cutout),
 73              (&modifier, Tag::Modifier),
 74          ],
 75          eps,
 76      )
 77      .unwrap();
 78      let bbox = top.bounding_box();
 79      let min_x = bbox.min_x();
 80      let min_y = bbox.min_y();
 81      let max_x = bbox.max_x();
 82      let max_y = bbox.max_y();
 83      let pad = 1.0 + eps;
 84      let one_width = max_x - min_x + 2.0 * pad;
 85      let one_height = max_y - min_y + 2.0 * pad;
 86      let mut doc = svg::Document::new().set(
 87          "viewBox",
 88          (min_x - pad, min_y - pad, one_width * 3.0, one_height * 2.0),
 89      );
 90  
 91      let contours = top.contours(|w| {
 92          let inside = |winding| winding != 0;
 93  
 94          (inside(w.main) && !inside(w.cutout)) || inside(w.modifier)
 95      });
 96  
 97      for group in contours.grouped() {
 98          let mut data = svg::node::element::path::Data::new();
 99  
100          for contour_idx in group {
101              let path = &contours[contour_idx].path;
102              for el in path.iter() {
103                  data = match el {
104                      kurbo::PathEl::MoveTo(p) => data.move_to((p.x, p.y)),
105                      kurbo::PathEl::LineTo(p) => data.line_to((p.x, p.y)),
106                      kurbo::PathEl::QuadTo(p0, p1) => {
107                          data.quadratic_curve_to(((p0.x, p0.y), (p1.x, p1.y)))
108                      }
109                      kurbo::PathEl::CurveTo(p0, p1, p2) => {
110                          data.cubic_curve_to(((p0.x, p0.y), (p1.x, p1.y), (p2.x, p2.y)))
111                      }
112                      kurbo::PathEl::ClosePath => data.close(),
113                  };
114              }
115          }
116          let path = svg::node::element::Path::new()
117              .set("d", data)
118              .set("stroke", "black")
119              .set("stroke-linecap", "round")
120              .set("stroke-linejoin", "round");
121          doc = doc.add(path);
122      }
123  
124      svg::save("out.svg", &doc)?;
125  
126      Ok(())
127  }