HilVert  1v2
A demo of the Hilbert Curve in SVG.
HilVert Class Reference

Generate SVG visualisations of the Hilbert Curve (HC). More...

#include <HilVert.hpp>

Public Types

enum  HVenm {
  HVenm::errNone, HVenm::errArgc, HVenm::errOrder, HVenm::errHdng,
  HVenm::errRotat, HVenm::errFile, HVenm::errUknown, HVenm::right,
  HVenm::down, HVenm::left, HVenm::up, HVenm::stard,
  HVenm::anticlock, HVenm::clockwise, HVenm::alike, HVenm::widdershins
}
 

Public Member Functions

 HilVert (int curvOrd=1, HVenm heading=HVenm::down, HVenm rotation=HVenm::clockwise, std::ostream *outSink=&std::cout)
 
 HilVert (const HilVert &src)
 
HilVert & operator= (const HilVert &src)
 
 ~HilVert ()
 
void draw ()
 Output the SVG line segments for a complete HC. More...
 
HilVert::HVenm cmndLn (int argc, const char *argv[])
 This is called by the main() function to set up the HilVert object. More...
 
void grfc ()
 Output a complete HC in Scaleable Vector Graphic format. More...
 
std::string vers ()
 Generate the version number for HilVert. More...
 
const std::string & errs (HVenm erno)
 Translate an error code to a text string. More...
 

Private Member Functions

void turnExe (HVenm sense)
 Alter the current heading by 90 degrees. More...
 
void sgmnt ()
 Draw a path segment. More...
 
void drawChld (HVenm flipRot)
 

Private Attributes

std::ofstream ofs
 
std::ostream * osink
 
HVenm hdng
 
int crvOrd
 
bool flush {true}
 
HVenm rotn {HVenm::clockwise}
 

Static Private Attributes

static constexpr int segPx {4}
 
static constexpr int linSegsCrd {20}
 
static const std::map< HVenm, std::string > errTyp
 

Detailed Description

The HilVert class may be employed in two ways. The first is to use the class programmatically via the interface provided by its constructor. This method allows either a 'ready to browse' SVG file complete with SMIL animation, or just the HC path commands alone. See HilVert.cpp for details.

The second way is to use the 'ready rolled' HilVert command line application. See main.cpp for details.

Definition at line 59 of file HilVert.hpp.

Member Enumeration Documentation

◆ HVenm

enum HilVert::HVenm
strong

A multi-purpose in-class enumeration.
Used for both internal operations and to pass arguments to the constructor, or from the OS command line. Also returned by cmndLn() as an error code.

Enumerator
errNone 

See errs()

errArgc 

See errs()

errOrder 

See errs()

errHdng 

See errs()

errRotat 

See errs()

errFile 

See errs()

errUknown 

See errs()

right 

See cmndLn()

down 

See cmndLn()

left 

See cmndLn()

up 

See cmndLn()

stard 

Internal use

anticlock 

See cmndLn()

clockwise 

See cmndLn()

alike 

Internal use

widdershins 

Internal use

Definition at line 69 of file HilVert.hpp.

Constructor & Destructor Documentation

◆ HilVert() [1/2]

HilVert::HilVert ( int  curvOrd = 1,
HVenm  heading = HVenm::down,
HVenm  rotation = HVenm::clockwise,
std::ostream *  outSink = &std::cout 
)
explicit

◆ HilVert() [2/2]

HilVert::HilVert ( const HilVert &  src)

Definition at line 138 of file HilVert.cpp.

◆ ~HilVert()

HilVert::~HilVert ( )
inline

Definition at line 97 of file HilVert.hpp.

Member Function Documentation

◆ cmndLn()

Hen HilVert::cmndLn ( int  argc,
const char *  argv[] 
)

The main() function passes argc and argv to here unmodified. cmndLn() will leave HilVert's settings unmodified (likely as defaults) unless they are set explicitly from the command line. Make sanity checks on each one.

See main.cpp for details of the command line format.

Parameters
argcThe argument count (1 though 5) as forwarded by main().
argvThe argument strings as forwarded by main().
Returns
An enum value of errNone if everything was good.

Definition at line 567 of file HilVert.cpp.

◆ draw()

void HilVert::draw ( )

This is the crucial method of the HilVert class. It produces all of the one and only SVG path (except for the initial M4,4). It achieves this both by direct calls to sgmnt() and, indirectly, through recursion.

