|
|
//----------------------------------------------------------------------------
// Laser Guard Ltd.
// Copyright � 2000-2002. All Rights Reserved.
// Version 1.0 01/01/2000
// Last modification: 08/10/2003
// PROJECT:
// SUBSYSTEM:
// FILE: LgDPolygon.cpp
// AUTHOR: Sergey Chizhevskiy
// OVERVIEW
// ~~~~~~~~
// Source file of LgDPolygon.cpp
//----------------------------------------------------------------------------
#include "stdafx.h"
#include "LgLogger.h"
#include "LgDPolygon.h"
#include "LgPerformanceCounter.h"
#include "LgGeometry.h"
CLgDPolygon::CLgDPolygon(){
}
CLgDPolygon::~CLgDPolygon(){
}
CLgDPolygon& CLgDPolygon::operator =( CLgDPolygon& polygon ){ RemoveAll();
for( INT i = 0; i < polygon.GetCount(); i++ ) Add( polygon[i] );
return *this;}
INT CLgDPolygon::Minimize(){ // store count
INT nCount = GetCount(); if( !nCount ) return nCount;
// minimize polygon
double odx = 0; double ody = 0;
CLgDPoint odp;
INT nPoint = 0;
for( INT i = 0; i <= GetCount(); i++ ) { CLgDPoint &dp = GetAt( i % GetCount() );
double dx, dy; double at, oat;
switch( nPoint ) { case 0: nPoint++;
// store point
odp = dp; break;
case 1: nPoint++;
// calculate delta
odx = odp.x - dp.x; ody = odp.y - dp.y;
// store point
odp = dp; break;
case 2: // calculate delta
dx = odp.x - dp.x; dy = odp.y - dp.y;
// store point
odp = dp;
// check delta
at = atan2( dx, dy ); oat = atan2( odx, ody ); if( at == oat ) { // remove previous point
Remove( (i - 1 + GetCount())%GetCount() );
// decrease counter
i--; }
// store delta
odx = dx; ody = dy; break; } }
return nCount - GetCount();}
typedef struct CLgDPointEx : public CLgDLinePoint{ enum CLgDPointExDir{ DIR_CROSS, DIR_TOP, DIR_BOTTOM } dir; INT nIndex; BOOL bDelete; }CLgDPointEx;
BOOL CLgDPolygon::CrossProduct_( CLgDPoint dp1, CLgDPoint dp2, CLgDLinePointArray &array, BOOL bGlueLines ){ if( !GetCount() ) return TRUE;
CLgPerformanceCounter pc; pc.Begin();
CLgDPoint &dp11 = dp1; CLgDPoint &dp12 = dp2;
if( dp11.x == dp12.x && dp11.y == dp12.y ) { LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_WARNING, this, _T("CLgDPolygon::CrossProduct"), _T("Line segment represented by two equal point") ); return FALSE; }
BOOL bSortTopBottom = fabs(dp11.y - dp12.y) > fabs(dp11.x - dp12.x);
// find all cross points
CLgArray<CLgDPointEx> points;
double dx1 = dp11.x - dp12.x; double dy1 = dp11.y - dp12.y;
for( INT i = 0; i < GetCount(); i++ ) { INT nIndex1 = ( i ) % GetCount(); INT nIndex2 = ( i + 1 ) % GetCount();
CLgDPoint dp21 = GetAt(nIndex1); CLgDPoint dp22 = GetAt(nIndex2);
// calculate scalar vector multiplication
double dx2 = dp21.x - dp22.x; double dy2 = dp21.y - dp22.y;
double d = dx1 * dy2 - dx2 * dy1;
// direct vectors perpendiculary to sort direction
BOOL bSwap = d < 0; if( bSwap ) { CLgDPoint sdp = dp21; dp21 = dp22; dp22 = sdp; }
CLgDPointEx dpe; INT nCrossType = CLgGeometry::CrossProduct( dp11, dp12, dp21, dp22, &dpe ); if( nCrossType >= CLgGeometry::LGCT_CROSS ) { if( nCrossType & (CLgGeometry::LGCT_CROSS_P11|CLgGeometry::LGCT_CROSS_P12) ) { LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgDPolygon::CrossProduct"), _T("End points of cross line should outside of region") ); return FALSE; }
if( ( nCrossType & (CLgGeometry::LGCT_CROSS_P21|CLgGeometry::LGCT_CROSS_P22) ) == (CLgGeometry::LGCT_CROSS_P21|CLgGeometry::LGCT_CROSS_P22) ) { LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgDPolygon::CrossProduct"), _T("Line crossed by another line in two points") ); return FALSE; }
dpe.bDelete = FALSE;
if( nCrossType & CLgGeometry::LGCT_CROSS_P21 ) { dpe.dir = CLgDPointEx::DIR_TOP; dpe.nIndex = bSwap ? nIndex2 : nIndex1; } else if( nCrossType & CLgGeometry::LGCT_CROSS_P22 ) { dpe.dir = CLgDPointEx::DIR_BOTTOM; dpe.nIndex = bSwap ? nIndex1 : nIndex2; } else { dpe.dir = CLgDPointEx::DIR_CROSS; dpe.nIndex = -1; }
points.Add( dpe ); } }
// delete points with equal index
for( i = 0; i < points.GetCount(); i++ ) { CLgDPointEx &dpe = points.GetAt(i);
if( dpe.bDelete || dpe.dir == CLgDPointEx::DIR_CROSS ) continue;
// find connected enge
for( int j = i+1; j < points.GetCount(); j++ ) { CLgDPointEx &dpe2 = points.GetAt(j); if( dpe.nIndex != dpe2.nIndex ) continue;
if( dpe.dir == dpe2.dir ) dpe.bDelete = TRUE;
dpe2.bDelete = TRUE; break; } }
// sort points along bSortTopBottom and fill segments
BOOL bBegin = TRUE; for( i = 0; i < points.GetCount(); i++ ) { CLgDPointEx &dp = points.GetAt(i);
INT nIndex = i; for( INT j = i + 1; j < points.GetCount(); j++ ) { CLgDPointEx &sdp = points.GetAt(j); CLgDPointEx &mdp = points.GetAt(nIndex);
if( bSortTopBottom ) { if( sdp.y < mdp.y ) nIndex = j; } else { if( sdp.x < mdp.x ) nIndex = j; } } if( nIndex != i ) { CLgDPointEx &mdp = points.GetAt(nIndex);
CLgDPointEx sdp = dp; dp = mdp; mdp = sdp; }
// fill segments
if( dp.bDelete ) continue;
// store point
CLgDLinePoint ldp = dp; ldp.bBegin = bBegin;
// if nessessary to glue lines
if( bGlueLines ) { // add into appropriate place of array
BOOL bInserted = FALSE; for( INT j = 0; j < array.GetCount(); j++ ) { if( bSortTopBottom ) { if( array[j].y > ldp.y ) { array.InsertAt( j, ldp ); bInserted = TRUE; break; } } else { if( array[j].x > ldp.x ) { array.InsertAt( j, ldp ); bInserted = TRUE; break; } } } if( !bInserted ) array.Add( ldp ); } else array.Add( ldp );
bBegin = !bBegin; }
// if nessessary to glue lines
if( bGlueLines ) { bBegin = FALSE; INT coef = 0; for( INT i = 0; i < array.GetCount(); ) { CLgDLinePoint &lp = array[i];
if( lp.bBegin ) coef++; else coef--;
bool bDelete = TRUE; if( bBegin ) { if( coef == 0 ) { bBegin = FALSE; bDelete = FALSE; } } else { if( coef == 1 ) { bBegin = TRUE; bDelete = FALSE; } }
if( bDelete ) array.Remove(i); else i++; } }
LGLOG( LOG_LEVEL_REALTIME, LOG_TYPE_INFO, this, _T("CLgDPolygon::CrossProduct"), _T("Processing time is %0.6f for %d points"), pc.End(), GetCount() );
return TRUE;}
BOOL CLgDPolygon::PtInPolygon_( CLgDPoint pt ){ if( !GetCount() ) return FALSE;
CLgPerformanceCounter pc; pc.Begin();
CLgDRect rect( DBL_MAX, DBL_MAX, -DBL_MAX, -DBL_MAX ); for( INT i = 0; i < GetCount(); i++ ) { CLgDPoint& dp = GetAt(i); if( dp.x < rect.left ) rect.left = dp.x; if( dp.x > rect.right ) rect.right = dp.x; }
double delta = 100.0; CLgDPoint dp11( rect.left - delta, pt.y ); CLgDPoint dp12( rect.right + delta, pt.y );
BOOL bSortTopBottom = FALSE;
// find all cross points
CLgArray<CLgDPointEx> points;
double dx1 = dp11.x - dp12.x; double dy1 = dp11.y - dp12.y;
for( i = 0; i < GetCount(); i++ ) { INT nIndex1 = ( i ) % GetCount(); INT nIndex2 = ( i + 1 ) % GetCount();
CLgDPoint dp21 = GetAt(nIndex1); CLgDPoint dp22 = GetAt(nIndex2);
double dx2 = dp21.x - dp22.x; double dy2 = dp21.y - dp22.y;
// calculate scalar vector addition
double d = dx1 * dy2 - dx2 * dy1;
// direct vectors perpendiculary to sort direction
BOOL bSwap = d < 0; if( bSwap ) { CLgDPoint sdp = dp21; dp21 = dp22; dp22 = sdp; }
CLgDPointEx dpe; INT nCrossType = CLgGeometry::CrossProduct( dp11, dp12, dp21, dp22, &dpe ); if( nCrossType >= CLgGeometry::LGCT_CROSS ) { if( nCrossType & (CLgGeometry::LGCT_CROSS_P11|CLgGeometry::LGCT_CROSS_P12) ) { LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgDPolygon::PtInPolygon"), _T("End points of cross line should outside of region") ); return FALSE; }
if( ( nCrossType & (CLgGeometry::LGCT_CROSS_P21|CLgGeometry::LGCT_CROSS_P22) ) == (CLgGeometry::LGCT_CROSS_P21|CLgGeometry::LGCT_CROSS_P22) ) { LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgDPolygon::PtInPolygon"), _T("Line crossed by another line in two points") ); return FALSE; }
dpe.bDelete = FALSE;
if( nCrossType & CLgGeometry::LGCT_CROSS_P21 ) { dpe.dir = CLgDPointEx::DIR_TOP; dpe.nIndex = bSwap ? nIndex2 : nIndex1; } else if( nCrossType & CLgGeometry::LGCT_CROSS_P22 ) { dpe.dir = CLgDPointEx::DIR_BOTTOM; dpe.nIndex = bSwap ? nIndex1 : nIndex2; } else { dpe.dir = CLgDPointEx::DIR_CROSS; dpe.nIndex = -1; }
points.Add( dpe ); } }
// delete points with equal index
for( i = 0; i < points.GetCount(); i++ ) { CLgDPointEx &dpe = points.GetAt(i);
if( dpe.bDelete || dpe.dir == CLgDPointEx::DIR_CROSS ) continue;
// find connected enge
for( int j = i+1; j < points.GetCount(); j++ ) { CLgDPointEx &dpe2 = points.GetAt(j); if( dpe.nIndex != dpe2.nIndex ) continue;
if( dpe.dir == dpe2.dir ) dpe.bDelete = TRUE;
dpe2.bDelete = TRUE; break; } }
// sort points along bSortTopBottom and fill segments
BOOL bResult = FALSE; BOOL bStart = FALSE; for( i = 0; i < points.GetCount(); i++ ) { CLgDPointEx &dp = points.GetAt(i);
INT nIndex = i; for( INT j = i + 1; j < points.GetCount(); j++ ) { CLgDPointEx &sdp = points.GetAt(j); CLgDPointEx &mdp = points.GetAt(nIndex);
if( bSortTopBottom ) { if( sdp.y < mdp.y ) nIndex = j; } else { if( sdp.x < mdp.x ) nIndex = j; } } if( nIndex != i ) { CLgDPointEx &mdp = points.GetAt(nIndex);
CLgDPointEx sdp = dp; dp = mdp; mdp = sdp; }
// fill segments
if( dp.bDelete ) continue;
if( bStart ) { if( pt.x < dp.x ) { bResult = TRUE; break; } } else { if( pt.x < dp.x ) break; }
bStart = !bStart; }
LGLOG( LOG_LEVEL_REALTIME, LOG_TYPE_INFO, this, _T("CLgDPolygon::PtInPolygon"), _T("Processing time is %0.6f for %d points"), pc.End(), GetCount() ); return bResult;}
BOOL CLgDPolygon::CrossProduct( CLgDPoint dp1, CLgDPoint dp2, CLgDLinePointArray &array, BOOL bGlueLines ){ BOOL bResult = FALSE;
__try { bResult = CrossProduct_( dp1, dp2, array, bGlueLines ); } __except( EXCEPTION_EXECUTE_HANDLER ) { LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgDPolygon::CrossProduct"), _T("Exception 0x%08X"), _exception_code() ); }
return bResult;}
BOOL CLgDPolygon::PtInPolygon( CLgDPoint pt ){ BOOL bResult = FALSE;
__try { bResult = PtInPolygon_( pt ); } __except( EXCEPTION_EXECUTE_HANDLER ) { LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgDPolygon::PtInPolygon"), _T("Exception 0x%08X"), _exception_code() ); }
return bResult;}
|