Skip to content

Commit 2315dda

Browse files
committed
Added templates to allow for other intervals other than interval<int> to be drawn.
1 parent caacb28 commit 2315dda

File tree

1 file changed

+40
-41
lines changed

1 file changed

+40
-41
lines changed

draw.hpp

Lines changed: 40 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@ namespace lib_interval_tree
1616
{
1717
namespace
1818
{
19-
using int_tree_for_draw = interval_tree <interval<int, closed>>;
20-
using int_tree_iterator = int_tree_for_draw::iterator;
21-
using const_int_tree_iterator = int_tree_for_draw::const_iterator;
22-
2319
template <unsigned Size>
2420
struct NumericalPointerEquivalent
2521
{};
@@ -36,7 +32,8 @@ namespace lib_interval_tree
3632
using type = unsigned long long;
3733
};
3834

39-
std::string iterCaption(const_int_tree_iterator iter)
35+
template <typename... List>
36+
std::string iterCaption(typename lib_interval_tree::interval_tree <List...>::const_iterator iter)
4037
{
4138
auto ival = iter->interval();
4239
std::stringstream sstr;
@@ -98,33 +95,36 @@ namespace lib_interval_tree
9895
{
9996
return getiterBounds().getWidth() / 2. + margin * 2.;
10097
}
101-
}
98+
}
10299
//#####################################################################################################################
100+
template <typename... List>
103101
struct TreeGriditer
104102
{
105-
const_int_tree_iterator iter;
103+
typename lib_interval_tree::interval_tree <List...>::const_iterator iter;
106104
std::pair <int, int> parentCoords;
107105
};
106+
template <typename... List>
108107
struct TreeGrid
109108
{
110109
// (row-major)
111110
std::vector < // rows
112111
std::vector < // columns
113-
boost::optional <TreeGriditer>
112+
boost::optional <TreeGriditer<List...>>
114113
>
115114
> grid;
116115
int xMax = 0;
117116
int xMin = 0;
118117
int yMax = 0;
119-
};
118+
};
120119
//#####################################################################################################################
121-
void drawIter(Cairo::DrawContext* ctx, const_int_tree_iterator iter, double x, double y, bool drawPointers)
120+
template <typename... List>
121+
void drawIterator(Cairo::DrawContext* ctx, typename lib_interval_tree::interval_tree <List...>::const_iterator iter, double x, double y, bool drawPointers)
122122
{
123123
auto caption = Cairo::Text(
124124
ctx,
125125
0,
126126
0,
127-
iterCaption(iter),
127+
iterCaption<List...>(iter),
128128
{"Arial", 18, CAIRO_FONT_WEIGHT_BOLD}
129129
);
130130

@@ -212,34 +212,31 @@ namespace lib_interval_tree
212212
ptr.draw(Cairo::Colors::Yellow);
213213
}
214214
*/
215-
}
215+
}
216216
//---------------------------------------------------------------------------------------------------------------------
217-
template <typename iterT>
218-
void drawiter(Cairo::DrawContext* ctx, interval_tree_iterator <iterT> const& iter, double x, double y, bool drawPointers)
219-
{
220-
drawiter(ctx, iter, x, y, drawPointers);
221-
}
222-
//---------------------------------------------------------------------------------------------------------------------
223-
TreeGrid createGrid(int_tree_for_draw const& tree)
217+
template <typename... List>
218+
TreeGrid<List...> createGrid(lib_interval_tree::interval_tree <List...> const& tree)
224219
{
225220
auto root = tree.root();
226221
if (root == std::end(tree))
227222
return {};
228223

229-
TreeGrid grid;
224+
TreeGrid<List...> grid;
225+
226+
using tree_const_iterator = typename lib_interval_tree::interval_tree <List...>::const_iterator;
230227

231-
struct _GridPoint
228+
struct GridPoint
232229
{
233-
const_int_tree_iterator iter;
234-
const_int_tree_iterator parent;
230+
tree_const_iterator iter;
231+
tree_const_iterator parent;
235232
int x;
236233
int y;
237234
};
238235

239-
std::vector <_GridPoint> gridPoints;
236+
std::vector <GridPoint> gridPoints;
240237

241-
std::function <int(const_int_tree_iterator iter)> subtreeSize;
242-
subtreeSize = [&](const_int_tree_iterator iter) {
238+
std::function <int(tree_const_iterator iter)> subtreeSize;
239+
subtreeSize = [&](tree_const_iterator iter) {
243240
if (iter == std::end(tree))
244241
return 0;
245242
if (iter.left() == std::end(tree) && iter.right() == std::end(tree))
@@ -253,8 +250,8 @@ namespace lib_interval_tree
253250
return 0;
254251
};
255252

256-
std::function <void(const_int_tree_iterator, int pX, int pY)> deduceCoordinates;
257-
deduceCoordinates = [&](const_int_tree_iterator iter, int pX, int pY)
253+
std::function <void(tree_const_iterator, int pX, int pY)> deduceCoordinates;
254+
deduceCoordinates = [&](tree_const_iterator iter, int pX, int pY)
258255
{
259256
int y = pY;
260257
int x = pX;
@@ -311,19 +308,20 @@ namespace lib_interval_tree
311308
}
312309
}
313310

314-
grid.grid[i.y][i.x + -grid.xMin] = {TreeGriditer{i.iter, parentCoords}};
311+
grid.grid[i.y][i.x + -grid.xMin] = {TreeGriditer<List...>{i.iter, parentCoords}};
315312
}
316313

