Model2D.cpp 48 KB

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