Skip to content

Commit 5ee73f1

Browse files
committed
Added bracket type to draw function.
1 parent 8b5aac7 commit 5ee73f1

File tree

1 file changed

+116
-115
lines changed

1 file changed

+116
-115
lines changed

include/interval-tree/draw.hpp

Lines changed: 116 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -16,72 +16,73 @@ namespace lib_interval_tree
1616
{
1717
namespace
1818
{
19-
template <unsigned Size>
20-
struct NumericalPointerEquivalent
21-
{};
22-
23-
template <>
24-
struct NumericalPointerEquivalent <sizeof(std::uint32_t)>
25-
{
26-
using type = uint32_t;
27-
};
28-
29-
template <>
30-
struct NumericalPointerEquivalent <sizeof(std::uint64_t)>
31-
{
32-
using type = uint64_t;
33-
};
34-
35-
template <typename... List>
36-
std::string iterCaption(typename lib_interval_tree::interval_tree <List...>::const_iterator iter)
19+
template <unsigned Size>
20+
struct NumericalPointerEquivalent
21+
{};
22+
23+
template <>
24+
struct NumericalPointerEquivalent<sizeof(std::uint32_t)>
25+
{
26+
using type = uint32_t;
27+
};
28+
29+
template <>
30+
struct NumericalPointerEquivalent<sizeof(std::uint64_t)>
31+
{
32+
using type = uint64_t;
33+
};
34+
35+
template <typename IntervalT, typename... List>
36+
std::string iterCaption(typename lib_interval_tree::interval_tree<IntervalT, List...>::const_iterator iter)
3737
{
3838
auto ival = *iter.node()->interval();
3939
std::stringstream sstr;
40-
sstr << '[' << ival.low() << ',' << ival.high() << ']';
40+
char leftBorder = [&ival]() {
41+
if (ival.within(ival.low()))
42+
return '[';
43+
else
44+
return '(';
45+
}();
46+
char rightBorder = [&ival]() {
47+
if (ival.within(ival.high()))
48+
return ']';
49+
else
50+
return ')';
51+
}();
52+
sstr << leftBorder << ival.low() << ',' << ival.high() << rightBorder;
4153
return sstr.str();
4254
}
4355

4456
std::string pointerString(void const* ptr)
4557
{
4658
std::stringstream sstr;
47-
sstr << "0x" << std::hex << reinterpret_cast <NumericalPointerEquivalent<sizeof(void*)>::type> (ptr);
59+
sstr << "0x" << std::hex << reinterpret_cast<NumericalPointerEquivalent<sizeof(void*)>::type>(ptr);
4860
return sstr.str();
4961
}
5062

51-
constexpr double margin = 5.;
52-
constexpr double gridMargin = 5.;
53-
constexpr double yPadding = 0.;
54-
constexpr double xPadding = 30.;
55-
constexpr double leftPadding = 10.;
56-
constexpr double rightPadding = 10.;
57-
constexpr double topPadding = 10.;
58-
constexpr double bottomPadding = 10.;
59-
constexpr Cairo::Pen blackPen = {3., Cairo::Colors::Black};
60-
constexpr Cairo::Pen iterCaptionPen = {3., Cairo::Colors::Black};
61-
constexpr Cairo::Pen ptrPen = {3., Cairo::Colors::Red};
62-
constexpr Cairo::Pen edgePen = {8., Cairo::Colors::Black};
63-
constexpr auto whitePen = Cairo::Colors::White;
64-
65-
auto getiterBounds()
63+
constexpr double margin = 5.;
64+
constexpr double gridMargin = 5.;
65+
constexpr double yPadding = 0.;
66+
constexpr double xPadding = 30.;
67+
constexpr double leftPadding = 10.;
68+
constexpr double rightPadding = 10.;
69+
constexpr double topPadding = 10.;
70+
constexpr double bottomPadding = 10.;
71+
constexpr Cairo::Pen blackPen = {3., Cairo::Colors::Black};
72+
constexpr Cairo::Pen iterCaptionPen = {3., Cairo::Colors::Black};
73+
constexpr Cairo::Pen ptrPen = {3., Cairo::Colors::Red};
74+
constexpr Cairo::Pen edgePen = {8., Cairo::Colors::Black};
75+
constexpr auto whitePen = Cairo::Colors::White;
76+
77+
auto getiterBounds()
6678
{
6779
Cairo::Surface dummySurface(0, 0);
6880
Cairo::DrawContext dummyContext(&dummySurface);
6981

70-
auto const captionBoundProvider = Cairo::Text(
71-
&dummyContext,
72-
0,
73-
0,
74-
"[00,00]",
75-
{"Arial", 18, CAIRO_FONT_WEIGHT_BOLD}
76-
);
77-
78-
auto ptr = Cairo::Text(
79-
&dummyContext,
80-
0,
81-
0,
82-
pointerString(nullptr),
83-
{"Arial", 10}
84-
);
82+
auto const captionBoundProvider =
83+
Cairo::Text(&dummyContext, 0, 0, "[00,00]", {"Arial", 18, CAIRO_FONT_WEIGHT_BOLD});
84+
85+
auto ptr = Cairo::Text(&dummyContext, 0, 0, pointerString(nullptr), {"Arial", 10});
8586
auto ptrBounds = ptr.calculateBounds(ptrPen);
8687

8788
auto bounds = captionBoundProvider.calculateBounds(iterCaptionPen);
@@ -96,43 +97,42 @@ namespace lib_interval_tree
9697
return getiterBounds().getWidth() / 2. + margin * 2.;
9798
}
9899
}
99-
//#####################################################################################################################
100+
// #####################################################################################################################
100101
template <typename... List>
101102
struct TreeGriditer
102103
{
103-
typename lib_interval_tree::interval_tree <List...>::const_iterator iter;
104-
std::pair <int, int> parentCoords;
104+
typename lib_interval_tree::interval_tree<List...>::const_iterator iter;
105+
std::pair<int, int> parentCoords;
105106
};
106107
template <typename... List>
107108
struct TreeGrid
108109
{
109110
// (row-major)
110-
std::vector < // rows
111-
std::vector < // columns
112-
boost::optional <TreeGriditer<List...>>
113-
>
114-
> grid;
111+
std::vector< // rows
112+
std::vector< // columns
113+
boost::optional<TreeGriditer<List...>>>>
114+
grid;
115115
int xMax = 0;
116116
int xMin = 0;
117117
int yMax = 0;
118118
};
119-
//#####################################################################################################################
119+
// #####################################################################################################################
120120
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)
121+
void drawIterator(
122+
Cairo::DrawContext* ctx,
123+
typename lib_interval_tree::interval_tree<List...>::const_iterator iter,
124+
double x,
125+
double y,
126+
bool drawPointers
127+
)
122128
{
123-
auto caption = Cairo::Text(
124-
ctx,
125-
0,
126-
0,
127-
iterCaption<List...>(iter),
128-
{"Arial", 18, CAIRO_FONT_WEIGHT_BOLD}
129-
);
129+
auto caption = Cairo::Text(ctx, 0, 0, iterCaption<List...>(iter), {"Arial", 18, CAIRO_FONT_WEIGHT_BOLD});
130130

131131
auto max = Cairo::Text(
132132
ctx,
133133
0,
134134
0,
135-
//pointerString(iter),
135+
// pointerString(iter),
136136
std::to_string(iter.max()),
137137
{"Arial", 12}
138138
);
@@ -151,7 +151,7 @@ namespace lib_interval_tree
151151
auto iterCaptionBounds = getiterBounds();
152152
auto bounds = iterCaptionBounds;
153153
auto maxBounds = max.calculateBounds(ptrPen);
154-
//auto ptrBounds = ptr.calculateBounds(ptrPen);
154+
// auto ptrBounds = ptr.calculateBounds(ptrPen);
155155

156156
iterCaptionBounds.setWidth(bounds.getWidth() - 30.);
157157
iterCaptionBounds.setHeight(bounds.getWidth() - maxBounds.getHeight() - 5.);
@@ -166,37 +166,36 @@ namespace lib_interval_tree
166166
maxBounds = max.calculateBounds(ptrPen);
167167
}
168168

