HilVert  1v2
A demo of the Hilbert Curve in SVG.
HilVert.cpp
Go to the documentation of this file.
1 // FILE HilVert.cpp
2 // DATE 201011
3 // WRITTEN RAC
4 // LANGUAGE C++, XCode
5 // PURPOSE Impl for the hilbert curve SVG demo. $
6 // KEYWORDS HILBERT/CURVE/DRAW/PLOT/ANIMATE
7 // PROJECT WEB
8 // TARGET Command line C++ 17
9 // CATEGORY Utility
10 //
11 //
12 //
13 
14 
80 #include <cstddef>
81 #include <cstdlib>
82 #include "HilVert.hpp"
83 
84 using std::string;
85 using std::ostream;
87 
88 // Note to self, C++20 'using enum' fixes the need for this pain -
89 constexpr auto right = Hen::right;
90 constexpr auto down = Hen::down;
91 constexpr auto left = Hen::left;
92 constexpr auto up = Hen::up;
93 constexpr auto stard = Hen::stard;
94 constexpr auto anticlock = Hen::anticlock;
95 constexpr auto clockwise = Hen::clockwise;
96 constexpr auto alike = Hen::alike;
97 constexpr auto widdershins = Hen::widdershins;
98 
124  int curvOrd,
125  HVenm heading,
126  HVenm rotation,
127  ostream* outSink
128 ) : osink{outSink},
129  hdng{heading},
130  rotn{rotation},
131  crvOrd{curvOrd},
132  flush{true} {
133 }
134 /*
135  * End HilVert ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
136  */
137 
139  osink{src.osink},
140  hdng{src.hdng},
141  rotn{src.rotn},
142  crvOrd{src.crvOrd - 1},
143  flush{false} {
144 }
145 /*
146  * End HilVert ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
147  */
148 
149 
150 /*
151  * @brief A mapping of error codes to text strings.
152  *
153  *
154  *
155  *
156  */
157 const std::map<Hen, string> HilVert::errTyp {
158  {Hen::errNone, ""}, {Hen::errArgc, "argc"},
159  {Hen::errOrder, "order"}, {Hen::errHdng, "heading"},
160  {Hen::errRotat, "rotation"}, {Hen::errFile, "file"},
161  {Hen::errUknown,"unknown"}
162 };
163 
164 
187 auto& operator^=(Hen& e, const bool& b) {
188  return b ? (e = (e == clockwise ? anticlock : clockwise)) : e;
189 }
190 /*
191  * End operator^= ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
192  */
193 
194 
212 auto& operator++(Hen& e) {
213  switch (e) {
214  case right: return e = down;
215  case down: return e = left;
216  case left: return e = up;
217  default: return e = right;
218  }
219 }
220 /*
221  * End operator++ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
222  */
223 
242  switch (e) {
243  case right: return e = up;
244  case down: return e = right;
245  case left: return e = down;
246  default: return e = left;
247  }
248 }
249 /*
250  * End operator-- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
251  */
252 
253 
254 
290 void HilVert::turnExe(HVenm sense) {
291 
292  // Incrementing a Heading performs a right (clockwise) turn.
293  // Decrementing a Heading performs a left (anticlockwise) turn.
294  auto r{rotn};
295  (r ^= sense == widdershins) == clockwise ? ++hdng : --hdng;
296 }
297 /*
298  * End turnExe ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
299  */
300 
301 
323 const string& HilVert::errs(Hen erno) {
324 
325  static string errtxt{"not_known"};
326  return HilVert::errTyp.count(erno) ? errTyp.at(erno) : errtxt;
327 }
328 /*
329  * End errs ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
330  */
331 
332 
333 
395 
396  static HVenm oldhdg{stard};
397  static int mple{0};
398  static int segsf{linSegsCrd};
399  bool rpete{hdng == oldhdg};
400  if (rpete) ++mple; else if (osink != nullptr && oldhdg != stard) {
401  if (segsf == linSegsCrd) *osink << "\t\t\t";
402  *osink << ((oldhdg == up || oldhdg == down) ? 'v' : 'h');
403  *osink << ((oldhdg == left || oldhdg == up ) ? "-" : "");
404  *osink << segPx * (mple + 1);
405  mple = 0;
406  if (--segsf) ; else segsf = linSegsCrd, *osink << '\n';
407  }
408  oldhdg = hdng;
409 }
410 /*
411  * End sgmnt ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
412  */
413 
414 
423 void HilVert::drawChld(HVenm flipRtn) {
424 
425  // If we are already at the lowest order then its a no-op -
426  if (crvOrd <= 1) return;
427  HilVert chld{*this}; // Recursion
428  chld.rotn ^= flipRtn == widdershins;
429  chld.draw();
430 }
431 /*
432  * End drawChld ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
433  */
434 
435 
528 
531  sgmnt();
532  turnExe(alike);
533  drawChld(alike);
534  sgmnt();
535  drawChld(alike);
536  turnExe(alike);
537  sgmnt();
540  if (flush) {
541  hdng = stard;
542  sgmnt();
543  }
544 }
545 /*
546  * End HilVert::HilVert ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
547  */
548 
549 
567 Hen HilVert::cmndLn(int argc, const char * argv[]) {
568 
569  auto co{crvOrd};
570  auto hd{hdng};
571  auto rt{rotn};
572  auto* os{osink};
573 
574  std::map<string, HVenm> m {
575  {"up", up}, {"down", down}, {"clockwise", clockwise},
576  {"left", left}, {"right", right}, {"anticlock", anticlock}
577  };
578 
579 
580  switch (argc) {
581  case 5:
582  if (ofs.is_open()) ofs.close();
583  ofs.open(argv[4]);
584  if (ofs.is_open()) os = &ofs; else
585  return HVenm::errFile;
586  case 4:
587  if (m.count(argv[3])) rt = m[argv[3]]; else return HVenm::errRotat;
588  case 3:
589  if (m.count(argv[2])) hd = m[argv[2]]; else return HVenm::errHdng;
590  case 2:
591  // stoi() threw an exception in my face -
592  co = std::atoi(argv[1]);
593  if (co < 1 || co > 12) return HVenm::errOrder;
594  crvOrd = co; hdng = hd; rotn = rt; osink = os;
595  case 1: return HVenm::errNone;
596  default: return HVenm::errArgc;
597  }
598 }
599 /*
600  * End cmndLn ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
601  */
602 
603 
610 string HilVert::vers() {
611  return "HilVert: 1v2";
612 }
613 /*
614  * End vers ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
615  */
616 
617 
678 
679  auto clsSde{1 << crvOrd}; // Cells per side (2^Ord)
680  auto shigh{clsSde * segPx}; // Higher start ordinate / px
681  auto vside{shigh + segPx}; // Viewbox side / px
682  auto dst{(clsSde * clsSde - 1) * segPx}; // Total distance along HC / px
683  auto sda{(dst * 11) / 10 + 6}; // Stroke dash array/ px
684  auto dtime{sda / 40 + 3}; // Animation duration / seconds
685  auto cw{rotn == clockwise};
686  auto l{hdng == left};
687  auto u{hdng == up};
688  auto d{hdng == down};
689  auto r{hdng == right};
690 
691  if (osink == nullptr) return;
692  *osink <<
693  "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
694  "<!-- Generator: " << vers() << " -->\n<svg\n" <<
695  "\txmlns=\"http://www.w3.org/2000/svg\"\n" <<
696  "\txmlns:xlink=\"http://www.w3.org/1999/xlink\"\n" <<
697  "\tviewBox=\"0 0 " << vside << ' ' << vside << "\"\n" <<
698  ">\n" <<
699  "\t<path\n" <<
700  "\t\tid=\"pth\"\n" <<
701  "\t\tstroke=\"blue\"\n" <<
702  "\t\tfill=\"yellow\"\n" <<
703  "\t\tstroke-width=\"1px\"\n" <<
704  "\t\tstroke-dasharray=\"" << sda << "\"\n" <<
705  "\t\td=\"\n" <<
706  "\t\t\tM"
707  << (r || (!l && u ^ cw) ? segPx : shigh)
708  << ','
709  << (d || (!u && r ^ cw) ? segPx : shigh)
710  << '\n'
711  ;
712  draw();
713  *osink <<
714  "\n\t\t\"\n" <<
715  "\t/><animate\n" <<
716  "\t\tid=\"sdo\"\n" <<
717  "\t\txlink:href=\"#pth\"\n" <<
718  "\t\tattributeName=\"stroke-dashoffset\"\n" <<
719  "\t\tfrom=\"" << sda << "\"\n" <<
720  "\t\tto=\"0\"\n" <<
721  "\t\tbegin=\"2s\"\n" <<
722  "\t\tdur=\"" << dtime << "s\"\n" <<
723  "\t\tfill=\"freeze\"\n" <<
724  "\t\trepeatCount=\"indefinite\"\n" <<
725  "\t/>\n" <<
726  "</svg>\n"
727  ;
728 }
729 /*
730  * End grfc ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
731  */
732 
733 /*
734  * End HilVert.cpp ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
735  */
736 
737 
738 
739 
HilVert::draw
void draw()
Output the SVG line segments for a complete HC.
Definition: HilVert.cpp:527
HilVert::HVenm::errOrder
@ errOrder
HilVert::HVenm::errArgc
@ errArgc
HilVert::grfc
void grfc()
Output a complete HC in Scaleable Vector Graphic format.
Definition: HilVert.cpp:677
HilVert::rotn
HVenm rotn
Definition: HilVert.hpp:126
HilVert::errs
const std::string & errs(HVenm erno)
Translate an error code to a text string.
Definition: HilVert.cpp:323
HilVert::HVenm::stard
@ stard
operator--
Hen & operator--(Hen &e)
Turn to the left of the current heading.
Definition: HilVert.cpp:241
HilVert::osink
std::ostream * osink
Definition: HilVert.hpp:109
HilVert
Generate SVG visualisations of the Hilbert Curve (HC).
Definition: HilVert.hpp:59
HilVert::HVenm::errNone
@ errNone
HilVert::segPx
static constexpr int segPx
Definition: HilVert.hpp:118
HilVert::ofs
std::ofstream ofs
Definition: HilVert.hpp:108
up
constexpr auto up
Definition: HilVert.cpp:92
HilVert::errTyp
static const std::map< HVenm, std::string > errTyp
Definition: HilVert.hpp:127
clockwise
constexpr auto clockwise
Definition: HilVert.cpp:95
HilVert::vers
std::string vers()
Generate the version number for HilVert.
Definition: HilVert.cpp:610
HilVert::HVenm
HVenm
Definition: HilVert.hpp:69
HilVert.hpp
All declarations for the HilVert class.
HilVert::HVenm::anticlock
@ anticlock
HilVert::linSegsCrd
static constexpr int linSegsCrd
Definition: HilVert.hpp:119
HilVert::HVenm::up
@ up
HilVert::HVenm::clockwise
@ clockwise
HilVert::HVenm::down
@ down
alike
constexpr auto alike
Definition: HilVert.cpp:96
HilVert::HVenm::errFile
@ errFile
HilVert::crvOrd
int crvOrd
Definition: HilVert.hpp:114
HilVert::HVenm::widdershins
@ widdershins
HilVert::sgmnt
void sgmnt()
Draw a path segment.
Definition: HilVert.cpp:394
left
constexpr auto left
Definition: HilVert.cpp:91
right
constexpr auto right
Definition: HilVert.cpp:89
anticlock
constexpr auto anticlock
Definition: HilVert.cpp:94
HilVert::HVenm::alike
@ alike
widdershins
constexpr auto widdershins
Definition: HilVert.cpp:97
HilVert::HilVert
HilVert(int curvOrd=1, HVenm heading=HVenm::down, HVenm rotation=HVenm::clockwise, std::ostream *outSink=&std::cout)
HilVert::HVenm::left
@ left
stard
constexpr auto stard
Definition: HilVert.cpp:93
HilVert::HVenm::errHdng
@ errHdng
HilVert::turnExe
void turnExe(HVenm sense)
Alter the current heading by 90 degrees.
Definition: HilVert.cpp:290
down
constexpr auto down
Definition: HilVert.cpp:90
HilVert::HVenm::errUknown
@ errUknown
HilVert::flush
bool flush
Definition: HilVert.hpp:120
operator++
auto & operator++(Hen &e)
Turn to the right of the current heading.
Definition: HilVert.cpp:212
HilVert::HVenm::right
@ right
HilVert::cmndLn
HilVert::HVenm cmndLn(int argc, const char *argv[])
This is called by the main() function to set up the HilVert object.
Definition: HilVert.cpp:567
HilVert::hdng
HVenm hdng
Definition: HilVert.hpp:110
HilVert::drawChld
void drawChld(HVenm flipRot)
Definition: HilVert.cpp:423
HilVert::HVenm::errRotat
@ errRotat
operator^=
auto & operator^=(Hen &e, const bool &b)
Swap clockwise with anticlock on condition.
Definition: HilVert.cpp:187