2017-08-01 12:30:58 +00:00
|
|
|
#ifndef GIM_BOX_SET_H_INCLUDED
|
|
|
|
#define GIM_BOX_SET_H_INCLUDED
|
|
|
|
|
|
|
|
/*! \file gim_box_set.h
|
|
|
|
\author Francisco Leon Najera
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
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_array.h"
|
|
|
|
#include "gim_radixsort.h"
|
|
|
|
#include "gim_box_collision.h"
|
|
|
|
#include "gim_tri_collision.h"
|
2019-06-11 11:18:05 +00:00
|
|
|
#include "gim_pair.h"
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
//! A pairset array
|
2019-01-03 13:26:51 +00:00
|
|
|
class gim_pair_set : public gim_array<GIM_PAIR>
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
public:
|
2019-01-03 13:26:51 +00:00
|
|
|
gim_pair_set() : gim_array<GIM_PAIR>(32)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
}
|
2019-01-03 13:26:51 +00:00
|
|
|
inline void push_pair(GUINT index1, GUINT index2)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
push_back(GIM_PAIR(index1, index2));
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
inline void push_pair_inv(GUINT index1, GUINT index2)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
push_back(GIM_PAIR(index2, index1));
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
//! Prototype Base class for primitive classification
|
|
|
|
/*!
|
|
|
|
This class is a wrapper for primitive collections.
|
|
|
|
This tells relevant info for the Bounding Box set classes, which take care of space classification.
|
|
|
|
This class can manage Compound shapes and trimeshes, and if it is managing trimesh then the Hierarchy Bounding Box classes will take advantage of primitive Vs Box overlapping tests for getting optimal results and less Per Box compairisons.
|
|
|
|
*/
|
|
|
|
class GIM_PRIMITIVE_MANAGER_PROTOTYPE
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual ~GIM_PRIMITIVE_MANAGER_PROTOTYPE() {}
|
|
|
|
//! determines if this manager consist on only triangles, which special case will be optimized
|
|
|
|
virtual bool is_trimesh() = 0;
|
|
|
|
virtual GUINT get_primitive_count() = 0;
|
2019-01-03 13:26:51 +00:00
|
|
|
virtual void get_primitive_box(GUINT prim_index, GIM_AABB& primbox) = 0;
|
|
|
|
virtual void get_primitive_triangle(GUINT prim_index, GIM_TRIANGLE& triangle) = 0;
|
2017-08-01 12:30:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct GIM_AABB_DATA
|
|
|
|
{
|
|
|
|
GIM_AABB m_bound;
|
|
|
|
GUINT m_data;
|
|
|
|
};
|
|
|
|
|
|
|
|
//! Node Structure for trees
|
|
|
|
struct GIM_BOX_TREE_NODE
|
|
|
|
{
|
|
|
|
GIM_AABB m_bound;
|
2019-01-03 13:26:51 +00:00
|
|
|
GUINT m_left; //!< Left subtree
|
|
|
|
GUINT m_right; //!< Right subtree
|
|
|
|
GUINT m_escapeIndex; //!< Scape index for traversing
|
|
|
|
GUINT m_data; //!< primitive index if apply
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
GIM_BOX_TREE_NODE()
|
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
m_left = 0;
|
|
|
|
m_right = 0;
|
|
|
|
m_escapeIndex = 0;
|
|
|
|
m_data = 0;
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SIMD_FORCE_INLINE bool is_leaf_node() const
|
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
return (!m_left && !m_right);
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
//! Basic Box tree structure
|
|
|
|
class GIM_BOX_TREE
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
GUINT m_num_nodes;
|
|
|
|
gim_array<GIM_BOX_TREE_NODE> m_node_array;
|
2019-01-03 13:26:51 +00:00
|
|
|
|
2017-08-01 12:30:58 +00:00
|
|
|
protected:
|
|
|
|
GUINT _sort_and_calc_splitting_index(
|
2019-01-03 13:26:51 +00:00
|
|
|
gim_array<GIM_AABB_DATA>& primitive_boxes,
|
|
|
|
GUINT startIndex, GUINT endIndex, GUINT splitAxis);
|
2017-08-01 12:30:58 +00:00
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
GUINT _calc_splitting_axis(gim_array<GIM_AABB_DATA>& primitive_boxes, GUINT startIndex, GUINT endIndex);
|
|
|
|
|
|
|
|
void _build_sub_tree(gim_array<GIM_AABB_DATA>& primitive_boxes, GUINT startIndex, GUINT endIndex);
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
GIM_BOX_TREE()
|
|
|
|
{
|
|
|
|
m_num_nodes = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//! prototype functions for box tree management
|
|
|
|
//!@{
|
2019-01-03 13:26:51 +00:00
|
|
|
void build_tree(gim_array<GIM_AABB_DATA>& primitive_boxes);
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
SIMD_FORCE_INLINE void clearNodes()
|
|
|
|
{
|
|
|
|
m_node_array.clear();
|
|
|
|
m_num_nodes = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//! node count
|
|
|
|
SIMD_FORCE_INLINE GUINT getNodeCount() const
|
|
|
|
{
|
|
|
|
return m_num_nodes;
|
|
|
|
}
|
|
|
|
|
|
|
|
//! tells if the node is a leaf
|
|
|
|
SIMD_FORCE_INLINE bool isLeafNode(GUINT nodeindex) const
|
|
|
|
{
|
|
|
|
return m_node_array[nodeindex].is_leaf_node();
|
|
|
|
}
|
|
|
|
|
|
|
|
SIMD_FORCE_INLINE GUINT getNodeData(GUINT nodeindex) const
|
|
|
|
{
|
|
|
|
return m_node_array[nodeindex].m_data;
|
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
SIMD_FORCE_INLINE void getNodeBound(GUINT nodeindex, GIM_AABB& bound) const
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
bound = m_node_array[nodeindex].m_bound;
|
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
SIMD_FORCE_INLINE void setNodeBound(GUINT nodeindex, const GIM_AABB& bound)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
m_node_array[nodeindex].m_bound = bound;
|
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
SIMD_FORCE_INLINE GUINT getLeftNodeIndex(GUINT nodeindex) const
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
return m_node_array[nodeindex].m_left;
|
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
SIMD_FORCE_INLINE GUINT getRightNodeIndex(GUINT nodeindex) const
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
return m_node_array[nodeindex].m_right;
|
|
|
|
}
|
|
|
|
|
|
|
|
SIMD_FORCE_INLINE GUINT getScapeNodeIndex(GUINT nodeindex) const
|
|
|
|
{
|
|
|
|
return m_node_array[nodeindex].m_escapeIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
//!@}
|
|
|
|
};
|
|
|
|
|
|
|
|
//! Generic Box Tree Template
|
|
|
|
/*!
|
|
|
|
This class offers an structure for managing a box tree of primitives.
|
|
|
|
Requires a Primitive prototype (like GIM_PRIMITIVE_MANAGER_PROTOTYPE ) and
|
|
|
|
a Box tree structure ( like GIM_BOX_TREE).
|
|
|
|
*/
|
2019-01-03 13:26:51 +00:00
|
|
|
template <typename _GIM_PRIMITIVE_MANAGER_PROTOTYPE, typename _GIM_BOX_TREE_PROTOTYPE>
|
2017-08-01 12:30:58 +00:00
|
|
|
class GIM_BOX_TREE_TEMPLATE_SET
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
_GIM_PRIMITIVE_MANAGER_PROTOTYPE m_primitive_manager;
|
|
|
|
_GIM_BOX_TREE_PROTOTYPE m_box_tree;
|
2019-01-03 13:26:51 +00:00
|
|
|
|
2017-08-01 12:30:58 +00:00
|
|
|
protected:
|
|
|
|
//stackless refit
|
|
|
|
SIMD_FORCE_INLINE void refit()
|
|
|
|
{
|
|
|
|
GUINT nodecount = getNodeCount();
|
2019-01-03 13:26:51 +00:00
|
|
|
while (nodecount--)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
if (isLeafNode(nodecount))
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
GIM_AABB leafbox;
|
2019-01-03 13:26:51 +00:00
|
|
|
m_primitive_manager.get_primitive_box(getNodeData(nodecount), leafbox);
|
|
|
|
setNodeBound(nodecount, leafbox);
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//get left bound
|
|
|
|
GUINT childindex = getLeftNodeIndex(nodecount);
|
|
|
|
GIM_AABB bound;
|
2019-01-03 13:26:51 +00:00
|
|
|
getNodeBound(childindex, bound);
|
2017-08-01 12:30:58 +00:00
|
|
|
//get right bound
|
|
|
|
childindex = getRightNodeIndex(nodecount);
|
|
|
|
GIM_AABB bound2;
|
2019-01-03 13:26:51 +00:00
|
|
|
getNodeBound(childindex, bound2);
|
2017-08-01 12:30:58 +00:00
|
|
|
bound.merge(bound2);
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
setNodeBound(nodecount, bound);
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
public:
|
2017-08-01 12:30:58 +00:00
|
|
|
GIM_BOX_TREE_TEMPLATE_SET()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
SIMD_FORCE_INLINE GIM_AABB getGlobalBox() const
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
GIM_AABB totalbox;
|
|
|
|
getNodeBound(0, totalbox);
|
|
|
|
return totalbox;
|
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
SIMD_FORCE_INLINE void setPrimitiveManager(const _GIM_PRIMITIVE_MANAGER_PROTOTYPE& primitive_manager)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
m_primitive_manager = primitive_manager;
|
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
const _GIM_PRIMITIVE_MANAGER_PROTOTYPE& getPrimitiveManager() const
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
return m_primitive_manager;
|
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
_GIM_PRIMITIVE_MANAGER_PROTOTYPE& getPrimitiveManager()
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
return m_primitive_manager;
|
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
//! node manager prototype functions
|
|
|
|
///@{
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
//! this attemps to refit the box set.
|
|
|
|
SIMD_FORCE_INLINE void update()
|
|
|
|
{
|
|
|
|
refit();
|
|
|
|
}
|
|
|
|
|
|
|
|
//! this rebuild the entire set
|
|
|
|
SIMD_FORCE_INLINE void buildSet()
|
|
|
|
{
|
|
|
|
//obtain primitive boxes
|
|
|
|
gim_array<GIM_AABB_DATA> primitive_boxes;
|
2019-01-03 13:26:51 +00:00
|
|
|
primitive_boxes.resize(m_primitive_manager.get_primitive_count(), false);
|
2017-08-01 12:30:58 +00:00
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
for (GUINT i = 0; i < primitive_boxes.size(); i++)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
m_primitive_manager.get_primitive_box(i, primitive_boxes[i].m_bound);
|
|
|
|
primitive_boxes[i].m_data = i;
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m_box_tree.build_tree(primitive_boxes);
|
|
|
|
}
|
|
|
|
|
|
|
|
//! returns the indices of the primitives in the m_primitive_manager
|
2019-01-03 13:26:51 +00:00
|
|
|
SIMD_FORCE_INLINE bool boxQuery(const GIM_AABB& box, gim_array<GUINT>& collided_results) const
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
GUINT curIndex = 0;
|
|
|
|
GUINT numNodes = getNodeCount();
|
|
|
|
|
|
|
|
while (curIndex < numNodes)
|
|
|
|
{
|
|
|
|
GIM_AABB bound;
|
2019-01-03 13:26:51 +00:00
|
|
|
getNodeBound(curIndex, bound);
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
//catch bugs in tree data
|
|
|
|
|
|
|
|
bool aabbOverlap = bound.has_collision(box);
|
|
|
|
bool isleafnode = isLeafNode(curIndex);
|
|
|
|
|
|
|
|
if (isleafnode && aabbOverlap)
|
|
|
|
{
|
|
|
|
collided_results.push_back(getNodeData(curIndex));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aabbOverlap || isleafnode)
|
|
|
|
{
|
|
|
|
//next subnode
|
|
|
|
curIndex++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//skip node
|
2019-01-03 13:26:51 +00:00
|
|
|
curIndex += getScapeNodeIndex(curIndex);
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
}
|
2019-01-03 13:26:51 +00:00
|
|
|
if (collided_results.size() > 0) return true;
|
2017-08-01 12:30:58 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//! returns the indices of the primitives in the m_primitive_manager
|
2019-01-03 13:26:51 +00:00
|
|
|
SIMD_FORCE_INLINE bool boxQueryTrans(const GIM_AABB& box,
|
|
|
|
const btTransform& transform, gim_array<GUINT>& collided_results) const
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
GIM_AABB transbox = box;
|
2017-08-01 12:30:58 +00:00
|
|
|
transbox.appy_transform(transform);
|
2019-01-03 13:26:51 +00:00
|
|
|
return boxQuery(transbox, collided_results);
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//! returns the indices of the primitives in the m_primitive_manager
|
|
|
|
SIMD_FORCE_INLINE bool rayQuery(
|
2019-01-03 13:26:51 +00:00
|
|
|
const btVector3& ray_dir, const btVector3& ray_origin,
|
|
|
|
gim_array<GUINT>& collided_results) const
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
GUINT curIndex = 0;
|
|
|
|
GUINT numNodes = getNodeCount();
|
|
|
|
|
|
|
|
while (curIndex < numNodes)
|
|
|
|
{
|
|
|
|
GIM_AABB bound;
|
2019-01-03 13:26:51 +00:00
|
|
|
getNodeBound(curIndex, bound);
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
//catch bugs in tree data
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
bool aabbOverlap = bound.collide_ray(ray_origin, ray_dir);
|
2017-08-01 12:30:58 +00:00
|
|
|
bool isleafnode = isLeafNode(curIndex);
|
|
|
|
|
|
|
|
if (isleafnode && aabbOverlap)
|
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
collided_results.push_back(getNodeData(curIndex));
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (aabbOverlap || isleafnode)
|
|
|
|
{
|
|
|
|
//next subnode
|
|
|
|
curIndex++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//skip node
|
2019-01-03 13:26:51 +00:00
|
|
|
curIndex += getScapeNodeIndex(curIndex);
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
}
|
2019-01-03 13:26:51 +00:00
|
|
|
if (collided_results.size() > 0) return true;
|
2017-08-01 12:30:58 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//! tells if this set has hierarcht
|
|
|
|
SIMD_FORCE_INLINE bool hasHierarchy() const
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//! tells if this set is a trimesh
|
2019-01-03 13:26:51 +00:00
|
|
|
SIMD_FORCE_INLINE bool isTrimesh() const
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
return m_primitive_manager.is_trimesh();
|
|
|
|
}
|
|
|
|
|
|
|
|
//! node count
|
|
|
|
SIMD_FORCE_INLINE GUINT getNodeCount() const
|
|
|
|
{
|
|
|
|
return m_box_tree.getNodeCount();
|
|
|
|
}
|
|
|
|
|
|
|
|
//! tells if the node is a leaf
|
|
|
|
SIMD_FORCE_INLINE bool isLeafNode(GUINT nodeindex) const
|
|
|
|
{
|
|
|
|
return m_box_tree.isLeafNode(nodeindex);
|
|
|
|
}
|
|
|
|
|
|
|
|
SIMD_FORCE_INLINE GUINT getNodeData(GUINT nodeindex) const
|
|
|
|
{
|
|
|
|
return m_box_tree.getNodeData(nodeindex);
|
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
SIMD_FORCE_INLINE void getNodeBound(GUINT nodeindex, GIM_AABB& bound) const
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
m_box_tree.getNodeBound(nodeindex, bound);
|
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
SIMD_FORCE_INLINE void setNodeBound(GUINT nodeindex, const GIM_AABB& bound)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
m_box_tree.setNodeBound(nodeindex, bound);
|
|
|
|
}
|
|
|
|
|
|
|
|
SIMD_FORCE_INLINE GUINT getLeftNodeIndex(GUINT nodeindex) const
|
|
|
|
{
|
|
|
|
return m_box_tree.getLeftNodeIndex(nodeindex);
|
|
|
|
}
|
|
|
|
|
|
|
|
SIMD_FORCE_INLINE GUINT getRightNodeIndex(GUINT nodeindex) const
|
|
|
|
{
|
|
|
|
return m_box_tree.getRightNodeIndex(nodeindex);
|
|
|
|
}
|
|
|
|
|
|
|
|
SIMD_FORCE_INLINE GUINT getScapeNodeIndex(GUINT nodeindex) const
|
|
|
|
{
|
|
|
|
return m_box_tree.getScapeNodeIndex(nodeindex);
|
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
SIMD_FORCE_INLINE void getNodeTriangle(GUINT nodeindex, GIM_TRIANGLE& triangle) const
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
m_primitive_manager.get_primitive_triangle(getNodeData(nodeindex), triangle);
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
//! Class for Box Tree Sets
|
|
|
|
/*!
|
|
|
|
this has the GIM_BOX_TREE implementation for bounding boxes.
|
|
|
|
*/
|
2019-01-03 13:26:51 +00:00
|
|
|
template <typename _GIM_PRIMITIVE_MANAGER_PROTOTYPE>
|
|
|
|
class GIM_BOX_TREE_SET : public GIM_BOX_TREE_TEMPLATE_SET<_GIM_PRIMITIVE_MANAGER_PROTOTYPE, GIM_BOX_TREE>
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
};
|
|
|
|
|
|
|
|
/// GIM_BOX_SET collision methods
|
2019-01-03 13:26:51 +00:00
|
|
|
template <typename BOX_SET_CLASS0, typename BOX_SET_CLASS1>
|
2017-08-01 12:30:58 +00:00
|
|
|
class GIM_TREE_TREE_COLLIDER
|
|
|
|
{
|
|
|
|
public:
|
2019-01-03 13:26:51 +00:00
|
|
|
gim_pair_set* m_collision_pairs;
|
|
|
|
BOX_SET_CLASS0* m_boxset0;
|
|
|
|
BOX_SET_CLASS1* m_boxset1;
|
2017-08-01 12:30:58 +00:00
|
|
|
GUINT current_node0;
|
|
|
|
GUINT current_node1;
|
|
|
|
bool node0_is_leaf;
|
|
|
|
bool node1_is_leaf;
|
|
|
|
bool t0_is_trimesh;
|
|
|
|
bool t1_is_trimesh;
|
|
|
|
bool node0_has_triangle;
|
|
|
|
bool node1_has_triangle;
|
|
|
|
GIM_AABB m_box0;
|
|
|
|
GIM_AABB m_box1;
|
|
|
|
GIM_BOX_BOX_TRANSFORM_CACHE trans_cache_1to0;
|
|
|
|
btTransform trans_cache_0to1;
|
|
|
|
GIM_TRIANGLE m_tri0;
|
|
|
|
btVector4 m_tri0_plane;
|
|
|
|
GIM_TRIANGLE m_tri1;
|
|
|
|
btVector4 m_tri1_plane;
|
|
|
|
|
|
|
|
public:
|
|
|
|
GIM_TREE_TREE_COLLIDER()
|
|
|
|
{
|
|
|
|
current_node0 = G_UINT_INFINITY;
|
|
|
|
current_node1 = G_UINT_INFINITY;
|
|
|
|
}
|
2019-01-03 13:26:51 +00:00
|
|
|
|
2017-08-01 12:30:58 +00:00
|
|
|
protected:
|
|
|
|
SIMD_FORCE_INLINE void retrieve_node0_triangle(GUINT node0)
|
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
if (node0_has_triangle) return;
|
|
|
|
m_boxset0->getNodeTriangle(node0, m_tri0);
|
2017-08-01 12:30:58 +00:00
|
|
|
//transform triangle
|
|
|
|
m_tri0.m_vertices[0] = trans_cache_0to1(m_tri0.m_vertices[0]);
|
|
|
|
m_tri0.m_vertices[1] = trans_cache_0to1(m_tri0.m_vertices[1]);
|
|
|
|
m_tri0.m_vertices[2] = trans_cache_0to1(m_tri0.m_vertices[2]);
|
|
|
|
m_tri0.get_plane(m_tri0_plane);
|
|
|
|
|
|
|
|
node0_has_triangle = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
SIMD_FORCE_INLINE void retrieve_node1_triangle(GUINT node1)
|
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
if (node1_has_triangle) return;
|
|
|
|
m_boxset1->getNodeTriangle(node1, m_tri1);
|
2017-08-01 12:30:58 +00:00
|
|
|
//transform triangle
|
|
|
|
m_tri1.m_vertices[0] = trans_cache_1to0.transform(m_tri1.m_vertices[0]);
|
|
|
|
m_tri1.m_vertices[1] = trans_cache_1to0.transform(m_tri1.m_vertices[1]);
|
|
|
|
m_tri1.m_vertices[2] = trans_cache_1to0.transform(m_tri1.m_vertices[2]);
|
|
|
|
m_tri1.get_plane(m_tri1_plane);
|
|
|
|
|
|
|
|
node1_has_triangle = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
SIMD_FORCE_INLINE void retrieve_node0_info(GUINT node0)
|
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
if (node0 == current_node0) return;
|
|
|
|
m_boxset0->getNodeBound(node0, m_box0);
|
2017-08-01 12:30:58 +00:00
|
|
|
node0_is_leaf = m_boxset0->isLeafNode(node0);
|
|
|
|
node0_has_triangle = false;
|
|
|
|
current_node0 = node0;
|
|
|
|
}
|
|
|
|
|
|
|
|
SIMD_FORCE_INLINE void retrieve_node1_info(GUINT node1)
|
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
if (node1 == current_node1) return;
|
|
|
|
m_boxset1->getNodeBound(node1, m_box1);
|
2017-08-01 12:30:58 +00:00
|
|
|
node1_is_leaf = m_boxset1->isLeafNode(node1);
|
|
|
|
node1_has_triangle = false;
|
|
|
|
current_node1 = node1;
|
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
SIMD_FORCE_INLINE bool node_collision(GUINT node0, GUINT node1)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
retrieve_node0_info(node0);
|
|
|
|
retrieve_node1_info(node1);
|
2019-01-03 13:26:51 +00:00
|
|
|
bool result = m_box0.overlapping_trans_cache(m_box1, trans_cache_1to0, true);
|
|
|
|
if (!result) return false;
|
2017-08-01 12:30:58 +00:00
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
if (t0_is_trimesh && node0_is_leaf)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
//perform primitive vs box collision
|
|
|
|
retrieve_node0_triangle(node0);
|
|
|
|
//do triangle vs box collision
|
|
|
|
m_box1.increment_margin(m_tri0.m_margin);
|
|
|
|
|
|
|
|
result = m_box1.collide_triangle_exact(
|
2019-01-03 13:26:51 +00:00
|
|
|
m_tri0.m_vertices[0], m_tri0.m_vertices[1], m_tri0.m_vertices[2], m_tri0_plane);
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
m_box1.increment_margin(-m_tri0.m_margin);
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
if (!result) return false;
|
2017-08-01 12:30:58 +00:00
|
|
|
return true;
|
|
|
|
}
|
2019-01-03 13:26:51 +00:00
|
|
|
else if (t1_is_trimesh && node1_is_leaf)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
//perform primitive vs box collision
|
|
|
|
retrieve_node1_triangle(node1);
|
|
|
|
//do triangle vs box collision
|
|
|
|
m_box0.increment_margin(m_tri1.m_margin);
|
|
|
|
|
|
|
|
result = m_box0.collide_triangle_exact(
|
2019-01-03 13:26:51 +00:00
|
|
|
m_tri1.m_vertices[0], m_tri1.m_vertices[1], m_tri1.m_vertices[2], m_tri1_plane);
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
m_box0.increment_margin(-m_tri1.m_margin);
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
if (!result) return false;
|
2017-08-01 12:30:58 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//stackless collision routine
|
|
|
|
void find_collision_pairs()
|
|
|
|
{
|
|
|
|
gim_pair_set stack_collisions;
|
|
|
|
stack_collisions.reserve(32);
|
|
|
|
|
|
|
|
//add the first pair
|
2019-01-03 13:26:51 +00:00
|
|
|
stack_collisions.push_pair(0, 0);
|
2017-08-01 12:30:58 +00:00
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
while (stack_collisions.size())
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
//retrieve the last pair and pop
|
|
|
|
GUINT node0 = stack_collisions.back().m_index1;
|
|
|
|
GUINT node1 = stack_collisions.back().m_index2;
|
|
|
|
stack_collisions.pop_back();
|
2019-01-03 13:26:51 +00:00
|
|
|
if (node_collision(node0, node1)) // a collision is found
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
if (node0_is_leaf)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
if (node1_is_leaf)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
m_collision_pairs->push_pair(m_boxset0->getNodeData(node0), m_boxset1->getNodeData(node1));
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//collide left
|
2019-01-03 13:26:51 +00:00
|
|
|
stack_collisions.push_pair(node0, m_boxset1->getLeftNodeIndex(node1));
|
2017-08-01 12:30:58 +00:00
|
|
|
|
|
|
|
//collide right
|
2019-01-03 13:26:51 +00:00
|
|
|
stack_collisions.push_pair(node0, m_boxset1->getRightNodeIndex(node1));
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
if (node1_is_leaf)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
//collide left
|
2019-01-03 13:26:51 +00:00
|
|
|
stack_collisions.push_pair(m_boxset0->getLeftNodeIndex(node0), node1);
|
2017-08-01 12:30:58 +00:00
|
|
|
//collide right
|
2019-01-03 13:26:51 +00:00
|
|
|
stack_collisions.push_pair(m_boxset0->getRightNodeIndex(node0), node1);
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
GUINT left0 = m_boxset0->getLeftNodeIndex(node0);
|
|
|
|
GUINT right0 = m_boxset0->getRightNodeIndex(node0);
|
|
|
|
GUINT left1 = m_boxset1->getLeftNodeIndex(node1);
|
|
|
|
GUINT right1 = m_boxset1->getRightNodeIndex(node1);
|
|
|
|
//collide left
|
2019-01-03 13:26:51 +00:00
|
|
|
stack_collisions.push_pair(left0, left1);
|
2017-08-01 12:30:58 +00:00
|
|
|
//collide right
|
2019-01-03 13:26:51 +00:00
|
|
|
stack_collisions.push_pair(left0, right1);
|
2017-08-01 12:30:58 +00:00
|
|
|
//collide left
|
2019-01-03 13:26:51 +00:00
|
|
|
stack_collisions.push_pair(right0, left1);
|
2017-08-01 12:30:58 +00:00
|
|
|
//collide right
|
2019-01-03 13:26:51 +00:00
|
|
|
stack_collisions.push_pair(right0, right1);
|
2017-08-01 12:30:58 +00:00
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
} // else if node1 is not a leaf
|
|
|
|
} // else if node0 is not a leaf
|
2017-08-01 12:30:58 +00:00
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
} // if(node_collision(node0,node1))
|
|
|
|
} //while(stack_collisions.size())
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
2019-01-03 13:26:51 +00:00
|
|
|
|
2017-08-01 12:30:58 +00:00
|
|
|
public:
|
2019-01-03 13:26:51 +00:00
|
|
|
void find_collision(BOX_SET_CLASS0* boxset1, const btTransform& trans1,
|
|
|
|
BOX_SET_CLASS1* boxset2, const btTransform& trans2,
|
|
|
|
gim_pair_set& collision_pairs, bool complete_primitive_tests = true)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
m_collision_pairs = &collision_pairs;
|
|
|
|
m_boxset0 = boxset1;
|
|
|
|
m_boxset1 = boxset2;
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
trans_cache_1to0.calc_from_homogenic(trans1, trans2);
|
2017-08-01 12:30:58 +00:00
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
trans_cache_0to1 = trans2.inverse();
|
2017-08-01 12:30:58 +00:00
|
|
|
trans_cache_0to1 *= trans1;
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
if (complete_primitive_tests)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
t0_is_trimesh = boxset1->getPrimitiveManager().is_trimesh();
|
|
|
|
t1_is_trimesh = boxset2->getPrimitiveManager().is_trimesh();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
t0_is_trimesh = false;
|
|
|
|
t1_is_trimesh = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
find_collision_pairs();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
#endif // GIM_BOXPRUNING_H_INCLUDED
|