From 04c0f19584475a5d0748406df0737336487a9fac Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Wed, 18 May 2022 23:23:50 +0200 Subject: [PATCH] Add `Double2`, `Double3`, `Double4` to C# scripting API --- .../CustomEditors/Editors/Double2Editor.cs | 85 + .../CustomEditors/Editors/Double3Editor.cs | 96 + .../CustomEditors/Editors/Double4Editor.cs | 107 + Source/Engine/Core/Math/Double2.cs | 1773 ++++++++++++++ Source/Engine/Core/Math/Double3.cs | 2060 +++++++++++++++++ Source/Engine/Core/Math/Double4.cs | 1534 ++++++++++++ .../Math/TypeConverters/Double2Converter.cs | 46 + .../Math/TypeConverters/Double3Converter.cs | 46 + .../Math/TypeConverters/Double4Converter.cs | 46 + 9 files changed, 5793 insertions(+) create mode 100644 Source/Editor/CustomEditors/Editors/Double2Editor.cs create mode 100644 Source/Editor/CustomEditors/Editors/Double3Editor.cs create mode 100644 Source/Editor/CustomEditors/Editors/Double4Editor.cs create mode 100644 Source/Engine/Core/Math/Double2.cs create mode 100644 Source/Engine/Core/Math/Double3.cs create mode 100644 Source/Engine/Core/Math/Double4.cs create mode 100644 Source/Engine/Core/Math/TypeConverters/Double2Converter.cs create mode 100644 Source/Engine/Core/Math/TypeConverters/Double3Converter.cs create mode 100644 Source/Engine/Core/Math/TypeConverters/Double4Converter.cs diff --git a/Source/Editor/CustomEditors/Editors/Double2Editor.cs b/Source/Editor/CustomEditors/Editors/Double2Editor.cs new file mode 100644 index 000000000..6d3a69f0e --- /dev/null +++ b/Source/Editor/CustomEditors/Editors/Double2Editor.cs @@ -0,0 +1,85 @@ +// Copyright (c) 2012-2022 Wojciech Figat. All rights reserved. + +using System.Linq; +using FlaxEditor.CustomEditors.Elements; +using FlaxEngine; +using FlaxEngine.GUI; + +namespace FlaxEditor.CustomEditors.Editors +{ + /// + /// Default implementation of the inspector used to edit Double2 value type properties. + /// + [CustomEditor(typeof(Double2)), DefaultEditor] + public class Double2Editor : CustomEditor + { + /// + /// The X component editor. + /// + protected DoubleValueElement XElement; + + /// + /// The Y component editor. + /// + protected DoubleValueElement YElement; + + /// + public override DisplayStyle Style => DisplayStyle.Inline; + + /// + public override void Initialize(LayoutElementsContainer layout) + { + var grid = layout.CustomContainer(); + var gridControl = grid.CustomControl; + gridControl.ClipChildren = false; + gridControl.Height = TextBox.DefaultHeight; + gridControl.SlotsHorizontally = 2; + gridControl.SlotsVertically = 1; + + LimitAttribute limit = null; + var attributes = Values.GetAttributes(); + if (attributes != null) + { + limit = (LimitAttribute)attributes.FirstOrDefault(x => x is LimitAttribute); + } + + XElement = grid.DoubleValue(); + XElement.SetLimits(limit); + XElement.DoubleValue.ValueChanged += OnValueChanged; + XElement.DoubleValue.SlidingEnd += ClearToken; + + YElement = grid.DoubleValue(); + YElement.SetLimits(limit); + YElement.DoubleValue.ValueChanged += OnValueChanged; + YElement.DoubleValue.SlidingEnd += ClearToken; + } + + private void OnValueChanged() + { + if (IsSetBlocked) + return; + + var isSliding = XElement.IsSliding || YElement.IsSliding; + var token = isSliding ? this : null; + var value = new Double2(XElement.DoubleValue.Value, YElement.DoubleValue.Value); + SetValue(value, token); + } + + /// + public override void Refresh() + { + base.Refresh(); + + if (HasDifferentValues) + { + // TODO: support different values for ValueBox + } + else + { + var value = (Double2)Values[0]; + XElement.DoubleValue.Value = value.X; + YElement.DoubleValue.Value = value.Y; + } + } + } +} diff --git a/Source/Editor/CustomEditors/Editors/Double3Editor.cs b/Source/Editor/CustomEditors/Editors/Double3Editor.cs new file mode 100644 index 000000000..601206f7d --- /dev/null +++ b/Source/Editor/CustomEditors/Editors/Double3Editor.cs @@ -0,0 +1,96 @@ +// Copyright (c) 2012-2022 Wojciech Figat. All rights reserved. + +using System.Linq; +using FlaxEditor.CustomEditors.Elements; +using FlaxEngine; +using FlaxEngine.GUI; + +namespace FlaxEditor.CustomEditors.Editors +{ + /// + /// Default implementation of the inspector used to edit Double3 value type properties. + /// + [CustomEditor(typeof(Double3)), DefaultEditor] + public class Double3Editor : CustomEditor + { + /// + /// The X component editor. + /// + protected DoubleValueElement XElement; + + /// + /// The Y component editor. + /// + protected DoubleValueElement YElement; + + /// + /// The Z component editor. + /// + protected DoubleValueElement ZElement; + + /// + public override DisplayStyle Style => DisplayStyle.Inline; + + /// + public override void Initialize(LayoutElementsContainer layout) + { + var grid = layout.CustomContainer(); + var gridControl = grid.CustomControl; + gridControl.ClipChildren = false; + gridControl.Height = TextBox.DefaultHeight; + gridControl.SlotsHorizontally = 3; + gridControl.SlotsVertically = 1; + + LimitAttribute limit = null; + var attributes = Values.GetAttributes(); + if (attributes != null) + { + limit = (LimitAttribute)attributes.FirstOrDefault(x => x is LimitAttribute); + } + + XElement = grid.DoubleValue(); + XElement.SetLimits(limit); + XElement.DoubleValue.ValueChanged += OnValueChanged; + XElement.DoubleValue.SlidingEnd += ClearToken; + + YElement = grid.DoubleValue(); + YElement.SetLimits(limit); + YElement.DoubleValue.ValueChanged += OnValueChanged; + YElement.DoubleValue.SlidingEnd += ClearToken; + + ZElement = grid.DoubleValue(); + ZElement.SetLimits(limit); + ZElement.DoubleValue.ValueChanged += OnValueChanged; + ZElement.DoubleValue.SlidingEnd += ClearToken; + } + + private void OnValueChanged() + { + if (IsSetBlocked) + return; + + var isSliding = XElement.IsSliding || YElement.IsSliding || ZElement.IsSliding; + var token = isSliding ? this : null; + var value = new Double3(XElement.DoubleValue.Value, YElement.DoubleValue.Value, ZElement.DoubleValue.Value); + SetValue(value, token); + } + + /// + public override void Refresh() + { + base.Refresh(); + + if (HasDifferentValues) + { + // TODO: support different values for ValueBox + } + else + { + var value = (Double3)Values[0]; + XElement.DoubleValue.Value = value.X; + YElement.DoubleValue.Value = value.Y; + ZElement.DoubleValue.Value = value.Z; + } + } + } +} diff --git a/Source/Editor/CustomEditors/Editors/Double4Editor.cs b/Source/Editor/CustomEditors/Editors/Double4Editor.cs new file mode 100644 index 000000000..9244bf89d --- /dev/null +++ b/Source/Editor/CustomEditors/Editors/Double4Editor.cs @@ -0,0 +1,107 @@ +// Copyright (c) 2012-2022 Wojciech Figat. All rights reserved. + +using System.Linq; +using FlaxEditor.CustomEditors.Elements; +using FlaxEngine; +using FlaxEngine.GUI; + +namespace FlaxEditor.CustomEditors.Editors +{ + /// + /// Default implementation of the inspector used to edit Double4 value type properties. + /// + [CustomEditor(typeof(Double4)), DefaultEditor] + public class Double4Editor : CustomEditor + { + /// + /// The X component editor. + /// + protected DoubleValueElement XElement; + + /// + /// The Y component editor. + /// + protected DoubleValueElement YElement; + + /// + /// The Z component editor. + /// + protected DoubleValueElement ZElement; + + /// + /// The W component editor. + /// + protected DoubleValueElement WElement; + + /// + public override DisplayStyle Style => DisplayStyle.Inline; + + /// + public override void Initialize(LayoutElementsContainer layout) + { + var grid = layout.CustomContainer(); + var gridControl = grid.CustomControl; + gridControl.ClipChildren = false; + gridControl.Height = TextBox.DefaultHeight; + gridControl.SlotsHorizontally = 4; + gridControl.SlotsVertically = 1; + + LimitAttribute limit = null; + var attributes = Values.GetAttributes(); + if (attributes != null) + { + limit = (LimitAttribute)attributes.FirstOrDefault(x => x is LimitAttribute); + } + + XElement = grid.DoubleValue(); + XElement.SetLimits(limit); + XElement.DoubleValue.ValueChanged += OnValueChanged; + XElement.DoubleValue.SlidingEnd += ClearToken; + + YElement = grid.DoubleValue(); + YElement.SetLimits(limit); + YElement.DoubleValue.ValueChanged += OnValueChanged; + YElement.DoubleValue.SlidingEnd += ClearToken; + + ZElement = grid.DoubleValue(); + ZElement.SetLimits(limit); + ZElement.DoubleValue.ValueChanged += OnValueChanged; + ZElement.DoubleValue.SlidingEnd += ClearToken; + + WElement = grid.DoubleValue(); + WElement.SetLimits(limit); + WElement.DoubleValue.ValueChanged += OnValueChanged; + WElement.DoubleValue.SlidingEnd += ClearToken; + } + + private void OnValueChanged() + { + if (IsSetBlocked) + return; + + var isSliding = XElement.IsSliding || YElement.IsSliding || ZElement.IsSliding || WElement.IsSliding; + var token = isSliding ? this : null; + var value = new Double4(XElement.DoubleValue.Value, YElement.DoubleValue.Value, ZElement.DoubleValue.Value, WElement.DoubleValue.Value); + SetValue(value, token); + } + + /// + public override void Refresh() + { + base.Refresh(); + + if (HasDifferentValues) + { + // TODO: support different values for ValueBox + } + else + { + var value = (Double4)Values[0]; + XElement.DoubleValue.Value = value.X; + YElement.DoubleValue.Value = value.Y; + ZElement.DoubleValue.Value = value.Z; + WElement.DoubleValue.Value = value.W; + } + } + } +} diff --git a/Source/Engine/Core/Math/Double2.cs b/Source/Engine/Core/Math/Double2.cs new file mode 100644 index 000000000..ff2926779 --- /dev/null +++ b/Source/Engine/Core/Math/Double2.cs @@ -0,0 +1,1773 @@ +// Copyright (c) 2012-2022 Wojciech Figat. All rights reserved. + +// ----------------------------------------------------------------------------- +// Original code from SharpDX project. https://github.com/sharpdx/SharpDX/ +// Greetings to Alexandre Mutel. Original code published with the following license: +// ----------------------------------------------------------------------------- +// Copyright (c) 2010-2014 SharpDX - Alexandre Mutel +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +// ----------------------------------------------------------------------------- +// Original code from SlimMath project. http://code.google.com/p/slimmath/ +// Greetings to SlimDX Group. Original code published with the following license: +// ----------------------------------------------------------------------------- +/* +* Copyright (c) 2007-2011 SlimDX Group +* +* Permission is hereby granted, free of charge, to any person obtaining a copy +* of this software and associated documentation files (the "Software"), to deal +* in the Software without restriction, including without limitation the rights +* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +* copies of the Software, and to permit persons to whom the Software is +* furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice shall be included in +* all copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +* THE SOFTWARE. +*/ + +using System; +using System.ComponentModel; +using System.Globalization; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +namespace FlaxEngine +{ + [Serializable] + [TypeConverter(typeof(TypeConverters.Double2Converter))] + partial struct Double2 : IEquatable, IFormattable + { + private static readonly string _formatString = "X:{0:F2} Y:{1:F2}"; + + /// + /// The size of the type, in bytes. + /// + public static readonly int SizeInBytes = Marshal.SizeOf(typeof(Double2)); + + /// + /// A with all of its components set to zero. + /// + public static readonly Double2 Zero; + + /// + /// The X unit (1, 0). + /// + public static readonly Double2 UnitX = new Double2(1.0, 0.0); + + /// + /// The Y unit (0, 1). + /// + public static readonly Double2 UnitY = new Double2(0.0, 1.0); + + /// + /// A with all of its components set to half. + /// + public static readonly Double2 Half = new Double2(0.5f, 0.5f); + + /// + /// A with all of its components set to one. + /// + public static readonly Double2 One = new Double2(1.0, 1.0); + + /// + /// A with all components equal to . + /// + public static readonly Double2 Minimum = new Double2(double.MinValue); + + /// + /// A with all components equal to . + /// + public static readonly Double2 Maximum = new Double2(double.MaxValue); + + /// + /// Initializes a new instance of the struct. + /// + /// The value that will be assigned to all components. + public Double2(double value) + { + X = value; + Y = value; + } + + /// + /// Initializes a new instance of the struct. + /// + /// Initial value for the X component of the vector. + /// Initial value for the Y component of the vector. + public Double2(double x, double y) + { + X = x; + Y = y; + } + + /// + /// Initializes a new instance of the struct. + /// + /// A vector containing the values with which to initialize the X and Y components. + public Double2(Vector3 value) + { + X = value.X; + Y = value.Y; + } + + /// + /// Initializes a new instance of the struct. + /// + /// A vector containing the values with which to initialize the X and Y components. + public Double2(Double3 value) + { + X = value.X; + Y = value.Y; + } + + /// + /// Initializes a new instance of the struct. + /// + /// A vector containing the values with which to initialize the X and Y components. + public Double2(Double4 value) + { + X = value.X; + Y = value.Y; + } + + /// + /// Initializes a new instance of the struct. + /// + /// The values to assign to the X and Y components of the vector. This must be an array with two elements. + /// Thrown when is null. + /// Thrown when contains more or less than two elements. + public Double2(double[] values) + { + if (values == null) + throw new ArgumentNullException(nameof(values)); + if (values.Length != 2) + throw new ArgumentOutOfRangeException(nameof(values), "There must be two and only two input values for Double2."); + X = values[0]; + Y = values[1]; + } + + /// + /// Gets a value indicting whether this instance is normalized. + /// + public bool IsNormalized => Mathd.IsOne(X * X + Y * Y); + + /// + /// Gets a value indicting whether this vector is zero + /// + public bool IsZero => Mathd.IsZero(X) && Mathd.IsZero(Y); + + /// + /// Gets a minimum component value + /// + public double MinValue => Mathd.Min(X, Y); + + /// + /// Gets a maximum component value + /// + public double MaxValue => Mathd.Max(X, Y); + + /// + /// Gets an arithmetic average value of all vector components. + /// + public double AvgValue => (X + Y) * (1.0 / 2.0); + + /// + /// Gets a sum of the component values. + /// + public double ValuesSum => X + Y; + + /// + /// Gets a vector with values being absolute values of that vector. + /// + public Double2 Absolute => new Double2(Mathd.Abs(X), Mathd.Abs(Y)); + + /// + /// Gets a vector with values being opposite to values of that vector. + /// + public Double2 Negative => new Double2(-X, -Y); + + /// + /// Gets or sets the component at the specified index. + /// + /// The value of the X or Y component, depending on the index. + /// The index of the component to access. Use 0 for the X component and 1 for the Y component. + /// The value of the component at the specified index. + /// Thrown when the is out of the range [0,1]. + public double this[int index] + { + get + { + switch (index) + { + case 0: return X; + case 1: return Y; + } + throw new ArgumentOutOfRangeException(nameof(index), "Indices for Double2 run from 0 to 1, inclusive."); + } + set + { + switch (index) + { + case 0: + X = value; + break; + case 1: + Y = value; + break; + default: throw new ArgumentOutOfRangeException(nameof(index), "Indices for Double2 run from 0 to 1, inclusive."); + } + } + } + + /// + /// Calculates the length of the vector. + /// + /// The length of the vector. + /// may be preferred when only the relative length is needed and speed is of the essence. + public double Length => Math.Sqrt(X * X + Y * Y); + + /// + /// Calculates the squared length of the vector. + /// + /// The squared length of the vector. + /// This method may be preferred to when only a relative length is needed and speed is of the essence. + public double LengthSquared => X * X + Y * Y; + + /// + /// Converts the vector into a unit vector. + /// + public void Normalize() + { + double length = Length; + if (!Mathd.IsZero(length)) + { + double inv = 1.0 / length; + X *= inv; + Y *= inv; + } + } + + /// + /// Creates an array containing the elements of the vector. + /// + public double[] ToArray() + { + return new[] { X, Y }; + } + + /// + /// Adds two vectors. + /// + /// The first vector to add. + /// The second vector to add. + /// When the method completes, contains the sum of the two vectors. + public static void Add(ref Double2 left, ref Double2 right, out Double2 result) + { + result = new Double2(left.X + right.X, left.Y + right.Y); + } + + /// + /// Adds two vectors. + /// + /// The first vector to add. + /// The second vector to add. + /// The sum of the two vectors. + public static Double2 Add(Double2 left, Double2 right) + { + return new Double2(left.X + right.X, left.Y + right.Y); + } + + /// + /// Performs a component-wise addition. + /// + /// The input vector + /// The scalar value to be added to elements + /// The vector with added scalar for each element. + public static void Add(ref Double2 left, ref double right, out Double2 result) + { + result = new Double2(left.X + right, left.Y + right); + } + + /// + /// Performs a component-wise addition. + /// + /// The input vector + /// The scalar value to be added to elements + /// The vector with added scalar for each element. + public static Double2 Add(Double2 left, double right) + { + return new Double2(left.X + right, left.Y + right); + } + + /// + /// Subtracts two vectors. + /// + /// The first vector to subtract. + /// The second vector to subtract. + /// When the method completes, contains the difference of the two vectors. + public static void Subtract(ref Double2 left, ref Double2 right, out Double2 result) + { + result = new Double2(left.X - right.X, left.Y - right.Y); + } + + /// + /// Subtracts two vectors. + /// + /// The first vector to subtract. + /// The second vector to subtract. + /// The difference of the two vectors. + public static Double2 Subtract(Double2 left, Double2 right) + { + return new Double2(left.X - right.X, left.Y - right.Y); + } + + /// + /// Performs a component-wise subtraction. + /// + /// The input vector + /// The scalar value to be subtracted from elements + /// The vector with subtracted scalar for each element. + public static void Subtract(ref Double2 left, ref double right, out Double2 result) + { + result = new Double2(left.X - right, left.Y - right); + } + + /// + /// Performs a component-wise subtraction. + /// + /// The input vector + /// The scalar value to be subtracted from elements + /// The vector with subtracted scalar for each element. + public static Double2 Subtract(Double2 left, double right) + { + return new Double2(left.X - right, left.Y - right); + } + + /// + /// Performs a component-wise subtraction. + /// + /// The scalar value to be subtracted from elements + /// The input vector + /// The vector with subtracted scalar for each element. + public static void Subtract(ref double left, ref Double2 right, out Double2 result) + { + result = new Double2(left - right.X, left - right.Y); + } + + /// + /// Performs a component-wise subtraction. + /// + /// The scalar value to be subtracted from elements + /// The input vector + /// The vector with subtracted scalar for each element. + public static Double2 Subtract(double left, Double2 right) + { + return new Double2(left - right.X, left - right.Y); + } + + /// + /// Scales a vector by the given value. + /// + /// The vector to scale. + /// The amount by which to scale the vector. + /// When the method completes, contains the scaled vector. + public static void Multiply(ref Double2 value, double scale, out Double2 result) + { + result = new Double2(value.X * scale, value.Y * scale); + } + + /// + /// Scales a vector by the given value. + /// + /// The vector to scale. + /// The amount by which to scale the vector. + /// The scaled vector. + public static Double2 Multiply(Double2 value, double scale) + { + return new Double2(value.X * scale, value.Y * scale); + } + + /// + /// Multiplies a vector with another by performing component-wise multiplication. + /// + /// The first vector to multiply. + /// The second vector to multiply. + /// When the method completes, contains the multiplied vector. + public static void Multiply(ref Double2 left, ref Double2 right, out Double2 result) + { + result = new Double2(left.X * right.X, left.Y * right.Y); + } + + /// + /// Multiplies a vector with another by performing component-wise multiplication. + /// + /// The first vector to multiply. + /// The second vector to multiply. + /// The multiplied vector. + public static Double2 Multiply(Double2 left, Double2 right) + { + return new Double2(left.X * right.X, left.Y * right.Y); + } + + /// + /// Scales a vector by the given value. + /// + /// The vector to scale. + /// The amount by which to scale the vector. + /// When the method completes, contains the scaled vector. + public static void Divide(ref Double2 value, double scale, out Double2 result) + { + result = new Double2(value.X / scale, value.Y / scale); + } + + /// + /// Scales a vector by the given value. + /// + /// The vector to scale. + /// The amount by which to scale the vector. + /// The scaled vector. + public static Double2 Divide(Double2 value, double scale) + { + return new Double2(value.X / scale, value.Y / scale); + } + + /// + /// Scales a vector by the given value. + /// + /// The amount by which to scale the vector. + /// The vector to scale. + /// When the method completes, contains the scaled vector. + public static void Divide(double scale, ref Double2 value, out Double2 result) + { + result = new Double2(scale / value.X, scale / value.Y); + } + + /// + /// Scales a vector by the given value. + /// + /// The vector to scale. + /// The amount by which to scale the vector. + /// The scaled vector. + public static Double2 Divide(double scale, Double2 value) + { + return new Double2(scale / value.X, scale / value.Y); + } + + /// + /// Reverses the direction of a given vector. + /// + /// The vector to negate. + /// When the method completes, contains a vector facing in the opposite direction. + public static void Negate(ref Double2 value, out Double2 result) + { + result = new Double2(-value.X, -value.Y); + } + + /// + /// Reverses the direction of a given vector. + /// + /// The vector to negate. + /// A vector facing in the opposite direction. + public static Double2 Negate(Double2 value) + { + return new Double2(-value.X, -value.Y); + } + + /// + /// Returns a containing the 2D Cartesian coordinates of a point specified in Barycentric + /// coordinates relative to a 2D triangle. + /// + /// A containing the 2D Cartesian coordinates of vertex 1 of the triangle. + /// A containing the 2D Cartesian coordinates of vertex 2 of the triangle. + /// A containing the 2D Cartesian coordinates of vertex 3 of the triangle. + /// Barycentric coordinate b2, which expresses the weighting factor toward vertex 2 (specified in ). + /// Barycentric coordinate b3, which expresses the weighting factor toward vertex 3 (specified in ). + /// When the method completes, contains the 2D Cartesian coordinates of the specified point. + public static void Barycentric(ref Double2 value1, ref Double2 value2, ref Double2 value3, double amount1, double amount2, out Double2 result) + { + result = new Double2(value1.X + amount1 * (value2.X - value1.X) + amount2 * (value3.X - value1.X), + value1.Y + amount1 * (value2.Y - value1.Y) + amount2 * (value3.Y - value1.Y)); + } + + /// + /// Returns a containing the 2D Cartesian coordinates of a point specified in Barycentric + /// coordinates relative to a 2D triangle. + /// + /// A containing the 2D Cartesian coordinates of vertex 1 of the triangle. + /// A containing the 2D Cartesian coordinates of vertex 2 of the triangle. + /// A containing the 2D Cartesian coordinates of vertex 3 of the triangle. + /// Barycentric coordinate b2, which expresses the weighting factor toward vertex 2 (specified in ). + /// Barycentric coordinate b3, which expresses the weighting factor toward vertex 3 (specified in ). + /// A new containing the 2D Cartesian coordinates of the specified point. + public static Double2 Barycentric(Double2 value1, Double2 value2, Double2 value3, double amount1, double amount2) + { + Barycentric(ref value1, ref value2, ref value3, amount1, amount2, out Double2 result); + return result; + } + + /// + /// Restricts a value to be within a specified range. + /// + /// The value to clamp. + /// The minimum value. + /// The maximum value. + /// When the method completes, contains the clamped value. + public static void Clamp(ref Double2 value, ref Double2 min, ref Double2 max, out Double2 result) + { + double x = value.X; + x = x > max.X ? max.X : x; + x = x < min.X ? min.X : x; + double y = value.Y; + y = y > max.Y ? max.Y : y; + y = y < min.Y ? min.Y : y; + result = new Double2(x, y); + } + + /// + /// Restricts a value to be within a specified range. + /// + /// The value to clamp. + /// The minimum value. + /// The maximum value. + /// The clamped value. + public static Double2 Clamp(Double2 value, Double2 min, Double2 max) + { + Clamp(ref value, ref min, ref max, out Double2 result); + return result; + } + + /// + /// Saturates this instance in the range [0,1]. + /// + public void Saturate() + { + X = X < 0.0 ? 0.0 : X > 1.0 ? 1.0 : X; + Y = Y < 0.0 ? 0.0 : Y > 1.0 ? 1.0 : Y; + } + + /// + /// Calculates the area of the triangle. + /// + /// The first triangle vertex. + /// The second triangle vertex. + /// The third triangle vertex. + /// The triangle area. + public static double TriangleArea(ref Double2 v0, ref Double2 v1, ref Double2 v2) + { + return Math.Abs((v0.X * (v1.Y - v2.Y) + v1.X * (v2.Y - v0.Y) + v2.X * (v0.Y - v1.Y)) / 2); + } + + /// + /// Calculates the distance between two vectors. + /// + /// The first vector. + /// The second vector. + /// When the method completes, contains the distance between the two vectors. + /// may be preferred when only the relative distance is needed and speed is of the essence. + public static void Distance(ref Double2 value1, ref Double2 value2, out double result) + { + double x = value1.X - value2.X; + double y = value1.Y - value2.Y; + result = Math.Sqrt(x * x + y * y); + } + + /// + /// Calculates the distance between two vectors. + /// + /// The first vector. + /// The second vector. + /// The distance between the two vectors. + /// may be preferred when only the relative distance is needed and speed is of the essence. + public static double Distance(Double2 value1, Double2 value2) + { + double x = value1.X - value2.X; + double y = value1.Y - value2.Y; + return Math.Sqrt(x * x + y * y); + } + + /// + /// Calculates the distance between two vectors. + /// + /// The first vector. + /// The second vector. + /// The distance between the two vectors. + /// may be preferred when only the relative distance is needed and speed is of the essence. + public static double Distance(ref Double2 value1, ref Double2 value2) + { + double x = value1.X - value2.X; + double y = value1.Y - value2.Y; + return Math.Sqrt(x * x + y * y); + } + + /// + /// Calculates the squared distance between two vectors. + /// + /// The first vector. + /// The second vector + /// When the method completes, contains the squared distance between the two vectors. + public static void DistanceSquared(ref Double2 value1, ref Double2 value2, out double result) + { + double x = value1.X - value2.X; + double y = value1.Y - value2.Y; + result = x * x + y * y; + } + + /// + /// Calculates the squared distance between two vectors. + /// + /// The first vector. + /// The second vector + /// The squared distance between the two vectors. + public static double DistanceSquared(ref Double2 value1, ref Double2 value2) + { + double x = value1.X - value2.X; + double y = value1.Y - value2.Y; + return x * x + y * y; + } + + /// + /// Calculates the squared distance between two vectors. + /// + /// The first vector. + /// The second vector. + /// The squared distance between the two vectors. + public static double DistanceSquared(Double2 value1, Double2 value2) + { + double x = value1.X - value2.X; + double y = value1.Y - value2.Y; + return x * x + y * y; + } + + /// + /// Tests whether one vector is near another vector. + /// + /// The left vector. + /// The right vector. + /// The epsilon. + /// true if left and right are near, false otherwise + public static bool NearEqual(Double2 left, Double2 right, double epsilon = Mathd.Epsilon) + { + return NearEqual(ref left, ref right, epsilon); + } + + /// + /// Tests whether one vector is near another vector. + /// + /// The left vector. + /// The right vector. + /// The epsilon. + /// true if left and right are near another, false otherwise + public static bool NearEqual(ref Double2 left, ref Double2 right, double epsilon = Mathd.Epsilon) + { + return Mathd.WithinEpsilon(left.X, right.X, epsilon) && Mathd.WithinEpsilon(left.Y, right.Y, epsilon); + } + + /// + /// Calculates the dot product of two vectors. + /// + /// First source vector. + /// Second source vector. + /// When the method completes, contains the dot product of the two vectors. + public static void Dot(ref Double2 left, ref Double2 right, out double result) + { + result = left.X * right.X + left.Y * right.Y; + } + + /// + /// Calculates the dot product of two vectors. + /// + /// First source vector. + /// Second source vector. + /// The dot product of the two vectors. + public static double Dot(ref Double2 left, ref Double2 right) + { + return left.X * right.X + left.Y * right.Y; + } + + /// + /// Calculates the dot product of two vectors. + /// + /// First source vector. + /// Second source vector. + /// The dot product of the two vectors. + public static double Dot(Double2 left, Double2 right) + { + return left.X * right.X + left.Y * right.Y; + } + + /// + /// Calculates the cross product of two vectors. + /// + /// First source vector. + /// Second source vector. + /// When the method completes, contains the cross product of the two vectors. + public static void Cross(ref Double2 left, ref Double2 right, out double result) + { + result = left.X * right.Y - left.Y * right.X; + } + + /// + /// Calculates the cross product of two vectors. + /// + /// First source vector. + /// Second source vector. + /// The cross product of the two vectors. + public static double Cross(ref Double2 left, ref Double2 right) + { + return left.X * right.Y - left.Y * right.X; + } + + /// + /// Calculates the cross product of two vectors. + /// + /// First source vector. + /// Second source vector. + /// The cross product of the two vectors. + public static double Cross(Double2 left, Double2 right) + { + return left.X * right.Y - left.Y * right.X; + } + + /// + /// Converts the vector into a unit vector. + /// + /// The vector to normalize. + /// When the method completes, contains the normalized vector. + public static void Normalize(ref Double2 value, out Double2 result) + { + result = value; + result.Normalize(); + } + + /// + /// Converts the vector into a unit vector. + /// + /// The vector to normalize. + /// The normalized vector. + public static Double2 Normalize(Double2 value) + { + value.Normalize(); + return value; + } + + /// + /// Makes sure that Length of the output vector is always below max and above 0. + /// + /// Input Vector. + /// Max Length + public static Double2 ClampLength(Double2 vector, double max) + { + return ClampLength(vector, 0, max); + } + + /// + /// Makes sure that Length of the output vector is always below max and above min. + /// + /// Input Vector. + /// Min Length + /// Max Length + public static Double2 ClampLength(Double2 vector, double min, double max) + { + ClampLength(vector, min, max, out Double2 result); + return result; + } + + /// + /// Makes sure that Length of the output vector is always below max and above min. + /// + /// Input Vector. + /// Min Length + /// Max Length + /// The result value. + public static void ClampLength(Double2 vector, double min, double max, out Double2 result) + { + result = vector; + double lenSq = result.LengthSquared; + if (lenSq > max * max) + { + double scaleFactor = max / Math.Sqrt(lenSq); + result.X *= scaleFactor; + result.Y *= scaleFactor; + } + if (lenSq < min * min) + { + double scaleFactor = min / Math.Sqrt(lenSq); + result.X *= scaleFactor; + result.Y *= scaleFactor; + } + } + + /// + /// Returns the vector with components rounded to the nearest integer. + /// + /// The value. + /// The result. + public static Double2 Round(Double2 v) + { + return new Double2(Mathd.Round(v.X), Mathd.Round(v.Y)); + } + + /// + /// Returns the vector with components containing the smallest integer greater to or equal to the original value. + /// + /// The value. + /// The result. + public static Double2 Ceil(Double2 v) + { + return new Double2(Mathd.Ceil(v.X), Mathd.Ceil(v.Y)); + } + + /// + /// Breaks the components of the vector into an integral and a fractional part. Returns vector made of fractional parts. + /// + /// The value. + /// The result. + public static Double2 Mod(Double2 v) + { + return new Double2(v.X - (int)v.X, v.Y - (int)v.Y); + } + + /// + /// Performs a linear interpolation between two vectors. + /// + /// Start vector. + /// End vector. + /// Value between 0 and 1 indicating the weight of . + /// When the method completes, contains the linear interpolation of the two vectors. + /// Passing a value of 0 will cause to be returned; a value of 1 will cause to be returned. + public static void Lerp(ref Double2 start, ref Double2 end, double amount, out Double2 result) + { + result.X = Mathd.Lerp(start.X, end.X, amount); + result.Y = Mathd.Lerp(start.Y, end.Y, amount); + } + + /// + /// Performs a linear interpolation between two vectors. + /// + /// Start vector. + /// End vector. + /// Value between 0 and 1 indicating the weight of . + /// The linear interpolation of the two vectors. + /// Passing a value of 0 will cause to be returned; a value of 1 will cause to be returned. + public static Double2 Lerp(Double2 start, Double2 end, double amount) + { + Lerp(ref start, ref end, amount, out Double2 result); + return result; + } + + /// + /// Performs a linear interpolation between two vectors. + /// + /// Start vector. + /// End vector. + /// Value between 0 and 1 indicating the weight of . + /// When the method completes, contains the linear interpolation of the two vectors. + /// Passing a value of 0 will cause to be returned; a value of 1 will cause to be returned. + public static void Lerp(ref Double2 start, ref Double2 end, ref Double2 amount, out Double2 result) + { + result.X = Mathd.Lerp(start.X, end.X, amount.X); + result.Y = Mathd.Lerp(start.Y, end.Y, amount.Y); + } + + /// + /// Performs a linear interpolation between two vectors. + /// + /// Start vector. + /// End vector. + /// Value between 0 and 1 indicating the weight of . + /// The linear interpolation of the two vectors. + /// Passing a value of 0 will cause to be returned; a value of 1 will cause to be returned. + public static Double2 Lerp(Double2 start, Double2 end, Double2 amount) + { + Lerp(ref start, ref end, ref amount, out Double2 result); + return result; + } + + /// + /// Performs a cubic interpolation between two vectors. + /// + /// Start vector. + /// End vector. + /// Value between 0 and 1 indicating the weight of . + /// When the method completes, contains the cubic interpolation of the two vectors. + public static void SmoothStep(ref Double2 start, ref Double2 end, double amount, out Double2 result) + { + amount = Mathd.SmoothStep(amount); + Lerp(ref start, ref end, amount, out result); + } + + /// + /// Performs a cubic interpolation between two vectors. + /// + /// Start vector. + /// End vector. + /// Value between 0 and 1 indicating the weight of . + /// The cubic interpolation of the two vectors. + public static Double2 SmoothStep(Double2 start, Double2 end, double amount) + { + SmoothStep(ref start, ref end, amount, out Double2 result); + return result; + } + + /// + /// Performs a Hermite spline interpolation. + /// + /// First source position vector. + /// First source tangent vector. + /// Second source position vector. + /// Second source tangent vector. + /// Weighting factor. + /// When the method completes, contains the result of the Hermite spline interpolation. + public static void Hermite(ref Double2 value1, ref Double2 tangent1, ref Double2 value2, ref Double2 tangent2, double amount, out Double2 result) + { + double squared = amount * amount; + double cubed = amount * squared; + double part1 = 2.0 * cubed - 3.0 * squared + 1.0; + double part2 = -2.0 * cubed + 3.0 * squared; + double part3 = cubed - 2.0 * squared + amount; + double part4 = cubed - squared; + result.X = value1.X * part1 + value2.X * part2 + tangent1.X * part3 + tangent2.X * part4; + result.Y = value1.Y * part1 + value2.Y * part2 + tangent1.Y * part3 + tangent2.Y * part4; + } + + /// + /// Performs a Hermite spline interpolation. + /// + /// First source position vector. + /// First source tangent vector. + /// Second source position vector. + /// Second source tangent vector. + /// Weighting factor. + /// The result of the Hermite spline interpolation. + public static Double2 Hermite(Double2 value1, Double2 tangent1, Double2 value2, Double2 tangent2, double amount) + { + Hermite(ref value1, ref tangent1, ref value2, ref tangent2, amount, out Double2 result); + return result; + } + + /// + /// Calculates the 2D vector perpendicular to the given 2D vector. The result is always rotated 90-degrees in a counter-clockwise direction for a 2D coordinate system where the positive Y axis goes up. + /// + /// The input direction. + /// The result. + public static Double2 Perpendicular(Double2 inDirection) + { + return new Double2(-inDirection.Y, inDirection.X); + } + + /// + /// Calculates the 2D vector perpendicular to the given 2D vector. The result is always rotated 90-degrees in a counter-clockwise direction for a 2D coordinate system where the positive Y axis goes up. + /// + /// The in direction. + /// When the method completes, contains the result of the calculation. + public static void Perpendicular(ref Double2 inDirection, out Double2 result) + { + result = new Double2(-inDirection.Y, inDirection.X); + } + + /// + /// Performs a Catmull-Rom interpolation using the specified positions. + /// + /// The first position in the interpolation. + /// The second position in the interpolation. + /// The third position in the interpolation. + /// The fourth position in the interpolation. + /// Weighting factor. + /// When the method completes, contains the result of the Catmull-Rom interpolation. + public static void CatmullRom(ref Double2 value1, ref Double2 value2, ref Double2 value3, ref Double2 value4, double amount, out Double2 result) + { + double squared = amount * amount; + double cubed = amount * squared; + result.X = 0.5f * (2.0 * value2.X + (-value1.X + value3.X) * amount + + (2.0 * value1.X - 5.0 * value2.X + 4.0 * value3.X - value4.X) * squared + + (-value1.X + 3.0 * value2.X - 3.0 * value3.X + value4.X) * cubed); + result.Y = 0.5f * (2.0 * value2.Y + (-value1.Y + value3.Y) * amount + + (2.0 * value1.Y - 5.0 * value2.Y + 4.0 * value3.Y - value4.Y) * squared + + (-value1.Y + 3.0 * value2.Y - 3.0 * value3.Y + value4.Y) * cubed); + } + + /// + /// Performs a Catmull-Rom interpolation using the specified positions. + /// + /// The first position in the interpolation. + /// The second position in the interpolation. + /// The third position in the interpolation. + /// The fourth position in the interpolation. + /// Weighting factor. + /// A vector that is the result of the Catmull-Rom interpolation. + public static Double2 CatmullRom(Double2 value1, Double2 value2, Double2 value3, Double2 value4, double amount) + { + CatmullRom(ref value1, ref value2, ref value3, ref value4, amount, out Double2 result); + return result; + } + + /// + /// Returns a vector containing the largest components of the specified vectors. + /// + /// The first source vector. + /// The second source vector. + /// When the method completes, contains an new vector composed of the largest components of the source vectors. + public static void Max(ref Double2 left, ref Double2 right, out Double2 result) + { + result.X = left.X > right.X ? left.X : right.X; + result.Y = left.Y > right.Y ? left.Y : right.Y; + } + + /// + /// Returns a vector containing the largest components of the specified vectors. + /// + /// The first source vector. + /// The second source vector. + /// A vector containing the largest components of the source vectors. + public static Double2 Max(Double2 left, Double2 right) + { + Max(ref left, ref right, out Double2 result); + return result; + } + + /// + /// Returns a vector containing the smallest components of the specified vectors. + /// + /// The first source vector. + /// The second source vector. + /// When the method completes, contains an new vector composed of the smallest components of the source vectors. + public static void Min(ref Double2 left, ref Double2 right, out Double2 result) + { + result.X = left.X < right.X ? left.X : right.X; + result.Y = left.Y < right.Y ? left.Y : right.Y; + } + + /// + /// Returns a vector containing the smallest components of the specified vectors. + /// + /// The first source vector. + /// The second source vector. + /// A vector containing the smallest components of the source vectors. + public static Double2 Min(Double2 left, Double2 right) + { + Min(ref left, ref right, out Double2 result); + return result; + } + + /// + /// Returns the absolute value of a vector. + /// + /// The value. + /// A vector which components are less or equal to 0. + public static Double2 Abs(Double2 v) + { + return new Double2(Math.Abs(v.X), Math.Abs(v.Y)); + } + + /// + /// Returns the reflection of a vector off a surface that has the specified normal. + /// + /// The source vector. + /// Normal of the surface. + /// When the method completes, contains the reflected vector. + /// Reflect only gives the direction of a reflection off a surface, it does not determine whether the original vector was close enough to the surface to hit it. + public static void Reflect(ref Double2 vector, ref Double2 normal, out Double2 result) + { + double dot = vector.X * normal.X + vector.Y * normal.Y; + result.X = vector.X - 2.0 * dot * normal.X; + result.Y = vector.Y - 2.0 * dot * normal.Y; + } + + /// + /// Returns the reflection of a vector off a surface that has the specified normal. + /// + /// The source vector. + /// Normal of the surface. + /// The reflected vector. + /// Reflect only gives the direction of a reflection off a surface, it does not determine whether the original vector was close enough to the surface to hit it. + public static Double2 Reflect(Double2 vector, Double2 normal) + { + Reflect(ref vector, ref normal, out Double2 result); + return result; + } + + /// + /// Orthogonalizes a list of vectors. + /// + /// The list of orthogonalized vectors. + /// The list of vectors to orthogonalize. + /// + /// + /// Orthogonalization is the process of making all vectors orthogonal to each other. This + /// means that any given vector in the list will be orthogonal to any other given vector in the + /// list. + /// + /// + /// Because this method uses the modified Gram-Schmidt process, the resulting vectors + /// tend to be numerically unstable. The numeric stability decreases according to the vectors + /// position in the list so that the first vector is the most stable and the last vector is the + /// least stable. + /// + /// + /// Thrown when or is null. + /// Thrown when is shorter in length than . + public static void Orthogonalize(Double2[] destination, params Double2[] source) + { + //Uses the modified Gram-Schmidt process. + //q1 = m1 + //q2 = m2 - ((q1 ⋅ m2) / (q1 ⋅ q1)) * q1 + //q3 = m3 - ((q1 ⋅ m3) / (q1 ⋅ q1)) * q1 - ((q2 ⋅ m3) / (q2 ⋅ q2)) * q2 + //q4 = m4 - ((q1 ⋅ m4) / (q1 ⋅ q1)) * q1 - ((q2 ⋅ m4) / (q2 ⋅ q2)) * q2 - ((q3 ⋅ m4) / (q3 ⋅ q3)) * q3 + //q5 = ... + if (source == null) + throw new ArgumentNullException(nameof(source)); + if (destination == null) + throw new ArgumentNullException(nameof(destination)); + if (destination.Length < source.Length) + throw new ArgumentOutOfRangeException(nameof(destination), "The destination array must be of same length or larger length than the source array."); + for (var i = 0; i < source.Length; ++i) + { + Double2 v = source[i]; + for (var r = 0; r < i; ++r) + v -= Dot(destination[r], v) / Dot(destination[r], destination[r]) * destination[r]; + destination[i] = v; + } + } + + /// + /// Orthonormalizes a list of vectors. + /// + /// The list of orthonormalized vectors. + /// The list of vectors to orthonormalize. + /// + /// + /// Orthonormalization is the process of making all vectors orthogonal to each + /// other and making all vectors of unit length. This means that any given vector will + /// be orthogonal to any other given vector in the list. + /// + /// + /// Because this method uses the modified Gram-Schmidt process, the resulting vectors + /// tend to be numerically unstable. The numeric stability decreases according to the vectors + /// position in the list so that the first vector is the most stable and the last vector is the + /// least stable. + /// + /// + /// Thrown when or is null. + /// Thrown when is shorter in length than. + public static void Orthonormalize(Double2[] destination, params Double2[] source) + { + //Uses the modified Gram-Schmidt process. + //Because we are making unit vectors, we can optimize the math for orthogonalization + //and simplify the projection operation to remove the division. + //q1 = m1 / |m1| + //q2 = (m2 - (q1 ⋅ m2) * q1) / |m2 - (q1 ⋅ m2) * q1| + //q3 = (m3 - (q1 ⋅ m3) * q1 - (q2 ⋅ m3) * q2) / |m3 - (q1 ⋅ m3) * q1 - (q2 ⋅ m3) * q2| + //q4 = (m4 - (q1 ⋅ m4) * q1 - (q2 ⋅ m4) * q2 - (q3 ⋅ m4) * q3) / |m4 - (q1 ⋅ m4) * q1 - (q2 ⋅ m4) * q2 - (q3 ⋅ m4) * q3| + //q5 = ... + if (source == null) + throw new ArgumentNullException(nameof(source)); + if (destination == null) + throw new ArgumentNullException(nameof(destination)); + if (destination.Length < source.Length) + throw new ArgumentOutOfRangeException(nameof(destination), "The destination array must be of same length or larger length than the source array."); + for (var i = 0; i < source.Length; ++i) + { + Double2 v = source[i]; + for (var r = 0; r < i; ++r) + v -= Dot(destination[r], v) * destination[r]; + v.Normalize(); + destination[i] = v; + } + } + + /// + /// Transforms a 2D vector by the given rotation. + /// + /// The vector to rotate. + /// The rotation to apply. + /// When the method completes, contains the transformed . + public static void Transform(ref Double2 vector, ref Quaternion rotation, out Double2 result) + { + double x = rotation.X + rotation.X; + double y = rotation.Y + rotation.Y; + double z = rotation.Z + rotation.Z; + double wz = rotation.W * z; + double xx = rotation.X * x; + double xy = rotation.X * y; + double yy = rotation.Y * y; + double zz = rotation.Z * z; + result = new Double2(vector.X * (1.0 - yy - zz) + vector.Y * (xy - wz), vector.X * (xy + wz) + vector.Y * (1.0 - xx - zz)); + } + + /// + /// Transforms a 2D vector by the given rotation. + /// + /// The vector to rotate. + /// The rotation to apply. + /// The transformed . + public static Double2 Transform(Double2 vector, Quaternion rotation) + { + Transform(ref vector, ref rotation, out Double2 result); + return result; + } + + /// + /// Transforms an array of vectors by the given rotation. + /// + /// The array of vectors to transform. + /// The rotation to apply. + /// The array for which the transformed vectors are stored. This array may be the same array as . + /// Thrown when or is null. + /// Thrown when is shorter in length than . + public static void Transform(Double2[] source, ref Quaternion rotation, Double2[] destination) + { + if (source == null) + throw new ArgumentNullException(nameof(source)); + if (destination == null) + throw new ArgumentNullException(nameof(destination)); + if (destination.Length < source.Length) + throw new ArgumentOutOfRangeException(nameof(destination), "The destination array must be of same length or larger length than the source array."); + double x = rotation.X + rotation.X; + double y = rotation.Y + rotation.Y; + double z = rotation.Z + rotation.Z; + double wz = rotation.W * z; + double xx = rotation.X * x; + double xy = rotation.X * y; + double yy = rotation.Y * y; + double zz = rotation.Z * z; + double num1 = 1.0 - yy - zz; + double num2 = xy - wz; + double num3 = xy + wz; + double num4 = 1.0 - xx - zz; + for (var i = 0; i < source.Length; ++i) + destination[i] = new Double2(source[i].X * num1 + source[i].Y * num2, source[i].X * num3 + source[i].Y * num4); + } + + /// + /// Transforms a 2D vector by the given . + /// + /// The source vector. + /// The transformation . + /// When the method completes, contains the transformed . + public static void Transform(ref Double2 vector, ref Matrix transform, out Double4 result) + { + result = new Double4(vector.X * transform.M11 + vector.Y * transform.M21 + transform.M41, + vector.X * transform.M12 + vector.Y * transform.M22 + transform.M42, + vector.X * transform.M13 + vector.Y * transform.M23 + transform.M43, + vector.X * transform.M14 + vector.Y * transform.M24 + transform.M44); + } + + /// + /// Transforms a 2D vector by the given . + /// + /// The source vector. + /// The transformation . + /// The transformed . + public static Double4 Transform(Double2 vector, Matrix transform) + { + Transform(ref vector, ref transform, out Double4 result); + return result; + } + + /// + /// Transforms an array of 2D vectors by the given . + /// + /// The array of vectors to transform. + /// The transformation . + /// The array for which the transformed vectors are stored. + /// Thrown when or is null. + /// Thrown when is shorter in length than . + public static void Transform(Double2[] source, ref Matrix transform, Double4[] destination) + { + if (source == null) + throw new ArgumentNullException(nameof(source)); + if (destination == null) + throw new ArgumentNullException(nameof(destination)); + if (destination.Length < source.Length) + throw new ArgumentOutOfRangeException(nameof(destination), "The destination array must be of same length or larger length than the source array."); + for (var i = 0; i < source.Length; ++i) + Transform(ref source[i], ref transform, out destination[i]); + } + + /// + /// Performs a coordinate transformation using the given . + /// + /// The coordinate vector to transform. + /// The transformation . + /// When the method completes, contains the transformed coordinates. + /// + /// A coordinate transform performs the transformation with the assumption that the w component + /// is one. The four dimensional vector obtained from the transformation operation has each + /// component in the vector divided by the w component. This forces the w component to be one and + /// therefore makes the vector homogeneous. The homogeneous vector is often preferred when working + /// with coordinates as the w component can safely be ignored. + /// + public static void TransformCoordinate(ref Double2 coordinate, ref Matrix transform, out Double2 result) + { + var vector = new Double4 + { + X = coordinate.X * transform.M11 + coordinate.Y * transform.M21 + transform.M41, + Y = coordinate.X * transform.M12 + coordinate.Y * transform.M22 + transform.M42, + Z = coordinate.X * transform.M13 + coordinate.Y * transform.M23 + transform.M43, + W = 1f / (coordinate.X * transform.M14 + coordinate.Y * transform.M24 + transform.M44) + }; + result = new Double2(vector.X * vector.W, vector.Y * vector.W); + } + + /// + /// Performs a coordinate transformation using the given . + /// + /// The coordinate vector to transform. + /// The transformation . + /// The transformed coordinates. + /// + /// A coordinate transform performs the transformation with the assumption that the w component + /// is one. The four dimensional vector obtained from the transformation operation has each + /// component in the vector divided by the w component. This forces the w component to be one and + /// therefore makes the vector homogeneous. The homogeneous vector is often preferred when working + /// with coordinates as the w component can safely be ignored. + /// + public static Double2 TransformCoordinate(Double2 coordinate, Matrix transform) + { + TransformCoordinate(ref coordinate, ref transform, out Double2 result); + return result; + } + + /// + /// Performs a coordinate transformation on an array of vectors using the given . + /// + /// The array of coordinate vectors to transform. + /// The transformation . + /// The array for which the transformed vectors are stored. This array may be the same array as . + /// Thrown when or is null. + /// Thrown when is shorter in length than . + /// + /// A coordinate transform performs the transformation with the assumption that the w component + /// is one. The four dimensional vector obtained from the transformation operation has each + /// component in the vector divided by the w component. This forces the w component to be one and + /// therefore makes the vector homogeneous. The homogeneous vector is often preferred when working + /// with coordinates as the w component can safely be ignored. + /// + public static void TransformCoordinate(Double2[] source, ref Matrix transform, Double2[] destination) + { + if (source == null) + throw new ArgumentNullException(nameof(source)); + if (destination == null) + throw new ArgumentNullException(nameof(destination)); + if (destination.Length < source.Length) + throw new ArgumentOutOfRangeException(nameof(destination), "The destination array must be of same length or larger length than the source array."); + for (var i = 0; i < source.Length; ++i) + TransformCoordinate(ref source[i], ref transform, out destination[i]); + } + + /// + /// Performs a normal transformation using the given . + /// + /// The normal vector to transform. + /// The transformation . + /// When the method completes, contains the transformed normal. + /// + /// A normal transform performs the transformation with the assumption that the w component + /// is zero. This causes the fourth row and fourth column of the matrix to be unused. The + /// end result is a vector that is not translated, but all other transformation properties + /// apply. This is often preferred for normal vectors as normals purely represent direction + /// rather than location because normal vectors should not be translated. + /// + public static void TransformNormal(ref Double2 normal, ref Matrix transform, out Double2 result) + { + result = new Double2(normal.X * transform.M11 + normal.Y * transform.M21, + normal.X * transform.M12 + normal.Y * transform.M22); + } + + /// + /// Performs a normal transformation using the given . + /// + /// The normal vector to transform. + /// The transformation . + /// The transformed normal. + /// + /// A normal transform performs the transformation with the assumption that the w component + /// is zero. This causes the fourth row and fourth column of the matrix to be unused. The + /// end result is a vector that is not translated, but all other transformation properties + /// apply. This is often preferred for normal vectors as normals purely represent direction + /// rather than location because normal vectors should not be translated. + /// + public static Double2 TransformNormal(Double2 normal, Matrix transform) + { + TransformNormal(ref normal, ref transform, out Double2 result); + return result; + } + + /// + /// Performs a normal transformation on an array of vectors using the given . + /// + /// The array of normal vectors to transform. + /// The transformation . + /// The array for which the transformed vectors are stored. This array may be the same array as . + /// Thrown when or is null. + /// Thrown when is shorter in length than . + /// + /// A normal transform performs the transformation with the assumption that the w component + /// is zero. This causes the fourth row and fourth column of the matrix to be unused. The + /// end result is a vector that is not translated, but all other transformation properties + /// apply. This is often preferred for normal vectors as normals purely represent direction + /// rather than location because normal vectors should not be translated. + /// + public static void TransformNormal(Double2[] source, ref Matrix transform, Double2[] destination) + { + if (source == null) + throw new ArgumentNullException(nameof(source)); + if (destination == null) + throw new ArgumentNullException(nameof(destination)); + if (destination.Length < source.Length) + throw new ArgumentOutOfRangeException(nameof(destination), "The destination array must be of same length or larger length than the source array."); + for (var i = 0; i < source.Length; ++i) + TransformNormal(ref source[i], ref transform, out destination[i]); + } + + /// + /// Adds two vectors. + /// + /// The first vector to add. + /// The second vector to add. + /// The sum of the two vectors. + public static Double2 operator +(Double2 left, Double2 right) + { + return new Double2(left.X + right.X, left.Y + right.Y); + } + + /// + /// Multiplies a vector with another by performing component-wise multiplication equivalent to . + /// + /// The first vector to multiply. + /// The second vector to multiply. + /// The multiplication of the two vectors. + public static Double2 operator *(Double2 left, Double2 right) + { + return new Double2(left.X * right.X, left.Y * right.Y); + } + + /// + /// Assert a vector (return it unchanged). + /// + /// The vector to assert (unchanged). + /// The asserted (unchanged) vector. + public static Double2 operator +(Double2 value) + { + return value; + } + + /// + /// Subtracts two vectors. + /// + /// The first vector to subtract. + /// The second vector to subtract. + /// The difference of the two vectors. + public static Double2 operator -(Double2 left, Double2 right) + { + return new Double2(left.X - right.X, left.Y - right.Y); + } + + /// + /// Reverses the direction of a given vector. + /// + /// The vector to negate. + /// A vector facing in the opposite direction. + public static Double2 operator -(Double2 value) + { + return new Double2(-value.X, -value.Y); + } + + /// + /// Scales a vector by the given value. + /// + /// The vector to scale. + /// The amount by which to scale the vector. + /// The scaled vector. + public static Double2 operator *(double scale, Double2 value) + { + return new Double2(value.X * scale, value.Y * scale); + } + + /// + /// Scales a vector by the given value. + /// + /// The vector to scale. + /// The amount by which to scale the vector. + /// The scaled vector. + public static Double2 operator *(Double2 value, double scale) + { + return new Double2(value.X * scale, value.Y * scale); + } + + /// + /// Scales a vector by the given value. + /// + /// The vector to scale. + /// The amount by which to scale the vector. + /// The scaled vector. + public static Double2 operator /(Double2 value, double scale) + { + return new Double2(value.X / scale, value.Y / scale); + } + + /// + /// Scales a vector by the given value. + /// + /// The amount by which to scale the vector. + /// The vector to scale. + /// The scaled vector. + public static Double2 operator /(double scale, Double2 value) + { + return new Double2(scale / value.X, scale / value.Y); + } + + /// + /// Scales a vector by the given value. + /// + /// The vector to scale. + /// The amount by which to scale the vector. + /// The scaled vector. + public static Double2 operator /(Double2 value, Double2 scale) + { + return new Double2(value.X / scale.X, value.Y / scale.Y); + } + + /// + /// Remainder of value divided by scale. + /// + /// The vector to scale. + /// The amount by which to scale the vector. + /// The remained vector. + public static Double2 operator %(Double2 value, double scale) + { + return new Double2(value.X % scale, value.Y % scale); + } + + /// + /// Remainder of value divided by scale. + /// + /// The amount by which to scale the vector. + /// The vector to scale. + /// The remained vector. + public static Double2 operator %(double value, Double2 scale) + { + return new Double2(value % scale.X, value % scale.Y); + } + + /// + /// Remainder of value divided by scale. + /// + /// The vector to scale. + /// The amount by which to scale the vector. + /// The remained vector. + public static Double2 operator %(Double2 value, Double2 scale) + { + return new Double2(value.X % scale.X, value.Y % scale.Y); + } + + /// + /// Performs a component-wise addition. + /// + /// The input vector. + /// The scalar value to be added on elements + /// The vector with added scalar for each element. + public static Double2 operator +(Double2 value, double scalar) + { + return new Double2(value.X + scalar, value.Y + scalar); + } + + /// + /// Performs a component-wise addition. + /// + /// The input vector. + /// The scalar value to be added on elements + /// The vector with added scalar for each element. + public static Double2 operator +(double scalar, Double2 value) + { + return new Double2(scalar + value.X, scalar + value.Y); + } + + /// + /// Performs a component-wise subtraction. + /// + /// The input vector. + /// The scalar value to be subtracted from elements + /// The vector with subtracted scalar from each element. + public static Double2 operator -(Double2 value, double scalar) + { + return new Double2(value.X - scalar, value.Y - scalar); + } + + /// + /// Performs a component-wise subtraction. + /// + /// The input vector. + /// The scalar value to be subtracted from elements + /// The vector with subtracted scalar from each element. + public static Double2 operator -(double scalar, Double2 value) + { + return new Double2(scalar - value.X, scalar - value.Y); + } + + /// + /// Tests for equality between two objects. + /// + /// The first value to compare. + /// The second value to compare. + /// true if has the same value as ; otherwise,false. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool operator ==(Double2 left, Double2 right) + { + return Mathd.NearEqual(left.X, right.X) && Mathd.NearEqual(left.Y, right.Y); + } + + /// + /// Tests for inequality between two objects. + /// + /// The first value to compare. + /// The second value to compare. + /// true if has a different value than ; otherwise,false. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool operator !=(Double2 left, Double2 right) + { + return !Mathd.NearEqual(left.X, right.X) || !Mathd.NearEqual(left.Y, right.Y); + } + + /// + /// Performs an explicit conversion from to . + /// + /// The value. + /// The result of the conversion. + public static explicit operator Double3(Double2 value) + { + return new Double3(value, 0.0f); + } + + /// + /// Performs an explicit conversion from to . + /// + /// The value. + /// The result of the conversion. + public static explicit operator Double4(Double2 value) + { + return new Double4(value, 0.0, 0.0); + } + + /// + /// Returns a that represents this instance. + /// + /// A that represents this instance. + public override string ToString() + { + return string.Format(CultureInfo.CurrentCulture, "X:{0} Y:{1}", X, Y); + } + + /// + /// Returns a that represents this instance. + /// + /// The format. + /// A that represents this instance. + public string ToString(string format) + { + if (format == null) + return ToString(); + return string.Format(CultureInfo.CurrentCulture, _formatString, X.ToString(format, CultureInfo.CurrentCulture), Y.ToString(format, CultureInfo.CurrentCulture)); + } + + /// + /// Returns a that represents this instance. + /// + /// The format provider. + /// A that represents this instance. + public string ToString(IFormatProvider formatProvider) + { + return string.Format(formatProvider, _formatString, X, Y); + } + + /// + /// Returns a that represents this instance. + /// + /// The format. + /// The format provider. + /// A that represents this instance. + public string ToString(string format, IFormatProvider formatProvider) + { + if (format == null) + return ToString(formatProvider); + return string.Format(formatProvider, _formatString, X.ToString(format, formatProvider), Y.ToString(format, formatProvider)); + } + + /// + /// Returns a hash code for this instance. + /// + public override int GetHashCode() + { + unchecked + { + return (X.GetHashCode() * 397) ^ Y.GetHashCode(); + } + } + + /// + /// Determines whether the specified is equal to this instance. + /// + /// The to compare with this instance. + /// true if the specified is equal to this instance; otherwise, false. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public bool Equals(ref Double2 other) + { + return Mathd.NearEqual(other.X, X) && Mathd.NearEqual(other.Y, Y); + } + + /// + /// Determines whether the specified are equal. + /// + public static bool Equals(ref Double2 a, ref Double2 b) + { + return Mathd.NearEqual(a.X, b.X) && Mathd.NearEqual(a.Y, b.Y); + } + + /// + /// Determines whether the specified is equal to this instance. + /// + /// The to compare with this instance. + /// true if the specified is equal to this instance; otherwise, false. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public bool Equals(Double2 other) + { + return Mathd.NearEqual(other.X, X) && Mathd.NearEqual(other.Y, Y); + } + + /// + /// Determines whether the specified is equal to this instance. + /// + /// The to compare with this instance. + /// true if the specified is equal to this instance; otherwise, false. + public override bool Equals(object value) + { + if (!(value is Double2 other)) + return false; + return Mathd.NearEqual(other.X, X) && Mathd.NearEqual(other.Y, Y); + } + } +} diff --git a/Source/Engine/Core/Math/Double3.cs b/Source/Engine/Core/Math/Double3.cs new file mode 100644 index 000000000..24146022a --- /dev/null +++ b/Source/Engine/Core/Math/Double3.cs @@ -0,0 +1,2060 @@ +// Copyright (c) 2012-2022 Wojciech Figat. All rights reserved. + +// ----------------------------------------------------------------------------- +// Original code from SharpDX project. https://github.com/sharpdx/SharpDX/ +// Greetings to Alexandre Mutel. Original code published with the following license: +// ----------------------------------------------------------------------------- +// Copyright (c) 2010-2014 SharpDX - Alexandre Mutel +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +// ----------------------------------------------------------------------------- +// Original code from SlimMath project. http://code.google.com/p/slimmath/ +// Greetings to SlimDX Group. Original code published with the following license: +// ----------------------------------------------------------------------------- +/* +* Copyright (c) 2007-2011 SlimDX Group +* +* Permission is hereby granted, free of charge, to any person obtaining a copy +* of this software and associated documentation files (the "Software"), to deal +* in the Software without restriction, including without limitation the rights +* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +* copies of the Software, and to permit persons to whom the Software is +* furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice shall be included in +* all copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +* THE SOFTWARE. +*/ + +using System; +using System.ComponentModel; +using System.Globalization; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +namespace FlaxEngine +{ + [Serializable] + [TypeConverter(typeof(TypeConverters.Double3Converter))] + partial struct Double3 : IEquatable, IFormattable + { + private static readonly string _formatString = "X:{0:F2} Y:{1:F2} Z:{2:F2}"; + + /// + /// The size of the type, in bytes. + /// + public static readonly int SizeInBytes = Marshal.SizeOf(typeof(Double3)); + + /// + /// A with all of its components set to zero. + /// + public static readonly Double3 Zero; + + /// + /// The X unit (1, 0, 0). + /// + public static readonly Double3 UnitX = new Double3(1.0, 0.0, 0.0); + + /// + /// The Y unit (0, 1, 0). + /// + public static readonly Double3 UnitY = new Double3(0.0, 1.0, 0.0); + + /// + /// The Z unit (0, 0, 1). + /// + public static readonly Double3 UnitZ = new Double3(0.0, 0.0, 1.0); + + /// + /// A with all of its components set to one. + /// + public static readonly Double3 One = new Double3(1.0, 1.0, 1.0); + + /// + /// A with all of its components set to half. + /// + public static readonly Double3 Half = new Double3(0.5f, 0.5f, 0.5f); + + /// + /// A unit designating up (0, 1, 0). + /// + public static readonly Double3 Up = new Double3(0.0, 1.0, 0.0); + + /// + /// A unit designating down (0, -1, 0). + /// + public static readonly Double3 Down = new Double3(0.0, -1.0, 0.0); + + /// + /// A unit designating left (-1, 0, 0). + /// + public static readonly Double3 Left = new Double3(-1.0, 0.0, 0.0); + + /// + /// A unit designating right (1, 0, 0). + /// + public static readonly Double3 Right = new Double3(1.0, 0.0, 0.0); + + /// + /// A unit designating forward in a left-handed coordinate system (0, 0, 1). + /// + public static readonly Double3 Forward = new Double3(0.0, 0.0, 1.0); + + /// + /// A unit designating backward in a left-handed coordinate system (0, 0, -1). + /// + public static readonly Double3 Backward = new Double3(0.0, 0.0, -1.0); + + /// + /// A with all components equal to . + /// + public static readonly Double3 Minimum = new Double3(double.MinValue); + + /// + /// A with all components equal to . + /// + public static readonly Double3 Maximum = new Double3(double.MaxValue); + + /// + /// Initializes a new instance of the struct. + /// + /// The value that will be assigned to all components. + public Double3(double value) + { + X = value; + Y = value; + Z = value; + } + + /// + /// Initializes a new instance of the struct. + /// + /// Initial value for the X component of the vector. + /// Initial value for the Y component of the vector. + /// Initial value for the Z component of the vector. + public Double3(double x, double y, double z) + { + X = x; + Y = y; + Z = z; + } + + /// + /// Initializes a new instance of the struct. + /// + /// A vector containing the values with which to initialize the X and Y components. + /// Initial value for the Z component of the vector. + public Double3(Double2 value, double z) + { + X = value.X; + Y = value.Y; + Z = z; + } + + /// + /// Initializes a new instance of the struct. + /// + /// A vector containing the values with which to initialize the X, Y and Z components. + public Double3(Vector3 value) + { + X = value.X; + Y = value.Y; + Z = value.Z; + } + + /// + /// Initializes a new instance of the struct. + /// + /// A vector containing the values with which to initialize the X, Y and Z components. + public Double3(Double4 value) + { + X = value.X; + Y = value.Y; + Z = value.Z; + } + + /// + /// Initializes a new instance of the struct. + /// + /// The values to assign to the X, Y, and Z components of the vector. This must be an array with three elements. + /// Thrown when is null. + /// Thrown when contains more or less than three elements. + public Double3(double[] values) + { + if (values == null) + throw new ArgumentNullException(nameof(values)); + if (values.Length != 3) + throw new ArgumentOutOfRangeException(nameof(values), "There must be three and only three input values for Double3."); + X = values[0]; + Y = values[1]; + Z = values[2]; + } + + /// + /// Gets a value indicting whether this instance is normalized. + /// + public bool IsNormalized => Mathd.IsOne(X * X + Y * Y + Z * Z); + + /// + /// Gets the normalized vector. Returned vector has length equal 1. + /// + public Double3 Normalized + { + get + { + Double3 result = this; + result.Normalize(); + return result; + } + } + + /// + /// Gets a value indicting whether this vector is zero + /// + public bool IsZero => Mathd.IsZero(X) && Mathd.IsZero(Y) && Mathd.IsZero(Z); + + /// + /// Gets a value indicting whether this vector is one + /// + public bool IsOne => Mathd.IsOne(X) && Mathd.IsOne(Y) && Mathd.IsOne(Z); + + /// + /// Gets a minimum component value + /// + public double MinValue => Mathd.Min(X, Mathd.Min(Y, Z)); + + /// + /// Gets a maximum component value + /// + public double MaxValue => Mathd.Max(X, Mathd.Max(Y, Z)); + + /// + /// Gets an arithmetic average value of all vector components. + /// + public double AvgValue => (X + Y + Z) * (1.0 / 3.0); + + /// + /// Gets a sum of the component values. + /// + public double ValuesSum => X + Y + Z; + + /// + /// Gets a vector with values being absolute values of that vector. + /// + public Double3 Absolute => new Double3(Math.Abs(X), Math.Abs(Y), Math.Abs(Z)); + + /// + /// Gets a vector with values being opposite to values of that vector. + /// + public Double3 Negative => new Double3(-X, -Y, -Z); + + /// + /// Gets or sets the component at the specified index. + /// + /// The value of the X, Y, or Z component, depending on the index. + /// The index of the component to access. Use 0 for the X component, 1 for the Y component, and 2 for the Z component. + /// The value of the component at the specified index. + /// Thrown when the is out of the range [0, 2]. + public double this[int index] + { + get + { + switch (index) + { + case 0: return X; + case 1: return Y; + case 2: return Z; + } + throw new ArgumentOutOfRangeException(nameof(index), "Indices for Double3 run from 0 to 2, inclusive."); + } + set + { + switch (index) + { + case 0: + X = value; + break; + case 1: + Y = value; + break; + case 2: + Z = value; + break; + default: throw new ArgumentOutOfRangeException(nameof(index), "Indices for Double3 run from 0 to 2, inclusive."); + } + } + } + + /// + /// Calculates the length of the vector. + /// + /// The length of the vector. + /// may be preferred when only the relative length is needed and speed is of the essence. + public double Length => Math.Sqrt(X * X + Y * Y + Z * Z); + + /// + /// Calculates the squared length of the vector. + /// + /// The squared length of the vector. + /// This method may be preferred to when only a relative length is needed and speed is of the essence. + public double LengthSquared => X * X + Y * Y + Z * Z; + + /// + /// Converts the vector into a unit vector. + /// + public void Normalize() + { + double length = Length; + if (!Mathd.IsZero(length)) + { + double inv = 1.0 / length; + X *= inv; + Y *= inv; + Z *= inv; + } + } + + /// + /// Reverses the direction of the vector. + /// + public void Negate() + { + X *= -1; + Y *= -1; + Z *= -1; + } + + /// + /// When this vector contains Euler angles (degrees), ensure that angles are between +/-180 + /// + public void UnwindEuler() + { + X = Mathd.UnwindDegrees(X); + Y = Mathd.UnwindDegrees(Y); + Z = Mathd.UnwindDegrees(Z); + } + + /// + /// Creates an array containing the elements of the vector. + /// + /// A three-element array containing the components of the vector. + public double[] ToArray() + { + return new[] { X, Y, Z }; + } + + /// + /// Adds two vectors. + /// + /// The first vector to add. + /// The second vector to add. + /// When the method completes, contains the sum of the two vectors. + public static void Add(ref Double3 left, ref Double3 right, out Double3 result) + { + result = new Double3(left.X + right.X, left.Y + right.Y, left.Z + right.Z); + } + + /// + /// Adds two vectors. + /// + /// The first vector to add. + /// The second vector to add. + /// The sum of the two vectors. + public static Double3 Add(Double3 left, Double3 right) + { + return new Double3(left.X + right.X, left.Y + right.Y, left.Z + right.Z); + } + + /// + /// Performs a component-wise addition. + /// + /// The input vector + /// The scalar value to be added to elements + /// The vector with added scalar for each element. + public static void Add(ref Double3 left, ref double right, out Double3 result) + { + result = new Double3(left.X + right, left.Y + right, left.Z + right); + } + + /// + /// Performs a component-wise addition. + /// + /// The input vector + /// The scalar value to be added to elements + /// The vector with added scalar for each element. + public static Double3 Add(Double3 left, double right) + { + return new Double3(left.X + right, left.Y + right, left.Z + right); + } + + /// + /// Subtracts two vectors. + /// + /// The first vector to subtract. + /// The second vector to subtract. + /// When the method completes, contains the difference of the two vectors. + public static void Subtract(ref Double3 left, ref Double3 right, out Double3 result) + { + result = new Double3(left.X - right.X, left.Y - right.Y, left.Z - right.Z); + } + + /// + /// Subtracts two vectors. + /// + /// The first vector to subtract. + /// The second vector to subtract. + /// The difference of the two vectors. + public static Double3 Subtract(Double3 left, Double3 right) + { + return new Double3(left.X - right.X, left.Y - right.Y, left.Z - right.Z); + } + + /// + /// Performs a component-wise subtraction. + /// + /// The input vector + /// The scalar value to be subtracted from elements + /// The vector with subtracted scalar for each element. + public static void Subtract(ref Double3 left, ref double right, out Double3 result) + { + result = new Double3(left.X - right, left.Y - right, left.Z - right); + } + + /// + /// Performs a component-wise subtraction. + /// + /// The input vector + /// The scalar value to be subtracted from elements + /// The vector with subtracted scalar for each element. + public static Double3 Subtract(Double3 left, double right) + { + return new Double3(left.X - right, left.Y - right, left.Z - right); + } + + /// + /// Performs a component-wise subtraction. + /// + /// The scalar value to be subtracted from elements + /// The input vector. + /// The vector with subtracted scalar for each element. + public static void Subtract(ref double left, ref Double3 right, out Double3 result) + { + result = new Double3(left - right.X, left - right.Y, left - right.Z); + } + + /// + /// Performs a component-wise subtraction. + /// + /// The scalar value to be subtracted from elements + /// The input vector. + /// The vector with subtracted scalar for each element. + public static Double3 Subtract(double left, Double3 right) + { + return new Double3(left - right.X, left - right.Y, left - right.Z); + } + + /// + /// Scales a vector by the given value. + /// + /// The vector to scale. + /// The amount by which to scale the vector. + /// When the method completes, contains the scaled vector. + public static void Multiply(ref Double3 value, double scale, out Double3 result) + { + result = new Double3(value.X * scale, value.Y * scale, value.Z * scale); + } + + /// + /// Scales a vector by the given value. + /// + /// The vector to scale. + /// The amount by which to scale the vector. + /// The scaled vector. + public static Double3 Multiply(Double3 value, double scale) + { + return new Double3(value.X * scale, value.Y * scale, value.Z * scale); + } + + /// + /// Multiply a vector with another by performing component-wise multiplication. + /// + /// The first vector to multiply. + /// The second vector to multiply. + /// When the method completes, contains the multiplied vector. + public static void Multiply(ref Double3 left, ref Double3 right, out Double3 result) + { + result = new Double3(left.X * right.X, left.Y * right.Y, left.Z * right.Z); + } + + /// + /// Multiply a vector with another by performing component-wise multiplication. + /// + /// The first vector to Multiply. + /// The second vector to multiply. + /// The multiplied vector. + public static Double3 Multiply(Double3 left, Double3 right) + { + return new Double3(left.X * right.X, left.Y * right.Y, left.Z * right.Z); + } + + /// + /// Divides a vector by the given value. + /// + /// The vector to scale. + /// The amount by which to scale the vector (per component). + /// When the method completes, contains the divided vector. + public static void Divide(ref Double3 value, ref Double3 scale, out Double3 result) + { + result = new Double3(value.X / scale.X, value.Y / scale.Y, value.Z / scale.Z); + } + + /// + /// Divides a vector by the given value. + /// + /// The vector to scale. + /// The amount by which to scale the vector (per component). + /// The divided vector. + public static Double3 Divide(Double3 value, Double3 scale) + { + return new Double3(value.X / scale.X, value.Y / scale.Y, value.Z / scale.Z); + } + + /// + /// Scales a vector by the given value. + /// + /// The vector to scale. + /// The amount by which to scale the vector. + /// When the method completes, contains the scaled vector. + public static void Divide(ref Double3 value, double scale, out Double3 result) + { + result = new Double3(value.X / scale, value.Y / scale, value.Z / scale); + } + + /// + /// Scales a vector by the given value. + /// + /// The vector to scale. + /// The amount by which to scale the vector. + /// The scaled vector. + public static Double3 Divide(Double3 value, double scale) + { + return new Double3(value.X / scale, value.Y / scale, value.Z / scale); + } + + /// + /// Scales a vector by the given value. + /// + /// The amount by which to scale the vector. + /// The vector to scale. + /// When the method completes, contains the scaled vector. + public static void Divide(double scale, ref Double3 value, out Double3 result) + { + result = new Double3(scale / value.X, scale / value.Y, scale / value.Z); + } + + /// + /// Scales a vector by the given value. + /// + /// The vector to scale. + /// The amount by which to scale the vector. + /// The scaled vector. + public static Double3 Divide(double scale, Double3 value) + { + return new Double3(scale / value.X, scale / value.Y, scale / value.Z); + } + + /// + /// Reverses the direction of a given vector. + /// + /// The vector to negate. + /// When the method completes, contains a vector facing in the opposite direction. + public static void Negate(ref Double3 value, out Double3 result) + { + result = new Double3(-value.X, -value.Y, -value.Z); + } + + /// + /// Reverses the direction of a given vector. + /// + /// The vector to negate. + /// A vector facing in the opposite direction. + public static Double3 Negate(Double3 value) + { + return new Double3(-value.X, -value.Y, -value.Z); + } + + /// + /// Returns a containing the 3D Cartesian coordinates of a point specified in Barycentric coordinates relative to a 3D triangle. + /// + /// A containing the 3D Cartesian coordinates of vertex 1 of the triangle. + /// A containing the 3D Cartesian coordinates of vertex 2 of the triangle. + /// A containing the 3D Cartesian coordinates of vertex 3 of the triangle. + /// Barycentric coordinate b2, which expresses the weighting factor toward vertex 2 (specified in ). + /// Barycentric coordinate b3, which expresses the weighting factor toward vertex 3 (specified in ). + /// When the method completes, contains the 3D Cartesian coordinates of the specified point. + public static void Barycentric(ref Double3 value1, ref Double3 value2, ref Double3 value3, double amount1, double amount2, out Double3 result) + { + result = new Double3(value1.X + amount1 * (value2.X - value1.X) + amount2 * (value3.X - value1.X), + value1.Y + amount1 * (value2.Y - value1.Y) + amount2 * (value3.Y - value1.Y), + value1.Z + amount1 * (value2.Z - value1.Z) + amount2 * (value3.Z - value1.Z)); + } + + /// + /// Returns a containing the 3D Cartesian coordinates of a point specified in Barycentric coordinates relative to a 3D triangle. + /// + /// A containing the 3D Cartesian coordinates of vertex 1 of the triangle. + /// A containing the 3D Cartesian coordinates of vertex 2 of the triangle. + /// A containing the 3D Cartesian coordinates of vertex 3 of the triangle. + /// Barycentric coordinate b2, which expresses the weighting factor toward vertex 2 (specified in ). + /// Barycentric coordinate b3, which expresses the weighting factor toward vertex 3 (specified in ). + /// A new containing the 3D Cartesian coordinates of the specified point. + public static Double3 Barycentric(Double3 value1, Double3 value2, Double3 value3, double amount1, double amount2) + { + Barycentric(ref value1, ref value2, ref value3, amount1, amount2, out var result); + return result; + } + + /// + /// Restricts a value to be within a specified range. + /// + /// The value to clamp. + /// The minimum value. + /// The maximum value. + /// When the method completes, contains the clamped value. + public static void Clamp(ref Double3 value, ref Double3 min, ref Double3 max, out Double3 result) + { + double x = value.X; + x = x > max.X ? max.X : x; + x = x < min.X ? min.X : x; + double y = value.Y; + y = y > max.Y ? max.Y : y; + y = y < min.Y ? min.Y : y; + double z = value.Z; + z = z > max.Z ? max.Z : z; + z = z < min.Z ? min.Z : z; + result = new Double3(x, y, z); + } + + /// + /// Restricts a value to be within a specified range. + /// + /// The value to clamp. + /// The minimum value. + /// The maximum value. + /// The clamped value. + public static Double3 Clamp(Double3 value, Double3 min, Double3 max) + { + Clamp(ref value, ref min, ref max, out var result); + return result; + } + + /// + /// Calculates the cross product of two vectors. + /// + /// First source vector. + /// Second source vector. + /// When the method completes, contains he cross product of the two vectors. + public static void Cross(ref Double3 left, ref Double3 right, out Double3 result) + { + result = new Double3(left.Y * right.Z - left.Z * right.Y, + left.Z * right.X - left.X * right.Z, + left.X * right.Y - left.Y * right.X); + } + + /// + /// Calculates the cross product of two vectors. + /// + /// First source vector. + /// Second source vector. + /// The cross product of the two vectors. + public static Double3 Cross(Double3 left, Double3 right) + { + Cross(ref left, ref right, out var result); + return result; + } + + /// + /// Calculates the distance between two vectors. + /// + /// The first vector. + /// The second vector. + /// When the method completes, contains the distance between the two vectors. + /// may be preferred when only the relative distance is needed and speed is of the essence. + public static void Distance(ref Double3 value1, ref Double3 value2, out double result) + { + double x = value1.X - value2.X; + double y = value1.Y - value2.Y; + double z = value1.Z - value2.Z; + result = Math.Sqrt(x * x + y * y + z * z); + } + + /// + /// Calculates the distance between two vectors. + /// + /// The first vector. + /// The second vector. + /// The distance between the two vectors. + /// may be preferred when only the relative distance is needed and speed is of the essence. + public static double Distance(ref Double3 value1, ref Double3 value2) + { + double x = value1.X - value2.X; + double y = value1.Y - value2.Y; + double z = value1.Z - value2.Z; + return Math.Sqrt(x * x + y * y + z * z); + } + + /// + /// Calculates the distance between two vectors. + /// + /// The first vector. + /// The second vector. + /// The distance between the two vectors. + /// may be preferred when only the relative distance is needed and speed is of the essence. + public static double Distance(Double3 value1, Double3 value2) + { + double x = value1.X - value2.X; + double y = value1.Y - value2.Y; + double z = value1.Z - value2.Z; + return Math.Sqrt(x * x + y * y + z * z); + } + + /// + /// Calculates the squared distance between two vectors. + /// + /// The first vector. + /// The second vector. + /// When the method completes, contains the squared distance between the two vectors. + public static void DistanceSquared(ref Double3 value1, ref Double3 value2, out double result) + { + double x = value1.X - value2.X; + double y = value1.Y - value2.Y; + double z = value1.Z - value2.Z; + result = x * x + y * y + z * z; + } + + /// + /// Calculates the squared distance between two vectors. + /// + /// The first vector. + /// The second vector. + /// The squared distance between the two vectors. + public static double DistanceSquared(ref Double3 value1, ref Double3 value2) + { + double x = value1.X - value2.X; + double y = value1.Y - value2.Y; + double z = value1.Z - value2.Z; + return x * x + y * y + z * z; + } + + /// + /// Calculates the squared distance between two vectors. + /// + /// The first vector. + /// The second vector. + /// The squared distance between the two vectors. + public static double DistanceSquared(Double3 value1, Double3 value2) + { + double x = value1.X - value2.X; + double y = value1.Y - value2.Y; + double z = value1.Z - value2.Z; + return x * x + y * y + z * z; + } + + /// + /// Calculates the distance between two vectors on the XY plane (ignoring Z). + /// + /// The first vector. + /// The second vector. + /// When the method completes, contains the distance between the two vectors in the XY plane. + public static void DistanceXY(ref Double3 value1, ref Double3 value2, out double result) + { + double x = value1.X - value2.X; + double y = value1.Y - value2.Y; + result = Math.Sqrt(x * x + y * y); + } + + /// + /// Calculates the squared distance between two vectors on the XY plane (ignoring Z). + /// + /// The first vector. + /// The second vector + /// When the method completes, contains the squared distance between the two vectors in the XY plane. + public static void DistanceXYSquared(ref Double3 value1, ref Double3 value2, out double result) + { + double x = value1.X - value2.X; + double y = value1.Y - value2.Y; + result = x * x + y * y; + } + + /// + /// Calculates the distance between two vectors on the XZ plane (ignoring Y). + /// + /// The first vector. + /// The second vector. + /// When the method completes, contains the distance between the two vectors in the XY plane. + public static void DistanceXZ(ref Double3 value1, ref Double3 value2, out double result) + { + double x = value1.X - value2.X; + double z = value1.Z - value2.Z; + result = Math.Sqrt(x * x + z * z); + } + + /// + /// Calculates the squared distance between two vectors on the XZ plane (ignoring Y). + /// + /// The first vector. + /// The second vector + /// When the method completes, contains the squared distance between the two vectors in the XY plane. + public static void DistanceXZSquared(ref Double3 value1, ref Double3 value2, out double result) + { + double x = value1.X - value2.X; + double z = value1.Z - value2.Z; + result = x * x + z * z; + } + + /// + /// Calculates the distance between two vectors on the YZ plane (ignoring X). + /// + /// The first vector. + /// The second vector. + /// When the method completes, contains the distance between the two vectors in the YZ plane. + public static void DistanceYZ(ref Double3 value1, ref Double3 value2, out double result) + { + double y = value1.Y - value2.Y; + double z = value1.Z - value2.Z; + result = Math.Sqrt(y * y + z * z); + } + + /// + /// Calculates the squared distance between two vectors on the YZ plane (ignoring X). + /// + /// The first vector. + /// The second vector + /// When the method completes, contains the squared distance between the two vectors in the YZ plane. + public static void DistanceYZSquared(ref Double3 value1, ref Double3 value2, out double result) + { + double y = value1.Y - value2.Y; + double z = value1.Z - value2.Z; + result = y * y + z * z; + } + + /// + /// Tests whether one vector is near another vector. + /// + /// The left vector. + /// The right vector. + /// The epsilon. + /// true if left and right are near another, false otherwise + public static bool NearEqual(Double3 left, Double3 right, double epsilon = Mathd.Epsilon) + { + return NearEqual(ref left, ref right, epsilon); + } + + /// + /// Tests whether one vector is near another vector. + /// + /// The left vector. + /// The right vector. + /// The epsilon. + /// true if left and right are near another, false otherwise + public static bool NearEqual(ref Double3 left, ref Double3 right, double epsilon = Mathd.Epsilon) + { + return Mathd.WithinEpsilon(left.X, right.X, epsilon) && Mathd.WithinEpsilon(left.Y, right.Y, epsilon) && Mathd.WithinEpsilon(left.Z, right.Z, epsilon); + } + + /// + /// Calculates the dot product of two vectors. + /// + /// First source vector. + /// Second source vector. + /// When the method completes, contains the dot product of the two vectors. + public static void Dot(ref Double3 left, ref Double3 right, out double result) + { + result = left.X * right.X + left.Y * right.Y + left.Z * right.Z; + } + + /// + /// Calculates the dot product of two vectors. + /// + /// First source vector. + /// Second source vector. + /// The dot product of the two vectors. + public static double Dot(ref Double3 left, ref Double3 right) + { + return left.X * right.X + left.Y * right.Y + left.Z * right.Z; + } + + /// + /// Calculates the dot product of two vectors. + /// + /// First source vector. + /// Second source vector. + /// The dot product of the two vectors. + public static double Dot(Double3 left, Double3 right) + { + return left.X * right.X + left.Y * right.Y + left.Z * right.Z; + } + + /// + /// Converts the vector into a unit vector. + /// + /// The vector to normalize. + /// When the method completes, contains the normalized vector. + public static void Normalize(ref Double3 value, out Double3 result) + { + result = value; + result.Normalize(); + } + + /// + /// Converts the vector into a unit vector. + /// + /// The vector to normalize. + /// The normalized vector. + public static Double3 Normalize(Double3 value) + { + value.Normalize(); + return value; + } + + /// + /// Makes sure that Length of the output vector is always below max and above 0. + /// + /// Input Vector. + /// Max Length + public static Double3 ClampLength(Double3 vector, double max) + { + return ClampLength(vector, 0, max); + } + + /// + /// Makes sure that Length of the output vector is always below max and above min. + /// + /// Input Vector. + /// Min Length + /// Max Length + public static Double3 ClampLength(Double3 vector, double min, double max) + { + ClampLength(vector, min, max, out Double3 result); + return result; + } + + /// + /// Makes sure that Length of the output vector is always below max and above min. + /// + /// Input Vector. + /// Min Length + /// Max Length + /// The result vector. + public static void ClampLength(Double3 vector, double min, double max, out Double3 result) + { + result.X = vector.X; + result.Y = vector.Y; + result.Z = vector.Z; + double lenSq = result.LengthSquared; + if (lenSq > max * max) + { + double scaleFactor = max / Math.Sqrt(lenSq); + result.X *= scaleFactor; + result.Y *= scaleFactor; + result.Z *= scaleFactor; + } + if (lenSq < min * min) + { + double scaleFactor = min / Math.Sqrt(lenSq); + result.X *= scaleFactor; + result.Y *= scaleFactor; + result.Z *= scaleFactor; + } + } + + /// + /// Performs a linear interpolation between two vectors. + /// + /// Start vector. + /// End vector. + /// Value between 0 and 1 indicating the weight of . + /// When the method completes, contains the linear interpolation of the two vectors. + /// Passing a value of 0 will cause to be returned; a value of 1 will cause to be returned. + public static void Lerp(ref Double3 start, ref Double3 end, double amount, out Double3 result) + { + result.X = Mathd.Lerp(start.X, end.X, amount); + result.Y = Mathd.Lerp(start.Y, end.Y, amount); + result.Z = Mathd.Lerp(start.Z, end.Z, amount); + } + + /// + /// Performs a linear interpolation between two vectors. + /// + /// Start vector. + /// End vector. + /// Value between 0 and 1 indicating the weight of . + /// The linear interpolation of the two vectors. + /// Passing a value of 0 will cause to be returned; a value of 1 will cause to be returned. + public static Double3 Lerp(Double3 start, Double3 end, double amount) + { + Lerp(ref start, ref end, amount, out var result); + return result; + } + + /// + /// Performs a cubic interpolation between two vectors. + /// + /// Start vector. + /// End vector. + /// Value between 0 and 1 indicating the weight of . + /// When the method completes, contains the cubic interpolation of the two vectors. + public static void SmoothStep(ref Double3 start, ref Double3 end, double amount, out Double3 result) + { + amount = Mathd.SmoothStep(amount); + Lerp(ref start, ref end, amount, out result); + } + + /// + /// Performs a cubic interpolation between two vectors. + /// + /// Start vector. + /// End vector. + /// Value between 0 and 1 indicating the weight of . + /// The cubic interpolation of the two vectors. + public static Double3 SmoothStep(Double3 start, Double3 end, double amount) + { + SmoothStep(ref start, ref end, amount, out var result); + return result; + } + + /// + /// Performs a Hermite spline interpolation. + /// + /// First source position vector. + /// First source tangent vector. + /// Second source position vector. + /// Second source tangent vector. + /// Weighting factor. + /// When the method completes, contains the result of the Hermite spline interpolation. + public static void Hermite(ref Double3 value1, ref Double3 tangent1, ref Double3 value2, ref Double3 tangent2, double amount, out Double3 result) + { + double squared = amount * amount; + double cubed = amount * squared; + double part1 = 2.0 * cubed - 3.0 * squared + 1.0; + double part2 = -2.0 * cubed + 3.0 * squared; + double part3 = cubed - 2.0 * squared + amount; + double part4 = cubed - squared; + result.X = value1.X * part1 + value2.X * part2 + tangent1.X * part3 + tangent2.X * part4; + result.Y = value1.Y * part1 + value2.Y * part2 + tangent1.Y * part3 + tangent2.Y * part4; + result.Z = value1.Z * part1 + value2.Z * part2 + tangent1.Z * part3 + tangent2.Z * part4; + } + + /// + /// Performs a Hermite spline interpolation. + /// + /// First source position vector. + /// First source tangent vector. + /// Second source position vector. + /// Second source tangent vector. + /// Weighting factor. + /// The result of the Hermite spline interpolation. + public static Double3 Hermite(Double3 value1, Double3 tangent1, Double3 value2, Double3 tangent2, double amount) + { + Hermite(ref value1, ref tangent1, ref value2, ref tangent2, amount, out var result); + return result; + } + + /// + /// Performs a Catmull-Rom interpolation using the specified positions. + /// + /// The first position in the interpolation. + /// The second position in the interpolation. + /// The third position in the interpolation. + /// The fourth position in the interpolation. + /// Weighting factor. + /// When the method completes, contains the result of the Catmull-Rom interpolation. + public static void CatmullRom(ref Double3 value1, ref Double3 value2, ref Double3 value3, ref Double3 value4, double amount, out Double3 result) + { + double squared = amount * amount; + double cubed = amount * squared; + result.X = 0.5f * (2.0 * value2.X + (-value1.X + value3.X) * amount + + (2.0 * value1.X - 5.0 * value2.X + 4.0 * value3.X - value4.X) * squared + + (-value1.X + 3.0 * value2.X - 3.0 * value3.X + value4.X) * cubed); + result.Y = 0.5f * (2.0 * value2.Y + (-value1.Y + value3.Y) * amount + + (2.0 * value1.Y - 5.0 * value2.Y + 4.0 * value3.Y - value4.Y) * squared + + (-value1.Y + 3.0 * value2.Y - 3.0 * value3.Y + value4.Y) * cubed); + result.Z = 0.5f * (2.0 * value2.Z + (-value1.Z + value3.Z) * amount + + (2.0 * value1.Z - 5.0 * value2.Z + 4.0 * value3.Z - value4.Z) * squared + + (-value1.Z + 3.0 * value2.Z - 3.0 * value3.Z + value4.Z) * cubed); + } + + /// + /// Performs a Catmull-Rom interpolation using the specified positions. + /// + /// The first position in the interpolation. + /// The second position in the interpolation. + /// The third position in the interpolation. + /// The fourth position in the interpolation. + /// Weighting factor. + /// A vector that is the result of the Catmull-Rom interpolation. + public static Double3 CatmullRom(Double3 value1, Double3 value2, Double3 value3, Double3 value4, double amount) + { + CatmullRom(ref value1, ref value2, ref value3, ref value4, amount, out var result); + return result; + } + + /// + /// Returns a vector containing the largest components of the specified vectors. + /// + /// The first source vector. + /// The second source vector. + /// When the method completes, contains an new vector composed of the largest components of the source vectors. + public static void Max(ref Double3 left, ref Double3 right, out Double3 result) + { + result.X = left.X > right.X ? left.X : right.X; + result.Y = left.Y > right.Y ? left.Y : right.Y; + result.Z = left.Z > right.Z ? left.Z : right.Z; + } + + /// + /// Returns a vector containing the largest components of the specified vectors. + /// + /// The first source vector. + /// The second source vector. + /// A vector containing the largest components of the source vectors. + public static Double3 Max(Double3 left, Double3 right) + { + Max(ref left, ref right, out var result); + return result; + } + + /// + /// Returns a vector containing the smallest components of the specified vectors. + /// + /// The first source vector. + /// The second source vector. + /// When the method completes, contains an new vector composed of the smallest components of the source vectors. + public static void Min(ref Double3 left, ref Double3 right, out Double3 result) + { + result.X = left.X < right.X ? left.X : right.X; + result.Y = left.Y < right.Y ? left.Y : right.Y; + result.Z = left.Z < right.Z ? left.Z : right.Z; + } + + /// + /// Returns a vector containing the smallest components of the specified vectors. + /// + /// The first source vector. + /// The second source vector. + /// A vector containing the smallest components of the source vectors. + public static Double3 Min(Double3 left, Double3 right) + { + Min(ref left, ref right, out var result); + return result; + } + + /// + /// Returns the absolute value of a vector. + /// + /// The value. + /// A vector which components are less or equal to 0. + public static Double3 Abs(Double3 v) + { + return new Double3(Math.Abs(v.X), Math.Abs(v.Y), Math.Abs(v.Z)); + } + + /// + /// Projects a vector onto another vector. + /// + /// The vector to project. + /// The projection normal vector. + /// The projected vector. + public static Double3 Project(Double3 vector, Double3 onNormal) + { + double sqrMag = Dot(onNormal, onNormal); + if (sqrMag < Mathd.Epsilon) + return Zero; + return onNormal * Dot(vector, onNormal) / sqrMag; + } + + /// + /// Projects a vector onto a plane defined by a normal orthogonal to the plane. + /// + /// The vector to project. + /// The plane normal vector. + /// The projected vector. + public static Double3 ProjectOnPlane(Double3 vector, Double3 planeNormal) + { + return vector - Project(vector, planeNormal); + } + + /// + /// Calculates the angle (in degrees) between and . This is always the smallest value. + /// + /// The first vector. + /// The second vector. + /// The angle (in degrees). + public static double Angle(Double3 from, Double3 to) + { + double dot = Mathd.Clamp(Dot(from.Normalized, to.Normalized), -1.0, 1.0); + if (Math.Abs(dot) > (1 - Mathd.Epsilon)) + return dot > 0.0 ? 0.0 : 180.0; + return Mathd.Acos(dot) * Mathd.RadiansToDegrees; + } + + /// + /// Projects a 3D vector from object space into screen space. + /// + /// The vector to project. + /// The X position of the viewport. + /// The Y position of the viewport. + /// The width of the viewport. + /// The height of the viewport. + /// The minimum depth of the viewport. + /// The maximum depth of the viewport. + /// The combined world-view-projection matrix. + /// When the method completes, contains the vector in screen space. + public static void Project(ref Double3 vector, double x, double y, double width, double height, double minZ, double maxZ, ref Matrix worldViewProjection, out Double3 result) + { + TransformCoordinate(ref vector, ref worldViewProjection, out var v); + result = new Double3((1.0 + v.X) * 0.5f * width + x, (1.0 - v.Y) * 0.5f * height + y, v.Z * (maxZ - minZ) + minZ); + } + + /// + /// Projects a 3D vector from object space into screen space. + /// + /// The vector to project. + /// The X position of the viewport. + /// The Y position of the viewport. + /// The width of the viewport. + /// The height of the viewport. + /// The minimum depth of the viewport. + /// The maximum depth of the viewport. + /// The combined world-view-projection matrix. + /// The vector in screen space. + public static Double3 Project(Double3 vector, double x, double y, double width, double height, double minZ, double maxZ, Matrix worldViewProjection) + { + Project(ref vector, x, y, width, height, minZ, maxZ, ref worldViewProjection, out var result); + return result; + } + + /// + /// Projects a 3D vector from screen space into object space. + /// + /// The vector to project. + /// The X position of the viewport. + /// The Y position of the viewport. + /// The width of the viewport. + /// The height of the viewport. + /// The minimum depth of the viewport. + /// The maximum depth of the viewport. + /// The combined world-view-projection matrix. + /// When the method completes, contains the vector in object space. + public static void Unproject(ref Double3 vector, double x, double y, double width, double height, double minZ, double maxZ, ref Matrix worldViewProjection, out Double3 result) + { + Matrix.Invert(ref worldViewProjection, out var matrix); + var v = new Double3 + { + X = (vector.X - x) / width * 2.0 - 1.0, + Y = -((vector.Y - y) / height * 2.0 - 1.0), + Z = (vector.Z - minZ) / (maxZ - minZ) + }; + TransformCoordinate(ref v, ref matrix, out result); + } + + /// + /// Projects a 3D vector from screen space into object space. + /// + /// The vector to project. + /// The X position of the viewport. + /// The Y position of the viewport. + /// The width of the viewport. + /// The height of the viewport. + /// The minimum depth of the viewport. + /// The maximum depth of the viewport. + /// The combined world-view-projection matrix. + /// The vector in object space. + public static Double3 Unproject(Double3 vector, double x, double y, double width, double height, double minZ, double maxZ, Matrix worldViewProjection) + { + Unproject(ref vector, x, y, width, height, minZ, maxZ, ref worldViewProjection, out var result); + return result; + } + + /// + /// Returns the reflection of a vector off a surface that has the specified normal. + /// + /// The source vector. + /// Normal of the surface. + /// When the method completes, contains the reflected vector. + /// + /// Reflect only gives the direction of a reflection off a surface, it does not determine + /// whether the original vector was close enough to the surface to hit it. + /// + public static void Reflect(ref Double3 vector, ref Double3 normal, out Double3 result) + { + double dot = vector.X * normal.X + vector.Y * normal.Y + vector.Z * normal.Z; + result.X = vector.X - 2.0 * dot * normal.X; + result.Y = vector.Y - 2.0 * dot * normal.Y; + result.Z = vector.Z - 2.0 * dot * normal.Z; + } + + /// + /// Returns the reflection of a vector off a surface that has the specified normal. + /// + /// The source vector. + /// Normal of the surface. + /// The reflected vector. + /// Reflect only gives the direction of a reflection off a surface, it does not determine whether the original vector was close enough to the surface to hit it. + public static Double3 Reflect(Double3 vector, Double3 normal) + { + Reflect(ref vector, ref normal, out var result); + return result; + } + + /// + /// Orthogonalizes a list of vectors. + /// + /// The list of orthogonalized vectors. + /// The list of vectors to orthogonalize. + /// + /// + /// Orthogonalization is the process of making all vectors orthogonal to each other. This + /// means that any given vector in the list will be orthogonal to any other given vector in the + /// list. + /// + /// + /// Because this method uses the modified Gram-Schmidt process, the resulting vectors + /// tend to be numerically unstable. The numeric stability decreases according to the vectors + /// position in the list so that the first vector is the most stable and the last vector is the + /// least stable. + /// + /// + /// Thrown when or is null. + /// Thrown when is shorter in length than . + public static void Orthogonalize(Double3[] destination, params Double3[] source) + { + //Uses the modified Gram-Schmidt process. + //q1 = m1 + //q2 = m2 - ((q1 ⋅ m2) / (q1 ⋅ q1)) * q1 + //q3 = m3 - ((q1 ⋅ m3) / (q1 ⋅ q1)) * q1 - ((q2 ⋅ m3) / (q2 ⋅ q2)) * q2 + //q4 = m4 - ((q1 ⋅ m4) / (q1 ⋅ q1)) * q1 - ((q2 ⋅ m4) / (q2 ⋅ q2)) * q2 - ((q3 ⋅ m4) / (q3 ⋅ q3)) * q3 + //q5 = ... + if (source == null) + throw new ArgumentNullException(nameof(source)); + if (destination == null) + throw new ArgumentNullException(nameof(destination)); + if (destination.Length < source.Length) + throw new ArgumentOutOfRangeException(nameof(destination), "The destination array must be of same length or larger length than the source array."); + for (var i = 0; i < source.Length; ++i) + { + Double3 v = source[i]; + for (var r = 0; r < i; ++r) + v -= Dot(destination[r], v) / Dot(destination[r], destination[r]) * destination[r]; + destination[i] = v; + } + } + + /// + /// Orthonormalizes a list of vectors. + /// + /// The list of orthonormalized vectors. + /// The list of vectors to orthonormalize. + /// + /// + /// Orthonormalization is the process of making all vectors orthogonal to each + /// other and making all vectors of unit length. This means that any given vector will + /// be orthogonal to any other given vector in the list. + /// + /// + /// Because this method uses the modified Gram-Schmidt process, the resulting vectors + /// tend to be numerically unstable. The numeric stability decreases according to the vectors + /// position in the list so that the first vector is the most stable and the last vector is the + /// least stable. + /// + /// + /// Thrown when or is null. + /// Thrown when is shorter in length than . + public static void Orthonormalize(Double3[] destination, params Double3[] source) + { + //Uses the modified Gram-Schmidt process. + //Because we are making unit vectors, we can optimize the math for orthogonalization + //and simplify the projection operation to remove the division. + //q1 = m1 / |m1| + //q2 = (m2 - (q1 ⋅ m2) * q1) / |m2 - (q1 ⋅ m2) * q1| + //q3 = (m3 - (q1 ⋅ m3) * q1 - (q2 ⋅ m3) * q2) / |m3 - (q1 ⋅ m3) * q1 - (q2 ⋅ m3) * q2| + //q4 = (m4 - (q1 ⋅ m4) * q1 - (q2 ⋅ m4) * q2 - (q3 ⋅ m4) * q3) / |m4 - (q1 ⋅ m4) * q1 - (q2 ⋅ m4) * q2 - (q3 ⋅ m4) * q3| + //q5 = ... + if (source == null) + throw new ArgumentNullException(nameof(source)); + if (destination == null) + throw new ArgumentNullException(nameof(destination)); + if (destination.Length < source.Length) + throw new ArgumentOutOfRangeException(nameof(destination), "The destination array must be of same length or larger length than the source array."); + for (var i = 0; i < source.Length; ++i) + { + Double3 v = source[i]; + for (var r = 0; r < i; ++r) + v -= Dot(destination[r], v) * destination[r]; + v.Normalize(); + destination[i] = v; + } + } + + /// + /// Transforms a 3D vector by the given rotation. + /// + /// The vector to rotate. + /// The rotation to apply. + /// When the method completes, contains the transformed . + public static void Transform(ref Double3 vector, ref Quaternion rotation, out Double3 result) + { + double x = rotation.X + rotation.X; + double y = rotation.Y + rotation.Y; + double z = rotation.Z + rotation.Z; + double wx = rotation.W * x; + double wy = rotation.W * y; + double wz = rotation.W * z; + double xx = rotation.X * x; + double xy = rotation.X * y; + double xz = rotation.X * z; + double yy = rotation.Y * y; + double yz = rotation.Y * z; + double zz = rotation.Z * z; + result = new Double3(vector.X * (1.0 - yy - zz) + vector.Y * (xy - wz) + vector.Z * (xz + wy), + vector.X * (xy + wz) + vector.Y * (1.0 - xx - zz) + vector.Z * (yz - wx), + vector.X * (xz - wy) + vector.Y * (yz + wx) + vector.Z * (1.0 - xx - yy)); + } + + /// + /// Transforms a 3D vector by the given rotation. + /// + /// The vector to rotate. + /// The rotation to apply. + /// The transformed . + public static Double3 Transform(Double3 vector, Quaternion rotation) + { + Transform(ref vector, ref rotation, out var result); + return result; + } + + /// + /// Transforms an array of vectors by the given rotation. + /// + /// The array of vectors to transform. + /// The rotation to apply. + /// The array for which the transformed vectors are stored. This array may be the same array as . + /// Thrown when or is null. + /// Thrown when is shorter in length than . + public static void Transform(Double3[] source, ref Quaternion rotation, Double3[] destination) + { + if (source == null) + throw new ArgumentNullException(nameof(source)); + if (destination == null) + throw new ArgumentNullException(nameof(destination)); + if (destination.Length < source.Length) + throw new ArgumentOutOfRangeException(nameof(destination), "The destination array must be of same length or larger length than the source array."); + double x = rotation.X + rotation.X; + double y = rotation.Y + rotation.Y; + double z = rotation.Z + rotation.Z; + double wx = rotation.W * x; + double wy = rotation.W * y; + double wz = rotation.W * z; + double xx = rotation.X * x; + double xy = rotation.X * y; + double xz = rotation.X * z; + double yy = rotation.Y * y; + double yz = rotation.Y * z; + double zz = rotation.Z * z; + double num1 = 1.0 - yy - zz; + double num2 = xy - wz; + double num3 = xz + wy; + double num4 = xy + wz; + double num5 = 1.0 - xx - zz; + double num6 = yz - wx; + double num7 = xz - wy; + double num8 = yz + wx; + double num9 = 1.0 - xx - yy; + for (var i = 0; i < source.Length; i++) + { + destination[i] = new Double3(source[i].X * num1 + source[i].Y * num2 + source[i].Z * num3, + source[i].X * num4 + source[i].Y * num5 + source[i].Z * num6, + source[i].X * num7 + source[i].Y * num8 + source[i].Z * num9); + } + } + + /// + /// Transforms a 3D vector by the given . + /// + /// The source vector. + /// The transformation . + /// When the method completes, contains the transformed . + public static void Transform(ref Double3 vector, ref Matrix3x3 transform, out Double3 result) + { + result = new Double3((vector.X * transform.M11) + (vector.Y * transform.M21) + (vector.Z * transform.M31), + (vector.X * transform.M12) + (vector.Y * transform.M22) + (vector.Z * transform.M32), + (vector.X * transform.M13) + (vector.Y * transform.M23) + (vector.Z * transform.M33)); + } + + /// + /// Transforms a 3D vector by the given . + /// + /// The source vector. + /// The transformation . + /// The transformed . + public static Double3 Transform(Double3 vector, Matrix3x3 transform) + { + Transform(ref vector, ref transform, out var result); + return result; + } + + /// + /// Transforms a 3D vector by the given . + /// + /// The source vector. + /// The transformation . + /// When the method completes, contains the transformed . + public static void Transform(ref Double3 vector, ref Matrix transform, out Double3 result) + { + result = new Double3(vector.X * transform.M11 + vector.Y * transform.M21 + vector.Z * transform.M31 + transform.M41, + vector.X * transform.M12 + vector.Y * transform.M22 + vector.Z * transform.M32 + transform.M42, + vector.X * transform.M13 + vector.Y * transform.M23 + vector.Z * transform.M33 + transform.M43); + } + + /// + /// Transforms a 3D vector by the given . + /// + /// The source vector. + /// The transformation . + /// When the method completes, contains the transformed . + public static void Transform(ref Double3 vector, ref Matrix transform, out Double4 result) + { + result = new Double4(vector.X * transform.M11 + vector.Y * transform.M21 + vector.Z * transform.M31 + transform.M41, + vector.X * transform.M12 + vector.Y * transform.M22 + vector.Z * transform.M32 + transform.M42, + vector.X * transform.M13 + vector.Y * transform.M23 + vector.Z * transform.M33 + transform.M43, + vector.X * transform.M14 + vector.Y * transform.M24 + vector.Z * transform.M34 + transform.M44); + } + + /// + /// Transforms a 3D vector by the given . + /// + /// The source vector. + /// The transformation . + /// The transformed . + public static Double3 Transform(Double3 vector, Matrix transform) + { + Transform(ref vector, ref transform, out Double3 result); + return result; + } + + /// + /// Transforms an array of 3D vectors by the given . + /// + /// The array of vectors to transform. + /// The transformation . + /// The array for which the transformed vectors are stored. + /// Thrown when or is null. + /// Thrown when is shorter in length than . + public static void Transform(Double3[] source, ref Matrix transform, Double4[] destination) + { + if (source == null) + throw new ArgumentNullException(nameof(source)); + if (destination == null) + throw new ArgumentNullException(nameof(destination)); + if (destination.Length < source.Length) + throw new ArgumentOutOfRangeException(nameof(destination), "The destination array must be of same length or larger length than the source array."); + for (var i = 0; i < source.Length; ++i) + Transform(ref source[i], ref transform, out destination[i]); + } + + /// + /// Performs a coordinate transformation using the given . + /// + /// The coordinate vector to transform. + /// The transformation . + /// When the method completes, contains the transformed coordinates. + /// + /// A coordinate transform performs the transformation with the assumption that the w component + /// is one. The four dimensional vector obtained from the transformation operation has each + /// component in the vector divided by the w component. This forces the w component to be one and + /// therefore makes the vector homogeneous. The homogeneous vector is often preferred when working + /// with coordinates as the w component can safely be ignored. + /// + public static void TransformCoordinate(ref Double3 coordinate, ref Matrix transform, out Double3 result) + { + var vector = new Double4 + { + X = coordinate.X * transform.M11 + coordinate.Y * transform.M21 + coordinate.Z * transform.M31 + transform.M41, + Y = coordinate.X * transform.M12 + coordinate.Y * transform.M22 + coordinate.Z * transform.M32 + transform.M42, + Z = coordinate.X * transform.M13 + coordinate.Y * transform.M23 + coordinate.Z * transform.M33 + transform.M43, + W = 1f / (coordinate.X * transform.M14 + coordinate.Y * transform.M24 + coordinate.Z * transform.M34 + transform.M44) + }; + result = new Double3(vector.X * vector.W, vector.Y * vector.W, vector.Z * vector.W); + } + + /// + /// Performs a coordinate transformation using the given . + /// + /// The coordinate vector to transform. + /// The transformation . + /// The transformed coordinates. + /// + /// A coordinate transform performs the transformation with the assumption that the w component + /// is one. The four dimensional vector obtained from the transformation operation has each + /// component in the vector divided by the w component. This forces the w component to be one and + /// therefore makes the vector homogeneous. The homogeneous vector is often preferred when working + /// with coordinates as the w component can safely be ignored. + /// + public static Double3 TransformCoordinate(Double3 coordinate, Matrix transform) + { + TransformCoordinate(ref coordinate, ref transform, out var result); + return result; + } + + /// + /// Performs a coordinate transformation on an array of vectors using the given . + /// + /// The array of coordinate vectors to transform. + /// The transformation . + /// The array for which the transformed vectors are stored. This array may be the same array as . + /// Thrown when or is null. + /// Thrown when is shorter in length than + /// . + /// + /// + /// A coordinate transform performs the transformation with the assumption that the w component + /// is one. The four dimensional vector obtained from the transformation operation has each + /// component in the vector divided by the w component. This forces the w component to be one and + /// therefore makes the vector homogeneous. The homogeneous vector is often preferred when working + /// with coordinates as the w component can safely be ignored. + /// + public static void TransformCoordinate(Double3[] source, ref Matrix transform, Double3[] destination) + { + if (source == null) + throw new ArgumentNullException(nameof(source)); + if (destination == null) + throw new ArgumentNullException(nameof(destination)); + if (destination.Length < source.Length) + throw new ArgumentOutOfRangeException(nameof(destination), "The destination array must be of same length or larger length than the source array."); + for (var i = 0; i < source.Length; i++) + TransformCoordinate(ref source[i], ref transform, out destination[i]); + } + + /// + /// Performs a normal transformation using the given . + /// + /// The normal vector to transform. + /// The transformation . + /// When the method completes, contains the transformed normal. + /// + /// A normal transform performs the transformation with the assumption that the w component + /// is zero. This causes the fourth row and fourth column of the matrix to be unused. The + /// end result is a vector that is not translated, but all other transformation properties + /// apply. This is often preferred for normal vectors as normals purely represent direction + /// rather than location because normal vectors should not be translated. + /// + public static void TransformNormal(ref Double3 normal, ref Matrix transform, out Double3 result) + { + result = new Double3(normal.X * transform.M11 + normal.Y * transform.M21 + normal.Z * transform.M31, + normal.X * transform.M12 + normal.Y * transform.M22 + normal.Z * transform.M32, + normal.X * transform.M13 + normal.Y * transform.M23 + normal.Z * transform.M33); + } + + /// + /// Performs a normal transformation using the given . + /// + /// The normal vector to transform. + /// The transformation . + /// The transformed normal. + /// + /// A normal transform performs the transformation with the assumption that the w component + /// is zero. This causes the fourth row and fourth column of the matrix to be unused. The + /// end result is a vector that is not translated, but all other transformation properties + /// apply. This is often preferred for normal vectors as normals purely represent direction + /// rather than location because normal vectors should not be translated. + /// + public static Double3 TransformNormal(Double3 normal, Matrix transform) + { + TransformNormal(ref normal, ref transform, out var result); + return result; + } + + /// + /// Performs a normal transformation on an array of vectors using the given . + /// + /// The array of normal vectors to transform. + /// The transformation . + /// The array for which the transformed vectors are stored. This array may be the same array as . + /// Thrown when or is null. + /// Thrown when is shorter in length than . + /// + /// A normal transform performs the transformation with the assumption that the w component + /// is zero. This causes the fourth row and fourth column of the matrix to be unused. The + /// end result is a vector that is not translated, but all other transformation properties + /// apply. This is often preferred for normal vectors as normals purely represent direction + /// rather than location because normal vectors should not be translated. + /// + public static void TransformNormal(Double3[] source, ref Matrix transform, Double3[] destination) + { + if (source == null) + throw new ArgumentNullException(nameof(source)); + if (destination == null) + throw new ArgumentNullException(nameof(destination)); + if (destination.Length < source.Length) + throw new ArgumentOutOfRangeException(nameof(destination), "The destination array must be of same length or larger length than the source array."); + for (var i = 0; i < source.Length; ++i) + TransformNormal(ref source[i], ref transform, out destination[i]); + } + + /// + /// Adds two vectors. + /// + /// The first vector to add. + /// The second vector to add. + /// The sum of the two vectors. + public static Double3 operator +(Double3 left, Double3 right) + { + return new Double3(left.X + right.X, left.Y + right.Y, left.Z + right.Z); + } + + /// + /// Multiplies a vector with another by performing component-wise multiplication equivalent to . + /// + /// The first vector to multiply. + /// The second vector to multiply. + /// The multiplication of the two vectors. + public static Double3 operator *(Double3 left, Double3 right) + { + return new Double3(left.X * right.X, left.Y * right.Y, left.Z * right.Z); + } + + /// + /// Assert a vector (return it unchanged). + /// + /// The vector to assert (unchanged). + /// The asserted (unchanged) vector. + public static Double3 operator +(Double3 value) + { + return value; + } + + /// + /// Subtracts two vectors. + /// + /// The first vector to subtract. + /// The second vector to subtract. + /// The difference of the two vectors. + public static Double3 operator -(Double3 left, Double3 right) + { + return new Double3(left.X - right.X, left.Y - right.Y, left.Z - right.Z); + } + + /// + /// Reverses the direction of a given vector. + /// + /// The vector to negate. + /// A vector facing in the opposite direction. + public static Double3 operator -(Double3 value) + { + return new Double3(-value.X, -value.Y, -value.Z); + } + + /// + /// Transforms a vector by the given rotation. + /// + /// The vector to transform. + /// The quaternion. + /// The scaled vector. + public static Double3 operator *(Double3 vector, Quaternion rotation) + { + Transform(ref vector, ref rotation, out var result); + return result; + } + + /// + /// Scales a vector by the given value. + /// + /// The vector to scale. + /// The amount by which to scale the vector. + /// The scaled vector. + public static Double3 operator *(double scale, Double3 value) + { + return new Double3(value.X * scale, value.Y * scale, value.Z * scale); + } + + /// + /// Scales a vector by the given value. + /// + /// The vector to scale. + /// The amount by which to scale the vector. + /// The scaled vector. + public static Double3 operator *(Double3 value, double scale) + { + return new Double3(value.X * scale, value.Y * scale, value.Z * scale); + } + + /// + /// Scales a vector by the given value. + /// + /// The vector to scale. + /// The amount by which to scale the vector. + /// The scaled vector. + public static Double3 operator /(Double3 value, double scale) + { + return new Double3(value.X / scale, value.Y / scale, value.Z / scale); + } + + /// + /// Scales a vector by the given value. + /// + /// The amount by which to scale the vector. + /// The vector to scale. + /// The scaled vector. + public static Double3 operator /(double scale, Double3 value) + { + return new Double3(scale / value.X, scale / value.Y, scale / value.Z); + } + + /// + /// Scales a vector by the given value. + /// + /// The vector to scale. + /// The amount by which to scale the vector. + /// The scaled vector. + public static Double3 operator /(Double3 value, Double3 scale) + { + return new Double3(value.X / scale.X, value.Y / scale.Y, value.Z / scale.Z); + } + + /// + /// Remainder of value divided by scale. + /// + /// The vector to scale. + /// The amount by which to scale the vector. + /// The remained vector. + public static Double3 operator %(Double3 value, double scale) + { + return new Double3(value.X % scale, value.Y % scale, value.Z % scale); + } + + /// + /// Remainder of value divided by scale. + /// + /// The amount by which to scale the vector. + /// The vector to scale. + /// The remained vector. + public static Double3 operator %(double value, Double3 scale) + { + return new Double3(value % scale.X, value % scale.Y, value % scale.Z); + } + + /// + /// Remainder of value divided by scale. + /// + /// The vector to scale. + /// The amount by which to scale the vector. + /// The remained vector. + public static Double3 operator %(Double3 value, Double3 scale) + { + return new Double3(value.X % scale.X, value.Y % scale.Y, value.Z % scale.Z); + } + + /// + /// Performs a component-wise addition. + /// + /// The input vector. + /// The scalar value to be added on elements + /// The vector with added scalar for each element. + public static Double3 operator +(Double3 value, double scalar) + { + return new Double3(value.X + scalar, value.Y + scalar, value.Z + scalar); + } + + /// + /// Performs a component-wise addition. + /// + /// The input vector. + /// The scalar value to be added on elements + /// The vector with added scalar for each element. + public static Double3 operator +(double scalar, Double3 value) + { + return new Double3(scalar + value.X, scalar + value.Y, scalar + value.Z); + } + + /// + /// Performs a component-wise subtraction. + /// + /// The input vector. + /// The scalar value to be subtracted from elements + /// The vector with added scalar from each element. + public static Double3 operator -(Double3 value, double scalar) + { + return new Double3(value.X - scalar, value.Y - scalar, value.Z - scalar); + } + + /// + /// Performs a component-wise subtraction. + /// + /// The input vector. + /// The scalar value to be subtracted from elements + /// The vector with subtracted scalar from each element. + public static Double3 operator -(double scalar, Double3 value) + { + return new Double3(scalar - value.X, scalar - value.Y, scalar - value.Z); + } + + /// + /// Tests for equality between two objects. + /// + /// The first value to compare. + /// The second value to compare. + /// true if has the same value as ; otherwise, false. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool operator ==(Double3 left, Double3 right) + { + return Mathd.NearEqual(left.X, right.X) && Mathd.NearEqual(left.Y, right.Y) && Mathd.NearEqual(left.Z, right.Z); + } + + /// + /// Tests for inequality between two objects. + /// + /// The first value to compare. + /// The second value to compare. + /// true if has a different value than ; otherwise, false. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool operator !=(Double3 left, Double3 right) + { + return !Mathd.NearEqual(left.X, right.X) || !Mathd.NearEqual(left.Y, right.Y) || !Mathd.NearEqual(left.Z, right.Z); + } + + /// + /// Performs an explicit conversion from to . + /// + /// The value. + /// The result of the conversion. + public static explicit operator Double2(Double3 value) + { + return new Double2(value.X, value.Y); + } + + /// + /// Performs an explicit conversion from to . + /// + /// The value. + /// The result of the conversion. + public static explicit operator Double4(Double3 value) + { + return new Double4(value, 0.0); + } + + /// + /// Returns a that represents this instance. + /// + /// A that represents this instance. + public override string ToString() + { + return string.Format(CultureInfo.CurrentCulture, _formatString, X, Y, Z); + } + + /// + /// Returns a that represents this instance. + /// + /// The format. + /// A that represents this instance. + public string ToString(string format) + { + if (format == null) + return ToString(); + return string.Format(CultureInfo.CurrentCulture, _formatString, X.ToString(format, CultureInfo.CurrentCulture), Y.ToString(format, CultureInfo.CurrentCulture), Z.ToString(format, CultureInfo.CurrentCulture)); + } + + /// + /// Returns a that represents this instance. + /// + /// The format provider. + /// A that represents this instance. + public string ToString(IFormatProvider formatProvider) + { + return string.Format(formatProvider, _formatString, X, Y, Z); + } + + /// + /// Returns a that represents this instance. + /// + /// The format. + /// The format provider. + /// A that represents this instance. + public string ToString(string format, IFormatProvider formatProvider) + { + if (format == null) + return ToString(formatProvider); + return string.Format(formatProvider, "X:{0} Y:{1} Z:{2}", X.ToString(format, formatProvider), Y.ToString(format, formatProvider), Z.ToString(format, formatProvider)); + } + + /// + /// Returns a hash code for this instance. + /// + public override int GetHashCode() + { + unchecked + { + int hashCode = X.GetHashCode(); + hashCode = (hashCode * 397) ^ Y.GetHashCode(); + hashCode = (hashCode * 397) ^ Z.GetHashCode(); + return hashCode; + } + } + + /// + /// Determines whether the specified is equal to this instance. + /// + /// The to compare with this instance. + /// true if the specified is equal to this instance; otherwise, false. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public bool Equals(ref Double3 other) + { + return Mathd.NearEqual(other.X, X) && Mathd.NearEqual(other.Y, Y) && Mathd.NearEqual(other.Z, Z); + } + + /// + /// Determines whether the specified is equal to this instance. + /// + /// The to compare with this instance. + /// true if the specified is equal to this instance; otherwise, false. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public bool Equals(Double3 other) + { + return Mathd.NearEqual(other.X, X) && Mathd.NearEqual(other.Y, Y) && Mathd.NearEqual(other.Z, Z); + } + + /// + /// Determines whether the specified is equal to this instance. + /// + /// The to compare with this instance. + /// true if the specified is equal to this instance; otherwise, false. + public override bool Equals(object value) + { + if (!(value is Double3 other)) + return false; + return Mathd.NearEqual(other.X, X) && Mathd.NearEqual(other.Y, Y) && Mathd.NearEqual(other.Z, Z); + } + } +} diff --git a/Source/Engine/Core/Math/Double4.cs b/Source/Engine/Core/Math/Double4.cs new file mode 100644 index 000000000..8157f3ea0 --- /dev/null +++ b/Source/Engine/Core/Math/Double4.cs @@ -0,0 +1,1534 @@ +// Copyright (c) 2012-2022 Wojciech Figat. All rights reserved. + +// ----------------------------------------------------------------------------- +// Original code from SharpDX project. https://github.com/sharpdx/SharpDX/ +// Greetings to Alexandre Mutel. Original code published with the following license: +// ----------------------------------------------------------------------------- +// Copyright (c) 2010-2014 SharpDX - Alexandre Mutel +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +// ----------------------------------------------------------------------------- +// Original code from SlimMath project. http://code.google.com/p/slimmath/ +// Greetings to SlimDX Group. Original code published with the following license: +// ----------------------------------------------------------------------------- +/* +* Copyright (c) 2007-2011 SlimDX Group +* +* Permission is hereby granted, free of charge, to any person obtaining a copy +* of this software and associated documentation files (the "Software"), to deal +* in the Software without restriction, including without limitation the rights +* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +* copies of the Software, and to permit persons to whom the Software is +* furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice shall be included in +* all copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +* THE SOFTWARE. +*/ + +using System; +using System.ComponentModel; +using System.Globalization; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +namespace FlaxEngine +{ + [Serializable] + [TypeConverter(typeof(TypeConverters.Double4Converter))] + partial struct Double4 : IEquatable, IFormattable + { + private static readonly string _formatString = "X:{0:F2} Y:{1:F2} Z:{2:F2} W:{3:F2}"; + + /// + /// The size of the type, in bytes. + /// + public static readonly int SizeInBytes = Marshal.SizeOf(typeof(Double4)); + + /// + /// A with all of its components set to zero. + /// + public static readonly Double4 Zero; + + /// + /// The X unit (1, 0, 0, 0). + /// + public static readonly Double4 UnitX = new Double4(1.0, 0.0, 0.0, 0.0); + + /// + /// The Y unit (0, 1, 0, 0). + /// + public static readonly Double4 UnitY = new Double4(0.0, 1.0, 0.0, 0.0); + + /// + /// The Z unit (0, 0, 1, 0). + /// + public static readonly Double4 UnitZ = new Double4(0.0, 0.0, 1.0, 0.0); + + /// + /// The W unit (0, 0, 0, 1). + /// + public static readonly Double4 UnitW = new Double4(0.0, 0.0, 0.0, 1.0); + + /// + /// A with all of its components set to half. + /// + public static readonly Double4 Half = new Double4(0.5f, 0.5f, 0.5f, 0.5f); + + /// + /// A with all of its components set to one. + /// + public static readonly Double4 One = new Double4(1.0, 1.0, 1.0, 1.0); + + /// + /// A with all components equal to . + /// + public static readonly Double4 Minimum = new Double4(double.MinValue); + + /// + /// A with all components equal to . + /// + public static readonly Double4 Maximum = new Double4(double.MaxValue); + + /// + /// Initializes a new instance of the struct. + /// + /// The value that will be assigned to all components. + public Double4(double value) + { + X = value; + Y = value; + Z = value; + W = value; + } + + /// + /// Initializes a new instance of the struct. + /// + /// Initial value for the X component of the vector. + /// Initial value for the Y component of the vector. + /// Initial value for the Z component of the vector. + /// Initial value for the W component of the vector. + public Double4(double x, double y, double z, double w) + { + X = x; + Y = y; + Z = z; + W = w; + } + + /// + /// Initializes a new instance of the struct. + /// + /// A vector containing the values with which to initialize the X, Y, Z, and W components. + public Double4(Vector4 value) + { + X = value.X; + Y = value.Y; + Z = value.Z; + W = value.W; + } + + /// + /// Initializes a new instance of the struct. + /// + /// A vector containing the values with which to initialize the X, Y, and Z components. + /// Initial value for the W component of the vector. + public Double4(Double3 value, double w) + { + X = value.X; + Y = value.Y; + Z = value.Z; + W = w; + } + + /// + /// Initializes a new instance of the struct. + /// + /// A vector containing the values with which to initialize the X and Y components. + /// A vector containing the values with which to initialize the Z and W components. + public Double4(Double2 xy, Double2 zw) + { + X = xy.X; + Y = xy.Y; + Z = zw.X; + W = zw.Y; + } + + /// + /// Initializes a new instance of the struct. + /// + /// A vector containing the values with which to initialize the X and Y components. + /// Initial value for the Z component of the vector. + /// Initial value for the W component of the vector. + public Double4(Double2 value, double z, double w) + { + X = value.X; + Y = value.Y; + Z = z; + W = w; + } + + /// + /// Initializes a new instance of the struct. + /// + /// The values to assign to the X, Y, Z, and W components of the vector. This must be an array with four elements. + /// Thrown when is null. + /// Thrown when contains more or less than four elements. + public Double4(double[] values) + { + if (values == null) + throw new ArgumentNullException(nameof(values)); + if (values.Length != 4) + throw new ArgumentOutOfRangeException(nameof(values), "There must be four and only four input values for Double4."); + X = values[0]; + Y = values[1]; + Z = values[2]; + W = values[3]; + } + + /// + /// Gets a value indicting whether this instance is normalized. + /// + public bool IsNormalized => Mathd.IsOne(X * X + Y * Y + Z * Z + W * W); + + /// + /// Gets a value indicting whether this vector is zero + /// + public bool IsZero => Mathd.IsZero(X) && Mathd.IsZero(Y) && Mathd.IsZero(Z) && Mathd.IsZero(W); + + /// + /// Gets a value indicting whether this vector is one + /// + public bool IsOne => Mathd.IsOne(X) && Mathd.IsOne(Y) && Mathd.IsOne(Z) && Mathd.IsOne(W); + + /// + /// Gets a minimum component value + /// + public double MinValue => Mathd.Min(X, Mathd.Min(Y, Mathd.Min(Z, W))); + + /// + /// Gets a maximum component value + /// + public double MaxValue => Mathd.Max(X, Mathd.Max(Y, Mathd.Max(Z, W))); + + /// + /// Gets an arithmetic average value of all vector components. + /// + public double AvgValue => (X + Y + Z + W) * (1.0 / 4.0); + + /// + /// Gets a sum of the component values. + /// + public double ValuesSum => X + Y + Z + W; + + /// + /// Gets a vector with values being absolute values of that vector. + /// + public Double4 Absolute => new Double4(Math.Abs(X), Math.Abs(Y), Math.Abs(Z), Math.Abs(W)); + + /// + /// Gets a vector with values being opposite to values of that vector. + /// + public Double4 Negative => new Double4(-X, -Y, -Z, -W); + + /// + /// Gets or sets the component at the specified index. + /// + /// The value of the X, Y, Z, or W component, depending on the index. + /// The index of the component to access. Use 0 for the X component, 1 for the Y component, 2 for the Z component, and 3 for the W component. + /// The value of the component at the specified index. + /// Thrown when the is out of the range [0,3]. + public double this[int index] + { + get + { + switch (index) + { + case 0: return X; + case 1: return Y; + case 2: return Z; + case 3: return W; + } + throw new ArgumentOutOfRangeException(nameof(index), "Indices for Double4 run from 0 to 3, inclusive."); + } + set + { + switch (index) + { + case 0: + X = value; + break; + case 1: + Y = value; + break; + case 2: + Z = value; + break; + case 3: + W = value; + break; + default: throw new ArgumentOutOfRangeException(nameof(index), "Indices for Double4 run from 0 to 3, inclusive."); + } + } + } + + /// + /// Calculates the length of the vector. + /// + /// The length of the vector. + /// may be preferred when only the relative length is needed and speed is of the essence. + public double Length => Math.Sqrt(X * X + Y * Y + Z * Z + W * W); + + /// + /// Calculates the squared length of the vector. + /// + /// The squared length of the vector. + /// This method may be preferred to when only a relative length is needed and speed is of the essence. + public double LengthSquared => X * X + Y * Y + Z * Z + W * W; + + /// + /// Converts the vector into a unit vector. + /// + public void Normalize() + { + double length = Length; + if (!Mathd.IsZero(length)) + { + double inverse = 1.0 / length; + X *= inverse; + Y *= inverse; + Z *= inverse; + W *= inverse; + } + } + + /// + /// Creates an array containing the elements of the vector. + /// + /// A four-element array containing the components of the vector. + public double[] ToArray() + { + return new[] { X, Y, Z, W }; + } + + /// + /// Adds two vectors. + /// + /// The first vector to add. + /// The second vector to add. + /// When the method completes, contains the sum of the two vectors. + public static void Add(ref Double4 left, ref Double4 right, out Double4 result) + { + result = new Double4(left.X + right.X, left.Y + right.Y, left.Z + right.Z, left.W + right.W); + } + + /// + /// Adds two vectors. + /// + /// The first vector to add. + /// The second vector to add. + /// The sum of the two vectors. + public static Double4 Add(Double4 left, Double4 right) + { + return new Double4(left.X + right.X, left.Y + right.Y, left.Z + right.Z, left.W + right.W); + } + + /// + /// Performs a component-wise addition. + /// + /// The input vector + /// The scalar value to be added to elements + /// The vector with added scalar for each element. + public static void Add(ref Double4 left, ref double right, out Double4 result) + { + result = new Double4(left.X + right, left.Y + right, left.Z + right, left.W + right); + } + + /// + /// Performs a component-wise addition. + /// + /// The input vector + /// The scalar value to be added to elements + /// The vector with added scalar for each element. + public static Double4 Add(Double4 left, double right) + { + return new Double4(left.X + right, left.Y + right, left.Z + right, left.W + right); + } + + /// + /// Subtracts two vectors. + /// + /// The first vector to subtract. + /// The second vector to subtract. + /// When the method completes, contains the difference of the two vectors. + public static void Subtract(ref Double4 left, ref Double4 right, out Double4 result) + { + result = new Double4(left.X - right.X, left.Y - right.Y, left.Z - right.Z, left.W - right.W); + } + + /// + /// Subtracts two vectors. + /// + /// The first vector to subtract. + /// The second vector to subtract. + /// The difference of the two vectors. + public static Double4 Subtract(Double4 left, Double4 right) + { + return new Double4(left.X - right.X, left.Y - right.Y, left.Z - right.Z, left.W - right.W); + } + + /// + /// Performs a component-wise subtraction. + /// + /// The input vector + /// The scalar value to be subtracted from elements + /// The vector with subtracted scalar for each element. + public static void Subtract(ref Double4 left, ref double right, out Double4 result) + { + result = new Double4(left.X - right, left.Y - right, left.Z - right, left.W - right); + } + + /// + /// Performs a component-wise subtraction. + /// + /// The input vector + /// The scalar value to be subtracted from elements + /// The vector with subtracted scalar for each element. + public static Double4 Subtract(Double4 left, double right) + { + return new Double4(left.X - right, left.Y - right, left.Z - right, left.W - right); + } + + /// + /// Performs a component-wise subtraction. + /// + /// The scalar value to be subtracted from elements + /// The input vector. + /// The vector with subtracted scalar for each element. + public static void Subtract(ref double left, ref Double4 right, out Double4 result) + { + result = new Double4(left - right.X, left - right.Y, left - right.Z, left - right.W); + } + + /// + /// Performs a component-wise subtraction. + /// + /// The scalar value to be subtracted from elements + /// The input vector. + /// The vector with subtracted scalar for each element. + public static Double4 Subtract(double left, Double4 right) + { + return new Double4(left - right.X, left - right.Y, left - right.Z, left - right.W); + } + + /// + /// Scales a vector by the given value. + /// + /// The vector to scale. + /// The amount by which to scale the vector. + /// When the method completes, contains the scaled vector. + public static void Multiply(ref Double4 value, double scale, out Double4 result) + { + result = new Double4(value.X * scale, value.Y * scale, value.Z * scale, value.W * scale); + } + + /// + /// Scales a vector by the given value. + /// + /// The vector to scale. + /// The amount by which to scale the vector. + /// The scaled vector. + public static Double4 Multiply(Double4 value, double scale) + { + return new Double4(value.X * scale, value.Y * scale, value.Z * scale, value.W * scale); + } + + /// + /// Multiplies a vector with another by performing component-wise multiplication. + /// + /// The first vector to multiply. + /// The second vector to multiply. + /// When the method completes, contains the multiplied vector. + public static void Multiply(ref Double4 left, ref Double4 right, out Double4 result) + { + result = new Double4(left.X * right.X, left.Y * right.Y, left.Z * right.Z, left.W * right.W); + } + + /// + /// Multiplies a vector with another by performing component-wise multiplication. + /// + /// The first vector to multiply. + /// The second vector to multiply. + /// The multiplied vector. + public static Double4 Multiply(Double4 left, Double4 right) + { + return new Double4(left.X * right.X, left.Y * right.Y, left.Z * right.Z, left.W * right.W); + } + + /// + /// Scales a vector by the given value. + /// + /// The vector to scale. + /// The amount by which to scale the vector. + /// When the method completes, contains the scaled vector. + public static void Divide(ref Double4 value, double scale, out Double4 result) + { + result = new Double4(value.X / scale, value.Y / scale, value.Z / scale, value.W / scale); + } + + /// + /// Scales a vector by the given value. + /// + /// The vector to scale. + /// The amount by which to scale the vector. + /// The scaled vector. + public static Double4 Divide(Double4 value, double scale) + { + return new Double4(value.X / scale, value.Y / scale, value.Z / scale, value.W / scale); + } + + /// + /// Scales a vector by the given value. + /// + /// The amount by which to scale the vector. + /// The vector to scale. + /// When the method completes, contains the scaled vector. + public static void Divide(double scale, ref Double4 value, out Double4 result) + { + result = new Double4(scale / value.X, scale / value.Y, scale / value.Z, scale / value.W); + } + + /// + /// Scales a vector by the given value. + /// + /// The vector to scale. + /// The amount by which to scale the vector. + /// The scaled vector. + public static Double4 Divide(double scale, Double4 value) + { + return new Double4(scale / value.X, scale / value.Y, scale / value.Z, scale / value.W); + } + + /// + /// Reverses the direction of a given vector. + /// + /// The vector to negate. + /// When the method completes, contains a vector facing in the opposite direction. + public static void Negate(ref Double4 value, out Double4 result) + { + result = new Double4(-value.X, -value.Y, -value.Z, -value.W); + } + + /// + /// Reverses the direction of a given vector. + /// + /// The vector to negate. + /// A vector facing in the opposite direction. + public static Double4 Negate(Double4 value) + { + return new Double4(-value.X, -value.Y, -value.Z, -value.W); + } + + /// + /// Returns a containing the 4D Cartesian coordinates of a point specified in Barycentric coordinates relative to a 4D triangle. + /// + /// A containing the 4D Cartesian coordinates of vertex 1 of the triangle. + /// A containing the 4D Cartesian coordinates of vertex 2 of the triangle. + /// A containing the 4D Cartesian coordinates of vertex 3 of the triangle. + /// Barycentric coordinate b2, which expresses the weighting factor toward vertex 2 (specified in ). + /// Barycentric coordinate b3, which expresses the weighting factor toward vertex 3 (specified in ). + /// When the method completes, contains the 4D Cartesian coordinates of the specified point. + public static void Barycentric(ref Double4 value1, ref Double4 value2, ref Double4 value3, double amount1, double amount2, out Double4 result) + { + result = new Double4(value1.X + amount1 * (value2.X - value1.X) + amount2 * (value3.X - value1.X), + value1.Y + amount1 * (value2.Y - value1.Y) + amount2 * (value3.Y - value1.Y), + value1.Z + amount1 * (value2.Z - value1.Z) + amount2 * (value3.Z - value1.Z), + value1.W + amount1 * (value2.W - value1.W) + amount2 * (value3.W - value1.W)); + } + + /// + /// Returns a containing the 4D Cartesian coordinates of a point specified in Barycentric coordinates relative to a 4D triangle. + /// + /// A containing the 4D Cartesian coordinates of vertex 1 of the triangle. + /// A containing the 4D Cartesian coordinates of vertex 2 of the triangle. + /// A containing the 4D Cartesian coordinates of vertex 3 of the triangle. + /// Barycentric coordinate b2, which expresses the weighting factor toward vertex 2 (specified in ). + /// Barycentric coordinate b3, which expresses the weighting factor toward vertex 3 (specified in ). + /// A new containing the 4D Cartesian coordinates of the specified point. + public static Double4 Barycentric(Double4 value1, Double4 value2, Double4 value3, double amount1, double amount2) + { + Barycentric(ref value1, ref value2, ref value3, amount1, amount2, out Double4 result); + return result; + } + + /// + /// Restricts a value to be within a specified range. + /// + /// The value to clamp. + /// The minimum value. + /// The maximum value. + /// When the method completes, contains the clamped value. + public static void Clamp(ref Double4 value, ref Double4 min, ref Double4 max, out Double4 result) + { + double x = value.X; + x = x > max.X ? max.X : x; + x = x < min.X ? min.X : x; + double y = value.Y; + y = y > max.Y ? max.Y : y; + y = y < min.Y ? min.Y : y; + double z = value.Z; + z = z > max.Z ? max.Z : z; + z = z < min.Z ? min.Z : z; + double w = value.W; + w = w > max.W ? max.W : w; + w = w < min.W ? min.W : w; + result = new Double4(x, y, z, w); + } + + /// + /// Restricts a value to be within a specified range. + /// + /// The value to clamp. + /// The minimum value. + /// The maximum value. + /// The clamped value. + public static Double4 Clamp(Double4 value, Double4 min, Double4 max) + { + Clamp(ref value, ref min, ref max, out Double4 result); + return result; + } + + /// + /// Calculates the distance between two vectors. + /// + /// The first vector. + /// The second vector. + /// When the method completes, contains the distance between the two vectors. + /// may be preferred when only the relative distance is needed and speed is of the essence. + public static void Distance(ref Double4 value1, ref Double4 value2, out double result) + { + double x = value1.X - value2.X; + double y = value1.Y - value2.Y; + double z = value1.Z - value2.Z; + double w = value1.W - value2.W; + result = Math.Sqrt(x * x + y * y + z * z + w * w); + } + + /// + /// Calculates the distance between two vectors. + /// + /// The first vector. + /// The second vector. + /// The distance between the two vectors. + /// may be preferred when only the relative distance is needed and speed is of the essence. + public static double Distance(Double4 value1, Double4 value2) + { + double x = value1.X - value2.X; + double y = value1.Y - value2.Y; + double z = value1.Z - value2.Z; + double w = value1.W - value2.W; + return Math.Sqrt(x * x + y * y + z * z + w * w); + } + + /// + /// Calculates the squared distance between two vectors. + /// + /// The first vector. + /// The second vector. + /// When the method completes, contains the squared distance between the two vectors. + public static void DistanceSquared(ref Double4 value1, ref Double4 value2, out double result) + { + double x = value1.X - value2.X; + double y = value1.Y - value2.Y; + double z = value1.Z - value2.Z; + double w = value1.W - value2.W; + result = x * x + y * y + z * z + w * w; + } + + /// + /// Calculates the squared distance between two vectors. + /// + /// The first vector. + /// The second vector. + /// The squared distance between the two vectors. + public static double DistanceSquared(Double4 value1, Double4 value2) + { + double x = value1.X - value2.X; + double y = value1.Y - value2.Y; + double z = value1.Z - value2.Z; + double w = value1.W - value2.W; + return x * x + y * y + z * z + w * w; + } + + /// + /// Tests whether one vector is near another vector. + /// + /// The left vector. + /// The right vector. + /// The epsilon. + /// true if left and right are near another, false otherwise + public static bool NearEqual(Double4 left, Double4 right, double epsilon = Mathd.Epsilon) + { + return NearEqual(ref left, ref right, epsilon); + } + + /// + /// Tests whether one vector is near another vector. + /// + /// The left vector. + /// The right vector. + /// The epsilon. + /// true if left and right are near another, false otherwise + public static bool NearEqual(ref Double4 left, ref Double4 right, double epsilon = Mathd.Epsilon) + { + return Mathd.WithinEpsilon(left.X, right.X, epsilon) && Mathd.WithinEpsilon(left.Y, right.Y, epsilon) && Mathd.WithinEpsilon(left.Z, right.Z, epsilon) && Mathd.WithinEpsilon(left.W, right.W, epsilon); + } + + /// + /// Calculates the dot product of two vectors. + /// + /// First source vector + /// Second source vector. + /// When the method completes, contains the dot product of the two vectors. + public static void Dot(ref Double4 left, ref Double4 right, out double result) + { + result = left.X * right.X + left.Y * right.Y + left.Z * right.Z + left.W * right.W; + } + + /// + /// Calculates the dot product of two vectors. + /// + /// First source vector. + /// Second source vector. + /// The dot product of the two vectors. + public static double Dot(Double4 left, Double4 right) + { + return left.X * right.X + left.Y * right.Y + left.Z * right.Z + left.W * right.W; + } + + /// + /// Converts the vector into a unit vector. + /// + /// The vector to normalize. + /// When the method completes, contains the normalized vector. + public static void Normalize(ref Double4 value, out Double4 result) + { + result = value; + result.Normalize(); + } + + /// + /// Converts the vector into a unit vector. + /// + /// The vector to normalize. + /// The normalized vector. + public static Double4 Normalize(Double4 value) + { + value.Normalize(); + return value; + } + + /// + /// Makes sure that Length of the output vector is always below max and above 0. + /// + /// Input Vector. + /// Max Length + public static Double4 ClampLength(Double4 vector, double max) + { + return ClampLength(vector, 0, max); + } + + /// + /// Makes sure that Length of the output vector is always below max and above min. + /// + /// Input Vector. + /// Min Length + /// Max Length + public static Double4 ClampLength(Double4 vector, double min, double max) + { + ClampLength(vector, min, max, out Double4 result); + return result; + } + + /// + /// Makes sure that Length of the output vector is always below max and above min. + /// + /// Input Vector. + /// Min Length + /// Max Length + /// The result vector. + public static void ClampLength(Double4 vector, double min, double max, out Double4 result) + { + result = vector; + double lenSq = result.LengthSquared; + if (lenSq > max * max) + { + double scaleFactor = max / Math.Sqrt(lenSq); + result.X *= scaleFactor; + result.Y *= scaleFactor; + result.Z *= scaleFactor; + result.W *= scaleFactor; + } + if (lenSq < min * min) + { + double scaleFactor = min / Math.Sqrt(lenSq); + result.X *= scaleFactor; + result.Y *= scaleFactor; + result.Z *= scaleFactor; + result.W *= scaleFactor; + } + } + + /// + /// Performs a linear interpolation between two vectors. + /// + /// Start vector. + /// End vector. + /// Value between 0 and 1 indicating the weight of . + /// When the method completes, contains the linear interpolation of the two vectors. + /// Passing a value of 0 will cause to be returned; a value of 1 will cause to be returned. + public static void Lerp(ref Double4 start, ref Double4 end, double amount, out Double4 result) + { + result.X = Mathd.Lerp(start.X, end.X, amount); + result.Y = Mathd.Lerp(start.Y, end.Y, amount); + result.Z = Mathd.Lerp(start.Z, end.Z, amount); + result.W = Mathd.Lerp(start.W, end.W, amount); + } + + /// + /// Performs a linear interpolation between two vectors. + /// + /// Start vector. + /// End vector. + /// Value between 0 and 1 indicating the weight of . + /// The linear interpolation of the two vectors. + /// Passing a value of 0 will cause to be returned; a value of 1 will cause to be returned. + public static Double4 Lerp(Double4 start, Double4 end, double amount) + { + Lerp(ref start, ref end, amount, out Double4 result); + return result; + } + + /// + /// Performs a cubic interpolation between two vectors. + /// + /// Start vector. + /// End vector. + /// Value between 0 and 1 indicating the weight of . + /// When the method completes, contains the cubic interpolation of the two vectors. + public static void SmoothStep(ref Double4 start, ref Double4 end, double amount, out Double4 result) + { + amount = Mathd.SmoothStep(amount); + Lerp(ref start, ref end, amount, out result); + } + + /// + /// Performs a cubic interpolation between two vectors. + /// + /// Start vector. + /// End vector. + /// Value between 0 and 1 indicating the weight of . + /// The cubic interpolation of the two vectors. + public static Double4 SmoothStep(Double4 start, Double4 end, double amount) + { + SmoothStep(ref start, ref end, amount, out Double4 result); + return result; + } + + /// + /// Performs a Hermite spline interpolation. + /// + /// First source position vector. + /// First source tangent vector. + /// Second source position vector. + /// Second source tangent vector. + /// Weighting factor. + /// When the method completes, contains the result of the Hermite spline interpolation. + public static void Hermite(ref Double4 value1, ref Double4 tangent1, ref Double4 value2, ref Double4 tangent2, double amount, out Double4 result) + { + double squared = amount * amount; + double cubed = amount * squared; + double part1 = 2.0 * cubed - 3.0 * squared + 1.0; + double part2 = -2.0 * cubed + 3.0 * squared; + double part3 = cubed - 2.0 * squared + amount; + double part4 = cubed - squared; + result = new Double4(value1.X * part1 + value2.X * part2 + tangent1.X * part3 + tangent2.X * part4, + value1.Y * part1 + value2.Y * part2 + tangent1.Y * part3 + tangent2.Y * part4, + value1.Z * part1 + value2.Z * part2 + tangent1.Z * part3 + tangent2.Z * part4, + value1.W * part1 + value2.W * part2 + tangent1.W * part3 + tangent2.W * part4); + } + + /// + /// Performs a Hermite spline interpolation. + /// + /// First source position vector. + /// First source tangent vector. + /// Second source position vector. + /// Second source tangent vector. + /// Weighting factor. + /// The result of the Hermite spline interpolation. + public static Double4 Hermite(Double4 value1, Double4 tangent1, Double4 value2, Double4 tangent2, double amount) + { + Hermite(ref value1, ref tangent1, ref value2, ref tangent2, amount, out Double4 result); + return result; + } + + /// + /// Performs a Catmull-Rom interpolation using the specified positions. + /// + /// The first position in the interpolation. + /// The second position in the interpolation. + /// The third position in the interpolation. + /// The fourth position in the interpolation. + /// Weighting factor. + /// When the method completes, contains the result of the Catmull-Rom interpolation. + public static void CatmullRom(ref Double4 value1, ref Double4 value2, ref Double4 value3, ref Double4 value4, double amount, out Double4 result) + { + double squared = amount * amount; + double cubed = amount * squared; + result.X = 0.5f * (2.0 * value2.X + (-value1.X + value3.X) * amount + (2.0 * value1.X - 5.0 * value2.X + 4.0 * value3.X - value4.X) * squared + (-value1.X + 3.0 * value2.X - 3.0 * value3.X + value4.X) * cubed); + result.Y = 0.5f * (2.0 * value2.Y + (-value1.Y + value3.Y) * amount + (2.0 * value1.Y - 5.0 * value2.Y + 4.0 * value3.Y - value4.Y) * squared + (-value1.Y + 3.0 * value2.Y - 3.0 * value3.Y + value4.Y) * cubed); + result.Z = 0.5f * (2.0 * value2.Z + (-value1.Z + value3.Z) * amount + (2.0 * value1.Z - 5.0 * value2.Z + 4.0 * value3.Z - value4.Z) * squared + (-value1.Z + 3.0 * value2.Z - 3.0 * value3.Z + value4.Z) * cubed); + result.W = 0.5f * (2.0 * value2.W + (-value1.W + value3.W) * amount + (2.0 * value1.W - 5.0 * value2.W + 4.0 * value3.W - value4.W) * squared + (-value1.W + 3.0 * value2.W - 3.0 * value3.W + value4.W) * cubed); + } + + /// + /// Performs a Catmull-Rom interpolation using the specified positions. + /// + /// The first position in the interpolation. + /// The second position in the interpolation. + /// The third position in the interpolation. + /// The fourth position in the interpolation. + /// Weighting factor. + /// A vector that is the result of the Catmull-Rom interpolation. + public static Double4 CatmullRom(Double4 value1, Double4 value2, Double4 value3, Double4 value4, double amount) + { + CatmullRom(ref value1, ref value2, ref value3, ref value4, amount, out Double4 result); + return result; + } + + /// + /// Returns a vector containing the largest components of the specified vectors. + /// + /// The first source vector. + /// The second source vector. + /// When the method completes, contains an new vector composed of the largest components of the source vectors. + public static void Max(ref Double4 left, ref Double4 right, out Double4 result) + { + result.X = left.X > right.X ? left.X : right.X; + result.Y = left.Y > right.Y ? left.Y : right.Y; + result.Z = left.Z > right.Z ? left.Z : right.Z; + result.W = left.W > right.W ? left.W : right.W; + } + + /// + /// Returns a vector containing the largest components of the specified vectors. + /// + /// The first source vector. + /// The second source vector. + /// A vector containing the largest components of the source vectors. + public static Double4 Max(Double4 left, Double4 right) + { + Max(ref left, ref right, out Double4 result); + return result; + } + + /// + /// Returns a vector containing the smallest components of the specified vectors. + /// + /// The first source vector. + /// The second source vector. + /// When the method completes, contains an new vector composed of the smallest components of the source vectors. + public static void Min(ref Double4 left, ref Double4 right, out Double4 result) + { + result.X = left.X < right.X ? left.X : right.X; + result.Y = left.Y < right.Y ? left.Y : right.Y; + result.Z = left.Z < right.Z ? left.Z : right.Z; + result.W = left.W < right.W ? left.W : right.W; + } + + /// + /// Returns a vector containing the smallest components of the specified vectors. + /// + /// The first source vector. + /// The second source vector. + /// A vector containing the smallest components of the source vectors. + public static Double4 Min(Double4 left, Double4 right) + { + Min(ref left, ref right, out Double4 result); + return result; + } + + /// + /// Returns the absolute value of a vector. + /// + /// The value. + /// A vector which components are less or equal to 0. + public static Double4 Abs(Double4 v) + { + return new Double4(Math.Abs(v.X), Math.Abs(v.Y), Math.Abs(v.Z), Math.Abs(v.W)); + } + + /// + /// Orthogonalizes a list of vectors. + /// + /// The list of orthogonalized vectors. + /// The list of vectors to orthogonalize. + /// + /// + /// Orthogonalization is the process of making all vectors orthogonal to each other. This + /// means that any given vector in the list will be orthogonal to any other given vector in the + /// list. + /// + /// + /// Because this method uses the modified Gram-Schmidt process, the resulting vectors + /// tend to be numerically unstable. The numeric stability decreases according to the vectors + /// position in the list so that the first vector is the most stable and the last vector is the + /// least stable. + /// + /// + /// Thrown when or is null. + /// Thrown when is shorter in length than . + public static void Orthogonalize(Double4[] destination, params Double4[] source) + { + //Uses the modified Gram-Schmidt process. + //q1 = m1 + //q2 = m2 - ((q1 ⋅ m2) / (q1 ⋅ q1)) * q1 + //q3 = m3 - ((q1 ⋅ m3) / (q1 ⋅ q1)) * q1 - ((q2 ⋅ m3) / (q2 ⋅ q2)) * q2 + //q4 = m4 - ((q1 ⋅ m4) / (q1 ⋅ q1)) * q1 - ((q2 ⋅ m4) / (q2 ⋅ q2)) * q2 - ((q3 ⋅ m4) / (q3 ⋅ q3)) * q3 + //q5 = ... + if (source == null) + throw new ArgumentNullException(nameof(source)); + if (destination == null) + throw new ArgumentNullException(nameof(destination)); + if (destination.Length < source.Length) + throw new ArgumentOutOfRangeException(nameof(destination), "The destination array must be of same length or larger length than the source array."); + for (var i = 0; i < source.Length; ++i) + { + Double4 v = source[i]; + for (var r = 0; r < i; ++r) + v -= Dot(destination[r], v) / Dot(destination[r], destination[r]) * destination[r]; + destination[i] = v; + } + } + + /// + /// Orthonormalizes a list of vectors. + /// + /// The list of orthonormalized vectors. + /// The list of vectors to orthonormalize. + /// + /// + /// Orthonormalization is the process of making all vectors orthogonal to each + /// other and making all vectors of unit length. This means that any given vector will + /// be orthogonal to any other given vector in the list. + /// + /// + /// Because this method uses the modified Gram-Schmidt process, the resulting vectors + /// tend to be numerically unstable. The numeric stability decreases according to the vectors + /// position in the list so that the first vector is the most stable and the last vector is the + /// least stable. + /// + /// + /// Thrown when or is null. + /// Thrown when is shorter in length than . + public static void Orthonormalize(Double4[] destination, params Double4[] source) + { + //Uses the modified Gram-Schmidt process. + //Because we are making unit vectors, we can optimize the math for orthogonalization + //and simplify the projection operation to remove the division. + //q1 = m1 / |m1| + //q2 = (m2 - (q1 ⋅ m2) * q1) / |m2 - (q1 ⋅ m2) * q1| + //q3 = (m3 - (q1 ⋅ m3) * q1 - (q2 ⋅ m3) * q2) / |m3 - (q1 ⋅ m3) * q1 - (q2 ⋅ m3) * q2| + //q4 = (m4 - (q1 ⋅ m4) * q1 - (q2 ⋅ m4) * q2 - (q3 ⋅ m4) * q3) / |m4 - (q1 ⋅ m4) * q1 - (q2 ⋅ m4) * q2 - (q3 ⋅ m4) * q3| + //q5 = ... + if (source == null) + throw new ArgumentNullException(nameof(source)); + if (destination == null) + throw new ArgumentNullException(nameof(destination)); + if (destination.Length < source.Length) + throw new ArgumentOutOfRangeException(nameof(destination), "The destination array must be of same length or larger length than the source array."); + for (var i = 0; i < source.Length; ++i) + { + Double4 v = source[i]; + for (var r = 0; r < i; ++r) + v -= Dot(destination[r], v) * destination[r]; + v.Normalize(); + destination[i] = v; + } + } + + /// + /// Transforms a 4D vector by the given rotation. + /// + /// The vector to rotate. + /// The rotation to apply. + /// When the method completes, contains the transformed . + public static void Transform(ref Double4 vector, ref Quaternion rotation, out Double4 result) + { + double x = rotation.X + rotation.X; + double y = rotation.Y + rotation.Y; + double z = rotation.Z + rotation.Z; + double wx = rotation.W * x; + double wy = rotation.W * y; + double wz = rotation.W * z; + double xx = rotation.X * x; + double xy = rotation.X * y; + double xz = rotation.X * z; + double yy = rotation.Y * y; + double yz = rotation.Y * z; + double zz = rotation.Z * z; + result = new Double4(vector.X * (1.0 - yy - zz) + vector.Y * (xy - wz) + vector.Z * (xz + wy), + vector.X * (xy + wz) + vector.Y * (1.0 - xx - zz) + vector.Z * (yz - wx), + vector.X * (xz - wy) + vector.Y * (yz + wx) + vector.Z * (1.0 - xx - yy), + vector.W); + } + + /// + /// Transforms a 4D vector by the given rotation. + /// + /// The vector to rotate. + /// The rotation to apply. + /// The transformed . + public static Double4 Transform(Double4 vector, Quaternion rotation) + { + Transform(ref vector, ref rotation, out Double4 result); + return result; + } + + /// + /// Transforms an array of vectors by the given rotation. + /// + /// The array of vectors to transform. + /// The rotation to apply. + /// The array for which the transformed vectors are stored. This array may be the same array as . + /// Thrown when or is null. + /// Thrown when is shorter in length than . + public static void Transform(Double4[] source, ref Quaternion rotation, Double4[] destination) + { + if (source == null) + throw new ArgumentNullException(nameof(source)); + if (destination == null) + throw new ArgumentNullException(nameof(destination)); + if (destination.Length < source.Length) + throw new ArgumentOutOfRangeException(nameof(destination), "The destination array must be of same length or larger length than the source array."); + double x = rotation.X + rotation.X; + double y = rotation.Y + rotation.Y; + double z = rotation.Z + rotation.Z; + double wx = rotation.W * x; + double wy = rotation.W * y; + double wz = rotation.W * z; + double xx = rotation.X * x; + double xy = rotation.X * y; + double xz = rotation.X * z; + double yy = rotation.Y * y; + double yz = rotation.Y * z; + double zz = rotation.Z * z; + double num1 = 1.0 - yy - zz; + double num2 = xy - wz; + double num3 = xz + wy; + double num4 = xy + wz; + double num5 = 1.0 - xx - zz; + double num6 = yz - wx; + double num7 = xz - wy; + double num8 = yz + wx; + double num9 = 1.0 - xx - yy; + for (var i = 0; i < source.Length; ++i) + destination[i] = new Double4(source[i].X * num1 + source[i].Y * num2 + source[i].Z * num3, + source[i].X * num4 + source[i].Y * num5 + source[i].Z * num6, + source[i].X * num7 + source[i].Y * num8 + source[i].Z * num9, + source[i].W); + } + + /// + /// Transforms a 4D vector by the given . + /// + /// The source vector. + /// The transformation . + /// When the method completes, contains the transformed . + public static void Transform(ref Double4 vector, ref Matrix transform, out Double4 result) + { + result = new Double4(vector.X * transform.M11 + vector.Y * transform.M21 + vector.Z * transform.M31 + vector.W * transform.M41, + vector.X * transform.M12 + vector.Y * transform.M22 + vector.Z * transform.M32 + vector.W * transform.M42, + vector.X * transform.M13 + vector.Y * transform.M23 + vector.Z * transform.M33 + vector.W * transform.M43, + vector.X * transform.M14 + vector.Y * transform.M24 + vector.Z * transform.M34 + vector.W * transform.M44); + } + + /// + /// Transforms a 4D vector by the given . + /// + /// The source vector. + /// The transformation . + /// The transformed . + public static Double4 Transform(Double4 vector, Matrix transform) + { + Transform(ref vector, ref transform, out Double4 result); + return result; + } + + /// + /// Transforms an array of 4D vectors by the given . + /// + /// The array of vectors to transform. + /// The transformation . + /// The array for which the transformed vectors are stored. This array may be the same array as . + /// Thrown when or is null. + /// Thrown when is shorter in length than . + public static void Transform(Double4[] source, ref Matrix transform, Double4[] destination) + { + if (source == null) + throw new ArgumentNullException(nameof(source)); + if (destination == null) + throw new ArgumentNullException(nameof(destination)); + if (destination.Length < source.Length) + throw new ArgumentOutOfRangeException(nameof(destination), "The destination array must be of same length or larger length than the source array."); + for (var i = 0; i < source.Length; ++i) + Transform(ref source[i], ref transform, out destination[i]); + } + + /// + /// Adds two vectors. + /// + /// The first vector to add. + /// The second vector to add. + /// The sum of the two vectors. + public static Double4 operator +(Double4 left, Double4 right) + { + return new Double4(left.X + right.X, left.Y + right.Y, left.Z + right.Z, left.W + right.W); + } + + /// + /// Multiplies a vector with another by performing component-wise multiplication equivalent to . + /// + /// The first vector to multiply. + /// The second vector to multiply. + /// The multiplication of the two vectors. + public static Double4 operator *(Double4 left, Double4 right) + { + return new Double4(left.X * right.X, left.Y * right.Y, left.Z * right.Z, left.W * right.W); + } + + /// + /// Assert a vector (return it unchanged). + /// + /// The vector to assert (unchanged). + /// The asserted (unchanged) vector. + public static Double4 operator +(Double4 value) + { + return value; + } + + /// + /// Subtracts two vectors. + /// + /// The first vector to subtract. + /// The second vector to subtract. + /// The difference of the two vectors. + public static Double4 operator -(Double4 left, Double4 right) + { + return new Double4(left.X - right.X, left.Y - right.Y, left.Z - right.Z, left.W - right.W); + } + + /// + /// Reverses the direction of a given vector. + /// + /// The vector to negate. + /// A vector facing in the opposite direction. + public static Double4 operator -(Double4 value) + { + return new Double4(-value.X, -value.Y, -value.Z, -value.W); + } + + /// + /// Scales a vector by the given value. + /// + /// The vector to scale. + /// The amount by which to scale the vector. + /// The scaled vector. + public static Double4 operator *(double scale, Double4 value) + { + return new Double4(value.X * scale, value.Y * scale, value.Z * scale, value.W * scale); + } + + /// + /// Scales a vector by the given value. + /// + /// The vector to scale. + /// The amount by which to scale the vector. + /// The scaled vector. + public static Double4 operator *(Double4 value, double scale) + { + return new Double4(value.X * scale, value.Y * scale, value.Z * scale, value.W * scale); + } + + /// + /// Scales a vector by the given value. + /// + /// The vector to scale. + /// The amount by which to scale the vector. + /// The scaled vector. + public static Double4 operator /(Double4 value, double scale) + { + return new Double4(value.X / scale, value.Y / scale, value.Z / scale, value.W / scale); + } + + /// + /// Scales a vector by the given value. + /// + /// The amount by which to scale the vector. + /// The vector to scale. + /// The scaled vector. + public static Double4 operator /(double scale, Double4 value) + { + return new Double4(scale / value.X, scale / value.Y, scale / value.Z, scale / value.W); + } + + /// + /// Scales a vector by the given value. + /// + /// The vector to scale. + /// The amount by which to scale the vector. + /// The scaled vector. + public static Double4 operator /(Double4 value, Double4 scale) + { + return new Double4(value.X / scale.X, value.Y / scale.Y, value.Z / scale.Z, value.W / scale.W); + } + + /// + /// Remainder of value divided by scale. + /// + /// The vector to scale. + /// The amount by which to scale the vector. + /// The remained vector. + public static Double4 operator %(Double4 value, double scale) + { + return new Double4(value.X % scale, value.Y % scale, value.Z % scale, value.W % scale); + } + + /// + /// Remainder of value divided by scale. + /// + /// The amount by which to scale the vector. + /// The vector to scale. + /// The remained vector. + public static Double4 operator %(double value, Double4 scale) + { + return new Double4(value % scale.X, value % scale.Y, value % scale.Z, value % scale.W); + } + + /// + /// Remainder of value divided by scale. + /// + /// The vector to scale. + /// The amount by which to scale the vector. + /// The remained vector. + public static Double4 operator %(Double4 value, Double4 scale) + { + return new Double4(value.X % scale.X, value.Y % scale.Y, value.Z % scale.Z, value.W % scale.W); + } + + /// + /// Performs a component-wise addition. + /// + /// The input vector. + /// The scalar value to be added on elements + /// The vector with added scalar for each element. + public static Double4 operator +(Double4 value, double scalar) + { + return new Double4(value.X + scalar, value.Y + scalar, value.Z + scalar, value.W + scalar); + } + + /// + /// Performs a component-wise addition. + /// + /// The input vector. + /// The scalar value to be added on elements + /// The vector with added scalar for each element. + public static Double4 operator +(double scalar, Double4 value) + { + return new Double4(scalar + value.X, scalar + value.Y, scalar + value.Z, scalar + value.W); + } + + /// + /// Performs a component-wise subtraction. + /// + /// The input vector. + /// The scalar value to be subtracted from elements + /// The vector with subtracted scalar from each element. + public static Double4 operator -(Double4 value, double scalar) + { + return new Double4(value.X - scalar, value.Y - scalar, value.Z - scalar, value.W - scalar); + } + + /// + /// Performs a component-wise subtraction. + /// + /// The input vector. + /// The scalar value to be subtracted from elements + /// The vector with subtracted scalar from each element. + public static Double4 operator -(double scalar, Double4 value) + { + return new Double4(scalar - value.X, scalar - value.Y, scalar - value.Z, scalar - value.W); + } + + /// + /// Tests for equality between two objects. + /// + /// The first value to compare. + /// The second value to compare. + /// true if has the same value as ; otherwise, false. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool operator ==(Double4 left, Double4 right) + { + return Mathd.NearEqual(left.X, right.X) && Mathd.NearEqual(left.Y, right.Y) && Mathd.NearEqual(left.Z, right.Z) && Mathd.NearEqual(left.W, right.W); + } + + /// + /// Tests for inequality between two objects. + /// + /// The first value to compare. + /// The second value to compare. + /// true if has a different value than ; otherwise, false. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool operator !=(Double4 left, Double4 right) + { + return !left.Equals(ref right); + } + + /// + /// Performs an explicit conversion from to . + /// + /// The value. + /// The result of the conversion. + public static explicit operator Double2(Double4 value) + { + return new Double2(value.X, value.Y); + } + + /// + /// Performs an explicit conversion from to . + /// + /// The value. + /// The result of the conversion. + public static explicit operator Double3(Double4 value) + { + return new Double3(value.X, value.Y, value.Z); + } + + /// + /// Returns a that represents this instance. + /// + /// A that represents this instance. + public override string ToString() + { + return string.Format(CultureInfo.CurrentCulture, _formatString, X, Y, Z, W); + } + + /// + /// Returns a that represents this instance. + /// + /// The format. + /// A that represents this instance. + public string ToString(string format) + { + if (format == null) + return ToString(); + return string.Format(CultureInfo.CurrentCulture, _formatString, X.ToString(format, CultureInfo.CurrentCulture), Y.ToString(format, CultureInfo.CurrentCulture), Z.ToString(format, CultureInfo.CurrentCulture), W.ToString(format, CultureInfo.CurrentCulture)); + } + + /// + /// Returns a that represents this instance. + /// + /// The format provider. + /// A that represents this instance. + public string ToString(IFormatProvider formatProvider) + { + return string.Format(formatProvider, _formatString, X, Y, Z, W); + } + + /// + /// Returns a that represents this instance. + /// + /// The format. + /// The format provider. + /// A that represents this instance. + public string ToString(string format, IFormatProvider formatProvider) + { + if (format == null) + return ToString(formatProvider); + return string.Format(formatProvider, "X:{0} Y:{1} Z:{2} W:{3}", X.ToString(format, formatProvider), Y.ToString(format, formatProvider), Z.ToString(format, formatProvider), W.ToString(format, formatProvider)); + } + + /// + /// Returns a hash code for this instance. + /// + public override int GetHashCode() + { + unchecked + { + int hashCode = X.GetHashCode(); + hashCode = (hashCode * 397) ^ Y.GetHashCode(); + hashCode = (hashCode * 397) ^ Z.GetHashCode(); + hashCode = (hashCode * 397) ^ W.GetHashCode(); + return hashCode; + } + } + + /// + /// Determines whether the specified is equal to this instance. + /// + /// The to compare with this instance. + /// true if the specified is equal to this instance; otherwise, false. + public bool Equals(ref Double4 other) + { + return Mathd.NearEqual(other.X, X) && Mathd.NearEqual(other.Y, Y) && Mathd.NearEqual(other.Z, Z) && Mathd.NearEqual(other.W, W); + } + + /// + /// Determines whether the specified is equal to this instance. + /// + /// The to compare with this instance. + /// true if the specified is equal to this instance; otherwise, false. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public bool Equals(Double4 other) + { + return Mathd.NearEqual(other.X, X) && Mathd.NearEqual(other.Y, Y) && Mathd.NearEqual(other.Z, Z) && Mathd.NearEqual(other.W, W); + } + + /// + /// Determines whether the specified is equal to this instance. + /// + /// The to compare with this instance. + /// true if the specified is equal to this instance; otherwise, false. + public override bool Equals(object value) + { + if (!(value is Double4 other)) + return false; + return Mathd.NearEqual(other.X, X) && Mathd.NearEqual(other.Y, Y) && Mathd.NearEqual(other.Z, Z) && Mathd.NearEqual(other.W, W); + } + } +} diff --git a/Source/Engine/Core/Math/TypeConverters/Double2Converter.cs b/Source/Engine/Core/Math/TypeConverters/Double2Converter.cs new file mode 100644 index 000000000..0a20c7a5f --- /dev/null +++ b/Source/Engine/Core/Math/TypeConverters/Double2Converter.cs @@ -0,0 +1,46 @@ +// Copyright (c) 2012-2022 Wojciech Figat. All rights reserved. + +using System; +using System.ComponentModel; +using System.Globalization; + +namespace FlaxEngine.TypeConverters +{ + internal class Double2Converter : TypeConverter + { + /// + public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) + { + if (sourceType == typeof(string)) + { + return true; + } + + return base.CanConvertFrom(context, sourceType); + } + + /// + public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) + { + if (value is string str) + { + string[] v = str.Split(','); + return new Double2(double.Parse(v[0]), double.Parse(v[1])); + } + + return base.ConvertFrom(context, culture, value); + } + + /// + public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) + { + if (destinationType == typeof(string)) + { + var v = (Double2)value; + return v.X + "," + v.Y; + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } +} diff --git a/Source/Engine/Core/Math/TypeConverters/Double3Converter.cs b/Source/Engine/Core/Math/TypeConverters/Double3Converter.cs new file mode 100644 index 000000000..c740a1fe0 --- /dev/null +++ b/Source/Engine/Core/Math/TypeConverters/Double3Converter.cs @@ -0,0 +1,46 @@ +// Copyright (c) 2012-2022 Wojciech Figat. All rights reserved. + +using System; +using System.ComponentModel; +using System.Globalization; + +namespace FlaxEngine.TypeConverters +{ + internal class Double3Converter : TypeConverter + { + /// + public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) + { + if (sourceType == typeof(string)) + { + return true; + } + + return base.CanConvertFrom(context, sourceType); + } + + /// + public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) + { + if (value is string str) + { + string[] v = str.Split(','); + return new Double3(double.Parse(v[0]), double.Parse(v[1]), double.Parse(v[2])); + } + + return base.ConvertFrom(context, culture, value); + } + + /// + public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) + { + if (destinationType == typeof(string)) + { + var v = (Double3)value; + return v.X + "," + v.Y + "," + v.Z; + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } +} diff --git a/Source/Engine/Core/Math/TypeConverters/Double4Converter.cs b/Source/Engine/Core/Math/TypeConverters/Double4Converter.cs new file mode 100644 index 000000000..bc49a37bc --- /dev/null +++ b/Source/Engine/Core/Math/TypeConverters/Double4Converter.cs @@ -0,0 +1,46 @@ +// Copyright (c) 2012-2022 Wojciech Figat. All rights reserved. + +using System; +using System.ComponentModel; +using System.Globalization; + +namespace FlaxEngine.TypeConverters +{ + internal class Double4Converter : TypeConverter + { + /// + public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) + { + if (sourceType == typeof(string)) + { + return true; + } + + return base.CanConvertFrom(context, sourceType); + } + + /// + public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) + { + if (value is string str) + { + string[] v = str.Split(','); + return new Double4(double.Parse(v[0]), double.Parse(v[1]), double.Parse(v[2]), double.Parse(v[3])); + } + + return base.ConvertFrom(context, culture, value); + } + + /// + public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) + { + if (destinationType == typeof(string)) + { + var v = (Double4)value; + return v.X + "," + v.Y + "," + v.Z + "," + v.W; + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } +}