Animate 3D symbols

View inC++QMLView on GitHubSample viewer app

Demonstrates how to animate a graphic's position and rotation and follow it using a OrbitGeoElementCameraController. Also shows how to combine a SceneView and MapView in an MVC application with property binding.

screenshot

Use case

Visualize movement through a 3D landscape.

How to use the sample

Animation Controls (Top Left Corner):

  • Select a mission -- selects a location with a route for the plane to fly
  • Play/Pause -- toggles playing and stopping the animation
  • Fixed/Follow -- toggles the camera's free cam mode and follow
  • Mission progress -- shows how far along the route the plane is. Slide to change keyframe in animation

Camera Controls (Top Right Corner):

  • Camare zoom -- distance between camera and plane
  • Camera angle -- viewing angle between camera and plane
  • Flight speed -- controls speed of animation

2D Map Controls (Bottom Left Corner):

  • Plus and Minus -- controls distance of 2D view from ground level

How it works

  1. Create a GraphicsOverlay and add it to the SceneView.
  2. Create a ModelSceneSymbol object.
  3. Create a Graphic object and set its geometry to a Point.
  4. Set the ModelSceneSymbol object to the graphic.
  5. Add heading, pitch, and roll attributes to the graphic. Get the attributes from the graphic with Graphic::attributes.
  6. Create a SimpleRenderer object and set its expression properties.
  7. Add graphic and a renderer to the graphics overlay.
  8. Create a OrbitGeoElementCameraController which is set to target the graphic.
  9. Assign the camera controller to the SceneView.
  10. Update the graphic's location, heading, pitch, and roll.

Relevant API

  • Camera
  • GlobeCameraController
  • Graphic
  • GraphicsOverlay
  • ModelSceneSymbol
  • OrbitGeoElementCameraController
  • Renderer
  • Scene
  • SceneProperties
  • SceneView
  • SurfacePlacement

Offline Data

Read more about how to set up the sample's offline data here.

Link Local Location
Model Marker Symbol Data <userhome>/ArcGIS/Runtime/Data/3D/Bristol/Collada/Bristol.dae
GrandCanyon.csv mission data <userhome>/ArcGIS/Runtime/Data/3D/Missions/GrandCanyon.csv
Hawaii.csv mission data <userhome>/ArcGIS/Runtime/Data/3D/Missions/Hawaii.csv
Pyrenees.csv mission data <userhome>/ArcGIS/Runtime/Data/3D/Missions/Pyrenees.csv
Snowdon.csv mission data <userhome>/ArcGIS/Runtime/Data/3D/Missions/Snowdon.csv

Tags

animation, camera, heading, pitch, roll, rotation, visualize

Sample Code

Animate3DSymbols.cppAnimate3DSymbols.cppAnimate3DSymbols.hLabeledSlider.qmlMissionData.cppMissionData.hAnimate3DSymbols.qml
Use dark colors for code blocksCopy
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
// [WriteFile Name=Animate3DSymbols, Category=Scenes]
// [Legal]
// Copyright 2016 Esri.

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// [Legal]

#ifdef PCH_BUILD
#include "pch.hpp"
#endif // PCH_BUILD

#include "Animate3DSymbols.h"

#include "ArcGISTiledElevationSource.h"
#include "Camera.h"
#include "DistanceCompositeSceneSymbol.h"
#include "GlobeCameraController.h"
#include "GraphicsOverlay.h"
#include "Map.h"
#include "MapQuickView.h"
#include "ModelSceneSymbol.h"
#include "OrbitGeoElementCameraController.h"
#include "PointCollection.h"
#include "Polyline.h"
#include "PolylineBuilder.h"
#include "Scene.h"
#include "SceneQuickView.h"
#include "SceneViewTypes.h"
#include "SimpleMarkerSymbol.h"
#include "SimpleMarkerSceneSymbol.h"
#include "SimpleRenderer.h"
#include "SpatialReference.h"

#include "MissionData.h"

