LineT.tcc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * NICE-Core - efficient algebra and computer vision methods
  3. * - libimage - An image/template for new NICE libraries
  4. * See file License for license information.
  5. */
  6. #include "core/image/LineT.h"
  7. namespace NICE {
  8. // Constructors:
  9. // -------------
  10. template<class P>
  11. LineT<P>::LineT(const Coord &_begin, const Coord &_end)
  12. {
  13. begin=_begin;
  14. end=_end;
  15. steep = (::abs(end.y - begin.y) > ::abs(end.x - begin.x));
  16. if(steep) {
  17. std::swap(begin.x, begin.y);
  18. std::swap(end.x, end.y);
  19. }
  20. if(begin.x > end.x) {
  21. std::swap(begin, end);
  22. }
  23. }
  24. template<class P>
  25. LineT<P>::LineT(const Coord &_begin, const Coord &_end,
  26. const ColorT<P>& _defaultColor)
  27. : Drawable<P>(_defaultColor)
  28. {
  29. begin=_begin;
  30. end=_end;
  31. steep= (::abs(end.y - begin.y) > ::abs(end.x - begin.x));
  32. if(steep) {
  33. std::swap(begin.x, begin.y);
  34. std::swap(end.x, end.y);
  35. }
  36. if(begin.x > end.x) {
  37. std::swap(begin, end);
  38. }
  39. }
  40. template<class P>
  41. LineT<P>::LineT(const LineT<P>& ex) : Drawable<P>(ex)
  42. {
  43. *this=ex;
  44. }
  45. // Operators:
  46. // ----------
  47. template<class P>
  48. LineT<P>& LineT<P>::operator=(const LineT<P>& ex)
  49. {
  50. begin = ex.begin;
  51. end = ex.end;
  52. this->defaultColor = ex.defaultColor;
  53. return *this;
  54. }
  55. template<class P>
  56. bool LineT<P>::operator==(const LineT<P>& ex) const
  57. {
  58. if(begin==ex.begin && end==ex.end &&
  59. this->defaultColor==ex.defaultColor)
  60. return true;
  61. return false;
  62. }
  63. template<class P>
  64. bool LineT<P>::operator!=(const LineT<P>& ex) const {
  65. return !(this->operator==(ex));
  66. }
  67. // Methods:
  68. // --------
  69. template<class P>
  70. void LineT<P>::doDraw(ColorImageT<P> &image, const ColorT<P>& color) const
  71. {
  72. int maxx = image.width();
  73. int maxy = image.height();
  74. if(steep)
  75. {
  76. maxx = image.height();
  77. maxy = image.width();
  78. }
  79. Coord start=begin;
  80. Coord stop=end;
  81. if(start.x<0) start.x=0;
  82. if(stop.x>maxx) stop.x= maxx;
  83. int deltax = stop.x - start.x;
  84. int deltay = ::abs(stop.y - start.y);
  85. int error = 0;
  86. int y = start.y;
  87. int ystep;
  88. if(start.y < stop.y) {
  89. if(stop.y > maxy)
  90. stop.y = maxy;
  91. if(start.y<0)
  92. start.y=0;
  93. ystep = 1;
  94. } else {
  95. if(stop.y<0)
  96. stop.y=0;
  97. if(start.y > maxy)
  98. start.y = maxy;
  99. ystep = -1;
  100. }
  101. for(int x=start.x;x<=stop.x;x++) {
  102. if(steep) {
  103. image.setPixelSave(y,x,color[0],color[1],color[2]);
  104. } else {
  105. image.setPixelSave(x,y,color[0],color[1],color[2]);
  106. }
  107. error = error + deltay;
  108. if((error<<1) >= deltax) {
  109. y = y + ystep;
  110. error = error - deltax;
  111. }
  112. }
  113. }
  114. template<class P>
  115. void LineT<P>::doDraw(ImageT<P> &image, const P& gray) const
  116. {
  117. int maxx = image.width();
  118. int maxy = image.height();
  119. if(steep)
  120. {
  121. maxx = image.height();
  122. maxy = image.width();
  123. }
  124. Coord start=begin;
  125. Coord stop=end;
  126. if(start.x < 0) start.x = 0;
  127. if(stop.x > maxx) stop.x = maxx;
  128. int deltax = stop.x - start.x;
  129. int deltay = ::abs(stop.y - start.y);
  130. int error = 0;
  131. int y = start.y;
  132. int ystep;
  133. if(start.y < stop.y) {
  134. if(stop.y > maxy)
  135. stop.y = maxy;
  136. if(start.y<0)
  137. start.y=0;
  138. ystep = 1;
  139. } else {
  140. if(stop.y<0)
  141. stop.y=0;
  142. if(start.y > maxy)
  143. start.y = maxy;
  144. ystep = -1;
  145. }
  146. for(int x=start.x;x<=stop.x;x++) {
  147. if(steep) {
  148. image.setPixelSave(y,x,gray);
  149. } else {
  150. image.setPixelSave(x,y,gray);
  151. }
  152. error = error + deltay;
  153. if((error<<1) >= deltax) {
  154. y = y + ystep;
  155. error = error - deltax;
  156. }
  157. }
  158. }
  159. // Destructor:
  160. // -----------
  161. template<class P>
  162. LineT<P>::~LineT()
  163. {
  164. }
  165. }; // namespace NICE