317314
return grid;
318-
}
315+
}
319316
//---------------------------------------------------------------------------------------------------------------------
320-
void drawGrid(Cairo::DrawContext* ctx, TreeGrid const& grid, bool drawPointers, bool drawEmpty)
317+
template <typename... List>
318+
void drawGrid(Cairo::DrawContext* ctx, TreeGrid<List...> const& grid, bool drawPointers, bool drawEmpty)
321319
{
322320
auto iterRadius = getiterRadius();
323321
auto cellSize = iterRadius * 2. + gridMargin;
324322

325-
auto iterX = [&](int x_) {return leftPadding + iterRadius + x_ * cellSize + x_ * xPadding;};
326-
auto iterY = [&](int y_) {return topPadding + iterRadius + y_ * cellSize + y_ * yPadding;};
323+
auto iterX = [&](auto x_) {return leftPadding + iterRadius + x_ * cellSize + x_ * xPadding;};
324+
auto iterY = [&](auto y_) {return topPadding + iterRadius + y_ * cellSize + y_ * yPadding;};
327325

328326
// Draw Lines
329327
int y = 0;
@@ -358,7 +356,7 @@ namespace lib_interval_tree
358356
if (cell)
359357
{
360358
auto iter = cell.get().iter;
361-
drawIter(ctx, iter, iterX(x), iterY(y), drawPointers);
359+
drawIterator<List...>(ctx, iter, iterX(x), iterY(y), drawPointers);
362360
}
363361
else if (drawEmpty)
364362
{
@@ -375,9 +373,10 @@ namespace lib_interval_tree
375373
}
376374
++y;
377375
}
378-
}
376+
}
379377
//---------------------------------------------------------------------------------------------------------------------
380-
Cairo::Surface createSurface(TreeGrid const& grid)
378+
template <typename... List>
379+
Cairo::Surface createSurface(TreeGrid<List...> const& grid)
381380
{
382381
auto iterRadius = getiterRadius();
383382
auto cellSize = iterRadius * 2. + gridMargin;
@@ -388,16 +387,16 @@ namespace lib_interval_tree
388387
static_cast <int> (leftPadding + (width) * cellSize + (width-1) * xPadding + rightPadding),
389388
static_cast <int> (topPadding + (height) * cellSize + (height-1) * yPadding + bottomPadding)
390389
};
391-
}
390+
}
392391
//---------------------------------------------------------------------------------------------------------------------
393392
template <typename... List>
394-
void drawTree(std::string const& fileName, lib_interval_tree::interval_tree <List...> const* tree, bool drawPointers = false, bool drawEmpty = false)
393+
void drawTree(std::string const& fileName, lib_interval_tree::interval_tree <List...> const& tree, bool drawPointers = false, bool drawEmpty = false)
395394
{
396-
auto grid = createGrid(*tree);
395+
auto grid = createGrid(tree);
397396
auto surface = createSurface(grid);
398397
Cairo::DrawContext ctx(&surface);
399-
drawGrid(&ctx, grid, drawPointers, drawEmpty);
398+
drawGrid<List...>(&ctx, grid, drawPointers, drawEmpty);
400399
surface.saveToFile(fileName);
401-
}
400+
}
402401
//######################################################################################################
403402
}

0 commit comments

Comments
 (0)