Exemplo n.º 1
0
 public static KalkMatrix Diagonal(KalkVector x)
 {
     if (x == null)
     {
         throw new ArgumentNullException(nameof(x));
     }
     return(x.GenericDiagonal());
 }
Exemplo n.º 2
0
 public static object Cross(KalkVector x, KalkVector y)
 {
     AssertValidVectors(x, y);
     if (x.Length != 3)
     {
         throw new ArgumentException($"Expecting a vector with 3 elements instead of {x.Length} for cross function.");
     }
     return(x.GenericCross(y));
 }
Exemplo n.º 3
0
 private void AddListItem(TemplateContext context, ref int index, object arg, KalkVector <T> vector)
 {
     if (arg is IList list)
     {
         var count = list.Count;
         for (int j = 0; j < count; j++)
         {
             AddListItem(context, ref index, list[j], vector);
         }
     }
     else
     {
         var value = GetArgumentValue(context, arg);
         vector[index++] = value;
     }
 }
Exemplo n.º 4
0
 public static object Multiply(KalkMatrix x, KalkVector y)
 {
     if (x == null)
     {
         throw new ArgumentNullException(nameof(x));
     }
     if (y == null)
     {
         throw new ArgumentNullException(nameof(y));
     }
     CheckElementType(x, y);
     if (x.ColumnCount != y.Length)
     {
         throw new ArgumentException($"Invalid size between the vector type length {y.Length} and the matrix column count {x.ColumnCount}. They Must be equal.", nameof(x));
     }
     return(x.GenericMultiplyRight(y));
 }
Exemplo n.º 5
0
 public static object Multiply(KalkVector x, KalkMatrix y)
 {
     if (x == null)
     {
         throw new ArgumentNullException(nameof(x));
     }
     if (y == null)
     {
         throw new ArgumentNullException(nameof(y));
     }
     CheckElementType(x, y);
     if (x.Length != y.RowCount)
     {
         throw new ArgumentException($"Invalid size between the vector type length {x.Length} and the matrix row count {y.RowCount}. They Must be equal.", nameof(x));
     }
     return(y.GenericMultiplyLeft(x));
 }
Exemplo n.º 6
0
 private static void AssertValidVectors(KalkVector x, KalkVector y)
 {
     if (x == null)
     {
         throw new ArgumentNullException(nameof(x));
     }
     if (y == null)
     {
         throw new ArgumentNullException(nameof(y));
     }
     if (x.Length != y.Length)
     {
         throw new ArgumentException($"Vectors must have the same length instead of {x.Length} vs {y.Length}", nameof(x));
     }
     if (x.ElementType != y.ElementType)
     {
         throw new ArgumentException($"Vectors must have the same element type instead of {x.ElementType.ScriptPrettyName()} vs {y.ElementType.ScriptPrettyName()}", nameof(x));
     }
 }
Exemplo n.º 7
0
        public bool TryGetValue(TemplateContext context, SourceSpan span, object target, string member, out object value)
        {
            var targetFloat = context.ToObject <float>(span, target);

            if (member.Length == 1)
            {
                value = targetFloat;
                return(true);
            }

            var vector = new KalkVector <float>(member.Length);
            int index  = 0;

            for (int i = 0; i < member.Length; i++)
            {
                var c = member[i];
                switch (c)
                {
                case 'x':
                    vector[index] = targetFloat;
                    break;

                case 'y':
                    vector[index] = targetFloat;
                    break;

                case 'z':
                    vector[index] = targetFloat;
                    break;

                case 'w':
                    vector[index] = targetFloat;
                    break;
                }
                index++;
            }

            value = vector;
            return(true);
        }
Exemplo n.º 8
0
 protected abstract KalkVector GenericMultiplyLeft(KalkVector x);
