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.
349 lines
8.4 KiB
349 lines
8.4 KiB
//----------------------------------------------------------------------------
|
|
// Laser Guard Ltd.
|
|
// Copyright © 2000-2002. All Rights Reserved.
|
|
// Version 1.0 01/01/2001
|
|
// Last modification: 08/10/2003
|
|
// PROJECT: Laserguard libraries
|
|
// SUBSYSTEM: LgLib
|
|
// FILE: LgAux.h
|
|
// AUTHOR: Chizhevskiy Sergey
|
|
// OVERVIEW
|
|
// ~~~~~~~~
|
|
// Header file of for auxilarity functions/classes
|
|
//----------------------------------------------------------------------------
|
|
|
|
#ifndef __LG__LIB__AUXILARITY__H__
|
|
#define __LG__LIB__AUXILARITY__H__
|
|
|
|
struct CLgDPoint
|
|
{
|
|
public:
|
|
CLgDPoint() : x(0.0), y(0.0){}
|
|
CLgDPoint( double ix, double iy ) : x(ix), y(iy) {}
|
|
CLgDPoint( const CLgDPoint& dp ) : x(dp.x), y(dp.y) {}
|
|
|
|
#if defined(_AFXDLL) || defined(_AFX)
|
|
public:
|
|
CLgDPoint( const CPoint& p ) : x(p.x), y(p.y) {}
|
|
operator CPoint() const { return CPoint( (int)x, (int)y ); }
|
|
#endif
|
|
|
|
friend CLgDPoint operator -( CLgDPoint& p1, CLgDPoint& p2 ) { return CLgDPoint( p1.x - p2.x, p1.y - p2.y); }
|
|
CLgDPoint& operator +=( CLgDPoint& p ) { x += p.x; y += p.y; return *this; }
|
|
|
|
public:
|
|
double x;
|
|
double y;
|
|
};
|
|
|
|
struct CLgDSize
|
|
{
|
|
public:
|
|
CLgDSize() : cx(0.0), cy(0.0){}
|
|
CLgDSize( double icx, double icy ) : cx(icx), cy(icy) {}
|
|
CLgDSize( CLgDSize& ds ) : cx(ds.cx), cy(ds.cy) {}
|
|
|
|
#if defined(_AFXDLL) || defined(_AFX)
|
|
public:
|
|
operator CSize() const { return CSize( (int)cx, (int)cy ); }
|
|
#endif
|
|
|
|
public:
|
|
double cx;
|
|
double cy;
|
|
};
|
|
|
|
struct CLgDRect
|
|
{
|
|
public:
|
|
CLgDRect() : left(0.0), right(0.0), top(0.0), bottom(0.0) {}
|
|
CLgDRect( double l, double t, double r, double b ) : left(l), right(r), top(t), bottom(b) {}
|
|
CLgDRect( CLgDRect& dp ) : left(dp.left), right(dp.right), top(dp.top), bottom(dp.bottom) {}
|
|
|
|
#if defined(_AFXDLL) || defined(_AFX)
|
|
public:
|
|
operator CRect() const { return CRect( (int)left, (int)top, (int)right, (int)bottom ); }
|
|
#endif
|
|
|
|
public:
|
|
BOOL PtInRect( CLgDPoint& p );
|
|
void NormalizeRect();
|
|
void SetRect( double l, double t, double r, double b ){ left = l; right = r; top = t; bottom = b; }
|
|
|
|
public:
|
|
double left;
|
|
double right;
|
|
double top;
|
|
double bottom;
|
|
};
|
|
|
|
inline void CLgDRect::NormalizeRect()
|
|
{
|
|
if( left > right ) { double swap = right; right = left; left = swap; }
|
|
if( top > bottom ) { double swap = bottom; bottom = top; top = swap; }
|
|
}
|
|
|
|
inline BOOL CLgDRect::PtInRect( CLgDPoint& p )
|
|
{
|
|
NormalizeRect();
|
|
return left <= p.x && p.x <= right && top <= p.y && p.y <= bottom;
|
|
}
|
|
|
|
inline void FitRectWithAspect( LPRECT pWholeRect, LPRECT pFitRect, double dAspect )
|
|
{
|
|
if( !pWholeRect )
|
|
return;
|
|
|
|
if( !pFitRect )
|
|
return;
|
|
|
|
int nWholeWidth = pWholeRect->right - pWholeRect->left;
|
|
int nWholeHeight = pWholeRect->bottom - pWholeRect->top;
|
|
|
|
int nFrameWidth = nWholeWidth;
|
|
int nFrameHeight = (int)( (double)( nFrameWidth ) / dAspect + 0.5 );
|
|
if( nFrameHeight > nWholeHeight )
|
|
{
|
|
nFrameHeight = nWholeHeight;
|
|
nFrameWidth = (int)( (double)( nFrameHeight ) * dAspect + 0.5 );
|
|
}
|
|
|
|
int nFrameShiftX = ( nWholeWidth - nFrameWidth ) >> 1;
|
|
int nFrameShiftY = ( nWholeHeight - nFrameHeight ) >> 1;
|
|
|
|
::SetRect( pFitRect, pWholeRect->left + nFrameShiftX, pWholeRect->top + nFrameShiftY,
|
|
pWholeRect->right - nFrameShiftX, pWholeRect->bottom - nFrameShiftY );
|
|
}
|
|
|
|
/*
|
|
// D C-D
|
|
//p1 ----------------- p2
|
|
// \ | /
|
|
// \ | /
|
|
// B \ | / A
|
|
// \ /
|
|
// p3
|
|
|
|
inline BOOL PtInLine( CLgDPoint p1, CLgDPoint p2, CLgDPoint p3, double nQuality )
|
|
{
|
|
double C = sqrt( pow(p1.x-p2.x,2) + pow(p1.y-p2.y,2) );
|
|
double B = sqrt( pow(p1.x-p3.x,2) + pow(p1.y-p3.y,2) );
|
|
double A = sqrt( pow(p2.x-p3.x,2) + pow(p2.y-p3.y,2) );
|
|
|
|
double D = ( pow(B,2) - pow(A,2) + pow(C,2) ) / (2*C);
|
|
|
|
if( ( fabs(D) - C ) > nQuality )
|
|
return FALSE;
|
|
|
|
double X = p1.x + (p2.x-p1.x) * D / C;
|
|
double Y = p1.y + (p2.y-p1.y) * D / C;
|
|
|
|
BOOL bInX = FALSE;
|
|
BOOL bInY = FALSE;
|
|
|
|
double X1, X2;
|
|
if( p1.x < p2.x ) { X1 = p1.x - nQuality; X2 = p2.x + nQuality; }
|
|
else { X1 = p2.x - nQuality; X2 = p1.x + nQuality; }
|
|
|
|
double Y1, Y2;
|
|
if( p1.y < p2.y ) { Y1 = p1.y - nQuality; Y2 = p2.y + nQuality; }
|
|
else { Y1 = p2.y - nQuality; Y2 = p1.y + nQuality; }
|
|
|
|
double R = sqrt( pow(p3.x-X,2) + pow(p3.y-Y,2) );
|
|
|
|
return ( R <= nQuality ) && ( X1 <= X ) && ( X <= X2 ) &&
|
|
( Y1 <= Y ) && ( Y <= Y2 );
|
|
}
|
|
|
|
#ifdef _AFXDLL
|
|
inline BOOL PtInLine( CPoint p1, CPoint p2, CPoint p3, int nQuality )
|
|
{
|
|
CLgDPoint dp1( p1.x, p1.y );
|
|
CLgDPoint dp2( p2.x, p2.y );
|
|
CLgDPoint dp3( p3.x, p3.y );
|
|
return PtInLine( dp1, dp2, dp3, (double)nQuality );
|
|
}
|
|
#endif
|
|
|
|
// p22
|
|
// /
|
|
// p11 /
|
|
// \ /
|
|
// \ p /
|
|
// \ /
|
|
// / \
|
|
// / \
|
|
// / \
|
|
// / \
|
|
// p21 \
|
|
// \
|
|
// p12
|
|
|
|
typedef enum
|
|
{
|
|
LGCT_ERROR = -4,
|
|
LGCT_NOCROSS = -3,
|
|
LGCT_PARALLEL = -2,
|
|
LGCT_COLINEAR = -1,
|
|
LGCT_CROSS = 0,
|
|
LGCT_CROSS_P11 = 0x01,
|
|
LGCT_CROSS_P12 = 0x02,
|
|
LGCT_CROSS_P21 = 0x04,
|
|
LGCT_CROSS_P22 = 0x08,
|
|
}LG_CROSSTYPE;
|
|
|
|
inline bool LgDCmpE( double a, double b, double q ) { return fabs( a - b ) < q; }
|
|
inline bool LgDCmpL( double a, double b, double q ) { return !LgDCmpE(a,b,q) && a < b; }
|
|
inline bool LgDCmpB( double a, double b, double q ) { return !LgDCmpE(a,b,q) && a > b; }
|
|
|
|
inline LG_CROSSTYPE LgCrossProduct( CLgDPoint &p11, CLgDPoint &p12, CLgDPoint &p21, CLgDPoint &p22, CLgDPoint *pP, BOOL bCheckBounds = TRUE )
|
|
{
|
|
if( !pP )
|
|
return LGCT_ERROR;
|
|
|
|
double XA = p11.x, XB = p12.x;
|
|
double YA = p11.y, YB = p12.y;
|
|
double XC = p21.x, XD = p22.x;
|
|
double YC = p21.y, YD = p22.y;
|
|
|
|
double XBA = XB - XA;
|
|
double YBA = YB - YA;
|
|
double XCD = XC - XD;
|
|
double YCD = YC - YD;
|
|
|
|
double D = XBA * YCD - XCD * YBA;
|
|
|
|
const double q = 1E-10;
|
|
|
|
if( LgDCmpE(D,0.0,q) )
|
|
{
|
|
// D = XA * ( YB - YC ) + XC * ( YA - YB ) + XB * ( YC - YA );
|
|
// return ( fabs(D) == 0.0 ) ? LGCT_COLINEAR : LGCT_PARALLEL;
|
|
return LGCT_COLINEAR;
|
|
}
|
|
|
|
double XCA = XC - XA;
|
|
double YCA = YC - YA;
|
|
double T1 = (XCA * YCD - XCD * YCA)/D;
|
|
double T2 = (XBA * YCA - YBA * XCA)/D;
|
|
|
|
if( bCheckBounds && ( LgDCmpL(T1,0,q) || LgDCmpB(T1,1,q) || LgDCmpL(T2,0,q) || LgDCmpB(T2,1,q) ) )
|
|
return LGCT_NOCROSS;
|
|
|
|
pP->x = XA + T1 * XBA;
|
|
pP->y = YA + T1 * YBA;
|
|
|
|
INT nCrossResult = LGCT_CROSS;
|
|
if( LgDCmpE(T1,0,q) ) nCrossResult |= LGCT_CROSS_P11;
|
|
if( LgDCmpE(T1,1,q) ) nCrossResult |= LGCT_CROSS_P12;
|
|
if( LgDCmpE(T2,0,q) ) nCrossResult |= LGCT_CROSS_P21;
|
|
if( LgDCmpE(T2,1,q) ) nCrossResult |= LGCT_CROSS_P22;
|
|
|
|
return (LG_CROSSTYPE)nCrossResult;
|
|
}
|
|
|
|
inline LG_CROSSTYPE LgCrossLines( CLgDPoint &dp11, CLgDPoint &dp12, CLgDPoint &dp21, CLgDPoint &dp22, CLgDPoint *pDP )
|
|
{
|
|
if( !pDP )
|
|
return LGCT_ERROR;
|
|
|
|
BOOL PA0 = FALSE, PA1 = FALSE;
|
|
|
|
double A0, A1, B0, B1;
|
|
|
|
if( dp11.x == dp12.x )
|
|
{
|
|
PA0 = TRUE;
|
|
B0 = dp11.x;
|
|
}
|
|
else
|
|
{
|
|
A0 = (dp11.y - dp12.y) / (dp11.x - dp12.x);
|
|
B0 = dp11.y - A0 * dp11.x;
|
|
}
|
|
|
|
if( dp21.x == dp22.x )
|
|
{
|
|
PA1 = TRUE;
|
|
B1 = dp21.x;
|
|
}
|
|
else
|
|
{
|
|
A1 = (dp21.y - dp22.y) / (dp21.x - dp22.x);
|
|
B1 = dp21.y - A1 * dp21.x;
|
|
}
|
|
|
|
if( PA0 )
|
|
{
|
|
if( PA1 ) return LGCT_COLINEAR;
|
|
|
|
pDP->x = B0;
|
|
pDP->y = A1 * pDP->x + B1;
|
|
}
|
|
else
|
|
{
|
|
if( PA1 ) pDP->x = B1;
|
|
else
|
|
{
|
|
if( A1 != A0 ) pDP->x = (B0 - B1) / (A1 - A0);
|
|
else return LGCT_COLINEAR;
|
|
}
|
|
|
|
pDP->y = A0 * pDP->x + B0;
|
|
}
|
|
|
|
return LGCT_CROSS;
|
|
}
|
|
*/
|
|
inline BOOL LgCreateDirectory( LPCTSTR pFolderName )
|
|
{
|
|
if( !pFolderName )
|
|
return FALSE;
|
|
|
|
// prepare storage for parsed path
|
|
TCHAR pElement[MAX_PATH];
|
|
LPTSTR pElementPtr = pElement;
|
|
|
|
// skip root name (we can't create root)
|
|
LPTSTR pPathPtr = (LPTSTR)PathSkipRoot( pFolderName );
|
|
if( !pPathPtr )
|
|
{
|
|
pPathPtr = (LPTSTR)pFolderName;
|
|
// return FALSE;
|
|
}
|
|
|
|
// copy root
|
|
memcpy( pElement, pFolderName, pPathPtr - pFolderName );
|
|
pElementPtr += (pPathPtr - pFolderName);
|
|
|
|
while( TRUE )
|
|
{
|
|
if( ( *pPathPtr == _T('\\') && pPathPtr != pFolderName ) || *pPathPtr == _T('\0') )
|
|
{
|
|
*pElementPtr = 0;
|
|
if( !::CreateDirectory( pElement, NULL ) )
|
|
{
|
|
DWORD dwError = ::GetLastError();
|
|
switch( dwError )
|
|
{
|
|
case ERROR_SUCCESS:
|
|
case ERROR_FILE_EXISTS:
|
|
case ERROR_ALREADY_EXISTS:
|
|
break;
|
|
|
|
default:
|
|
return FALSE;
|
|
}
|
|
}
|
|
}
|
|
|
|
if( *pPathPtr == _T('\0') )
|
|
break;
|
|
|
|
*pElementPtr++ = *pPathPtr++;
|
|
}
|
|
|
|
return TRUE;
|
|
}
|
|
|
|
#endif
|