GeometriCS/structs/Plane.cs
Zdeněk Borovec ad0599f481 Decided on architecture
A sizeable commit, I know, but I wanted to try and write a few aspects before deciding to settle on a system. This one seems to be fine so far. But I would still not rely on it not changing.
2024-04-18 01:29:47 +02:00

153 lines
5 KiB
C#

namespace GeometriCS
{
/// <summary>
/// Euclidean plane with double precision.
/// </summary>
/// <remarks>
/// Plane follows the equation Ax + By + Cz - D = 0
/// </remarks>
public class Planed
{
/// <summary>
/// A coefficient of the plane.
/// </summary>
/// <remarks>
/// Plane follows the equation Ax + By + Cz - D = 0
/// </remarks>
public double Coeff_A { get; set; }
/// <summary>
/// B coefficient of the plane.
/// </summary>
/// <remarks>
/// Plane follows the equation Ax + By + Cz - D = 0
/// </remarks>
public double Coeff_B { get; set; }
/// <summary>
/// C coefficient of the plane.
/// </summary>
/// <remarks>
/// Plane follows the equation Ax + By + Cz - D = 0
/// </remarks>
public double Coeff_C { get; set; }
/// <summary>
/// D coefficient of the plane.
/// </summary>
/// <remarks>
/// Plane follows the equation Ax + By + Cz - D = 0
/// </remarks>
public double Coeff_D { get; set; }
/// <summary>
/// Normal vector to the plane.
/// </summary>
public Vector3d Normal => new Vector3d(Coeff_A, Coeff_B, Coeff_C);
/// <summary>
/// Distance of the plane from origin along the normal vector. (signed!)
/// </summary>
public double Distance => -Coeff_D;
/// <summary>
/// Construct the plane from its normal and distance from origin.
/// </summary>
/// <param name="normal">Normal vector of the plane.</param>
/// <param name="distance">Distance from origin along normal.</param>
public Planed(Vector3d normal, double distance)
{
Coeff_A = normal.X;
Coeff_B = normal.Y;
Coeff_C = normal.Z;
Coeff_D = -distance;
}
/// <summary>
/// Construct the plane from its normal and distance from origin.
/// </summary>
/// <param name="coeff_a">A coefficient of the plane.</param>
/// <param name="coeff_b">B coefficient of the plane.</param>
/// <param name="coeff_c">C coefficient of the plane.</param>
/// <param name="coeff_d">D coefficient of the plane.</param>
/// <remarks>
/// Plane follows the equation Ax + By + Cz - D = 0
/// </remarks>
public Planed(double coeff_a, double coeff_b, double coeff_c, double coeff_d)
{
Coeff_A = coeff_a;
Coeff_B = coeff_b;
Coeff_C = coeff_c;
Coeff_D = coeff_d;
}
/// <summary>
/// String representation of the plane.
/// </summary>
/// <returns>(normal vector, distance from origin)</returns>
public override string ToString()
{
return $"({Normal}, {Distance})";
}
/// <summary>
/// Do the two objects represent the same plane?
/// </summary>
/// <param name="obj">Object to compare.</param>
/// <remarks>
/// If the planes have inverse normals and also inverse distances, they are considered not equal despite looking the same.
/// </remarks>
/// <returns><c>true</c> If the planes have the same coefficients. <c>false</c></returns>
public override bool Equals(object? obj)
{
if (obj is Planed asPlane)
{
return this == asPlane;
}
else
{
return false;
}
}
public override int GetHashCode()
{
return HashCode.Combine(Coeff_A, Coeff_B, Coeff_C, Coeff_D);
}
/// <summary>
/// Deep copy the plane.
/// </summary>
/// <returns>Deep copy of the plane.</returns>
public object Clone()
{
return new Planed(Coeff_A, Coeff_B, Coeff_C, Coeff_D);
}
/// <summary>
/// Are the planes equal?
/// </summary>
/// <param name="a">First plane.</param>
/// <param name="b">Second plane.</param>
/// <remarks>
/// If the planes have inverse normals and also inverse distances, they are considered not equal despite looking the same.
/// </remarks>
/// <returns><c>true</c> If the planes have the same coefficients. <c>false</c></returns>
public static bool operator ==(Planed a, Planed b)
{
return a.Coeff_A == b.Coeff_A && a.Coeff_B == b.Coeff_B &&
a.Coeff_C == b.Coeff_C && a.Coeff_D == b.Coeff_D;
}
/// <summary>
/// Are the planes different?
/// </summary>
/// <param name="a">First plane.</param>
/// <param name="b">Second plane.</param>
/// <returns><c>true</c> If either the normals or the distance differs, otherwise <c>false</c>.</returns>
public static bool operator !=(Planed a, Planed b)
{
return !(a == b);
}
}
}