Model2D.cpp 48 KB

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