Render.hpp 17.8 KB
Newer Older
Olivier Lagrasse's avatar
Olivier Lagrasse committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
/*
** This file is part of the ViTE project.
**
** This software is governed by the CeCILL-A license under French law
** and abiding by the rules of distribution of free software. You can
** use, modify and/or redistribute the software under the terms of the
** CeCILL-A license as circulated by CEA, CNRS and INRIA at the following
** URL: "http://www.cecill.info".
** 
** As a counterpart to the access to the source code and rights to copy,
** modify and redistribute granted by the license, users are provided
** only with a limited warranty and the software's author, the holder of
** the economic rights, and the successive licensors have only limited
** liability.
** 
** In this respect, the user's attention is drawn to the risks associated
** with loading, using, modifying and/or developing or reproducing the
** software by the user in light of its specific status of free software,
** that may mean that it is complicated to manipulate, and that also
** therefore means that it is reserved for developers and experienced
** professionals having in-depth computer knowledge. Users are therefore
** encouraged to load and test the software's suitability as regards
** their requirements in conditions enabling the security of their
** systems and/or data to be ensured and, more generally, to use and
** operate it in the same conditions as regards security.
** 
** The fact that you are presently reading this means that you have had
** knowledge of the CeCILL-A license and that you accept its terms.
**
**
** ViTE developers are (for version 0.* to 1.0):
**
**        - COULOMB Kevin
**        - FAVERGE Mathieu
**        - JAZEIX Johnny
**        - LAGRASSE Olivier
**        - MARCOUEILLE Jule
**        - NOISETTE Pascal
**        - REDONDY Arthur
**        - VUCHENER Clément 
**
*/
/*!
 *\file Render.hpp
 */

#ifndef RENDER_HPP
#define RENDER_HPP

//#include "render/Render_opengl_dl.hpp"
#include "render/Geometry.hpp"
Olivier Lagrasse's avatar
Olivier Lagrasse committed
52
#include "render/Ruler.hpp"
Olivier Lagrasse's avatar
Olivier Lagrasse committed
53 54
#include "common/info.hpp"

55
#include <GL/glut.h>
Olivier Lagrasse's avatar
Olivier Lagrasse committed
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152

/*!
 * \brief Structure used to store container information.
 */
struct Container_{
    /*!
     * \brief Coordinates.
     */
    Element_pos x, y, w, h;
};

/*!
 * \brief Structure used to store container text.
 */
struct Container_text_{
    /*!
     * \brief Coordinates.
     */
    Element_pos x, y;
    /*!
     * \brief Text.
     */
    std::string value;
};




/*!
 * \brief This class is used to called opengl or svg primitive functions to draw trace elements.
 */
template<class T>
class Render: public Geometry
{
    private:

    /*!
     * This attribute store the instance of a drawing class. (OpenGL or SVG for example)
     */
    T* drawing_instance;

    /*!
     * \brief Used to draw counter.
     */
    bool _start_new_line;


    std::vector<Container_> _containers;
    std::vector<Container_text_> _container_texts;

    /*!
     * \brief The default constructor is in private scope to prevent Render instanciation without
     * provide a drawing instance.
     */
    Render(){
    }

public:

    /***********************************
     *
     * Constructor and destructor.
     *
     **********************************/

    /*!
     * \brief The default constructor
     */
    Render(T* instance){
        drawing_instance = instance;
    }

    /*!
     * \brief The destructor
     */
    virtual ~Render(){
    }


    /***********************************
     *
     * Building functions.
     *
     **********************************/

    /*!
     * \brief Proceeds with the initialization of the draw functions.
     */
    inline void start_draw(){
        /* clear vectors */
        _containers.clear();
        _container_texts.clear();
        
        drawing_instance->start_draw();
    }

    /*!
Olivier Lagrasse's avatar
Olivier Lagrasse committed
153
     * \brief Initialize container draws.
Olivier Lagrasse's avatar
Olivier Lagrasse committed
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
     */
    inline void start_draw_containers(){
    }

