Model2D.cpp 48 KB

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