Model2D.cpp 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377
  1. #include "Model2D.h"
  2. #include "Textur2D.h"
  3. #include "FrameworkMath.h"
  4. #include "Mat3.h"
  5. #include "MausEreignis.h"
  6. #include "Bild.h"
  7. using namespace Framework;
  8. // Inhalt der Model2DData Klasse aus Model2D.h
  9. // Konstruktor
  10. Model2DData::Model2DData()
  11. : polygons( 0 ),
  12. vListen( 0 ),
  13. minP( 0, 0 ),
  14. maxP( 0, 0 )
  15. {
  16. ref = 1;
  17. }
  18. // Destruktor
  19. Model2DData::~Model2DData()
  20. {
  21. if( polygons )
  22. {
  23. int anz = polygons->getEintragAnzahl();
  24. for( int i = 0; i < anz; i++ )
  25. {
  26. if( polygons->get( i ).name )
  27. polygons->get( i ).name->release();
  28. if( polygons->get( i ).tKordinaten )
  29. polygons->get( i ).tKordinaten->release();
  30. if( polygons->get( i ).vertex )
  31. polygons->get( i ).vertex->release();
  32. if( polygons->get( i ).schwerpunkt )
  33. delete polygons->get( i ).schwerpunkt;
  34. }
  35. polygons = polygons->release();
  36. }
  37. if( vListen )
  38. vListen->release();
  39. }
  40. // privat
  41. bool Model2DData::istPunktInnen( Vertex p, int polygonId ) const
  42. {
  43. if( p < minP || p > maxP || !polygons )
  44. return 0;
  45. int num = 0;
  46. auto outListP = outList.getIterator();
  47. for( auto polygon = polygons->getIterator(); polygon; polygon++, num++, outListP++ )
  48. {
  49. if( polygonId >= 0 && num != polygonId )
  50. continue;
  51. int anz = polygon._.vertex->getEintragAnzahl();
  52. bool c = 0;
  53. int j = anz - 1;
  54. for( auto outListPP = outListP->getIterator(); outListPP; outListPP++ )
  55. {
  56. Punkt out = outListPP;
  57. if( out.x < out.y && j > out.x && j < out.y )
  58. j = out.x;
  59. if( out.x > out.y && ( j > out.x || j < out.y ) )
  60. j = out.x;
  61. }
  62. auto point = polygon._.vertex->getIterator();
  63. for( int i = 0; i < anz; i++, point++ )
  64. {
  65. bool cont = 0;
  66. for( auto outListPP = outListP->getIterator(); outListPP; outListPP++ )
  67. {
  68. Punkt out = outListPP;
  69. if( out.x < out.y && i > out.x && i < out.y )
  70. cont = 1;
  71. if( out.x > out.y && ( i > out.x || i < out.y ) )
  72. cont = 1;
  73. }
  74. if( cont )
  75. continue;
  76. Vertex a = point;
  77. Vertex b = polygon._.vertex->get( j );
  78. 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 ) )
  79. c = !c;
  80. j = i;
  81. }
  82. if( c )
  83. return 1;
  84. }
  85. return 0;
  86. }
  87. bool Model2DData::istLinieInnen( Vertex a, Vertex b, int polygonId ) const
  88. {
  89. if( !polygons )
  90. return 0;
  91. int pAnz = polygons->getEintragAnzahl();
  92. for( int p = 0; p < pAnz; p++ )
  93. {
  94. if( polygonId >= 0 && p != polygonId )
  95. continue;
  96. int ola = outList.z( p )->getEintragAnzahl();
  97. int anz = polygons->get( p ).vertex->getEintragAnzahl();
  98. int j = anz - 1;
  99. for( int k = 0; k < ola; k++ )
  100. {
  101. Punkt out = outList.z( p )->get( k );
  102. if( out.x < out.y && j > out.x && j < out.y )
  103. j = out.x;
  104. if( out.x > out.y && ( j > out.x || j < out.y ) )
  105. j = out.x;
  106. }
  107. for( int i = 0; i < anz; i++ )
  108. {
  109. bool cont = 0;
  110. for( int k = 0; k < ola; k++ )
  111. {
  112. Punkt out = outList.z( p )->get( k );
  113. if( out.x < out.y && i > out.x && i < out.y )
  114. cont = 1;
  115. if( out.x > out.y && ( i > out.x || i < out.y ) )
  116. cont = 1;
  117. }
  118. if( cont )
  119. continue;
  120. Punkt va = polygons->get( p ).vertex->get( i );
  121. Punkt vb = polygons->get( p ).vertex->get( j );
  122. if( (Punkt)a == va && (Punkt)b == vb )
  123. return 1;
  124. if( (Punkt)a == vb && (Punkt)b == va )
  125. return 1;
  126. j = i;
  127. }
  128. Vertex len = b - a;
  129. Vertex speed( len.x > 0 ? 1 : -1.f, len.y > 0 ? 1 : -1.f );
  130. int mLen = 0;
  131. if( fabs( len.x ) > fabs( len.y ) )
  132. {
  133. mLen = (int)fabs( len.x );
  134. speed.y = len.y / (float)fabs( len.x );
  135. }
  136. else
  137. {
  138. mLen = (int)fabs( len.y );
  139. speed.x = len.x / (float)fabs( len.y );
  140. }
  141. int i = 1;
  142. bool inside = 1;
  143. for( Vertex vp = speed + a; (Punkt)vp != (Punkt)( b - speed ) && inside && i < mLen - 1; vp += speed, i++ )
  144. inside &= istPunktInnen( vp, p );
  145. if( inside )
  146. return 1;
  147. }
  148. return 0;
  149. }
  150. // nicht constant
  151. bool Model2DData::erstelleModell( Array< Polygon2D > * polygons )
  152. {
  153. removeModell();
  154. if( !polygons || !polygons->getEintragAnzahl() )
  155. {
  156. this->polygons = polygons;
  157. vListen = new RCArray< RCArray< DreieckListe< Vertex > > >();
  158. return 1;
  159. }
  160. this->polygons = polygons;
  161. int pAnz = polygons->getEintragAnzahl();
  162. vListen = new RCArray< RCArray< DreieckListe< Vertex > > >();
  163. for( int p = 0; p < pAnz; p++ )
  164. {
  165. Polygon2D pg = polygons->get( p );
  166. if( !pg.vertex || pg.vertex->getEintragAnzahl() < 3 )
  167. continue;
  168. vListen->add( new RCArray< DreieckListe< Vertex > >() );
  169. outList.set( new Array< Punkt >, p );
  170. int vAnz = pg.vertex->getEintragAnzahl();
  171. bool textur = pg.tKordinaten != 0;
  172. for( int i = 0; i < vAnz && textur; i++ )
  173. textur &= pg.tKordinaten->hat( i );
  174. for( int i = 0; i < vAnz; i++ )
  175. {
  176. if( maxP.x < fabs( pg.vertex->get( i ).x ) )
  177. {
  178. maxP.x = abs( (int)pg.vertex->get( i ).x ) + 1;
  179. maxP.y = abs( (int)pg.vertex->get( i ).x ) + 1;
  180. }
  181. if( maxP.y < fabs( pg.vertex->get( i ).y ) )
  182. {
  183. maxP.x = abs( (int)pg.vertex->get( i ).y ) + 1;
  184. maxP.y = abs( (int)pg.vertex->get( i ).y ) + 1;
  185. }
  186. }
  187. minP = -maxP;
  188. if( !textur )
  189. {
  190. if( pg.tKordinaten )
  191. pg.tKordinaten->leeren();
  192. }
  193. RCArray< RCArray< DreieckListe< Vertex > > > lists;
  194. int lauf = 0;
  195. while( 1 )
  196. {
  197. lists.set( new RCArray< DreieckListe< Vertex > >(), lauf );
  198. outList.z( p )->set( Punkt( 0, 0 ), lauf );
  199. bool fertig = 0;
  200. Vertex a;
  201. Vertex b;
  202. Array< Punkt > tmpOutList;
  203. for( int i = 0; i < vAnz; i++ )
  204. {
  205. bool cont = 0;
  206. int vorher = i - 1;
  207. int nachher = i + 1;
  208. if( nachher >= vAnz )
  209. nachher = 0;
  210. if( vorher < 0 )
  211. vorher = vAnz - 1;
  212. int ola = outList.z( p )->getEintragAnzahl();
  213. for( int j = 0; j < ola; j++ )
  214. {
  215. Punkt out = outList.z( p )->get( j );
  216. if( out.x < out.y )
  217. {
  218. if( nachher > out.x && nachher < out.y )
  219. nachher = out.y;
  220. if( vorher > out.x && vorher < out.y )
  221. vorher = out.x;
  222. }
  223. if( out.x > out.y )
  224. {
  225. if( nachher > out.x || nachher < out.y )
  226. nachher = out.y;
  227. if( vorher > out.x || vorher < out.y )
  228. vorher = out.x;
  229. }
  230. if( out.x < out.y && i > out.x && i < out.y )
  231. cont = 1;
  232. if( out.x > out.y && ( i > out.x || i < out.y ) )
  233. cont = 1;
  234. }
  235. if( cont )
  236. continue;
  237. if( vorher < 0 )
  238. a = pg.vertex->get( vAnz + vorher );
  239. else
  240. a = pg.vertex->get( vorher );
  241. if( nachher > vAnz - 1 )
  242. b = pg.vertex->get( nachher - vAnz + 1 );
  243. else
  244. b = pg.vertex->get( nachher );
  245. if( istLinieInnen( a, b, p ) )
  246. {
  247. DreieckListe< Vertex > *lowL = new DreieckListe< Vertex >();
  248. DreieckListe< Vertex > *heightL = new DreieckListe< Vertex >();
  249. lowL->addPunkt( new Vertex( pg.vertex->get( i ) ), textur ? new Vertex( pg.tKordinaten->get( i ) ) : 0 );
  250. heightL->addPunkt( new Vertex( pg.vertex->get( i ) ), textur ? new Vertex( pg.tKordinaten->get( i ) ) : 0 );
  251. int height = i + 1;
  252. int low = i - 1;
  253. Punkt outL( 0, 0 );
  254. Punkt outH( 0, 0 );
  255. for( int k = 0; k < 2; k++ )
  256. {
  257. bool lowp = !k;
  258. while( 1 )
  259. {
  260. if( height >= vAnz )
  261. height = 0;
  262. if( low < 0 )
  263. low = vAnz - 1;
  264. for( int j = 0; j <= lauf; j++ )
  265. {
  266. Punkt out = outList.z( p )->get( j );
  267. if( out.x < out.y )
  268. {
  269. if( height > out.x && height < out.y )
  270. height = out.y;
  271. if( low > out.x && low < out.y )
  272. low = out.x;
  273. }
  274. if( out.x > out.y )
  275. {
  276. if( height > out.x || height < out.y )
  277. height = out.y;
  278. if( low > out.x || low < out.y )
  279. low = out.x;
  280. }
  281. }
  282. Vertex a = pg.vertex->get( height );
  283. Vertex b = pg.vertex->get( low );
  284. if( low == height )
  285. {
  286. fertig = 1;
  287. outList.z( p )->set( Punkt( 0, 0 ), lauf );
  288. if( !k )
  289. lowL->addPunkt( new Vertex( b ), textur ? new Vertex( pg.tKordinaten->get( low ) ) : 0 );
  290. else
  291. heightL->addPunkt( new Vertex( b ), textur ? new Vertex( pg.tKordinaten->get( low ) ) : 0 );
  292. break;
  293. }
  294. bool inside = istLinieInnen( a, b, p );
  295. if( inside )
  296. {
  297. if( !k )
  298. outL = Punkt( low, height );
  299. else
  300. outH = Punkt( low, height );
  301. outList.z( p )->set( Punkt( low, height ), lauf );
  302. }
  303. if( lowp )
  304. {
  305. if( !k )
  306. lowL->addPunkt( new Vertex( b ), textur ? new Vertex( pg.tKordinaten->get( low ) ) : 0 );
  307. else
  308. heightL->addPunkt( new Vertex( b ), textur ? new Vertex( pg.tKordinaten->get( low ) ) : 0 );
  309. low--;
  310. }
  311. else
  312. {
  313. if( !k )
  314. lowL->addPunkt( new Vertex( a ), textur ? new Vertex( pg.tKordinaten->get( height ) ) : 0 );
  315. else
  316. heightL->addPunkt( new Vertex( a ), textur ? new Vertex( pg.tKordinaten->get( height ) ) : 0 );
  317. height++;
  318. }
  319. lowp = !lowp;
  320. if( !inside )
  321. {
  322. height = i + 1;
  323. low = i - 1;
  324. outList.z( p )->set( Punkt( 0, 0 ), lauf );
  325. break;
  326. }
  327. }
  328. if( fertig )
  329. break;
  330. }
  331. if( lowL->getDreieckAnzahl() > heightL->getDreieckAnzahl() )
  332. {
  333. lists.z( lauf )->set( lowL, i );
  334. tmpOutList.set( outL, i );
  335. heightL->release();
  336. }
  337. else
  338. {
  339. lists.z( lauf )->set( heightL, i );
  340. tmpOutList.set( outH, i );
  341. lowL->release();
  342. }
  343. }
  344. else
  345. lists.z( lauf )->set( new DreieckListe< Vertex >(), i );
  346. if( fertig )
  347. break;
  348. }
  349. int maxP = -1;
  350. int max = 0;
  351. for( int i = 0; i < vAnz; i++ )
  352. {
  353. if( lists.z( lauf )->z( i ) && lists.z( lauf )->z( i )->getDreieckAnzahl() > max )
  354. {
  355. max = lists.z( lauf )->z( i )->getDreieckAnzahl();
  356. maxP = i;
  357. }
  358. }
  359. if( !max || maxP < 0 )
  360. break;
  361. vListen->z( p )->add( lists.z( lauf )->get( maxP ) );
  362. outList.z( p )->set( tmpOutList.get( maxP ), lauf );
  363. if( fertig )
  364. break;
  365. lauf++;
  366. }
  367. outList.z( p )->leeren();
  368. }
  369. return 1;
  370. }
  371. void Model2DData::removeModell() // setzt die Vertex daten zurück
  372. {
  373. if( polygons )
  374. {
  375. int anz = polygons->getEintragAnzahl();
  376. for( int i = 0; i < anz; i++ )
  377. {
  378. if( polygons->get( i ).name )
  379. polygons->get( i ).name->release();
  380. if( polygons->get( i ).tKordinaten )
  381. polygons->get( i ).tKordinaten->release();
  382. if( polygons->get( i ).vertex )
  383. polygons->get( i ).vertex->release();
  384. if( polygons->get( i ).schwerpunkt )
  385. delete polygons->get( i ).schwerpunkt;
  386. }
  387. polygons = polygons->release();
  388. }
  389. if( vListen )
  390. vListen = vListen->release();
  391. outList.leeren();
  392. minP = Punkt( 0, 0 );
  393. maxP = Punkt( 0, 0 );
  394. }
  395. bool Model2DData::calcHitPoint( Vertex pos, Vertex dir, const char *polygonName, Vertex & hitpoint, Vertex & moveSpeed, float &rotSpeed ) const
  396. {
  397. if( dir.x == 0 && dir.y == 0 )
  398. return 0;
  399. bool ret = 0;
  400. for( auto polygon = polygons->getIterator(); polygon; polygon++ )
  401. {
  402. if( polygon._.name->istGleich( polygonName ) )
  403. {
  404. int anz = polygon._.vertex->getEintragAnzahl();
  405. for( int i = 0; i < anz; i++ )
  406. {
  407. Vertex a = polygon._.vertex->get( i );
  408. Vertex b = polygon._.vertex->get( ( i + 1 ) % anz );
  409. b -= a;
  410. float offset = 0;
  411. if( dir.y != 0 && dir.x != 0 )
  412. offset = ( ( a.y - pos.y ) / dir.y - ( a.x - pos.x ) / dir.x ) / ( b.x / dir.x - b.y / dir.y ); // solve hitpoint equasion
  413. else if( dir.y == 0 )
  414. {
  415. if( b.y == 0 )
  416. continue;
  417. offset = ( pos.y - a.y ) / b.y;
  418. }
  419. else if( dir.x == 0 )
  420. {
  421. if( b.x == 0 )
  422. continue;
  423. offset = ( pos.x - a.x ) / b.x;
  424. }
  425. Vertex point = a + ( b * offset );
  426. if( offset >= 0 && offset <= 1 )
  427. {
  428. float f = ( point.x - pos.x ) / dir.x;
  429. if( !dir.x )
  430. f = ( point.y - pos.y ) / dir.y;
  431. if( ( !ret || ( hitpoint - pos ).getLengthSq() > ( point - pos ).getLengthSq() ) && f > 0 )
  432. {
  433. Vertex normal = b.CW90().normalize();
  434. Vertex kNorm = Vertex( dir ).normalize();
  435. moveSpeed = normal * ( normal * kNorm ) * dir.getLength();
  436. normal = ( point - *polygon._.schwerpunkt ).CW90().normalize();
  437. Vertex rotKraft = normal * ( normal * kNorm ) * dir.getLength();
  438. rotSpeed = ( (float)sqrt( rotKraft.getLength() * ( point - *polygon._.schwerpunkt ).getLength() ) / 180.f ) * 3.14f * ( normal * kNorm );
  439. hitpoint = point;
  440. if( isnan( moveSpeed.x ) || isnan( moveSpeed.y ) || isnan( rotSpeed ) )
  441. return 0;
  442. ret = 1;
  443. }
  444. }
  445. }
  446. }
  447. }
  448. return ret;
  449. }
  450. bool Model2DData::split( Vertex pos, Vertex dir, char *polygonName, Polygon2D & partA, Polygon2D & partB, Punkt & posA, Punkt & posB, std::function< double() > random ) const
  451. {
  452. Vertex originalDir = dir;
  453. bool ret = 0;
  454. int num = 0;
  455. for( auto polygon = polygons->getIterator(); polygon; polygon++, num++ )
  456. {
  457. if( polygon._.name->istGleich( polygonName ) )
  458. {
  459. while( istPunktInnen( pos, num ) )
  460. {
  461. pos -= dir;
  462. }
  463. int anz = polygon._.vertex->getEintragAnzahl();
  464. Vertex startPoint;
  465. Vertex texturSP;
  466. int leftI = 0;
  467. int rightI = 0;
  468. Vertex txtChpPix( 0, 0 );
  469. for( int i = 0; i < anz; i++ )
  470. {
  471. Vertex a = polygon._.vertex->get( i );
  472. Vertex b = polygon._.vertex->get( ( i + 1 ) % anz );
  473. b -= a;
  474. if( ( txtChpPix.x == 0 || txtChpPix.y == 0 ) && b.x != 0 && b.y != 0 )
  475. {
  476. Vertex ta = polygon._.tKordinaten->get( i );
  477. Vertex tb = polygon._.tKordinaten->get( ( i + 1 ) % anz );
  478. tb -= ta;
  479. txtChpPix = Vertex( tb.x / b.x, tb.y / b.y );
  480. }
  481. float offset = 0;
  482. if( dir.y != 0 && dir.x != 0 )
  483. offset = ( ( a.y - pos.y ) / dir.y - ( a.x - pos.x ) / dir.x ) / ( b.x / dir.x - b.y / dir.y ); // solve hitpoint equasion
  484. else if( dir.y == 0 )
  485. offset = ( pos.y - a.y ) / b.y;
  486. else if( dir.x == 0 )
  487. offset = ( pos.x - a.x ) / b.x;
  488. Vertex point = a + ( b * offset );
  489. if( offset >= 0 && offset <= 1 )
  490. {
  491. if( !ret || ( startPoint - pos ).getLengthSq() > ( point - pos ).getLengthSq() )
  492. {
  493. leftI = i;
  494. rightI = ( i + 1 ) % anz;
  495. startPoint = point;
  496. texturSP = polygon._.tKordinaten->get( i ) + ( polygon._.tKordinaten->get( ( i + 1 ) % anz ) - polygon._.tKordinaten->get( i ) ) * offset;
  497. }
  498. ret = 1;
  499. }
  500. }
  501. if( ret )
  502. {
  503. partA.transparent = polygon._.transparent;
  504. partA.schwerpunkt = new Vertex( 0, 0 );
  505. partA.tKordinaten = new Array< Vertex >();
  506. partA.name = new Text( polygon._.name->getText() );
  507. partA.vertex = new Array< Vertex >();
  508. partB.transparent = polygon._.transparent;
  509. partB.schwerpunkt = new Vertex( 0, 0 );
  510. partB.tKordinaten = new Array< Vertex >();
  511. partB.name = new Text( polygon._.name->getText() );
  512. partB.vertex = new Array< Vertex >();
  513. *partA.schwerpunkt += startPoint;
  514. *partB.schwerpunkt += startPoint;
  515. partA.vertex->add( startPoint );
  516. partB.vertex->add( startPoint );
  517. partA.tKordinaten->add( texturSP );
  518. partB.tKordinaten->add( texturSP );
  519. int leftIE = 0;
  520. int rightIE = 0;
  521. while( 1 )
  522. {
  523. pos = startPoint;
  524. Vertex next = startPoint + dir;
  525. Vertex nextT = texturSP + Vertex( dir.x * txtChpPix.x, dir.y * txtChpPix.y );
  526. ret = 0;
  527. bool needOne = !istPunktInnen( next );
  528. int bestI = -1;
  529. float bo1 = 1000;
  530. float bo2 = 1000;
  531. for( int i = 0; i < anz; i++ )
  532. {
  533. if( i == leftI )
  534. continue;
  535. Vertex a = polygon._.vertex->get( i );
  536. Vertex b = polygon._.vertex->get( ( i + 1 ) % anz );
  537. b -= a;
  538. float offset1 = 0;
  539. if( dir.y != 0 && dir.x != 0 )
  540. offset1 = ( ( a.y - pos.y ) / dir.y - ( a.x - pos.x ) / dir.x ) / ( b.x / dir.x - b.y / dir.y ); // solve hitpoint equasion
  541. else if( dir.y == 0 )
  542. offset1 = ( pos.y - a.y ) / b.y;
  543. else if( dir.x == 0 )
  544. offset1 = ( pos.x - a.x ) / b.x;
  545. Vertex point = a + ( b * offset1 );
  546. float offset2 = 0;
  547. if( dir.x != 0 )
  548. offset2 = ( point.x - pos.x ) / dir.x;
  549. else
  550. offset2 = ( point.y - pos.y ) / dir.y;
  551. if( needOne && MIN( abs( bo1 ), abs( bo1 - 1 ) ) + MIN( abs( bo2 ), bo2 - 1 ) > MIN( abs( offset1 ), abs( offset1 - 1 ) ) + MIN( abs( offset2 ), abs( offset2 - 1 ) ) )
  552. {
  553. bo1 = offset1;
  554. bo2 = offset2;
  555. bestI = i;
  556. }
  557. if( offset1 >= 0 && offset1 <= 1 && offset2 >= 0 && offset2 <= 1 )
  558. {
  559. if( !ret || ( startPoint - pos ).getLengthSq() > ( point - pos ).getLengthSq() )
  560. {
  561. leftIE = i;
  562. rightIE = ( i + 1 ) % anz;
  563. startPoint = point;
  564. texturSP = polygon._.tKordinaten->get( i ) + ( polygon._.tKordinaten->get( ( i + 1 ) % anz ) - polygon._.tKordinaten->get( i ) ) * offset1;
  565. }
  566. ret = 1;
  567. }
  568. }
  569. if( needOne && !ret )
  570. {
  571. Vertex a = polygon._.vertex->get( bestI );
  572. Vertex b = polygon._.vertex->get( ( bestI + 1 ) % anz );
  573. b -= a;
  574. leftIE = bestI;
  575. rightIE = ( bestI + 1 ) % anz;
  576. startPoint = a + ( b * bo1 );
  577. texturSP = polygon._.tKordinaten->get( bestI ) + ( polygon._.tKordinaten->get( ( bestI + 1 ) % anz ) - polygon._.tKordinaten->get( bestI ) ) * bo1;
  578. ret = 1;
  579. }
  580. if( ret )
  581. break;
  582. *partA.schwerpunkt += next;
  583. *partB.schwerpunkt += next;
  584. partA.vertex->add( next );
  585. partB.vertex->add( next );
  586. partA.tKordinaten->add( nextT );
  587. partB.tKordinaten->add( nextT );
  588. startPoint = next;
  589. texturSP = nextT;
  590. dir = originalDir.rotation( (float)( random() - 0.5 ) );
  591. }
  592. *partA.schwerpunkt += startPoint;
  593. *partB.schwerpunkt += startPoint;
  594. partA.vertex->add( startPoint );
  595. partB.vertex->add( startPoint );
  596. partA.tKordinaten->add( texturSP );
  597. partB.tKordinaten->add( texturSP );
  598. for( int i = rightIE; i != leftI; i++ )
  599. {
  600. i = i % anz;
  601. if( i == leftI )
  602. break;
  603. *partA.schwerpunkt += polygon._.vertex->get( i );
  604. partA.vertex->add( polygon._.vertex->get( i ) );
  605. partA.tKordinaten->add( polygon._.tKordinaten->get( i ) );
  606. }
  607. *partA.schwerpunkt += polygon._.vertex->get( leftI );
  608. partA.vertex->add( polygon._.vertex->get( leftI ) );
  609. partA.tKordinaten->add( polygon._.tKordinaten->get( leftI ) );
  610. for( int i = leftIE; i != rightI; i-- )
  611. {
  612. if( i < 0 )
  613. i += anz;
  614. if( i == rightI )
  615. break;
  616. *partB.schwerpunkt += polygon._.vertex->get( i );
  617. partB.vertex->add( polygon._.vertex->get( i ) );
  618. partB.tKordinaten->add( polygon._.tKordinaten->get( i ) );
  619. }
  620. *partB.schwerpunkt += polygon._.vertex->get( rightI );
  621. partB.vertex->add( polygon._.vertex->get( rightI ) );
  622. partB.tKordinaten->add( polygon._.tKordinaten->get( rightI ) );
  623. *partA.schwerpunkt /= (float)partA.vertex->getEintragAnzahl();
  624. *partB.schwerpunkt /= (float)partB.vertex->getEintragAnzahl();
  625. posA = (Punkt)* partA.schwerpunkt;
  626. posB = (Punkt)* partB.schwerpunkt;
  627. for( int i = 0; i < partA.vertex->getEintragAnzahl(); i++ )
  628. partA.vertex->set( partA.vertex->get( i ) - *partA.schwerpunkt, i );
  629. for( int i = 0; i < partB.vertex->getEintragAnzahl(); i++ )
  630. partB.vertex->set( partB.vertex->get( i ) - *partB.schwerpunkt, i );
  631. *partA.schwerpunkt = Vertex( 0, 0 );
  632. *partB.schwerpunkt = Vertex( 0, 0 );
  633. }
  634. }
  635. }
  636. return ret;
  637. }
  638. float Model2DData::getMasse() const
  639. {
  640. float m = 0;
  641. for( auto p = polygons->getIterator(); p; p++ )
  642. {
  643. if( p._.transparent )
  644. continue;
  645. int anz = p._.vertex->getEintragAnzahl();
  646. if( anz < 3 )
  647. continue;
  648. Vertex p1 = p._.vertex->get( anz - 1 );
  649. Vertex p2 = p._.vertex->get( 0 );
  650. m += ( p1.y + p2.y ) * ( p1.x - p2.x );
  651. for( int i = 1; i < anz; i++ )
  652. {
  653. p1 = p._.vertex->get( i - 1 );
  654. p2 = p._.vertex->get( i );
  655. m += ( p1.y + p2.y ) * ( p1.x - p2.x );
  656. }
  657. }
  658. m *= 0.5f;
  659. return m;
  660. }
  661. // Reference Counting
  662. Model2DData * Model2DData::getThis()
  663. {
  664. ref++;
  665. return this;
  666. }
  667. Model2DData *Model2DData::release()
  668. {
  669. ref--;
  670. if( !ref )
  671. delete this;
  672. return 0;
  673. }
  674. // Inhalt der Model2D Klasse aus Model2D.h
  675. // Konstruktor
  676. Model2DObject::Model2DObject()
  677. : Object2D()
  678. {
  679. rData = 0;
  680. textur = new RCArray< Textur2D >();
  681. }
  682. // Destruktor
  683. Model2DObject::~Model2DObject()
  684. {
  685. if( rData )
  686. rData->release();
  687. textur->release();
  688. }
  689. // nicht constant
  690. void Model2DObject::setModel( Model2DData * mdl )
  691. {
  692. if( rData )
  693. rData->release();
  694. rData = mdl;
  695. }
  696. void Model2DObject::setTextur( Textur2D * t )
  697. {
  698. int index = 0;
  699. if( rData )
  700. {
  701. for( auto i = rData->polygons->getIterator(); i; i++ )
  702. textur->set( t->getThis(), index++ );
  703. }
  704. t->release();
  705. }
  706. void Model2DObject::impuls( Vertex start, Vertex speed, float strength )
  707. {
  708. start = getObjectPos( start );
  709. speed = getObjectDir( speed );
  710. if( rData )
  711. {
  712. Vertex resSpeed;
  713. float resRotSpeed = 0;
  714. Vertex hp;
  715. Vertex mSpeed;
  716. float rSpeed;
  717. float dist = INFINITY;
  718. for( auto p = rData->polygons->getIterator(); p; p++ )
  719. {
  720. if( !p._.transparent && rData->calcHitPoint( start, speed, p._.name->getText(), hp, mSpeed, rSpeed ) )
  721. {
  722. float f = ( hp.x - start.x ) / speed.x;
  723. if( !speed.x )
  724. f = ( hp.y - start.y ) / speed.y;
  725. if( ( hp - start ).getLengthSq() < dist && f > 0 )
  726. {
  727. resSpeed = mSpeed.rotation( rotation );
  728. resRotSpeed = rSpeed;
  729. dist = ( hp - start ).getLengthSq();
  730. }
  731. }
  732. }
  733. // TODO schleife über alle polygone und translation von start und speed in Object koordinaten
  734. if( dist < INFINITY )
  735. {
  736. this->speed += resSpeed * strength;
  737. this->rSpeed += resRotSpeed * strength;
  738. }
  739. }
  740. }
  741. void Model2DObject::setTextur( Textur2D * t, const char *polygonName )
  742. {
  743. int index = 0;
  744. for( auto i = rData->polygons->getIterator(); i; i++, index++ )
  745. {
  746. if( i._.name->istGleich( polygonName ) )
  747. textur->set( t->getThis(), index );
  748. }
  749. t->release();
  750. }
  751. void Model2DObject::render( Mat3< float > & kamMat, Bild & zRObj, const char *kamName )
  752. {
  753. if( !rData || !rData->polygons || !textur )
  754. return;
  755. int num = 0;
  756. for( auto p = rData->vListen->getIterator(); p; p++, num++ )
  757. {
  758. Mat3< float > mat = kamMat * getObjectMatrix();
  759. if( textur->z( num ) )
  760. {
  761. Bild *txt = textur->z( num )->zTextur();
  762. for( auto i = p->getIterator(); i && txt; i++ )
  763. {
  764. for( auto j = i->zListe()->getIterator(); j.hasNext() && j.next().hasNext(); j++ )
  765. {
  766. Vertex a = mat * *j->punkt;
  767. Vertex b = mat * *j.next()->punkt;
  768. Vertex c = mat * *j.next().next()->punkt;
  769. Punkt ta = (Punkt)Vertex( j->textur->x * (float)( txt->getBreite() - 1 ), j->textur->y * (float)( txt->getHeight() - 1 ) );
  770. Punkt tb = (Punkt)Vertex( j.next()->textur->x * (float)( txt->getBreite() - 1 ), j.next()->textur->y * (float)( txt->getHeight() - 1 ) );
  771. Punkt tc = (Punkt)Vertex( j.next().next()->textur->x * (float)( txt->getBreite() - 1 ), j.next().next()->textur->y * (float)( txt->getHeight() - 1 ) );
  772. zRObj.drawDreieckTexturAlpha( a, b, c, ta, tb, tc, *txt );
  773. }
  774. }
  775. }
  776. }
  777. /* Draws 2D Mesh
  778. for( auto *p = &rData->vListen->getIterator(); p && p->set; p = p->next, num++ )
  779. {
  780. Mat3< float > mat = kamMat * getObjectMatrix();
  781. for( auto *i = &p->var->getArray(); i && i->set; i = i->next )
  782. {
  783. for( auto *j = &i->var->zListe()->getArray(); j->next->next && j->next->next->set; j = j->next )
  784. {
  785. Vertex a = mat * *j->var->punkt;
  786. Vertex b = mat * *j->next->var->punkt;
  787. Vertex c = mat * *j->next->next->var->punkt;
  788. zRObj.drawLinie( a, b, 0xFFFFFFFF );
  789. zRObj.drawLinie( a, c, 0xFFFFFFFF );
  790. zRObj.drawLinie( b, c, 0xFFFFFFFF );
  791. }
  792. }
  793. }
  794. */
  795. }
  796. // constant
  797. bool Model2DObject::istPunktInnen( Vertex p, bool ignoreTransparent ) const
  798. {
  799. if( !rData )
  800. return 0;
  801. p -= position;
  802. if( p < Mat3< float >::scaling( size ) * rData->minP || p > Mat3< float >::scaling( size ) * rData->maxP || !rData->polygons )
  803. return 0;
  804. int num = 0;
  805. Mat3< float > mat = Mat3< float >::rotation( -rotation ) * Mat3< float >::scaling( 1 / size );
  806. p = mat * p;
  807. for( auto polygon = rData->polygons->getIterator(); polygon; polygon++, num++ )
  808. {
  809. if( polygon._.transparent && !ignoreTransparent )
  810. continue;
  811. bool c = 0;
  812. for( auto point = polygon._.vertex->getIterator(); point; point++ )
  813. {
  814. Vertex a;
  815. if( point.next() )
  816. a = point.next();
  817. else
  818. a = polygon._.vertex->get( 0 );
  819. Vertex b = point;
  820. 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 ) )
  821. c = !c;
  822. }
  823. if( c )
  824. return 1;
  825. }
  826. return 0;
  827. }
  828. bool Model2DObject::istLinieInnen( Vertex a, Vertex b, bool ignoreTransparent ) const
  829. {
  830. if( !rData || !rData->polygons )
  831. return 0;
  832. int pAnz = rData->polygons->getEintragAnzahl();
  833. for( int p = 0; p < pAnz; p++ )
  834. {
  835. if( rData->polygons->get( p ).transparent && !ignoreTransparent )
  836. continue;
  837. Mat3< float > mat = Mat3< float >::rotation( rotation ) * Mat3< float >::scaling( size );
  838. int anz = rData->polygons->get( p ).vertex->getEintragAnzahl();
  839. int j = anz - 1;
  840. for( int i = 0; i < anz; i++ )
  841. {
  842. Punkt va = mat * rData->polygons->get( p ).vertex->get( i );
  843. Punkt vb = mat * rData->polygons->get( p ).vertex->get( j );
  844. if( (Punkt)a == position + va && (Punkt)b == position + vb )
  845. return 1;
  846. if( (Punkt)a == position + vb && (Punkt)b == position + va )
  847. return 1;
  848. j = i;
  849. }
  850. Vertex len = b - a;
  851. Vertex speed( len.x > 0 ? 1 : -1.f, len.y > 0 ? 1 : -1.f );
  852. int mLen = 0;
  853. if( fabs( len.x ) > fabs( len.y ) )
  854. {
  855. mLen = (int)fabs( len.x );
  856. speed.y = len.y / (float)fabs( len.x );
  857. }
  858. else
  859. {
  860. mLen = (int)fabs( len.y );
  861. speed.x = len.x / (float)fabs( len.y );
  862. }
  863. int i = 1;
  864. bool inside = 1;
  865. for( Vertex vp = speed + a; (Punkt)vp != (Punkt)( b - speed ) && inside && i < mLen - 1; vp += speed, i++ )
  866. inside &= istPunktInnen( vp, ignoreTransparent );
  867. if( inside )
  868. return 1;
  869. }
  870. return 0;
  871. }
  872. bool Model2DObject::istModelInnen( const Object2D * zObj, Vertex * sp, bool end, bool ignoreTransparent ) const
  873. {
  874. if( !end )
  875. {
  876. if( !getBoundingBox().collidesWith( zObj->getBoundingBox() ) )
  877. return 0;
  878. }
  879. Mat3< float > mat = getObjectMatrix();
  880. for( auto polygon = rData->polygons->getIterator(); polygon; polygon++ )
  881. {
  882. if( polygon._.transparent && !ignoreTransparent )
  883. continue;
  884. int anz = polygon._.vertex->getEintragAnzahl();
  885. for( int i = 0; i < anz; i++ )
  886. {
  887. if( zObj->istPunktInnen( mat * polygon._.vertex->get( i ), ignoreTransparent ) )
  888. {
  889. if( sp )
  890. *sp = mat * polygon._.vertex->get( i );
  891. return 1;
  892. }
  893. }
  894. }
  895. if( end )
  896. return 0;
  897. return zObj->istModelInnen( this, sp, 1, ignoreTransparent );
  898. }
  899. Rect2< float > Model2DObject::getBoundingBox() const
  900. {
  901. if( rData )
  902. return Rect2< float >{ (Vertex)rData->minP *size + position, (Vertex)rData->maxP *size + position };
  903. return Rect2< float >();
  904. }
  905. bool Model2DObject::calcHitPoint( Vertex pos, Vertex dir, Vertex & hitpoint ) const
  906. {
  907. pos = getObjectPos( pos );
  908. dir = getObjectDir( dir );
  909. Vertex ms;
  910. Vertex hp;
  911. float rs;
  912. float dist = INFINITY;
  913. if( rData )
  914. {
  915. for( auto p = rData->polygons->getIterator(); p; p++ )
  916. {
  917. if( !p._.transparent && rData->calcHitPoint( pos, dir, p._.name->getText(), hp, ms, rs ) )
  918. {
  919. float f = ( hp.x - pos.x ) / dir.x;
  920. if( !speed.x )
  921. f = ( hp.y - pos.y ) / dir.y;
  922. if( ( hp - pos ).getLengthSq() < dist && f > 0 )
  923. {
  924. hitpoint = getObjectMatrix() * hp;
  925. dist = ( hp - pos ).getLengthSq();
  926. }
  927. }
  928. }
  929. if( dist < INFINITY )
  930. return 1;
  931. }
  932. return 0;
  933. }
  934. float Model2DObject::getLuftWiederstand() const
  935. {
  936. if( !rData )
  937. return 0;
  938. float angle = speed.angle( Vertex( 1, 0 ) );
  939. float faktor = -1;
  940. if( getDrehung() > PI )
  941. faktor = -faktor;
  942. if( getDrehung() < -PI )
  943. faktor = -faktor;
  944. Mat3< float > m = Mat3< float >::rotation( rotation + faktor * angle ) * Mat3< float >::scaling( size );
  945. float yMin = INFINITY;
  946. float yMax = -INFINITY;
  947. for( auto p = rData->polygons->getIterator(); p; p++ )
  948. {
  949. if( p._.transparent )
  950. continue;
  951. for( auto point = p._.vertex->getIterator(); point; point++ )
  952. {
  953. Vertex v = m * point._;
  954. if( v.y > yMax )
  955. yMax = v.y;
  956. if( v.y < yMin )
  957. yMin = v.y;
  958. }
  959. }
  960. if( yMin != INFINITY )
  961. {
  962. return yMax - yMin;
  963. }
  964. return 0;
  965. }
  966. float Model2DObject::getMasse() const
  967. {
  968. if( !rData )
  969. return 0;
  970. return abs( rData->getMasse() * size * size );
  971. }
  972. // Gibt die Textur des ersten Polygons zurück
  973. Textur2D *Model2DObject::getTextur() const
  974. {
  975. return textur->get( 0 );
  976. }
  977. // Gibt die Textur eines Polygons zurück
  978. // polygonName: Der Name des Polygons
  979. Textur2D *Model2DObject::getTextur( const char *polygonName ) const
  980. {
  981. int index = 0;
  982. for( auto i = rData->polygons->getIterator(); i; i++, index++ )
  983. {
  984. if( i._.name->istGleich( polygonName ) )
  985. return textur->get( index );
  986. }
  987. return 0;
  988. }
  989. // Gibt die Textur des ersten Polygons ohne erhöhten Reference Counter zurück
  990. Textur2D *Model2DObject::zTextur() const
  991. {
  992. return textur->z( 0 );
  993. }
  994. // Gibt die Textur eines Polygons ohne erhöhten Reference Counter zurück
  995. // polygonName: Der Name des Polygons
  996. Textur2D *Model2DObject::zTextur( const char *polygonName ) const
  997. {
  998. int index = 0;
  999. for( auto i = rData->polygons->getIterator(); i; i++, index++ )
  1000. {
  1001. if( i._.name->istGleich( polygonName ) )
  1002. return textur->z( index );
  1003. }
  1004. return 0;
  1005. }
  1006. Model2DData *Model2DObject::getModel() const
  1007. {
  1008. return rData ? rData->getThis() : 0;
  1009. }
  1010. Model2DData *Model2DObject::zModel() const
  1011. {
  1012. return rData;
  1013. }
  1014. // Inhalt der Model2D Klasse aus Model2D.h
  1015. // Konstruktor
  1016. Model2D::Model2D()
  1017. : Zeichnung()
  1018. {
  1019. farbe = 0;
  1020. style = 0;
  1021. rData = 0;
  1022. drehung = 0;
  1023. size = 1;
  1024. textur = new RCArray< Textur2D >;
  1025. }
  1026. // Destruktor
  1027. Model2D::~Model2D()
  1028. {
  1029. if( rData )
  1030. rData->release();
  1031. textur->release();
  1032. }
  1033. // nicht constant
  1034. void Model2D::setModel( Model2DData * mdl )
  1035. {
  1036. if( rData )
  1037. rData->release();
  1038. rData = mdl;
  1039. }
  1040. void Model2D::setDrehung( float drehung )
  1041. {
  1042. this->drehung = drehung;
  1043. while( this->drehung > PI * 2 )
  1044. this->drehung -= (float)PI * 2;
  1045. while( this->drehung < 0 )
  1046. this->drehung += (float)PI * 2;
  1047. rend = 1;
  1048. }
  1049. void Model2D::addDrehung( float drehung )
  1050. {
  1051. this->drehung += drehung;
  1052. while( this->drehung > PI * 2 )
  1053. this->drehung -= (float)PI * 2;
  1054. while( this->drehung < 0 )
  1055. this->drehung += (float)PI * 2;
  1056. rend = 1;
  1057. }
  1058. void Model2D::setSize( float size )
  1059. {
  1060. this->size = size;
  1061. rend = 1;
  1062. }
  1063. void Model2D::addSize( float size )
  1064. {
  1065. this->size += size;
  1066. rend = 1;
  1067. }
  1068. void Model2D::setTextur( Textur2D * t )
  1069. {
  1070. int index = 0;
  1071. if( rData )
  1072. {
  1073. for( auto i = rData->polygons->getIterator(); i; i++ )
  1074. textur->set( t->getThis(), index++ );
  1075. }
  1076. t->release();
  1077. }
  1078. void Model2D::setTextur( Textur2D * t, const char *polygonName )
  1079. {
  1080. int index = 0;
  1081. for( auto i = rData->polygons->getIterator(); i; i++, index++ )
  1082. {
  1083. if( i._.name->istGleich( polygonName ) )
  1084. textur->set( t->getThis(), index );
  1085. }
  1086. t->release();
  1087. }
  1088. void Model2D::setFarbe( int f )
  1089. {
  1090. farbe = f;
  1091. rend = 1;
  1092. }
  1093. void Model2D::doMausEreignis( MausEreignis & me )
  1094. {
  1095. if( !mak || me.verarbeitet || !istPunktInnen( Punkt( me.mx, me.my ) ) )
  1096. return;
  1097. me.mx -= pos.x;
  1098. me.my -= pos.y;
  1099. mak( makParam, this, me );
  1100. me.mx += pos.x;
  1101. me.my += pos.y;
  1102. me.verarbeitet = 1;
  1103. }
  1104. bool Model2D::tick( double tickVal )
  1105. {
  1106. return Zeichnung::tick( tickVal );
  1107. }
  1108. void Model2D::render( Bild & zRObj )
  1109. {
  1110. if( !rData || hatStyleNicht( Model2D::Style::Sichtbar ) || !rData->polygons )
  1111. return;
  1112. Zeichnung::render( zRObj );
  1113. int num = 0;
  1114. for( auto p = rData->vListen->getIterator(); p; p++, num++ )
  1115. {
  1116. Mat3< float > mat = Mat3< float >::translation( pos ) * Mat3< float >::rotation( drehung ) * Mat3< float >::scaling( size );
  1117. if( hatStyle( Model2D::Style::Textur ) )
  1118. {
  1119. if( !textur || !textur->z( num ) || !textur->z( num )->zTextur() || !rData->polygons->get( num ).tKordinaten )
  1120. {
  1121. for( auto i = p->getIterator(); i; i++ )
  1122. {
  1123. for( auto j = i->zListe()->getIterator(); j.hasNext() && j.next().hasNext(); j++ )
  1124. {
  1125. Vertex a = mat * *j->punkt;
  1126. Vertex b = mat * *j.next()->punkt;
  1127. Vertex c = mat * *j.next().next()->punkt;
  1128. if( hatStyle( Model2D::Style::Alpha ) )
  1129. zRObj.drawDreieckAlpha( a, b, c, farbe );
  1130. else
  1131. zRObj.drawDreieck( a, b, c, farbe );
  1132. }
  1133. }
  1134. }
  1135. else
  1136. {
  1137. Bild *txt = textur->z( num )->zTextur();
  1138. for( auto i = p->getIterator(); i; i++ )
  1139. {
  1140. for( auto j = i->zListe()->getIterator(); j.hasNext() && j.next().hasNext(); j++ )
  1141. {
  1142. Vertex a = mat * *j->punkt;
  1143. Vertex b = mat * *j.next()->punkt;
  1144. Vertex c = mat * *j.next().next()->punkt;
  1145. Punkt ta = (Punkt)Vertex( j->textur->x * (float)( txt->getBreite() - 1 ), j->textur->y * (float)( txt->getHeight() - 1 ) );
  1146. Punkt tb = (Punkt)Vertex( j.next()->textur->x * (float)( txt->getBreite() - 1 ), j.next()->textur->y * (float)( txt->getHeight() - 1 ) );
  1147. Punkt tc = (Punkt)Vertex( j.next().next()->textur->x * (float)( txt->getBreite() - 1 ), j.next().next()->textur->y * (float)( txt->getHeight() - 1 ) );
  1148. if( hatStyle( Model2D::Style::Alpha ) )
  1149. zRObj.drawDreieckTexturAlpha( a, b, c, ta, tb, tc, *txt );
  1150. else
  1151. zRObj.drawDreieckTextur( a, b, c, ta, tb, tc, *txt );
  1152. }
  1153. }
  1154. }
  1155. }
  1156. if( hatStyle( Model2D::Style::Mesh ) )
  1157. {
  1158. for( auto i = p->getIterator(); i; i++ )
  1159. {
  1160. for( auto j = i->zListe()->getIterator(); j.hasNext() && j.next().hasNext(); j++ )
  1161. {
  1162. Vertex a = mat * *j->punkt;
  1163. Vertex b = mat * *j.next()->punkt;
  1164. Vertex c = mat * *j.next().next()->punkt;
  1165. if( hatStyle( Model2D::Style::Alpha ) )
  1166. {
  1167. zRObj.drawLinieAlpha( a, b, farbe );
  1168. zRObj.drawLinieAlpha( b, c, farbe );
  1169. zRObj.drawLinieAlpha( c, a, farbe );
  1170. }
  1171. else
  1172. {
  1173. zRObj.drawLinie( a, b, farbe );
  1174. zRObj.drawLinie( b, c, farbe );
  1175. zRObj.drawLinie( c, a, farbe );
  1176. }
  1177. }
  1178. }
  1179. }
  1180. if( hatStyle( Model2D::Style::Rahmen ) )
  1181. {
  1182. auto beg = rData->polygons->get( num ).vertex->getIterator();
  1183. if( beg )
  1184. {
  1185. Vertex letzter;
  1186. for( auto e = beg; e && e.hasNext(); e++ )
  1187. {
  1188. if( hatStyle( Model2D::Style::Alpha ) )
  1189. zRObj.drawLinieAlpha( mat * e._, mat * e.next()._, farbe );
  1190. else
  1191. zRObj.drawLinie( mat * e._, mat * e.next()._, farbe );
  1192. letzter = e.next();
  1193. }
  1194. if( beg.hasNext() )
  1195. {
  1196. if( hatStyle( Model2D::Style::Alpha ) )
  1197. zRObj.drawLinieAlpha( mat * letzter, mat * beg._, farbe );
  1198. else
  1199. zRObj.drawLinie( mat * letzter, mat * beg._, farbe );
  1200. }
  1201. }
  1202. }
  1203. }
  1204. }
  1205. // constant
  1206. float Model2D::getDrehung() const
  1207. {
  1208. return drehung;
  1209. }
  1210. float Model2D::getSize() const
  1211. {
  1212. return size;
  1213. }
  1214. bool Model2D::istPunktInnen( Vertex p ) const
  1215. {
  1216. if( !rData )
  1217. return 0;
  1218. p -= pos;
  1219. if( p < Mat3< float >::scaling( size ) * rData->minP || p > Mat3< float >::scaling( size ) * rData->maxP || !rData->polygons )
  1220. return 0;
  1221. int num = 0;
  1222. for( auto polygon = rData->polygons->getIterator(); polygon; polygon++, num++ )
  1223. {
  1224. if( polygon._.transparent )
  1225. continue;
  1226. Mat3< float > mat = Mat3< float >::rotation( drehung ) * Mat3< float >::scaling( size );
  1227. int anz = polygon._.vertex->getEintragAnzahl();
  1228. bool c = 0;
  1229. int j = anz - 1;
  1230. for( int i = 0; i < anz; i++ )
  1231. {
  1232. Vertex a = mat * polygon._.vertex->get( i );
  1233. Vertex b = mat * polygon._.vertex->get( j );
  1234. 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 ) )
  1235. c = !c;
  1236. j = i;
  1237. }
  1238. if( c )
  1239. return 1;
  1240. }
  1241. return 0;
  1242. }
  1243. bool Model2D::istLinieInnen( Vertex a, Vertex b ) const
  1244. {
  1245. if( !rData || !rData->polygons )
  1246. return 0;
  1247. int pAnz = rData->polygons->getEintragAnzahl();
  1248. for( int p = 0; p < pAnz; p++ )
  1249. {
  1250. if( rData->polygons->get( p ).transparent )
  1251. continue;
  1252. Mat3< float > mat = Mat3< float >::rotation( drehung ) * Mat3< float >::scaling( size );
  1253. int anz = rData->polygons->get( p ).vertex->getEintragAnzahl();
  1254. int j = anz - 1;
  1255. for( int i = 0; i < anz; i++ )
  1256. {
  1257. Punkt va = mat * rData->polygons->get( p ).vertex->get( i );
  1258. Punkt vb = mat * rData->polygons->get( p ).vertex->get( j );
  1259. if( (Punkt)a == pos + va && (Punkt)b == pos + vb )
  1260. return 1;
  1261. if( (Punkt)a == pos + vb && (Punkt)b == pos + va )
  1262. return 1;
  1263. j = i;
  1264. }
  1265. Vertex len = b - a;
  1266. Vertex speed( len.x > 0 ? 1 : -1.f, len.y > 0 ? 1 : -1.f );
  1267. int mLen = 0;
  1268. if( fabs( len.x ) > fabs( len.y ) )
  1269. {
  1270. mLen = (int)fabs( len.x );
  1271. speed.y = len.y / (float)fabs( len.x );
  1272. }
  1273. else
  1274. {
  1275. mLen = (int)fabs( len.y );
  1276. speed.x = len.x / (float)fabs( len.y );
  1277. }
  1278. int i = 1;
  1279. bool inside = 1;
  1280. for( Vertex vp = speed + a; (Punkt)vp != (Punkt)( b - speed ) && inside && i < mLen - 1; vp += speed, i++ )
  1281. inside &= istPunktInnen( vp );
  1282. if( inside )
  1283. return 1;
  1284. }
  1285. return 0;
  1286. }
  1287. bool Model2D::istModelInnen( const Model2D * zMdl, bool end ) const
  1288. {
  1289. if( !end )
  1290. {
  1291. Vertex min = (Vertex)rData->minP * size + pos;
  1292. Vertex max = (Vertex)rData->maxP * size + pos;
  1293. Vertex min2 = (Vertex)zMdl->zModel()->minP * zMdl->getSize() + zMdl->getPosition();
  1294. Vertex max2 = (Vertex)zMdl->zModel()->maxP * zMdl->getSize() + zMdl->getPosition();
  1295. if( max.x < min2.x || min.x > max2.x || max.y < min2.y || min.y > max2.y )
  1296. return 0;
  1297. }
  1298. Mat3< float > mat = Mat3< float >::translation( pos ) * Mat3< float >::rotation( drehung ) * Mat3< float >::scaling( size );
  1299. for( auto polygon = rData->polygons->getIterator(); polygon; polygon++ )
  1300. {
  1301. if( polygon._.transparent )
  1302. continue;
  1303. int anz = polygon._.vertex->getEintragAnzahl();
  1304. for( int i = 0; i < anz; i++ )
  1305. {
  1306. if( zMdl->istPunktInnen( mat * polygon._.vertex->get( i ) ) )
  1307. return 1;
  1308. }
  1309. }
  1310. if( end )
  1311. return 0;
  1312. return zMdl->istModelInnen( this, 1 );
  1313. }
  1314. Model2DData *Model2D::getModel() const
  1315. {
  1316. return rData ? rData->getThis() : 0;
  1317. }
  1318. Model2DData *Model2D::zModel() const
  1319. {
  1320. return rData;
  1321. }