QPointObj.cpp
资源名称:QGIS.rar [点击查看]
上传用户:oybseng
上传日期:2015-04-27
资源大小:7831k
文件大小:11k
源码类别:
GDI/图象编程
开发平台:
Visual C++
- // QPointObj.cpp: implementation of the CQPointObj class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "..stdafx.h"
- #include "..includeQPointObj.h"
- #include "..includeQCoordSys.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- //////////////////////////////////////////
- ///***************CQGIS****************///
- ///函数名称:
- ///返回类型:
- ///入口参数:
- ///出口参数:
- ///思路说明:
- ///***************CQGIS****************///
- //////////////////////////////////////////
- CQPointObj::CQPointObj():CQBaseObj()
- {
- m_ptPos.SetPoint(0,0);
- m_BoundRect.SetRect(0,0,0,0);
- m_pArcList = 0;
- m_nObjType = QGIS_POINT;
- m_fPtElevation = 0.0f;
- }
- //////////////////////////////////////////
- ///***************CQGIS****************///
- ///函数名称:CQPointObj
- ///返回类型:无
- ///入口参数:无
- ///出口参数:无
- ///思路说明:构造函数
- ///***************CQGIS****************///
- //////////////////////////////////////////
- CQPointObj::CQPointObj(CQPoint & pt):CQBaseObj()
- {
- m_ptPos = pt; //点图元的点位坐标
- m_szObjName.Format("点图元%ld",m_lObjID);
- m_nObjType = QGIS_POINT;
- m_pArcList = new CArray<long,long>;
- m_fPtElevation = 0.0f;
- m_BoundRect.m_fMinX = pt.GetX() - 5.0;
- m_BoundRect.m_fMinY = pt.GetY() - 5.0;
- m_BoundRect.m_fMaxX = pt.GetX() + 5.0;
- m_BoundRect.m_fMaxY = pt.GetY() + 5.0;
- }
- CQPointObj::CQPointObj(CQPointObj & ptObj):CQBaseObj(ptObj)
- {
- m_ptPos = ptObj.GetPointPos();
- m_szObjName = ptObj.GetObjName();
- m_nObjType = ptObj.GetObjType();
- m_pArcList->Copy(*(ptObj.GetArcArray()));
- ptObj.GetBoundingRect(&m_BoundRect);
- m_fPtElevation = ptObj.GetPtElevation();
- }
- CQPointObj::~CQPointObj()
- {
- }
- inline void CQPointObj::AddArcID(long lID)
- {
- if(m_pArcList)
- m_pArcList->Add(lID);
- }
- //////////////////////////////////////////
- ///***************CQGIS****************///
- ///函数名称:CalculateBoundary
- ///返回类型:无
- ///入口参数:无
- ///出口参数:CBoundaryRect * pRect
- ///思路说明:
- ///***************CQGIS****************///
- //////////////////////////////////////////
- void CQPointObj::CalculateBoundary(CBoundaryRect * pRect /* = 0 */)
- {
- m_BoundRect.m_fMinX = m_ptPos.GetX() - 5.0;
- m_BoundRect.m_fMinY = m_ptPos.GetY() - 5.0;
- m_BoundRect.m_fMaxX = m_ptPos.GetX() + 5.0;
- m_BoundRect.m_fMaxY = m_ptPos.GetY() + 5.0;
- if(pRect)
- {
- pRect->m_fMinX = m_BoundRect.m_fMinX;
- pRect->m_fMinY = m_BoundRect.m_fMinY;
- pRect->m_fMaxX = m_BoundRect.m_fMaxX;
- pRect->m_fMaxY = m_BoundRect.m_fMaxY;
- }
- }
- void CQPointObj::Copy(CQPointObj & ptObj)
- {
- CQBaseObj::Copy(ptObj);
- m_ptPos = ptObj.GetPointPos();
- m_szObjName = ptObj.GetObjName();
- m_nObjType = ptObj.GetObjType();
- m_pArcList = ptObj.GetArcArray();
- ptObj.GetBoundingRect(&m_BoundRect);
- m_fPtElevation = ptObj.GetPtElevation();
- }
- //////////////////////////////////////////
- ///***************CQGIS****************///
- ///函数名称:CutOut
- ///返回类型:CQPointObj *
- ///入口参数:CBoundaryRect * pBox
- ///出口参数:
- ///思路说明:
- ///***************CQGIS****************///
- //////////////////////////////////////////
- CQPointObj * CQPointObj::CutOut(CBoundaryRect * pBox)
- {
- CQPointObj * ptObj;
- if(pBox)
- {
- CQPoint pt;
- pt = m_BoundRect.CenterPoint();
- if(pBox->PtInRect(pt))
- {
- ptObj = new CQPointObj(pt);
- ptObj->SetBoundRect(m_BoundRect);
- ptObj->SetObjName(m_szObjName);
- if(m_pArcList)
- {
- for(int i=0;i<m_pArcList->GetSize();i++)
- ptObj->GetArcArray()->Add(m_pArcList->GetAt(i));
- }
- }
- }
- return ptObj;
- }
- void CQPointObj::Display(CQCoordSys * pSys,CDC * pDC,int nDrawMode,int nSpecialMode,COLORREF * pColor)
- {
- //假如是删除或者隐藏状态则不显示
- if(m_bDeleted || m_bHided || !pSys)return;
- CPoint pt;
- pSys->WPtoLP(m_ptPos,&pt); //将点对象的位置坐标转换为屏幕逻辑坐标
- CBoundaryRect brect;
- CRect rect;
- pSys->GetWndport(rect.left,rect.top,rect.right,rect.bottom);
- pSys->LPtoWP(rect,&brect);
- if(!m_BoundRect.IsCross(&brect))return;
- //设定画笔的颜色、线型,以及宽度
- COLORREF crPen,crBrush;
- if(nSpecialMode == 1) //被鼠标选择选择后特殊显示
- {
- //临时的 因为还没有做选择对象管理器
- crPen = RGB(255,0,0);
- crBrush = RGB(0,255,0);
- }
- else if(nSpecialMode == 2) //指定颜色绘制
- {
- crPen = *pColor;
- crBrush = *pColor;
- }
- else
- {
- crPen = m_crPenColor;
- crBrush = m_crBrushColor;
- }
- pDC->SetROP2(nDrawMode); ////设定覆盖的绘制模式
- CPoint ptCur;
- pSys->WPtoLP(m_ptPos,&ptCur); //将世界坐标转换为逻辑坐标
- double fWidth = m_BoundRect.Width();
- double fHeight = m_BoundRect.Height();
- double fRadius = fHeight;
- if(fWidth<fHeight) fRadius = fWidth; //要小的
- fRadius /= 5; //半径
- CPen newPen(m_nLineType,(int)m_fLineWidth,crPen);
- CPen * pOldPen = pDC->SelectObject(&newPen);
- CBrush newBrush(crBrush);
- CBrush * pOldBrush = pDC->SelectObject(&newBrush);
- pDC->Ellipse(int(ptCur.x-fRadius),int(ptCur.y-fRadius),int(ptCur.x+fRadius),int(ptCur.y+fRadius));
- pDC->SelectObject(pOldBrush);
- pDC->SelectObject(pOldPen);
- }
- inline CArray<long,long> * CQPointObj::GetArcArray()
- {
- return m_pArcList;
- }
- inline long CQPointObj::GetArcID(long nIndex)
- {
- if(m_pArcList)
- {
- if(nIndex<0 || nIndex>=m_pArcList->GetSize()) return -1;
- return m_pArcList->GetAt(nIndex);
- }
- return -1;
- }
- void CQPointObj::GetBoundingRect(CBoundaryRect * pRect)
- {
- if(!pRect)return;
- CalculateBoundary(pRect);
- }
- CQPoint CQPointObj::GetPointPos()
- {
- return m_ptPos;
- }
- void CQPointObj::GetPointPos(double &dx,double &dy)
- {
- dx = m_ptPos.GetX();
- dy = m_ptPos.GetY();
- }
- //////////////////////////////////////////
- ///***************CQGIS****************///
- ///函数名称:Move
- ///返回类型:无
- ///入口参数:double dx,double dy
- ///出口参数:无
- ///思路说明:移动点对象的位置
- ///***************CQGIS****************///
- //////////////////////////////////////////
- void CQPointObj::Move(double dx,double dy)
- {
- double xx = m_ptPos.GetX();
- double yy = m_ptPos.GetY();
- xx += dx;
- yy += dy;
- m_ptPos.SetPoint(xx,yy);
- }
- void CQPointObj::ReadFromFile(CFile * pFile)
- {
- CQBaseObj::ReadFromFile(pFile);
- if(m_pArcList)
- {
- int nsize = 0;
- pFile->Read(&nsize,sizeof(int));
- long lArc = 0;
- for(int i=0;i<nsize;i++)
- {
- pFile->Read(&lArc,sizeof(long));
- m_pArcList->Add(lArc);
- }
- }
- float fx,fy;
- pFile->Read(&fx,sizeof(float));
- pFile->Read(&fy,sizeof(float));
- m_ptPos.SetPoint(fx,fy);
- float fEle;
- pFile->Read(&fEle,sizeof(float));
- m_fPtElevation = fEle;
- float frect;
- pFile->Read(&frect,sizeof(float));
- m_BoundRect.SetLeftBottomX(frect);
- pFile->Read(&frect,sizeof(float));
- m_BoundRect.SetLeftBottomY(frect);
- pFile->Read(&frect,sizeof(float));
- m_BoundRect.SetRightTopX(frect);
- pFile->Read(&frect,sizeof(float));
- m_BoundRect.SetRightTopX(frect);
- }
- void CQPointObj::WriteToFile(CFile * pFile)
- {
- if(m_pArcList)
- {
- int nsize = m_pArcList->GetSize();
- pFile->Write(&nsize,sizeof(int));
- for(int i=0;i<nsize;i++)
- pFile->Write((&(*m_pArcList)[i]),sizeof(long));
- }
- float fx = (float)m_ptPos.GetX();
- float fy = (float)m_ptPos.GetY();
- pFile->Write(&fx,sizeof(float));
- pFile->Write(&fy,sizeof(float));
- float fEle = static_cast<float>(m_fPtElevation);
- pFile->Write(&fEle,sizeof(float)); //加入了一行高程的写出
- float frect = static_cast<float>(m_BoundRect.GetLeftBottomX());
- pFile->Write(&frect,sizeof(float));
- frect = static_cast<float>(m_BoundRect.GetLeftBottomY());
- pFile->Write(&frect,sizeof(float));
- frect = static_cast<float>(m_BoundRect.GetRightTopX());
- pFile->Write(&frect,sizeof(float));
- frect = static_cast<float>(m_BoundRect.GetRightTopY());
- pFile->Write(&frect,sizeof(float));
- }
- void CQPointObj::Rotate(CQPoint & pt,double & fAngleDegree)
- {
- CQPoint ptOrign = m_ptPos;
- CQBaseObj::Rotate(&ptOrign,pt,fAngleDegree);
- m_ptPos.Copy(ptOrign);
- }
- //////////////////////////////////////////
- ///***************CQGIS****************///
- ///函数名称:Select
- ///返回类型:BOOL
- ///入口参数:CQCoordSys * pSys,CBoundaryRect & rect
- ///出口参数:无
- ///思路说明:框选点对象,判断点选位置是否在选择的矩形框中
- ///***************CQGIS****************///
- //////////////////////////////////////////
- BOOL CQPointObj::Select(CQCoordSys * pSys,CBoundaryRect & rect)
- {
- return rect.IsCross(&m_BoundRect);
- }
- //////////////////////////////////////////
- ///***************CQGIS****************///
- ///函数名称:Select
- ///返回类型:BOOL
- ///入口参数:CQCoordSys * pSys,CBoundaryRect & rect
- ///出口参数:无
- ///思路说明:点对象,判断点选位置是否在选择的矩形框中
- ///***************CQGIS****************///
- //////////////////////////////////////////
- BOOL CQPointObj::Select(CQCoordSys * pSys,CQPoint & pt, double fEffectDistance)
- {
- double fws = pSys->LLtoWL((long)fEffectDistance);
- double fx = (m_BoundRect.m_fMinX + m_BoundRect.m_fMinY)/2;
- double fy = (m_BoundRect.m_fMaxX + m_BoundRect.m_fMaxY)/2;
- double dd = pt.Distance(fx,fy);
- if(m_BoundRect.PtInRect(pt))
- {
- return TRUE;
- }
- else //如果点没有直接在边界矩形里
- {
- if(dd>=fws)
- return TRUE;
- }
- return FALSE;
- }
- IMPLEMENT_SERIAL(CQPointObj,CObject,1)
- void CQPointObj::Serialize(CArchive& ar)
- {
- CQBaseObj::Serialize(ar);
- if(ar.IsStoring())
- {
- int nsize = m_pArcList->GetSize();
- ar.Write(&nsize,sizeof(float));
- if(m_pArcList)
- {
- for(int i=0;i<nsize;i++)
- ar.Write(&((*m_pArcList)[i]),sizeof(long));
- }
- float fx = (float)m_ptPos.GetX();
- float fy = (float)m_ptPos.GetY();
- ar.Write(&fx,sizeof(float));
- ar.Write(&fy,sizeof(float));
- float fEle = static_cast<float>(m_fPtElevation);
- ar.Write(&fEle,sizeof(float));
- m_BoundRect.Serialize(ar);
- }
- else
- {
- int nsize = 0;
- long lArc = 0;
- if(m_pArcList)
- {
- nsize = m_pArcList->GetSize();
- ar.Write(&nsize,sizeof(int));
- for(int i=0;i<nsize;i++)
- {
- ar.Read(&lArc,sizeof(int));
- m_pArcList->Add(lArc);
- }
- }
- float fx = 0,fy = 0;
- ar.Read(&fx,sizeof(float));
- ar.Read(&fy,sizeof(float));
- m_ptPos.SetPoint(fx,fy);
- float fEle;
- ar.Read(&fEle,sizeof(float));
- m_fPtElevation = fEle;
- m_BoundRect.Serialize(ar);
- }
- }
- void CQPointObj::SetBoundRect(CBoundaryRect & Rect)
- {
- m_BoundRect = Rect;
- }
- void CQPointObj::SetPointPos(double dx,double dy)
- {
- m_ptPos.SetPoint(dx,dy);
- }
- void CQPointObj::SetPointPos(CQPoint & pt)
- {
- double fx = pt.GetX();
- double fy = pt.GetY();
- m_ptPos.SetPoint(fx,fy);
- }
- CString CQPointObj::VarToStr()
- {
- CString szBaseObj = CQBaseObj::VarToStr();
- CString szPointObj;
- szPointObj.Format("边界矩形的左下角X:%f,边界矩形的左下角Y:%f,边界矩形的右上角X:%f,边界矩形的右上角Y:%f,点图元的X坐标:%f,点图元的Y坐标:%f,点图元的高程:%f",m_BoundRect.m_fMinX,m_BoundRect.m_fMinY,m_BoundRect.m_fMaxX,m_BoundRect.m_fMaxY,m_ptPos.GetX(),m_ptPos.GetY(),m_fPtElevation);
- szBaseObj += szPointObj;
- return szBaseObj;
- }
- //XFlex>0放大<0缩小
- void CQPointObj::XFlex(double dcp_x,double dFlex)
- {
- float fx = (float)m_ptPos.GetX();
- float fy = (float)m_ptPos.GetY();
- double xoff = (fx - dcp_x)*dFlex;
- SetPointPos(xoff,fy);
- }
- //YFlex>0放大<0缩小 dcp_y偏移距离
- void CQPointObj::YFlex(double dcp_y,double dFlex)
- {
- float fx = (float)m_ptPos.GetX();
- float fy = (float)m_ptPos.GetY();
- double yoff = (fy - dcp_y)*dFlex;
- SetPointPos(fx,yoff);
- }
- void CQPointObj::SetPtElevation(double fEle)
- {
- m_fPtElevation = fEle;
- }
- double CQPointObj::GetPtElevation() const
- {
- return m_fPtElevation;
- }