render_svg.hpp 8.38 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
#define MARGIN 10        //distance between two object
12 13 14
#define ARROWSIZE 2      //spike size
#define WIDE 20
#define MARGINSTATE 0.5
15

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

Pascal Noisette's avatar
Pascal Noisette committed
20 21


22 23 24 25
#include "render.hpp"
#include "resource.hpp"


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

29
class Svg : public Render{
30 31


32
private:
33
    std::ostringstream _buffer;
34
    std::ostringstream _thin_element_buffer;
35
    std::ostringstream _chronogramme;
Pascal Noisette's avatar
Pascal Noisette committed
36
    int _chronogramme_state;
37
    std::ofstream _svg_file;
Pascal Noisette's avatar
Pascal Noisette committed
38 39
    Element_pos container_width_max;
  Element_pos container_height_min;
40
  int debug_counter;
Pascal Noisette's avatar
Pascal Noisette committed
41

Pascal Noisette's avatar
Pascal Noisette committed
42
    inline void print();
Pascal Noisette's avatar
Pascal Noisette committed
43 44 45
  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);
46 47
public:
    /*!
48 49
     * \brief SVG header buiding
     */
50
    void init(const char *path);
51

52
    /*!
53 54
     * \brief SVG bottom file buiding
     */
55
    void end();
56

57 58
    void start_draw();
    void start_draw_containers();
59

60
    /*!
61 62 63 64 65 66
     * \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
     */
67 68
    void draw_container(const Element_pos x, const Element_pos y, const Element_pos w, const Element_pos h) ;

69 70 71 72 73
    void draw_container_text(const Element_pos x, const Element_pos y, const std::string value);

    void end_draw_containers();
    void start_draw_states();

74 75 76 77 78 79 80 81 82
    /*!
     * \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.
     */
83
    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
84 85 86 87 88 89 90

    /*!
    * \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);

91 92 93 94
    void end_draw_states();

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

95 96
    void start_draw_counter();
    
97
  void draw_counter(const Element_pos x, const Element_pos y);
98 99 100
    
    void end_draw_counter();

101
    void end_draw();
102 103
};

104 105 106 107 108 109 110 111 112 113 114 115 116 117


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


inline void Svg::start_draw(){
118
 
119 120 121
}

inline void Svg::draw_container_text(const Element_pos x, const Element_pos y, const std::string value){
Pascal Noisette's avatar
Pascal Noisette committed
122 123 124 125 126 127 128 129 130 131 132
  
  Element_pos xprime,yprime;
  
  yprime = LEVEL*y;
  xprime = LEVEL*x;

  _thin_element_buffer << "<text x='" << xprime <<"' y='" << yprime << "'> ";
  _thin_element_buffer << value;
  _thin_element_buffer << "</text>";

} 
133 134

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

136 137 138 139 140 141 142 143 144 145 146
}

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
147

148 149 150 151 152
/********************
 * Counter
 *******************/

inline void Svg::start_draw_counter(){
Pascal Noisette's avatar
Pascal Noisette committed
153
  _chronogramme_state=INIT;
154 155 156
}

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

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

Pascal Noisette's avatar
Pascal Noisette committed
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 189 190 191 192 193 194 195 196 197 198 199 200 201 202
  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";
	  

    }
  

203 204 205
}

inline void Svg::end_draw_counter(){
Pascal Noisette's avatar
Pascal Noisette committed
206 207
  if (_chronogramme_state!=WAIT_NEW_CHRONO)
    std::cerr<<"draw_counter not un-initialised";
208 209
}

210 211

inline void Svg::end_draw(){
Olivier Lagrasse's avatar
Olivier Lagrasse committed
212
    Svg::end();
213 214
}

