Example #1
0
 public static KalkMatrix Diagonal(KalkVector x)
 {
     if (x == null)
     {
         throw new ArgumentNullException(nameof(x));
     }
     return(x.GenericDiagonal());
 }
Example #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));
 }
Example #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;
     }
 }
Example #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));
 }
Example #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));
 }
Example #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));
     }
 }
Example #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);
        }
Example #8
0
 protected abstract KalkVector GenericMultiplyLeft(KalkVector x);
Example #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);
            }
        }
Example #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;
         }
     }
 }
Example #11
0
 protected abstract object GenericDot(KalkVector y);
Example #12
0
 protected abstract KalkVector GenericCross(KalkVector y);
Example #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);
        }
Example #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;
     }
 }
Example #15
0
 protected abstract KalkVector GenericMultiplyRight(KalkVector y);
Example #16
0
 protected KalkColor(KalkVector <byte> values) : base(values)
 {
 }
Example #17
0
 public static object Dot(KalkVector x, KalkVector y)
 {
     AssertValidVectors(x, y);
     return(x.GenericDot(y));
 }