transform.hpp 15 KB

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