72 if (node->
isinternal() && ((depth < maxdepth) || (maxdepth < 0)))
74 drawTree(idraw, node->
childs[0], depth + 1, ncolor, lcolor, mindepth, maxdepth);
75 drawTree(idraw, node->
childs[1], depth + 1, ncolor, lcolor, mindepth, maxdepth);
77 if (depth >= mindepth)
95 for (
int i = 1, ni = items.
size(); i < ni; ++i)
104 template <
typename T,
typename Q>
107 for (
int i = 0, ni = items.
size(); i < ni; ++i)
114 template <
typename T,
typename Q>
117 for (
int i = 0, ni = items.
size(); i < ni; ++i)
124 template <
typename T>
128 return (
sum(items) / n);
156 static const int ncolors=
sizeof(spectrum)/
sizeof(spectrum[0])-1;
158 stress=btMax<btScalar>(0,btMin<btScalar>(1,stress))*ncolors;
159 const int sel=(int)stress;
161 return(spectrum[sel]+(spectrum[sel+1]-spectrum[sel])*frc);
191 for (j = 0, nj = vertices.
size(); j < nj; ++j)
193 vertices[j] = psb->
m_clusters[i]->m_nodes[j]->m_x;
195 #define USE_NEW_CONVEX_HULL_COMPUTER 196 #ifdef USE_NEW_CONVEX_HULL_COMPUTER 199 int count = vertices.
size();
202 computer.
compute(&vertices[0].getX(), stride, count, shrink, shrinkClamp);
203 for (
int i = 0; i < computer.
faces.
size(); i++)
205 int face = computer.
faces[i];
212 while (edge != firstEdge)
226 hdsc.mMaxVertices = vertices.
size();
229 add(hres.m_OutputVertices, -center);
231 add(hres.m_OutputVertices, center);
232 for (j = 0; j < (int)hres.mNumFaces; ++j)
234 const int idx[] = {hres.m_Indices[j * 3 + 0], hres.m_Indices[j * 3 + 1], hres.m_Indices[j * 3 + 2]};
236 hres.m_OutputVertices[idx[1]],
237 hres.m_OutputVertices[idx[2]],
309 idraw->
drawLine(o - x * nscl, o + x * nscl, ccolor);
310 idraw->
drawLine(o - y * nscl, o + y * nscl, ccolor);
325 const btVector3 c = (x[0] + x[1] + x[2]) / 3;
327 (x[1] - c) * scl + c,
328 (x[2] - c) * scl + c,
343 const btVector3 c = (x[0] + x[1] + x[2] + x[3]) / 4;
344 idraw->
drawTriangle((x[0] - c) * scl + c, (x[1] - c) * scl + c, (x[2] - c) * scl + c, col, alp);
345 idraw->
drawTriangle((x[0] - c) * scl + c, (x[1] - c) * scl + c, (x[3] - c) * scl + c, col, alp);
346 idraw->
drawTriangle((x[1] - c) * scl + c, (x[2] - c) * scl + c, (x[3] - c) * scl + c, col, alp);
347 idraw->
drawTriangle((x[2] - c) * scl + c, (x[0] - c) * scl + c, (x[3] - c) * scl + c, col, alp);
380 for (
int j = 0; j < n.
m_rank; ++j)
443 char text[2048] = {0};
447 sprintf(buff,
" M(%.2f)", 1 / n.
m_im);
452 sprintf(buff,
" A(%.2f)", n.
m_area);
465 drawTree(idraw, psb->
m_ndbvt.
m_root, 0,
btVector3(1, 0, 1),
btVector3(1, 1, 1), mindepth, maxdepth);
474 drawTree(idraw, psb->
m_fdbvt.
m_root, 0,
btVector3(0, 1, 0),
btVector3(1, 0, 0), mindepth, maxdepth);
483 drawTree(idraw, psb->
m_cdbvt.
m_root, 0,
btVector3(0, 1, 1),
btVector3(1, 0, 0), mindepth, maxdepth);
534 #define REOP_NOT_DEPENDENT -1 535 #define REOP_NODE_COMPLETE -2 // Must be less than REOP_NOT_DEPENDENT 545 int readyListHead, readyListTail, linkNum, linkDepFrees, depLink;
548 int* nodeWrittenAt =
new int[nNodes + 1];
549 int* linkDepA =
new int[nLinks];
550 int* linkDepB =
new int[nLinks];
551 int* readyList =
new int[nLinks];
560 for (i = 0; i < nNodes + 1; i++)
564 for (i = 0; i < nLinks; i++)
566 linkDepListStarts[i] = NULL;
568 readyListHead = readyListTail = linkDepFrees = 0;
571 for (i = 0; i < nLinks; i++)
575 ar = (lr->
m_n[0] - node0) / (node1 - node0);
576 br = (lr->
m_n[1] - node0) / (node1 - node0);
579 linkDepA[i] = nodeWrittenAt[ar];
580 linkDep = &linkDepFreeList[linkDepFrees++];
582 linkDep->
next = linkDepListStarts[nodeWrittenAt[ar]];
583 linkDepListStarts[nodeWrittenAt[ar]] = linkDep;
591 linkDepB[i] = nodeWrittenAt[br];
592 linkDep = &linkDepFreeList[linkDepFrees++];
593 linkDep->
value = -(i + 1);
594 linkDep->
next = linkDepListStarts[nodeWrittenAt[br]];
595 linkDepListStarts[nodeWrittenAt[br]] = linkDep;
605 readyList[readyListTail++] = i;
610 nodeWrittenAt[ar] = nodeWrittenAt[br] = i;
619 while (readyListHead != readyListTail)
622 linkNum = readyList[readyListHead++];
624 psb->
m_links[i++] = linkBuffer[linkNum];
627 linkDep = linkDepListStarts[linkNum];
630 depLink = linkDep->
value;
637 depLink = -depLink - 1;
643 readyList[readyListTail++] = depLink;
646 linkDep = linkDep->
next;
651 delete[] nodeWrittenAt;
655 delete[] linkDepFreeList;
656 delete[] linkDepListStarts;
691 const int r = res + 2;
696 for (i = 0; i < r; ++i)
699 x[i] =
lerp(from, to, t);
703 if (fixeds & 1) psb->
setMass(0, 0);
704 if (fixeds & 2) psb->
setMass(r - 1, 0);
708 for (i = 1; i < r; ++i)
726 #define IDX(_x_, _y_) ((_y_)*rx + (_x_)) 728 if ((resx < 2) || (resy < 2))
return (0);
731 const int tot = rx * ry;
736 for (iy = 0; iy < ry; ++iy)
741 for (
int ix = 0; ix < rx; ++ix)
744 x[
IDX(ix, iy)] =
lerp(py0, py1, tx);
750 if (fixeds & 2) psb->
setMass(
IDX(rx - 1, 0), 0);
751 if (fixeds & 4) psb->
setMass(
IDX(0, ry - 1), 0);
752 if (fixeds & 8) psb->
setMass(
IDX(rx - 1, ry - 1), 0);
756 for (iy = 0; iy < ry; ++iy)
758 for (
int ix = 0; ix < rx; ++ix)
760 const int idx =
IDX(ix, iy);
761 const bool mdx = (ix + 1) < rx;
762 const bool mdy = (iy + 1) < ry;
872 #define IDX(_x_, _y_) ((_y_)*rx + (_x_)) 874 if ((resx < 2) || (resy < 2))
return (0);
877 const int tot = rx * ry;
883 for (iy = 0; iy < ry; ++iy)
888 for (
int ix = 0; ix < rx; ++ix)
891 x[
IDX(ix, iy)] =
lerp(py0, py1, tx);
897 if (fixeds & 2) psb->
setMass(
IDX(rx - 1, 0), 0);
898 if (fixeds & 4) psb->
setMass(
IDX(0, ry - 1), 0);
899 if (fixeds & 8) psb->
setMass(
IDX(rx - 1, ry - 1), 0);
900 if (fixeds & 16) psb->
setMass(
IDX((rx - 1) / 2, 0), 0);
901 if (fixeds & 32) psb->
setMass(
IDX(0, (ry - 1) / 2), 0);
902 if (fixeds & 64) psb->
setMass(
IDX(rx - 1, (ry - 1) / 2), 0);
903 if (fixeds & 128) psb->
setMass(
IDX((rx - 1) / 2, ry - 1), 0);
904 if (fixeds & 256) psb->
setMass(
IDX((rx - 1) / 2, (ry - 1) / 2), 0);
910 for (iy = 0; iy < ry; ++iy)
912 for (
int ix = 0; ix < rx; ++ix)
914 const bool mdx = (ix + 1) < rx;
915 const bool mdy = (iy + 1) < ry;
917 int node00 =
IDX(ix, iy);
918 int node01 =
IDX(ix + 1, iy);
919 int node10 =
IDX(ix, iy + 1);
920 int node11 =
IDX(ix + 1, iy + 1);
929 tex_coords[z + 0] =
CalculateUV(resx, resy, ix, iy, 0);
930 tex_coords[z + 1] =
CalculateUV(resx, resy, ix, iy, 1);
931 tex_coords[z + 2] =
CalculateUV(resx, resy, ix, iy, 0);
932 tex_coords[z + 3] =
CalculateUV(resx, resy, ix, iy, 2);
933 tex_coords[z + 4] =
CalculateUV(resx, resy, ix, iy, 3);
934 tex_coords[z + 5] =
CalculateUV(resx, resy, ix, iy, 2);
939 tex_coords[z + 6] =
CalculateUV(resx, resy, ix, iy, 3);
940 tex_coords[z + 7] =
CalculateUV(resx, resy, ix, iy, 2);
941 tex_coords[z + 8] =
CalculateUV(resx, resy, ix, iy, 3);
942 tex_coords[z + 9] =
CalculateUV(resx, resy, ix, iy, 1);
943 tex_coords[z + 10] =
CalculateUV(resx, resy, ix, iy, 0);
944 tex_coords[z + 11] =
CalculateUV(resx, resy, ix, iy, 1);
946 if (gendiags) psb->
appendLink(node00, node11);
986 tc = (1.0f / ((resx - 1)) * ix);
990 tc = (1.0f / ((resy - 1)) * (resy - 1 - iy));
994 tc = (1.0f / ((resy - 1)) * (resy - 1 - iy - 1));
998 tc = (1.0f / ((resx - 1)) * (ix + 1));
1009 static void Generate(
btVector3* x,
int n)
1011 for (
int i = 0; i < n; i++)
1014 for (
int j = i; j; p *= 0.5, j >>= 1)
1025 Hammersley::Generate(&vtx[0], vtx.
size());
1026 for (
int i = 0; i < vtx.
size(); ++i)
1028 vtx[i] = vtx[i] * radius + center;
1035 const int* triangles,
1036 int ntriangles,
bool randomizeConstraints)
1041 for (i = 0, ni = ntriangles * 3; i < ni; ++i)
1043 maxidx =
btMax(triangles[i], maxidx);
1048 chks.
resize(maxidx * maxidx,
false);
1050 for (i = 0, j = 0, ni = maxidx * 3; i < ni; ++j, i += 3)
1052 vtx[j] =
btVector3(vertices[i], vertices[i + 1], vertices[i + 2]);
1055 for (i = 0, ni = ntriangles * 3; i < ni; i += 3)
1057 const int idx[] = {triangles[i], triangles[i + 1], triangles[i + 2]};
1058 #define IDX(_x_, _y_) ((_y_)*maxidx + (_x_)) 1059 for (
int j = 2, k = 0; k < 3; j = k++)
1061 if (!chks[
IDX(idx[j], idx[k])])
1063 chks[
IDX(idx[j], idx[k])] =
true;
1064 chks[
IDX(idx[k], idx[j])] =
true;
1072 if (randomizeConstraints)
1082 int nvertices,
bool randomizeConstraints)
1091 for (
int i = 0; i < (int)hres.
mNumFaces; ++i)
1093 const int idx[] = {
static_cast<int>(hres.
m_Indices[i * 3 + 0]),
1094 static_cast<int>(hres.
m_Indices[i * 3 + 1]),
1095 static_cast<int>(hres.
m_Indices[i * 3 + 2])};
1096 if (idx[0] < idx[1]) psb->
appendLink(idx[0], idx[1]);
1097 if (idx[1] < idx[2]) psb->
appendLink(idx[1], idx[2]);
1098 if (idx[2] < idx[0]) psb->
appendLink(idx[2], idx[0]);
1102 if (randomizeConstraints)
1111 int numBytesRead = 0;
1113 while (*buffer !=
'\n')
1119 if (buffer[0] == 0x0a)
1124 return numBytesRead;
1134 bool bfacesfromtetras)
1141 int result = sscanf(node,
"%d %d %d %d", &nnode, &ndims, &nattrb, &hasbounds);
1142 result = sscanf(node,
"%d %d %d %d", &nnode, &ndims, &nattrb, &hasbounds);
1146 for (
int i = 0; i < pos.
size(); ++i)
1151 sscanf(node,
"%d %f %f %f", &index, &x, &y, &z);
1172 sf>>nface;sf>>hasbounds;
1173 for(
int i=0;i<nface;++i)
1179 sf>>ni[0];sf>>ni[1];sf>>ni[2];
1197 sscanf(ele,
"%d %d %d", &ntetra, &ncorner, &neattrb);
1201 for (
int i = 0; i < ntetra; ++i)
1208 sscanf(ele,
"%d %d %d %d %d", &index, &ni[0], &ni[1], &ni[2], &ni[3]);
static T sum(const btAlignedObjectArray< T > &items)
btAlignedObjectArray< Edge > edges
static btSoftBody * CreatePatchUV(btSoftBodyWorldInfo &worldInfo, const btVector3 &corner00, const btVector3 &corner10, const btVector3 &corner01, const btVector3 &corner11, int resx, int resy, int fixeds, bool gendiags, float *tex_coords=0)
The btAlignedObjectArray template class uses a subset of the stl::vector interface for its methods It...
btScalar btSin(btScalar x)
virtual void drawLine(const btVector3 &from, const btVector3 &to, const btVector3 &color)=0
static btSoftBody * CreateEllipsoid(btSoftBodyWorldInfo &worldInfo, const btVector3 ¢er, const btVector3 &radius, int res)
static btSoftBody * CreatePatch(btSoftBodyWorldInfo &worldInfo, const btVector3 &corner00, const btVector3 &corner10, const btVector3 &corner01, const btVector3 &corner11, int resx, int resy, int fixeds, bool gendiags)
btScalar btSqrt(btScalar y)
btAlignedObjectArray< unsigned int > m_Indices
btAlignedObjectArray< Node * > m_nodes
DBVT_INLINE btVector3 Center() const
#define REOP_NOT_DEPENDENT
static btSoftBody * CreateFromTetGenData(btSoftBodyWorldInfo &worldInfo, const char *ele, const char *face, const char *node, bool bfacelinks, bool btetralinks, bool bfacesfromtetras)
static void DrawFaceTree(btSoftBody *psb, btIDebugDraw *idraw, int mindepth=0, int maxdepth=-1)
void appendLink(int model=-1, Material *mat=0)
LinkDeps_t * LinkDepsPtr_t
Convex hull implementation based on Preparata and Hong See http://code.google.com/p/bullet/issues/det...
DBVT_INLINE btVector3 Extents() const
btVector3 normalized() const
Return a normalized version of this vector.
static void Draw(btSoftBody *psb, btIDebugDraw *idraw, int drawflags=fDrawFlags::Std)
static void add(btAlignedObjectArray< T > &items, const Q &value)
void appendFace(int model=-1, Material *mat=0)
static btSoftBody * CreateFromConvexHull(btSoftBodyWorldInfo &worldInfo, const btVector3 *vertices, int nvertices, bool randomizeConstraints=true)
static void drawBox(btIDebugDraw *idraw, const btVector3 &mins, const btVector3 &maxs, const btVector3 &color)
btVector3 btCross(const btVector3 &v1, const btVector3 &v2)
Return the cross product of two vectors.
int getTargetVertex() const
virtual void draw3dText(const btVector3 &location, const char *textString)=0
btTransform & getWorldTransform()
int minAxis() const
Return the axis with the smallest value Note return values are 0,1,2 for x, y, or z...
static void mul(btAlignedObjectArray< T > &items, const Q &value)
HullError CreateConvexHull(const HullDesc &desc, HullResult &result)
virtual eType::_ Type() const =0
void randomizeConstraints()
const btScalar & x() const
Return the x value.
static btSoftBody * CreateRope(btSoftBodyWorldInfo &worldInfo, const btVector3 &from, const btVector3 &to, int res, int fixeds)
static void DrawInfos(btSoftBody *psb, btIDebugDraw *idraw, bool masses, bool areas, bool stress)
void appendTetra(int model, Material *mat)
const btScalar & y() const
Return the y value.
const btScalar & z() const
Return the z value.
The btIDebugDraw interface class allows hooking up a debug renderer to visually debug simulations...
btAlignedObjectArray< btVector3 > m_OutputVertices
static void DrawNodeTree(btSoftBody *psb, btIDebugDraw *idraw, int mindepth=0, int maxdepth=-1)
static void drawVertex(btIDebugDraw *idraw, const btVector3 &x, btScalar s, const btVector3 &c)
btSoftBodyHelpers.cpp by Nathanael Presson
static void DrawClusterTree(btSoftBody *psb, btIDebugDraw *idraw, int mindepth=0, int maxdepth=-1)
btVector3 can be used to represent 3D points and vectors.
int size() const
return the number of elements in the array
btAlignedObjectArray< btVector3 > vertices
virtual void drawTriangle(const btVector3 &v0, const btVector3 &v1, const btVector3 &v2, const btVector3 &, const btVector3 &, const btVector3 &, const btVector3 &color, btScalar alpha)
const Edge * getNextEdgeOfFace() const
DBVT_INLINE bool isleaf() const
static float CalculateUV(int resx, int resy, int ix, int iy, int id)
unsigned int mNumOutputVertices
static void ReoptimizeLinkOrder(btSoftBody *psb)
Sort the list of links to move link calculations that are dependent upon earlier ones as far as possi...
void resize(int newsize, const T &fillData=T())
DBVT_INLINE bool isinternal() const
static void drawTree(btIDebugDraw *idraw, const btDbvtNode *node, int depth, const btVector3 &ncolor, const btVector3 &lcolor, int mindepth, int maxdepth)
static btSoftBody * CreateFromTriMesh(btSoftBodyWorldInfo &worldInfo, const btScalar *vertices, const int *triangles, int ntriangles, bool randomizeConstraints=true)
const btTransform & xform() const
const T & btMax(const T &a, const T &b)
The btMatrix3x3 class implements a 3x3 rotation matrix, to perform linear algebra in combination with...
btScalar compute(const void *coords, bool doubleCoords, int stride, int count, btScalar shrink, btScalar shrinkClamp)
The HullLibrary class can create a convex hull from a collection of vertices, using the ComputeHull m...
static int nextLine(const char *buffer)
btScalar btDot(const btVector3 &v1, const btVector3 &v2)
Return the dot product between two vectors.
static T average(const btAlignedObjectArray< T > &items)
HullError ReleaseResult(HullResult &result)
The btSoftBody is an class to simulate cloth and volumetric soft bodies.
unsigned int mMaxVertices
void setMass(int node, btScalar mass)
int getSourceVertex() const
tRContactArray m_rcontacts
btAlignedObjectArray< int > faces
btVector3 lerp(const btVector3 &v1, const btVector3 &v2, const btScalar &t)
Return the linear interpolation between two vectors.
static void DrawFrame(btSoftBody *psb, btIDebugDraw *idraw)
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
btScalar btCos(btScalar x)
#define REOP_NODE_COMPLETE