Debian Codegraph @

src/pcolor.cpp

raw symbols SCIP
1 /* -*- coding: utf-8;mode:c++;c-file-style:"stroustrup" -*- */
2 /*
3 Licensed under the terms of the BSD 3-Clause
4 (see plotpy/__init__.py for details)
5 */
6 #include <Python.h>
7 #define NO_IMPORT_ARRAY
8 #define PY_ARRAY_UNIQUE_SYMBOL PyScalerArray
9 #include <numpy/arrayobject.h>
10 #ifdef _MSC_VER
11 #include <float.h>
12 #else
13 #include <fenv.h>
14 #endif
15 #include <math.h>
16 #if defined(_MSC_VER) || defined(__MINGW32__)
17 #define isnan(x) _isnan(x)
18 #endif
19 #include <stdio.h>
20 #include <algorithm>
21 #include <vector>
22 #include "arrays.hpp"
23 #include "scaler.hpp"
24
25 using std::max;
26 using std::min;
27 using std::swap;
28 using std::vector;
29
30 #if 0
31 /** return min(max(a,b,c,d),bound) */
32 static int max4(int a, int b, int c, int d, int bound)
33 {
34 int x, y, z;
35 x = (a>b ? a : b);
36 y = (c>d ? c : d);
37 z = (x>y ? x : y);
38 return (z>bound ? bound : z);
39 }
40
41 /** return max(min(a,b,c,d),bound) */
42 static int min4(int a, int b, int c, int d, int bound)
43 {
44 int x, y, z;
45 x = (a<b ? a : b);
46 y = (c<d ? c : d);
47 z = (x<y ? x : y);
48 return (z<bound ? bound : z);
49 }
50 #endif
51
52 static bool vert_line(double _x0, double _y0, double _x1, double _y1, int NX,
53 vector<int> &imin, vector<int> &imax,
54 bool draw, npy_uint32 col, Array2D<npy_uint32> &D)
55 {
56 int x0 = lrint(_x0);
57 int y0 = lrint(_y0);
58 int x1 = lrint(_x1);
59 int y1 = lrint(_y1);
60 int dx = abs(x1 - x0);
61 int dy = abs(y1 - y0);
62 int sx, sy;
63 int NY = imin.size() - 1;
64 int err, e2;
65 bool visible = false;
66 NX = NX - 1;
67 if (x0 < x1)
68 sx = 1;
69 else
70 sx = -1;
71 if (y0 < y1)
72 sy = 1;
73 else
74 sy = -1;
75 err = dx - dy;
76
77 do
78 {
79 if (y0 >= 0 && y0 <= NY)
80 {
81 int _min = min(imin[y0], x0);
82 int _max = max(imax[y0], x0);
83 if (draw)
84 {
85 if (x0 >= 0 && x0 <= NX)
86 {
87 D.value(x0, y0) = col;
88 }
89 }
90 imin[y0] = max(0, _min);
91 imax[y0] = min(NX, _max);
92 if (_min <= NX && _max >= 0)
93 {
94 visible = true;
95 }
96 }
97 if ((x0 == x1) && (y0 == y1))
98 break;
99 e2 = 2 * err;
100 if (e2 > -dy)
101 {
102 err = err - dy;
103 x0 = x0 + sx;
104 }
105 if (e2 < dx)
106 {
107 err = err + dx;
108 y0 = y0 + sy;
109 }
110 } while (true);
111 return visible;
112 }
113
114 template <class T>
115 struct QuadHelper
116 {
117 const Array2D<T> &X;
118 const Array2D<T> &Y;
119 const Array2D<T> &Z;
120 Array2D<npy_uint32> &D;
121 LutScale<T, npy_uint32> &scale;
122 double x1, x2, y1, y2, m_dx, m_dy;
123 npy_uint32 bgcolor;
124 bool border;
125 bool flat;
126 double uflat, vflat;
127 int ixmin, ixmax, iymin, iymax;
128
129 QuadHelper(const Array2D<T> &X_,
130 const Array2D<T> &Y_,
131 const Array2D<T> &Z_,
132 Array2D<npy_uint32> &D_,
133 LutScale<T, npy_uint32> &scale_,
134 double x1_, double x2_, double y1_, double y2_,
135 bool _border, bool _flat,
136 double _uflat, double _vflat) : X(X_), Y(Y_), Z(Z_), D(D_), scale(scale_),
137 x1(x1_), x2(x2_), y1(y1_), y2(y2_),
138 bgcolor(0xff000000),
139 border(_border),
140 flat(_flat), uflat(_uflat), vflat(_vflat)
141 {
142 m_dx = D.nj / (x2 - x1);
143 m_dy = D.ni / (y2 - y1);
144 }
145
146 void draw_triangles()
147 {
148 int i, j;
149 vector<int> imin, imax;
150 imin.resize(D.ni);
151 imax.resize(D.ni);
152 ixmin = D.nj;
153 iymin = D.ni;
154 ixmax = -1;
155 iymax = -1;
156 for (i = 0; i < X.ni - 1; ++i)
157 {
158 for (j = 0; j < X.nj - 1; ++j)
159 {
160 draw_quad(i, j, imin, imax);
161 }
162 }
163 }
164
165 void draw_quad(int qi, int qj,
166 vector<int> &imin, vector<int> &imax)
167 {
168 int i, j;
169 double u, v;
170 double v0, v1, v2, v3, v4;
171
172 // Coordonnees du quad dans l'offscreen
173 double ax = (X.value(qj + 0, qi + 0) - x1) * m_dx, ay = (Y.value(qj + 0, qi + 0) - y1) * m_dy;
174 double bx = (X.value(qj + 0, qi + 1) - x1) * m_dx, by = (Y.value(qj + 0, qi + 1) - y1) * m_dy;
175 double cx = (X.value(qj + 1, qi + 1) - x1) * m_dx, cy = (Y.value(qj + 1, qi + 1) - y1) * m_dy;
176 double dx = (X.value(qj + 1, qi + 0) - x1) * m_dx, dy = (Y.value(qj + 1, qi + 0) - y1) * m_dy;
177
178 // indice des sommets (A,B,C,D)<->0,1,2,3<->(qi,qj),(qi+1,qj),(qi+1,qj+1),(qi,qj+1)
179 // trie par ordre x croissant ou y croissant (selon xarg, yarg)
180 double ymin = min(ay, min(by, min(cy, dy)));
181 double ymax = max(ay, max(by, max(cy, dy)));
182
183 int i0 = int(ymin + .5);
184 int i1 = int(ymax + .5);
185 // printf("Quads: i=%d->%d\n", i0, i1);
186
187 if (i0 < 0)
188 i0 = 0;
189 if (i1 >= D.ni)
190 i1 = D.ni - 1;
191 if (i1 < i0)
192 return;
193
194 iymin = min(iymin, i0);
195 iymax = max(iymax, i1);
196 for (i = i0; i <= i1; ++i)
197 {
198 imax[i] = -1;
199 imin[i] = D.nj;
200 }
201
202 // Compute the rasterized border of the quad
203 bool visible = false;
204 visible |= vert_line(ax, ay, bx, by, D.nj, imin, imax, border, 0xff000000, D);
205 visible |= vert_line(bx, by, cx, cy, D.nj, imin, imax, border, 0xff000000, D);
206 visible |= vert_line(cx, cy, dx, dy, D.nj, imin, imax, border, 0xff000000, D);
207 visible |= vert_line(dx, dy, ax, ay, D.nj, imin, imax, border, 0xff000000, D);
208 if (!visible)
209 return;
210
211 double ex = ax + cx - dx - bx;
212 double ey = ay + cy - dy - by;
213 double n = 1. / sqrt((cx - ax) * (cx - ax) + (cy - ay) * (cy - ay));
214 if (n > 1e2)
215 n = 1.0;
216
217 // Normalize vectors with ||AC||
218 ax *= n;
219 ay *= n;
220 bx = bx * n - ax;
221 by = by * n - ay;
222 cx = cx * n - ax;
223 cy = cy * n - ay;
224 dx = dx * n - ax;
225 dy = dy * n - ay;
226 ex *= n;
227 ey *= n;
228
229 v1 = Z.value(qj, qi);
230 v2 = Z.value(qj + 1, qi);
231 v3 = Z.value(qj + 1, qi + 1);
232 v4 = Z.value(qj, qi + 1);
233
234 if (isnan(v1) || isnan(v2) || isnan(v3) || isnan(v4))
235 {
236 // XXX Color = Alpha
237 return;
238 }
239 int dm = 0, dM = 0;
240 if (border)
241 {
242 dm = 1;
243 dM = -1;
244 }
245 npy_uint32 col = scale.eval(v1 * (1 - vflat) * (1 - uflat) +
246 v2 * vflat * (1 - uflat) +
247 v3 * vflat * uflat +
248 v4 * (1 - vflat) * uflat);
249 for (i = i0 + dm; i <= i1 + dM; ++i)
250 {
251 ixmin = min(ixmin, imin[i]);
252 ixmax = max(ixmax, imax[i]);
253 int jmin = max(0, imin[i]) + dm;
254 int jmax = min(imax[i], D.nj - 1) + dM;
255 for (j = jmin; j <= jmax; ++j)
256 {
257 if (!flat)
258 {
259 params(j * n, i * n, ax, ay, bx, by, cx, cy, dx, dy, ex, ey, u, v);
260 if (u < 0)
261 u = 0.;
262 else if (u > 1.)
263 u = 1.;
264 if (v < 0)
265 v = 0.;
266 else if (v > 1.)
267 v = 1.;
268 /* v0 = v1*(1-v)*(1-u) + v2*v*(1-u) + v3*v*u + v4*(1-v)*u; */
269 v0 = u * (v * (v1 - v2 + v3 - v4) + v4 - v1) + v * (v2 - v1) + v1;
270 col = scale.eval(v0);
271 }
272 D.value(j, i) = col;
273 }
274 }
275 }
276
277 void params(double x, double y,
278 double ax, double ay,
279 double bx, double by,
280 double cx, double cy,
281 double dx, double dy,
282 double ex, double ey,
283 double &u, double &v)
284 {
285 /* solves AM=u.AB+v.AD+uv.AE with A,B,C,D quad, AE=DC+BA
286 M = (x,y)
287 with u^2.(AB^AE) +u.(AB^AD+AE^AM)+AD^AM=0
288 v = (AM-u.AB)/(AD+u.AE)
289 */
290
291 double mx = x - ax, my = y - ay;
292 double a1, a2, b, c, delta;
293
294 if (false && (ex * ex + ey * ey) < 1e-8)
295 {
296 // fast case : parallelogram
297 if (fabs(dy) > 1e-16)
298 {
299 double a = dx / dy;
300 u = (mx - a * y) / (bx - a * by);
301 v = (my - u * by) / dy;
302 return;
303 }
304 else
305 {
306 double a = dy / dx;
307 u = (my - a * x) / (by - a * bx);
308 v = (mx - u * bx) / dx;
309 return;
310 }
311 }
312 a1 = bx * ey - ex * by;
313 a2 = dx * ey - ex * dy;
314 if (a1 > a2)
315 {
316 b = bx * dy - dx * by + ex * my - mx * ey;
317 c = dx * my - mx * dy;
318 if (fabs(a1) > 1e-8)
319 {
320 delta = b * b - 4 * a1 * c;
321 u = (-b + sqrt(delta)) / (2 * a1);
322 }
323 else
324 {
325 u = -c / b;
326 }
327 double den = (dx + u * ex);
328 if (fabs(den) > 1e-8)
329 {
330 v = (mx - u * bx) / den;
331 }
332 else
333 {
334 den = (dy + u * ey);
335 v = (my - u * by) / den;
336 }
337 }
338 else
339 {
340 b = dx * by - bx * dy + ex * my - mx * ey;
341 c = bx * my - mx * by;
342 if (fabs(a2) > 1e-8)
343 {
344 delta = b * b - 4 * a2 * c;
345 v = (-b + sqrt(delta)) / (2 * a2);
346 }
347 else
348 {
349 v = -c / b;
350 }
351 double den = (bx + v * ex);
352 if (fabs(den) > 1e-8)
353 {
354 u = (mx - v * dx) / den;
355 }
356 else
357 {
358 den = (by + v * ey);
359 u = (my - v * dy) / den;
360 }
361 }
362 #if 0
363 if (isnan(u)) {
364 printf("AM=(%g,%g)\n", mx, my);
365 printf("AB=(%g,%g)\n", bx, by);
366 printf("AC=(%g,%g)\n", cx, cy);
367 printf("AD=(%g,%g)\n", dx, dy);
368 printf("AE=(%g,%g)\n", ex, ey);
369 printf("a1=%g, a2=%g, b=%g, c=%g\n", a1, a2, b, c);
370 printf("u=%g v=%g\n", u, v);
371 }
372 #endif
373 }
374 };
375
376 /**
377 Draw a structured grid composed of quads (xy[i,j],xy[i+1,j],xy[i+1,j+1],xy[i,j+1] )
378 */
379 PyObject *py_scale_quads(PyObject *self, PyObject *args)
380 {
381 PyArrayObject *p_src_x = 0, *p_src_y = 0, *p_src_z = 0, *p_dst = 0;
382 PyObject *p_lut_data, *p_dst_data, *p_interp_data, *p_src_data;
383 double x1, x2, y1, y2;
384 int border = 0, flat = 0;
385 double uflat = 0.5;
386 double vflat = 0.5;
387
388 if (!PyArg_ParseTuple(args, "OOOOOOOO|i:_scale_quads",
389 &p_src_x, &p_src_y, &p_src_z, &p_src_data,
390 &p_dst, &p_dst_data,
391 &p_lut_data, &p_interp_data,
392 &border))
393 {
394 return NULL;
395 }
396 if (!PyArg_ParseTuple(p_interp_data, "i|dd", &flat, &uflat, &vflat))
397 {
398 PyErr_SetString(PyExc_ValueError, "Interpolation should be a tuple (type[,uflat,vflat])");
399 return NULL;
400 }
401 if (!check_arrays(p_src_x, p_dst))
402 {
403 return NULL;
404 }
405 if (!PyArg_ParseTuple(p_src_data, "dddd:_scale_quads",
406 &x1, &y1, &x2, &y2))
407 {
408 return NULL;
409 }
410 if (PyArray_TYPE(p_src_x) != NPY_FLOAT64 ||
411 PyArray_TYPE(p_src_y) != NPY_FLOAT64 ||
412 PyArray_TYPE(p_src_z) != NPY_FLOAT64)
413 {
414 PyErr_SetString(PyExc_TypeError, "Only support float X,Y,Z");
415 return NULL;
416 }
417 if (PyArray_TYPE(p_dst) != NPY_UINT32)
418 {
419 PyErr_SetString(PyExc_TypeError, "Only support RGB dest for now");
420 return NULL;
421 }
422
423 double a = 1.0, b = 0.0;
424 PyObject *p_bg;
425 PyArrayObject *p_cmap;
426 bool apply_bg = true;
427 if (!PyArg_ParseTuple(p_lut_data, "ddO|O", &a, &b, &p_bg, &p_cmap))
428 {
429 PyErr_SetString(PyExc_ValueError, "Can't interpret pixel transformation tuple");
430 return NULL;
431 }
432 if (p_bg == Py_None)
433 apply_bg = false;
434
435 Array2D<double> X(p_src_x), Y(p_src_y), Z(p_src_z);
436 /* Destination is RGB */
437 unsigned long bg = 0;
438 Array2D<npy_uint32> dest(p_dst);
439 if (apply_bg)
440 {
441 #if PY_MAJOR_VERSION >= 3
442 bg = PyLong_AsUnsignedLongMask(p_bg);
443 #else
444 bg = PyInt_AsUnsignedLongMask(p_bg);
445 #endif
446 if (PyErr_Occurred())
447 return NULL;
448 }
449 if (!check_lut(p_cmap))
450 {
451 return NULL;
452 }
453 Array1D<npy_uint32> cmap(p_cmap);
454 LutScale<npy_float64, npy_uint32> scale(a, b, cmap, bg, apply_bg);
455 QuadHelper<double> quad(X, Y, Z, dest, scale, x1, x2, y1, y2, border, flat, uflat, vflat);
456
457 quad.draw_triangles();
458
459 // examine source type
460 return Py_BuildValue("iiii", quad.ixmin, quad.iymin, quad.ixmax, quad.iymax);
461 }
462
463 PyObject *py_vert_line(PyObject *self, PyObject *args)
464 {
465 double x0, y0, x1, y1;
466 int xmax;
467 PyArrayObject *p_min, *p_max;
468
469 if (!PyArg_ParseTuple(args, "ddddiOO:_vert_line", &x0, &y0, &x1, &y1, &xmax, &p_min, &p_max))
470 {
471 return NULL;
472 }
473 if (!PyArray_Check(p_min) ||
474 !PyArray_Check(p_max))
475 {
476 PyErr_SetString(PyExc_TypeError, "imin, imax must be ndarray");
477 return NULL;
478 }
479 if (PyArray_TYPE(p_min) != NPY_INT32 ||
480 PyArray_TYPE(p_max) != NPY_INT32)
481 {
482 PyErr_SetString(PyExc_TypeError, "imin, imax must be int ndarray");
483 return NULL;
484 }
485 Array1D<int> pmin(p_min), pmax(p_max);
486 vector<int> imin, imax;
487 int nx = int(max(y0, y1)) + 1;
488 if (pmin.ni < nx || pmax.ni < nx)
489 {
490 PyErr_SetString(PyExc_TypeError, "imin, imax not large enough");
491 return NULL;
492 }
493 if (y0 < 0 || y1 < 0)
494 {
495 PyErr_SetString(PyExc_ValueError, "y bounds must be positive");
496 }
497 imin.resize(nx);
498 imax.resize(nx);
499 for (int i = 0; i < nx; ++i)
500 {
501 imin[i] = pmin.value(i);
502 imax[i] = pmax.value(i);
503 }
504 Array2D<npy_uint32> dummy;
505 vert_line(x0, y0, x1, y1, xmax, imin, imax, false, 0, dummy);
506 for (int i = 0; i < nx; ++i)
507 {
508 pmin.value(i) = imin[i];
509 pmax.value(i) = imax[i];
510 }
511 Py_INCREF(Py_None);
512 return Py_None;
513 }