Model2D.cpp 48 KB

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