    /*!
     * \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
     */
    inline void draw_container(const Element_pos x, const Element_pos y, const Element_pos w, const Element_pos h){
        
        Container_ buf;

        buf.x = x;
        buf.y = y;
        buf.w = w;
        buf.h = h;

        _containers.push_back(buf);
     
Olivier Lagrasse's avatar
Olivier Lagrasse committed
176 177
        if ((x+w)>Info::Container::x_max)
            Info::Container::x_max = x+w;
Olivier Lagrasse's avatar
Olivier Lagrasse committed
178
        
Olivier Lagrasse's avatar
Olivier Lagrasse committed
179 180
        if ((y+h)>Info::Container::y_max)
            Info::Container::y_max = y+h;
Olivier Lagrasse's avatar
Olivier Lagrasse committed
181
        
Olivier Lagrasse's avatar
Olivier Lagrasse committed
182 183
        if (Info::Container::x_min > x)
            Info::Container::x_min = x;
Olivier Lagrasse's avatar
Olivier Lagrasse committed
184
        
Olivier Lagrasse's avatar
Olivier Lagrasse committed
185 186 187
        if (Info::Container::y_min > y)
            Info::Container::y_min = y;

Olivier Lagrasse's avatar
Olivier Lagrasse committed
188 189 190 191 192 193
        
        
#ifdef DEBUG_MODE_RENDER_AREA
        
        std::cerr << __FILE__ << " l." << __LINE__ << ":" << std::endl;
        std::cerr < "Container drawing:" << std::endl;
Olivier Lagrasse's avatar
Olivier Lagrasse committed
194
        std::cerr << "x: " << x << " y: " << y << " w: " << w << " h: " << h << " xmax-xmin: " << Info::Container::x_max << " - " << Info::Container::x_min << " ymax-ymin: " << Info::Container::y_max << " - " << Info::Container::y_min << std::endl;
Olivier Lagrasse's avatar
Olivier Lagrasse committed
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
        
#endif
        
    }

    /*!
     * \brief Draw the text of a container.
     * \param x the x position of the text.
     * \param y the y position of the text.
     * \param value the string value of the text.
     *
     * This function stores text in a list. This list will be display each time the render area need to be updated.
     */
    inline void draw_container_text(const Element_pos x, const Element_pos y, const std::string value){
      
        Container_text_ buf;
Olivier Lagrasse's avatar
Olivier Lagrasse committed
211 212
        buf.x     = x;
        buf.y     = y;
Olivier Lagrasse's avatar
Olivier Lagrasse committed
213 214 215 216 217 218 219 220 221 222 223
        buf.value = value;

        _container_texts.push_back(buf);
    }

    /*!
     * \brief Closes the container display list.
     */
    inline void end_draw_containers(){
        float j = 0.6;

Olivier Lagrasse's avatar
Olivier Lagrasse committed
224 225 226 227
        //   std::cout << __FILE__ << " ~~ " << __LINE__ << " " << Info::Container::y_max << " " << Info::Container::y_min << std::endl;
        /* Before calling start_draw_container(), Info::Container::y_max should have a correct value */
        drawing_instance->start_draw_containers();

Olivier Lagrasse's avatar
Olivier Lagrasse committed
228 229
        for (unsigned int i=0 ; i<_containers.size() ; i++){
            drawing_instance->set_color(0, 0, j);
Olivier Lagrasse's avatar
Olivier Lagrasse committed
230 231
            drawing_instance->draw_quad(_containers[i].x * (Info::Render::width/Info::Container::x_max)*_x_scale_container_state,
                                        trace_to_render_y(_containers[i].y),// * _y_state_scale*(_render_height-_ruler_height)/Info::Container::y_max) - _y_state_translate + _ruler_y + _ruler_height,
Olivier Lagrasse's avatar
Olivier Lagrasse committed
232
                                        _z_container, 
Olivier Lagrasse's avatar
Olivier Lagrasse committed
233 234
                                        _containers[i].w * (Info::Render::width/Info::Container::x_max)*_x_scale_container_state,
                                        _containers[i].h * _y_state_scale*(Info::Render::height-_ruler_height)/Info::Container::y_max);
Olivier Lagrasse's avatar
Olivier Lagrasse committed
235
        }
Olivier Lagrasse's avatar
Olivier Lagrasse committed
236 237
        //  std::cout << __FILE__ << " ~ " << __LINE__ << " " << Info::Container::y_max << " " << Info::Container::y_min << std::endl;
  
Olivier Lagrasse's avatar
Olivier Lagrasse committed
238
        for (unsigned int i=0 ; i<_container_texts.size() ; i++){
Olivier Lagrasse's avatar
Olivier Lagrasse committed
239 240 241
            _container_texts[i].x *= (Info::Render::width/Info::Container::x_max)*_x_scale_container_state;
            _container_texts[i].y = trace_to_render_y(_container_texts[i].y);// + _render_height-_ruler_height)*coeff_trace_render_y();

Olivier Lagrasse's avatar
Olivier Lagrasse committed
242 243 244 245
            drawing_instance->draw_text(_container_texts[i].x, _container_texts[i].y, _z_container+1, _container_texts[i].value);
        }
        
        drawing_instance->end_draw_containers();
246

Olivier Lagrasse's avatar
Olivier Lagrasse committed
247 248
    }

249 250
    static int bb;

Olivier Lagrasse's avatar
Olivier Lagrasse committed
251 252 253 254 255 256 257
    /*!
     * \brief Creates and opens the display list for state draws.
     */
    inline void start_draw_states(){
        drawing_instance->start_draw_states();
    }

258 259


Olivier Lagrasse's avatar
Olivier Lagrasse committed
260 261 262 263 264 265 266 267 268 269 270
    /*!
     * \brief Draw a state of the trace.
     * \param start the beginning time of the state.
     * \param end the ending time of the state.
     * \param base vertical position of the state.
     * \param height the state height.
     * \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.
     */

271 272
    inline void draw_state(const Element_pos start, const Element_pos end, const Element_pos base, const Element_pos height, const Element_col r, const Element_col g, const Element_col b) {
       
273

Olivier Lagrasse's avatar
Olivier Lagrasse committed
274
        drawing_instance->set_color(r, g, b);
275
        
276
        drawing_instance->draw_quad(trace_to_render_x(start - Info::Render::_x_min_visible),
Olivier Lagrasse's avatar
Olivier Lagrasse committed
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
                                    trace_to_render_y(base),
                                    _z_state, 
                                    (end-start)*coeff_trace_render_x(), 
                                    height*coeff_trace_render_y());
    }

