1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
|
/******************************************************************************
* SOFA, Simulation Open-Framework Architecture, version 1.0 beta 4 *
* (c) 2006-2009 MGH, INRIA, USTL, UJF, CNRS *
* *
* This library is free software; you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation; either version 2.1 of the License, or (at *
* your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
* for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this library; if not, write to the Free Software Foundation, *
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
*******************************************************************************
* SOFA :: Modules *
* *
* Authors: The SOFA Team and external contributors (see Authors.txt) *
* *
* Contact information: contact@sofa-framework.org *
******************************************************************************/
#include <sofa/component/collision/SphereTreeModel.h>
#include <sofa/helper/io/SphereLoader.h>
#include <sofa/component/collision/CubeModel.h>
#include <sofa/core/ObjectFactory.h>
#include <iostream>
#include <sofa/helper/system/gl.h>
#include <sofa/helper/system/glut.h>
#include <sofa/helper/system/FileRepository.h>
namespace sofa
{
namespace component
{
namespace collision
{
// Required for compilation purposes
SOFA_DECL_CLASS(SphereTreeModel)
using namespace sofa::defaulttype;
using namespace std;
int SphereTreeModelClass = core::RegisterObject("CollisionModel based on a hierarchy of bounding spheres")
.add< SphereTreeModel >()
.addAlias("SphereTree")
;
SphereTreeModel::SphereTreeModel(double radius)
: defaultRadius(initData(&defaultRadius, radius, "radius","TODO"))
{
}
void SphereTreeModel::resize(int size)
{ // Correcting sizes
this->component::MechanicalObject<Vec3Types>::resize(size);
this->core::CollisionModel::resize(size);
//this->elems.resize(size);
if ((int)radius.size() < size)
{
radius.reserve(size);
while ((int)radius.size() < size)
radius.push_back(defaultRadius.getValue());
}
else
{
radius.resize(size);
}
}
int SphereTreeModel::addSphere(const Vector3& pos, SReal radius)
{
int i = size;
resize(i+1);
setSphere(i, pos, radius);
return i;
}
void SphereTreeModel::setSphere(int i, const Vector3& pos, SReal r)
{
if ((unsigned)i >= (unsigned) size) return;
(*this->getX())[i] = pos;
radius[i] = r;
}
bool SphereTreeModel::load(const char* filename)
{
this->resize(0);
return loadSphereTree(filename);
}
bool SphereTreeModel::loadSphereTree( const char *fileName )
{
std::ifstream inFile;
std::string fileInRepository(fileName);
if ( !sofa::helper::system::DataRepository.findFile ( fileInRepository ) )
return false;
fileInRepository = sofa::helper::system::DataRepository.getFile ( fileInRepository);
inFile.open(fileInRepository.c_str());
if (inFile.fail())
{
serr << "Fail to open file sph" << sendl;
return false;
}
levels = 0; degree = 0;
inFile >> levels >> degree;
if (!levels || !degree)
return false;
int numnodes = 1;
int numSpheres=0;
double x, y, z, r, errDec;
for (int level = 0; level < levels; level++ )
{
// bool isLeaf = (level==levels-1);
for (int i = 0; i < numnodes; i++)
{
inFile >> x >> y >> z >> r >> errDec;
if (inFile.fail())
{
serr << "Error: incorrect file format sph " << sendl;
return false;
}
resize( numSpheres+1 ); //need to resize the "x" vector of DOF's of the sphereTree
// Collision element iterator
SingleSphere s(this, numSpheres);
s.setCenter(x,y,z);
radius[numSpheres] = r;
#if 0
SphereData sphData;
//sphData.isLeaf = isLeaf;
if (isLeaf)
{
sphData.leaf = core::CollisionElementIterator(this,numSpheres);
}
else
{
sphData.leaf = core::CollisionElementIterator();
}
sphData.node = s;
this->elems.push_back( sphData );
#endif
numSpheres=numSpheres+1;
} // end numnodes
numnodes *= degree;
} // end for
#if 0
//Create relationships
numnodes = 1;
int node = 0;
int nodeinit = 0;
int firstchild;
for (int level = 0; level < levels-1; level++ )
{
for (int i = 0; i < numnodes; i++)
{
while ( node < nodeinit ) // <=
{
firstchild = node * degree + 1;
for ( int deg = 0; deg <= degree; deg++)
{
//core::CollisionElementIterator *s = this->nodes[firstchild + deg];
SphereData s = this->elems[ firstchild + deg ];
core::CollisionElementIterator begin(this, (firstchild+deg) );
core::CollisionElementIterator end(this, (firstchild+degree) );
//Setting first child.
if ( deg == 0 ) this->elems[node].subcells.first = begin;
//Setting end for children
if ( deg == degree ) this->elems[node].subcells.second = end;
} // endfor degree.
node++;
} // endwhile
} // end for numnodes
numnodes *= degree;
nodeinit = node+numnodes;
} //end for levels
#endif
inFile.close();
return true;
}
void SphereTreeModel::applyScale(const double s)
{
Inherit::applyScale(s);
sout << "Applying scale " << s << " to " << size << " spheres" << sendl;
for (int i=0;i<size;i++)
radius[i] *= s;
}
void SphereTreeModel::draw(int index)
{
Vector3 p = (*getX())[index];
glPushMatrix();
glTranslated(p[0], p[1], p[2]);
glutWireSphere(radius[index], 16, 8);
glPopMatrix();
}
void SphereTreeModel::draw()
{
float color = 1.0f;
if ( getContext()->getShowCollisionModels())
{
glDisable(GL_LIGHTING);
if (!isSimulated())
glColor4f(1.0f, 1.0f, 1.0f, color);
else
glColor4f(1.0f, 1.0f, 0.0f, color);
//if (color < 1.0f)
//{
// glEnable(GL_BLEND);
// glDepthMask(0);
//}
int numnodes = 1;
int node = 0;
for (int level = 0; level < levels; level++ )
{
if ( level < levels-1 ) {
glColor4f(1.0f, color, color*color, (1.0 - color) );
color *= 0.5f;
}
else glColor4f(0.0f, 0.0f, 1.0f, 1.0);
for (int i = 0; i < numnodes; i++)
{
draw( node );
node++;
}
numnodes *= degree;
} //end for levels
//if (color < 1.0f)
//{
// glDisable(GL_BLEND);
// glDepthMask(1);
//}
}
if (isActive() && getPrevious()!=NULL && getContext()->getShowBoundingCollisionModels())
getPrevious()->draw();
}
void SphereTreeModel::computeBoundingTree(int /*maxDepth*/)
{
// To do:
// The tree is obtained from an off line proccess. T
// This function should be used to update the tree in case of deformable bodies.
}
void SphereTreeModel::computeContinuousBoundingTree(double /*dt*/, int /*maxDepth*/)
{ // To do
sout << "SphereTreeModel::computeContinuousBoundingTree - not implemented" << endl;
}
std::pair<core::CollisionElementIterator,core::CollisionElementIterator> SphereTreeModel::getInternalChildren(int index) const
{
//return elems[index].subcells;
int firstchild = index*degree+1;
if (firstchild >= size)
return std::make_pair(core::CollisionElementIterator(),core::CollisionElementIterator());
int lastchild = firstchild+degree-1;
if (lastchild >= size) lastchild = size-1; // note that this cannot happen if the last level of the tree is full
return std::make_pair(core::CollisionElementIterator(const_cast<SphereTreeModel*>(this),firstchild),core::CollisionElementIterator(const_cast<SphereTreeModel*>(this),lastchild+1));
}
bool SphereTreeModel::isLeaf( int index ) const
{
int firstchild = index*degree+1;
return (firstchild >= size);
//return !elems[index].subcells.first.valid();
//core::CollisionElementIterator i1 = elems[index].leaf;
//if ( i1.valid() ) return true;
//else return false;
}
} // namespace collision
} // namespace component
} // namespace sofa
|