Skip to content

Commit

Permalink
UPBGE: Use only min and max operation for computing AABB.
Browse files Browse the repository at this point in the history
Previously the code parts computing AABB were using a condition
to detect the first point computed used to initialize the AABB
dimensions. But this method requiered a test at each points and
also to be able to detect the first point.
To avoid these restrictions the AABB dimension is initialized to
float min for max and float max for min. This make sure that any
point will affect the AABB the first time.
  • Loading branch information
panzergame committed Oct 16, 2017
1 parent 9a77819 commit b339165
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 51 deletions.
24 changes: 8 additions & 16 deletions source/gameengine/Converter/BL_SkinDeformer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -276,10 +276,9 @@ void BL_SkinDeformer::UpdateTransverts()
return;
}

bool first = true;
// AABB Box : min/max.
MT_Vector3 aabbMin;
MT_Vector3 aabbMax;
MT_Vector3 aabbMin(FLT_MAX, FLT_MAX, FLT_MAX);
MT_Vector3 aabbMax(FLT_MIN, FLT_MIN, FLT_MIN);

// the vertex cache is unique to this deformer, no need to update it
// if it wasn't updated! We must update all the materials at once
Expand All @@ -300,19 +299,12 @@ void BL_SkinDeformer::UpdateTransverts()
continue;
}

// For the first vertex of the mesh, only initialize AABB.
if (first) {
aabbMin = aabbMax = vertpos;
first = false;
}
else {
aabbMin.x() = std::min(aabbMin.x(), vertpos.x());
aabbMin.y() = std::min(aabbMin.y(), vertpos.y());
aabbMin.z() = std::min(aabbMin.z(), vertpos.z());
aabbMax.x() = std::max(aabbMax.x(), vertpos.x());
aabbMax.y() = std::max(aabbMax.y(), vertpos.y());
aabbMax.z() = std::max(aabbMax.z(), vertpos.z());
}
aabbMin.x() = std::min(aabbMin.x(), vertpos.x());
aabbMin.y() = std::min(aabbMin.y(), vertpos.y());
aabbMin.z() = std::min(aabbMin.z(), vertpos.z());
aabbMax.x() = std::max(aabbMax.x(), vertpos.x());
aabbMax.y() = std::max(aabbMax.y(), vertpos.y());
aabbMax.z() = std::max(aabbMax.z(), vertpos.z());
}

array->SetModifiedFlag(RAS_IDisplayArray::POSITION_MODIFIED | RAS_IDisplayArray::NORMAL_MODIFIED);
Expand Down
18 changes: 6 additions & 12 deletions source/gameengine/Ketsji/KX_FontObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,22 +224,16 @@ void KX_FontObject::GetTextAabb(MT_Vector2& min, MT_Vector2& max)

BLF_size(m_fontid, size, m_dpi);

min = MT_Vector2(FLT_MAX, FLT_MAX);
max = MT_Vector2(FLT_MIN, FLT_MIN);
for (unsigned short i = 0, size = m_texts.size(); i < size; ++i) {
rctf box;
const std::string& text = m_texts[i];
BLF_boundbox(m_fontid, text.c_str(), text.size(), &box);
if (i == 0) {
min.x() = box.xmin;
min.y() = box.ymin;
max.x() = box.xmax;
max.y() = box.ymax;
}
else {
min.x() = std::min(min.x(), box.xmin);
min.y() = std::min(min.y(), box.ymin - lineSpacing * i);
max.x() = std::max(max.x(), box.xmax);
max.y() = std::max(max.y(), box.ymax - lineSpacing * i);
}
min.x() = std::min(min.x(), box.xmin);
min.y() = std::min(min.y(), box.ymin - lineSpacing * i);
max.x() = std::max(max.x(), box.xmax);
max.y() = std::max(max.y(), box.ymax - lineSpacing * i);
}

min *= aspect;
Expand Down
21 changes: 8 additions & 13 deletions source/gameengine/Ketsji/KX_SoftBodyDeformer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ void KX_SoftBodyDeformer::Apply(RAS_MeshMaterial *meshmat, RAS_IDisplayArray *ar
}

// AABB Box : min/max.
MT_Vector3 aabbMin;
MT_Vector3 aabbMax;
MT_Vector3 aabbMin(FLT_MAX, FLT_MAX, FLT_MAX);
MT_Vector3 aabbMax(FLT_MIN, FLT_MIN, FLT_MIN);

for (unsigned int i = 0, size = array->GetVertexCount(); i < size; ++i) {
RAS_IVertex *v = array->GetVertex(i);
Expand All @@ -129,17 +129,12 @@ void KX_SoftBodyDeformer::Apply(RAS_MeshMaterial *meshmat, RAS_IDisplayArray *ar
// Extract object transform from the vertex position.
const MT_Vector3 ptWorld = (pt - pos) * rot * invertscale;
// if the AABB need an update.
if (i == 0) {
aabbMin = aabbMax = ptWorld;
}
else {
aabbMin.x() = std::min(aabbMin.x(), ptWorld.x());
aabbMin.y() = std::min(aabbMin.y(), ptWorld.y());
aabbMin.z() = std::min(aabbMin.z(), ptWorld.z());
aabbMax.x() = std::max(aabbMax.x(), ptWorld.x());
aabbMax.y() = std::max(aabbMax.y(), ptWorld.y());
aabbMax.z() = std::max(aabbMax.z(), ptWorld.z());
}
aabbMin.x() = std::min(aabbMin.x(), ptWorld.x());
aabbMin.y() = std::min(aabbMin.y(), ptWorld.y());
aabbMin.z() = std::min(aabbMin.z(), ptWorld.z());
aabbMax.x() = std::max(aabbMax.x(), ptWorld.x());
aabbMax.y() = std::max(aabbMax.y(), ptWorld.y());
aabbMax.z() = std::max(aabbMax.z(), ptWorld.z());
}

array->UpdateFrom(origarray, origarray->GetModifiedFlag() &
Expand Down
16 changes: 6 additions & 10 deletions source/gameengine/Rasterizer/RAS_BoundingBox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,19 +161,15 @@ void RAS_MeshBoundingBox::Update(bool force)
return;
}

for (unsigned short i = 0, size = m_displayArrayList.size(); i < size; ++i) {
RAS_IDisplayArray *displayArray = m_displayArrayList[i];
m_aabbMin = MT_Vector3(FLT_MAX, FLT_MAX, FLT_MAX);
m_aabbMax = MT_Vector3(FLT_MIN, FLT_MIN, FLT_MIN);

for (RAS_IDisplayArray *displayArray : m_displayArrayList) {
// For each vertex.
for (unsigned int j = 0, size = displayArray->GetVertexCount(); j < size; ++j) {
RAS_IVertex *vert = displayArray->GetVertex(j);
for (unsigned int i = 0, size = displayArray->GetVertexCount(); i < size; ++i) {
RAS_IVertex *vert = displayArray->GetVertex(i);
const MT_Vector3 vertPos = vert->xyz();

// Initialize the AABB to the first vertex position.
if (j == 0 && i == 0) {
m_aabbMin = m_aabbMax = vertPos;
continue;
}

m_aabbMin.x() = std::min(m_aabbMin.x(), vertPos.x());
m_aabbMin.y() = std::min(m_aabbMin.y(), vertPos.y());
m_aabbMin.z() = std::min(m_aabbMin.z(), vertPos.z());
Expand Down

0 comments on commit b339165

Please sign in to comment.