    /*!
     * \brief Closes the state display list.
     */
    inline void end_draw_states(){
        drawing_instance->end_draw_states();
    }

    /*!
     * \brief Open the arrow display list.
     */
    inline void start_draw_arrows(){
        drawing_instance->start_draw_arrows();
    }

    /*!
     * \brief Draw an arrow.
     * \param start_time the beginning time of the arrow.
     * \param end_time the ending time of the arrow.
     * \param start_height vertical position of the begining time of the arrow.
     * \param end_height vertical position of the ending time of the arrow.
     *
     * This function stores all the information of the arrow to display it each time the render area need to be updated.
     */
Olivier Lagrasse's avatar
Olivier Lagrasse committed
306
    inline void draw_arrow(Element_pos start_time, Element_pos end_time, Element_pos start_height, Element_pos end_height, const Element_col r, const Element_col g, const Element_col b){
Olivier Lagrasse's avatar
Olivier Lagrasse committed
307 308 309 310 311 312
 
        /* For SVG */
        const Element_pos triangle_size = 2.0;
        const Element_pos coeff         = 180.0f/M_PI;
        Element_pos angle;
        
Olivier Lagrasse's avatar
Olivier Lagrasse committed
313 314
        drawing_instance->set_color(r, g, b);

315 316
        start_time   = trace_to_render_x(start_time - Info::Render::_x_min_visible);
        end_time     = trace_to_render_x(end_time - Info::Render::_x_min_visible);
Olivier Lagrasse's avatar
Olivier Lagrasse committed
317 318 319 320 321 322 323 324 325 326 327 328 329 330
        start_height = trace_to_render_y(start_height);
        end_height   = trace_to_render_y(end_height);
        
        if (start_time != end_time)
            angle = atan2((end_height - start_height), (end_time - start_time))*coeff;/* arc tangent */
        else
            angle = 90;
       
        drawing_instance->draw_triangle(end_time, end_height, triangle_size, angle);
         
        drawing_instance->draw_line(start_time, start_height, 
                  end_time,   end_height,   _z_arrow);

        /* For OpenGL */
Olivier Lagrasse's avatar
Olivier Lagrasse committed
331 332 333
        drawing_instance->draw_arrow(start_time, end_time, start_height, end_height, r, g, b);

        drawing_instance->set_color(1, 1, 1);/* init */
Olivier Lagrasse's avatar
Olivier Lagrasse committed
334 335 336 337 338 339 340 341
    }