#include <QDir>
#include <QFileInfo>
#include <QStringListModel>
#include <QDir>
#include <QtCore/qglobal.h>

#ifdef Q_OS_IOS
#include <QStandardPaths>
#endif // Q_OS_IOS

using namespace Esri::ArcGISRuntime;

// helper method to get cross platform data path
namespace
{
  QString defaultDataPath()
  {
    QString dataPath;

  #ifdef Q_OS_ANDROID
    dataPath = "/sdcard";
  #elif defined Q_OS_IOS
    dataPath = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
  #else
    dataPath = QDir::homePath();
  #endif

    return dataPath;
  }
} // namespace

using namespace Esri::ArcGISRuntime;

const QString Animate3DSymbols::HEADING = QStringLiteral("heading");
const QString Animate3DSymbols::ROLL = QStringLiteral("roll");
const QString Animate3DSymbols::PITCH = QStringLiteral("pitch");
const QString Animate3DSymbols::ANGLE = QStringLiteral("angle");

Animate3DSymbols::Animate3DSymbols(QQuickItem* parent /* = nullptr */):
  QQuickItem(parent),
  m_dataPath(defaultDataPath() + "/ArcGIS/Runtime/Data/3D"),
  m_missionsModel(new QStringListModel({QStringLiteral("Grand Canyon"),
                                        QStringLiteral("Hawaii"),
                                        QStringLiteral("Pyrenees"),
                                        QStringLiteral("Snowdon")},
                                       this)),
  m_missionData(new MissionData())
{
}

Animate3DSymbols::~Animate3DSymbols() = default;

void Animate3DSymbols::init()
{
  qmlRegisterType<SceneQuickView>("Esri.Samples", 1, 0, "SceneView");
  qmlRegisterType<MapQuickView>("Esri.Samples", 1, 0, "MapView");
  qmlRegisterType<Animate3DSymbols>("Esri.Samples", 1, 0, "Animate3DSymbolsSample");
  qmlRegisterUncreatableType<QAbstractListModel>("Esri.Samples", 1, 0, "AbstractListModel", "AbstractListModel is uncreateable");
}

void Animate3DSymbols::componentComplete()
{
  QQuickItem::componentComplete();

  // find QML SceneView component
  m_sceneView = findChild<SceneQuickView*>("sceneView");
  // create a new scene instance
  Scene* scene = new Scene(BasemapStyle::ArcGISImageryStandard, this);

  // set scene on the scene view
  m_sceneView->setArcGISScene(scene);

  // for use when not in following mode
  m_globeController = new GlobeCameraController(this);

  // create a new elevation source
  ArcGISTiledElevationSource* elevationSource = new ArcGISTiledElevationSource(
    QUrl("https://elevation3d.arcgis.com/arcgis/rest/services/WorldElevation3D/Terrain3D/ImageServer"), this);

  // add the elevation source to the scene to display elevation
  scene->baseSurface()->elevationSources()->append(elevationSource);

  // create a new graphics overlay and add it to the sceneview
  GraphicsOverlay* sceneOverlay = new GraphicsOverlay(this);
  sceneOverlay->setSceneProperties(LayerSceneProperties(SurfacePlacement::Absolute));
  m_sceneView->graphicsOverlays()->append(sceneOverlay);

  SimpleRenderer* renderer3D = new SimpleRenderer(this);
  RendererSceneProperties renderProperties = renderer3D->sceneProperties();
  renderProperties.setHeadingExpression(QString("[%1]").arg(HEADING));
  renderProperties.setPitchExpression(QString("[%1]").arg(PITCH));
  renderProperties.setRollExpression(QString("[%1]").arg(ROLL));
  renderer3D->setSceneProperties(renderProperties);
  sceneOverlay->setRenderer(renderer3D);

  // find QML MapView component
  m_mapView = findChild<MapQuickView*>("mapView");
  m_mapView->setAttributionTextVisible(false);

  // set up mini map
  Map* map = new Map(BasemapStyle::ArcGISImageryStandard, this);
  m_mapView->setMap(map);

  // create a graphics overlay for the mini map
  GraphicsOverlay* mapOverlay = new GraphicsOverlay(this);
  m_mapView->graphicsOverlays()->append(mapOverlay);

  // set up route graphic
  createRoute2d(mapOverlay);

  // set up overlay 2D graphic
  createModel2d(mapOverlay);
}

