Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JavaScript bindings for element equivalence #2126

Merged
145 changes: 145 additions & 0 deletions javascript/MaterialXTest/element.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,148 @@ describe('Element', () =>
});
});
});

describe('Equivalence', () =>
{
let mx, doc, doc2, inputMap, inputMap2, floatInputs;

before(async () => {
mx = await Module();

doc = mx.createDocument();
inputMap = new Map([
["color3", " 1.0, +2.0, 3.0 "],
jstone-lucasfilm marked this conversation as resolved.
Show resolved Hide resolved
["color4", "1.0, 2.00, 0.3000, -4"],
["integer", " 12 "],
["matrix33", "01.0, 2.0, 0000.2310, 01.0, 2.0, 0000.2310, 01.0, 2.0, 0000.2310"],
["matrix44", "01.0, 2.0, 0000.2310, 0.100, 01.0, 2.0, 0000.2310, 0.100, 01.0, 2.0, 0000.2310, 0.100, 01.0, 2.0, 0000.2310, 0.100"],
["vector2", "1.0, 0.012345608"],
["vector3", " 1.0, +2.0, 3.0 "],
["vector4", "1.0, 2.00, 0.3000, -4"],
["string", "mystring"],
["boolean", "false"],
["filename", "filename1"],
["float", " 1.2e-10 "],
["float", " 00.1000 "]
]);

let index = 0;
let child = doc.addNodeGraph("mygraph");
let graph = child;
const comment = doc.addChildOfCategory(mx.CommentElement.CATEGORY);
comment.setDocString("Comment 1");

inputMap.forEach((value, key) => {
if (index == 0)
{
let input = graph.addInput(`input_${index}`, key);
if (key === "float") {
input.setAttribute(mx.ValueElement.UI_MIN_ATTRIBUTE, " 0.0100 ");
input.setAttribute(mx.ValueElement.UI_MAX_ATTRIBUTE, " 01.0100 ");
index++;
} else {
input.setName(`input_${key}`);
}
input.setValueString(value, key);
}
});

doc2 = mx.createDocument();
inputMap2 = new Map([
["color3", " 1.0, 2.0, 3.0 "],
["color4", "1, 2, 0.3, -4"],

["integer", "12"],
["matrix33", "1, 2, 0.231, 1, 2, 0.231, 1, 2, 0.231, 1, 2, 0.231"],
["matrix44", "1, 2, 0.231, 0.1, 1, 2, 0.231, 0.1, 1, 2, 0.231, 0.1, 1, 2, 0.231, 0.1"],
["vector2", "1, 0.012345611"],
["string", "mystring"],
["boolean", "false"],
["color3", "1, 2, 3"],
["vector3", "1, 2, 3"],
["vector4", "1, 2, 0.3, -4"],
["filename", "filename1"],
["float", "1.2e-10"],
["float", "0.1"]
]);

index = 0;
let child2 = doc2.addNodeGraph("mygraph");
let graph2 = child2;
floatInputs = [];

inputMap2.forEach((value, key) => {
if (index == 0) {
let input = graph2.addInput(`input_${index}`, key);
input.setValueString(value, key);
if (key === "float") {
input.setAttribute(mx.ValueElement.UI_MIN_ATTRIBUTE, " 0.01");
input.setAttribute(mx.ValueElement.UI_MAX_ATTRIBUTE, " 1.01");
floatInputs.push(input);
index++;
} else {
input.setName(`input_${key}`);
}
}
});

const comment2 = doc2.addChildOfCategory(mx.CommentElement.CATEGORY);
comment2.setDocString("Comment 2");
const comment3 = doc2.addChildOfCategory(mx.CommentElement.CATEGORY);
comment3.setDocString("Comment 3");
});

it('Compare document equivalency', () =>
{
let options = new mx.ElementEquivalenceOptions();

let differences = {};
options.performValueComparisons = false;
let result = doc.isEquivalent(doc2, options, differences);
expect(result).to.be.false;
console.log(differences.message);

options.performValueComparisons = true;
result = doc.isEquivalent(doc2, options, differences);
expect(result).to.be.true;

let currentPrecision = mx.Value.getFloatPrecision();
options.floatPrecision = 8;
result = doc.isEquivalent(doc2, options, differences);
expect(result).to.be.false;
options.floatPrecision = currentPrecision;

options.setAttributeExclusionList([mx.ValueElement.UI_MIN_ATTRIBUTE, mx.ValueElement.UI_MAX_ATTRIBUTE]);
floatInputs.forEach(input => {
input.setAttribute(mx.ValueElement.UI_MIN_ATTRIBUTE, "0.9");
input.setAttribute(mx.ValueElement.UI_MAX_ATTRIBUTE, "100.0");
});
result = doc.isEquivalent(doc2, options, differences);
expect(result).to.be.true;
floatInputs.forEach(input => {
input.setAttribute(mx.ValueElement.UI_MIN_ATTRIBUTE, " 0.01");
input.setAttribute(mx.ValueElement.UI_MAX_ATTRIBUTE, " 1.01");
});

let mismatchElement = doc.getDescendant("mygraph/input_color4");
let previousName = mismatchElement.getName();
mismatchElement.setName("mismatch_color4");
result = doc.isEquivalent(doc2, options, differences);
expect(result).to.be.false;

mismatchElement.setName(previousName);
result = doc.isEquivalent(doc2, options, differences);
expect(result).to.be.true;

let nodeGraph = doc.getNodeGraph("mygraph");
expect(nodeGraph).to.exist;
doc.addNodeDef("ND_mygraph");
nodeGraph.setNodeDefString("ND_mygraph");
let nodeGraph2 = doc2.getNodeGraph("mygraph");
expect(nodeGraph2).to.exist;
doc2.addNodeDef("ND_mygraph");
nodeGraph2.setNodeDefString("ND_mygraph");
result = doc.isEquivalent(doc2, options, differences);
expect(result).to.be.false;
});
});
35 changes: 35 additions & 0 deletions source/JsMaterialX/JsMaterialXCore/JsElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,46 @@ namespace mx = MaterialX;