169-
Cairo::Arc circle {
170-
ctx,
171-
circleX,
172-
circleY,
173-
circleRadius
174-
};
169+
Cairo::Arc circle{ctx, circleX, circleY, circleRadius};
175170
switch (iter.node()->color())
176171
{
177-
case (rb_color::red):
178-
circle.draw(blackPen, Cairo::Colors::Red);
179-
break;
180-
case (rb_color::black):
181-
circle.draw(blackPen, Cairo::Colors::Black);
182-
break;
183-
case (rb_color::fail):
184-
circle.draw(blackPen, Cairo::Colors::White);
185-
break;
186-
case (rb_color::double_black):
187-
circle.draw(blackPen, Cairo::Colors::Gray);
188-
break;
172+
case (rb_color::red):
173+
circle.draw(blackPen, Cairo::Colors::Red);
174+
break;
175+
case (rb_color::black):
176+
circle.draw(blackPen, Cairo::Colors::Black);
177+
break;
178+
case (rb_color::fail):
179+
circle.draw(blackPen, Cairo::Colors::White);
180+
break;
181+
case (rb_color::double_black):
182+
circle.draw(blackPen, Cairo::Colors::Gray);
183+
break;
189184
}
190185

191-
caption.move(circleX - actualCaptionBounds.getWidth() / 2., circleY - actualCaptionBounds.getHeight() / 2. - maxBounds.getHeight());
186+
caption.move(
187+
circleX - actualCaptionBounds.getWidth() / 2.,
188+
circleY - actualCaptionBounds.getHeight() / 2. - maxBounds.getHeight()
189+
);
192190