void Animate3DSymbols::setMissionFrame(int newFrame)
{
  if (!m_missionData||
     newFrame < 0 ||
     m_frame == newFrame)
    return;

  m_frame = newFrame;
  emit missionFrameChanged();
}

void Animate3DSymbols::animate()
{
  if (!m_missionData)
    return;

  if (missionFrame() < missionSize())
  {
    // get the data for this stage in the mission
    const MissionData::DataPoint& dp = m_missionData->dataAt(missionFrame());

    // move 3D graphic to the new position
    m_graphic3d->setGeometry(dp.m_pos);
    // update attribute expressions to immediately update rotation
    m_graphic3d->attributes()->replaceAttribute(HEADING, dp.m_heading);
    m_graphic3d->attributes()->replaceAttribute(PITCH, dp.m_pitch);
    m_graphic3d->attributes()->replaceAttribute(ROLL, dp.m_roll);

    // move 2D graphic to the new position
    m_graphic2d->setGeometry(dp.m_pos);
    m_symbol2d->setAngle(dp.m_heading);
  }

  // increment the frame count
  emit nextFrameRequested();
}

void Animate3DSymbols::changeMission(const QString &missionNameStr)
{
  setMissionFrame(0);

  // read the mission data from the samples .csv files
  QString formattedname = missionNameStr;
  m_missionData->parse(m_dataPath + "/Missions/" + formattedname.remove(" ") + ".csv");

  // if the mission was loaded successfully, move to the start position
  if (missionReady() && m_routeGraphic)
  {
    // create a polyline representing the route for the mission
    PolylineBuilder* routeBldr = new PolylineBuilder(SpatialReference::wgs84(), this);
    for (int i = 0; i < missionSize(); ++i)
    {
      const MissionData::DataPoint& dp = m_missionData->dataAt(i);
      routeBldr->addPoint(dp.m_pos);
    }

    // set the polyline as a graphic on the mapView
    m_routeGraphic->setGeometry(routeBldr->toGeometry());

    m_mapView->setViewpointAndWait(Viewpoint(m_routeGraphic->geometry()));
    createGraphic3D();
  }

  emit missionReadyChanged();
  emit missionSizeChanged();
}

QAbstractListModel *Animate3DSymbols::missionsModel()
{
  return m_missionsModel;
}

void Animate3DSymbols::setZoom(double zoomDist)
{
  if (m_followingController)
  {
    m_followingController->setCameraDistance(zoomDist);
    emit zoomChanged();
  }
}

void Animate3DSymbols::setAngle(double angle)
{
  if (m_followingController)
  {
    m_followingController->setCameraPitchOffset(angle);
    emit angleChanged();
  }
}

void Animate3DSymbols::createModel2d(GraphicsOverlay* mapOverlay)
{
  if (m_symbol2d || m_graphic2d)
    return;

  // get the mission data for the frame
  const MissionData::DataPoint& dp = m_missionData->dataAt(missionFrame());

  // create a blue triangle symbol to represent the plane on the mini map
  m_symbol2d = new SimpleMarkerSymbol(SimpleMarkerSymbolStyle::Triangle, Qt::blue, 10, this);
  m_symbol2d->setAngle(dp.m_heading);

  // create a graphic with the symbol
  m_graphic2d = new Graphic(dp.m_pos, m_symbol2d, this);

  mapOverlay->graphics()->append(m_graphic2d);
}

