OPC_MeshInterface.cpp
Go to the documentation of this file.
00001 
00002 /*
00003  *      OPCODE - Optimized Collision Detection
00004  *      Copyright (C) 2001 Pierre Terdiman
00005  *      Homepage: http://www.codercorner.com/Opcode.htm
00006  */
00008 
00010 
00016 
00017 
00019 
00029 
00030 
00032 
00115 
00116 
00118 // Precompiled Header
00119 #include "Stdafx.h"
00120 
00121 using namespace Opcode;
00122 
00123 Point MeshInterface::VertexCache[3];
00124 
00126 
00129 
00130 MeshInterface::MeshInterface() :
00131         mNbTris                 (0),
00132         mNbVerts                (0),
00133 #ifdef OPC_USE_CALLBACKS
00134         mUserData               (null),
00135         mObjCallback    (null),
00136 #else
00137         mTris                   (null),
00138         mVerts                  (null),
00139         #ifdef OPC_USE_STRIDE
00140         mTriStride              (sizeof(IndexedTriangle)),
00141         mVertexStride   (sizeof(Point)),
00142         #endif
00143 #endif
00144 
00145         Single(true)
00146 {
00147 }
00148 
00150 
00153 
00154 MeshInterface::~MeshInterface()
00155 {
00156 }
00157 
00159 
00163 
00164 bool MeshInterface::IsValid() const
00165 {
00166         if(!mNbTris || !mNbVerts)       return false;
00167 #ifdef OPC_USE_CALLBACKS
00168         if(!mObjCallback)                       return false;
00169 #else
00170         if(!mTris || !mVerts)           return false;
00171 #endif
00172         return true;
00173 }
00174 
00176 
00181 
00182 udword MeshInterface::CheckTopology()   const
00183 {
00184         // Check topology. If the model contains degenerate faces, collision report can be wrong in some cases.
00185         // e.g. it happens with the standard MAX teapot. So clean your meshes first... If you don't have a mesh cleaner
00186         // you can try this: www.codercorner.com/Consolidation.zip
00187 
00188         udword NbDegenerate = 0;
00189 
00190         VertexPointers VP;
00191 
00192         // Using callbacks, we don't have access to vertex indices. Nevertheless we still can check for
00193         // redundant vertex pointers, which cover all possibilities (callbacks/pointers/strides).
00194         for(udword i=0;i<mNbTris;i++)
00195         {
00196                 GetTriangle(VP, i);
00197 
00198                 if(             (VP.Vertex[0]==VP.Vertex[1])
00199                         ||      (VP.Vertex[1]==VP.Vertex[2])
00200                         ||      (VP.Vertex[2]==VP.Vertex[0]))   NbDegenerate++;
00201         }
00202 
00203         return NbDegenerate;
00204 }
00205 
00206 #ifdef OPC_USE_CALLBACKS
00207 
00208 
00214 
00215 bool MeshInterface::SetCallback(RequestCallback callback, void* user_data)
00216 {
00217         if(!callback)   return SetIceError("MeshInterface::SetCallback: callback pointer is null");
00218 
00219         mObjCallback    = callback;
00220         mUserData               = user_data;
00221         return true;
00222 }
00223 #else
00224 
00225 
00231 
00232 bool MeshInterface::SetPointers(const IndexedTriangle* tris, const Point* verts)
00233 {
00234         if(!tris || !verts)     return SetIceError("MeshInterface::SetPointers: pointer is null", null);
00235 
00236         mTris   = tris;
00237         mVerts  = verts;
00238         return true;
00239 }
00240 #ifdef OPC_USE_STRIDE
00241 
00242 
00248 
00249 bool MeshInterface::SetStrides(udword tri_stride, udword vertex_stride)
00250 {
00251         if(tri_stride<sizeof(IndexedTriangle))  return SetIceError("MeshInterface::SetStrides: invalid triangle stride", null);
00252         if(vertex_stride<sizeof(Point))                 return SetIceError("MeshInterface::SetStrides: invalid vertex stride", null);
00253 
00254         mTriStride              = tri_stride;
00255         mVertexStride   = vertex_stride;
00256         return true;
00257 }
00258 #endif
00259 #endif
00260 
00262 
00268 
00269 bool MeshInterface::RemapClient(udword nb_indices, const udword* permutation) const
00270 {
00271         // Checkings
00272         if(!nb_indices || !permutation) return false;
00273         if(nb_indices!=mNbTris)                 return false;
00274 
00275 #ifdef OPC_USE_CALLBACKS
00276         // We can't really do that using callbacks
00277         return false;
00278 #else
00279         IndexedTriangle* Tmp = new IndexedTriangle[mNbTris];
00280         CHECKALLOC(Tmp);
00281 
00282         #ifdef OPC_USE_STRIDE
00283         udword Stride = mTriStride;
00284         #else
00285         udword Stride = sizeof(IndexedTriangle);
00286         #endif
00287 
00288         for(udword i=0;i<mNbTris;i++)
00289         {
00290                 const IndexedTriangle* T = (const IndexedTriangle*)(((ubyte*)mTris) + i * Stride);
00291                 Tmp[i] = *T;
00292         }
00293 
00294         for(udword i=0;i<mNbTris;i++)
00295         {
00296                 IndexedTriangle* T = (IndexedTriangle*)(((ubyte*)mTris) + i * Stride);
00297                 *T = Tmp[permutation[i]];
00298         }
00299 
00300         DELETEARRAY(Tmp);
00301 #endif
00302         return true;
00303 }


openhrp3
Author(s): AIST, General Robotix Inc., Nakamura Lab of Dept. of Mechano Informatics at University of Tokyo
autogenerated on Thu Apr 11 2019 03:30:18