193191
if (iter.node()->color() != rb_color::black)
194192
caption.draw(iterCaptionPen);
195193
else
196194
caption.draw(Cairo::Colors::White);
197195

198196
max.move(circleX - maxBounds.getWidth() / 2., circleY - maxBounds.getHeight() / 2. + 10.);
199-
//ptr.move(circleX - ptrBounds.getWidth() / 2., circleY - ptrBounds.getHeight() / 2. + 10. + maxBounds.getHeight() + margin);
197+
// ptr.move(circleX - ptrBounds.getWidth() / 2., circleY - ptrBounds.getHeight() / 2. + 10. +
198+
// maxBounds.getHeight() + margin);
200199

201200
if (iter.node()->color() != rb_color::red)
202201
max.draw(ptrPen);
@@ -213,17 +212,17 @@ namespace lib_interval_tree
213212
}
214213
*/
215214
}
216-
//---------------------------------------------------------------------------------------------------------------------
215+
//---------------------------------------------------------------------------------------------------------------------
217216
template <typename... List>
218-
TreeGrid<List...> createGrid(lib_interval_tree::interval_tree <List...> const& tree)
217+
TreeGrid<List...> createGrid(lib_interval_tree::interval_tree<List...> const& tree)
219218
{
220219
auto root = tree.root();
221220
if (root == std::end(tree))
222221
return {};
223222

224223
TreeGrid<List...> grid;
225224

226-
using tree_const_iterator = typename lib_interval_tree::interval_tree <List...>::const_iterator;
225+
using tree_const_iterator = typename lib_interval_tree::interval_tree<List...>::const_iterator;
227226

228227
struct GridPoint
229228
{
@@ -233,9 +232,9 @@ namespace lib_interval_tree
233232
int y;
234233
};
235234

236-
std::vector <GridPoint> gridPoints;
235+
std::vector<GridPoint> gridPoints;
237236

238-
std::function <int(tree_const_iterator iter)> subtreeSize;
237+
std::function<int(tree_const_iterator iter)> subtreeSize;
239238
subtreeSize = [&](tree_const_iterator iter) {
240239
if (iter == std::end(tree))
241240
return 0;
@@ -250,9 +249,8 @@ namespace lib_interval_tree
250249
return 0;
251250
};
252251

253-
std::function <void(tree_const_iterator, int pX, int pY)> deduceCoordinates;
254-
deduceCoordinates = [&](tree_const_iterator iter, int pX, int pY)
255-
{
252+
std::function<void(tree_const_iterator, int pX, int pY)> deduceCoordinates;
253+
deduceCoordinates = [&](tree_const_iterator iter, int pX, int pY) {
256254
int y = pY;
257255
int x = pX;
258256
if (!iter.node()->is_root())
@@ -298,7 +296,7 @@ namespace lib_interval_tree
298296

299297
for (auto& i : gridPoints)
300298
{
301-
std::pair <int, int> parentCoords = {-1, -1};
299+
std::pair<int, int> parentCoords = {-1, -1};
302300
for (auto const& j : gridPoints)
303301
{
304302
if (j.iter == i.parent)
@@ -313,15 +311,19 @@ namespace lib_interval_tree
313311

314312
return grid;
315313
}
316-
//---------------------------------------------------------------------------------------------------------------------
314+
//---------------------------------------------------------------------------------------------------------------------
317315
template <typename... List>
318316
void drawGrid(Cairo::DrawContext* ctx, TreeGrid<List...> const& grid, bool drawPointers, bool drawEmpty)
319317
{
320318
auto iterRadius = getiterRadius();
321319
auto cellSize = iterRadius * 2. + gridMargin;
322320

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

326328
// Draw Lines
327329
int y = 0;
@@ -332,7 +334,7 @@ namespace lib_interval_tree
332334
{
333335
if (cell && cell.get().parentCoords.first != -1)
334336
{
335-
auto line = Cairo::Line {
337+
auto line = Cairo::Line{
336338
ctx,
337339
iterX(x),
338340
iterY(y),
@@ -360,21 +362,15 @@ namespace lib_interval_tree
360362
}
361363
else if (drawEmpty)
362364
{
363-
Cairo::Arc circle {
364-
ctx,
365-
iterX(x),
366-
iterY(y),
367-
iterRadius
368-
};
365+
Cairo::Arc circle{ctx, iterX(x), iterY(y), iterRadius};
369366
circle.draw(blackPen, Cairo::Colors::White);
370-
371367
}
372368
++x;
373369
}
374370
++y;
375371
}
376372
}
377-
//---------------------------------------------------------------------------------------------------------------------
373+
//---------------------------------------------------------------------------------------------------------------------
378374
template <typename... List>
379375
Cairo::Surface createSurface(TreeGrid<List...> const& grid)
380376
{
@@ -384,19 +380,24 @@ namespace lib_interval_tree
384380
int height = grid.yMax + 1;
385381

386382
return {
387-
static_cast <int> (leftPadding + (width) * cellSize + (width-1) * xPadding + rightPadding),
388-
static_cast <int> (topPadding + (height) * cellSize + (height-1) * yPadding + bottomPadding)
383+
static_cast<int>(leftPadding + (width)*cellSize + (width - 1) * xPadding + rightPadding),
384+
static_cast<int>(topPadding + (height)*cellSize + (height - 1) * yPadding + bottomPadding)
389385
};
390386
}
391-
//---------------------------------------------------------------------------------------------------------------------
387+
//---------------------------------------------------------------------------------------------------------------------
392388
template <typename... List>
393-
void drawTree(std::string const& fileName, lib_interval_tree::interval_tree <List...> const& tree, bool drawPointers = false, bool drawEmpty = false)
389+
void drawTree(
390+
std::string const& fileName,
391+
lib_interval_tree::interval_tree<List...> const& tree,
392+
bool drawPointers = false,
393+
bool drawEmpty = false
394+
)
394395
{
395396
auto grid = createGrid(tree);
396397
auto surface = createSurface(grid);
397398
Cairo::DrawContext ctx(&surface);
398399
drawGrid<List...>(&ctx, grid, drawPointers, drawEmpty);
399400
surface.saveToFile(fileName);
400401
}
401-
//######################################################################################################
402+
// ######################################################################################################
402403
}

0 commit comments

Comments
 (0)