Skip to content

Commit

Permalink
Reursive variantSets w.i.p.
Browse files Browse the repository at this point in the history
  • Loading branch information
syoyo committed Oct 8, 2023
1 parent 28be5f1 commit c77ec4a
Showing 1 changed file with 129 additions and 2 deletions.
131 changes: 129 additions & 2 deletions src/usda-reader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,7 @@ class USDAReader::Impl {
for (const auto &variantContext : in_variants) {
const std::string variant_name = variantContext.first;

DCOUT("variantName: " << variant_name);

// Convert VariantContent -> VariantNode
std::map<std::string, VariantNode> variantNodes;
Expand Down Expand Up @@ -415,6 +416,7 @@ class USDAReader::Impl {
variantSets[variant_name] = std::move(variantNodes);
}

DCOUT("variantSets.size = " << variantSets.size());
dst = std::move(variantSets);

return true;
Expand Down Expand Up @@ -1423,6 +1425,129 @@ bool USDAReader::Impl::GetAsLayer(Layer *layer) {

namespace {

//
// TODO: Refeactor ConstructPrimTreeRec and ConstructVariantPrimTreeRec
//
bool ConstructPrimTreeRec(const size_t primIdx,
const std::vector<PrimNode> &prim_nodes,
const bool parent_is_variant,
Prim *destPrim,
std::string *err);

//
// Construct VariantPrim from with botom-up approach
//
bool ConstructVariantPrimTreeRec(const size_t variantPrimIdx,
const std::vector<PrimNode> &prim_nodes,
const std::string &variantName,
const std::map<std::string, VariantNode> &variantNodeMap,
std::map<std::string, VariantSet> &destVariantSets, /* inout */
std::string *err) {

if (variantPrimIdx >= prim_nodes.size()) {
if (err) {
(*err) = "primIndex exceeds prim_nodes.size()\n";
}
return false;
}

const auto &node = prim_nodes[variantPrimIdx];

std::set<int64_t> variantChildrenIndices; // record variantChildren indices

std::map<std::string, VariantSet> variantSets;
VariantSet variantSet;
for (const auto &item : variantNodeMap) {

DCOUT("variant " << item.first);
Variant variant;

// Firstly process nested variants.
for (const auto &childVariantNode: item.second.variantSets) {
DCOUT("variantSet child " << childVariantNode.first);
DCOUT(" variantPrimIdx " << variantPrimIdx);

const std::string childVariantName = childVariantNode.first;
Prim variantChildPrim(value::Value(nullptr)); // dummy
if (!ConstructVariantPrimTreeRec(size_t(variantPrimIdx), prim_nodes, childVariantName, childVariantNode.second, variant.variantSets(), err)) {
return false;
}

#if 0
// extract variant part.
DCOUT("childVariant.size " << variantChildPrim.variantSets().size());
for (auto &childVariantItem : variantChildPrim.variantSets())
{
DCOUT("childVariant " << childVariantItem.first);
std::map<std::string, Variant> childVariant;
for (auto &vitem : childVariantItem.second.variantSet) {
childVariant[vitem.first] = vitem.second;
}
variant.variantSets()[item.first].name = item.first;
variant.variantSets()[item.first].variantSet = childVariant;
}
#endif
}

for (const int64_t vidx : item.second.primChildren) {
if (variantChildrenIndices.count(vidx)) {
// Duplicated variant childrenIndices
if (err) {
(*err) = fmt::format("variant primIdx {} is referenced multiple times.\n", vidx);
}
return false;
} else {
// Add prim to variants
if ((vidx >= 0) && (size_t(vidx) <= prim_nodes.size())) {

Prim variantChildPrim(value::Value(nullptr)); // dummy
if (!ConstructPrimTreeRec(size_t(vidx), prim_nodes, /* parent_is_variant */true, &variantChildPrim, err)) {
return false;
}

variant.primChildren().emplace_back(variantChildPrim);
} else {
if (err) {
(*err) = "primIndex exceeds prim_nodes.size()\n";
}
return false;
}

variantChildrenIndices.insert(vidx);
}
}
variant.metas() = std::move(item.second.metas);
variant.properties() = std::move(item.second.props);

variantSet.name = item.first;
variantSet.variantSet[item.first] = std::move(variant);
}

destVariantSets[variantName] = std::move(variantSet);

for (const auto &cidx : node.children) {
DCOUT("parent: " << variantPrimIdx << ", child: " << cidx);
if (variantChildrenIndices.count(int64_t(cidx))) {
DCOUT("primIdx " << cidx << " processed");
// Prim is processed
continue;
}

Prim childPrim(value::Value(nullptr)); // dummy
if (!ConstructPrimTreeRec(cidx, prim_nodes, /*parent_is_variant*/true, &childPrim, err)) {
return false;
}

//DCOUT("Add childPrim " << childPrim.element_name() << " to Prim " << prim.element_name());
//prim.children().emplace_back(std::move(childPrim));
}

//prim.variantSets() = std::move(variantSets);
//(*destPrim) = std::move(prim);

return true;
}

//
// Construct Prim from PrimNode with botom-up approach
//
Expand Down Expand Up @@ -1477,14 +1602,15 @@ bool ConstructPrimTreeRec(const size_t primIdx,

DCOUT("# of child variantSet " << item.second.variantSets.size());
for (const auto &childVariantNode: item.second.variantSets) {
DCOUT("variantSet child " << childVariantNode.first);
DCOUT("variantSet node " << childVariantNode.first);
DCOUT(" variantPrimIdx " << variantPrimIdx);

Prim variantChildPrim(value::Value(nullptr)); // dummy
if (!ConstructPrimTreeRec(size_t(variantPrimIdx), prim_nodes, /* parent_is_variant */true, &variantChildPrim, err)) {
if (!ConstructVariantPrimTreeRec(size_t(variantPrimIdx), prim_nodes, childVariantNode.first, childVariantNode.second, variant.variantSets(), err)) {
return false;
}

#if 0
// extract variant part.
DCOUT("childVariant.size " << variantChildPrim.variantSets().size());
for (auto &childVariantItem : variantChildPrim.variantSets())
Expand All @@ -1497,6 +1623,7 @@ bool ConstructPrimTreeRec(const size_t primIdx,
variant.variantSets()[item.first].name = item.first;
variant.variantSets()[item.first].variantSet = childVariant;
}
#endif
}

for (const int64_t vidx : item.second.primChildren) {
Expand Down

0 comments on commit c77ec4a

Please sign in to comment.