OPC_Model.cpp
Go to the documentation of this file.
1 /*
3  * OPCODE - Optimized Collision Detection
4  * Copyright (C) 2001 Pierre Terdiman
5  * Homepage: http://www.codercorner.com/Opcode.htm
6  */
8 
10 
16 
19 
89 
92 // Precompiled Header
93 #include "Stdafx.h"
94 
95 using namespace Opcode;
96 
98 
101 Model::Model()
103 {
104 #ifdef __MESHMERIZER_H__ // Collision hulls only supported within ICE !
105  mHull = null;
106 #endif // __MESHMERIZER_H__
107 }
108 
110 
115 {
116  Release();
117 }
118 
120 
123 void Model::Release()
125 {
126  ReleaseBase();
127 #ifdef __MESHMERIZER_H__ // Collision hulls only supported within ICE !
128  DELETESINGLE(mHull);
129 #endif // __MESHMERIZER_H__
130 }
131 
133 
138 bool Model::Build(const OPCODECREATE& create)
140 {
141  // 1) Checkings
142  if(!create.mIMesh || !create.mIMesh->IsValid()) return false;
143 
144  // For this model, we only support complete trees
145  if(create.mSettings.mLimit!=1) return SetIceError("OPCODE WARNING: supports complete trees only! Use mLimit = 1.\n", null);
146 
147  // Look for degenerate faces.
148  udword NbDegenerate = create.mIMesh->CheckTopology();
149  if(NbDegenerate) Log("OPCODE WARNING: found %d degenerate faces in model! Collision might report wrong results!\n", NbDegenerate);
150  // We continue nonetheless....
151 
152  Release(); // Make sure previous tree has been discarded [Opcode 1.3, thanks Adam]
153 
154  // 1-1) Setup mesh interface automatically [Opcode 1.3]
155  SetMeshInterface(create.mIMesh);
156 
157  // Special case for 1-triangle meshes [Opcode 1.3]
158  udword NbTris = create.mIMesh->GetNbTriangles();
159  /*
160  if(NbTris==1)
161  {
162  // We don't need to actually create a tree here, since we'll only have a single triangle to deal with anyway.
163  // It's a waste to use a "model" for this but at least it will work.
164  mModelCode |= OPC_SINGLE_NODE;
165  return true;
166  }
167  */
168  // 2) Build a generic AABB Tree.
169  mSource = new AABBTree;
171 
172  // 2-1) Setup a builder. Our primitives here are triangles from input mesh,
173  // so we use an AABBTreeOfTrianglesBuilder.....
174  {
176  TB.mIMesh = create.mIMesh;
177  TB.mSettings = create.mSettings;
178  TB.mNbPrimitives = NbTris;
179  if(!mSource->Build(&TB)) return false;
180  }
181 
182  // 3) Create an optimized tree according to user-settings
183  if(!CreateTree(create.mNoLeaf, create.mQuantized)) return false;
184 
185  // 3-2) Create optimized tree
186  if(!mTree->Build(mSource)) return false;
187 
188  // 3-3) Delete generic tree if needed
189  if(!create.mKeepOriginal) DELETESINGLE(mSource);
190 
191 #ifdef __MESHMERIZER_H__
192  // 4) Convex hull
193  if(create.mCollisionHull)
194  {
195  // Create hull
196  mHull = new CollisionHull;
197  CHECKALLOC(mHull);
198 
199  CONVEXHULLCREATE CHC;
200  // ### doesn't work with strides
201  CHC.NbVerts = create.mIMesh->GetNbVertices();
202  CHC.Vertices = create.mIMesh->GetVerts();
203  CHC.UnifyNormals = true;
204  CHC.ReduceVertices = true;
205  CHC.WordFaces = false;
206  mHull->Compute(CHC);
207  }
208 #endif // __MESHMERIZER_H__
209 
210  return true;
211 }
212 
214 
220 {
221  if(!mTree) return 0;
222  return mTree->GetUsedBytes();
223 }
virtual udword GetUsedBytes() const =0
virtual udword GetUsedBytes() const =0
bool mKeepOriginal
true => keep a copy of the original tree (debug purpose)
Definition: Opcode.h:37
#define null
our own NULL pointer
Definition: IceTypes.h:57
AABBTree * mSource
Original source tree.
Definition: Opcode.h:169
inline_ const Point * GetVerts() const
Definition: Opcode.h:93
virtual bool Build(const OPCODECREATE &create)=0
inline_ void SetMeshInterface(const MeshInterface *imesh)
Definition: Opcode.h:164
#define SetIceError(a, b)
Definition: OPC_IceHook.h:29
bool Build(AABBTreeBuilder *builder)
Model creation structure.
Definition: Opcode.h:25
bool mQuantized
true => quantize the tree (else use a normal tree)
Definition: Opcode.h:33
inline_ udword GetNbVertices() const
Definition: Opcode.h:62
unsigned int udword
sizeof(udword) must be 4
Definition: IceTypes.h:65
#define DELETESINGLE(x)
Deletes an instance of a class.
#define CHECKALLOC(x)
virtual ~Model()
Definition: OPC_Model.cpp:114
udword mNbPrimitives
Total number of primitives.
Definition: Opcode.h:115
virtual bool Build(AABBTree *tree)=0
udword CheckTopology() const
bool mNoLeaf
true => discard leaf nodes (else use a normal tree)
Definition: Opcode.h:32
AABBOptimizedTree * mTree
Optimized tree owned by the model.
Definition: Opcode.h:170
udword mLimit
Limit number of primitives / node. If limit is 1, build a complete tree (2*N-1 nodes) ...
Definition: Opcode.h:44
inline_ udword GetNbTriangles() const
Definition: Opcode.h:61
void Release()
Definition: OPC_Model.cpp:124
MeshInterface * mIMesh
Mesh interface (access to triangles & vertices) (*)
Definition: Opcode.h:30
#define Log
Definition: OPC_IceHook.h:28
const MeshInterface * mIMesh
Shortcut to an app-controlled mesh interface.
Definition: Opcode.h:171
static BodyCustomizerHandle create(BodyHandle bodyHandle, const char *modelName)
bool CreateTree(bool no_leaf, bool quantized)
BuildSettings mSettings
Builder's settings.
Definition: Opcode.h:31
BuildSettings mSettings
Splitting rules & split limit [Opcode 1.3].
Definition: Opcode.h:114


openhrp3
Author(s): AIST, General Robotix Inc., Nakamura Lab of Dept. of Mechano Informatics at University of Tokyo
autogenerated on Sat Apr 13 2019 02:14:24