fixed a few issues, removed dependency from BulletDynamics to Extras/ConvexHull

This commit is contained in:
erwin.coumans
2008-03-31 01:09:41 +00:00
parent 80714cab13
commit a151ec1c73
3 changed files with 1402 additions and 1395 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -33,6 +33,10 @@ ADD_LIBRARY(LibBulletCollision
CollisionDispatch/btDefaultCollisionConfiguration.h CollisionDispatch/btDefaultCollisionConfiguration.h
CollisionDispatch/btSphereSphereCollisionAlgorithm.cpp CollisionDispatch/btSphereSphereCollisionAlgorithm.cpp
CollisionDispatch/btSphereSphereCollisionAlgorithm.h CollisionDispatch/btSphereSphereCollisionAlgorithm.h
CollisionDispatch/btBoxBoxCollisionAlgorithm.cpp
CollisionDispatch/btBoxBoxCollisionAlgorithm.h
CollisionDispatch/btBoxBoxDetector.cpp
CollisionDispatch/btBoxBoxDetector.h
CollisionDispatch/btSphereBoxCollisionAlgorithm.cpp CollisionDispatch/btSphereBoxCollisionAlgorithm.cpp
CollisionDispatch/btSphereBoxCollisionAlgorithm.h CollisionDispatch/btSphereBoxCollisionAlgorithm.h
CollisionDispatch/btConvexPlaneCollisionAlgorithm.cpp CollisionDispatch/btConvexPlaneCollisionAlgorithm.cpp

View File

@@ -1,429 +1,399 @@
/* /*
Bullet Continuous Collision Detection and Physics Library Bullet Continuous Collision Detection and Physics Library
Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/ Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/
This software is provided 'as-is', without any express or implied warranty. This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising from the use of this software. In no event will the authors be held liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose, Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it freely, including commercial applications, and to alter it and redistribute it freely,
subject to the following restrictions: subject to the following restrictions:
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. 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.
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution. 3. This notice may not be removed or altered from any source distribution.
*/ */
///btSoftBodyHelpers.cpp by Nathanael Presson ///btSoftBodyHelpers.cpp by Nathanael Presson
#include "btSoftBody.h" #include "btSoftBody.h"
#include "Extras/ConvexHull/btConvexHull.h" #include <stdio.h>
#include <stdio.h> #include <string.h>
#include <string.h>
namespace btsoftbody_internals
namespace btsoftbody_internals {
{
//
// static void drawVertex( btIDebugDraw* idraw,
static void drawVertex( btIDebugDraw* idraw, const btVector3& x,btScalar s,const btVector3& c)
const btVector3& x,btScalar s,const btVector3& c) {
{ idraw->drawLine(x-btVector3(s,0,0),x+btVector3(s,0,0),c);
idraw->drawLine(x-btVector3(s,0,0),x+btVector3(s,0,0),c); idraw->drawLine(x-btVector3(0,s,0),x+btVector3(0,s,0),c);
idraw->drawLine(x-btVector3(0,s,0),x+btVector3(0,s,0),c); idraw->drawLine(x-btVector3(0,0,s),x+btVector3(0,0,s),c);
idraw->drawLine(x-btVector3(0,0,s),x+btVector3(0,0,s),c); }
}
//
// static btVector3 stresscolor(btScalar stress)
static btVector3 stresscolor(btScalar stress) {
{ static const btVector3 spectrum[]= {
static const btVector3 spectrum[]= { btVector3(1,0,1),
btVector3(1,0,1), btVector3(0,0,1),
btVector3(0,0,1), btVector3(0,1,1),
btVector3(0,1,1), btVector3(0,1,0),
btVector3(0,1,0), btVector3(1,1,0),
btVector3(1,1,0), btVector3(1,0,0),
btVector3(1,0,0), btVector3(1,0,0),
btVector3(1,0,0), };
}; static const int ncolors=sizeof(spectrum)/sizeof(spectrum[0])-1;
static const int ncolors=sizeof(spectrum)/sizeof(spectrum[0])-1; static const btScalar one=1;
static const btScalar one=1; stress=btMax<btScalar>(0,btMin<btScalar>(1,stress))*ncolors;
stress=btMax<btScalar>(0,btMin<btScalar>(1,stress))*ncolors; const int sel=(int)stress;
const int sel=(int)stress; const btScalar frc=stress-sel;
const btScalar frc=stress-sel; return(spectrum[sel]+(spectrum[sel+1]-spectrum[sel])*frc);
return(spectrum[sel]+(spectrum[sel+1]-spectrum[sel])*frc); }
}
}
}
using namespace btsoftbody_internals;
using namespace btsoftbody_internals;
//
// void Draw( btSoftBody* psb,
void Draw( btSoftBody* psb, btIDebugDraw* idraw,
btIDebugDraw* idraw, int drawflags)
int drawflags) {
{ const btScalar scl=(btScalar)0.1;
const btScalar scl=(btScalar)0.1; const btScalar nscl=scl*5;
const btScalar nscl=scl*5; const btScalar alpha=(btScalar)0.5;
const btScalar alpha=(btScalar)0.5; const btVector3 scolor=btVector3(0,0,0);
const btVector3 scolor=btVector3(0,0,0); const btVector3 bcolor=btVector3(1,1,0);
const btVector3 bcolor=btVector3(1,1,0); const btVector3 ncolor=btVector3(1,1,1);
const btVector3 ncolor=btVector3(1,1,1); const btVector3 ccolor=btVector3(1,0,0);
const btVector3 ccolor=btVector3(1,0,0); /* Nodes */
/* Nodes */ if(0!=(drawflags&fDrawFlags::Nodes))
if(0!=(drawflags&fDrawFlags::Nodes)) {
{ for(int i=0;i<psb->m_nodes.size();++i)
for(int i=0;i<psb->m_nodes.size();++i) {
{ const btSoftBody::Node& n=psb->m_nodes[i];
const btSoftBody::Node& n=psb->m_nodes[i]; idraw->drawLine(n.m_x-btVector3(scl,0,0),n.m_x+btVector3(scl,0,0),btVector3(1,0,0));
idraw->drawLine(n.m_x-btVector3(scl,0,0),n.m_x+btVector3(scl,0,0),btVector3(1,0,0)); idraw->drawLine(n.m_x-btVector3(0,scl,0),n.m_x+btVector3(0,scl,0),btVector3(0,1,0));
idraw->drawLine(n.m_x-btVector3(0,scl,0),n.m_x+btVector3(0,scl,0),btVector3(0,1,0)); idraw->drawLine(n.m_x-btVector3(0,0,scl),n.m_x+btVector3(0,0,scl),btVector3(0,0,1));
idraw->drawLine(n.m_x-btVector3(0,0,scl),n.m_x+btVector3(0,0,scl),btVector3(0,0,1)); }
} }
} /* Links */
/* Links */ if(0!=(drawflags&fDrawFlags::Links))
if(0!=(drawflags&fDrawFlags::Links)) {
{ for(int i=0;i<psb->m_links.size();++i)
for(int i=0;i<psb->m_links.size();++i) {
{ const btSoftBody::Link& l=psb->m_links[i];
const btSoftBody::Link& l=psb->m_links[i]; switch(l.m_type)
switch(l.m_type) {
{ case btSoftBody::eLType::Structural:
case btSoftBody::eLType::Structural: if(0!=(drawflags&fDrawFlags::SLinks)) idraw->drawLine(l.m_n[0]->m_x,l.m_n[1]->m_x,scolor);break;
if(0!=(drawflags&fDrawFlags::SLinks)) idraw->drawLine(l.m_n[0]->m_x,l.m_n[1]->m_x,scolor);break; case btSoftBody::eLType::Bending:
case btSoftBody::eLType::Bending: if(0!=(drawflags&fDrawFlags::BLinks)) idraw->drawLine(l.m_n[0]->m_x,l.m_n[1]->m_x,bcolor);break;
if(0!=(drawflags&fDrawFlags::BLinks)) idraw->drawLine(l.m_n[0]->m_x,l.m_n[1]->m_x,bcolor);break; }
} }
} }
} /* Normals */
/* Normals */ if(0!=(drawflags&fDrawFlags::Normals))
if(0!=(drawflags&fDrawFlags::Normals)) {
{ for(int i=0;i<psb->m_nodes.size();++i)
for(int i=0;i<psb->m_nodes.size();++i) {
{ const btSoftBody::Node& n=psb->m_nodes[i];
const btSoftBody::Node& n=psb->m_nodes[i]; const btVector3 d=n.m_n*nscl;
const btVector3 d=n.m_n*nscl; idraw->drawLine(n.m_x,n.m_x+d,ncolor);
idraw->drawLine(n.m_x,n.m_x+d,ncolor); idraw->drawLine(n.m_x,n.m_x-d,ncolor*0.5);
idraw->drawLine(n.m_x,n.m_x-d,ncolor*0.5); }
} }
} /* Contacts */
/* Contacts */ if(0!=(drawflags&fDrawFlags::Contacts))
if(0!=(drawflags&fDrawFlags::Contacts)) {
{ static const btVector3 axis[]={btVector3(1,0,0),
static const btVector3 axis[]={btVector3(1,0,0), btVector3(0,1,0),
btVector3(0,1,0), btVector3(0,0,1)};
btVector3(0,0,1)}; for(int i=0;i<psb->m_contacts.size();++i)
for(int i=0;i<psb->m_contacts.size();++i) {
{ const btSoftBody::Contact& c=psb->m_contacts[i];
const btSoftBody::Contact& c=psb->m_contacts[i]; const btVector3 o= c.m_node->m_x-c.m_cti.m_normal*
const btVector3 o= c.m_node->m_x-c.m_cti.m_normal* (dot(c.m_node->m_x,c.m_cti.m_normal)+c.m_cti.m_offset);
(dot(c.m_node->m_x,c.m_cti.m_normal)+c.m_cti.m_offset); const btVector3 x=cross(c.m_cti.m_normal,axis[c.m_cti.m_normal.minAxis()]).normalized();
const btVector3 x=cross(c.m_cti.m_normal,axis[c.m_cti.m_normal.minAxis()]).normalized(); const btVector3 y=cross(x,c.m_cti.m_normal).normalized();
const btVector3 y=cross(x,c.m_cti.m_normal).normalized(); idraw->drawLine(o-x*nscl,o+x*nscl,ccolor);
idraw->drawLine(o-x*nscl,o+x*nscl,ccolor); idraw->drawLine(o-y*nscl,o+y*nscl,ccolor);
idraw->drawLine(o-y*nscl,o+y*nscl,ccolor); idraw->drawLine(o,o+c.m_cti.m_normal*nscl*3,btVector3(1,1,0));
idraw->drawLine(o,o+c.m_cti.m_normal*nscl*3,btVector3(1,1,0)); }
} }
} /* Anchors */
/* Anchors */ if(0!=(drawflags&fDrawFlags::Anchors))
if(0!=(drawflags&fDrawFlags::Anchors)) {
{ for(int i=0;i<psb->m_anchors.size();++i)
for(int i=0;i<psb->m_anchors.size();++i) {
{ const btSoftBody::Anchor& a=psb->m_anchors[i];
const btSoftBody::Anchor& a=psb->m_anchors[i]; const btVector3 q=a.m_body->getWorldTransform()*a.m_local;
const btVector3 q=a.m_body->getWorldTransform()*a.m_local; drawVertex(idraw,a.m_node->m_x,0.25,btVector3(1,0,0));
drawVertex(idraw,a.m_node->m_x,0.25,btVector3(1,0,0)); drawVertex(idraw,q,0.25,btVector3(0,1,0));
drawVertex(idraw,q,0.25,btVector3(0,1,0)); idraw->drawLine(a.m_node->m_x,q,btVector3(1,1,1));
idraw->drawLine(a.m_node->m_x,q,btVector3(1,1,1)); }
} for(int i=0;i<psb->m_nodes.size();++i)
for(int i=0;i<psb->m_nodes.size();++i) {
{ const btSoftBody::Node& n=psb->m_nodes[i];
const btSoftBody::Node& n=psb->m_nodes[i]; if(n.m_im<=0)
if(n.m_im<=0) {
{ drawVertex(idraw,n.m_x,0.25,btVector3(1,0,0));
drawVertex(idraw,n.m_x,0.25,btVector3(1,0,0)); }
} }
} }
} /* Faces */
/* Faces */ if(0!=(drawflags&fDrawFlags::Faces))
if(0!=(drawflags&fDrawFlags::Faces)) {
{ const btScalar scl=(btScalar)0.7;
const btScalar scl=(btScalar)0.7; const btScalar alp=(btScalar)1;
const btScalar alp=(btScalar)1; const btVector3 col(0,(btScalar)0.7,0);
const btVector3 col(0,(btScalar)0.7,0); for(int i=0;i<psb->m_faces.size();++i)
for(int i=0;i<psb->m_faces.size();++i) {
{ const btSoftBody::Face& f=psb->m_faces[i];
const btSoftBody::Face& f=psb->m_faces[i]; const btVector3 x[]={f.m_n[0]->m_x,f.m_n[1]->m_x,f.m_n[2]->m_x};
const btVector3 x[]={f.m_n[0]->m_x,f.m_n[1]->m_x,f.m_n[2]->m_x}; const btVector3 c=(x[0]+x[1]+x[2])/3;
const btVector3 c=(x[0]+x[1]+x[2])/3; idraw->drawTriangle((x[0]-c)*scl+c,
idraw->drawTriangle((x[0]-c)*scl+c, (x[1]-c)*scl+c,
(x[1]-c)*scl+c, (x[2]-c)*scl+c,
(x[2]-c)*scl+c, f.m_n[0]->m_n,f.m_n[1]->m_n,f.m_n[2]->m_n,
f.m_n[0]->m_n,f.m_n[1]->m_n,f.m_n[2]->m_n, col,alp);
col,alp); }
} }
} }
}
//
// void DrawInfos( btSoftBody* psb,
void DrawInfos( btSoftBody* psb, btIDebugDraw* idraw,
btIDebugDraw* idraw, bool masses,
bool masses, bool areas,
bool areas, bool stress)
bool stress) {
{ for(int i=0;i<psb->m_nodes.size();++i)
for(int i=0;i<psb->m_nodes.size();++i) {
{ const btSoftBody::Node& n=psb->m_nodes[i];
const btSoftBody::Node& n=psb->m_nodes[i]; char text[2048]={0};
char text[2048]={0}; char buff[1024];
char buff[1024]; if(masses)
if(masses) {
{ sprintf(buff," M(%.2f)",1/n.m_im);
sprintf(buff," M(%.2f)",1/n.m_im); strcat(text,buff);
strcat(text,buff); }
} if(areas)
if(areas) {
{ sprintf(buff," A(%.2f)",n.m_area);
sprintf(buff," A(%.2f)",n.m_area); strcat(text,buff);
strcat(text,buff); }
} if(text[0]) idraw->draw3dText(n.m_x,text);
if(text[0]) idraw->draw3dText(n.m_x,text); }
} }
}
//
// void DrawFrame( btSoftBody* psb,
void DrawFrame( btSoftBody* psb, btIDebugDraw* idraw)
btIDebugDraw* idraw) {
{ if(psb->m_pose.m_bframe)
if(psb->m_pose.m_bframe) {
{ static const btScalar ascl=10;
static const btScalar ascl=10; static const btScalar nscl=(btScalar)0.1;
static const btScalar nscl=(btScalar)0.1; const btVector3 com=psb->m_pose.m_com;
const btVector3 com=psb->m_pose.m_com; const btMatrix3x3& trs=psb->m_pose.m_trs;
const btMatrix3x3& trs=psb->m_pose.m_trs; const btVector3 Xaxis=(trs*btVector3(1,0,0)).normalized();
const btVector3 Xaxis=(trs*btVector3(1,0,0)).normalized(); const btVector3 Yaxis=(trs*btVector3(0,1,0)).normalized();
const btVector3 Yaxis=(trs*btVector3(0,1,0)).normalized(); const btVector3 Zaxis=(trs*btVector3(0,0,1)).normalized();
const btVector3 Zaxis=(trs*btVector3(0,0,1)).normalized(); idraw->drawLine(com,com+Xaxis*ascl,btVector3(1,0,0));
idraw->drawLine(com,com+Xaxis*ascl,btVector3(1,0,0)); idraw->drawLine(com,com+Yaxis*ascl,btVector3(0,1,0));
idraw->drawLine(com,com+Yaxis*ascl,btVector3(0,1,0)); idraw->drawLine(com,com+Zaxis*ascl,btVector3(0,0,1));
idraw->drawLine(com,com+Zaxis*ascl,btVector3(0,0,1)); for(int i=0;i<psb->m_pose.m_pos.size();++i)
for(int i=0;i<psb->m_pose.m_pos.size();++i) {
{ const btVector3 x=com+trs*psb->m_pose.m_pos[i];
const btVector3 x=com+trs*psb->m_pose.m_pos[i]; idraw->drawLine(x-btVector3(1,0,0)*nscl,x+btVector3(1,0,0)*nscl,btVector3(1,0,1));
idraw->drawLine(x-btVector3(1,0,0)*nscl,x+btVector3(1,0,0)*nscl,btVector3(1,0,1)); idraw->drawLine(x-btVector3(0,1,0)*nscl,x+btVector3(0,1,0)*nscl,btVector3(1,0,1));
idraw->drawLine(x-btVector3(0,1,0)*nscl,x+btVector3(0,1,0)*nscl,btVector3(1,0,1)); idraw->drawLine(x-btVector3(0,0,1)*nscl,x+btVector3(0,0,1)*nscl,btVector3(1,0,1));
idraw->drawLine(x-btVector3(0,0,1)*nscl,x+btVector3(0,0,1)*nscl,btVector3(1,0,1)); }
} }
} }
}
//
// btSoftBody* CreateRope( btSoftBody::ISoftBody* isoftbody,
btSoftBody* CreateRope( btSoftBody::ISoftBody* isoftbody, const btVector3& from,
const btVector3& from, const btVector3& to,
const btVector3& to, int res,
int res, int fixeds)
int fixeds) {
{ /* Create nodes */
/* Create nodes */ const int r=res+2;
const int r=res+2; btVector3* x=new btVector3[r];
btVector3* x=new btVector3[r]; btScalar* m=new btScalar[r];
btScalar* m=new btScalar[r]; for(int i=0;i<r;++i)
for(int i=0;i<r;++i) {
{ const btScalar t=i/(btScalar)(r-1);
const btScalar t=i/(btScalar)(r-1); x[i]=lerp(from,to,t);
x[i]=lerp(from,to,t); m[i]=1;
m[i]=1; }
} btSoftBody* psb=btSoftBody::Create(isoftbody,r,x,m);
btSoftBody* psb=btSoftBody::Create(isoftbody,r,x,m); if(fixeds&1) psb->SetMass(0,0);
if(fixeds&1) psb->SetMass(0,0); if(fixeds&2) psb->SetMass(r-1,0);
if(fixeds&2) psb->SetMass(r-1,0); delete[] x;
delete[] x; delete[] m;
delete[] m; /* Create links */
/* Create links */ for(int i=1;i<r;++i)
for(int i=1;i<r;++i) {
{ psb->AppendLink(i-1,i,1,btSoftBody::eLType::Structural);
psb->AppendLink(i-1,i,1,btSoftBody::eLType::Structural); }
} /* Finished */
/* Finished */ return(psb);
return(psb); }
}
//
// btSoftBody* CreatePatch( btSoftBody::ISoftBody* isoftbody,
btSoftBody* CreatePatch( btSoftBody::ISoftBody* isoftbody, const btVector3& corner00,
const btVector3& corner00, const btVector3& corner10,
const btVector3& corner10, const btVector3& corner01,
const btVector3& corner01, const btVector3& corner11,
const btVector3& corner11, int resx,
int resx, int resy,
int resy, int fixeds,
int fixeds, bool gendiags)
bool gendiags) {
{ #define IDX(_x_,_y_) ((_y_)*rx+(_x_))
#define IDX(_x_,_y_) ((_y_)*rx+(_x_)) /* Create nodes */
/* Create nodes */ if((resx<2)||(resy<2)) return(0);
if((resx<2)||(resy<2)) return(0); const int rx=resx;
const int rx=resx; const int ry=resy;
const int ry=resy; const int tot=rx*ry;
const int tot=rx*ry; btVector3* x=new btVector3[tot];
btVector3* x=new btVector3[tot]; btScalar* m=new btScalar[tot];
btScalar* m=new btScalar[tot]; for(int iy=0;iy<ry;++iy)
for(int iy=0;iy<ry;++iy) {
{ const btScalar ty=iy/(btScalar)(ry-1);
const btScalar ty=iy/(btScalar)(ry-1); const btVector3 py0=lerp(corner00,corner01,ty);
const btVector3 py0=lerp(corner00,corner01,ty); const btVector3 py1=lerp(corner10,corner11,ty);
const btVector3 py1=lerp(corner10,corner11,ty); for(int ix=0;ix<rx;++ix)
for(int ix=0;ix<rx;++ix) {
{ const btScalar tx=ix/(btScalar)(rx-1);
const btScalar tx=ix/(btScalar)(rx-1); x[IDX(ix,iy)]=lerp(py0,py1,tx);
x[IDX(ix,iy)]=lerp(py0,py1,tx); m[IDX(ix,iy)]=1;
m[IDX(ix,iy)]=1; }
} }
} btSoftBody* psb=btSoftBody::Create(isoftbody,tot,x,m);
btSoftBody* psb=btSoftBody::Create(isoftbody,tot,x,m); if(fixeds&1) psb->SetMass(IDX(0,0),0);
if(fixeds&1) psb->SetMass(IDX(0,0),0); if(fixeds&2) psb->SetMass(IDX(rx-1,0),0);
if(fixeds&2) psb->SetMass(IDX(rx-1,0),0); if(fixeds&4) psb->SetMass(IDX(0,ry-1),0);
if(fixeds&4) psb->SetMass(IDX(0,ry-1),0); if(fixeds&8) psb->SetMass(IDX(rx-1,ry-1),0);
if(fixeds&8) psb->SetMass(IDX(rx-1,ry-1),0); delete[] x;
delete[] x; delete[] m;
delete[] m; /* Create links and faces */
/* Create links and faces */ for(int iy=0;iy<ry;++iy)
for(int iy=0;iy<ry;++iy) {
{ for(int ix=0;ix<rx;++ix)
for(int ix=0;ix<rx;++ix) {
{ const int idx=IDX(ix,iy);
const int idx=IDX(ix,iy); const bool mdx=(ix+1)<rx;
const bool mdx=(ix+1)<rx; const bool mdy=(iy+1)<ry;
const bool mdy=(iy+1)<ry; if(mdx) psb->AppendLink(idx,IDX(ix+1,iy),
if(mdx) psb->AppendLink(idx,IDX(ix+1,iy), 1,btSoftBody::eLType::Structural);
1,btSoftBody::eLType::Structural); if(mdy) psb->AppendLink(idx,IDX(ix,iy+1),
if(mdy) psb->AppendLink(idx,IDX(ix,iy+1), 1,btSoftBody::eLType::Structural);
1,btSoftBody::eLType::Structural); if(mdx&&mdy)
if(mdx&&mdy) {
{ if((ix+iy)&1)
if((ix+iy)&1) {
{ psb->AppendFace(IDX(ix,iy),IDX(ix+1,iy),IDX(ix+1,iy+1));
psb->AppendFace(IDX(ix,iy),IDX(ix+1,iy),IDX(ix+1,iy+1)); psb->AppendFace(IDX(ix,iy),IDX(ix+1,iy+1),IDX(ix,iy+1));
psb->AppendFace(IDX(ix,iy),IDX(ix+1,iy+1),IDX(ix,iy+1)); if(gendiags)
if(gendiags) {
{ psb->AppendLink(IDX(ix,iy),IDX(ix+1,iy+1),
psb->AppendLink(IDX(ix,iy),IDX(ix+1,iy+1), 1,btSoftBody::eLType::Structural);
1,btSoftBody::eLType::Structural); }
} }
} else
else {
{ psb->AppendFace(IDX(ix,iy+1),IDX(ix,iy),IDX(ix+1,iy));
psb->AppendFace(IDX(ix,iy+1),IDX(ix,iy),IDX(ix+1,iy)); psb->AppendFace(IDX(ix,iy+1),IDX(ix+1,iy),IDX(ix+1,iy+1));
psb->AppendFace(IDX(ix,iy+1),IDX(ix+1,iy),IDX(ix+1,iy+1)); if(gendiags)
if(gendiags) {
{ psb->AppendLink(IDX(ix+1,iy),IDX(ix,iy+1),
psb->AppendLink(IDX(ix+1,iy),IDX(ix,iy+1), 1,btSoftBody::eLType::Structural);
1,btSoftBody::eLType::Structural); }
} }
} }
} }
} }
} /* Finished */
/* Finished */ #undef IDX
#undef IDX return(psb);
return(psb); }
}
//
// btSoftBody* CreateEllipsoid(btSoftBody::ISoftBody* isoftbody,
btSoftBody* CreateEllipsoid(btSoftBody::ISoftBody* isoftbody, const btVector3& center,
const btVector3& center, const btVector3& radius,
const btVector3& radius, int res)
int res) {
{ struct Hammersley
struct Hammersley {
{ static void Generate(btVector3* x,int n)
static void Generate(btVector3* x,int n) {
{ for(int i=0;i<n;i++)
for(int i=0;i<n;i++) {
{ btScalar p=0.5,t=0;
btScalar p=0.5,t=0; for(int j=i;j;p*=0.5,j>>=1) if(j&1) t+=p;
for(int j=i;j;p*=0.5,j>>=1) if(j&1) t+=p; btScalar w=2*t-1;
btScalar w=2*t-1; btScalar a=(SIMD_PI+2*i*SIMD_PI)/n;
btScalar a=(SIMD_PI+2*i*SIMD_PI)/n; btScalar s=btSqrt(1-w*w);
btScalar s=btSqrt(1-w*w); *x++=btVector3(s*btCos(a),s*btSin(a),w);
*x++=btVector3(s*btCos(a),s*btSin(a),w); }
} }
} };
}; btAlignedObjectArray<btVector3> vtx;
btAlignedObjectArray<btVector3> vtx; vtx.resize(3+res);
vtx.resize(3+res); Hammersley::Generate(&vtx[0],vtx.size());
Hammersley::Generate(&vtx[0],vtx.size()); for(int i=0;i<vtx.size();++i)
for(int i=0;i<vtx.size();++i) {
{ vtx[i]=vtx[i]*radius+center;
vtx[i]=vtx[i]*radius+center; }
} return(CreateFromConvexHull(isoftbody,&vtx[0],vtx.size()));
return(CreateFromConvexHull(isoftbody,&vtx[0],vtx.size())); }
}
//
btSoftBody* CreateFromConvexHull( btSoftBody::ISoftBody* isoftbody, //
const btVector3* vertices, btSoftBody* CreateFromTriMesh( btSoftBody::ISoftBody* isoftbody,
int nvertices) const btScalar* vertices,
{ const int* triangles,
HullDesc hdsc(QF_TRIANGLES,nvertices,vertices); int ntriangles)
HullResult hres; {
HullLibrary hlib;/*??*/ int maxidx=0;
hdsc.mMaxVertices=nvertices; for(int i=0,ni=ntriangles*3;i<ni;++i)
hlib.CreateConvexHull(hdsc,hres); {
btSoftBody* psb=btSoftBody::Create( isoftbody, maxidx=btMax(triangles[i],maxidx);
(int)hres.mNumOutputVertices, }
hres.mOutputVertices,0); ++maxidx;
for(int i=0;i<(int)hres.mNumFaces;++i) btAlignedObjectArray<bool> chks;
{ btAlignedObjectArray<btVector3> vtx;
const int idx[]={ hres.mIndices[i*3+0], chks.resize(maxidx*maxidx,false);
hres.mIndices[i*3+1], vtx.resize(maxidx);
hres.mIndices[i*3+2]}; for(int i=0,j=0,ni=maxidx*3;i<ni;++j,i+=3)
if(idx[0]<idx[1]) psb->AppendLink( idx[0],idx[1], {
1,btSoftBody::eLType::Structural); vtx[j]=btVector3(vertices[i],vertices[i+1],vertices[i+2]);
if(idx[1]<idx[2]) psb->AppendLink( idx[1],idx[2], }
1,btSoftBody::eLType::Structural); btSoftBody* psb=btSoftBody::Create(isoftbody,vtx.size(),&vtx[0],0);
if(idx[2]<idx[0]) psb->AppendLink( idx[2],idx[0], for(int i=0,ni=ntriangles*3;i<ni;i+=3)
1,btSoftBody::eLType::Structural); {
psb->AppendFace(idx[0],idx[1],idx[2]); const int idx[]={triangles[i],triangles[i+1],triangles[i+2]};
} #define IDX(_x_,_y_) ((_y_)*maxidx+(_x_))
hlib.ReleaseResult(hres); for(int j=2,k=0;k<3;j=k++)
psb->RandomizeConstraints(); {
return(psb); if(!chks[IDX(idx[j],idx[k])])
} {
chks[IDX(idx[j],idx[k])]=true;
// chks[IDX(idx[k],idx[k])]=true;
btSoftBody* CreateFromTriMesh( btSoftBody::ISoftBody* isoftbody, psb->AppendLink(idx[j],idx[k],1,btSoftBody::eLType::Structural);
const btScalar* vertices, }
const int* triangles, }
int ntriangles) #undef IDX
{ psb->AppendFace(idx[0],idx[1],idx[2]);
int maxidx=0; }
for(int i=0,ni=ntriangles*3;i<ni;++i) psb->RandomizeConstraints();
{ return(psb);
maxidx=btMax(triangles[i],maxidx); }
}
++maxidx;
btAlignedObjectArray<bool> chks;
btAlignedObjectArray<btVector3> vtx;
chks.resize(maxidx*maxidx,false);
vtx.resize(maxidx);
for(int i=0,j=0,ni=maxidx*3;i<ni;++j,i+=3)
{
vtx[j]=btVector3(vertices[i],vertices[i+1],vertices[i+2]);
}
btSoftBody* psb=btSoftBody::Create(isoftbody,vtx.size(),&vtx[0],0);
for(int i=0,ni=ntriangles*3;i<ni;i+=3)
{
const int idx[]={triangles[i],triangles[i+1],triangles[i+2]};
#define IDX(_x_,_y_) ((_y_)*maxidx+(_x_))
for(int j=2,k=0;k<3;j=k++)
{
if(!chks[IDX(idx[j],idx[k])])
{
chks[IDX(idx[j],idx[k])]=true;
chks[IDX(idx[k],idx[k])]=true;
psb->AppendLink(idx[j],idx[k],1,btSoftBody::eLType::Structural);
}
}
#undef IDX
psb->AppendFace(idx[0],idx[1],idx[2]);
}
psb->RandomizeConstraints();
return(psb);
}