Cleanups BoundingBoxAxisAligned and fixes coding styles a bit.

Reviewed By: wraitii
Differential Revision: https://code.wildfiregames.com/D1951
This was SVN commit r22372.
This commit is contained in:
Vladislav Belov 2019-06-12 20:23:14 +00:00
parent 3e53d05298
commit d1fb66d2a7
8 changed files with 121 additions and 98 deletions

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2012 Wildfire Games.
/* Copyright (C) 2019 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -23,16 +23,15 @@
#include "BoundingBoxAligned.h"
#include "lib/ogl.h"
#include <float.h>
#include "graphics/Frustum.h"
#include "graphics/ShaderProgram.h"
#include "lib/ogl.h"
#include "maths/BoundingBoxOriented.h"
#include "maths/Brush.h"
#include "maths/Matrix3D.h"
#include <limits>
const CBoundingBoxAligned CBoundingBoxAligned::EMPTY = CBoundingBoxAligned(); // initializes to an empty bound
///////////////////////////////////////////////////////////////////////////////
@ -40,81 +39,96 @@ const CBoundingBoxAligned CBoundingBoxAligned::EMPTY = CBoundingBoxAligned(); //
// if ray hits (and store entry and exit times), or false
// otherwise
// note: incoming ray direction must be normalised
bool CBoundingBoxAligned::RayIntersect(const CVector3D& origin,const CVector3D& dir,
float& tmin,float& tmax) const
bool CBoundingBoxAligned::RayIntersect(
const CVector3D& origin, const CVector3D& dir, float& tmin, float& tmax) const
{
float t1,t2;
float tnear,tfar;
float t1, t2;
float tnear, tfar;
if (dir[0]==0) {
if (origin[0]<m_Data[0][0] || origin[0]>m_Data[1][0])
if (dir[0] == 0)
{
if (origin[0] < m_Data[0][0] || origin[0] > m_Data[1][0])
return false;
else {
tnear=(float) -FLT_MAX;
tfar=(float) FLT_MAX;
else
{
tnear = -std::numeric_limits<float>::max();
tfar = std::numeric_limits<float>::max();
}
} else {
t1=(m_Data[0][0]-origin[0])/dir[0];
t2=(m_Data[1][0]-origin[0])/dir[0];
}
else
{
t1 = (m_Data[0][0] - origin[0]) / dir[0];
t2 = (m_Data[1][0] - origin[0]) / dir[0];
if (dir[0]<0) {
if (dir[0] < 0)
{
tnear = t2;
tfar = t1;
} else {
}
else
{
tnear = t1;
tfar = t2;
}
if (tfar<0)
if (tfar < 0)
return false;
}
if (dir[1]==0 && (origin[1]<m_Data[0][1] || origin[1]>m_Data[1][1]))
if (dir[1] == 0 && (origin[1] < m_Data[0][1] || origin[1] > m_Data[1][1]))
return false;
else {
t1=(m_Data[0][1]-origin[1])/dir[1];
t2=(m_Data[1][1]-origin[1])/dir[1];
else
{
t1 = (m_Data[0][1] - origin[1]) / dir[1];
t2 = (m_Data[1][1] - origin[1]) / dir[1];
if (dir[1]<0) {
if (t2>tnear)
if (dir[1] < 0)
{
if (t2 > tnear)
tnear = t2;
if (t1<tfar)
if (t1 < tfar)
tfar = t1;
} else {
if (t1>tnear)
}
else
{
if (t1 > tnear)
tnear = t1;
if (t2<tfar)
if (t2 < tfar)
tfar = t2;
}
if (tnear>tfar || tfar<0)
if (tnear > tfar || tfar < 0)
return false;
}
if (dir[2]==0 && (origin[2]<m_Data[0][2] || origin[2]>m_Data[1][2]))
if (dir[2] == 0 && (origin[2] < m_Data[0][2] || origin[2] > m_Data[1][2]))
return false;
else {
t1=(m_Data[0][2]-origin[2])/dir[2];
t2=(m_Data[1][2]-origin[2])/dir[2];
else
{
t1 = (m_Data[0][2] - origin[2]) / dir[2];
t2 = (m_Data[1][2] - origin[2]) / dir[2];
if (dir[2]<0) {
if (t2>tnear)
if (dir[2] < 0)
{
if (t2 > tnear)
tnear = t2;
if (t1<tfar)
if (t1 < tfar)
tfar = t1;
} else {
if (t1>tnear)
}
else
{
if (t1 > tnear)
tnear = t1;
if (t2<tfar)
if (t2 < tfar)
tfar = t2;
}
if (tnear>tfar || tfar<0)
return false;
if (tnear > tfar || tfar < 0)
return false;
}
tmin=tnear;
tmax=tfar;
tmin = tnear;
tmax = tfar;
return true;
}
@ -123,16 +137,15 @@ bool CBoundingBoxAligned::RayIntersect(const CVector3D& origin,const CVector3D&
// SetEmpty: initialise this bound as empty
void CBoundingBoxAligned::SetEmpty()
{
m_Data[0]=CVector3D( FLT_MAX, FLT_MAX, FLT_MAX);
m_Data[1]=CVector3D(-FLT_MAX,-FLT_MAX,-FLT_MAX);
m_Data[0] = CVector3D::Max();
m_Data[1] = CVector3D::Min();
}
///////////////////////////////////////////////////////////////////////////////
// IsEmpty: tests whether this bound is empty
bool CBoundingBoxAligned::IsEmpty() const
{
return (m_Data[0].X == FLT_MAX && m_Data[0].Y == FLT_MAX && m_Data[0].Z == FLT_MAX
&& m_Data[1].X == -FLT_MAX && m_Data[1].Y == -FLT_MAX && m_Data[1].Z == -FLT_MAX);
return m_Data[0] == CVector3D::Max() && m_Data[1] == CVector3D::Min();
}
///////////////////////////////////////////////////////////////////////////////
@ -141,25 +154,25 @@ bool CBoundingBoxAligned::IsEmpty() const
// (can't remember which one it was, though)
void CBoundingBoxAligned::Transform(const CMatrix3D& m, CBoundingBoxAligned& result) const
{
ENSURE(this!=&result);
ENSURE(this != &result);
for (int i=0;i<3;++i) {
for (int i = 0; i < 3; ++i)
{
// handle translation
result[0][i]=result[1][i]=m(i,3);
result[0][i] = result[1][i] = m(i, 3);
// Now find the extreme points by considering the product of the
// min and max with each component of matrix
for(int j=0;j<3;j++) {
float a=m(i,j)*m_Data[0][j];
float b=m(i,j)*m_Data[1][j];
for (int j = 0; j < 3; ++j)
{
float a = m(i, j) * m_Data[0][j];
float b = m(i, j) * m_Data[1][j];
if (a<b) {
result[0][i]+=a;
result[1][i]+=b;
} else {
result[0][i]+=b;
result[1][i]+=a;
}
if (a >= b)
std::swap(a, b);
result[0][i] += a;
result[1][i] += b;
}
}
}

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2011 Wildfire Games.
/* Copyright (C) 2019 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -22,22 +22,22 @@
#ifndef INCLUDED_BOUND
#define INCLUDED_BOUND
// necessary includes
#include "Vector3D.h"
#include "maths/Vector3D.h"
#include "graphics/ShaderProgramPtr.h"
class CFrustum;
class CMatrix3D;
class CBoundingBoxOriented;
///////////////////////////////////////////////////////////////////////////////
// basic axis aligned bounding box (AABB) class
// Basic axis aligned bounding box (AABB) class
class CBoundingBoxAligned
{
public:
static const CBoundingBoxAligned EMPTY;
CBoundingBoxAligned() { SetEmpty(); }
CBoundingBoxAligned(const CVector3D& min, const CVector3D& max) {
CBoundingBoxAligned(const CVector3D& min, const CVector3D& max)
{
m_Data[0] = min;
m_Data[1] = max;
}
@ -114,10 +114,10 @@ public:
return (std::max(v.X, 0.0f) * std::max(v.Y, 0.0f) * std::max(v.Z, 0.0f));
}
// return the centre of this bounding box
void GetCentre(CVector3D& centre) const
// return the center of this bounding box
void GetCenter(CVector3D& center) const
{
centre = (m_Data[0] + m_Data[1]) * 0.5f;
center = (m_Data[0] + m_Data[1]) * 0.5f;
}
/**
@ -160,12 +160,6 @@ public:
private:
// Holds the minimal and maximal coordinate points in m_Data[0] and m_Data[1], respectively.
CVector3D m_Data[2];
public:
static const CBoundingBoxAligned EMPTY;
};
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#endif
#endif // INCLUDED_BOUND

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2011 Wildfire Games.
/* Copyright (C) 2019 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -32,7 +32,7 @@ CBoundingBoxOriented::CBoundingBoxOriented(const CBoundingBoxAligned& bound)
}
else
{
bound.GetCentre(m_Center);
bound.GetCenter(m_Center);
// the axes of an AABB are the world-space axes
m_Basis[0].X = 1.f; m_Basis[0].Y = 0.f; m_Basis[0].Z = 0.f;

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2010 Wildfire Games.
/* Copyright (C) 2019 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -24,16 +24,31 @@
#include "Vector3D.h"
#include <math.h>
#include <float.h>
#include "MathUtil.h"
#include "FixedVector3D.h"
#include <cmath>
#include <limits>
CVector3D::CVector3D(const CFixedVector3D& v) :
X(v.X.ToFloat()), Y(v.Y.ToFloat()), Z(v.Z.ToFloat())
{
}
// static
CVector3D CVector3D::Max()
{
const float max_float = std::numeric_limits<float>::max();
return CVector3D(max_float, max_float, max_float);
}
// static
CVector3D CVector3D::Min()
{
const float max_float = std::numeric_limits<float>::max();
return CVector3D(-max_float, -max_float, -max_float);
}
int CVector3D::operator ! () const
{
if (X != 0.0f ||
@ -76,8 +91,5 @@ CVector3D CVector3D::Normalized () const
float MaxComponent(const CVector3D& v)
{
float max = -FLT_MAX;
for(int i = 0; i < 3; i++)
max = std::max(max, v[i]);
return max;
return std::max({v.X, v.Y, v.Z});
}

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2010 Wildfire Games.
/* Copyright (C) 2019 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -31,6 +31,10 @@ class CVector3D
float X, Y, Z;
public:
// Returns maximum/minimum possible position stored in the CVector3D.
static CVector3D Max();
static CVector3D Min();
CVector3D() : X(0.0f), Y(0.0f), Z(0.0f) {}
CVector3D(float x, float y, float z) : X(x), Y(y), Z(z) {}
CVector3D(const CFixedVector3D& v);

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2012 Wildfire Games.
/* Copyright (C) 2019 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -64,9 +64,9 @@ public:
CVector3D v (1, 2, 3);
bound += v;
CVector3D centre;
bound.GetCentre(centre);
TS_ASSERT_EQUALS(centre, v);
CVector3D center;
bound.GetCenter(center);
TS_ASSERT_EQUALS(center, v);
}
void test_extend_bound()
@ -76,9 +76,9 @@ public:
CBoundingBoxAligned b (v, v);
bound += b;
CVector3D centre;
bound.GetCentre(centre);
TS_ASSERT_EQUALS(centre, v);
CVector3D center;
bound.GetCenter(center);
TS_ASSERT_EQUALS(center, v);
}
void test_aabb_to_obb_translation()

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2018 Wildfire Games.
/* Copyright (C) 2019 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -311,7 +311,7 @@ public:
if (!aABBox.RayIntersect(origin, dir, tmin, tmax))
continue;
aABBox.GetCentre(center);
aABBox.GetCenter(center);
}
else
{

View File

@ -511,7 +511,7 @@ void ActorViewer::Render()
CVector3D centre;
CmpPtr<ICmpVisual> cmpVisual(m.Simulation2, m.Entity);
if (cmpVisual)
cmpVisual->GetBounds().GetCentre(centre);
cmpVisual->GetBounds().GetCenter(centre);
else
centre.Y = 0.f;
centre.X = centre.Z = TERRAIN_TILE_SIZE * m.Terrain.GetPatchesPerSide()*PATCH_SIZE/2;