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.

1241 lines
28 KiB

4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. //----------------------------------------------------------------------------
  2. // Laser Guard Ltd.
  3. // Copyright � 2000-2002. All Rights Reserved.
  4. // Version 1.0 01/01/2000
  5. // Last modification: 08/10/2003
  6. // PROJECT:
  7. // SUBSYSTEM:
  8. // FILE: LocatorWnd.cpp
  9. // AUTHOR: Sergey Chizhevskiy
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // Source file of LocatorWnd.cpp
  13. //----------------------------------------------------------------------------
  14. #include "stdafx.h"
  15. #include "LocatorWnd.h"
  16. #include "..\LGLib\Comm\Lgsm.h"
  17. #include "..\LgLib\Version.h"
  18. #define LOCATOR_REGION_D2P 100.0
  19. CLocatorRegion::CLocatorRegion()
  20. {
  21. // clear region handle
  22. m_hRgn = NULL;
  23. // set default color
  24. m_nColor = RGB(0,0,0);
  25. LGLOG( LOG_LEVEL_HIGH, LOG_TYPE_INFO, this, "CLocatorRegion::CLocatorRegion" );
  26. }
  27. CLocatorRegion::CLocatorRegion( const CLocatorRegion& region ) :
  28. CLgArray<CLgDPoint>( (CLgArray<CLgDPoint>&)region )
  29. {
  30. // clear region handle
  31. m_hRgn = NULL;
  32. // copy color data
  33. m_nColor = region.m_nColor;
  34. // update region handle
  35. UpdateRgn();
  36. LGLOG( LOG_LEVEL_HIGH, LOG_TYPE_INFO, this, "CLocatorRegion::CLocatorRegion( const CLocatorRegion& )" );
  37. }
  38. CLocatorRegion::~CLocatorRegion()
  39. {
  40. RemoveAll();
  41. LGLOG( LOG_LEVEL_HIGH, LOG_TYPE_INFO, this, "CLocatorRegion::~CLocatorRegion" );
  42. }
  43. BOOL CLocatorRegion::Add( CLgDPoint& data )
  44. {
  45. BOOL bResult = CLgArray<CLgDPoint>::Add( data );
  46. UpdateRgn();
  47. return bResult;
  48. }
  49. BOOL CLocatorRegion::Remove( INT nIndex )
  50. {
  51. BOOL bResult = CLgArray<CLgDPoint>::Remove( nIndex );
  52. UpdateRgn();
  53. return bResult;
  54. }
  55. BOOL CLocatorRegion::RemoveAll()
  56. {
  57. BOOL bResult = CLgArray<CLgDPoint>::RemoveAll();
  58. UpdateRgn();
  59. return bResult;
  60. }
  61. BOOL CLocatorRegion::Load( HANDLE hFile )
  62. {
  63. RemoveAll();
  64. DWORD nPointCount;
  65. // read point count
  66. DWORD dwRead = 0;
  67. if( !::ReadFile( hFile, &nPointCount, sizeof(nPointCount), &dwRead, NULL ) || sizeof(nPointCount) != dwRead )
  68. {
  69. ::CloseHandle( hFile );
  70. return FALSE;
  71. }
  72. // read points
  73. for( DWORD j = 0; j < nPointCount; j++ )
  74. {
  75. CLgDPoint point;
  76. // read point from file
  77. if( !::ReadFile( hFile, &point, sizeof(point), &dwRead, NULL ) || sizeof(point) != dwRead )
  78. {
  79. ::CloseHandle( hFile );
  80. return FALSE;
  81. }
  82. // add into array
  83. if( !Add( point ) )
  84. {
  85. ::CloseHandle( hFile );
  86. return FALSE;
  87. }
  88. }
  89. // write color information
  90. if( !::ReadFile( hFile, &m_nColor, sizeof(m_nColor), &dwRead, NULL ) || sizeof(m_nColor) != dwRead )
  91. {
  92. ::CloseHandle( hFile );
  93. return FALSE;
  94. }
  95. return TRUE;
  96. }
  97. BOOL CLocatorRegion::Store( HANDLE hFile )
  98. {
  99. INT nPointCount = GetCount();
  100. // write point count
  101. DWORD dwWritten = 0;
  102. if( !::WriteFile( hFile, &nPointCount, sizeof(nPointCount), &dwWritten, NULL ) || sizeof(nPointCount) != dwWritten )
  103. {
  104. ::CloseHandle( hFile );
  105. return FALSE;
  106. }
  107. // write points
  108. for( DWORD i = 0; i < nPointCount; i++ )
  109. {
  110. CLgDPoint &point = GetAt(i);
  111. if( !::WriteFile( hFile, &point, sizeof(point), &dwWritten, NULL ) || sizeof(point) != dwWritten )
  112. {
  113. ::CloseHandle( hFile );
  114. return TRUE;
  115. }
  116. }
  117. // write color information
  118. if( !::WriteFile( hFile, &m_nColor, sizeof(m_nColor), &dwWritten, NULL ) || sizeof(m_nColor) != dwWritten )
  119. {
  120. ::CloseHandle( hFile );
  121. return FALSE;
  122. }
  123. return TRUE;
  124. }
  125. BOOL CLocatorRegion::PtInRegion( CLgDPoint &dp )
  126. {
  127. if( !m_hRgn )
  128. return FALSE;
  129. return ::PtInRegion( m_hRgn, LOCATOR_REGION_D2P * dp.x, LOCATOR_REGION_D2P * dp.y );
  130. }
  131. BOOL CLocatorRegion::UpdateRgn()
  132. {
  133. // delet old region
  134. if( m_hRgn )
  135. {
  136. ::DeleteObject( m_hRgn );
  137. m_hRgn = NULL;
  138. }
  139. // check amount of points
  140. if( !GetCount() )
  141. return TRUE;
  142. // allocate storage for new point
  143. LPPOINT pPoint = new POINT[ GetCount() ];
  144. if( !pPoint )
  145. return FALSE;
  146. for( INT i = 0; i < GetCount(); i++ )
  147. {
  148. pPoint[i].x = LOCATOR_REGION_D2P * GetAt( i ).x;
  149. pPoint[i].y = LOCATOR_REGION_D2P * GetAt( i ).y;
  150. }
  151. m_hRgn = ::CreatePolygonRgn( pPoint, GetCount(), ALTERNATE );
  152. delete pPoint;
  153. return TRUE;
  154. }
  155. /////////////////////////////////////////////////////////////////////////////////////////////////////
  156. //
  157. /////////////////////////////////////////////////////////////////////////////////////////////////////
  158. INT CLocatorRegionArray::PtInRegion( CLgDPoint &dp )
  159. {
  160. for( INT i = 0; i < GetCount(); i++ )
  161. if( GetAt(i).PtInRegion( dp ) )
  162. return i;
  163. return INVALID_INDEX;
  164. }
  165. BOOL CLocatorRegionArray::Load( LPCTSTR pFile )
  166. {
  167. RemoveAll();
  168. // open file
  169. HANDLE hFile = ::CreateFile( pFile, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL );
  170. if( hFile == INVALID_HANDLE_VALUE )
  171. return FALSE;
  172. DWORD nRegionCount;
  173. // read regions count
  174. DWORD dwRead = 0;
  175. if( !::ReadFile( hFile, &nRegionCount, sizeof(nRegionCount), &dwRead, NULL ) || sizeof(nRegionCount) != dwRead )
  176. {
  177. ::CloseHandle( hFile );
  178. return FALSE;
  179. }
  180. // store regions
  181. for( DWORD i = 0; i < nRegionCount; i++ )
  182. {
  183. CLocatorRegion region;
  184. // read points
  185. if( !region.Load( hFile ) )
  186. {
  187. ::CloseHandle( hFile );
  188. return FALSE;
  189. }
  190. // add into regions array
  191. if( !Add( region ) )
  192. {
  193. ::CloseHandle( hFile );
  194. return TRUE;
  195. }
  196. }
  197. ::CloseHandle( hFile );
  198. return TRUE;
  199. }
  200. BOOL CLocatorRegionArray::Store( LPCTSTR pFile )
  201. {
  202. // open file
  203. HANDLE hFile = ::CreateFile( pFile, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, 0, NULL );
  204. if( hFile == INVALID_HANDLE_VALUE )
  205. return FALSE;
  206. DWORD nRegionCount = GetCount();
  207. // write regions count
  208. DWORD dwWritten = 0;
  209. if( !::WriteFile( hFile, &nRegionCount, sizeof(nRegionCount), &dwWritten, NULL ) || sizeof(nRegionCount) != dwWritten )
  210. {
  211. ::CloseHandle( hFile );
  212. return FALSE;
  213. }
  214. // store regions
  215. for( DWORD i = 0; i < nRegionCount; i++ )
  216. {
  217. CLocatorRegion &region = GetAt(i);
  218. // write points
  219. if( !region.Store( hFile ) )
  220. {
  221. ::CloseHandle( hFile );
  222. return FALSE;
  223. }
  224. }
  225. ::CloseHandle( hFile );
  226. return TRUE;
  227. }
  228. int CLocatorRegionArray::DBL_CMP(const double& a, const double& b, const double quality )
  229. {
  230. if(fabs(a - b) <= fabs(quality))
  231. return 0;
  232. if(a > b)
  233. return 1;
  234. return -1;
  235. }
  236. BOOL CLocatorRegionArray::CrossProduct( LPVERTEX ln0, LPVERTEX ln1, LPVERTEX pCross, BOOL fCheck0, BOOL fCheck1)
  237. {
  238. double A0, A1, B0, B1, X, Y;
  239. if(DBL_CMP(ln0[0].x, ln0[1].x) == 0)
  240. {
  241. A0 = DBL_MAX;
  242. B0 = ln0[0].x;
  243. }
  244. else
  245. {
  246. A0 = (ln0[0].y - ln0[1].y) / (ln0[0].x - ln0[1].x);
  247. B0 = ln0[0].y - A0 * ln0[0].x;
  248. }
  249. if(DBL_CMP(ln1[0].x , ln1[1].x) == 0)
  250. {
  251. A1 = DBL_MAX;
  252. B1 = ln1[0].x;
  253. }
  254. else
  255. {
  256. A1 = (ln1[0].y - ln1[1].y) / (ln1[0].x - ln1[1].x);
  257. B1 = ln1[0].y - A1 * ln1[0].x;
  258. }
  259. if(A0 == DBL_MAX)
  260. {
  261. if(A1 == DBL_MAX)
  262. return FALSE;
  263. X = B0;
  264. Y = A1 * X + B1;
  265. }
  266. else
  267. {
  268. if(A1 == DBL_MAX)
  269. X = B1;
  270. else
  271. {
  272. if(DBL_CMP( A1, A0 ) != 0)
  273. X = (B0 - B1) / (A1 - A0);
  274. else
  275. return FALSE;
  276. }
  277. Y = A0 * X + B0;
  278. }
  279. double y = Y, x = X;
  280. if(fCheck0)
  281. {
  282. double x0 = ln0[0].x, x1 = ln0[1].x;
  283. double y0 = ln0[0].y, y1 = ln0[1].y;
  284. if(DBL_CMP(y0, y1) == -1 && (DBL_CMP(y0, y) == 1 || DBL_CMP(y, y1) == 1))
  285. return FALSE;
  286. if(DBL_CMP(y0, y1) == 1 && (DBL_CMP(y0, y) == -1 || DBL_CMP(y, y1) == -1))
  287. return FALSE;
  288. if(DBL_CMP(x0, x1) == -1 && (DBL_CMP(x0, x) == 1 || DBL_CMP(x, x1) == 1))
  289. return FALSE;
  290. if(DBL_CMP(x0, x1) == 1 && (DBL_CMP(x0, x) == -1 || DBL_CMP(x, x1) == -1))
  291. return FALSE;
  292. }
  293. if(fCheck1)
  294. {
  295. double x0 = ln1[0].x, x1 = ln1[1].x;
  296. double y0 = ln1[0].y, y1 = ln1[1].y;
  297. if(DBL_CMP(y0, y1) == -1 && (DBL_CMP(y0, y) == 1 || DBL_CMP(y, y1) == 1))
  298. return FALSE;
  299. if(DBL_CMP(y0, y1) == 1 && (DBL_CMP(y0, y) == -1 || DBL_CMP(y, y1) == -1))
  300. return FALSE;
  301. if(DBL_CMP(x0, x1) == -1 && (DBL_CMP(x0, x) == 1 || DBL_CMP(x, x1) == 1))
  302. return FALSE;
  303. if(DBL_CMP(x0, x1) == 1 && (DBL_CMP(x0, x) == -1 || DBL_CMP(x, x1) == -1))
  304. return FALSE;
  305. }
  306. pCross->x = X;
  307. pCross->y = Y;
  308. return TRUE;
  309. }
  310. BOOL CLocatorRegionArray::PtInLine(LPVERTEX ln, VERTEX& p)
  311. {
  312. double A, B;
  313. if(DBL_CMP(ln[0].x, ln[1].x) == 0)
  314. {
  315. A = DBL_MAX;
  316. B = ln[0].x;
  317. }
  318. else
  319. {
  320. A = (ln[0].y - ln[1].y) / (ln[0].x - ln[1].x);
  321. B = ln[0].y - A * ln[0].x;
  322. }
  323. if(A == DBL_MAX)
  324. {
  325. if( DBL_CMP(p.x, B) == 0)
  326. return TRUE;
  327. }
  328. else
  329. {
  330. if(DBL_CMP(p.x * A + B, p.y) == 0)
  331. return TRUE;
  332. }
  333. return FALSE;
  334. }
  335. int CLocatorRegionArray::comparePoints(const void *arg1, const void *arg2)
  336. {
  337. if(((LPVERTEX)arg1)->r > ((LPVERTEX)arg2)->r)
  338. return +1;
  339. if(((LPVERTEX)arg1)->r < ((LPVERTEX)arg2)->r)
  340. return -1;
  341. return 0;
  342. }
  343. void CLocatorRegionArray::AddPointToSegment(LPSEGMENTS pSegments, INT nIndex, LPVERTEX pPoint)
  344. {
  345. LPVERTEX pNew = new VERTEX[pSegments[nIndex].nCount + 1];
  346. ::CopyMemory(pNew, pSegments[nIndex].pPoints, sizeof(VERTEX) * pSegments[nIndex].nCount);
  347. ::CopyMemory(pNew + pSegments[nIndex].nCount, pPoint, sizeof(VERTEX));
  348. if(pSegments[nIndex].pPoints)
  349. delete pSegments[nIndex].pPoints;
  350. pSegments[nIndex].pPoints = pNew;
  351. pSegments[nIndex].nCount++;
  352. }
  353. void CLocatorRegionArray::StickSegments(LPSEGMENTS pSegments, INT nIndex)
  354. {
  355. if(pSegments[nIndex].nCount)
  356. {
  357. qsort(pSegments[nIndex].pPoints, pSegments[nIndex].nCount, sizeof(VERTEX), comparePoints);
  358. LPVERTEX pTemp = new VERTEX[pSegments[nIndex].nCount];
  359. LPVERTEX pPoints = pSegments[nIndex].pPoints;
  360. int f, nT = 0;
  361. pTemp[nT++] = pPoints[0];
  362. f = pPoints[0].f;
  363. for(int i = 1; i < pSegments[nIndex].nCount;)
  364. {
  365. f += pPoints[i].f;
  366. if(f == 0)
  367. {
  368. pTemp[nT++] = pPoints[i++];
  369. if(i < pSegments[nIndex].nCount)
  370. {
  371. pTemp[nT++] = pPoints[i];
  372. f = pPoints[i++].f;
  373. }
  374. }
  375. else
  376. i++;
  377. }
  378. if(pSegments[nIndex].pPoints)
  379. delete[] pSegments[nIndex].pPoints;
  380. pSegments[nIndex].pPoints = new VERTEX[nT];
  381. ::CopyMemory(pSegments[nIndex].pPoints, pTemp, sizeof(VERTEX) * nT);
  382. pSegments[nIndex].nCount = nT;
  383. if(pTemp)
  384. delete[] pTemp;
  385. }
  386. }
  387. int CLocatorRegionArray::compareSegments(const void *arg1, const void *arg2)
  388. {
  389. if(((LPSEGMENT)arg1)->nIndex > ((LPSEGMENT)arg2)->nIndex)
  390. return +1;
  391. if(((LPSEGMENT)arg1)->nIndex < ((LPSEGMENT)arg2)->nIndex)
  392. return -1;
  393. if(((LPSEGMENT)arg1)->pPoints[0] > ((LPSEGMENT)arg2)->pPoints[0])
  394. return +1;
  395. if(((LPSEGMENT)arg1)->pPoints[0] < ((LPSEGMENT)arg2)->pPoints[0])
  396. return -1;
  397. return 0;
  398. }
  399. BOOL CLocatorRegionArray::ProduceHiddenZones()
  400. {
  401. const int MAX_RAY_COUNT = 360;
  402. const double TWO_PI = 6.2831853071796; // 2 * PI, 18/04/2001
  403. VERTEX pLines[MAX_RAY_COUNT];
  404. SEGMENTS pSegments[MAX_RAY_COUNT >> 1];
  405. double double_range = 2.0;
  406. int ray_count = MAX_RAY_COUNT;
  407. for(int idx = 0; idx < (ray_count >> 1); idx++)
  408. {
  409. double a = TWO_PI * idx / ray_count;
  410. pLines[(idx << 1) + 0 ].x = cos(a) * double_range;
  411. pLines[(idx << 1) + 0 ].y = -sin(a) * double_range;
  412. pLines[(idx << 1) + 1 ].x = -cos(a) * double_range;
  413. pLines[(idx << 1) + 1 ].y = sin(a) * double_range;
  414. pSegments[idx].nCount = 0;
  415. pSegments[idx].pPoints = NULL;
  416. }
  417. for(int i = 0; i < GetCount(); i++)
  418. {
  419. CLocatorRegion& region = GetAt(i);
  420. if(region.GetCount() > 2 && region.GetColor() == 0)
  421. {
  422. LPVERTEX pPoints = new VERTEX[region.GetCount() + 1];
  423. for(int j = 0; j < region.GetCount(); j++)
  424. {
  425. pPoints[j].x = ((double)region[j].x);
  426. pPoints[j].y = ((double)region[j].y);
  427. }
  428. pPoints[region.GetCount()].x = (double)region[0].x;
  429. pPoints[region.GetCount()].y = (double)region[0].y;
  430. for(j = 0; j <= region.GetCount(); j++)
  431. for(int v = 0; v < (ray_count >> 1); v++)
  432. if(PtInLine(pLines + (v << 1), pPoints[j] ) )
  433. {
  434. for(int g = -1; g < 2; g += 2)
  435. for(int f = -1; f < 2; f += 2)
  436. {
  437. CLgDPoint p( pPoints[j].x + f, pPoints[j].y + g );
  438. if(!region.PtInRegion(p))
  439. {
  440. pPoints[j].x += (double)f * 1e-5;
  441. pPoints[j].y += (double)g * 1e-5;
  442. goto NEXT;
  443. }
  444. }
  445. NEXT:;
  446. }
  447. for(j = 0; j < (ray_count >> 1); j ++ )
  448. {
  449. INT nCCount = 0;
  450. VERTEX pCPoints[MAX_RAY_COUNT * 10];
  451. for(int m = 0; m < region.GetCount(); m++)
  452. {
  453. VERTEX cp;
  454. if(CrossProduct(pPoints + m, pLines + (j << 1), &cp))
  455. {
  456. cp.n = m;
  457. cp.r = sqrt(pow(cp.x - pLines[j << 1].x, 2) + pow(cp.y - pLines[j << 1].y, 2));
  458. if(nCCount > 0 &&
  459. DBL_CMP(pCPoints[nCCount-1].x, cp.x) == 0 &&
  460. DBL_CMP(pCPoints[nCCount-1].y, cp.y) == 0)
  461. {
  462. int i1 = (m + 1) % region.GetCount();
  463. int i2 = (m - 1 + region.GetCount()) % region.GetCount();
  464. VERTEX line[] = { { pPoints[i1].x, pPoints[i1].y },
  465. { pPoints[i2].x, pPoints[i2].y } };
  466. LPVERTEX l0 = pLines + (j << 1);
  467. if(!CrossProduct( pLines + (j << 1), line, &cp, FALSE))
  468. pCPoints[nCCount++] = cp;
  469. }
  470. else
  471. pCPoints[nCCount++] = cp;
  472. }
  473. } // end of for(int m = 0; m < pPolygon->GetCount(); m++)
  474. if(nCCount > 0 &&
  475. DBL_CMP( pCPoints[nCCount-1].x, pCPoints[0].x) == 0 &&
  476. DBL_CMP( pCPoints[nCCount-1].y, pCPoints[0].y) == 0)
  477. {
  478. int i1 = (pCPoints[0].n + 1) % region.GetCount();
  479. int i2 = (pCPoints[0].n - 1 + region.GetCount()) % region.GetCount();
  480. VERTEX line[] = { { pPoints[i1].x, pPoints[i1].y },
  481. { pPoints[i2].x, pPoints[i2].y } };
  482. LPVERTEX l0 = pLines + (j << 1);
  483. VERTEX cp;
  484. if(CrossProduct( pLines + (j << 1), line, &cp, FALSE))
  485. nCCount--;
  486. }
  487. if(nCCount % 2 == 0)
  488. {
  489. qsort(pCPoints, nCCount, sizeof(VERTEX), comparePoints);
  490. for(m = 0; m < nCCount; m++)
  491. {
  492. pCPoints[m].f = ( m % 2 ) ? -1 : +1;
  493. AddPointToSegment( pSegments, j, pCPoints + m );
  494. }
  495. }
  496. } // end of for(j = 0; j < (ray_count >> 1); j ++ )
  497. delete[] pPoints; // Added delete[] 22/04/2001
  498. } // end of if(pPolygon->GetCount() > 2 && pPolygon->GetType() == 0)
  499. } // end of for(int i = 0; i < m_nCount; i++)
  500. INT sc = 0;
  501. for(INT j = 0; j < (ray_count >> 1); j ++)
  502. {
  503. StickSegments( pSegments, j );
  504. sc += pSegments[j].nCount;
  505. }
  506. LPSEGMENT pSegment = new SEGMENT[sc];
  507. ::FillMemory(pSegment, sizeof(SEGMENT)*sc, 0xFF);
  508. INT cn = 0;
  509. for(j = 0; j < (ray_count >> 1); j ++)
  510. {
  511. LPVERTEX pPoints = pSegments[j].pPoints;
  512. if(pSegments[j].nCount % 2)
  513. {
  514. // CMessageBox::MessageBox(NULL, "Produced odd number of points", NULL, MB_OK);
  515. }
  516. else
  517. {
  518. // convert zero shift to radians
  519. for(int m = 0; m < pSegments[j].nCount; m += 2)
  520. {
  521. pPoints[m].r -= double_range;//*scale;
  522. pPoints[m+1].r -= double_range;//*scale;
  523. if(pPoints[m].r < 0.0 && pPoints[m+1].r > 0.0)
  524. {
  525. pSegment[cn].nIndex = j;
  526. pSegment[cn].pPoints[0] = 0;
  527. pSegment[cn++].pPoints[1] = (INT)(-pPoints[m].r);
  528. pSegment[cn].nIndex = j + (ray_count >> 1);
  529. pSegment[cn].pPoints[0] = 0;
  530. pSegment[cn++].pPoints[1] = (INT)(pPoints[m + 1].r);
  531. }
  532. else
  533. {
  534. if(pPoints[m].r < 0 || pPoints[m+1].r < 0)
  535. {
  536. if(pPoints[m].r > pPoints[m+1].r)
  537. {
  538. pSegment[cn].nIndex = j;
  539. pSegment[cn].pPoints[0] = (INT)(-pPoints[m].r);
  540. pSegment[cn++].pPoints[1] = (INT)(-pPoints[m + 1].r);
  541. }
  542. else
  543. {
  544. pSegment[cn].nIndex = j;
  545. pSegment[cn].pPoints[0] = (INT)(-pPoints[m + 1].r);
  546. pSegment[cn++].pPoints[1] = (INT)(-pPoints[m].r);
  547. }
  548. }
  549. else
  550. if(pPoints[m].r < pPoints[m+1].r)
  551. {
  552. pSegment[cn].nIndex = j + (ray_count >> 1);
  553. pSegment[cn].pPoints[0] = (INT)(pPoints[m].r);
  554. pSegment[cn++].pPoints[1] = (INT)(pPoints[m + 1].r);
  555. }
  556. else
  557. {
  558. pSegment[cn].nIndex = j + (ray_count >> 1);
  559. pSegment[cn].pPoints[0] = (INT)(pPoints[m + 1].r);
  560. pSegment[cn++].pPoints[1] = (INT)(pPoints[m].r);
  561. }
  562. } // end of else
  563. } // end of for(int m = 0; m < pSegments[j].nCount; m += 2)
  564. } // end of else
  565. delete [] pPoints;
  566. pSegments[j].pPoints = NULL;
  567. } // end of for(j = 0; j < (ray_count >> 1); j ++)
  568. qsort(pSegment, sc, sizeof(SEGMENT), compareSegments);
  569. FILE *file = fopen("Hidden.txt", "wt");
  570. if( file )
  571. {
  572. for(j = 0; j < sc; j++)
  573. if(pSegment[j].nIndex != 0xFFFFFFFF)
  574. {
  575. INT nMin = pSegment[j].pPoints[0];
  576. INT nMax = pSegment[j].pPoints[1];
  577. if( nMin <= LGS_MAX_DETECTOR_RANGE )
  578. {
  579. if( nMax > LGS_MAX_DETECTOR_RANGE )
  580. nMax = LGS_MAX_DETECTOR_RANGE;
  581. fprintf(file, "%d %d %d\n", pSegment[j].nIndex, nMin, nMax);
  582. }
  583. // fprintf(file, "%d %d %d\n", pSegment[j].nIndex, pSegment[j].pPoints[0], pSegment[j].pPoints[1]);
  584. }
  585. fclose(file);
  586. }
  587. delete[] pSegment;
  588. return TRUE;
  589. }
  590. /////////////////////////////////////////////////////////////////////////////////////////////////////
  591. //
  592. /////////////////////////////////////////////////////////////////////////////////////////////////////
  593. CLocatorWnd::CLocatorWnd()
  594. {
  595. m_bEditMode = FALSE;
  596. m_eEditMode = EDIT_MODE_POINT;
  597. m_bEditStart = FALSE;
  598. m_hMemDC = NULL;
  599. m_hMemBitmap = NULL;
  600. m_hBackgroundDC = NULL;
  601. m_hBackgroundBitmap = NULL;
  602. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CLocatorWnd::CLocatorWnd" );
  603. }
  604. CLocatorWnd::~CLocatorWnd()
  605. {
  606. DestroyWindow();
  607. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CLocatorWnd::~CLocatorWnd" );
  608. }
  609. BOOL CLocatorWnd::Create( HWND hParentWnd, INT x, INT y, INT cx, INT cy )
  610. {
  611. HINSTANCE hInstance = (HINSTANCE)GetWindowLong( GWL_HINSTANCE );
  612. LPCTSTR pClassName = _T("LaserGuardLocatorWindowClass");
  613. // register window class
  614. if( !GetClassInfoEx( hInstance, pClassName, NULL ) )
  615. {
  616. if( !RegisterClass( pClassName, hInstance ) )
  617. return FALSE;
  618. }
  619. // create memory DC
  620. if( !m_hMemDC )
  621. {
  622. HDC hDC = ::GetDC( ::GetDesktopWindow() );
  623. if( hDC )
  624. {
  625. m_hMemDC = ::CreateCompatibleDC( hDC );
  626. ::ReleaseDC( ::GetDesktopWindow(), hDC );
  627. }
  628. }
  629. // create back DC
  630. if( !m_hBackgroundDC )
  631. {
  632. HDC hDC = ::GetDC( ::GetDesktopWindow() );
  633. if( hDC )
  634. {
  635. m_hBackgroundDC = ::CreateCompatibleDC( hDC );
  636. ::ReleaseDC( ::GetDesktopWindow(), hDC );
  637. }
  638. }
  639. // create window
  640. if( !CGenericWnd::Create( pClassName, NULL, WS_CHILD | WS_VISIBLE, x, y, cx, cy, hParentWnd, NULL, hInstance ) )
  641. return FALSE;
  642. // show window
  643. ShowWindow( SW_SHOW );
  644. UpdateWindow();
  645. return TRUE;
  646. }
  647. BOOL CLocatorWnd::ClearRegions()
  648. {
  649. // remove region from array
  650. m_RegionArray.RemoveAll();
  651. // redraw editor
  652. InvalidateRect( NULL, FALSE );
  653. return TRUE;
  654. }
  655. void CLocatorWnd::SetBackground( HBITMAP hBitmap, BOOL bCopy )
  656. {
  657. // delete previus bitmap
  658. if( m_hBackgroundBitmap )
  659. {
  660. ::DeleteObject( m_hBackgroundBitmap );
  661. m_hBackgroundBitmap = NULL;
  662. }
  663. // take copy or original image
  664. m_hBackgroundBitmap = bCopy ? (HBITMAP)::CopyImage( hBitmap, IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION ) : hBitmap;
  665. // redraw according to new background
  666. InvalidateRect( NULL, FALSE );
  667. }
  668. void CLocatorWnd::OnPaint( HDC hDC )
  669. {
  670. HBITMAP hBitmap = (HBITMAP)::SelectObject( m_hMemDC, m_hMemBitmap );
  671. // perfrom drawing
  672. OnDraw( m_hMemDC );
  673. // get client rectangle
  674. RECT rect;
  675. GetClientRect( &rect );
  676. // copy from memory to dc
  677. ::BitBlt( hDC, 0, 0, rect.right, rect.bottom, m_hMemDC, 0, 0, SRCCOPY );
  678. // deselect bitmap in the context
  679. ::SelectObject( m_hMemDC, hBitmap );
  680. }
  681. void CLocatorWnd::OnDraw( HDC hDC )
  682. {
  683. // get client rectangle
  684. RECT rect;
  685. GetClientRect( &rect );
  686. // draw background
  687. if( m_hBackgroundBitmap )
  688. {
  689. DIBSECTION DibSection;
  690. if( GetObject( m_hBackgroundBitmap, sizeof(DibSection), &DibSection ) )
  691. {
  692. HBITMAP hBitmap = (HBITMAP)::SelectObject( m_hBackgroundDC, m_hBackgroundBitmap );
  693. // copy from memory to dc
  694. ::StretchBlt( hDC, 0, 0, rect.right, rect.bottom,
  695. m_hBackgroundDC, 0, 0, DibSection.dsBm.bmWidth, DibSection.dsBm.bmHeight, SRCCOPY );
  696. ::SelectObject( m_hBackgroundDC, hBitmap );
  697. }
  698. }
  699. else
  700. {
  701. ::Ellipse( hDC, rect.left, rect.top, rect.right, rect.bottom );
  702. }
  703. // draw regions
  704. for( INT i = 0; i < m_RegionArray.GetCount(); i++ )
  705. {
  706. CLocatorRegion& region = m_RegionArray[i];
  707. // draw edit points
  708. LPPOINT pPoint = (LPPOINT)malloc( sizeof(POINT)* region.GetCount() );
  709. if( !pPoint )
  710. return;
  711. // fill point array
  712. for( INT j = 0; j < region.GetCount(); j++ )
  713. {
  714. CLgDPoint& dp = region[j];
  715. pPoint[j] = LocatorToClient( dp );
  716. }
  717. // create pen & brush
  718. HPEN hPen = ::CreatePen( PS_SOLID, 1, region.GetColor() );
  719. HPEN hPenOld = (HPEN)::SelectObject( hDC, hPen );
  720. HBRUSH hBrush = ::CreateHatchBrush( HS_DIAGCROSS, region.GetColor() );
  721. HBRUSH hBrushOld = (HBRUSH)::SelectObject( hDC, hBrush );
  722. // change fill mode to transparent
  723. INT nBkMode = ::SetBkMode( hDC, TRANSPARENT );
  724. // draw polygon
  725. Polygon( hDC, pPoint, region.GetCount() );
  726. // change fill mode back
  727. ::SetBkMode( hDC, nBkMode );
  728. // delete pen & brush
  729. ::SelectObject( hDC, hBrushOld );
  730. ::DeleteObject( hBrush );
  731. ::SelectObject( hDC, hPenOld );
  732. ::DeleteObject( hPen );
  733. // delete point array
  734. free( pPoint );
  735. }
  736. // draw editing region
  737. if( m_bEditMode )
  738. {
  739. // draw edit points
  740. LPPOINT pPoint = (LPPOINT)malloc( sizeof(POINT)* m_EditRegion.GetCount() );
  741. if( !pPoint )
  742. return;
  743. // fill point array
  744. for( INT j = 0; j < m_EditRegion.GetCount(); j++ )
  745. {
  746. CLgDPoint& dp = m_EditRegion[j];
  747. pPoint[j] = LocatorToClient( dp );
  748. }
  749. // create pen
  750. HPEN hPen = ::CreatePen( PS_SOLID, 3, m_EditRegion.GetColor() );
  751. HPEN hPenOld = (HPEN)::SelectObject( hDC, hPen );
  752. // draw polygon
  753. ::Polyline( hDC, pPoint, m_EditRegion.GetCount() );
  754. // draw small ellipces
  755. if( m_eEditMode == EDIT_MODE_LINE )
  756. {
  757. for( j = 0; j < m_EditRegion.GetCount(); j++ )
  758. {
  759. ::Ellipse( hDC, pPoint[j].x - 1, pPoint[j].y - 1,
  760. pPoint[j].x + 1, pPoint[j].y + 1 );
  761. }
  762. }
  763. // delete pen
  764. ::SelectObject( hDC, hPenOld );
  765. ::DeleteObject( hPen );
  766. // delete point array
  767. free( pPoint );
  768. }
  769. }
  770. LRESULT CLocatorWnd::OnDestroy( WPARAM wParam, LPARAM lParam )
  771. {
  772. // delete memory device context
  773. if( m_hMemDC )
  774. {
  775. ::DeleteDC( m_hMemDC );
  776. m_hMemDC = NULL;
  777. }
  778. // delete memory device context
  779. if( m_hMemBitmap )
  780. {
  781. ::DeleteObject( m_hMemBitmap );
  782. m_hMemBitmap = NULL;
  783. }
  784. // delete background device context
  785. if( m_hBackgroundDC )
  786. {
  787. ::DeleteDC( m_hBackgroundDC );
  788. m_hBackgroundDC = NULL;
  789. }
  790. // delete background device context
  791. if( m_hBackgroundBitmap )
  792. {
  793. ::DeleteObject( m_hBackgroundBitmap );
  794. m_hBackgroundBitmap = NULL;
  795. }
  796. return CGenericWnd::OnDestroy( wParam, lParam );
  797. }
  798. LRESULT CLocatorWnd::OnSize( WPARAM wParam, LPARAM lParam )
  799. {
  800. SIZE size = { (INT)(SHORT)LOWORD( lParam ), (INT)(SHORT)HIWORD( lParam ) };
  801. BOOL bCreateNew = !m_hMemBitmap;
  802. // check bitmaps sizes
  803. if( m_hMemBitmap )
  804. {
  805. DIBSECTION DibSection;
  806. if( GetObject( m_hMemBitmap, sizeof(DibSection), &DibSection ) )
  807. {
  808. if( DibSection.dsBm.bmWidth != size.cx || DibSection.dsBm.bmHeight != size.cy )
  809. bCreateNew = TRUE;
  810. }
  811. }
  812. // create new bitmap
  813. if( bCreateNew )
  814. {
  815. // delete old bitmap
  816. if( m_hMemBitmap )
  817. {
  818. ::DeleteObject( m_hMemBitmap );
  819. m_hMemBitmap = NULL;
  820. }
  821. // create new one
  822. HDC hDC = GetDC();
  823. if( hDC )
  824. {
  825. m_hMemBitmap = ::CreateCompatibleBitmap( hDC, size.cx, size.cy );
  826. ReleaseDC( hDC );
  827. }
  828. }
  829. return CGenericWnd::OnLButtonDown( wParam, lParam );
  830. }
  831. void CLocatorWnd::SetEditMode( BOOL bEditMode, COLORREF nEditColor, EDIT_MODE eEditMode )
  832. {
  833. m_bEditMode = bEditMode;
  834. m_eEditMode = eEditMode;
  835. m_EditRegion.SetColor( nEditColor );
  836. // set cursor shape
  837. SetClassLong( GetSafeHwnd(), GCL_HCURSOR, (LONG)::LoadCursor( NULL, bEditMode ? IDC_CROSS : IDC_ARROW ) );
  838. }
  839. BOOL CLocatorWnd::GetEditMode( COLORREF *pEditColor )
  840. {
  841. if( *pEditColor )
  842. *pEditColor = m_EditRegion.GetColor();
  843. return m_bEditMode;
  844. }
  845. POINT CLocatorWnd::LocatorToClient( CLgDPoint& dp )
  846. {
  847. // get sizes
  848. RECT rect;
  849. GetClientRect( &rect );
  850. SIZE s = { rect.right, rect.bottom };
  851. POINT p = { s.cx * ( 1.0 + dp.x ) / 2.0,
  852. s.cy * ( 1.0 + dp.y ) / 2.0 };
  853. return p;
  854. }
  855. CLgDPoint CLocatorWnd::ClientToLocator( POINT& p )
  856. {
  857. // get sizes
  858. RECT rect;
  859. GetClientRect( &rect );
  860. SIZE s = { rect.right, rect.bottom };
  861. return CLgDPoint( p.x * 2.0 / s.cx - 1.0,
  862. p.y * 2.0 / s.cy - 1.0 );
  863. }
  864. LRESULT CLocatorWnd::OnLButtonDown( WPARAM wParam, LPARAM lParam )
  865. {
  866. POINT point = { (INT)(SHORT)LOWORD( lParam ), (INT)(SHORT)HIWORD( lParam ) };
  867. // if window in edit mode && mouse was previosly pressed
  868. if( m_bEditMode )
  869. {
  870. switch( m_eEditMode )
  871. {
  872. case EDIT_MODE_POINT:
  873. // mark that mouse cuptured
  874. m_bEditStart = TRUE;
  875. // clear array
  876. m_EditRegion.RemoveAll();
  877. // capture mouse control
  878. ::SetCapture( GetSafeHwnd() );
  879. break;
  880. case EDIT_MODE_LINE:
  881. if( m_bEditStart )
  882. {
  883. // translate into client coordinates
  884. POINT fpoint = LocatorToClient( m_EditRegion[0] );
  885. if( abs( fpoint.x - point.x ) <= 2 &&
  886. abs( fpoint.y - point.y ) <= 2 )
  887. {
  888. // mark that draw finished
  889. m_bEditStart = FALSE;
  890. // plase region into region list
  891. m_RegionArray.Add( m_EditRegion );
  892. // empty region
  893. m_EditRegion.RemoveAll();
  894. // release mouse control
  895. ReleaseCapture();
  896. }
  897. else
  898. {
  899. CLgDPoint dp = ClientToLocator( point );
  900. // place point into array
  901. m_EditRegion.Add( dp );
  902. }
  903. }
  904. else
  905. {
  906. // mark that draw started
  907. m_bEditStart = TRUE;
  908. // add point into array
  909. CLgDPoint dp = ClientToLocator( point );
  910. // place point into array
  911. m_EditRegion.Add( dp );
  912. // capture mouse control
  913. ::SetCapture( GetSafeHwnd() );
  914. }
  915. // redraw editor
  916. InvalidateRect( NULL, FALSE );
  917. break;
  918. }
  919. }
  920. return CGenericWnd::OnLButtonDown( wParam, lParam );
  921. }
  922. LRESULT CLocatorWnd::OnMouseMove( WPARAM wParam, LPARAM lParam )
  923. {
  924. POINT point = { (INT)(SHORT)LOWORD( lParam ), (INT)(SHORT)HIWORD( lParam ) };
  925. // if window in edit mode && mouse was previosly pressed
  926. if( m_bEditMode )
  927. {
  928. switch( m_eEditMode )
  929. {
  930. case EDIT_MODE_POINT:
  931. if( m_bEditStart )
  932. {
  933. CLgDPoint dp = ClientToLocator( point );
  934. // place point into array
  935. m_EditRegion.Add( dp );
  936. // redraw editor
  937. InvalidateRect( NULL, FALSE );
  938. }
  939. break;
  940. case EDIT_MODE_LINE:
  941. // to do nothing
  942. break;
  943. }
  944. }
  945. return CGenericWnd::OnMouseMove( wParam, lParam );
  946. }
  947. LRESULT CLocatorWnd::OnLButtonUp( WPARAM wParam, LPARAM lParam )
  948. {
  949. POINT point = { (INT)(SHORT)LOWORD( lParam ), (INT)(SHORT)HIWORD( lParam ) };
  950. // if window in edit mode && mouse was previosly pressed
  951. if( m_bEditMode )
  952. {
  953. switch( m_eEditMode )
  954. {
  955. case EDIT_MODE_POINT:
  956. if( m_bEditStart )
  957. {
  958. m_bEditStart = FALSE;
  959. // plase region into region list
  960. m_RegionArray.Add( m_EditRegion );
  961. // empty region
  962. m_EditRegion.RemoveAll();
  963. // relase mouse control
  964. ReleaseCapture();
  965. // redraw editor
  966. InvalidateRect( NULL, FALSE );
  967. }
  968. break;
  969. case EDIT_MODE_LINE:
  970. // to do nothing
  971. break;
  972. }
  973. }
  974. return CGenericWnd::OnLButtonUp( wParam, lParam );
  975. }
  976. LRESULT CLocatorWnd::OnRButtonUp( WPARAM wParam, LPARAM lParam )
  977. {
  978. POINT point =
  979. {
  980. (INT)(SHORT)LOWORD( lParam ),
  981. (INT)(SHORT)HIWORD( lParam )
  982. };
  983. // if window in edit mode && mouse was previosly pressed
  984. if( m_bEditMode )
  985. {
  986. CLgDPoint dp = ClientToLocator( point );
  987. // find region
  988. INT nIndex = m_RegionArray.PtInRegion( dp );
  989. // remove region from array
  990. m_RegionArray.Remove( nIndex );
  991. // redraw editor
  992. InvalidateRect( NULL, FALSE );
  993. }
  994. return CGenericWnd::OnRButtonUp( wParam, lParam );
  995. }