-
Notifications
You must be signed in to change notification settings - Fork 305
/
Copy pathCesiumFeatureIdSet.cpp
254 lines (220 loc) · 8.66 KB
/
CesiumFeatureIdSet.cpp
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
// Copyright 2020-2024 CesiumGS, Inc. and Contributors
#include "CesiumFeatureIdSet.h"
#include "CesiumGltf/Accessor.h"
#include "CesiumGltf/ExtensionExtInstanceFeaturesFeatureId.h"
#include "CesiumGltf/ExtensionModelExtStructuralMetadata.h"
#include "CesiumGltf/FeatureId.h"
#include "CesiumGltf/Model.h"
#include "CesiumGltfPrimitiveComponent.h"
static FCesiumFeatureIdAttribute EmptyFeatureIDAttribute;
static FCesiumFeatureIdTexture EmptyFeatureIDTexture;
FCesiumFeatureIdSet::FCesiumFeatureIdSet(
const CesiumGltf::Model& InModel,
const CesiumGltf::MeshPrimitive& Primitive,
const CesiumGltf::FeatureId& FeatureID)
: _featureID(),
_featureIDSetType(ECesiumFeatureIdSetType::None),
_featureCount(FeatureID.featureCount),
_nullFeatureID(FeatureID.nullFeatureId.value_or(-1)),
_propertyTableIndex(FeatureID.propertyTable),
_label(FString(FeatureID.label.value_or("").c_str())) {
FString propertyTableName;
// For backwards compatibility with GetFeatureTableName.
const CesiumGltf::ExtensionModelExtStructuralMetadata* pMetadata =
InModel.getExtension<CesiumGltf::ExtensionModelExtStructuralMetadata>();
if (pMetadata && _propertyTableIndex >= 0) {
size_t index = static_cast<size_t>(_propertyTableIndex);
if (index < pMetadata->propertyTables.size()) {
const CesiumGltf::PropertyTable& propertyTable =
pMetadata->propertyTables[index];
std::string name = propertyTable.name.value_or("");
propertyTableName = FString(name.c_str());
}
}
if (FeatureID.attribute) {
_featureID = FCesiumFeatureIdAttribute(
InModel,
Primitive,
*FeatureID.attribute,
propertyTableName);
_featureIDSetType = ECesiumFeatureIdSetType::Attribute;
return;
}
if (FeatureID.texture) {
_featureID = FCesiumFeatureIdTexture(
InModel,
Primitive,
*FeatureID.texture,
propertyTableName);
_featureIDSetType = ECesiumFeatureIdSetType::Texture;
return;
}
if (_featureCount > 0) {
_featureIDSetType = ECesiumFeatureIdSetType::Implicit;
}
}
FCesiumFeatureIdSet::FCesiumFeatureIdSet(
const CesiumGltf::Model& InModel,
const CesiumGltf::Node& Node,
const CesiumGltf::ExtensionExtInstanceFeaturesFeatureId& InstanceFeatureID)
: _featureID(),
_featureIDSetType(ECesiumFeatureIdSetType::Instance),
_featureCount(InstanceFeatureID.featureCount),
_nullFeatureID(InstanceFeatureID.nullFeatureId.value_or(-1)),
_propertyTableIndex(InstanceFeatureID.propertyTable.value_or(-1)),
_label(FString(InstanceFeatureID.label.value_or("").c_str())) {
FString propertyTableName;
// For backwards compatibility with GetFeatureTableName.
const CesiumGltf::ExtensionModelExtStructuralMetadata* pMetadata =
InModel.getExtension<CesiumGltf::ExtensionModelExtStructuralMetadata>();
if (pMetadata && this->_propertyTableIndex >= 0) {
size_t index = static_cast<size_t>(_propertyTableIndex);
if (index < pMetadata->propertyTables.size()) {
const CesiumGltf::PropertyTable& propertyTable =
pMetadata->propertyTables[index];
std::string name = propertyTable.name.value_or("");
propertyTableName = FString(name.c_str());
}
}
if (InstanceFeatureID.attribute) {
_featureID = FCesiumFeatureIdAttribute(
InModel,
Node,
*InstanceFeatureID.attribute,
propertyTableName);
} else if (_featureCount > 0) {
_featureIDSetType = ECesiumFeatureIdSetType::InstanceImplicit;
}
}
const ECesiumFeatureIdSetType
UCesiumFeatureIdSetBlueprintLibrary::GetFeatureIDSetType(
UPARAM(ref) const FCesiumFeatureIdSet& FeatureIDSet) {
return FeatureIDSet._featureIDSetType;
}
const FCesiumFeatureIdAttribute&
UCesiumFeatureIdSetBlueprintLibrary::GetAsFeatureIDAttribute(
UPARAM(ref) const FCesiumFeatureIdSet& FeatureIDSet) {
if (FeatureIDSet._featureIDSetType == ECesiumFeatureIdSetType::Attribute ||
FeatureIDSet._featureIDSetType == ECesiumFeatureIdSetType::Instance) {
return std::get<FCesiumFeatureIdAttribute>(FeatureIDSet._featureID);
}
return EmptyFeatureIDAttribute;
}
const FCesiumFeatureIdTexture&
UCesiumFeatureIdSetBlueprintLibrary::GetAsFeatureIDTexture(
UPARAM(ref) const FCesiumFeatureIdSet& FeatureIDSet) {
if (FeatureIDSet._featureIDSetType == ECesiumFeatureIdSetType::Texture) {
return std::get<FCesiumFeatureIdTexture>(FeatureIDSet._featureID);
}
return EmptyFeatureIDTexture;
}
const int64 UCesiumFeatureIdSetBlueprintLibrary::GetPropertyTableIndex(
UPARAM(ref) const FCesiumFeatureIdSet& FeatureIDSet) {
return FeatureIDSet._propertyTableIndex;
}
int64 UCesiumFeatureIdSetBlueprintLibrary::GetFeatureCount(
UPARAM(ref) const FCesiumFeatureIdSet& FeatureIDSet) {
return FeatureIDSet._featureCount;
}
const int64 UCesiumFeatureIdSetBlueprintLibrary::GetNullFeatureID(
UPARAM(ref) const FCesiumFeatureIdSet& FeatureIDSet) {
return FeatureIDSet._nullFeatureID;
}
const FString UCesiumFeatureIdSetBlueprintLibrary::GetLabel(
UPARAM(ref) const FCesiumFeatureIdSet& FeatureIDSet) {
return FeatureIDSet._label;
}
int64 UCesiumFeatureIdSetBlueprintLibrary::GetFeatureIDForVertex(
UPARAM(ref) const FCesiumFeatureIdSet& FeatureIDSet,
int64 VertexIndex) {
if (FeatureIDSet._featureIDSetType == ECesiumFeatureIdSetType::Attribute) {
FCesiumFeatureIdAttribute attribute =
std::get<FCesiumFeatureIdAttribute>(FeatureIDSet._featureID);
return UCesiumFeatureIdAttributeBlueprintLibrary::GetFeatureID(
attribute,
VertexIndex);
}
if (FeatureIDSet._featureIDSetType == ECesiumFeatureIdSetType::Texture) {
FCesiumFeatureIdTexture texture =
std::get<FCesiumFeatureIdTexture>(FeatureIDSet._featureID);
return UCesiumFeatureIdTextureBlueprintLibrary::GetFeatureIDForVertex(
texture,
VertexIndex);
}
if (FeatureIDSet._featureIDSetType == ECesiumFeatureIdSetType::Implicit) {
return (VertexIndex >= 0 && VertexIndex < FeatureIDSet._featureCount)
? VertexIndex
: -1;
}
return -1;
}
int64 UCesiumFeatureIdSetBlueprintLibrary::GetFeatureIDForInstance(
UPARAM(ref) const FCesiumFeatureIdSet& FeatureIDSet,
int64 InstanceIndex) {
ECesiumFeatureIdSetType type = FeatureIDSet._featureIDSetType;
if (type == ECesiumFeatureIdSetType::InstanceImplicit) {
return InstanceIndex;
} else if (
type != ECesiumFeatureIdSetType::Instance ||
!std::holds_alternative<FCesiumFeatureIdAttribute>(
FeatureIDSet._featureID) ||
InstanceIndex < 0) {
return -1;
}
const auto& featureIdAttribute =
std::get<FCesiumFeatureIdAttribute>(FeatureIDSet._featureID);
return UCesiumFeatureIdAttributeBlueprintLibrary::GetFeatureID(
featureIdAttribute,
InstanceIndex);
}
int64 UCesiumFeatureIdSetBlueprintLibrary::GetFeatureIDFromHit(
UPARAM(ref) const FCesiumFeatureIdSet& FeatureIDSet,
const FHitResult& Hit) {
// FeatureIDs from instanced geometry take precedence.
const auto* pInstancedComponent =
Cast<UCesiumGltfInstancedComponent>(Hit.Component);
if (IsValid(pInstancedComponent)) {
const FCesiumPrimitiveFeatures& instanceFeatures =
*pInstancedComponent->pInstanceFeatures;
return UCesiumPrimitiveFeaturesBlueprintLibrary::GetFeatureIDFromInstance(
instanceFeatures,
Hit.Item);
}
if (FeatureIDSet._featureIDSetType == ECesiumFeatureIdSetType::Texture) {
FCesiumFeatureIdTexture texture =
std::get<FCesiumFeatureIdTexture>(FeatureIDSet._featureID);
return UCesiumFeatureIdTextureBlueprintLibrary::GetFeatureIDFromHit(
texture,
Hit);
}
// Find the first vertex of the face.
const UCesiumGltfPrimitiveComponent* pGltfComponent =
Cast<UCesiumGltfPrimitiveComponent>(Hit.Component);
if (!IsValid(pGltfComponent)) {
return -1;
}
const CesiumPrimitiveData& primData = pGltfComponent->getPrimitiveData();
if (!primData.pMeshPrimitive) {
return -1;
}
auto VertexIndices = std::visit(
CesiumGltf::IndicesForFaceFromAccessor{
Hit.FaceIndex,
primData.PositionAccessor.size(),
primData.pMeshPrimitive->mode},
primData.IndexAccessor);
int64 VertexIndex = VertexIndices[0];
if (FeatureIDSet._featureIDSetType == ECesiumFeatureIdSetType::Attribute) {
FCesiumFeatureIdAttribute attribute =
std::get<FCesiumFeatureIdAttribute>(FeatureIDSet._featureID);
return UCesiumFeatureIdAttributeBlueprintLibrary::GetFeatureID(
attribute,
VertexIndex);
}
if (FeatureIDSet._featureIDSetType == ECesiumFeatureIdSetType::Implicit) {
return (VertexIndex >= 0 && VertexIndex < FeatureIDSet._featureCount)
? VertexIndex
: -1;
}
return -1;
}