// DlgDemAlt.cpp : 实现文件
//

#include "stdafx.h"
#include "DlgDemAlt.h"
#include "afxdialogex.h"

#include "MapElevation.h"

MapElevation m_mapElevationDem;

// CDlgDemAlt 对话框

IMPLEMENT_DYNAMIC(CDlgDemAlt, CBCGPDialog)

CDlgDemAlt::CDlgDemAlt(CWnd* pParent /*=NULL*/)
	: CBCGPDialog(CDlgDemAlt::IDD, pParent)
{
	EnableVisualManagerStyle(TRUE, TRUE);
}

CDlgDemAlt::~CDlgDemAlt()
{
}

void CDlgDemAlt::DoDataExchange(CDataExchange* pDX)
{
	CBCGPDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_CONTAINER_CHARTDEM, m_wndChart);
}


BEGIN_MESSAGE_MAP(CDlgDemAlt, CBCGPDialog)
	ON_WM_SIZE()
	ON_WM_CLOSE()
END_MESSAGE_MAP()


// CDlgDemAlt 消息处理程序


BOOL CDlgDemAlt::OnInitDialog()
{
	CBCGPDialog::OnInitDialog();

	CenterWindow();

	pChart = m_wndChart.GetChart();
	ASSERT_VALID(pChart);

	//Release模式下,曲线背景会变白,在此设置颜色
#ifdef DEBUG
	//pChart->SetColors(CBCGPChartTheme::CT_DEFAULT);
#else
	pChart->SetColors(CBCGPChartTheme::CT_DEFAULT, TRUE);
#endif

	//设置放大缩小方式
	pChart->SetZoomScrollConfig(BCGPChartMouseConfig::ZSO_WHEEL_PAN, BCGPChartFormatSelection::ST_VERT_AXIS_ONLY);
	pChart->SetSelectionType(BCGPChartFormatSelection::ST_VERT_AXIS_ONLY);

	//曲线使能鼠标跟踪
	pChart->EnableMouseTrackingMode(
		BCGPChartHitInfo::HIT_DATA_POINT |
		BCGPChartHitInfo::HIT_DATA_LABEL |
		BCGPChartHitInfo::HIT_AXIS |
		BCGPChartHitInfo::HIT_AXIS_NAME |
		BCGPChartHitInfo::HIT_CHART_AREA |
		BCGPChartHitInfo::HIT_LEGEND |
		BCGPChartHitInfo::HIT_TITLE |
		BCGPChartHitInfo::HIT_DIAGRAM |
		BCGPChartHitInfo::HIT_DATA_TABLE |
		BCGPChartHitInfo::HIT_ALL_ELEMENTS);

	//设置:当鼠标移动到曲线上,会显示当前点的信息
	CBCGPInfoTipOptions infoTipOptions;
	infoTipOptions.m_StemLocation = CBCGPPopupWindow::BCGPPopupWindowStemLocation_BottomCenter;
	m_wndChart.EnableInfoTip(TRUE, &infoTipOptions);

	pChart->SetLegendPosition(BCGPChartLayout::LP_NONE);

	pXAxis = pChart->GetChartAxis(BCGP_CHART_X_PRIMARY_AXIS);
	pYAxis = pChart->GetChartAxis(BCGP_CHART_Y_PRIMARY_AXIS);

	//创建曲线1,绘制高程信息
	pSeries1 = pChart->CreateSeries(_T(""), CBCGPColor(), BCGP_CT_DEFAULT, BCGPChartArea);

	pSeries2 = pChart->CreateSeries(_T(""), CBCGPColor(/*CBCGPColor::LimeGreen*/), BCGP_CT_DEFAULT, BCGPChartLine);
	BCGPChartFormatSeries style = pSeries2->GetSeriesFormat();
	style.m_curveType = BCGPChartFormatSeries::CCT_NO_LINE;
	pSeries2->SetSeriesFormat(style);

	pSeries2->ShowDataLabel(TRUE);
	pSeries2->ShowMarker(TRUE);

	pChart->SetDirty(TRUE, TRUE);
	m_wndChart.RedrawWindow();

	return TRUE;
}

