DASH  0.3.0
dash::tools::PatternVisualizer< PatternT > Class Template Reference

Take a generic pattern instance and visualize it as an SVG image. More...

#include <PatternVisualizer.h>

Public Member Functions

 PatternVisualizer (const PatternT &pat, std::string title="", std::string descr="")
 Constructs the Pattern Visualizer with a pattern instance. More...
 
 PatternVisualizer (const self_t &other)=delete
 
void set_description (const std::string &str)
 Sets a description for the pattern. More...
 
void set_title (const std::string &str)
 Sets the title displayed above the pattern. More...
 
void draw_pattern (std::ostream &os, bool blocked_display=false, std::array< index_t, PatternT::ndim()> coords={}, int dimx=1, int dimy=0)
 Outputs the pattern as a svg over the given output stream. More...
 
void draw_pane (std::ostream &os, bool blocked_display, const sizes &sz, std::array< index_t, PatternT::ndim()> coords, int dimx, int dimy)
 Draws a pane (svg group) containing axes, key, tiles/blocks For the non blocked display (tiles) the local blocks and the memory layout gets drawn, too. More...
 
void draw_axes (std::ostream &os, const sizes &sz, int dimx, int dimy, int offsx=0, int offsy=0)
 Draws the axes labeled with their dedicated dimension. More...
 
void draw_key (std::ostream &os, const sizes &sz, int offsx=0, int offsy=0)
 Draws a list of units with their matching color. More...
 
void draw_tiles (std::ostream &os, const sizes &sz, std::array< index_t, PatternT::ndim()> coords, int dimx, int dimy)
 Draws the seperate tiles of the pattern. More...
 
void draw_blocks (std::ostream &os, const sizes &sz, int dimx, int dimy)
 Draws the blocks of the pattern. More...
 
void draw_local_blocks (std::ostream &os, const sizes &sz, std::array< index_t, PatternT::ndim()> coords, int dimx, int dimy)
 Draws the local blocks of the current unit (usually unit 0) More...
 
void draw_local_memlayout (std::ostream &os, const sizes &sz, int dimx, int dimy)
 Draws the memory layout for the current unit (usually unit 0) More...
 

Detailed Description

template<typename PatternT>
class dash::tools::PatternVisualizer< PatternT >

Take a generic pattern instance and visualize it as an SVG image.

The visualization is limited to two dimensions at the moment, but for higher-dimensional patterns any two dimensions can be specified for visualization.

Examples:
ex.06.pattern-block-visualizer/main.cpp.

Definition at line 30 of file PatternVisualizer.h.

Constructor & Destructor Documentation

◆ PatternVisualizer()

template<typename PatternT>
dash::tools::PatternVisualizer< PatternT >::PatternVisualizer ( const PatternT &  pat,
std::string  title = "",
std::string  descr = "" 
)
inline

Constructs the Pattern Visualizer with a pattern instance.

The pattern instance is constant. For a different pattern a new PatternVisualizer has to be constructed.

Parameters
titleAn optional title
descrAn optional describtion, currently not used

Definition at line 83 of file PatternVisualizer.h.

89  : _pattern(pat)
90  , _title(std::move(title))
91  , _descr(std::move(descr))
92  {
93  _tile_base_size = 10;
94  _block_base_size = 26;
95  _fontsz_tiny = 9;
96  _fontsz = 10;
97  _fontsz_title = 12;
98  }

Member Function Documentation

◆ draw_axes()

template<typename PatternT>
void dash::tools::PatternVisualizer< PatternT >::draw_axes ( std::ostream &  os,
const sizes &  sz,
int  dimx,
int  dimy,
int  offsx = 0,
int  offsy = 0 
)
inline

Draws the axes labeled with their dedicated dimension.

Definition at line 208 of file PatternVisualizer.h.

Referenced by dash::tools::PatternVisualizer< PatternT >::draw_pane().

