object.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. #define Global
  2. #include "object.h"
  3. #include <limits>
  4. Object::Object( QString id, int classId )
  5. : id( id ),
  6. classId( classId )
  7. {}
  8. // Setzt die Objekt-ID
  9. void Object::setId( QString id )
  10. {
  11. this->id = id;
  12. }
  13. // Gibt die Objekt-ID zurück
  14. QString Object::getId() const
  15. {
  16. return id;
  17. }
  18. // Setzt die ID der Klasse
  19. void Object::setClassId( int id )
  20. {
  21. classId = id;
  22. }
  23. // Gibt die ID der Klasse zurück
  24. int Object::getClassId() const
  25. {
  26. return classId;
  27. }
  28. // Vergleicht zwei Objekte anhand ihrer ID
  29. bool operator == (const Object &p1, const Object &p2)
  30. {
  31. return p1.getId() == p2.getId();
  32. }
  33. // Inhalt der _ObjektPolygon Klasse
  34. //----------------------------------
  35. _ObjectPolygon::_ObjectPolygon( QString id, QPolygon po, bool truncated, int index, FrameTreeNode *parent )
  36. : FrameTreeNode( index, parent, 2 ),
  37. id( id ),
  38. truncated( truncated ),
  39. selected( 1 ),
  40. ref( 1 )
  41. {
  42. polygon.append( po );
  43. }
  44. // Setzt die Objekt-ID
  45. void _ObjectPolygon::setId( QString id )
  46. {
  47. this->id = id;
  48. }
  49. // Legt fest, ob das Objekt versteckt ist (falls s = 0)
  50. void _ObjectPolygon::setSelected( bool s )
  51. {
  52. this->selected = s;
  53. }
  54. // Entfernt alle Ecken in einer Region r. Gibt 0 zurück, fall das Objekt komplett entfernt werden soll
  55. bool _ObjectPolygon::removeVertices( QRect r )
  56. {
  57. for( auto p = polygon.begin(); p != polygon.end(); p++ )
  58. {
  59. QPolygon &pol = *p;
  60. int anz = pol.size();
  61. for( int i = 0; i < anz; i++ )
  62. {
  63. QPoint v = pol.at( i );
  64. if( r.contains( v ) )
  65. {
  66. pol.removeAt( i );
  67. i--;
  68. anz--;
  69. }
  70. }
  71. }
  72. int pAnz = polygon.size();
  73. for( int j = 0; j < pAnz; j++ )
  74. {
  75. int anz = polygon.at( j ).size();
  76. if( anz < 3 )
  77. {
  78. polygon.removeAt( j );
  79. j--;
  80. pAnz--;
  81. }
  82. }
  83. return pAnz >= 1;
  84. }
  85. // Verschiebt den intex-ten Vertex aus dem pIndex-ten Polygon auf Position newPos.
  86. // max: Die maximale Größe des Bildes. Falls newPos außerhalb des Bildes liegt, wird automatisch der nächste Punkt im Bild genommen.
  87. void _ObjectPolygon::moveVertex( int index, QPoint newPos, QSize max, int pIndex )
  88. {
  89. int pI = 0;
  90. for( auto p = polygon.begin(); p != polygon.end(); p++ )
  91. {
  92. QPolygon &pol = *p;
  93. if( pI == pIndex )
  94. {
  95. int i = 0;
  96. for( auto point = pol.begin(); point != pol.end(); point++, i++ )
  97. {
  98. if( i == index )
  99. {
  100. *point = newPos;
  101. if( point->x() < 0 )
  102. point->setX( 0 );
  103. if( point->y() < 0 )
  104. point->setY( 0 );
  105. if( point->x() >= max.width() )
  106. point->setX( max.width() - 1 );
  107. if( point->y() >= max.height() )
  108. point->setY( max.height() - 1 );
  109. }
  110. }
  111. }
  112. pI++;
  113. }
  114. }
  115. // Fügt an Slette index im pIndexten Polygon den Eckpunkt v ein
  116. void _ObjectPolygon::insertVertex( int index, QPoint v, int pIndex )
  117. {
  118. int pI = 0;
  119. for( auto p = polygon.begin(); p != polygon.end(); p++ )
  120. {
  121. QPolygon &pol = *p;
  122. if( pI == pIndex )
  123. pol.insert( index, v );
  124. pI++;
  125. }
  126. }
  127. // Spaltet das pIndexte Polygon indem zwischen dem begin-ten Eckpunkt und dem end-ten Eckpunkt geschnitten wird.
  128. // Gibt das neu entstandene Polygon zurück (eine hälfte des alten, die aus diesem entfernt wurde)
  129. QPolygon _ObjectPolygon::split( int begin, int end, int pIndex )
  130. {
  131. int pI = 0;
  132. for( auto p = polygon.begin(); p != polygon.end(); p++ )
  133. {
  134. QPolygon &pol = *p;
  135. if( pI == pIndex )
  136. {
  137. QPolygon newP;
  138. int size = pol.size();
  139. for( int i = begin; true; i++ )
  140. {
  141. if( i >= size )
  142. i = 0;
  143. newP.append( pol.at( i ) );
  144. if( i == end )
  145. break;
  146. }
  147. int removeIndex = begin + 1;
  148. for( int i = begin + 1; true; i++ )
  149. {
  150. if( i >= size )
  151. {
  152. i = 0;
  153. removeIndex = 0;
  154. }
  155. if( i == end )
  156. break;
  157. pol.removeAt( removeIndex );
  158. }
  159. return newP;
  160. }
  161. }
  162. }
  163. // Setzt den Truncated Flag
  164. void _ObjectPolygon::setTruncated( bool truncated )
  165. {
  166. this->truncated = truncated;
  167. }
  168. // Gibt 1 zurück, wenn das Objekt nicht Versteckt ist
  169. bool _ObjectPolygon::isSelected() const
  170. {
  171. return selected;
  172. }
  173. // Gibt die Bounding Box zurück
  174. QRect _ObjectPolygon::getBoundingBox() const
  175. {
  176. int minX = INT_MAX, minY = INT_MAX, maxX = 0, maxY = 0;
  177. for( QPolygon pol : polygon )
  178. {
  179. for( QPoint point : pol )
  180. {
  181. if( point.x() < minX )
  182. minX = point.x();
  183. if( point.y() < minY )
  184. minY = point.y();
  185. if( point.x() > maxX )
  186. maxX = point.x();
  187. if( point.y() > maxY )
  188. maxY = point.y();
  189. }
  190. }
  191. return QRect( minX, minY, maxX - minX, maxY - minY );
  192. }
  193. // Gibt this zurück
  194. void *_ObjectPolygon::getNodeObject() const
  195. {
  196. return (void*)this;
  197. }
  198. // Gibt 1 zurück, falls Truncated gesetzt wurde
  199. bool _ObjectPolygon::isTruncated() const
  200. {
  201. return truncated;
  202. }
  203. // Gibt die Objekt-ID zurück
  204. QString _ObjectPolygon::getId() const
  205. {
  206. return id;
  207. }
  208. // Gibt eine Liste mit den Polygonen zurück
  209. QList< QPolygon > &_ObjectPolygon::getPolygonList()
  210. {
  211. return polygon;
  212. }
  213. // Inhalt der ObjektPolygon Klasse
  214. //---------------------------------
  215. // Erstellt ein auf Null zeigendes ObjektPolygon
  216. ObjectPolygon::ObjectPolygon()
  217. {
  218. ptr = 0;
  219. }
  220. // Erzeugt ein neues Objekt Polygon, welches auf ein bestimmtes Objekt zeigt
  221. ObjectPolygon::ObjectPolygon( _ObjectPolygon *ptr )
  222. {
  223. this->ptr = ptr;
  224. if( this->ptr )
  225. this->ptr->ref++;
  226. }
  227. // Erzeugt ein neues Objekt Polygon mit neuem Objekt
  228. // id: Die Objekt ID
  229. // po: Das Polygon
  230. // truncated: 1, falls das Objekt abgeschnitten ist
  231. // parent: Der Elternknoten im Baum der Sequenz (das Bild zu dem das Objekt gehört)
  232. ObjectPolygon::ObjectPolygon( QString id, QPolygon po, bool truncated, int index, FrameTreeNode *parent )
  233. {
  234. ptr = new _ObjectPolygon( id, po, truncated, index, parent );
  235. numObjects++;
  236. }
  237. // Copy Constructor
  238. ObjectPolygon::ObjectPolygon( const ObjectPolygon &op )
  239. : ObjectPolygon( op.ptr )
  240. {}
  241. ObjectPolygon::~ObjectPolygon()
  242. {
  243. if( ptr && !--ptr->ref )
  244. {
  245. delete ptr;
  246. numObjects--;
  247. }
  248. }
  249. // Operator um auf das unterliegende _ObjectPolygon zuzugreifen
  250. _ObjectPolygon *ObjectPolygon::operator->() const
  251. {
  252. return ptr;
  253. }
  254. // Gibt 1 zurück, falls auf das gleiche Objekt verwiesen wird
  255. bool ObjectPolygon::operator==( const ObjectPolygon &op ) const
  256. {
  257. return ptr == op.ptr;
  258. }
  259. // Gibt 1 zurück, falls nicht auf das gleiche Objekt verwiesen wird
  260. bool ObjectPolygon::operator!=( const ObjectPolygon &op ) const
  261. {
  262. return ptr != op.ptr;
  263. }
  264. // Setzt den Zeiger auf das _ObjectPolygon Objekt
  265. bool ObjectPolygon::operator=( const ObjectPolygon &op )
  266. {
  267. if( ptr && !--ptr->ref )
  268. {
  269. delete ptr;
  270. numObjects--;
  271. }
  272. ptr = op.ptr;
  273. if( ptr )
  274. ptr->ref++;
  275. }
  276. // Gibt 1 zurück, falls der Zeiger nicht inizialisiert wurde
  277. bool ObjectPolygon::isNull() const
  278. {
  279. return ptr == 0;
  280. }