It is important to understand how the strategy of draw() is a correct algorithm for production of any of the 8 variants of the HC. These variants are predicated on the initial settings of 4 possible orientations in combination with 2 reflections. That is, class members hdng and rotn must be set prior to the call to draw().

draw() uses the following sequence. We here assume an 'overall' clockwise rotation sense. For anticlock, the sense of each turn and each child becomes the inverse -

turn left produce child anticlock draw segment turn right produce child clockwise draw segment produce child clockwise turn right draw segment produce child anticlock turn left

Note: Production of a child causes no change in heading. A parent passes the heading hdng to its children, but returning it to the parent would be redundant. The final turn is retained only for subsequent 'draws' on the same HilVert instance (if any).

V Enter with this heading ...
V
V viewbox
-----------------
|Strt |
| > > > \ |
| V |
| V |
| V |
| < < < / |
|Fin |
-----------------
V
V
V ... and leave heading the same way.

More generally, the start has to be one of the four corners -

4,4 8,4
-------------
|A B|
| |
| |
| |
|C D|
-------------
4,8 8,8

hdng | rotn | | Start | | corner | | | d | | | | D CW A M4,4 h 4 v 4 h -4 D AC B M8,4 h -4 v 4 h 4 U CW D M8,8 h -4 v -4 h 4 U AC C M4,8 h 4 v -4 h -4 L CW B M8,4 v 4 h -4 v -4 L AC D M8,8 v -4 h -4 v 4 R CW C M4,8 v -4 h 4 v 4 R AC A M4,4 v 4 h 4 v -4

Most of the constants here are chosen for aesthetic/empirical reasons. Tweak them as you wish.

Definition at line 527 of file HilVert.cpp.

◆ drawChld()

void HilVert::drawChld ( HVenm  flipRtn)
private

Draw a Hilbert curve of the next lower order.

Parameters
flipRtnis HVenm::alike if the child must be drawn with the same overall rotation as the current curve (sub HCs 2 and 3). HVenm::widdershins if we draw in the opposite direction (sub HCs 1 & 4).

Definition at line 423 of file HilVert.cpp.

◆ errs()

const string & HilVert::errs ( HVenm  erno)

Function HilVert::cmndLn() performs error checks and returns an enumerator from HilVert::HVenm to specify the result.

Parameters
ernoAn error number, actualy any one of 7 enumerators from HilVert::HVenm
Returns
A std::string representing erno in human readable form.
erno Return
errNone ""
errArgc "argc"
errOrder "order"
errHdng "heading"
errRotat "rotation"
errFile "file"
errUknown "unknown"
If erno is other than these 7, the function returns "not_known".

Definition at line 323 of file HilVert.cpp.

◆ grfc()

void HilVert::grfc ( )

To draw a complete SVG, only a single <path> element is required describing the entire HC. See draw() for details. However, the first part of the associated 'd' attribute is an absolute Move to the coordinates of the proper starting point within the area defined by the viewbox attribute. For the default HC this is the top left corner -

d="M4,4 ..."

and 'clockwise' rotation from a 'down' heading -

d="M4,4h4v4h-4"

viewbox

|Strt | | > > > \ | | V | | V | | V | | < < < / |

|Fin |

More generally, the start has to be one of the four corners -

4,4 8,4

A B
C D

4,8 8,8

hdng | rotn | | Start | | corner | | | d | | | | D CW A M4,4 h 4 v 4 h -4 D AC B M8,4 h -4 v 4 h 4 U CW D M8,8 h -4 v -4 h 4 U AC C M4,8 h 4 v -4 h -4 L CW B M8,4 v 4 h -4 v -4 L AC D M8,8 v -4 h -4 v 4 R CW C M4,8 v -4 h 4 v 4 R AC A M4,4 v 4 h 4 v -4

Most of the constants here are chosen for aesthetic/empirical reasons. Tweak them as you wish.

Definition at line 677 of file HilVert.cpp.

◆ operator=()

HilVert& HilVert::operator= ( const HilVert &  src)

◆ sgmnt()

void HilVert::sgmnt ( )
private

Generate the portion of an SVG path command corresponding to one line segment of the curve in the direction of the current heading.

Use the lower case (relative move) direction specifiers v and h for drawing in vertical or horizontal directions as specified by the value of hdng. For example, if hdng == HVenm::down then sgmnt() may emit v4 meaning draw a line 4 pixels long vertically downwards in the SVG coordinate system.

