render_svg.hpp 7.76 KB
Newer Older
1
#ifndef RENDER_SVG
2
#define RENDER_SVG
3 4


Pascal Noisette's avatar
Pascal Noisette committed
5 6 7
#include <iostream>
#include <sstream>
#include <fstream>
8

Olivier Lagrasse's avatar
Olivier Lagrasse committed
9
#define BUFFER_SIZE 10 //character number stored before flush the _buffer
Pascal Noisette's avatar
Pascal Noisette committed
10
#define LEVEL 20        //distance between two containers
Pascal Noisette's avatar
Pascal Noisette committed
11 12
#define MARGIN 10        //distance between two object
#define ARROWSIZE 4      //spike size
13

Pascal Noisette's avatar
Pascal Noisette committed
14 15 16
#define WAIT_NEW_CHRONO 0
#define INIT 1
#define WAIT_FOR_POINT 2
17

Pascal Noisette's avatar
Pascal Noisette committed
18 19


20 21 22 23
#include "render.hpp"
#include "resource.hpp"


Pascal Noisette's avatar
Pascal Noisette committed
24 25
#define max(a,b) (a>b?a:b);
#define min(a,b) (a>b?b:a);
26

27
class Svg : public Render{
28 29


30
private:
31
    std::ostringstream _buffer;
32
    std::ostringstream _chronogramme;
Pascal Noisette's avatar
Pascal Noisette committed
33
    int _chronogramme_state;
34
    std::ofstream _svg_file;
Pascal Noisette's avatar
Pascal Noisette committed
35 36 37
    Element_pos container_width_max;
  Element_pos container_height_min;

Pascal Noisette's avatar
Pascal Noisette committed
38
    inline void print();
Pascal Noisette's avatar
Pascal Noisette committed
39 40 41
  inline void rectangle(const char* name,Element_pos w, Element_pos h,Element_pos x1,Element_pos y1,  unsigned int r,   unsigned int g,   unsigned int b );
    inline void line(const char* name, Element_pos x1, Element_pos y1, Element_pos x2, Element_pos y2);
    inline void triangle(const char* name,Element_pos x1,Element_pos y1,Element_pos x2,Element_pos y2, Element_pos x3,Element_pos y3);
42 43
public:
    /*!
44 45
     * \brief SVG header buiding
     */
46
    void init(const char *path);
47

48
    /*!
49 50
     * \brief SVG bottom file buiding
     */
51
    void end();
52

53 54
    void start_draw();
    void start_draw_containers();
55

56
    /*!
57 58 59 60 61 62
     * \brief Draw a container according to the parameters
     * \param x the x position of the container
     * \param y the y position of the container
     * \param w the width of the container
     * \param h the height of the container
     */
63 64
    void draw_container(const Element_pos x, const Element_pos y, const Element_pos w, const Element_pos h) ;

65 66 67 68 69
    void draw_container_text(const Element_pos x, const Element_pos y, const std::string value);

    void end_draw_containers();
    void start_draw_states();

70 71 72 73 74 75 76 77 78
    /*!
     * \brief Draw a state of the trace.
     * \param r the red color rate of the state.
     * \param g the green color rate of the state.
     * \param b the blue color rate of the state.
     * \param start the beginning time of the state.
     * \param end the ending time of the state.
     * \param level refer to the container which state belongs to.
     */
79
    void draw_state(const Element_pos start , const Element_pos end, const Element_pos level, const Element_pos height, const Element_col r, const Element_col g, const Element_col b) ;
Pascal Noisette's avatar
Pascal Noisette committed
80 81 82 83 84 85 86

    /*!
    * \brief Draw an arrow
    *
    */
    void draw_arrow(const Element_pos start_time, const Element_pos end_time, const Element_pos start_height, const Element_pos end_height);

87 88 89 90
    void end_draw_states();

    void draw_event(const Element_pos time, const Element_pos height, const Element_pos container_height);

91 92
    void start_draw_counter();
    
93
  void draw_counter(const Element_pos x, const Element_pos y);
94 95 96
    
    void end_draw_counter();

97
    void end_draw();
98 99
};

