📄 curversimpleview.cpp
字号:
// CurverSimpleView.cpp : implementation of the CCurverSimpleView class
//
#include "stdafx.h"
#include "CurverSimple.h"
#include "CurverSimpleDoc.h"
#include "CurverSimpleView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CCurverSimpleView
IMPLEMENT_DYNCREATE(CCurverSimpleView, CView)
BEGIN_MESSAGE_MAP(CCurverSimpleView, CView)
//{{AFX_MSG_MAP(CCurverSimpleView)
ON_COMMAND(ID_DATAIMPORT, OnDataimport)
ON_WM_SIZE()
ON_WM_CREATE()
ON_WM_DESTROY()
ON_WM_LBUTTONDOWN()
ON_COMMAND(ID_DOUGLAS_PEUKER, OnDouglasPeuker)
ON_COMMAND(ID_CLEAR, OnClear)
ON_COMMAND(ID_DRAWNEWLINE, OnDrawnewline)
ON_WM_MOUSEMOVE()
ON_WM_LBUTTONUP()
ON_COMMAND(ID_JUAGEDISTANCE, OnJuagedistance)
ON_COMMAND(ID_JUAGEPOINTINOROUT, OnJuagepointinorout)
ON_COMMAND(ID_DISPLAY_RESULT, OnDisplayResult)
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CCurverSimpleView construction/destruction
CCurverSimpleView::CCurverSimpleView()
{
// TODO: add construction code here
m_bDraw=FALSE;
m_MouseDown=FALSE;
FlagEnd=0;
m_MouseDownNum=0;
m_display=FALSE;
m_newdraw=FALSE;
m_PtInTriangle=0;
}
CCurverSimpleView::~CCurverSimpleView()
{
}
BOOL CCurverSimpleView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
cs.style|=WS_CLIPSIBLINGS|WS_CLIPCHILDREN;
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CCurverSimpleView drawing
void CCurverSimpleView::OnDraw(CDC* pDC)
{
CCurverSimpleDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
wglMakeCurrent(pDC->GetSafeHdc(),m_hglrc);//该语句要放在以下两句的下面
glClearColor(1.0,1.0,1.0,0);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
GLDraw();
/*if(m_display)
display(selectedpoints);*/
wglMakeCurrent(pDC->GetSafeHdc(),NULL);
SwapBuffers(pDC->GetSafeHdc());
}
/////////////////////////////////////////////////////////////////////////////
// CCurverSimpleView printing
BOOL CCurverSimpleView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CCurverSimpleView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CCurverSimpleView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CCurverSimpleView diagnostics
#ifdef _DEBUG
void CCurverSimpleView::AssertValid() const
{
CView::AssertValid();
}
void CCurverSimpleView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CCurverSimpleDoc* CCurverSimpleView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CCurverSimpleDoc)));
return (CCurverSimpleDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CCurverSimpleView message handlers
void CCurverSimpleView::OnDataimport()
{
// TODO: Add your command handler code here
CFileDialog filedlg(TRUE,NULL,NULL,OFN_ALLOWMULTISELECT|OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,"DAT FILE(*.dat)|*.dat|ALL FILE(*.*)");
if (filedlg.DoModal() == IDCANCEL)
return;
CString filename;
filename=filedlg.GetPathName();
CStdioFile myfile(filename,CFile::modeRead);//得到数据流
long total;
total = myfile.GetLength();//得到数据流的长度
CString str;
int pos;
int count=0;
Orignpoint point;
//读取每一行的数据
while(myfile.GetPosition()!=total)
{
myfile.ReadString(str);
str.TrimLeft();
pos=str.Find(",");
point.pointID=atoi(str.Left(pos));//ID从0开始
str=str.Mid(pos+1);//从pos+1起取所有的字符
pos=str.Find(",");
point.pointofxy.x=atof(str.Left(pos));//从0开始取字符到pos位置
str=str.Mid(pos+1);
point.pointofxy.y=atof(str);
point.usedFlag=0;//未使用过用0表示
OrignVS.push_back(point);
}
Invalidate(false);
for(int i=0;i<OrignVS.size();i++)
{
array[i][0]=OrignVS[i].pointofxy.x/100.0;
array[i][1]=OrignVS[i].pointofxy.y/100.0;
array[i][2]=0.0;
}
m_bDraw=TRUE;
}
void CCurverSimpleView::InitPara()
{
/*void glMap1{fd}(GLenum target,TYPE u1,TYPE u2,GLint stride, GLint order,const TYPE *points);
函数的第一个参数target指出控制顶点的意义以及在参数points中需要提供多少值,
参数points指针可以指向控制点集、RGBA颜色值或纹理坐标串等。
参数u1和u2,指明变量U的范围,U一般从0变化到1。参数stride是跨度
,表示在每块存储区内浮点数或双精度数的个数,即两个控制点间的偏移量,
比如上例中的控制点集ctrpoint[4][3]的跨度就为3,即单个控制点的坐标元素个数。函数参数order是次数加1,
叫阶数,与控制点数一致。*/
glMap1f(GL_MAP1_VERTEX_3,0.0,1.0,3,13,&array[0][0]);
glEnable(GL_MAP1_VERTEX_3);
glShadeModel(GL_FLAT);
}
/*void CCurverSimpleView::GLDrawPoint(CPoint pt)
{
if(m_MouseDown)
{
glPointSize(4);
glBegin(GL_POINTS);
glColor3f(0.f,1.f,0.f);
glVertex2f(GLfloat(pt.x/100.0),GLfloat(pt.y/100.0));
}
return;
}*/
void CCurverSimpleView::GLDraw()
{
if(!m_bDraw)
return;
InitPara();
glLineWidth(3);
for(int i=0;i<OrignVS.size();i++)
{
glBegin(GL_LINE_STRIP);
glColor3f(1.f,0.f,0.f);
//glVertex2f(OrignVS[i].pointofxy.x/100,OrignVS[i].pointofxy.y/100);
//glEvalCoord2f(OrignVS[i].pointofxy.x/100,OrignVS[i].pointofxy.y/100);
glEvalCoord1f((GLfloat)i/30.0);
}
if(m_display)
{
float array[M][3];
for(int i=0;i<selectedpoints.size();i++)
{
array[i][0]=selectedpoints[i].pointofxy.x/100.0;
array[i][1]=selectedpoints[i].pointofxy.y/100.0;
array[i][2]=0.0;
}
glMap1f(GL_MAP1_VERTEX_3,0.0,1.0,3,selectedpoints.size(),&array[0][0]);
glEnable(GL_MAP1_VERTEX_3);
glShadeModel(GL_FLAT);
for(i=0;i<selectedpoints.size();i++)
{
glBegin(GL_LINE_LOOP);
glColor3f(0.f,1.f,0.f);
//glVertex3f(array[i][0],array[i][1],array[i][2]);
glEvalCoord1f((GLfloat)i/20);
}
}
glEnd();
/*glFlush();
glBegin(GL_LINES);
glColor3f(1.f,0.f,0.f);
glVertex3f(0.f,0.f,0.f);
glVertex3f(1.f,0.f,0.f);
glVertex3f(1.f,0.f,0.f);
glVertex3f(0.9f,0.1f,0.f);
glVertex3f(1.0f,0.f,0.f);
glVertex3f(1.0f,-0.1f,0.f);
glColor3f(0.f,1.f,0.f);
glVertex3f(0.f,0.f,0.f);
glVertex3f(0.f,1.f,0.f);
glVertex3f(0.f,1.f,0.f);
glVertex3f(0.1f,1.0f,0.f);
glVertex3f(0.0f,1.0f,0.0f);
glVertex3f(-0.1f,0.9f,0.0f);
glColor3f(0.f,0.f,1.f);
glVertex3f(0.f,0.f,0.f);
glVertex3f(0.f,0.f,1.f);
glVertex3f(0.f,0.f,1.f);
glVertex3f(0.f,0.1f,0.9f);
glVertex3f(0.f,0.f,1.f);
glVertex3f(0.f,0.1f,0.9f);
glEnd();*/
}
void CCurverSimpleView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
// TODO: Add your message handler code here
/*GLsizei w=cx;
GLsizei h=cy;
if(!h)
return;
CClientDC dc(this);
wglMakeCurrent(dc.m_hDC,m_hglrc);
glViewport(0,0,w,h);
glPushMatrix();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glTranslatef(0.f,0.f,-4.f);
glPopMatrix();
wglMakeCurrent(NULL,NULL);
Invalidate(TRUE);*/
CClientDC clientDC(this);
wglMakeCurrent(clientDC.m_hDC,m_hglrc);
int w=cx;
int h=cy;
glViewport(0, 0, w, h);
glPushMatrix();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(40.0,double(cx)/double(cy),0.1f,10.f);
glTranslatef(-2.3f,-1.8f,-4.f);
glMatrixMode(GL_MODELVIEW);//没有该语句无法显示物体
glPopMatrix();
wglMakeCurrent(NULL,NULL);
Invalidate(TRUE);
}
int CCurverSimpleView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
PIXELFORMATDESCRIPTOR pfd={
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_WINDOW|
PFD_SUPPORT_OPENGL|
PFD_DOUBLEBUFFER|PFD_SUPPORT_GDI,
PFD_TYPE_RGBA,
24,
0,0,0,0,0,0,
0,
0,
0,
0,0,0,0,
32,
0,
0,
PFD_MAIN_PLANE,
0,
0,0,0
};
//获取当前DC句柄
CClientDC clientdc(this);
//返回最佳匹配的像素格式
int pf=ChoosePixelFormat(clientdc.m_hDC,&pfd);
//设置像素格式
BOOL rt=SetPixelFormat(clientdc.m_hDC,pf,&pfd);
//crate rendering context
m_hglrc=wglCreateContext(clientdc.m_hDC);
return 0;
}
void CCurverSimpleView::OnDestroy()
{
CView::OnDestroy();
// TODO: Add your message handler code here
if(wglGetCurrentContext()!=NULL)
wglMakeCurrent(NULL,NULL);
if(m_hglrc!=NULL)
{
wglDeleteContext(m_hglrc);
m_hglrc=NULL;//?一定要这一句吗
}
}
void CCurverSimpleView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CMainFrame*pFrame=(CMainFrame*)AfxGetApp()->m_pMainWnd;
CStatusBar*pStatus=&pFrame->m_wndStatusBar;
CString str;
//int count=0;
if(pStatus)
{
str.Format("x=%f",float(point.x));
pStatus->SetPaneText(1,str);
str.Format("y=%f",float(point.y));
pStatus->SetPaneText(2,str);
}
//GLDrawPoint(point);
/*glPointSize(i*2);
glBegin (GL_POINTS);*/
Orignpoint temppoint;
if(m_bDraw)
{
//temppoint.pointID=count;
//count++;
temppoint.pointID=m_MouseDownNum;
CString str1;
str1.Format("%d",m_MouseDownNum);
//str=atoc(m_MouseDownNum);
CClientDC dc(this);
dc.SetPixel(point,RGB(0,0,0));
dc.TextOut(point.x,point.y,str1);
temppoint.pointofxy.x=point.x;
temppoint.pointofxy.y=point.y;
temppoint.usedFlag=false;
featurepoints.push_back(temppoint);
//m_MouseDownNum+=1;
}
/*pt=point;
ptbegin=point;
SetCapture();*/
if(m_newdraw=TRUE)
{ m_ptStart=point;
m_ptOld=point;
temppoint.pointID=m_MouseDownNum;
CString str1;
str1.Format("%d",m_MouseDownNum);
m_MouseDownNum++;
temppoint.pointofxy.x=point.x;
temppoint.pointofxy.y=point.y;
temppoint.usedFlag=false;
featurepoints2.push_back(temppoint);
SetCapture();
CRect rect;
GetClientRect(&rect);
ClientToScreen(&rect);
ClipCursor(&rect);
}
if(m_PtInTriangle==1)
{
CClientDC dc(this);
dc.TextOut(point.x,point.y,"▲");
TrianglePt.push_back(point);
if(TrianglePt.size()>=4)
m_PtInTriangle=2;
}
CView::OnLButtonDown(nFlags, point);
}
/*void CCurverSimpleView::OnGetfeaturepoint()
{
// TODO: Add your command handler code here
m_MouseDown=TRUE;
featurepoints.size();
}*/
void CCurverSimpleView::GetLineABC(CPoint pt1,CPoint pt2,float&A,float&B,float&C)
{
float dis=0.0;
dis=sqrt(pow((pt1.x-pt2.x),2)+pow((pt1.y-pt2.y),2));
A=(pt1.y-pt2.y)/dis;
B=(pt2.x-pt2.x)/dis;
C=(pt1.x*pt2.y-pt2.x*pt1.y)/dis;
}
int CCurverSimpleView::GetSelectpointID(vector<Orignpoint>fps,int&id,float fa)
{
float a,b,c,max,temp;
max=0.0;
/*if(fps.size()<=2)
return -1;*/
if(fps.size()<=2)
{
FlagEnd=1;
id=fps[fps.size()-1].pointID;
return 0;
}
FlagEnd=0;
GetLineABC(fps[0].pointofxy,fps[fps.size()-1].pointofxy,a,b,c);//怎么有9个点
for(int i=1;i<fps.size()-1;i++)//去掉第一点和最后一个点
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -