Model2D.cpp 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359
  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.begin();
  46. for( auto polygon = polygons->begin(); 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->begin(); 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->begin();
  62. for( int i = 0; i < anz; i++, point++ )
  63. {
  64. bool cont = 0;
  65. for( auto outListPP = outListP->begin(); 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( Polygon2D polygon : *polygons )
  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( Polygon2D polygon : *polygons )
  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. num++;
  635. }
  636. return ret;
  637. }
  638. float Model2DData::getMasse() const
  639. {
  640. float m = 0;
  641. for( Polygon2D p : *polygons )
  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. // Inhalt der Model2D Klasse aus Model2D.h
  662. // Konstruktor
  663. Model2DObject::Model2DObject()
  664. : Object2D()
  665. {
  666. rData = 0;
  667. textur = new RCArray< Textur2D >();
  668. }
  669. // Destruktor
  670. Model2DObject::~Model2DObject()
  671. {
  672. if( rData )
  673. rData->release();
  674. textur->release();
  675. }
  676. // nicht constant
  677. void Model2DObject::setModel( Model2DData *mdl )
  678. {
  679. if( rData )
  680. rData->release();
  681. rData = mdl;
  682. }
  683. void Model2DObject::setTextur( Textur2D *t )
  684. {
  685. if( rData )
  686. {
  687. for( int i = 0; i< rData->polygons->getEintragAnzahl(); i++ )
  688. textur->set( dynamic_cast<Textur2D *>( t->getThis() ), i );
  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( Polygon2D p : *rData->polygons )
  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( Polygon2D i : *rData->polygons )
  731. {
  732. if( i.name->istGleich( polygonName ) )
  733. textur->set( dynamic_cast<Textur2D *>( t->getThis() ), index );
  734. index++;
  735. }
  736. t->release();
  737. }
  738. void Model2DObject::render( Mat3< float > &kamMat, Bild &zRObj, const char *kamName )
  739. {
  740. if( !rData || !rData->polygons || !textur )
  741. return;
  742. int num = 0;
  743. for( auto p : *rData->vListen )
  744. {
  745. Mat3< float > mat = kamMat * getObjectMatrix();
  746. if( textur->z( num ) )
  747. {
  748. Bild *txt = textur->z( num )->zTextur();
  749. for( auto i = p->begin(); i && txt; i++ )
  750. {
  751. for( auto j = i->zListe()->begin(); j.hasNext() && j.next().hasNext(); j++ )
  752. {
  753. Vertex a = mat * *j->punkt;
  754. Vertex b = mat * *j.next()->punkt;
  755. Vertex c = mat * *j.next().next()->punkt;
  756. Punkt ta = (Punkt)Vertex( j->textur->x * (float)( txt->getBreite() - 1 ), j->textur->y * (float)( txt->getHeight() - 1 ) );
  757. Punkt tb = (Punkt)Vertex( j.next()->textur->x * (float)( txt->getBreite() - 1 ), j.next()->textur->y * (float)( txt->getHeight() - 1 ) );
  758. Punkt tc = (Punkt)Vertex( j.next().next()->textur->x * (float)( txt->getBreite() - 1 ), j.next().next()->textur->y * (float)( txt->getHeight() - 1 ) );
  759. zRObj.drawDreieckTexturAlpha( a, b, c, ta, tb, tc, *txt );
  760. }
  761. }
  762. }
  763. num++;
  764. }
  765. /* Draws 2D Mesh
  766. for( auto *p = &rData->vListen->getIterator(); p && p->set; p = p->next, num++ )
  767. {
  768. Mat3< float > mat = kamMat * getObjectMatrix();
  769. for( auto *i = &p->var->getArray(); i && i->set; i = i->next )
  770. {
  771. for( auto *j = &i->var->zListe()->getArray(); j->next->next && j->next->next->set; j = j->next )
  772. {
  773. Vertex a = mat * *j->var->punkt;
  774. Vertex b = mat * *j->next->var->punkt;
  775. Vertex c = mat * *j->next->next->var->punkt;
  776. zRObj.drawLinie( a, b, 0xFFFFFFFF );
  777. zRObj.drawLinie( a, c, 0xFFFFFFFF );
  778. zRObj.drawLinie( b, c, 0xFFFFFFFF );
  779. }
  780. }
  781. }
  782. */
  783. }
  784. // constant
  785. bool Model2DObject::istPunktInnen( Vertex p, bool ignoreTransparent ) const
  786. {
  787. if( !rData )
  788. return 0;
  789. p -= position;
  790. if( p < Mat3< float >::scaling( size ) * rData->minP || p > Mat3< float >::scaling( size ) * rData->maxP || !rData->polygons )
  791. return 0;
  792. Mat3< float > mat = Mat3< float >::rotation( -rotation ) * Mat3< float >::scaling( 1 / size );
  793. p = mat * p;
  794. for( Polygon2D polygon : *rData->polygons)
  795. {
  796. if( polygon.transparent && !ignoreTransparent )
  797. continue;
  798. bool c = 0;
  799. for( auto point = polygon.vertex->begin(); point; point++ )
  800. {
  801. Vertex a;
  802. if( point.next() )
  803. a = point.next();
  804. else
  805. a = polygon.vertex->get( 0 );
  806. Vertex b = point;
  807. 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 ) )
  808. c = !c;
  809. }
  810. if( c )
  811. return 1;
  812. }
  813. return 0;
  814. }
  815. bool Model2DObject::istLinieInnen( Vertex a, Vertex b, bool ignoreTransparent ) const
  816. {
  817. if( !rData || !rData->polygons )
  818. return 0;
  819. int pAnz = rData->polygons->getEintragAnzahl();
  820. for( int p = 0; p < pAnz; p++ )
  821. {
  822. if( rData->polygons->get( p ).transparent && !ignoreTransparent )
  823. continue;
  824. Mat3< float > mat = Mat3< float >::rotation( rotation ) * Mat3< float >::scaling( size );
  825. int anz = rData->polygons->get( p ).vertex->getEintragAnzahl();
  826. int j = anz - 1;
  827. for( int i = 0; i < anz; i++ )
  828. {
  829. Punkt va = mat * rData->polygons->get( p ).vertex->get( i );
  830. Punkt vb = mat * rData->polygons->get( p ).vertex->get( j );
  831. if( (Punkt)a == position + va && (Punkt)b == position + vb )
  832. return 1;
  833. if( (Punkt)a == position + vb && (Punkt)b == position + va )
  834. return 1;
  835. j = i;
  836. }
  837. Vertex len = b - a;
  838. Vertex speed( len.x > 0 ? 1 : -1.f, len.y > 0 ? 1 : -1.f );
  839. int mLen = 0;
  840. if( fabs( len.x ) > fabs( len.y ) )
  841. {
  842. mLen = (int)fabs( len.x );
  843. speed.y = len.y / (float)fabs( len.x );
  844. }
  845. else
  846. {
  847. mLen = (int)fabs( len.y );
  848. speed.x = len.x / (float)fabs( len.y );
  849. }
  850. int i = 1;
  851. bool inside = 1;
  852. for( Vertex vp = speed + a; (Punkt)vp != (Punkt)( b - speed ) && inside && i < mLen - 1; vp += speed, i++ )
  853. inside &= istPunktInnen( vp, ignoreTransparent );
  854. if( inside )
  855. return 1;
  856. }
  857. return 0;
  858. }
  859. bool Model2DObject::istModelInnen( const Object2D *zObj, Vertex *sp, bool end, bool ignoreTransparent ) const
  860. {
  861. if( !end )
  862. {
  863. if( !getBoundingBox().collidesWith( zObj->getBoundingBox() ) )
  864. return 0;
  865. }
  866. Mat3< float > mat = getObjectMatrix();
  867. for( Polygon2D polygon : *rData->polygons )
  868. {
  869. if( polygon.transparent && !ignoreTransparent )
  870. continue;
  871. int anz = polygon.vertex->getEintragAnzahl();
  872. for( int i = 0; i < anz; i++ )
  873. {
  874. if( zObj->istPunktInnen( mat * polygon.vertex->get( i ), ignoreTransparent ) )
  875. {
  876. if( sp )
  877. *sp = mat * polygon.vertex->get( i );
  878. return 1;
  879. }
  880. }
  881. }
  882. if( end )
  883. return 0;
  884. return zObj->istModelInnen( this, sp, 1, ignoreTransparent );
  885. }
  886. Rect2< float > Model2DObject::getBoundingBox() const
  887. {
  888. if( rData )
  889. return Rect2< float >{ (Vertex)rData->minP *size + position, (Vertex)rData->maxP *size + position };
  890. return Rect2< float >();
  891. }
  892. bool Model2DObject::calcHitPoint( Vertex pos, Vertex dir, Vertex &hitpoint ) const
  893. {
  894. pos = getObjectPos( pos );
  895. dir = getObjectDir( dir );
  896. Vertex ms;
  897. Vertex hp;
  898. float rs;
  899. float dist = INFINITY;
  900. if( rData )
  901. {
  902. for( Polygon2D p : *rData->polygons )
  903. {
  904. if( !p.transparent && rData->calcHitPoint( pos, dir, p.name->getText(), hp, ms, rs ) )
  905. {
  906. float f = ( hp.x - pos.x ) / dir.x;
  907. if( !speed.x )
  908. f = ( hp.y - pos.y ) / dir.y;
  909. if( ( hp - pos ).getLengthSq() < dist && f > 0 )
  910. {
  911. hitpoint = getObjectMatrix() * hp;
  912. dist = ( hp - pos ).getLengthSq();
  913. }
  914. }
  915. }
  916. if( dist < INFINITY )
  917. return 1;
  918. }
  919. return 0;
  920. }
  921. float Model2DObject::getLuftWiederstand() const
  922. {
  923. if( !rData )
  924. return 0;
  925. float angle = speed.angle( Vertex( 1, 0 ) );
  926. float faktor = -1;
  927. if( getDrehung() > PI )
  928. faktor = -faktor;
  929. if( getDrehung() < -PI )
  930. faktor = -faktor;
  931. Mat3< float > m = Mat3< float >::rotation( rotation + faktor * angle ) * Mat3< float >::scaling( size );
  932. float yMin = INFINITY;
  933. float yMax = -INFINITY;
  934. for( Polygon2D p : *rData->polygons )
  935. {
  936. if( p.transparent )
  937. continue;
  938. for( Vertex point : *p.vertex )
  939. {
  940. Vertex v = m * point;
  941. if( v.y > yMax )
  942. yMax = v.y;
  943. if( v.y < yMin )
  944. yMin = v.y;
  945. }
  946. }
  947. if( yMin != INFINITY )
  948. {
  949. return yMax - yMin;
  950. }
  951. return 0;
  952. }
  953. float Model2DObject::getMasse() const
  954. {
  955. if( !rData )
  956. return 0;
  957. return abs( rData->getMasse() * size * size );
  958. }
  959. // Gibt die Textur des ersten Polygons zurück
  960. Textur2D *Model2DObject::getTextur() const
  961. {
  962. return textur->get( 0 );
  963. }
  964. // Gibt die Textur eines Polygons zurück
  965. // polygonName: Der Name des Polygons
  966. Textur2D *Model2DObject::getTextur( const char *polygonName ) const
  967. {
  968. int index = 0;
  969. for( Polygon2D p : *rData->polygons )
  970. {
  971. if( p.name->istGleich( polygonName ) )
  972. return textur->get( index );
  973. index++;
  974. }
  975. return 0;
  976. }
  977. // Gibt die Textur des ersten Polygons ohne erhöhten Reference Counter zurück
  978. Textur2D *Model2DObject::zTextur() const
  979. {
  980. return textur->z( 0 );
  981. }
  982. // Gibt die Textur eines Polygons ohne erhöhten Reference Counter zurück
  983. // polygonName: Der Name des Polygons
  984. Textur2D *Model2DObject::zTextur( const char *polygonName ) const
  985. {
  986. int index = 0;
  987. for( Polygon2D p : *rData->polygons )
  988. {
  989. if( p.name->istGleich( polygonName ) )
  990. return textur->z( index );
  991. index++;
  992. }
  993. return 0;
  994. }
  995. Model2DData *Model2DObject::getModel() const
  996. {
  997. return rData ? dynamic_cast<Model2DData *>( rData->getThis() ) : 0;
  998. }
  999. Model2DData *Model2DObject::zModel() const
  1000. {
  1001. return rData;
  1002. }
  1003. // Inhalt der Model2D Klasse aus Model2D.h
  1004. // Konstruktor
  1005. Model2D::Model2D()
  1006. : Zeichnung()
  1007. {
  1008. farbe = 0;
  1009. style = 0;
  1010. rData = 0;
  1011. drehung = 0;
  1012. size = 1;
  1013. textur = new RCArray< Textur2D >;
  1014. }
  1015. // Destruktor
  1016. Model2D::~Model2D()
  1017. {
  1018. if( rData )
  1019. rData->release();
  1020. textur->release();
  1021. }
  1022. // nicht constant
  1023. void Model2D::setModel( Model2DData *mdl )
  1024. {
  1025. if( rData )
  1026. rData->release();
  1027. rData = mdl;
  1028. }
  1029. void Model2D::setDrehung( float drehung )
  1030. {
  1031. this->drehung = drehung;
  1032. while( this->drehung > PI * 2 )
  1033. this->drehung -= (float)PI * 2;
  1034. while( this->drehung < 0 )
  1035. this->drehung += (float)PI * 2;
  1036. rend = 1;
  1037. }
  1038. void Model2D::addDrehung( float drehung )
  1039. {
  1040. this->drehung += drehung;
  1041. while( this->drehung > PI * 2 )
  1042. this->drehung -= (float)PI * 2;
  1043. while( this->drehung < 0 )
  1044. this->drehung += (float)PI * 2;
  1045. rend = 1;
  1046. }
  1047. void Model2D::setSize( float size )
  1048. {
  1049. this->size = size;
  1050. rend = 1;
  1051. }
  1052. void Model2D::addSize( float size )
  1053. {
  1054. this->size += size;
  1055. rend = 1;
  1056. }
  1057. void Model2D::setTextur( Textur2D *t )
  1058. {
  1059. if( rData )
  1060. {
  1061. for( int i = 0; i < rData->polygons->getEintragAnzahl(); i++ )
  1062. textur->set( dynamic_cast<Textur2D *>( t->getThis() ), i );
  1063. }
  1064. t->release();
  1065. }
  1066. void Model2D::setTextur( Textur2D *t, const char *polygonName )
  1067. {
  1068. int index = 0;
  1069. for( Polygon2D p : *rData->polygons )
  1070. {
  1071. if( p.name->istGleich( polygonName ) )
  1072. textur->set( dynamic_cast<Textur2D *>( t->getThis() ), index );
  1073. index++;
  1074. }
  1075. t->release();
  1076. }
  1077. void Model2D::setFarbe( int f )
  1078. {
  1079. farbe = f;
  1080. rend = 1;
  1081. }
  1082. bool Model2D::tick( double tickVal )
  1083. {
  1084. return Zeichnung::tick( tickVal );
  1085. }
  1086. void Model2D::render( Bild &zRObj )
  1087. {
  1088. if( !rData || hatStyleNicht( Model2D::Style::Sichtbar ) || !rData->polygons )
  1089. return;
  1090. Zeichnung::render( zRObj );
  1091. int num = 0;
  1092. for( auto p : *rData->vListen )
  1093. {
  1094. Mat3< float > mat = Mat3< float >::translation( pos ) * Mat3< float >::rotation( drehung ) * Mat3< float >::scaling( size );
  1095. if( hatStyle( Model2D::Style::Textur ) )
  1096. {
  1097. if( !textur || !textur->z( num ) || !textur->z( num )->zTextur() || !rData->polygons->get( num ).tKordinaten )
  1098. {
  1099. for( auto i : *p )
  1100. {
  1101. for( auto j = i->zListe()->begin(); j.hasNext() && j.next().hasNext(); j++ )
  1102. {
  1103. Vertex a = mat * *j->punkt;
  1104. Vertex b = mat * *j.next()->punkt;
  1105. Vertex c = mat * *j.next().next()->punkt;
  1106. if( hatStyle( Model2D::Style::Alpha ) )
  1107. zRObj.drawDreieckAlpha( a, b, c, farbe );
  1108. else
  1109. zRObj.drawDreieck( a, b, c, farbe );
  1110. }
  1111. }
  1112. }
  1113. else
  1114. {
  1115. Bild *txt = textur->z( num )->zTextur();
  1116. for( auto i : *p )
  1117. {
  1118. for( auto j = i->zListe()->begin(); j.hasNext() && j.next().hasNext(); j++ )
  1119. {
  1120. Vertex a = mat * *j->punkt;
  1121. Vertex b = mat * *j.next()->punkt;
  1122. Vertex c = mat * *j.next().next()->punkt;
  1123. Punkt ta = (Punkt)Vertex( j->textur->x * (float)( txt->getBreite() - 1 ), j->textur->y * (float)( txt->getHeight() - 1 ) );
  1124. Punkt tb = (Punkt)Vertex( j.next()->textur->x * (float)( txt->getBreite() - 1 ), j.next()->textur->y * (float)( txt->getHeight() - 1 ) );
  1125. Punkt tc = (Punkt)Vertex( j.next().next()->textur->x * (float)( txt->getBreite() - 1 ), j.next().next()->textur->y * (float)( txt->getHeight() - 1 ) );
  1126. if( hatStyle( Model2D::Style::Alpha ) )
  1127. zRObj.drawDreieckTexturAlpha( a, b, c, ta, tb, tc, *txt );
  1128. else
  1129. zRObj.drawDreieckTextur( a, b, c, ta, tb, tc, *txt );
  1130. }
  1131. }
  1132. }
  1133. }
  1134. if( hatStyle( Model2D::Style::Mesh ) )
  1135. {
  1136. for( auto i : *p )
  1137. {
  1138. for( auto j = i->zListe()->begin(); j.hasNext() && j.next().hasNext(); j++ )
  1139. {
  1140. Vertex a = mat * *j->punkt;
  1141. Vertex b = mat * *j.next()->punkt;
  1142. Vertex c = mat * *j.next().next()->punkt;
  1143. if( hatStyle( Model2D::Style::Alpha ) )
  1144. {
  1145. zRObj.drawLinieAlpha( a, b, farbe );
  1146. zRObj.drawLinieAlpha( b, c, farbe );
  1147. zRObj.drawLinieAlpha( c, a, farbe );
  1148. }
  1149. else
  1150. {
  1151. zRObj.drawLinie( a, b, farbe );
  1152. zRObj.drawLinie( b, c, farbe );
  1153. zRObj.drawLinie( c, a, farbe );
  1154. }
  1155. }
  1156. }
  1157. }
  1158. if( hatStyle( Model2D::Style::Rahmen ) )
  1159. {
  1160. auto beg = rData->polygons->get( num ).vertex->begin();
  1161. if( beg )
  1162. {
  1163. Vertex letzter;
  1164. for( auto e = beg; e && e.hasNext(); e++ )
  1165. {
  1166. if( hatStyle( Model2D::Style::Alpha ) )
  1167. zRObj.drawLinieAlpha( mat * e._, mat * e.next()._, farbe );
  1168. else
  1169. zRObj.drawLinie( mat * e._, mat * e.next()._, farbe );
  1170. letzter = e.next();
  1171. }
  1172. if( beg.hasNext() )
  1173. {
  1174. if( hatStyle( Model2D::Style::Alpha ) )
  1175. zRObj.drawLinieAlpha( mat * letzter, mat * beg._, farbe );
  1176. else
  1177. zRObj.drawLinie( mat * letzter, mat * beg._, farbe );
  1178. }
  1179. }
  1180. }
  1181. num++;
  1182. }
  1183. }
  1184. // constant
  1185. float Model2D::getDrehung() const
  1186. {
  1187. return drehung;
  1188. }
  1189. float Model2D::getSize() const
  1190. {
  1191. return size;
  1192. }
  1193. // Gibt zurück, ob ein Punkt in dem Model enthalten ist
  1194. // p: Der Punkt
  1195. bool Model2D::istPunktInnen( int x, int y ) const
  1196. {
  1197. return istPunktInnen( Vertex( (float)x, (float)y ) );
  1198. }
  1199. bool Model2D::istPunktInnen( Vertex p ) const
  1200. {
  1201. if( !rData )
  1202. return 0;
  1203. p -= pos;
  1204. if( p < Mat3< float >::scaling( size ) * rData->minP || p > Mat3< float >::scaling( size ) * rData->maxP || !rData->polygons )
  1205. return 0;
  1206. for( Polygon2D polygon : *rData->polygons )
  1207. {
  1208. if( polygon.transparent )
  1209. continue;
  1210. Mat3< float > mat = Mat3< float >::rotation( drehung ) * Mat3< float >::scaling( size );
  1211. int anz = polygon.vertex->getEintragAnzahl();
  1212. bool c = 0;
  1213. int j = anz - 1;
  1214. for( int i = 0; i < anz; i++ )
  1215. {
  1216. Vertex a = mat * polygon.vertex->get( i );
  1217. Vertex b = mat * polygon.vertex->get( j );
  1218. 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 ) )
  1219. c = !c;
  1220. j = i;
  1221. }
  1222. if( c )
  1223. return 1;
  1224. }
  1225. return 0;
  1226. }
  1227. bool Model2D::istLinieInnen( Vertex a, Vertex b ) const
  1228. {
  1229. if( !rData || !rData->polygons )
  1230. return 0;
  1231. int pAnz = rData->polygons->getEintragAnzahl();
  1232. for( int p = 0; p < pAnz; p++ )
  1233. {
  1234. if( rData->polygons->get( p ).transparent )
  1235. continue;
  1236. Mat3< float > mat = Mat3< float >::rotation( drehung ) * Mat3< float >::scaling( size );
  1237. int anz = rData->polygons->get( p ).vertex->getEintragAnzahl();
  1238. int j = anz - 1;
  1239. for( int i = 0; i < anz; i++ )
  1240. {
  1241. Punkt va = mat * rData->polygons->get( p ).vertex->get( i );
  1242. Punkt vb = mat * rData->polygons->get( p ).vertex->get( j );
  1243. if( (Punkt)a == pos + va && (Punkt)b == pos + vb )
  1244. return 1;
  1245. if( (Punkt)a == pos + vb && (Punkt)b == pos + va )
  1246. return 1;
  1247. j = i;
  1248. }
  1249. Vertex len = b - a;
  1250. Vertex speed( len.x > 0 ? 1 : -1.f, len.y > 0 ? 1 : -1.f );
  1251. int mLen = 0;
  1252. if( fabs( len.x ) > fabs( len.y ) )
  1253. {
  1254. mLen = (int)fabs( len.x );
  1255. speed.y = len.y / (float)fabs( len.x );
  1256. }
  1257. else
  1258. {
  1259. mLen = (int)fabs( len.y );
  1260. speed.x = len.x / (float)fabs( len.y );
  1261. }
  1262. int i = 1;
  1263. bool inside = 1;
  1264. for( Vertex vp = speed + a; (Punkt)vp != (Punkt)( b - speed ) && inside && i < mLen - 1; vp += speed, i++ )
  1265. inside &= istPunktInnen( vp );
  1266. if( inside )
  1267. return 1;
  1268. }
  1269. return 0;
  1270. }
  1271. bool Model2D::istModelInnen( const Model2D *zMdl, bool end ) const
  1272. {
  1273. if( !end )
  1274. {
  1275. Vertex min = (Vertex)rData->minP * size + pos;
  1276. Vertex max = (Vertex)rData->maxP * size + pos;
  1277. Vertex min2 = (Vertex)zMdl->zModel()->minP * zMdl->getSize() + zMdl->getPosition();
  1278. Vertex max2 = (Vertex)zMdl->zModel()->maxP * zMdl->getSize() + zMdl->getPosition();
  1279. if( max.x < min2.x || min.x > max2.x || max.y < min2.y || min.y > max2.y )
  1280. return 0;
  1281. }
  1282. Mat3< float > mat = Mat3< float >::translation( pos ) * Mat3< float >::rotation( drehung ) * Mat3< float >::scaling( size );
  1283. for( Polygon2D polygon : *rData->polygons )
  1284. {
  1285. if( polygon.transparent )
  1286. continue;
  1287. int anz = polygon.vertex->getEintragAnzahl();
  1288. for( int i = 0; i < anz; i++ )
  1289. {
  1290. if( zMdl->istPunktInnen( mat * polygon.vertex->get( i ) ) )
  1291. return 1;
  1292. }
  1293. }
  1294. if( end )
  1295. return 0;
  1296. return zMdl->istModelInnen( this, 1 );
  1297. }
  1298. Model2DData *Model2D::getModel() const
  1299. {
  1300. return rData ? dynamic_cast<Model2DData *>( rData->getThis() ) : 0;
  1301. }
  1302. Model2DData *Model2D::zModel() const
  1303. {
  1304. return rData;
  1305. }