100 101 102 103 104 105 106 107 108 109 110 111 112 113


/***********************************
 *
 *
 *
 * Drawing function for the trace.
 *
 *
 *
 **********************************/


inline void Svg::start_draw(){
114
 
115 116 117 118 119 120
}

inline void Svg::draw_container_text(const Element_pos x, const Element_pos y, const std::string value){
}

inline void Svg::start_draw_containers(){
Pascal Noisette's avatar
Pascal Noisette committed
121

122 123 124 125 126 127 128 129 130 131 132
}

inline void Svg::end_draw_containers(){
}

inline void Svg::start_draw_states(){
}

inline void Svg::end_draw_states(){
}

Pascal Noisette's avatar
Pascal Noisette committed
133

134 135 136 137 138
/********************
 * Counter
 *******************/

inline void Svg::start_draw_counter(){
Pascal Noisette's avatar
Pascal Noisette committed
139
  _chronogramme_state=INIT;
140 141 142
}

inline void Svg::draw_counter(const Element_pos x, const Element_pos y){
Pascal Noisette's avatar
Pascal Noisette committed
143

Pascal Noisette's avatar
Pascal Noisette committed
144 145
std::cout<<"s.draw_counter("<<x<<","<<y<<");\n";

Pascal Noisette's avatar
Pascal Noisette committed
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
  if (_chronogramme_state==INIT)
    {
      if (x==0)
	{
	  _chronogramme << "\n<path d='M0 "<< y;
	  _chronogramme_state=WAIT_FOR_POINT;
	}
      else
	std::cerr<<"draw_counter not initialised";
	  
    }
  else  if (_chronogramme_state==WAIT_FOR_POINT)
    
    {
      
      if (x==0)
	{
	  _chronogramme << "'/>";
	  _svg_file.write(_chronogramme.str().c_str(), _chronogramme.str().size());
	  _chronogramme.str("");
	  _chronogramme_state=WAIT_NEW_CHRONO;
	}
      else
	{
	  _chronogramme << " L" << x << " " << y;
	}
      
    }
  
  else  if (_chronogramme_state==WAIT_NEW_CHRONO)
    {
        if (x==0)
	{
	  _chronogramme << "\n<path d='M0 "<< y;
	  _chronogramme_state=WAIT_FOR_POINT;
	}
      else
	std::cerr<<"draw_counter not initialised";
	  

    }
  

189 190 191
}

inline void Svg::end_draw_counter(){
Pascal Noisette's avatar
Pascal Noisette committed
192 193
  if (_chronogramme_state!=WAIT_NEW_CHRONO)
    std::cerr<<"draw_counter not un-initialised";
194 195
}

196 197

inline void Svg::end_draw(){
Olivier Lagrasse's avatar
Olivier Lagrasse committed
198
    Svg::end();
199 200
}

Pascal Noisette's avatar
Pascal Noisette committed
201
void Svg::rectangle(const char* name,Element_pos w, Element_pos h,Element_pos x1,Element_pos y1,  unsigned int r,   unsigned int g,   unsigned int b ){
Pascal Noisette's avatar
Pascal Noisette committed
202
    _buffer << "\n<rect title='"<<name<<"' width='" << w
Pascal Noisette's avatar
Pascal Noisette committed
203 204 205 206
            <<"' height='"<< h
            <<"' x='"     << x1
            <<"' y='"     << y1
            <<"' fill='rgb("<<r<<","<<g<<","<<b
Pascal Noisette's avatar
Pascal Noisette committed
207
            <<")'/>";
Pascal Noisette's avatar
Pascal Noisette committed
208 209 210 211 212 213 214
    print();
}





Pascal Noisette's avatar
Pascal Noisette committed
215
void Svg::triangle(const char* name,Element_pos x1,Element_pos y1,Element_pos x2,Element_pos y2, Element_pos x3,Element_pos y3){
Pascal Noisette's avatar
Pascal Noisette committed
216
    _buffer << "\n<polyline title='"<<name<<"' class='triangle' points='"
Pascal Noisette's avatar
Pascal Noisette committed
217 218 219
            <<        x1 << "," << y1
            << " " << x2 << "," << y2
            << " " << x3 << "," << y3
Pascal Noisette's avatar
Pascal Noisette committed
220
            <<"' />";
Pascal Noisette's avatar
Pascal Noisette committed
221 222 223 224
    print();
}

