Model2D.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  1. #include "Model2D.h"
  2. #include "Bild.h"
  3. #include "FrameworkMath.h"
  4. #include "Mat3.h"
  5. #include "MausEreignis.h"
  6. using namespace Framework;
  7. // Inhalt der Model2DData Klasse aus Model2D.h
  8. // Konstruktor
  9. Model2DData::Model2DData()
  10. : polygons( 0 ),
  11. vListen( 0 ),
  12. minP( 0, 0 ),
  13. maxP( 0, 0 )
  14. {
  15. ref = 1;
  16. }
  17. // Destruktor
  18. Model2DData::~Model2DData()
  19. {
  20. if( polygons )
  21. {
  22. int anz = polygons->getEintragAnzahl();
  23. for( int i = 0; i < anz; i++ )
  24. {
  25. if( polygons->get( i ).tKordinaten )
  26. polygons->get( i ).tKordinaten->release();
  27. if( polygons->get( i ).vertex )
  28. polygons->get( i ).vertex->release();
  29. }
  30. polygons = polygons->release();
  31. }
  32. if( vListen )
  33. vListen->release();
  34. }
  35. // privat
  36. bool Model2DData::istPunktInnen( Vertex p, int polygonId ) const
  37. {
  38. if( p < minP || p > maxP || !polygons )
  39. return 0;
  40. int num = 0;
  41. for( auto *polygon = &polygons->getArray(); polygon && polygon->set; polygon = polygon->next, num++ )
  42. {
  43. if( polygonId >= 0 && num != polygonId )
  44. continue;
  45. int anz = polygon->var.vertex->getEintragAnzahl();
  46. int ola = outList.z( num )->getEintragAnzahl();
  47. bool c = 0;
  48. int j = anz - 1;
  49. for( int k = 0; k < ola; k++ )
  50. {
  51. Punkt out = outList.z( num )->get( k );
  52. if( out.x < out.y && j > out.x && j < out.y )
  53. j = out.x;
  54. if( out.x > out.y && ( j > out.x || j < out.y ) )
  55. j = out.x;
  56. }
  57. for( int i = 0; i < anz; i++ )
  58. {
  59. bool cont = 0;
  60. for( int k = 0; k < ola; k++ )
  61. {
  62. Punkt out = outList.z( num )->get( k );
  63. if( out.x < out.y && i > out.x && i < out.y )
  64. cont = 1;
  65. if( out.x > out.y && ( i > out.x || i < out.y ) )
  66. cont = 1;
  67. }
  68. if( cont )
  69. continue;
  70. Vertex a = polygon->var.vertex->get( i );
  71. Vertex b = polygon->var.vertex->get( j );
  72. if( ( ( a.y >= p.y ) != ( b.y >= p.y ) ) && ( p.x <= ( b.x - a.x ) * ( p.y - a.y ) / (float)( b.y - a.y ) + a.x ) )
  73. c = !c;
  74. j = i;
  75. }
  76. if( c )
  77. return 1;
  78. }
  79. return 0;
  80. }
  81. bool Model2DData::istLinieInnen( Vertex a, Vertex b, int polygonId ) const
  82. {
  83. if( !polygons )
  84. return 0;
  85. int pAnz = polygons->getEintragAnzahl();
  86. for( int p = 0; p < pAnz; p++ )
  87. {
  88. if( polygonId >= 0 && p != polygonId )
  89. continue;
  90. int ola = outList.z( p )->getEintragAnzahl();
  91. int anz = polygons->get( p ).vertex->getEintragAnzahl();
  92. int j = anz - 1;
  93. for( int k = 0; k < ola; k++ )
  94. {
  95. Punkt out = outList.z( p )->get( k );
  96. if( out.x < out.y && j > out.x && j < out.y )
  97. j = out.x;
  98. if( out.x > out.y && ( j > out.x || j < out.y ) )
  99. j = out.x;
  100. }
  101. for( int i = 0; i < anz; i++ )
  102. {
  103. bool cont = 0;
  104. for( int k = 0; k < ola; k++ )
  105. {
  106. Punkt out = outList.z( p )->get( k );
  107. if( out.x < out.y && i > out.x && i < out.y )
  108. cont = 1;
  109. if( out.x > out.y && ( i > out.x || i < out.y ) )
  110. cont = 1;
  111. }
  112. if( cont )
  113. continue;
  114. Punkt va = polygons->get( p ).vertex->get( i );
  115. Punkt vb = polygons->get( p ).vertex->get( j );
  116. if( (Punkt)a == va && (Punkt)b == vb )
  117. return 1;
  118. if( (Punkt)a == vb && (Punkt)b == va )
  119. return 1;
  120. j = i;
  121. }
  122. Vertex len = b - a;
  123. Vertex speed( len.x > 0 ? 1 : -1.f, len.y > 0 ? 1 : -1.f );
  124. int mLen = 0;
  125. if( fabs( len.x ) > fabs( len.y ) )
  126. {
  127. mLen = (int)fabs( len.x );
  128. speed.y = len.y / (float)fabs( len.x );
  129. }
  130. else
  131. {
  132. mLen = (int)fabs( len.y );
  133. speed.x = len.x / (float)fabs( len.y );
  134. }
  135. int i = 1;
  136. bool inside = 1;
  137. for( Vertex vp = speed + a; (Punkt)vp != (Punkt)( b - speed ) && inside && i < mLen - 1; vp += speed, i++ )
  138. inside &= istPunktInnen( vp, p );
  139. if( inside )
  140. return 1;
  141. }
  142. return 0;
  143. }
  144. // nicht constant
  145. bool Model2DData::erstelleModell( Array< Polygon2D > *polygons )
  146. {
  147. removeModell();
  148. if( !polygons || !polygons->getEintragAnzahl() )
  149. {
  150. this->polygons = polygons;
  151. vListen = new RCArray< RCArray< DreieckListe< Vertex > > >();
  152. return 1;
  153. }
  154. this->polygons = polygons;
  155. int pAnz = polygons->getEintragAnzahl();
  156. vListen = new RCArray< RCArray< DreieckListe< Vertex > > >();
  157. for( int p = 0; p < pAnz; p++ )
  158. {
  159. Polygon2D pg = polygons->get( p );
  160. if( !pg.vertex || pg.vertex->getEintragAnzahl() < 3 )
  161. continue;
  162. vListen->add( new RCArray< DreieckListe< Vertex > >() );
  163. outList.set( new Array< Punkt >, p );
  164. int vAnz = pg.vertex->getEintragAnzahl();
  165. bool textur = pg.tKordinaten != 0;
  166. for( int i = 0; i < vAnz && textur; i++ )
  167. textur &= pg.tKordinaten->hat( i );
  168. for( int i = 0; i < vAnz; i++ )
  169. {
  170. if( maxP.x < fabs( pg.vertex->get( i ).x ) )
  171. maxP.x = abs( (int)pg.vertex->get( i ).x ) + 1;
  172. if( maxP.y < fabs( pg.vertex->get( i ).y ) )
  173. maxP.y = abs( (int)pg.vertex->get( i ).y ) + 1;
  174. }
  175. minP = -maxP;
  176. if( !textur )
  177. {
  178. if( pg.tKordinaten )
  179. pg.tKordinaten->leeren();
  180. }
  181. RCArray< RCArray< DreieckListe< Vertex > > > lists;
  182. int lauf = 0;
  183. while( 1 )
  184. {
  185. lists.set( new RCArray< DreieckListe< Vertex > >(), lauf );
  186. outList.z( p )->set( Punkt( 0, 0 ), lauf );
  187. bool fertig = 0;
  188. Vertex a;
  189. Vertex b;
  190. Array< Punkt > tmpOutList;
  191. for( int i = 0; i < vAnz; i++ )
  192. {
  193. bool cont = 0;
  194. int vorher = i - 1;
  195. int nachher = i + 1;
  196. if( nachher >= vAnz )
  197. nachher = 0;
  198. if( vorher < 0 )
  199. vorher = vAnz - 1;
  200. int ola = outList.z( p )->getEintragAnzahl();
  201. for( int j = 0; j < ola; j++ )
  202. {
  203. Punkt out = outList.z( p )->get( j );
  204. if( out.x < out.y )
  205. {
  206. if( nachher > out.x && nachher < out.y )
  207. nachher = out.y;
  208. if( vorher > out.x && vorher < out.y )
  209. vorher = out.x;
  210. }
  211. if( out.x > out.y )
  212. {
  213. if( nachher > out.x || nachher < out.y )
  214. nachher = out.y;
  215. if( vorher > out.x || vorher < out.y )
  216. vorher = out.x;
  217. }
  218. if( out.x < out.y && i > out.x && i < out.y )
  219. cont = 1;
  220. if( out.x > out.y && ( i > out.x || i < out.y ) )
  221. cont = 1;
  222. }
  223. if( cont )
  224. continue;
  225. if( vorher < 0 )
  226. a = pg.vertex->get( vAnz + vorher );
  227. else
  228. a = pg.vertex->get( vorher );
  229. if( nachher > vAnz - 1 )
  230. b = pg.vertex->get( nachher - vAnz + 1 );
  231. else
  232. b = pg.vertex->get( nachher );
  233. if( istLinieInnen( a, b, p ) )
  234. {
  235. DreieckListe< Vertex > *lowL = new DreieckListe< Vertex >();
  236. DreieckListe< Vertex > *heightL = new DreieckListe< Vertex >();
  237. lowL->addPunkt( new Vertex( pg.vertex->get( i ) ), textur ? new Punkt( pg.tKordinaten->get( i ) ) : 0 );
  238. heightL->addPunkt( new Vertex( pg.vertex->get( i ) ), textur ? new Punkt( pg.tKordinaten->get( i ) ) : 0 );
  239. int height = i + 1;
  240. int low = i - 1;
  241. Punkt outL( 0, 0 );
  242. Punkt outH( 0, 0 );
  243. for( int k = 0; k < 2; k++ )
  244. {
  245. bool lowp = !k;
  246. while( 1 )
  247. {
  248. if( height >= vAnz )
  249. height = 0;
  250. if( low < 0 )
  251. low = vAnz - 1;
  252. for( int j = 0; j <= lauf; j++ )
  253. {
  254. Punkt out = outList.z( p )->get( j );
  255. if( out.x < out.y )
  256. {
  257. if( height > out.x && height < out.y )
  258. height = out.y;
  259. if( low > out.x && low < out.y )
  260. low = out.x;
  261. }
  262. if( out.x > out.y )
  263. {
  264. if( height > out.x || height < out.y )
  265. height = out.y;
  266. if( low > out.x || low < out.y )
  267. low = out.x;
  268. }
  269. }
  270. Vertex a = pg.vertex->get( height );
  271. Vertex b = pg.vertex->get( low );
  272. if( low == height )
  273. {
  274. fertig = 1;
  275. outList.z( p )->set( Punkt( 0, 0 ), lauf );
  276. if( !k )
  277. lowL->addPunkt( new Vertex( b ), textur ? new Punkt( pg.tKordinaten->get( low ) ) : 0 );
  278. else
  279. heightL->addPunkt( new Vertex( b ), textur ? new Punkt( pg.tKordinaten->get( low ) ) : 0 );
  280. break;
  281. }
  282. bool inside = istLinieInnen( a, b, p );
  283. if( inside )
  284. {
  285. if( !k )
  286. outL = Punkt( low, height );
  287. else
  288. outH = Punkt( low, height );
  289. outList.z( p )->set( Punkt( low, height ), lauf );
  290. }
  291. if( lowp )
  292. {
  293. if( !k )
  294. lowL->addPunkt( new Vertex( b ), textur ? new Punkt( pg.tKordinaten->get( low ) ) : 0 );
  295. else
  296. heightL->addPunkt( new Vertex( b ), textur ? new Punkt( pg.tKordinaten->get( low ) ) : 0 );
  297. low--;
  298. }
  299. else
  300. {
  301. if( !k )
  302. lowL->addPunkt( new Vertex( a ), textur ? new Punkt( pg.tKordinaten->get( height ) ) : 0 );
  303. else
  304. heightL->addPunkt( new Vertex( a ), textur ? new Punkt( pg.tKordinaten->get( height ) ) : 0 );
  305. height++;
  306. }
  307. lowp = !lowp;
  308. if( !inside )
  309. {
  310. height = i + 1;
  311. low = i - 1;
  312. outList.z( p )->set( Punkt( 0, 0 ), lauf );
  313. break;
  314. }
  315. }
  316. if( fertig )
  317. break;
  318. }
  319. if( lowL->getDreieckAnzahl() > heightL->getDreieckAnzahl() )
  320. {
  321. lists.z( lauf )->set( lowL, i );
  322. tmpOutList.set( outL, i );
  323. heightL->release();
  324. }
  325. else
  326. {
  327. lists.z( lauf )->set( heightL, i );
  328. tmpOutList.set( outH, i );
  329. lowL->release();
  330. }
  331. }
  332. else
  333. lists.z( lauf )->set( new DreieckListe< Vertex >(), i );
  334. if( fertig )
  335. break;
  336. }
  337. int maxP = -1;
  338. int max = 0;
  339. for( int i = 0; i < vAnz; i++ )
  340. {
  341. if( lists.z( lauf )->z( i ) && lists.z( lauf )->z( i )->getDreieckAnzahl() > max )
  342. {
  343. max = lists.z( lauf )->z( i )->getDreieckAnzahl();
  344. maxP = i;
  345. }
  346. }
  347. if( !max || maxP < 0 )
  348. break;
  349. vListen->z( p )->add( lists.z( lauf )->get( maxP ) );
  350. outList.z( p )->set( tmpOutList.get( maxP ), lauf );
  351. if( fertig )
  352. break;
  353. lauf++;
  354. }
  355. outList.z( p )->leeren();
  356. }
  357. return 1;
  358. }
  359. void Model2DData::removeModell() // setzt die Vertex daten zurück
  360. {
  361. if( polygons )
  362. {
  363. int anz = polygons->getEintragAnzahl();
  364. for( int i = 0; i < anz; i++ )
  365. {
  366. if( polygons->get( i ).tKordinaten )
  367. polygons->get( i ).tKordinaten->release();
  368. if( polygons->get( i ).vertex )
  369. polygons->get( i ).vertex->release();
  370. }
  371. polygons = polygons->release();
  372. }
  373. if( vListen )
  374. vListen = vListen->release();
  375. outList.leeren();
  376. minP = Punkt( 0, 0 );
  377. maxP = Punkt( 0, 0 );
  378. }
  379. // Reference Counting
  380. Model2DData *Model2DData::getThis()
  381. {
  382. ref++;
  383. return this;
  384. }
  385. Model2DData *Model2DData::release()
  386. {
  387. ref--;
  388. if( !ref )
  389. delete this;
  390. return 0;
  391. }
  392. // Inhalt der Model2D Klasse aus Model2D.h
  393. // Konstruktor
  394. Model2D::Model2D()
  395. : Zeichnung(),
  396. textur( 0 )
  397. {
  398. farbe = 0;
  399. style = 0;
  400. rData = 0;
  401. drehung = 0;
  402. size = 1;
  403. ref = 1;
  404. }
  405. // Destruktor
  406. Model2D::~Model2D()
  407. {
  408. if( rData )
  409. rData->release();
  410. if( textur )
  411. textur->release();
  412. }
  413. // nicht constant
  414. void Model2D::setModel( Model2DData *mdl )
  415. {
  416. if( rData )
  417. rData->release();
  418. rData = mdl;
  419. }
  420. void Model2D::setDrehung( float drehung )
  421. {
  422. this->drehung = drehung;
  423. while( this->drehung > PI * 2 )
  424. this->drehung -= (float)PI * 2;
  425. while( this->drehung < 0 )
  426. this->drehung += (float)PI * 2;
  427. rend = 1;
  428. }
  429. void Model2D::addDrehung( float drehung )
  430. {
  431. this->drehung += drehung;
  432. while( this->drehung > PI * 2 )
  433. this->drehung -= (float)PI * 2;
  434. while( this->drehung < 0 )
  435. this->drehung += (float)PI * 2;
  436. rend = 1;
  437. }
  438. void Model2D::setSize( float size )
  439. {
  440. this->size = size;
  441. rend = 1;
  442. }
  443. void Model2D::addSize( float size )
  444. {
  445. this->size += size;
  446. rend = 1;
  447. }
  448. void Model2D::setTextur( Bild *t )
  449. {
  450. if( textur )
  451. textur->release();
  452. textur = t;
  453. }
  454. void Model2D::setFarbe( int f )
  455. {
  456. farbe = f;
  457. rend = 1;
  458. }
  459. void Model2D::doMausEreignis( MausEreignis &me )
  460. {
  461. if( !mak || me.verarbeitet || !istPunktInnen( Punkt( me.mx, me.my ) ) )
  462. return;
  463. me.mx -= pos.x;
  464. me.my -= pos.y;
  465. mak( makParam, this, me );
  466. me.mx += pos.x;
  467. me.my += pos.y;
  468. me.verarbeitet = 1;
  469. }
  470. bool Model2D::tick( double tickVal )
  471. {
  472. bool ret = rend;
  473. rend = 0;
  474. return ret;
  475. }
  476. void Model2D::render( Bild &zRObj )
  477. {
  478. if( !rData || hatStyleNicht( Model2D::Style::Sichtbar ) || !rData->polygons )
  479. return;
  480. Zeichnung::render( zRObj );
  481. int num = 0;
  482. for( auto *p = &rData->vListen->getArray(); p && p->set; p = p->next, num++ )
  483. {
  484. Mat3< float > mat = Mat3< float >::translation( pos ) * Mat3< float >::rotation( drehung ) * Mat3< float >::scaling( size );
  485. if( hatStyle( Model2D::Style::Textur ) )
  486. {
  487. if( !textur || !rData->polygons->get( num ).tKordinaten )
  488. {
  489. for( auto *i = &p->var->getArray(); i && i->set; i = i->next )
  490. {
  491. for( auto *j = &i->var->zListe()->getArray(); j->next->next && j->next->next->set; j = j->next )
  492. {
  493. Vertex a = mat * *j->var->punkt;
  494. Vertex b = mat * *j->next->var->punkt;
  495. Vertex c = mat * *j->next->next->var->punkt;
  496. if( hatStyle( Model2D::Style::Alpha ) )
  497. zRObj.drawDreieckAlpha( a, b, c, farbe );
  498. else
  499. zRObj.drawDreieck( a, b, c, farbe );
  500. }
  501. }
  502. }
  503. else
  504. {
  505. for( auto *i = &p->var->getArray(); i && i->set; i = i->next )
  506. {
  507. for( auto *j = &i->var->zListe()->getArray(); j->next->next && j->next->next->set; j = j->next )
  508. {
  509. Vertex a = mat * *j->var->punkt;
  510. Vertex b = mat * *j->next->var->punkt;
  511. Vertex c = mat * *j->next->next->var->punkt;
  512. if( hatStyle( Model2D::Style::Alpha ) )
  513. zRObj.drawDreieckTexturAlpha( a, b, c, *j->var->textur, *j->next->var->textur, *j->next->next->var->textur, *textur );
  514. else
  515. zRObj.drawDreieckTextur( a, b, c, *j->var->textur, *j->next->var->textur, *j->next->next->var->textur, *textur );
  516. }
  517. }
  518. }
  519. }
  520. if( hatStyle( Model2D::Style::Mesh ) )
  521. {
  522. for( auto *i = &p->var->getArray(); i && i->set; i = i->next )
  523. {
  524. for( auto *j = &i->var->zListe()->getArray(); j->next->next && j->next->next->set; j = j->next )
  525. {
  526. Vertex a = mat * *j->var->punkt;
  527. Vertex b = mat * *j->next->var->punkt;
  528. Vertex c = mat * *j->next->next->var->punkt;
  529. if( hatStyle( Model2D::Style::Alpha ) )
  530. {
  531. zRObj.drawLinieAlpha( a, b, farbe );
  532. zRObj.drawLinieAlpha( b, c, farbe );
  533. zRObj.drawLinieAlpha( c, a, farbe );
  534. }
  535. else
  536. {
  537. zRObj.drawLinie( a, b, farbe );
  538. zRObj.drawLinie( b, c, farbe );
  539. zRObj.drawLinie( c, a, farbe );
  540. }
  541. }
  542. }
  543. }
  544. if( hatStyle( Model2D::Style::Rahmen ) )
  545. {
  546. ArrayEintrag< Vertex > &beg = rData->polygons->get( num ).vertex->getArray();
  547. if( beg.set )
  548. {
  549. ArrayEintrag< Vertex > *letzter = 0;
  550. for( ArrayEintrag< Vertex > *e = &beg; e && e->next && e->set && e->next->set; e = e->next )
  551. {
  552. if( hatStyle( Model2D::Style::Alpha ) )
  553. zRObj.drawLinieAlpha( mat * e->var, mat * e->next->var, farbe );
  554. else
  555. zRObj.drawLinie( mat * e->var, mat * e->next->var, farbe );
  556. letzter = e->next;
  557. }
  558. if( letzter && letzter->set )
  559. {
  560. if( hatStyle( Model2D::Style::Alpha ) )
  561. zRObj.drawLinieAlpha( mat * letzter->var, mat * beg.var, farbe );
  562. else
  563. zRObj.drawLinie( mat * letzter->var, mat * beg.var, farbe );
  564. }
  565. }
  566. }
  567. }
  568. }
  569. // constant
  570. float Model2D::getDrehung() const
  571. {
  572. return drehung;
  573. }
  574. float Model2D::getSize() const
  575. {
  576. return size;
  577. }
  578. bool Model2D::istPunktInnen( Vertex p ) const
  579. {
  580. if( !rData )
  581. return 0;
  582. p -= pos;
  583. if( p < Mat3< float >::scaling( size ) * rData->minP || p > Mat3< float >::scaling( size ) * rData->maxP || !rData->polygons )
  584. return 0;
  585. int num = 0;
  586. for( auto *polygon = &rData->polygons->getArray(); polygon && polygon->set; polygon = polygon->next, num++ )
  587. {
  588. Mat3< float > mat = Mat3< float >::rotation( drehung ) * Mat3< float >::scaling( size );
  589. int anz = polygon->var.vertex->getEintragAnzahl();
  590. bool c = 0;
  591. int j = anz - 1;
  592. for( int i = 0; i < anz; i++ )
  593. {
  594. Vertex a = mat * polygon->var.vertex->get( i );
  595. Vertex b = mat * polygon->var.vertex->get( j );
  596. if( ( ( a.y >= p.y ) != ( b.y >= p.y ) ) && ( p.x <= ( b.x - a.x ) * ( p.y - a.y ) / (float)( b.y - a.y ) + a.x ) )
  597. c = !c;
  598. j = i;
  599. }
  600. if( c )
  601. return 1;
  602. }
  603. return 0;
  604. }
  605. bool Model2D::istLinieInnen( Vertex a, Vertex b ) const
  606. {
  607. if( !rData || !rData->polygons )
  608. return 0;
  609. int pAnz = rData->polygons->getEintragAnzahl();
  610. for( int p = 0; p < pAnz; p++ )
  611. {
  612. Mat3< float > mat = Mat3< float >::rotation( drehung ) * Mat3< float >::scaling( size );
  613. int anz = rData->polygons->get( p ).vertex->getEintragAnzahl();
  614. int j = anz - 1;
  615. for( int i = 0; i < anz; i++ )
  616. {
  617. Punkt va = mat * rData->polygons->get( p ).vertex->get( i );
  618. Punkt vb = mat * rData->polygons->get( p ).vertex->get( j );
  619. if( (Punkt)a == pos + va && (Punkt)b == pos + vb )
  620. return 1;
  621. if( (Punkt)a == pos + vb && (Punkt)b == pos + va )
  622. return 1;
  623. j = i;
  624. }
  625. Vertex len = b - a;
  626. Vertex speed( len.x > 0 ? 1 : -1.f, len.y > 0 ? 1 : -1.f );
  627. int mLen = 0;
  628. if( fabs( len.x ) > fabs( len.y ) )
  629. {
  630. mLen = (int)fabs( len.x );
  631. speed.y = len.y / (float)fabs( len.x );
  632. }
  633. else
  634. {
  635. mLen = (int)fabs( len.y );
  636. speed.x = len.x / (float)fabs( len.y );
  637. }
  638. int i = 1;
  639. bool inside = 1;
  640. for( Vertex vp = speed + a; (Punkt)vp != (Punkt)( b - speed ) && inside && i < mLen - 1; vp += speed, i++ )
  641. inside &= istPunktInnen( vp );
  642. if( inside )
  643. return 1;
  644. }
  645. return 0;
  646. }
  647. bool Model2D::istModelInnen( const Model2D *zMdl, bool end ) const
  648. {
  649. if( !end )
  650. {
  651. Vertex min = (Vertex)rData->minP * size + pos;
  652. Vertex max = (Vertex)rData->maxP * size + pos;
  653. Vertex min2 = (Vertex)zMdl->zModel()->minP * zMdl->getSize() + zMdl->getPosition();
  654. Vertex max2 = (Vertex)zMdl->zModel()->maxP * zMdl->getSize() + zMdl->getPosition();
  655. if( max.x < min2.x || min.x > max2.x || max.y < min2.y || min.y > max2.y )
  656. return 0;
  657. }
  658. Mat3< float > mat = Mat3< float >::translation( pos ) * Mat3< float >::rotation( drehung ) * Mat3< float >::scaling( size );
  659. for( auto *polygon = &rData->polygons->getArray(); polygon && polygon->set; polygon = polygon->next )
  660. {
  661. int anz = polygon->var.vertex->getEintragAnzahl();
  662. for( int i = 0; i < anz; i++ )
  663. {
  664. if( zMdl->istPunktInnen( mat * polygon->var.vertex->get( i ) ) )
  665. return 1;
  666. }
  667. }
  668. if( end )
  669. return 0;
  670. return zMdl->istModelInnen( this, 1 );
  671. }
  672. Model2DData *Model2D::getModel() const
  673. {
  674. return rData ? rData->getThis() : 0;
  675. }
  676. Model2DData *Model2D::zModel() const
  677. {
  678. return rData;
  679. }
  680. // Reference Counting
  681. Model2D *Model2D::getThis()
  682. {
  683. ++ref;
  684. return this;
  685. }
  686. Model2D *Model2D::release()
  687. {
  688. --ref;
  689. if( !ref )
  690. delete this;
  691. return 0;
  692. }