render_svg.hpp 8.14 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 122 123 124
}

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
125

126 127 128 129 130 131 132 133 134 135 136
}

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
137

138 139 140 141 142
/********************
 * Counter
 *******************/

inline void Svg::start_draw_counter(){
Pascal Noisette's avatar
Pascal Noisette committed
143
  _chronogramme_state=INIT;
144 145 146
}

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

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

Pascal Noisette's avatar
Pascal Noisette committed
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 189 190 191 192
  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";
	  

    }
  

193 194 195
}

inline void Svg::end_draw_counter(){
Pascal Noisette's avatar
Pascal Noisette committed
196 197
  if (_chronogramme_state!=WAIT_NEW_CHRONO)
    std::cerr<<"draw_counter not un-initialised";
198 199
}

200 201

inline void Svg::end_draw(){
Olivier Lagrasse's avatar
Olivier Lagrasse committed
202
    Svg::end();
203 204
}

Pascal Noisette's avatar
Pascal Noisette committed
205
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 ){
206
  _buffer << "\n<rect class='"<<name<<"' title='"<<name<<"' width='" << w
Pascal Noisette's avatar
Pascal Noisette committed
207 208 209 210
            <<"' height='"<< h
            <<"' x='"     << x1
            <<"' y='"     << y1
            <<"' fill='rgb("<<r<<","<<g<<","<<b
Pascal Noisette's avatar
Pascal Noisette committed
211
            <<")'/>";
Pascal Noisette's avatar
Pascal Noisette committed
212 213 214 215 216 217 218
    print();
}





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

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




Pascal Noisette's avatar
Pascal Noisette committed
237
inline void Svg::line(const char* name, Element_pos x1, Element_pos y1, Element_pos x2, Element_pos y2){
238
   _thin_element_buffer << "\n<line title='"<<name<<"' x1='" << x1
Pascal Noisette's avatar
Pascal Noisette committed
239 240 241 242 243 244 245 246
            <<"' 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
247 248 249

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

250 251 252 253 254 255 256
  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
257

258 259
  Svg::triangle("arrow",x2 ,y2+ARROWSIZE,x2 ,y2-ARROWSIZE,x2+ARROWSIZE,y2);//spike
  
Pascal Noisette's avatar
Pascal Noisette committed
260 261 262
}

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
263 264 265 266 267 268 269 270 271 272 273 274 275 276

  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
277 278 279 280 281
}



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

284
  
Pascal Noisette's avatar
Pascal Noisette committed
285

286 287 288 289 290 291 292 293 294 295 296 297 298
  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";
 
  Svg::rectangle("state",w,h, x,y,r , g,  b);
Pascal Noisette's avatar
Pascal Noisette committed
299 300 301
}

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
302 303 304

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

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

308
#endif // RENDER_SVG