Cleanup and adjustments for vector types

This commit is contained in:
Wojtek Figat
2022-05-16 23:52:38 +02:00
parent 778e4e0ea7
commit 4a2e8d0a69
11 changed files with 144 additions and 311 deletions

View File

@@ -19,7 +19,7 @@ struct Color;
struct Matrix; struct Matrix;
/// <summary> /// <summary>
/// Represents a two dimensional mathematical vector. /// Represents a two dimensional mathematical vector with 64-bit precision (per-component).
/// </summary> /// </summary>
API_STRUCT() struct FLAXENGINE_API Double2 API_STRUCT() struct FLAXENGINE_API Double2
{ {
@@ -115,7 +115,7 @@ public:
// Init // Init
// @param v Vector2 to use X and Y components // @param v Vector2 to use X and Y components
explicit Double2(const Vector2& xy); Double2(const Vector2& xy);
// Init // Init
// @param v Vector3 to use X and Y components // @param v Vector3 to use X and Y components

View File

@@ -19,7 +19,7 @@ struct Color;
struct Matrix; struct Matrix;
/// <summary> /// <summary>
/// Represents a two dimensional mathematical vector. /// Represents a two dimensional mathematical vector with 64-bit precision (per-component).
/// </summary> /// </summary>
API_STRUCT() struct FLAXENGINE_API Double3 API_STRUCT() struct FLAXENGINE_API Double3
{ {
@@ -146,7 +146,7 @@ public:
// Init // Init
// @param xyz Vector3 value // @param xyz Vector3 value
explicit Double3(const Vector3& xyz); Double3(const Vector3& xyz);
// Init // Init
// @param xyz Vector4 value // @param xyz Vector4 value

View File

@@ -2,11 +2,10 @@
#pragma once #pragma once
#include "Engine/Platform/Platform.h"
#include "Engine/Core/Formatting.h"
#include "Engine/Core/Templates.h"
#include "Math.h" #include "Math.h"
#include "Mathd.h" #include "Mathd.h"
#include "Engine/Core/Formatting.h"
#include "Engine/Core/Templates.h"
struct Double2; struct Double2;
struct Double3; struct Double3;
@@ -22,7 +21,7 @@ struct Int3;
struct Int4; struct Int4;
/// <summary> /// <summary>
/// Represents a four dimensional mathematical vector. /// Represents a four dimensional mathematical vector with 64-bit precision (per-component).
/// </summary> /// </summary>
API_STRUCT() struct FLAXENGINE_API Double4 API_STRUCT() struct FLAXENGINE_API Double4
{ {
@@ -140,7 +139,7 @@ public:
// Init // Init
// @param xyzw Vector4 value // @param xyzw Vector4 value
explicit Double4(const Vector4& xyzw); Double4(const Vector4& xyzw);
// Init // Init
// @param xy X and Y values in the vector // @param xy X and Y values in the vector

View File

@@ -1,4 +1,4 @@
// Copyright (c) 2012-2022 Wojciech Figat. All rights reserved. // Copyright (c) 2012-2022 Wojciech Figat. All rights reserved.
using System; using System;
using System.ComponentModel; using System.ComponentModel;
@@ -444,8 +444,7 @@ namespace FlaxEngine
/// <param name="maxSpeed">The maximum speed.</param> /// <param name="maxSpeed">The maximum speed.</param>
/// <param name="deltaTime">The delta time (in seconds) since last update.</param> /// <param name="deltaTime">The delta time (in seconds) since last update.</param>
/// <returns>The smoothed value.</returns> /// <returns>The smoothed value.</returns>
public static double SmoothDamp(double current, double target, ref double currentVelocity, double smoothTime, [DefaultValue("double.PositiveInfinity")] public static double SmoothDamp(double current, double target, ref double currentVelocity, double smoothTime, [DefaultValue("double.PositiveInfinity")] double maxSpeed, [DefaultValue("Time.DeltaTime")] double deltaTime)
double maxSpeed, [DefaultValue("Time.DeltaTime")] double deltaTime)
{ {
smoothTime = Max(0.0001d, smoothTime); smoothTime = Max(0.0001d, smoothTime);
double a = 2d / smoothTime; double a = 2d / smoothTime;
@@ -504,8 +503,7 @@ namespace FlaxEngine
/// <param name="maxSpeed">The maximum speed.</param> /// <param name="maxSpeed">The maximum speed.</param>
/// <param name="deltaTime">The delta time (in seconds) since last update.</param> /// <param name="deltaTime">The delta time (in seconds) since last update.</param>
/// <returns>The smoothed value.</returns> /// <returns>The smoothed value.</returns>
public static double SmoothDampAngle(double current, double target, ref double currentVelocity, double smoothTime, [DefaultValue("double.PositiveInfinity")] public static double SmoothDampAngle(double current, double target, ref double currentVelocity, double smoothTime, [DefaultValue("double.PositiveInfinity")] double maxSpeed, [DefaultValue("Time.DeltaTime")] double deltaTime)
double maxSpeed, [DefaultValue("Time.DeltaTime")] double deltaTime)
{ {
target = current + DeltaAngle(current, target); target = current + DeltaAngle(current, target);
return SmoothDamp(current, target, ref currentVelocity, smoothTime, maxSpeed, deltaTime); return SmoothDamp(current, target, ref currentVelocity, smoothTime, maxSpeed, deltaTime);

View File

@@ -2,8 +2,8 @@
#pragma once #pragma once
#include <math.h>
#include "Engine/Core/Types/BaseTypes.h" #include "Engine/Core/Types/BaseTypes.h"
#include <math.h>
namespace Math namespace Math
{ {

View File

@@ -218,7 +218,6 @@ namespace FlaxEngine
} }
throw new ArgumentOutOfRangeException(nameof(index), "Indices for Vector2 run from 0 to 1, inclusive."); throw new ArgumentOutOfRangeException(nameof(index), "Indices for Vector2 run from 0 to 1, inclusive.");
} }
set set
{ {
switch (index) switch (index)
@@ -267,11 +266,7 @@ namespace FlaxEngine
/// </summary> /// </summary>
public float[] ToArray() public float[] ToArray()
{ {
return new[] return new[] { X, Y };
{
X,
Y
};
} }
/// <summary> /// <summary>
@@ -297,7 +292,7 @@ namespace FlaxEngine
} }
/// <summary> /// <summary>
/// Perform a component-wise addition /// Performs a component-wise addition.
/// </summary> /// </summary>
/// <param name="left">The input vector</param> /// <param name="left">The input vector</param>
/// <param name="right">The scalar value to be added to elements</param> /// <param name="right">The scalar value to be added to elements</param>
@@ -308,7 +303,7 @@ namespace FlaxEngine
} }
/// <summary> /// <summary>
/// Perform a component-wise addition /// Performs a component-wise addition.
/// </summary> /// </summary>
/// <param name="left">The input vector</param> /// <param name="left">The input vector</param>
/// <param name="right">The scalar value to be added to elements</param> /// <param name="right">The scalar value to be added to elements</param>
@@ -341,7 +336,7 @@ namespace FlaxEngine
} }
/// <summary> /// <summary>
/// Perform a component-wise subtraction /// Performs a component-wise subtraction.
/// </summary> /// </summary>
/// <param name="left">The input vector</param> /// <param name="left">The input vector</param>
/// <param name="right">The scalar value to be subtracted from elements</param> /// <param name="right">The scalar value to be subtracted from elements</param>
@@ -352,7 +347,7 @@ namespace FlaxEngine
} }
/// <summary> /// <summary>
/// Perform a component-wise subtraction /// Performs a component-wise subtraction.
/// </summary> /// </summary>
/// <param name="left">The input vector</param> /// <param name="left">The input vector</param>
/// <param name="right">The scalar value to be subtracted from elements</param> /// <param name="right">The scalar value to be subtracted from elements</param>
@@ -363,7 +358,7 @@ namespace FlaxEngine
} }
/// <summary> /// <summary>
/// Perform a component-wise subtraction /// Performs a component-wise subtraction.
/// </summary> /// </summary>
/// <param name="left">The scalar value to be subtracted from elements</param> /// <param name="left">The scalar value to be subtracted from elements</param>
/// <param name="right">The input vector</param> /// <param name="right">The input vector</param>
@@ -374,7 +369,7 @@ namespace FlaxEngine
} }
/// <summary> /// <summary>
/// Perform a component-wise subtraction /// Performs a component-wise subtraction.
/// </summary> /// </summary>
/// <param name="left">The scalar value to be subtracted from elements</param> /// <param name="left">The scalar value to be subtracted from elements</param>
/// <param name="right">The input vector</param> /// <param name="right">The input vector</param>
@@ -536,11 +531,9 @@ namespace FlaxEngine
float x = value.X; float x = value.X;
x = x > max.X ? max.X : x; x = x > max.X ? max.X : x;
x = x < min.X ? min.X : x; x = x < min.X ? min.X : x;
float y = value.Y; float y = value.Y;
y = y > max.Y ? max.Y : y; y = y > max.Y ? max.Y : y;
y = y < min.Y ? min.Y : y; y = y < min.Y ? min.Y : y;
result = new Vector2(x, y); result = new Vector2(x, y);
} }
@@ -626,14 +619,6 @@ namespace FlaxEngine
/// <param name="value1">The first vector.</param> /// <param name="value1">The first vector.</param>
/// <param name="value2">The second vector</param> /// <param name="value2">The second vector</param>
/// <param name="result">When the method completes, contains the squared distance between the two vectors.</param> /// <param name="result">When the method completes, contains the squared distance between the two vectors.</param>
/// <remarks>
/// Distance squared is the value before taking the square root.
/// Distance squared can often be used in place of distance if relative comparisons are being made.
/// For example, consider three points A, B, and C. To determine whether B or C is further from A,
/// compare the distance between A and B to the distance between A and C. Calculating the two distances
/// involves two square roots, which are computationally expensive. However, using distance squared
/// provides the same information and avoids calculating two square roots.
/// </remarks>
public static void DistanceSquared(ref Vector2 value1, ref Vector2 value2, out float result) public static void DistanceSquared(ref Vector2 value1, ref Vector2 value2, out float result)
{ {
float x = value1.X - value2.X; float x = value1.X - value2.X;
@@ -647,14 +632,6 @@ namespace FlaxEngine
/// <param name="value1">The first vector.</param> /// <param name="value1">The first vector.</param>
/// <param name="value2">The second vector</param> /// <param name="value2">The second vector</param>
/// <returns>The squared distance between the two vectors.</returns> /// <returns>The squared distance between the two vectors.</returns>
/// <remarks>
/// Distance squared is the value before taking the square root.
/// Distance squared can often be used in place of distance if relative comparisons are being made.
/// For example, consider three points A, B, and C. To determine whether B or C is further from A,
/// compare the distance between A and B to the distance between A and C. Calculating the two distances
/// involves two square roots, which are computationally expensive. However, using distance squared
/// provides the same information and avoids calculating two square roots.
/// </remarks>
public static float DistanceSquared(ref Vector2 value1, ref Vector2 value2) public static float DistanceSquared(ref Vector2 value1, ref Vector2 value2)
{ {
float x = value1.X - value2.X; float x = value1.X - value2.X;
@@ -668,14 +645,6 @@ namespace FlaxEngine
/// <param name="value1">The first vector.</param> /// <param name="value1">The first vector.</param>
/// <param name="value2">The second vector.</param> /// <param name="value2">The second vector.</param>
/// <returns>The squared distance between the two vectors.</returns> /// <returns>The squared distance between the two vectors.</returns>
/// <remarks>
/// Distance squared is the value before taking the square root.
/// Distance squared can often be used in place of distance if relative comparisons are being made.
/// For example, consider three points A, B, and C. To determine whether B or C is further from A,
/// compare the distance between A and B to the distance between A and C. Calculating the two distances
/// involves two square roots, which are computationally expensive. However, using distance squared
/// provides the same information and avoids calculating two square roots.
/// </remarks>
public static float DistanceSquared(Vector2 value1, Vector2 value2) public static float DistanceSquared(Vector2 value1, Vector2 value2)
{ {
float x = value1.X - value2.X; float x = value1.X - value2.X;
@@ -813,8 +782,8 @@ namespace FlaxEngine
/// <param name="max">Max Length</param> /// <param name="max">Max Length</param>
public static Vector2 ClampLength(Vector2 vector, float min, float max) public static Vector2 ClampLength(Vector2 vector, float min, float max)
{ {
ClampLength(ref vector, min, max, out Vector2 retVect); ClampLength(vector, min, max, out Vector2 result);
return retVect; return result;
} }
/// <summary> /// <summary>
@@ -823,24 +792,22 @@ namespace FlaxEngine
/// <param name="vector">Input Vector.</param> /// <param name="vector">Input Vector.</param>
/// <param name="min">Min Length</param> /// <param name="min">Min Length</param>
/// <param name="max">Max Length</param> /// <param name="max">Max Length</param>
/// <param name="retVect">The Return Vector</param> /// <param name="result">The result value.</param>
public static void ClampLength(ref Vector2 vector, float min, float max, out Vector2 retVect) public static void ClampLength(Vector2 vector, float min, float max, out Vector2 result)
{ {
retVect.X = vector.X; result = vector;
retVect.Y = vector.Y; float lenSq = result.LengthSquared;
float lenSq = retVect.LengthSquared;
if (lenSq > max * max) if (lenSq > max * max)
{ {
float scaleFactor = max / (float)Math.Sqrt(lenSq); float scaleFactor = max / (float)Math.Sqrt(lenSq);
retVect.X = retVect.X * scaleFactor; result.X *= scaleFactor;
retVect.Y = retVect.Y * scaleFactor; result.Y *= scaleFactor;
} }
if (lenSq < min * min) if (lenSq < min * min)
{ {
float scaleFactor = min / (float)Math.Sqrt(lenSq); float scaleFactor = min / (float)Math.Sqrt(lenSq);
retVect.X = retVect.X * scaleFactor; result.X *= scaleFactor;
retVect.Y = retVect.Y * scaleFactor; result.Y *= scaleFactor;
} }
} }
@@ -973,7 +940,6 @@ namespace FlaxEngine
float part2 = -2.0f * cubed + 3.0f * squared; float part2 = -2.0f * cubed + 3.0f * squared;
float part3 = cubed - 2.0f * squared + amount; float part3 = cubed - 2.0f * squared + amount;
float part4 = cubed - squared; float part4 = cubed - squared;
result.X = value1.X * part1 + value2.X * part2 + tangent1.X * part3 + tangent2.X * part4; 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.Y = value1.Y * part1 + value2.Y * part2 + tangent1.Y * part3 + tangent2.Y * part4;
} }
@@ -1026,11 +992,9 @@ namespace FlaxEngine
{ {
float squared = amount * amount; float squared = amount * amount;
float cubed = amount * squared; float cubed = amount * squared;
result.X = 0.5f * (2.0f * value2.X + (-value1.X + value3.X) * amount + result.X = 0.5f * (2.0f * value2.X + (-value1.X + value3.X) * amount +
(2.0f * value1.X - 5.0f * value2.X + 4.0f * value3.X - value4.X) * squared + (2.0f * value1.X - 5.0f * value2.X + 4.0f * value3.X - value4.X) * squared +
(-value1.X + 3.0f * value2.X - 3.0f * value3.X + value4.X) * cubed); (-value1.X + 3.0f * value2.X - 3.0f * value3.X + value4.X) * cubed);
result.Y = 0.5f * (2.0f * value2.Y + (-value1.Y + value3.Y) * amount + result.Y = 0.5f * (2.0f * value2.Y + (-value1.Y + value3.Y) * amount +
(2.0f * value1.Y - 5.0f * value2.Y + 4.0f * value3.Y - value4.Y) * squared + (2.0f * value1.Y - 5.0f * value2.Y + 4.0f * value3.Y - value4.Y) * squared +
(-value1.Y + 3.0f * value2.Y - 3.0f * value3.Y + value4.Y) * cubed); (-value1.Y + 3.0f * value2.Y - 3.0f * value3.Y + value4.Y) * cubed);
@@ -1119,7 +1083,6 @@ namespace FlaxEngine
public static void Reflect(ref Vector2 vector, ref Vector2 normal, out Vector2 result) public static void Reflect(ref Vector2 vector, ref Vector2 normal, out Vector2 result)
{ {
float dot = vector.X * normal.X + vector.Y * normal.Y; float dot = vector.X * normal.X + vector.Y * normal.Y;
result.X = vector.X - 2.0f * dot * normal.X; result.X = vector.X - 2.0f * dot * normal.X;
result.Y = vector.Y - 2.0f * dot * normal.Y; result.Y = vector.Y - 2.0f * dot * normal.Y;
} }
@@ -1165,20 +1128,18 @@ namespace FlaxEngine
//q3 = m3 - ((q1 ⋅ m3) / (q1 ⋅ q1)) * q1 - ((q2 ⋅ m3) / (q2 ⋅ q2)) * q2 //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 //q4 = m4 - ((q1 ⋅ m4) / (q1 ⋅ q1)) * q1 - ((q2 ⋅ m4) / (q2 ⋅ q2)) * q2 - ((q3 ⋅ m4) / (q3 ⋅ q3)) * q3
//q5 = ... //q5 = ...
if (source == null) if (source == null)
throw new ArgumentNullException(nameof(source)); throw new ArgumentNullException(nameof(source));
if (destination == null) if (destination == null)
throw new ArgumentNullException(nameof(destination)); throw new ArgumentNullException(nameof(destination));
if (destination.Length < source.Length) 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."); 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) for (var i = 0; i < source.Length; ++i)
{ {
Vector2 newvector = source[i]; Vector2 v = source[i];
for (var r = 0; r < i; ++r) for (var r = 0; r < i; ++r)
newvector -= Dot(destination[r], newvector) / Dot(destination[r], destination[r]) * destination[r]; v -= Dot(destination[r], v) / Dot(destination[r], destination[r]) * destination[r];
destination[i] = newvector; destination[i] = v;
} }
} }
@@ -1212,21 +1173,19 @@ namespace FlaxEngine
//q3 = (m3 - (q1 ⋅ m3) * q1 - (q2 ⋅ m3) * q2) / |m3 - (q1 ⋅ m3) * q1 - (q2 ⋅ m3) * q2| //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| //q4 = (m4 - (q1 ⋅ m4) * q1 - (q2 ⋅ m4) * q2 - (q3 ⋅ m4) * q3) / |m4 - (q1 ⋅ m4) * q1 - (q2 ⋅ m4) * q2 - (q3 ⋅ m4) * q3|
//q5 = ... //q5 = ...
if (source == null) if (source == null)
throw new ArgumentNullException(nameof(source)); throw new ArgumentNullException(nameof(source));
if (destination == null) if (destination == null)
throw new ArgumentNullException(nameof(destination)); throw new ArgumentNullException(nameof(destination));
if (destination.Length < source.Length) 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."); 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) for (var i = 0; i < source.Length; ++i)
{ {
Vector2 newvector = source[i]; Vector2 v = source[i];
for (var r = 0; r < i; ++r) for (var r = 0; r < i; ++r)
newvector -= Dot(destination[r], newvector) * destination[r]; v -= Dot(destination[r], v) * destination[r];
newvector.Normalize(); v.Normalize();
destination[i] = newvector; destination[i] = v;
} }
} }
@@ -1277,7 +1236,6 @@ namespace FlaxEngine
throw new ArgumentNullException(nameof(destination)); throw new ArgumentNullException(nameof(destination));
if (destination.Length < source.Length) 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."); throw new ArgumentOutOfRangeException(nameof(destination), "The destination array must be of same length or larger length than the source array.");
float x = rotation.X + rotation.X; float x = rotation.X + rotation.X;
float y = rotation.Y + rotation.Y; float y = rotation.Y + rotation.Y;
float z = rotation.Z + rotation.Z; float z = rotation.Z + rotation.Z;
@@ -1286,16 +1244,12 @@ namespace FlaxEngine
float xy = rotation.X * y; float xy = rotation.X * y;
float yy = rotation.Y * y; float yy = rotation.Y * y;
float zz = rotation.Z * z; float zz = rotation.Z * z;
float num1 = 1.0f - yy - zz; float num1 = 1.0f - yy - zz;
float num2 = xy - wz; float num2 = xy - wz;
float num3 = xy + wz; float num3 = xy + wz;
float num4 = 1.0f - xx - zz; float num4 = 1.0f - xx - zz;
for (var i = 0; i < source.Length; ++i) for (var i = 0; i < source.Length; ++i)
destination[i] = new Vector2( destination[i] = new Vector2(source[i].X * num1 + source[i].Y * num2, source[i].X * num3 + source[i].Y * num4);
source[i].X * num1 + source[i].Y * num2,
source[i].X * num3 + source[i].Y * num4);
} }
/// <summary> /// <summary>
@@ -1306,11 +1260,10 @@ namespace FlaxEngine
/// <param name="result">When the method completes, contains the transformed <see cref="Vector4" />.</param> /// <param name="result">When the method completes, contains the transformed <see cref="Vector4" />.</param>
public static void Transform(ref Vector2 vector, ref Matrix transform, out Vector4 result) public static void Transform(ref Vector2 vector, ref Matrix transform, out Vector4 result)
{ {
result = new Vector4( result = new Vector4(vector.X * transform.M11 + vector.Y * transform.M21 + transform.M41,
vector.X * transform.M11 + vector.Y * transform.M21 + transform.M41, vector.X * transform.M12 + vector.Y * transform.M22 + transform.M42,
vector.X * transform.M12 + vector.Y * transform.M22 + transform.M42, vector.X * transform.M13 + vector.Y * transform.M23 + transform.M43,
vector.X * transform.M13 + vector.Y * transform.M23 + transform.M43, vector.X * transform.M14 + vector.Y * transform.M24 + transform.M44);
vector.X * transform.M14 + vector.Y * transform.M24 + transform.M44);
} }
/// <summary> /// <summary>
@@ -1431,9 +1384,8 @@ namespace FlaxEngine
/// </remarks> /// </remarks>
public static void TransformNormal(ref Vector2 normal, ref Matrix transform, out Vector2 result) public static void TransformNormal(ref Vector2 normal, ref Matrix transform, out Vector2 result)
{ {
result = new Vector2( result = new Vector2(normal.X * transform.M11 + normal.Y * transform.M21,
normal.X * transform.M11 + normal.Y * transform.M21, normal.X * transform.M12 + normal.Y * transform.M22);
normal.X * transform.M12 + normal.Y * transform.M22);
} }
/// <summary> /// <summary>
@@ -1474,13 +1426,10 @@ namespace FlaxEngine
{ {
if (source == null) if (source == null)
throw new ArgumentNullException(nameof(source)); throw new ArgumentNullException(nameof(source));
if (destination == null) if (destination == null)
throw new ArgumentNullException(nameof(destination)); throw new ArgumentNullException(nameof(destination));
if (destination.Length < source.Length) 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."); 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) for (var i = 0; i < source.Length; ++i)
TransformNormal(ref source[i], ref transform, out destination[i]); TransformNormal(ref source[i], ref transform, out destination[i]);
} }
@@ -1627,7 +1576,7 @@ namespace FlaxEngine
} }
/// <summary> /// <summary>
/// Perform a component-wise addition /// Performs a component-wise addition.
/// </summary> /// </summary>
/// <param name="value">The input vector.</param> /// <param name="value">The input vector.</param>
/// <param name="scalar">The scalar value to be added on elements</param> /// <param name="scalar">The scalar value to be added on elements</param>
@@ -1638,7 +1587,7 @@ namespace FlaxEngine
} }
/// <summary> /// <summary>
/// Perform a component-wise addition /// Performs a component-wise addition.
/// </summary> /// </summary>
/// <param name="value">The input vector.</param> /// <param name="value">The input vector.</param>
/// <param name="scalar">The scalar value to be added on elements</param> /// <param name="scalar">The scalar value to be added on elements</param>
@@ -1649,7 +1598,7 @@ namespace FlaxEngine
} }
/// <summary> /// <summary>
/// Perform a component-wise subtraction /// Performs a component-wise subtraction.
/// </summary> /// </summary>
/// <param name="value">The input vector.</param> /// <param name="value">The input vector.</param>
/// <param name="scalar">The scalar value to be subtracted from elements</param> /// <param name="scalar">The scalar value to be subtracted from elements</param>
@@ -1660,7 +1609,7 @@ namespace FlaxEngine
} }
/// <summary> /// <summary>
/// Perform a component-wise subtraction /// Performs a component-wise subtraction.
/// </summary> /// </summary>
/// <param name="value">The input vector.</param> /// <param name="value">The input vector.</param>
/// <param name="scalar">The scalar value to be subtracted from elements</param> /// <param name="scalar">The scalar value to be subtracted from elements</param>