211  {
212 
213  int startx = offsx;
214  int starty = offsy;
215  int lenx = sz.grid_width * sz.gridx + sz.grid_base;
216  int leny = sz.grid_height * sz.gridy + sz.grid_base;
217 
218  os << "<defs>\n";
219  os << R"(<marker id="arrowhead" orient="auto")";
220  os << R"( markerWidth="6" markerHeight="6")";
221  os << R"( refX="0" refY="0")";
222  os << " viewBox=\"-10 -15 30 30\">\n";
223  os << "<path d=\"";
224  os << "M " << -10 << " " << -15 << " ";
225  os << "L " << 20 << " " << 0 << " ";
226  os << "L " << -10 << " " << 15 << " ";
227  os << "L " << 0 << " " << 0 << " ";
228  os << "z ";
229  os << "\"";
230  os << " style=\"fill:#808080;stroke:#808080;stroke-width:1\"";
231  os << "/>";
232  os << "</marker>\n";
233  os << "</defs>\n";
234 
235  os << "<path d=\"";
236  os << "M " << startx << " " << starty << " ";
237  os << "h " << lenx << " ";
238  os << "\"";
239  os << " style=\"fill:none;stroke:#808080;stroke-width:1\"";
240  os << " marker-end=\"url(#arrowhead)\"";
241  os << "/>";
242 
243  os << "<text x=\"" << startx + lenx/3 << "\" y=\"" << starty - _fontsz/2 << "\" ";
244  os << R"( fill="grey" font-size=")" << _fontsz << "\" >";
245  os << "Dimension " << dimx << std::endl;
246  os << "</text>" << std::endl;
247 
248  os << "<path d=\"";
249  os << "M " << startx << " " << starty << " ";
250  os << "v " << leny << " ";
251  os << "\"";
252  os << " style=\"fill:none;stroke:#808080;stroke-width:1\"";
253  os << " marker-end=\"url(#arrowhead)\"";
254  os << "/>";
255 
256  os << "<text x=\"" << startx - _fontsz/2 << "\" y=\"" << starty + leny/3 << "\" ";
257  os << " transform=\"rotate(-90," << startx - _fontsz/2 << "," << starty + leny/3 << ")\" ";
258  os << R"( fill="grey" font-size=")" << _fontsz << "\" >";
259  os << "Dimension " << dimy << std::endl;
260  os << "</text>" << std::endl;
261  }

◆ draw_blocks()

template<typename PatternT>
void dash::tools::PatternVisualizer< PatternT >::draw_blocks ( std::ostream &  os,
const sizes &  sz,
int  dimx,
int  dimy 
)
inline

Draws the blocks of the pattern.

Definition at line 326 of file PatternVisualizer.h.

References dash::block().

Referenced by dash::tools::PatternVisualizer< PatternT >::draw_pane().

328  {
329  std::array<index_t, PatternT::ndim()> block_coords;
330  std::array<index_t, PatternT::ndim()> block_begin_coords;
331  auto blockspec = _pattern.blockspec();
332  for (int i = 0; i < blockspec.extent(dimx); i++) {
333  for (int j = 0; j < blockspec.extent(dimy); j++) {
334  block_coords[dimx] = i;
335  block_coords[dimy] = j;
336  auto block_idx = _pattern.blockspec().at(block_coords);
337  auto block = _pattern.block(block_idx);
338  block_begin_coords[dimx] = block.offset(dimx);
339  block_begin_coords[dimy] = block.offset(dimy);
340  auto unit = _pattern.unit_at(block_begin_coords);
341 
342  int i_grid = i * sz.gridx;
343  int j_grid = j * sz.gridy;
344 
345  os << "<rect x=\"" << i_grid << "\" y=\"" << j_grid << "\" ";
346  os << "height=\"" << sz.blockszy << "\" width=\"" << sz.blockszx << "\" ";
347  os << tilestyle(unit);
348  os << "> ";
349  os << "<!-- i=" << i << " j=" << j << "--> ";
350  os << "</rect>" << std::endl;
351  }
352  }
353  }
constexpr auto block(OffsetT block_idx, const ViewType &view) -> typename std::enable_if<(!dash::view_traits< ViewType >::is_local::value), ViewBlockMod< ViewType > >::type
Blocks view from global view.
Definition: Chunked.h:49

◆ draw_key()

template<typename PatternT>
void dash::tools::PatternVisualizer< PatternT >::draw_key ( std::ostream &  os,
const sizes &  sz,
int  offsx = 0,
int  offsy = 0 
)
inline

Draws a list of units with their matching color.

Definition at line 266 of file PatternVisualizer.h.

