dgtalboard-4-colormaps.cpp

This example shows you how to use colormaps with DGtalBoard streams. Note how we redefine the style of a specific mode for the drawable element.


#include <cmath>
#include <iostream>
#include "DGtal/base/Common.h"
#include "DGtal/io/DGtalBoard.h"
#include "DGtal/io/colormaps/GradientColorMap.h"
#include "DGtal/helpers/Shapes.h"
#include "DGtal/helpers/StdDefs.h"

using namespace std;
using namespace DGtal;
using namespace Z2i;


int main( int argc, char** argv )
{
  trace.beginBlock ( "Example dgtalboard-4-colormaps" );

  Point p1( -10, -7 );
  Point p2( 10, 7 );
  Domain domain( p1, p2 );
  Point c1( -5, -1 );
  Point c2( 5, 1 );
  DigitalSet shape_set( domain );
  Shapes<Domain>::addNorm1Ball( shape_set, c1, 5 );
  Shapes<Domain>::addNorm1Ball( shape_set, c2, 5 );
  shape_set.erase( c1 );
  shape_set.erase( c2 );

  // Creating colormap.
  GradientColorMap<int> cmap_grad( 0, 15 );
  cmap_grad.addColor( DGtalBoard::Color( 50, 50, 255 ) );
  cmap_grad.addColor( DGtalBoard::Color( 255, 0, 0 ) );
  cmap_grad.addColor( DGtalBoard::Color( 255, 255, 10 ) );

  // Creating board.
  DGtalBoard board;
  board << SetMode( domain.styleName(), "Paving" )
        << domain
        << SetMode( p1.styleName(), "Paving" );
  // This is the name of the style for a Point in mode "Paving".
  string specificStyle =  p1.styleName() + "/Paving";
  for ( DigitalSet::ConstIterator it = shape_set.begin();
        it != shape_set.end();
        ++it )
    {
      unsigned int d = (unsigned int) ceil( ( *it - c1 ).norm() );
      // specific color depending on the distance to point c1.
      board << CustomStyle( specificStyle,
                            new CustomColors( DGtalBoard::Color::Black,
                                              cmap_grad( d ) ) )
            << *it;
    }
  board.saveSVG( "dgtalboard-4-colormaps.svg");
  board.saveEPS( "dgtalboard-4-colormaps.eps");

  trace.endBlock();
  return 0;
}
//                                                                           //