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.

849 lines
20 KiB

4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. //----------------------------------------------------------------------------
  2. // Laser Guard Ltd.
  3. // Copyright � 2000-2002. All Rights Reserved.
  4. // Version 1.0 11/05/2000
  5. // Last modification: 08/10/2003
  6. // Project Intruder Position Indicator
  7. // SUBSYSTEM: Polygon
  8. // FILE: Polygon.cpp
  9. // AUTHOR: Chizhevskiy Sergey
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // Source file of class CPolygon
  13. //----------------------------------------------------------------------------
  14. #include "stdafx.h"
  15. #include "..\LgLib\Version.h"
  16. #include "..\LgLib\Comm\LGSm.h"
  17. #include "Polygon.h"
  18. struct VERTEX
  19. {
  20. double x, y, r;
  21. BOOL f;
  22. int n;
  23. };
  24. typedef VERTEX* LPVERTEX;
  25. struct SEGMENT
  26. {
  27. INT pPoints[2];
  28. INT nIndex;
  29. };
  30. typedef SEGMENT* LPSEGMENT;
  31. struct SEGMENTS
  32. {
  33. LPVERTEX pPoints;
  34. INT nCount;
  35. };
  36. typedef SEGMENTS* LPSEGMENTS;
  37. CPolygon::CPolygon(INT type)
  38. {
  39. m_hRgn = NULL;
  40. m_pPoints = NULL;
  41. m_nCount = 0;
  42. m_pPointsDraw = NULL;
  43. m_Type = type;
  44. m_fVisible = TRUE;
  45. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CPolygon::CPolygon(INT)" );
  46. }
  47. CPolygon::CPolygon(HANDLE hFile)
  48. {
  49. m_hRgn = NULL;
  50. m_pPoints = NULL;
  51. m_nCount = 0;
  52. m_Type = 0;
  53. m_fVisible = TRUE;
  54. Load(hFile);
  55. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CPolygon::CPolygon(HANDLE)" );
  56. }
  57. CPolygon::~CPolygon()
  58. {
  59. Empty();
  60. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CPolygon::~CPolygon" );
  61. }
  62. void CPolygon::Empty()
  63. {
  64. if(m_pPoints)
  65. {
  66. delete[] m_pPoints;
  67. delete[] m_pPointsDraw;
  68. }
  69. if(m_hRgn)
  70. ::DeleteObject(m_hRgn);
  71. m_hRgn = NULL;
  72. m_pPoints = NULL;
  73. m_nCount = 0;
  74. }
  75. BOOL CPolygon::Save(HANDLE hFile)
  76. {
  77. DWORD dwWritten = 0UL;
  78. ::WriteFile(hFile, &m_Type, sizeof(m_Type), &dwWritten, NULL);
  79. ::WriteFile(hFile, &m_nCount, sizeof(m_nCount), &dwWritten, NULL);
  80. ::WriteFile(hFile, m_pPoints, m_nCount * sizeof(POINT), &dwWritten, NULL);
  81. return 0;
  82. }
  83. BOOL CPolygon::Load(HANDLE hFile)
  84. {
  85. DWORD dwReaded = 0UL;
  86. Empty();
  87. ::ReadFile(hFile, &m_Type, sizeof(m_Type), &dwReaded, NULL);
  88. ::ReadFile(hFile, &m_nCount, sizeof(m_nCount), &dwReaded, NULL);
  89. m_pPoints = new POINT[m_nCount];
  90. m_pPointsDraw = new POINT[m_nCount];
  91. ::ReadFile(hFile, m_pPoints, m_nCount * sizeof(POINT), &dwReaded, NULL);
  92. m_hRgn = ::CreatePolygonRgn(m_pPoints, m_nCount, WINDING);
  93. return 0;
  94. }
  95. BOOL CPolygon::AddPoint(LPPOINT pPoint, const double& diff)
  96. {
  97. BOOL bEndPoint = FALSE;
  98. if(!m_hRgn)
  99. {
  100. if(m_nCount)
  101. {
  102. if((m_pPoints->x + diff >= pPoint->x) && (m_pPoints->x - diff <= pPoint->x) &&
  103. (m_pPoints->y + diff >= pPoint->y) && (m_pPoints->y - diff <= pPoint->y))
  104. {
  105. m_hRgn = ::CreatePolygonRgn( m_pPoints, m_nCount, ALTERNATE );
  106. return TRUE;
  107. }
  108. }
  109. LPPOINT pPoints = new POINT[m_nCount + 1];
  110. LPPOINT pPointsDraw = new POINT[m_nCount + 1];
  111. if(m_pPoints)
  112. {
  113. memcpy(pPoints, m_pPoints, sizeof(POINT) * m_nCount);
  114. delete[] m_pPoints;
  115. delete[] m_pPointsDraw;
  116. }
  117. m_pPoints = pPoints;
  118. m_pPointsDraw = pPointsDraw;
  119. m_pPoints[m_nCount++] = *pPoint;
  120. }
  121. return FALSE;
  122. }
  123. int DBL_CMP(const double& a, const double& b, const double quality = 1e-10)
  124. {
  125. if(fabs(a - b) <= fabs(quality))
  126. return 0;
  127. if(a > b)
  128. return 1;
  129. return -1;
  130. }
  131. BOOL CrossProduct( LPVERTEX ln0, LPVERTEX ln1, LPVERTEX pCross, BOOL fCheck0 = TRUE, BOOL fCheck1 = TRUE )
  132. {
  133. double A0, A1, B0, B1, X, Y;
  134. if(DBL_CMP(ln0[0].x, ln0[1].x) == 0)
  135. {
  136. A0 = DBL_MAX;
  137. B0 = ln0[0].x;
  138. }
  139. else
  140. {
  141. A0 = (ln0[0].y - ln0[1].y) / (ln0[0].x - ln0[1].x);
  142. B0 = ln0[0].y - A0 * ln0[0].x;
  143. }
  144. if(DBL_CMP(ln1[0].x , ln1[1].x) == 0)
  145. {
  146. A1 = DBL_MAX;
  147. B1 = ln1[0].x;
  148. }
  149. else
  150. {
  151. A1 = (ln1[0].y - ln1[1].y) / (ln1[0].x - ln1[1].x);
  152. B1 = ln1[0].y - A1 * ln1[0].x;
  153. }
  154. if(A0 == DBL_MAX)
  155. {
  156. if(A1 == DBL_MAX)
  157. return FALSE;
  158. X = B0;
  159. Y = A1 * X + B1;
  160. }
  161. else
  162. {
  163. if(A1 == DBL_MAX)
  164. X = B1;
  165. else
  166. {
  167. if(DBL_CMP( A1, A0 ) != 0)
  168. X = (B0 - B1) / (A1 - A0);
  169. else
  170. return FALSE;
  171. }
  172. Y = A0 * X + B0;
  173. }
  174. double y = Y, x = X;
  175. if(fCheck0)
  176. {
  177. double x0 = ln0[0].x, x1 = ln0[1].x;
  178. double y0 = ln0[0].y, y1 = ln0[1].y;
  179. if(DBL_CMP(y0, y1) == -1 && (DBL_CMP(y0, y) == 1 || DBL_CMP(y, y1) == 1))
  180. return FALSE;
  181. if(DBL_CMP(y0, y1) == 1 && (DBL_CMP(y0, y) == -1 || DBL_CMP(y, y1) == -1))
  182. return FALSE;
  183. if(DBL_CMP(x0, x1) == -1 && (DBL_CMP(x0, x) == 1 || DBL_CMP(x, x1) == 1))
  184. return FALSE;
  185. if(DBL_CMP(x0, x1) == 1 && (DBL_CMP(x0, x) == -1 || DBL_CMP(x, x1) == -1))
  186. return FALSE;
  187. }
  188. if(fCheck1)
  189. {
  190. double x0 = ln1[0].x, x1 = ln1[1].x;
  191. double y0 = ln1[0].y, y1 = ln1[1].y;
  192. if(DBL_CMP(y0, y1) == -1 && (DBL_CMP(y0, y) == 1 || DBL_CMP(y, y1) == 1))
  193. return FALSE;
  194. if(DBL_CMP(y0, y1) == 1 && (DBL_CMP(y0, y) == -1 || DBL_CMP(y, y1) == -1))
  195. return FALSE;
  196. if(DBL_CMP(x0, x1) == -1 && (DBL_CMP(x0, x) == 1 || DBL_CMP(x, x1) == 1))
  197. return FALSE;
  198. if(DBL_CMP(x0, x1) == 1 && (DBL_CMP(x0, x) == -1 || DBL_CMP(x, x1) == -1))
  199. return FALSE;
  200. }
  201. pCross->x = X;
  202. pCross->y = Y;
  203. return TRUE;
  204. }
  205. BOOL PtInLine(LPVERTEX ln, VERTEX& p)
  206. {
  207. double A, B;
  208. if(DBL_CMP(ln[0].x, ln[1].x) == 0)
  209. {
  210. A = DBL_MAX;
  211. B = ln[0].x;
  212. }
  213. else
  214. {
  215. A = (ln[0].y - ln[1].y) / (ln[0].x - ln[1].x);
  216. B = ln[0].y - A * ln[0].x;
  217. }
  218. if(A == DBL_MAX)
  219. {
  220. if( DBL_CMP(p.x, B) == 0)
  221. return TRUE;
  222. }
  223. else
  224. {
  225. if(DBL_CMP(p.x * A + B, p.y) == 0)
  226. return TRUE;
  227. }
  228. return FALSE;
  229. }
  230. int comparePoints(const void *arg1, const void *arg2)
  231. {
  232. if(((LPVERTEX)arg1)->r > ((LPVERTEX)arg2)->r)
  233. return +1;
  234. if(((LPVERTEX)arg1)->r < ((LPVERTEX)arg2)->r)
  235. return -1;
  236. return 0;
  237. }
  238. void AddPointToSegment(LPSEGMENTS pSegments, INT nIndex, LPVERTEX pPoint)
  239. {
  240. LPVERTEX pNew = new VERTEX[pSegments[nIndex].nCount + 1];
  241. ::CopyMemory(pNew, pSegments[nIndex].pPoints, sizeof(VERTEX) * pSegments[nIndex].nCount);
  242. ::CopyMemory(pNew + pSegments[nIndex].nCount, pPoint, sizeof(VERTEX));
  243. if(pSegments[nIndex].pPoints)
  244. delete pSegments[nIndex].pPoints;
  245. pSegments[nIndex].pPoints = pNew;
  246. pSegments[nIndex].nCount++;
  247. }
  248. void StickSegments(LPSEGMENTS pSegments, INT nIndex)
  249. {
  250. if(pSegments[nIndex].nCount)
  251. {
  252. qsort(pSegments[nIndex].pPoints, pSegments[nIndex].nCount, sizeof(VERTEX), comparePoints);
  253. LPVERTEX pTemp = new VERTEX[pSegments[nIndex].nCount];
  254. LPVERTEX pPoints = pSegments[nIndex].pPoints;
  255. int f, nT = 0;
  256. pTemp[nT++] = pPoints[0];
  257. f = pPoints[0].f;
  258. for(int i = 1; i < pSegments[nIndex].nCount;)
  259. {
  260. f += pPoints[i].f;
  261. if(f == 0)
  262. {
  263. pTemp[nT++] = pPoints[i++];
  264. if(i < pSegments[nIndex].nCount)
  265. {
  266. pTemp[nT++] = pPoints[i];
  267. f = pPoints[i++].f;
  268. }
  269. }
  270. else
  271. i++;
  272. }
  273. if(pSegments[nIndex].pPoints)
  274. delete[] pSegments[nIndex].pPoints;
  275. pSegments[nIndex].pPoints = new VERTEX[nT];
  276. ::CopyMemory(pSegments[nIndex].pPoints, pTemp, sizeof(VERTEX) * nT);
  277. pSegments[nIndex].nCount = nT;
  278. if(pTemp)
  279. delete[] pTemp;
  280. }
  281. }
  282. int compareSegments(const void *arg1, const void *arg2)
  283. {
  284. if(((LPSEGMENT)arg1)->nIndex > ((LPSEGMENT)arg2)->nIndex)
  285. return +1;
  286. if(((LPSEGMENT)arg1)->nIndex < ((LPSEGMENT)arg2)->nIndex)
  287. return -1;
  288. if(((LPSEGMENT)arg1)->pPoints[0] > ((LPSEGMENT)arg2)->pPoints[0])
  289. return +1;
  290. if(((LPSEGMENT)arg1)->pPoints[0] < ((LPSEGMENT)arg2)->pPoints[0])
  291. return -1;
  292. return 0;
  293. }
  294. void CPolygons::ProduceHiddenZones(HDC hDC, POINT& center, double range,
  295. const double& scale, double dZeroShift )
  296. {
  297. const int MAX_RAY_COUNT = 360;
  298. const double TWO_PI = 6.2831853071796; // 2 * PI, 18/04/2001
  299. VERTEX pLines[MAX_RAY_COUNT];
  300. SEGMENTS pSegments[MAX_RAY_COUNT >> 1];
  301. double double_range = range * 2;
  302. for(int idx = 0; idx < (m_nRayCount >> 1); idx++)
  303. {
  304. double a = TWO_PI * idx / m_nRayCount;
  305. pLines[(idx << 1) + 0 ].x = cos(a) * double_range;
  306. pLines[(idx << 1) + 0 ].y = -sin(a) * double_range;
  307. pLines[(idx << 1) + 1 ].x = -cos(a) * double_range;
  308. pLines[(idx << 1) + 1 ].y = sin(a) * double_range;
  309. pSegments[idx].nCount = 0;
  310. pSegments[idx].pPoints = NULL;
  311. }
  312. for(int i = 0; i < m_nCount; i++)
  313. {
  314. CPolygon *pPolygon = m_pPolygons[i];
  315. if(pPolygon->GetCount() > 2/* && pPolygon->GetType() == 0*/)
  316. {
  317. LPVERTEX pPoints = new VERTEX[pPolygon->GetCount() + 1];
  318. for(int j = 0; j < pPolygon->GetCount(); j++)
  319. {
  320. pPoints[j].x = ((double)pPolygon->GetPoints()[j].x);
  321. pPoints[j].y = ((double)pPolygon->GetPoints()[j].y);
  322. }
  323. pPoints[pPolygon->GetCount()].x = ((double)pPolygon->GetPoints()[0].x);
  324. pPoints[pPolygon->GetCount()].y = ((double)pPolygon->GetPoints()[0].y);
  325. for(j = 0; j <= pPolygon->GetCount(); j++)
  326. for(int v = 0; v < (m_nRayCount >> 1); v++)
  327. if(PtInLine(pLines + (v << 1), pPoints[j] ) )
  328. {
  329. for(int g = -1; g < 2; g += 2)
  330. for(int f = -1; f < 2; f += 2)
  331. {
  332. POINT p = { (INT)(pPoints[j].x) + f,
  333. (INT)(pPoints[j].y) + g };
  334. if(!pPolygon->PtInPolygon(p))
  335. {
  336. pPoints[j].x += (double)f * 1e-5;
  337. pPoints[j].y += (double)g * 1e-5;
  338. goto NEXT;
  339. }
  340. }
  341. NEXT:;
  342. }
  343. for(j = 0; j < (m_nRayCount >> 1); j ++ )
  344. {
  345. INT nCCount = 0;
  346. VERTEX pCPoints[MAX_RAY_COUNT * 10];
  347. for(int m = 0; m < pPolygon->GetCount(); m++)
  348. {
  349. VERTEX cp;
  350. if(CrossProduct(pPoints + m, pLines + (j << 1), &cp))
  351. {
  352. cp.n = m;
  353. cp.r = sqrt(pow(cp.x - pLines[j << 1].x, 2) + pow(cp.y - pLines[j << 1].y, 2));
  354. if(nCCount > 0 &&
  355. DBL_CMP(pCPoints[nCCount-1].x, cp.x) == 0 &&
  356. DBL_CMP(pCPoints[nCCount-1].y, cp.y) == 0)
  357. {
  358. int i1 = (m + 1) % pPolygon->GetCount();
  359. int i2 = (m - 1 + pPolygon->GetCount()) % pPolygon->GetCount();
  360. VERTEX line[] = { { pPoints[i1].x, pPoints[i1].y },
  361. { pPoints[i2].x, pPoints[i2].y } };
  362. LPVERTEX l0 = pLines + (j << 1);
  363. if(!CrossProduct( pLines + (j << 1), line, &cp, FALSE))
  364. pCPoints[nCCount++] = cp;
  365. }
  366. else
  367. pCPoints[nCCount++] = cp;
  368. }
  369. } // end of for(int m = 0; m < pPolygon->GetCount(); m++)
  370. if(nCCount > 0 &&
  371. DBL_CMP( pCPoints[nCCount-1].x, pCPoints[0].x) == 0 &&
  372. DBL_CMP( pCPoints[nCCount-1].y, pCPoints[0].y) == 0)
  373. {
  374. int i1 = (pCPoints[0].n + 1) % pPolygon->GetCount();
  375. int i2 = (pCPoints[0].n - 1 + pPolygon->GetCount()) % pPolygon->GetCount();
  376. VERTEX line[] = { { pPoints[i1].x, pPoints[i1].y },
  377. { pPoints[i2].x, pPoints[i2].y } };
  378. LPVERTEX l0 = pLines + (j << 1);
  379. VERTEX cp;
  380. if(CrossProduct( pLines + (j << 1), line, &cp, FALSE))
  381. nCCount--;
  382. }
  383. if(nCCount % 2 == 0)
  384. {
  385. qsort(pCPoints, nCCount, sizeof(VERTEX), comparePoints);
  386. for(m = 0; m < nCCount; m++)
  387. {
  388. pCPoints[m].f = ( m % 2 ) ? -1 : +1;
  389. AddPointToSegment( pSegments, j, pCPoints + m );
  390. }
  391. }
  392. } // end of for(j = 0; j < (m_nRayCount >> 1); j ++ )
  393. delete[] pPoints; // Added delete[] 22/04/2001
  394. } // end of if(pPolygon->GetCount() > 2 && pPolygon->GetType() == 0)
  395. } // end of for(int i = 0; i < m_nCount; i++)
  396. INT sc = 0;
  397. for(INT j = 0; j < (m_nRayCount >> 1); j ++)
  398. {
  399. StickSegments( pSegments, j );
  400. sc += pSegments[j].nCount;
  401. }
  402. LPSEGMENT pSegment = new SEGMENT[sc];
  403. ::FillMemory(pSegment, sizeof(SEGMENT)*sc, 0xFF);
  404. INT cn = 0;
  405. dZeroShift *= 3.1415 / 180.0;
  406. for(j = 0; j < (m_nRayCount >> 1); j ++)
  407. {
  408. LPVERTEX pPoints = pSegments[j].pPoints;
  409. if(pSegments[j].nCount % 2)
  410. CMessageBox::MessageBox(NULL, "Produced odd number of points", NULL, MB_OK);
  411. else
  412. {
  413. double k = scale;
  414. HPEN hPen = ::CreatePen( PS_SOLID, 1, 0xFFFFFFFF );
  415. HPEN hOldPen = (HPEN)::SelectObject(hDC, hPen);
  416. // convert zero shift to radians
  417. for(int m = 0; m < pSegments[j].nCount; m += 2)
  418. {
  419. pPoints[m].r -= double_range;//*scale;
  420. pPoints[m+1].r -= double_range;//*scale;
  421. pPoints[m].x *= k;
  422. pPoints[m].y *= k;
  423. // rotate coordinates
  424. double x = pPoints[m].x * cos( dZeroShift ) + pPoints[m].y * sin( dZeroShift );
  425. double y = -pPoints[m].x * sin( dZeroShift ) + pPoints[m].y * cos( dZeroShift );
  426. pPoints[m].x = x;
  427. pPoints[m].y = y;
  428. pPoints[m].x += center.x;
  429. pPoints[m].y += center.y;
  430. ::MoveToEx(hDC, (int)pPoints[m].x, (int)pPoints[m].y, NULL);
  431. pPoints[m+1].x *= k;
  432. pPoints[m+1].y *= k;
  433. // rotate coordinates
  434. x = pPoints[m+1].x * cos( dZeroShift ) + pPoints[m+1].y * sin( dZeroShift );
  435. y = -pPoints[m+1].x * sin( dZeroShift ) + pPoints[m+1].y * cos( dZeroShift );
  436. pPoints[m+1].x = x;
  437. pPoints[m+1].y = y;
  438. pPoints[m+1].x += center.x;
  439. pPoints[m+1].y += center.y;
  440. ::LineTo(hDC, (int)pPoints[m + 1].x, (int)pPoints[m + 1].y);
  441. if(pPoints[m].r < 0.0 && pPoints[m+1].r > 0.0)
  442. {
  443. pSegment[cn].nIndex = j;
  444. pSegment[cn].pPoints[0] = 0;
  445. pSegment[cn++].pPoints[1] = (INT)(-pPoints[m].r);
  446. pSegment[cn].nIndex = j + (m_nRayCount >> 1);
  447. pSegment[cn].pPoints[0] = 0;
  448. pSegment[cn++].pPoints[1] = (INT)(pPoints[m + 1].r);
  449. }
  450. else
  451. {
  452. if(pPoints[m].r < 0 || pPoints[m+1].r < 0)
  453. {
  454. if(pPoints[m].r > pPoints[m+1].r)
  455. {
  456. pSegment[cn].nIndex = j;
  457. pSegment[cn].pPoints[0] = (INT)(-pPoints[m].r);
  458. pSegment[cn++].pPoints[1] = (INT)(-pPoints[m + 1].r);
  459. }
  460. else
  461. {
  462. pSegment[cn].nIndex = j;
  463. pSegment[cn].pPoints[0] = (INT)(-pPoints[m + 1].r);
  464. pSegment[cn++].pPoints[1] = (INT)(-pPoints[m].r);
  465. }
  466. }
  467. else
  468. if(pPoints[m].r < pPoints[m+1].r)
  469. {
  470. pSegment[cn].nIndex = j + (m_nRayCount >> 1);
  471. pSegment[cn].pPoints[0] = (INT)(pPoints[m].r);
  472. pSegment[cn++].pPoints[1] = (INT)(pPoints[m + 1].r);
  473. }
  474. else
  475. {
  476. pSegment[cn].nIndex = j + (m_nRayCount >> 1);
  477. pSegment[cn].pPoints[0] = (INT)(pPoints[m + 1].r);
  478. pSegment[cn++].pPoints[1] = (INT)(pPoints[m].r);
  479. }
  480. } // end of else
  481. } // end of for(int m = 0; m < pSegments[j].nCount; m += 2)
  482. ::SelectObject(hDC, hOldPen);
  483. ::DeleteObject(hPen);
  484. } // end of else
  485. delete [] pPoints;
  486. pSegments[j].pPoints = NULL;
  487. } // end of for(j = 0; j < (m_nRayCount >> 1); j ++)
  488. qsort(pSegment, sc, sizeof(SEGMENT), compareSegments);
  489. FILE *file = fopen("Hidden.txt", "wt");
  490. if( file )
  491. {
  492. for(j = 0; j < sc; j++)
  493. if(pSegment[j].nIndex != 0xFFFFFFFF)
  494. {
  495. INT nMin = pSegment[j].pPoints[0] * LGS_MAX_DETECTOR_RANGE / range; //?
  496. INT nMax = pSegment[j].pPoints[1] * LGS_MAX_DETECTOR_RANGE / range; //?
  497. if( nMin <= LGS_MAX_DETECTOR_RANGE )
  498. {
  499. if( nMax > LGS_MAX_DETECTOR_RANGE )
  500. nMax = LGS_MAX_DETECTOR_RANGE;
  501. fprintf(file, "%d %d %d\n", pSegment[j].nIndex, nMin, nMax);
  502. }
  503. }
  504. fclose(file);
  505. }
  506. file = fopen("Polygons.txt", "wt");
  507. if( file )
  508. {
  509. for(j = 0; j < m_nCount; j++)
  510. {
  511. CPolygon *pPolygon = m_pPolygons[j];
  512. fprintf(file, "polygon %d\n", pPolygon->GetType() );
  513. LPPOINT pPoint = pPolygon->GetPoints();
  514. for( int m = 0; m < pPolygon->GetCount(); m++ )
  515. {
  516. POINT sp = pPoint[m];
  517. sp.x = (sp.x + range) * LGS_MAX_DETECTOR_RANGE / range;
  518. sp.y = (sp.y + range) * LGS_MAX_DETECTOR_RANGE / range;
  519. fprintf(file, "%d %d\n", sp.x, sp.y );
  520. }
  521. }
  522. fclose(file);
  523. }
  524. delete[] pSegment;
  525. }
  526. void CPolygon::Draw(HDC hDC, POINT& center, const double& scale, double dZeroShift )
  527. {
  528. if(m_fVisible)
  529. {
  530. double k = scale;
  531. COLORREF colors[] = { 0x00000000, 0x007F0000, 0x00FF0000, // 18/04/2001
  532. 0x007F007F, 0x00FF00FF, 0x00004F00 };
  533. int background_mode;
  534. HPEN hPen = ::CreatePen(PS_SOLID, 1, colors[m_Type]);
  535. HPEN hOldPen = (HPEN)::SelectObject(hDC, hPen);
  536. HBRUSH hBrush = ::CreateHatchBrush(HS_DIAGCROSS, colors[m_Type]);
  537. HBRUSH hOldBrush = (HBRUSH)::SelectObject(hDC, hBrush);
  538. background_mode = ::SetBkMode(hDC, TRANSPARENT);
  539. dZeroShift *= 3.1415 / 180;
  540. for(int j = 0; j < GetCount(); j++)
  541. {
  542. double x = m_pPoints[j].x * cos( dZeroShift ) + m_pPoints[j].y * sin( dZeroShift );
  543. double y = -m_pPoints[j].x * sin( dZeroShift ) + m_pPoints[j].y * cos( dZeroShift );
  544. m_pPointsDraw[j].x = (long)(x * k + center.x);
  545. m_pPointsDraw[j].y = (long)(y * k + center.y);
  546. ::Ellipse(hDC, m_pPointsDraw[j].x - 2, m_pPointsDraw[j].y - 2,
  547. m_pPointsDraw[j].x + 2, m_pPointsDraw[j].y + 2);
  548. }
  549. ::Polygon(hDC, m_pPointsDraw, GetCount());
  550. ::SetBkMode(hDC, background_mode);
  551. ::SelectObject(hDC, hOldBrush);
  552. ::DeleteObject(hBrush);
  553. ::SelectObject(hDC, hOldPen);
  554. ::DeleteObject(hPen);
  555. } // end of if(m_fVisible)
  556. }
  557. CPolygons::CPolygons()
  558. {
  559. m_pPolygons = NULL;
  560. m_nCount = NULL;
  561. m_nRayCount = 360;
  562. InitializeCriticalSection(&cs);
  563. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CPolygons::CPolygons" );
  564. }
  565. CPolygons::~CPolygons()
  566. {
  567. Empty();
  568. DeleteCriticalSection(&cs);
  569. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CPolygons::~CPolygons" );
  570. }
  571. BOOL CPolygons::AddPolygon(CPolygon *pPolygon)
  572. {
  573. CPolygon** pPolygons = new CPolygon*[m_nCount + 1];
  574. if(m_pPolygons)
  575. {
  576. memcpy(pPolygons, m_pPolygons, sizeof(CPolygon*) * m_nCount);
  577. delete[] m_pPolygons;
  578. }
  579. EnterCriticalSection(&cs); // 24/05/2001
  580. m_pPolygons = pPolygons;
  581. m_pPolygons[m_nCount++] = pPolygon;
  582. LeaveCriticalSection(&cs);
  583. return TRUE;
  584. }
  585. BOOL CPolygons::PtInPolygons(POINT& point, BOOL *pFlags) const
  586. {
  587. for(int i = 0; i < m_nCount; i++)
  588. if(pFlags[m_pPolygons[i]->GetType()])
  589. if(m_pPolygons[i]->PtInPolygon(point))
  590. return TRUE;
  591. return FALSE;
  592. }
  593. void CPolygons::Empty()
  594. {
  595. if(m_pPolygons)
  596. {
  597. for(int i = 0; i < m_nCount; i++)
  598. delete m_pPolygons[i];
  599. delete[] m_pPolygons;
  600. }
  601. m_pPolygons = NULL;
  602. m_nCount = 0;
  603. }
  604. BOOL CPolygons::Save(LPCTSTR pName)
  605. {
  606. HANDLE hFile = ::CreateFile(pName, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
  607. NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  608. DWORD dwWritten = 0UL;
  609. ::WriteFile(hFile, &m_nCount, sizeof(m_nCount), &dwWritten, NULL);
  610. for(int i = 0; i < m_nCount; i++)
  611. m_pPolygons[i]->Save(hFile);
  612. ::CloseHandle(hFile);
  613. return TRUE;
  614. }
  615. BOOL CPolygons::Load(LPCTSTR pName)
  616. {
  617. Empty();
  618. HANDLE hFile = ::CreateFile(pName, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
  619. NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  620. if(hFile != INVALID_HANDLE_VALUE)
  621. {
  622. DWORD dwReaded = 0UL;
  623. ::ReadFile(hFile, &m_nCount, sizeof(m_nCount), &dwReaded, NULL);
  624. m_pPolygons = new CPolygon*[m_nCount];
  625. for(int i = 0; i < m_nCount; i++)
  626. m_pPolygons[i] = new CPolygon(hFile);
  627. ::CloseHandle(hFile);
  628. return TRUE;
  629. }
  630. return FALSE;
  631. }
  632. void CPolygons::Draw(HDC hDC, POINT& center, const double& scale, double dZeroShift )
  633. {
  634. for(int i = 0; i < GetCount(); i++)
  635. m_pPolygons[i]->Draw(hDC, center, scale, dZeroShift );
  636. }
  637. BOOL CPolygons::Delete(POINT& p)
  638. {
  639. BOOL fDeleted = FALSE;
  640. for(int i = 0; i < m_nCount;)
  641. {
  642. if(m_pPolygons[i]->PtInPolygon(p))
  643. {
  644. CPolygon *pPolygon = m_pPolygons[i];
  645. m_pPolygons[i] = NULL;
  646. delete pPolygon;
  647. m_nCount--;
  648. memcpy(m_pPolygons + i, m_pPolygons + i + 1, sizeof(CPolygon*) * (m_nCount - i));
  649. fDeleted = TRUE;
  650. }
  651. else
  652. i++;
  653. }
  654. return fDeleted;
  655. }
  656. void CPolygons::SetVisible(INT nType, BOOL fVisible)
  657. {
  658. for(int i = 0; i < GetCount(); i++)
  659. if(m_pPolygons[i]->GetType() == nType)
  660. m_pPolygons[i]->SetVisible(fVisible);
  661. }