render_svg.hpp 5.73 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

Pascal Noisette's avatar
Pascal Noisette committed
9 10
#define BUFFER_SIZE 2048 //character number stored before flush the _buffer
#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 29
    std::ostringstream _buffer;
    std::ofstream _svg_file;
Pascal Noisette's avatar
Pascal Noisette committed
30
    inline void print();
Pascal Noisette's avatar
Pascal Noisette committed
31
    inline void rectangle(unsigned long w, unsigned long h,unsigned long x1,unsigned long y1,  unsigned int r,   unsigned int g,   unsigned int b );
Pascal Noisette's avatar
Pascal Noisette committed
32
    inline void line( long unsigned int x1, long unsigned int y1, long unsigned int x2, long unsigned int y2);
Pascal Noisette's avatar
Pascal Noisette committed
33
    inline void triangle(unsigned long x1,unsigned long y1,unsigned long x2,unsigned long y2, unsigned long x3,unsigned long y3);
34 35
public:
    /*!
36 37
     * \brief SVG header buiding
     */
38
    void init(const char *path);
39

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

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

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

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

    void end_draw_containers();
    void start_draw_states();

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

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

79 80 81 82
    void end_draw_states();

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

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

89
    void end_draw();
90 91
};

92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122


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


inline void Svg::start_draw(){
}

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
123

124 125 126 127 128 129 130 131 132 133 134 135 136
/********************
 * 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(){
}

137 138 139 140

inline void Svg::end_draw(){
}

Pascal Noisette's avatar
Pascal Noisette committed
141 142 143 144 145 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 189 190 191 192 193 194 195 196 197 198 199 200
void Svg::rectangle(unsigned long w, unsigned long h,unsigned long x1,unsigned long y1,  unsigned int r,   unsigned int g,   unsigned int b ){
    _buffer << "<rect title='container' width='" << w
            <<"' height='"<< h
            <<"' x='"     << x1
            <<"' y='"     << y1
            <<"' fill='rgb("<<r<<","<<g<<","<<b
            <<")'/>";
    print();
}





void Svg::triangle(unsigned long x1,unsigned long y1,unsigned long x2,unsigned long y2, unsigned long x3,unsigned long y3){
    _buffer << "<polyline title='triangle' class='triangle' points='"
            <<        x1 << "," << y1
            << " " << x2 << "," << y2
            << " " << x3 << "," << y3
            <<"' />";
    print();
}

void Svg::print(){
    if (_buffer.str().size()>BUFFER_SIZE){
        _svg_file.write(_buffer.str().c_str(),  _buffer.str().size());
        _buffer.flush();
    }
}




inline void Svg::line( long unsigned int x1, long unsigned int y1, long unsigned int x2, long unsigned int y2){
    _buffer << "<line title='line' x1='" << x1
            <<"' 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){
  Svg::line(start_time,start_height,end_time,end_height);
  Svg::triangle(end_time,end_height+ARROWSIZE,end_time,end_height-ARROWSIZE,end_time+ARROWSIZE,end_height);//spike
}

inline void Svg::draw_container(const Element_pos x, const Element_pos y, const Element_pos w, const Element_pos h){
    Svg::rectangle(w, h, x, y, 0xff, 0x44, 0xcc);
}



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){
  Svg::rectangle(end-start,LEVEL - MARGIN, start,level*LEVEL,r , g,  b);
}

inline  void Svg::draw_event(const Element_pos time, const Element_pos height, const Element_pos container_height){
    rectangle(1,container_height*LEVEL, time, container_height, 0xff, 0x44, 0xcc);
}
201

202
#endif // RENDER_SVG