-
Notifications
You must be signed in to change notification settings - Fork 3
/
MapViewer.cc
361 lines (303 loc) · 7.5 KB
/
MapViewer.cc
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
// -*- c++ -*-
// Copyright 2009 Isis Innovation Limited
#include <algorithm>
#include "MapViewer.h"
#include "MapPoint.h"
#include "KeyFrame.h"
#include "LevelHelpers.h"
#include <iomanip>
#include <cvd/gl_helpers.h>
namespace PTAMM {
using namespace CVD;
using namespace std;
/**
* Map viewer constructor
* @param maps the vector of all maps
* @param map the current map
* @param glw the GL window reference
*/
MapViewer::MapViewer(std::vector<Map*> &maps, Map *map, GLWindow2 &glw):
mvpMaps(maps),
mpMap(map),
mpViewingMap(map),
mGLWindow(glw),
mbBrowseMode(false)
{
mse3ViewerFromWorld =
SE3<>::exp(makeVector(0,0,2,0,0,0)) * SE3<>::exp(makeVector(0,0,0,0.8 * M_PI,0,0));
}
/**
* Draw the map dots
*/
void MapViewer::DrawMapDots()
{
SetupFrustum();
SetupModelView();
int nForMass = 0;
glColor3f(0,1,1);
glPointSize(3);
glBegin(GL_POINTS);
mv3MassCenter = Zeros;
for(size_t i=0; i<mpViewingMap->vpPoints.size(); i++)
{
Vector<3> v3Pos = mpViewingMap->vpPoints[i]->v3WorldPos;
glColor(gavLevelColors[mpViewingMap->vpPoints[i]->nSourceLevel]);
if( (v3Pos * v3Pos) < 10000)
{
nForMass++;
mv3MassCenter += v3Pos;
}
glVertex(v3Pos);
}
glEnd();
mv3MassCenter = mv3MassCenter / (0.1 + nForMass);
}
/**
* Draw the Grid
*/
void MapViewer::DrawGrid()
{
SetupFrustum();
SetupModelView();
glLineWidth(1);
glBegin(GL_LINES);
// Draw a larger grid around the outside..
double dGridInterval = 0.1;
double dMin = -100.0 * dGridInterval;
double dMax = 100.0 * dGridInterval;
for(int x=-10;x<=10;x+=1)
{
if(x==0)
glColor3f(1,1,1);
else
glColor3f(0.3,0.3,0.3);
glVertex3d((double)x * 10 * dGridInterval, dMin, 0.0);
glVertex3d((double)x * 10 * dGridInterval, dMax, 0.0);
}
for(int y=-10;y<=10;y+=1)
{
if(y==0)
glColor3f(1,1,1);
else
glColor3f(0.3,0.3,0.3);
glVertex3d(dMin, (double)y * 10 * dGridInterval, 0.0);
glVertex3d(dMax, (double)y * 10 * dGridInterval, 0.0);
}
glEnd();
glBegin(GL_LINES);
dMin = -10.0 * dGridInterval;
dMax = 10.0 * dGridInterval;
for(int x=-10;x<=10;x++)
{
if(x==0)
glColor3f(1,1,1);
else
glColor3f(0.5,0.5,0.5);
glVertex3d((double)x * dGridInterval, dMin, 0.0);
glVertex3d((double)x * dGridInterval, dMax, 0.0);
}
for(int y=-10;y<=10;y++)
{
if(y==0)
glColor3f(1,1,1);
else
glColor3f(0.5,0.5,0.5);
glVertex3d(dMin, (double)y * dGridInterval, 0.0);
glVertex3d(dMax, (double)y * dGridInterval, 0.0);
}
glColor3f(1,0,0);
glVertex3d(0,0,0);
glVertex3d(1,0,0);
glColor3f(0,1,0);
glVertex3d(0,0,0);
glVertex3d(0,1,0);
glColor3f(1,1,1);
glVertex3d(0,0,0);
glVertex3d(0,0,1);
glEnd();
// glColor3f(0.8,0.8,0.8);
// glRasterPos3f(1.1,0,0);
// mGLWindow.PrintString("x");
// glRasterPos3f(0,1.1,0);
// mGLWindow.PrintString("y");
// glRasterPos3f(0,0,1.1);
// mGLWindow.PrintString("z");
}
/**
* Draw the map
* @param se3CamFromWorld Current camera location
*/
void MapViewer::DrawMap(SE3<> se3CamFromWorld)
{
mMessageForUser.str(""); // Wipe the user message clean
// Update viewer position according to mouse input:
{
pair<Vector<6>, Vector<6> > pv6 = mGLWindow.GetMousePoseUpdate();
SE3<> se3CamFromMC;
se3CamFromMC.get_translation() = mse3ViewerFromWorld * mv3MassCenter;
mse3ViewerFromWorld = SE3<>::exp(pv6.first) *
se3CamFromMC * SE3<>::exp(pv6.second) * se3CamFromMC.inverse() * mse3ViewerFromWorld;
}
mGLWindow.SetupViewport();
glClearColor(0,0,0,0);
glClearDepth(1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glEnable(GL_POINT_SMOOTH);
glEnable(GL_LINE_SMOOTH);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glColorMask(1,1,1,1);
glEnable(GL_DEPTH_TEST);
DrawGrid();
DrawMapDots();
if( mpViewingMap == mpMap ) {
DrawCamera(se3CamFromWorld);
}
for(size_t i=0; i<mpViewingMap->vpKeyFrames.size(); i++)
DrawCamera(mpViewingMap->vpKeyFrames[i]->se3CfromW, true);
glDisable(GL_DEPTH_TEST);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
mMessageForUser << " Map " << mpViewingMap->MapID() << ": "
<< mpViewingMap->vpPoints.size() << "P, " << mpViewingMap->vpKeyFrames.size() << "KF";
mMessageForUser << setprecision(4);
mMessageForUser << " Camera Pos: " << se3CamFromWorld.inverse().get_translation();
}
/**
* Return the status bar message
* @return the message string
*/
string MapViewer::GetMessageForUser()
{
return mMessageForUser.str();
}
/**
* set up the viewer frustrum
*/
void MapViewer::SetupFrustum()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
double zNear = 0.03;
glFrustum(-zNear, zNear, 0.75*zNear,-0.75*zNear,zNear,50);
glScalef(1,1,-1);
return;
}
/**
* Set up the model view.
* @param se3WorldFromCurrent se3 that converts from
* world frame to camera frame
*/
void MapViewer::SetupModelView(SE3<> se3WorldFromCurrent)
{
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glMultMatrix(mse3ViewerFromWorld * se3WorldFromCurrent);
return;
}
/**
* Draw the camera / keyframe
* @param se3CfromW Camera's location in thr world
* @param bSmall Draw the small camera (keyframe)
*/
void MapViewer::DrawCamera(SE3<> se3CfromW, bool bSmall)
{
SetupModelView(se3CfromW.inverse());
SetupFrustum();
if(bSmall)
glLineWidth(1);
else
glLineWidth(3);
glBegin(GL_LINES);
glColor3f(1,0,0);
glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(0.1f, 0.0f, 0.0f);
glColor3f(0,1,0);
glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(0.0f, 0.1f, 0.0f);
glColor3f(1,1,1);
glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(0.0f, 0.0f, 0.1f);
glEnd();
if(!bSmall)
{
glLineWidth(1);
glColor3f(0.5,0.5,0.5);
SetupModelView();
Vector<2> v2CamPosXY = se3CfromW.inverse().get_translation().slice<0,2>();
glBegin(GL_LINES);
glColor3f(1,1,1);
glVertex2d(v2CamPosXY[0] - 0.04, v2CamPosXY[1] + 0.04);
glVertex2d(v2CamPosXY[0] + 0.04, v2CamPosXY[1] - 0.04);
glVertex2d(v2CamPosXY[0] - 0.04, v2CamPosXY[1] - 0.04);
glVertex2d(v2CamPosXY[0] + 0.04, v2CamPosXY[1] + 0.04);
glEnd();
}
}
/**
* Switch to the specified map.
* @param map the map to switch to.
* @param bForce forces the map view to view the specifed map. This is used when deleting a map.
*/
void MapViewer::SwitchMap( Map * map, bool bForce )
{
if( map != NULL && map != mpMap ) {
mpMap = map;
if(!mbBrowseMode || bForce) {
mpViewingMap = mpMap;
}
}
/* If this was in a separate thread then
a switching mechanism such as the one
in MapMaker would be required
*/
}
/**
* Switch to the next map in the list.
*/
void MapViewer::ViewNextMap()
{
vector<Map*>::iterator it = find(mvpMaps.begin(), mvpMaps.end(), mpViewingMap);
if(it == mvpMaps.end()) {
return;
}
mbBrowseMode = true;
it++;
if(it != mvpMaps.end()) {
mpViewingMap = (*it);
}
else {
mpViewingMap = mvpMaps.front();
}
}
/**
* Switch to the previous map in the list.
*/
void MapViewer::ViewPrevMap()
{
vector<Map*>::iterator it = find(mvpMaps.begin(), mvpMaps.end(), mpViewingMap);
if(it == mvpMaps.end()) {
return;
}
mbBrowseMode = true;
int pos = it - mvpMaps.begin();
if(pos > 0)
{
--it;
mpViewingMap = (*it);
}
else {
mpViewingMap = mvpMaps.back();
}
}
/**
* View the current map, and leave browsing mode.
*/
void MapViewer::ViewCurrentMap()
{
mpViewingMap = mpMap;
mbBrowseMode = false;
}
}