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.

677 lines
20 KiB

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