293 lines
7.8 KiB
C#
293 lines
7.8 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: 10 Feb 2019
|
|
*/
|
|
|
|
namespace BasicGeometry
|
|
{
|
|
public struct Matrix2x2FP64
|
|
{
|
|
public double r1c1 = 0.0, r1c2 = 0.0;
|
|
public double r2c1 = 0.0, r2c2 = 0.0;
|
|
|
|
public Matrix2x2FP64(double d1, double d2)
|
|
{
|
|
this.r1c1 = d1;
|
|
this.r2c2 = d2;
|
|
}
|
|
|
|
public Matrix2x2FP64(in Matrix2x2FP64 matrix)
|
|
{
|
|
this.r1c1 = matrix.r1c1;
|
|
this.r1c2 = matrix.r1c2;
|
|
|
|
this.r2c1 = matrix.r2c1;
|
|
this.r2c2 = matrix.r2c2;
|
|
}
|
|
|
|
public Matrix2x2FP64(in Matrix2x2FP32 matrix)
|
|
{
|
|
this.r1c1 = matrix.r1c1;
|
|
this.r1c2 = matrix.r1c2;
|
|
|
|
this.r2c1 = matrix.r2c1;
|
|
this.r2c2 = matrix.r2c2;
|
|
}
|
|
|
|
public readonly double GetDeterminant()
|
|
{
|
|
return this.r1c1 * this.r2c2 - this.r1c2 * this.r2c1;
|
|
}
|
|
|
|
public readonly bool IsSingular()
|
|
{
|
|
return UtilityFP64.IsZero(this.GetDeterminant());
|
|
}
|
|
|
|
public void Transpose()
|
|
{
|
|
(this.r1c2, this.r2c1) = (this.r2c1, this.r1c2);
|
|
}
|
|
|
|
public bool Invert()
|
|
{
|
|
double determinant = this.GetDeterminant();
|
|
|
|
if (UtilityFP64.IsZero(determinant))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
double r1c1 = this.r2c2;
|
|
double r1c2 = -this.r1c2;
|
|
|
|
double r2c1 = -this.r2c1;
|
|
double r2c2 = this.r1c1;
|
|
|
|
double multiplier = 1.0 / determinant;
|
|
|
|
this.r1c1 = r1c1 * multiplier;
|
|
this.r1c2 = r1c2 * multiplier;
|
|
|
|
this.r2c1 = r2c1 * multiplier;
|
|
this.r2c2 = r2c2 * multiplier;
|
|
|
|
return true;
|
|
}
|
|
|
|
public void Reset()
|
|
{
|
|
this.r1c1 = 0.0;
|
|
this.r1c2 = 0.0;
|
|
|
|
this.r2c1 = 0.0;
|
|
this.r2c2 = 0.0;
|
|
}
|
|
|
|
public void SetToIdentity()
|
|
{
|
|
this.r1c1 = 1.0;
|
|
this.r1c2 = 0.0;
|
|
|
|
this.r2c1 = 0.0;
|
|
this.r2c2 = 1.0;
|
|
}
|
|
|
|
public void SetToDiagonal(double d1, double d2)
|
|
{
|
|
this.r1c1 = d1;
|
|
this.r1c2 = 0.0;
|
|
|
|
this.r2c1 = 0.0;
|
|
this.r2c2 = d2;
|
|
}
|
|
|
|
public void Set(in Matrix2x2FP64 matrix)
|
|
{
|
|
this.r1c1 = matrix.r1c1;
|
|
this.r1c2 = matrix.r1c2;
|
|
|
|
this.r2c1 = matrix.r2c1;
|
|
this.r2c2 = matrix.r2c2;
|
|
}
|
|
|
|
public void Set(in Matrix2x2FP32 matrix)
|
|
{
|
|
this.r1c1 = matrix.r1c1;
|
|
this.r1c2 = matrix.r1c2;
|
|
|
|
this.r2c1 = matrix.r2c1;
|
|
this.r2c2 = matrix.r2c2;
|
|
}
|
|
|
|
public void SetTransposedOf(in Matrix2x2FP64 matrix)
|
|
{
|
|
this.r1c1 = matrix.r1c1;
|
|
this.r2c2 = matrix.r2c2;
|
|
(this.r1c2, this.r2c1) = (matrix.r2c1, matrix.r1c2);
|
|
}
|
|
|
|
public void SetTransposedOf(in Matrix2x2FP32 matrix)
|
|
{
|
|
this.r1c1 = matrix.r1c1;
|
|
this.r1c2 = matrix.r2c1;
|
|
|
|
this.r2c1 = matrix.r1c2;
|
|
this.r2c2 = matrix.r2c2;
|
|
}
|
|
|
|
public bool SetInvertedOf(in Matrix2x2FP64 matrix)
|
|
{
|
|
double determinant = matrix.GetDeterminant();
|
|
|
|
if (UtilityFP64.IsZero(determinant))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
double r1c1 = matrix.r2c2;
|
|
double r1c2 = -matrix.r1c2;
|
|
|
|
double r2c1 = -matrix.r2c1;
|
|
double r2c2 = matrix.r1c1;
|
|
|
|
double multiplier = 1.0 / determinant;
|
|
|
|
this.r1c1 = r1c1 * multiplier;
|
|
this.r1c2 = r1c2 * multiplier;
|
|
|
|
this.r2c1 = r2c1 * multiplier;
|
|
this.r2c2 = r2c2 * multiplier;
|
|
|
|
return true;
|
|
}
|
|
|
|
public void SetRow1(double c1, double c2)
|
|
{
|
|
this.r1c1 = c1;
|
|
this.r1c2 = c2;
|
|
}
|
|
|
|
public void SetRow2(double c1, double c2)
|
|
{
|
|
this.r2c1 = c1;
|
|
this.r2c2 = c2;
|
|
}
|
|
|
|
public void SetColumn1(double r1, double r2)
|
|
{
|
|
this.r1c1 = r1;
|
|
this.r2c1 = r2;
|
|
}
|
|
|
|
public void SetColumn2(double r1, double r2)
|
|
{
|
|
this.r1c2 = r1;
|
|
this.r2c2 = r2;
|
|
}
|
|
|
|
public void AppendScaled(in Matrix2x2FP64 matrix, double scale)
|
|
{
|
|
this.r1c1 += matrix.r1c1 * scale;
|
|
this.r1c2 += matrix.r1c2 * scale;
|
|
|
|
this.r2c1 += matrix.r2c1 * scale;
|
|
this.r2c2 += matrix.r2c2 * scale;
|
|
}
|
|
|
|
public static void Add(in Matrix2x2FP64 matrix1, in Matrix2x2FP64 matrix2, out Matrix2x2FP64 sum)
|
|
{
|
|
sum.r1c1 = matrix1.r1c1 + matrix2.r1c1;
|
|
sum.r1c2 = matrix1.r1c2 + matrix2.r1c2;
|
|
|
|
sum.r2c1 = matrix1.r2c1 + matrix2.r2c1;
|
|
sum.r2c2 = matrix1.r2c2 + matrix2.r2c2;
|
|
}
|
|
|
|
public static void Subtract(in Matrix2x2FP64 minuend, in Matrix2x2FP64 subtrahend, out Matrix2x2FP64 difference)
|
|
{
|
|
difference.r1c1 = minuend.r1c1 - subtrahend.r1c1;
|
|
difference.r1c2 = minuend.r1c2 - subtrahend.r1c2;
|
|
|
|
difference.r2c1 = minuend.r2c1 - subtrahend.r2c1;
|
|
difference.r2c2 = minuend.r2c2 - subtrahend.r2c2;
|
|
}
|
|
|
|
public static void Multiply(in Matrix2x2FP64 multiplicand, double multiplier, out Matrix2x2FP64 product)
|
|
{
|
|
product.r1c1 = multiplicand.r1c1 * multiplier;
|
|
product.r1c2 = multiplicand.r1c2 * multiplier;
|
|
|
|
product.r2c1 = multiplicand.r2c1 * multiplier;
|
|
product.r2c2 = multiplicand.r2c2 * multiplier;
|
|
}
|
|
|
|
public static void Divide(in Matrix2x2FP64 dividend, double divisor, out Matrix2x2FP64 quotient)
|
|
{
|
|
Multiply(dividend, 1.0 / divisor, out quotient);
|
|
}
|
|
|
|
public static void GetRightProduct(in Matrix2x2FP64 matrix, in Vector2FP64 vector, out Vector2FP64 result)
|
|
{
|
|
double x1 = matrix.r1c1 * vector.x1 + matrix.r1c2 * vector.x2;
|
|
double x2 = matrix.r2c1 * vector.x1 + matrix.r2c2 * vector.x2;
|
|
|
|
result.x1 = x1;
|
|
result.x2 = x2;
|
|
}
|
|
|
|
public static void GetLeftProduct(in Vector2FP64 vector, in Matrix2x2FP64 matrix, out Vector2FP64 result)
|
|
{
|
|
double x1 = vector.x1 * matrix.r1c1 + vector.x2 * matrix.r2c1;
|
|
double x2 = vector.x1 * matrix.r1c2 + vector.x2 * matrix.r2c2;
|
|
|
|
result.x1 = x1;
|
|
result.x2 = x2;
|
|
}
|
|
|
|
public static void LoadZero(out Matrix2x2FP64 matrix)
|
|
{
|
|
matrix.r1c1 = 0.0;
|
|
matrix.r1c2 = 0.0;
|
|
|
|
matrix.r2c1 = 0.0;
|
|
matrix.r2c2 = 0.0;
|
|
}
|
|
|
|
public static void LoadIdentity(out Matrix2x2FP64 matrix)
|
|
{
|
|
matrix.r1c1 = 1.0;
|
|
matrix.r1c2 = 0.0;
|
|
|
|
matrix.r2c1 = 0.0;
|
|
matrix.r2c2 = 1.0;
|
|
}
|
|
|
|
public static void LoadDiagonal(double d1, double d2, out Matrix2x2FP64 matrix)
|
|
{
|
|
matrix.r1c1 = d1;
|
|
matrix.r1c2 = 0.0;
|
|
|
|
matrix.r2c1 = 0.0;
|
|
matrix.r2c2 = d2;
|
|
}
|
|
}
|
|
}
|