Referenced by dash::tools::PatternVisualizer< PatternT >::draw_pane().

268  {
269  int startx, starty;
270 
271  startx = offsx;
272  for (int unit = 0; unit < _pattern.num_units(); unit++) {
273  startx = offsx;
274  starty = offsy + (unit * (sz.tileszy + 2));
275  os << "<rect x=\"" << startx << "\" y=\"" << starty << "\" ";
276  os << "height=\"" << sz.tileszy << "\" width=\"" << sz.tileszx << "\" ";
277  os << tilestyle(unit);
278  os << "> ";
279  os << "</rect>" << std::endl;
280 
281  starty += sz.tileszy - 2;
282  startx += sz.tileszx + 1;
283  os << "<text x=\"" << startx << "\" y=\"" << starty << "\" ";
284  os << R"( fill="grey" font-size=")" << _fontsz << "\"";
285  os << " >";
286  os << "Unit " << unit << std::endl;
287  os << "</text>" << std::endl;
288  }
289  }

◆ draw_local_blocks()

template<typename PatternT>
void dash::tools::PatternVisualizer< PatternT >::draw_local_blocks ( std::ostream &  os,
const sizes &  sz,
std::array< index_t, PatternT::ndim()>  coords,
int  dimx,
int  dimy 
)
inline

Draws the local blocks of the current unit (usually unit 0)

Definition at line 358 of file PatternVisualizer.h.

References dash::block().

Referenced by dash::tools::PatternVisualizer< PatternT >::draw_pane().

361  {
362 
363  std::array<index_t, PatternT::ndim()> block_coords = coords;
364  std::array<index_t, PatternT::ndim()> block_begin_coords = coords;
365 
366  auto blockspec = _pattern.blockspec();
367 
368  for (int i = 0; i < blockspec.extent(dimx); i++) {
369  for (int j = 0; j < blockspec.extent(dimy); j++) {
370 
371  block_coords[dimx] = i;
372  block_coords[dimy] = j;
373  auto block_idx = _pattern.blockspec().at(block_coords);
374  auto block = _pattern.block(block_idx);
375 
376  block_begin_coords[dimx] = block.offset(dimx);
377  block_begin_coords[dimy] = block.offset(dimy);
378  auto unit = _pattern.unit_at(block_begin_coords);
379 
380  if( unit==0 ) {
381  int i_grid = block.offset(dimx)*sz.gridx - 1;
382  int j_grid = block.offset(dimy)*sz.gridy - 1;
383 
384  int width = (block.extent(dimx)-1)*sz.gridx + sz.tileszx + 2;
385  int height = (block.extent(dimy)-1)*sz.gridy + sz.tileszy + 2;
386 
387  os << "<rect x=\"" << i_grid << "\" y=\"" << j_grid << "\" ";
388  os << "height=\"" << height << "\" width=\"" << width << "\" ";
389  os << "style=\"fill:#999999;stroke-width:0\" >";
390  os << "</rect>" << std::endl;
391  }
392  }
393  }
394  }
constexpr auto block(OffsetT block_idx, const ViewType &view) -> typename std::enable_if<(!dash::view_traits< ViewType >::is_local::value), ViewBlockMod< ViewType > >::type
Blocks view from global view.
Definition: Chunked.h:49

◆ draw_local_memlayout()

template<typename PatternT>
void dash::tools::PatternVisualizer< PatternT >::draw_local_memlayout ( std::ostream &  os,
const sizes &  sz,
int  dimx,
int  dimy 
)
inline

Draws the memory layout for the current unit (usually unit 0)

Definition at line 399 of file PatternVisualizer.h.

Referenced by dash::tools::PatternVisualizer< PatternT >::draw_pane().

