Refactor Vector types to support 64-bit precision via define switch

This commit is contained in:
Wojtek Figat
2022-05-25 20:04:33 +02:00
parent 1303740611
commit f82e370392
55 changed files with 2264 additions and 5482 deletions

View File

@@ -1,117 +1,265 @@
// Copyright (c) 2012-2022 Wojciech Figat. All rights reserved.
#include "Double2.h"
#include "Double3.h"
#include "Double4.h"
#include "Vector2.h"
#include "Vector3.h"
#include "Vector4.h"
#include "Color.h"
#include "Int2.h"
#include "Int3.h"
#include "Int4.h"
#include "../Types/String.h"
static_assert(sizeof(Vector2) == 8, "Invalid Vector2 type size.");
// Float
const Vector2 Vector2::Zero(0);
const Vector2 Vector2::One(1);
const Vector2 Vector2::UnitX(1, 0);
const Vector2 Vector2::UnitY(0, 1);
const Vector2 Vector2::Minimum(MIN_float);
const Vector2 Vector2::Maximum(MAX_float);
static_assert(sizeof(Float2) == 8, "Invalid Float2 type size.");
Vector2::Vector2(const Int2& xy)
: X(static_cast<float>(xy.X))
, Y(static_cast<float>(xy.Y))
template<>
const Float2 Float2::Zero(0.0f);
template<>
const Float2 Float2::One(1.0f);
template<>
const Float2 Float2::UnitX(1.0f, 0.0f);
template<>
const Float2 Float2::UnitY(0.0f, 1.0f);
template<>
const Float2 Float2::Minimum(MIN_float);
template<>
const Float2 Float2::Maximum(MAX_float);
template<>
Float2::Vector2Base(const Int3& xy)
: X((float)xy.X)
, Y((float)xy.Y)
{
}
Vector2::Vector2(const Int3& xyz)
: X(static_cast<float>(xyz.X))
, Y(static_cast<float>(xyz.Y))
template<>
Float2::Vector2Base(const Int4& xy)
: X((float)xy.X)
, Y((float)xy.Y)
{
}
Vector2::Vector2(const Int4& xyzw)
: X(static_cast<float>(xyzw.X))
, Y(static_cast<float>(xyzw.Y))
template<>
Float2::Vector2Base(const Float3& xy)
: X(xy.X)
, Y(xy.Y)
{
}
Vector2::Vector2(const Vector3& xyz)
: X(xyz.X)
, Y(xyz.Y)
template<>
Float2::Vector2Base(const Float4& xy)
: X(xy.X)
, Y(xy.Y)
{
}
Vector2::Vector2(const Vector4& xyzw)
: X(xyzw.X)
, Y(xyzw.Y)
template<>
Float2::Vector2Base(const Double3& xy)
: X((float)xy.X)
, Y((float)xy.Y)
{
}
Vector2::Vector2(const Double2& xy)
: X(static_cast<float>(xy.X))
, Y(static_cast<float>(xy.Y))
template<>
Float2::Vector2Base(const Double4& xy)
: X((float)xy.X)
, Y((float)xy.Y)
{
}
Vector2::Vector2(const Double3& xyz)
: X(static_cast<float>(xyz.X))
, Y(static_cast<float>(xyz.Y))
{
}
Vector2::Vector2(const Double4& xyzw)
: X(static_cast<float>(xyzw.X))
, Y(static_cast<float>(xyzw.Y))
{
}
Vector2::Vector2(const Color& color)
template<>
Float2::Vector2Base(const Color& color)
: X(color.R)
, Y(color.G)
{
}
String Vector2::ToString() const
template<>
String Float2::ToString() const
{
return String::Format(TEXT("{}"), *this);
}
Vector2 Vector2::Normalize(const Vector2& v)
{
Vector2 result = v;
const float length = v.Length();
if (!Math::IsZero(length))
{
const float inv = 1.0f / length;
result.X *= inv;
result.Y *= inv;
}
return result;
}
Int2 Vector2::CeilToInt(const Vector2& v)
{
return Int2(Math::CeilToInt(v.X), Math::CeilToInt(v.Y));
}
Int2 Vector2::FloorToInt(const Vector2& v)
{
return Int2(Math::FloorToInt(v.X), Math::FloorToInt(v.Y));
}
float Vector2::TriangleArea(const Vector2& v0, const Vector2& v1, const Vector2& v2)
template<>
float Float2::TriangleArea(const Float2& v0, const Float2& v1, const Float2& v2)
{
return Math::Abs((v0.X * (v1.Y - v2.Y) + v1.X * (v2.Y - v0.Y) + v2.X * (v0.Y - v1.Y)) / 2);
}
float Vector2::Angle(const Vector2& from, const Vector2& to)
template<>
float Float2::Angle(const Float2& from, const Float2& to)
{
const float dot = Math::Clamp(Dot(Normalize(from), Normalize(to)), -1.0f, 1.0f);
if (Math::Abs(dot) > (1.0f - ZeroTolerance))
if (Math::Abs(dot) > 1.0f - ZeroTolerance)
return dot > 0.0f ? 0.0f : PI;
return Math::Acos(dot);
}
// Double
static_assert(sizeof(Double2) == 16, "Invalid Double2 type size.");
template<>
const Double2 Double2::Zero(0.0);
template<>
const Double2 Double2::One(1.0);
template<>
const Double2 Double2::UnitX(1.0, 0.0);
template<>
const Double2 Double2::UnitY(0.0, 1.0);
template<>
const Double2 Double2::Minimum(MIN_double);
template<>
const Double2 Double2::Maximum(MAX_double);
template<>
Double2::Vector2Base(const Int3& xy)
: X((double)xy.X)
, Y((double)xy.Y)
{
}
template<>
Double2::Vector2Base(const Int4& xy)
: X((double)xy.X)
, Y((double)xy.Y)
{
}
template<>
Double2::Vector2Base(const Float3& xy)
: X((double)xy.X)
, Y((double)xy.Y)
{
}
template<>
Double2::Vector2Base(const Float4& xy)
: X((int32)xy.X)
, Y((double)xy.Y)
{
}
template<>
Double2::Vector2Base(const Double3& xy)
: X(xy.X)
, Y(xy.Y)
{
}
template<>
Double2::Vector2Base(const Double4& xy)
: X(xy.X)
, Y(xy.Y)
{
}
template<>
Double2::Vector2Base(const Color& color)
: X((double)color.R)
, Y((double)color.G)
{
}
template<>
String Double2::ToString() const
{
return String::Format(TEXT("{}"), *this);
}
template<>
double Double2::TriangleArea(const Double2& v0, const Double2& v1, const Double2& v2)
{
return Math::Abs((v0.X * (v1.Y - v2.Y) + v1.X * (v2.Y - v0.Y) + v2.X * (v0.Y - v1.Y)) / 2);
}
template<>
double Double2::Angle(const Double2& from, const Double2& to)
{
const double dot = Math::Clamp(Dot(Normalize(from), Normalize(to)), -1.0, 1.0);
if (Math::Abs(dot) > 1.0 - ZeroTolerance)
return dot > 0.0 ? 0.0 : PI;
return Math::Acos(dot);
}
// Int
static_assert(sizeof(Int2) == 8, "Invalid Int2 type size.");
template<>
const Int2 Int2::Zero(0);
template<>
const Int2 Int2::One(1);
template<>
const Int2 Int2::UnitX(1, 0);
template<>
const Int2 Int2::UnitY(0, 1);
template<>
const Int2 Int2::Minimum(MIN_int32);
template<>
const Int2 Int2::Maximum(MAX_int32);
template<>
Int2::Vector2Base(const Int3& xy)
: X(xy.X)
, Y(xy.Y)
{
}
template<>
Int2::Vector2Base(const Int4& xy)
: X(xy.X)
, Y(xy.Y)
{
}
template<>
Int2::Vector2Base(const Float3& xy)
: X((int32)xy.X)
, Y((int32)xy.Y)
{
}
template<>
Int2::Vector2Base(const Float4& xy)
: X((int32)xy.X)
, Y((int32)xy.Y)
{
}
template<>
Int2::Vector2Base(const Double3& xy)
: X((int32)xy.X)
, Y((int32)xy.Y)
{
}
template<>
Int2::Vector2Base(const Double4& xy)
: X((int32)xy.X)
, Y((int32)xy.Y)
{
}
template<>
Int2::Vector2Base(const Color& color)
: X((int32)color.R)
, Y((int32)color.G)
{
}
template<>
String Int2::ToString() const
{
return String::Format(TEXT("{}"), *this);
}
template<>
int32 Int2::TriangleArea(const Int2& v0, const Int2& v1, const Int2& v2)
{
return Math::Abs((v0.X * (v1.Y - v2.Y) + v1.X * (v2.Y - v0.Y) + v2.X * (v0.Y - v1.Y)) / 2);
}
template<>
int32 Int2::Angle(const Int2& from, const Int2& to)
{
return 0;
}