Imported Upstream version 2.81
[platform/upstream/libbullet.git] / Extras / CDTestFramework / Opcode / OPC_BaseModel.cpp
1 /*
2  *      OPCODE - Optimized Collision Detection
3  * http://www.codercorner.com/Opcode.htm
4  * 
5  * Copyright (c) 2001-2008 Pierre Terdiman,  pierre@codercorner.com
6
7 This software is provided 'as-is', without any express or implied warranty.
8 In no event will the authors be held liable for any damages arising from the use of this software.
9 Permission is granted to anyone to use this software for any purpose, 
10 including commercial applications, and to alter it and redistribute it freely, 
11 subject to the following restrictions:
12
13 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
14 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
15 3. This notice may not be removed or altered from any source distribution.
16 */
17
18 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
19 /**
20  *      Contains base model interface.
21  *      \file           OPC_BaseModel.cpp
22  *      \author         Pierre Terdiman
23  *      \date           May, 18, 2003
24  */
25 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
26
27 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
28 /**
29  *      The base class for collision models.
30  *
31  *      \class          BaseModel
32  *      \author         Pierre Terdiman
33  *      \version        1.3
34  *      \date           May, 18, 2003
35 */
36 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
37
38 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
39 // Precompiled Header
40 #include "Stdafx.h"
41
42 using namespace Opcode;
43
44 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
45 /**
46  *      Constructor.
47  */
48 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
49 OPCODECREATE::OPCODECREATE()
50 {
51         mIMesh                          = null;
52         mSettings.mRules        = SPLIT_SPLATTER_POINTS | SPLIT_GEOM_CENTER;
53         mSettings.mLimit        = 1;    // Mandatory for complete trees
54         mNoLeaf                         = true;
55         mQuantized                      = true;
56 #ifdef __MESHMERIZER_H__
57         mCollisionHull          = false;
58 #endif // __MESHMERIZER_H__
59         mKeepOriginal           = false;
60         mCanRemap                       = false;
61 }
62
63 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
64 /**
65  *      Constructor.
66  */
67 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
68 BaseModel::BaseModel() : mIMesh(null), mModelCode(0), mSource(null), mTree(null)
69 {
70 }
71
72 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
73 /**
74  *      Destructor.
75  */
76 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
77 BaseModel::~BaseModel()
78 {
79         ReleaseBase();
80 }
81
82 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
83 /**
84  *      Releases everything.
85  */
86 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
87 void BaseModel::ReleaseBase()
88 {
89         DELETESINGLE(mSource);
90         DELETESINGLE(mTree);
91 }
92
93 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
94 /**
95  *      Creates an optimized tree according to user-settings, and setups mModelCode.
96  *      \param          no_leaf         [in] true for "no leaf" tree
97  *      \param          quantized       [in] true for quantized tree
98  *      \return         true if success
99  */
100 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
101 bool BaseModel::CreateTree(bool no_leaf, bool quantized)
102 {
103         DELETESINGLE(mTree);
104
105         // Setup model code
106         if(no_leaf)             mModelCode |= OPC_NO_LEAF;
107         else                    mModelCode &= ~OPC_NO_LEAF;
108
109         if(quantized)   mModelCode |= OPC_QUANTIZED;
110         else                    mModelCode &= ~OPC_QUANTIZED;
111
112         // Create the correct class
113         if(mModelCode & OPC_NO_LEAF)
114         {
115                 if(mModelCode & OPC_QUANTIZED)  mTree = new AABBQuantizedNoLeafTree;
116                 else                                                    mTree = new AABBNoLeafTree;
117         }
118         else
119         {
120                 if(mModelCode & OPC_QUANTIZED)  mTree = new AABBQuantizedTree;
121                 else                                                    mTree = new AABBCollisionTree;
122         }
123         CHECKALLOC(mTree);
124
125         return true;
126 }
127
128 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
129 /**
130  *      Refits the collision model. This can be used to handle dynamic meshes. Usage is:
131  *      1. modify your mesh vertices (keep the topology constant!)
132  *      2. refit the tree (call this method)
133  *      \return         true if success
134  */
135 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
136 bool BaseModel::Refit()
137 {
138         // Refit the optimized tree
139         return mTree->Refit(mIMesh);
140
141 // Old code kept for reference : refit the source tree then rebuild !
142 //      if(!mSource)    return false;
143 //      // Ouch...
144 //      mSource->Refit(&mTB);
145 //      // Ouch...
146 //      return mTree->Build(mSource);
147 }