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

101 lines
1.9 KiB
C++
Executable File

//
// Math.inl
// amoeba
//
// Created by Timothy Prepscius on 12/30/16.
// Copyright © 2016 Timothy Prepscius. All rights reserved.
//
#pragma once
#include "Math.hpp"
#include "Zero.h"
namespace tjp {
namespace core {
namespace math {
// inline
template<typename Real>
Real Plane3<Real>::distanceTo (const Vector3<Real>& p) const
{
return (normal * p) - constant;
}
// inline
template<typename Real>
int Plane3<Real>::whichSide (const Vector3<Real> & p) const
{
Real distance = distanceTo(p);
if (distance < 0.0f)
{
return -1;
}
else if (distance > 0.0f)
{
return +1;
}
else
{
return 0;
}
}
// inline
template<typename Real>
Real angleBetween (const Vector3<Real> &l, const Vector3<Real> &r)
{
auto v1 = l.normalize();
auto v2 = r.normalize();
// | unit(V1) * unit(V2) | = cos A
double cosA = v1 * v2;
// if the shadow is completely in the same direction, then the
// difference in angle is zero
const double episolon = 0.00001;
const auto oneMepsilon = 1 - episolon;
const auto negOneMepsilon = -1 + episolon;
if (cosA >= oneMepsilon)
return 0;
// if the shadow is completely in the opposite direction, then the
// difference in angle is PI
if (cosA <= negOneMepsilon)
return constants<Real>::PI;
double Acos = acos(cosA);
return (Real) Acos;
}
// inline
template<typename Real>
Vector3<Real> axisBetween (const Vector3<Real> &l, const Vector3<Real> &r)
{
return (l ^ r).normalize();
}
// inline
template<typename Real>
Vector3<Real> Plane3<Real>::closestPoint (const Vector3<Real> &p) const
{
// http://stackoverflow.com/questions/9605556/how-to-project-a-3d-point-to-a-3d-plane
auto r = p - (normal * p + constant) * normal;
return r;
}
// --------
template<typename Real>
Real area(const math::Triangle3<Real> &t)
{
auto e0 = t.v[1] - t.v[0];
auto e1 = t.v[2] - t.v[0];
return length(cross(e0, e1)) / 2;
}
} // namespace
} // namespace
} // namespace