Files
core_math/tjp/core/math/Math.hpp
Timothy Prepscius 0807c0286a flatten 20260225
2026-02-25 12:36:47 -05:00

229 lines
3.7 KiB
C++
Executable File

//
// Math.hpp
// amoeba
//
// Created by Timothy Prepscius on 12/30/16.
// Copyright © 2016 Timothy Prepscius. All rights reserved.
//
#pragma once
#include "Vector3.hpp"
#include "Vector2.hpp"
#include <cstddef>
#include <array>
namespace tjp {
namespace core {
namespace math {
template<typename Real>
struct Ray3
{
Vector3<Real> origin, direction;
} ;
template<typename Real>
struct Line3
{
Vector3<Real> origin, direction;
} ;
template<typename Real>
class Plane3
{
public:
typedef Real value_type;
Vector3<Real> normal;
Real constant;
Plane3 () {}
Plane3 (const Vector3<Real> &normal_, Real constant_) :
normal(normal_),
constant(constant_)
{
}
Plane3 (const Vector3<Real> &o, const Vector3<Real> &d)
{
normal = d;
constant = d * o;
}
Plane3 (const Vector3<Real> &p0, const Vector3<Real> &p1, const Vector3<Real> &p2)
{
auto v1 = p2 - p0;
auto v2 = p1 - p0;
normal = (v1 ^ v2).normalize();
constant = normal * p0;
}
int whichSide(const Vector3<Real> &p) const;
Real distanceTo (const Vector3<Real> &p) const;
Vector3<Real> closestPoint(const Vector3<Real> &v) const;
};
template<typename Real>
class Frustum3
{
public:
typedef Real value_type;
Vector3<Real> origin, dVector, uVector, rVector;
Real dMin, dMax, uBound, rBound;
template<typename U>
Frustum3<U> asType() const
{
return Frustum3<U> {
origin.template asType<U>(),
dVector.template asType<U>(),
uVector.template asType<U>(),
rVector.template asType<U>(),
(U)dMin, (U)dMax, (U)uBound, (U)rBound
};
}
} ;
//template<typename Real>
//struct Box3
//{
// typedef Real value_type;
//
// Vector3<Real> center;
// std::array<Vector3<Real>, 3> axis;
// Vector3<Real> extent;
//} ;
template<typename Real>
struct Cone3
{
typedef Real value_type;
Ray3<Real> ray;
Real angle;
} ;
template<typename Real>
struct Segment2
{
typedef Real value_type;
Vector2<Real> begin, end;
} ;
template<typename Real>
struct Segment3
{
typedef Real value_type;
Vector3<Real> begin, end;
} ;
template<typename Real>
class Circle2
{
public:
typedef Real value_type;
Vector2<Real> center;
Real radius;
Circle2 () {}
Circle2 (const Vector2<Real> &center_, Real radius_) :
center(center_),
radius(radius_)
{
}
const static Circle2 Zero;
};
template<typename Real>
class Circle3
{
public:
typedef Real value_type;
Vector3<Real> center;
Vector3<Real> normal;
Real radius;
Circle3 () {}
Circle3 (const Vector3<Real> &center_, const Vector3<Real> &normal_, Real radius_) :
center(center_),
normal(normal_),
radius(radius_)
{
}
const static Circle3 Zero;
};
template<typename Real>
const Circle3<Real> Circle3<Real>::Zero = {
Vector3<Real>::Zero, Vector3<Real>::Zero, 0
};
template<typename Real>
class Triangle3
{
public:
Vector3<Real> v[3];
Vector3<Real> normal() const
{
auto s0 = v[0] - v[1];
auto s1 = v[0] - v[2];
return cross(s0, s1);
}
} ;
typedef double Real;
const Real Meter = 1.0;
const Real Centimeter = 0.01;
const Real Millimeter = 0.001;
typedef Ray3<double> Ray3d;
typedef Ray3<float> Ray3f;
typedef Ray3<Real> Ray3r;
typedef Segment2<double> Segment2d;
typedef Segment2<float> Segment2f;
typedef Segment2<Real> Segment2r;
typedef Circle2<double> Circle2d;
typedef Circle2<float> Circle2f;
typedef Circle2<Real> Circle2r;
typedef Line3<double> Line3d;
typedef Line3<float> Line3f;
typedef Line3<Real> Line3r;
typedef Plane3<float> Plane3f;
typedef Plane3<double> Plane3d;
typedef Plane3<Real> Plane3r;
typedef Cone3<float> Cone3f;
typedef Cone3<double> Cone3d;
typedef Cone3<Real> Cone3r;
typedef Frustum3<double> Frustum3d;
typedef Frustum3<float> Frustum3f;
typedef Frustum3<Real> Frustum3r;
} // namespace
} // namespace
} // namespace