For DOS Protected Mode version of sensor core software
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.

520 lines
11 KiB

3 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. INT i;
  110. double dx1 = dp11.x - dp12.x;
  111. double dy1 = dp11.y - dp12.y;
  112. for( i = 0; i < GetCount(); i++ )
  113. {
  114. INT nIndex1 = ( i ) % GetCount();
  115. INT nIndex2 = ( i + 1 ) % GetCount();
  116. CLgDPoint dp21 = GetAt(nIndex1);
  117. CLgDPoint dp22 = GetAt(nIndex2);
  118. // calculate scalar vector multiplication
  119. double dx2 = dp21.x - dp22.x;
  120. double dy2 = dp21.y - dp22.y;
  121. double d = dx1 * dy2 - dx2 * dy1;
  122. // direct vectors perpendiculary to sort direction
  123. BOOL bSwap = d < 0;
  124. if( bSwap )
  125. {
  126. CLgDPoint sdp = dp21;
  127. dp21 = dp22;
  128. dp22 = sdp;
  129. }
  130. CLgDPointEx dpe;
  131. INT nCrossType = CLgGeometry::CrossProduct( dp11, dp12, dp21, dp22, &dpe );
  132. if( nCrossType >= CLgGeometry::LGCT_CROSS )
  133. {
  134. if( nCrossType & (CLgGeometry::LGCT_CROSS_P11|CLgGeometry::LGCT_CROSS_P12) )
  135. {
  136. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgDPolygon::CrossProduct"), _T("End points of cross line should outside of region") );
  137. return FALSE;
  138. }
  139. if( ( nCrossType & (CLgGeometry::LGCT_CROSS_P21|CLgGeometry::LGCT_CROSS_P22) ) == (CLgGeometry::LGCT_CROSS_P21|CLgGeometry::LGCT_CROSS_P22) )
  140. {
  141. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgDPolygon::CrossProduct"), _T("Line crossed by another line in two points") );
  142. return FALSE;
  143. }
  144. dpe.bDelete = FALSE;
  145. if( nCrossType & CLgGeometry::LGCT_CROSS_P21 ) { dpe.dir = CLgDPointEx::DIR_TOP; dpe.nIndex = bSwap ? nIndex2 : nIndex1; }
  146. else if( nCrossType & CLgGeometry::LGCT_CROSS_P22 ) { dpe.dir = CLgDPointEx::DIR_BOTTOM; dpe.nIndex = bSwap ? nIndex1 : nIndex2; }
  147. else { dpe.dir = CLgDPointEx::DIR_CROSS; dpe.nIndex = -1; }
  148. points.Add( dpe );
  149. }
  150. }
  151. // delete points with equal index
  152. for( i = 0; i < points.GetCount(); i++ )
  153. {
  154. CLgDPointEx &dpe = points.GetAt(i);
  155. if( dpe.bDelete || dpe.dir == CLgDPointEx::DIR_CROSS )
  156. continue;
  157. // find connected enge
  158. for( int j = i+1; j < points.GetCount(); j++ )
  159. {
  160. CLgDPointEx &dpe2 = points.GetAt(j);
  161. if( dpe.nIndex != dpe2.nIndex )
  162. continue;
  163. if( dpe.dir == dpe2.dir )
  164. dpe.bDelete = TRUE;
  165. dpe2.bDelete = TRUE;
  166. break;
  167. }
  168. }
  169. // sort points along bSortTopBottom and fill segments
  170. BOOL bBegin = TRUE;
  171. for( i = 0; i < points.GetCount(); i++ )
  172. {
  173. CLgDPointEx &dp = points.GetAt(i);
  174. INT nIndex = i;
  175. for( INT j = i + 1; j < points.GetCount(); j++ )
  176. {
  177. CLgDPointEx &sdp = points.GetAt(j);
  178. CLgDPointEx &mdp = points.GetAt(nIndex);
  179. if( bSortTopBottom )
  180. {
  181. if( sdp.y < mdp.y )
  182. nIndex = j;
  183. }
  184. else
  185. {
  186. if( sdp.x < mdp.x )
  187. nIndex = j;
  188. }
  189. }
  190. if( nIndex != i )
  191. {
  192. CLgDPointEx &mdp = points.GetAt(nIndex);
  193. CLgDPointEx sdp = dp;
  194. dp = mdp;
  195. mdp = sdp;
  196. }
  197. // fill segments
  198. if( dp.bDelete )
  199. continue;
  200. // store point
  201. CLgDLinePoint ldp = dp;
  202. ldp.bBegin = bBegin;
  203. // if nessessary to glue lines
  204. if( bGlueLines )
  205. {
  206. // add into appropriate place of array
  207. BOOL bInserted = FALSE;
  208. for( INT j = 0; j < array.GetCount(); j++ )
  209. {
  210. if( bSortTopBottom )
  211. {
  212. if( array[j].y > ldp.y )
  213. {
  214. array.InsertAt( j, ldp );
  215. bInserted = TRUE;
  216. break;
  217. }
  218. }
  219. else
  220. {
  221. if( array[j].x > ldp.x )
  222. {
  223. array.InsertAt( j, ldp );
  224. bInserted = TRUE;
  225. break;
  226. }
  227. }
  228. }
  229. if( !bInserted )
  230. array.Add( ldp );
  231. }
  232. else
  233. array.Add( ldp );
  234. bBegin = !bBegin;
  235. }
  236. // if nessessary to glue lines
  237. if( bGlueLines )
  238. {
  239. bBegin = FALSE;
  240. INT coef = 0;
  241. for( INT i = 0; i < array.GetCount(); )
  242. {
  243. CLgDLinePoint &lp = array[i];
  244. if( lp.bBegin ) coef++;
  245. else coef--;
  246. bool bDelete = TRUE;
  247. if( bBegin )
  248. {
  249. if( coef == 0 )
  250. {
  251. bBegin = FALSE;
  252. bDelete = FALSE;
  253. }
  254. }
  255. else
  256. {
  257. if( coef == 1 )
  258. {
  259. bBegin = TRUE;
  260. bDelete = FALSE;
  261. }
  262. }
  263. if( bDelete ) array.Remove(i);
  264. else i++;
  265. }
  266. }
  267. LGLOG( LOG_LEVEL_REALTIME, LOG_TYPE_INFO, this, _T("CLgDPolygon::CrossProduct"), _T("Processing time is %0.6f for %d points"), pc.End(), GetCount() );
  268. return TRUE;
  269. }
  270. BOOL CLgDPolygon::PtInPolygon_( CLgDPoint pt )
  271. {
  272. if( !GetCount() )
  273. return FALSE;
  274. CLgPerformanceCounter pc;
  275. pc.Begin();
  276. CLgDRect rect( DBL_MAX, DBL_MAX, -DBL_MAX, -DBL_MAX );
  277. INT i;
  278. for( i = 0; i < GetCount(); i++ )
  279. {
  280. CLgDPoint& dp = GetAt(i);
  281. if( dp.x < rect.left ) rect.left = dp.x;
  282. if( dp.x > rect.right ) rect.right = dp.x;
  283. }
  284. double delta = 100.0;
  285. CLgDPoint dp11( rect.left - delta, pt.y );
  286. CLgDPoint dp12( rect.right + delta, pt.y );
  287. BOOL bSortTopBottom = FALSE;
  288. // find all cross points
  289. CLgArray<CLgDPointEx> points;
  290. double dx1 = dp11.x - dp12.x;
  291. double dy1 = dp11.y - dp12.y;
  292. for( i = 0; i < GetCount(); i++ )
  293. {
  294. INT nIndex1 = ( i ) % GetCount();
  295. INT nIndex2 = ( i + 1 ) % GetCount();
  296. CLgDPoint dp21 = GetAt(nIndex1);
  297. CLgDPoint dp22 = GetAt(nIndex2);
  298. double dx2 = dp21.x - dp22.x;
  299. double dy2 = dp21.y - dp22.y;
  300. // calculate scalar vector addition
  301. double d = dx1 * dy2 - dx2 * dy1;
  302. // direct vectors perpendiculary to sort direction
  303. BOOL bSwap = d < 0;
  304. if( bSwap )
  305. {
  306. CLgDPoint sdp = dp21;
  307. dp21 = dp22;
  308. dp22 = sdp;
  309. }
  310. CLgDPointEx dpe;
  311. INT nCrossType = CLgGeometry::CrossProduct( dp11, dp12, dp21, dp22, &dpe );
  312. if( nCrossType >= CLgGeometry::LGCT_CROSS )
  313. {
  314. if( nCrossType & (CLgGeometry::LGCT_CROSS_P11|CLgGeometry::LGCT_CROSS_P12) )
  315. {
  316. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgDPolygon::PtInPolygon"), _T("End points of cross line should outside of region") );
  317. return FALSE;
  318. }
  319. if( ( nCrossType & (CLgGeometry::LGCT_CROSS_P21|CLgGeometry::LGCT_CROSS_P22) ) == (CLgGeometry::LGCT_CROSS_P21|CLgGeometry::LGCT_CROSS_P22) )
  320. {
  321. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgDPolygon::PtInPolygon"), _T("Line crossed by another line in two points") );
  322. return FALSE;
  323. }
  324. dpe.bDelete = FALSE;
  325. if( nCrossType & CLgGeometry::LGCT_CROSS_P21 ) { dpe.dir = CLgDPointEx::DIR_TOP; dpe.nIndex = bSwap ? nIndex2 : nIndex1; }
  326. else if( nCrossType & CLgGeometry::LGCT_CROSS_P22 ) { dpe.dir = CLgDPointEx::DIR_BOTTOM; dpe.nIndex = bSwap ? nIndex1 : nIndex2; }
  327. else { dpe.dir = CLgDPointEx::DIR_CROSS; dpe.nIndex = -1; }
  328. points.Add( dpe );
  329. }
  330. }
  331. // delete points with equal index
  332. for( i = 0; i < points.GetCount(); i++ )
  333. {
  334. CLgDPointEx &dpe = points.GetAt(i);
  335. if( dpe.bDelete || dpe.dir == CLgDPointEx::DIR_CROSS )
  336. continue;
  337. // find connected enge
  338. for( int j = i+1; j < points.GetCount(); j++ )
  339. {
  340. CLgDPointEx &dpe2 = points.GetAt(j);
  341. if( dpe.nIndex != dpe2.nIndex )
  342. continue;
  343. if( dpe.dir == dpe2.dir )
  344. dpe.bDelete = TRUE;
  345. dpe2.bDelete = TRUE;
  346. break;
  347. }
  348. }
  349. // sort points along bSortTopBottom and fill segments
  350. BOOL bResult = FALSE;
  351. BOOL bStart = FALSE;
  352. for( i = 0; i < points.GetCount(); i++ )
  353. {
  354. CLgDPointEx &dp = points.GetAt(i);
  355. INT nIndex = i;
  356. for( INT j = i + 1; j < points.GetCount(); j++ )
  357. {
  358. CLgDPointEx &sdp = points.GetAt(j);
  359. CLgDPointEx &mdp = points.GetAt(nIndex);
  360. if( bSortTopBottom )
  361. {
  362. if( sdp.y < mdp.y )
  363. nIndex = j;
  364. }
  365. else
  366. {
  367. if( sdp.x < mdp.x )
  368. nIndex = j;
  369. }
  370. }
  371. if( nIndex != i )
  372. {
  373. CLgDPointEx &mdp = points.GetAt(nIndex);
  374. CLgDPointEx sdp = dp;
  375. dp = mdp;
  376. mdp = sdp;
  377. }
  378. // fill segments
  379. if( dp.bDelete )
  380. continue;
  381. if( bStart )
  382. {
  383. if( pt.x < dp.x )
  384. {
  385. bResult = TRUE;
  386. break;
  387. }
  388. }
  389. else
  390. {
  391. if( pt.x < dp.x )
  392. break;
  393. }
  394. bStart = !bStart;
  395. }
  396. LGLOG( LOG_LEVEL_REALTIME, LOG_TYPE_INFO, this, _T("CLgDPolygon::PtInPolygon"), _T("Processing time is %0.6f for %d points"), pc.End(), GetCount() );
  397. return bResult;
  398. }
  399. BOOL CLgDPolygon::CrossProduct( CLgDPoint dp1, CLgDPoint dp2, CLgDLinePointArray &array, BOOL bGlueLines )
  400. {
  401. BOOL bResult = FALSE;
  402. __try
  403. {
  404. bResult = CrossProduct_( dp1, dp2, array, bGlueLines );
  405. }
  406. __except( EXCEPTION_EXECUTE_HANDLER )
  407. {
  408. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgDPolygon::CrossProduct"), _T("Exception 0x%08X"), _exception_code() );
  409. }
  410. return bResult;
  411. }
  412. BOOL CLgDPolygon::PtInPolygon( CLgDPoint pt )
  413. {
  414. BOOL bResult = FALSE;
  415. __try
  416. {
  417. bResult = PtInPolygon_( pt );
  418. }
  419. __except( EXCEPTION_EXECUTE_HANDLER )
  420. {
  421. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgDPolygon::PtInPolygon"), _T("Exception 0x%08X"), _exception_code() );
  422. }
  423. return bResult;
  424. }