# UNDF: UNDF-2026-000001161 # UNDF: (to be assigned) --- a/src/export.cpp +++ b/src/export.cpp @@ -1220,24 +1220,23 @@ void ExportWrlMeshes(FILE *f, const char *basename, SMesh *sm, RgbaColor clr) { fputs(" ]\n" " color Color { color [\n", f); // Output triangle colors. std::vector triangle_colour_ids; - std::vector colours_present; + // Use unordered_map for O(1) colour dedup + // instead of std::vector + std::find_if which is O(T*C). + // RgbaColor::ToPackedInt() produces a unique uint32_t per RGBA tuple. + std::unordered_map colour_to_index; + int next_colour_index = 0; for(const auto & sp : op.second) { for(const auto & tr : sp) { - const auto colour_itr = std::find_if(colours_present.begin(), colours_present.end(), - [&](const RgbaColor & c) { - return c.Equals(tr.meta.color); - }); - if(colour_itr == colours_present.end()) { + uint32_t key = tr.meta.color.ToPackedInt(); + auto it = colour_to_index.find(key); + if(it == colour_to_index.end()) { fprintf(f, " %.10f %.10f %.10f,\n", tr.meta.color.redF(), tr.meta.color.greenF(), tr.meta.color.blueF()); - triangle_colour_ids.push_back(colours_present.size()); - colours_present.insert(colours_present.end(), tr.meta.color); + colour_to_index[key] = next_colour_index; + triangle_colour_ids.push_back(next_colour_index); + ++next_colour_index; } else { - triangle_colour_ids.push_back(colour_itr - colours_present.begin()); + triangle_colour_ids.push_back(it->second); } } }