401  {
402  int startx, starty;
403  int endx, endy;
404 
405  startx = starty = 0;
406  for ( auto offset = 0; offset < _pattern.local_size(); offset++ ) {
407  auto coords = _pattern.coords(_pattern.global(offset));
408 
409  endx = (coords[dimx] * sz.gridx) + sz.tileszx / 2;
410  endy = (coords[dimy] * sz.gridy) + sz.tileszy / 2;
411 
412  if ( startx > 0 && starty > 0 ) {
413  os << "<line x1=\"" << startx << "\" y1=\"" << starty << "\"";
414  os << " x2=\"" << endx << "\" y2=\"" << endy << "\"";
415  os << " style=\"stroke:#E0E0E0;stroke-width:1\"/>";
416  os << " <!-- (" << offset << ") -->";
417  os << std::endl;
418  }
419 
420  os << "<circle cx=\"" << endx << "\" cy=\"" << endy << R"(" r="1.5" )";
421  os << " style=\"stroke:#E0E0E0;stroke-width:1;fill:#E0E0E0\" />";
422  os << std::endl;
423 
424  startx = endx;
425  starty = endy;
426  }
427 
428  }

◆ draw_pane()

template<typename PatternT>
void dash::tools::PatternVisualizer< PatternT >::draw_pane ( std::ostream &  os,
bool  blocked_display,
const sizes &  sz,
std::array< index_t, PatternT::ndim()>  coords,
int  dimx,
int  dimy 
)
inline

Draws a pane (svg group) containing axes, key, tiles/blocks For the non blocked display (tiles) the local blocks and the memory layout gets drawn, too.

Definition at line 184 of file PatternVisualizer.h.

References dash::tools::PatternVisualizer< PatternT >::draw_axes(), dash::tools::PatternVisualizer< PatternT >::draw_blocks(), dash::tools::PatternVisualizer< PatternT >::draw_key(), dash::tools::PatternVisualizer< PatternT >::draw_local_blocks(), dash::tools::PatternVisualizer< PatternT >::draw_local_memlayout(), and dash::tools::PatternVisualizer< PatternT >::draw_tiles().

Referenced by dash::tools::PatternVisualizer< PatternT >::draw_pattern().

187  {
188  os << "<g transform=\"translate(10,10)\">" << std::endl;
189  draw_axes(os, sz, dimx, dimy);
190 
191  os << "<g transform=\"translate(4,4)\">" << std::endl;
192  if(!blocked_display) {
193  draw_local_blocks(os, sz, coords, dimx, dimy);
194  draw_tiles(os, sz, coords, dimx, dimy);
195  draw_local_memlayout(os, sz, dimx, dimy);
196  } else {
197  draw_blocks(os, sz, dimx, dimy);
198  }
199  os << "</g>" << std::endl;
200 
201  draw_key(os, sz, sz.grid_width * sz.gridx + 2*sz.grid_base, 0);
202  os << "</g>" << std::endl;
203  }
void draw_axes(std::ostream &os, const sizes &sz, int dimx, int dimy, int offsx=0, int offsy=0)
Draws the axes labeled with their dedicated dimension.
void draw_tiles(std::ostream &os, const sizes &sz, std::array< index_t, PatternT::ndim()> coords, int dimx, int dimy)
Draws the seperate tiles of the pattern.
void draw_local_blocks(std::ostream &os, const sizes &sz, std::array< index_t, PatternT::ndim()> coords, int dimx, int dimy)
Draws the local blocks of the current unit (usually unit 0)
void draw_key(std::ostream &os, const sizes &sz, int offsx=0, int offsy=0)
Draws a list of units with their matching color.
void draw_local_memlayout(std::ostream &os, const sizes &sz, int dimx, int dimy)
Draws the memory layout for the current unit (usually unit 0)
void draw_blocks(std::ostream &os, const sizes &sz, int dimx, int dimy)
Draws the blocks of the pattern.

◆ draw_pattern()

template<typename PatternT>
void dash::tools::PatternVisualizer< PatternT >::draw_pattern ( std::ostream &  os,
bool  blocked_display = false,
std::array< index_t, PatternT::ndim()>  coords = {},
int  dimx = 1,
int  dimy = 0 
)
inline

Outputs the pattern as a svg over the given output stream.

This method should only be called by a single unit.

Parameters
blocked_displayIf this option is false every tile of the pattern is displayed seperate. If this option is true only the blocks (group of elements) get displayed.
coordsFor higher dimensional patterns, defines which slice gets displayed
dimxDefines which dimension gets displayed in x-direction
dimyDefines which dimension gets displayed in y-direction

Definition at line 121 of file PatternVisualizer.h.

References dash::tools::PatternVisualizer< PatternT >::draw_pane().

