Model2D.cpp 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  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. for( auto *polygon = &polygons->getArray(); polygon && polygon->set; polygon = polygon->next, num++ )
  47. {
  48. if( polygonId >= 0 && num != polygonId )
  49. continue;
  50. int anz = polygon->var.vertex->getEintragAnzahl();
  51. int ola = outList.z( num )->getEintragAnzahl();
  52. bool c = 0;
  53. int j = anz - 1;
  54. for( int k = 0; k < ola; k++ )
  55. {
  56. Punkt out = outList.z( num )->get( k );
  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. for( int i = 0; i < anz; i++ )
  63. {
  64. bool cont = 0;
  65. for( int k = 0; k < ola; k++ )
  66. {
  67. Punkt out = outList.z( num )->get( k );
  68. if( out.x < out.y && i > out.x && i < out.y )
  69. cont = 1;
  70. if( out.x > out.y && ( i > out.x || i < out.y ) )
  71. cont = 1;
  72. }
  73. if( cont )
  74. continue;
  75. Vertex a = polygon->var.vertex->get( i );
  76. Vertex b = polygon->var.vertex->get( j );
  77. if( ( ( a.y >= p.y ) != ( b.y >= p.y ) ) && ( p.x <= ( b.x - a.x ) * ( p.y - a.y ) / (float)( b.y - a.y ) + a.x ) )
  78. c = !c;
  79. j = i;
  80. }
  81. if( c )
  82. return 1;
  83. }
  84. return 0;
  85. }
  86. bool Model2DData::istLinieInnen( Vertex a, Vertex b, int polygonId ) const
  87. {
  88. if( !polygons )
  89. return 0;
  90. int pAnz = polygons->getEintragAnzahl();
  91. for( int p = 0; p < pAnz; p++ )
  92. {
  93. if( polygonId >= 0 && p != polygonId )
  94. continue;
  95. int ola = outList.z( p )->getEintragAnzahl();
  96. int anz = polygons->get( p ).vertex->getEintragAnzahl();
  97. int j = anz - 1;
  98. for( int k = 0; k < ola; k++ )
  99. {
  100. Punkt out = outList.z( p )->get( k );
  101. if( out.x < out.y && j > out.x && j < out.y )
  102. j = out.x;
  103. if( out.x > out.y && ( j > out.x || j < out.y ) )
  104. j = out.x;
  105. }
  106. for( int i = 0; i < anz; i++ )
  107. {
  108. bool cont = 0;
  109. for( int k = 0; k < ola; k++ )
  110. {
  111. Punkt out = outList.z( p )->get( k );
  112. if( out.x < out.y && i > out.x && i < out.y )
  113. cont = 1;
  114. if( out.x > out.y && ( i > out.x || i < out.y ) )
  115. cont = 1;
  116. }
  117. if( cont )
  118. continue;
  119. Punkt va = polygons->get( p ).vertex->get( i );
  120. Punkt vb = polygons->get( p ).vertex->get( j );
  121. if( (Punkt)a == va && (Punkt)b == vb )
  122. return 1;
  123. if( (Punkt)a == vb && (Punkt)b == va )
  124. return 1;
  125. j = i;
  126. }
  127. Vertex len = b - a;
  128. Vertex speed( len.x > 0 ? 1 : -1.f, len.y > 0 ? 1 : -1.f );
  129. int mLen = 0;
  130. if( fabs( len.x ) > fabs( len.y ) )
  131. {
  132. mLen = (int)fabs( len.x );
  133. speed.y = len.y / (float)fabs( len.x );
  134. }
  135. else
  136. {
  137. mLen = (int)fabs( len.y );
  138. speed.x = len.x / (float)fabs( len.y );
  139. }
  140. int i = 1;
  141. bool inside = 1;
  142. for( Vertex vp = speed + a; (Punkt)vp != (Punkt)( b - speed ) && inside && i < mLen - 1; vp += speed, i++ )
  143. inside &= istPunktInnen( vp, p );
  144. if( inside )
  145. return 1;
  146. }
  147. return 0;
  148. }
  149. // nicht constant
  150. bool Model2DData::erstelleModell( Array< Polygon2D > *polygons )
  151. {
  152. removeModell();
  153. if( !polygons || !polygons->getEintragAnzahl() )
  154. {
  155. this->polygons = polygons;
  156. vListen = new RCArray< RCArray< DreieckListe< Vertex > > >();
  157. return 1;
  158. }
  159. this->polygons = polygons;
  160. int pAnz = polygons->getEintragAnzahl();
  161. vListen = new RCArray< RCArray< DreieckListe< Vertex > > >();
  162. for( int p = 0; p < pAnz; p++ )
  163. {
  164. Polygon2D pg = polygons->get( p );
  165. if( !pg.vertex || pg.vertex->getEintragAnzahl() < 3 )
  166. continue;
  167. vListen->add( new RCArray< DreieckListe< Vertex > >() );
  168. outList.set( new Array< Punkt >, p );
  169. int vAnz = pg.vertex->getEintragAnzahl();
  170. bool textur = pg.tKordinaten != 0;
  171. for( int i = 0; i < vAnz && textur; i++ )
  172. textur &= pg.tKordinaten->hat( i );
  173. for( int i = 0; i < vAnz; i++ )
  174. {
  175. if( maxP.x < fabs( pg.vertex->get( i ).x ) )
  176. maxP.x = abs( (int)pg.vertex->get( i ).x ) + 1;
  177. if( maxP.y < fabs( pg.vertex->get( i ).y ) )
  178. maxP.y = abs( (int)pg.vertex->get( i ).y ) + 1;
  179. }
  180. minP = -maxP;
  181. if( !textur )
  182. {
  183. if( pg.tKordinaten )
  184. pg.tKordinaten->leeren();
  185. }
  186. RCArray< RCArray< DreieckListe< Vertex > > > lists;
  187. int lauf = 0;
  188. while( 1 )
  189. {
  190. lists.set( new RCArray< DreieckListe< Vertex > >(), lauf );
  191. outList.z( p )->set( Punkt( 0, 0 ), lauf );
  192. bool fertig = 0;
  193. Vertex a;
  194. Vertex b;
  195. Array< Punkt > tmpOutList;
  196. for( int i = 0; i < vAnz; i++ )
  197. {
  198. bool cont = 0;
  199. int vorher = i - 1;
  200. int nachher = i + 1;
  201. if( nachher >= vAnz )
  202. nachher = 0;
  203. if( vorher < 0 )
  204. vorher = vAnz - 1;
  205. int ola = outList.z( p )->getEintragAnzahl();
  206. for( int j = 0; j < ola; j++ )
  207. {
  208. Punkt out = outList.z( p )->get( j );
  209. if( out.x < out.y )
  210. {
  211. if( nachher > out.x && nachher < out.y )
  212. nachher = out.y;
  213. if( vorher > out.x && vorher < out.y )
  214. vorher = out.x;
  215. }
  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 && i > out.x && i < out.y )
  224. cont = 1;
  225. if( out.x > out.y && ( i > out.x || i < out.y ) )
  226. cont = 1;
  227. }
  228. if( cont )
  229. continue;
  230. if( vorher < 0 )
  231. a = pg.vertex->get( vAnz + vorher );
  232. else
  233. a = pg.vertex->get( vorher );
  234. if( nachher > vAnz - 1 )
  235. b = pg.vertex->get( nachher - vAnz + 1 );
  236. else
  237. b = pg.vertex->get( nachher );
  238. if( istLinieInnen( a, b, p ) )
  239. {
  240. DreieckListe< Vertex > *lowL = new DreieckListe< Vertex >();
  241. DreieckListe< Vertex > *heightL = new DreieckListe< Vertex >();
  242. lowL->addPunkt( new Vertex( pg.vertex->get( i ) ), textur ? new Vertex( pg.tKordinaten->get( i ) ) : 0 );
  243. heightL->addPunkt( new Vertex( pg.vertex->get( i ) ), textur ? new Vertex( pg.tKordinaten->get( i ) ) : 0 );
  244. int height = i + 1;
  245. int low = i - 1;
  246. Punkt outL( 0, 0 );
  247. Punkt outH( 0, 0 );
  248. for( int k = 0; k < 2; k++ )
  249. {
  250. bool lowp = !k;
  251. while( 1 )
  252. {
  253. if( height >= vAnz )
  254. height = 0;
  255. if( low < 0 )
  256. low = vAnz - 1;
  257. for( int j = 0; j <= lauf; j++ )
  258. {
  259. Punkt out = outList.z( p )->get( j );
  260. if( out.x < out.y )
  261. {
  262. if( height > out.x && height < out.y )
  263. height = out.y;
  264. if( low > out.x && low < out.y )
  265. low = out.x;
  266. }
  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. }
  275. Vertex a = pg.vertex->get( height );
  276. Vertex b = pg.vertex->get( low );
  277. if( low == height )
  278. {
  279. fertig = 1;
  280. outList.z( p )->set( Punkt( 0, 0 ), lauf );
  281. if( !k )
  282. lowL->addPunkt( new Vertex( b ), textur ? new Vertex( pg.tKordinaten->get( low ) ) : 0 );
  283. else
  284. heightL->addPunkt( new Vertex( b ), textur ? new Vertex( pg.tKordinaten->get( low ) ) : 0 );
  285. break;
  286. }
  287. bool inside = istLinieInnen( a, b, p );
  288. if( inside )
  289. {
  290. if( !k )
  291. outL = Punkt( low, height );
  292. else
  293. outH = Punkt( low, height );
  294. outList.z( p )->set( Punkt( low, height ), lauf );
  295. }
  296. if( lowp )
  297. {
  298. if( !k )
  299. lowL->addPunkt( new Vertex( b ), textur ? new Vertex( pg.tKordinaten->get( low ) ) : 0 );
  300. else
  301. heightL->addPunkt( new Vertex( b ), textur ? new Vertex( pg.tKordinaten->get( low ) ) : 0 );
  302. low--;
  303. }
  304. else
  305. {
  306. if( !k )
  307. lowL->addPunkt( new Vertex( a ), textur ? new Vertex( pg.tKordinaten->get( height ) ) : 0 );
  308. else
  309. heightL->addPunkt( new Vertex( a ), textur ? new Vertex( pg.tKordinaten->get( height ) ) : 0 );
  310. height++;
  311. }
  312. lowp = !lowp;
  313. if( !inside )
  314. {
  315. height = i + 1;
  316. low = i - 1;
  317. outList.z( p )->set( Punkt( 0, 0 ), lauf );
  318. break;
  319. }
  320. }
  321. if( fertig )
  322. break;
  323. }
  324. if( lowL->getDreieckAnzahl() > heightL->getDreieckAnzahl() )
  325. {
  326. lists.z( lauf )->set( lowL, i );
  327. tmpOutList.set( outL, i );
  328. heightL->release();
  329. }
  330. else
  331. {
  332. lists.z( lauf )->set( heightL, i );
  333. tmpOutList.set( outH, i );
  334. lowL->release();
  335. }
  336. }
  337. else
  338. lists.z( lauf )->set( new DreieckListe< Vertex >(), i );
  339. if( fertig )
  340. break;
  341. }
  342. int maxP = -1;
  343. int max = 0;
  344. for( int i = 0; i < vAnz; i++ )
  345. {
  346. if( lists.z( lauf )->z( i ) && lists.z( lauf )->z( i )->getDreieckAnzahl() > max )
  347. {
  348. max = lists.z( lauf )->z( i )->getDreieckAnzahl();
  349. maxP = i;
  350. }
  351. }
  352. if( !max || maxP < 0 )
  353. break;
  354. vListen->z( p )->add( lists.z( lauf )->get( maxP ) );
  355. outList.z( p )->set( tmpOutList.get( maxP ), lauf );
  356. if( fertig )
  357. break;
  358. lauf++;
  359. }
  360. outList.z( p )->leeren();
  361. }
  362. return 1;
  363. }
  364. void Model2DData::removeModell() // setzt die Vertex daten zurück
  365. {
  366. if( polygons )
  367. {
  368. int anz = polygons->getEintragAnzahl();
  369. for( int i = 0; i < anz; i++ )
  370. {
  371. if( polygons->get( i ).name )
  372. polygons->get( i ).name->release();
  373. if( polygons->get( i ).tKordinaten )
  374. polygons->get( i ).tKordinaten->release();
  375. if( polygons->get( i ).vertex )
  376. polygons->get( i ).vertex->release();
  377. if( polygons->get( i ).schwerpunkt )
  378. delete polygons->get( i ).schwerpunkt;
  379. }
  380. polygons = polygons->release();
  381. }
  382. if( vListen )
  383. vListen = vListen->release();
  384. outList.leeren();
  385. minP = Punkt( 0, 0 );
  386. maxP = Punkt( 0, 0 );
  387. }
  388. bool Model2DData::calcHitPoint( Vertex pos, Vertex dir, char *polygonName, Vertex &hitpoint, Vertex &moveSpeed, float &rotSpeed ) const
  389. {
  390. bool ret = 0;
  391. for( auto *polygon = &polygons->getArray(); polygon && polygon->set; polygon = polygon->next )
  392. {
  393. if( polygon->var.name->istGleich( polygonName ) )
  394. {
  395. int anz = polygon->var.vertex->getEintragAnzahl();
  396. for( int i = 0; i < anz; i++ )
  397. {
  398. Vertex a = polygon->var.vertex->get( i );
  399. Vertex b = polygon->var.vertex->get( ( i + 1 ) % anz );
  400. b -= a;
  401. float offset = 0;
  402. if( dir.y != 0 && dir.x != 0 )
  403. offset = ( ( a.y - pos.y ) / dir.y - ( a.x - pos.x ) / dir.x ) / ( b.x / dir.x - b.y / dir.y ); // solve hitpoint equasion
  404. else if( dir.y == 0 )
  405. offset = ( pos.y - a.y ) / b.y;
  406. else if( dir.x == 0 )
  407. offset = ( pos.x - a.x ) / b.x;
  408. Vertex point = a + ( b * offset );
  409. if( offset >= 0 && offset <= 1 )
  410. {
  411. if( !ret || ( hitpoint - pos ).getLengthSq() > ( point - pos ).getLengthSq() )
  412. {
  413. Vertex normal = b.CW90().normalize();
  414. Vertex kNorm = Vertex( dir ).normalize();
  415. moveSpeed = normal * ( normal * kNorm ) * dir.getLengthSq();
  416. normal = ( point - *polygon->var.schwerpunkt ).CW90().normalize();
  417. Vertex rotKraft = normal * ( normal * kNorm ) * dir.getLength();
  418. rotSpeed = ( ( rotKraft.getLength() * ( point - *polygon->var.schwerpunkt ).getLength() ) / 180.f ) * 3.14f;
  419. if( point.x >= polygon->var.schwerpunkt->x )
  420. {
  421. if( point.y >= polygon->var.schwerpunkt->y && rotKraft.x > 0 && rotKraft.y < 0 )
  422. rotSpeed = -rotSpeed;
  423. if( point.y < polygon->var.schwerpunkt->y && rotKraft.x < 0 && rotKraft.y < 0 )
  424. rotSpeed = -rotSpeed;
  425. }
  426. else
  427. {
  428. if( point.y >= polygon->var.schwerpunkt->y && rotKraft.x > 0 && rotKraft.y > 0 )
  429. rotSpeed = -rotSpeed;
  430. if( point.y < polygon->var.schwerpunkt->y && rotKraft.x < 0 && rotKraft.y > 0 )
  431. rotSpeed = -rotSpeed;
  432. }
  433. hitpoint = point;
  434. }
  435. ret = 1;
  436. }
  437. }
  438. }
  439. }
  440. return ret;
  441. }
  442. bool Model2DData::split( Vertex pos, Vertex dir, char *polygonName, Polygon2D &partA, Polygon2D &partB, Punkt &posA, Punkt &posB, std::function< double() > random ) const
  443. {
  444. Vertex originalDir = dir;
  445. bool ret = 0;
  446. int num = 0;
  447. for( auto *polygon = &polygons->getArray(); polygon && polygon->set; polygon = polygon->next, num++ )
  448. {
  449. if( polygon->var.name->istGleich( polygonName ) )
  450. {
  451. while( istPunktInnen( pos, num ) )
  452. {
  453. pos -= dir;
  454. }
  455. int anz = polygon->var.vertex->getEintragAnzahl();
  456. Vertex startPoint;
  457. Vertex texturSP;
  458. int leftI = 0;
  459. int rightI = 0;
  460. Vertex txtChpPix( 0, 0 );
  461. for( int i = 0; i < anz; i++ )
  462. {
  463. Vertex a = polygon->var.vertex->get( i );
  464. Vertex b = polygon->var.vertex->get( ( i + 1 ) % anz );
  465. b -= a;
  466. if( ( txtChpPix.x == 0 || txtChpPix.y == 0 ) && b.x != 0 && b.y != 0 )
  467. {
  468. Vertex ta = polygon->var.tKordinaten->get( i );
  469. Vertex tb = polygon->var.tKordinaten->get( ( i + 1 ) % anz );
  470. tb -= ta;
  471. txtChpPix = Vertex( tb.x / b.x, tb.y / b.y );
  472. }
  473. float offset = 0;
  474. if( dir.y != 0 && dir.x != 0 )
  475. offset = ( ( a.y - pos.y ) / dir.y - ( a.x - pos.x ) / dir.x ) / ( b.x / dir.x - b.y / dir.y ); // solve hitpoint equasion
  476. else if( dir.y == 0 )
  477. offset = ( pos.y - a.y ) / b.y;
  478. else if( dir.x == 0 )
  479. offset = ( pos.x - a.x ) / b.x;
  480. Vertex point = a + ( b * offset );
  481. if( offset >= 0 && offset <= 1 )
  482. {
  483. if( !ret || ( startPoint - pos ).getLengthSq() > ( point - pos ).getLengthSq() )
  484. {
  485. leftI = i;
  486. rightI = ( i + 1 ) % anz;
  487. startPoint = point;
  488. texturSP = polygon->var.tKordinaten->get( i ) + ( polygon->var.tKordinaten->get( ( i + 1 ) % anz ) - polygon->var.tKordinaten->get( i ) ) * offset;
  489. }
  490. ret = 1;
  491. }
  492. }
  493. if( ret )
  494. {
  495. partA.transparent = polygon->var.transparent;
  496. partA.schwerpunkt = new Vertex( 0, 0 );
  497. partA.tKordinaten = new Array< Vertex >();
  498. partA.name = new Text( polygon->var.name->getText() );
  499. partA.vertex = new Array< Vertex >();
  500. partB.transparent = polygon->var.transparent;
  501. partB.schwerpunkt = new Vertex( 0, 0 );
  502. partB.tKordinaten = new Array< Vertex >();
  503. partB.name = new Text( polygon->var.name->getText() );
  504. partB.vertex = new Array< Vertex >();
  505. *partA.schwerpunkt += startPoint;
  506. *partB.schwerpunkt += startPoint;
  507. partA.vertex->add( startPoint );
  508. partB.vertex->add( startPoint );
  509. partA.tKordinaten->add( texturSP );
  510. partB.tKordinaten->add( texturSP );
  511. int leftIE = 0;
  512. int rightIE = 0;
  513. while( 1 )
  514. {
  515. pos = startPoint;
  516. Vertex next = startPoint + dir;
  517. Vertex nextT = texturSP + Vertex( dir.x * txtChpPix.x, dir.y * txtChpPix.y );
  518. ret = 0;
  519. bool needOne = !istPunktInnen( next );
  520. int bestI = -1;
  521. float bo1 = 1000;
  522. float bo2 = 1000;
  523. for( int i = 0; i < anz; i++ )
  524. {
  525. if( i == leftI )
  526. continue;
  527. Vertex a = polygon->var.vertex->get( i );
  528. Vertex b = polygon->var.vertex->get( ( i + 1 ) % anz );
  529. b -= a;
  530. float offset1 = 0;
  531. if( dir.y != 0 && dir.x != 0 )
  532. offset1 = ( ( a.y - pos.y ) / dir.y - ( a.x - pos.x ) / dir.x ) / ( b.x / dir.x - b.y / dir.y ); // solve hitpoint equasion
  533. else if( dir.y == 0 )
  534. offset1 = ( pos.y - a.y ) / b.y;
  535. else if( dir.x == 0 )
  536. offset1 = ( pos.x - a.x ) / b.x;
  537. Vertex point = a + ( b * offset1 );
  538. float offset2 = 0;
  539. if( dir.x != 0 )
  540. offset2 = ( point.x - pos.x ) / dir.x;
  541. else
  542. offset2 = ( point.y - pos.y ) / dir.y;
  543. 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 ) ) )
  544. {
  545. bo1 = offset1;
  546. bo2 = offset2;
  547. bestI = i;
  548. }
  549. if( offset1 >= 0 && offset1 <= 1 && offset2 >= 0 && offset2 <= 1 )
  550. {
  551. if( !ret || ( startPoint - pos ).getLengthSq() > ( point - pos ).getLengthSq() )
  552. {
  553. leftIE = i;
  554. rightIE = ( i + 1 ) % anz;
  555. startPoint = point;
  556. texturSP = polygon->var.tKordinaten->get( i ) + ( polygon->var.tKordinaten->get( ( i + 1 ) % anz ) - polygon->var.tKordinaten->get( i ) ) * offset1;
  557. }
  558. ret = 1;
  559. }
  560. }
  561. if( needOne && !ret )
  562. {
  563. Vertex a = polygon->var.vertex->get( bestI );
  564. Vertex b = polygon->var.vertex->get( ( bestI + 1 ) % anz );
  565. b -= a;
  566. leftIE = bestI;
  567. rightIE = ( bestI + 1 ) % anz;
  568. startPoint = a + ( b * bo1 );
  569. texturSP = polygon->var.tKordinaten->get( bestI ) + ( polygon->var.tKordinaten->get( ( bestI + 1 ) % anz ) - polygon->var.tKordinaten->get( bestI ) ) * bo1;
  570. ret = 1;
  571. }
  572. if( ret )
  573. break;
  574. *partA.schwerpunkt += next;
  575. *partB.schwerpunkt += next;
  576. partA.vertex->add( next );
  577. partB.vertex->add( next );
  578. partA.tKordinaten->add( nextT );
  579. partB.tKordinaten->add( nextT );
  580. startPoint = next;
  581. texturSP = nextT;
  582. dir = originalDir.rotation( (float)(random() - 0.5) );
  583. }
  584. *partA.schwerpunkt += startPoint;
  585. *partB.schwerpunkt += startPoint;
  586. partA.vertex->add( startPoint );
  587. partB.vertex->add( startPoint );
  588. partA.tKordinaten->add( texturSP );
  589. partB.tKordinaten->add( texturSP );
  590. for( int i = rightIE; i != leftI; i++ )
  591. {
  592. i = i % anz;
  593. if( i == leftI )
  594. break;
  595. *partA.schwerpunkt += polygon->var.vertex->get( i );
  596. partA.vertex->add( polygon->var.vertex->get( i ) );
  597. partA.tKordinaten->add( polygon->var.tKordinaten->get( i ) );
  598. }
  599. *partA.schwerpunkt += polygon->var.vertex->get( leftI );
  600. partA.vertex->add( polygon->var.vertex->get( leftI ) );
  601. partA.tKordinaten->add( polygon->var.tKordinaten->get( leftI ) );
  602. for( int i = leftIE; i != rightI; i-- )
  603. {
  604. if( i < 0 )
  605. i += anz;
  606. if( i == rightI )
  607. break;
  608. *partB.schwerpunkt += polygon->var.vertex->get( i );
  609. partB.vertex->add( polygon->var.vertex->get( i ) );
  610. partB.tKordinaten->add( polygon->var.tKordinaten->get( i ) );
  611. }
  612. *partB.schwerpunkt += polygon->var.vertex->get( rightI );
  613. partB.vertex->add( polygon->var.vertex->get( rightI ) );
  614. partB.tKordinaten->add( polygon->var.tKordinaten->get( rightI ) );
  615. *partA.schwerpunkt /= (float)partA.vertex->getEintragAnzahl();
  616. *partB.schwerpunkt /= (float)partB.vertex->getEintragAnzahl();
  617. posA = (Punkt)*partA.schwerpunkt;
  618. posB = (Punkt)*partB.schwerpunkt;
  619. for( int i = 0; i < partA.vertex->getEintragAnzahl(); i++ )
  620. partA.vertex->set( partA.vertex->get( i ) - *partA.schwerpunkt, i );
  621. for( int i = 0; i < partB.vertex->getEintragAnzahl(); i++ )
  622. partB.vertex->set( partB.vertex->get( i ) - *partB.schwerpunkt, i );
  623. *partA.schwerpunkt = Vertex( 0, 0 );
  624. *partB.schwerpunkt = Vertex( 0, 0 );
  625. }
  626. }
  627. }
  628. return ret;
  629. }
  630. // Reference Counting
  631. Model2DData *Model2DData::getThis()
  632. {
  633. ref++;
  634. return this;
  635. }
  636. Model2DData *Model2DData::release()
  637. {
  638. ref--;
  639. if( !ref )
  640. delete this;
  641. return 0;
  642. }
  643. // Inhalt der Model2D Klasse aus Model2D.h
  644. // Konstruktor
  645. Model2D::Model2D()
  646. : Zeichnung()
  647. {
  648. farbe = 0;
  649. style = 0;
  650. rData = 0;
  651. drehung = 0;
  652. size = 1;
  653. textur = new RCArray< Textur2D >;
  654. ref = 1;
  655. }
  656. // Destruktor
  657. Model2D::~Model2D()
  658. {
  659. if( rData )
  660. rData->release();
  661. textur->release();
  662. }
  663. // nicht constant
  664. void Model2D::setModel( Model2DData *mdl )
  665. {
  666. if( rData )
  667. rData->release();
  668. rData = mdl;
  669. }
  670. void Model2D::setDrehung( float drehung )
  671. {
  672. this->drehung = drehung;
  673. while( this->drehung > PI * 2 )
  674. this->drehung -= (float)PI * 2;
  675. while( this->drehung < 0 )
  676. this->drehung += (float)PI * 2;
  677. rend = 1;
  678. }
  679. void Model2D::addDrehung( float drehung )
  680. {
  681. this->drehung += drehung;
  682. while( this->drehung > PI * 2 )
  683. this->drehung -= (float)PI * 2;
  684. while( this->drehung < 0 )
  685. this->drehung += (float)PI * 2;
  686. rend = 1;
  687. }
  688. void Model2D::setSize( float size )
  689. {
  690. this->size = size;
  691. rend = 1;
  692. }
  693. void Model2D::addSize( float size )
  694. {
  695. this->size += size;
  696. rend = 1;
  697. }
  698. void Model2D::setTextur( Textur2D *t )
  699. {
  700. int index = 0;
  701. if( rData )
  702. {
  703. for( auto i = rData->polygons->getArray(); i.set; i++ )
  704. textur->set( t->getThis(), index++ );
  705. }
  706. t->release();
  707. }
  708. void Model2D::setTextur( Textur2D *t, const char *polygonName )
  709. {
  710. int index = 0;
  711. for( auto i = rData->polygons->getArray(); i.set; i++, index++ )
  712. {
  713. if( i.var.name->istGleich( polygonName ) )
  714. textur->set( t->getThis(), index );
  715. }
  716. t->release();
  717. }
  718. void Model2D::setFarbe( int f )
  719. {
  720. farbe = f;
  721. rend = 1;
  722. }
  723. void Model2D::doMausEreignis( MausEreignis &me )
  724. {
  725. if( !mak || me.verarbeitet || !istPunktInnen( Punkt( me.mx, me.my ) ) )
  726. return;
  727. me.mx -= pos.x;
  728. me.my -= pos.y;
  729. mak( makParam, this, me );
  730. me.mx += pos.x;
  731. me.my += pos.y;
  732. me.verarbeitet = 1;
  733. }
  734. bool Model2D::tick( double tickVal )
  735. {
  736. bool ret = rend;
  737. rend = 0;
  738. return ret;
  739. }
  740. void Model2D::render( Bild &zRObj )
  741. {
  742. if( !rData || hatStyleNicht( Model2D::Style::Sichtbar ) || !rData->polygons )
  743. return;
  744. Zeichnung::render( zRObj );
  745. int num = 0;
  746. for( auto *p = &rData->vListen->getArray(); p && p->set; p = p->next, num++ )
  747. {
  748. Mat3< float > mat = Mat3< float >::translation( pos ) * Mat3< float >::rotation( drehung ) * Mat3< float >::scaling( size );
  749. if( hatStyle( Model2D::Style::Textur ) )
  750. {
  751. if( !textur || !textur->z( num ) || !textur->z( num )->zTextur() || !rData->polygons->get( num ).tKordinaten )
  752. {
  753. for( auto *i = &p->var->getArray(); i && i->set; i = i->next )
  754. {
  755. for( auto *j = &i->var->zListe()->getArray(); j->next->next && j->next->next->set; j = j->next )
  756. {
  757. Vertex a = mat * *j->var->punkt;
  758. Vertex b = mat * *j->next->var->punkt;
  759. Vertex c = mat * *j->next->next->var->punkt;
  760. if( hatStyle( Model2D::Style::Alpha ) )
  761. zRObj.drawDreieckAlpha( a, b, c, farbe );
  762. else
  763. zRObj.drawDreieck( a, b, c, farbe );
  764. }
  765. }
  766. }
  767. else
  768. {
  769. Bild *txt = textur->z( num )->zTextur();
  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. Punkt ta = (Punkt)Vertex( j->var->textur->x * (float)txt->getBreite(), j->var->textur->y * (float)txt->getHeight() );
  778. Punkt tb = (Punkt)Vertex( j->next->var->textur->x * (float)txt->getBreite(), j->next->var->textur->y * (float)txt->getHeight() );
  779. Punkt tc = (Punkt)Vertex( j->next->next->var->textur->x * (float)txt->getBreite(), j->next->next->var->textur->y * (float)txt->getHeight() );
  780. if( hatStyle( Model2D::Style::Alpha ) )
  781. zRObj.drawDreieckTexturAlpha( a, b, c, ta, tb, tc, *txt );
  782. else
  783. zRObj.drawDreieckTextur( a, b, c, ta, tb, tc, *txt );
  784. }
  785. }
  786. }
  787. }
  788. if( hatStyle( Model2D::Style::Mesh ) )
  789. {
  790. for( auto *i = &p->var->getArray(); i && i->set; i = i->next )
  791. {
  792. for( auto *j = &i->var->zListe()->getArray(); j->next->next && j->next->next->set; j = j->next )
  793. {
  794. Vertex a = mat * *j->var->punkt;
  795. Vertex b = mat * *j->next->var->punkt;
  796. Vertex c = mat * *j->next->next->var->punkt;
  797. if( hatStyle( Model2D::Style::Alpha ) )
  798. {
  799. zRObj.drawLinieAlpha( a, b, farbe );
  800. zRObj.drawLinieAlpha( b, c, farbe );
  801. zRObj.drawLinieAlpha( c, a, farbe );
  802. }
  803. else
  804. {
  805. zRObj.drawLinie( a, b, farbe );
  806. zRObj.drawLinie( b, c, farbe );
  807. zRObj.drawLinie( c, a, farbe );
  808. }
  809. }
  810. }
  811. }
  812. if( hatStyle( Model2D::Style::Rahmen ) )
  813. {
  814. ArrayEintrag< Vertex > &beg = rData->polygons->get( num ).vertex->getArray();
  815. if( beg.set )
  816. {
  817. ArrayEintrag< Vertex > *letzter = 0;
  818. for( ArrayEintrag< Vertex > *e = &beg; e && e->next && e->set && e->next->set; e = e->next )
  819. {
  820. if( hatStyle( Model2D::Style::Alpha ) )
  821. zRObj.drawLinieAlpha( mat * e->var, mat * e->next->var, farbe );
  822. else
  823. zRObj.drawLinie( mat * e->var, mat * e->next->var, farbe );
  824. letzter = e->next;
  825. }
  826. if( letzter && letzter->set )
  827. {
  828. if( hatStyle( Model2D::Style::Alpha ) )
  829. zRObj.drawLinieAlpha( mat * letzter->var, mat * beg.var, farbe );
  830. else
  831. zRObj.drawLinie( mat * letzter->var, mat * beg.var, farbe );
  832. }
  833. }
  834. }
  835. }
  836. }
  837. // constant
  838. float Model2D::getDrehung() const
  839. {
  840. return drehung;
  841. }
  842. float Model2D::getSize() const
  843. {
  844. return size;
  845. }
  846. bool Model2D::istPunktInnen( Vertex p ) const
  847. {
  848. if( !rData )
  849. return 0;
  850. p -= pos;
  851. if( p < Mat3< float >::scaling( size ) * rData->minP || p > Mat3< float >::scaling( size ) * rData->maxP || !rData->polygons )
  852. return 0;
  853. int num = 0;
  854. for( auto *polygon = &rData->polygons->getArray(); polygon && polygon->set; polygon = polygon->next, num++ )
  855. {
  856. if( polygon->var.transparent )
  857. continue;
  858. Mat3< float > mat = Mat3< float >::rotation( drehung ) * Mat3< float >::scaling( size );
  859. int anz = polygon->var.vertex->getEintragAnzahl();
  860. bool c = 0;
  861. int j = anz - 1;
  862. for( int i = 0; i < anz; i++ )
  863. {
  864. Vertex a = mat * polygon->var.vertex->get( i );
  865. Vertex b = mat * polygon->var.vertex->get( j );
  866. 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 ) )
  867. c = !c;
  868. j = i;
  869. }
  870. if( c )
  871. return 1;
  872. }
  873. return 0;
  874. }
  875. bool Model2D::istLinieInnen( Vertex a, Vertex b ) const
  876. {
  877. if( !rData || !rData->polygons )
  878. return 0;
  879. int pAnz = rData->polygons->getEintragAnzahl();
  880. for( int p = 0; p < pAnz; p++ )
  881. {
  882. if( rData->polygons->get( p ).transparent )
  883. continue;
  884. Mat3< float > mat = Mat3< float >::rotation( drehung ) * Mat3< float >::scaling( size );
  885. int anz = rData->polygons->get( p ).vertex->getEintragAnzahl();
  886. int j = anz - 1;
  887. for( int i = 0; i < anz; i++ )
  888. {
  889. Punkt va = mat * rData->polygons->get( p ).vertex->get( i );
  890. Punkt vb = mat * rData->polygons->get( p ).vertex->get( j );
  891. if( (Punkt)a == pos + va && (Punkt)b == pos + vb )
  892. return 1;
  893. if( (Punkt)a == pos + vb && (Punkt)b == pos + va )
  894. return 1;
  895. j = i;
  896. }
  897. Vertex len = b - a;
  898. Vertex speed( len.x > 0 ? 1 : -1.f, len.y > 0 ? 1 : -1.f );
  899. int mLen = 0;
  900. if( fabs( len.x ) > fabs( len.y ) )
  901. {
  902. mLen = (int)fabs( len.x );
  903. speed.y = len.y / (float)fabs( len.x );
  904. }
  905. else
  906. {
  907. mLen = (int)fabs( len.y );
  908. speed.x = len.x / (float)fabs( len.y );
  909. }
  910. int i = 1;
  911. bool inside = 1;
  912. for( Vertex vp = speed + a; (Punkt)vp != (Punkt)( b - speed ) && inside && i < mLen - 1; vp += speed, i++ )
  913. inside &= istPunktInnen( vp );
  914. if( inside )
  915. return 1;
  916. }
  917. return 0;
  918. }
  919. bool Model2D::istModelInnen( const Model2D *zMdl, bool end ) const
  920. {
  921. if( !end )
  922. {
  923. Vertex min = (Vertex)rData->minP * size + pos;
  924. Vertex max = (Vertex)rData->maxP * size + pos;
  925. Vertex min2 = (Vertex)zMdl->zModel()->minP * zMdl->getSize() + zMdl->getPosition();
  926. Vertex max2 = (Vertex)zMdl->zModel()->maxP * zMdl->getSize() + zMdl->getPosition();
  927. if( max.x < min2.x || min.x > max2.x || max.y < min2.y || min.y > max2.y )
  928. return 0;
  929. }
  930. Mat3< float > mat = Mat3< float >::translation( pos ) * Mat3< float >::rotation( drehung ) * Mat3< float >::scaling( size );
  931. for( auto *polygon = &rData->polygons->getArray(); polygon && polygon->set; polygon = polygon->next )
  932. {
  933. if( polygon->var.transparent )
  934. continue;
  935. int anz = polygon->var.vertex->getEintragAnzahl();
  936. for( int i = 0; i < anz; i++ )
  937. {
  938. if( zMdl->istPunktInnen( mat * polygon->var.vertex->get( i ) ) )
  939. return 1;
  940. }
  941. }
  942. if( end )
  943. return 0;
  944. return zMdl->istModelInnen( this, 1 );
  945. }
  946. Model2DData *Model2D::getModel() const
  947. {
  948. return rData ? rData->getThis() : 0;
  949. }
  950. Model2DData *Model2D::zModel() const
  951. {
  952. return rData;
  953. }
  954. // Reference Counting
  955. Model2D *Model2D::getThis()
  956. {
  957. ++ref;
  958. return this;
  959. }
  960. Model2D *Model2D::release()
  961. {
  962. --ref;
  963. if( !ref )
  964. delete this;
  965. return 0;
  966. }