static string GetOpCodeFormat(MathOperationsMetaData.OpSignature sig) { switch (sig.OpType) { case nameof(MathOperationsMetaData.CustomOps.Negate): return("- {0}"); case nameof(MathOperationsMetaData.CustomOps.Modulo): return("{0} % {1}"); case nameof(MathOperationsMetaData.CustomOps.Add): return("{0} + {1}"); case nameof(MathOperationsMetaData.CustomOps.Subtract): return("{0} - {1}"); case nameof(MathOperationsMetaData.CustomOps.Multiply): return("{0} * {1}"); case nameof(MathOperationsMetaData.CustomOps.Divide): return("{0} / {1}"); case nameof(MathOperationsMetaData.CustomOps.CubicRoot): return("math.pow(math.abs({0}), 1f / 3f)"); } var formatParams = Enumerable.Range(0, sig.Params.Length).Select(i => $"{{{i}}}"); // {0}, {1}, ... return($"math.{sig.OpType.ToLower()}({string.Join(", ", formatParams)})"); // something like "math.dot({0}, {1}) or math.cos({0})" }
protected override void OnDefineNode() { m_MethodName = TypedNode.Type.GetMethodsSignature().OpType; if (m_MethodName != null && !MathOperationsMetaData.MethodsByName.TryGetValue(m_MethodName, out m_CompatibleMethods)) { m_CompatibleMethods = new MathOperationsMetaData.OpSignature[0]; } if (m_CompatibleMethods != null) { m_CurrentMethod = m_CompatibleMethods.SingleOrDefault(o => o.EnumName == TypedNode.Type.ToString()); } if (InputCount == default) { InputCount = m_CurrentMethod.Params?.Length ?? 2; } var mathGenericNode = TypedNode; mathGenericNode.GenerationVersion = MathGeneratedDelegates.GenerationVersion; Node = mathGenericNode; base.OnDefineNode(); }
public override void OnConnection(IPortModel selfConnectedPortModel, IPortModel otherConnectedPortModel) { var connectedInputTypes = this.InputsByDisplayOrder .Select(i => { if (i == selfConnectedPortModel) // not connected yet { return(otherConnectedPortModel?.DataTypeHandle.ToValueTypeOrUnknown() ?? ValueType.Unknown); } if (i.IsConnected) { return(i.ConnectionPortModels.First().DataTypeHandle.ToValueTypeOrUnknown()); } return(ValueType.Unknown); }).ToArray(); var bestCandidate = MathOperationsMetaData.ScoreCompatibleMethodsAccordingToInputParameters(m_CompatibleMethods, connectedInputTypes) .FirstOrDefault(); if (bestCandidate.Score > 0) { m_CurrentMethod = bestCandidate.Signature; var mathGenericNode = TypedNode; mathGenericNode.Type = (MathGeneratedFunction)Enum.Parse(typeof(MathGeneratedFunction), bestCandidate.Signature.EnumName); Node = mathGenericNode; DefineNode(); } base.OnConnection(selfConnectedPortModel, otherConnectedPortModel); }
internal static ulong GenerateStableValueForOp(MathOperationsMetaData.OpSignature method) { if (method.IsCustomOp) { return(GenerateStableValueForCustomOp(method.CustomOp, method.Return, method.Params)); } return(GenerateStableValueForMathOp(method.MathOp, method.Return, method.Params)); }
static string GetOpCodeGen(MathOperationsMetaData.OpSignature sig) { if (!sig.SupportsMultiInputs()) { var indexedValues = sig.Params.Select((p, index) => $"values[{index}].{p}").ToArray(); return($"{{ MathGeneratedFunction.{sig.EnumName}, (Value[] values) => {string.Format(GetOpCodeFormat(sig), indexedValues)} }},"); } var aType = sig.Params[0].ToString(); var bType = sig.Params[1].ToString(); return($"{{ MathGeneratedFunction.{sig.EnumName}, (Value[] values) =>\n" + "\t\t\t{\n" + "\t\t\t\tAssert.IsTrue(values.Length >= 2);\n" + "\t\t\t\tvar result = values[0];\n" + "\t\t\t\tfor (int i = 1; i < values.Length; ++i)\n" + $"\t\t\t\t\tresult = {string.Format(GetOpCodeFormat(sig), $"result.{aType}", $"values[i].{bType}")};\n" + "\t\t\t\treturn result;\n" + "\t\t\t} },"); }