atomes 1.3.1
atomes: an atomic scale modeling tool box
Loading...
Searching...
No Matches
draw.c
Go to the documentation of this file.
1/* This file is part of the 'atomes' software
2
3'atomes' is free software: you can redistribute it and/or modify it under the terms
4of the GNU Affero General Public License as published by the Free Software Foundation,
5either version 3 of the License, or (at your option) any later version.
6
7'atomes' is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
8without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
9See the GNU General Public License for more details.
10
11You should have received a copy of the GNU Affero General Public License along with 'atomes'.
12If not, see <https://www.gnu.org/licenses/>
13
14Copyright (C) 2022-2026 by CNRS and University of Strasbourg */
15
21
22/*
23* This file: 'draw.c'
24*
25* Contains:
26*
27
28- The function to draw a curve
29
30*
31*/
32
33#include <gtk/gtk.h>
34#include <gdk/gdk.h>
35#include <stdlib.h>
36#include <math.h>
37#include <cairo.h>
38
39#include "global.h"
40#include "curve.h"
41
85void draw_curve (cairo_t * cr,
86 int cid,
87 int rid,
88 project * this_proj,
89 int points,
90 ColRGBA withcolor,
91 int xscale,
92 int yscale,
93 int asp,
94 int vdash,
95 double thick,
96 int glyp,
97 double gize,
98 int freq,
99 double hwidth,
100 double hopac,
101 int hpos,
102 int extra,
103 int pid)
104{
105 int i, j, k;
106 double x, y;
107 double x1, x2, y1, y2;
108 double dx1, dx2, dy1, dy2;
109 double slope, bval;
110 double ** plotdata;
111 gboolean plot;
112 gboolean dglyp;
113 curve_dash * dasht;
114 Curve * this_curve = this_proj -> analysis[rid] -> curves[cid];
115
116 plotdata = allocddouble (points, 2);
117 for ( i=0 ; i < points; i++ )
118 {
119 if (xscale == 0)
120 {
121 plotdata[i][0] = x_min + XDRAW * (this_curve -> data[0][i] - cxy[0]) / xmax;
122 }
123 else
124 {
125 x = (i+1) * this_proj -> analysis[rid] -> num_delta * this_proj -> analysis[rid] -> delta * pow(10, dxlog);
126 x = log(x) / log(pow(10, xlog));
127 plotdata[i][0] = x_min + XDRAW * x;
128 }
129 if (yscale == 0)
130 {
131 plotdata[i][1] = y_min + YDRAW * (this_curve -> data[1][i] - cxy[1]) / ymax;
132 }
133 else
134 {
135 y = this_curve -> data[1][i] * pow(10, dylog);
136 y = log(y) / log(pow(10, ylog));
137 plotdata[i][1] = y_min + YDRAW * y;
138 }
139#ifdef DEBUG
140 // g_debug ("CURVE: DRAWCURVE: x= %f, y= %f", plotdata[i][0], plotdata[i][1]);
141#endif
142 }
143 if (vdash > 0)
144 {
145 dasht = selectdash (vdash);
146 cairo_set_dash(cr, dasht -> a, dasht -> b, 0.0);
147 cairo_set_line_width (cr, thick);
148 g_free (dasht);
149 }
150 else
151 {
152 cairo_set_line_width (cr, 0.0);
153 }
154 cairo_set_source_rgba (cr, withcolor.red,
155 withcolor.green,
156 withcolor.blue, 1.0);
157
158 if (asp == 0)
159 {
160 j = 0;
161 k = 1;
162 if (rid == RIN) j = 2;
163 for ( i = j ; i < points - k ; i ++)
164 {
165 plot = TRUE;
166 dglyp = FALSE;
167 slope = (plotdata[i+k][1] - plotdata[i][1])/ (plotdata[i+k][0] - plotdata[i][0]);
168 bval = plotdata[i][1] - slope*plotdata[i][0];
169 dy1 = slope*x_min + bval;
170 dy2 = slope*x_max + bval;
171 if (slope < 0)
172 {
173 dx1 = (y_min - bval) / slope;
174 dx2 = (y_max - bval) / slope;
175 }
176 else
177 {
178 dx1 = (y_max - bval) / slope;
179 dx2 = (y_min - bval) / slope;
180 }
181 if (plotdata[i][0] < x_min && plotdata[i+k][0] < x_min)
182 {
183 plot = FALSE;
184 }
185 else if (plotdata[i][0] > x_max && plotdata[i+k][0] > x_max)
186 {
187 plot = FALSE;
188 }
189 if (plotdata[i][1] < y_max && plotdata[i+k][1] < y_max)
190 {
191 plot = FALSE;
192 }
193 else if (plotdata[i][1] > y_min && plotdata[i+k][1] > y_min)
194 {
195 plot = FALSE;
196 }
197 if (plot)
198 {
199 if (plotdata[i][0] >= x_min && plotdata[i][0] <= x_max)
200 {
201 if(plotdata[i][1] >= y_max && plotdata[i][1] <= y_min)
202 {
203 x1 = plotdata[i][0];
204 y1 = plotdata[i][1];
205 dglyp = TRUE;
206 }
207 else
208 {
209 x1 = dx1;
210 y1 = (slope < 0) ? y_min : y_max;
211 }
212 }
213 else
214 {
215 x1 = x_min;
216 y1 = dy1;
217 }
218 if (plotdata[i+k][0] >= x_min && plotdata[i+k][0] <= x_max)
219 {
220 if (plotdata[i+k][1] >= y_max && plotdata[i+k][1] <= y_min)
221 {
222 x2 = plotdata[i+k][0];
223 y2 = plotdata[i+k][1];
224 }
225 else
226 {
227 x2 = dx2;
228 y2 = (slope < 0) ? y_max : y_min;
229 }
230 }
231 else
232 {
233 x2 = x_max;
234 y2 = dy2;
235 }
236 cairo_move_to (cr, x1, y1);
237 cairo_line_to (cr, x2, y2);
238 cairo_stroke (cr);
239 if (i % freq == 0 && dglyp) draw_glyph (cr, glyp, x1, y1, withcolor, gize);
240 if (i == points - 2 && (i+k) % freq == 0 && dglyp)
241 {
242 draw_glyph (cr, glyp, x2, y2, withcolor, gize);
243 }
244 }
245 }
246 }
247 else if (asp == 1)
248 {
249 j = 0;
250 k = 1;
251 if (rid == RIN) j = 2;
252 // g_debug ("x_min= %f, x_max= %f, y_min= %f, y_max= %f", x_min, x_max, y_min, y_max);
253 for ( i = j ; i < points ; i ++)
254 {
255 if (this_curve -> data[1][i] != 0.0)
256 {
257 if (plotdata[i][0] >= x_min && plotdata[i][0] <= x_max)
258 {
259 if (plotdata[i][1] <= y_min || (cxy[1] <= 0.0 && plotdata[i][1] > y_min))
260 {
261 x1 = plotdata[i][0] - hwidth * 0.5 * XDRAW / xmax;
262 if (pid) x1 -= hwidth * 0.5 * ((float)pid / (float)extra) * XDRAW / xmax;
263 x2 = hwidth * XDRAW / xmax;
264 if (x1 < x_min)
265 {
266 x2 -= (x_min - x1);
267 x1 = x_min;
268 }
269 if (x1+x2 > x_max)
270 {
271 x2 = x_max - x1;
272 }
273 y1 = plotdata[i][1];
274 if (plotdata[i][1] > y_min)
275 {
276 y1 = y_min;
277 }
278 else if (plotdata[i][1] < y_max)
279 {
280 y1 = y_max;
281 }
282 y2 = y_min - y1;
283 if (cxy[1] <= 0.0)
284 {
285 y2 = y2 - cxy[1] * YDRAW / ymax;
286 }
287 if (hpos)
288 {
289 cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 1.0);
290 cairo_rectangle (cr, x1, y1, x2, y2);
291 cairo_fill(cr);
292 }
293 cairo_set_source_rgba (cr, withcolor.red,
294 withcolor.green,
295 withcolor.blue, hopac);
296 cairo_rectangle (cr, x1, y1, x2, y2);
297 cairo_fill(cr);
298 cairo_set_source_rgba (cr, withcolor.red,
299 withcolor.green,
300 withcolor.blue, 1.0);
301 y2 = y_min;
302 if (cxy[1] <= 0.0)
303 {
304 y2 = y2 - cxy[1] * YDRAW / ymax;
305 }
306 cairo_move_to (cr, x1, y2);
307 cairo_line_to (cr, x1, y1);
308 cairo_move_to (cr, x1, y1);
309 x2 = plotdata[i][0] + hwidth * 0.5 * XDRAW / xmax;
310 if (pid) x2 -= hwidth * 0.5 * ((float)pid / (float)extra) * XDRAW / xmax;
311 if (x2 > x_max)
312 {
313 x2 = x_max;
314 cairo_line_to(cr, x2, y1);
315 cairo_move_to(cr, x2, y1);
316 }
317 else
318 {
319 cairo_line_to(cr, x2, y1);
320 cairo_move_to(cr, x2, y1);
321 cairo_line_to(cr, x2, y2);
322 }
323 cairo_stroke(cr);
324 }
325 }
326 }
327 }
328 }
329 cairo_stroke(cr);
330 g_free (plotdata);
331}
int xlog
Definition curve.c:75
double cxy[2]
Definition curve.c:72
double xmax
Definition curve.c:68
int ylog
Definition curve.c:75
int dylog
Definition curve.c:76
double YDRAW
Definition curve.c:66
curve_dash * selectdash(int iddash)
setup dash pointer
Definition curve.c:135
double XDRAW
Definition curve.c:66
double y_max
Definition curve.c:70
double x_max
Definition curve.c:69
double y_min
Definition curve.c:70
int dxlog
Definition curve.c:76
double x_min
Definition curve.c:69
double ymax
Definition curve.c:68
Variable declarations for the curve widget Functions for interactions with the curve widget.
void draw_glyph(cairo_t *in, int theglyph, double x, double y, ColRGBA gcolor, double size)
draw glyph at (x,y)
Definition glyph.c:88
void draw_curve(cairo_t *cr, int cid, int rid, project *this_proj, int points, ColRGBA withcolor, int xscale, int yscale, int asp, int vdash, double thick, int glyp, double gize, int freq, double hwidth, double hopac, int hpos, int extra, int pid)
draw target curve to the cairo drawing context
Definition draw.c:85
double ** allocddouble(int xal, int yal)
allocate a double ** pointer
Definition global.c:462
Global variable declarations Global convenience function declarations Global data structure defin...
#define RIN
Definition global.h:343
image * plot
Definition ogl_draw.c:72
double y
Definition ogl_draw.c:63
double x
Definition ogl_draw.c:63
float blue
Definition global.h:138
float red
Definition global.h:136
float green
Definition global.h:137