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 <sofa/component/topology/GridTopology.h>
00026 #include <sofa/core/ObjectFactory.h>
00027
00028 namespace sofa
00029 {
00030
00031 namespace component
00032 {
00033
00034 namespace topology
00035 {
00036
00037 SOFA_DECL_CLASS(GridTopology)
00038
00039 int GridTopologyClass = core::RegisterObject("Base class fo a regular grid in 3D")
00040 .addAlias("Grid")
00041 .add< GridTopology >()
00042 ;
00043
00044 GridTopology::GridTopology()
00045 : n(initData(&n,Vec<3, int>(0,0,0),"n","grid resolution"))
00046 {
00047 }
00048
00049 GridTopology::GridTopology(int _nx, int _ny, int _nz)
00050 : n(initData(&n,Vec<3, int>(_nx,_ny,_nz),"n","grid resolution"))
00051 {
00052 nbPoints = _nx*_ny*_nz;
00053 }
00054
00055 void GridTopology::setSize(int nx, int ny, int nz)
00056 {
00057 if (nx == this->n.getValue()[0] && ny == this->n.getValue()[1] && nz == this->n.getValue()[2])
00058 return;
00059 this->n.setValue(Vec<3, int>(nx,ny,nz));
00060 setSize();
00061 }
00062
00063 void GridTopology::setSize()
00064 {
00065 this->nbPoints = n.getValue()[0]*n.getValue()[1]*n.getValue()[2];
00066 invalidate();
00067 }
00068
00069 void GridTopology::updateEdges()
00070 {
00071 SeqEdges& edges = *seqEdges.beginEdit();
00072 edges.clear();
00073 edges.reserve((n.getValue()[0]-1)*n.getValue()[1]*n.getValue()[2]+n.getValue()[0]*(n.getValue()[1]-1)*n.getValue()[2]+n.getValue()[0]*n.getValue()[1]*(n.getValue()[2]-1));
00074
00075 for (int z=0; z<n.getValue()[2]; z++)
00076 for (int y=0; y<n.getValue()[1]; y++)
00077 for (int x=0; x<n.getValue()[0]-1; x++)
00078 edges.push_back(Edge(point(x,y,z),point(x+1,y,z)));
00079
00080 for (int z=0; z<n.getValue()[2]; z++)
00081 for (int y=0; y<n.getValue()[1]-1; y++)
00082 for (int x=0; x<n.getValue()[0]; x++)
00083 edges.push_back(Edge(point(x,y,z),point(x,y+1,z)));
00084
00085 for (int z=0; z<n.getValue()[2]-1; z++)
00086 for (int y=0; y<n.getValue()[1]; y++)
00087 for (int x=0; x<n.getValue()[0]; x++)
00088 edges.push_back(Edge(point(x,y,z),point(x,y,z+1)));
00089 seqEdges.endEdit();
00090 }
00091
00092 void GridTopology::updateQuads()
00093 {
00094 SeqQuads& quads = *seqQuads.beginEdit();
00095 quads.clear();
00096 quads.reserve((n.getValue()[0]-1)*(n.getValue()[1]-1)*n.getValue()[2]+(n.getValue()[0]-1)*n.getValue()[1]*(n.getValue()[2]-1)+n.getValue()[0]*(n.getValue()[1]-1)*(n.getValue()[2]-1));
00097
00098 for (int z=0; z<n.getValue()[2]; z++)
00099 for (int y=0; y<n.getValue()[1]-1; y++)
00100 for (int x=0; x<n.getValue()[0]-1; x++)
00101 quads.push_back(Quad(point(x,y,z),point(x+1,y,z),point(x+1,y+1,z),point(x,y+1,z)));
00102
00103 for (int z=0; z<n.getValue()[2]-1; z++)
00104 for (int y=0; y<n.getValue()[1]; y++)
00105 for (int x=0; x<n.getValue()[0]-1; x++)
00106 quads.push_back(Quad(point(x,y,z),point(x+1,y,z),point(x+1,y,z+1),point(x,y,z+1)));
00107
00108 for (int z=0; z<n.getValue()[2]-1; z++)
00109 for (int y=0; y<n.getValue()[1]-1; y++)
00110 for (int x=0; x<n.getValue()[0]; x++)
00111 quads.push_back(Quad(point(x,y,z),point(x,y+1,z),point(x,y+1,z+1),point(x,y,z+1)));
00112
00113 seqQuads.endEdit();
00114 }
00115
00116 void GridTopology::updateHexas()
00117 {
00118 SeqHexas& hexas = *seqHexas.beginEdit();
00119 hexas.clear();
00120 hexas.reserve((n.getValue()[0]-1)*(n.getValue()[1]-1)*(n.getValue()[2]-1));
00121 for (int z=0; z<n.getValue()[2]-1; z++)
00122 for (int y=0; y<n.getValue()[1]-1; y++)
00123 for (int x=0; x<n.getValue()[0]-1; x++)
00124 #ifdef SOFA_NEW_HEXA
00125 hexas.push_back(Hexa(point(x ,y ,z ),point(x+1,y ,z ),
00126 point(x+1,y+1,z ),point(x ,y+1,z ),
00127 point(x ,y ,z+1),point(x+1,y ,z+1),
00128 point(x+1,y+1,z+1),point(x ,y+1,z+1)));
00129 #else
00130 hexas.push_back(Hexa(point(x ,y ,z ),point(x+1,y ,z ),
00131 point(x ,y+1,z ),point(x+1,y+1,z ),
00132 point(x ,y ,z+1),point(x+1,y ,z+1),
00133 point(x ,y+1,z+1),point(x+1,y+1,z+1)));
00134 #endif
00135 seqHexas.endEdit();
00136 }
00137
00138 GridTopology::Hexa GridTopology::getHexaCopy(int i)
00139 {
00140 int x = i%(n.getValue()[0]-1); i/=(n.getValue()[0]-1);
00141 int y = i%(n.getValue()[1]-1); i/=(n.getValue()[1]-1);
00142 int z = i;
00143 return getHexa(x,y,z);
00144 }
00145
00146 GridTopology::Hexa GridTopology::getHexa(int x, int y, int z)
00147 {
00148 #ifdef SOFA_NEW_HEXA
00149 return Hexa(point(x ,y ,z ),point(x+1,y ,z ),
00150 point(x+1,y+1,z ),point(x ,y+1,z ),
00151 point(x ,y ,z+1),point(x+1,y ,z+1),
00152 point(x+1,y+1,z+1),point(x ,y+1,z+1));
00153 #else
00154 return Hexa(point(x ,y ,z ),point(x+1,y ,z ),
00155 point(x ,y+1,z ),point(x+1,y+1,z ),
00156 point(x ,y ,z+1),point(x+1,y ,z+1),
00157 point(x ,y+1,z+1),point(x+1,y+1,z+1));
00158 #endif
00159 }
00160
00161 GridTopology::Quad GridTopology::getQuadCopy(int i)
00162 {
00163 if (n.getValue()[0] == 1)
00164 {
00165 int y = i%(n.getValue()[1]-1);
00166 i/=(n.getValue()[1]-1);
00167 int z = i%(n.getValue()[2]-1);
00168
00169 return getQuad(1,y,z);
00170 }
00171 else if (n.getValue()[1] == 1)
00172 {
00173 int x = i%(n.getValue()[0]-1);
00174 i/=(n.getValue()[0]-1);
00175 int z = i%(n.getValue()[2]-1);
00176
00177 return getQuad(x,1,z);
00178 }
00179 else
00180 {
00181 int x = i%(n.getValue()[0]-1);
00182 i/=(n.getValue()[0]-1);
00183 int y = i%(n.getValue()[1]-1);
00184
00185 return getQuad(x,y,1);
00186 }
00187 }
00188
00189 GridTopology::Quad GridTopology::getQuad(int x, int y, int z)
00190 {
00191 if (n.getValue()[2] == 1)
00192 return Quad(point(x, y, 1), point(x+1, y, 1),
00193 point(x+1, y+1, 1), point(x, y+1, 1));
00194 else if (n.getValue()[1] == 1)
00195 return Quad(point(x, 1, z), point(x+1, 1, z),
00196 point(x+1, 1, z+1), point(x, 1, z+1));
00197 else
00198 return Quad(point(1, y, z),point(1, y+1, z),
00199 point(1, y+1, z+1),point(1, y, z+1));
00200 }
00201
00202 }
00203
00204 }
00205
00206 }
00207