mirror of
https://github.com/eliasstepanik/OSCADSharpDotnet7.git
synced 2026-01-11 21:48:34 +00:00
Nested CompoundVariable and VariableCalculator under Variable.
This commit is contained in:
parent
cf7eaa9000
commit
90fce7428f
@ -1,15 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace OSCADSharp.DataBinding
|
||||
{
|
||||
internal class CompoundVariable : Variable
|
||||
{
|
||||
internal CompoundVariable(string name, object value) : base(name, value, false)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -2,6 +2,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
@ -229,5 +230,80 @@ namespace OSCADSharp.DataBinding
|
||||
return applyMixedOperatorRight("/", left, right, VariableCalculator.Divide);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region CompoundVariable
|
||||
private class CompoundVariable : Variable
|
||||
{
|
||||
internal CompoundVariable(string name, object value) : base(name, value, false)
|
||||
{
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region VariableCalculator
|
||||
private static class VariableCalculator
|
||||
{
|
||||
internal static bool IsNumeric(object value)
|
||||
{
|
||||
return value is int || value is double || value is float || value is decimal;
|
||||
}
|
||||
|
||||
internal static bool IsVector(object value)
|
||||
{
|
||||
return value is Vector3 || value is BindableVector;
|
||||
}
|
||||
|
||||
private static object computeExpression(BinaryExpression expr, object left, object right)
|
||||
{
|
||||
object result = null;
|
||||
|
||||
if (IsNumeric(left) && IsNumeric(right))
|
||||
result = Expression.Lambda<Func<double>>(expr).Compile()();
|
||||
if (IsVector(left) || IsVector(right))
|
||||
result = Expression.Lambda<Func<Vector3>>(expr).Compile()();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private static BinaryExpression makeExpression(Func<ConstantExpression, ConstantExpression, BinaryExpression> methodToUse,
|
||||
object left, object right)
|
||||
{
|
||||
if (IsNumeric(left))
|
||||
left = Convert.ToDouble(left);
|
||||
if (IsNumeric(right))
|
||||
right = Convert.ToDouble(right);
|
||||
|
||||
var leftExpr = Expression.Constant(left, left.GetType());
|
||||
var rightExpr = Expression.Constant(right, right.GetType());
|
||||
BinaryExpression expr = methodToUse(leftExpr, rightExpr);
|
||||
return expr;
|
||||
}
|
||||
|
||||
internal static object Add(object left, object right)
|
||||
{
|
||||
BinaryExpression expr = makeExpression(Expression.Add, left, right);
|
||||
return computeExpression(expr, left, right);
|
||||
}
|
||||
|
||||
internal static object Subtract(object left, object right)
|
||||
{
|
||||
BinaryExpression expr = makeExpression(Expression.Subtract, left, right);
|
||||
return computeExpression(expr, left, right);
|
||||
}
|
||||
|
||||
internal static object Multiply(object left, object right)
|
||||
{
|
||||
BinaryExpression expr = makeExpression(Expression.Multiply, left, right);
|
||||
return computeExpression(expr, left, right);
|
||||
}
|
||||
|
||||
internal static object Divide(object left, object right)
|
||||
{
|
||||
BinaryExpression expr = makeExpression(Expression.Divide, left, right);
|
||||
return computeExpression(expr, left, right);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,73 +0,0 @@
|
||||
using OSCADSharp.Spatial;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace OSCADSharp.DataBinding
|
||||
{
|
||||
internal static class VariableCalculator
|
||||
{
|
||||
internal static bool IsNumeric(object value)
|
||||
{
|
||||
return value is int || value is double || value is float || value is decimal;
|
||||
}
|
||||
|
||||
internal static bool IsVector(object value)
|
||||
{
|
||||
return value is Vector3 || value is BindableVector;
|
||||
}
|
||||
|
||||
private static object computeExpression(BinaryExpression expr, object left, object right)
|
||||
{
|
||||
object result = null;
|
||||
|
||||
if (IsNumeric(left) && IsNumeric(right))
|
||||
result = Expression.Lambda<Func<double>>(expr).Compile()();
|
||||
if (IsVector(left) || IsVector(right))
|
||||
result = Expression.Lambda<Func<Vector3>>(expr).Compile()();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private static BinaryExpression makeExpression(Func<ConstantExpression, ConstantExpression, BinaryExpression> methodToUse,
|
||||
object left, object right)
|
||||
{
|
||||
if (IsNumeric(left))
|
||||
left = Convert.ToDouble(left);
|
||||
if (IsNumeric(right))
|
||||
right = Convert.ToDouble(right);
|
||||
|
||||
var leftExpr = Expression.Constant(left, left.GetType());
|
||||
var rightExpr = Expression.Constant(right, right.GetType());
|
||||
BinaryExpression expr = methodToUse(leftExpr, rightExpr);
|
||||
return expr;
|
||||
}
|
||||
|
||||
internal static object Add(object left, object right)
|
||||
{
|
||||
BinaryExpression expr = makeExpression(Expression.Add, left, right);
|
||||
return computeExpression(expr, left, right);
|
||||
}
|
||||
|
||||
internal static object Subtract(object left, object right)
|
||||
{
|
||||
BinaryExpression expr = makeExpression(Expression.Subtract, left, right);
|
||||
return computeExpression(expr, left, right);
|
||||
}
|
||||
|
||||
internal static object Multiply(object left, object right)
|
||||
{
|
||||
BinaryExpression expr = makeExpression(Expression.Multiply, left, right);
|
||||
return computeExpression(expr, left, right);
|
||||
}
|
||||
|
||||
internal static object Divide(object left, object right)
|
||||
{
|
||||
BinaryExpression expr = makeExpression(Expression.Divide, left, right);
|
||||
return computeExpression(expr, left, right);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -43,8 +43,6 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="DataBinding\BindableBoolean.cs" />
|
||||
<Compile Include="DataBinding\VariableCalculator.cs" />
|
||||
<Compile Include="DataBinding\CompoundVariable.cs" />
|
||||
<Compile Include="OSCADObject.AbstractTransforms.cs" />
|
||||
<Compile Include="OSCADObject.BaseTransform.cs" />
|
||||
<Compile Include="OSCADObject.BasicTransforms.cs" />
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user