When a HC of any particular order is drawn, sgmnt() is called three times directly (rather than via recursion to lower orders).

There are two complications. Firstly, although most of the HC requires 'single length' segments (e.g. 4 pixels), curves of order 2 and above have some double segments (8 px). Higher order curves also have at least one triple length segment (12 px). This last is most easily drawn as -

h-4 h-4 h-4

but it is clearly more efficient to say -

h-12

Rather than clutter the actual HC algorithm, we instead modify sgmnt() to handle aggregation. So, segments are not drawn when first seen, but instead stored in local static oldhdg At the subsequent call to sgmnt(), a check is made for a repeat move. This continues until a different direction is passed in. At that point the aggregate move is drawn and sgmnt() then repeats this stratagem.

For example, if the following sequence of hdngs is seen -

hdng oldhdg mple emit
R S 0 .
L R 0 h4
U L 0 h-4
D U 0 v-4
R D 0 v4
L R 0 h4
L L 0 .
L L 1 .
U L 2 h-12
D U 0 v-4
S D 0 v4

then aggregation saves two unnecessary segments. Heading value S ( HVenm::stard ) is a dummy direction filtered out by sgmnt(), and will never be treated as a repeat itself but causes any buffered repeat sequence to be flushed.

The second complication is that higher order HCs are best not filed as a single line. After every 20 segments we generate EOL.

Definition at line 394 of file HilVert.cpp.

◆ turnExe()

void HilVert::turnExe ( HVenm  sense)
private

Execute a left or right turn according to:

  • Whether we are currently headed right, left, up or down (hdng).
  • Overall rotation sense for the current level of curve order (rotn).
  • Which of the four sequential calls to drawChld() is in progress (alike/widdershins).

Heading changes -

Old Heading rotn sense New Heading
right CW alike down
right CW widdershins up
right AC alike up
right AC widdershins down
down CW alike left
down CW widdershins right
down AC alike right
down AC widdershins left
left CW alike up
left CW widdershins down
left AC alike down
left AC widdershins up
up CW alike right
up CW widdershins left
up AC alike left
up AC widdershins right
@param sense 
  • = alike if we want to turn in the same direction as the current order of HC.
  • = widdershins if we must turn in the opposite way to the HC.

Definition at line 290 of file HilVert.cpp.

◆ vers()

string HilVert::vers ( )
Returns
a std::string like "HilVert: 1v2".

Definition at line 610 of file HilVert.cpp.

Field Documentation

◆ crvOrd

int HilVert::crvOrd
private

The order of the Hilbert curve to draw. = 1 for first order, = 2 for second order etc.

Definition at line 114 of file HilVert.hpp.

◆ errTyp

const std::map< Hen, string > HilVert::errTyp
staticprivate
Initial value:
{
{Hen::errNone, ""}, {Hen::errArgc, "argc"},
{Hen::errOrder, "order"}, {Hen::errHdng, "heading"},
{Hen::errRotat, "rotation"}, {Hen::errFile, "file"},
{Hen::errUknown,"unknown"}
}

Definition at line 127 of file HilVert.hpp.

◆ flush

bool HilVert::flush {true}
private

Definition at line 120 of file HilVert.hpp.

◆ hdng

HVenm HilVert::hdng
private

Definition at line 110 of file HilVert.hpp.

◆ linSegsCrd

constexpr int HilVert::linSegsCrd {20}
staticconstexprprivate

Definition at line 119 of file HilVert.hpp.

◆ ofs

std::ofstream HilVert::ofs
private

Definition at line 108 of file HilVert.hpp.

◆ osink

std::ostream* HilVert::osink
private

Definition at line 109 of file HilVert.hpp.

◆ rotn

HVenm HilVert::rotn {HVenm::clockwise}
private

The overall rotation direction at this level.
HilVert::anticlock or clockwise.

Definition at line 126 of file HilVert.hpp.

◆ segPx

constexpr int HilVert::segPx {4}
staticconstexprprivate

Definition at line 118 of file HilVert.hpp.


The documentation for this class was generated from the following files:
HilVert::HVenm::errOrder
@ errOrder
HilVert::HVenm::errArgc
@ errArgc
HilVert::HVenm::errNone
@ errNone
HilVert::HVenm::errFile
@ errFile
HilVert::HVenm::errHdng
@ errHdng
HilVert::HVenm::errUknown
@ errUknown
HilVert::HVenm::errRotat
@ errRotat