00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "LampBasic.h"
00026 #include "Collision/System/CollisionScene.h"
00027 #include "Core/Renamer/CountRenamer.h"
00028 #include "Collision/System/CollisionNode.h"
00029 #include "Collision/Leaf/StaticSphereCollision.h"
00030 #include "Collision/Leaf/StaticDeformedMeshCollision.h"
00031
00032 namespace Lamp{
00033
00034
00035
00036
00037
00038 CollisionScene::CollisionScene() : nodeDatabase_(256, 0.75f), nodeArray_(256),
00039 leafDatabase_(256, 0.75f), leafArray_(256), tick_(0){
00040
00041 nodeRenamer_ = new CountRenamer();
00042 nodeRenamerCallback_.scene_ = this;
00043 leafRenamer_ = new CountRenamer();
00044 leafRenamerCallback_.scene_ = this;
00045
00046 rootNode_ = createCollisionNode("RootNode");
00047 }
00048
00049
00050 CollisionScene::~CollisionScene(){
00051 Assert(nodeDatabase_.getCount() == 1);
00052 Assert(nodeArray_.getCount() == 1);
00053 Assert(leafDatabase_.getCount() == 0);
00054 Assert(leafArray_.getCount() == 0);
00055
00056 if(nodeArray_.removeByValue(rootNode_) == -1){
00057 ErrorOut("CollisionScene::~CollisionScene() "
00058 "Not found RootNode in array");
00059 }
00060 if(nodeDatabase_.remove(rootNode_->getName()) == NULL){
00061 ErrorOut("CollisionScene::~CollisionScene() "
00062 "Not found RootNode in hashmap");
00063 }
00064 SafeDelete(rootNode_);
00065
00066 SafeDelete(leafRenamer_);
00067 SafeDelete(nodeRenamer_);
00068 }
00069
00070
00071
00072
00073 void CollisionScene::traverse(){
00074
00075 tick_++;
00076 getRootNode()->traverse();
00077 }
00078
00079
00080
00081
00082 void CollisionScene::intersection(
00083 IntersectionResult* result, const Sphere& sphere, u_int collisionMask){
00084 int leafCount = getLeafCount();
00085 for(int i = 0; i < leafCount; i++){
00086 getLeaf(i)->intersection(result, sphere, collisionMask);
00087 }
00088 }
00089
00090
00091 void CollisionScene::intersection(IntersectionResult* result,
00092 StaticSphereCollision* sphere, u_int collisionMask){
00093 Assert(sphere->isGlobalEnabled());
00094 int leafCount = getLeafCount();
00095 for(int i = 0; i < leafCount; i++){
00096 CollisionLeaf* leaf = getLeaf(i);
00097 leaf->intersection(result, sphere, collisionMask);
00098 }
00099 }
00100
00101
00102
00103
00104 CollisionNode* CollisionScene::createCollisionNode(const String& name){
00105
00106 if(name.getSize() == 0){
00107 ErrorOut("CollisionScene::createCollisionNode() 名前が空文字列です");
00108 return NULL;
00109 }
00110
00111 if(existNodeName(name)){
00112 ErrorOut("CollisionScene::createCollisionNode() "
00113 "名前が重複しています %s", name.getBytes());
00114 return NULL;
00115 }
00116 CollisionNode* collisionNode = new CollisionNode(name, this);
00117 nodeDatabase_.put(name, collisionNode);
00118 nodeArray_.add(collisionNode);
00119 return collisionNode;
00120 }
00121
00122
00123
00124
00125 StaticSphereCollision* CollisionScene::createStaticSphereCollision(
00126 const String& name){
00127 if(!checkLeafName(name)){ return NULL; }
00128 StaticSphereCollision* sphere = new StaticSphereCollision(name, this);
00129 leafDatabase_.put(name, sphere);
00130 leafArray_.add(sphere);
00131 return sphere;
00132 }
00133
00134
00135 StaticDeformedMeshCollision* CollisionScene::createStaticDeformedMeshCollision(
00136 const String& name){
00137 if(!checkLeafName(name)){ return NULL; }
00138 StaticDeformedMeshCollision* mesh =
00139 new StaticDeformedMeshCollision(name, this);
00140 leafDatabase_.put(name, mesh);
00141 leafArray_.add(mesh);
00142 return mesh;
00143 }
00144
00145
00146
00147
00148 bool CollisionScene::checkLeafName(const String& name){
00149
00150 if(name.getSize() == 0){
00151 ErrorOut("CollisionScene::checkLeafName() 名前が空文字列です");
00152 return false;
00153 }
00154
00155 if(existLeafName(name)){
00156 ErrorOut("CollisionScene::checkLeafName() 名前が重複しています %s",
00157 name.getBytes());
00158 return false;
00159 }
00160 return true;
00161 }
00162
00163
00164
00165
00166 void CollisionScene::destroyNode(CollisionNode* node){
00167 Assert(node != rootNode_);
00168 if(nodeArray_.removeByValue(node) == -1){
00169 ErrorOut("CollisionScene::~destroyNode() Not found node in array");
00170 }
00171 if(nodeDatabase_.remove(node->getName()) == NULL){
00172 ErrorOut("CollisionScene::~destroyNode() Not found node in hashmap");
00173 }
00174 delete node;
00175 }
00176
00177
00178 void CollisionScene::destroyLeaf(CollisionLeaf* leaf){
00179 if(leafArray_.removeByValue(leaf) == -1){
00180 ErrorOut("CollisionScene::~destroyLeaf() Not found leaf in array");
00181 }
00182 if(leafDatabase_.remove(leaf->getName()) == NULL){
00183 ErrorOut("CollisionScene::~destroyLeaf() Not found leaf in hashmap");
00184 }
00185 delete leaf;
00186 }
00187
00188
00189 int CollisionScene::clear(){
00190
00191 int childCount = rootNode_->getChildCount();
00192 for(int i = childCount - 1; i >= 0; i--){
00193 rootNode_->removeChild(rootNode_->getChild(i));
00194 }
00195
00196 int result = getLeafCount();
00197 for(int i = getLeafCount() - 1; i >= 0; i--){ delete getLeaf(i); }
00198
00199 result += getNodeCount();
00200 for(int i = getNodeCount() - 1; i >= 1; i--){ delete getNode(i); }
00201 leafArray_.clear();
00202 leafDatabase_.clear();
00203 nodeArray_.clear();
00204 nodeDatabase_.clear();
00205 nodeArray_.add(rootNode_);
00206 nodeDatabase_.put(rootNode_->getName(), rootNode_);
00207 return (result - 1);
00208 }
00209
00210 }
00211