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 tolerance based equals node #10004

Merged
merged 14 commits into from
Sep 30, 2019
Merged
9 changes: 7 additions & 2 deletions src/Engine/ProtoCore/Utils/MathUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,20 @@ public static bool IsLessThanOrEquals(double lhs, double rhs)
return (lhs < rhs) || Equals(lhs, rhs);
}

public static bool Equals(double lhs, double rhs)
public static bool Equals(double lhs, double rhs, double tolerance)
{
if (double.IsPositiveInfinity(lhs) && double.IsPositiveInfinity(rhs))
return true;

if (double.IsNegativeInfinity(lhs) && double.IsNegativeInfinity(rhs))
return true;

return Math.Abs(lhs - rhs) < Tolerance;
return Math.Abs(lhs - rhs) < tolerance;
}

public static bool Equals(double lhs, double rhs)
{
return Equals(lhs, rhs, Tolerance);
}
}
}
1 change: 1 addition & 0 deletions src/Libraries/CoreNodeModels/CoreNodeModels.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Equals.cs" />
<Compile Include="Input\ColorPalette.cs" />
<Compile Include="Input\DateTime.cs" />
<Compile Include="DynamoConvert.cs" />
Expand Down
75 changes: 59 additions & 16 deletions src/Libraries/CoreNodeModels/CoreNodeModelsImages.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1274,22 +1274,21 @@
</data>
<data name="CoreNodeModels.Input.ColorPalette.Large" type="System.Drawing.Bitmap, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>
iVBORw0KGgoAAAANSUhEUgAAAGAAAABgCAYAAADimHc4AAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8
YQUAAAAZdEVYdFNvZnR3YXJlAEFkb2JlIEltYWdlUmVhZHlxyWU8AAADEElEQVR4Xu2XPWp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kAL7q0f
z/qY/IiFEEIIIYQQQgghhBBCCCGEEEIIIYQQQtgHR0f/AS+kcoIgxh26AAAAAElFTkSuQmCC
</value>
</data>
<data name="CoreNodeModels.Input.ColorPalette.Small" type="System.Drawing.Bitmap, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
Expand All @@ -1300,6 +1299,50 @@
ICj70UgJyp63VwRxc8YOInU/QPXl4eaMnTCo3Zyxg9B7nxGU8O2QEZR9n3YIym4tLgRxc8YOInU/QPXl
4eaMnTCo3Zyxg9D62CMo4WHVJyh7UOUEZSezK0HcnLGDSN0PUH15uDljJwxqN2fsILQ5dwlKeHtqEpRd
7lKCsvNyQhA3Z+wgUvcDVF8ebs44juP8IIo+91F0jL9NUtcAAAAASUVORK5CYII=
</value>
</data>
<data name="CoreNodeModels.Equals.Large" type="System.Drawing.Bitmap, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>
iVBORw0KGgoAAAANSUhEUgAAAGAAAABgCAYAAADimHc4AAAABGdBTUEAALGPC/xhBQAABaZJREFUeF7t
nEuIHEUcxtdE8Q0aUUJ838SDB18gnmbGRBOIFxEMQsxJFAJiQNSAkIGoJxGmZ4UgQchBIaABBcHozT0J
RoWgdk/WFYQoLBFiMAkijt9X8++Z7s5kMtPTu109+/3gg5mq6qr6V3e9q3tOCCGEEEIIIYQQQgghhBBC
CCGEY+v7nds2txcbjXb4XL0V7q61w9cp/qYb/RjGgotpqbc799eDzmv1VvRFLQjP1FphdywhLK9x1yIO
i06Mw7b5pY2NVrgXhfjT0MLNI8TFOBm3JSOyPHZg8Q4U1HwtiM4NLcSelhFmAU3OITzdQSPovEXxN93o
58IMvxY3AnEjDaZlyYpdHyxdhcJposk4ny0wuJ12BdsKd07StjMsr3E3CnEk46QsrSbTtkvWJpuDxQdR
GCeyBYQn9Tu473j68G9XW9DcMA7GxTiz6bi03zvxgAVdW2DU8kytFZ3NFMpSY77zbLfbvcyCFQbjZNxM
I51mdJZ5sWCzDwu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c65GMeCg+6xv0OB/3XQHiiCkpyBWBNutaBTQRtie2ibOfsLl2wH
Gc7XEaPw1tvPsUD4TRCbrfugQveekx0wbTNnf9GGjAegrdSWZJloU75kdCzFA1BldTCrTHicD82PjiaW
CQyo/OHcehBOPZkrDXc8PfEaqo6nl4Be0PAAvu4zMMxVa72itNqgauslvTLhxAaG6jXVMtGL2h7Qqwn6
VEHp9DpmfayjVC72uRq4HYMK/VwN4xySztr9XE0MJzooDH2wqWz0yTJP4LKv209IbOpMLcTFOCu3pFw2
3ALkPixGKEdRiJN9thLXuGurso3oOxzFsG3nwSieTkOTow+3CiGEEEIIIYQQQgghhBBCCCHEKObm/gdF
uTYQicmIVQAAAABJRU5ErkJggg==
</value>
</data>
<data name="CoreNodeModels.Equals.Small" type="System.Drawing.Bitmap, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>
iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAABGdBTUEAALGPC/xhBQAAAd1JREFUWEft
Vb1KA0EQTiJ2Igimt7C2s5ZcEMTOiCD+FDa+gCksfQEtLlERwcLkBUyh6CvYRJLmNkUabeJDrN+3OxxG
Et27jQhyH3zc7NzOzO7O7GwuQ4YMSaG1zq9d9heCem8/qKmqIWTq+E+mTR50HtSjjVKoOuAgqEWNUhid
klamTnU5Z+ILObzS0whyH4TRy+pFb+VE64L8ikEd/3EO526dD2bklx/oCLvUcPrmsjNzUqFq0YYLF3U6
GGdm56olKmdwwbT1SgfyWkFe22mc2JNAOlATokoGu3vVZV5FlRimJuAj1SnwWrGyRxWcK2hLH/QlKneU
a709GDdlOATsaBd8Bu/AA7AIroPLMiUG6qDBPiFDd5TD6AjGZzKMgSAF8BhclKBPIG9IA5yTaTFsn1BV
Gbpj3AIIBFoCN8F5UVEXy5+RfgFjUoBA2+AjeAO+i3wLXsuUIaROwbgiRKCiiJRnwR2QqZgSdQyvIoTD
PIw7vteQPuhLVMkA479rRIQ9hegBi3gVlTPYvr1bMcEHxT5Ginf+R2dm53gHaDPpF5En0WZeR3VH6qT1
trlz75fwK2w6VIVFBQ7AJvsESZk6BO/iW/E+9u9A57xW7BNsVoaQqfvVwBky/FPkch8Yc7bTvUnhewAA
AABJRU5ErkJggg==
</value>
</data>
</root>
93 changes: 93 additions & 0 deletions src/Libraries/CoreNodeModels/Equals.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
using System;
using System.Collections.Generic;
using System.Linq;
using CoreNodeModels.Properties;
using Dynamo.Graph.Nodes;
using Newtonsoft.Json;
using ProtoCore.AST.AssociativeAST;
using ProtoCore.DSASM;
using ProtoCore.Utils;

