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.

519 lines
11 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: LgDPolygon.cpp
  9. // AUTHOR: Sergey Chizhevskiy
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // Source file of LgDPolygon.cpp
  13. //----------------------------------------------------------------------------
  14. #include "stdafx.h"
  15. #include "LgLogger.h"
  16. #include "LgDPolygon.h"
  17. #include "LgPerformanceCounter.h"
  18. #include "LgGeometry.h"
  19. CLgDPolygon::CLgDPolygon()
  20. {
  21. }
  22. CLgDPolygon::~CLgDPolygon()
  23. {
  24. }
  25. CLgDPolygon& CLgDPolygon::operator =( CLgDPolygon& polygon )
  26. {
  27. RemoveAll();
  28. for( INT i = 0; i < polygon.GetCount(); i++ )
  29. Add( polygon[i] );
  30. return *this;
  31. }
  32. INT CLgDPolygon::Minimize()
  33. {
  34. // store count
  35. INT nCount = GetCount();
  36. if( !nCount )
  37. return nCount;
  38. // minimize polygon
  39. double odx = 0;
  40. double ody = 0;
  41. CLgDPoint odp;
  42. INT nPoint = 0;
  43. for( INT i = 0; i <= GetCount(); i++ )
  44. {
  45. CLgDPoint &dp = GetAt( i % GetCount() );
  46. double dx, dy;
  47. double at, oat;
  48. switch( nPoint )
  49. {
  50. case 0:
  51. nPoint++;
  52. // store point
  53. odp = dp;
  54. break;
  55. case 1:
  56. nPoint++;
  57. // calculate delta
  58. odx = odp.x - dp.x;
  59. ody = odp.y - dp.y;
  60. // store point
  61. odp = dp;
  62. break;
  63. case 2:
  64. // calculate delta
  65. dx = odp.x - dp.x;
  66. dy = odp.y - dp.y;
  67. // store point
  68. odp = dp;
  69. // check delta
  70. at = atan2( dx, dy );
  71. oat = atan2( odx, ody );
  72. if( at == oat )
  73. {
  74. // remove previous point
  75. Remove( (i - 1 + GetCount())%GetCount() );
  76. // decrease counter
  77. i--;
  78. }
  79. // store delta
  80. odx = dx;
  81. ody = dy;
  82. break;
  83. }
  84. }
  85. return nCount - GetCount();
  86. }
  87. typedef struct CLgDPointEx : public CLgDLinePoint
  88. {
  89. enum CLgDPointExDir{ DIR_CROSS, DIR_TOP, DIR_BOTTOM } dir;
  90. INT nIndex;
  91. BOOL bDelete;
  92. }CLgDPointEx;
  93. BOOL CLgDPolygon::CrossProduct_( CLgDPoint dp1, CLgDPoint dp2, CLgDLinePointArray &array, BOOL bGlueLines )
  94. {
  95. if( !GetCount() )
  96. return TRUE;
  97. CLgPerformanceCounter pc;
  98. pc.Begin();
  99. CLgDPoint &dp11 = dp1;
  100. CLgDPoint &dp12 = dp2;
  101. if( dp11.x == dp12.x && dp11.y == dp12.y )
  102. {
  103. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_WARNING, this, _T("CLgDPolygon::CrossProduct"), _T("Line segment represented by two equal point") );
  104. return FALSE;
  105. }
  106. BOOL bSortTopBottom = fabs(dp11.y - dp12.y) > fabs(dp11.x - dp12.x);
  107. // find all cross points
  108. CLgArray<CLgDPointEx> points;
  109. double dx1 = dp11.x - dp12.x;
  110. double dy1 = dp11.y - dp12.y;
  111. for( INT i = 0; i < GetCount(); i++ )
  112. {
  113. INT nIndex1 = ( i ) % GetCount();
  114. INT nIndex2 = ( i + 1 ) % GetCount();
  115. CLgDPoint dp21 = GetAt(nIndex1);
  116. CLgDPoint dp22 = GetAt(nIndex2);
  117. // calculate scalar vector multiplication
  118. double dx2 = dp21.x - dp22.x;
  119. double dy2 = dp21.y - dp22.y;
  120. double d = dx1 * dy2 - dx2 * dy1;
  121. // direct vectors perpendiculary to sort direction
  122. BOOL bSwap = d < 0;
  123. if( bSwap )
  124. {
  125. CLgDPoint sdp = dp21;
  126. dp21 = dp22;
  127. dp22 = sdp;
  128. }
  129. CLgDPointEx dpe;
  130. INT nCrossType = CLgGeometry::CrossProduct( dp11, dp12, dp21, dp22, &dpe );
  131. if( nCrossType >= CLgGeometry::LGCT_CROSS )
  132. {
  133. if( nCrossType & (CLgGeometry::LGCT_CROSS_P11|CLgGeometry::LGCT_CROSS_P12) )
  134. {
  135. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgDPolygon::CrossProduct"), _T("End points of cross line should outside of region") );
  136. return FALSE;
  137. }
  138. if( ( nCrossType & (CLgGeometry::LGCT_CROSS_P21|CLgGeometry::LGCT_CROSS_P22) ) == (CLgGeometry::LGCT_CROSS_P21|CLgGeometry::LGCT_CROSS_P22) )
  139. {
  140. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgDPolygon::CrossProduct"), _T("Line crossed by another line in two points") );
  141. return FALSE;
  142. }
  143. dpe.bDelete = FALSE;
  144. if( nCrossType & CLgGeometry::LGCT_CROSS_P21 ) { dpe.dir = CLgDPointEx::DIR_TOP; dpe.nIndex = bSwap ? nIndex2 : nIndex1; }
  145. else if( nCrossType & CLgGeometry::LGCT_CROSS_P22 ) { dpe.dir = CLgDPointEx::DIR_BOTTOM; dpe.nIndex = bSwap ? nIndex1 : nIndex2; }
  146. else { dpe.dir = CLgDPointEx::DIR_CROSS; dpe.nIndex = -1; }
  147. points.Add( dpe );
  148. }
  149. }
  150. // delete points with equal index
  151. for( i = 0; i < points.GetCount(); i++ )
  152. {
  153. CLgDPointEx &dpe = points.GetAt(i);
  154. if( dpe.bDelete || dpe.dir == CLgDPointEx::DIR_CROSS )
  155. continue;
  156. // find connected enge
  157. for( int j = i+1; j < points.GetCount(); j++ )
  158. {
  159. CLgDPointEx &dpe2 = points.GetAt(j);
  160. if( dpe.nIndex != dpe2.nIndex )
  161. continue;
  162. if( dpe.dir == dpe2.dir )
  163. dpe.bDelete = TRUE;
  164. dpe2.bDelete = TRUE;
  165. break;
  166. }
  167. }
  168. // sort points along bSortTopBottom and fill segments
  169. BOOL bBegin = TRUE;
  170. for( i = 0; i < points.GetCount(); i++ )
  171. {
  172. CLgDPointEx &dp = points.GetAt(i);
  173. INT nIndex = i;
  174. for( INT j = i + 1; j < points.GetCount(); j++ )
  175. {
  176. CLgDPointEx &sdp = points.GetAt(j);
  177. CLgDPointEx &mdp = points.GetAt(nIndex);
  178. if( bSortTopBottom )
  179. {
  180. if( sdp.y < mdp.y )
  181. nIndex = j;
  182. }
  183. else
  184. {
  185. if( sdp.x < mdp.x )
  186. nIndex = j;
  187. }
  188. }
  189. if( nIndex != i )
  190. {
  191. CLgDPointEx &mdp = points.GetAt(nIndex);
  192. CLgDPointEx sdp = dp;
  193. dp = mdp;
  194. mdp = sdp;
  195. }
  196. // fill segments
  197. if( dp.bDelete )
  198. continue;
  199. // store point
  200. CLgDLinePoint ldp = dp;
  201. ldp.bBegin = bBegin;
  202. // if nessessary to glue lines
  203. if( bGlueLines )
  204. {
  205. // add into appropriate place of array
  206. BOOL bInserted = FALSE;
  207. for( INT j = 0; j < array.GetCount(); j++ )
  208. {
  209. if( bSortTopBottom )
  210. {
  211. if( array[j].y > ldp.y )
  212. {
  213. array.InsertAt( j, ldp );
  214. bInserted = TRUE;
  215. break;
  216. }
  217. }
  218. else
  219. {
  220. if( array[j].x > ldp.x )
  221. {
  222. array.InsertAt( j, ldp );
  223. bInserted = TRUE;
  224. break;
  225. }
  226. }
  227. }
  228. if( !bInserted )
  229. array.Add( ldp );
  230. }
  231. else
  232. array.Add( ldp );
  233. bBegin = !bBegin;
  234. }
  235. // if nessessary to glue lines
  236. if( bGlueLines )
  237. {
  238. bBegin = FALSE;
  239. INT coef = 0;
  240. for( INT i = 0; i < array.GetCount(); )
  241. {
  242. CLgDLinePoint &lp = array[i];
  243. if( lp.bBegin ) coef++;
  244. else coef--;
  245. bool bDelete = TRUE;
  246. if( bBegin )
  247. {
  248. if( coef == 0 )
  249. {
  250. bBegin = FALSE;
  251. bDelete = FALSE;
  252. }
  253. }
  254. else
  255. {
  256. if( coef == 1 )
  257. {
  258. bBegin = TRUE;
  259. bDelete = FALSE;
  260. }
  261. }
  262. if( bDelete ) array.Remove(i);
  263. else i++;
  264. }
  265. }
  266. LGLOG( LOG_LEVEL_REALTIME, LOG_TYPE_INFO, this, _T("CLgDPolygon::CrossProduct"), _T("Processing time is %0.6f for %d points"), pc.End(), GetCount() );
  267. return TRUE;
  268. }
  269. BOOL CLgDPolygon::PtInPolygon_( CLgDPoint pt )
  270. {
  271. if( !GetCount() )
  272. return FALSE;
  273. CLgPerformanceCounter pc;
  274. pc.Begin();
  275. CLgDRect rect( DBL_MAX, DBL_MAX, -DBL_MAX, -DBL_MAX );
  276. for( INT i = 0; i < GetCount(); i++ )
  277. {
  278. CLgDPoint& dp = GetAt(i);
  279. if( dp.x < rect.left ) rect.left = dp.x;
  280. if( dp.x > rect.right ) rect.right = dp.x;
  281. }
  282. double delta = 100.0;
  283. CLgDPoint dp11( rect.left - delta, pt.y );
  284. CLgDPoint dp12( rect.right + delta, pt.y );
  285. BOOL bSortTopBottom = FALSE;
  286. // find all cross points
  287. CLgArray<CLgDPointEx> points;
  288. double dx1 = dp11.x - dp12.x;
  289. double dy1 = dp11.y - dp12.y;
  290. for( i = 0; i < GetCount(); i++ )
  291. {
  292. INT nIndex1 = ( i ) % GetCount();
  293. INT nIndex2 = ( i + 1 ) % GetCount();
  294. CLgDPoint dp21 = GetAt(nIndex1);
  295. CLgDPoint dp22 = GetAt(nIndex2);
  296. double dx2 = dp21.x - dp22.x;
  297. double dy2 = dp21.y - dp22.y;
  298. // calculate scalar vector addition
  299. double d = dx1 * dy2 - dx2 * dy1;
  300. // direct vectors perpendiculary to sort direction
  301. BOOL bSwap = d < 0;
  302. if( bSwap )
  303. {
  304. CLgDPoint sdp = dp21;
  305. dp21 = dp22;
  306. dp22 = sdp;
  307. }
  308. CLgDPointEx dpe;
  309. INT nCrossType = CLgGeometry::CrossProduct( dp11, dp12, dp21, dp22, &dpe );
  310. if( nCrossType >= CLgGeometry::LGCT_CROSS )
  311. {
  312. if( nCrossType & (CLgGeometry::LGCT_CROSS_P11|CLgGeometry::LGCT_CROSS_P12) )
  313. {
  314. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgDPolygon::PtInPolygon"), _T("End points of cross line should outside of region") );
  315. return FALSE;
  316. }
  317. if( ( nCrossType & (CLgGeometry::LGCT_CROSS_P21|CLgGeometry::LGCT_CROSS_P22) ) == (CLgGeometry::LGCT_CROSS_P21|CLgGeometry::LGCT_CROSS_P22) )
  318. {
  319. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgDPolygon::PtInPolygon"), _T("Line crossed by another line in two points") );
  320. return FALSE;
  321. }
  322. dpe.bDelete = FALSE;
  323. if( nCrossType & CLgGeometry::LGCT_CROSS_P21 ) { dpe.dir = CLgDPointEx::DIR_TOP; dpe.nIndex = bSwap ? nIndex2 : nIndex1; }
  324. else if( nCrossType & CLgGeometry::LGCT_CROSS_P22 ) { dpe.dir = CLgDPointEx::DIR_BOTTOM; dpe.nIndex = bSwap ? nIndex1 : nIndex2; }
  325. else { dpe.dir = CLgDPointEx::DIR_CROSS; dpe.nIndex = -1; }
  326. points.Add( dpe );
  327. }
  328. }
  329. // delete points with equal index
  330. for( i = 0; i < points.GetCount(); i++ )
  331. {
  332. CLgDPointEx &dpe = points.GetAt(i);
  333. if( dpe.bDelete || dpe.dir == CLgDPointEx::DIR_CROSS )
  334. continue;
  335. // find connected enge
  336. for( int j = i+1; j < points.GetCount(); j++ )
  337. {
  338. CLgDPointEx &dpe2 = points.GetAt(j);
  339. if( dpe.nIndex != dpe2.nIndex )
  340. continue;
  341. if( dpe.dir == dpe2.dir )
  342. dpe.bDelete = TRUE;
  343. dpe2.bDelete = TRUE;
  344. break;
  345. }
  346. }
  347. // sort points along bSortTopBottom and fill segments
  348. BOOL bResult = FALSE;
  349. BOOL bStart = FALSE;
  350. for( i = 0; i < points.GetCount(); i++ )
  351. {
  352. CLgDPointEx &dp = points.GetAt(i);
  353. INT nIndex = i;
  354. for( INT j = i + 1; j < points.GetCount(); j++ )
  355. {
  356. CLgDPointEx &sdp = points.GetAt(j);
  357. CLgDPointEx &mdp = points.GetAt(nIndex);
  358. if( bSortTopBottom )
  359. {
  360. if( sdp.y < mdp.y )
  361. nIndex = j;
  362. }
  363. else
  364. {
  365. if( sdp.x < mdp.x )
  366. nIndex = j;
  367. }
  368. }
  369. if( nIndex != i )
  370. {
  371. CLgDPointEx &mdp = points.GetAt(nIndex);
  372. CLgDPointEx sdp = dp;
  373. dp = mdp;
  374. mdp = sdp;
  375. }
  376. // fill segments
  377. if( dp.bDelete )
  378. continue;
  379. if( bStart )
  380. {
  381. if( pt.x < dp.x )
  382. {
  383. bResult = TRUE;
  384. break;
  385. }
  386. }
  387. else
  388. {
  389. if( pt.x < dp.x )
  390. break;
  391. }
  392. bStart = !bStart;
  393. }
  394. LGLOG( LOG_LEVEL_REALTIME, LOG_TYPE_INFO, this, _T("CLgDPolygon::PtInPolygon"), _T("Processing time is %0.6f for %d points"), pc.End(), GetCount() );
  395. return bResult;
  396. }
  397. BOOL CLgDPolygon::CrossProduct( CLgDPoint dp1, CLgDPoint dp2, CLgDLinePointArray &array, BOOL bGlueLines )
  398. {
  399. BOOL bResult = FALSE;
  400. __try
  401. {
  402. bResult = CrossProduct_( dp1, dp2, array, bGlueLines );
  403. }
  404. __except( EXCEPTION_EXECUTE_HANDLER )
  405. {
  406. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgDPolygon::CrossProduct"), _T("Exception 0x%08X"), _exception_code() );
  407. }
  408. return bResult;
  409. }
  410. BOOL CLgDPolygon::PtInPolygon( CLgDPoint pt )
  411. {
  412. BOOL bResult = FALSE;
  413. __try
  414. {
  415. bResult = PtInPolygon_( pt );
  416. }
  417. __except( EXCEPTION_EXECUTE_HANDLER )
  418. {
  419. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgDPolygon::PtInPolygon"), _T("Exception 0x%08X"), _exception_code() );
  420. }
  421. return bResult;
  422. }