MainFrm.cpp 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192
  1. // MainFrm.cpp : implementation of class CMainFrame
  2. //
  3. #include "stdafx.h"
  4. #include "MainFrm.h"
  5. #include "Resource.h"
  6. #include "MAth.h"
  7. #include "ToolsMAth.h"
  8. // CMainFrame
  9. IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)
  10. BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
  11. //{{AFX_MSG_MAP(CMainFrame)
  12. ON_COMMAND(ID_APP_EXIT, OnClose)
  13. ON_WM_LBUTTONDOWN()
  14. ON_WM_LBUTTONUP()
  15. ON_WM_RBUTTONUP()
  16. ON_WM_MOUSEMOVE()
  17. ON_WM_CREATE()
  18. ON_WM_SETCURSOR()
  19. ON_WM_SETFOCUS()
  20. ON_WM_MOVE()
  21. ON_WM_KEYDOWN()
  22. ON_COMMAND(ID_OPENFILE, OnOpenfile)
  23. //}}AFX_MSG_MAP
  24. END_MESSAGE_MAP()
  25. // CMainFrame constructor
  26. CMainFrame::CMainFrame()
  27. {
  28. firstPoint = 1;
  29. m_canChange = FALSE;
  30. m_initialized = FALSE;
  31. m_nbPts =0;
  32. m_bPlaque1 = FALSE;
  33. m_bPlaque2 = FALSE;
  34. m_bPlaque3 = FALSE;
  35. m_bCaptured = FALSE;
  36. m_input = (int *) malloc( 4 * sizeof(int) );
  37. m_points = (unsigned char *) malloc( 1024 * sizeof(unsigned char *) );
  38. }
  39. CMainFrame::~CMainFrame()
  40. {
  41. free(m_input);
  42. free(m_points);
  43. m_image.ReleaseDC();
  44. }
  45. void CMainFrame::SetRect( RECT& rect )
  46. {
  47. m_wRect = rect;
  48. m_cRect = rect;
  49. m_cRect.DeflateRect( 0, 0, 0, 32 );
  50. }
  51. int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
  52. {
  53. if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
  54. return -1;
  55. static UINT indicators[] =
  56. {
  57. ID_SEPARATOR,
  58. ID_SEPARATOR,
  59. ID_SEPARATOR
  60. };
  61. m_sBar.Create( this );
  62. m_sBar.SetIndicators( indicators, 3 );
  63. m_sBar.SetPaneInfo( 2, m_sBar.GetItemID( 2 ), SBPS_NORMAL, 200 );
  64. m_sBar.SetPaneInfo( 1, m_sBar.GetItemID( 1 ), SBPS_NORMAL, 200 );
  65. m_sBar.SetPaneInfo( 0, m_sBar.GetItemID( 0 ), SBPS_STRETCH, NULL );
  66. m_hArrow = AfxGetApp()->LoadStandardCursor( IDC_ARROW );
  67. m_hCross = AfxGetApp()->LoadStandardCursor( IDC_CROSS );
  68. if ( !plaqueInterface.IsValid() )
  69. {
  70. AfxMessageBox( _T( "LibPlaqueDLL.dll not found in the directory of the executable" ) );
  71. return -1;
  72. }
  73. return 0;
  74. }
  75. BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
  76. {
  77. if ( !CFrameWnd::PreCreateWindow(cs) )
  78. return FALSE;
  79. cs.style = WS_VISIBLE | WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU;
  80. return TRUE;
  81. }
  82. // Calcul de la distance entre 2 points
  83. double CMainFrame::computeDistance(CPoint pt1, CPoint pt2)
  84. {
  85. double distance;
  86. distance = sqrt((double) (pt1.x - pt2.x) * (pt1.x - pt2.x) + (pt1.y - pt2.y) * (pt1.y - pt2.y));
  87. return distance;
  88. }
  89. void CMainFrame::onInit()
  90. {
  91. m_bCaptured = FALSE;
  92. m_nbPts = 1;
  93. m_bPlaque1 = FALSE;
  94. m_bPlaque2 = FALSE;
  95. m_bPlaque3 = FALSE;
  96. ReleaseCapture();
  97. m_tbPtsNouveauxTrouvesFin.clear();
  98. if ( !m_image.IsNull() )
  99. {
  100. DrawCurves();
  101. }
  102. }
  103. void CMainFrame::OnRButtonUp( UINT nFlags, CPoint point )
  104. {
  105. onInit();
  106. }
  107. void CMainFrame::OnLButtonDown( UINT nFlags, CPoint point )
  108. {
  109. BOOL trouve;
  110. if ((m_bPlaque3 == TRUE) && (m_bCaptured == FALSE)&& (m_lastPoint.x != point.x) && (m_lastPoint.y != point.y))
  111. {
  112. // On va voir s'il est dans la zone de capture
  113. std::vector< Point >::iterator
  114. p = m_tbPtsNouveauxTrouvesFin.begin(),
  115. pe = m_tbPtsNouveauxTrouvesFin.end();
  116. trouve = FALSE;
  117. while (( p < pe ) && (trouve == FALSE))
  118. {
  119. if (abs((*p).y - point.y) < 10)
  120. {
  121. trouve = TRUE;
  122. }
  123. ++p;
  124. }
  125. // if (trouve == TRUE)
  126. {
  127. // Capture
  128. SetCapture();
  129. ::SetCursor(::LoadCursor(NULL, IDC_ARROW));
  130. // On recherche le point qui est le plus proche de celui cliqué
  131. m_bCaptured = TRUE;
  132. m_previousPoint.x = point.x;
  133. m_previousPoint.y = point.y;
  134. m_pointCaptured.x = point.x;
  135. m_pointCaptured.y = point.y;
  136. std::vector< Point >::iterator
  137. p = m_tbPtsNouveauxTrouvesFin.begin();
  138. pe = m_tbPtsNouveauxTrouvesFin.end();
  139. m_positionxCaptured = 0;
  140. trouve = FALSE;
  141. while (( p < pe ) && (!trouve))
  142. {
  143. if (((*p).x >= m_pointCaptured.x))
  144. {
  145. trouve = TRUE;
  146. }
  147. m_positionxCaptured++;
  148. ++p;
  149. }
  150. m_positionxCaptured--;
  151. // Beep(100, 100);
  152. if ( !m_image.IsNull() )
  153. {
  154. DrawCurves();
  155. }
  156. }
  157. }
  158. }
  159. // Message manager for CMainFrame
  160. void CMainFrame::OnLButtonUp( UINT nFlags, CPoint point )
  161. {
  162. int i;
  163. unsigned char *dadd;
  164. int retour;
  165. CString vStr;
  166. int iVector;
  167. CPoint vPoint;
  168. plaque::PlaqueResult* plaqueResult;
  169. Point oldP;
  170. CPoint pt = point;
  171. ClientToScreen( &pt );
  172. m_canChange = m_cRect.PtInRect( pt );
  173. if ( m_canChange && m_initialized )
  174. {
  175. if (m_nbPts < 2) DrawMyText(TRUE);
  176. if ((m_nbPts > 0) && (m_nbPts < 32) && (m_bPlaque1 == FALSE) && (m_bCaptured == FALSE) && (m_lastPoint.x != point.x) && (m_lastPoint.y != point.y))
  177. {
  178. m_pts[m_nbPts-1].x = point.x;
  179. m_pts[m_nbPts-1].y = point.y;
  180. updateDisplay(point);
  181. }
  182. if ((m_bPlaque1 == FALSE) && (m_bCaptured == FALSE) && (m_lastPoint.x != point.x) && (m_lastPoint.y != point.y))
  183. {
  184. m_nbPts++;
  185. }
  186. if ((m_bPlaque3 == TRUE) && (m_bCaptured == TRUE) && (m_lastPoint.x != point.x) && (m_lastPoint.y != point.y))
  187. {
  188. ReleaseCapture();
  189. m_bCaptured = FALSE;
  190. // On va voir s'il est dans la zone de capture
  191. dadd = m_points;
  192. for (i = 0; i < m_oldnbPts; i++)
  193. {
  194. m_input[0] = ((m_pts[i].x & 0xFF000000) >> 24);
  195. m_input[1] = ((m_pts[i].x & 0x00FF0000) >> 16);
  196. m_input[2] = ((m_pts[i].x & 0x0000FF00) >> 8);
  197. m_input[3] = ((m_pts[i].x & 0x000000FF));
  198. *dadd = m_input[0];
  199. dadd++;
  200. *dadd = m_input[1];
  201. dadd++;
  202. *dadd = m_input[2];
  203. dadd++;
  204. *dadd = m_input[3];
  205. dadd++;
  206. m_input[0] = ((m_pts[i].y & 0xFF000000) >> 24);
  207. m_input[1] = ((m_pts[i].y & 0x00FF0000) >> 16);
  208. m_input[2] = ((m_pts[i].y & 0x0000FF00) >> 8);
  209. m_input[3] = ((m_pts[i].y & 0x000000FF));
  210. *dadd = m_input[0];
  211. dadd++;
  212. *dadd = m_input[1];
  213. dadd++;
  214. *dadd = m_input[2];
  215. dadd++;
  216. *dadd = m_input[3];
  217. dadd++;
  218. }
  219. std::vector< Point >::iterator
  220. p = m_tbPtsNouveauxTrouvesFin.end(),
  221. pe = m_tbPtsNouveauxTrouvesFin.begin();
  222. // Il faut itérer dans le sens inverse pour garder la continuité du contour
  223. if ( p > pe )
  224. {
  225. p--;
  226. }
  227. i = 0;
  228. while ( p > pe )
  229. {
  230. /* if (i == 0)
  231. {
  232. CString vStr;
  233. vStr.Format(_T("%d, %d"),(*p).x, (*p).y);
  234. AfxMessageBox(vStr);
  235. }
  236. */
  237. m_input[0] = (((*p).x & 0xFF000000) >> 24);
  238. m_input[1] = (((*p).x & 0x00FF0000) >> 16);
  239. m_input[2] = (((*p).x & 0x0000FF00) >> 8);
  240. m_input[3] = (((*p).x & 0x000000FF));
  241. *dadd = m_input[0];
  242. dadd++;
  243. *dadd = m_input[1];
  244. dadd++;
  245. *dadd = m_input[2];
  246. dadd++;
  247. *dadd = m_input[3];
  248. dadd++;
  249. m_input[0] = (((*p).y & 0xFF000000) >> 24);
  250. m_input[1] = (((*p).y & 0x00FF0000) >> 16);
  251. m_input[2] = (((*p).y & 0x0000FF00) >> 8);
  252. m_input[3] = (((*p).y & 0x000000FF));
  253. *dadd = m_input[0];
  254. dadd++;
  255. *dadd = m_input[1];
  256. dadd++;
  257. *dadd = m_input[2];
  258. dadd++;
  259. *dadd = m_input[3];
  260. dadd++;
  261. i++;
  262. p--;
  263. }
  264. retour = plaqueInterface.CalculerPlaqueManuelle( m_oldnbPts + i, m_points );
  265. plaqueResult = plaqueInterface.GetResult();
  266. DrawMyText(FALSE);
  267. }
  268. if ((m_bPlaque2 == TRUE) && (m_bPlaque3 == FALSE) && (m_lastPoint.x != point.x) && (m_lastPoint.y != point.y) && (m_bCaptured == FALSE))
  269. {
  270. dadd = m_points;
  271. for (i = 0; i < m_nbPts; i++)
  272. {
  273. m_input[0] = ((m_pts[i].x & 0xFF000000) >> 24);
  274. m_input[1] = ((m_pts[i].x & 0x00FF0000) >> 16);
  275. m_input[2] = ((m_pts[i].x & 0x0000FF00) >> 8);
  276. m_input[3] = ((m_pts[i].x & 0x000000FF));
  277. *dadd = m_input[0];
  278. dadd++;
  279. *dadd = m_input[1];
  280. dadd++;
  281. *dadd = m_input[2];
  282. dadd++;
  283. *dadd = m_input[3];
  284. dadd++;
  285. m_input[0] = ((m_pts[i].y & 0xFF000000) >> 24);
  286. m_input[1] = ((m_pts[i].y & 0x00FF0000) >> 16);
  287. m_input[2] = ((m_pts[i].y & 0x0000FF00) >> 8);
  288. m_input[3] = ((m_pts[i].y & 0x000000FF));
  289. *dadd = m_input[0];
  290. dadd++;
  291. *dadd = m_input[1];
  292. dadd++;
  293. *dadd = m_input[2];
  294. dadd++;
  295. *dadd = m_input[3];
  296. dadd++;
  297. }
  298. retour = plaqueInterface.computePlaque( point.x, point.y, m_nbPts, m_points, 60, 90 );
  299. plaqueResult = plaqueInterface.GetResult();
  300. for (iVector = 0; iVector < plaqueResult->m_nVecteursTrouves - 1; iVector++)
  301. {
  302. oldP.x = plaqueResult->m_tbPtsTrouvesFin[iVector].x;
  303. oldP.y = plaqueResult->m_tbPtsTrouvesFin[iVector].y;
  304. m_tbPtsNouveauxTrouvesFin.push_back( oldP );
  305. }
  306. ::SetCursor( m_hCross );
  307. m_bPlaque3 = TRUE;
  308. // vStr.Format(_T("C1 = %d, C2 = %d, C3 = %d, C4 = %d, C5 = %d,"), plaqueResult->code_debug1, plaqueResult->code_debug2, plaqueResult->code_debug3, plaqueResult->code_debug4, plaqueResult->code_debug5);
  309. // m_sBar.SetPaneText( 0, vStr );
  310. if (!m_image.IsNull())
  311. {
  312. DrawCurves();
  313. }
  314. DrawMyText(FALSE);
  315. m_oldnbPts = m_nbPts;
  316. // onInit();
  317. m_nbPts = 1;
  318. m_bPlaque1 = FALSE;
  319. m_bPlaque2 = FALSE;
  320. }
  321. if ((m_bPlaque3 == TRUE) && (m_bCaptured == TRUE))
  322. {
  323. /* m_newPoint.x = point.x;
  324. m_newPoint.y = point.y;
  325. minDist = INT_MAX;
  326. m_posModif = 0;
  327. std::vector< Point >::iterator
  328. p = m_tbPtsNouveauxTrouvesFin.begin(),
  329. pe = m_tbPtsNouveauxTrouvesFin.end();
  330. iVector = 0;
  331. while ( p < pe )
  332. {
  333. vPoint.x = (*p).x;
  334. vPoint.y = (*p).y;
  335. vDist = computeDistance(vPoint, m_newPoint);
  336. if (vDist < minDist)
  337. {
  338. minDist = vDist;
  339. m_posModif = iVector;
  340. }
  341. iVector++;
  342. ++p;
  343. }
  344. p = m_tbPtsNouveauxTrouvesFin.begin();
  345. pe = m_tbPtsNouveauxTrouvesFin.end();
  346. iVector = 0;
  347. while ( p < pe )
  348. {
  349. if (iVector == m_posModif)
  350. {
  351. (*p).x = point.x;
  352. (*p).y = point.y;
  353. }
  354. iVector++;
  355. ++p;
  356. }
  357. DrawCurves();
  358. */ }
  359. // Double click
  360. if ((m_bPlaque1 == FALSE) && (m_nbPts > 3) && (m_bCaptured == FALSE) && (m_bPlaque3 == FALSE))
  361. {
  362. if ((m_lastPoint.x == point.x) && (m_lastPoint.y == point.y))
  363. {
  364. // Remplissage du nouveau tableau de points
  365. plaqueResult = plaqueInterface.GetResult();
  366. if (m_bPlaque1 == FALSE)
  367. {
  368. m_bPlaque1 = TRUE;
  369. m_nbPts -=1;
  370. }
  371. if ( (m_bPlaque1 == TRUE) && (m_bPlaque2 == FALSE) )
  372. {
  373. m_bPlaque2 = TRUE;
  374. ::SetCursor (theApp.LoadCursor (IDC_TACHE));
  375. updateDisplay(point);
  376. }
  377. }
  378. }
  379. m_lastPoint.x = point.x;
  380. m_lastPoint.y = point.y;
  381. // retour = plaqueInterface.OnLButtonUp( point.x, point.y );
  382. /*
  383. m_pts[0].x = 267;
  384. m_pts[0].y = 218;
  385. m_pts[1].x = 300;
  386. m_pts[1].y = 227;
  387. m_pts[2].x = 364;
  388. m_pts[2].y = 232;
  389. m_pts[3].x = 446;
  390. m_pts[3].y = 238;
  391. m_pts[4].x = 509;
  392. m_pts[4].y = 238;
  393. m_pts[5].x = 561;
  394. m_pts[5].y = 234;
  395. */
  396. }
  397. }
  398. void CMainFrame::updateDisplay(CPoint point)
  399. {
  400. int i;
  401. CDC *pDC = GetDC();
  402. CRect rect;
  403. GetClientRect(&rect);
  404. m_image.StretchBlt( pDC->m_hDC, rect.left, rect.top, m_cRect.Width(), m_cRect.Height() );
  405. CPen pen( PS_SOLID, 2, RGB( 255, 0, 255 ) );
  406. CPen *pOldPen = pDC->SelectObject( &pen );
  407. if (m_nbPts > 1)
  408. {
  409. for (i = 1; i < m_nbPts-1; i++)
  410. {
  411. pDC->MoveTo( m_pts[i-1].x, m_pts[i-1].y );
  412. pDC->LineTo( m_pts[i].x, m_pts[i].y );
  413. }
  414. pDC->MoveTo( m_pts[m_nbPts-2].x, m_pts[m_nbPts-2].y );
  415. pDC->LineTo( point );
  416. pDC->SelectObject( pOldPen );
  417. }
  418. }
  419. void CMainFrame::OnMouseMove(UINT nFlags, CPoint point)
  420. {
  421. int i, rayon;
  422. dpoint p0, d1, d2;
  423. dpoint sr1, sr2;
  424. BOOL askReplace;
  425. dpoint vPoint, vPoint2;
  426. int iVector;
  427. std::vector< Point >::iterator p, pe;
  428. CString vStr;
  429. int ng;
  430. CString text;
  431. rayon = 7;
  432. CDC *pDC = GetDC();
  433. if (pDC != NULL)
  434. {
  435. COLORREF colorPixel = pDC->GetPixel(point.x, point.y);
  436. ng = (GetRValue(colorPixel) + GetGValue(colorPixel) + GetBValue(colorPixel)) / 3;
  437. text.Format( _T("x,y: %d,%d Gsl: %d"),point.x,point.y,ng );
  438. m_sBar.SetPaneText (2, text);
  439. }
  440. if (((m_bPlaque3 == TRUE) && (m_bCaptured == TRUE)) || (GetKeyState (VK_UP) < 0) || (GetKeyState (VK_DOWN) < 0) || (GetKeyState (VK_RIGHT) < 0) || (GetKeyState (VK_LEFT) < 0))
  441. {
  442. // Point du centre
  443. // p0.x = (double) m_pointCaptured.x;
  444. p0.x = (double) m_pointCaptured.x;
  445. /*
  446. deltay = (m_previousPoint.y - point.y);
  447. // if (deltay > 4) deltay /= 2;
  448. p = m_tbPtsNouveauxTrouvesFin.begin();
  449. pe = m_tbPtsNouveauxTrouvesFin.end();
  450. while ( p < pe )
  451. {
  452. if ((*p).x == p0.x)
  453. {
  454. if (m_previousPoint.y >= point.y)
  455. {
  456. // Beep(100, 100);
  457. p0.y = (double) point.y - rayon;
  458. // vStr.Format(_T("%d"), deltay);
  459. // AfxMessageBox(vStr);
  460. }
  461. else
  462. {
  463. p0.y = (double) point.y + rayon;
  464. }
  465. }
  466. ++p
  467. }
  468. */
  469. if (m_previousPoint.y >= point.y) // En montant
  470. {
  471. p0.y = (double) point.y - rayon;
  472. }
  473. else
  474. {
  475. p0.y = (double) point.y + rayon;
  476. }
  477. for (i = -rayon; i < rayon; i++)
  478. {
  479. d1.x = p0.x + i;
  480. d1.y = p0.y;
  481. d2.x = p0.x + i;
  482. d2.y = p0.y + 2;
  483. // On rechercle le point sur la courbe
  484. p = m_tbPtsNouveauxTrouvesFin.begin();
  485. pe = m_tbPtsNouveauxTrouvesFin.end();
  486. iVector = 0;
  487. while ( p < pe )
  488. {
  489. if ((*p).x == d1.x)
  490. {
  491. vPoint.x = d1.x;
  492. vPoint.y = (*p).y;
  493. m_posModif = iVector;
  494. }
  495. iVector++;
  496. ++p;
  497. }
  498. InterCercleDroite(p0, rayon, d1, d2, &sr1, &sr2);
  499. askReplace = FALSE;
  500. if (m_previousPoint.y >= point.y)
  501. {
  502. if (sr1.y < p0.y) // En montant : Demi cercle du haut
  503. {
  504. if ((fabs(vPoint.y - p0.y) < rayon) && (vPoint.y >= sr1.y))
  505. {
  506. askReplace = TRUE;
  507. vPoint2.x = vPoint.x;
  508. vPoint2.y = sr1.y;
  509. }
  510. }
  511. else
  512. {
  513. if ((fabs(vPoint.y - p0.y) < rayon) && (vPoint.y >= sr2.y))
  514. {
  515. askReplace = TRUE;
  516. vPoint2.x = vPoint.x;
  517. vPoint2.y = sr2.y;
  518. }
  519. }
  520. }
  521. else
  522. {
  523. if (sr1.y > p0.y) // En descendant : Demi cercle du haut
  524. {
  525. if ((fabs(vPoint.y - p0.y) < rayon) && (vPoint.y < sr1.y))
  526. {
  527. askReplace = TRUE;
  528. vPoint2.x = vPoint.x;
  529. vPoint2.y = sr1.y;
  530. }
  531. }
  532. else
  533. {
  534. if ((fabs(vPoint.y - p0.y) < rayon) && (vPoint.y < sr2.y))
  535. {
  536. askReplace = TRUE;
  537. vPoint2.x = vPoint.x;
  538. vPoint2.y = sr2.y;
  539. }
  540. }
  541. }
  542. if ((askReplace == TRUE) && ((m_keyPress == 3) || (m_keyPress == 4)))
  543. {
  544. p = m_tbPtsNouveauxTrouvesFin.begin();
  545. pe = m_tbPtsNouveauxTrouvesFin.end();
  546. iVector = 0;
  547. while ( p < pe )
  548. {
  549. if (iVector == m_posModif)
  550. {
  551. (*p).x = (int) vPoint2.x;
  552. (*p).y = (int) vPoint2.y;
  553. }
  554. iVector++;
  555. ++p;
  556. }
  557. }
  558. }
  559. m_previousPoint.x = point.x;
  560. m_previousPoint.y = point.y;
  561. DrawCurves();
  562. }
  563. if ((m_bPlaque1 == FALSE) && (m_nbPts > 1))
  564. {
  565. updateDisplay(point);
  566. CDC *pDC = GetDC();
  567. CPen pen( PS_SOLID, 1, RGB( 0, 255, 0 ) );
  568. CPen *pOldPen = pDC->SelectObject( &pen );
  569. pDC->SelectObject( pOldPen );
  570. PrintDistance( point );
  571. }
  572. }
  573. BOOL CMainFrame::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
  574. {
  575. if ( m_initialized && firstPoint && m_canChange && ( nHitTest == HTCLIENT ))
  576. {
  577. if ((m_bPlaque1 != TRUE) || (m_bPlaque3 == TRUE))
  578. {
  579. ::SetCursor(::LoadCursor(NULL, IDC_ARROW));
  580. return TRUE;
  581. }
  582. else if ((m_bPlaque1 != TRUE) || (m_bPlaque3 == TRUE))
  583. {
  584. ::SetCursor( m_hCross );
  585. }
  586. else
  587. {
  588. ::SetCursor (theApp.LoadCursor (IDC_TACHE));
  589. return TRUE;
  590. }
  591. }
  592. return CFrameWnd::OnSetCursor( pWnd, nHitTest, message );
  593. }
  594. void CMainFrame::OnSetFocus( CWnd* wnd )
  595. {
  596. CFrameWnd::OnSetFocus( wnd );
  597. if ( !m_image.IsNull() )
  598. {
  599. DrawCurves();
  600. }
  601. }
  602. void CMainFrame::OnMove( int cx, int cy )
  603. {
  604. CFrameWnd::OnMove( cx, cy );
  605. if ( !m_image.IsNull() )
  606. {
  607. DrawCurves();
  608. }
  609. }
  610. void CMainFrame::OnDraw( CDC* pDC )
  611. {
  612. if ( !m_image.IsNull() )
  613. {
  614. DrawCurves();
  615. }
  616. }
  617. CRect CMainFrame::GetTextRect()
  618. {
  619. CRect rect( 0, m_cRect.Height(), m_wRect.Width(), m_wRect.Height() );
  620. return rect;
  621. }
  622. void CMainFrame::DrawPoint( CDC *pDC, CPoint& pt, COLORREF color )
  623. {
  624. pDC->SetPixel( pt, color );
  625. }
  626. void CMainFrame::DrawCurves()
  627. {
  628. CDC *pDC = GetDC();
  629. CRect rect;
  630. GetClientRect(&rect);
  631. CBrush brush( RGB( 0, 0, 0 ) );
  632. pDC->FillRect( GetTextRect(), &brush );
  633. m_image.StretchBlt( pDC->m_hDC, rect.left, rect.top, m_cRect.Width(), m_cRect.Height() );
  634. if ( !m_image.IsNull() )
  635. {
  636. plaque::PlaqueResult* plaqueResult = plaqueInterface.GetResult();
  637. if (((plaqueResult->m_nVecteursTrouves > 0) || (plaqueResult->m_nPtLongeantPlaque > 0)) && (m_bPlaque1||m_bPlaque2||m_bPlaque3 || (m_bCaptured)) )
  638. {
  639. short iVector;
  640. CPen *pOldPen;
  641. CString vStr;
  642. if (plaqueResult->m_type == typeRGB)
  643. {
  644. pOldPen = pDC->SelectObject (&theApp.m_penVert);
  645. }
  646. else
  647. {
  648. pOldPen = pDC->SelectObject (&theApp.m_penDraw);
  649. }
  650. if (plaqueResult->m_Etape == etapeTacheSeuil)
  651. {
  652. // Mesure en cours de saisie : Afficher croix au pt du seuil inférieur
  653. pDC->MoveTo (plaqueResult->m_ptInferieur.x - 4, plaqueResult->m_ptInferieur.y);
  654. pDC->LineTo (plaqueResult->m_ptInferieur.x + 5, plaqueResult->m_ptInferieur.y);
  655. pDC->MoveTo (plaqueResult->m_ptInferieur.x, plaqueResult->m_ptInferieur.y - 4);
  656. pDC->LineTo (plaqueResult->m_ptInferieur.x, plaqueResult->m_ptInferieur.y + 5);
  657. }
  658. /* Draw the contour of the plaque */
  659. /* if ( plaqueResult->m_ptList.size() )
  660. {
  661. pDC->MoveTo( plaqueResult->m_ptList[0].x, plaqueResult->m_ptList[0].y );
  662. for (int i = 1; i < plaqueResult->m_ptList.size(); ++i)
  663. {
  664. pDC->LineTo( plaqueResult->m_ptList[i].x, plaqueResult->m_ptList[i].y );
  665. }
  666. }
  667. */
  668. pDC->SelectObject (&theApp.m_penVert);
  669. // pDC->MoveTo (plaqueResult->m_tbPtsTrouvesFin [0].x, plaqueResult->m_tbPtsTrouvesFin [0].y);
  670. for (iVector = 0; iVector < plaqueResult->m_nVecteursTrouves - 1; iVector++)
  671. {
  672. // pDC->LineTo (plaqueResult->m_tbPtsTrouvesFin [iVector + 1].x, plaqueResult->m_tbPtsTrouvesFin [iVector + 1].y);
  673. }
  674. pDC->MoveTo (plaqueResult->m_tPtLongeantPlaque[0].x, plaqueResult->m_tPtLongeantPlaque[0].y);
  675. for (iVector = 1; iVector < plaqueResult->m_nPtLongeantPlaque; iVector++)
  676. {
  677. pDC->LineTo(plaqueResult->m_tPtLongeantPlaque[iVector].x, plaqueResult->m_tPtLongeantPlaque[iVector].y);
  678. }
  679. pDC->SelectObject (&theApp.m_penBleu);
  680. if ((m_bPlaque3) || (m_bCaptured))
  681. {
  682. m_spline.fit( m_tbPtsNouveauxTrouvesFin );
  683. std::vector< Point > fit = m_spline.getValues( 20 );
  684. std::vector< Point >::iterator
  685. p = fit.begin(),
  686. pe = fit.end();
  687. iVector = 0;
  688. if ( p < pe )
  689. {
  690. pDC->MoveTo( (*p).x, (*p).y );
  691. ++p;
  692. }
  693. while ( p < pe )
  694. {
  695. if ((*p).x == (m_pointCaptured.x))
  696. {
  697. pDC->SelectObject (&theApp.m_penRouge);
  698. }
  699. else if ( ((*p).x >= (m_pointCaptured.x - 8)) && ((*p).x <= (m_pointCaptured.x + 8)) )
  700. {
  701. pDC->SelectObject (&theApp.m_penVert);
  702. }
  703. else
  704. {
  705. pDC->SelectObject (&theApp.m_penBleu);
  706. }
  707. pDC->LineTo( (*p).x, (*p).y );
  708. ++p;
  709. }
  710. }
  711. }
  712. if (plaqueResult->m_Etape == etapeSegments)
  713. {
  714. // pDC->LineTo (ptCurrent);
  715. }
  716. else if ( (plaqueResult->m_Etape == etapeReglage)
  717. || (plaqueResult->m_Etape == etapeTacheSeuil))
  718. {
  719. // if ( (plaqueResult->m_pointLimite1.x != 0)
  720. // || (plaqueResult->m_pointLimite1.y != 0))
  721. {
  722. // pOldPen = pDC->SelectObject (&theApp.m_penDraw);
  723. // pDC->LineTo (plaqueResult->m_pointLimite1.x, plaqueResult->m_pointLimite1.y);//
  724. // pDC->MoveTo (plaqueResult->m_pointLimite2.x, plaqueResult->m_pointLimite2.y);
  725. // pDC->LineTo (plaqueResult->m_tPtLongeantPlaque [0].x, plaqueResult->m_tPtLongeantPlaque [0].y);
  726. }
  727. }
  728. }
  729. }
  730. void CMainFrame::DrawMyText(BOOL init)
  731. {
  732. CDC *pDC = GetDC();
  733. CString text;
  734. plaque::PlaqueResult* plaqueResult = plaqueInterface.GetResult();
  735. if (init == TRUE)
  736. {
  737. text.Format( IDS_DEMO_MEASURE_PLAQUE,
  738. 0.0,
  739. 0.0,
  740. 0,
  741. 0.0,
  742. 0.0);
  743. }
  744. else
  745. {
  746. text.Format( IDS_DEMO_MEASURE_PLAQUE,
  747. plaqueResult->plaque_max_thickness,
  748. plaqueResult->plaque_mean_thickness,
  749. plaqueResult->numberOfMeasures,
  750. plaqueResult->plaque_area,
  751. plaqueResult->plaque_mean_density);
  752. }
  753. PrintText( text );
  754. }
  755. void CMainFrame::PrintCoordinates( CPoint pt )
  756. {
  757. CString text;
  758. text.Format( IDS_DEMO_COORDINATES, pt.x, pt.y );
  759. m_sBar.SetPaneText( 1, text );
  760. }
  761. void CMainFrame::PrintDistance( CPoint pt )
  762. {
  763. CString text;
  764. float d = plaqueInterface.getDistanceToFirstPoint( pt.x, pt.y, m_pts[0].x, m_pts[0].y);
  765. text.Format( IDS_DEMO_LONGUEUR, pt.x, pt.y, d );
  766. m_sBar.SetPaneText( 0, text );
  767. }
  768. void CMainFrame::PrintText( CString text )
  769. {
  770. CDC *pDC = GetDC();
  771. DRAWTEXTPARAMS textParam;
  772. textParam.cbSize = sizeof( DRAWTEXTPARAMS );
  773. textParam.iLeftMargin = 0;
  774. textParam.iRightMargin = 0;
  775. textParam.iTabLength = 3;
  776. textParam.uiLengthDrawn = 0;
  777. COLORREF colorText = pDC->SetTextColor( RGB( 255, 255, 255 ) );
  778. COLORREF colorBk = pDC->SetBkColor( RGB( 0, 0, 0 ) );
  779. CFont newFont;
  780. newFont.CreateFont( 12, 0, 0, 0, FW_THIN, FALSE, FALSE, FALSE,
  781. ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
  782. NONANTIALIASED_QUALITY ,FIXED_PITCH | FF_MODERN,
  783. _T( "Courier" ) );
  784. CFont* pOldFont = pDC->SelectObject( &newFont );
  785. CBrush brush( RGB( 0, 0, 0 ) );
  786. pDC->FillRect( GetTextRect(), &brush );
  787. pDC->DrawTextEx( text, GetTextRect(), DT_NOCLIP | DT_VCENTER, &textParam );
  788. pDC->SelectObject( pOldFont );
  789. if ( newFont.m_hObject != NULL )
  790. newFont.Detach();
  791. pDC->SetTextColor( colorText );
  792. pDC->SetBkColor( colorBk );
  793. }
  794. void CMainFrame::OnKeyDown (UINT nChar, UINT nRepCnt, UINT nFlags)
  795. {
  796. CPoint point;
  797. int rayon;
  798. rayon = 7;
  799. bool trouve;
  800. int iVector;
  801. m_keyPress = 0;
  802. switch (nChar)
  803. {
  804. case VK_RIGHT:
  805. {
  806. m_keyPress = 1;
  807. std::vector< Point >::iterator
  808. p = m_tbPtsNouveauxTrouvesFin.begin(),
  809. pe = m_tbPtsNouveauxTrouvesFin.end();
  810. trouve = false;
  811. iVector = 0;
  812. while (( p < pe ) && (!trouve))
  813. {
  814. if (iVector == (m_positionxCaptured + 1))
  815. {
  816. point.x = (*p).x;
  817. m_pointCaptured.x = point.x;
  818. point.y = (*p).y + rayon + 6;
  819. m_previousPoint.y = point.y + rayon;
  820. trouve = true;
  821. m_positionxCaptured++;
  822. OnMouseMove(nFlags, point);
  823. DrawCurves();
  824. }
  825. iVector++;
  826. ++p ;
  827. }
  828. }
  829. break;
  830. case VK_LEFT:
  831. {
  832. m_keyPress = 2;
  833. std::vector< Point >::iterator
  834. p = m_tbPtsNouveauxTrouvesFin.begin(),
  835. pe = m_tbPtsNouveauxTrouvesFin.end();
  836. trouve = false;
  837. iVector = 0;
  838. while (( p < pe ) && (!trouve))
  839. {
  840. if (iVector == (m_positionxCaptured - 1))
  841. {
  842. point.x = (*p).x;
  843. m_pointCaptured.x = point.x;
  844. point.y = (*p).y + rayon + 6;
  845. m_previousPoint.y = point.y + rayon;
  846. trouve = true;
  847. m_positionxCaptured--;
  848. OnMouseMove(nFlags, point);
  849. DrawCurves();
  850. }
  851. iVector++;
  852. ++p ;
  853. }
  854. }
  855. break;
  856. case VK_UP:
  857. {
  858. m_keyPress = 3;
  859. std::vector< Point >::iterator
  860. p = m_tbPtsNouveauxTrouvesFin.begin(),
  861. pe = m_tbPtsNouveauxTrouvesFin.end();
  862. iVector = 0;
  863. trouve = false;
  864. while (( p < pe )&& (!trouve))
  865. {
  866. if (iVector == m_positionxCaptured )
  867. {
  868. trouve = true;
  869. point.x = (*p).x;
  870. point.y = (*p).y + rayon + 6;
  871. m_previousPoint.y = point.y + rayon;
  872. OnMouseMove(nFlags, point);
  873. DrawCurves();
  874. }
  875. iVector++;
  876. ++p ;
  877. }
  878. }
  879. break;
  880. case VK_DOWN:
  881. {
  882. m_keyPress = 4;
  883. std::vector< Point >::iterator
  884. p = m_tbPtsNouveauxTrouvesFin.begin(),
  885. pe = m_tbPtsNouveauxTrouvesFin.end();
  886. iVector = 0;
  887. trouve = false;
  888. while (( p < pe ) && (!trouve))
  889. {
  890. if (iVector == m_positionxCaptured )
  891. {
  892. trouve = true;
  893. point.x = (*p).x;
  894. point.y = (*p).y - rayon - 6;
  895. m_previousPoint.y = point.y - rayon;
  896. OnMouseMove(nFlags, point);
  897. DrawCurves();
  898. }
  899. iVector++;
  900. ++p ;
  901. }
  902. }
  903. break;
  904. }
  905. }
  906. void CMainFrame::OnOpenfile()
  907. {
  908. CPoint point;
  909. point.x = 0;
  910. point.y = 0;
  911. CFileDialog cfd( TRUE, NULL, NULL,
  912. OFN_HIDEREADONLY | OFN_PATHMUSTEXIST |
  913. OFN_FILEMUSTEXIST | OFN_EXPLORER,
  914. _T( "Test Dicom file|*|Test jpgbmp|*.jpg;*.jpeg;*.bmp||" ), NULL );
  915. if ( cfd.DoModal() == IDOK )
  916. {
  917. m_image.Destroy();
  918. onInit();
  919. m_nbPts = 0;
  920. USES_CONVERSION;
  921. if ( cfd.GetOFN().nFilterIndex == 1 )
  922. {
  923. m_initialized = plaqueInterface.initializeFromFile( T2A( cfd.GetPathName() ) );
  924. if ( m_initialized )
  925. {
  926. int x, dimX = plaqueInterface.getImageWidth();
  927. int y, dimY = plaqueInterface.getImageHeight();
  928. m_image.Create( dimX, dimY, plaqueInterface.getImageBitsPerPixel() );
  929. char* buffer = plaqueInterface.getPixelArray();
  930. for ( y = 0; y < dimY; y++ )
  931. {
  932. for ( x = 0; x < dimX; x++ )
  933. {
  934. m_image.SetPixelRGB( x, y, *buffer++, *buffer++, *buffer++ );
  935. }
  936. }
  937. }
  938. }
  939. else
  940. {
  941. if ( SUCCEEDED( m_image.Load( cfd.GetPathName() ) ) )
  942. {
  943. char* buffer = NULL;
  944. int width = m_image.GetWidth();
  945. int height = m_image.GetHeight();
  946. int x, y, bpp;
  947. unsigned long count = width * height;
  948. // test for a 24 bits buffer
  949. bpp = 24;
  950. buffer = new char[ 3 * count ];
  951. if ( buffer )
  952. {
  953. char* p = buffer;
  954. for ( y = 0; y < height; y++ )
  955. {
  956. for ( x = 0; x < width; x++ )
  957. {
  958. *p++ = GetRValue( m_image.GetPixel( x, y ) );
  959. *p++ = GetGValue( m_image.GetPixel( x, y ) );
  960. *p++ = GetBValue( m_image.GetPixel( x, y ) );
  961. }
  962. }
  963. }
  964. if ( buffer )
  965. {
  966. m_initialized = plaqueInterface.initializeFromRaw( buffer, width, height, bpp, FALSE, 0.0826440, 0.0826440 );
  967. }
  968. }
  969. }
  970. if ( m_initialized )
  971. {
  972. plaqueInterface.Clear();
  973. ResizeWindow();
  974. Invalidate( FALSE );
  975. updateDisplay(point);
  976. DrawMyText(TRUE);
  977. }
  978. else
  979. {
  980. AfxMessageBox( _T( "File cannot be loaded" ), MB_ICONSTOP | MB_OK );
  981. }
  982. }
  983. }
  984. void CMainFrame::ResizeWindow()
  985. {
  986. CRect winRect;
  987. CRect cRect;
  988. CRect sRect;
  989. GetWindowRect( &winRect );
  990. GetClientRect( &cRect );
  991. int imgWidth = plaqueInterface.getImageWidth();
  992. int imgHeight = plaqueInterface.getImageHeight();
  993. m_sBar.GetWindowRect( &sRect );
  994. m_cRect = CRect( 0, 0, imgWidth, imgHeight );
  995. m_wRect = m_cRect;
  996. m_wRect.InflateRect( 0, 0, 0, 32 );
  997. ClientToScreen( &m_cRect );
  998. ClientToScreen( &m_wRect );
  999. SetWindowPos( NULL, 0, 0,
  1000. winRect.Width() + imgWidth - cRect.Width(),
  1001. winRect.Height() + imgHeight + 32 +
  1002. sRect.Height() - cRect.Height(),
  1003. SWP_NOMOVE | SWP_NOZORDER );
  1004. DrawCurves();
  1005. }