Example.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #include "Example.h"
  2. using namespace OBJREC;
  3. using namespace std;
  4. using namespace NICE;
  5. Example::Example()
  6. {
  7. weight = 0.0;
  8. height = 0;
  9. width = 0;
  10. x = 0;
  11. y = 0;
  12. vec = NULL;
  13. svec = NULL;
  14. position = 0;
  15. }
  16. Example::~Example ()
  17. {
  18. }
  19. Example::Example ( CachedExample *_ce )
  20. {
  21. _ce->getImageSize ( width, height );
  22. if ( width % 2 == 0 )
  23. width--;
  24. if ( height % 2 == 0 )
  25. height--;
  26. x = width/2;
  27. y = height/2;
  28. ce = _ce;
  29. vec = NULL;
  30. svec = NULL;
  31. weight = 1.0;
  32. position = 0;
  33. }
  34. Example::Example ( CachedExample *_ce,
  35. int _x,
  36. int _y,
  37. double _weight )
  38. {
  39. ce = _ce;
  40. x = _x;
  41. y = _y;
  42. width = 0;
  43. height = 0;
  44. vec = NULL;
  45. svec = NULL;
  46. weight = _weight;
  47. position = 0;
  48. }
  49. Example::Example ( CachedExample *_ce,
  50. int _x,
  51. int _y,
  52. int _width,
  53. int _height,
  54. double _weight )
  55. {
  56. ce = _ce;
  57. x = _x;
  58. y = _y;
  59. width = _width;
  60. height = _height;
  61. assert ( (width > 0) && (height > 0) );
  62. vec = NULL;
  63. svec = NULL;
  64. weight = _weight;
  65. position = 0;
  66. }
  67. Example::Example ( NICE::Vector *_vec,
  68. double _weight )
  69. {
  70. x = y = 0;
  71. width = height = 0;
  72. ce = NULL;
  73. vec = _vec;
  74. svec = NULL;
  75. weight = _weight;
  76. position = 0;
  77. }
  78. void Example::clean ()
  79. {
  80. if ( ce != NULL )
  81. {
  82. delete ce;
  83. ce = NULL;
  84. }
  85. if ( vec != NULL )
  86. {
  87. delete vec;
  88. vec = NULL;
  89. }
  90. if ( svec != NULL )
  91. {
  92. delete svec;
  93. svec = NULL;
  94. }
  95. }
  96. Example::Example ( const Example &ex)
  97. {
  98. copy(ex);
  99. }
  100. void Example::copy ( const Example &ex)
  101. {
  102. vec = ex.vec;
  103. svec = ex.svec;
  104. weight = ex.weight;
  105. position = ex.position;
  106. width = ex.width;
  107. height = ex.height;
  108. x = ex.x;
  109. y = ex.y;
  110. ce = ex.ce;
  111. scale = ex.scale;
  112. }
  113. void Example::restore (istream & is, int format)
  114. {
  115. is >> weight;
  116. is >> x;
  117. is >> y;
  118. is >> width;
  119. is >> height;
  120. is >> position;
  121. int tmp;
  122. is >> tmp;
  123. if(tmp == 1)
  124. {
  125. svec = new SparseVector();
  126. svec->restore(is);
  127. }
  128. else
  129. svec = NULL;
  130. is >> tmp;
  131. if(tmp >= 0 )
  132. {
  133. vec = new Vector(tmp);
  134. for(int i = 0; i < tmp; i++)
  135. {
  136. is >> vec[i];
  137. }
  138. }
  139. else
  140. vec = NULL;
  141. }
  142. void Example::store (ostream & os, int format) const
  143. {
  144. os << weight << " " << x << " " << y << " " << width << " " << height << " " << position << endl;
  145. if(svec == NULL)
  146. os << 0 << endl;
  147. else
  148. {
  149. os << 1 << endl;
  150. svec->store(os);
  151. }
  152. if(vec == NULL)
  153. os << -1 << endl;
  154. else
  155. {
  156. os << vec->size() << endl;
  157. for(int i = 0; i < (int)vec->size(); i++)
  158. {
  159. os << vec[i] << " ";
  160. }
  161. }
  162. }
  163. void Examples::clean ()
  164. {
  165. for ( iterator i = begin(); i != end(); i++ )
  166. {
  167. Example & example = i->second;
  168. example.clean();
  169. }
  170. clear();
  171. }