annotate DEPENDENCIES/generic/include/boost/polygon/transform.hpp @ 16:2665513ce2d3

Add boost headers
author Chris Cannam
date Tue, 05 Aug 2014 11:11:38 +0100
parents
children c530137014c0
rev   line source
Chris@16 1 // Boost.Polygon library point_data.hpp header file
Chris@16 2
Chris@16 3 // Copyright (c) Intel Corporation 2008.
Chris@16 4 // Copyright (c) 2008-2012 Simonson Lucanus.
Chris@16 5 // Copyright (c) 2012-2012 Andrii Sydorchuk.
Chris@16 6
Chris@16 7 // See http://www.boost.org for updates, documentation, and revision history.
Chris@16 8 // Use, modification and distribution is subject to the Boost Software License,
Chris@16 9 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
Chris@16 10 // http://www.boost.org/LICENSE_1_0.txt)
Chris@16 11
Chris@16 12 #ifndef BOOST_POLYGON_TRANSFORM_HPP
Chris@16 13 #define BOOST_POLYGON_TRANSFORM_HPP
Chris@16 14
Chris@16 15 #include "isotropy.hpp"
Chris@16 16
Chris@16 17 namespace boost {
Chris@16 18 namespace polygon {
Chris@16 19 // Transformation of Coordinate System.
Chris@16 20 // Enum meaning:
Chris@16 21 // Select which direction_2d to change the positive direction of each
Chris@16 22 // axis in the old coordinate system to map it to the new coordiante system.
Chris@16 23 // The first direction_2d listed for each enum is the direction to map the
Chris@16 24 // positive horizontal direction to.
Chris@16 25 // The second direction_2d listed for each enum is the direction to map the
Chris@16 26 // positive vertical direction to.
Chris@16 27 // The zero position bit (LSB) indicates whether the horizontal axis flips
Chris@16 28 // when transformed.
Chris@16 29 // The 1st postion bit indicates whether the vertical axis flips when
Chris@16 30 // transformed.
Chris@16 31 // The 2nd position bit indicates whether the horizontal and vertical axis
Chris@16 32 // swap positions when transformed.
Chris@16 33 // Enum Values:
Chris@16 34 // 000 EAST NORTH
Chris@16 35 // 001 WEST NORTH
Chris@16 36 // 010 EAST SOUTH
Chris@16 37 // 011 WEST SOUTH
Chris@16 38 // 100 NORTH EAST
Chris@16 39 // 101 SOUTH EAST
Chris@16 40 // 110 NORTH WEST
Chris@16 41 // 111 SOUTH WEST
Chris@16 42 class axis_transformation {
Chris@16 43 public:
Chris@16 44 enum ATR {
Chris@16 45 NULL_TRANSFORM = 0,
Chris@16 46 BEGIN_TRANSFORM = 0,
Chris@16 47 EN = 0, EAST_NORTH = 0,
Chris@16 48 WN = 1, WEST_NORTH = 1, FLIP_X = 1,
Chris@16 49 ES = 2, EAST_SOUTH = 2, FLIP_Y = 2,
Chris@16 50 WS = 3, WEST_SOUTH = 3, FLIP_XY = 3,
Chris@16 51 NE = 4, NORTH_EAST = 4, SWAP_XY = 4,
Chris@16 52 SE = 5, SOUTH_EAST = 5, ROTATE_LEFT = 5,
Chris@16 53 NW = 6, NORTH_WEST = 6, ROTATE_RIGHT = 6,
Chris@16 54 SW = 7, SOUTH_WEST = 7, FLIP_SWAP_XY = 7,
Chris@16 55 END_TRANSFORM = 7
Chris@16 56 };
Chris@16 57
Chris@16 58 // Individual axis enum values indicate which axis an implicit individual
Chris@16 59 // axis will be mapped to.
Chris@16 60 // The value of the enum paired with an axis provides the information
Chris@16 61 // about what the axis will transform to.
Chris@16 62 // Three individual axis values, one for each axis, are equivalent to one
Chris@16 63 // ATR enum value, but easier to work with because they are independent.
Chris@16 64 // Converting to and from the individual axis values from the ATR value
Chris@16 65 // is a convenient way to implement tranformation related functionality.
Chris@16 66 // Enum meanings:
Chris@16 67 // PX: map to positive x axis
Chris@16 68 // NX: map to negative x axis
Chris@16 69 // PY: map to positive y axis
Chris@16 70 // NY: map to negative y axis
Chris@16 71 enum INDIVIDUAL_AXIS {
Chris@16 72 PX = 0,
Chris@16 73 NX = 1,
Chris@16 74 PY = 2,
Chris@16 75 NY = 3
Chris@16 76 };
Chris@16 77
Chris@16 78 axis_transformation() : atr_(NULL_TRANSFORM) {}
Chris@16 79 explicit axis_transformation(ATR atr) : atr_(atr) {}
Chris@16 80 axis_transformation(const axis_transformation& atr) : atr_(atr.atr_) {}
Chris@16 81
Chris@16 82 explicit axis_transformation(const orientation_2d& orient) {
Chris@16 83 const ATR tmp[2] = {
Chris@16 84 NORTH_EAST, // sort x, then y
Chris@16 85 EAST_NORTH // sort y, then x
Chris@16 86 };
Chris@16 87 atr_ = tmp[orient.to_int()];
Chris@16 88 }
Chris@16 89
Chris@16 90 explicit axis_transformation(const direction_2d& dir) {
Chris@16 91 const ATR tmp[4] = {
Chris@16 92 SOUTH_EAST, // sort x, then y
Chris@16 93 NORTH_EAST, // sort x, then y
Chris@16 94 EAST_SOUTH, // sort y, then x
Chris@16 95 EAST_NORTH // sort y, then x
Chris@16 96 };
Chris@16 97 atr_ = tmp[dir.to_int()];
Chris@16 98 }
Chris@16 99
Chris@16 100 // assignment operator
Chris@16 101 axis_transformation& operator=(const axis_transformation& a) {
Chris@16 102 atr_ = a.atr_;
Chris@16 103 return *this;
Chris@16 104 }
Chris@16 105
Chris@16 106 // assignment operator
Chris@16 107 axis_transformation& operator=(const ATR& atr) {
Chris@16 108 atr_ = atr;
Chris@16 109 return *this;
Chris@16 110 }
Chris@16 111
Chris@16 112 // equivalence operator
Chris@16 113 bool operator==(const axis_transformation& a) const {
Chris@16 114 return atr_ == a.atr_;
Chris@16 115 }
Chris@16 116
Chris@16 117 // inequivalence operator
Chris@16 118 bool operator!=(const axis_transformation& a) const {
Chris@16 119 return !(*this == a);
Chris@16 120 }
Chris@16 121
Chris@16 122 // ordering
Chris@16 123 bool operator<(const axis_transformation& a) const {
Chris@16 124 return atr_ < a.atr_;
Chris@16 125 }
Chris@16 126
Chris@16 127 // concatenate this with that
Chris@16 128 axis_transformation& operator+=(const axis_transformation& a) {
Chris@16 129 bool abit2 = (a.atr_ & 4) != 0;
Chris@16 130 bool abit1 = (a.atr_ & 2) != 0;
Chris@16 131 bool abit0 = (a.atr_ & 1) != 0;
Chris@16 132 bool bit2 = (atr_ & 4) != 0;
Chris@16 133 bool bit1 = (atr_ & 2) != 0;
Chris@16 134 bool bit0 = (atr_ & 1) != 0;
Chris@16 135 int indexes[2][2] = {
Chris@16 136 { (int)bit2, (int)(!bit2) },
Chris@16 137 { (int)abit2, (int)(!abit2) }
Chris@16 138 };
Chris@16 139 int zero_bits[2][2] = {
Chris@16 140 {bit0, bit1}, {abit0, abit1}
Chris@16 141 };
Chris@16 142 int nbit1 = zero_bits[0][1] ^ zero_bits[1][indexes[0][1]];
Chris@16 143 int nbit0 = zero_bits[0][0] ^ zero_bits[1][indexes[0][0]];
Chris@16 144 indexes[0][0] = indexes[1][indexes[0][0]];
Chris@16 145 indexes[0][1] = indexes[1][indexes[0][1]];
Chris@16 146 int nbit2 = indexes[0][0] & 1; // swap xy
Chris@16 147 atr_ = (ATR)((nbit2 << 2) + (nbit1 << 1) + nbit0);
Chris@16 148 return *this;
Chris@16 149 }
Chris@16 150
Chris@16 151 // concatenation operator
Chris@16 152 axis_transformation operator+(const axis_transformation& a) const {
Chris@16 153 axis_transformation retval(*this);
Chris@16 154 return retval+=a;
Chris@16 155 }
Chris@16 156
Chris@16 157 // populate_axis_array writes the three INDIVIDUAL_AXIS values that the
Chris@16 158 // ATR enum value of 'this' represent into axis_array
Chris@16 159 void populate_axis_array(INDIVIDUAL_AXIS axis_array[]) const {
Chris@16 160 bool bit2 = (atr_ & 4) != 0;
Chris@16 161 bool bit1 = (atr_ & 2) != 0;
Chris@16 162 bool bit0 = (atr_ & 1) != 0;
Chris@16 163 axis_array[1] = (INDIVIDUAL_AXIS)(((int)(!bit2) << 1) + bit1);
Chris@16 164 axis_array[0] = (INDIVIDUAL_AXIS)(((int)(bit2) << 1) + bit0);
Chris@16 165 }
Chris@16 166
Chris@16 167 // it is recommended that the directions stored in an array
Chris@16 168 // in the caller code for easier isotropic access by orientation value
Chris@16 169 void get_directions(direction_2d& horizontal_dir,
Chris@16 170 direction_2d& vertical_dir) const {
Chris@16 171 bool bit2 = (atr_ & 4) != 0;
Chris@16 172 bool bit1 = (atr_ & 2) != 0;
Chris@16 173 bool bit0 = (atr_ & 1) != 0;
Chris@16 174 vertical_dir = direction_2d((direction_2d_enum)(((int)(!bit2) << 1) + !bit1));
Chris@16 175 horizontal_dir = direction_2d((direction_2d_enum)(((int)(bit2) << 1) + !bit0));
Chris@16 176 }
Chris@16 177
Chris@16 178 // combine_axis_arrays concatenates this_array and that_array overwriting
Chris@16 179 // the result into this_array
Chris@16 180 static void combine_axis_arrays(INDIVIDUAL_AXIS this_array[],
Chris@16 181 const INDIVIDUAL_AXIS that_array[]) {
Chris@16 182 int indexes[2] = { this_array[0] >> 1, this_array[1] >> 1 };
Chris@16 183 int zero_bits[2][2] = {
Chris@16 184 { this_array[0] & 1, this_array[1] & 1 },
Chris@16 185 { that_array[0] & 1, that_array[1] & 1 }
Chris@16 186 };
Chris@16 187 this_array[0] = (INDIVIDUAL_AXIS)((int)this_array[0] |
Chris@16 188 ((int)zero_bits[0][0] ^
Chris@16 189 (int)zero_bits[1][indexes[0]]));
Chris@16 190 this_array[1] = (INDIVIDUAL_AXIS)((int)this_array[1] |
Chris@16 191 ((int)zero_bits[0][1] ^
Chris@16 192 (int)zero_bits[1][indexes[1]]));
Chris@16 193 }
Chris@16 194
Chris@16 195 // write_back_axis_array converts an array of three INDIVIDUAL_AXIS values
Chris@16 196 // to the ATR enum value and sets 'this' to that value
Chris@16 197 void write_back_axis_array(const INDIVIDUAL_AXIS this_array[]) {
Chris@16 198 int bit2 = ((int)this_array[0] & 2) != 0; // swap xy
Chris@16 199 int bit1 = ((int)this_array[1] & 1);
Chris@16 200 int bit0 = ((int)this_array[0] & 1);
Chris@16 201 atr_ = ATR((bit2 << 2) + (bit1 << 1) + bit0);
Chris@16 202 }
Chris@16 203
Chris@16 204 // behavior is deterministic but undefined in the case where illegal
Chris@16 205 // combinations of directions are passed in.
Chris@16 206 axis_transformation& set_directions(const direction_2d& horizontal_dir,
Chris@16 207 const direction_2d& vertical_dir) {
Chris@16 208 int bit2 = (static_cast<orientation_2d>(horizontal_dir).to_int()) != 0;
Chris@16 209 int bit1 = !(vertical_dir.to_int() & 1);
Chris@16 210 int bit0 = !(horizontal_dir.to_int() & 1);
Chris@16 211 atr_ = ATR((bit2 << 2) + (bit1 << 1) + bit0);
Chris@16 212 return *this;
Chris@16 213 }
Chris@16 214
Chris@16 215 // transform the three coordinates by reference
Chris@16 216 template <typename coordinate_type>
Chris@16 217 void transform(coordinate_type& x, coordinate_type& y) const {
Chris@16 218 int bit2 = (atr_ & 4) != 0;
Chris@16 219 int bit1 = (atr_ & 2) != 0;
Chris@16 220 int bit0 = (atr_ & 1) != 0;
Chris@16 221 x *= -((bit0 << 1) - 1);
Chris@16 222 y *= -((bit1 << 1) - 1);
Chris@16 223 predicated_swap(bit2 != 0, x, y);
Chris@16 224 }
Chris@16 225
Chris@16 226 // invert this axis_transformation
Chris@16 227 axis_transformation& invert() {
Chris@16 228 int bit2 = ((atr_ & 4) != 0);
Chris@16 229 int bit1 = ((atr_ & 2) != 0);
Chris@16 230 int bit0 = ((atr_ & 1) != 0);
Chris@16 231 // swap bit 0 and bit 1 if bit2 is 1
Chris@16 232 predicated_swap(bit2 != 0, bit0, bit1);
Chris@16 233 bit1 = bit1 << 1;
Chris@16 234 atr_ = (ATR)(atr_ & (32+16+8+4)); // mask away bit0 and bit1
Chris@16 235 atr_ = (ATR)(atr_ | bit0 | bit1);
Chris@16 236 return *this;
Chris@16 237 }
Chris@16 238
Chris@16 239 // get the inverse axis_transformation of this
Chris@16 240 axis_transformation inverse() const {
Chris@16 241 axis_transformation retval(*this);
Chris@16 242 return retval.invert();
Chris@16 243 }
Chris@16 244
Chris@16 245 private:
Chris@16 246 ATR atr_;
Chris@16 247 };
Chris@16 248
Chris@16 249 // Scaling object to be used to store the scale factor for each axis.
Chris@16 250 // For use by the transformation object, in that context the scale factor
Chris@16 251 // is the amount that each axis scales by when transformed.
Chris@16 252 template <typename scale_factor_type>
Chris@16 253 class anisotropic_scale_factor {
Chris@16 254 public:
Chris@16 255 anisotropic_scale_factor() {
Chris@16 256 scale_[0] = 1;
Chris@16 257 scale_[1] = 1;
Chris@16 258 }
Chris@16 259 anisotropic_scale_factor(scale_factor_type xscale,
Chris@16 260 scale_factor_type yscale) {
Chris@16 261 scale_[0] = xscale;
Chris@16 262 scale_[1] = yscale;
Chris@16 263 }
Chris@16 264
Chris@16 265 // get a component of the anisotropic_scale_factor by orientation
Chris@16 266 scale_factor_type get(orientation_2d orient) const {
Chris@16 267 return scale_[orient.to_int()];
Chris@16 268 }
Chris@16 269
Chris@16 270 // set a component of the anisotropic_scale_factor by orientation
Chris@16 271 void set(orientation_2d orient, scale_factor_type value) {
Chris@16 272 scale_[orient.to_int()] = value;
Chris@16 273 }
Chris@16 274
Chris@16 275 scale_factor_type x() const {
Chris@16 276 return scale_[HORIZONTAL];
Chris@16 277 }
Chris@16 278
Chris@16 279 scale_factor_type y() const {
Chris@16 280 return scale_[VERTICAL];
Chris@16 281 }
Chris@16 282
Chris@16 283 void x(scale_factor_type value) {
Chris@16 284 scale_[HORIZONTAL] = value;
Chris@16 285 }
Chris@16 286
Chris@16 287 void y(scale_factor_type value) {
Chris@16 288 scale_[VERTICAL] = value;
Chris@16 289 }
Chris@16 290
Chris@16 291 // concatination operator (convolve scale factors)
Chris@16 292 anisotropic_scale_factor operator+(const anisotropic_scale_factor& s) const {
Chris@16 293 anisotropic_scale_factor<scale_factor_type> retval(*this);
Chris@16 294 return retval += s;
Chris@16 295 }
Chris@16 296
Chris@16 297 // concatinate this with that
Chris@16 298 const anisotropic_scale_factor& operator+=(
Chris@16 299 const anisotropic_scale_factor& s) {
Chris@16 300 scale_[0] *= s.scale_[0];
Chris@16 301 scale_[1] *= s.scale_[1];
Chris@16 302 return *this;
Chris@16 303 }
Chris@16 304
Chris@16 305 // transform this scale with an axis_transform
Chris@16 306 anisotropic_scale_factor& transform(axis_transformation atr) {
Chris@16 307 direction_2d dirs[2];
Chris@16 308 atr.get_directions(dirs[0], dirs[1]);
Chris@16 309 scale_factor_type tmp[2] = {scale_[0], scale_[1]};
Chris@16 310 for (int i = 0; i < 2; ++i) {
Chris@16 311 scale_[orientation_2d(dirs[i]).to_int()] = tmp[i];
Chris@16 312 }
Chris@16 313 return *this;
Chris@16 314 }
Chris@16 315
Chris@16 316 // scale the two coordinates
Chris@16 317 template <typename coordinate_type>
Chris@16 318 void scale(coordinate_type& x, coordinate_type& y) const {
Chris@16 319 x = scaling_policy<coordinate_type>::round(
Chris@16 320 (scale_factor_type)x * get(HORIZONTAL));
Chris@16 321 y = scaling_policy<coordinate_type>::round(
Chris@16 322 (scale_factor_type)y * get(HORIZONTAL));
Chris@16 323 }
Chris@16 324
Chris@16 325 // invert this scale factor to give the reverse scale factor
Chris@16 326 anisotropic_scale_factor& invert() {
Chris@16 327 x(1/x());
Chris@16 328 y(1/y());
Chris@16 329 return *this;
Chris@16 330 }
Chris@16 331
Chris@16 332 private:
Chris@16 333 scale_factor_type scale_[2];
Chris@16 334 };
Chris@16 335
Chris@16 336 // Transformation object, stores and provides services for transformations.
Chris@16 337 // Consits of axis transformation, scale factor and translation.
Chris@16 338 // The tranlation is the position of the origin of the new coordinate system of
Chris@16 339 // in the old system. Coordinates are scaled before they are transformed.
Chris@16 340 template <typename coordinate_type>
Chris@16 341 class transformation {
Chris@16 342 public:
Chris@16 343 transformation() : atr_(), p_(0, 0) {}
Chris@16 344 explicit transformation(axis_transformation atr) : atr_(atr), p_(0, 0) {}
Chris@16 345 explicit transformation(axis_transformation::ATR atr) : atr_(atr), p_(0, 0) {}
Chris@16 346 transformation(const transformation& tr) : atr_(tr.atr_), p_(tr.p_) {}
Chris@16 347
Chris@16 348 template <typename point_type>
Chris@16 349 explicit transformation(const point_type& p) : atr_(), p_(0, 0) {
Chris@16 350 set_translation(p);
Chris@16 351 }
Chris@16 352
Chris@16 353 template <typename point_type>
Chris@16 354 transformation(axis_transformation atr,
Chris@16 355 const point_type& p) : atr_(atr), p_(0, 0) {
Chris@16 356 set_translation(p);
Chris@16 357 }
Chris@16 358
Chris@16 359 template <typename point_type>
Chris@16 360 transformation(axis_transformation atr,
Chris@16 361 const point_type& referencePt,
Chris@16 362 const point_type& destinationPt) : atr_(), p_(0, 0) {
Chris@16 363 transformation<coordinate_type> tmp(referencePt);
Chris@16 364 transformation<coordinate_type> rotRef(atr);
Chris@16 365 transformation<coordinate_type> tmpInverse = tmp.inverse();
Chris@16 366 point_type decon(referencePt);
Chris@16 367 deconvolve(decon, destinationPt);
Chris@16 368 transformation<coordinate_type> displacement(decon);
Chris@16 369 tmp += rotRef;
Chris@16 370 tmp += tmpInverse;
Chris@16 371 tmp += displacement;
Chris@16 372 (*this) = tmp;
Chris@16 373 }
Chris@16 374
Chris@16 375 // equivalence operator
Chris@16 376 bool operator==(const transformation& tr) const {
Chris@16 377 return (atr_ == tr.atr_) && (p_ == tr.p_);
Chris@16 378 }
Chris@16 379
Chris@16 380 // inequivalence operator
Chris@16 381 bool operator!=(const transformation& tr) const {
Chris@16 382 return !(*this == tr);
Chris@16 383 }
Chris@16 384
Chris@16 385 // ordering
Chris@16 386 bool operator<(const transformation& tr) const {
Chris@16 387 return (atr_ < tr.atr_) || ((atr_ == tr.atr_) && (p_ < tr.p_));
Chris@16 388 }
Chris@16 389
Chris@16 390 // concatenation operator
Chris@16 391 transformation operator+(const transformation& tr) const {
Chris@16 392 transformation<coordinate_type> retval(*this);
Chris@16 393 return retval+=tr;
Chris@16 394 }
Chris@16 395
Chris@16 396 // concatenate this with that
Chris@16 397 const transformation& operator+=(const transformation& tr) {
Chris@16 398 coordinate_type x, y;
Chris@16 399 transformation<coordinate_type> inv = inverse();
Chris@16 400 inv.transform(x, y);
Chris@16 401 p_.set(HORIZONTAL, p_.get(HORIZONTAL) + x);
Chris@16 402 p_.set(VERTICAL, p_.get(VERTICAL) + y);
Chris@16 403 // concatenate axis transforms
Chris@16 404 atr_ += tr.atr_;
Chris@16 405 return *this;
Chris@16 406 }
Chris@16 407
Chris@16 408 // get the axis_transformation portion of this
Chris@16 409 axis_transformation get_axis_transformation() const {
Chris@16 410 return atr_;
Chris@16 411 }
Chris@16 412
Chris@16 413 // set the axis_transformation portion of this
Chris@16 414 void set_axis_transformation(const axis_transformation& atr) {
Chris@16 415 atr_ = atr;
Chris@16 416 }
Chris@16 417
Chris@16 418 // get the translation
Chris@16 419 template <typename point_type>
Chris@16 420 void get_translation(point_type& p) const {
Chris@16 421 assign(p, p_);
Chris@16 422 }
Chris@16 423
Chris@16 424 // set the translation
Chris@16 425 template <typename point_type>
Chris@16 426 void set_translation(const point_type& p) {
Chris@16 427 assign(p_, p);
Chris@16 428 }
Chris@16 429
Chris@16 430 // apply the 2D portion of this transformation to the two coordinates given
Chris@16 431 void transform(coordinate_type& x, coordinate_type& y) const {
Chris@16 432 y -= p_.get(VERTICAL);
Chris@16 433 x -= p_.get(HORIZONTAL);
Chris@16 434 atr_.transform(x, y);
Chris@16 435 }
Chris@16 436
Chris@16 437 // invert this transformation
Chris@16 438 transformation& invert() {
Chris@16 439 coordinate_type x = p_.get(HORIZONTAL), y = p_.get(VERTICAL);
Chris@16 440 atr_.transform(x, y);
Chris@16 441 x *= -1;
Chris@16 442 y *= -1;
Chris@16 443 p_ = point_data<coordinate_type>(x, y);
Chris@16 444 atr_.invert();
Chris@16 445 return *this;
Chris@16 446 }
Chris@16 447
Chris@16 448 // get the inverse of this transformation
Chris@16 449 transformation inverse() const {
Chris@16 450 transformation<coordinate_type> ret_val(*this);
Chris@16 451 return ret_val.invert();
Chris@16 452 }
Chris@16 453
Chris@16 454 void get_directions(direction_2d& horizontal_dir,
Chris@16 455 direction_2d& vertical_dir) const {
Chris@16 456 return atr_.get_directions(horizontal_dir, vertical_dir);
Chris@16 457 }
Chris@16 458
Chris@16 459 private:
Chris@16 460 axis_transformation atr_;
Chris@16 461 point_data<coordinate_type> p_;
Chris@16 462 };
Chris@16 463 } // polygon
Chris@16 464 } // boost
Chris@16 465
Chris@16 466 #endif // BOOST_POLYGON_TRANSFORM_HPP