-
Notifications
You must be signed in to change notification settings - Fork 264
Expand file tree
/
Copy pathRasterMappedTo3DTile.cpp
More file actions
446 lines (388 loc) · 14.9 KB
/
RasterMappedTo3DTile.cpp
File metadata and controls
446 lines (388 loc) · 14.9 KB
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
#include <Cesium3DTilesSelection/BoundingVolume.h>
#include <Cesium3DTilesSelection/IPrepareRendererResources.h>
#include <Cesium3DTilesSelection/RasterMappedTo3DTile.h>
#include <Cesium3DTilesSelection/Tile.h>
#include <Cesium3DTilesSelection/TileContent.h>
#include <Cesium3DTilesSelection/TilesetExternals.h>
#include <CesiumGeospatial/BoundingRegion.h>
#include <CesiumGeospatial/Ellipsoid.h>
#include <CesiumGeospatial/Projection.h>
#include <CesiumRasterOverlays/ActivatedRasterOverlay.h>
#include <CesiumRasterOverlays/RasterOverlayDetails.h>
#include <CesiumRasterOverlays/RasterOverlayTileProvider.h>
#include <CesiumRasterOverlays/RasterOverlayUtilities.h>
#include <CesiumUtility/Assert.h>
#include <CesiumUtility/IntrusivePointer.h>
#include <CesiumUtility/Tracing.h>
#include <glm/ext/vector_double4.hpp>
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <optional>
#include <vector>
using namespace Cesium3DTilesSelection;
using namespace CesiumGeometry;
using namespace CesiumGeospatial;
using namespace CesiumRasterOverlays;
using namespace CesiumUtility;
namespace {
// Find the given overlay in the given tile.
RasterOverlayTile* findTileOverlay(Tile& tile, const RasterOverlay& overlay) {
std::vector<RasterMappedTo3DTile>& tiles = tile.getMappedRasterTiles();
auto parentTileIt = std::find_if(
tiles.begin(),
tiles.end(),
[&overlay](RasterMappedTo3DTile& raster) noexcept {
RasterOverlayTile* pReady = raster.getReadyTile();
if (pReady == nullptr)
return false;
return &pReady->getTileProvider().getOwner() == &overlay;
});
if (parentTileIt != tiles.end()) {
RasterMappedTo3DTile& mapped = *parentTileIt;
// Prefer the loading tile if there is one.
if (mapped.getLoadingTile()) {
return mapped.getLoadingTile();
} else {
return mapped.getReadyTile();
}
}
return nullptr;
}
} // namespace
namespace Cesium3DTilesSelection {
RasterMappedTo3DTile::RasterMappedTo3DTile(
const CesiumUtility::IntrusivePointer<RasterOverlayTile>& pRasterTile,
int32_t textureCoordinateIndex)
: _pLoadingTile(pRasterTile),
_pReadyTile(nullptr),
_textureCoordinateID(textureCoordinateIndex),
_translation(0.0, 0.0),
_scale(1.0, 1.0),
_state(AttachmentState::Unattached),
_originalFailed(false) {
CESIUM_ASSERT(this->_pLoadingTile != nullptr);
}
RasterOverlayTile::MoreDetailAvailable RasterMappedTo3DTile::update(
IPrepareRendererResources& prepareRendererResources,
Tile& tile) {
CESIUM_ASSERT(this->_pLoadingTile != nullptr || this->_pReadyTile != nullptr);
if (this->getState() == AttachmentState::Attached) {
return !this->_originalFailed && this->_pReadyTile &&
this->_pReadyTile->isMoreDetailAvailable() !=
RasterOverlayTile::MoreDetailAvailable::No
? RasterOverlayTile::MoreDetailAvailable::Yes
: RasterOverlayTile::MoreDetailAvailable::No;
}
auto const isTileUpToDate = [](Tile const& testedTile) {
const TileRenderContent* pRenderContent =
testedTile.getContent().getRenderContent();
return testedTile.getState() == TileLoadState::Done && pRenderContent &&
pRenderContent->getGltfModifierState() == GltfModifierState::Idle;
};
// If the loading tile has failed, try its parent's loading tile.
Tile* pTile = &tile;
while (this->_pLoadingTile &&
this->_pLoadingTile->getState() ==
RasterOverlayTile::LoadState::Failed &&
pTile) {
// Note when our original tile fails to load so that we don't report more
// data available. This means - by design - we won't upsample for a failed
// raster overlay tile. However, each real (non-upsampled) geometry tile
// will have raster overlay images mapped to it, even if the parent geometry
// tile's images already failed to load.
this->_originalFailed = true;
pTile = pTile->getParent();
if (pTile) {
RasterOverlayTile* pOverlayTile = findTileOverlay(
*pTile,
this->_pLoadingTile->getTileProvider().getOwner());
if (pOverlayTile) {
this->_pLoadingTile = pOverlayTile;
}
}
}
// If the loading tile is now ready, make it the ready tile.
if (this->_pLoadingTile &&
this->_pLoadingTile->getState() >= RasterOverlayTile::LoadState::Loaded) {
// Unattach the old tile
if (this->_pReadyTile && this->getState() != AttachmentState::Unattached) {
prepareRendererResources.detachRasterInMainThread(
tile,
this->getTextureCoordinateID(),
*this->_pReadyTile,
this->_pReadyTile->getRendererResources());
this->_state = AttachmentState::Unattached;
}
// Mark the loading tile ready.
this->_pReadyTile = this->_pLoadingTile;
this->_pLoadingTile = nullptr;
// Compute the translation and scale for the new tile.
this->computeTranslationAndScale(tile);
}
// Find the closest ready ancestor tile.
if (this->_pLoadingTile) {
CesiumUtility::IntrusivePointer<RasterOverlayTile> pCandidate;
pTile = tile.getParent();
while (pTile) {
pCandidate = findTileOverlay(
*pTile,
this->_pLoadingTile->getTileProvider().getOwner());
if (pCandidate &&
pCandidate->getState() >= RasterOverlayTile::LoadState::Loaded &&
isTileUpToDate(*pTile)) {
break;
}
pTile = pTile->getParent();
}
if (pCandidate &&
pCandidate->getState() >= RasterOverlayTile::LoadState::Loaded &&
isTileUpToDate(*pTile) &&
this->_pReadyTile != pCandidate) {
if (this->getState() != AttachmentState::Unattached) {
prepareRendererResources.detachRasterInMainThread(
tile,
this->getTextureCoordinateID(),
*this->_pReadyTile,
this->_pReadyTile->getRendererResources());
this->_state = AttachmentState::Unattached;
}
this->_pReadyTile = pCandidate;
// Compute the translation and scale for the new tile.
this->computeTranslationAndScale(tile);
} else if (
pCandidate == nullptr && this->_pReadyTile == nullptr &&
this->_pLoadingTile->getState() ==
RasterOverlayTile::LoadState::Failed) {
// This overlay tile failed to load, and there are no better candidates
// available. So mark this failed tile ready so that it doesn't block the
// entire tileset from rendering.
this->_pReadyTile = this->_pLoadingTile;
this->_pLoadingTile = nullptr;
this->_state = AttachmentState::Attached;
}
}
// Attach the ready tile if it's not already attached.
if (this->_pReadyTile &&
this->getState() == RasterMappedTo3DTile::AttachmentState::Unattached &&
isTileUpToDate(tile)) {
this->_pReadyTile->loadInMainThread();
prepareRendererResources.attachRasterInMainThread(
tile,
this->getTextureCoordinateID(),
*this->_pReadyTile,
this->_pReadyTile->getRendererResources(),
this->getTranslation(),
this->getScale());
this->_state = this->_pLoadingTile ? AttachmentState::TemporarilyAttached
: AttachmentState::Attached;
}
CESIUM_ASSERT(this->_pLoadingTile != nullptr || this->_pReadyTile != nullptr);
// TODO: check more precise raster overlay tile availability, rather than just
// max level?
if (this->_pLoadingTile) {
return RasterOverlayTile::MoreDetailAvailable::Unknown;
}
if (!this->_originalFailed && this->_pReadyTile) {
return this->_pReadyTile->isMoreDetailAvailable();
} else {
return RasterOverlayTile::MoreDetailAvailable::No;
}
}
bool RasterMappedTo3DTile::isMoreDetailAvailable() const noexcept {
return !_pLoadingTile && !_originalFailed && _pReadyTile &&
_pReadyTile->isMoreDetailAvailable() ==
RasterOverlayTile::MoreDetailAvailable::Yes;
}
void RasterMappedTo3DTile::detachFromTile(
IPrepareRendererResources& prepareRendererResources,
Tile& tile) noexcept {
if (this->getState() == AttachmentState::Unattached) {
return;
}
if (!this->_pReadyTile) {
return;
}
// Failed tiles aren't attached with the renderer, so don't detach them,
// either.
if (this->_pReadyTile->getState() != RasterOverlayTile::LoadState::Failed) {
prepareRendererResources.detachRasterInMainThread(
tile,
this->getTextureCoordinateID(),
*this->_pReadyTile,
this->_pReadyTile->getRendererResources());
}
this->_state = AttachmentState::Unattached;
}
bool RasterMappedTo3DTile::loadThrottled() noexcept {
CESIUM_TRACE("RasterMappedTo3DTile::loadThrottled");
RasterOverlayTile* pLoading = this->getLoadingTile();
if (!pLoading) {
return true;
}
ActivatedRasterOverlay& activated = pLoading->getActivatedOverlay();
return activated.loadTileThrottled(*pLoading);
}
namespace {
std::optional<Rectangle> getPreciseRectangleFromBoundingVolume(
const Projection& projection,
const BoundingVolume& boundingVolume) {
const BoundingRegion* pRegion =
getBoundingRegionFromBoundingVolume(boundingVolume);
if (!pRegion) {
return std::nullopt;
}
// Currently _all_ supported projections can have a rectangle precisely
// determined from a bounding region. This may not be true, however, for
// projections we add in the future where X is not purely a function of
// longitude or Y is not purely a function of latitude.
return projectRectangleSimple(projection, pRegion->getRectangle());
}
int32_t addProjectionToList(
std::vector<Projection>& projections,
const Projection& projection) {
auto it = std::find(projections.begin(), projections.end(), projection);
if (it == projections.end()) {
projections.emplace_back(projection);
return int32_t(projections.size()) - 1;
} else {
return int32_t(it - projections.begin());
}
}
RasterMappedTo3DTile* addRealTile(
Tile& tile,
ActivatedRasterOverlay& activatedOverlay,
const Rectangle& rectangle,
const glm::dvec2& screenPixels,
int32_t textureCoordinateIndex) {
IntrusivePointer<RasterOverlayTile> pTile =
activatedOverlay.getTile(rectangle, screenPixels);
if (!pTile) {
return nullptr;
} else {
return &tile.getMappedRasterTiles().emplace_back(
pTile,
textureCoordinateIndex);
}
}
} // namespace
/*static*/ RasterMappedTo3DTile* RasterMappedTo3DTile::mapOverlayToTile(
double maximumScreenSpaceError,
ActivatedRasterOverlay& activatedOverlay,
Tile& tile,
std::vector<Projection>& missingProjections,
const CesiumGeospatial::Ellipsoid& ellipsoid) {
if (activatedOverlay.getTileProvider() == nullptr) {
// Provider not created yet, so add a placeholder tile.
return &tile.getMappedRasterTiles().emplace_back(
activatedOverlay.getPlaceholderTile(),
-1);
}
const Projection& projection =
activatedOverlay.getTileProvider()->getProjection();
// If the tile is loaded, use the precise rectangle computed from the content.
const TileContent& content = tile.getContent();
const TileRenderContent* pRenderContent = content.getRenderContent();
if (pRenderContent) {
const RasterOverlayDetails& overlayDetails =
pRenderContent->getRasterOverlayDetails();
const Rectangle* pRectangle =
overlayDetails.findRectangleForOverlayProjection(projection);
if (pRectangle) {
// We have a rectangle and texture coordinates for this projection.
int32_t index =
int32_t(pRectangle - &overlayDetails.rasterOverlayRectangles[0]);
const glm::dvec2 screenPixels =
RasterOverlayUtilities::computeDesiredScreenPixels(
tile.getNonZeroGeometricError(),
maximumScreenSpaceError,
projection,
*pRectangle,
ellipsoid);
return addRealTile(
tile,
activatedOverlay,
*pRectangle,
screenPixels,
index);
} else {
// We don't have a precise rectangle for this projection, which means the
// tile was loaded before we knew we needed this projection. We'll need to
// reload the tile (later).
int32_t existingIndex =
int32_t(overlayDetails.rasterOverlayProjections.size());
int32_t textureCoordinateIndex =
existingIndex + addProjectionToList(missingProjections, projection);
return &tile.getMappedRasterTiles().emplace_back(
activatedOverlay.getPlaceholderTile(),
textureCoordinateIndex);
}
}
// Maybe we can derive a precise rectangle from the bounding volume.
int32_t textureCoordinateIndex =
addProjectionToList(missingProjections, projection);
std::optional<Rectangle> maybeRectangle =
getPreciseRectangleFromBoundingVolume(
activatedOverlay.getTileProvider()->getProjection(),
tile.getBoundingVolume());
if (maybeRectangle) {
const glm::dvec2 screenPixels =
RasterOverlayUtilities::computeDesiredScreenPixels(
tile.getNonZeroGeometricError(),
maximumScreenSpaceError,
projection,
*maybeRectangle,
ellipsoid);
return addRealTile(
tile,
activatedOverlay,
*maybeRectangle,
screenPixels,
textureCoordinateIndex);
} else {
// No precise rectangle yet, so return a placeholder for now.
return &tile.getMappedRasterTiles().emplace_back(
activatedOverlay.getPlaceholderTile(),
textureCoordinateIndex);
}
}
void RasterMappedTo3DTile::computeTranslationAndScale(const Tile& tile) {
if (!this->_pReadyTile) {
// This shouldn't happen
CESIUM_ASSERT(false);
return;
}
const TileRenderContent* pRenderContent =
tile.getContent().getRenderContent();
if (!pRenderContent) {
return;
}
const RasterOverlayDetails& overlayDetails =
pRenderContent->getRasterOverlayDetails();
const RasterOverlayTileProvider& tileProvider =
this->_pReadyTile->getTileProvider();
const Projection& projection = tileProvider.getProjection();
const std::vector<Projection>& projections =
overlayDetails.rasterOverlayProjections;
const std::vector<Rectangle>& rectangles =
overlayDetails.rasterOverlayRectangles;
auto projectionIt =
std::find(projections.begin(), projections.end(), projection);
if (projectionIt == projections.end()) {
return;
}
int32_t projectionIndex = int32_t(projectionIt - projections.begin());
if (projectionIndex < 0 || size_t(projectionIndex) >= rectangles.size()) {
return;
}
const Rectangle& geometryRectangle = rectangles[size_t(projectionIndex)];
const CesiumGeometry::Rectangle imageryRectangle =
this->_pReadyTile->getRectangle();
glm::dvec4 translationAndScale =
RasterOverlayUtilities::computeTranslationAndScale(
geometryRectangle,
imageryRectangle);
this->_translation = glm::dvec2(translationAndScale.x, translationAndScale.y);
this->_scale = glm::dvec2(translationAndScale.z, translationAndScale.w);
}
} // namespace Cesium3DTilesSelection