void CDlgDemAlt::DrawDemAltBetweenPts(double* ptLon, double* ptLat, int ptNumber)
{
	pSeries1->RemoveAllDataPoints();
	pSeries2->RemoveAllDataPoints();
	pChart->RemoveAllChartObjects();

	double _dHaveGotDis = -30;

	for (int i=0;i<(ptNumber-1);i++)
	{
		double _dSecDis, _dSecDir; //航段两点的距离和方向

		//计算航段起点和终点的水平距离和方位角
		CalculateTwoPtsDistanceAzimuth(_dSecDis, _dSecDir, ptLon[i], ptLat[i], ptLon[i+1], ptLat[i+1], 3);

		//插值距离
		int _iInterDis = 30;

		//以100米为间距,计算插值个数
		int _iInterNum = (int)(floor(_dSecDis/_iInterDis));

		//插值点位置
		double interPolLon = 0.0, interPolLat = 0.0;
		float interPolHight = 0.0;

		for (int j=0;j<=_iInterNum;j++)
		{
			CalculatePtCoordinate(interPolLon, interPolLat, ptLon[i], ptLat[i], _dSecDir, j*_iInterDis, 3);

			//获取插值点的高程
			if(!m_mapElevationDem.getElevation(interPolHight, interPolLon, interPolLat))
			{
				//高程信息不全
				BCGPMessageBox(_T("缺少高程数据!"));
				return ;
			}

			_dHaveGotDis += _iInterDis;

			pSeries1->AddDataPoint(interPolHight, _dHaveGotDis);

			//标记第一个高程分析点序号
			if ((i == 0) && (j == 0))
			{
				pSeries2->AddDataPoint(interPolHight, 0.0);

				CString str;
				str.Format(_T("%d"),i+1);
				pSeries2->SetDataLabelDataFormat(str,i);
			}
		}

		//标记高程分析的点序号
		{
			pSeries2->AddDataPoint(interPolHight, _dHaveGotDis);

			CString str;
			str.Format(_T("%d"),i+2);
			pSeries2->SetDataLabelDataFormat(str,i+1);
		}

		//绘制纵向分隔线
		if (ptNumber > 1)
		{
			CBCGPStrokeStyle m_strokeStyle;
			m_strokeStyle.SetDashStyle(CBCGPStrokeStyle::BCGP_DASH_STYLE_DOT);

			CBCGPChartLineObject* m_pLineSep;
			m_pLineSep = pChart->AddChartLineObject(_dHaveGotDis, 0, _dHaveGotDis, interPolHight, CBCGPBrush(CBCGPColor::Tomato), 1.5, &m_strokeStyle);
		}
	}

	pChart->SetAutoDisplayRange(TRUE, TRUE);
	pChart->SetDirty(TRUE, TRUE);
	m_wndChart.RedrawWindow();
}

void CDlgDemAlt::OnSize(UINT nType, int cx, int cy)
{
	CBCGPDialog::OnSize(nType, cx, cy);

	//图表控件随对话框大小变化而变化
	if (m_wndChart.GetSafeHwnd())
	{
		CRect rect;
		GetClientRect(&rect);

		/*CRect rectChart;
		m_wndChart.GetWindowRect(rectChart);

		ScreenToClient(rectChart);

		rectChart.right  = rect.right - 5;
		rectChart.bottom = rect.bottom - 5;*/

		m_wndChart.MoveWindow(&rect,TRUE);
	}
}

void CDlgDemAlt::OnClose()
{
	::PostMessage(g_mapHwnd,WM_SEND_CLOSEDEMALT,0,0);

	CBCGPDialog::OnClose();
}