Exemplo n.º 9
0
        protected override void ProcessSingleArgument(TemplateContext context, ref int index, object arg, KalkVector <byte> vector)
        {
            int value;

            switch (arg)
            {
            case string rgbStr:
                try
                {
                    if (!KalkColorRgb.TryGetKnownColor(rgbStr, out value))
                    {
                        value = int.Parse(rgbStr.TrimStart('#'), System.Globalization.NumberStyles.HexNumber);
                    }
                }
                catch
                {
                    throw new ScriptArgumentException(0, $"Expecting a known color (e.g `AliceBlue`) or an hexadecimal rgb string (e.g #FF80C2) instead of `{rgbStr}`. Type `colors` for listing known colors.");
                }
                break;

            default:
                if (arg is IList)
                {
                    base.ProcessSingleArgument(context, ref index, arg, vector);
                    return;
                }
                value = context.ToObject <int>(context.CurrentSpan, arg);
                break;
            }

            vector[index++] = (byte)((value >> 16) & 0xFF);
            vector[index++] = (byte)((value >> 8) & 0xFF);
            vector[index++] = (byte)(value & 0xFF);

            if (Dimension == 4)
            {
                vector[index++] = (byte)((value >> 24) & 0xFF);
            }
        }
Exemplo n.º 10
0
 protected virtual void ProcessSingleArgument(TemplateContext context, ref int index, object arg, KalkVector <T> vector)
 {
     if (arg is IList list)
     {
         AddListItem(context, ref index, list, vector);
     }
     else
     {
         var value = GetArgumentValue(context, arg);
         for (int j = 0; j < Dimension; j++)
         {
             vector[index++] = value;
         }
     }
 }
Exemplo n.º 11
0
 protected abstract object GenericDot(KalkVector y);
Exemplo n.º 12
0
 protected abstract KalkVector GenericCross(KalkVector y);
Exemplo n.º 13
0
        public KalkVector <T> Invoke(TemplateContext context, object[] arguments)
        {
            if (arguments.Length == 0)
            {
                return(NewVector(Dimension));
            }

            var vector = NewVector(Dimension);
            int index  = 0;

            if (arguments.Length == 1)
            {
                var arg = arguments[0];
                // Replace implicitly Rgb/Rgba to xyzw
                if (arg is KalkColor color)
                {
                    if (this is KalkColorConstructor)
                    {
                        var colorLength = color is KalkColorRgb ? 3 : 4;
                        if (Dimension != colorLength)
                        {
                            if (Dimension == 3) // 4 to 3
                            {
                                arg = new KalkVector <byte>(color.r, color.g, color.b);
                            }
                            else // 3 to 4
                            {
                                Debug.Assert(Dimension == 4);
                                arg = new KalkVector <byte>(color.r, color.g, color.b, 255);
                            }
                        }
                    }
                    else
                    {
                        arg = color.GetFloatVector(Dimension);
                    }
                }
                var argLength = GetArgLength(arg, true);
                var length    = index + argLength;
                if (length != Dimension)
                {
                    throw new ScriptArgumentException(0, $"Invalid number of arguments for {vector.TypeName}. Expecting {Dimension} arguments instead of {length}.");
                }

                ProcessSingleArgument(context, ref index, arg, vector);
            }
            else
            {
                for (var i = 0; i < arguments.Length; i++)
                {
                    var arg       = arguments[i];
                    var argLength = GetArgLength(arg, false);

                    var length = index + argLength;
                    if (length > Dimension)
                    {
                        throw new ScriptArgumentException(i, $"Invalid number of arguments for {vector.TypeName}. Expecting {Dimension} arguments instead of {length}.");
                    }

                    ProcessArgument(context, ref index, arg, vector);
                }
            }

            if (index != Dimension)
            {
                throw new ScriptArgumentException(arguments.Length - 1, $"Invalid number of arguments for {vector.TypeName}. Expecting {Dimension} arguments instead of {index}.");
            }

            return(vector);
        }
Exemplo n.º 14
0
 protected void ProcessArgument(TemplateContext context, ref int index, object arg, KalkVector <T> vector)
 {
     if (arg is IList list)
     {
         AddListItem(context, ref index, list, vector);
     }
     else
     {
         var value = GetArgumentValue(context, arg);
         vector[index++] = value;
     }
 }
Exemplo n.º 15
0
 protected abstract KalkVector GenericMultiplyRight(KalkVector y);
Exemplo n.º 16
0
 protected KalkColor(KalkVector <byte> values) : base(values)
 {
 }
Exemplo n.º 17
0
 public static object Dot(KalkVector x, KalkVector y)
 {
     AssertValidVectors(x, y);
     return(x.GenericDot(y));
 }