View File

@@ -18,7 +18,7 @@ struct Color;
struct Matrix; struct Matrix;
/// <summary> /// <summary>
/// Represents a two dimensional mathematical vector. /// Represents a two dimensional mathematical vector with 32-bit precision (per-component).
/// </summary> /// </summary>
API_STRUCT() struct FLAXENGINE_API Vector2 API_STRUCT() struct FLAXENGINE_API Vector2
{ {
@@ -112,7 +112,7 @@ public:
// Init // Init
// @param xy Double2 to use X and Y components // @param xy Double2 to use X and Y components
explicit Vector2(const Double2& xy); Vector2(const Double2& xy);
// Init // Init
// @param xyz Double3 to use X and Y components // @param xyz Double3 to use X and Y components

View File

@@ -279,7 +279,6 @@ namespace FlaxEngine
} }
throw new ArgumentOutOfRangeException(nameof(index), "Indices for Vector3 run from 0 to 2, inclusive."); throw new ArgumentOutOfRangeException(nameof(index), "Indices for Vector3 run from 0 to 2, inclusive.");
} }
set set
{ {
switch (index) switch (index)
@@ -353,12 +352,7 @@ namespace FlaxEngine
/// <returns>A three-element array containing the components of the vector.</returns> /// <returns>A three-element array containing the components of the vector.</returns>
public float[] ToArray() public float[] ToArray()
{ {
return new[] return new[] { X, Y, Z };
{
X,
Y,
Z
};
} }
/// <summary> /// <summary>
@@ -384,7 +378,7 @@ namespace FlaxEngine
} }
/// <summary> /// <summary>
/// Perform a component-wise addition /// Performs a component-wise addition.
/// </summary> /// </summary>
/// <param name="left">The input vector</param> /// <param name="left">The input vector</param>
/// <param name="right">The scalar value to be added to elements</param> /// <param name="right">The scalar value to be added to elements</param>
@@ -395,7 +389,7 @@ namespace FlaxEngine
} }
/// <summary> /// <summary>
/// Perform a component-wise addition /// Performs a component-wise addition.
/// </summary> /// </summary>
/// <param name="left">The input vector</param> /// <param name="left">The input vector</param>
/// <param name="right">The scalar value to be added to elements</param> /// <param name="right">The scalar value to be added to elements</param>
@@ -428,7 +422,7 @@ namespace FlaxEngine
} }
/// <summary> /// <summary>
/// Perform a component-wise subtraction /// Performs a component-wise subtraction.
/// </summary> /// </summary>
/// <param name="left">The input vector</param> /// <param name="left">The input vector</param>
/// <param name="right">The scalar value to be subtracted from elements</param> /// <param name="right">The scalar value to be subtracted from elements</param>
@@ -439,7 +433,7 @@ namespace FlaxEngine
} }
/// <summary> /// <summary>
/// Perform a component-wise subtraction /// Performs a component-wise subtraction.
/// </summary> /// </summary>
/// <param name="left">The input vector</param> /// <param name="left">The input vector</param>
/// <param name="right">The scalar value to be subtracted from elements</param> /// <param name="right">The scalar value to be subtracted from elements</param>
@@ -450,7 +444,7 @@ namespace FlaxEngine
} }
/// <summary> /// <summary>
/// Perform a component-wise subtraction /// Performs a component-wise subtraction.
/// </summary> /// </summary>
/// <param name="left">The scalar value to be subtracted from elements</param> /// <param name="left">The scalar value to be subtracted from elements</param>
/// <param name="right">The input vector.</param> /// <param name="right">The input vector.</param>
@@ -461,7 +455,7 @@ namespace FlaxEngine
} }
/// <summary> /// <summary>
/// Perform a component-wise subtraction /// Performs a component-wise subtraction.
/// </summary> /// </summary>
/// <param name="left">The scalar value to be subtracted from elements</param> /// <param name="left">The scalar value to be subtracted from elements</param>
/// <param name="right">The input vector.</param> /// <param name="right">The input vector.</param>
@@ -602,8 +596,7 @@ namespace FlaxEngine
} }
/// <summary> /// <summary>
/// Returns a <see cref="Vector3" /> containing the 3D Cartesian coordinates of a point specified in Barycentric /// Returns a <see cref="Vector3" /> containing the 3D Cartesian coordinates of a point specified in Barycentric coordinates relative to a 3D triangle.
/// coordinates relative to a 3D triangle.
/// </summary> /// </summary>
/// <param name="value1">A <see cref="Vector3" /> containing the 3D Cartesian coordinates of vertex 1 of the triangle.</param> /// <param name="value1">A <see cref="Vector3" /> containing the 3D Cartesian coordinates of vertex 1 of the triangle.</param>
/// <param name="value2">A <see cref="Vector3" /> containing the 3D Cartesian coordinates of vertex 2 of the triangle.</param> /// <param name="value2">A <see cref="Vector3" /> containing the 3D Cartesian coordinates of vertex 2 of the triangle.</param>
@@ -619,8 +612,7 @@ namespace FlaxEngine
} }
/// <summary> /// <summary>
/// Returns a <see cref="Vector3" /> containing the 3D Cartesian coordinates of a point specified in Barycentric /// Returns a <see cref="Vector3" /> containing the 3D Cartesian coordinates of a point specified in Barycentric coordinates relative to a 3D triangle.
/// coordinates relative to a 3D triangle.
/// </summary> /// </summary>
/// <param name="value1">A <see cref="Vector3" /> containing the 3D Cartesian coordinates of vertex 1 of the triangle.</param> /// <param name="value1">A <see cref="Vector3" /> containing the 3D Cartesian coordinates of vertex 1 of the triangle.</param>
/// <param name="value2">A <see cref="Vector3" /> containing the 3D Cartesian coordinates of vertex 2 of the triangle.</param> /// <param name="value2">A <see cref="Vector3" /> containing the 3D Cartesian coordinates of vertex 2 of the triangle.</param>
@@ -646,15 +638,12 @@ namespace FlaxEngine
float x = value.X; float x = value.X;
x = x > max.X ? max.X : x; x = x > max.X ? max.X : x;
x = x < min.X ? min.X : x; x = x < min.X ? min.X : x;
float y = value.Y; float y = value.Y;
y = y > max.Y ? max.Y : y; y = y > max.Y ? max.Y : y;
y = y < min.Y ? min.Y : y; y = y < min.Y ? min.Y : y;
float z = value.Z; float z = value.Z;
z = z > max.Z ? max.Z : z; z = z > max.Z ? max.Z : z;
z = z < min.Z ? min.Z : z; z = z < min.Z ? min.Z : z;
result = new Vector3(x, y, z); result = new Vector3(x, y, z);
} }
@@ -747,14 +736,6 @@ namespace FlaxEngine
/// <param name="value1">The first vector.</param> /// <param name="value1">The first vector.</param>
/// <param name="value2">The second vector.</param> /// <param name="value2">The second vector.</param>
/// <param name="result">When the method completes, contains the squared distance between the two vectors.</param> /// <param name="result">When the method completes, contains the squared distance between the two vectors.</param>
/// <remarks>
/// Distance squared is the value before taking the square root.
/// Distance squared can often be used in place of distance if relative comparisons are being made.
/// For example, consider three points A, B, and C. To determine whether B or C is further from A,
/// compare the distance between A and B to the distance between A and C. Calculating the two distances
/// involves two square roots, which are computationally expensive. However, using distance squared
/// provides the same information and avoids calculating two square roots.
/// </remarks>
public static void DistanceSquared(ref Vector3 value1, ref Vector3 value2, out float result) public static void DistanceSquared(ref Vector3 value1, ref Vector3 value2, out float result)
{ {
float x = value1.X - value2.X; float x = value1.X - value2.X;
@@ -769,14 +750,6 @@ namespace FlaxEngine
/// <param name="value1">The first vector.</param> /// <param name="value1">The first vector.</param>
/// <param name="value2">The second vector.</param> /// <param name="value2">The second vector.</param>
/// <returns>The squared distance between the two vectors.</returns> /// <returns>The squared distance between the two vectors.</returns>
/// <remarks>
/// Distance squared is the value before taking the square root.
/// Distance squared can often be used in place of distance if relative comparisons are being made.
/// For example, consider three points A, B, and C. To determine whether B or C is further from A,
/// compare the distance between A and B to the distance between A and C. Calculating the two distances
/// involves two square roots, which are computationally expensive. However, using distance squared
/// provides the same information and avoids calculating two square roots.
/// </remarks>
public static float DistanceSquared(ref Vector3 value1, ref Vector3 value2) public static float DistanceSquared(ref Vector3 value1, ref Vector3 value2)
{ {
float x = value1.X - value2.X; float x = value1.X - value2.X;
@@ -791,14 +764,6 @@ namespace FlaxEngine
/// <param name="value1">The first vector.</param> /// <param name="value1">The first vector.</param>
/// <param name="value2">The second vector.</param> /// <param name="value2">The second vector.</param>
/// <returns>The squared distance between the two vectors.</returns> /// <returns>The squared distance between the two vectors.</returns>
/// <remarks>
/// Distance squared is the value before taking the square root.
/// Distance squared can often be used in place of distance if relative comparisons are being made.
/// For example, consider three points A, B, and C. To determine whether B or C is further from A,
/// compare the distance between A and B to the distance between A and C. Calculating the two distances
/// involves two square roots, which are computationally expensive. However, using distance squared
/// provides the same information and avoids calculating two square roots.
/// </remarks>
public static float DistanceSquared(Vector3 value1, Vector3 value2) public static float DistanceSquared(Vector3 value1, Vector3 value2)
{ {
float x = value1.X - value2.X; float x = value1.X - value2.X;
@@ -982,7 +947,7 @@ namespace FlaxEngine
/// <param name="max">Max Length</param> /// <param name="max">Max Length</param>
public static Vector3 ClampLength(Vector3 vector, float min, float max) public static Vector3 ClampLength(Vector3 vector, float min, float max)
{ {
ClampLength(ref vector, min, max, out Vector3 retVect); ClampLength(vector, min, max, out Vector3 retVect);
return retVect; return retVect;
} }
@@ -992,27 +957,26 @@ namespace FlaxEngine
/// <param name="vector">Input Vector.</param> /// <param name="vector">Input Vector.</param>
/// <param name="min">Min Length</param> /// <param name="min">Min Length</param>
/// <param name="max">Max Length</param> /// <param name="max">Max Length</param>
/// <param name="retVect">The Return Vector</param> /// <param name="result">The result vector.</param>
public static void ClampLength(ref Vector3 vector, float min, float max, out Vector3 retVect) public static void ClampLength(Vector3 vector, float min, float max, out Vector3 result)
{ {
retVect.X = vector.X; result.X = vector.X;
retVect.Y = vector.Y; result.Y = vector.Y;
retVect.Z = vector.Z; result.Z = vector.Z;
float lenSq = result.LengthSquared;
float lenSq = retVect.LengthSquared;
if (lenSq > max * max) if (lenSq > max * max)
{ {
float scaleFactor = max / (float)Math.Sqrt(lenSq); float scaleFactor = max / (float)Math.Sqrt(lenSq);
retVect.X = retVect.X * scaleFactor; result.X *= scaleFactor;
retVect.Y = retVect.Y * scaleFactor; result.Y *= scaleFactor;
retVect.Z = retVect.Z * scaleFactor; result.Z *= scaleFactor;
} }
if (lenSq < min * min) if (lenSq < min * min)
{ {
float scaleFactor = min / (float)Math.Sqrt(lenSq); float scaleFactor = min / (float)Math.Sqrt(lenSq);
retVect.X = retVect.X * scaleFactor; result.X *= scaleFactor;
retVect.Y = retVect.Y * scaleFactor; result.Y *= scaleFactor;
retVect.Z = retVect.Z * scaleFactor; result.Z *= scaleFactor;
} }
} }
@@ -1088,7 +1052,6 @@ namespace FlaxEngine
float part2 = -2.0f * cubed + 3.0f * squared; float part2 = -2.0f * cubed + 3.0f * squared;
float part3 = cubed - 2.0f * squared + amount; float part3 = cubed - 2.0f * squared + amount;
float part4 = cubed - squared; float part4 = cubed - squared;
result.X = value1.X * part1 + value2.X * part2 + tangent1.X * part3 + tangent2.X * part4; 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.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; result.Z = value1.Z * part1 + value2.Z * part2 + tangent1.Z * part3 + tangent2.Z * part4;
@@ -1122,15 +1085,12 @@ namespace FlaxEngine
{ {
float squared = amount * amount; float squared = amount * amount;
float cubed = amount * squared; float cubed = amount * squared;
result.X = 0.5f * (2.0f * value2.X + (-value1.X + value3.X) * amount + result.X = 0.5f * (2.0f * value2.X + (-value1.X + value3.X) * amount +
(2.0f * value1.X - 5.0f * value2.X + 4.0f * value3.X - value4.X) * squared + (2.0f * value1.X - 5.0f * value2.X + 4.0f * value3.X - value4.X) * squared +
(-value1.X + 3.0f * value2.X - 3.0f * value3.X + value4.X) * cubed); (-value1.X + 3.0f * value2.X - 3.0f * value3.X + value4.X) * cubed);
result.Y = 0.5f * (2.0f * value2.Y + (-value1.Y + value3.Y) * amount + result.Y = 0.5f * (2.0f * value2.Y + (-value1.Y + value3.Y) * amount +
(2.0f * value1.Y - 5.0f * value2.Y + 4.0f * value3.Y - value4.Y) * squared + (2.0f * value1.Y - 5.0f * value2.Y + 4.0f * value3.Y - value4.Y) * squared +
(-value1.Y + 3.0f * value2.Y - 3.0f * value3.Y + value4.Y) * cubed); (-value1.Y + 3.0f * value2.Y - 3.0f * value3.Y + value4.Y) * cubed);
result.Z = 0.5f * (2.0f * value2.Z + (-value1.Z + value3.Z) * amount + result.Z = 0.5f * (2.0f * value2.Z + (-value1.Z + value3.Z) * amount +
(2.0f * value1.Z - 5.0f * value2.Z + 4.0f * value3.Z - value4.Z) * squared + (2.0f * value1.Z - 5.0f * value2.Z + 4.0f * value3.Z - value4.Z) * squared +
(-value1.Z + 3.0f * value2.Z - 3.0f * value3.Z + value4.Z) * cubed); (-value1.Z + 3.0f * value2.Z - 3.0f * value3.Z + value4.Z) * cubed);
@@ -1265,7 +1225,6 @@ namespace FlaxEngine
public static void Project(ref Vector3 vector, float x, float y, float width, float height, float minZ, float maxZ, ref Matrix worldViewProjection, out Vector3 result) public static void Project(ref Vector3 vector, float x, float y, float width, float height, float minZ, float maxZ, ref Matrix worldViewProjection, out Vector3 result)
{ {
TransformCoordinate(ref vector, ref worldViewProjection, out var v); TransformCoordinate(ref vector, ref worldViewProjection, out var v);
result = new Vector3((1.0f + v.X) * 0.5f * width + x, (1.0f - v.Y) * 0.5f * height + y, v.Z * (maxZ - minZ) + minZ); result = new Vector3((1.0f + v.X) * 0.5f * width + x, (1.0f - v.Y) * 0.5f * height + y, v.Z * (maxZ - minZ) + minZ);
} }
@@ -1301,13 +1260,13 @@ namespace FlaxEngine
/// <param name="result">When the method completes, contains the vector in object space.</param> /// <param name="result">When the method completes, contains the vector in object space.</param>
public static void Unproject(ref Vector3 vector, float x, float y, float width, float height, float minZ, float maxZ, ref Matrix worldViewProjection, out Vector3 result) public static void Unproject(ref Vector3 vector, float x, float y, float width, float height, float minZ, float maxZ, ref Matrix worldViewProjection, out Vector3 result)
{ {
var v = new Vector3();
Matrix.Invert(ref worldViewProjection, out var matrix); Matrix.Invert(ref worldViewProjection, out var matrix);
var v = new Vector3
v.X = (vector.X - x) / width * 2.0f - 1.0f; {
v.Y = -((vector.Y - y) / height * 2.0f - 1.0f); X = (vector.X - x) / width * 2.0f - 1.0f,
v.Z = (vector.Z - minZ) / (maxZ - minZ); Y = -((vector.Y - y) / height * 2.0f - 1.0f),
Z = (vector.Z - minZ) / (maxZ - minZ)
};
TransformCoordinate(ref v, ref matrix, out result); TransformCoordinate(ref v, ref matrix, out result);
} }
@@ -1342,7 +1301,6 @@ namespace FlaxEngine
public static void Reflect(ref Vector3 vector, ref Vector3 normal, out Vector3 result) public static void Reflect(ref Vector3 vector, ref Vector3 normal, out Vector3 result)
{ {
float dot = vector.X * normal.X + vector.Y * normal.Y + vector.Z * normal.Z; float dot = vector.X * normal.X + vector.Y * normal.Y + vector.Z * normal.Z;
result.X = vector.X - 2.0f * dot * normal.X; result.X = vector.X - 2.0f * dot * normal.X;
result.Y = vector.Y - 2.0f * dot * normal.Y; result.Y = vector.Y - 2.0f * dot * normal.Y;
result.Z = vector.Z - 2.0f * dot * normal.Z; result.Z = vector.Z - 2.0f * dot * normal.Z;
@@ -1389,22 +1347,18 @@ namespace FlaxEngine
//q3 = m3 - ((q1 ⋅ m3) / (q1 ⋅ q1)) * q1 - ((q2 ⋅ m3) / (q2 ⋅ q2)) * q2 //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 //q4 = m4 - ((q1 ⋅ m4) / (q1 ⋅ q1)) * q1 - ((q2 ⋅ m4) / (q2 ⋅ q2)) * q2 - ((q3 ⋅ m4) / (q3 ⋅ q3)) * q3
//q5 = ... //q5 = ...
if (source == null) if (source == null)
throw new ArgumentNullException(nameof(source)); throw new ArgumentNullException(nameof(source));
if (destination == null) if (destination == null)
throw new ArgumentNullException(nameof(destination)); throw new ArgumentNullException(nameof(destination));
if (destination.Length < source.Length) 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."); 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) for (var i = 0; i < source.Length; ++i)
{ {
Vector3 s = source[i]; Vector3 v = source[i];
for (var r = 0; r < i; ++r) for (var r = 0; r < i; ++r)
s -= Dot(destination[r], s) / Dot(destination[r], destination[r]) * destination[r]; v -= Dot(destination[r], v) / Dot(destination[r], destination[r]) * destination[r];
destination[i] = v;
destination[i] = s;
} }
} }
@@ -1438,23 +1392,19 @@ namespace FlaxEngine
//q3 = (m3 - (q1 ⋅ m3) * q1 - (q2 ⋅ m3) * q2) / |m3 - (q1 ⋅ m3) * q1 - (q2 ⋅ m3) * q2| //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| //q4 = (m4 - (q1 ⋅ m4) * q1 - (q2 ⋅ m4) * q2 - (q3 ⋅ m4) * q3) / |m4 - (q1 ⋅ m4) * q1 - (q2 ⋅ m4) * q2 - (q3 ⋅ m4) * q3|
//q5 = ... //q5 = ...
if (source == null) if (source == null)
throw new ArgumentNullException(nameof(source)); throw new ArgumentNullException(nameof(source));
if (destination == null) if (destination == null)
throw new ArgumentNullException(nameof(destination)); throw new ArgumentNullException(nameof(destination));
if (destination.Length < source.Length) 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."); 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) for (var i = 0; i < source.Length; ++i)
{ {
Vector3 s = source[i]; Vector3 v = source[i];
for (var r = 0; r < i; ++r) for (var r = 0; r < i; ++r)
s -= Dot(destination[r], s) * destination[r]; v -= Dot(destination[r], v) * destination[r];
v.Normalize();
s.Normalize(); destination[i] = v;
destination[i] = s;
} }
} }
@@ -1478,7 +1428,6 @@ namespace FlaxEngine
float yy = rotation.Y * y; float yy = rotation.Y * y;
float yz = rotation.Y * z; float yz = rotation.Y * z;
float zz = rotation.Z * z; float zz = rotation.Z * z;
result = new Vector3(vector.X * (1.0f - yy - zz) + vector.Y * (xy - wz) + vector.Z * (xz + wy), result = new Vector3(vector.X * (1.0f - yy - zz) + vector.Y * (xy - wz) + vector.Z * (xz + wy),
vector.X * (xy + wz) + vector.Y * (1.0f - xx - zz) + vector.Z * (yz - wx), vector.X * (xy + wz) + vector.Y * (1.0f - xx - zz) + vector.Z * (yz - wx),
vector.X * (xz - wy) + vector.Y * (yz + wx) + vector.Z * (1.0f - xx - yy)); vector.X * (xz - wy) + vector.Y * (yz + wx) + vector.Z * (1.0f - xx - yy));
@@ -1512,7 +1461,6 @@ namespace FlaxEngine
throw new ArgumentNullException(nameof(destination)); throw new ArgumentNullException(nameof(destination));
if (destination.Length < source.Length) 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."); throw new ArgumentOutOfRangeException(nameof(destination), "The destination array must be of same length or larger length than the source array.");
float x = rotation.X + rotation.X; float x = rotation.X + rotation.X;
float y = rotation.Y + rotation.Y; float y = rotation.Y + rotation.Y;
float z = rotation.Z + rotation.Z; float z = rotation.Z + rotation.Z;
@@ -1525,7 +1473,6 @@ namespace FlaxEngine
float yy = rotation.Y * y; float yy = rotation.Y * y;
float yz = rotation.Y * z; float yz = rotation.Y * z;
float zz = rotation.Z * z; float zz = rotation.Z * z;
float num1 = 1.0f - yy - zz; float num1 = 1.0f - yy - zz;
float num2 = xy - wz; float num2 = xy - wz;
float num3 = xz + wy; float num3 = xz + wy;
@@ -1535,7 +1482,6 @@ namespace FlaxEngine
float num7 = xz - wy; float num7 = xz - wy;
float num8 = yz + wx; float num8 = yz + wx;
float num9 = 1.0f - xx - yy; float num9 = 1.0f - xx - yy;
for (var i = 0; i < source.Length; i++) for (var i = 0; i < source.Length; i++)
{ {
destination[i] = new Vector3(source[i].X * num1 + source[i].Y * num2 + source[i].Z * num3, destination[i] = new Vector3(source[i].X * num1 + source[i].Y * num2 + source[i].Z * num3,
@@ -1624,7 +1570,6 @@ namespace FlaxEngine
throw new ArgumentNullException(nameof(destination)); throw new ArgumentNullException(nameof(destination));
if (destination.Length < source.Length) 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."); 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) for (var i = 0; i < source.Length; ++i)
Transform(ref source[i], ref transform, out destination[i]); Transform(ref source[i], ref transform, out destination[i]);
} }
@@ -1651,7 +1596,6 @@ namespace FlaxEngine
Z = coordinate.X * transform.M13 + coordinate.Y * transform.M23 + coordinate.Z * transform.M33 + transform.M43, 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) W = 1f / (coordinate.X * transform.M14 + coordinate.Y * transform.M24 + coordinate.Z * transform.M34 + transform.M44)
}; };
result = new Vector3(vector.X * vector.W, vector.Y * vector.W, vector.Z * vector.W); result = new Vector3(vector.X * vector.W, vector.Y * vector.W, vector.Z * vector.W);
} }
@@ -1699,7 +1643,6 @@ namespace FlaxEngine
throw new ArgumentNullException(nameof(destination)); throw new ArgumentNullException(nameof(destination));
if (destination.Length < source.Length) 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."); 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++) for (var i = 0; i < source.Length; i++)
TransformCoordinate(ref source[i], ref transform, out destination[i]); TransformCoordinate(ref source[i], ref transform, out destination[i]);
} }
@@ -1766,7 +1709,6 @@ namespace FlaxEngine
throw new ArgumentNullException(nameof(destination)); throw new ArgumentNullException(nameof(destination));
if (destination.Length < source.Length) 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."); 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) for (var i = 0; i < source.Length; ++i)
TransformNormal(ref source[i], ref transform, out destination[i]); TransformNormal(ref source[i], ref transform, out destination[i]);
} }
@@ -1783,8 +1725,7 @@ namespace FlaxEngine
} }
/// <summary> /// <summary>
/// Multiplies a vector with another by performing component-wise multiplication equivalent to /// Multiplies a vector with another by performing component-wise multiplication equivalent to <see cref="Multiply(ref Vector3,ref Vector3,out Vector3)" />.
/// <see cref="Multiply(ref Vector3,ref Vector3,out Vector3)" />.
/// </summary> /// </summary>
/// <param name="left">The first vector to multiply.</param> /// <param name="left">The first vector to multiply.</param>
/// <param name="right">The second vector to multiply.</param> /// <param name="right">The second vector to multiply.</param>
@@ -1926,7 +1867,7 @@ namespace FlaxEngine
} }
/// <summary> /// <summary>
/// Perform a component-wise addition /// Performs a component-wise addition.
/// </summary> /// </summary>
/// <param name="value">The input vector.</param> /// <param name="value">The input vector.</param>
/// <param name="scalar">The scalar value to be added on elements</param> /// <param name="scalar">The scalar value to be added on elements</param>
@@ -1937,7 +1878,7 @@ namespace FlaxEngine
} }
/// <summary> /// <summary>
/// Perform a component-wise addition /// Performs a component-wise addition.
/// </summary> /// </summary>
/// <param name="value">The input vector.</param> /// <param name="value">The input vector.</param>
/// <param name="scalar">The scalar value to be added on elements</param> /// <param name="scalar">The scalar value to be added on elements</param>
@@ -1948,7 +1889,7 @@ namespace FlaxEngine
} }
/// <summary> /// <summary>
/// Perform a component-wise subtraction /// Performs a component-wise subtraction.
/// </summary> /// </summary>
/// <param name="value">The input vector.</param> /// <param name="value">The input vector.</param>
/// <param name="scalar">The scalar value to be subtracted from elements</param> /// <param name="scalar">The scalar value to be subtracted from elements</param>
@@ -1959,7 +1900,7 @@ namespace FlaxEngine
} }
/// <summary> /// <summary>
/// Perform a component-wise subtraction /// Performs a component-wise subtraction.
/// </summary> /// </summary>
/// <param name="value">The input vector.</param> /// <param name="value">The input vector.</param>
/// <param name="scalar">The scalar value to be subtracted from elements</param> /// <param name="scalar">The scalar value to be subtracted from elements</param>

View File

@@ -19,7 +19,7 @@ struct Int3;
struct Int4; struct Int4;
/// <summary> /// <summary>
/// Represents a three dimensional mathematical vector. /// Represents a three dimensional mathematical vector with 32-bit precision (per-component).
/// </summary> /// </summary>
API_STRUCT() struct FLAXENGINE_API Vector3 API_STRUCT() struct FLAXENGINE_API Vector3
{ {
@@ -167,11 +167,11 @@ public:
// Init // Init
// @param xyz Double3 value // @param xyz Double3 value
explicit Vector3(const Double3& xyz); Vector3(const Double3& xyz);
// Init // Init
// @param xyzw Double4 value // @param xyzw Double4 value
explicit Vector3(const Double4& xyzw); explicit Vector3(const Double4& xyzw);
// Init // Init
// @param color Color value // @param color Color value

View File

@@ -263,7 +263,6 @@ namespace FlaxEngine
} }
throw new ArgumentOutOfRangeException(nameof(index), "Indices for Vector4 run from 0 to 3, inclusive."); throw new ArgumentOutOfRangeException(nameof(index), "Indices for Vector4 run from 0 to 3, inclusive.");
} }
set set
{ {
switch (index) switch (index)
@@ -321,13 +320,7 @@ namespace FlaxEngine
/// <returns>A four-element array containing the components of the vector.</returns> /// <returns>A four-element array containing the components of the vector.</returns>
public float[] ToArray() public float[] ToArray()
{ {
return new[] return new[] { X, Y, Z, W };
{
X,
Y,
Z,
W
};
} }
/// <summary> /// <summary>
@@ -353,7 +346,7 @@ namespace FlaxEngine
} }
/// <summary> /// <summary>
/// Perform a component-wise addition /// Performs a component-wise addition.
/// </summary> /// </summary>
/// <param name="left">The input vector</param> /// <param name="left">The input vector</param>
/// <param name="right">The scalar value to be added to elements</param> /// <param name="right">The scalar value to be added to elements</param>
@@ -364,7 +357,7 @@ namespace FlaxEngine
} }
/// <summary> /// <summary>
/// Perform a component-wise addition /// Performs a component-wise addition.
/// </summary> /// </summary>
/// <param name="left">The input vector</param> /// <param name="left">The input vector</param>
/// <param name="right">The scalar value to be added to elements</param> /// <param name="right">The scalar value to be added to elements</param>
@@ -397,7 +390,7 @@ namespace FlaxEngine
} }
/// <summary> /// <summary>
/// Perform a component-wise subtraction /// Performs a component-wise subtraction.
/// </summary> /// </summary>
/// <param name="left">The input vector</param> /// <param name="left">The input vector</param>
/// <param name="right">The scalar value to be subtracted from elements</param> /// <param name="right">The scalar value to be subtracted from elements</param>
@@ -408,7 +401,7 @@ namespace FlaxEngine
} }
/// <summary> /// <summary>
/// Perform a component-wise subtraction /// Performs a component-wise subtraction.
/// </summary> /// </summary>
/// <param name="left">The input vector</param> /// <param name="left">The input vector</param>
/// <param name="right">The scalar value to be subtracted from elements</param> /// <param name="right">The scalar value to be subtracted from elements</param>
@@ -419,7 +412,7 @@ namespace FlaxEngine
} }
/// <summary> /// <summary>
/// Perform a component-wise subtraction /// Performs a component-wise subtraction.
/// </summary> /// </summary>
/// <param name="left">The scalar value to be subtracted from elements</param> /// <param name="left">The scalar value to be subtracted from elements</param>
/// <param name="right">The input vector.</param> /// <param name="right">The input vector.</param>
@@ -430,7 +423,7 @@ namespace FlaxEngine
} }
/// <summary> /// <summary>
/// Perform a component-wise subtraction /// Performs a component-wise subtraction.
/// </summary> /// </summary>
/// <param name="left">The scalar value to be subtracted from elements</param> /// <param name="left">The scalar value to be subtracted from elements</param>
/// <param name="right">The input vector.</param> /// <param name="right">The input vector.</param>
@@ -592,19 +585,15 @@ namespace FlaxEngine
float x = value.X; float x = value.X;
x = x > max.X ? max.X : x; x = x > max.X ? max.X : x;
x = x < min.X ? min.X : x; x = x < min.X ? min.X : x;
float y = value.Y; float y = value.Y;
y = y > max.Y ? max.Y : y; y = y > max.Y ? max.Y : y;
y = y < min.Y ? min.Y : y; y = y < min.Y ? min.Y : y;
float z = value.Z; float z = value.Z;
z = z > max.Z ? max.Z : z; z = z > max.Z ? max.Z : z;
z = z < min.Z ? min.Z : z; z = z < min.Z ? min.Z : z;
float w = value.W; float w = value.W;
w = w > max.W ? max.W : w; w = w > max.W ? max.W : w;
w = w < min.W ? min.W : w; w = w < min.W ? min.W : w;
result = new Vector4(x, y, z, w); result = new Vector4(x, y, z, w);
} }
@@ -659,14 +648,6 @@ namespace FlaxEngine
/// <param name="value1">The first vector.</param> /// <param name="value1">The first vector.</param>
/// <param name="value2">The second vector.</param> /// <param name="value2">The second vector.</param>
/// <param name="result">When the method completes, contains the squared distance between the two vectors.</param> /// <param name="result">When the method completes, contains the squared distance between the two vectors.</param>
/// <remarks>
/// Distance squared is the value before taking the square root.
/// Distance squared can often be used in place of distance if relative comparisons are being made.
/// For example, consider three points A, B, and C. To determine whether B or C is further from A,
/// compare the distance between A and B to the distance between A and C. Calculating the two distances
/// involves two square roots, which are computationally expensive. However, using distance squared
/// provides the same information and avoids calculating two square roots.
/// </remarks>
public static void DistanceSquared(ref Vector4 value1, ref Vector4 value2, out float result) public static void DistanceSquared(ref Vector4 value1, ref Vector4 value2, out float result)
{ {
float x = value1.X - value2.X; float x = value1.X - value2.X;
@@ -682,14 +663,6 @@ namespace FlaxEngine
/// <param name="value1">The first vector.</param> /// <param name="value1">The first vector.</param>
/// <param name="value2">The second vector.</param> /// <param name="value2">The second vector.</param>
/// <returns>The squared distance between the two vectors.</returns> /// <returns>The squared distance between the two vectors.</returns>
/// <remarks>
/// Distance squared is the value before taking the square root.
/// Distance squared can often be used in place of distance if relative comparisons are being made.
/// For example, consider three points A, B, and C. To determine whether B or C is further from A,
/// compare the distance between A and B to the distance between A and C. Calculating the two distances
/// involves two square roots, which are computationally expensive. However, using distance squared
/// provides the same information and avoids calculating two square roots.
/// </remarks>
public static float DistanceSquared(Vector4 value1, Vector4 value2) public static float DistanceSquared(Vector4 value1, Vector4 value2)
{ {
float x = value1.X - value2.X; float x = value1.X - value2.X;
@@ -720,9 +693,7 @@ namespace FlaxEngine
/// <returns><c>true</c> if left and right are near another, <c>false</c> otherwise</returns> /// <returns><c>true</c> if left and right are near another, <c>false</c> otherwise</returns>
public static bool NearEqual(ref Vector4 left, ref Vector4 right, float epsilon = Mathf.Epsilon) public static bool NearEqual(ref Vector4 left, ref Vector4 right, float epsilon = Mathf.Epsilon)
{ {
return Mathf.WithinEpsilon(left.X, right.X, epsilon) && return Mathf.WithinEpsilon(left.X, right.X, epsilon) && Mathf.WithinEpsilon(left.Y, right.Y, epsilon) && Mathf.WithinEpsilon(left.Z, right.Z, epsilon) && Mathf.WithinEpsilon(left.W, right.W, epsilon);
Mathf.WithinEpsilon(left.Y, right.Y, epsilon) &&
Mathf.WithinEpsilon(left.Z, right.Z, epsilon);
} }
/// <summary> /// <summary>
@@ -754,8 +725,7 @@ namespace FlaxEngine
/// <param name="result">When the method completes, contains the normalized vector.</param> /// <param name="result">When the method completes, contains the normalized vector.</param>
public static void Normalize(ref Vector4 value, out Vector4 result) public static void Normalize(ref Vector4 value, out Vector4 result)
{ {
Vector4 temp = value; result = value;
result = temp;
result.Normalize(); result.Normalize();
} }
@@ -788,8 +758,8 @@ namespace FlaxEngine
/// <param name="max">Max Length</param> /// <param name="max">Max Length</param>
public static Vector4 ClampLength(Vector4 vector, float min, float max) public static Vector4 ClampLength(Vector4 vector, float min, float max)
{ {
ClampLength(ref vector, min, max, out Vector4 retVect); ClampLength(vector, min, max, out Vector4 result);
return retVect; return result;
} }
/// <summary> /// <summary>
@@ -798,30 +768,26 @@ namespace FlaxEngine
/// <param name="vector">Input Vector.</param> /// <param name="vector">Input Vector.</param>
/// <param name="min">Min Length</param> /// <param name="min">Min Length</param>
/// <param name="max">Max Length</param> /// <param name="max">Max Length</param>
/// <param name="retVect">The Return Vector</param> /// <param name="result">The result vector.</param>
public static void ClampLength(ref Vector4 vector, float min, float max, out Vector4 retVect) public static void ClampLength(Vector4 vector, float min, float max, out Vector4 result)
{ {
retVect.X = vector.X; result = vector;
retVect.Y = vector.Y; float lenSq = result.LengthSquared;
retVect.Z = vector.Z;
retVect.W = vector.W;
float lenSq = retVect.LengthSquared;
if (lenSq > max * max) if (lenSq > max * max)
{ {
float scaleFactor = max / (float)Math.Sqrt(lenSq); float scaleFactor = max / (float)Math.Sqrt(lenSq);
retVect.X = retVect.X * scaleFactor; result.X *= scaleFactor;
retVect.Y = retVect.Y * scaleFactor; result.Y *= scaleFactor;
retVect.Z = retVect.Z * scaleFactor; result.Z *= scaleFactor;
retVect.W = retVect.W * scaleFactor; result.W *= scaleFactor;
} }
if (lenSq < min * min) if (lenSq < min * min)
{ {
float scaleFactor = min / (float)Math.Sqrt(lenSq); float scaleFactor = min / (float)Math.Sqrt(lenSq);
retVect.X = retVect.X * scaleFactor; result.X *= scaleFactor;
retVect.Y = retVect.Y * scaleFactor; result.Y *= scaleFactor;
retVect.Z = retVect.Z * scaleFactor; result.Z *= scaleFactor;
retVect.W = retVect.W * scaleFactor; result.W *= scaleFactor;
} }
} }
@@ -898,7 +864,6 @@ namespace FlaxEngine
float part2 = -2.0f * cubed + 3.0f * squared; float part2 = -2.0f * cubed + 3.0f * squared;
float part3 = cubed - 2.0f * squared + amount; float part3 = cubed - 2.0f * squared + amount;
float part4 = cubed - squared; float part4 = cubed - squared;
result = new Vector4(value1.X * part1 + value2.X * part2 + tangent1.X * part3 + tangent2.X * part4, result = new Vector4(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.Y * part1 + value2.Y * part2 + tangent1.Y * part3 + tangent2.Y * part4,
value1.Z * part1 + value2.Z * part2 + tangent1.Z * part3 + tangent2.Z * part4, value1.Z * part1 + value2.Z * part2 + tangent1.Z * part3 + tangent2.Z * part4,
@@ -933,7 +898,6 @@ namespace FlaxEngine
{ {
float squared = amount * amount; float squared = amount * amount;
float cubed = amount * squared; float cubed = amount * squared;
result.X = 0.5f * (2.0f * value2.X + (-value1.X + value3.X) * amount + (2.0f * value1.X - 5.0f * value2.X + 4.0f * value3.X - value4.X) * squared + (-value1.X + 3.0f * value2.X - 3.0f * value3.X + value4.X) * cubed); result.X = 0.5f * (2.0f * value2.X + (-value1.X + value3.X) * amount + (2.0f * value1.X - 5.0f * value2.X + 4.0f * value3.X - value4.X) * squared + (-value1.X + 3.0f * value2.X - 3.0f * value3.X + value4.X) * cubed);
result.Y = 0.5f * (2.0f * value2.Y + (-value1.Y + value3.Y) * amount + (2.0f * value1.Y - 5.0f * value2.Y + 4.0f * value3.Y - value4.Y) * squared + (-value1.Y + 3.0f * value2.Y - 3.0f * value3.Y + value4.Y) * cubed); result.Y = 0.5f * (2.0f * value2.Y + (-value1.Y + value3.Y) * amount + (2.0f * value1.Y - 5.0f * value2.Y + 4.0f * value3.Y - value4.Y) * squared + (-value1.Y + 3.0f * value2.Y - 3.0f * value3.Y + value4.Y) * cubed);
result.Z = 0.5f * (2.0f * value2.Z + (-value1.Z + value3.Z) * amount + (2.0f * value1.Z - 5.0f * value2.Z + 4.0f * value3.Z - value4.Z) * squared + (-value1.Z + 3.0f * value2.Z - 3.0f * value3.Z + value4.Z) * cubed); result.Z = 0.5f * (2.0f * value2.Z + (-value1.Z + value3.Z) * amount + (2.0f * value1.Z - 5.0f * value2.Z + 4.0f * value3.Z - value4.Z) * squared + (-value1.Z + 3.0f * value2.Z - 3.0f * value3.Z + value4.Z) * cubed);
@@ -1045,22 +1009,18 @@ namespace FlaxEngine
//q3 = m3 - ((q1 ⋅ m3) / (q1 ⋅ q1)) * q1 - ((q2 ⋅ m3) / (q2 ⋅ q2)) * q2 //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 //q4 = m4 - ((q1 ⋅ m4) / (q1 ⋅ q1)) * q1 - ((q2 ⋅ m4) / (q2 ⋅ q2)) * q2 - ((q3 ⋅ m4) / (q3 ⋅ q3)) * q3
//q5 = ... //q5 = ...
if (source == null) if (source == null)
throw new ArgumentNullException(nameof(source)); throw new ArgumentNullException(nameof(source));
if (destination == null) if (destination == null)
throw new ArgumentNullException(nameof(destination)); throw new ArgumentNullException(nameof(destination));
if (destination.Length < source.Length) 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."); 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) for (var i = 0; i < source.Length; ++i)
{ {
Vector4 newvector = source[i]; Vector4 v = source[i];
for (var r = 0; r < i; ++r) for (var r = 0; r < i; ++r)
newvector -= Dot(destination[r], newvector) / Dot(destination[r], destination[r]) * destination[r]; v -= Dot(destination[r], v) / Dot(destination[r], destination[r]) * destination[r];
destination[i] = v;
destination[i] = newvector;
} }
} }
@@ -1094,23 +1054,19 @@ namespace FlaxEngine
//q3 = (m3 - (q1 ⋅ m3) * q1 - (q2 ⋅ m3) * q2) / |m3 - (q1 ⋅ m3) * q1 - (q2 ⋅ m3) * q2| //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| //q4 = (m4 - (q1 ⋅ m4) * q1 - (q2 ⋅ m4) * q2 - (q3 ⋅ m4) * q3) / |m4 - (q1 ⋅ m4) * q1 - (q2 ⋅ m4) * q2 - (q3 ⋅ m4) * q3|
//q5 = ... //q5 = ...
if (source == null) if (source == null)
throw new ArgumentNullException(nameof(source)); throw new ArgumentNullException(nameof(source));
if (destination == null) if (destination == null)
throw new ArgumentNullException(nameof(destination)); throw new ArgumentNullException(nameof(destination));
if (destination.Length < source.Length) 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."); 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) for (var i = 0; i < source.Length; ++i)
{ {
Vector4 newvector = source[i]; Vector4 v = source[i];
for (var r = 0; r < i; ++r) for (var r = 0; r < i; ++r)
newvector -= Dot(destination[r], newvector) * destination[r]; v -= Dot(destination[r], v) * destination[r];
v.Normalize();
newvector.Normalize(); destination[i] = v;
destination[i] = newvector;
} }
} }
@@ -1134,12 +1090,10 @@ namespace FlaxEngine
float yy = rotation.Y * y; float yy = rotation.Y * y;
float yz = rotation.Y * z; float yz = rotation.Y * z;
float zz = rotation.Z * z; float zz = rotation.Z * z;
result = new Vector4(vector.X * (1.0f - yy - zz) + vector.Y * (xy - wz) + vector.Z * (xz + wy),
result = new Vector4( vector.X * (xy + wz) + vector.Y * (1.0f - xx - zz) + vector.Z * (yz - wx),
vector.X * (1.0f - yy - zz) + vector.Y * (xy - wz) + vector.Z * (xz + wy), vector.X * (xz - wy) + vector.Y * (yz + wx) + vector.Z * (1.0f - xx - yy),
vector.X * (xy + wz) + vector.Y * (1.0f - xx - zz) + vector.Z * (yz - wx), vector.W);
vector.X * (xz - wy) + vector.Y * (yz + wx) + vector.Z * (1.0f - xx - yy),
vector.W);
} }
/// <summary> /// <summary>
@@ -1170,7 +1124,6 @@ namespace FlaxEngine
throw new ArgumentNullException(nameof(destination)); throw new ArgumentNullException(nameof(destination));
if (destination.Length < source.Length) 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."); throw new ArgumentOutOfRangeException(nameof(destination), "The destination array must be of same length or larger length than the source array.");
float x = rotation.X + rotation.X; float x = rotation.X + rotation.X;
float y = rotation.Y + rotation.Y; float y = rotation.Y + rotation.Y;
float z = rotation.Z + rotation.Z; float z = rotation.Z + rotation.Z;
@@ -1183,7 +1136,6 @@ namespace FlaxEngine
float yy = rotation.Y * y; float yy = rotation.Y * y;
float yz = rotation.Y * z; float yz = rotation.Y * z;
float zz = rotation.Z * z; float zz = rotation.Z * z;
float num1 = 1.0f - yy - zz; float num1 = 1.0f - yy - zz;
float num2 = xy - wz; float num2 = xy - wz;
float num3 = xz + wy; float num3 = xz + wy;
@@ -1193,13 +1145,11 @@ namespace FlaxEngine
float num7 = xz - wy; float num7 = xz - wy;
float num8 = yz + wx; float num8 = yz + wx;
float num9 = 1.0f - xx - yy; float num9 = 1.0f - xx - yy;
for (var i = 0; i < source.Length; ++i) for (var i = 0; i < source.Length; ++i)
destination[i] = new Vector4( destination[i] = new Vector4(source[i].X * num1 + source[i].Y * num2 + source[i].Z * num3,
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 * num4 + source[i].Y * num5 + source[i].Z * num6, source[i].X * num7 + source[i].Y * num8 + source[i].Z * num9,
source[i].X * num7 + source[i].Y * num8 + source[i].Z * num9, source[i].W);
source[i].W);
} }
/// <summary> /// <summary>
@@ -1210,11 +1160,10 @@ namespace FlaxEngine
/// <param name="result">When the method completes, contains the transformed <see cref="Vector4" />.</param> /// <param name="result">When the method completes, contains the transformed <see cref="Vector4" />.</param>
public static void Transform(ref Vector4 vector, ref Matrix transform, out Vector4 result) public static void Transform(ref Vector4 vector, ref Matrix transform, out Vector4 result)
{ {
result = new Vector4( result = new Vector4(vector.X * transform.M11 + vector.Y * transform.M21 + vector.Z * transform.M31 + vector.W * transform.M41,
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.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.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);
vector.X * transform.M14 + vector.Y * transform.M24 + vector.Z * transform.M34 + vector.W * transform.M44);
} }
/// <summary> /// <summary>
@@ -1245,7 +1194,6 @@ namespace FlaxEngine
throw new ArgumentNullException(nameof(destination)); throw new ArgumentNullException(nameof(destination));
if (destination.Length < source.Length) 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."); 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) for (var i = 0; i < source.Length; ++i)
Transform(ref source[i], ref transform, out destination[i]); Transform(ref source[i], ref transform, out destination[i]);
} }
@@ -1262,8 +1210,7 @@ namespace FlaxEngine
} }
/// <summary> /// <summary>
/// Multiplies a vector with another by performing component-wise multiplication equivalent to /// Multiplies a vector with another by performing component-wise multiplication equivalent to <see cref="Multiply(ref Vector4,ref Vector4,out Vector4)" />.
/// <see cref="Multiply(ref Vector4,ref Vector4,out Vector4)" />.
/// </summary> /// </summary>
/// <param name="left">The first vector to multiply.</param> /// <param name="left">The first vector to multiply.</param>
/// <param name="right">The second vector to multiply.</param> /// <param name="right">The second vector to multiply.</param>
@@ -1393,7 +1340,7 @@ namespace FlaxEngine
} }
/// <summary> /// <summary>
/// Perform a component-wise addition /// Performs a component-wise addition.
/// </summary> /// </summary>
/// <param name="value">The input vector.</param> /// <param name="value">The input vector.</param>
/// <param name="scalar">The scalar value to be added on elements</param> /// <param name="scalar">The scalar value to be added on elements</param>
@@ -1404,7 +1351,7 @@ namespace FlaxEngine
} }
/// <summary> /// <summary>
/// Perform a component-wise addition /// Performs a component-wise addition.
/// </summary> /// </summary>
/// <param name="value">The input vector.</param> /// <param name="value">The input vector.</param>
/// <param name="scalar">The scalar value to be added on elements</param> /// <param name="scalar">The scalar value to be added on elements</param>
@@ -1415,7 +1362,7 @@ namespace FlaxEngine
} }
/// <summary> /// <summary>
/// Perform a component-wise subtraction /// Performs a component-wise subtraction.
/// </summary> /// </summary>
/// <param name="value">The input vector.</param> /// <param name="value">The input vector.</param>
/// <param name="scalar">The scalar value to be subtracted from elements</param> /// <param name="scalar">The scalar value to be subtracted from elements</param>
@@ -1426,7 +1373,7 @@ namespace FlaxEngine
} }
/// <summary> /// <summary>
/// Perform a component-wise subtraction /// Performs a component-wise subtraction.
/// </summary> /// </summary>
/// <param name="value">The input vector.</param> /// <param name="value">The input vector.</param>
/// <param name="scalar">The scalar value to be subtracted from elements</param> /// <param name="scalar">The scalar value to be subtracted from elements</param>

View File

@@ -2,10 +2,9 @@
#pragma once #pragma once
#include "Engine/Platform/Platform.h" #include "Math.h"
#include "Engine/Core/Formatting.h" #include "Engine/Core/Formatting.h"
#include "Engine/Core/Templates.h" #include "Engine/Core/Templates.h"
#include "Math.h"
struct Double2; struct Double2;
struct Double3; struct Double3;
@@ -21,7 +20,7 @@ struct Int3;
struct Int4; struct Int4;
/// <summary> /// <summary>
/// Represents a four dimensional mathematical vector. /// Represents a four dimensional mathematical vector with 32-bit precision (per-component).
/// </summary> /// </summary>
API_STRUCT() struct FLAXENGINE_API Vector4 API_STRUCT() struct FLAXENGINE_API Vector4
{ {
@@ -167,7 +166,7 @@ public:
// Init // Init
// @param xyzw Double4 value // @param xyzw Double4 value
explicit Vector4(const Double4& xyzw); Vector4(const Double4& xyzw);
// Init // Init
// @param color Color value // @param color Color value