2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
This source file is part of GIMPACT Library.
|
|
|
|
|
|
|
|
For the latest info, see http://gimpact.sourceforge.net/
|
|
|
|
|
|
|
|
Copyright (c) 2006 Francisco Leon Najera. C.C. 80087371.
|
|
|
|
email: projectileman@yahoo.com
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of EITHER:
|
|
|
|
(1) The GNU Lesser General Public License as published by the Free
|
|
|
|
Software Foundation; either version 2.1 of the License, or (at
|
|
|
|
your option) any later version. The text of the GNU Lesser
|
|
|
|
General Public License is included with this library in the
|
|
|
|
file GIMPACT-LICENSE-LGPL.TXT.
|
|
|
|
(2) The BSD-style license that is included with this library in
|
|
|
|
the file GIMPACT-LICENSE-BSD.TXT.
|
|
|
|
(3) The zlib/libpng license that is included with this library in
|
|
|
|
the file GIMPACT-LICENSE-ZLIB.TXT.
|
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files
|
|
|
|
GIMPACT-LICENSE-LGPL.TXT, GIMPACT-LICENSE-ZLIB.TXT and GIMPACT-LICENSE-BSD.TXT for more details.
|
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "gim_box_set.h"
|
|
|
|
|
|
|
|
GUINT GIM_BOX_TREE::_calc_splitting_axis(
|
2019-01-03 13:26:51 +00:00
|
|
|
gim_array<GIM_AABB_DATA>& primitive_boxes, GUINT startIndex, GUINT endIndex)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
GUINT i;
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
btVector3 means(btScalar(0.), btScalar(0.), btScalar(0.));
|
|
|
|
btVector3 variance(btScalar(0.), btScalar(0.), btScalar(0.));
|
|
|
|
GUINT numIndices = endIndex - startIndex;
|
2017-08-01 12:30:58 +00:00
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
for (i = startIndex; i < endIndex; i++)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
btVector3 center = btScalar(0.5) * (primitive_boxes[i].m_bound.m_max +
|
|
|
|
primitive_boxes[i].m_bound.m_min);
|
|
|
|
means += center;
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
2019-01-03 13:26:51 +00:00
|
|
|
means *= (btScalar(1.) / (btScalar)numIndices);
|
2017-08-01 12:30:58 +00:00
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
for (i = startIndex; i < endIndex; i++)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
btVector3 center = btScalar(0.5) * (primitive_boxes[i].m_bound.m_max +
|
|
|
|
primitive_boxes[i].m_bound.m_min);
|
|
|
|
btVector3 diff2 = center - means;
|
2017-08-01 12:30:58 +00:00
|
|
|
diff2 = diff2 * diff2;
|
|
|
|
variance += diff2;
|
|
|
|
}
|
2019-01-03 13:26:51 +00:00
|
|
|
variance *= (btScalar(1.) / ((btScalar)numIndices - 1));
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
return variance.maxAxis();
|
|
|
|
}
|
|
|
|
|
|
|
|
GUINT GIM_BOX_TREE::_sort_and_calc_splitting_index(
|
2019-01-03 13:26:51 +00:00
|
|
|
gim_array<GIM_AABB_DATA>& primitive_boxes, GUINT startIndex,
|
2017-08-01 12:30:58 +00:00
|
|
|
GUINT endIndex, GUINT splitAxis)
|
|
|
|
{
|
|
|
|
GUINT i;
|
2019-01-03 13:26:51 +00:00
|
|
|
GUINT splitIndex = startIndex;
|
2017-08-01 12:30:58 +00:00
|
|
|
GUINT numIndices = endIndex - startIndex;
|
|
|
|
|
|
|
|
// average of centers
|
|
|
|
btScalar splitValue = 0.0f;
|
2019-01-03 13:26:51 +00:00
|
|
|
for (i = startIndex; i < endIndex; i++)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
splitValue += 0.5f * (primitive_boxes[i].m_bound.m_max[splitAxis] +
|
|
|
|
primitive_boxes[i].m_bound.m_min[splitAxis]);
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
splitValue /= (btScalar)numIndices;
|
|
|
|
|
|
|
|
//sort leafNodes so all values larger then splitValue comes first, and smaller values start from 'splitIndex'.
|
2019-01-03 13:26:51 +00:00
|
|
|
for (i = startIndex; i < endIndex; i++)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
btScalar center = 0.5f * (primitive_boxes[i].m_bound.m_max[splitAxis] +
|
|
|
|
primitive_boxes[i].m_bound.m_min[splitAxis]);
|
2017-08-01 12:30:58 +00:00
|
|
|
if (center > splitValue)
|
|
|
|
{
|
|
|
|
//swap
|
2019-01-03 13:26:51 +00:00
|
|
|
primitive_boxes.swap(i, splitIndex);
|
2017-08-01 12:30:58 +00:00
|
|
|
splitIndex++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//if the splitIndex causes unbalanced trees, fix this by using the center in between startIndex and endIndex
|
|
|
|
//otherwise the tree-building might fail due to stack-overflows in certain cases.
|
|
|
|
//unbalanced1 is unsafe: it can cause stack overflows
|
|
|
|
//bool unbalanced1 = ((splitIndex==startIndex) || (splitIndex == (endIndex-1)));
|
|
|
|
|
|
|
|
//unbalanced2 should work too: always use center (perfect balanced trees)
|
|
|
|
//bool unbalanced2 = true;
|
|
|
|
|
|
|
|
//this should be safe too:
|
2019-01-03 13:26:51 +00:00
|
|
|
GUINT rangeBalancedIndices = numIndices / 3;
|
|
|
|
bool unbalanced = ((splitIndex <= (startIndex + rangeBalancedIndices)) || (splitIndex >= (endIndex - 1 - rangeBalancedIndices)));
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
if (unbalanced)
|
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
splitIndex = startIndex + (numIndices >> 1);
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
btAssert(!((splitIndex == startIndex) || (splitIndex == (endIndex))));
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
return splitIndex;
|
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
void GIM_BOX_TREE::_build_sub_tree(gim_array<GIM_AABB_DATA>& primitive_boxes, GUINT startIndex, GUINT endIndex)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
GUINT current_index = m_num_nodes++;
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
btAssert((endIndex - startIndex) > 0);
|
2017-08-01 12:30:58 +00:00
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
if ((endIndex - startIndex) == 1) //we got a leaf
|
|
|
|
{
|
2017-08-01 12:30:58 +00:00
|
|
|
m_node_array[current_index].m_left = 0;
|
|
|
|
m_node_array[current_index].m_right = 0;
|
|
|
|
m_node_array[current_index].m_escapeIndex = 0;
|
|
|
|
|
|
|
|
m_node_array[current_index].m_bound = primitive_boxes[startIndex].m_bound;
|
|
|
|
m_node_array[current_index].m_data = primitive_boxes[startIndex].m_data;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//configure inner node
|
|
|
|
|
|
|
|
GUINT splitIndex;
|
|
|
|
|
|
|
|
//calc this node bounding box
|
2019-01-03 13:26:51 +00:00
|
|
|
m_node_array[current_index].m_bound.invalidate();
|
|
|
|
for (splitIndex = startIndex; splitIndex < endIndex; splitIndex++)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
m_node_array[current_index].m_bound.merge(primitive_boxes[splitIndex].m_bound);
|
|
|
|
}
|
|
|
|
|
|
|
|
//calculate Best Splitting Axis and where to split it. Sort the incoming 'leafNodes' array within range 'startIndex/endIndex'.
|
|
|
|
|
|
|
|
//split axis
|
2019-01-03 13:26:51 +00:00
|
|
|
splitIndex = _calc_splitting_axis(primitive_boxes, startIndex, endIndex);
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
splitIndex = _sort_and_calc_splitting_index(
|
2019-01-03 13:26:51 +00:00
|
|
|
primitive_boxes, startIndex, endIndex, splitIndex);
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
//configure this inner node : the left node index
|
|
|
|
m_node_array[current_index].m_left = m_num_nodes;
|
|
|
|
//build left child tree
|
2019-01-03 13:26:51 +00:00
|
|
|
_build_sub_tree(primitive_boxes, startIndex, splitIndex);
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
//configure this inner node : the right node index
|
|
|
|
m_node_array[current_index].m_right = m_num_nodes;
|
|
|
|
|
|
|
|
//build right child tree
|
2019-01-03 13:26:51 +00:00
|
|
|
_build_sub_tree(primitive_boxes, splitIndex, endIndex);
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
//configure this inner node : the escape index
|
2019-01-03 13:26:51 +00:00
|
|
|
m_node_array[current_index].m_escapeIndex = m_num_nodes - current_index;
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//! stackless build tree
|
|
|
|
void GIM_BOX_TREE::build_tree(
|
2019-01-03 13:26:51 +00:00
|
|
|
gim_array<GIM_AABB_DATA>& primitive_boxes)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
// initialize node count to 0
|
|
|
|
m_num_nodes = 0;
|
|
|
|
// allocate nodes
|
2019-01-03 13:26:51 +00:00
|
|
|
m_node_array.resize(primitive_boxes.size() * 2);
|
|
|
|
|
2017-08-01 12:30:58 +00:00
|
|
|
_build_sub_tree(primitive_boxes, 0, primitive_boxes.size());
|
|
|
|
}
|