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.

485 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. #ifdef __linux__
  218. unlink( m_pVertexFileName );
  219. #endif
  220. for( vectorZoneMask::iterator index = m_vZoneMask.begin(); index < m_vZoneMask.end(); index++ ){
  221. tRay = *index;
  222. delete( tRay->zones );
  223. delete( tRay );
  224. };
  225. m_vZoneMask.clear();
  226. #ifdef __linux__
  227. unlink( m_pRasterFileName );
  228. #endif
  229. m_bActive = false;
  230. return true;
  231. };
  232. bool ShadowMask::deletePoly( int ID ){
  233. if( m_vVectorMask.size() < ID ) return false;
  234. delete( m_vVectorMask.at(ID)->points );
  235. delete( m_vVectorMask.at(ID) );
  236. m_vVectorMask.erase( m_vVectorMask.begin()+ID );
  237. #ifdef WIN32
  238. m_vVectorMask.shrink_to_fit();
  239. #endif
  240. return true;
  241. };
  242. #ifdef __linux__
  243. bool ShadowMask::saveVertexes( CSTR fName ){
  244. if( m_vVectorMask.size() == 0 ) return true;
  245. FILE *fd;
  246. if (fName == NULL){
  247. fd = fopen( m_pVertexFileName, "wb+");
  248. } else {
  249. fd = fopen(fName, "wb+");
  250. };
  251. if( fd == NULL ) return false;
  252. for( vectorMasking::iterator index = m_vVectorMask.begin(); index < m_vVectorMask.end(); index++ ){
  253. fprintf( fd, "%d\n", (*index)->count );
  254. fprintf( fd, "%d %d %d\n", (*index)->color[0], (*index)->color[1], (*index)->color[2] );
  255. for( int j = 0; j < (*index)->count; j++){
  256. int cX, cY;
  257. cX = (*index)->points[j].x();
  258. cY = (*index)->points[j].y();
  259. fprintf( fd, "%d %d\n", cX, cY );
  260. };
  261. };
  262. fclose( fd );
  263. return true;
  264. };
  265. bool ShadowMask::loadVertexes( CSTR fName ){
  266. if( m_vVectorMask.size() ){
  267. for( vectorMasking::iterator index = m_vVectorMask.begin(); index < m_vVectorMask.end(); index++ ){
  268. free( (*index)->points );
  269. free( (*index) );
  270. };
  271. m_vVectorMask.clear();
  272. };
  273. FILE *fd;
  274. if (fName == NULL){
  275. fd = fopen( m_pVertexFileName, "r");
  276. } else {
  277. fd = fopen(fName, "r");
  278. };
  279. if( fd == NULL ) return false;
  280. int count = 0;
  281. while( !feof( fd ) ){
  282. fscanf( fd, "%d\n", &count );
  283. simplePolygone *tSP;
  284. tSP = (simplePolygone *) malloc( sizeof(simplePolygone));
  285. if( tSP != NULL ){
  286. #ifdef WIN32
  287. tSP->points = (QPoint *) malloc( sizeof( QPoint ) * count );
  288. #else
  289. tSP->points = (simplePoint *) malloc( sizeof( simplePoint ) * count );
  290. #endif
  291. if( tSP->points != NULL ){
  292. tSP->count = count;
  293. int r,g,b;
  294. fscanf( fd, "%d %d %d\n", &r, &g, &b );
  295. tSP->color[0] = r;
  296. tSP->color[1] = g;
  297. tSP->color[2] = b;
  298. for( int index = 0; index < count; index++ ){
  299. int tX, tY;
  300. fscanf( fd, "%d %d\n", &tX, &tY );
  301. tSP->points[index].setX( tX );
  302. tSP->points[index].setY( tY );
  303. };
  304. m_vVectorMask.push_back( tSP );
  305. };
  306. };
  307. };
  308. fclose( fd );
  309. m_bRasterized = false;
  310. return true;
  311. };
  312. #endif
  313. bool ShadowMask::getMPoly( vectorMasking &VM ){
  314. VM.clear();
  315. if( m_vVectorMask.size() == 0 ) return false;
  316. for( int index = 0; index < m_vVectorMask.size(); index++ ) {
  317. VM.push_back( m_vVectorMask.at( index ) );
  318. };
  319. return true;
  320. };
  321. bool ShadowMask::getZoneMask(vectorZoneMask &ZM){
  322. ZM.clear();
  323. if (m_vZoneMask.size() == 0) return false;
  324. for (int index = 0; index < m_vZoneMask.size(); index++){
  325. ZM.push_back(m_vZoneMask.at(index));
  326. };
  327. return true;
  328. };
  329. #ifdef WIN32
  330. bool ShadowMask::addVertex( uint16_t polyID, uint32_t SP ){// Binary Data to Vertex
  331. QPoint simpleP;
  332. simpleP.setX( ( SP >> 16 ) & 0xFFFF );
  333. simpleP.setY( SP & 0xFFFF );
  334. return addVertex( polyID, simpleP );
  335. };
  336. bool ShadowMask::addVertex( uint16_t polyID, QPoint SP ){
  337. if( polyID >= m_vVectorMask.size() ){
  338. // New Polygone
  339. simplePolygone *tPoly;
  340. tPoly = (simplePolygone *) malloc( sizeof( simplePolygone ) );
  341. if( tPoly != NULL ){
  342. tPoly->points = (QPoint *) malloc( sizeof(QPoint));
  343. if( tPoly->points!= NULL ){
  344. tPoly->count = 1;
  345. tPoly->points[0].setX(SP.x());
  346. tPoly->points[0].setY(SP.y());
  347. m_vVectorMask.push_back(tPoly);
  348. } else {
  349. free( tPoly );
  350. return false;
  351. };
  352. } else {
  353. return false;
  354. };
  355. } else {
  356. m_vVectorMask.at( polyID )->points = (QPoint *) realloc( m_vVectorMask.at( polyID )->points, sizeof(QPoint) * ( m_vVectorMask.at( polyID )->count+1 ));
  357. if( m_vVectorMask.at( polyID )->points!= NULL ){
  358. m_vVectorMask.at( polyID )->points[m_vVectorMask.at( polyID )->count].setX( SP.x() );
  359. m_vVectorMask.at( polyID )->points[m_vVectorMask.at( polyID )->count].setY( SP.y() );
  360. m_vVectorMask.at( polyID )->count++;
  361. } else {
  362. return false;
  363. };
  364. };
  365. m_bRasterized = false;
  366. return true;
  367. };
  368. bool ShadowMask::rasterizeVertexes( void ){
  369. vectorQPoly tVectorPoly;
  370. if( getQPoly( tVectorPoly ) ){
  371. if( tVectorPoly.size() == 0 ) return false;
  372. //FILE *fd;
  373. //fd = fopen( m_pRasterFileName, "w" );
  374. //if( fd == NULL ) return false;
  375. if( m_vZoneMask.size() ){
  376. // Clear Data
  377. for (vectorZoneMask::iterator index = m_vZoneMask.begin(); index < m_vZoneMask.end(); index++){
  378. free((*index)->zones);
  379. free((*index));
  380. };
  381. m_vZoneMask.clear();
  382. };
  383. // Check each TH Matrix on Polygones
  384. uint16_t cColumn = 0;
  385. uint16_t cStart = 0;
  386. uint16_t cStop = 0;
  387. raySegment RS;
  388. bool oldZone = false;
  389. bool inZone = false;
  390. // Check each point for polygons
  391. for( int x = 0; x < M_SCAN_POINTS; x++){
  392. cColumn = x;
  393. oldZone = false;
  394. inZone = false;
  395. cStart = 0;
  396. cStop = 0;
  397. for( int y = 0; y < Q_SCAN_COUNT; y++ ){
  398. QPoint tPoint;
  399. tPoint.setX( x );
  400. tPoint.setY( y );
  401. inZone = false;
  402. for( int i = 0; i < tVectorPoly.size(); i++ ){
  403. if( !inZone ){
  404. if( tVectorPoly.at(i)->containsPoint( tPoint, Qt::OddEvenFill) ){
  405. if( cStart == 0 ) cStart = y;
  406. inZone = true;
  407. break; // No need to check Other Polygones
  408. };
  409. };
  410. };
  411. if( inZone != oldZone ){
  412. if( !inZone ){
  413. cStop = y;
  414. RS.start = cStart;
  415. RS.stop = cStop;
  416. addRay(cColumn, RS);
  417. //fprintf( fd, "%d %d %d\n", cColumn, cStart, cStop );
  418. cStart = 0;
  419. };
  420. oldZone = inZone;
  421. };
  422. };
  423. if( inZone ){
  424. cStop = Q_SCAN_COUNT;
  425. inZone = false;
  426. oldZone = false;
  427. RS.start = cStart;
  428. RS.stop = cStop;
  429. addRay(cColumn, RS);
  430. //fprintf( fd, "%d %d %d\n", cColumn, cStart, cStop );
  431. };
  432. };
  433. //fclose( fd );
  434. m_bRasterized = true;
  435. };
  436. return true;
  437. };
  438. bool ShadowMask::getQPoly( vectorQPoly &VP ){
  439. QPolygon *tCurrentPoly;
  440. QPoint *tPoint;
  441. VP.clear();
  442. if( m_vVectorMask.size() == 0 ) return false;
  443. for( int index = 0; index < m_vVectorMask.size(); index++ ) {
  444. tCurrentPoly = new QPolygon();
  445. if( tCurrentPoly!= NULL ){
  446. tCurrentPoly->clear();
  447. for( int j = 0; j < m_vVectorMask.at( index )->count; j++ ){
  448. tPoint = new QPoint( m_vVectorMask.at( index )->points[j].x(), m_vVectorMask.at( index )->points[j].y() );
  449. if( tPoint!= NULL ) tCurrentPoly->push_back( *tPoint );
  450. tPoint = NULL;
  451. };
  452. VP.push_back( tCurrentPoly );
  453. } else return false;
  454. };
  455. return true;
  456. };
  457. #endif
  458. #undef LOG
  459. #undef LOG_ERROR