Pascal Noisette's avatar
Pascal Noisette committed
215
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 ){
216
  _buffer << "\n<rect class='"<<name<<"' title='"<<name<<"' width='" << w
Pascal Noisette's avatar
Pascal Noisette committed
217 218 219 220
            <<"' height='"<< h
            <<"' x='"     << x1
            <<"' y='"     << y1
            <<"' fill='rgb("<<r<<","<<g<<","<<b
Pascal Noisette's avatar
Pascal Noisette committed
221
            <<")'/>";
Pascal Noisette's avatar
Pascal Noisette committed
222 223 224 225 226 227 228
    print();
}





Pascal Noisette's avatar
Pascal Noisette committed
229
void Svg::triangle(const char* name,Element_pos x1,Element_pos y1,Element_pos x2,Element_pos y2, Element_pos x3,Element_pos y3){
230
   _thin_element_buffer << "\n<polyline title='"<<name<<"' class='triangle' points='"
Pascal Noisette's avatar
Pascal Noisette committed
231 232 233
            <<        x1 << "," << y1
            << " " << x2 << "," << y2
            << " " << x3 << "," << y3
Pascal Noisette's avatar
Pascal Noisette committed
234
            <<"' />";
235
    
Pascal Noisette's avatar
Pascal Noisette committed
236 237 238
}

void Svg::print(){
Olivier Lagrasse's avatar
Olivier Lagrasse committed
239
    // if (_buffer.str().size()>BUFFER_SIZE){
Pascal Noisette's avatar
Pascal Noisette committed
240
        _svg_file.write(_buffer.str().c_str(),  _buffer.str().size());
Pascal Noisette's avatar
Pascal Noisette committed
241
	_buffer.str("");
Pascal Noisette's avatar
Pascal Noisette committed
242 243 244 245 246
}




Pascal Noisette's avatar
Pascal Noisette committed
247
inline void Svg::line(const char* name, Element_pos x1, Element_pos y1, Element_pos x2, Element_pos y2){
248
   _thin_element_buffer << "\n<line title='"<<name<<"' x1='" << x1
Pascal Noisette's avatar
Pascal Noisette committed
249 250 251 252 253 254 255 256
            <<"' 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
257 258 259

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

260 261 262 263 264 265 266
  Element_pos x1=(start_time )*WIDE +  container_width_max ;
  Element_pos y1=start_height*LEVEL;
  Element_pos x2=(end_time)*WIDE+  container_width_max;
  Element_pos y2=end_height*LEVEL;


  Svg::line("arrow",x1 ,y1 ,x2,y2);
Pascal Noisette's avatar
Pascal Noisette committed
267

268 269
  Svg::triangle("arrow",x2 ,y2+ARROWSIZE,x2 ,y2-ARROWSIZE,x2+ARROWSIZE,y2);//spike
  
Pascal Noisette's avatar
Pascal Noisette committed
270 271 272
}

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
273 274 275 276 277 278 279 280 281 282 283 284 285 286

  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
287 288 289 290 291
}



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){
292
  
Pascal Noisette's avatar
Pascal Noisette committed
293

294
  
Pascal Noisette's avatar
Pascal Noisette committed
295

296 297 298 299 300 301 302 303 304 305 306 307
  Element_pos w =end*WIDE-start*WIDE;
  Element_pos h = container_height_min;
  Element_pos x = (start )*WIDE+  container_width_max + MARGINSTATE;
  Element_pos y = level*LEVEL;

  if(w==0){
    //std::cerr<<"state width is null";
      return;
  }
  if( debug_counter++<1000)
    std::cout<<"s.draw_state("<<start<<","<<end<<","<<level<<","<< height<<","<<r<<","<<g<<","<< b  <<");\n";
 
Pascal Noisette's avatar
Pascal Noisette committed
308
  Svg::rectangle("state",w,h, x,y,r*255 , g*255,  b*255);
Pascal Noisette's avatar
Pascal Noisette committed
309 310 311
}

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
312 313 314

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

315
  Svg::rectangle("event",MARGIN,container_height, time +  container_width_max , height, 0x17, 0x60, 0xe7);
Pascal Noisette's avatar
Pascal Noisette committed
316
}
317

318
#endif // RENDER_SVG