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.

470 lines
13 KiB

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. #ifndef WIN32
  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. LOG(m_pVertexFileName);
  13. loadVertexes(m_pVertexFileName);
  14. LOG("done");
  15. LOG(m_pRasterFileName);
  16. loadRastered(m_pRasterFileName);
  17. LOG("done");
  18. #endif
  19. };
  20. ShadowMask::~ShadowMask(){
  21. if( m_vVectorMask.size() ){
  22. #ifdef __linux__
  23. saveVertexes(m_pVertexFileName);
  24. #else
  25. if( !m_bRasterized ){
  26. if( rasterizeVertexes() ){
  27. // Save rasterized
  28. // in Linux Section
  29. };
  30. };
  31. #endif
  32. for( vectorMasking::iterator index = m_vVectorMask.begin(); index < m_vVectorMask.end(); index++ ){
  33. free( (*index)->points );
  34. free( (*index) );
  35. };
  36. m_vVectorMask.clear();
  37. };
  38. #ifdef __linux__
  39. if( m_pVertexFileName ) free( m_pVertexFileName );
  40. #endif
  41. if(m_vZoneMask.size()){
  42. #ifdef __linux__
  43. saveRastered( m_pRasterFileName );
  44. #endif
  45. // Clear Data
  46. for( vectorZoneMask::iterator index = m_vZoneMask.begin(); index < m_vZoneMask.end(); index++ ){
  47. free( (*index)->zones );
  48. free( (*index) );
  49. };
  50. m_vZoneMask.clear();
  51. };
  52. #ifdef __linux__
  53. if( m_pRasterFileName ) free( m_pRasterFileName );
  54. #endif
  55. };
  56. #ifdef __linux__
  57. bool ShadowMask::loadRastered( CSTR fName ){
  58. FILE *fd;
  59. if (fName != NULL){
  60. fd = fopen(fName, "rb");
  61. } else {
  62. fd = fopen(m_pRasterFileName, "rb");
  63. };
  64. if( fd != NULL ){
  65. char tBuffer[256];
  66. bool InRay;
  67. rawRay *tRay;
  68. int tColumnID, tCount, tStart, tStop, tIndex;
  69. m_bActive = false;
  70. if( m_vZoneMask.size() ){
  71. // Clear Data
  72. LOG("Clear Data");
  73. for( vectorZoneMask::iterator index = m_vZoneMask.begin(); index < m_vZoneMask.end(); index++ ){
  74. free( (*index)->zones );
  75. free( (*index) );
  76. };
  77. m_vZoneMask.clear();
  78. LOG("Done");
  79. };
  80. fscanf( fd, "%s\n", tBuffer );
  81. InRay = false;
  82. tIndex = 0;
  83. while( !feof( fd ) ){
  84. if( InRay ){
  85. fscanf( fd, "<%d:%d>\n", &tStart, &tStop );
  86. tRay->zones[tIndex].start = tStart;
  87. tRay->zones[tIndex].stop = tStop;
  88. tIndex++;
  89. if( tIndex == tCount ){
  90. m_vZoneMask.push_back( tRay );
  91. InRay = false;
  92. };
  93. } else {
  94. fscanf( fd, "[%d]:[%d]\n", &tColumnID, &tCount );
  95. if( tCount > 0 ){
  96. tRay = (rawRay *) malloc( sizeof( rawRay ) );
  97. if( tRay != NULL ){
  98. tRay->zones = (raySegment *) malloc( sizeof(raySegment) * tCount );
  99. if( tRay->zones != NULL ){
  100. tRay->column = tColumnID;
  101. tRay->count = tCount;
  102. tIndex = 0;
  103. InRay = true;
  104. } else {
  105. free(tRay);
  106. // Handle Memory Alloc Error
  107. };
  108. } else {
  109. // Handle Memory Alloc Error
  110. };
  111. };
  112. };
  113. };
  114. fclose( fd );
  115. m_bActive = strncmp( tBuffer, "ENA", 3 )==0;
  116. m_bRasterized = true;
  117. LOG("Loading Done");
  118. return true;
  119. } else {
  120. LOG_ERROR("Can not open Data File!");
  121. };
  122. return false;
  123. };
  124. bool ShadowMask::saveRastered( CSTR fName ){
  125. FILE *fd;
  126. if (fName == NULL){
  127. fd = fopen( m_pRasterFileName, "wb+");
  128. } else {
  129. fd = fopen(fName, "wb+");
  130. };
  131. if( fd != NULL ){
  132. if( m_bActive ) fprintf( fd, "ENABLED\n" ); else fprintf( fd, "DISABLED\n" );
  133. for( vectorZoneMask::iterator index = m_vZoneMask.begin(); index < m_vZoneMask.end(); index++ ){
  134. fprintf( fd, "[%d]:[%d]\n", (*index)->column , (*index)->count );
  135. for( int j = 0; j < (*index)->count; j++ ){
  136. fprintf( fd, "<%d:%d>\n", (*index)->zones[j].start, (*index)->zones[j].stop );
  137. };
  138. };
  139. fclose( fd );
  140. m_bRasterized = true;
  141. return true;
  142. };
  143. return false;
  144. };
  145. #endif
  146. bool ShadowMask::isOutside( uint16_t x, uint16_t y ){
  147. if( !m_bActive ) return true;
  148. for( vectorZoneMask::iterator index = m_vZoneMask.begin(); index < m_vZoneMask.end(); index++ ){
  149. if( (*index)->column == x ){
  150. for( int j = 0; j < (*index)->count; j++ ){
  151. if( ( y >= (*index)->zones[j].start ) && ( y <= (*index)->zones[j].stop ) ) return false;
  152. };
  153. break;
  154. };
  155. };
  156. return true;
  157. };
  158. bool ShadowMask::isOutside( simplePoint A ){
  159. if( !m_bActive ) return true;
  160. return isOutside( A.x(), A.y() );
  161. };
  162. void ShadowMask::setActive( bool tVal ){
  163. m_bActive = tVal;
  164. };
  165. bool ShadowMask::addRay( uint16_t column, raySegment RS ){
  166. for( vectorZoneMask::iterator index = m_vZoneMask.begin(); index < m_vZoneMask.end(); index++ ){
  167. if( (*index)->column == column ){
  168. (*index)->zones = (raySegment *) realloc( (*index)->zones, sizeof(raySegment) * ((*index)->count+1) );
  169. (*index)->zones[(*index)->count].start = RS.start;
  170. (*index)->zones[(*index)->count].stop = RS.stop;
  171. (*index)->count++;
  172. return true;
  173. };
  174. };
  175. // not found column
  176. rawRay *tRay;
  177. tRay = (rawRay *) malloc( sizeof( rawRay ) );
  178. if( tRay != NULL ){
  179. tRay->zones = (raySegment *) malloc( sizeof(raySegment) );
  180. if( tRay->zones != NULL ){
  181. tRay->column = column;
  182. tRay->count = 1;
  183. tRay->zones[0].start = RS.start;
  184. tRay->zones[0].stop = RS.stop;
  185. m_vZoneMask.push_back( tRay );
  186. return true;
  187. } else {
  188. free(tRay);
  189. // Handle Memory Alloc Error
  190. };
  191. };
  192. return false;
  193. };
  194. bool ShadowMask::addRay( uint16_t column, uint32_t RS ){// Binary Data to RaySegment
  195. raySegment segR;
  196. segR.start = ( RS >> 16 ) & 0xFFFF;
  197. segR.stop = RS & 0xFFFF;
  198. return addRay( column, segR );
  199. };
  200. bool ShadowMask::addPoly( simplePolygone *SP ){
  201. m_vVectorMask.push_back( SP );
  202. return true;
  203. };
  204. simplePolygone *ShadowMask::getPoly(int id){
  205. if (id >= m_vVectorMask.size()) return NULL;
  206. return m_vVectorMask.at(id);
  207. };
  208. bool ShadowMask::clear( void ){
  209. simplePolygone *tPoly;
  210. rawRay *tRay;
  211. for( vectorMasking::iterator index = m_vVectorMask.begin(); index < m_vVectorMask.end(); index++ ){
  212. tPoly = *index;
  213. delete( tPoly->points );
  214. delete( tPoly );
  215. };
  216. m_vVectorMask.clear();
  217. for( vectorZoneMask::iterator index = m_vZoneMask.begin(); index < m_vZoneMask.end(); index++ ){
  218. tRay = *index;
  219. delete( tRay->zones );
  220. delete( tRay );
  221. };
  222. m_vZoneMask.clear();
  223. return true;
  224. };
  225. bool ShadowMask::deletePoly( int ID ){
  226. if( m_vVectorMask.size() < ID ) return false;
  227. delete( m_vVectorMask.at(ID)->points );
  228. delete( m_vVectorMask.at(ID) );
  229. m_vVectorMask.erase( m_vVectorMask.begin()+ID );
  230. #ifdef WIN32
  231. m_vVectorMask.shrink_to_fit();
  232. #endif
  233. return true;
  234. };
  235. #ifdef __linux__
  236. bool ShadowMask::saveVertexes( CSTR fName ){
  237. if( m_vVectorMask.size() == 0 ) return true;
  238. FILE *fd;
  239. fd = fopen( fName, "w" );
  240. if( fd == NULL ) return false;
  241. for( vectorMasking::iterator index = m_vVectorMask.begin(); index < m_vVectorMask.end(); index++ ){
  242. fprintf( fd, "%d\n", (*index)->count );
  243. fprintf( fd, "%d %d %d\n", (*index)->color[0], (*index)->color[1], (*index)->color[2] );
  244. for( int j = 0; j < (*index)->count; j++){
  245. int cX, cY;
  246. cX = (*index)->points[j].x();
  247. cY = (*index)->points[j].y();
  248. fprintf( fd, "%d %d\n", cX, cY );
  249. };
  250. };
  251. fclose( fd );
  252. return true;
  253. };
  254. bool ShadowMask::loadVertexes( CSTR fName ){
  255. if( m_vVectorMask.size() ){
  256. for( vectorMasking::iterator index = m_vVectorMask.begin(); index < m_vVectorMask.end(); index++ ){
  257. free( (*index)->points );
  258. free( (*index) );
  259. };
  260. m_vVectorMask.clear();
  261. };
  262. FILE *fd;
  263. fd = fopen( fName, "r" );
  264. if( fd == NULL ) return false;
  265. int count = 0;
  266. while( !feof( fd ) ){
  267. fscanf( fd, "%d\n", &count );
  268. simplePolygone *tSP;
  269. tSP = (simplePolygone *) malloc( sizeof(simplePolygone));
  270. if( tSP != NULL ){
  271. #ifdef WIN32
  272. tSP->points = (QPoint *) malloc( sizeof( QPoint ) * count );
  273. #else
  274. tSP->points = (simplePoint *) malloc( sizeof( simplePoint ) * count );
  275. #endif
  276. if( tSP->points != NULL ){
  277. tSP->count = count;
  278. int r,g,b;
  279. fscanf( fd, "%d %d %d\n", &r, &g, &b );
  280. tSP->color[0] = r;
  281. tSP->color[1] = g;
  282. tSP->color[2] = b;
  283. for( int index = 0; index < count; index++ ){
  284. int tX, tY;
  285. fscanf( fd, "%d %d\n", &tX, &tY );
  286. tSP->points[index].setX( tX );
  287. tSP->points[index].setY( tY );
  288. };
  289. m_vVectorMask.push_back( tSP );
  290. };
  291. };
  292. };
  293. fclose( fd );
  294. m_bRasterized = false;
  295. return true;
  296. };
  297. #endif
  298. bool ShadowMask::getMPoly( vectorMasking &VM ){
  299. VM.clear();
  300. if( m_vVectorMask.size() == 0 ) return false;
  301. for( int index = 0; index < m_vVectorMask.size(); index++ ) {
  302. VM.push_back( m_vVectorMask.at( index ) );
  303. };
  304. return true;
  305. };
  306. bool ShadowMask::getZoneMask(vectorZoneMask &ZM){
  307. ZM.clear();
  308. if (m_vZoneMask.size() == 0) return false;
  309. for (int index = 0; index < m_vZoneMask.size(); index++){
  310. ZM.push_back(m_vZoneMask.at(index));
  311. };
  312. return true;
  313. };
  314. #ifdef WIN32
  315. bool ShadowMask::addVertex( uint16_t polyID, uint32_t SP ){// Binary Data to Vertex
  316. QPoint simpleP;
  317. simpleP.setX( ( SP >> 16 ) & 0xFFFF );
  318. simpleP.setY( SP & 0xFFFF );
  319. return addVertex( polyID, simpleP );
  320. };
  321. bool ShadowMask::addVertex( uint16_t polyID, QPoint SP ){
  322. if( polyID >= m_vVectorMask.size() ){
  323. // New Polygone
  324. simplePolygone *tPoly;
  325. tPoly = (simplePolygone *) malloc( sizeof( simplePolygone ) );
  326. if( tPoly != NULL ){
  327. tPoly->points = (QPoint *) malloc( sizeof(QPoint));
  328. if( tPoly->points!= NULL ){
  329. tPoly->count = 1;
  330. tPoly->points[0].setX(SP.x());
  331. tPoly->points[0].setY(SP.y());
  332. m_vVectorMask.push_back(tPoly);
  333. } else {
  334. free( tPoly );
  335. return false;
  336. };
  337. } else {
  338. return false;
  339. };
  340. } else {
  341. m_vVectorMask.at( polyID )->points = (QPoint *) realloc( m_vVectorMask.at( polyID )->points, sizeof(QPoint) * ( m_vVectorMask.at( polyID )->count+1 ));
  342. if( m_vVectorMask.at( polyID )->points!= NULL ){
  343. m_vVectorMask.at( polyID )->points[m_vVectorMask.at( polyID )->count].setX( SP.x() );
  344. m_vVectorMask.at( polyID )->points[m_vVectorMask.at( polyID )->count].setY( SP.y() );
  345. m_vVectorMask.at( polyID )->count++;
  346. } else {
  347. return false;
  348. };
  349. };
  350. m_bRasterized = false;
  351. return true;
  352. };
  353. bool ShadowMask::rasterizeVertexes( void ){
  354. vectorQPoly tVectorPoly;
  355. if( getQPoly( tVectorPoly ) ){
  356. if( tVectorPoly.size() == 0 ) return false;
  357. //FILE *fd;
  358. //fd = fopen( m_pRasterFileName, "w" );
  359. //if( fd == NULL ) return false;
  360. if( m_vZoneMask.size() ){
  361. // Clear Data
  362. for (vectorZoneMask::iterator index = m_vZoneMask.begin(); index < m_vZoneMask.end(); index++){
  363. free((*index)->zones);
  364. free((*index));
  365. };
  366. m_vZoneMask.clear();
  367. };
  368. // Check each TH Matrix on Polygones
  369. uint16_t cColumn = 0;
  370. uint16_t cStart = 0;
  371. uint16_t cStop = 0;
  372. raySegment RS;
  373. bool oldZone = false;
  374. bool inZone = false;
  375. // Check each point for polygons
  376. for( int x = 0; x < M_SCAN_POINTS; x++){
  377. cColumn = x;
  378. oldZone = false;
  379. inZone = false;
  380. cStart = 0;
  381. cStop = 0;
  382. for( int y = 0; y < Q_SCAN_COUNT; y++ ){
  383. QPoint tPoint;
  384. tPoint.setX( x );
  385. tPoint.setY( y );
  386. inZone = false;
  387. for( int i = 0; i < tVectorPoly.size(); i++ ){
  388. if( !inZone ){
  389. if( tVectorPoly.at(i)->containsPoint( tPoint, Qt::OddEvenFill) ){
  390. if( cStart == 0 ) cStart = y;
  391. inZone = true;
  392. break; // No need to check Other Polygones
  393. };
  394. };
  395. };
  396. if( inZone != oldZone ){
  397. if( !inZone ){
  398. cStop = y;
  399. RS.start = cStart;
  400. RS.stop = cStop;
  401. addRay(cColumn, RS);
  402. //fprintf( fd, "%d %d %d\n", cColumn, cStart, cStop );
  403. cStart = 0;
  404. };
  405. oldZone = inZone;
  406. };
  407. };
  408. if( inZone ){
  409. cStop = Q_SCAN_COUNT;
  410. inZone = false;
  411. oldZone = false;
  412. RS.start = cStart;
  413. RS.stop = cStop;
  414. addRay(cColumn, RS);
  415. //fprintf( fd, "%d %d %d\n", cColumn, cStart, cStop );
  416. };
  417. };
  418. //fclose( fd );
  419. m_bRasterized = true;
  420. };
  421. return true;
  422. };
  423. bool ShadowMask::getQPoly( vectorQPoly &VP ){
  424. QPolygon *tCurrentPoly;
  425. QPoint *tPoint;
  426. VP.clear();
  427. if( m_vVectorMask.size() == 0 ) return false;
  428. for( int index = 0; index < m_vVectorMask.size(); index++ ) {
  429. tCurrentPoly = new QPolygon();
  430. if( tCurrentPoly!= NULL ){
  431. tCurrentPoly->clear();
  432. for( int j = 0; j < m_vVectorMask.at( index )->count; j++ ){
  433. tPoint = new QPoint( m_vVectorMask.at( index )->points[j].x(), m_vVectorMask.at( index )->points[j].y() );
  434. if( tPoint!= NULL ) tCurrentPoly->push_back( *tPoint );
  435. tPoint = NULL;
  436. };
  437. VP.push_back( tCurrentPoly );
  438. } else return false;
  439. };
  440. return true;
  441. };
  442. #endif
  443. #undef LOG
  444. #undef LOG_ERROR