Robotran C Documentation
plot_sdl.h
Go to the documentation of this file.
1 
7 #ifdef SDL
8 
9 #ifndef _PLOT_SDL_H_INCLUDED_
10 #define _PLOT_SDL_H_INCLUDED_
11 
12 #include "SDL.h"
13 #undef main
14 #include "SDL_ttf.h"
15 #include "SDL_opengl.h"
16 
17 #include "realtime.h"
18 #include "auto_plot.h"
19 #include "color_sdl.h"
20 
21 #include "mbs_data.h"
22 //#include "user_realtime.h"
23 
24 // y axis: main parts
25 #define SCREEN_UP_HEIGHT_LOSS 40
26 #define SCREEN_BOTTOM_HEIGHT_LOSS 40
27 #define PLOT_Y_START SCREEN_UP_HEIGHT_LOSS
28 
29 // x axis: main parts
30 #define SCREEN_WIDTH_LOSS 60
31 #define PLOT_X_START 0
32 #define X_ERASE_WIDTH SCREEN_WIDTH_LOSS
33 
34 // up indications
35 #define UP_FIRST_Y_INDICATIONS (SCREEN_UP_HEIGHT_LOSS / 3)
36 #define UP_SECOND_Y_INDICATIONS (UP_FIRST_Y_INDICATIONS + ((3*SCREEN_UP_HEIGHT_LOSS) / 8))
37 #define UP_SEPARATION_INDICATION ((UP_FIRST_Y_INDICATIONS + UP_SECOND_Y_INDICATIONS) / 2)
38 #define UP_COLOR_WIDTH_LABEL 15
39 #define UP_COLOR_SPACE_LABEL 5
40 #define UP_COLOR_WIDTH_LOSS_BEGIN (UP_COLOR_SPACE_LABEL + UP_COLOR_SPACE_LABEL + 40)
41 #define UP_COLOR_WIDTH_LOSS_END 20
42 
43 // bottom indications: x axis
44 #define LEGEND_SIGNAL_SCALING_X_POS 10
45 #define SIGNAL_SCALING_X_POS 22
46 #define LEGEND_PLOT_SCALING_X_POS 45
47 #define PLOT_SCALING_X_POS 57
48 #define LEGEND_PLOT_MOVE_X_POS_1 93
49 #define LEGEND_PLOT_MOVE_X_POS_1_BIS 110
50 #define LEGEND_PLOT_MOVE_X_POS_2 118
51 #define PLOT_MOVE_X_POS_1 105
52 #define PLOT_MOVE_X_POS_2 130
53 #define LEGEND_SWITCH_HOR_VERT_X_POS 138
54 #define SWITCH_HOR_VERT_X_POS 165
55 #define LEGEND_XY_LABEL_X_POS 210
56 #define XY_LABEL_X_POS 250
57 #define LEGEND_TIME_SPEED_X_POS 385
58 #define TIME_SPEED_X_POS 425
59 #define VISU_PAST_X_POS_1 535
60 #define VISU_PAST_X_POS_2 550
61 #define VISU_PAST_X_POS_3 557
62 #define LEGEND_SPEED_X_POS 580
63 #define SPEED_X_POS 592
64 #define LEGEND_QUIT_X_POS 620
65 #define QUIT_X_POS 632
66 
67 // font sizes
68 #define FONT_AXIS_SIZE 12
69 #define FONT_LABEL_SIZE 12
70 #define FONT_SMALL_LABEL_SIZE 10
71 #define FONT_SCALING 25
72 #define FONT_ARROWS 15
73 #define FONT_PLAY_BREAK 14
74 #define FONT_HOR_MOUSE 25
75 #define FONT_MOUSE 45
76 #define FONT_SPEED 25
77 
78 // x labels
79 #define X_LABEL_NB 10
80 #define X_LABEL_HEIGHT 10
81 #define X_LABEL_SHIFT_X 2
82 #define X_LABEL_SHIFT_Y 0
83 
84 // y labels
85 #define Y_LABEL_NB 10
86 #define Y_LABEL_WIDTH 10
87 #define Y_LABEL_SHIFT_X 3
88 #define Y_LABEL_SHIFT_Y 2
89 #define Y_LABEL_SHIFT_Y_START (Y_LABEL_SHIFT_Y + 9)
90 #define Y_LABEL_SHIFT_Y_END (Y_LABEL_SHIFT_Y - 10)
91 
92 #define STR_LABEL_MIN 1.0e-9
93 
94 // scaling
95 #define PLOT_SCALE_MAX 4.0
96 #define SCALING_MULT_THRESHOLD 4.0
97 #define SCALING_PLOT_TOO_SMALL 0.0
98 #define SCALING_PLOT_TOO_HIGH 0.4
99 #define SCALING_PLOT_OPTIMAL_INCREASE 0.25
100 #define SCALING_PLOT_OPTIMAL_DECREASE 0.15
101 #define SCALING_NUL_DIFF 4.0
102 #define MANUAL_SCALING_PLOT 0.5
103 #define MANUAL_OFFSET_PLOT 0.25
104 
105 // other
106 #define NUL_AXIS_DASH_WIDTH 8
107 #define X_AXIS_NB_Y_PIXELS_WIDTH 2
108 #define START_VECTOR 12345
109 
110 // mouse
111 #define WHEEL_MOUSE_FACTOR 0.9 // [-]
112 #define FQC_MOUSE 30.0 // frequence of the SDL window refresh when using the mouse for translations [Hz]
113 #define TIME_RELEASE_MOUSE_PLOT (1e6 / FQC_MOUSE) // [us]
114 
115 #define TIME_MOUSE_DELAY 5e4
116 
117 
118 // -- Structures -- //
119 
122 typedef struct Screen_sdl
123 {
124  // SDL pointers
125  SDL_Window *win;
126  SDL_Renderer *ren;
127 
128  // fonts pointers
129  TTF_Font *font_label;
130  TTF_Font *font_small_label;
131  TTF_Font *font_axis;
132  TTF_Font *font_scaling;
133  TTF_Font *font_arrows;
134  TTF_Font *font_play;
135  TTF_Font *font_hor_mouse;
136  TTF_Font *font_mouse;
137  TTF_Font *font_break;
138  TTF_Font *font_speed;
139 
140  // colors
141  SDL_Color text_color;
142 
143  int color_tab[NB_COLORS_SDL];
144 
145  // other variables
146 
147  int break_plot_flag;
148  int plot_auto_scaling;
149  int signal_auto_scaling;
150  int last_plot_auto_scaling;
151  int last_signal_auto_scaling;
152  int hor_plot_scaling;
153  int index_vec;
154  int index_sdl;
155  int last_x_label_flag;
156  int expo_y;
157  int expo_x;
158  int increase_plot_y_diff_flag;
159  int decrease_plot_y_diff_flag;
160  int up_plot_y_flag;
161  int bottom_plot_y_flag;
162  int increase_plot_x_diff_flag;
163  int decrease_plot_x_diff_flag;
164  int left_plot_y_flag;
165  int right_plot_y_flag;
166  int bottom_flag;
167  int scaling_flag;
168  int last_scaling_flag;
169  int last_color;
170  int x_label_print_count;
171  int change_mouse_wheel_flag;
172  int buffer_size;
173  int min_tsim_index;
174  int max_tsim_index;
175  int flag_buffer_round;
176  int flag_started;
177  int max_nb_curves;
178  int cur_nb_curves;
179  int max_nb_legends;
180  int nb_legends;
181 
182  double tsim_pixel;
183  double y_min, y_max;
184  double x_min, x_max;
185  double last_y_min, last_y_max;
186  double last_legend_y_min, last_legend_y_max;
187  double last_legend_x_min, last_legend_x_max;
188  double min_tsim;
189  double max_tsim;
190 
191  // pixel size related
192  int curve_width;
193  int screen_width;
194  int screen_height;
195 
196  // screen width related
197  int plot_x_end;
198  int screen_plot_width;
199  int x_label_width;
200 
201  // screen height related
202  int plot_y_end;
203  int plot_y_mid;
204  int screen_plot_height;
205  int y_label_height;
206 
207  // bottom indications: y axis
208  int bottom_mid_y_indications;
209  int scaling_indications;
210  int x_axis_indication;
211  int y_axis_indication;
212  int speed_indication_1;
213  int speed_indication_2;
214  int legend_speed_indication_1;
215  int legend_speed_indication_2;
216  int scaling_bottom_indications;
217  int scaling_bottom_arrow_indications_1;
218  int scaling_bottom_arrow_indications_2;
219  int scaling_bottom_arrow_indications_3;
220  int scaling_up_indications;
221  int scaling_up_indications_2;
222 
223  int *scaling_expo_mult;
224  int *last_scaling_expo_mult;
225  int *pixel_pos_label;
226 
227  double *t_vec;
228  double *y_tab_min;
229  double *y_tab_max;
230  double *previous_y_vec;
231  double *scaling_offset;
232  double *last_scaling_offset;
233 
234  double **y_tab;
235 
236  double *tsim_save;
237  double **y_save;
238 
239  AutoPlot *auto_plot;
240 
241  // joysticks
242  int nb_joysticks;
243  SDL_Joystick **joysticks;
244 
245 } Screen_sdl;
246 
247 
248 // -- Functions prototypes -- //
249 
250 void wait_key_generic(Simu_realtime *realtime, int cur_t_usec, double tsim);
251 
252 Screen_sdl* init_screen_sdl(Realtime_option *options, int *err);
253 void new_curves_save(Screen_sdl *screen_sdl, int nb_new_curves, double *y_curves);
254 TTF_Font* init_font(char *folder, int font_size, int *err);
255 void free_screen_sdl(Screen_sdl *screen_sdl);
256 
257 int log_SDL_error(char *SDL_function);
258 void SDL_set_color(Screen_sdl *screen_sdl, int color_num);
259 int refresh_screen(Screen_sdl *screen_sdl);
260 void draw_pixel(SDL_Renderer *ren, int i, int j);
261 void draw_rectangle(SDL_Renderer *ren, int x1, int x2, int y1, int y2);
262 void draw_horizontal_line(SDL_Renderer *ren, int x1, int x2, int y);
263 void draw_vertical_line(SDL_Renderer *ren, int x, int y1, int y2);
264 void print_text_sdl(char *str, TTF_Font *font, SDL_Color txt_col, SDL_Renderer *ren, int x, int y, int pos_flag);
265 void renderTexture(SDL_Texture *tex, SDL_Renderer *ren, int x, int y, int pos_flag);
266 SDL_Texture* renderText(TTF_Font *font, char *message, SDL_Color text_color, SDL_Renderer *ren);
267 
268 void update_plot_vectors(Simu_realtime *realtime, double t);
269 void update_scale_signals(Screen_sdl *screen_sdl, Simu_realtime *realtime, int screen_flag);
270 void update_full_vectors(Simu_realtime *realtime, double tsim);
271 void update_x_min_max(Screen_sdl *screen_sdl, Simu_realtime *realtime);
272 void update_y_min_max(Simu_realtime *realtime);
273 void update_y_tab(Screen_sdl *screen_sdl);
274 void update_sdl_legend(Simu_realtime *realtime, int new_nb_legends);
275 double bound_diff_min_max(double min_thres, double max_thres, double cur_min, double cur_max, double cur_move, int sign_flag);
276 void mouse_sdl(Screen_sdl *screen_sdl, Simu_realtime *realtime, double tsim, int mouse_flag);
277 void set_bottom_flag(Simu_realtime *realtime);
278 
279 void str_scaling(char *str, int scaling_expo_mult, double scaling_offset);
280 int str_label(char *str, double low_nb, double high_nb);
281 void str_axis(char *str, double nb, int expo);
282 
283 void limit_x_limits(Screen_sdl *screen_sdl);
284 double scale_signal(double no_scale_nb, int scaling_expo_mult, double scaling_offset);
285 double unscale_signal(double scale_nb, int scaling_expo_mult, double scaling_offset);
286 
287 void plot_screen_sdl(Simu_realtime *realtime, double tsim, int screen_flag);
288 
289 int break_gestion(Simu_realtime *realtime, double tsim);
290 int events_simu(Simu_realtime *realtime, double tsim);
291 int last_break_gestion(Simu_realtime *realtime, double tsim);
292 
293 #endif
294 #endif
realtime_functions.h
useful functions used by the real-time modules
Simu_realtime::real_simu_speed_factor
double real_simu_speed_factor
real speed factor
Definition: realtime.h:109
WHITE_SDL
@ WHITE_SDL
Definition: color_sdl.h:18
LIGHT_GREEN_SDL
@ LIGHT_GREEN_SDL
Definition: color_sdl.h:19
Simu_realtime::simu_break
int simu_break
1 to make a break, 0 otherwise
Definition: realtime.h:103
Realtime_option
user real-time option
Definition: realtime.h:42
NB_COLORS_INIT_SDL
#define NB_COLORS_INIT_SDL
number of initial colors (not available to the user)
Definition: color_sdl.h:14
Realtime_option::screen_width
int screen_width
plot screen width in number of pixels
Definition: realtime.h:61
Realtime_option::max_nb_legends
int max_nb_legends
maximum number of curves legends indicated
Definition: realtime.h:65
auto_plot.h
functions used to automatically update the user curves plotted
useful_functions.h
BLUE_SDL
@ BLUE_SDL
Definition: color_sdl.h:18
LIGHT_BLUE_SDL
@ LIGHT_BLUE_SDL
Definition: color_sdl.h:19
mbs_data.h
Simu_realtime::init_t_sec
int init_t_sec
initial real time [s], without us
Definition: realtime.h:122
java_functions.h
Java functions used for the visualization.
LIGHT_GRAY_SDL
@ LIGHT_GRAY_SDL
Definition: color_sdl.h:18
_MBS_ERR_MOD_SPEC_13
#define _MBS_ERR_MOD_SPEC_13
Generic error number Module specific errors range from -10 to -19 please read the error message a...
Definition: mbs_errors_names.h:174
DARK_GREEN_SDL
@ DARK_GREEN_SDL
Definition: color_sdl.h:18
Simu_realtime::last_real_simu_speed_factor
double last_real_simu_speed_factor
last real speed factor
Definition: realtime.h:110
PINK_SDL
@ PINK_SDL
Definition: color_sdl.h:19
TURQUOISE_SDL
@ TURQUOISE_SDL
Definition: color_sdl.h:18
NB_COLORS_SDL
#define NB_COLORS_SDL
number of colors available to the user
Definition: color_sdl.h:12
Simu_realtime::t0
double t0
initial simulation time [s]
Definition: realtime.h:116
SAFETY_TIME
#define SAFETY_TIME
safety time: difference between min and max time in case of problems [s]
Definition: realtime_ext.h:16
GREEN_SDL
@ GREEN_SDL
Definition: color_sdl.h:19
set_plot.h
set_plot function called by the user to plot a curve
update_realtime_constraints_break
void update_realtime_constraints_break(Simu_realtime *realtime, int delta_break_u_sec)
update the real-time constraints after a break
Definition: realtime.c:952
mbs_msg
void mbs_msg(char *msg,...)
Send a message.
Definition: mbs_message.c:76
Realtime_option::y_max_init
double y_max_init
initial upper bound for the y values plotted
Definition: realtime.h:68
Simu_realtime::ext
Realtime_extern * ext
external variables
Definition: realtime.h:143
plot_sdl.h
main SDL functions (plot curves and handles keyboard, joystick... inputs)
YELLOW_SDL
@ YELLOW_SDL
Definition: color_sdl.h:19
Simu_realtime::tf
double tf
final simulation time [s]
Definition: realtime.h:117
time_functions.h
functions used to get the time expressed in us (micro seconds)
BLACK_SDL
@ BLACK_SDL
Definition: color_sdl.h:18
Realtime_option::y_min_init
double y_min_init
initial lower bound for the y values plotted
Definition: realtime.h:67
DARK_YELLOW_SDL
@ DARK_YELLOW_SDL
Definition: color_sdl.h:19
ORANGE_SDL
@ ORANGE_SDL
Definition: color_sdl.h:19
NB_COLORS_TOT_SDL
#define NB_COLORS_TOT_SDL
total number of colors
Definition: color_sdl.h:13
Realtime_option::curve_width
int curve_width
plot curve width in number of pixels
Definition: realtime.h:60
t_usec
int t_usec(int init_t_sec, int init_t_usec)
return the elapsed time (in us) since time 'init_t' (expressed in s and us)
Definition: time_functions.c:25
RED_SDL
@ RED_SDL
Definition: color_sdl.h:18
open_gl_c_int.h
interface to call C++ functions from C code
guess_tsim_index
void guess_tsim_index(double request_tsim, double *tsim_save, int min_ind, int max_ind, double min_tsim, double max_tsim, int buffer_size, int flag_round, int *prev_ind, int *next_ind)
get the indexes for a requested time on a turning vector of time values
Definition: realtime_functions.c:17
Simu_realtime::tsim
double tsim
current simulation time [s]
Definition: realtime.h:114
mbs_message.h
color_sdl.h
colors description, used for the SDL plots
Simu_realtime::first_break
int first_break
1 if during the first break, 0 otherwise
Definition: realtime.h:139
Realtime_option::buffer_size
int buffer_size
size of the buffer for 3D and sdl
Definition: realtime.h:53
expo_ten_inf
int expo_ten_inf(double nb)
get log with base 10
Definition: useful_functions.c:89
realtime.h
Real-time constraints main header.
Simu_realtime
real-time structure
Definition: realtime.h:99
Realtime_option::t0
double t0
initial simulation time [s]
Definition: realtime.h:45
Realtime_option::screen_height
int screen_height
plot screen height in number of pixels
Definition: realtime.h:62
Realtime_option::max_nb_curves
int max_nb_curves
maximum number of curves plotted
Definition: realtime.h:64
events_sdl.h
Simu_realtime::speed_last_t_usec
int speed_last_t_usec
last real time [us], to compute the simulation speed
Definition: realtime.h:126
Simu_realtime::simu_speed_flag
int simu_speed_flag
used to compute the simulation factor
Definition: realtime.h:105
pow_int
double pow_int(double basis, int expo)
compute efficiently pow with an integer exponent
Definition: useful_functions.c:60
PURPLE_SDL
@ PURPLE_SDL
Definition: color_sdl.h:18
get_simu_speed_factor
double get_simu_speed_factor(int simu_speed_flag)
get simulation factor depending on the 'simu_speed_flag' flag
Definition: realtime.c:903
Simu_realtime::init_t_usec
int init_t_usec
initial real time [us], without s (kind of modulo)
Definition: realtime.h:123
Realtime_option::fqc_screen
double fqc_screen
frequence of the SDL plots refreshment [Hz]
Definition: realtime.h:72
Simu_realtime::flag_visu
int flag_visu
1 if Java visualization features activated, 0 otherwise
Definition: realtime.h:137
Simu_realtime::last_tsim
double last_tsim
last simulation time [s]
Definition: realtime.h:113
Realtime_option::color_tab
int color_tab[NB_COLORS_SDL]
colors of the curves
Definition: realtime.h:70
mbs_errors_names.h