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
6 changes: 3 additions & 3 deletions src/Engine/ProtoCore/Utils/MathUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace ProtoCore.Utils
{
public static class MathUtils
{
public static double Tolerance = 1e-5;
aparajit-pratap marked this conversation as resolved.
Show resolved Hide resolved
public const double Tolerance = 1e-5;

[Obsolete("This method is no longer used. Remove in Dynamo 3.0")]
public static bool IsLessThan(double lhs, double rhs)
Expand All @@ -30,15 +30,15 @@ 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 = Tolerance)
aparajit-pratap marked this conversation as resolved.
Show resolved Hide resolved
{
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;
}
}
}
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
92 changes: 92 additions & 0 deletions src/Libraries/CoreNodeModels/Equals.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Dynamo.Graph.Nodes;
using Newtonsoft.Json;
using ProtoCore.AST.AssociativeAST;
using ProtoCore.DSASM;
using ProtoCore.Utils;

namespace CoreNodeModels
{
[NodeName("==")]
[NodeDescription("Equals with tolerance input")]
aparajit-pratap marked this conversation as resolved.
Show resolved Hide resolved
[NodeCategory("Core.Math")]
[NodeSearchTags("Equals")]
[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("lhs", "integer or double value")));
aparajit-pratap marked this conversation as resolved.
Show resolved Hide resolved
aparajit-pratap marked this conversation as resolved.
Show resolved Hide resolved
InPorts.Add(new PortModel(PortType.Input, this, new PortData("rhs", "integer or double value")));
InPorts.Add(new PortModel(PortType.Input, this,
new PortData("tolerance", "tolerance", tolerancePortDefaultValue)));
OutPorts.Add(new PortModel(PortType.Output, this, new PortData("bool", "result of equality check")));
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)
};
}
}
}
9 changes: 8 additions & 1 deletion src/Libraries/CoreNodeModels/Logic/If.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,15 @@ namespace CoreNodeModels.Logic
public class If : NodeModel
{
[JsonConstructor]
private If(IEnumerable<PortModel> inPorts, IEnumerable<PortModel> outPorts) : base(inPorts, outPorts) { }
private If(IEnumerable<PortModel> inPorts, IEnumerable<PortModel> outPorts) : base(inPorts, outPorts)
aparajit-pratap marked this conversation as resolved.
Show resolved Hide resolved
{
ArgumentLacing = LacingStrategy.Auto;
}

public If()
{
ArgumentLacing = LacingStrategy.Auto;

InPorts.Add(new PortModel(PortType.Input, this, new PortData("test", Resources.PortDataTestBlockToolTip)));
InPorts.Add(new PortModel(PortType.Input, this, new PortData("true", Resources.PortDataTrueBlockToolTip)));
InPorts.Add(new PortModel(PortType.Input, this, new PortData("false", Resources.PortDataFalseBlockToolTip)));
Expand Down Expand Up @@ -58,6 +63,8 @@ public override IEnumerable<AssociativeNode> BuildOutputAst(List<AssociativeNode
}
else
{
UseLevelAndReplicationGuide(inputAstNodes);
aparajit-pratap marked this conversation as resolved.
Show resolved Hide resolved

rhs = new InlineConditionalNode
{
ConditionExpression = inputAstNodes[0],
Expand Down
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
12 changes: 12 additions & 0 deletions src/Libraries/CoreNodes/Math.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Media;
aparajit-pratap marked this conversation as resolved.
Show resolved Hide resolved
using Autodesk.DesignScript.Runtime;
using DSCore.Properties;
using NCalc;
using ProtoCore.Utils;
using CSMath = System.Math;

namespace DSCore
Expand All @@ -14,6 +16,10 @@ namespace DSCore
/// </summary>
public static class Math
{
// This is chosen to be the same as the default tolerance used for VM equality operator.
// See ProtoCore\Utils\MathUtils.cs
private const double tolerance = MathUtils.Tolerance;

/// <summary>
/// Generates a random double in the range of [0, 1).
/// </summary>
Expand Down Expand Up @@ -200,6 +206,12 @@ public static double Abs(double number)
return CSMath.Abs(number);
}

[IsVisibleInDynamoLibrary(false)]
public static bool Equals(double lhs, double rhs, double tol = tolerance)
{
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
7 changes: 7 additions & 0 deletions test/Engine/FFITarget/TestDefaultArgumentAttributes.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.DesignScript.Runtime;
using FFITarget.DesignScript;

Expand All @@ -16,5 +18,10 @@ public static double ComputeCircleConflict([DefaultArgument("Point.ByCoordinates
{
return radius * radius * Math.PI;
}

public static IEnumerable<Point> ComputeFlattenedPoints([DefaultArgument("FFITarget.DesignScript.Point.ByCoordinates((1..10)<1>, (2..20)<2>, 0)")]IEnumerable<IEnumerable<Point>> points)
aparajit-pratap marked this conversation as resolved.
Show resolved Hide resolved
{
return points.SelectMany(x => x);
}
}
}