| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192 |
- // MainFrm.cpp : implementation of class CMainFrame
- //
- #include "stdafx.h"
- #include "MainFrm.h"
- #include "Resource.h"
- #include "MAth.h"
- #include "ToolsMAth.h"
- // CMainFrame
- IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)
- BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
- //{{AFX_MSG_MAP(CMainFrame)
- ON_COMMAND(ID_APP_EXIT, OnClose)
- ON_WM_LBUTTONDOWN()
- ON_WM_LBUTTONUP()
- ON_WM_RBUTTONUP()
- ON_WM_MOUSEMOVE()
- ON_WM_CREATE()
- ON_WM_SETCURSOR()
- ON_WM_SETFOCUS()
- ON_WM_MOVE()
- ON_WM_KEYDOWN()
- ON_COMMAND(ID_OPENFILE, OnOpenfile)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- // CMainFrame constructor
- CMainFrame::CMainFrame()
- {
- firstPoint = 1;
- m_canChange = FALSE;
- m_initialized = FALSE;
- m_nbPts =0;
- m_bPlaque1 = FALSE;
- m_bPlaque2 = FALSE;
- m_bPlaque3 = FALSE;
- m_bCaptured = FALSE;
- m_input = (int *) malloc( 4 * sizeof(int) );
- m_points = (unsigned char *) malloc( 1024 * sizeof(unsigned char *) );
- }
- CMainFrame::~CMainFrame()
- {
- free(m_input);
- free(m_points);
- m_image.ReleaseDC();
- }
- void CMainFrame::SetRect( RECT& rect )
- {
- m_wRect = rect;
- m_cRect = rect;
- m_cRect.DeflateRect( 0, 0, 0, 32 );
- }
- int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
- {
- if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
- return -1;
- static UINT indicators[] =
- {
- ID_SEPARATOR,
- ID_SEPARATOR,
- ID_SEPARATOR
- };
- m_sBar.Create( this );
- m_sBar.SetIndicators( indicators, 3 );
- m_sBar.SetPaneInfo( 2, m_sBar.GetItemID( 2 ), SBPS_NORMAL, 200 );
- m_sBar.SetPaneInfo( 1, m_sBar.GetItemID( 1 ), SBPS_NORMAL, 200 );
- m_sBar.SetPaneInfo( 0, m_sBar.GetItemID( 0 ), SBPS_STRETCH, NULL );
- m_hArrow = AfxGetApp()->LoadStandardCursor( IDC_ARROW );
- m_hCross = AfxGetApp()->LoadStandardCursor( IDC_CROSS );
- if ( !plaqueInterface.IsValid() )
- {
- AfxMessageBox( _T( "LibPlaqueDLL.dll not found in the directory of the executable" ) );
- return -1;
- }
- return 0;
- }
- BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
- {
- if ( !CFrameWnd::PreCreateWindow(cs) )
- return FALSE;
- cs.style = WS_VISIBLE | WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU;
- return TRUE;
- }
- // Calcul de la distance entre 2 points
- double CMainFrame::computeDistance(CPoint pt1, CPoint pt2)
- {
- double distance;
- distance = sqrt((double) (pt1.x - pt2.x) * (pt1.x - pt2.x) + (pt1.y - pt2.y) * (pt1.y - pt2.y));
- return distance;
- }
- void CMainFrame::onInit()
- {
- m_bCaptured = FALSE;
- m_nbPts = 1;
- m_bPlaque1 = FALSE;
- m_bPlaque2 = FALSE;
- m_bPlaque3 = FALSE;
- ReleaseCapture();
- m_tbPtsNouveauxTrouvesFin.clear();
- if ( !m_image.IsNull() )
- {
- DrawCurves();
- }
- }
- void CMainFrame::OnRButtonUp( UINT nFlags, CPoint point )
- {
- onInit();
- }
- void CMainFrame::OnLButtonDown( UINT nFlags, CPoint point )
- {
- BOOL trouve;
- if ((m_bPlaque3 == TRUE) && (m_bCaptured == FALSE)&& (m_lastPoint.x != point.x) && (m_lastPoint.y != point.y))
- {
- // On va voir s'il est dans la zone de capture
- std::vector< Point >::iterator
- p = m_tbPtsNouveauxTrouvesFin.begin(),
- pe = m_tbPtsNouveauxTrouvesFin.end();
- trouve = FALSE;
- while (( p < pe ) && (trouve == FALSE))
- {
- if (abs((*p).y - point.y) < 10)
- {
- trouve = TRUE;
- }
- ++p;
- }
- // if (trouve == TRUE)
- {
- // Capture
- SetCapture();
-
- ::SetCursor(::LoadCursor(NULL, IDC_ARROW));
- // On recherche le point qui est le plus proche de celui cliqué
- m_bCaptured = TRUE;
- m_previousPoint.x = point.x;
- m_previousPoint.y = point.y;
- m_pointCaptured.x = point.x;
- m_pointCaptured.y = point.y;
- std::vector< Point >::iterator
- p = m_tbPtsNouveauxTrouvesFin.begin();
- pe = m_tbPtsNouveauxTrouvesFin.end();
-
- m_positionxCaptured = 0;
- trouve = FALSE;
- while (( p < pe ) && (!trouve))
- {
- if (((*p).x >= m_pointCaptured.x))
- {
- trouve = TRUE;
- }
- m_positionxCaptured++;
- ++p;
- }
- m_positionxCaptured--;
- // Beep(100, 100);
- if ( !m_image.IsNull() )
- {
- DrawCurves();
- }
- }
- }
- }
- // Message manager for CMainFrame
- void CMainFrame::OnLButtonUp( UINT nFlags, CPoint point )
- {
- int i;
- unsigned char *dadd;
- int retour;
- CString vStr;
- int iVector;
- CPoint vPoint;
- plaque::PlaqueResult* plaqueResult;
- Point oldP;
- CPoint pt = point;
- ClientToScreen( &pt );
- m_canChange = m_cRect.PtInRect( pt );
-
- if ( m_canChange && m_initialized )
- {
- if (m_nbPts < 2) DrawMyText(TRUE);
- if ((m_nbPts > 0) && (m_nbPts < 32) && (m_bPlaque1 == FALSE) && (m_bCaptured == FALSE) && (m_lastPoint.x != point.x) && (m_lastPoint.y != point.y))
- {
- m_pts[m_nbPts-1].x = point.x;
- m_pts[m_nbPts-1].y = point.y;
- updateDisplay(point);
- }
- if ((m_bPlaque1 == FALSE) && (m_bCaptured == FALSE) && (m_lastPoint.x != point.x) && (m_lastPoint.y != point.y))
- {
- m_nbPts++;
- }
- if ((m_bPlaque3 == TRUE) && (m_bCaptured == TRUE) && (m_lastPoint.x != point.x) && (m_lastPoint.y != point.y))
- {
- ReleaseCapture();
- m_bCaptured = FALSE;
- // On va voir s'il est dans la zone de capture
- dadd = m_points;
- for (i = 0; i < m_oldnbPts; i++)
- {
- m_input[0] = ((m_pts[i].x & 0xFF000000) >> 24);
- m_input[1] = ((m_pts[i].x & 0x00FF0000) >> 16);
- m_input[2] = ((m_pts[i].x & 0x0000FF00) >> 8);
- m_input[3] = ((m_pts[i].x & 0x000000FF));
- *dadd = m_input[0];
- dadd++;
- *dadd = m_input[1];
- dadd++;
- *dadd = m_input[2];
- dadd++;
- *dadd = m_input[3];
- dadd++;
- m_input[0] = ((m_pts[i].y & 0xFF000000) >> 24);
- m_input[1] = ((m_pts[i].y & 0x00FF0000) >> 16);
- m_input[2] = ((m_pts[i].y & 0x0000FF00) >> 8);
- m_input[3] = ((m_pts[i].y & 0x000000FF));
- *dadd = m_input[0];
- dadd++;
- *dadd = m_input[1];
- dadd++;
- *dadd = m_input[2];
- dadd++;
- *dadd = m_input[3];
- dadd++;
- }
- std::vector< Point >::iterator
- p = m_tbPtsNouveauxTrouvesFin.end(),
- pe = m_tbPtsNouveauxTrouvesFin.begin();
- // Il faut itérer dans le sens inverse pour garder la continuité du contour
- if ( p > pe )
- {
- p--;
- }
- i = 0;
- while ( p > pe )
- {
- /* if (i == 0)
- {
- CString vStr;
- vStr.Format(_T("%d, %d"),(*p).x, (*p).y);
- AfxMessageBox(vStr);
- }
- */
- m_input[0] = (((*p).x & 0xFF000000) >> 24);
- m_input[1] = (((*p).x & 0x00FF0000) >> 16);
- m_input[2] = (((*p).x & 0x0000FF00) >> 8);
- m_input[3] = (((*p).x & 0x000000FF));
- *dadd = m_input[0];
- dadd++;
- *dadd = m_input[1];
- dadd++;
- *dadd = m_input[2];
- dadd++;
- *dadd = m_input[3];
- dadd++;
- m_input[0] = (((*p).y & 0xFF000000) >> 24);
- m_input[1] = (((*p).y & 0x00FF0000) >> 16);
- m_input[2] = (((*p).y & 0x0000FF00) >> 8);
- m_input[3] = (((*p).y & 0x000000FF));
- *dadd = m_input[0];
- dadd++;
- *dadd = m_input[1];
- dadd++;
- *dadd = m_input[2];
- dadd++;
- *dadd = m_input[3];
- dadd++;
- i++;
- p--;
- }
-
- retour = plaqueInterface.CalculerPlaqueManuelle( m_oldnbPts + i, m_points );
- plaqueResult = plaqueInterface.GetResult();
- DrawMyText(FALSE);
- }
- if ((m_bPlaque2 == TRUE) && (m_bPlaque3 == FALSE) && (m_lastPoint.x != point.x) && (m_lastPoint.y != point.y) && (m_bCaptured == FALSE))
- {
- dadd = m_points;
- for (i = 0; i < m_nbPts; i++)
- {
- m_input[0] = ((m_pts[i].x & 0xFF000000) >> 24);
- m_input[1] = ((m_pts[i].x & 0x00FF0000) >> 16);
- m_input[2] = ((m_pts[i].x & 0x0000FF00) >> 8);
- m_input[3] = ((m_pts[i].x & 0x000000FF));
- *dadd = m_input[0];
- dadd++;
- *dadd = m_input[1];
- dadd++;
- *dadd = m_input[2];
- dadd++;
- *dadd = m_input[3];
- dadd++;
- m_input[0] = ((m_pts[i].y & 0xFF000000) >> 24);
- m_input[1] = ((m_pts[i].y & 0x00FF0000) >> 16);
- m_input[2] = ((m_pts[i].y & 0x0000FF00) >> 8);
- m_input[3] = ((m_pts[i].y & 0x000000FF));
- *dadd = m_input[0];
- dadd++;
- *dadd = m_input[1];
- dadd++;
- *dadd = m_input[2];
- dadd++;
- *dadd = m_input[3];
- dadd++;
- }
- retour = plaqueInterface.computePlaque( point.x, point.y, m_nbPts, m_points, 60, 90 );
- plaqueResult = plaqueInterface.GetResult();
- for (iVector = 0; iVector < plaqueResult->m_nVecteursTrouves - 1; iVector++)
- {
- oldP.x = plaqueResult->m_tbPtsTrouvesFin[iVector].x;
- oldP.y = plaqueResult->m_tbPtsTrouvesFin[iVector].y;
- m_tbPtsNouveauxTrouvesFin.push_back( oldP );
- }
- ::SetCursor( m_hCross );
- m_bPlaque3 = TRUE;
- // 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);
- // m_sBar.SetPaneText( 0, vStr );
- if (!m_image.IsNull())
- {
- DrawCurves();
- }
-
- DrawMyText(FALSE);
- m_oldnbPts = m_nbPts;
- // onInit();
- m_nbPts = 1;
- m_bPlaque1 = FALSE;
- m_bPlaque2 = FALSE;
- }
- if ((m_bPlaque3 == TRUE) && (m_bCaptured == TRUE))
- {
- /* m_newPoint.x = point.x;
- m_newPoint.y = point.y;
- minDist = INT_MAX;
- m_posModif = 0;
- std::vector< Point >::iterator
- p = m_tbPtsNouveauxTrouvesFin.begin(),
- pe = m_tbPtsNouveauxTrouvesFin.end();
-
- iVector = 0;
- while ( p < pe )
- {
- vPoint.x = (*p).x;
- vPoint.y = (*p).y;
- vDist = computeDistance(vPoint, m_newPoint);
- if (vDist < minDist)
- {
- minDist = vDist;
- m_posModif = iVector;
- }
- iVector++;
- ++p;
- }
- p = m_tbPtsNouveauxTrouvesFin.begin();
- pe = m_tbPtsNouveauxTrouvesFin.end();
- iVector = 0;
- while ( p < pe )
- {
- if (iVector == m_posModif)
- {
- (*p).x = point.x;
- (*p).y = point.y;
- }
- iVector++;
- ++p;
- }
- DrawCurves();
- */ }
- // Double click
- if ((m_bPlaque1 == FALSE) && (m_nbPts > 3) && (m_bCaptured == FALSE) && (m_bPlaque3 == FALSE))
- {
- if ((m_lastPoint.x == point.x) && (m_lastPoint.y == point.y))
- {
- // Remplissage du nouveau tableau de points
- plaqueResult = plaqueInterface.GetResult();
- if (m_bPlaque1 == FALSE)
- {
- m_bPlaque1 = TRUE;
- m_nbPts -=1;
- }
- if ( (m_bPlaque1 == TRUE) && (m_bPlaque2 == FALSE) )
- {
- m_bPlaque2 = TRUE;
- ::SetCursor (theApp.LoadCursor (IDC_TACHE));
- updateDisplay(point);
- }
- }
- }
- m_lastPoint.x = point.x;
- m_lastPoint.y = point.y;
- // retour = plaqueInterface.OnLButtonUp( point.x, point.y );
- /*
- m_pts[0].x = 267;
- m_pts[0].y = 218;
- m_pts[1].x = 300;
- m_pts[1].y = 227;
- m_pts[2].x = 364;
- m_pts[2].y = 232;
- m_pts[3].x = 446;
- m_pts[3].y = 238;
- m_pts[4].x = 509;
- m_pts[4].y = 238;
- m_pts[5].x = 561;
- m_pts[5].y = 234;
- */
- }
- }
- void CMainFrame::updateDisplay(CPoint point)
- {
- int i;
- CDC *pDC = GetDC();
- CRect rect;
- GetClientRect(&rect);
- m_image.StretchBlt( pDC->m_hDC, rect.left, rect.top, m_cRect.Width(), m_cRect.Height() );
- CPen pen( PS_SOLID, 2, RGB( 255, 0, 255 ) );
- CPen *pOldPen = pDC->SelectObject( &pen );
- if (m_nbPts > 1)
- {
- for (i = 1; i < m_nbPts-1; i++)
- {
- pDC->MoveTo( m_pts[i-1].x, m_pts[i-1].y );
- pDC->LineTo( m_pts[i].x, m_pts[i].y );
- }
-
- pDC->MoveTo( m_pts[m_nbPts-2].x, m_pts[m_nbPts-2].y );
- pDC->LineTo( point );
- pDC->SelectObject( pOldPen );
- }
- }
-
- void CMainFrame::OnMouseMove(UINT nFlags, CPoint point)
- {
- int i, rayon;
- dpoint p0, d1, d2;
- dpoint sr1, sr2;
- BOOL askReplace;
- dpoint vPoint, vPoint2;
- int iVector;
- std::vector< Point >::iterator p, pe;
- CString vStr;
- int ng;
- CString text;
- rayon = 7;
- CDC *pDC = GetDC();
- if (pDC != NULL)
- {
- COLORREF colorPixel = pDC->GetPixel(point.x, point.y);
- ng = (GetRValue(colorPixel) + GetGValue(colorPixel) + GetBValue(colorPixel)) / 3;
- text.Format( _T("x,y: %d,%d Gsl: %d"),point.x,point.y,ng );
- m_sBar.SetPaneText (2, text);
- }
- if (((m_bPlaque3 == TRUE) && (m_bCaptured == TRUE)) || (GetKeyState (VK_UP) < 0) || (GetKeyState (VK_DOWN) < 0) || (GetKeyState (VK_RIGHT) < 0) || (GetKeyState (VK_LEFT) < 0))
- {
- // Point du centre
- // p0.x = (double) m_pointCaptured.x;
- p0.x = (double) m_pointCaptured.x;
- /*
- deltay = (m_previousPoint.y - point.y);
- // if (deltay > 4) deltay /= 2;
- p = m_tbPtsNouveauxTrouvesFin.begin();
- pe = m_tbPtsNouveauxTrouvesFin.end();
-
- while ( p < pe )
- {
- if ((*p).x == p0.x)
- {
- if (m_previousPoint.y >= point.y)
- {
- // Beep(100, 100);
-
- p0.y = (double) point.y - rayon;
- // vStr.Format(_T("%d"), deltay);
- // AfxMessageBox(vStr);
- }
- else
- {
- p0.y = (double) point.y + rayon;
- }
- }
- ++p
- }
- */
- if (m_previousPoint.y >= point.y) // En montant
- {
- p0.y = (double) point.y - rayon;
- }
- else
- {
- p0.y = (double) point.y + rayon;
- }
- for (i = -rayon; i < rayon; i++)
- {
- d1.x = p0.x + i;
- d1.y = p0.y;
- d2.x = p0.x + i;
- d2.y = p0.y + 2;
- // On rechercle le point sur la courbe
- p = m_tbPtsNouveauxTrouvesFin.begin();
- pe = m_tbPtsNouveauxTrouvesFin.end();
-
- iVector = 0;
- while ( p < pe )
- {
- if ((*p).x == d1.x)
- {
- vPoint.x = d1.x;
- vPoint.y = (*p).y;
- m_posModif = iVector;
- }
- iVector++;
- ++p;
- }
-
- InterCercleDroite(p0, rayon, d1, d2, &sr1, &sr2);
- askReplace = FALSE;
- if (m_previousPoint.y >= point.y)
- {
- if (sr1.y < p0.y) // En montant : Demi cercle du haut
- {
- if ((fabs(vPoint.y - p0.y) < rayon) && (vPoint.y >= sr1.y))
- {
- askReplace = TRUE;
- vPoint2.x = vPoint.x;
- vPoint2.y = sr1.y;
- }
- }
- else
- {
- if ((fabs(vPoint.y - p0.y) < rayon) && (vPoint.y >= sr2.y))
- {
- askReplace = TRUE;
- vPoint2.x = vPoint.x;
- vPoint2.y = sr2.y;
- }
- }
- }
- else
- {
- if (sr1.y > p0.y) // En descendant : Demi cercle du haut
- {
- if ((fabs(vPoint.y - p0.y) < rayon) && (vPoint.y < sr1.y))
- {
- askReplace = TRUE;
- vPoint2.x = vPoint.x;
- vPoint2.y = sr1.y;
- }
- }
- else
- {
- if ((fabs(vPoint.y - p0.y) < rayon) && (vPoint.y < sr2.y))
- {
- askReplace = TRUE;
- vPoint2.x = vPoint.x;
- vPoint2.y = sr2.y;
- }
- }
- }
- if ((askReplace == TRUE) && ((m_keyPress == 3) || (m_keyPress == 4)))
- {
- p = m_tbPtsNouveauxTrouvesFin.begin();
- pe = m_tbPtsNouveauxTrouvesFin.end();
-
- iVector = 0;
- while ( p < pe )
- {
- if (iVector == m_posModif)
- {
- (*p).x = (int) vPoint2.x;
- (*p).y = (int) vPoint2.y;
- }
- iVector++;
- ++p;
- }
- }
- }
- m_previousPoint.x = point.x;
- m_previousPoint.y = point.y;
- DrawCurves();
- }
- if ((m_bPlaque1 == FALSE) && (m_nbPts > 1))
- {
- updateDisplay(point);
- CDC *pDC = GetDC();
- CPen pen( PS_SOLID, 1, RGB( 0, 255, 0 ) );
- CPen *pOldPen = pDC->SelectObject( &pen );
- pDC->SelectObject( pOldPen );
- PrintDistance( point );
- }
- }
- BOOL CMainFrame::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
- {
- if ( m_initialized && firstPoint && m_canChange && ( nHitTest == HTCLIENT ))
- {
- if ((m_bPlaque1 != TRUE) || (m_bPlaque3 == TRUE))
- {
- ::SetCursor(::LoadCursor(NULL, IDC_ARROW));
- return TRUE;
- }
- else if ((m_bPlaque1 != TRUE) || (m_bPlaque3 == TRUE))
- {
- ::SetCursor( m_hCross );
- }
- else
- {
- ::SetCursor (theApp.LoadCursor (IDC_TACHE));
- return TRUE;
- }
- }
- return CFrameWnd::OnSetCursor( pWnd, nHitTest, message );
- }
- void CMainFrame::OnSetFocus( CWnd* wnd )
- {
- CFrameWnd::OnSetFocus( wnd );
- if ( !m_image.IsNull() )
- {
- DrawCurves();
- }
- }
- void CMainFrame::OnMove( int cx, int cy )
- {
- CFrameWnd::OnMove( cx, cy );
- if ( !m_image.IsNull() )
- {
- DrawCurves();
- }
- }
- void CMainFrame::OnDraw( CDC* pDC )
- {
- if ( !m_image.IsNull() )
- {
- DrawCurves();
- }
- }
- CRect CMainFrame::GetTextRect()
- {
- CRect rect( 0, m_cRect.Height(), m_wRect.Width(), m_wRect.Height() );
- return rect;
- }
- void CMainFrame::DrawPoint( CDC *pDC, CPoint& pt, COLORREF color )
- {
- pDC->SetPixel( pt, color );
- }
- void CMainFrame::DrawCurves()
- {
- CDC *pDC = GetDC();
- CRect rect;
- GetClientRect(&rect);
- CBrush brush( RGB( 0, 0, 0 ) );
- pDC->FillRect( GetTextRect(), &brush );
- m_image.StretchBlt( pDC->m_hDC, rect.left, rect.top, m_cRect.Width(), m_cRect.Height() );
-
- if ( !m_image.IsNull() )
- {
- plaque::PlaqueResult* plaqueResult = plaqueInterface.GetResult();
- if (((plaqueResult->m_nVecteursTrouves > 0) || (plaqueResult->m_nPtLongeantPlaque > 0)) && (m_bPlaque1||m_bPlaque2||m_bPlaque3 || (m_bCaptured)) )
- {
- short iVector;
- CPen *pOldPen;
- CString vStr;
- if (plaqueResult->m_type == typeRGB)
- {
- pOldPen = pDC->SelectObject (&theApp.m_penVert);
- }
- else
- {
- pOldPen = pDC->SelectObject (&theApp.m_penDraw);
- }
-
- if (plaqueResult->m_Etape == etapeTacheSeuil)
- {
- // Mesure en cours de saisie : Afficher croix au pt du seuil inférieur
- pDC->MoveTo (plaqueResult->m_ptInferieur.x - 4, plaqueResult->m_ptInferieur.y);
- pDC->LineTo (plaqueResult->m_ptInferieur.x + 5, plaqueResult->m_ptInferieur.y);
- pDC->MoveTo (plaqueResult->m_ptInferieur.x, plaqueResult->m_ptInferieur.y - 4);
- pDC->LineTo (plaqueResult->m_ptInferieur.x, plaqueResult->m_ptInferieur.y + 5);
- }
- /* Draw the contour of the plaque */
- /* if ( plaqueResult->m_ptList.size() )
- {
- pDC->MoveTo( plaqueResult->m_ptList[0].x, plaqueResult->m_ptList[0].y );
- for (int i = 1; i < plaqueResult->m_ptList.size(); ++i)
- {
- pDC->LineTo( plaqueResult->m_ptList[i].x, plaqueResult->m_ptList[i].y );
- }
- }
- */
- pDC->SelectObject (&theApp.m_penVert);
- // pDC->MoveTo (plaqueResult->m_tbPtsTrouvesFin [0].x, plaqueResult->m_tbPtsTrouvesFin [0].y);
- for (iVector = 0; iVector < plaqueResult->m_nVecteursTrouves - 1; iVector++)
- {
- // pDC->LineTo (plaqueResult->m_tbPtsTrouvesFin [iVector + 1].x, plaqueResult->m_tbPtsTrouvesFin [iVector + 1].y);
- }
- pDC->MoveTo (plaqueResult->m_tPtLongeantPlaque[0].x, plaqueResult->m_tPtLongeantPlaque[0].y);
- for (iVector = 1; iVector < plaqueResult->m_nPtLongeantPlaque; iVector++)
- {
- pDC->LineTo(plaqueResult->m_tPtLongeantPlaque[iVector].x, plaqueResult->m_tPtLongeantPlaque[iVector].y);
- }
- pDC->SelectObject (&theApp.m_penBleu);
- if ((m_bPlaque3) || (m_bCaptured))
- {
- m_spline.fit( m_tbPtsNouveauxTrouvesFin );
- std::vector< Point > fit = m_spline.getValues( 20 );
- std::vector< Point >::iterator
- p = fit.begin(),
- pe = fit.end();
- iVector = 0;
- if ( p < pe )
- {
- pDC->MoveTo( (*p).x, (*p).y );
- ++p;
- }
- while ( p < pe )
- {
- if ((*p).x == (m_pointCaptured.x))
- {
- pDC->SelectObject (&theApp.m_penRouge);
- }
- else if ( ((*p).x >= (m_pointCaptured.x - 8)) && ((*p).x <= (m_pointCaptured.x + 8)) )
- {
- pDC->SelectObject (&theApp.m_penVert);
- }
- else
- {
- pDC->SelectObject (&theApp.m_penBleu);
- }
- pDC->LineTo( (*p).x, (*p).y );
- ++p;
- }
- }
- }
- if (plaqueResult->m_Etape == etapeSegments)
- {
- // pDC->LineTo (ptCurrent);
- }
- else if ( (plaqueResult->m_Etape == etapeReglage)
- || (plaqueResult->m_Etape == etapeTacheSeuil))
- {
- // if ( (plaqueResult->m_pointLimite1.x != 0)
- // || (plaqueResult->m_pointLimite1.y != 0))
- {
- // pOldPen = pDC->SelectObject (&theApp.m_penDraw);
- // pDC->LineTo (plaqueResult->m_pointLimite1.x, plaqueResult->m_pointLimite1.y);//
- // pDC->MoveTo (plaqueResult->m_pointLimite2.x, plaqueResult->m_pointLimite2.y);
- // pDC->LineTo (plaqueResult->m_tPtLongeantPlaque [0].x, plaqueResult->m_tPtLongeantPlaque [0].y);
- }
- }
- }
- }
- void CMainFrame::DrawMyText(BOOL init)
- {
- CDC *pDC = GetDC();
- CString text;
- plaque::PlaqueResult* plaqueResult = plaqueInterface.GetResult();
- if (init == TRUE)
- {
- text.Format( IDS_DEMO_MEASURE_PLAQUE,
- 0.0,
- 0.0,
- 0,
- 0.0,
- 0.0);
- }
- else
- {
- text.Format( IDS_DEMO_MEASURE_PLAQUE,
- plaqueResult->plaque_max_thickness,
- plaqueResult->plaque_mean_thickness,
- plaqueResult->numberOfMeasures,
- plaqueResult->plaque_area,
- plaqueResult->plaque_mean_density);
- }
- PrintText( text );
- }
- void CMainFrame::PrintCoordinates( CPoint pt )
- {
- CString text;
- text.Format( IDS_DEMO_COORDINATES, pt.x, pt.y );
- m_sBar.SetPaneText( 1, text );
- }
- void CMainFrame::PrintDistance( CPoint pt )
- {
- CString text;
- float d = plaqueInterface.getDistanceToFirstPoint( pt.x, pt.y, m_pts[0].x, m_pts[0].y);
- text.Format( IDS_DEMO_LONGUEUR, pt.x, pt.y, d );
- m_sBar.SetPaneText( 0, text );
- }
- void CMainFrame::PrintText( CString text )
- {
- CDC *pDC = GetDC();
- DRAWTEXTPARAMS textParam;
- textParam.cbSize = sizeof( DRAWTEXTPARAMS );
- textParam.iLeftMargin = 0;
- textParam.iRightMargin = 0;
- textParam.iTabLength = 3;
- textParam.uiLengthDrawn = 0;
-
- COLORREF colorText = pDC->SetTextColor( RGB( 255, 255, 255 ) );
- COLORREF colorBk = pDC->SetBkColor( RGB( 0, 0, 0 ) );
- CFont newFont;
- newFont.CreateFont( 12, 0, 0, 0, FW_THIN, FALSE, FALSE, FALSE,
- ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
- NONANTIALIASED_QUALITY ,FIXED_PITCH | FF_MODERN,
- _T( "Courier" ) );
- CFont* pOldFont = pDC->SelectObject( &newFont );
- CBrush brush( RGB( 0, 0, 0 ) );
- pDC->FillRect( GetTextRect(), &brush );
- pDC->DrawTextEx( text, GetTextRect(), DT_NOCLIP | DT_VCENTER, &textParam );
- pDC->SelectObject( pOldFont );
- if ( newFont.m_hObject != NULL )
- newFont.Detach();
- pDC->SetTextColor( colorText );
- pDC->SetBkColor( colorBk );
- }
- void CMainFrame::OnKeyDown (UINT nChar, UINT nRepCnt, UINT nFlags)
- {
- CPoint point;
- int rayon;
- rayon = 7;
- bool trouve;
- int iVector;
- m_keyPress = 0;
- switch (nChar)
- {
- case VK_RIGHT:
- {
- m_keyPress = 1;
- std::vector< Point >::iterator
- p = m_tbPtsNouveauxTrouvesFin.begin(),
- pe = m_tbPtsNouveauxTrouvesFin.end();
- trouve = false;
- iVector = 0;
- while (( p < pe ) && (!trouve))
- {
- if (iVector == (m_positionxCaptured + 1))
- {
- point.x = (*p).x;
- m_pointCaptured.x = point.x;
- point.y = (*p).y + rayon + 6;
- m_previousPoint.y = point.y + rayon;
- trouve = true;
- m_positionxCaptured++;
- OnMouseMove(nFlags, point);
- DrawCurves();
- }
- iVector++;
- ++p ;
- }
- }
- break;
- case VK_LEFT:
- {
- m_keyPress = 2;
- std::vector< Point >::iterator
- p = m_tbPtsNouveauxTrouvesFin.begin(),
- pe = m_tbPtsNouveauxTrouvesFin.end();
-
- trouve = false;
- iVector = 0;
- while (( p < pe ) && (!trouve))
- {
- if (iVector == (m_positionxCaptured - 1))
- {
- point.x = (*p).x;
- m_pointCaptured.x = point.x;
- point.y = (*p).y + rayon + 6;
- m_previousPoint.y = point.y + rayon;
- trouve = true;
- m_positionxCaptured--;
- OnMouseMove(nFlags, point);
- DrawCurves();
- }
- iVector++;
- ++p ;
- }
- }
- break;
-
- case VK_UP:
- {
- m_keyPress = 3;
- std::vector< Point >::iterator
- p = m_tbPtsNouveauxTrouvesFin.begin(),
- pe = m_tbPtsNouveauxTrouvesFin.end();
- iVector = 0;
- trouve = false;
-
- while (( p < pe )&& (!trouve))
- {
- if (iVector == m_positionxCaptured )
- {
- trouve = true;
- point.x = (*p).x;
- point.y = (*p).y + rayon + 6;
- m_previousPoint.y = point.y + rayon;
- OnMouseMove(nFlags, point);
- DrawCurves();
- }
- iVector++;
- ++p ;
- }
- }
- break;
- case VK_DOWN:
- {
- m_keyPress = 4;
- std::vector< Point >::iterator
- p = m_tbPtsNouveauxTrouvesFin.begin(),
- pe = m_tbPtsNouveauxTrouvesFin.end();
- iVector = 0;
- trouve = false;
-
- while (( p < pe ) && (!trouve))
- {
- if (iVector == m_positionxCaptured )
- {
- trouve = true;
- point.x = (*p).x;
- point.y = (*p).y - rayon - 6;
- m_previousPoint.y = point.y - rayon;
- OnMouseMove(nFlags, point);
- DrawCurves();
- }
- iVector++;
- ++p ;
- }
- }
- break;
- }
- }
- void CMainFrame::OnOpenfile()
- {
- CPoint point;
- point.x = 0;
- point.y = 0;
- CFileDialog cfd( TRUE, NULL, NULL,
- OFN_HIDEREADONLY | OFN_PATHMUSTEXIST |
- OFN_FILEMUSTEXIST | OFN_EXPLORER,
- _T( "Test Dicom file|*|Test jpgbmp|*.jpg;*.jpeg;*.bmp||" ), NULL );
- if ( cfd.DoModal() == IDOK )
- {
- m_image.Destroy();
- onInit();
- m_nbPts = 0;
- USES_CONVERSION;
- if ( cfd.GetOFN().nFilterIndex == 1 )
- {
- m_initialized = plaqueInterface.initializeFromFile( T2A( cfd.GetPathName() ) );
- if ( m_initialized )
- {
- int x, dimX = plaqueInterface.getImageWidth();
- int y, dimY = plaqueInterface.getImageHeight();
- m_image.Create( dimX, dimY, plaqueInterface.getImageBitsPerPixel() );
- char* buffer = plaqueInterface.getPixelArray();
- for ( y = 0; y < dimY; y++ )
- {
- for ( x = 0; x < dimX; x++ )
- {
- m_image.SetPixelRGB( x, y, *buffer++, *buffer++, *buffer++ );
- }
- }
- }
- }
- else
- {
- if ( SUCCEEDED( m_image.Load( cfd.GetPathName() ) ) )
- {
- char* buffer = NULL;
- int width = m_image.GetWidth();
- int height = m_image.GetHeight();
- int x, y, bpp;
- unsigned long count = width * height;
- // test for a 24 bits buffer
- bpp = 24;
- buffer = new char[ 3 * count ];
- if ( buffer )
- {
- char* p = buffer;
- for ( y = 0; y < height; y++ )
- {
- for ( x = 0; x < width; x++ )
- {
- *p++ = GetRValue( m_image.GetPixel( x, y ) );
- *p++ = GetGValue( m_image.GetPixel( x, y ) );
- *p++ = GetBValue( m_image.GetPixel( x, y ) );
- }
- }
- }
- if ( buffer )
- {
- m_initialized = plaqueInterface.initializeFromRaw( buffer, width, height, bpp, FALSE, 0.0826440, 0.0826440 );
- }
- }
- }
- if ( m_initialized )
- {
- plaqueInterface.Clear();
- ResizeWindow();
- Invalidate( FALSE );
- updateDisplay(point);
- DrawMyText(TRUE);
- }
- else
- {
- AfxMessageBox( _T( "File cannot be loaded" ), MB_ICONSTOP | MB_OK );
- }
- }
- }
- void CMainFrame::ResizeWindow()
- {
- CRect winRect;
- CRect cRect;
- CRect sRect;
- GetWindowRect( &winRect );
- GetClientRect( &cRect );
- int imgWidth = plaqueInterface.getImageWidth();
- int imgHeight = plaqueInterface.getImageHeight();
- m_sBar.GetWindowRect( &sRect );
- m_cRect = CRect( 0, 0, imgWidth, imgHeight );
- m_wRect = m_cRect;
- m_wRect.InflateRect( 0, 0, 0, 32 );
- ClientToScreen( &m_cRect );
- ClientToScreen( &m_wRect );
- SetWindowPos( NULL, 0, 0,
- winRect.Width() + imgWidth - cRect.Width(),
- winRect.Height() + imgHeight + 32 +
- sRect.Height() - cRect.Height(),
- SWP_NOMOVE | SWP_NOZORDER );
- DrawCurves();
- }
|