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
105 changes: 105 additions & 0 deletions javascript/MaterialXTest/element.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,108 @@ 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;
expect(differences.message).to.not.be.empty;

result = doc.isEquivalent(doc2, options, undefined);
jstone-lucasfilm marked this conversation as resolved.
Show resolved Hide resolved
expect(result).to.be.false;
});
});
24 changes: 24 additions & 0 deletions source/JsMaterialX/JsMaterialXCore/JsElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,35 @@ 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 = ems::val::undefined())
{
mx::ConstElementPtr rhsPtr = rhs.getSelf();
std::string nativeMessage;
bool handleMessage = !message.isUndefined() && message.typeOf().as<std::string>() == "object";
bool res = self.isEquivalent(rhsPtr, options, handleMessage ? &nativeMessage : nullptr);
if (!res && handleMessage)
{
message.set("message", nativeMessage);
}
return res;
}))
.function("setCategory", &mx::Element::setCategory)
.function("getCategory", &mx::Element::getCategory)
.function("setName", &mx::Element::setName)
Expand Down