render_svg.hpp 5.95 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 110        //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 14


15 16 17 18 19 20 21 22
#include "render.hpp"
#include "resource.hpp"


/*typedef unsigned long Element_count ;
typedef double Element_pos;
typedef unsigned char Element_col;
*/
23

24
class Svg : public Render{
25 26


27
private:
28
    std::ostringstream _buffer;
29
    std::ostringstream _chronogramme;
30
    std::ofstream _svg_file;
Pascal Noisette's avatar
Pascal Noisette committed
31
    inline void print();
Pascal Noisette's avatar
Pascal Noisette committed
32 33 34
  inline void rectangle(const char* name,unsigned long w, unsigned long h,unsigned long x1,unsigned long y1,  unsigned int r,   unsigned int g,   unsigned int b );
    inline void line(const char* name, long unsigned int x1, long unsigned int y1, long unsigned int x2, long unsigned int y2);
    inline void triangle(const char* name,unsigned long x1,unsigned long y1,unsigned long x2,unsigned long y2, unsigned long x3,unsigned long y3);
35 36
public:
    /*!
37 38
     * \brief SVG header buiding
     */
39
    void init(const char *path);
40

41
    /*!
42 43
     * \brief SVG bottom file buiding
     */
44
    void end();
45

46 47
    void start_draw();
    void start_draw_containers();
48

49
    /*!
50 51 52 53 54 55
     * \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
     */
56 57
    void draw_container(const Element_pos x, const Element_pos y, const Element_pos w, const Element_pos h) ;

58 59 60 61 62
    void draw_container_text(const Element_pos x, const Element_pos y, const std::string value);

    void end_draw_containers();
    void start_draw_states();

63 64 65 66 67 68 69 70 71
    /*!
     * \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.
     */
72
    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
73 74 75 76 77 78 79

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

80 81 82 83
    void end_draw_states();

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

84 85
    void start_draw_counter();
    
86
  void draw_counter(const Element_pos x, const Element_pos y);
87 88 89
    
    void end_draw_counter();

90
    void end_draw();
91 92
};

93 94 95 96 97 98 99 100 101 102 103 104 105 106


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


inline void Svg::start_draw(){
107
 
108 109 110 111 112 113 114 115 116 117 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(){
}

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
125

126 127 128 129 130 131 132 133 134 135 136 137 138
/********************
 * Counter
 *******************/

inline void Svg::start_draw_counter(){
}

inline void Svg::draw_counter(const Element_pos x, const Element_pos y){
}

inline void Svg::end_draw_counter(){
}

139 140

inline void Svg::end_draw(){
Olivier Lagrasse's avatar
Olivier Lagrasse committed
141
    Svg::end();
142 143
}

Pascal Noisette's avatar
Pascal Noisette committed
144 145
void Svg::rectangle(const char* name,unsigned long w, unsigned long h,unsigned long x1,unsigned long y1,  unsigned int r,   unsigned int g,   unsigned int b ){
    _buffer << "\n<rect title='"<<name<<"' width='" << w
Pascal Noisette's avatar
Pascal Noisette committed
146 147 148 149
            <<"' height='"<< h
            <<"' x='"     << x1
            <<"' y='"     << y1
            <<"' fill='rgb("<<r<<","<<g<<","<<b
Olivier Lagrasse's avatar
Olivier Lagrasse committed
150
            <<")'/>" << std::endl;
Pascal Noisette's avatar
Pascal Noisette committed
151 152 153 154 155 156 157
    print();
}





Pascal Noisette's avatar
Pascal Noisette committed
158 159
void Svg::triangle(const char* name,unsigned long x1,unsigned long y1,unsigned long x2,unsigned long y2, unsigned long x3,unsigned long y3){
    _buffer << "\n<polyline title='"<<name<<"' class='triangle' points='"
Pascal Noisette's avatar
Pascal Noisette committed
160 161 162
            <<        x1 << "," << y1
            << " " << x2 << "," << y2
            << " " << x3 << "," << y3
Olivier Lagrasse's avatar
Olivier Lagrasse committed
163
            <<"' />" << std::endl;
Pascal Noisette's avatar
Pascal Noisette committed
164 165 166 167
    print();
}

void Svg::print(){
Olivier Lagrasse's avatar
Olivier Lagrasse committed
168
    // if (_buffer.str().size()>BUFFER_SIZE){
Pascal Noisette's avatar
Pascal Noisette committed
169
        _svg_file.write(_buffer.str().c_str(),  _buffer.str().size());
Pascal Noisette's avatar
Pascal Noisette committed
170
	_buffer.str("");
Pascal Noisette's avatar
Pascal Noisette committed
171 172 173 174 175
}




Pascal Noisette's avatar
Pascal Noisette committed
176 177
inline void Svg::line(const char* name, long unsigned int x1, long unsigned int y1, long unsigned int x2, long unsigned int y2){
    _buffer << "\n<line title='"<<name<<"' x1='" << x1
Pascal Noisette's avatar
Pascal Noisette committed
178 179 180 181 182 183 184 185
            <<"' 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
186 187
  Svg::line("arrow",start_time,start_height,end_time,end_height);
  Svg::triangle("arrow",end_time,end_height+ARROWSIZE,end_time,end_height-ARROWSIZE,end_time+ARROWSIZE,end_height);//spike
Pascal Noisette's avatar
Pascal Noisette committed
188 189 190
}

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
191
  Svg::rectangle("container",w, h, x, y, 0xff, 0x44, 0xcc);
Pascal Noisette's avatar
Pascal Noisette committed
192 193 194 195 196
}



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
197
  Svg::rectangle("state",end-start,LEVEL - MARGIN, start,level*LEVEL,r , g,  b);
Pascal Noisette's avatar
Pascal Noisette committed
198 199 200
}

inline  void Svg::draw_event(const Element_pos time, const Element_pos height, const Element_pos container_height){
201
  Svg::rectangle("event",MARGIN,container_height, time, height, 0xff, 0xff, 0xff);
Pascal Noisette's avatar
Pascal Noisette committed
202
}
203

204
#endif // RENDER_SVG