EMSCRIPTEN_BINDINGS(element)
{
ems::class_<mx::ElementEquivalenceOptions>("ElementEquivalenceOptions")
.constructor<>()
.property("performValueComparisons", &mx::ElementEquivalenceOptions::performValueComparisons)
.property("floatFormat", &mx::ElementEquivalenceOptions::floatFormat)
.property("floatPrecision", &mx::ElementEquivalenceOptions::floatPrecision)
.function("setAttributeExclusionList", ems::optional_override([](mx::ElementEquivalenceOptions& self, const std::vector<std::string>& exclusionList)
{
self.attributeExclusionList = std::set<std::string>(exclusionList.begin(), exclusionList.end());
}));

ems::class_<mx::Element>("Element")
.smart_ptr<std::shared_ptr<mx::Element>>("Element")
.smart_ptr<std::shared_ptr<const mx::Element>>("Element") // mx::ConstElementPtr
.function("equals", ems::optional_override([](mx::Element& self, const mx::Element& rhs) { return self == rhs; }))
.function("notEquals", ems::optional_override([](mx::Element& self, const mx::Element& rhs) { return self != rhs; }))
.function("isEquivalent", ems::optional_override([](mx::Element &self, const mx::Element& rhs, const mx::ElementEquivalenceOptions& options,
ems::val message)
jstone-lucasfilm marked this conversation as resolved.
Show resolved Hide resolved
{
mx::ConstElementPtr rhsPtr = rhs.getSelf();
mx::ElementEquivalenceResultVec differences;
bool res = self.isEquivalent(rhsPtr, options, &differences);
bool handleMessage = message.typeOf().as<std::string>() == "object";
if (handleMessage && !differences.empty())
{
std::string nativeMessage;
for (const auto& difference : differences)
{
nativeMessage += "- Path: " + difference.path1 + " differs from path: " +
difference.path2 + ". Difference Type: " + difference.differenceType +
".";
if (!difference.attributeName.empty())
{
nativeMessage += " Attribute: " + difference.attributeName + ".";
}
nativeMessage += "\n";
}
message.set("message", nativeMessage);
}
return res;
}))
.function("setCategory", &mx::Element::setCategory)
.function("getCategory", &mx::Element::getCategory)
.function("setName", &mx::Element::setName)
Expand Down