File: mapdlg.cpp

package info (click to toggle)
rtklib 2.4.3%2Bdfsg1-2.1
  • links: PTS
  • area: main
  • in suites: bullseye
  • size: 41,796 kB
  • sloc: cpp: 51,592; ansic: 50,584; fortran: 987; makefile: 861; sh: 45
file content (478 lines) | stat: -rw-r--r-- 15,034 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "rtklib.h"
#include "navimain.h"
#include "pntdlg.h"
#include "mapdlg.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TMapDialog *MapDialog;

#define CHARDEG		"\260"			// character code of degree
#define CLORANGE    (TColor)0x00AAFF
#define CLLGRAY     (TColor)0xDDDDDD
#define SOLSIZE		13				// solution cycle size on map
#define PNTSIZE		10				// point cycle size on map
#define HISSIZE		4				// history cycle size on map
#define VELSIZE		40				// velocity cycle size on map
#define GRPHEIGHT	70				// vertical graph height
#define GRPHMARGIN	60				// vertical graph horizontal margin

//---------------------------------------------------------------------------
__fastcall TMapDialog::TMapDialog(TComponent* Owner)
	: TForm(Owner)
{
	Plot=new Graphics::TBitmap;
	Scale=6;
	PntIndex=0;
	DoubleBuffered=true;
	for (int i=0;i<3;i++) {
		RefPos[i]=CurrentPos[i]=CentPos[i]=0.0;
	}
	RefName="Start Point";
}
//---------------------------------------------------------------------------
void __fastcall TMapDialog::FormShow(TObject *Sender)
{
	UpdatePntList();
	UpdateEnable();
}
//---------------------------------------------------------------------------
void __fastcall TMapDialog::BtnCloseClick(TObject *Sender)
{
	Close();
}
//---------------------------------------------------------------------------
void __fastcall TMapDialog::BtnShrinkClick(TObject *Sender)
{
	if (Scale<MAXSCALE) Scale++;
	MainForm->UpdateMap();
}
//---------------------------------------------------------------------------
void __fastcall TMapDialog::BtnExpandClick(TObject *Sender)
{
	if (Scale>0) Scale--;
	MainForm->UpdateMap();
}
//---------------------------------------------------------------------------
void __fastcall TMapDialog::BtnPntClick(TObject *Sender)
{
	UpdateEnable();
}
//---------------------------------------------------------------------------
void __fastcall TMapDialog::PntListChange(TObject *Sender)
{
	PntIndex=PntList->ItemIndex;
	MainForm->UpdateMap();
}
//---------------------------------------------------------------------------
void __fastcall TMapDialog::BtnCenterClick(TObject *Sender)
{
	if (!BtnCenter->Down) {
		for (int i=0;i<3;i++) CentPos[i]=CurrentPos[i];
	}
	MainForm->UpdateMap();
}
//---------------------------------------------------------------------------
void __fastcall TMapDialog::BtnTrackClick(TObject *Sender)
{
	MainForm->UpdateMap();
}
//---------------------------------------------------------------------------
void __fastcall TMapDialog::BtnPntDlgClick(TObject *Sender)
{
	PntDialog->Left=Left+Width/2-PntDialog->Width/2;
	PntDialog->Top=Top+Height/2-PntDialog->Height/2;
	if (PntDialog->ShowModal()!=mrOk) return;
	UpdatePntList();
	MainForm->UpdateMap();
}
//---------------------------------------------------------------------------
void __fastcall TMapDialog::FormResize(TObject *Sender)
{
	Plot->SetSize(Disp->ClientWidth,Disp->ClientHeight);
	MainForm->UpdateMap();
}
//---------------------------------------------------------------------------
void __fastcall TMapDialog::DispMouseDown(TObject *Sender, TMouseButton Button,
      TShiftState Shift, int X, int Y)
{
	if (BtnCenter->Down||!Shift.Contains(ssLeft)) return;
	Drag=1;
	X0=X;
	Y0=Y;
	for (int i=0;i<3;i++) CentPos0[i]=CentPos[i];
	Screen->Cursor=crSizeAll;
}
//---------------------------------------------------------------------------
void __fastcall TMapDialog::DispMouseMove(TObject *Sender, TShiftState Shift,
      int X, int Y)
{
	double sc[]={
		0.01,0.02,0.05,0.1,0.2,0.5,1,2,5,10,20,50,100,200,500,1000,2000,5000,10000
	};
	double rr[3],posr[3],enu[3],fact=40.0/sc[Scale];
	if (!Drag) return;
	enu[0]=(X0-X)/fact;
	enu[1]=(Y-Y0)/fact;
	enu[2]=0.0;
	ecef2pos(RefPos,posr);
	enu2ecef(posr,enu,rr);
	for (int i=0;i<3;i++) CentPos[i]=CentPos0[i]+rr[i];
	MainForm->UpdateMap();
}
//---------------------------------------------------------------------------
void __fastcall TMapDialog::DispMouseUp(TObject *Sender, TMouseButton Button,
      TShiftState Shift, int X, int Y)
{
	if (Drag) Screen->Cursor=crDefault;
	Drag=0;
}
//---------------------------------------------------------------------------
void __fastcall TMapDialog::DispPaint(TObject *Sender)
{
	TRect r=Disp->ClientRect;
	Disp->Canvas->CopyRect(r,Plot->Canvas,r);
}
//---------------------------------------------------------------------------
void __fastcall TMapDialog::ResetRef(void)
{
	RefName="Start Point";
	UpdatePntList();
	for (int i=0;i<3;i++) RefPos[i]=0.0;
}
//---------------------------------------------------------------------------
void __fastcall TMapDialog::UpdateMap(const double *sol, const double *ref,
	const double *vel, const int *stat, int psol, int psols, int psole,
	int nsol, AnsiString *solstr, int currentstat)
{
	TColor color[]={clWhite,clGreen,CLORANGE,clFuchsia,clBlue,clRed,clTeal};
	TCanvas *c=Plot->Canvas;
	AnsiString s;
	TPoint p0;
	double pos[3],posr[3],rr[3],enu[3],enuv[3],dr[3],dir,*pntpos;
	int gint[]={20,20,16,20,20,16,20,20,16,20,20,16,20,20,16,20,20,16,20};
	int ng[]={10,5,5,10,5,5,10,5,5,10,5,5,10,5,5,10,5,5,10};
	TColor colsol=color[stat[psol]];
	
	TRect r(0,0,Plot->Width,Plot->Height);
	c->Brush->Style=bsSolid;
	c->Brush->Color=clWhite;
	c->FillRect(r);
	
	for (int i=0;i<3;i++) {
		CurrentPos[i]=sol[i+psol*3];
	}
	if (norm(RefPos,3)<=0.0) {
		if (ref&&norm(ref+psol,3)>0.0) {
			RefName="Base Station";
			for (int i=0;i<3;i++) RefPos[i]=ref[i+psol*3];
			UpdatePntList();
		}
		else  {
			for (int i=0;i<3;i++) RefPos[i]=CurrentPos[i];
		}
	}
	ecef2pos(RefPos,posr);
	ecef2enu(posr,vel+psol*3,enuv);
	
	DrawGrid(PosToPoint(RefPos),gint[Scale],ng[Scale],CLLGRAY,clSilver);
	
	if (BtnPnt->Down) {
		for (int i=0;i<MainForm->NMapPnt+1;i++) {
			DrawPoint(i==0?RefPos:MainForm->PntPos[i-1],PntList->Items->Strings[i],
					  i==PntIndex?clGray:clSilver);
		}
	}
	if (BtnTrack->Down) {
		TPoint *p  =new TPoint[nsol];
		TColor *col=new TColor[nsol];
		int n=0;
		for (int i=psols;i!=psole;) {
			p[n]=PosToPoint(sol+i*3);
			col[n++]=color[stat[i]];
			if (++i>=nsol) i=0;
		}
		c->Pen->Color=CLLGRAY;
		c->Pen->Style=psSolid;
		c->Polyline(p,n-1);
		for (int i=0;i<n;i++) {
			DrawCircle(p[i],HISSIZE,col[i],col[i]);
		}
		delete [] p;
		delete [] col;
	}
	p0=PosToPoint(CurrentPos);
	c->Pen->Color=clBlack;
	c->MoveTo(p0.x-SOLSIZE/2-4,p0.y); c->LineTo(p0.x+SOLSIZE/2+4,p0.y);
	c->MoveTo(p0.x,p0.y-SOLSIZE/2-4); c->LineTo(p0.x,p0.y+SOLSIZE/2+4);
	DrawCircle(p0,SOLSIZE+4,clBlack,clWhite);
	DrawCircle(p0,SOLSIZE,clBlack,color[currentstat]);
	
	DrawVel(enuv);
	DrawScale();
	
	c->Brush->Style=bsSolid;
	c->Brush->Color=clWhite;
	DrawText(6,1,solstr[0],currentstat?colsol:clGray,0);
	TSize off=c->TextExtent(solstr[2]);
	DrawText(Plot->Width-off.cx-6,1,solstr[2],clGray,0);
	DrawText(50,1,solstr[1],clBlack,0);
	
	if (BtnPnt->Down) {
		pntpos=PntIndex>0?MainForm->PntPos[PntIndex-1]:RefPos;
		for (int i=0;i<3;i++) {
			rr[i]=pntpos[i]-CurrentPos[i];
		}
		ecef2pos(pntpos,pos);
		ecef2enu(posr,rr,enu);
		dir=norm(enu,2)<1E-9?0.0:atan2(enu[0],enu[1])*R2D;
		if (dir<0.0) dir+=360.0;
		s.sprintf("To %s: Distance %.3fm Direction %.1f%s",
			PntList->Items->Strings[PntIndex].c_str(),norm(enu,2),dir,CHARDEG);
		int y=Plot->Height-off.cy/2-2;
		if (BtnGraph->Down) y-=GRPHEIGHT+2;
		DrawText(Plot->Width/2,y,s,clGray,1);
	}
	if (BtnGraph->Down) {
		DrawVertGraph(sol,stat,psol,psols,psole, nsol,currentstat);
	}
	Disp->Invalidate();
}
//---------------------------------------------------------------------------
void __fastcall TMapDialog::DrawVertGraph(const double *sol,
	const int *stat, int psol, int psols, int psole, int nsol, int currentstat)
{
	TColor color[]={clWhite,clGreen,CLORANGE,clFuchsia,clBlue,clRed};
	TCanvas *c=Plot->Canvas;
	TRect rg(GRPHMARGIN,Plot->Height-GRPHEIGHT-2,Plot->Width-GRPHMARGIN,Plot->Height-2);
	TPoint *p=new TPoint[nsol],p0;
	TColor *col=new TColor[nsol];
	int n=0,m=0;
	
	for (int i=psols;i!=psole;) {
		n++; if (++i>=nsol) i=0;
	}
	for (int i=psols;i!=psole;) {
		p[m]=PosToGraphP(sol+i*3,sol+psol*3,nsol-n+m,nsol,rg);
		if (i==psol) p0=p[m];
		col[m++]=color[stat[i]];
		if (++i>=nsol) i=0;
	}
	c->Brush->Style=bsSolid;
	c->Brush->Color=clWhite;
	c->FillRect(rg);
	c->Pen->Color=clSilver;
	c->Pen->Style=psSolid;
	c->Rectangle(rg);
	c->MoveTo(rg.Left,(rg.Top+rg.Bottom)/2);
	c->LineTo(rg.Right,(rg.Top+rg.Bottom)/2);
	c->Pen->Color=clSilver;
	c->MoveTo(p0.x,rg.Bottom); c->LineTo(p0.x,rg.Top);
	for (int i=0;i<nsol;i++) {
		if (i%100) continue;
		int x=rg.Left+(rg.Right-rg.Left)*((nsol+i-psole)%nsol)/nsol;
		int y=(rg.Top+rg.Bottom)/2;
		c->MoveTo(x,y-HISSIZE); c->LineTo(x,y+HISSIZE);
	}
	int i,j;
	for (i=0;i<m;i=j) {
		for (j=i;j<m;j++) {
			if (p[j].y<rg.Top+HISSIZE/2||rg.Bottom-HISSIZE/2<p[j].y) break;
		}
		if (i<j) c->Polyline(p+i,j-i-1);
		for (;j<m;j++) {
			if (rg.Top+HISSIZE/2<=p[j].y&&p[j].y<=rg.Bottom-HISSIZE/2) break;
		}
	}
	for (int i=0;i<m;i++) {
		if (p[i].y<rg.Top+HISSIZE/2||rg.Bottom-HISSIZE/2<p[i].y) continue;
		DrawCircle(p[i],HISSIZE,col[i],col[i]);
	}
	DrawCircle(p0,HISSIZE+6,clBlack,color[currentstat]);
	delete [] p;
	delete [] col;
}
//---------------------------------------------------------------------------
TPoint __fastcall TMapDialog::PosToPoint(const double *pos)
{
	double sc[]={
		0.01,0.02,0.05,0.1,0.2,0.5,1,2,5,10,20,50,100,200,500,1000,2000,5000,10000
	};
	double rr[3],posr[3],enu[3],fact=40.0/sc[Scale];
	TPoint p;
	ecef2pos(RefPos,posr);
	for (int i=0;i<3;i++) rr[i]=pos[i]-RefPos[i];
	ecef2enu(posr,rr,enu);
	p.x=Plot->Width /2+(int)(enu[0]*fact+0.5);
	p.y=Plot->Height/2-(int)(enu[1]*fact+0.5);
	if (BtnCenter->Down) {
		for (int i=0;i<3;i++) rr[i]=CurrentPos[i]-RefPos[i];
		ecef2enu(posr,rr,enu);
		p.x-=(int)(enu[0]*fact+0.5);
		p.y+=(int)(enu[1]*fact+0.5);
	}
	else if (norm(CentPos,3)>0.0) {
		for (int i=0;i<3;i++) rr[i]=CentPos[i]-RefPos[i];
		ecef2enu(posr,rr,enu);
		p.x-=(int)(enu[0]*fact+0.5);
		p.y+=(int)(enu[1]*fact+0.5);
	}
	return p;
}
//---------------------------------------------------------------------------
TPoint __fastcall TMapDialog::PosToGraphP(const double *pos,
	const double *ref, int index, int npos, TRect rect)
{
	double sc[]={
		0.01,0.02,0.05,0.1,0.2,0.5,1,2,5,10,20,50,100,200,500,1000,2000,5000,10000
	};
	double rr[3],posr[3],enu[3],fact=40.0/sc[Scale];
	TPoint p;
	ecef2pos(ref,posr);
	for (int i=0;i<3;i++) rr[i]=pos[i]-ref[i];
	ecef2enu(posr,rr,enu);
	p.x=rect.Left+(int)((rect.Right-rect.Left)*index/(npos-1.0)+0.5);
	p.y=(rect.Top+rect.Bottom)/2-(int)(enu[2]*fact+0.5);
	return p;
}
//---------------------------------------------------------------------------
void __fastcall TMapDialog::DrawPoint(const double *pos, AnsiString name,
	TColor color)
{
	TCanvas *c=Plot->Canvas;
	TPoint p=PosToPoint(pos);
	TSize off=c->TextExtent(name);
	c->Brush->Style=bsSolid;
	DrawCircle(p,PNTSIZE,color,color);
	c->Pen->Color=color;
	c->MoveTo(p.x-PNTSIZE/2-4,p.y); c->LineTo(p.x+PNTSIZE/2+4,p.y);
	c->MoveTo(p.x,p.y-PNTSIZE/2-4); c->LineTo(p.x,p.y+PNTSIZE/2+4);
	c->Brush->Style=bsClear;
	DrawText(p.x,p.y+PNTSIZE/2+off.cy/2+1,name,color,1);
}
//---------------------------------------------------------------------------
void __fastcall TMapDialog::DrawVel(const double *vel)
{
	TCanvas *c=Plot->Canvas;
	AnsiString s;
	double v,ang;
	TPoint p;
	TColor color=clGray;
	
	p.x=VELSIZE/2+10;
	p.y=Plot->Height-VELSIZE/2-15;
	c->Brush->Style=bsClear;
	if ((v=norm(vel,3))>1.0) {
		ang=atan2(vel[0],vel[1])*R2D;
		DrawArrow(p,VELSIZE-2,(int)ang,color);
	}
	DrawCircle(p,VELSIZE,color,(TColor)-1);
	DrawCircle(p,9,color,clWhite);
	DrawText(p.x,p.y-VELSIZE/2-7,"N",color,1);
	DrawText(p.x,p.y+VELSIZE/2+7,s.sprintf("%.0f km/h",v*3.6),color,1);
}
//---------------------------------------------------------------------------
void __fastcall TMapDialog::DrawScale(void)
{
	TCanvas *c=Plot->Canvas;
	AnsiString s;
	double sc[]={
		0.01,0.02,0.05,0.1,0.2,0.5,1,2,5,10,20,50,100,200,500,1000,2000,5000,10000
	};
	int pc[][2]={{-20,-4},{-20,4},{-20,0},{20,0},{20,-4},{20,4}};
	int x=Plot->Width-35,y=Plot->Height-15;
	TPoint p[6];
	
	for (int i=0;i<6;i++) {
		p[i].x=x+pc[i][0];
		p[i].y=y+pc[i][1];
	}
	c->Pen->Color=clGray;
	c->Polyline(p,5);
	c->Brush->Style=bsClear;
	char *unit="m";
	double sf=sc[Scale];
	if (sf<1.0) {sf*=100.0; unit="cm";}
	else if (sf>=1000.0) {sf/=1000.0; unit="km";}
	DrawText(x,y-10,s.sprintf("%.0f %s",sf,unit),clGray,1);
}
//---------------------------------------------------------------------------
void __fastcall TMapDialog::DrawCircle(TPoint p, int r, TColor color1,
	TColor color2)
{
	TCanvas *c=Plot->Canvas;
	int x1=p.x-r/2,x2=x1+r,y1=p.y-r/2,y2=y1+r;
	c->Pen->Color=color1; c->Pen->Style=psSolid; c->Pen->Width=1;
	if (color2!=-1) {
		c->Brush->Color=color2; c->Brush->Style=bsSolid;
	}
	else {
		c->Brush->Style=bsClear;
	}
	c->Ellipse(x1,y1,x2,y2);
}
//---------------------------------------------------------------------------
void __fastcall TMapDialog::DrawGrid(TPoint p, int gint, int ng,
	TColor color1, TColor color2)
{
	TCanvas *c=Plot->Canvas;
	int i,w=Plot->Width,h=Plot->Height,x,y;
	for (i=-p.x/gint;i<=(w-p.x)/gint;i++) {
		c->Pen->Color=i%ng?color1:color2;
		c->MoveTo(p.x+i*gint,0); c->LineTo(p.x+i*gint,h);
	}
	for (i=-p.y/gint;i<=(h-p.y)/gint;i++) {
		c->Pen->Color=i%ng?color1:color2;
		c->MoveTo(0,p.y+i*gint); c->LineTo(w,p.y+i*gint);
	}
}
//---------------------------------------------------------------------------
void __fastcall TMapDialog::DrawText(int x, int y, AnsiString s,
	TColor color, int align)
{
	TCanvas *c=Plot->Canvas;
	TSize off=c->TextExtent(s);
	c->Font->Charset=ANSI_CHARSET;
	if (align==1) {x-=off.cx/2; y-=off.cy/2;} else if (align==2) x-=off.cx;
	c->Font->Color=color;
	c->TextOut(x,y,s);
}
//---------------------------------------------------------------------------
void __fastcall TMapDialog::DrawArrow(TPoint p, int siz, int ang,
	TColor color)
{
	TCanvas *c=Plot->Canvas;
	TPoint p1[6],p2[6];
	p1[0].x=p1[1].x=0; p1[2].x=3; p1[3].x=0; p1[4].x=-3; p1[5].x=0;
	p1[0].y=-siz/2; p1[1].y=siz/2; p1[2].y=p1[4].y=p1[1].y-6;
	p1[3].y=p1[5].y=siz/2;
	
	for (int i=0;i<6;i++) {
		p2[i].x=p.x+(int)(p1[i].x*cos(-ang*D2R)-p1[i].y*sin(-ang*D2R)+0.5);
		p2[i].y=p.y-(int)(p1[i].x*sin(-ang*D2R)+p1[i].y*cos(-ang*D2R)+0.5);
	}
	c->Pen->Color=color;
	c->Polyline(p2,5);
}
//---------------------------------------------------------------------------
void __fastcall TMapDialog::UpdatePntList(void)
{
	PntList->Items->Clear();
	PntList->Items->Add(RefName);
	for (int i=0;i<MainForm->NMapPnt;i++) {
		PntList->Items->Add(MainForm->PntName[i]);
	}
	if (PntIndex>=PntList->Items->Count) PntIndex--;
	PntList->ItemIndex=PntIndex;
}
//---------------------------------------------------------------------------
void __fastcall TMapDialog::UpdateEnable(void)
{
	PntList  ->Enabled=BtnPnt->Down;
	BtnPntDlg->Enabled=BtnPnt->Down;
}
//---------------------------------------------------------------------------