You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

901 lines
19 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "mask.h"
  2. #define LOG(X) m_pLog->Log("ShadowMask", this, X)
  3. #define LOG_ERROR(X) m_pLog->Log("ShadowMask", this, _LOG_ERROR, X)
  4. ShadowMask::ShadowMask()
  5. {
  6. m_pLog = basicLogCollection::getInstance();
  7. m_bActive = false;
  8. #if defined(__linux__) && ! defined(__GUI__)
  9. m_pVertexFileName = (CSTR) malloc(256);
  10. m_pRasterFileName = (CSTR) malloc(256);
  11. sprintf(m_pVertexFileName, "masking.dat");
  12. sprintf(m_pRasterFileName, "shadow.dat");
  13. m_bAutoSave = true;
  14. #else
  15. m_bAutoSave = false;
  16. #endif
  17. }
  18. ShadowMask::~ShadowMask()
  19. {
  20. if (m_vVectorMask.size())
  21. {
  22. if (m_bAutoSave)
  23. {
  24. saveVertexes(m_pVertexFileName);
  25. }
  26. #if defined(__linux__) && ! defined(__GUI__)
  27. #else
  28. if (! m_bRasterized)
  29. {
  30. if (rasterizeVertexes())
  31. {
  32. // Save rasterized
  33. // in Linux Section
  34. };
  35. };
  36. #endif
  37. for (vectorMasking::iterator index = m_vVectorMask.begin(); index < m_vVectorMask.end(); index++)
  38. {
  39. free((*index)->points);
  40. free(*index);
  41. };
  42. m_vVectorMask.clear();
  43. };
  44. #if defined(__linux__) && ! defined(__GUI__)
  45. if (m_pVertexFileName)
  46. {
  47. free(m_pVertexFileName);
  48. }
  49. #endif
  50. if (m_vZoneMask.size())
  51. {
  52. if (m_bAutoSave)
  53. {
  54. saveRastered(m_pRasterFileName);
  55. }
  56. #if defined(__linux__) && ! defined(__GUI__)
  57. #endif
  58. // Clear Data
  59. for (vectorZoneMask::iterator index = m_vZoneMask.begin(); index < m_vZoneMask.end(); index++)
  60. {
  61. free((*index)->zones);
  62. free(*index);
  63. };
  64. m_vZoneMask.clear();
  65. };
  66. #if defined(__linux__) && ! defined(__GUI__)
  67. if (m_pRasterFileName)
  68. {
  69. free(m_pRasterFileName);
  70. }
  71. #endif
  72. }
  73. #if defined(__linux__) && ! defined(__GUI__)
  74. bool ShadowMask::loadRastered(CSTR fName)
  75. {
  76. FILE * fd;
  77. if (fName != NULL)
  78. {
  79. fd = fopen(fName, "rb");
  80. }
  81. else
  82. {
  83. fd = fopen(m_pRasterFileName, "rb");
  84. };
  85. if (fd != NULL)
  86. {
  87. char tBuffer[256];
  88. bool InRay;
  89. rawRay * tRay;
  90. int tColumnID, tCount, tStart, tStop, tIndex;
  91. m_bActive = false;
  92. if (m_vZoneMask.size())
  93. {
  94. // Clear Data
  95. LOG("Clear Data");
  96. for (vectorZoneMask::iterator index = m_vZoneMask.begin(); index < m_vZoneMask.end(); index++)
  97. {
  98. free((*index)->zones);
  99. free(*index);
  100. };
  101. m_vZoneMask.clear();
  102. LOG("Done");
  103. };
  104. fscanf(fd, "%s\n", tBuffer);
  105. InRay = false;
  106. tIndex = 0;
  107. while (! feof(fd))
  108. {
  109. if (InRay)
  110. {
  111. fscanf(fd, "<%d:%d>\n", &tStart, &tStop);
  112. tRay->zones[tIndex].start = tStart;
  113. tRay->zones[tIndex].stop = tStop;
  114. tIndex++;
  115. if (tIndex == tCount)
  116. {
  117. m_vZoneMask.push_back(tRay);
  118. InRay = false;
  119. };
  120. }
  121. else
  122. {
  123. fscanf(fd, "[%d]:[%d]\n", &tColumnID, &tCount);
  124. if (tCount > 0)
  125. {
  126. tRay = (rawRay *) malloc(sizeof(rawRay));
  127. if (tRay != NULL)
  128. {
  129. tRay->zones = (raySegment *) malloc(sizeof(raySegment) * tCount);
  130. if (tRay->zones != NULL)
  131. {
  132. tRay->column = tColumnID;
  133. tRay->count = tCount;
  134. tIndex = 0;
  135. InRay = true;
  136. }
  137. else
  138. {
  139. free(tRay);
  140. // Handle Memory Alloc Error
  141. };
  142. }
  143. else
  144. {
  145. // Handle Memory Alloc Error
  146. };
  147. };
  148. };
  149. };
  150. fclose(fd);
  151. m_bActive = strncmp(tBuffer, "ENA", 3) == 0;
  152. m_bRasterized = true;
  153. LOG("Loading Done");
  154. return true;
  155. }
  156. else
  157. {
  158. LOG_ERROR("Can not open Data File!");
  159. };
  160. return false;
  161. }
  162. bool ShadowMask::saveRastered(CSTR fName)
  163. {
  164. FILE * fd;
  165. if (m_vZoneMask.size() == 0)
  166. {
  167. return false;
  168. }
  169. if (fName != NULL)
  170. {
  171. strncpy(m_pRasterFileName, fName, 256);
  172. };
  173. fd = fopen(m_pRasterFileName, "wb+");
  174. if (fd != NULL)
  175. {
  176. if (m_bActive)
  177. {
  178. fprintf(fd, "ENABLED\n");
  179. }
  180. else
  181. {
  182. fprintf(fd, "DISABLED\n");
  183. }
  184. for (vectorZoneMask::iterator index = m_vZoneMask.begin(); index < m_vZoneMask.end(); index++)
  185. {
  186. fprintf(fd, "[%d]:[%d]\n", (*index)->column, (*index)->count);
  187. for (int j = 0; j < (*index)->count; j++)
  188. {
  189. fprintf(fd, "<%d:%d>\n", (*index)->zones[j].start, (*index)->zones[j].stop);
  190. };
  191. };
  192. fclose(fd);
  193. m_bRasterized = true;
  194. return true;
  195. };
  196. return false;
  197. }
  198. #endif
  199. bool ShadowMask::isOutside(uint16_t x, uint16_t y)
  200. {
  201. if (! m_bActive)
  202. {
  203. return true;
  204. }
  205. for (vectorZoneMask::iterator index = m_vZoneMask.begin(); index < m_vZoneMask.end(); index++)
  206. {
  207. if ((*index)->column == x)
  208. {
  209. for (int j = 0; j < (*index)->count; j++)
  210. {
  211. if ((y >= (*index)->zones[j].start) && (y <= (*index)->zones[j].stop))
  212. {
  213. return false;
  214. }
  215. };
  216. break;
  217. };
  218. };
  219. return true;
  220. }
  221. bool ShadowMask::isOutside(simplePoint A)
  222. {
  223. if (! m_bActive)
  224. {
  225. return true;
  226. }
  227. return isOutside(A.x(), A.y());
  228. }
  229. void ShadowMask::setActive(bool tVal)
  230. {
  231. m_bActive = tVal;
  232. }
  233. bool ShadowMask::addRay(uint16_t column, raySegment RS)
  234. {
  235. for (vectorZoneMask::iterator index = m_vZoneMask.begin(); index < m_vZoneMask.end(); index++)
  236. {
  237. if ((*index)->column == column)
  238. {
  239. (*index)->zones = (raySegment *) realloc((*index)->zones, sizeof(raySegment) * ((*index)->count + 1));
  240. (*index)->zones[(*index)->count].start = RS.start;
  241. (*index)->zones[(*index)->count].stop = RS.stop;
  242. (*index)->count++;
  243. return true;
  244. };
  245. };
  246. // not found column
  247. rawRay * tRay;
  248. tRay = (rawRay *) malloc(sizeof(rawRay));
  249. if (tRay != NULL)
  250. {
  251. tRay->zones = (raySegment *) malloc(sizeof(raySegment));
  252. if (tRay->zones != NULL)
  253. {
  254. tRay->column = column;
  255. tRay->count = 1;
  256. tRay->zones[0].start = RS.start;
  257. tRay->zones[0].stop = RS.stop;
  258. m_vZoneMask.push_back(tRay);
  259. return true;
  260. }
  261. else
  262. {
  263. free(tRay);
  264. // Handle Memory Alloc Error
  265. };
  266. };
  267. return false;
  268. }
  269. bool ShadowMask::addRay(uint16_t column, uint32_t RS)
  270. { // Binary Data to RaySegment
  271. raySegment segR;
  272. segR.start = (RS >> 16) & 0xFFFF;
  273. segR.stop = RS & 0xFFFF;
  274. return addRay(column, segR);
  275. }
  276. bool ShadowMask::addPoly(simplePolygone * SP)
  277. {
  278. LOG("Add Poly");
  279. m_vVectorMask.push_back(SP);
  280. LOG("Add Poly Done");
  281. return true;
  282. }
  283. simplePolygone * ShadowMask::getPoly(int id)
  284. {
  285. if (id >= (int)m_vVectorMask.size())
  286. {
  287. return NULL;
  288. }
  289. return m_vVectorMask.at(id);
  290. }
  291. bool ShadowMask::clear(void)
  292. {
  293. simplePolygone * tPoly;
  294. rawRay * tRay;
  295. for (vectorMasking::iterator index = m_vVectorMask.begin(); index < m_vVectorMask.end(); index++)
  296. {
  297. tPoly = *index;
  298. delete(tPoly->points);
  299. delete(tPoly);
  300. };
  301. m_vVectorMask.clear();
  302. if (m_bAutoSave)
  303. {
  304. unlink(m_pVertexFileName);
  305. }
  306. #ifdef __linux__
  307. #endif
  308. for (vectorZoneMask::iterator index = m_vZoneMask.begin(); index < m_vZoneMask.end(); index++)
  309. {
  310. tRay = *index;
  311. delete(tRay->zones);
  312. delete(tRay);
  313. };
  314. m_vZoneMask.clear();
  315. if (m_bAutoSave)
  316. {
  317. unlink(m_pRasterFileName);
  318. }
  319. #ifdef __linux__
  320. #endif
  321. m_bActive = false;
  322. return true;
  323. }
  324. bool ShadowMask::deletePoly(int ID)
  325. {
  326. if ( ID > (int)m_vVectorMask.size() )
  327. {
  328. return false;
  329. }
  330. delete(m_vVectorMask.at(ID)->points);
  331. delete(m_vVectorMask.at(ID));
  332. m_vVectorMask.erase(m_vVectorMask.begin() + ID);
  333. #if defined(WIN32) || defined(__GUI__)
  334. m_vVectorMask.shrink_to_fit();
  335. #endif
  336. return true;
  337. }
  338. #if defined(__linux__) && ! defined(__GUI__)
  339. bool ShadowMask::saveVertexes(CSTR fName)
  340. {
  341. if (m_vVectorMask.size() == 0)
  342. {
  343. return true;
  344. }
  345. FILE * fd;
  346. if (fName != NULL)
  347. {
  348. strncpy(m_pVertexFileName, fName, 256);
  349. };
  350. fd = fopen(m_pVertexFileName, "wb+");
  351. if (fd == NULL)
  352. {
  353. return false;
  354. }
  355. for (vectorMasking::iterator index = m_vVectorMask.begin(); index < m_vVectorMask.end(); index++)
  356. {
  357. LOG("VERTEX->SAVE");
  358. fprintf(fd, "%d\n", (*index)->count);
  359. fprintf(fd, "%d %d %d\n", (*index)->color[0], (*index)->color[1], (*index)->color[2]);
  360. for (int j = 0; j < (*index)->count; j++)
  361. {
  362. int cX, cY;
  363. cX = (*index)->points[j].x();
  364. cY = (*index)->points[j].y();
  365. fprintf(fd, "%d %d\n", cX, cY);
  366. };
  367. };
  368. fclose(fd);
  369. return true;
  370. }
  371. bool ShadowMask::loadVertexes(CSTR fName)
  372. {
  373. if (m_vVectorMask.size())
  374. {
  375. for (vectorMasking::iterator index = m_vVectorMask.begin(); index < m_vVectorMask.end(); index++)
  376. {
  377. free((*index)->points);
  378. free(*index);
  379. };
  380. m_vVectorMask.clear();
  381. };
  382. FILE * fd;
  383. if (fName == NULL)
  384. {
  385. fd = fopen(m_pVertexFileName, "r");
  386. }
  387. else
  388. {
  389. fd = fopen(fName, "r");
  390. };
  391. if (fd == NULL)
  392. {
  393. return false;
  394. }
  395. int count = 0;
  396. while (! feof(fd))
  397. {
  398. fscanf(fd, "%d\n", &count);
  399. simplePolygone * tSP;
  400. tSP = (simplePolygone *) malloc(sizeof(simplePolygone));
  401. if (tSP != NULL)
  402. {
  403. #if defined(WIN32) || defined(__GUI__)
  404. tSP->points = (QPoint *) malloc(sizeof(QPoint) * count);
  405. #else
  406. tSP->points = (simplePoint *) malloc(sizeof(simplePoint) * count);
  407. #endif
  408. if (tSP->points != NULL)
  409. {
  410. tSP->count = count;
  411. int r, g, b;
  412. fscanf(fd, "%d %d %d\n", &r, &g, &b);
  413. tSP->color[0] = r;
  414. tSP->color[1] = g;
  415. tSP->color[2] = b;
  416. for (int index = 0; index < count; index++)
  417. {
  418. int tX, tY;
  419. fscanf(fd, "%d %d\n", &tX, &tY);
  420. tSP->points[index].setX(tX);
  421. tSP->points[index].setY(tY);
  422. };
  423. m_vVectorMask.push_back(tSP);
  424. };
  425. };
  426. };
  427. fclose(fd);
  428. m_bRasterized = false;
  429. return true;
  430. }
  431. #endif
  432. bool ShadowMask::getMPoly(vectorMasking & VM)
  433. {
  434. VM.clear();
  435. if (m_vVectorMask.size() == 0)
  436. {
  437. return false;
  438. }
  439. for (size_t index = 0; index < m_vVectorMask.size(); index++)
  440. {
  441. VM.push_back(m_vVectorMask.at(index));
  442. };
  443. return true;
  444. }
  445. bool ShadowMask::getZoneMask(vectorZoneMask & ZM)
  446. {
  447. ZM.clear();
  448. if (m_vZoneMask.size() == 0)
  449. {
  450. return false;
  451. }
  452. for (size_t index = 0; index < m_vZoneMask.size(); index++)
  453. {
  454. ZM.push_back(m_vZoneMask.at(index));
  455. };
  456. return true;
  457. }
  458. #if defined(WIN32) || defined(__GUI__)
  459. bool ShadowMask::addVertex(uint16_t polyID, uint32_t SP)
  460. { // Binary Data to Vertex
  461. QPoint simpleP;
  462. simpleP.setX((SP >> 16) & 0xFFFF);
  463. simpleP.setY(SP & 0xFFFF);
  464. return addVertex(polyID, simpleP);
  465. }
  466. bool ShadowMask::addVertex(uint16_t polyID, QPoint SP)
  467. {
  468. if (polyID >= m_vVectorMask.size())
  469. {
  470. // New Polygone
  471. simplePolygone * tPoly;
  472. tPoly = (simplePolygone *) malloc(sizeof(simplePolygone));
  473. if (tPoly != NULL)
  474. {
  475. tPoly->points = (QPoint *) malloc(sizeof(QPoint));
  476. if (tPoly->points != NULL)
  477. {
  478. tPoly->count = 1;
  479. tPoly->points[0].setX(SP.x());
  480. tPoly->points[0].setY(SP.y());
  481. m_vVectorMask.push_back(tPoly);
  482. }
  483. else
  484. {
  485. free(tPoly);
  486. return false;
  487. };
  488. }
  489. else
  490. {
  491. return false;
  492. };
  493. }
  494. else
  495. {
  496. m_vVectorMask.at(polyID)->points = (QPoint *) realloc(m_vVectorMask.at(polyID)->points, sizeof(QPoint) * (m_vVectorMask.at(polyID)->count + 1));
  497. if (m_vVectorMask.at(polyID)->points != NULL)
  498. {
  499. m_vVectorMask.at(polyID)->points[m_vVectorMask.at(polyID)->count].setX(SP.x());
  500. m_vVectorMask.at(polyID)->points[m_vVectorMask.at(polyID)->count].setY(SP.y());
  501. m_vVectorMask.at(polyID)->count++;
  502. }
  503. else
  504. {
  505. return false;
  506. };
  507. };
  508. m_bRasterized = false;
  509. return true;
  510. }
  511. bool ShadowMask::rasterizeVertexes(void)
  512. {
  513. vectorQPoly tVectorPoly;
  514. if (getQPoly(tVectorPoly))
  515. {
  516. if (tVectorPoly.size() == 0)
  517. {
  518. return false;
  519. }
  520. // FILE *fd;
  521. // fd = fopen( m_pRasterFileName, "w" );
  522. // if( fd == NULL ) return false;
  523. if (m_vZoneMask.size())
  524. {
  525. // Clear Data
  526. for (vectorZoneMask::iterator index = m_vZoneMask.begin(); index < m_vZoneMask.end(); index++)
  527. {
  528. free((*index)->zones);
  529. free(*index);
  530. };
  531. m_vZoneMask.clear();
  532. };
  533. // Check each TH Matrix on Polygones
  534. uint16_t cColumn = 0;
  535. uint16_t cStart = 0;
  536. uint16_t cStop = 0;
  537. raySegment RS;
  538. bool oldZone = false;
  539. bool inZone = false;
  540. // Check each point for polygons
  541. for (int x = 0; x < M_SCAN_POINTS; x++)
  542. {
  543. cColumn = x;
  544. oldZone = false;
  545. inZone = false;
  546. cStart = 0;
  547. cStop = 0;
  548. for (int y = 0; y < Q_SCAN_COUNT; y++)
  549. {
  550. QPoint tPoint;
  551. tPoint.setX(x);
  552. tPoint.setY(y);
  553. inZone = false;
  554. for (int i = 0; i < tVectorPoly.size(); i++)
  555. {
  556. if (! inZone)
  557. {
  558. if (tVectorPoly.at(i)->containsPoint(tPoint, Qt::OddEvenFill))
  559. {
  560. if (cStart == 0)
  561. {
  562. cStart = y;
  563. }
  564. inZone = true;
  565. break; // No need to check Other Polygones
  566. };
  567. };
  568. };
  569. if (inZone != oldZone)
  570. {
  571. if (! inZone)
  572. {
  573. cStop = y;
  574. RS.start = cStart;
  575. RS.stop = cStop;
  576. addRay(cColumn, RS);
  577. // fprintf( fd, "%d %d %d\n", cColumn, cStart, cStop );
  578. cStart = 0;
  579. };
  580. oldZone = inZone;
  581. };
  582. };
  583. if (inZone)
  584. {
  585. cStop = Q_SCAN_COUNT;
  586. inZone = false;
  587. oldZone = false;
  588. RS.start = cStart;
  589. RS.stop = cStop;
  590. addRay(cColumn, RS);
  591. // fprintf( fd, "%d %d %d\n", cColumn, cStart, cStop );
  592. };
  593. };
  594. // fclose( fd );
  595. m_bRasterized = true;
  596. };
  597. return true;
  598. }
  599. bool ShadowMask::getQPoly(vectorQPoly & VP)
  600. {
  601. QPolygon * tCurrentPoly;
  602. QPoint * tPoint;
  603. VP.clear();
  604. if (m_vVectorMask.size() == 0)
  605. {
  606. return false;
  607. }
  608. for (int index = 0; index < m_vVectorMask.size(); index++)
  609. {
  610. tCurrentPoly = new QPolygon();
  611. if (tCurrentPoly != NULL)
  612. {
  613. tCurrentPoly->clear();
  614. for (int j = 0; j < m_vVectorMask.at(index)->count; j++)
  615. {
  616. tPoint = new QPoint(m_vVectorMask.at(index)->points[j].x(), m_vVectorMask.at(index)->points[j].y());
  617. if (tPoint != NULL)
  618. {
  619. tCurrentPoly->push_back(*tPoint);
  620. }
  621. tPoint = NULL;
  622. };
  623. VP.push_back(tCurrentPoly);
  624. }
  625. else
  626. {
  627. return false;
  628. }
  629. };
  630. return true;
  631. }
  632. #else // GUI - QT
  633. bool ShadowMask::rasterizeVertexes(void)
  634. {
  635. if (m_vZoneMask.size())
  636. {
  637. // Clear Data
  638. for (vectorZoneMask::iterator index = m_vZoneMask.begin(); index < m_vZoneMask.end(); index++)
  639. {
  640. free((*index)->zones);
  641. free(*index);
  642. };
  643. m_vZoneMask.clear();
  644. };
  645. // Check each TH Matrix on Polygones
  646. uint16_t cColumn = 0;
  647. uint16_t cStart = 0;
  648. uint16_t cStop = 0;
  649. raySegment RS;
  650. bool oldZone = false;
  651. bool inZone = false;
  652. // Check each point for polygons
  653. for (int x = 0; x < M_SCAN_POINTS; x++)
  654. {
  655. cColumn = x;
  656. oldZone = false;
  657. inZone = false;
  658. cStart = 0;
  659. cStop = 0;
  660. for (int y = 0; y < Q_SCAN_COUNT; y++)
  661. {
  662. simplePoint tPoint;
  663. tPoint.setX(x);
  664. tPoint.setY(y);
  665. inZone = false;
  666. for (size_t i = 0; i < m_vVectorMask.size(); i++)
  667. {
  668. if (! inZone)
  669. {
  670. if (isInside(m_vVectorMask.at(i), tPoint))
  671. {
  672. // if( tVectorPoly.at(i)->containsPoint( tPoint, Qt::OddEvenFill) ){
  673. if (cStart == 0)
  674. {
  675. cStart = y;
  676. }
  677. inZone = true;
  678. break; // No need to check Other Polygones
  679. };
  680. };
  681. };
  682. if (inZone != oldZone)
  683. {
  684. if (! inZone)
  685. {
  686. cStop = y;
  687. RS.start = cStart;
  688. RS.stop = cStop;
  689. addRay(cColumn, RS);
  690. // fprintf( fd, "%d %d %d\n", cColumn, cStart, cStop );
  691. cStart = 0;
  692. };
  693. oldZone = inZone;
  694. };
  695. };
  696. if (inZone)
  697. {
  698. cStop = Q_SCAN_COUNT;
  699. inZone = false;
  700. oldZone = false;
  701. RS.start = cStart;
  702. RS.stop = cStop;
  703. addRay(cColumn, RS);
  704. // fprintf( fd, "%d %d %d\n", cColumn, cStart, cStop );
  705. };
  706. };
  707. // fclose( fd );
  708. m_bRasterized = true;
  709. //};
  710. return true;
  711. }
  712. #endif
  713. // A C++ program to check if a given point lies inside a given polygon
  714. // Refer https://www.geeksforgeeks.org/check-if-two-given-line-segments-intersect/
  715. // for explanation of functions onSegment(), orientation() and doIntersect()
  716. // #include <iostream>
  717. // using namespace std;
  718. // Define Infinite (Using INT_MAX caused overflow problems)
  719. #define INF 10000
  720. // struct Point
  721. //{
  722. // int x;
  723. // int y;
  724. // };
  725. // Given three collinear points p, q, r, the function checks if
  726. // point q lies on line segment 'pr'
  727. bool ShadowMask::onSegment(simplePoint p, simplePoint q, simplePoint r)
  728. {
  729. if (q.x() <= max(p.x(), r.x()) && q.x() >= min(p.x(), r.x()) && q.y() <= max(p.y(), r.y()) && q.y() >= min(p.y(), r.y()))
  730. {
  731. return true;
  732. }
  733. return false;
  734. }
  735. // To find orientation of ordered triplet (p, q, r).
  736. // The function returns following values
  737. // 0 --> p, q and r are collinear
  738. // 1 --> Clockwise
  739. // 2 --> Counterclockwise
  740. int ShadowMask::orientation(simplePoint p, simplePoint q, simplePoint r)
  741. {
  742. int val = (q.y() - p.y()) * (r.x() - q.x()) - (q.x() - p.x()) * (r.y() - q.y());
  743. if (val == 0)
  744. {
  745. return 0; // collinear
  746. }
  747. return (val > 0) ? 1 : 2; // clock or counterclock wise
  748. }
  749. // The function that returns true if line segment 'p1q1'
  750. // and 'p2q2' intersect.
  751. bool ShadowMask::doIntersect(simplePoint p1, simplePoint q1, simplePoint p2, simplePoint q2)
  752. {
  753. // Find the four orientations needed for general and
  754. // special cases
  755. int o1 = orientation(p1, q1, p2);
  756. int o2 = orientation(p1, q1, q2);
  757. int o3 = orientation(p2, q2, p1);
  758. int o4 = orientation(p2, q2, q1);
  759. // General case
  760. if (o1 != o2 && o3 != o4)
  761. {
  762. return true;
  763. }
  764. // Special Cases
  765. // p1, q1 and p2 are collinear and p2 lies on segment p1q1
  766. if (o1 == 0 && onSegment(p1, p2, q1))
  767. {
  768. return true;
  769. }
  770. // p1, q1 and p2 are collinear and q2 lies on segment p1q1
  771. if (o2 == 0 && onSegment(p1, q2, q1))
  772. {
  773. return true;
  774. }
  775. // p2, q2 and p1 are collinear and p1 lies on segment p2q2
  776. if (o3 == 0 && onSegment(p2, p1, q2))
  777. {
  778. return true;
  779. }
  780. // p2, q2 and q1 are collinear and q1 lies on segment p2q2
  781. if (o4 == 0 && onSegment(p2, q1, q2))
  782. {
  783. return true;
  784. }
  785. return false; // Doesn't fall in any of the above cases
  786. }
  787. // Returns true if the point p lies inside the polygon[] with n vertices
  788. bool ShadowMask::isInside(simplePolygone * polygon, simplePoint p)
  789. {
  790. // There must be at least 3 vertices in polygon[]
  791. if (polygon->count < 3)
  792. {
  793. return false;
  794. }
  795. // Create a point for line segment from p to infinite
  796. simplePoint extreme; // = {INF, p.y};
  797. extreme.setX(INF);
  798. extreme.setY(p.y());
  799. // Count intersections of the above line with sides of polygon
  800. int count = 0, i = 0;
  801. do
  802. {
  803. int next = (i + 1) % polygon->count;
  804. // Check if the line segment from 'p' to 'extreme' intersects
  805. // with the line segment from 'polygon[i]' to 'polygon[next]'
  806. if (doIntersect(polygon->points[i], polygon->points[next], p, extreme))
  807. {
  808. // If the point 'p' is collinear with line segment 'i-next',
  809. // then check if it lies on segment. If it lies, return true,
  810. // otherwise false
  811. if (orientation(polygon->points[i], p, polygon->points[next]) == 0)
  812. {
  813. return onSegment(polygon->points[i], p, polygon->points[next]);
  814. }
  815. count++;
  816. };
  817. i = next;
  818. }
  819. while (i != 0);
  820. // Return true if count is odd, false otherwise
  821. return count & 1; // Same as (count%2 == 1)
  822. }
  823. /*
  824. // Driver program to test above functions
  825. int main()
  826. {
  827. Point polygon1[] = {{0, 0}, {10, 0}, {10, 10}, {0, 10}};
  828. int n = sizeof(polygon1)/sizeof(polygon1[0]);
  829. Point p = {20, 20};
  830. isInside(polygon1, n, p)? cout << "Yes \n": cout << "No \n";
  831. p = {5, 5};
  832. isInside(polygon1, n, p)? cout << "Yes \n": cout << "No \n";
  833. Point polygon2[] = {{0, 0}, {5, 5}, {5, 0}};
  834. p = {3, 3};
  835. n = sizeof(polygon2)/sizeof(polygon2[0]);
  836. isInside(polygon2, n, p)? cout << "Yes \n": cout << "No \n";
  837. p = {5, 1};
  838. isInside(polygon2, n, p)? cout << "Yes \n": cout << "No \n";
  839. p = {8, 1};
  840. isInside(polygon2, n, p)? cout << "Yes \n": cout << "No \n";
  841. Point polygon3[] = {{0, 0}, {10, 0}, {10, 10}, {0, 10}};
  842. p = {-1,10};
  843. n = sizeof(polygon3)/sizeof(polygon3[0]);
  844. isInside(polygon3, n, p)? cout << "Yes \n": cout << "No \n";
  845. return 0;
  846. }
  847. */
  848. #undef LOG
  849. #undef LOG_ERROR