Skip to content

Instantly share code, notes, and snippets.

@cdiggins
Created October 22, 2022 00:49
Show Gist options
  • Save cdiggins/8bc9628d2c9c93b9e4ca72407b767508 to your computer and use it in GitHub Desktop.
Save cdiggins/8bc9628d2c9c93b9e4ca72407b767508 to your computer and use it in GitHub Desktop.
Core Types for the Plato Geometry and Math Library
using System;
namespace Plato
{
public partial struct Float2 : IVector<float>
{
public float X { get; }
public float Y { get; }
}
public partial struct Float3 : IVector<float>
{
public float X { get; }
public float Y { get; }
public float Z { get; }
}
public partial struct Float4 : IVector<float>
{
public float X { get; }
public float Y { get; }
public float Z { get; }
public float W { get; }
}
public partial struct Double2 : IVector<double>
{
public double X { get; }
public double Y { get; }
}
public partial struct Double3 : IVector<double>
{
public double X { get; }
public double Y { get; }
public double Z { get; }
}
public partial struct Double4 : IVector<double>
{
public double X { get; }
public double Y { get; }
public double Z { get; }
public double W { get; }
}
public partial struct Quaternion : INumber
{
public double X { get; }
public double Y { get; }
public double Z { get; }
public double W { get; }
}
public partial struct Byte2 : IValue
{
public byte A { get; }
public byte B { get; }
}
public partial struct Byte3 : IValue
{
public byte A { get; }
public byte B { get; }
public byte C { get; }
}
public partial struct Byte4 : IValue
{
public byte A { get; }
public byte B { get; }
public byte C { get; }
public byte D { get; }
}
public partial struct Pose : INumber
{
public Double3 Position { get; }
public Quaternion Orientation { get; }
}
public partial struct Transform : INumber
{
public Double3 Translation { get; }
public Quaternion Rotation { get; }
public Double3 Scale { get; }
}
public partial struct BoundingBox2D : IInterval<Double2>
{
public Double2 Lower { get; }
public Double2 Upper { get; }
}
public partial struct BoundingBox3D : IInterval<Double3>
{
public Double3 Lower { get; }
public Double3 Upper { get; }
}
public partial struct Interval : IInterval<double>
{
public double Lower { get; }
public double Upper { get; }
}
public partial struct Complex : IVector<double>
{
public double Real { get; }
public double Imaginary { get; }
}
public partial struct Ray : IValue
{
public Double3 Direction { get; }
public Double3 Position { get; }
}
public partial struct Sphere : IValue
{
public Double3 Center { get; }
public float Radius { get; }
}
public partial struct Plane : IValue
{
public Double3 Normal { get; }
public float D { get; }
}
public partial struct Triangle : IVector<Double3>
{
public Double3 A { get; }
public Double3 B { get; }
public Double3 C { get; }
}
public partial struct Triangle2D : IVector<Double2>
{
public Double2 A { get; }
public Double2 B { get; }
public Double2 C { get; }
}
public partial struct Quad : IVector<Double3>
{
public Double3 A { get; }
public Double3 B { get; }
public Double3 C { get; }
public Double3 D { get; }
}
public partial struct Line : IVector<Double3>
{
public Double3 A { get; }
public Double3 B { get; }
}
public partial struct Line2D : IVector<Double2>
{
public Double2 A { get; }
public Double2 B { get; }
}
public partial struct Color : IValue
{
public double R { get; }
public double G { get; }
public double B { get; }
public double A { get; }
}
public partial struct ColorHSV : IValue
{
public double H { get; }
public double S { get; }
public double V { get; }
}
public partial struct ColorHSL : IValue
{
public double Hue { get; }
public double Saturation { get; }
public double Luminance { get; }
}
public partial struct ColorYCbCr : IValue
{
public double Y { get; }
public double Cb { get; }
public double Cr { get; }
}
public partial struct SphericalCoordinate : IValue
{
public double Radius { get; }
public Angle Azimuth { get; }
public Angle Inclination { get; }
}
public partial struct PolarCoordinate : IValue
{
public double Radius { get; }
public Angle Azimuth { get; }
}
public partial struct LogPolarCoordinate : IValue
{
public double Rho { get; }
public Angle Azimuth { get; }
}
public partial struct HorizontalCoordinate : IValue
{
public double Radius { get; }
public Angle Azimuth { get; }
public double Height { get; }
}
public partial struct GeoCoordinate : IValue
{
public double Latitude { get; }
public double Longitude { get; }
public double Altitude { get; }
}
public partial struct AxisAngle : IValue
{
public Double3 Axis { get; }
public Angle Angle { get; }
}
public partial struct EulerAngles : IValue
{
public Angle Yaw { get; }
public Angle Pitch { get; }
public Angle Roll { get; }
}
public partial struct Circle : IValue
{
public Double2 Position { get; }
public double Radius { get; }
}
public partial struct Size : IValue
{
public double Width { get; }
public double Height { get; }
}
public partial struct Rectangle : IValue
{
public Double2 TopLeft { get; }
public Size Size { get; }
}
public partial struct Percent : INumber
{
public double Value { get; }
public static Percent FromProportion(double value) => value * 100.0;
public Proportion ToProportion() => Value / 100.0;
public static implicit operator double(Percent amount) => amount.Value;
public static implicit operator Percent(double value) => new Percent(value);
}
public partial struct Proportion : INumber
{
public double Value { get; }
public static Proportion FromPercent(double percent) => percent / 100.0;
public Percent ToPercent() => 100.0 * Value;
public static implicit operator double(Proportion amount) => amount.Value;
public static implicit operator Proportion(double value) => new Proportion(value);
}
public partial struct Amount : IMeasure
{
public double Mole { get; }
public static implicit operator double(Amount amount) => amount.Value;
public static implicit operator Amount(double value) => new Amount(value);
}
public partial struct Fraction : INumber
{
public double Numerator { get; }
public double Denominator { get; }
}
public partial struct Ratio : INumber
{
public long A { get; }
public long B { get; }
}
public partial struct Angle : IMeasure
{
public double Radians { get; }
public const double RadiansPerRevolution = Math.PI * 2;
public const double DegreesPerRevolution = 360;
public const string InternalUnit = nameof(Radians);
public static Angle FromRadians(double radians) => new Angle(radians);
public static Angle FromRevolutions(double revolutions) => revolutions * RadiansPerRevolution;
public static Angle FromDegrees(double degrees) => FromRevolutions(DegreesPerRevolution / degrees);
public double ToDegrees() => DegreesPerRevolution * ToRevolutions();
public double ToRevolutions() => Radians / RadiansPerRevolution;
public double ToRadians() => Radians;
public static implicit operator double(Angle angle) => angle.Radians;
public static implicit operator Angle(double radians) => new Angle(radians);
}
public partial struct Distance : INumber
{
public const double FeetPerMeter = 3.280839895;
public const double FeetPerMile = 5280;
public const double MetersPerLightyear = 9.46073047258e+15;
public const double MetersPerAU = 149597870691;
public double Meters { get; }
public const string InternalUnit = nameof(Meters);
public static Distance FromMeters(double value) => value;
public static Distance FromKilometer(double value) => value * 1000;
public static Distance FromCentimeters(double value) => value / 100;
public static Distance FromMillimeters(double value) => value / 100;
public static Distance FromMicrons(double value) => value / 1000 / 1000;
public static Distance FromNanometers(double value) => value / 1000 / 1000 / 1000;
public static Distance FromInches(double value) => FromFeet(value / 12);
public static Distance FromFeet(double value) => value / FeetPerMeter;
public static Distance FromYards(double value) => FromFeet(value * 3);
public static Distance FromMiles(double value) => FromFeet(value * FeetPerMile);
public static Distance FromLightyears(double value) => value * MetersPerLightyear;
public static Distance FromAU(double value) => value * MetersPerAU;
public double ToMeters() => Meters;
public double ToKilometers() => Meters / 1000;
public double ToCentimeters() => Meters * 100;
public double ToMillimeters() => Meters * 1000;
public double ToMicrons() => ToMillimeters() * 1000;
public double ToNanometers() => ToMicrons() * 1000;
public double ToInches() => ToFeet() * 12;
public double ToFeet() => Meters * FeetPerMeter;
public double ToYards() => ToFeet() / 3;
public double ToMiles() => Meters * FeetPerMeter / FeetPerMile;
public double ToLightuears() => Meters / MetersPerLightyear;
public double ToAU() => Meters / MetersPerAU;
public static implicit operator double(Distance distance) => distance.Meters;
public static implicit operator Distance(double meters) => new Distance(meters);
}
public partial struct Mass : INumber
{
public double Kilograms { get; }
public const double DaltonPerKilogram = 1.66053e-27;
public const double PoundPerKilogram = 0.45359237;
public const double PoundPerTon = 2000;
public const double KilogramPerSolarMass = 1.9889200011446E+30;
public const string InternalUnit = nameof(Kilograms);
public static Mass FromMilligrams(double value) => value / 1000 / 1000;
public static Mass FromGrams(double value) => value / 1000;
public static Mass FromKilograms(double value) => value;
public static Mass FromDalton(double value) => value * DaltonPerKilogram;
public static Mass FromTonne(double value) => value * 1000;
public static Mass FromPound(double value) => value * PoundPerKilogram;
public static Mass FromTon(double value) => FromPound(value * PoundPerTon);
public static Mass FromSolarMass(double value) => value * KilogramPerSolarMass;
public double ToMilligrams() => ToGrams() * 1000;
public double ToGrams() => Kilograms * 1000;
public double ToKilograms() => Kilograms;
public double ToDalton() => Kilograms / DaltonPerKilogram;
public double ToTonne() => Kilograms / 1000;
public double ToPound() => Kilograms / PoundPerKilogram;
public double ToTon() => ToPound() / PoundPerTon;
public double ToSolarMass() => Kilograms / KilogramPerSolarMass;
public static implicit operator double(Mass mass) => mass.Kilograms;
public static implicit operator Mass(double kilograms) => new Mass(kilograms);
}
public partial struct Temperature : INumber
{
public double Celsius { get; }
public Temperature(double celsius) => Celsius = celsius;
public static Temperature FromCelsius(double value) => value;
public static Temperature FromKelvin(double value) => value - 273.15;
public static Temperature FromFaranheit(double value) => (value - 32.0) * 5.0 / 9.0;
public double ToCelsius() => Celsius;
public double ToKelvin() => Celsius + 273.15;
public double ToFaranheit() => (Celsius * 9.0 / 5.0) + 32.0;
public static implicit operator double(Temperature temperature) => temperature.Celsius;
public static implicit operator Temperature(double celsius) => new Temperature(celsius);
}
public partial struct Duration : INumber
{
public double Seconds { get; }
public static Duration FromNanoseconds(double value) => value / 1000 / 1000 / 10000;
public static Duration FromMicroseconds(double value) => value / 1000 / 1000;
public static Duration FromMilliseconds(double value) => value / 1000;
public static Duration FromSeconds(double value) => value;
public static Duration FromMinutes(double value) => value * 60;
public static Duration FromHours(double value) => value * 60 * 60;
public static Duration FromDays(double value) => value * 60 * 60 * 24;
public static Duration FromWeeks(double value) => value * 60 * 60 * 24 * 7;
public double ToNanosecond() => Seconds * 1000 * 1000 * 1000;
public double ToMicroeconds() => Seconds * 1000 * 1000;
public double ToMilliseconds() => Seconds * 1000;
public double ToSeconds() => Seconds;
public double ToMinutes() => Seconds / 60;
public double ToHours() => Seconds / (60 * 60);
public double ToDays() => Seconds / (60 * 60 * 24);
public double ToWeeks() => Seconds / (60 * 60 * 24 * 7);
public static implicit operator double(Duration duration) => duration.Seconds;
public static implicit operator Duration(double value) => new Duration(value);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment