123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254 |
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- /*
- * OPCODE - Optimized Collision Detection
- * Copyright (C) 2001 Pierre Terdiman
- * Homepage: http://www.codercorner.com/Opcode.htm
- */
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- /**
- * Contains code for tree builders.
- * \file OPC_TreeBuilders.cpp
- * \author Pierre Terdiman
- * \date March, 20, 2001
- */
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- /**
- * A builder for AABB-trees of vertices.
- *
- * \class AABBTreeOfVerticesBuilder
- * \author Pierre Terdiman
- * \version 1.3
- * \date March, 20, 2001
- */
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- /**
- * A builder for AABB-trees of AABBs.
- *
- * \class AABBTreeOfAABBsBuilder
- * \author Pierre Terdiman
- * \version 1.3
- * \date March, 20, 2001
- */
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- /**
- * A builder for AABB-trees of triangles.
- *
- * \class AABBTreeOfTrianglesBuilder
- * \author Pierre Terdiman
- * \version 1.3
- * \date March, 20, 2001
- */
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- #include "Opcode.h"
- using namespace Opcode;
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- /**
- * Computes the AABB of a set of primitives.
- * \param primitives [in] list of indices of primitives
- * \param nb_prims [in] number of indices
- * \param global_box [out] global AABB enclosing the set of input primitives
- * \return true if success
- */
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- bool AABBTreeOfAABBsBuilder::ComputeGlobalBox(const udword* primitives, udword nb_prims, AABB& global_box) const
- {
- // Checkings
- if(!primitives || !nb_prims) return false;
- // Initialize global box
- global_box = mAABBArray[primitives[0]];
- // Loop through boxes
- for(udword i=1;i<nb_prims;i++)
- {
- // Update global box
- global_box.Add(mAABBArray[primitives[i]]);
- }
- return true;
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- /**
- * Computes the splitting value along a given axis for a given primitive.
- * \param index [in] index of the primitive to split
- * \param axis [in] axis index (0,1,2)
- * \return splitting value
- */
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- float AABBTreeOfAABBsBuilder::GetSplittingValue(udword index, udword axis) const
- {
- // For an AABB, the splitting value is the middle of the given axis,
- // i.e. the corresponding component of the center point
- return mAABBArray[index].GetCenter(axis);
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- /**
- * Computes the AABB of a set of primitives.
- * \param primitives [in] list of indices of primitives
- * \param nb_prims [in] number of indices
- * \param global_box [out] global AABB enclosing the set of input primitives
- * \return true if success
- */
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- bool AABBTreeOfTrianglesBuilder::ComputeGlobalBox(const udword* primitives, udword nb_prims, AABB& global_box) const
- {
- // Checkings
- if(!primitives || !nb_prims) return false;
- // Initialize global box
- Point Min(MAX_FLOAT, MAX_FLOAT, MAX_FLOAT);
- Point Max(MIN_FLOAT, MIN_FLOAT, MIN_FLOAT);
- // Loop through triangles
- VertexPointers VP;
- while(nb_prims--)
- {
- // Get current triangle-vertices
- mIMesh->GetTriangle(VP, *primitives++);
- // Update global box
- Min.Min(*VP.Vertex[0]).Min(*VP.Vertex[1]).Min(*VP.Vertex[2]);
- Max.Max(*VP.Vertex[0]).Max(*VP.Vertex[1]).Max(*VP.Vertex[2]);
- }
- global_box.SetMinMax(Min, Max);
- return true;
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- /**
- * Computes the splitting value along a given axis for a given primitive.
- * \param index [in] index of the primitive to split
- * \param axis [in] axis index (0,1,2)
- * \return splitting value
- */
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- float AABBTreeOfTrianglesBuilder::GetSplittingValue(udword index, udword axis) const
- {
- /* // Compute center of triangle
- Point Center;
- mTriList[index].Center(mVerts, Center);
- // Return value
- return Center[axis];*/
- // Compute correct component from center of triangle
- // return (mVerts[mTriList[index].mVRef[0]][axis]
- // +mVerts[mTriList[index].mVRef[1]][axis]
- // +mVerts[mTriList[index].mVRef[2]][axis])*INV3;
- VertexPointers VP;
- mIMesh->GetTriangle(VP, index);
- // Compute correct component from center of triangle
- return ((*VP.Vertex[0])[axis]
- +(*VP.Vertex[1])[axis]
- +(*VP.Vertex[2])[axis])*INV3;
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- /**
- * Computes the splitting value along a given axis for a given node.
- * \param primitives [in] list of indices of primitives
- * \param nb_prims [in] number of indices
- * \param global_box [in] global AABB enclosing the set of input primitives
- * \param axis [in] axis index (0,1,2)
- * \return splitting value
- */
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- float AABBTreeOfTrianglesBuilder::GetSplittingValue(const udword* primitives, udword nb_prims, const AABB& global_box, udword axis) const
- {
- if(mSettings.mRules&SPLIT_GEOM_CENTER)
- {
- // Loop through triangles
- float SplitValue = 0.0f;
- VertexPointers VP;
- for(udword i=0;i<nb_prims;i++)
- {
- // Get current triangle-vertices
- mIMesh->GetTriangle(VP, primitives[i]);
- // Update split value
- SplitValue += (*VP.Vertex[0])[axis];
- SplitValue += (*VP.Vertex[1])[axis];
- SplitValue += (*VP.Vertex[2])[axis];
- }
- return SplitValue / float(nb_prims*3);
- }
- else return AABBTreeBuilder::GetSplittingValue(primitives, nb_prims, global_box, axis);
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- /**
- * Computes the AABB of a set of primitives.
- * \param primitives [in] list of indices of primitives
- * \param nb_prims [in] number of indices
- * \param global_box [out] global AABB enclosing the set of input primitives
- * \return true if success
- */
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- bool AABBTreeOfVerticesBuilder::ComputeGlobalBox(const udword* primitives, udword nb_prims, AABB& global_box) const
- {
- // Checkings
- if(!primitives || !nb_prims) return false;
- // Initialize global box
- global_box.SetEmpty();
- // Loop through vertices
- for(udword i=0;i<nb_prims;i++)
- {
- // Update global box
- global_box.Extend(mVertexArray[primitives[i]]);
- }
- return true;
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- /**
- * Computes the splitting value along a given axis for a given primitive.
- * \param index [in] index of the primitive to split
- * \param axis [in] axis index (0,1,2)
- * \return splitting value
- */
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- float AABBTreeOfVerticesBuilder::GetSplittingValue(udword index, udword axis) const
- {
- // For a vertex, the splitting value is simply the vertex coordinate.
- return mVertexArray[index][axis];
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- /**
- * Computes the splitting value along a given axis for a given node.
- * \param primitives [in] list of indices of primitives
- * \param nb_prims [in] number of indices
- * \param global_box [in] global AABB enclosing the set of input primitives
- * \param axis [in] axis index (0,1,2)
- * \return splitting value
- */
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- float AABBTreeOfVerticesBuilder::GetSplittingValue(const udword* primitives, udword nb_prims, const AABB& global_box, udword axis) const
- {
- if(mSettings.mRules&SPLIT_GEOM_CENTER)
- {
- // Loop through vertices
- float SplitValue = 0.0f;
- for(udword i=0;i<nb_prims;i++)
- {
- // Update split value
- SplitValue += mVertexArray[primitives[i]][axis];
- }
- return SplitValue / float(nb_prims);
- }
- else return AABBTreeBuilder::GetSplittingValue(primitives, nb_prims, global_box, axis);
- }
|