www.pudn.com > MapDB.rar > MapDBDoc.cpp
// MapDBDoc.cpp : implementation of the CMapDBDoc class
//
#include "stdafx.h"
#include "MapDB.h"
#include "MapDBDoc.h"
#include "MapDBView.h"
#include "ImportDlg.h"
#include "QueryDlg.h"
#include "ResultDlg.h"
#include "ImageObject.h"
#include "matrix.h"
#include "math.h"
#include "io.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define ZEROTEST 1.0E-10
#define DISTEST 100
#define ZOOMFAC 10
/////////////////////////////////////////////////////////////////////////////
// CMapDBDoc
IMPLEMENT_DYNCREATE(CMapDBDoc, CDocument)
BEGIN_MESSAGE_MAP(CMapDBDoc, CDocument)
//{{AFX_MSG_MAP(CMapDBDoc)
ON_COMMAND(ID_IMPORT_USR, OnImportUsr)
ON_UPDATE_COMMAND_UI(ID_IMPORT_USR, OnUpdateImportUsr)
ON_COMMAND(ID_QUERY_ID, OnQueryId)
ON_UPDATE_COMMAND_UI(ID_QUERY_ID, OnUpdateQueryId)
ON_COMMAND(ID_QUERY_TITLE, OnQueryTitle)
ON_UPDATE_COMMAND_UI(ID_QUERY_TITLE, OnUpdateQueryTitle)
ON_COMMAND(ID_QUERY_GRID, OnQueryGrid)
ON_UPDATE_COMMAND_UI(ID_QUERY_GRID, OnUpdateQueryGrid)
ON_COMMAND(ID_QUERYALL, OnQueryall)
ON_UPDATE_COMMAND_UI(ID_QUERYALL, OnUpdateQueryall)
ON_COMMAND(ID_REFRESH, OnRefresh)
ON_COMMAND(ID_QUERY_RESULT, OnQueryResult)
ON_UPDATE_COMMAND_UI(ID_QUERY_RESULT, OnUpdateQueryResult)
ON_UPDATE_COMMAND_UI(ID_ZOOMIN, OnUpdateZoomin)
ON_UPDATE_COMMAND_UI(ID_ZOOMOUT, OnUpdateZoomout)
ON_UPDATE_COMMAND_UI(ID_ZOOM_ORIGINAL, OnUpdateZoomOriginal)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMapDBDoc construction/destruction
CMapDBDoc::CMapDBDoc()
{
m_text = NULL;
m_lpbiText = NULL;
m_pbitmap = NULL;
m_sizePhoto.cx = 1;
m_sizePhoto.cy = 1;
m_sizeDraw.cx = 0;
m_sizeDraw.cy = 0;
m_version.LoadString(IDS_MAPVERSION_ZKCH);
m_querylist = NULL;
m_bInited = false;
m_bShowQuery = false;
m_bSelQuery = false;
}
CMapDBDoc::~CMapDBDoc()
{
if ( m_text ) delete [] m_text;
if ( m_lpbiText ) delete [] (BYTE *) m_lpbiText;
for (int count = 0;count < m_mapobjs.GetSize();count ++)
delete m_mapobjs[count];
for ( count = 0;count < m_normalpts.GetSize();count ++)
delete m_normalpts[count];
for ( count = 0;count < m_controlpts.GetSize();count ++)
delete m_controlpts[count];
for ( count = 0;count < m_schemapts.GetSize();count ++)
delete m_schemapts[count];
}
BOOL CMapDBDoc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE;
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CMapDBDoc serialization
void CMapDBDoc::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
ar << m_version << m_maptitle << m_mapid << m_mapscale << m_mapproject;
ar << m_mapcoords << m_mapaltitude << m_mapAttributes << m_mapdate;
ar << m_domfile;
ar << m_normalpts.GetSize();
for (int count = 0;count < m_normalpts.GetSize();count ++)
ar << m_normalpts[count];
ar << m_controlpts.GetSize();
for ( count = 0;count < m_controlpts.GetSize();count ++)
ar << m_controlpts[count];
ar << m_schemapts.GetSize();
for ( count = 0;count < m_schemapts.GetSize();count ++)
ar << m_schemapts[count];
ar << m_transx[0] << m_transx[1] << m_transx[2];
ar << m_transy[0] << m_transy[1] << m_transy[2];
ar << m_xo << m_yo << m_kx << m_ky;
ar << m_xmin << m_ymin << m_xmax << m_ymax;
ar << m_sxo << m_syo << m_ksx << m_ksy;
ar << m_maptitles.GetCount();
DWORD id,title,tvalue;
POSITION pos = m_maptitles.GetStartPosition();
for ( ; pos != NULL; )
{
m_maptitles.GetNextAssoc(pos,title,tvalue);
ar << title << tvalue;
}
ar << m_mapobjids.GetCount();
pos = m_mapobjids.GetStartPosition();
for ( ; pos != NULL; )
{
m_mapobjids.GetNextAssoc(pos,id,tvalue);
ar << id << tvalue;
}
ar << m_objsel.GetSize();
for ( count = 0;count < m_objsel.GetSize();count ++)
ar << m_objsel[count];
ar << m_titlesel.GetSize();
for ( count = 0;count < m_titlesel.GetSize();count ++)
ar << m_titlesel[count];
ar << m_objidx.GetSize();
for ( count = 0;count < m_objidx.GetSize();count ++)
ar << m_objidx[count];
ar << m_titleidx.GetSize();
for ( count = 0;count < m_titleidx.GetSize();count ++)
ar << m_titleidx[count];
ar << m_grididx.GetSize();
for ( count = 0;count < m_grididx.GetSize();count ++)
ar << m_grididx[count];
ar << m_mapobjs.GetSize();
for ( count = 0;count < m_mapobjs.GetSize();count ++)
ar << m_mapobjs[count];
}
else
{
ar >> m_version >> m_maptitle >> m_mapid >> m_mapscale >> m_mapproject;
ar >> m_mapcoords >> m_mapaltitude >> m_mapAttributes >> m_mapdate;
ar >> m_domfile;
int size;
ar >> size;
for (int count = 0;count < size;count ++)
{
CMPoint * npt;
ar >> npt;
m_normalpts.Add(npt);
}
ar >> size;
for ( count = 0;count < size;count ++)
{
CMPoint * cpt;
ar >> cpt;
m_controlpts.Add(cpt);
}
ar >> size;
for ( count = 0;count < size;count ++)
{
CMPoint * spt;
ar >> spt;
m_schemapts.Add(spt);
}
ar >> m_transx[0] >> m_transx[1] >> m_transx[2];
ar >> m_transy[0] >> m_transy[1] >> m_transy[2];
ar >> m_xo >> m_yo >> m_kx >> m_ky;
ar >> m_xmin >> m_ymin >> m_xmax >> m_ymax;
ar >> m_sxo >> m_syo >> m_ksx >> m_ksy;
ar >> size;
DWORD id,title,tvalue;
for (count = 0;count < size;count ++ )
{
ar >> title >> tvalue;
m_maptitles.SetAt(title,tvalue);
}
ar >> size;
for ( count = 0;count < size;count ++ )
{
ar >> id >> tvalue;
m_mapobjids.SetAt(id,tvalue);
}
ar >> size;
for ( count = 0;count < size;count ++)
{
BITS sel;
ar >> sel;
m_objsel.Add(sel);
}
ar >> size;
for ( count = 0;count < size;count ++)
{
BITS sel;
ar >> sel;
m_titlesel.Add(sel);
}
ar >> size;
for ( count = 0;count < size;count ++)
{
BITS sel;
ar >> sel;
m_objidx.Add(sel);
}
ar >> size;
for ( count = 0;count < size;count ++)
{
BITS sel;
ar >> sel;
m_titleidx.Add(sel);
}
ar >> size;
for ( count = 0;count < size;count ++)
{
BITS sel;
ar >> sel;
m_grididx.Add(sel);
}
ar >> size;
for ( count = 0;count < size;count ++)
{
CMapObject * obj;
ar >> obj;
m_mapobjs.Add(obj);
}
m_bInited = true;
}
}
/////////////////////////////////////////////////////////////////////////////
// CMapDBDoc diagnostics
#ifdef _DEBUG
void CMapDBDoc::AssertValid() const
{
CDocument::AssertValid();
}
void CMapDBDoc::Dump(CDumpContext& dc) const
{
CDocument::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMapDBDoc commands
void CMapDBDoc::OnImportUsr()
{
CImportDlg import;
if ( import.DoModal() != IDOK )
return;
m_usrfile = import.m_usrfile;
m_xyfile = import.m_xyfile;
m_domfile = import.m_domfile;
if ( ImportUsr() )
{
m_bInited = true;
SetImage();
}
else
{
CString message;
message.LoadString(IDS_USRFILEERROR);
AfxMessageBox(message);
}
}
void CMapDBDoc::OnUpdateImportUsr(CCmdUI* pCmdUI)
{
pCmdUI->Enable(!m_bInited);
}
BOOL CMapDBDoc::ImportUsr()
{
if ( m_usrfile.IsEmpty() || m_xyfile.IsEmpty() || m_domfile.IsEmpty() )
return FALSE;
BOOL bOK = LoadVector(m_usrfile) && LoadXY(m_xyfile) && LoadImage(m_domfile);
if ( ! bOK || ! GetTransform() )
return FALSE;
TransCoord();
GetIndexs();
SetModifiedFlag();
return TRUE;
}
BOOL CMapDBDoc::OnOpenDocument(LPCTSTR lpszPathName)
{
if (!CDocument::OnOpenDocument(lpszPathName))
return FALSE;
if ( GetImage(lpszPathName) )
SetImage();
else
{
DeleteContents();
return FALSE;
}
return TRUE;
}
BOOL CMapDBDoc::OnSaveDocument(LPCTSTR lpszPathName)
{
m_mapdate = CTime::GetCurrentTime();
return CDocument::OnSaveDocument(lpszPathName);
}
BOOL CMapDBDoc::LoadImage(CString imgFile)
{
CImageObject ImageObject;
if ( ! ImageObject.Load(imgFile) )
return false;
HGLOBAL hdib = ImageObject.GetDIBPointer();
BITMAPFILEHEADER * header = (BITMAPFILEHEADER*)hdib;
BITMAPINFOHEADER * infoheader = (BITMAPINFOHEADER*)((BYTE*)hdib + sizeof(BITMAPFILEHEADER));
if ( infoheader->biBitCount == 8 )
{
if ( m_lpbiText )
{
delete [] m_lpbiText;
m_lpbiText = NULL;
}
m_lpbiText = (LPBITMAPINFO) new BYTE[sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD)];
memcpy(m_lpbiText->bmiColors,(BYTE*)hdib + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER),256 * sizeof(RGBQUAD));
}
else if ( infoheader->biBitCount == 24)
{
if ( m_lpbiText )
{
delete [] m_lpbiText;
m_lpbiText = NULL;
}
m_lpbiText = (LPBITMAPINFO) new BYTE[sizeof(BITMAPINFOHEADER)];
}
else
return false;
memcpy(&m_lpbiText->bmiHeader,infoheader,sizeof(BITMAPINFOHEADER));
m_sizePhoto.cx = infoheader->biWidth;
m_sizePhoto.cy = infoheader->biHeight;
m_sizeDraw.cx = infoheader->biWidth;
m_sizeDraw.cy = infoheader->biHeight;
long bytes_per_line = infoheader->biBitCount / 8 * infoheader->biWidth;
if ( bytes_per_line % 4 )
bytes_per_line = 4 * ( bytes_per_line / 4 + 1 );
long textWidth = infoheader->biWidth;
long textHeight = infoheader->biHeight;
if ( m_text )
{
delete [] m_text;
m_text = NULL;
}
m_text = new unsigned char[m_sizePhoto.cy * bytes_per_line];
PBYTE pLine = (BYTE*)((BYTE*)hdib + header->bfOffBits);
memcpy(m_text,pLine,bytes_per_line * m_sizePhoto.cy);
return true;
}
void CMapDBDoc::SetImage()
{
CMapDBView * pView = GetView();
if ( pView )
pView->SetImage();
}
void CMapDBDoc::DeleteContents()
{
Release();
SetImage();
CDocument::DeleteContents();
}
CMapDBView * CMapDBDoc::GetView()
{
POSITION pos = GetFirstViewPosition();
while (pos != NULL)
{
CView * pView = GetNextView(pos);
if ( pView->IsKindOf(RUNTIME_CLASS(CMapDBView)) )
return (CMapDBView *)pView;
}
return NULL;
}
double CMapDBDoc::GetRate()
{
BOOL bSlow = HIBYTE(GetAsyncKeyState(VK_CONTROL));
BOOL bMiddle = HIBYTE(GetAsyncKeyState(VK_MENU));
BOOL bFast = HIBYTE(GetAsyncKeyState(VK_SHIFT));
double rate = ( bSlow ? 1.1 : 0 ) + ( bMiddle ? 1.5 : 0 ) + ( bFast ? 2 : 0 );
if ( rate < ZEROTEST ) rate = 1.2;
return rate;
}
BOOL CMapDBDoc::LoadVector(CString usrFile)
{
FILE * fp = fopen(usrFile,"rt");
long id,title;
double x,y;
for ( x = 0,y = 0; x > USRFINISHED || y > USRFINISHED ; )
{
fscanf(fp,"%ld,%*ld",&id);
if ( id < USRFINISHED )
break;
fscanf(fp,"%ld,%*ld",&title);
CMapObject * obj = new CMapObject;
m_mapobjs.Add(obj);
obj->ID = id;
obj->m_titles.Add(title);
obj->m_titles.Add(0);
for ( x = 0,y = 0; x > OBJFINISHED || y > OBJFINISHED ; )
{
fscanf(fp,"%lf,%lf",&x,&y);
if ( x > OBJFINISHED || y > OBJFINISHED )
{
CMPoint * mpt = new CMPoint;
obj->m_mpts.Add(mpt);
mpt->xi = x;
mpt->yi = y;
if ( title == OBJTITLE_CONTROLPT )
{
CMPoint * cpt = new CMPoint;
m_controlpts.Add(cpt);
cpt->xi = x;
cpt->yi = y;
}
else if ( title == OBJTITLE_SCHEMAPT )
{
CMPoint * spt = new CMPoint;
m_schemapts.Add(spt);
spt->xi = x;
spt->yi = y;
}
}
}
}
fclose(fp);
return m_controlpts.GetSize() >= 3;
}
BOOL CMapDBDoc::LoadXY(CString xyFile)
{
FILE * fp = fopen(xyFile,"rt");
double x = 0,y = 0;
for ( ; ! feof(fp) ; )
{
int bOK = fscanf(fp,"%*s%lf%lf",&x,&y);
if ( ! bOK || bOK == EOF )
break;
CMPoint * npt = new CMPoint;
m_normalpts.Add(npt);
npt->xi = x;
npt->yi = y;
}
fclose(fp);
return m_normalpts.GetSize() >= 3;
}
BOOL CMapDBDoc::GetTransform()
{
if ( m_controlpts.GetSize() < 3 || m_normalpts.GetSize() < 3 )
return FALSE;
int m = min(m_controlpts.GetSize(),m_normalpts.GetSize());
double * a = new double[m * 12];
double * b = new double[m * 2];
double x[6];
double * aa = new double[m * 12];
double * u = new double[m * m * 4];
double v[36];
double eps = 0.000001;
int ka = m * 2 + 1;
for (int count = 0;count < m;count ++)
{
CMPoint * cpt = m_controlpts[count];
CMPoint * npt = m_normalpts[count];
a[count * 6] = cpt->xi;
a[count * 6 + 1] = cpt->yi;
a[count * 6 + 2] = 1;
a[count * 6 + 3] = 0;
a[count * 6 + 4] = 0;
a[count * 6 + 5] = 0;
a[(count + 3) * 6] = 0;
a[(count + 3) * 6 + 1] = 0;
a[(count + 3) * 6 + 2] = 0;
a[(count + 3) * 6 + 3] = cpt->xi;
a[(count + 3) * 6 + 4] = cpt->yi;
a[(count + 3) * 6 + 5] = 1;
b[count] = npt->xi;
b[count + 3] = npt->yi;
}
CMatrix matrix;
BOOL bOK = matrix.agmiv(a,m * 2,6,b,x,aa,eps,u,v,ka) > 0;
if ( bOK )
{
memcpy(m_transx,&x[0],3 * sizeof(double));
memcpy(m_transy,&x[3],3 * sizeof(double));
}
for (count = 0;count < m;count ++)
{
CMPoint * npt = m_normalpts[count];
CMPoint * cpt = m_controlpts[count];
a[count * 6] = npt->xi;
a[count * 6 + 1] = npt->yi;
a[count * 6 + 2] = 1;
a[count * 6 + 3] = 0;
a[count * 6 + 4] = 0;
a[count * 6 + 5] = 0;
a[(count + 3) * 6] = 0;
a[(count + 3) * 6 + 1] = 0;
a[(count + 3) * 6 + 2] = 0;
a[(count + 3) * 6 + 3] = npt->xi;
a[(count + 3) * 6 + 4] = npt->yi;
a[(count + 3) * 6 + 5] = 1;
b[count] = cpt->xi;
b[count + 3] = cpt->yi;
}
bOK = matrix.agmiv(a,m * 2,6,b,x,aa,eps,u,v,ka) > 0;
if ( bOK )
{
memcpy(&m_transx[3],&x[0],3 * sizeof(double));
memcpy(&m_transy[3],&x[3],3 * sizeof(double));
}
delete [] a;
delete [] b;
delete [] aa;
delete [] u;
return bOK;
}
void CMapDBDoc::TransCoord()
{
GetTransCoord();
GetGridCoord();
GetScreenCoord();
}
void CMapDBDoc::GetTransCoord()
{
double xmin = 1.0E12,ymin = 1.0E12,xmax = 1.0E-12,ymax = 1.0E-12;
for (int count = 0;count < m_mapobjs.GetSize();count ++)
{
CMapObject * obj = m_mapobjs[count];
for (int mptcount = 0;mptcount < obj->m_mpts.GetSize();mptcount ++)
{
CMPoint * mpt = obj->m_mpts[mptcount];
UWC2WC(mpt->xi,mpt->yi,mpt->xt,mpt->yt);
}
}
for (int sptcount = 0;sptcount < m_schemapts.GetSize();sptcount ++)
{
CMPoint * spt = m_schemapts[sptcount];
UWC2WC(spt->xi,spt->yi,spt->xt,spt->yt);
if ( spt->xt < xmin )
xmin = spt->xt;
if ( spt->xt > xmax )
xmax = spt->xt;
if ( spt->yt < ymin )
ymin = spt->yt;
if ( spt->yt > ymax )
ymax = spt->yt;
}
m_xo = ( xmin + xmax ) / 2.0;
m_yo = ( ymin + ymax ) / 2.0;
m_kx = NDCEXTENT / ( xmax - xmin );
m_ky = NDCEXTENT / ( ymax - ymin );
}
void CMapDBDoc::GetGridCoord()
{
m_xmin = 1.0E12;
m_ymin = 1.0E12;
m_xmax = 1.0E-12;
m_ymax = 1.0E-12;
for (int count = 0;count < m_mapobjs.GetSize();count ++)
{
CMapObject * obj = m_mapobjs[count];
for (int mptcount = 0;mptcount < obj->m_mpts.GetSize();mptcount ++)
{
CMPoint * mpt = obj->m_mpts[mptcount];
WC2NDC(mpt->xt,mpt->yt,mpt->x,mpt->y);
}
}
for (int sptcount = 0;sptcount < m_schemapts.GetSize();sptcount ++)
{
CMPoint * spt = m_schemapts[sptcount];
WC2NDC(spt->xt,spt->yt,spt->x,spt->y);
if ( spt->x < m_xmin )
m_xmin = spt->x;
if ( spt->x > m_xmax )
m_xmax = spt->x;
if ( spt->y < m_ymin )
m_ymin = spt->y;
if ( spt->y > m_ymax )
m_ymax = spt->y;
}
}
void CMapDBDoc::GetScreenCoord()
{
m_sxo = NDCEXTENT / 2;
m_syo = NDCEXTENT / 2;
m_ksx = (double)(m_sizePhoto.cx / (m_xmax - m_xmin));
m_ksy = (double)(m_sizePhoto.cy / (m_ymax - m_ymin));
for (int count = 0;count < m_mapobjs.GetSize();count ++)
{
CMapObject * obj = m_mapobjs[count];
for (int mptcount = 0;mptcount < obj->m_mpts.GetSize();mptcount ++)
{
CMPoint * mpt = obj->m_mpts[mptcount];
NDC2DC(mpt->x,mpt->y,mpt->r,mpt->c);
}
}
}
void CMapDBDoc::Grid2NDC(long r, long c, long &x0, long &y0, long &x1, long &y1)
{
x0 = (long) ( c * ( (double)NDCEXTENT / (double)GRIDCOUNT ) - m_sxo );
y0 = (long) ( r * ( (double)NDCEXTENT / (double)GRIDCOUNT ) - m_syo );
x1 = (long) ( x0 + (double)NDCEXTENT / (double)GRIDCOUNT );
y1 = (long) ( y0 + (double)NDCEXTENT / (double)GRIDCOUNT );
}
void CMapDBDoc::NDC2Grid(long x, long y, long &r, long &c)
{
c = (long) ( ( x + m_sxo ) / ( (double)NDCEXTENT / (double)GRIDCOUNT ) );
r = (long) ( ( y + m_syo ) / ( (double)NDCEXTENT / (double)GRIDCOUNT ) );
c = min(c,GRIDCOUNT - 1);
r = min(r,GRIDCOUNT - 1);
}
void CMapDBDoc::NDC2DC(long xn, long yn, long &r, long &c)
{
c = (long)(( xn + m_sxo ) * m_ksx + 0.5);
r = (long)(( m_syo - yn ) * m_ksy + 0.5);
}
void CMapDBDoc::DC2NDC(long r, long c, long &x, long &y)
{
x = (long)(c / m_ksx - m_sxo + 0.5);
y = (long)(m_syo - r / m_ksy + 0.5);
}
void CMapDBDoc::UWC2WC(double xi, double yi, double &x, double &y)
{
x = xi * m_transx[0] + yi * m_transx[1] + m_transx[2];
y = xi * m_transy[0] + yi * m_transy[1] + m_transy[2];
}
void CMapDBDoc::WC2NDC(double x, double y, long &xn, long &yn)
{
xn = (long)(( x - m_xo ) * m_kx + 0.5);
yn = (long)(( y - m_yo ) * m_ky + 0.5);
}
void CMapDBDoc::NDC2WC(long xn, long yn, double &x, double &y)
{
x = xn / m_kx + m_xo;
y = yn / m_ky + m_yo;
}
void CMapDBDoc::OnQueryId()
{
CQueryDlg query(this);
query.SetQuery(IDQUERY);
if ( query.DoModal() == IDOK )
{
QuitSel();
UpdateAllViews(NULL);
ShowQuery(m_bShowQuery);
}
}
void CMapDBDoc::OnUpdateQueryId(CCmdUI* pCmdUI)
{
pCmdUI->Enable(IsInited());
}
void CMapDBDoc::OnQueryTitle()
{
CQueryDlg query(this);
query.SetQuery(TITLEQUERY);
if ( query.DoModal() == IDOK )
{
QuitSel();
UpdateAllViews(NULL);
ShowQuery(m_bShowQuery);
}
}
void CMapDBDoc::OnUpdateQueryTitle(CCmdUI* pCmdUI)
{
pCmdUI->Enable(IsInited());
}
void CMapDBDoc::OnQueryGrid()
{
m_bSelQuery = ! m_bSelQuery;
if ( ! m_bSelQuery )
{
QuitSel();
UpdateAllViews(NULL);
}
}
void CMapDBDoc::OnUpdateQueryGrid(CCmdUI* pCmdUI)
{
pCmdUI->Enable(IsInited());
pCmdUI->SetCheck(m_bSelQuery);
}
void CMapDBDoc::Release()
{
m_maptitle = _T("");
m_mapid = _T("");
m_mapscale = _T("");
m_mapAttributes = _T("");
m_mapdate = CTime::GetCurrentTime();
m_maptitles.RemoveAll();
m_mapobjids.RemoveAll();
m_objsel.RemoveAll();
m_titlesel.RemoveAll();
m_objidx.RemoveAll();
m_titleidx.RemoveAll();
m_grididx.RemoveAll();
m_sizePhoto.cx = 1;
m_sizePhoto.cy = 1;
m_sizeDraw.cx = 0;
m_sizeDraw.cy = 0;
if ( m_text )
{
delete [] m_text;
m_text = NULL;
}
if ( m_lpbiText )
{
delete [] (BYTE *) m_lpbiText;
m_lpbiText = NULL;
}
for (int count = 0;count < m_mapobjs.GetSize();count ++)
delete m_mapobjs[count];
m_mapobjs.RemoveAll();
for ( count = 0;count < m_normalpts.GetSize();count ++)
delete m_normalpts[count];
m_normalpts.RemoveAll();
for ( count = 0;count < m_controlpts.GetSize();count ++)
delete m_controlpts[count];
m_controlpts.RemoveAll();
for ( count = 0;count < m_schemapts.GetSize();count ++)
delete m_schemapts[count];
m_schemapts.RemoveAll();
m_usrfile = _T("");
m_xyfile = _T("");
m_domfile = _T("");
m_bSelQuery = false;
m_bShowQuery = false;
m_bInited = false;
}
void CMapDBDoc::GetObjIdx()
{
m_mapobjids.RemoveAll();
m_objidx.RemoveAll();
m_objsel.RemoveAll();
for (int count = 0;count < m_mapobjs.GetSize();count ++)
{
CMapObject * obj = m_mapobjs[count];
m_mapobjids.SetAt(obj->ID,count);
int idx = count % 8;
if ( ! idx )
m_objidx.Add(BITS());
m_objidx[count / 8].SetAt(idx);
}
m_objsel.Append(m_objidx);
}
void CMapDBDoc::GetTitleIdx()
{
m_maptitles.RemoveAll();
m_titlesel.RemoveAll();
m_titleidx.RemoveAll();
for (int count = 0;count < m_mapobjs.GetSize();count ++)
{
CMapObject * obj = m_mapobjs[count];
for (int titlecount = 0;titlecount < obj->m_titles.GetSize();titlecount += 2)
{
DWORD title = obj->m_titles[titlecount];
DWORD tvalue;
if ( ! m_maptitles.Lookup(title,tvalue) )
m_maptitles.SetAt(title,m_maptitles.GetCount());
}
}
m_titlesel.SetSize(GetByte(m_maptitles.GetCount()));
for (count = 0;count < m_maptitles.GetCount();count ++)
{
int idx = count % 8;
m_titlesel[count / 8].SetAt(idx);
}
m_titleidx.SetSize(m_maptitles.GetCount() * GetByte(m_mapobjs.GetSize()));
for (count = 0;count < m_mapobjs.GetSize();count ++)
{
CMapObject * obj = m_mapobjs[count];
for (int titlecount = 0;titlecount < obj->m_titles.GetSize();titlecount += 2)
{
DWORD title = obj->m_titles[titlecount];
DWORD tvalue;
if ( m_maptitles.Lookup(title,tvalue) )
{
int bytecount = tvalue * GetByte(m_mapobjs.GetSize()) + count / 8;
int idx = count % 8;
m_titleidx[bytecount].SetAt(idx);
}
}
}
}
void CMapDBDoc::GetGridIdx()
{
m_grididx.SetSize(GRIDCOUNT * GRIDCOUNT * GetByte(m_mapobjs.GetSize()));
BOOL grid[GRIDCOUNT][GRIDCOUNT];
for (int count = 0;count < m_mapobjs.GetSize();count ++)
{
ZeroMemory(grid,sizeof(grid));
CMapObject * obj = m_mapobjs[count];
if ( ! GetGrid(obj,grid) )
{
CString message;
message.LoadString(IDS_USRFILEERROR);
AfxMessageBox(message);
continue;
}
CRect rect;
GetGridRect(grid,rect);
for (int r = rect.bottom;r <= rect.top;r ++)
{
for (int c = rect.left;c <= rect.right;c ++)
{
if ( grid[r][c] )
{
long bytecount = ( r * GRIDCOUNT + c ) *
GetByte(m_mapobjs.GetSize()) + count / 8;
m_grididx[bytecount].SetAt(count % 8);
}
}
}
}
}
int CMapDBDoc::GetByte(DWORD bits)
{
int byte = bits / 8;
if ( bits % 8 )
byte ++;
return byte;
}
void CMapDBDoc::GetIndexs()
{
GetObjIdx();
GetTitleIdx();
GetGridIdx();
}
void CMapDBDoc::QueryGrid()
{
long xn0,yn0,xn1,yn1,gx0,gy0,gx1,gy1;
DC2NDC(m_selpt[0].y,m_selpt[0].x,xn0,yn0);
DC2NDC(m_selpt[1].y,m_selpt[1].x,xn1,yn1);
NDC2Grid(min(xn0,xn1),min(yn0,yn1),gy0,gx0);
NDC2Grid(max(xn0,xn1),max(yn0,yn1),gy1,gx1);
Grid2NDC(gy0,gx0,m_selpt[0].x,m_selpt[0].y,xn0,yn0);
Grid2NDC(gy1,gx1,xn1,yn1,m_selpt[1].x,m_selpt[1].y);
NDC2DC(m_selpt[0].x,m_selpt[0].y,yn0,xn0);
NDC2DC(m_selpt[1].x,m_selpt[1].y,yn1,xn1);
m_selpt[0] = CPoint(xn0,yn0);
m_selpt[1] = CPoint(xn1,yn1);
m_objsel.RemoveAll();
m_objsel.SetSize(m_mapobjs.GetSize());
for (long gycount = gy0;gycount <= gy1;gycount ++)
{
for (long gxcount = gx0;gxcount <= gx1;gxcount ++)
{
for (int objcount = 0;objcount < m_objsel.GetSize();objcount ++)
{
long bytecount = ( gycount * GRIDCOUNT + gxcount ) *
GetByte(m_mapobjs.GetSize()) + objcount / 8;
int idx = objcount % 8;
if ( m_grididx[bytecount][idx] )
m_objsel[objcount / 8].SetAt(objcount % 8);
}
}
}
UpdateAllViews(NULL);
ShowQuery(m_bShowQuery);
}
BOOL CMapDBDoc::GetGrid(CMapObject *obj,BOOL grid[GRIDCOUNT][GRIDCOUNT])
{
if ( obj->IsLine() )
{
for (int count = 0;count < obj->m_mpts.GetSize() - 1;count ++)
{
CMPoint * mpt = obj->m_mpts[count];
CMPoint * mpt1 = obj->m_mpts[count + 1];
GetGrid(mpt->x,mpt->y,mpt1->x,mpt1->y,grid);
}
}
else if ( obj->IsPlane() )
{
for (int count = 0;count < obj->m_mpts.GetSize();count ++)
{
CMPoint * mpt = obj->m_mpts[count];
GetGrid(mpt->x,mpt->y,grid);
}
CRect rect;
GetGridRect(grid,rect);
CRgn rgn;
if ( ! obj->GetPolyRgn(&rgn) )
return FALSE;
for (int r = rect.bottom;r <= rect.top;r ++)
{
for (int c = rect.left;c <= rect.right;c ++)
{
long x0,y0,x1,y1;
Grid2NDC(r,c,x0,y0,x1,y1);
if ( rgn.RectInRegion(CRect(x0,y0,x1,y1) ) )
grid[r][c] = true;
}
}
if ( rgn.m_hObject )
rgn.DeleteObject();
}
else
{
for (int count = 0;count < obj->m_mpts.GetSize();count ++)
{
CMPoint * mpt = obj->m_mpts[count];
GetGrid(mpt->x,mpt->y,grid);
}
}
return TRUE;
}
void CMapDBDoc::GetGrid(long x0, long y0, BOOL grid[GRIDCOUNT][GRIDCOUNT])
{
long r,c;
NDC2Grid(x0,y0,r,c);
grid[r][c] = true;
}
void CMapDBDoc::GetGrid(long x0, long y0, long x1, long y1, BOOL grid[GRIDCOUNT][GRIDCOUNT])
{
long r,c,r1,c1;
NDC2Grid(x0,y0,r,c);
NDC2Grid(x1,y1,r1,c1);
grid[r][c] = true;
grid[r1][c1] = true;
if ( r == r1 && c == c1 )
return;
if ( y1 == y0 )
{
for (int count = min(c,c1);count < max(c,c1);count ++)
grid[r][count] = true;
}
else if ( x1 == x0 )
{
for (int count = min(r,r1);count < max(r,r1);count ++)
grid[count][c] = true;
}
else
{
double k,kx,ky;
k = (y1 - y0) / (x1 - x0);
kx = 1.0 / sqrt( 1 + k * k );
ky = k / sqrt( 1 + k * k );
long d = (long)sqrt(( y1 - y0 ) * ( y1 - y0 ) + ( x1 - x0 ) * ( x1 - x0 ));
for (int t = DISTEST;t < d;t += DISTEST )
{
long x,y;
x = (long)(x0 + kx * DISTEST + 0.5);
y = (long)(y0 + ky * DISTEST + 0.5);
NDC2Grid(x,y,r,c);
grid[r][c] = true;
}
}
}
/*
void CMapDBDoc::GetGrid(long x0, long y0, long x1, long y1, BOOL grid[GRIDCOUNT][GRIDCOUNT])
{
long r,c,r1,c1;
NDC2Grid(x0,y0,r,c);
NDC2Grid(x1,y1,r1,c1);
grid[r][c] = true;
grid[r1][c1] = true;
if ( r == r1 && c == c1 )
return;
if ( y1 == y0 )
{
for (int count = c;count < c1;count ++)
grid[r][count] = true;
}
else if ( x1 == x0 )
{
for (int count = r;count < r1;count ++)
grid[count][c] = true;
}
else
{
long xmin,xmax,ymin,ymax;
double k,a0,b0,c0,f;
k = ( y1 - y0 ) / ( x1 - x0 );
a0 = -k;
b0 = 1;
c0 = k * x0 - y0;
if ( k > 0 )
{
if ( ( y1 - y0 ) > 0 )
{
for (long rt = r,ct = c;rt < r1 || ct < c1; )
{
Grid2NDC(rt,ct,xmin,ymin,xmax,ymax);
f = a0 * xmax + b0 * ymax + c0;
if ( fabs(f) < 10 )
{
rt ++;
ct ++;
}
else if ( f > 0 )
{
ct++;//rt ++;
}
else if ( f < 0 )
{
rt++;//ct ++;
}
grid[rt][ct] = true;
}
}
else
{
for (long rt = r,ct = c;rt > r1 || ct > c1; )
{
Grid2NDC( rt, ct, xmin, ymin, xmax, ymax);
f = a0 * xmin + b0 * ymin + c0;
if ( fabs(f) < 10 )
{
rt --;
ct --;
}
else if ( f > 0 )
{
ct --;//rt --;
}
else
{
rt --;//ct --;
}
grid[rt][ct] = true;
}
}
}
else
{
if ( ( y1 - y0 ) > 0 )
{
for (long rt = r,ct = c;rt != r1 || ct != c1; )
{
Grid2NDC( rt, ct, xmin, ymin, xmax, ymax);
f = a0 * xmin + b0 * ymax + c0;
if ( fabs(f) < ZEROTEST )
{
rt ++;
ct --;
}
else if ( f > 0 )
{
rt --;//ct --;
}
else
{
ct --;//rt ++;
}
grid[rt][ct] = true;
}
}
else
{
for (long rt = r,ct = c;rt != r1 || ct != c1; )
{
Grid2NDC( rt, ct, xmin, ymin, xmax, ymax);
f = a0 * xmax + b0 * ymin + c0;
if ( fabs(f) < ZEROTEST )
{
rt --;
ct ++;
}
else if ( f > 0 )
{
ct --;//rt --;
}
else
{
rt --;//ct ++;
}
grid[rt][ct] = true;
}
}
}
}
}
*/
void CMapDBDoc::OnRefresh()
{
UpdateAllViews(NULL);
}
void CMapDBDoc::GetGridRect(BOOL grid[GRIDCOUNT][GRIDCOUNT],CRect &rect)
{
long minr,minc,maxr,maxc;
minr = GRIDCOUNT;
minc = GRIDCOUNT;
maxr = -1;
maxc = -1;
for(int row = 0;row < GRIDCOUNT;row ++ )
{
for(int col = 0;col < GRIDCOUNT;col ++)
{
if ( grid[row][col] )
{
if ( row > maxr )
maxr = row;
if ( row < minr )
minr = row;
if ( col > maxc )
maxc = col;
if ( col < minc )
minc = col;
}
}
}
rect.left = minc;
rect.right = maxc;
rect.top = maxr;
rect.bottom = minr;
}
void CMapDBDoc::QueryAll()
{
m_objsel.RemoveAll();
m_objsel.SetSize(GetByte(m_mapobjs.GetSize()));
for (int count = 0;count < m_mapobjs.GetSize();count ++)
m_objsel[count / 8].SetAt(count % 8);
UpdateAllViews(NULL);
}
void CMapDBDoc::OnQueryall()
{
QueryAll();
QuitSel();
UpdateAllViews(NULL);
}
void CMapDBDoc::OnUpdateQueryall(CCmdUI* pCmdUI)
{
pCmdUI->Enable(IsInited());
}
void CMapDBDoc::QueryPos()
{
double x0 = 0;
double y0 = 0;
long selsum = 0;
for (int count = 0;count < m_objsel.GetSize();count ++)
{
for ( int bitcount = 0;bitcount < 8;bitcount ++)
if ( m_objsel[count][bitcount] )
{
CMapObject * obj = m_mapobjs[count * 8 + bitcount];
CPoint pt;
obj->GetCenter(pt);
x0 += pt.x;
y0 += pt.y;
selsum ++;
}
}
if ( selsum )
{
long x = (long)(x0 / selsum + 0.5);
long y = (long)(y0 / selsum + 0.5);
SetCenter(CPoint(x,y));
}
}
void CMapDBDoc::ShowQuery(BOOL bShow)
{
if ( ! m_querylist )
{
m_querylist = new CResultDlg(this,GetView());
m_querylist->Create(CResultDlg::IDD);
}
if ( m_querylist )
{
m_querylist->Update();
m_querylist->ShowWindow(bShow ? SW_SHOWNORMAL : SW_HIDE);
m_querylist->UpdateWindow();
}
}
void CMapDBDoc::OnQueryResult()
{
m_bShowQuery = ! m_bShowQuery;
ShowQuery(m_bShowQuery);
}
void CMapDBDoc::OnUpdateQueryResult(CCmdUI* pCmdUI)
{
if ( pCmdUI->m_pOther && pCmdUI->m_pOther->IsKindOf(RUNTIME_CLASS(CStatusBar)) )
{
CString text;
if ( IsInited() && m_bSelQuery && ! m_bShowQuery )
{
if ( ! m_querylist )
ShowQuery(m_bShowQuery);
if ( m_querylist->m_list.GetCount() )
m_querylist->m_list.GetText(0,text);
if ( m_querylist->m_list.GetCount() > 1 )
text += _T("...");
text = _T("\t") + text;
}
pCmdUI->SetText(text);
}
else
{
pCmdUI->Enable(IsInited());
pCmdUI->SetCheck(m_bShowQuery);
}
}
void CMapDBDoc::OnUpdateZoomin(CCmdUI* pCmdUI)
{
pCmdUI->Enable(IsInited() && ( m_sizeDraw.cx / m_sizePhoto.cx ) < ZOOMFAC );
}
void CMapDBDoc::OnUpdateZoomout(CCmdUI* pCmdUI)
{
pCmdUI->Enable(IsInited() && ( m_sizePhoto.cx / m_sizeDraw.cx ) < ZOOMFAC);
}
void CMapDBDoc::OnUpdateZoomOriginal(CCmdUI* pCmdUI)
{
pCmdUI->Enable(IsInited() && ( m_sizeDraw.cx != m_sizePhoto.cx ) );
}
void CMapDBDoc::QuitSel()
{
m_bSelQuery = FALSE;
m_selpt[1] = m_selpt[0];
}
void CMapDBDoc::SetCenter(CPoint pt)
{
long r,c;
NDC2DC(pt.x,pt.y,r,c);
double sx = (double)m_sizeDraw.cx / (double)m_sizePhoto.cx;
double sy = (double)m_sizeDraw.cy / (double)m_sizePhoto.cy;
r = (long)(r * sy + 0.5);
c = (long)(c * sx + 0.5);
CMapDBView * pView = GetView();
if ( pView )
{
CRect rect;
pView->GetClientRect(rect);
pt.x = c - rect.Width() / 2;
pt.y = r - rect.Height() / 2;
pView->ScrollToPosition(pt);
}
}
BOOL CMapDBDoc::GetImage(CString strFile)
{
if ( m_domfile.IsEmpty() || _access(m_domfile,0) )
{
CString file = m_domfile;
CString path = strFile;
if ( file.ReverseFind('\\') > 0 )
file = file.Right(file.GetLength() - file.ReverseFind('\\') - 1);
if ( ! file.IsEmpty() && path.ReverseFind('\\') > 0 )
{
path.Left(path.ReverseFind('\\'));
m_domfile.Format("%s\\%s",path.Left(path.ReverseFind('\\')),file);
}
if ( m_domfile.IsEmpty() || _access(m_domfile,0) )
{
CString message;
message.LoadString(IDS_FILENOTFOUND);
CFileDialog domfile(TRUE,"jpg",file,OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST,_T("DOM files(*.jpg;*.bmp)|*.jpg;*.bmp||"));
domfile.m_ofn.lpstrTitle = message;
if ( domfile.DoModal() == IDOK )
m_domfile = domfile.GetPathName();
else
return FALSE;
}
SetModifiedFlag();
}
return LoadImage(m_domfile);
}