Old fashion design for PC-104
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

4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. //----------------------------------------------------------------------------
  2. // Laser Guard Ltd.
  3. // Copyright � 2000-2002. All Rights Reserved.
  4. // Version 1.0 01/01/2001
  5. // Last modification: 08/10/2003
  6. // PROJECT: Laserguard libraries
  7. // SUBSYSTEM: LgLib
  8. // FILE: LgAux.h
  9. // AUTHOR: Chizhevskiy Sergey
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // Header file of for auxilarity functions/classes
  13. //----------------------------------------------------------------------------
  14. #ifndef __LG__LIB__AUXILARITY__H__
  15. #define __LG__LIB__AUXILARITY__H__
  16. struct CLgDPoint
  17. {
  18. public:
  19. CLgDPoint() : x(0.0), y(0.0){}
  20. CLgDPoint( double ix, double iy ) : x(ix), y(iy) {}
  21. CLgDPoint( const CLgDPoint& dp ) : x(dp.x), y(dp.y) {}
  22. #if defined(_AFXDLL) || defined(_AFX)
  23. public:
  24. CLgDPoint( const CPoint& p ) : x(p.x), y(p.y) {}
  25. operator CPoint() const { return CPoint( (int)x, (int)y ); }
  26. #endif
  27. friend CLgDPoint operator -( CLgDPoint& p1, CLgDPoint& p2 ) { return CLgDPoint( p1.x - p2.x, p1.y - p2.y); }
  28. CLgDPoint& operator +=( CLgDPoint& p ) { x += p.x; y += p.y; return *this; }
  29. public:
  30. double x;
  31. double y;
  32. };
  33. struct CLgDSize
  34. {
  35. public:
  36. CLgDSize() : cx(0.0), cy(0.0){}
  37. CLgDSize( double icx, double icy ) : cx(icx), cy(icy) {}
  38. CLgDSize( CLgDSize& ds ) : cx(ds.cx), cy(ds.cy) {}
  39. #if defined(_AFXDLL) || defined(_AFX)
  40. public:
  41. operator CSize() const { return CSize( (int)cx, (int)cy ); }
  42. #endif
  43. public:
  44. double cx;
  45. double cy;
  46. };
  47. struct CLgDRect
  48. {
  49. public:
  50. CLgDRect() : left(0.0), right(0.0), top(0.0), bottom(0.0) {}
  51. CLgDRect( double l, double t, double r, double b ) : left(l), right(r), top(t), bottom(b) {}
  52. CLgDRect( CLgDRect& dp ) : left(dp.left), right(dp.right), top(dp.top), bottom(dp.bottom) {}
  53. #if defined(_AFXDLL) || defined(_AFX)
  54. public:
  55. operator CRect() const { return CRect( (int)left, (int)top, (int)right, (int)bottom ); }
  56. #endif
  57. public:
  58. BOOL PtInRect( CLgDPoint& p );
  59. void NormalizeRect();
  60. void SetRect( double l, double t, double r, double b ){ left = l; right = r; top = t; bottom = b; }
  61. public:
  62. double left;
  63. double right;
  64. double top;
  65. double bottom;
  66. };
  67. inline void CLgDRect::NormalizeRect()
  68. {
  69. if( left > right ) { double swap = right; right = left; left = swap; }
  70. if( top > bottom ) { double swap = bottom; bottom = top; top = swap; }
  71. }
  72. inline BOOL CLgDRect::PtInRect( CLgDPoint& p )
  73. {
  74. NormalizeRect();
  75. return left <= p.x && p.x <= right && top <= p.y && p.y <= bottom;
  76. }
  77. inline void FitRectWithAspect( LPRECT pWholeRect, LPRECT pFitRect, double dAspect )
  78. {
  79. if( !pWholeRect )
  80. return;
  81. if( !pFitRect )
  82. return;
  83. int nWholeWidth = pWholeRect->right - pWholeRect->left;
  84. int nWholeHeight = pWholeRect->bottom - pWholeRect->top;
  85. int nFrameWidth = nWholeWidth;
  86. int nFrameHeight = (int)( (double)( nFrameWidth ) / dAspect + 0.5 );
  87. if( nFrameHeight > nWholeHeight )
  88. {
  89. nFrameHeight = nWholeHeight;
  90. nFrameWidth = (int)( (double)( nFrameHeight ) * dAspect + 0.5 );
  91. }
  92. int nFrameShiftX = ( nWholeWidth - nFrameWidth ) >> 1;
  93. int nFrameShiftY = ( nWholeHeight - nFrameHeight ) >> 1;
  94. ::SetRect( pFitRect, pWholeRect->left + nFrameShiftX, pWholeRect->top + nFrameShiftY,
  95. pWholeRect->right - nFrameShiftX, pWholeRect->bottom - nFrameShiftY );
  96. }
  97. /*
  98. // D C-D
  99. //p1 ----------------- p2
  100. // \ | /
  101. // \ | /
  102. // B \ | / A
  103. // \ /
  104. // p3
  105. inline BOOL PtInLine( CLgDPoint p1, CLgDPoint p2, CLgDPoint p3, double nQuality )
  106. {
  107. double C = sqrt( pow(p1.x-p2.x,2) + pow(p1.y-p2.y,2) );
  108. double B = sqrt( pow(p1.x-p3.x,2) + pow(p1.y-p3.y,2) );
  109. double A = sqrt( pow(p2.x-p3.x,2) + pow(p2.y-p3.y,2) );
  110. double D = ( pow(B,2) - pow(A,2) + pow(C,2) ) / (2*C);
  111. if( ( fabs(D) - C ) > nQuality )
  112. return FALSE;
  113. double X = p1.x + (p2.x-p1.x) * D / C;
  114. double Y = p1.y + (p2.y-p1.y) * D / C;
  115. BOOL bInX = FALSE;
  116. BOOL bInY = FALSE;
  117. double X1, X2;
  118. if( p1.x < p2.x ) { X1 = p1.x - nQuality; X2 = p2.x + nQuality; }
  119. else { X1 = p2.x - nQuality; X2 = p1.x + nQuality; }
  120. double Y1, Y2;
  121. if( p1.y < p2.y ) { Y1 = p1.y - nQuality; Y2 = p2.y + nQuality; }
  122. else { Y1 = p2.y - nQuality; Y2 = p1.y + nQuality; }
  123. double R = sqrt( pow(p3.x-X,2) + pow(p3.y-Y,2) );
  124. return ( R <= nQuality ) && ( X1 <= X ) && ( X <= X2 ) &&
  125. ( Y1 <= Y ) && ( Y <= Y2 );
  126. }
  127. #ifdef _AFXDLL
  128. inline BOOL PtInLine( CPoint p1, CPoint p2, CPoint p3, int nQuality )
  129. {
  130. CLgDPoint dp1( p1.x, p1.y );
  131. CLgDPoint dp2( p2.x, p2.y );
  132. CLgDPoint dp3( p3.x, p3.y );
  133. return PtInLine( dp1, dp2, dp3, (double)nQuality );
  134. }
  135. #endif
  136. // p22
  137. // /
  138. // p11 /
  139. // \ /
  140. // \ p /
  141. // \ /
  142. // / \
  143. // / \
  144. // / \
  145. // / \
  146. // p21 \
  147. // \
  148. // p12
  149. typedef enum
  150. {
  151. LGCT_ERROR = -4,
  152. LGCT_NOCROSS = -3,
  153. LGCT_PARALLEL = -2,
  154. LGCT_COLINEAR = -1,
  155. LGCT_CROSS = 0,
  156. LGCT_CROSS_P11 = 0x01,
  157. LGCT_CROSS_P12 = 0x02,
  158. LGCT_CROSS_P21 = 0x04,
  159. LGCT_CROSS_P22 = 0x08,
  160. }LG_CROSSTYPE;
  161. inline bool LgDCmpE( double a, double b, double q ) { return fabs( a - b ) < q; }
  162. inline bool LgDCmpL( double a, double b, double q ) { return !LgDCmpE(a,b,q) && a < b; }
  163. inline bool LgDCmpB( double a, double b, double q ) { return !LgDCmpE(a,b,q) && a > b; }
  164. inline LG_CROSSTYPE LgCrossProduct( CLgDPoint &p11, CLgDPoint &p12, CLgDPoint &p21, CLgDPoint &p22, CLgDPoint *pP, BOOL bCheckBounds = TRUE )
  165. {
  166. if( !pP )
  167. return LGCT_ERROR;
  168. double XA = p11.x, XB = p12.x;
  169. double YA = p11.y, YB = p12.y;
  170. double XC = p21.x, XD = p22.x;
  171. double YC = p21.y, YD = p22.y;
  172. double XBA = XB - XA;
  173. double YBA = YB - YA;
  174. double XCD = XC - XD;
  175. double YCD = YC - YD;
  176. double D = XBA * YCD - XCD * YBA;
  177. const double q = 1E-10;
  178. if( LgDCmpE(D,0.0,q) )
  179. {
  180. // D = XA * ( YB - YC ) + XC * ( YA - YB ) + XB * ( YC - YA );
  181. // return ( fabs(D) == 0.0 ) ? LGCT_COLINEAR : LGCT_PARALLEL;
  182. return LGCT_COLINEAR;
  183. }
  184. double XCA = XC - XA;
  185. double YCA = YC - YA;
  186. double T1 = (XCA * YCD - XCD * YCA)/D;
  187. double T2 = (XBA * YCA - YBA * XCA)/D;
  188. if( bCheckBounds && ( LgDCmpL(T1,0,q) || LgDCmpB(T1,1,q) || LgDCmpL(T2,0,q) || LgDCmpB(T2,1,q) ) )
  189. return LGCT_NOCROSS;
  190. pP->x = XA + T1 * XBA;
  191. pP->y = YA + T1 * YBA;
  192. INT nCrossResult = LGCT_CROSS;
  193. if( LgDCmpE(T1,0,q) ) nCrossResult |= LGCT_CROSS_P11;
  194. if( LgDCmpE(T1,1,q) ) nCrossResult |= LGCT_CROSS_P12;
  195. if( LgDCmpE(T2,0,q) ) nCrossResult |= LGCT_CROSS_P21;
  196. if( LgDCmpE(T2,1,q) ) nCrossResult |= LGCT_CROSS_P22;
  197. return (LG_CROSSTYPE)nCrossResult;
  198. }
  199. inline LG_CROSSTYPE LgCrossLines( CLgDPoint &dp11, CLgDPoint &dp12, CLgDPoint &dp21, CLgDPoint &dp22, CLgDPoint *pDP )
  200. {
  201. if( !pDP )
  202. return LGCT_ERROR;
  203. BOOL PA0 = FALSE, PA1 = FALSE;
  204. double A0, A1, B0, B1;
  205. if( dp11.x == dp12.x )
  206. {
  207. PA0 = TRUE;
  208. B0 = dp11.x;
  209. }
  210. else
  211. {
  212. A0 = (dp11.y - dp12.y) / (dp11.x - dp12.x);
  213. B0 = dp11.y - A0 * dp11.x;
  214. }
  215. if( dp21.x == dp22.x )
  216. {
  217. PA1 = TRUE;
  218. B1 = dp21.x;
  219. }
  220. else
  221. {
  222. A1 = (dp21.y - dp22.y) / (dp21.x - dp22.x);
  223. B1 = dp21.y - A1 * dp21.x;
  224. }
  225. if( PA0 )
  226. {
  227. if( PA1 ) return LGCT_COLINEAR;
  228. pDP->x = B0;
  229. pDP->y = A1 * pDP->x + B1;
  230. }
  231. else
  232. {
  233. if( PA1 ) pDP->x = B1;
  234. else
  235. {
  236. if( A1 != A0 ) pDP->x = (B0 - B1) / (A1 - A0);
  237. else return LGCT_COLINEAR;
  238. }
  239. pDP->y = A0 * pDP->x + B0;
  240. }
  241. return LGCT_CROSS;
  242. }
  243. */
  244. inline BOOL LgCreateDirectory( LPCTSTR pFolderName )
  245. {
  246. if( !pFolderName )
  247. return FALSE;
  248. // prepare storage for parsed path
  249. TCHAR pElement[MAX_PATH];
  250. LPTSTR pElementPtr = pElement;
  251. // skip root name (we can't create root)
  252. LPTSTR pPathPtr = (LPTSTR)PathSkipRoot( pFolderName );
  253. if( !pPathPtr )
  254. {
  255. pPathPtr = (LPTSTR)pFolderName;
  256. // return FALSE;
  257. }
  258. // copy root
  259. memcpy( pElement, pFolderName, pPathPtr - pFolderName );
  260. pElementPtr += (pPathPtr - pFolderName);
  261. while( TRUE )
  262. {
  263. if( ( *pPathPtr == _T('\\') && pPathPtr != pFolderName ) || *pPathPtr == _T('\0') )
  264. {
  265. *pElementPtr = 0;
  266. if( !::CreateDirectory( pElement, NULL ) )
  267. {
  268. DWORD dwError = ::GetLastError();
  269. switch( dwError )
  270. {
  271. case ERROR_SUCCESS:
  272. case ERROR_FILE_EXISTS:
  273. case ERROR_ALREADY_EXISTS:
  274. break;
  275. default:
  276. return FALSE;
  277. }
  278. }
  279. }
  280. if( *pPathPtr == _T('\0') )
  281. break;
  282. *pElementPtr++ = *pPathPtr++;
  283. }
  284. return TRUE;
  285. }
  286. #endif