#pragma once
class CMyView;
class CMyGdiRect : public CGdiRect
{
public:
void SetRect(double dLeft, double dTop, double dRight, double dBottom);
void SetRect(const IRECT& rect);
void SetRect(const DRECT& rect);
DRECT GetDRect() const { return m_rect; }
private:
DRECT m_rect;
};
class CEraser : public CGdiRect
{
public:
void SetRect(double x, double y, double l);
double GetSize() const;
void Action(CMyView* pView, double dx, double dy, double dl, bool bDrag = false);
};
class CMyContour : public CGdiContour
{
public:
void FetchContours(CMyView* pView, int nIndex, double cx, double cy, double angle, double scale, double offsetX, double offsetY);
};
class CMyModel : public CShapeModel
{
public:
void UpdateModel(CMyView* pView);
CGdiPoint FitOrgPoint();
IRECT& GetRect() { return m_rect; }
void SetRect(const IRECT& rect) { m_rect = rect; }
bool GetLearnFlag() const { return m_bLearned; }
void SetLearnFlag(bool bLearned) { m_bLearned = bLearned; }
private:
IRECT m_rect;
bool m_bLearned;
};
class CMyView : public CGdiView
{
public:
int LearnModel(int nEdgeMode, int nPercentage, int nMinLength);
static void InitPen(CGdiFigure* pFigure, const COLORREF& color = RGB(0, 0, 255), int nWidth = 1, int nStyle = PS_SOLID);
public:
CGdiPoint* GetCross() { return &m_cross; }
CPrImage* GetImage() { return &m_image; }
CMask* GetMask() { return &m_mask; }
CMaskImage* GetMaskImage() { return &m_maskImage; }
CShapeModel* GetModel() { return &m_model; }
CMyContour* GetContour() { return &m_contour; }
CMyGdiRect* GetRoi() { return &m_roi; }
IRECT* GetModelRect() { return &m_model.GetRect(); }
private:
CEraser m_eraser;
CGdiPoint m_cross;
CPrImage m_image;
CMask m_mask;
CMaskImage m_maskImage;
CMyContour m_contour;
CMyModel m_model;
CShapeMatch m_match;
CMyGdiRect m_roi;
COverlay m_active;
COverlay m_static;
};
#include "CMyView.h"
#define ROI_CX(_roi) (_roi.left+_roi.right)*0.5
#define ROI_CY(_roi) (_roi.top+_roi.bottom)*0.5
void CEraser::SetRect(double x, double y, double l)
{
left = x - l * 0.5;
right = x + l * 0.5;
top = y - l * 0.5;
bottom = y + l * 0.5;
}
double CEraser::GetSize() const
{
return right - left;
}
// 橡皮擦的行为
void CEraser::Action(CMyView* pView, double dx, double dy, double dl, bool bDrag)
{
if (pView->GetLMouseFun() == GV_LMF_POINT)
{
if (GetVisible())
{
pView->ViewToImage(dx, dy);
SetRect(dx, dy, dl);
if (bDrag)
{
pView->GetMask()->Fill(*this, 255);
pView->GetMaskImage()->Build(*pView->GetImage(), *pView->GetMask());
}
pView->Redraw();
}
}
}
void CMyView::InitPen(CGdiFigure* pFigure, const COLORREF& color, int nWidth, int nStyle)
{
pFigure->SetPenColor(color);
pFigure->SetPenWidth(nWidth);
pFigure->SetPenStyle(nStyle);
}
int CMyView::LearnModel(int nEdgeMode, int nPercentage, int nMinLength)
{
m_model.SetEdgeMode(nEdgeMode);
m_model.SetPercentage(nPercentage);
m_model.SetMinLength(nMinLength);
if (m_model.Learn(m_image, m_mask, m_roi))
{
m_model.SetLearnFlag(true);
m_model.SetRect(m_roi);
m_cross.x = ROI_CX(m_model.GetRect());
m_cross.y = ROI_CY(m_model.GetRect());
m_contour.FetchContours(this, 0, m_cross.x, m_cross.y, 0.0, 1.0, 0.5, 0.5);
return m_model.GetModelCount() - 1;
}
return -1;
}
void CMyContour::FetchContours(CMyView* pView, int nIndex, double cx, double cy, double angle, double scale,
double offsetX, double offsetY)
{
CModelContour templ;
templ.Create(*pView->GetModel(), 0, cx, cy, angle, scale);
Create(templ);
Offset(offsetX, offsetY);
}
void CMyModel::UpdateModel(CMyView* pView)
{
IRECT rect = *pView->GetModelRect();
pView->GetRoi()->SetRect(rect);
double cx = pView->GetCross()->x = ROI_CX(rect);
double cy = pView->GetCross()->y = ROI_CX(rect);
pView->GetContour()->FetchContours(pView, 0, cx, cy, 0.0, 1.0, 0.5, 0.5);
FitOrgPoint();
pView->Redraw();
}
CGdiPoint CMyModel::FitOrgPoint()
{
if (GetModelCount() > 0)
{
FRAME2D frame;
GetReferFrame(frame);
frame.point.x += m_rect.left;
frame.point.y += m_rect.top;
CGdiPoint point;
point.x = frame.point.x;
point.y = frame.point.y;
return point;
}
return CGdiPoint();
}
void CMyGdiRect::SetRect(double dLeft, double dTop, double dRight, double dBottom)
{
CGdiRect* pThis = (CMyGdiRect*)this;
pThis->left = dLeft;
pThis->top = dTop;
pThis->right = dRight;
pThis->bottom = dBottom;
}
void CMyGdiRect::SetRect(const DRECT& rect)
{
SetRect(rect.left, rect.top, rect.right, rect.bottom);
}
void CMyGdiRect::SetRect(const IRECT& rect)
{
CGdiRect* pThis = (CMyGdiRect*)this;
*pThis = rect;
}