void Svg::print(){
Olivier Lagrasse's avatar
Olivier Lagrasse committed
225
    // if (_buffer.str().size()>BUFFER_SIZE){
Pascal Noisette's avatar
Pascal Noisette committed
226
        _svg_file.write(_buffer.str().c_str(),  _buffer.str().size());
Pascal Noisette's avatar
Pascal Noisette committed
227
	_buffer.str("");
Pascal Noisette's avatar
Pascal Noisette committed
228 229 230 231 232
}




Pascal Noisette's avatar
Pascal Noisette committed
233
inline void Svg::line(const char* name, Element_pos x1, Element_pos y1, Element_pos x2, Element_pos y2){
Pascal Noisette's avatar
Pascal Noisette committed
234
    _buffer << "\n<line title='"<<name<<"' x1='" << x1
Pascal Noisette's avatar
Pascal Noisette committed
235 236 237 238 239 240 241 242
            <<"' y1='"     << y1
            <<"' x2='"     << x2
            <<"' y2='"     << y2
            <<"' />";
    print();
}

inline void Svg::draw_arrow(const Element_pos start_time, const Element_pos end_time, const Element_pos start_height, const Element_pos end_height){
Pascal Noisette's avatar
Pascal Noisette committed
243 244 245 246 247 248

  std::cout<<"s.draw_arrow("<<start_time<<","<<end_time<<","<<start_height<<","<< end_height<<");\n";


  Svg::line("arrow",start_time+  container_width_max ,start_height,end_time+  container_width_max ,end_height);
  Svg::triangle("arrow",end_time+  container_width_max ,end_height+ARROWSIZE,end_time+  container_width_max ,end_height-ARROWSIZE,end_time+ARROWSIZE+  container_width_max ,end_height);//spike
Pascal Noisette's avatar
Pascal Noisette committed
249 250 251
}

inline void Svg::draw_container(const Element_pos x, const Element_pos y, const Element_pos w, const Element_pos h){
Pascal Noisette's avatar
Pascal Noisette committed
252 253 254 255 256 257 258 259 260 261 262 263 264 265

  Element_pos xprime,yprime,wprime,hprime;
  
  wprime = LEVEL*w;
  hprime = LEVEL*h;
  yprime = LEVEL*y;
  xprime = LEVEL*x;

  std::cout<<"s.draw_container("<<x<<","<<y<<","<<w<<","<< h<<");\n";

  Svg::rectangle("container",wprime, hprime, xprime, yprime, 0xff, 0x44, 0xcc);
  
  container_width_max = max(wprime+xprime+ MARGIN,container_width_max);
  container_height_min = min(hprime,container_height_min);
Pascal Noisette's avatar
Pascal Noisette committed
266 267 268 269 270
}



inline void Svg::draw_state(const Element_pos start , const Element_pos end, const Element_pos level, const Element_pos height, const Element_col r, const Element_col g, const Element_col b){
Pascal Noisette's avatar
Pascal Noisette committed
271 272 273 274

  std::cout<<"s.draw_state("<<start<<","<<end<<","<<level<<","<< height<<","<<r<<","<<g<<","<< b  <<");\n";

  Svg::rectangle("state",end-start, container_height_min, start +  container_width_max ,level*LEVEL,r , g,  b);
Pascal Noisette's avatar
Pascal Noisette committed
275 276 277
}

inline  void Svg::draw_event(const Element_pos time, const Element_pos height, const Element_pos container_height){
Pascal Noisette's avatar
Pascal Noisette committed
278 279 280 281

  std::cout<<"s.draw_event("<<time<<","<< height<<","<<container_height<<");\n";

  Svg::rectangle("event",MARGIN,container_height, time +  container_width_max , height, 0xff, 0xff, 0xff);
Pascal Noisette's avatar
Pascal Noisette committed
282
}
283

284
#endif // RENDER_SVG