EllipseT.tcc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. /*****************************************************************************/
  7. #include "core/image/EllipseT.h"
  8. namespace NICE {
  9. // Constructors:
  10. // -------------
  11. template<class P>
  12. EllipseT<P>::EllipseT(const Coord &center, const Coord &axis, float angle)
  13. {
  14. this->center=center;
  15. this->axis=axis;
  16. this->angle=angle;
  17. }
  18. template<class P>
  19. EllipseT<P>::EllipseT(const Coord &center, const Coord &axis, float angle,
  20. const ColorT<P>& _defaultColor)
  21. : Drawable<P>(_defaultColor)
  22. {
  23. this->center=center;
  24. this->axis=axis;
  25. this->angle=angle;
  26. }
  27. template<class P>
  28. EllipseT<P>::EllipseT()
  29. {
  30. }
  31. template<class P>
  32. EllipseT<P>::EllipseT(const EllipseT<P>& ex) : Drawable<P>(ex)
  33. {
  34. *this=ex;
  35. }
  36. // Operators:
  37. // ----------
  38. template<class P>
  39. EllipseT<P>& EllipseT<P>::operator=(const EllipseT<P>& ex)
  40. {
  41. center=ex.center;
  42. axis=ex.axis;
  43. angle=ex.angle;
  44. this->defaultColor=ex.defaultColor;
  45. return *this;
  46. }
  47. template<class P>
  48. bool EllipseT<P>::operator==(const EllipseT<P>& ex) const
  49. {
  50. if(center==ex.center &&
  51. axis==ex.axis&&
  52. angle==ex.angle&&
  53. this->defaultColor==ex.defaultColor)
  54. return true;
  55. return false;
  56. }
  57. template<class P>
  58. bool EllipseT<P>::operator!=(const EllipseT<P>& ex) const {
  59. return !(this->operator==(ex));
  60. }
  61. // Methods:
  62. // --------
  63. template<class P>
  64. void EllipseT<P>::doDraw(ColorImageT<P> &image, const ColorT<P>& color) const
  65. {
  66. const int rx2 =(axis.x*axis.x);
  67. const int ry2 =(axis.y*axis.y);
  68. int F = (int)(ry2-rx2*axis.y+0.25*rx2+0.5);
  69. int ddF_x =0;
  70. int ddF_y =(2*rx2*axis.y);
  71. int x =0;
  72. int y =axis.y;
  73. float cosinus=cos(angle);
  74. float sinus=sin(angle);
  75. image.setPixelSave(center.x+int(cosinus*x-sinus*y), center.y+int(sinus*x+cosinus*y),color[0],color[1],color[2]);
  76. image.setPixelSave(center.x+int(cosinus*-x-sinus*y), center.y+int(sinus*-x+cosinus*y),color[0],color[1],color[2]);
  77. image.setPixelSave(center.x+int(cosinus*x+sinus*y), center.y+int(sinus*x-cosinus*y),color[0],color[1],color[2]);
  78. image.setPixelSave(center.x+int(cosinus*-x+sinus*y), center.y+int(sinus*-x-cosinus*y),color[0],color[1],color[2]);
  79. while( ddF_x < ddF_y ) {
  80. if(F >= 0) {
  81. y -= 1; // south
  82. ddF_y -= 2*rx2;
  83. F -= ddF_y;
  84. }
  85. x += 1; // east
  86. ddF_x += 2*ry2;
  87. F += ddF_x + ry2;
  88. image.setPixelSave(center.x+int(cosinus*x-sinus*y), center.y+int(sinus*x+cosinus*y),color[0],color[1],color[2]);
  89. image.setPixelSave(center.x+int(cosinus*-x-sinus*y), center.y+int(sinus*-x+cosinus*y),color[0],color[1],color[2]);
  90. image.setPixelSave(center.x+int(cosinus*x+sinus*y), center.y+int(sinus*x-cosinus*y),color[0],color[1],color[2]);
  91. image.setPixelSave(center.x+int(cosinus*-x+sinus*y), center.y+int(sinus*-x-cosinus*y),color[0],color[1],color[2]);
  92. }
  93. F = int(ry2*(x+0.5)*(x+0.5) + rx2*(y-1)*(y-1) - rx2*ry2+0.5);
  94. while( y > 0 ) {
  95. if(F <= 0) {
  96. x += 1; // east
  97. ddF_x += 2*ry2;
  98. F += ddF_x;
  99. }
  100. y -=1; // south
  101. ddF_y -= 2*rx2;
  102. F += rx2 - ddF_y;
  103. image.setPixelSave(center.x+int(cosinus*x-sinus*y), center.y+int(sinus*x+cosinus*y),color[0],color[1],color[2]);
  104. image.setPixelSave(center.x+int(cosinus*-x-sinus*y), center.y+int(sinus*-x+cosinus*y),color[0],color[1],color[2]);
  105. image.setPixelSave(center.x+int(cosinus*x+sinus*y), center.y+int(sinus*x-cosinus*y),color[0],color[1],color[2]);
  106. image.setPixelSave(center.x+int(cosinus*-x+sinus*y), center.y+int(sinus*-x-cosinus*y),color[0],color[1],color[2]);
  107. }
  108. }
  109. template<class P>
  110. void EllipseT<P>::doDraw(ImageT<P> &image, const P& gray) const
  111. {
  112. const int rx2 =(axis.x*axis.x);
  113. const int ry2 =(axis.y*axis.y);
  114. int F = (int)(ry2-rx2*axis.y+0.25*rx2+0.5);
  115. int ddF_x =0;
  116. int ddF_y =(2*rx2*axis.y);
  117. int x =0;
  118. int y =axis.y;
  119. float cosinus=cos(angle);
  120. float sinus=sin(angle);
  121. image.setPixelSave(center.x+int(cosinus*x-sinus*y), center.y+int(sinus*x+cosinus*y),gray);
  122. image.setPixelSave(center.x+int(cosinus*-x-sinus*y), center.y+int(sinus*-x+cosinus*y),gray);
  123. image.setPixelSave(center.x+int(cosinus*x+sinus*y), center.y+int(sinus*x-cosinus*y),gray);
  124. image.setPixelSave(center.x+int(cosinus*-x+sinus*y), center.y+int(sinus*-x-cosinus*y),gray);
  125. while( ddF_x < ddF_y ) {
  126. if(F >= 0) {
  127. y -= 1; // south
  128. ddF_y -= 2*rx2;
  129. F -= ddF_y;
  130. }
  131. x += 1; // east
  132. ddF_x += 2*ry2;
  133. F += ddF_x + ry2;
  134. image.setPixelSave(center.x+int(cosinus*x-sinus*y), center.y+int(sinus*x+cosinus*y),gray);
  135. image.setPixelSave(center.x+int(cosinus*-x-sinus*y), center.y+int(sinus*-x+cosinus*y),gray);
  136. image.setPixelSave(center.x+int(cosinus*x+sinus*y), center.y+int(sinus*x-cosinus*y),gray);
  137. image.setPixelSave(center.x+int(cosinus*-x+sinus*y), center.y+int(sinus*-x-cosinus*y),gray);
  138. }
  139. F = int(ry2*(x+0.5)*(x+0.5) + rx2*(y-1)*(y-1) - rx2*ry2+0.5);
  140. while( y > 0 ) {
  141. if(F <= 0) {
  142. x += 1; // east
  143. ddF_x += 2*ry2;
  144. F += ddF_x;
  145. }
  146. y -=1; // south
  147. ddF_y -= 2*rx2;
  148. F += rx2 - ddF_y;
  149. image.setPixelSave(center.x+int(cosinus*x-sinus*y), center.y+int(sinus*x+cosinus*y),gray);
  150. image.setPixelSave(center.x+int(cosinus*-x-sinus*y), center.y+int(sinus*-x+cosinus*y),gray);
  151. image.setPixelSave(center.x+int(cosinus*x+sinus*y), center.y+int(sinus*x-cosinus*y),gray);
  152. image.setPixelSave(center.x+int(cosinus*-x+sinus*y), center.y+int(sinus*-x-cosinus*y),gray);
  153. }
  154. }
  155. // Destructor:
  156. // -----------
  157. template<class P>
  158. EllipseT<P>::~EllipseT()
  159. {
  160. }
  161. }; // namespace NICE