namespace CoreNodeModels
{
[NodeName("==")]
[NodeDescription("EqualsWithToleranceDescription", typeof(Resources))]
[NodeCategory("Core.Math")]
[NodeSearchTags("EqualsWithToleranceSearchTags", typeof(Resources))]
[InPortTypes("double", "double", "double")]
[OutPortTypes("bool")]
[IsDesignScriptCompatible]
public class Equals : NodeModel
{
private static readonly DoubleNode tolerancePortDefaultValue = new DoubleNode(MathUtils.Tolerance);

[JsonConstructor]
private Equals(IEnumerable<PortModel> inPorts, IEnumerable<PortModel> outPorts) : base(inPorts, outPorts)
{
ArgumentLacing = LacingStrategy.Auto;
inPorts.ElementAt(2).DefaultValue = tolerancePortDefaultValue;
}

public Equals()
{
ArgumentLacing = LacingStrategy.Auto;

InPorts.Add(new PortModel(PortType.Input, this, new PortData("x", Resources.EqualsWithToleranceLhsRhsTooltip)));
InPorts.Add(new PortModel(PortType.Input, this, new PortData("y", Resources.EqualsWithToleranceLhsRhsTooltip)));
InPorts.Add(new PortModel(PortType.Input, this,
new PortData("tolerance", string.Format(Resources.EqualsWithToleranceTooltip, MathUtils.Tolerance), tolerancePortDefaultValue)));
OutPorts.Add(new PortModel(PortType.Output, this, new PortData("bool", Resources.EqualsWithToleranceOutportTooltip)));
RegisterAllPorts();
}

public override IEnumerable<AssociativeNode> BuildOutputAst(List<AssociativeNode> inputAstNodes)
aparajit-pratap marked this conversation as resolved.
Show resolved Hide resolved
{
if (!InPorts[0].IsConnected && !InPorts[1].IsConnected && !InPorts[2].IsConnected)
{
return new[] { AstFactory.BuildAssignment(GetAstIdentifierForOutputIndex(0), AstFactory.BuildNullNode()) };
}

AssociativeNode rhs;
if (IsPartiallyApplied)
{
var connectedInputs = Enumerable.Range(0, InPorts.Count)
.Where(index => InPorts[index].IsConnected)
.Select(x => new IntNode(x) as AssociativeNode)
.ToList();
var arguments = AstFactory.BuildExprList(inputAstNodes);


var functionNode = new IdentifierListNode
{
LeftNode = new IdentifierNode("DSCore.Math"),
RightNode = new IdentifierNode("Equals")
};
IntNode paramNumNode = new IntNode(3);
var positionNode = AstFactory.BuildExprList(connectedInputs);

var inputParams = new List<AssociativeNode>
{
functionNode,
paramNumNode,
positionNode,
arguments,
AstFactory.BuildBooleanNode(true)
};

rhs = AstFactory.BuildFunctionCall("__CreateFunctionObject", inputParams);
}
else
{
UseLevelAndReplicationGuide(inputAstNodes);

rhs = AstFactory.BuildFunctionCall(new Func<double, double, double, bool>(DSCore.Math.Equals),
inputAstNodes);
}

return new[]
{
AstFactory.BuildAssignment(GetAstIdentifierForOutputIndex(0), rhs)
};
}
}
}
54 changes: 50 additions & 4 deletions src/Libraries/CoreNodeModels/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions src/Libraries/CoreNodeModels/Properties/Resources.en-US.resx
Original file line number Diff line number Diff line change
Expand Up @@ -590,4 +590,22 @@
<data name="ColorPaletteSearchTags" xml:space="preserve">
<value>color</value>
</data>
<data name="EqualsWithToleranceDescription" xml:space="preserve">
<value>Equals with tolerance input.</value>
<comment>Description for Equals with tolerance node</comment>
</data>
<data name="EqualsWithToleranceLhsRhsTooltip" xml:space="preserve">
<value>Integer or double value</value>
<comment>Equals with tolerance node input port tooltip</comment>
</data>
<data name="EqualsWithToleranceOutportTooltip" xml:space="preserve">
<value>Result of equality check</value>
</data>
<data name="EqualsWithToleranceSearchTags" xml:space="preserve">
<value>equals;tolerance</value>
</data>
<data name="EqualsWithToleranceTooltip" xml:space="preserve">
<value>Tolerance
Default value: {0}</value>
</data>
</root>
18 changes: 18 additions & 0 deletions src/Libraries/CoreNodeModels/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -590,4 +590,22 @@
<data name="ColorPaletteSearchTags" xml:space="preserve">
<value>color</value>
</data>
<data name="EqualsWithToleranceDescription" xml:space="preserve">
<value>Equals with tolerance input.</value>
<comment>Description for Equals with tolerance node</comment>
</data>
<data name="EqualsWithToleranceLhsRhsTooltip" xml:space="preserve">
<value>Integer or double value</value>
<comment>Equals with tolerance node input port tooltip</comment>
</data>
<data name="EqualsWithToleranceOutportTooltip" xml:space="preserve">
<value>Result of equality check</value>
</data>
<data name="EqualsWithToleranceSearchTags" xml:space="preserve">
<value>equals;tolerance</value>
</data>
<data name="EqualsWithToleranceTooltip" xml:space="preserve">
<value>Tolerance
Default value: {0}</value>
</data>
</root>
4 changes: 4 additions & 0 deletions src/Libraries/CoreNodes/CoreNodes.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@
<Name>DynamoCore</Name>
<Private>False</Private>
</ProjectReference>
<ProjectReference Include="..\..\Engine\ProtoCore\ProtoCore.csproj">
<Project>{7a9e0314-966f-4584-baa3-7339cbb849d1}</Project>
<Name>ProtoCore</Name>
</ProjectReference>
<ProjectReference Include="..\..\NodeServices\DynamoServices.csproj">
<Project>{ef879a10-041d-4c68-83e7-3192685f1bae}</Project>
<Name>DynamoServices</Name>
Expand Down
14 changes: 14 additions & 0 deletions src/Libraries/CoreNodes/Math.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Autodesk.DesignScript.Runtime;
using DSCore.Properties;
using NCalc;
using ProtoCore.Utils;
using CSMath = System.Math;

namespace DSCore
Expand Down Expand Up @@ -200,6 +201,19 @@ public static double Abs(double number)
return CSMath.Abs(number);
}

/// <summary>
/// This method is ONLY used by the Equals (with tolerance) NodeModel node
/// </summary>
/// <param name="lhs"></param>
/// <param name="rhs"></param>
/// <param name="tol"></param>
/// <returns></returns>
[IsVisibleInDynamoLibrary(false)]
public static bool Equals(double lhs, double rhs, double tol)
{
return MathUtils.Equals(lhs, rhs, tol);
}

/// <summary>
/// Finds the absolute value of a number.
/// </summary>
Expand Down
3 changes: 3 additions & 0 deletions src/LibraryViewExtension/web/library/layoutSpecs.json
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,9 @@
{
"path": "Operators.=="
},
{
"path": "Core.Math.=="
},
{
"path": "Operators.>="
},
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading