rewrite q3map2 math in c++

This commit is contained in:
Garux
2021-02-25 19:41:43 +03:00
parent 820dd5e21f
commit 4ce944444d
55 changed files with 3630 additions and 4643 deletions

View File

@@ -103,6 +103,11 @@ Element* data(){
const Element* data() const {
return m_elements;
}
BasicVector3& set( const Element value ){
x() = y() = z() = value;
return *this;
}
};
/// \brief A 4-element vector.
@@ -171,8 +176,25 @@ Element* data(){
const Element* data() const {
return m_elements;
}
BasicVector3<Element>& vec3(){
return reinterpret_cast<BasicVector3<Element>&>( x() );
}
const BasicVector3<Element>& vec3() const {
return reinterpret_cast<const BasicVector3<Element>&>( x() );
}
BasicVector4& set( const Element value ){
x() = y() = z() = w() = value;
return *this;
}
};
template<typename Element>
inline BasicVector2<Element> vector2_from_array( const Element* array ){
return BasicVector2<Element>( array[0], array[1] );
}
template<typename Element>
inline BasicVector3<Element> vector3_from_array( const Element* array ){
return BasicVector3<Element>( array[0], array[1], array[2] );

View File

@@ -27,36 +27,41 @@
#include "math/matrix.h"
/// \brief A plane equation stored in double-precision floating-point.
class Plane3
template<typename T>
class Plane3___
{
public:
double a, b, c, d;
T a, b, c, d;
Plane3(){
Plane3___(){
}
Plane3( double _a, double _b, double _c, double _d )
Plane3___( double _a, double _b, double _c, double _d )
: a( _a ), b( _b ), c( _c ), d( _d ){
}
template<typename Element>
Plane3( const BasicVector3<Element>& normal, double dist )
Plane3___( const BasicVector3<Element>& normal, double dist )
: a( normal.x() ), b( normal.y() ), c( normal.z() ), d( dist ){
}
BasicVector3<double>& normal(){
return reinterpret_cast<BasicVector3<double>&>( *this );
BasicVector3<T>& normal(){
return reinterpret_cast<BasicVector3<T>&>( *this );
}
const BasicVector3<double>& normal() const {
return reinterpret_cast<const BasicVector3<double>&>( *this );
const BasicVector3<T>& normal() const {
return reinterpret_cast<const BasicVector3<T>&>( *this );
}
double& dist(){
T& dist(){
return d;
}
const double& dist() const {
const T& dist() const {
return d;
}
};
/// \brief A plane equation stored in double-precision floating-point.
using Plane3 = Plane3___<double>;
/// \brief A plane equation stored in single-precision floating-point.
using Plane3f = Plane3___<float>;
inline Plane3 plane3_normalised( const Plane3& plane ){
double rmagnitude = 1.0 / sqrt( plane.a * plane.a + plane.b * plane.b + plane.c * plane.c );
return Plane3(
@@ -105,8 +110,9 @@ inline Plane3 plane3_transformed_affine_full( const Plane3& plane, const Matrix4
return Plane3( normal, vector3_dot( normal, anchor ) );
}
inline Plane3 plane3_flipped( const Plane3& plane ){
return Plane3( vector3_negated( plane.normal() ), -plane.dist() );
template<typename T>
inline Plane3___<T> plane3_flipped( const Plane3___<T>& plane ){
return Plane3___<T>( vector3_negated( plane.normal() ), -plane.dist() );
}
const double c_PLANE_NORMAL_EPSILON = 0.0001f;
@@ -148,8 +154,8 @@ inline Plane3 plane3_for_points( const BasicVector3<Element> planepts[3] ){
return plane3_for_points( planepts[0], planepts[1], planepts[2] );
}
template<typename T>
inline double plane3_distance_to_point( const Plane3& plane, const BasicVector3<T>& point ){
template<typename P, typename V>
inline double plane3_distance_to_point( const Plane3___<P>& plane, const BasicVector3<V>& point ){
return vector3_dot( point, plane.normal() ) - plane.dist();
}
@@ -160,9 +166,9 @@ inline BasicVector3<T> plane3_project_point( const Plane3& plane, const BasicVec
return point + direction * d;
}
template<typename T>
inline BasicVector3<T> plane3_project_point( const Plane3& plane, const BasicVector3<T>& point ){
return ( point - plane.normal() * vector3_dot( point, plane.normal() ) + plane.normal() * plane.dist() );
template<typename P, typename V>
inline BasicVector3<V> plane3_project_point( const Plane3___<P>& plane, const BasicVector3<V>& point ){
return point - plane.normal() * plane3_distance_to_point( plane, point );
}
#endif

View File

@@ -289,19 +289,18 @@ inline void vector2_normalise( BasicVector2<Element>& self ){
self = vector2_normalised( self );
}
template<typename Element>
inline BasicVector2<Element> vector2_mid( const BasicVector2<Element>& begin, const BasicVector2<Element>& end ){
return vector2_scaled( vector2_added( begin, end ), 0.5 );
}
const Vector3 g_vector3_identity( 0, 0, 0 );
const Vector3 g_vector3_max = Vector3( FLT_MAX, FLT_MAX, FLT_MAX );
const Vector3 g_vector3_axis_x( 1, 0, 0 );
const Vector3 g_vector3_axis_y( 0, 1, 0 );
const Vector3 g_vector3_axis_z( 0, 0, 1 );
#ifdef __GNUC__
#define VARIABLE_IS_NOT_USED __attribute__ ((unused))
#else
#define VARIABLE_IS_NOT_USED
#endif
const Vector3 VARIABLE_IS_NOT_USED g_vector3_axes[3] = { g_vector3_axis_x, g_vector3_axis_y, g_vector3_axis_z };
const Vector3 g_vector3_axes[3] = { g_vector3_axis_x, g_vector3_axis_y, g_vector3_axis_z };
template<typename Element, typename OtherElement>
inline void vector3_swap( BasicVector3<Element>& self, BasicVector3<OtherElement>& other ){
@@ -560,20 +559,14 @@ inline Vector3 vector3_for_spherical( double theta, double phi ){
template<typename Element>
inline std::size_t vector3_max_abs_component_index( const BasicVector3<Element>& self ){
std::size_t maxi = 0;
for( std::size_t i = 1; i < 3; ++i )
if( fabs( self[i] ) > fabs( self[maxi] ) )
maxi = i;
return maxi;
const std::size_t maxi = ( fabs( self[1] ) > fabs( self[0] ) )? 1 : 0;
return ( fabs( self[2] ) > fabs( self[maxi] ) )? 2 : maxi;;
}
template<typename Element>
inline std::size_t vector3_min_abs_component_index( const BasicVector3<Element>& self ){
std::size_t mini = 0;
for( std::size_t i = 1; i < 3; ++i )
if( fabs( self[i] ) < fabs( self[mini] ) )
mini = i;
return mini;
const std::size_t mini = ( fabs( self[1] ) < fabs( self[0] ) )? 1 : 0;
return ( fabs( self[2] ) < fabs( self[mini] ) )? 2 : mini;
}