void Animate3DSymbols::createRoute2d(GraphicsOverlay* mapOverlay)
{
  // Create a 2d graphic of a solid red line to represen the route of the mission
  SimpleLineSymbol* routeSymbol = new SimpleLineSymbol(SimpleLineSymbolStyle::Solid, Qt::red, 1, this);
  m_routeGraphic = new Graphic(this);
  m_routeGraphic->setSymbol(routeSymbol);
  mapOverlay->graphics()->append(m_routeGraphic);
}

void Animate3DSymbols::createGraphic3D()
{
  if (!missionReady())
    return;

  // create the ModelSceneSymbol to be animated in the 3d view
  if (!m_model3d)
    m_model3d = new ModelSceneSymbol(QUrl(m_dataPath + "/Bristol/Collada/Bristol.dae"), 10.0f, this);

  // get the mission data for the frame
  const MissionData::DataPoint& dp = m_missionData->dataAt(missionFrame());

  if (!m_graphic3d)
  {
    // create a graphic using the model symbol
    m_graphic3d = new Graphic(dp.m_pos, m_model3d, this);
    m_graphic3d->attributes()->insertAttribute(HEADING, dp.m_heading);
    m_graphic3d->attributes()->insertAttribute(PITCH, dp.m_pitch);
    m_graphic3d->attributes()->insertAttribute(ROLL, dp.m_roll);

    // add the graphic to the graphics overlay
    m_sceneView->graphicsOverlays()->at(0)->graphics()->append(m_graphic3d);

    // create the camera controller to follow the graphic
    m_followingController = new OrbitGeoElementCameraController(m_graphic3d, 500, this);
    m_sceneView->setCameraController(m_followingController);
  }
  else
  {
    // update existing graphic's geometry and attributes
    m_graphic3d->setGeometry(dp.m_pos);
    m_graphic3d->attributes()->replaceAttribute(HEADING, dp.m_heading);
    m_graphic3d->attributes()->replaceAttribute(PITCH, dp.m_pitch);
    m_graphic3d->attributes()->replaceAttribute(ROLL, dp.m_roll);
  }
}

void Animate3DSymbols::setFollowing(bool following)
{
  if (following)
    m_sceneView->setCameraController(m_followingController);
  else
    m_sceneView->setCameraController(m_globeController);
}

void Animate3DSymbols::zoomMapIn()
{
  if (!m_mapView ||
      !m_routeGraphic)
    return;

  // zoom the map view in, focusing on the position of the 2d graphic for the helicopter
  m_mapView->setViewpoint(Viewpoint((Point)m_routeGraphic->geometry(), m_mapView->mapScale() / m_mapZoomFactor));
}

void Animate3DSymbols::zoomMapOut()
{
  if (!m_mapView ||
      !m_routeGraphic)
    return;

  // zoom the map view out, focusing on the position of the 2d graphic for the helicopter
  m_mapView->setViewpoint(Viewpoint((Point)m_routeGraphic->geometry(), m_mapView->mapScale() * m_mapZoomFactor));
}

void Animate3DSymbols::viewWidthChanged(bool sceneViewIsWider)
{
  if (!m_sceneView || !m_mapView)
  {
    return;
  }

  // only show the attribution text on the view with the widest visible extent
  m_sceneView->setAttributionTextVisible(sceneViewIsWider);
  m_mapView->setAttributionTextVisible(!sceneViewIsWider);
}

bool Animate3DSymbols::missionReady() const
{
  if (!m_missionData)
    return false;

  return m_missionData->ready();
}

int Animate3DSymbols::missionSize() const
{
  if (!m_missionData)
    return 0;

  return (int)m_missionData->size();
}

int Animate3DSymbols::missionFrame() const
{
  return m_frame;
}

double Animate3DSymbols::zoom() const
{
  return m_followingController ? m_followingController->cameraDistance() : 200.0;
}

double Animate3DSymbols::angle() const
{
  return m_followingController ? m_followingController->cameraDistance() : 45.0;
}

double Animate3DSymbols::minZoom() const
{
  return m_followingController ? m_followingController->minCameraDistance() : 0;
}

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.