#include "mask.h" #define LOG( X ) m_pLog->Log( "ShadowMask", this, X ) #define LOG_ERROR( X ) m_pLog->Log( "ShadowMask", this, _LOG_ERROR, X ) ShadowMask::ShadowMask(){ m_pLog = basicLogCollection::getInstance(); m_bActive = false; #if defined( __linux__ ) && !defined( __GUI__ ) m_pVertexFileName = (CSTR)malloc(256); m_pRasterFileName = (CSTR)malloc(256); sprintf( m_pVertexFileName, "masking.dat" ); sprintf( m_pRasterFileName, "shadow.dat" ); m_bAutoSave = true; #else m_bAutoSave = false; #endif }; ShadowMask::~ShadowMask(){ if( m_vVectorMask.size() ){ if( m_bAutoSave ) saveVertexes(m_pVertexFileName); #if defined( __linux__ ) && !defined( __GUI__ ) #else if( !m_bRasterized ){ if( rasterizeVertexes() ){ // Save rasterized // in Linux Section }; }; #endif for( vectorMasking::iterator index = m_vVectorMask.begin(); index < m_vVectorMask.end(); index++ ){ free( (*index)->points ); free( (*index) ); }; m_vVectorMask.clear(); }; #if defined( __linux__ ) && !defined( __GUI__ ) if( m_pVertexFileName ) free( m_pVertexFileName ); #endif if(m_vZoneMask.size()){ if( m_bAutoSave ) saveRastered( m_pRasterFileName ); #if defined( __linux__ ) && !defined( __GUI__ ) #endif // Clear Data for( vectorZoneMask::iterator index = m_vZoneMask.begin(); index < m_vZoneMask.end(); index++ ){ free( (*index)->zones ); free( (*index) ); }; m_vZoneMask.clear(); }; #if defined( __linux__ ) && !defined( __GUI__ ) if( m_pRasterFileName ) free( m_pRasterFileName ); #endif }; #if defined( __linux__ ) && !defined( __GUI__ ) bool ShadowMask::loadRastered( CSTR fName ){ FILE *fd; if (fName != NULL){ fd = fopen(fName, "rb"); } else { fd = fopen(m_pRasterFileName, "rb"); }; if( fd != NULL ){ char tBuffer[256]; bool InRay; rawRay *tRay; int tColumnID, tCount, tStart, tStop, tIndex; m_bActive = false; if( m_vZoneMask.size() ){ // Clear Data LOG("Clear Data"); for( vectorZoneMask::iterator index = m_vZoneMask.begin(); index < m_vZoneMask.end(); index++ ){ free( (*index)->zones ); free( (*index) ); }; m_vZoneMask.clear(); LOG("Done"); }; fscanf( fd, "%s\n", tBuffer ); InRay = false; tIndex = 0; while( !feof( fd ) ){ if( InRay ){ fscanf( fd, "<%d:%d>\n", &tStart, &tStop ); tRay->zones[tIndex].start = tStart; tRay->zones[tIndex].stop = tStop; tIndex++; if( tIndex == tCount ){ m_vZoneMask.push_back( tRay ); InRay = false; }; } else { fscanf( fd, "[%d]:[%d]\n", &tColumnID, &tCount ); if( tCount > 0 ){ tRay = (rawRay *) malloc( sizeof( rawRay ) ); if( tRay != NULL ){ tRay->zones = (raySegment *) malloc( sizeof(raySegment) * tCount ); if( tRay->zones != NULL ){ tRay->column = tColumnID; tRay->count = tCount; tIndex = 0; InRay = true; } else { free(tRay); // Handle Memory Alloc Error }; } else { // Handle Memory Alloc Error }; }; }; }; fclose( fd ); m_bActive = strncmp( tBuffer, "ENA", 3 )==0; m_bRasterized = true; LOG("Loading Done"); return true; } else { LOG_ERROR("Can not open Data File!"); }; return false; }; bool ShadowMask::saveRastered( CSTR fName ){ FILE *fd; if( m_vZoneMask.size() == 0 ) return false; if( fName != NULL ){ strncpy( m_pRasterFileName, fName, 256 ); }; fd = fopen( m_pRasterFileName, "wb+"); if( fd != NULL ){ if( m_bActive ) fprintf( fd, "ENABLED\n" ); else fprintf( fd, "DISABLED\n" ); for( vectorZoneMask::iterator index = m_vZoneMask.begin(); index < m_vZoneMask.end(); index++ ){ fprintf( fd, "[%d]:[%d]\n", (*index)->column , (*index)->count ); for( int j = 0; j < (*index)->count; j++ ){ fprintf( fd, "<%d:%d>\n", (*index)->zones[j].start, (*index)->zones[j].stop ); }; }; fclose( fd ); m_bRasterized = true; return true; }; return false; }; #endif bool ShadowMask::isOutside( uint16_t x, uint16_t y ){ if( !m_bActive ) return true; for( vectorZoneMask::iterator index = m_vZoneMask.begin(); index < m_vZoneMask.end(); index++ ){ if( (*index)->column == x ){ for( int j = 0; j < (*index)->count; j++ ){ if( ( y >= (*index)->zones[j].start ) && ( y <= (*index)->zones[j].stop ) ) return false; }; break; }; }; return true; }; bool ShadowMask::isOutside( simplePoint A ){ if( !m_bActive ) return true; return isOutside( A.x(), A.y() ); }; void ShadowMask::setActive( bool tVal ){ m_bActive = tVal; }; bool ShadowMask::addRay( uint16_t column, raySegment RS ){ for( vectorZoneMask::iterator index = m_vZoneMask.begin(); index < m_vZoneMask.end(); index++ ){ if( (*index)->column == column ){ (*index)->zones = (raySegment *) realloc( (*index)->zones, sizeof(raySegment) * ((*index)->count+1) ); (*index)->zones[(*index)->count].start = RS.start; (*index)->zones[(*index)->count].stop = RS.stop; (*index)->count++; return true; }; }; // not found column rawRay *tRay; tRay = (rawRay *) malloc( sizeof( rawRay ) ); if( tRay != NULL ){ tRay->zones = (raySegment *) malloc( sizeof(raySegment) ); if( tRay->zones != NULL ){ tRay->column = column; tRay->count = 1; tRay->zones[0].start = RS.start; tRay->zones[0].stop = RS.stop; m_vZoneMask.push_back( tRay ); return true; } else { free(tRay); // Handle Memory Alloc Error }; }; return false; }; bool ShadowMask::addRay( uint16_t column, uint32_t RS ){// Binary Data to RaySegment raySegment segR; segR.start = ( RS >> 16 ) & 0xFFFF; segR.stop = RS & 0xFFFF; return addRay( column, segR ); }; bool ShadowMask::addPoly( simplePolygone *SP ){ LOG("Add Poly"); m_vVectorMask.push_back( SP ); LOG("Add Poly Done"); return true; }; simplePolygone *ShadowMask::getPoly(int id){ if (id >= m_vVectorMask.size()) return NULL; return m_vVectorMask.at(id); }; bool ShadowMask::clear( void ){ simplePolygone *tPoly; rawRay *tRay; for( vectorMasking::iterator index = m_vVectorMask.begin(); index < m_vVectorMask.end(); index++ ){ tPoly = *index; delete( tPoly->points ); delete( tPoly ); }; m_vVectorMask.clear(); if( m_bAutoSave ) unlink( m_pVertexFileName ); #ifdef __linux__ #endif for( vectorZoneMask::iterator index = m_vZoneMask.begin(); index < m_vZoneMask.end(); index++ ){ tRay = *index; delete( tRay->zones ); delete( tRay ); }; m_vZoneMask.clear(); if( m_bAutoSave ) unlink( m_pRasterFileName ); #ifdef __linux__ #endif m_bActive = false; return true; }; bool ShadowMask::deletePoly( int ID ){ if( m_vVectorMask.size() < ID ) return false; delete( m_vVectorMask.at(ID)->points ); delete( m_vVectorMask.at(ID) ); m_vVectorMask.erase( m_vVectorMask.begin()+ID ); #if defined( WIN32 ) || defined( __GUI__ ) m_vVectorMask.shrink_to_fit(); #endif return true; }; #if defined( __linux__ ) && !defined( __GUI__ ) bool ShadowMask::saveVertexes( CSTR fName ){ if( m_vVectorMask.size() == 0 ) return true; FILE *fd; if( fName != NULL){ strncpy( m_pVertexFileName, fName, 256 ); }; fd = fopen( m_pVertexFileName, "wb+"); if( fd == NULL ) return false; for( vectorMasking::iterator index = m_vVectorMask.begin(); index < m_vVectorMask.end(); index++ ){ LOG("VERTEX->SAVE"); fprintf( fd, "%d\n", (*index)->count ); fprintf( fd, "%d %d %d\n", (*index)->color[0], (*index)->color[1], (*index)->color[2] ); for( int j = 0; j < (*index)->count; j++){ int cX, cY; cX = (*index)->points[j].x(); cY = (*index)->points[j].y(); fprintf( fd, "%d %d\n", cX, cY ); }; }; fclose( fd ); return true; }; bool ShadowMask::loadVertexes( CSTR fName ){ if( m_vVectorMask.size() ){ for( vectorMasking::iterator index = m_vVectorMask.begin(); index < m_vVectorMask.end(); index++ ){ free( (*index)->points ); free( (*index) ); }; m_vVectorMask.clear(); }; FILE *fd; if (fName == NULL){ fd = fopen( m_pVertexFileName, "r"); } else { fd = fopen(fName, "r"); }; if( fd == NULL ) return false; int count = 0; while( !feof( fd ) ){ fscanf( fd, "%d\n", &count ); simplePolygone *tSP; tSP = (simplePolygone *) malloc( sizeof(simplePolygone)); if( tSP != NULL ){ #if defined( WIN32 ) || defined( __GUI__ ) tSP->points = (QPoint *) malloc( sizeof( QPoint ) * count ); #else tSP->points = (simplePoint *) malloc( sizeof( simplePoint ) * count ); #endif if( tSP->points != NULL ){ tSP->count = count; int r,g,b; fscanf( fd, "%d %d %d\n", &r, &g, &b ); tSP->color[0] = r; tSP->color[1] = g; tSP->color[2] = b; for( int index = 0; index < count; index++ ){ int tX, tY; fscanf( fd, "%d %d\n", &tX, &tY ); tSP->points[index].setX( tX ); tSP->points[index].setY( tY ); }; m_vVectorMask.push_back( tSP ); }; }; }; fclose( fd ); m_bRasterized = false; return true; }; #endif bool ShadowMask::getMPoly( vectorMasking &VM ){ VM.clear(); if( m_vVectorMask.size() == 0 ) return false; for( int index = 0; index < m_vVectorMask.size(); index++ ) { VM.push_back( m_vVectorMask.at( index ) ); }; return true; }; bool ShadowMask::getZoneMask(vectorZoneMask &ZM){ ZM.clear(); if (m_vZoneMask.size() == 0) return false; for (int index = 0; index < m_vZoneMask.size(); index++){ ZM.push_back(m_vZoneMask.at(index)); }; return true; }; #if defined( WIN32 ) || defined( __GUI__ ) bool ShadowMask::addVertex( uint16_t polyID, uint32_t SP ){// Binary Data to Vertex QPoint simpleP; simpleP.setX( ( SP >> 16 ) & 0xFFFF ); simpleP.setY( SP & 0xFFFF ); return addVertex( polyID, simpleP ); }; bool ShadowMask::addVertex( uint16_t polyID, QPoint SP ){ if( polyID >= m_vVectorMask.size() ){ // New Polygone simplePolygone *tPoly; tPoly = (simplePolygone *) malloc( sizeof( simplePolygone ) ); if( tPoly != NULL ){ tPoly->points = (QPoint *) malloc( sizeof(QPoint)); if( tPoly->points!= NULL ){ tPoly->count = 1; tPoly->points[0].setX(SP.x()); tPoly->points[0].setY(SP.y()); m_vVectorMask.push_back(tPoly); } else { free( tPoly ); return false; }; } else { return false; }; } else { m_vVectorMask.at( polyID )->points = (QPoint *) realloc( m_vVectorMask.at( polyID )->points, sizeof(QPoint) * ( m_vVectorMask.at( polyID )->count+1 )); if( m_vVectorMask.at( polyID )->points!= NULL ){ m_vVectorMask.at( polyID )->points[m_vVectorMask.at( polyID )->count].setX( SP.x() ); m_vVectorMask.at( polyID )->points[m_vVectorMask.at( polyID )->count].setY( SP.y() ); m_vVectorMask.at( polyID )->count++; } else { return false; }; }; m_bRasterized = false; return true; }; bool ShadowMask::rasterizeVertexes( void ){ vectorQPoly tVectorPoly; if( getQPoly( tVectorPoly ) ){ if( tVectorPoly.size() == 0 ) return false; //FILE *fd; //fd = fopen( m_pRasterFileName, "w" ); //if( fd == NULL ) return false; if( m_vZoneMask.size() ){ // Clear Data for (vectorZoneMask::iterator index = m_vZoneMask.begin(); index < m_vZoneMask.end(); index++){ free((*index)->zones); free((*index)); }; m_vZoneMask.clear(); }; // Check each TH Matrix on Polygones uint16_t cColumn = 0; uint16_t cStart = 0; uint16_t cStop = 0; raySegment RS; bool oldZone = false; bool inZone = false; // Check each point for polygons for( int x = 0; x < M_SCAN_POINTS; x++){ cColumn = x; oldZone = false; inZone = false; cStart = 0; cStop = 0; for( int y = 0; y < Q_SCAN_COUNT; y++ ){ QPoint tPoint; tPoint.setX( x ); tPoint.setY( y ); inZone = false; for( int i = 0; i < tVectorPoly.size(); i++ ){ if( !inZone ){ if( tVectorPoly.at(i)->containsPoint( tPoint, Qt::OddEvenFill) ){ if( cStart == 0 ) cStart = y; inZone = true; break; // No need to check Other Polygones }; }; }; if( inZone != oldZone ){ if( !inZone ){ cStop = y; RS.start = cStart; RS.stop = cStop; addRay(cColumn, RS); //fprintf( fd, "%d %d %d\n", cColumn, cStart, cStop ); cStart = 0; }; oldZone = inZone; }; }; if( inZone ){ cStop = Q_SCAN_COUNT; inZone = false; oldZone = false; RS.start = cStart; RS.stop = cStop; addRay(cColumn, RS); //fprintf( fd, "%d %d %d\n", cColumn, cStart, cStop ); }; }; //fclose( fd ); m_bRasterized = true; }; return true; }; bool ShadowMask::getQPoly( vectorQPoly &VP ){ QPolygon *tCurrentPoly; QPoint *tPoint; VP.clear(); if( m_vVectorMask.size() == 0 ) return false; for( int index = 0; index < m_vVectorMask.size(); index++ ) { tCurrentPoly = new QPolygon(); if( tCurrentPoly!= NULL ){ tCurrentPoly->clear(); for( int j = 0; j < m_vVectorMask.at( index )->count; j++ ){ tPoint = new QPoint( m_vVectorMask.at( index )->points[j].x(), m_vVectorMask.at( index )->points[j].y() ); if( tPoint!= NULL ) tCurrentPoly->push_back( *tPoint ); tPoint = NULL; }; VP.push_back( tCurrentPoly ); } else return false; }; return true; }; #else // GUI - QT bool ShadowMask::rasterizeVertexes( void ){ if( m_vZoneMask.size() ){ // Clear Data for (vectorZoneMask::iterator index = m_vZoneMask.begin(); index < m_vZoneMask.end(); index++){ free((*index)->zones); free((*index)); }; m_vZoneMask.clear(); }; // Check each TH Matrix on Polygones uint16_t cColumn = 0; uint16_t cStart = 0; uint16_t cStop = 0; raySegment RS; bool oldZone = false; bool inZone = false; // Check each point for polygons for( int x = 0; x < M_SCAN_POINTS; x++){ cColumn = x; oldZone = false; inZone = false; cStart = 0; cStop = 0; for( int y = 0; y < Q_SCAN_COUNT; y++ ){ simplePoint tPoint; tPoint.setX( x ); tPoint.setY( y ); inZone = false; for( int i = 0; i < m_vVectorMask.size(); i++ ){ if( !inZone ){ if( isInside( m_vVectorMask.at(i), tPoint ) ){ //if( tVectorPoly.at(i)->containsPoint( tPoint, Qt::OddEvenFill) ){ if( cStart == 0 ) cStart = y; inZone = true; break; // No need to check Other Polygones }; }; }; if( inZone != oldZone ){ if( !inZone ){ cStop = y; RS.start = cStart; RS.stop = cStop; addRay(cColumn, RS); //fprintf( fd, "%d %d %d\n", cColumn, cStart, cStop ); cStart = 0; }; oldZone = inZone; }; }; if( inZone ){ cStop = Q_SCAN_COUNT; inZone = false; oldZone = false; RS.start = cStart; RS.stop = cStop; addRay(cColumn, RS); //fprintf( fd, "%d %d %d\n", cColumn, cStart, cStop ); }; }; //fclose( fd ); m_bRasterized = true; //}; return true; }; #endif // A C++ program to check if a given point lies inside a given polygon // Refer https://www.geeksforgeeks.org/check-if-two-given-line-segments-intersect/ // for explanation of functions onSegment(), orientation() and doIntersect() //#include //using namespace std; // Define Infinite (Using INT_MAX caused overflow problems) #define INF 10000 //struct Point //{ // int x; // int y; //}; // Given three collinear points p, q, r, the function checks if // point q lies on line segment 'pr' bool ShadowMask::onSegment( simplePoint p, simplePoint q, simplePoint r){ 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; return false; }; // To find orientation of ordered triplet (p, q, r). // The function returns following values // 0 --> p, q and r are collinear // 1 --> Clockwise // 2 --> Counterclockwise int ShadowMask::orientation( simplePoint p, simplePoint q, simplePoint r ){ int val = (q.y() - p.y()) * (r.x() - q.x()) - (q.x() - p.x()) * (r.y() - q.y()); if( val == 0 ) return 0; // collinear return ( val > 0 )? 1: 2; // clock or counterclock wise }; // The function that returns true if line segment 'p1q1' // and 'p2q2' intersect. bool ShadowMask::doIntersect( simplePoint p1, simplePoint q1, simplePoint p2, simplePoint q2 ){ // Find the four orientations needed for general and // special cases int o1 = orientation( p1, q1, p2 ); int o2 = orientation( p1, q1, q2 ); int o3 = orientation( p2, q2, p1 ); int o4 = orientation( p2, q2, q1 ); // General case if( o1 != o2 && o3 != o4 ) return true; // Special Cases // p1, q1 and p2 are collinear and p2 lies on segment p1q1 if( o1 == 0 && onSegment(p1, p2, q1) ) return true; // p1, q1 and p2 are collinear and q2 lies on segment p1q1 if( o2 == 0 && onSegment(p1, q2, q1) ) return true; // p2, q2 and p1 are collinear and p1 lies on segment p2q2 if( o3 == 0 && onSegment(p2, p1, q2) ) return true; // p2, q2 and q1 are collinear and q1 lies on segment p2q2 if( o4 == 0 && onSegment(p2, q1, q2) ) return true; return false; // Doesn't fall in any of the above cases } // Returns true if the point p lies inside the polygon[] with n vertices bool ShadowMask::isInside( simplePolygone *polygon, simplePoint p ){ // There must be at least 3 vertices in polygon[] if (polygon->count < 3) return false; // Create a point for line segment from p to infinite simplePoint extreme;// = {INF, p.y}; extreme.setX( INF ); extreme.setY( p.y() ); // Count intersections of the above line with sides of polygon int count = 0, i = 0; do{ int next = (i+1) % polygon->count; // Check if the line segment from 'p' to 'extreme' intersects // with the line segment from 'polygon[i]' to 'polygon[next]' if( doIntersect( polygon->points[i], polygon->points[next], p, extreme )){ // If the point 'p' is collinear with line segment 'i-next', // then check if it lies on segment. If it lies, return true, // otherwise false if( orientation( polygon->points[i], p, polygon->points[next] ) == 0 ) return onSegment( polygon->points[i], p, polygon->points[next] ); count++; }; i = next; } while( i != 0 ); // Return true if count is odd, false otherwise return count & 1; // Same as (count%2 == 1) }; /* // Driver program to test above functions int main() { Point polygon1[] = {{0, 0}, {10, 0}, {10, 10}, {0, 10}}; int n = sizeof(polygon1)/sizeof(polygon1[0]); Point p = {20, 20}; isInside(polygon1, n, p)? cout << "Yes \n": cout << "No \n"; p = {5, 5}; isInside(polygon1, n, p)? cout << "Yes \n": cout << "No \n"; Point polygon2[] = {{0, 0}, {5, 5}, {5, 0}}; p = {3, 3}; n = sizeof(polygon2)/sizeof(polygon2[0]); isInside(polygon2, n, p)? cout << "Yes \n": cout << "No \n"; p = {5, 1}; isInside(polygon2, n, p)? cout << "Yes \n": cout << "No \n"; p = {8, 1}; isInside(polygon2, n, p)? cout << "Yes \n": cout << "No \n"; Point polygon3[] = {{0, 0}, {10, 0}, {10, 10}, {0, 10}}; p = {-1,10}; n = sizeof(polygon3)/sizeof(polygon3[0]); isInside(polygon3, n, p)? cout << "Yes \n": cout << "No \n"; return 0; } */ #undef LOG #undef LOG_ERROR