    /*!
     * \brief Closes the arrow display list.
     */
    inline void end_draw_arrows(){
        drawing_instance->end_draw_arrows();
    }
342
    
Olivier Lagrasse's avatar
Olivier Lagrasse committed
343 344
    inline void start_draw_events(){
        drawing_instance->start_draw_events();
345
    }
Olivier Lagrasse's avatar
Olivier Lagrasse committed
346 347 348 349 350 351 352 353 354

    /*!
     * \brief Draw an event.
     * \param time time when the event occurs.
     * \param height vertical position of the event.
     * \param container_height information to draw event. It corresponds to the container height when they are drawn horizontally.
     *
     * This function stores all the information of the event to display it each time the render area need to be updated.
     */
355
    inline void draw_event(Element_pos time, Element_pos height, Element_pos container_height, const Element_col r, const Element_col g, const Element_col b){
356 357 358

      /* For SVG */

Olivier Lagrasse's avatar
Olivier Lagrasse committed
359
        Element_pos radius;/* the circle radius */
360 361

        drawing_instance->set_color(r, g, b);
Olivier Lagrasse's avatar
Olivier Lagrasse committed
362
        
363
        time             = trace_to_render_x(time - Info::Render::_x_min_visible);
Olivier Lagrasse's avatar
Olivier Lagrasse committed
364 365
        height           = trace_to_render_y(height);
        container_height = container_height*coeff_trace_render_y();
366
        radius           = .5f;    
Olivier Lagrasse's avatar
Olivier Lagrasse committed
367 368 369 370
        
        drawing_instance->draw_circle(time, height, _z_event, radius);
        drawing_instance->draw_line(time, height,
                                    time, height+container_height, _z_event);
371 372 373

        /* For OpenGL */
        drawing_instance->draw_event(time, height, container_height);
Olivier Lagrasse's avatar
Olivier Lagrasse committed
374
    }
375
    
Olivier Lagrasse's avatar
Olivier Lagrasse committed
376 377
    inline void end_draw_events(){
        drawing_instance->end_draw_events();
378
    }
Olivier Lagrasse's avatar
Olivier Lagrasse committed
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409

    /*!
     * \brief Creates and opens the display list for counter draws.
     */
    inline void start_draw_counter(){
        drawing_instance->start_draw_counter();
        _start_new_line = true;
    }

    /*!
     * \brief Draw a point of the counter.
     * \param x x position of the point.
     * \param y y position of the point.
     *
     * Each time counter is increased, this function is called with the coordinates of the new point.
     */
    inline void draw_counter(const Element_pos x, const Element_pos y){

        if (_start_new_line){/* Start a new line */
            
            _counter_last_x = x;
            _counter_last_y = y;
            _start_new_line = false;
            
        }else{/* line is already started */
            
            if (x <= 0.0){/* convention: the line is over */
                
                //                drawing_instance->draw_line( _counter_last_x,  _counter_last_y,
                //                           _state_x_max,       _counter_last_y,  _z_counter);
              
410
                drawing_instance->draw_line( trace_to_render_x(_counter_last_x - Info::Render::_x_min_visible), trace_to_render_y(_counter_last_y),
Olivier Lagrasse's avatar
Olivier Lagrasse committed
411 412 413 414 415 416 417 418 419 420 421 422 423
                                             trace_to_render_x(Info::Entity::x_max),    trace_to_render_y(_counter_last_y),  _z_counter);
              
  
                _start_new_line = true;
            }else{/* add new points to the line */
                
                /* Each time, the method draw the line from the previous point to 
                   the current point (x, y). This last is stored for the next line drawing. */
                //                drawing_instance->draw_line( _counter_last_x,  _counter_last_y,
                //                           x,                               _counter_last_y, _z_counter);
                // drawing_instance->draw_line( x,  _counter_last_y,
                //                           x,  y,                              _z_counter);
               
424 425 426 427
                drawing_instance->draw_line(  trace_to_render_x(_counter_last_x - Info::Render::_x_min_visible),  trace_to_render_y(_counter_last_y),
                                              trace_to_render_x(x - Info::Render::_x_min_visible),                trace_to_render_y(_counter_last_y), _z_counter);
                drawing_instance->draw_line(  trace_to_render_x(x - Info::Render::_x_min_visible),                trace_to_render_y(_counter_last_y),
                                              trace_to_render_x(x - Info::Render::_x_min_visible),                trace_to_render_y(y),               _z_counter);
Olivier Lagrasse's avatar
Olivier Lagrasse committed
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
               
                _counter_last_x = x;
                _counter_last_y = y;
            }
        }
    }


