| 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 | #ifndef __POINTS_HPP__ |
| 7 | #define __POINTS_HPP__ |
| 8 | |
| 9 | #include "traits.hpp" |
| 10 | |
| 11 | /* |
| 12 | This file defines several classes of 2D points |
| 13 | which are used by other algorithms |
| 14 | */ |
| 15 | |
| 16 | class Point |
| 17 | { |
| 18 | public: |
| 19 | typedef double real; |
| 20 | typedef num_trait<real> trait; |
| 21 | Point() : _ix(0), _iy(0), _x(0.0), _y(0.0) {} |
| 22 | real x() const { return _x; } |
| 23 | real y() const { return _y; } |
| 24 | int ix() const { return _ix; } |
| 25 | int iy() const { return _iy; } |
| 26 | |
| 27 | void setx(real x) |
| 28 | { |
| 29 | _x = x; |
| 30 | _ix = trait::toint(x); |
| 31 | } |
| 32 | void sety(real y) |
| 33 | { |
| 34 | _y = y; |
| 35 | _iy = trait::toint(y); |
| 36 | } |
| 37 | void set(real x, real y) |
| 38 | { |
| 39 | setx(x); |
| 40 | sety(y); |
| 41 | } |
| 42 | void copy(const Point &p) |
| 43 | { |
| 44 | _ix = p._ix; |
| 45 | _iy = p._iy; |
| 46 | _x = p._x; |
| 47 | _y = p._y; |
| 48 | } |
| 49 | Point &operator=(const Point &p) |
| 50 | { |
| 51 | copy(p); |
| 52 | return *this; |
| 53 | } |
| 54 | |
| 55 | protected: |
| 56 | int _ix, _iy; |
| 57 | real _x, _y; |
| 58 | }; |
| 59 | |
| 60 | /* A point that keep track of it's coordinates |
| 61 | as double and integers |
| 62 | */ |
| 63 | class Point2DRectilinear : public Point |
| 64 | { |
| 65 | public: |
| 66 | Point2DRectilinear() : _insidex(true), _insidey(true) {} |
| 67 | bool inside() const { return _insidex && _insidey; } |
| 68 | void testx(int _m, int _M) |
| 69 | { |
| 70 | if (_ix < _m || _ix >= _M) |
| 71 | { |
| 72 | _insidex = false; |
| 73 | } |
| 74 | else |
| 75 | { |
| 76 | _insidex = true; |
| 77 | } |
| 78 | } |
| 79 | void testy(int _m, int _M) |
| 80 | { |
| 81 | if (_iy < _m || _iy >= _M) |
| 82 | { |
| 83 | _insidey = false; |
| 84 | } |
| 85 | else |
| 86 | { |
| 87 | _insidey = true; |
| 88 | } |
| 89 | } |
| 90 | void copy(const Point2DRectilinear &p) |
| 91 | { |
| 92 | Point::copy(p); |
| 93 | _insidex = p._insidex; |
| 94 | _insidey = p._insidey; |
| 95 | } |
| 96 | Point2DRectilinear &operator=(const Point2DRectilinear &p) |
| 97 | { |
| 98 | copy(p); |
| 99 | return *this; |
| 100 | } |
| 101 | |
| 102 | protected: |
| 103 | bool _insidex, _insidey; |
| 104 | }; |
| 105 | |
| 106 | /* A special transformation operation that transforms |
| 107 | i,j (int) coordinates with a translation and scale |
| 108 | */ |
| 109 | class ScaleTransform |
| 110 | { |
| 111 | public: |
| 112 | typedef Point2DRectilinear point; |
| 113 | typedef point::real real; |
| 114 | |
| 115 | ScaleTransform(int _nx, int _ny, |
| 116 | real _x0, real _y0, |
| 117 | real _dx, real _dy) : nx(_nx), ny(_ny), x0(_x0), y0(_y0), dx(_dx), dy(_dy) {} |
| 118 | |
| 119 | void set(point &p, int x, int y) const |
| 120 | { |
| 121 | p.set(x0 + x * dx, y0 + y * dy); |
| 122 | p.testx(0, nx); |
| 123 | p.testy(0, ny); |
| 124 | } |
| 125 | void incx(point &p, real k = 1) const |
| 126 | { |
| 127 | p.setx(p.x() + k * dx); |
| 128 | p.testx(0, nx); |
| 129 | } |
| 130 | void incy(point &p, real k = 1) const |
| 131 | { |
| 132 | p.sety(p.y() + k * dy); |
| 133 | p.testy(0, ny); |
| 134 | } |
| 135 | |
| 136 | public: |
| 137 | int nx, ny; |
| 138 | real x0, y0; |
| 139 | real dx, dy; |
| 140 | }; |
| 141 | |
| 142 | class Point2D : public Point |
| 143 | { |
| 144 | public: |
| 145 | Point2D() : _inside(true) {} |
| 146 | bool inside() const { return _inside; } |
| 147 | void copy(const Point2D &p) |
| 148 | { |
| 149 | Point::copy(p); |
| 150 | _inside = p._inside; |
| 151 | } |
| 152 | Point2D &operator=(const Point2D &p) |
| 153 | { |
| 154 | copy(p); |
| 155 | return *this; |
| 156 | } |
| 157 | |
| 158 | void test(int _xm, int _xM, int _ym, int _yM) |
| 159 | { |
| 160 | if ((_ix < _xm) || (_ix >= _xM) || (_iy < _ym) || (_iy >= _yM)) |
| 161 | { |
| 162 | _inside = false; |
| 163 | } |
| 164 | else |
| 165 | { |
| 166 | _inside = true; |
| 167 | } |
| 168 | } |
| 169 | bool _inside; |
| 170 | }; |
| 171 | |
| 172 | class LinearTransform |
| 173 | { |
| 174 | public: |
| 175 | typedef Point2D point; |
| 176 | typedef point::real real; |
| 177 | |
| 178 | LinearTransform(int _nx, int _ny, |
| 179 | real _x0, real _y0, |
| 180 | real _xx, real _xy, |
| 181 | real _yx, real _yy) : nx(_nx), ny(_ny), |
| 182 | x0(_x0), y0(_y0), |
| 183 | xx(_xx), xy(_xy), |
| 184 | yx(_yx), yy(_yy) |
| 185 | { |
| 186 | } |
| 187 | void set(point &p, int x, int y) const |
| 188 | { |
| 189 | p.set(x0 + x * xx + y * xy, |
| 190 | y0 + x * yx + y * yy); |
| 191 | p.test(0, nx, 0, ny); |
| 192 | } |
| 193 | void incx(point &p, real k = 1) const |
| 194 | { |
| 195 | p.set(p.x() + k * xx, p.y() + k * yx); |
| 196 | p.test(0, nx, 0, ny); |
| 197 | } |
| 198 | void incy(point &p, real k = 1) const |
| 199 | { |
| 200 | p.set(p.x() + k * xy, p.y() + k * yy); |
| 201 | p.test(0, nx, 0, ny); |
| 202 | } |
| 203 | |
| 204 | public: |
| 205 | int nx, ny; |
| 206 | real x0, y0; |
| 207 | real xx, xy, yx, yy; |
| 208 | }; |
| 209 | |
| 210 | template <class axis_type> |
| 211 | class Point2DAxis : public Point |
| 212 | { |
| 213 | public: |
| 214 | Point2DAxis() : _insidex(true), _insidey(true) {} |
| 215 | bool inside() const { return _insidex && _insidey; } |
| 216 | |
| 217 | void set(const axis_type &ax, real x, |
| 218 | const axis_type &ay, real y) |
| 219 | { |
| 220 | setx(ax, x); |
| 221 | sety(ay, y); |
| 222 | } |
| 223 | void setx(const axis_type &ax, real x) |
| 224 | { |
| 225 | _ix = -1; |
| 226 | _x = x; |
| 227 | while (_ix < ax.ni - 1 && ax.value(_ix + 1) < x) |
| 228 | { |
| 229 | ++_ix; |
| 230 | } |
| 231 | } |
| 232 | void sety(const axis_type &ay, real y) |
| 233 | { |
| 234 | _iy = -1; |
| 235 | _y = y; |
| 236 | while (_iy < ay.ni - 1 && ay.value(_iy + 1) < y) |
| 237 | { |
| 238 | ++_iy; |
| 239 | } |
| 240 | } |
| 241 | void incx(const axis_type &ax, real dx) |
| 242 | { |
| 243 | _x += dx; |
| 244 | if (dx < 0) |
| 245 | { |
| 246 | while (_ix >= 0 && ax.value(_ix) >= _x) |
| 247 | { |
| 248 | --_ix; |
| 249 | } |
| 250 | } |
| 251 | else |
| 252 | { |
| 253 | while (_ix < ax.ni - 1 && ax.value(_ix + 1) < _x) |
| 254 | { |
| 255 | ++_ix; |
| 256 | } |
| 257 | } |
| 258 | } |
| 259 | void incy(const axis_type &ay, real dy) |
| 260 | { |
| 261 | _y += dy; |
| 262 | if (dy < 0) |
| 263 | { |
| 264 | while (_iy >= 0 && ay.value(_iy) >= _y) |
| 265 | { |
| 266 | --_iy; |
| 267 | } |
| 268 | } |
| 269 | else |
| 270 | { |
| 271 | while (_iy < ay.ni - 1 && ay.value(_iy + 1) < _y) |
| 272 | { |
| 273 | ++_iy; |
| 274 | } |
| 275 | } |
| 276 | } |
| 277 | void copy(const Point2DAxis<axis_type> &p) |
| 278 | { |
| 279 | Point::copy(p); |
| 280 | _insidex = p._insidex; |
| 281 | _insidey = p._insidey; |
| 282 | } |
| 283 | Point2DAxis<axis_type> &operator=(const Point2DAxis<axis_type> &p) |
| 284 | { |
| 285 | copy(p); |
| 286 | return *this; |
| 287 | } |
| 288 | |
| 289 | bool _insidex, _insidey; |
| 290 | }; |
| 291 | |
| 292 | template <class axis_type> |
| 293 | class XYTransform |
| 294 | { |
| 295 | public: |
| 296 | typedef Point2DAxis<axis_type> point; |
| 297 | typedef typename point::real real; |
| 298 | |
| 299 | XYTransform(int _nx, int _ny, |
| 300 | const axis_type &_ax, |
| 301 | const axis_type &_ay, |
| 302 | real _x0, real _y0, |
| 303 | real _dx, real _dy) : nx(_nx), ny(_ny), |
| 304 | ax(_ax), ay(_ay), |
| 305 | x0(_x0), y0(_y0), |
| 306 | dx(_dx), dy(_dy) {} |
| 307 | |
| 308 | void testx(point &p) const |
| 309 | { |
| 310 | if (p.ix() < 0 || p.ix() >= nx) |
| 311 | { |
| 312 | p._insidex = false; |
| 313 | } |
| 314 | else |
| 315 | { |
| 316 | p._insidex = true; |
| 317 | } |
| 318 | } |
| 319 | void testy(point &p) const |
| 320 | { |
| 321 | if (p.iy() < 0 || p.iy() >= ny) |
| 322 | { |
| 323 | p._insidey = false; |
| 324 | } |
| 325 | else |
| 326 | { |
| 327 | p._insidey = true; |
| 328 | } |
| 329 | } |
| 330 | void set(point &p, int x, int y) const |
| 331 | { |
| 332 | p.set(ax, x0 + x * dx, ay, y0 + y * dy); |
| 333 | testx(p); |
| 334 | testy(p); |
| 335 | } |
| 336 | void incx(point &p, real k = 1) const |
| 337 | { |
| 338 | p.incx(ax, k * dx); |
| 339 | testx(p); |
| 340 | } |
| 341 | void incy(point &p, real k = 1) const |
| 342 | { |
| 343 | p.incy(ay, k * dy); |
| 344 | testy(p); |
| 345 | } |
| 346 | |
| 347 | public: |
| 348 | int nx, ny; |
| 349 | const axis_type &ax; |
| 350 | const axis_type &ay; |
| 351 | real x0, y0; |
| 352 | real dx, dy; |
| 353 | }; |
| 354 | |
| 355 | #endif |