Exemplo n.º 1
0
        public override object GetValue(ConnectorViewModel connector, object context)
        {
            switch (CastToType)
            {
            //case CastType.SByte:
            //    return (SByte)retVal;
            //case CastType.Int16:
            //    return (Int16)retVal;
            //case CastType.Int32:
            //    return (Int32)retVal;
            //case CastType.Int64:
            //    return (Int64)retVal;
            //case CastType.Byte:
            //    return (Byte)retVal;
            //case CastType.UInt16:
            //    return (UInt16)retVal;
            //case CastType.UInt32:
            //    return (UInt32)retVal;
            //case CastType.UInt64:
            //    return (UInt64)retVal;
            case CastType.Double:
                return(NodeDataNumeric.FromDouble(Value));
                //case CastType.Single:
                //    return (Single)retVal;
                //case CastType.Decimal:
                //    return (Decimal)retVal;

                //case CastType.IConvertable:
                //    return enumeration.Sum(dr => dr);
            }
            return(NodeDataNumeric.MakeZero());
        }
Exemplo n.º 2
0
        public override object GetValue(ConnectorViewModel connector, object context)
        {
            if (conInput.IsConnected)
            {
                Cache  cache;
                double retVal;
                if (context == null || !DataCache.TryGetValue(context, out cache))
                {
                    retVal = conInput.AttachedConnections.Sum(connection =>
                    {
                        object tmp = connection.SourceConnector.ParentNode.GetValue(connection.SourceConnector, context);
                        try { if (typeof(NodeDataNumeric).IsAssignableFrom(tmp.GetType()))
                              {
                                  return(((NodeDataNumeric)tmp).GetDouble());
                              }
                        } catch { }
                        return(0d);
                    });
                    DataCache.Add(context, new Cache()
                    {
                        value = retVal
                    });
                }
                else
                {
                    retVal = cache.value;
                }

                switch (CastToType)
                {
                //case CastType.SByte:
                //    return (SByte)retVal;
                //case CastType.Int16:
                //    return (Int16)retVal;
                //case CastType.Int32:
                //    return (Int32)retVal;
                //case CastType.Int64:
                //    return (Int64)retVal;
                //case CastType.Byte:
                //    return (Byte)retVal;
                //case CastType.UInt16:
                //    return (UInt16)retVal;
                //case CastType.UInt32:
                //    return (UInt32)retVal;
                //case CastType.UInt64:
                //    return (UInt64)retVal;
                case CastType.Double:
                    return(NodeDataNumeric.FromDouble(retVal));
                    //case CastType.Single:
                    //    return (Single)retVal;
                    //case CastType.Decimal:
                    //    return (Decimal)retVal;

                    //case CastType.IConvertable:
                    //    return enumeration.Sum(dr => dr);
                }
                return(NodeDataNumeric.MakeZero());
            }
            return(NodeDataNumeric.MakeZero());
        }
Exemplo n.º 3
0
        // called when something tries to ask this node for a value
        public override object GetValue(ConnectorViewModel connector, object context)
        {
            if (conOutX == connector || conOutY == connector)
            {
                Cache  cache;
                double outX;
                double outY;

                // if the cache context is not set or there is no data cached for that context
                // ask attached nodes for values so we can calculate our output
                if (context == null || !DataCache.TryGetValue(context, out cache))
                {
                    NodeDataNumeric MinNodeVal = conInMin.AttachedConnections.Select(connection =>
                    {
                        try
                        {
                            object tmp = connection.SourceConnector.ParentNode.GetValue(connection.SourceConnector, context);
                            if (typeof(NodeDataNumeric).IsAssignableFrom(tmp.GetType()))
                            {
                                return((NodeDataNumeric)tmp);
                            }
                            return(null);
                        }
                        catch
                        {
                            return(null);
                        }
                    }).Where(val => val != null).FirstOrDefault();

                    NodeDataNumeric MaxNodeVal = conInMax.AttachedConnections.Select(connection =>
                    {
                        try
                        {
                            object tmp = connection.SourceConnector.ParentNode.GetValue(connection.SourceConnector, context);
                            if (typeof(NodeDataNumeric).IsAssignableFrom(tmp.GetType()))
                            {
                                return((NodeDataNumeric)tmp);
                            }
                            return(null);
                        }
                        catch
                        {
                            return(null);
                        }
                    }).Where(val => val != null).FirstOrDefault();

                    NodeDataNumeric XNodeVal = conInX.AttachedConnections.Select(connection =>
                    {
                        try
                        {
                            object tmp = connection.SourceConnector.ParentNode.GetValue(connection.SourceConnector, context);
                            if (typeof(NodeDataNumeric).IsAssignableFrom(tmp.GetType()))
                            {
                                return((NodeDataNumeric)tmp);
                            }
                            return(null);
                        }
                        catch
                        {
                            return(null);
                        }
                    }).Where(val => val != null).FirstOrDefault();

                    NodeDataNumeric YNodeVal = conInY.AttachedConnections.Select(connection =>
                    {
                        try
                        {
                            object tmp = connection.SourceConnector.ParentNode.GetValue(connection.SourceConnector, context);
                            if (typeof(NodeDataNumeric).IsAssignableFrom(tmp.GetType()))
                            {
                                return((NodeDataNumeric)tmp);
                            }
                            return(null);
                        }
                        catch
                        {
                            return(null);
                        }
                    }).Where(val => val != null).FirstOrDefault();

                    double Min = this.Min;
                    double Max = this.Max;
                    double X   = 0;
                    double Y   = 0;

                    try { if (MinNodeVal != null)
                          {
                              Min = MinNodeVal.GetDouble();
                          }
                    } catch { }
                    try { if (MaxNodeVal != null)
                          {
                              Max = MaxNodeVal.GetDouble();
                          }
                    } catch { }
                    try { if (XNodeVal != null)
                          {
                              X = XNodeVal.GetDouble();
                          }
                    } catch { }
                    try { if (YNodeVal != null)
                          {
                              Y = YNodeVal.GetDouble();
                          }
                    } catch { }

                    double MaxMag = Max - Min;

                    double inputMagnitude = System.Math.Sqrt(X * X + Y * Y);

                    if (inputMagnitude < Min)
                    {
                        DataCache.Add(context, new Cache()
                        {
                            X = 0d, Y = 0d
                        });
                        //Console.WriteLine("Storing data for {0,8:X8}", RuntimeHelpers.GetHashCode(context));
                        return(NodeDataNumeric.FromDouble(0d));
                    }
                    if (inputMagnitude > Max)
                    {
                        inputMagnitude = Max;
                    }

                    double newMagnitude = ((inputMagnitude - Min) / MaxMag);

                    outX = System.Math.Min(System.Math.Max(X / inputMagnitude, -1d), 1d) * newMagnitude;
                    outY = System.Math.Min(System.Math.Max(Y / inputMagnitude, -1d), 1d) * newMagnitude;

                    DataCache.Add(context, new Cache()
                    {
                        X = outX, Y = outY
                    });

                    //Console.WriteLine("Storing data for {0,8:X8}", RuntimeHelpers.GetHashCode(context));
                }
                else // we already have the output for this context cached, re-output it
                {
                    outX = cache.X;
                    outY = cache.Y;
                    //Console.WriteLine("Loading data for {0,8:X8}", RuntimeHelpers.GetHashCode(context));
                }

                if (conOutX == connector)
                {
                    return(NodeDataNumeric.FromDouble(outX));
                }
                if (conOutY == connector)
                {
                    return(NodeDataNumeric.FromDouble(outY));
                }
            }
            return(null);
        }