Completed V1 of Variable operators

This commit is contained in:
Michael Smith 2016-03-06 02:30:44 -08:00
parent 5afc0ed517
commit a048f8baa1
3 changed files with 130 additions and 6 deletions

View File

@ -35,5 +35,17 @@ namespace OSCADSharp.UnitTests.Transforms
string script = cyl.ToString();
Assert.IsTrue(script.Contains("rotate([xAngle, yAngle, 120])"));
}
[TestMethod]
public void Rotate_CanMultiplyAVectorVariableInline()
{
var rotation = new Variable("cubeRot", 15);
var cube = new Cube(20, 20, 80, true).Rotate(rotation * 1.5, 30, 30);
string script = cube.ToString();
Assert.IsTrue(script.Contains("rotate([cubeRot * 1.5, 30, 30])"));
}
}
}

View File

@ -45,6 +45,30 @@ namespace OSCADSharp.Scripting
}
#region Operators
private static Variable applyMixedOperatorLeft(string oprtor, Variable left, object right, Func<object, object, object> calcMethod)
{
if (VariableCalculator.IsNumeric(right))
{
return new Variable(String.Format("{0} {1} {2}", left.Name, oprtor, right.ToString()),
calcMethod(left.Value, right));
}
throw new NotSupportedException(String.Format("Cannot use {0} operator on a variable with an object of type {1}",
oprtor, typeof(object).ToString()));
}
private static Variable applyMixedOperatorRight(string oprtor, object left, Variable right, Func<object, object, object> calcMethod)
{
if (VariableCalculator.IsNumeric(left))
{
return new Variable(String.Format("{0} {1} {2}", left.ToString(), oprtor, right.Name),
calcMethod(left, right.Value));
}
throw new NotSupportedException(String.Format("Cannot use {0} operator on a variable with an object of type {1}",
oprtor, typeof(object).ToString()));
}
/// <summary>
/// Adds two variables together
/// </summary>
@ -56,6 +80,28 @@ namespace OSCADSharp.Scripting
return new Variable(String.Format("{0} + {1}", left.Name, right.Name), VariableCalculator.Add(left.Value, right.Value));
}
/// <summary>
/// Adds a value to a variable
/// </summary>
/// <param name="left"></param>
/// <param name="right"></param>
/// <returns></returns>
public static Variable operator +(Variable left, object right)
{
return applyMixedOperatorLeft("+", left, right, VariableCalculator.Add);
}
/// <summary>
/// Adds a value to a variable
/// </summary>
/// <param name="left"></param>
/// <param name="right"></param>
/// <returns></returns>
public static Variable operator +(object left, Variable right)
{
return applyMixedOperatorRight("+", left, right, VariableCalculator.Add);
}
/// <summary>
/// Subtracts two variables
/// </summary>
@ -67,6 +113,28 @@ namespace OSCADSharp.Scripting
return new Variable(String.Format("{0} - {1}", left.Name, right.Name), VariableCalculator.Subtract(left.Value, right.Value));
}
/// <summary>
/// Subtracts a value from a variable
/// </summary>
/// <param name="left"></param>
/// <param name="right"></param>
/// <returns></returns>
public static Variable operator -(Variable left, object right)
{
return applyMixedOperatorLeft("-", left, right, VariableCalculator.Subtract);
}
/// <summary>
/// Subtracts a value from a variable
/// </summary>
/// <param name="left"></param>
/// <param name="right"></param>
/// <returns></returns>
public static Variable operator -(object left, Variable right)
{
return applyMixedOperatorRight("-", left, right, VariableCalculator.Subtract);
}
/// <summary>
/// Multiplies two variables
/// </summary>
@ -78,6 +146,28 @@ namespace OSCADSharp.Scripting
return new Variable(String.Format("{0} * {1}", left.Name, right.Name), VariableCalculator.Multiply(left.Value, right.Value));
}
/// <summary>
/// Multiplies a variable by a value
/// </summary>
/// <param name="left"></param>
/// <param name="right"></param>
/// <returns></returns>
public static Variable operator *(Variable left, object right)
{
return applyMixedOperatorLeft("*", left, right, VariableCalculator.Multiply);
}
/// <summary>
/// Multiplies a variable by a value
/// </summary>
/// <param name="left"></param>
/// <param name="right"></param>
/// <returns></returns>
public static Variable operator *(object left, Variable right)
{
return applyMixedOperatorRight("*", left, right, VariableCalculator.Multiply);
}
/// <summary>
/// Divides two variables
/// </summary>
@ -88,6 +178,28 @@ namespace OSCADSharp.Scripting
{
return new Variable(String.Format("{0} / {1}", left.Name, right.Name), VariableCalculator.Divide(left.Value, right.Value));
}
/// <summary>
/// Divides a variable by a value
/// </summary>
/// <param name="left"></param>
/// <param name="right"></param>
/// <returns></returns>
public static Variable operator /(Variable left, object right)
{
return applyMixedOperatorLeft("/", left, right, VariableCalculator.Divide);
}
/// <summary>
/// Divides a variable by a value
/// </summary>
/// <param name="left"></param>
/// <param name="right"></param>
/// <returns></returns>
public static Variable operator /(object left, Variable right)
{
return applyMixedOperatorRight("/", left, right, VariableCalculator.Divide);
}
#endregion
}
}

View File

@ -10,12 +10,12 @@ namespace OSCADSharp.Scripting
{
internal static class VariableCalculator
{
private static bool isNumeric(object value)
internal static bool IsNumeric(object value)
{
return value is int || value is double || value is float || value is decimal;
}
private static bool isVector(object value)
internal static bool IsVector(object value)
{
return value is Vector3 || value is BindableVector;
}
@ -24,9 +24,9 @@ namespace OSCADSharp.Scripting
{
object result = null;
if (isNumeric(left) && isNumeric(right))
if (IsNumeric(left) && IsNumeric(right))
result = Expression.Lambda<Func<double>>(expr).Compile()();
if (isVector(left) || isVector(right))
if (IsVector(left) || IsVector(right))
result = Expression.Lambda<Func<Vector3>>(expr).Compile()();
return result;
@ -35,9 +35,9 @@ namespace OSCADSharp.Scripting
private static BinaryExpression makeExpression(Func<ConstantExpression, ConstantExpression, BinaryExpression> methodToUse,
object left, object right)
{
if (isNumeric(left))
if (IsNumeric(left))
left = Convert.ToDouble(left);
if (isNumeric(right))
if (IsNumeric(right))
right = Convert.ToDouble(right);
var leftExpr = Expression.Constant(left, left.GetType());