    /*!
     * \brief Closes the counter display list.
     */
    inline void end_draw_counter(){

        if (!_start_new_line) {/* If a line was previously opened */
            /* Draw the last line */
            //  drawing_instance->draw_line( _counter_last_x,  _counter_last_y,
            //                             _state_x_max,       _counter_last_y, _z_counter);
445
            drawing_instance->draw_line( trace_to_render_x(_counter_last_x - Info::Render::_x_min_visible), trace_to_render_y(_counter_last_y),
Olivier Lagrasse's avatar
Olivier Lagrasse committed
446 447 448 449 450 451 452 453 454
                                         trace_to_render_x(Info::Entity::x_max),    trace_to_render_y(_counter_last_y), _z_counter);
        }
        drawing_instance->end_draw_counter();
    }
    
    /*!
     * \brief Do nothing (it is present for compatibility of the Render class).
     */
    inline void end_draw(){
455
        //    std::cerr << __FILE__ << __LINE__ << " " << Info::Render::_x_min_visible << " " << Info::Render::_x_max_visible << std::endl;
Olivier Lagrasse's avatar
Olivier Lagrasse committed
456 457 458 459 460 461
        /* Now, draw ruler */
        Element_pos graduation_diff;
        Element_pos coeff_prefix;
        std::ostringstream buf_txt;
        
        /* Update Info::Render::_x_min_visible and Info::Render::_x_max_visible values */
462 463
        //        update_visible_interval_value();
        //  std::cerr << __FILE__ << __LINE__ << " " << Info::Render::_x_min_visible << " " << Info::Render::_x_max_visible << std::endl;
Olivier Lagrasse's avatar
Olivier Lagrasse committed
464 465 466
        graduation_diff = Ruler::get_graduation_diff(Info::Render::_x_min_visible, Info::Render::_x_max_visible);
        coeff_prefix    = Ruler::get_coeff_for_common_prefix(Info::Render::_x_min_visible, Info::Render::_x_max_visible);
     
Olivier Lagrasse's avatar
Olivier Lagrasse committed
467 468
        //     std::cout << __FILE__ << " " << __LINE__ << " : " << coeff_prefix << std::endl;

Olivier Lagrasse's avatar
Olivier Lagrasse committed
469 470 471 472 473
        drawing_instance->start_ruler();
        drawing_instance->set_color(1, 0, 0);
        for (Element_pos i = Info::Render::_x_min_visible ; i < Info::Render::_x_max_visible ; i+=graduation_diff){
            const Element_pos grad_div_by_5 = graduation_diff/5;
            for (Element_pos j = (i+grad_div_by_5) ; j<(i+graduation_diff) ; j+= grad_div_by_5){
474 475
                drawing_instance->draw_line( trace_to_render_x(j - Info::Render::_x_min_visible), 0,
                                             trace_to_render_x(j - Info::Render::_x_min_visible), 2, _z_ruler);
Olivier Lagrasse's avatar
Olivier Lagrasse committed
476 477
            }

478 479
            drawing_instance->draw_line( trace_to_render_x(i - Info::Render::_x_min_visible), 0,
                                         trace_to_render_x(i - Info::Render::_x_min_visible), 4, _z_ruler);
Olivier Lagrasse's avatar
Olivier Lagrasse committed
480 481 482

            buf_txt.str("");/* flush the buffer */
            buf_txt << Ruler::get_variable_part(i, coeff_prefix, 2);
483
            drawing_instance->draw_text(trace_to_render_x(i - Info::Render::_x_min_visible)+1, 4, _z_ruler, buf_txt.str());
Olivier Lagrasse's avatar
Olivier Lagrasse committed
484 485 486 487

        }
        drawing_instance->end_ruler();

Olivier Lagrasse's avatar
Olivier Lagrasse committed
488 489 490 491
        /* clear vectors */
        _containers.clear();
        _container_texts.clear();
        drawing_instance->end_draw();
Olivier Lagrasse's avatar
Olivier Lagrasse committed
492

Olivier Lagrasse's avatar
Olivier Lagrasse committed
493 494 495 496 497 498
    }
};



#endif