• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeedu API Reference
  • KDE Home
  • Contact Us
 

marble

  • sources
  • kde-4.14
  • kdeedu
  • marble
  • examples
  • cpp
  • tour-preview
tour-preview.cpp
Go to the documentation of this file.
1 //
2 // This file is part of the Marble Virtual Globe.
3 //
4 // This program is free software licensed under the GNU LGPL. You can
5 // find a copy of this license in LICENSE.txt in the top directory of
6 // the source code.
7 //
8 // Copyright 2014 Dennis Nienhüser <earthwings@gentoo.org>
9 //
10 
16 #include <marble/MarbleWidget.h>
17 #include <marble/MarbleMath.h>
18 #include <GeoDataCoordinates.h>
19 #include <GeoDataLineString.h>
20 #include <marble/RenderPlugin.h>
21 #include <marble/MarbleModel.h>
22 #include <marble/routing/RoutingManager.h>
23 #include <marble/routing/RoutingModel.h>
24 #include <marble/TourPlayback.h>
25 #include <marble/geodata/data/GeoDataTour.h>
26 #include <marble/geodata/data/GeoDataPlaylist.h>
27 #include <marble/geodata/data/GeoDataFlyTo.h>
28 #include <marble/geodata/data/GeoDataLookAt.h>
29 
30 #include <opencv2/highgui/highgui.hpp>
31 #include <opencv2/imgproc/imgproc.hpp>
32 
33 #include <QTimeLine>
34 #include <QApplication>
35 #include <QThread>
36 
37 #include <cstdio>
38 
39 using namespace Marble;
40 using namespace cv;
41 
42 class Waiter: private QThread { public: using QThread::msleep; };
43 
44 namespace {
45 // Some stuff you might want to change
46 
47 // The map theme in use
48 QString const mapTheme = "earth/openstreetmap/openstreetmap.dgml";
49 
50 // Enabled plugins. Everything else will be disabled
51 QStringList const features = QStringList() << "stars" << "atmosphere";
52 
53 // Frames per second
54 int const fps = 30;
55 
56 // Target video file name
57 std::string const videoFile = "marble-tour-preview.avi";
58 
59 // Video resolution
60 Size frameSize( 1280, 720 );
61 
62 // Camera velocity in km/h
63 double const velocity = 200.0;
64 }
65 
66 GeoDataTour* createTour( const Route &route )
67 {
68  GeoDataTour* tour = new GeoDataTour;
69  tour->setPlaylist( new GeoDataPlaylist );
70  GeoDataLineString path = route.path();
71  if ( path.size() < 1 ) {
72  return tour;
73  }
74 
75  // Extract tour points at about all 500 meters
76  GeoDataCoordinates last = path.at( 0 );
77  for ( int i=1; i<path.size(); ++i ) {
78  GeoDataCoordinates coordinates = path.at( i );
79  double const distance = EARTH_RADIUS * distanceSphere( last, coordinates );
80  if ( i > 1 && distance < 500 ) {
81  // Ignore waypoints that are quite close
82  continue;
83  }
84  last = coordinates;
85 
86  // Create a point in the tour from the given route point
87  GeoDataLookAt* lookat = new GeoDataLookAt;
88  coordinates.setAltitude( 800 );
89  lookat->setCoordinates( coordinates );
90  lookat->setRange( 800 );
91  GeoDataFlyTo* flyto = new GeoDataFlyTo;
92  double const duration = qBound( 0.2, distance / velocity / 3.6, 10.0 );
93  flyto->setDuration( duration );
94  flyto->setView( lookat );
95  flyto->setFlyToMode( GeoDataFlyTo::Smooth );
96  tour->playlist()->addPrimitive( flyto );
97  }
98 
99  return tour;
100 }
101 
102 void animatedFlight( MarbleWidget *mapWidget, GeoDataTour* tour )
103 {
104  mapWidget->resize( frameSize.width, frameSize.height );
105  TourPlayback* playback = new TourPlayback;
106  playback->setMarbleWidget( mapWidget );
107  playback->setTour( tour );
108  QObject::connect( playback, SIGNAL( centerOn( GeoDataCoordinates ) ),
109  mapWidget, SLOT( centerOn( GeoDataCoordinates ) ) );
110 
111  double const shift = 1.0 / fps;
112  double const duration = playback->duration();
113 
114  VideoWriter videoWriter( videoFile, CV_FOURCC('D','I','V','X'), fps, frameSize );
115  Mat buffer;
116  buffer.create(frameSize, CV_8UC3);
117  for ( double position = 0.0; position <= duration; position += shift ) {
118  printf("[%i%% done]\r", cvRound( (100.0*position)/duration ) );
119  fflush(stdout);
120 
121  playback->seek( position );
122  QImage screenshot = QPixmap::grabWidget( mapWidget ).toImage().convertToFormat( QImage::Format_RGB888 );
123  Mat converter( frameSize, CV_8UC3 );
124  converter.data = screenshot.bits();
125  cvtColor( converter, buffer, CV_RGB2BGR );
126  videoWriter.write( buffer );
127  }
128 
129  for ( int i=0; i<fps; ++i ) {
130  videoWriter.write( buffer ); // one second stand-still at end
131  }
132  printf("Wrote %s\n", videoFile.c_str());
133 }
134 
135 int main(int argc, char** argv)
136 {
137  QApplication app(argc,argv);
138  if (app.arguments().size() < 2) {
139  qDebug() << "Usage: " << app.applicationName() << " /path/to/route.kml";
140  qDebug() << "You can create a suitable route.kml file with Marble.";
141  return 0;
142  }
143 
144  MarbleWidget *mapWidget = new MarbleWidget;
145  mapWidget->setMapThemeId(mapTheme);
146  foreach( RenderPlugin* plugin, mapWidget->renderPlugins() ) {
147  if ( !features.contains( plugin->nameId() ) ) {
148  plugin->setEnabled( false );
149  }
150  }
151 
152  mapWidget->model()->routingManager()->loadRoute(argv[1]);
153  Route const route = mapWidget->model()->routingManager()->routingModel()->route();
154  if ( route.size() == 0 ) {
155  qDebug() << "Failed to open route " << argv[1];
156  return 1;
157  }
158  GeoDataCoordinates start = route.path().at( 0 );
159  start.setLongitude( start.longitude() + 1e-6 );
160  mapWidget->centerOn( start );
161  mapWidget->setDistance( 0.8 );
162  animatedFlight( mapWidget, createTour( route ) );
163  return 0;
164 }
Marble::TourPlayback::seek
void seek(double offset)
Seek to the given timestamp (in seconds)
Definition: TourPlayback.cpp:216
GeoDataCoordinates.h
QImage::convertToFormat
QImage convertToFormat(Format format, QFlags< Qt::ImageConversionFlag > flags) const
Marble::GeoDataCoordinates
A 3d point representation.
Definition: GeoDataCoordinates.h:52
RoutingModel.h
Marble::GeoDataPlaylist::addPrimitive
void addPrimitive(GeoDataTourPrimitive *primitive)
Definition: GeoDataPlaylist.cpp:103
Marble::GeoDataCoordinates::setAltitude
void setAltitude(const qreal altitude)
set the altitude of the Point in meters
Definition: GeoDataCoordinates.cpp:1191
MarbleMath.h
MarbleModel.h
This file contains the headers for MarbleModel.
Marble::GeoDataLineString::size
int size() const
Returns the number of nodes in a LineString.
Definition: GeoDataLineString.cpp:138
Marble::Route::size
int size() const
Definition: Route.cpp:56
QApplication
Marble::distanceSphere
qreal distanceSphere(qreal lon1, qreal lat1, qreal lon2, qreal lat2)
This method calculates the shortest distance between two points on a sphere.
Definition: MarbleMath.h:52
animatedFlight
void animatedFlight(MarbleWidget *mapWidget, GeoDataTour *tour)
Definition: tour-preview.cpp:102
Marble::RoutingModel::route
const Route & route() const
Definition: RoutingModel.cpp:434
Marble::GeoDataTour
Definition: GeoDataTour.h:25
QList::size
int size() const
Marble::MarbleWidget
A widget class that displays a view of the earth.
Definition: MarbleWidget.h:104
Marble::RoutingManager::routingModel
RoutingModel * routingModel()
Provides access to the routing model which contains a list of routing instructions describing steps t...
Definition: RoutingManager.cpp:261
Marble::MarbleWidget::setMapThemeId
void setMapThemeId(const QString &maptheme)
Set a new map theme.
Definition: MarbleWidget.cpp:759
QWidget::resize
void resize(int w, int h)
RoutingManager.h
GeoDataLookAt.h
Marble::EARTH_RADIUS
const qreal EARTH_RADIUS
Definition: MarbleGlobal.h:257
Marble::GeoDataFlyTo::setDuration
void setDuration(double duration)
Definition: GeoDataFlyTo.cpp:130
Marble::MarbleModel::routingManager
RoutingManager * routingManager()
Definition: MarbleModel.cpp:675
Marble::GeoDataLookAt::setRange
void setRange(qreal range)
Change the distance (in meters) between the camera and the object looked at.
Definition: GeoDataLookAt.cpp:114
Marble::GeoDataFlyTo
Definition: GeoDataFlyTo.h:23
Marble::MarbleWidget::model
MarbleModel * model()
Return the model that this view shows.
Definition: MarbleWidget.cpp:289
Marble::RoutingManager::loadRoute
void loadRoute(const QString &filename)
Opens the given filename (kml format) and loads the route contained in it.
Definition: RoutingManager.cpp:337
GeoDataLineString.h
TourPlayback.h
Marble::GeoDataPlaylist
Definition: GeoDataPlaylist.h:22
main
int main(int argc, char **argv)
Definition: tour-preview.cpp:135
Marble::TourPlayback
Definition: TourPlayback.h:28
QString
Marble::GeoDataTour::setPlaylist
void setPlaylist(GeoDataPlaylist *playlist)
Definition: GeoDataTour.cpp:74
Marble::GeoDataFlyTo::Smooth
Definition: GeoDataFlyTo.h:28
Marble::GeoDataLineString
A LineString that allows to store a contiguous set of line segments.
Definition: GeoDataLineString.h:75
QStringList
Marble::GeoDataLineString::at
GeoDataCoordinates & at(int pos)
Returns a reference to the coordinates of a node at a given position. This method detaches the return...
Definition: GeoDataLineString.cpp:143
Marble::GeoDataFlyTo::setView
void setView(GeoDataAbstractView *view)
Definition: GeoDataFlyTo.cpp:120
GeoDataPlaylist.h
Marble::RenderPlugin::nameId
QString nameId
Definition: RenderPlugin.h:48
Marble::GeoDataCoordinates::longitude
qreal longitude(GeoDataCoordinates::Unit unit=GeoDataCoordinates::Radian) const
retrieves the longitude of the GeoDataCoordinates object use the unit parameter to switch between Rad...
Definition: GeoDataCoordinates.cpp:739
Marble::MarbleWidget::centerOn
void centerOn(const qreal lon, const qreal lat, bool animated=false)
Center the view on a geographical point.
Definition: MarbleWidget.cpp:549
Marble::TourPlayback::setMarbleWidget
void setMarbleWidget(MarbleWidget *widget)
Definition: TourPlayback.cpp:110
QImage
Marble::GeoDataLookAt
Definition: GeoDataLookAt.h:23
Marble::GeoDataCoordinates::setLongitude
void setLongitude(qreal lon, GeoDataCoordinates::Unit unit=GeoDataCoordinates::Radian)
set the longitude in a GeoDataCoordinates object
Definition: GeoDataCoordinates.cpp:679
Marble::TourPlayback::setTour
void setTour(const GeoDataTour *tour)
Definition: TourPlayback.cpp:115
Marble::RenderPlugin::setEnabled
void setEnabled(bool enabled)
settting enabled
Definition: RenderPlugin.cpp:139
GeoDataFlyTo.h
RenderPlugin.h
QPixmap::grabWidget
QPixmap grabWidget(QWidget *widget, const QRect &rectangle)
QImage::bits
uchar * bits()
MarbleWidget.h
This file contains the headers for MarbleWidget.
Marble::Route
Definition: Route.h:20
createTour
GeoDataTour * createTour(const Route &route)
Definition: tour-preview.cpp:66
Marble::TourPlayback::duration
double duration() const
Tour duration in seconds.
Definition: TourPlayback.cpp:225
Marble::MarbleWidget::setDistance
void setDistance(qreal distance)
Set the distance of the observer to the globe in km.
Definition: MarbleWidget.cpp:1076
Marble::GeoDataFlyTo::setFlyToMode
void setFlyToMode(const FlyToMode flyToMode)
Definition: GeoDataFlyTo.cpp:140
QPixmap::toImage
QImage toImage() const
Marble::Route::path
const GeoDataLineString & path() const
Definition: Route.cpp:66
QCoreApplication::arguments
QStringList arguments()
QThread
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
Marble::GeoDataTour::playlist
GeoDataPlaylist * playlist()
Definition: GeoDataTour.cpp:64
Marble::RenderPlugin
The abstract class that creates a renderable item.
Definition: RenderPlugin.h:43
Marble::MarbleWidget::renderPlugins
QList< RenderPlugin * > renderPlugins() const
Returns a list of all RenderPlugins on the widget, this includes float items.
Definition: MarbleWidget.cpp:1107
GeoDataTour.h
QCoreApplication::applicationName
applicationName
Marble::GeoDataLookAt::setCoordinates
void setCoordinates(const GeoDataCoordinates &coordinates)
set the GeoDataCoordinates object
Definition: GeoDataLookAt.cpp:66
QThread::msleep
void msleep(unsigned long msecs)
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:13:42 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

marble

Skip menu "marble"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal