300 lines
9.5 KiB
C#
300 lines
9.5 KiB
C#
/*
|
|
* Copyright 2019-2025 Andrey Pokidov <andrey.pokidov@gmail.com>
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
using System;
|
|
|
|
/*
|
|
* Author: Andrey Pokidov
|
|
* Date: 1 Feb 2019
|
|
*/
|
|
|
|
namespace BasicGeometry
|
|
{
|
|
public struct Vector3FP64
|
|
{
|
|
public static readonly Vector3FP64 ZERO = new Vector3FP64(0.0, 0.0, 0.0);
|
|
|
|
public double x1 = 0.0;
|
|
public double x2 = 0.0;
|
|
public double x3 = 0.0;
|
|
|
|
public Vector3FP64(double x1, double x2, double x3)
|
|
{
|
|
this.x1 = x1;
|
|
this.x2 = x2;
|
|
this.x3 = x3;
|
|
}
|
|
|
|
public Vector3FP64(in Vector3FP64 vector)
|
|
{
|
|
this.x1 = vector.x1;
|
|
this.x2 = vector.x2;
|
|
this.x3 = vector.x3;
|
|
}
|
|
|
|
public Vector3FP64(in Vector3FP32 vector)
|
|
{
|
|
this.x1 = vector.x1;
|
|
this.x2 = vector.x2;
|
|
this.x3 = vector.x3;
|
|
}
|
|
|
|
public readonly double GetSquareModule()
|
|
{
|
|
return this.x1 * this.x1 + this.x2 * this.x2 + this.x3 * this.x3;
|
|
}
|
|
|
|
public readonly double GetModule()
|
|
{
|
|
return Math.Sqrt(this.GetSquareModule());
|
|
}
|
|
|
|
public int Normalize()
|
|
{
|
|
double squareModule = this.GetSquareModule();
|
|
|
|
|
|
if (UtilityFP64.IsSqareValueUnit(squareModule))
|
|
{
|
|
return 1;
|
|
}
|
|
|
|
if (squareModule <= UtilityFP64.SQUARE_EPSYLON)
|
|
{
|
|
this.Reset();
|
|
return 0;
|
|
}
|
|
|
|
double multiplier = Math.Sqrt(1.0 / squareModule);
|
|
|
|
this.x1 *= multiplier;
|
|
this.x2 *= multiplier;
|
|
this.x3 *= multiplier;
|
|
|
|
return 1;
|
|
}
|
|
|
|
public void Reverse()
|
|
{
|
|
this.x1 = -this.x1;
|
|
this.x2 = -this.x2;
|
|
this.x3 = -this.x3;
|
|
}
|
|
|
|
public readonly bool IsZero()
|
|
{
|
|
return this.GetSquareModule() <= UtilityFP64.SQUARE_EPSYLON;
|
|
}
|
|
|
|
public readonly bool IsUnit()
|
|
{
|
|
return UtilityFP64.IsSqareValueUnit(this.GetSquareModule());
|
|
}
|
|
|
|
public void Reset()
|
|
{
|
|
this.x1 = 0.0;
|
|
this.x2 = 0.0;
|
|
this.x3 = 0.0;
|
|
}
|
|
|
|
public void SetValues(double x1, double x2, double x3)
|
|
{
|
|
this.x1 = x1;
|
|
this.x2 = x2;
|
|
this.x3 = x3;
|
|
}
|
|
|
|
public void SetValues(in Vector3FP64 vector)
|
|
{
|
|
this.x1 = vector.x1;
|
|
this.x2 = vector.x2;
|
|
this.x3 = vector.x3;
|
|
}
|
|
|
|
public void SetValues(in Vector3FP32 vector)
|
|
{
|
|
this.x1 = vector.x1;
|
|
this.x2 = vector.x2;
|
|
this.x3 = vector.x3;
|
|
}
|
|
|
|
public void SetReverseOf(in Vector3FP64 vector)
|
|
{
|
|
this.x1 = -vector.x1;
|
|
this.x2 = -vector.x2;
|
|
this.x3 = -vector.x3;
|
|
}
|
|
|
|
public void SetReverseOf(in Vector3FP32 vector)
|
|
{
|
|
this.x1 = -vector.x1;
|
|
this.x2 = -vector.x2;
|
|
this.x3 = -vector.x3;
|
|
}
|
|
|
|
public void AppendScaled(Vector3FP64 summand, double scale)
|
|
{
|
|
this.x1 += summand.x1 * scale;
|
|
this.x2 += summand.x2 * scale;
|
|
this.x3 += summand.x3 * scale;
|
|
}
|
|
|
|
public readonly override string ToString()
|
|
{
|
|
return String.Format("DPVector3({0}, {1}, {2})", this.x1, this.x2, this.x3);
|
|
}
|
|
|
|
public static void Add(in Vector3FP64 vector1, in Vector3FP64 vector2, out Vector3FP64 sum)
|
|
{
|
|
sum.x1 = vector1.x1 + vector2.x1;
|
|
sum.x2 = vector1.x2 + vector2.x2;
|
|
sum.x3 = vector1.x3 + vector2.x3;
|
|
}
|
|
|
|
public static void Subtract(in Vector3FP64 minuend, in Vector3FP64 subtrahend, out Vector3FP64 difference)
|
|
{
|
|
difference.x1 = minuend.x1 - subtrahend.x1;
|
|
difference.x2 = minuend.x2 - subtrahend.x2;
|
|
difference.x3 = minuend.x3 - subtrahend.x3;
|
|
}
|
|
|
|
public static void Multiply(in Vector3FP64 multiplicand, double multiplier, out Vector3FP64 product)
|
|
{
|
|
product.x1 = multiplicand.x1 * multiplier;
|
|
product.x2 = multiplicand.x2 * multiplier;
|
|
product.x3 = multiplicand.x3 * multiplier;
|
|
}
|
|
|
|
public static void Divide(in Vector3FP64 dividend, double divisor, out Vector3FP64 quotient)
|
|
{
|
|
Multiply(dividend, 1.0 / divisor, out quotient);
|
|
}
|
|
|
|
public static void GetMean2(in Vector3FP64 vector1, in Vector3FP64 vector2, out Vector3FP64 result)
|
|
{
|
|
result.x1 = (vector1.x1 + vector2.x1) * 0.5;
|
|
result.x2 = (vector1.x2 + vector2.x2) * 0.5;
|
|
result.x3 = (vector1.x3 + vector2.x3) * 0.5;
|
|
}
|
|
|
|
public static void GetMean3(in Vector3FP64 vector1, in Vector3FP64 vector2, in Vector3FP64 vector3, out Vector3FP64 result)
|
|
{
|
|
result.x1 = (vector1.x1 + vector2.x1 + vector3.x1) * UtilityFP64.ONE_THIRD;
|
|
result.x2 = (vector1.x2 + vector2.x2 + vector3.x2) * UtilityFP64.ONE_THIRD;
|
|
result.x3 = (vector1.x3 + vector2.x3 + vector3.x3) * UtilityFP64.ONE_THIRD;
|
|
}
|
|
|
|
public static double GetScalarProduct(in Vector3FP64 vector1, in Vector3FP64 vector2)
|
|
{
|
|
return vector1.x1 * vector2.x1 + vector1.x2 * vector2.x2 + vector1.x3 * vector2.x3;
|
|
}
|
|
|
|
public static void GetCrossProduct(in Vector3FP64 vector1, in Vector3FP64 vector2, out Vector3FP64 result)
|
|
{
|
|
double x1 = vector1.x2 * vector2.x3 - vector1.x3 * vector2.x2;
|
|
double x2 = vector1.x3 * vector2.x1 - vector1.x1 * vector2.x3;
|
|
double x3 = vector1.x1 * vector2.x2 - vector1.x2 * vector2.x1;
|
|
|
|
result.x1 = x1;
|
|
result.x2 = x2;
|
|
result.x3 = x3;
|
|
}
|
|
|
|
public static double GetTripleProduct(in Vector3FP64 vector1, in Vector3FP64 vector2, in Vector3FP64 vector3)
|
|
{
|
|
return vector1.x1 * (vector2.x2 * vector3.x3 - vector2.x3 * vector3.x2)
|
|
+ vector1.x2 * (vector2.x3 * vector3.x1 - vector2.x1 * vector3.x3)
|
|
+ vector1.x3 * (vector2.x1 * vector3.x2 - vector2.x2 * vector3.x1);
|
|
}
|
|
|
|
public static void GetDoubleCrossProduct(in Vector3FP64 vector1, in Vector3FP64 vector2, in Vector3FP64 vector3, out Vector3FP64 result)
|
|
{
|
|
// [a x [b x c]] = b * (a, c) - c * (a, b)
|
|
double ac = GetScalarProduct(vector1, vector3);
|
|
double ab = GetScalarProduct(vector1, vector2);
|
|
|
|
result.x1 = ac * vector2.x1 - ab * vector3.x1;
|
|
result.x2 = ac * vector2.x2 - ab * vector3.x2;
|
|
result.x3 = ac * vector2.x3 - ab * vector3.x3;
|
|
}
|
|
|
|
public static double GetAngle(in Vector3FP64 vector1, in Vector3FP64 vector2, AngleUnit unit)
|
|
{
|
|
double squareModule1 = vector1.GetSquareModule();
|
|
|
|
if (squareModule1 <= UtilityFP64.SQUARE_EPSYLON)
|
|
{
|
|
return 0.0;
|
|
}
|
|
|
|
double squareModule2 = vector2.GetSquareModule();
|
|
|
|
if (squareModule2 <= UtilityFP64.SQUARE_EPSYLON)
|
|
{
|
|
return 0.0;
|
|
}
|
|
|
|
double cosine = Vector3FP64.GetScalarProduct(vector1, vector2) / Math.Sqrt(squareModule1 * squareModule2);
|
|
|
|
if (1.0 - UtilityFP64.EPSYLON <= cosine)
|
|
{
|
|
return 0.0;
|
|
}
|
|
|
|
if (cosine <= -(1.0 - UtilityFP64.EPSYLON))
|
|
{
|
|
return AngleFP64.GetHalfCircle(unit);
|
|
}
|
|
|
|
return RadianFP64.ToUnits(Math.Acos(cosine), unit);
|
|
}
|
|
|
|
public static double GetSquareDistance(in Vector3FP64 vector1, in Vector3FP64 vector2)
|
|
{
|
|
double dx1 = vector1.x1 - vector2.x1;
|
|
double dx2 = vector1.x2 - vector2.x2;
|
|
double dx3 = vector1.x3 - vector2.x3;
|
|
|
|
return dx1 * dx1 + dx2 * dx2 + dx3 * dx3;
|
|
}
|
|
|
|
public static double GetDistance(in Vector3FP64 vector1, in Vector3FP64 vector2)
|
|
{
|
|
return Math.Sqrt(GetSquareDistance(vector1, vector2));
|
|
}
|
|
|
|
public static bool AreEqual(in Vector3FP64 vector1, in Vector3FP64 vector2)
|
|
{
|
|
double squareModule1 = vector1.GetSquareModule();
|
|
double squareModule2 = vector2.GetSquareModule();
|
|
double squareModule3 = GetSquareDistance(vector1, vector2);
|
|
|
|
// 3.0 means dimension amount
|
|
if (squareModule1 < UtilityFP64.EPSYLON_EFFECTIVENESS_LIMIT || squareModule2 < UtilityFP64.EPSYLON_EFFECTIVENESS_LIMIT)
|
|
{
|
|
return squareModule3 < (3.0 * UtilityFP64.SQUARE_EPSYLON);
|
|
}
|
|
|
|
if (squareModule1 <= squareModule2)
|
|
{
|
|
return squareModule3 <= (3.0 * UtilityFP64.SQUARE_EPSYLON) * squareModule2;
|
|
}
|
|
|
|
return squareModule3 <= (3.0 * UtilityFP64.SQUARE_EPSYLON) * squareModule1;
|
|
}
|
|
}
|
|
}
|