File: rendererbase.cpp

package info (click to toggle)
monav 0.3-8
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 8,116 kB
  • ctags: 6,239
  • sloc: cpp: 34,354; ansic: 4,103; java: 1,436; sh: 108; xml: 49; makefile: 44
file content (436 lines) | stat: -rw-r--r-- 15,462 bytes parent folder | download | duplicates (2)
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
/*
Copyright 2010  Christian Vetter veaac.fdirct@gmail.com

This file is part of MoNav.

MoNav is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

MoNav is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with MoNav.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <QDir>
#include <QPainter>
#include <algorithm>
#include <cmath>
#include <QtDebug>

#include "rendererbase.h"
#include "utils/intersection.h"

RendererBase::RendererBase()
{
	m_loaded = false;
	setupPolygons();
	m_tileSize = 256;
	m_settingsDialog = NULL;
	m_advancedSettings = NULL;
	m_magnification = 1;
}

RendererBase::~RendererBase()
{
	if ( m_advancedSettings != NULL )
		delete m_advancedSettings;
	if ( m_settingsDialog != NULL )
		delete m_settingsDialog;
}

int RendererBase::GetMaxZoom()
{
	return m_zoomLevels.size() - 1;
}

void RendererBase::setupPolygons()
{
	double leftPointer  = 135.0 / 180.0 * M_PI;
	double rightPointer = -135.0 / 180.0 * M_PI;
	m_arrow << QPointF( cos( leftPointer ), sin( leftPointer ) );
	m_arrow << QPointF( 1, 0 );
	m_arrow << QPointF( cos( rightPointer ), sin( rightPointer ) );
	m_arrow << QPointF( -3.0 / 8, 0 );
}

void RendererBase::SetInputDirectory( const QString& dir )
{
	m_directory = dir;
}

void RendererBase::ShowSettings()
{
	assert( m_loaded );
	m_settingsDialog->setAdvanced( m_advancedSettings );
	m_settingsDialog->exec();
	if ( !m_settingsDialog->getSettings( &m_settings ) )
		return;
	m_cache.setMaxCost( 1024 * 1024 * m_settings.cacheSize );
	advancedSettingsChanged();
}

void RendererBase::advancedSettingsChanged()
{

}

bool RendererBase::LoadData()
{
	if ( m_loaded )
		UnloadData();

	if ( m_settingsDialog == NULL )
		m_settingsDialog = new BRSettingsDialog();
	if ( !m_settingsDialog->getSettings( &m_settings ) )
		return false;
	m_cache.setMaxCost( 1024 * 1024 * m_settings.cacheSize );

	if ( !load() )
		return false;

	if ( m_zoomLevels.size() == 0 )
		return false;

	m_loaded = true;
	return true;
}
bool RendererBase::UnloadData()
{
	m_cache.clear();
	m_zoomLevels.clear();
	// invoke derived class' callback
	unload();

	return true;
}

ProjectedCoordinate RendererBase::Move( int shiftX, int shiftY, const PaintRequest& request )
{
	if ( !m_loaded )
		return request.center;
	int zoom = m_zoomLevels[request.zoom];
	ProjectedCoordinate center = request.center;
	if ( request.rotation != 0 ) {
		int newX, newY;
		QTransform transform;
		transform.rotate( -request.rotation );
		transform.map( shiftX, shiftY, &newX, &newY );
		shiftX = newX;
		shiftY = newY;
	}
	center.x -= ( double ) shiftX / m_tileSize / ( 1u << zoom ) / ( request.virtualZoom );
	center.y -= ( double ) shiftY / m_tileSize / ( 1u << zoom ) / ( request.virtualZoom );
	return center;
}

ProjectedCoordinate RendererBase::PointToCoordinate( int shiftX, int shiftY, const PaintRequest& request )
{
	return Move( -shiftX, -shiftY, request );
}

void RendererBase::SetUpdateSlot( QObject* obj, const char* slot )
{
	connect( this, SIGNAL(changed()), obj, slot );
}

long long RendererBase::tileID( int x, int y, int zoom )
{
	assert( zoom < 24 );
	long long id = y;
	id = ( id << 24 ) | x;
	id = ( id << 5 ) | zoom;
	return id;
}

bool RendererBase::Paint( QPainter* painter, const PaintRequest& request )
{
	if ( !m_loaded )
		return false;
	if ( request.zoom < 0 || request.zoom >= ( int ) m_zoomLevels.size() )
		return false;
	if ( m_magnification != request.virtualZoom ) {
		m_cache.clear();
		m_magnification = request.virtualZoom;
	}

	int zoom = m_zoomLevels[request.zoom];

	int sizeX = painter->device()->width();
	int sizeY = painter->device()->height();

	if ( sizeX <= 1 && sizeY <= 1 )
		return true;
	double rotation = request.rotation;
	if ( fmod( rotation / 90, 1 ) < 0.01 )
		rotation = 90 * floor( rotation / 90 );
	else if ( fmod( rotation / 90, 1 ) > 0.99 )
		rotation = 90 * ceil( rotation / 90 );

	double tileFactor = 1u << zoom;
	double zoomFactor = tileFactor * m_tileSize;
	painter->translate( sizeX / 2, sizeY / 2 );
	if ( request.virtualZoom > 0 )
		painter->scale( request.virtualZoom, request.virtualZoom );
	painter->rotate( rotation );

	if ( m_settings.filter && fmod( rotation, 90 ) != 0 )
		painter->setRenderHint( QPainter::SmoothPixmapTransform );

	if ( m_settings.hqAntiAliasing )
		painter->setRenderHint( QPainter::HighQualityAntialiasing );

	QTransform transform = painter->worldTransform();
	QTransform inverseTransform = transform.inverted();

	const int xWidth = 1 << zoom;
	const int yWidth = 1 << zoom;

	QRect boundingBox = inverseTransform.mapRect( QRect( 0, 0, sizeX, sizeY ) );

	int minX = floor( ( double ) boundingBox.x() / m_tileSize + request.center.x * tileFactor );
	int maxX = ceil( ( double ) boundingBox.right() / m_tileSize + request.center.x * tileFactor );
	int minY = floor( ( double ) boundingBox.y() / m_tileSize + request.center.y * tileFactor );
	int maxY = ceil( ( double ) boundingBox.bottom() / m_tileSize + request.center.y * tileFactor );

	painter->resetTransform();
	painter->translate( sizeX / 2, sizeY / 2 );
	painter->rotate( rotation );

	int scaledTileSize = m_tileSize * request.virtualZoom;
	int posX = ( minX - request.center.x * tileFactor ) * m_tileSize;
	for ( int x = minX; x < maxX; ++x ) {
		int posY = ( minY - request.center.y * tileFactor ) * m_tileSize;
		for ( int y = minY; y < maxY; ++y ) {

			QPixmap* tile = NULL;
			if ( x >= 0 && x < xWidth && y >= 0 && y < yWidth ) {
				long long id = tileID( x, y, zoom );
				if ( !m_cache.contains( id ) ) {
					if ( !loadTile( x, y, zoom, request.virtualZoom, &tile ) ) {
						tile = new QPixmap( scaledTileSize, scaledTileSize );
						tile->fill( QColor( 241, 238 , 232, 255 ) );
					}

					long long minCacheSize = 2 * scaledTileSize * scaledTileSize * tile->depth() / 8 * ( maxX - minX ) * ( maxY - minY );
					if ( m_cache.maxCost() < minCacheSize ) {
						qDebug() << "had to increase cache size to accommodate all tiles for at least two images: " << minCacheSize / 1024 / 1024 << " MB";
						m_cache.setMaxCost( minCacheSize );
					}

					m_cache.insert( id, tile, scaledTileSize * scaledTileSize * tile->depth() / 8 );
				}
				else {
					tile = m_cache.object( id );
				}
			}

			if ( tile != NULL ) {
				if ( tile->width() != scaledTileSize || tile->height() != scaledTileSize )
					*tile = tile->scaled( scaledTileSize, scaledTileSize, Qt::IgnoreAspectRatio, m_settings.filter ? Qt::SmoothTransformation : Qt::FastTransformation );
				painter->drawPixmap( posX * request.virtualZoom, posY * request.virtualZoom, *tile );
			} else {
				painter->fillRect( posX * request.virtualZoom, posY * request.virtualZoom,  scaledTileSize, scaledTileSize, QColor( 241, 238 , 232, 255 ) );
			}
			posY += m_tileSize;
		}
		posX += m_tileSize;
	}

	painter->setTransform( transform );

	if ( m_settings.antiAliasing )
		painter->setRenderHint( QPainter::Antialiasing );

	if ( request.polygonEndpointsStreet.size() > 0 && request.polygonCoordsStreet.size() > 0 ) {
		int position = 0;
		for ( int i = 0; i < request.polygonEndpointsStreet.size(); i++ ) {
			QVector< ProjectedCoordinate > line;
			for ( ; position < request.polygonEndpointsStreet[i]; position++ ) {
				ProjectedCoordinate pos = request.polygonCoordsStreet[position].ToProjectedCoordinate();
				line.push_back( ProjectedCoordinate( ( pos.x - request.center.x ) * zoomFactor, ( pos.y - request.center.y ) * zoomFactor ) );
			}
			drawPolyline( painter, boundingBox, line, QColor( 255, 255, 0, 128 ) );
		}
	}
	if ( request.polygonEndpointsTracklog.size() > 0 && request.polygonCoordsTracklog.size() > 0 ) {
		int position = 0;
		for ( int i = 0; i < request.polygonEndpointsTracklog.size(); i++ ) {
			QVector< ProjectedCoordinate > line;
			for ( ; position < request.polygonEndpointsTracklog[i]; position++ ) {
				ProjectedCoordinate pos = request.polygonCoordsTracklog[position].ToProjectedCoordinate();
				line.push_back( ProjectedCoordinate( ( pos.x - request.center.x ) * zoomFactor, ( pos.y - request.center.y ) * zoomFactor ) );
			}
			drawPolyline( painter, boundingBox, line, QColor( 178, 034, 034, 128 ) );
		}
	}

	if ( request.route.size() > 0 ) {
		QVector< ProjectedCoordinate > line;
		for ( int i = 0; i < request.route.size(); i++ ){
			ProjectedCoordinate pos = request.route[i].coordinate.ToProjectedCoordinate();
			line.push_back( ProjectedCoordinate( ( pos.x - request.center.x ) * zoomFactor, ( pos.y - request.center.y ) * zoomFactor ) );
		}
		drawPolyline( painter, boundingBox, line, QColor( 0, 0, 128, 128 ) );
	}

	static const int numColors = 6;
	static const QColor outerColors[6] = {
		QColor( 0, 128 * 5 / 5, 128 * 0 / 5 ),
		QColor( 0, 128 * 4 / 5, 128 * 1 / 5 ),
		QColor( 0, 128 * 3 / 5, 128 * 2 / 5 ),
		QColor( 0, 128 * 2 / 5, 128 * 3 / 5 ),
		QColor( 0, 128 * 1 / 5, 128 * 4 / 5 ),
		QColor( 0, 128 * 0 / 5, 128 * 5 / 5 )
	};
	static const QColor innerColors[6] = {
		QColor( 255 * 5 / 5 + 255 * 0 / 5, 255 * 5 / 5, 0 ),
		QColor( 255 * 4 / 5 + 255 * 1 / 5, 255 * 4 / 5, 0 ),
		QColor( 255 * 3 / 5 + 255 * 2 / 5, 255 * 3 / 5, 0 ),
		QColor( 255 * 2 / 5 + 255 * 3 / 5, 255 * 2 / 5, 0 ),
		QColor( 255 * 1 / 5 + 255 * 4 / 5, 255 * 1 / 5, 0 ),
		QColor( 255 * 0 / 5 + 255 * 5 / 5, 255 * 0 / 5, 0 )
	};

	if ( request.POIs.size() > 0 ) {
		for ( int i = 0; i < request.POIs.size(); i++ ) {
			if ( !request.POIs[i].IsValid() )
				continue;
			ProjectedCoordinate pos = request.POIs[i].ToProjectedCoordinate();
			drawIndicator( painter, transform, inverseTransform, ( pos.x - request.center.x ) * zoomFactor, ( pos.y - request.center.y ) * zoomFactor, sizeX, sizeY, request.virtualZoom, QColor( 196, 0, 0 ), QColor( 0, 0, 196 ) );
		}
	}

	if ( request.target.IsValid() )
	{
		ProjectedCoordinate pos = request.target.ToProjectedCoordinate();
		drawIndicator( painter, transform, inverseTransform, ( pos.x - request.center.x ) * zoomFactor, ( pos.y - request.center.y ) * zoomFactor, sizeX, sizeY, request.virtualZoom, outerColors[numColors - 1], innerColors[numColors - 1] );
	}

	bool firstWaypoint = true;
	if ( request.waypoints.size() > 0 ) {
		for ( int i = 0; i < request.waypoints.size(); i++ ) {
			if ( !request.waypoints[i].IsValid() )
				continue;
			ProjectedCoordinate pos = request.waypoints[i].ToProjectedCoordinate();
			int color = i + 1;
			if ( color >= numColors )
				color = numColors - 1;
			if ( firstWaypoint )
				drawIndicator( painter, transform, inverseTransform, ( pos.x - request.center.x ) * zoomFactor, ( pos.y - request.center.y ) * zoomFactor, sizeX, sizeY, request.virtualZoom, outerColors[color], innerColors[color] );
			else
				drawCircle( painter, transform, inverseTransform, ( pos.x - request.center.x ) * zoomFactor, ( pos.y - request.center.y ) * zoomFactor, sizeX, sizeY, request.virtualZoom, outerColors[color], innerColors[color] );
			firstWaypoint = false;
		}
	}

	if ( request.position.IsValid() )
	{
		ProjectedCoordinate pos = request.position.ToProjectedCoordinate();
		drawIndicator( painter, transform, inverseTransform, ( pos.x - request.center.x ) * zoomFactor, ( pos.y - request.center.y ) * zoomFactor, sizeX, sizeY, request.virtualZoom, outerColors[0], innerColors[0] );
		drawArrow( painter, ( pos.x - request.center.x ) * zoomFactor, ( pos.y - request.center.y ) * zoomFactor, request.heading - 90, QColor( 0, 128, 0 ), QColor( 255, 255, 0 ) );
	}

	return true;
}

void RendererBase::drawArrow( QPainter* painter, int x, int y, double rotation, QColor outer, QColor inner )
{
	QMatrix arrowMatrix;
	arrowMatrix.translate( x, y );
	arrowMatrix.rotate( rotation );
	arrowMatrix.scale( 8, 8 );

	painter->setPen( QPen( outer, 5, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin ) );
	painter->setBrush( outer );
	painter->drawPolygon( arrowMatrix.map( m_arrow ) );
	painter->setPen( QPen( inner, 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin ) );
	painter->setBrush( inner );
	painter->drawPolygon( arrowMatrix.map( m_arrow ) );
}

void RendererBase::drawIndicator( QPainter* painter, const QTransform& transform, const QTransform& inverseTransform, int x, int y, int sizeX, int sizeY, int virtualZoom, QColor outer, QColor inner )
{
	QPoint mapped = transform.map( QPoint( x, y ) );
	int margin = 9 * virtualZoom;
	if ( mapped.x() < margin || mapped.y() < margin || mapped.x() >= sizeX - margin || mapped.y() >= sizeY - margin ) {
		//clip an imaginary line from the screen center to pos at the screen boundaries
		ProjectedCoordinate start( mapped.x(), mapped.y() );
		ProjectedCoordinate end( sizeX / 2, sizeY / 2 );
		clipEdge( &start, &end, ProjectedCoordinate( margin, margin ), ProjectedCoordinate( sizeX - margin, sizeY - margin ) );
		QPoint position = inverseTransform.map( QPoint( start.x, start.y ) );
		QPoint center = inverseTransform.map( QPoint( sizeX / 2, sizeY / 2 ) );
		double heading = atan2( ( double ) ( y - center.y() ), ( double ) ( x - center.x() ) ) * 360 / 2 / M_PI;
		drawArrow( painter, position.x(), position.y(), heading, outer, inner );
	}
	else {
		painter->setBrush( Qt::NoBrush );
		painter->setPen( QPen( outer, 5 ) );
		painter->drawEllipse( x - 8, y - 8, 16, 16);
		painter->setPen( QPen( inner, 2 ) );
		painter->drawEllipse( x - 8, y - 8, 16, 16);
	}
}

void RendererBase::drawCircle( QPainter* painter, const QTransform& transform, const QTransform& /*inverseTransform*/, int x, int y, int sizeX, int sizeY, int virtualZoom, QColor outer, QColor inner )
{
	QPoint mapped = transform.map( QPoint( x, y ) );
	int margin = 9 * virtualZoom;
	if ( mapped.x() < margin || mapped.y() < margin || mapped.x() >= sizeX - margin || mapped.y() >= sizeY - margin )
		return;
	painter->setBrush( Qt::NoBrush );
	painter->setPen( QPen( outer, 5 ) );
	painter->drawEllipse( x - 8, y - 8, 16, 16);
	painter->setPen( QPen( inner, 2 ) );
	painter->drawEllipse( x - 8, y - 8, 16, 16);
}

void RendererBase::drawPolyline( QPainter* painter, const QRect& boundingBox, QVector< ProjectedCoordinate > line, QColor color )
{
	painter->setPen( QPen( color, 8, Qt::SolidLine, Qt::FlatCap ) );

	QVector< bool > isInside;

	for ( int i = 0; i < line.size(); i++ ) {
		ProjectedCoordinate pos = line[i];
		QPoint point( pos.x, pos.y );
		isInside.push_back( boundingBox.contains( point ) );
	}

	QVector< bool > draw = isInside;
	for ( int i = 1; i < line.size(); i++ ) {
		if ( isInside[i - 1] )
			draw[i] = true;
		if ( isInside[i] )
			draw[i - 1] = true;
	}

	QVector< QPoint > polygon;
	bool firstPoint = true;
	ProjectedCoordinate lastCoord;
	for ( int i = 0; i < line.size(); i++ ) {
		if ( !draw[i] ) {
			if ( !polygon.empty() ) {
				painter->drawPolyline( polygon.data(), polygon.size() );
				polygon.clear();
			}
			firstPoint = true;
			continue;
		}
		ProjectedCoordinate pos = line[i];
		double xDiff = fabs( pos.x - lastCoord.x );
		double yDiff = fabs( pos.y - lastCoord.y );
		if ( !( firstPoint || i == line.size() - 1 || !draw[i + 1] ) &&  xDiff * xDiff + yDiff * yDiff <= 64 )
			continue;
		QPoint point( pos.x, pos.y );
		polygon.push_back( point );
		lastCoord = pos;
		firstPoint = false;
	}
	painter->drawPolyline( polygon.data(), polygon.size() );
}