127  {},
129  int dimx = 1,
131  int dimy = 0) {
132  sizes sz;
133  sz.tileszx = sz.tileszy = _tile_base_size;
134  sz.blockszx = sz.blockszy = _block_base_size;
135  if(!blocked_display) {
136  sz.grid_width = _pattern.extent(dimx);
137  sz.grid_height = _pattern.extent(dimy);
138  sz.grid_base = _tile_base_size + 2;
139  sz.gridx = sz.gridy = sz.grid_base;
140  } else {
141  sz.grid_width = _pattern.blockspec().extent(dimx);
142  sz.grid_height = _pattern.blockspec().extent(dimy);
143  sz.grid_base = _block_base_size + 2;
144 
145  // Adjust tile sizes proportional to block regions:
146  float block_format = static_cast<float>(_pattern.blocksize(dimy)) /
147  static_cast<float>(_pattern.blocksize(dimx));
148  if (block_format < 1) {
149  block_format = 1.0 / block_format;
150  sz.blockszx *= block_format;
151  } else {
152  sz.blockszy *= block_format;
153  }
154  sz.gridx = sz.blockszx + 2;
155  sz.gridy = sz.blockszy + 2;
156  }
157 
158  std::string title = _title;
159  replace_all(title, "<", "&lt;");
160  replace_all(title, ">", "&gt;");
161 
162  os << "<svg xmlns=\"http://www.w3.org/2000/svg\"";
163  os << " xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n";
164 
165  // typeset title line
166  os << R"(<text x="10" y="15" )";
167  os << R"( fill="grey" font-size=")" << _fontsz_title << "\">";
168  os << title;
169  os << "</text>\n";
170 
171  // draw the pane, including axes and key
172  os << "<g transform=\"translate(10,30)\">\n";
173  draw_pane(os, blocked_display, sz, coords, dimx, dimy);
174  os << "</g>\n";
175 
176  os << "</svg>" << std::endl;
177  }
void draw_pane(std::ostream &os, bool blocked_display, const sizes &sz, std::array< index_t, PatternT::ndim()> coords, int dimx, int dimy)
Draws a pane (svg group) containing axes, key, tiles/blocks For the non blocked display (tiles) the l...

◆ draw_tiles()

template<typename PatternT>
void dash::tools::PatternVisualizer< PatternT >::draw_tiles ( std::ostream &  os,
const sizes &  sz,
std::array< index_t, PatternT::ndim()>  coords,
int  dimx,
int  dimy 
)
inline

Draws the seperate tiles of the pattern.

Definition at line 294 of file PatternVisualizer.h.

Referenced by dash::tools::PatternVisualizer< PatternT >::draw_pane().

297  {
298  for (int i = 0; i < _pattern.extent(dimx); i++) {
299  for (int j = 0; j < _pattern.extent(dimy); j++) {
300 
301  os << "<rect x=\"" << (i * sz.gridx) << "\" y=\"" << (j * sz.gridy) << "\" ";
302  os << "height=\"" << sz.tileszy << "\" width=\"" << sz.tileszx << "\" ";
303 
304  coords[dimx] = i;
305  coords[dimy] = j;
306 
307  auto unit = _pattern.unit_at(coords);
308  auto loffs = _pattern.at(coords);
309 
310  os << tilestyle(unit);
311  os << " tooltip=\"enable\" > ";
312  os << " <title>Elem: (" << j << "," << i <<"),";
313  os << " Unit " << unit;
314  os << " Local offs. " << loffs;
315  os << "</title>";
316 
317  //os << "<!-- i=" << i << " j=" << j << "--> ";
318  os << "</rect>" << std::endl;
319  }
320  }
321  }

◆ set_description()

template<typename PatternT>
void dash::tools::PatternVisualizer< PatternT >::set_description ( const std::string &  str)
inline

Sets a description for the pattern.

Currently not used.

Definition at line 107 of file PatternVisualizer.h.

107  {
108  _descr = str;
109  }

◆ set_title()

template<typename PatternT>
void dash::tools::PatternVisualizer< PatternT >::set_title ( const std::string &  str)
inline

Sets the title displayed above the pattern.

Definition at line 113 of file PatternVisualizer.h.

113  {
114  _title = str;
115  }

The documentation for this class was generated from the following file: