Skip to content

Commit b3aa975

Browse files
committed
fix issue with duplicate keys across elements
1 parent e811324 commit b3aa975

File tree

3 files changed

+17
-12
lines changed

3 files changed

+17
-12
lines changed

source/example.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,11 @@ void write_ply_example(const std::string & filename)
6262

6363
verts = {1.f, 2.f, 3.f, 4.f, 5.f, 6.f};
6464
norms = {10.f, 20.f, 30.f, 40.f, 50.f, 60.f};
65-
colors = {100, 200, 100, 200, 100, 200, 100, 200};
65+
colors = {100, 200, 100, 200, 166, 255, 166, 255};
6666

6767
faces = { 0, 5, 3, 2, 1, 0};
6868
uvCoords = { .50f, .55f, .60f, .65f, .70f, .75f, .125f, .225f, .325f, .425f, .525f, .625f};
69-
colors = {105, 205, 105, 205, 150, 250, 150, 250};
69+
faceColors = {105, 205, 105, 205, 150, 250, 150, 250};
7070

7171
std::ofstream outputFile(filename);
7272
std::ostringstream outputStream;

source/tinyply.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ void PlyFile::write_binary_internal(std::ostringstream & os)
139139
{
140140
for (auto & p : e.properties)
141141
{
142-
auto & cursor = userDataTable[p.name];
142+
auto & cursor = userDataTable[make_key(e.name, p.name)];
143143
if (p.isList)
144144
{
145145
uint8_t listSize[4] = {0, 0, 0, 0};
@@ -170,7 +170,7 @@ void PlyFile::write_ascii_internal(std::ostringstream & os)
170170
{
171171
for (auto & p : e.properties)
172172
{
173-
auto & cursor = userDataTable[p.name];
173+
auto & cursor = userDataTable[make_key(e.name, p.name)];
174174
if (p.isList)
175175
{
176176
os << p.listCount << " ";
@@ -238,7 +238,7 @@ void PlyFile::parse_data_binary(std::istream & is, const std::vector<uint8_t> &
238238
bool foundPropetyListSize = false;
239239
for (const auto & property : element.properties)
240240
{
241-
if (const auto & cursor = userDataTable[property.name])
241+
if (const auto & cursor = userDataTable[make_key(element.name, property.name)])
242242
{
243243
int expectedListMultiplier = skip_property(localOffset, property, srcBuffer);
244244
if (expectedListMultiplier)
@@ -253,8 +253,8 @@ void PlyFile::parse_data_binary(std::istream & is, const std::vector<uint8_t> &
253253
if (foundPropetyListSize)
254254
break;
255255
}
256-
}
257-
};
256+
257+
} };
258258

259259
find_property_list_size();
260260

@@ -266,7 +266,7 @@ void PlyFile::parse_data_binary(std::istream & is, const std::vector<uint8_t> &
266266
{
267267
for (auto & property : element.properties)
268268
{
269-
if (auto & cursor = userDataTable[property.name])
269+
if (auto & cursor = userDataTable[make_key(element.name, property.name)])
270270
{
271271
if (property.isList)
272272
{
@@ -305,7 +305,7 @@ void PlyFile::parse_data_ascii(std::istream & is, const std::vector<uint8_t> & b
305305
{
306306
for (auto & property : element.properties)
307307
{
308-
if (auto & cursor = userDataTable[property.name])
308+
if (auto & cursor = userDataTable[make_key(element.name, property.name)])
309309
{
310310
if (property.isList)
311311
{

source/tinyply.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
namespace tinyply
2121
{
22-
22+
2323
static inline uint16_t swap_16(uint16_t value)
2424
{
2525
return (uint16_t)((value >> 8) | (value << 8));
@@ -79,6 +79,11 @@ class PlyProperty
7979
int listCount = 0;
8080
std::string name;
8181
};
82+
83+
inline std::string make_key(const std::string & a, const std::string & b)
84+
{
85+
return (a + "-" + b);
86+
}
8287

8388
template<typename T>
8489
void ply_cast(void * dest, const uint8_t * src)
@@ -326,7 +331,7 @@ class PlyFile
326331
if (int instanceCount = instance_counter(key))
327332
{
328333
instanceCounts.push_back(instanceCount);
329-
auto result = userDataTable.insert(std::pair<std::string, std::shared_ptr<DataCursor>>(key, cursor));
334+
auto result = userDataTable.insert(std::pair<std::string, std::shared_ptr<DataCursor>>(make_key(elementKey, key), cursor));
330335
if (result.second == false)
331336
throw std::runtime_error("property has already been requested: " + key);
332337
}
@@ -363,7 +368,7 @@ class PlyFile
363368
{
364369
PlyProperty::Type t = property_type_for_type(source);
365370
PlyProperty newProp = (listType == PlyProperty::Type::INVALID) ? PlyProperty(t, key) : PlyProperty(listType, t, key, listCount);
366-
userDataTable.insert(std::pair<std::string, std::shared_ptr<DataCursor>>(key, cursor));
371+
userDataTable.insert(std::pair<std::string, std::shared_ptr<DataCursor>>(make_key(e.name, key), cursor));
367372
e.properties.push_back(newProp);
368373
}
369374
};

0 commit comments

Comments
 (0)