RegularGridSpringForceField.inl

Go to the documentation of this file.
00001 /******************************************************************************
00002 *       SOFA, Simulation Open-Framework Architecture, version 1.0 beta 3      *
00003 *                (c) 2006-2008 MGH, INRIA, USTL, UJF, CNRS                    *
00004 *                                                                             *
00005 * This library is free software; you can redistribute it and/or modify it     *
00006 * under the terms of the GNU Lesser General Public License as published by    *
00007 * the Free Software Foundation; either version 2.1 of the License, or (at     *
00008 * your option) any later version.                                             *
00009 *                                                                             *
00010 * This library is distributed in the hope that it will be useful, but WITHOUT *
00011 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       *
00012 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
00013 * for more details.                                                           *
00014 *                                                                             *
00015 * You should have received a copy of the GNU Lesser General Public License    *
00016 * along with this library; if not, write to the Free Software Foundation,     *
00017 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.          *
00018 *******************************************************************************
00019 *                               SOFA :: Modules                               *
00020 *                                                                             *
00021 * Authors: The SOFA Team and external contributors (see Authors.txt)          *
00022 *                                                                             *
00023 * Contact information: contact@sofa-framework.org                             *
00024 ******************************************************************************/
00025 #ifndef SOFA_COMPONENT_FORCEFIELD_REGULARGRIDSPRINGFORCEFIELD_INL
00026 #define SOFA_COMPONENT_FORCEFIELD_REGULARGRIDSPRINGFORCEFIELD_INL
00027 
00028 #include <sofa/component/forcefield/RegularGridSpringForceField.h>
00029 #include <sofa/component/forcefield/StiffSpringForceField.inl>
00030 #include <sofa/helper/gl/template.h>
00031 
00032 namespace sofa
00033 {
00034 
00035 namespace component
00036 {
00037 
00038 namespace forcefield
00039 {
00040 
00041 template<class DataTypes>
00042 void RegularGridSpringForceField<DataTypes>::parse(core::objectmodel::BaseObjectDescription* arg)
00043 {
00044     this->Inherit::parse(arg);
00045     if (arg->getAttribute("stiffness")) this->setStiffness((Real)atof(arg->getAttribute("stiffness")));
00046     if (arg->getAttribute("damping")) this->setDamping((Real)atof(arg->getAttribute("damping")));
00047 }
00048 
00049 template<class DataTypes>
00050 void RegularGridSpringForceField<DataTypes>::init()
00051 {
00052     //this->StiffSpringForceField<DataTypes>::init();
00053     if (this->mstate1 == NULL){
00054         this->mstate1 = dynamic_cast<core::componentmodel::behavior::MechanicalState<DataTypes>* >(this->getContext()->getMechanicalState());
00055         this->mstate2 = this->mstate1;
00056     }
00057     if (this->mstate1==this->mstate2)
00058     {
00059         topology = dynamic_cast<topology::RegularGridTopology*>(this->mstate1->getContext()->getMeshTopology());
00060     }
00061     this->StiffSpringForceField<DataTypes>::init();
00062 }
00063 
00064 template<class DataTypes>
00065 void RegularGridSpringForceField<DataTypes>::addForce(VecDeriv& f1, VecDeriv& f2, const VecCoord& x1, const VecCoord& x2, const VecDeriv& v1, const VecDeriv& v2)
00066 {
00067     // Calc any custom springs
00068     this->StiffSpringForceField<DataTypes>::addForce(f1, f2, x1, x2, v1, v2);
00069     // Compute topological springs
00070     f1.resize(x1.size());
00071     f2.resize(x2.size());
00072         m_potentialEnergy = 0;
00073         const helper::vector<Spring>& springs = this->springs.getValue();
00074         if (this->mstate1==this->mstate2)
00075     {
00076         if (topology != NULL)
00077         {
00078             const int nx = topology->getNx();
00079             const int ny = topology->getNy();
00080             const int nz = topology->getNz();
00081             int index = springs.size();
00082             int size = index;
00083             if (this->linesStiffness.getValue() != 0.0 || this->linesDamping.getValue() != 0.0)
00084                 size += ((nx-1)*ny*nz+nx*(ny-1)*nz+nx*ny*(nz-1));
00085             if (this->quadsStiffness.getValue() != 0.0 || this->quadsDamping.getValue() != 0.0)
00086                 size += ((nx-1)*(ny-1)*nz+(nx-1)*ny*(nz-1)+nx*(ny-1)*(nz-1))*2;
00087             if (this->cubesStiffness.getValue() != 0.0 || this->cubesDamping.getValue() != 0.0)
00088                 size += ((nx-1)*(ny-1)*(nz-1))*4;
00089             this->dfdx.resize(size);
00090             if (this->linesStiffness.getValue() != 0.0 || this->linesDamping.getValue() != 0.0)
00091             {
00092                 Spring spring;
00093                 // lines along X
00094                 spring.initpos = topology->getDx().norm();
00095                 spring.ks = this->linesStiffness.getValue() / spring.initpos;
00096                 spring.kd = this->linesDamping.getValue() / spring.initpos;
00097                 for (int z=0; z<nz; z++)
00098                     for (int y=0; y<ny; y++)
00099                         for (int x=0; x<nx-1; x++)
00100                         {
00101                             spring.m1 = topology->point(x,y,z);
00102                             spring.m2 = topology->point(x+1,y,z);
00103                             this->addSpringForce(m_potentialEnergy,f1,x1,v1,f2,x2,v2, index++, spring);
00104                         }
00105                 // lines along Y
00106                 spring.initpos = topology->getDy().norm();
00107                 spring.ks = this->linesStiffness.getValue() / spring.initpos;
00108                 spring.kd = this->linesDamping.getValue() / spring.initpos;
00109                 for (int z=0; z<nz; z++)
00110                     for (int y=0; y<ny-1; y++)
00111                         for (int x=0; x<nx; x++)
00112                         {
00113                             spring.m1 = topology->point(x,y,z);
00114                             spring.m2 = topology->point(x,y+1,z);
00115                                                         this->addSpringForce(m_potentialEnergy,f1,x1,v1,f2,x2,v2, index++, spring);
00116                         }
00117                 // lines along Z
00118                 spring.initpos = topology->getDz().norm();
00119                 spring.ks = this->linesStiffness.getValue() / spring.initpos;
00120                 spring.kd = this->linesDamping.getValue() / spring.initpos;
00121                 for (int z=0; z<nz-1; z++)
00122                     for (int y=0; y<ny; y++)
00123                         for (int x=0; x<nx; x++)
00124                         {
00125                             spring.m1 = topology->point(x,y,z);
00126                             spring.m2 = topology->point(x,y,z+1);
00127                                                         this->addSpringForce(m_potentialEnergy,f1,x1,v1,f2,x2,v2, index++, spring);
00128                         }
00129                 
00130             }
00131             if (this->quadsStiffness.getValue() != 0.0 || this->quadsDamping.getValue() != 0.0)
00132             {
00133                 typename RegularGridSpringForceField<DataTypes>::Spring spring1;
00134                 typename RegularGridSpringForceField<DataTypes>::Spring spring2;
00135                 // quads along XY plane
00136                 // lines (x,y,z) -> (x+1,y+1,z)
00137                 spring1.initpos = (topology->getDx()+topology->getDy()).norm();
00138                 spring1.ks = this->quadsStiffness.getValue() / spring1.initpos;
00139                 spring1.kd = this->quadsDamping.getValue() / spring1.initpos;
00140                 // lines (x+1,y,z) -> (x,y+1,z)
00141                 spring2.initpos = (topology->getDx()-topology->getDy()).norm();
00142                 spring2.ks = this->quadsStiffness.getValue() / spring2.initpos;
00143                 spring2.kd = this->quadsDamping.getValue() / spring2.initpos;
00144                 for (int z=0; z<nz; z++)
00145                     for (int y=0; y<ny-1; y++)
00146                         for (int x=0; x<nx-1; x++)
00147                         {
00148                             spring1.m1 = topology->point(x,y,z);
00149                             spring1.m2 = topology->point(x+1,y+1,z);
00150                                                         this->addSpringForce(m_potentialEnergy,f1,x1,v1,f2,x2,v2, index++, spring1);
00151                             spring2.m1 = topology->point(x+1,y,z);
00152                             spring2.m2 = topology->point(x,y+1,z);
00153                                                         this->addSpringForce(m_potentialEnergy,f1,x1,v1,f2,x2,v2, index++, spring2);
00154                         }
00155                 // quads along XZ plane
00156                 // lines (x,y,z) -> (x+1,y,z+1)
00157                 spring1.initpos = (topology->getDx()+topology->getDz()).norm();
00158                 spring1.ks = this->quadsStiffness.getValue() / spring1.initpos;
00159                 spring1.kd = this->quadsDamping.getValue() / spring1.initpos;
00160                 // lines (x+1,y,z) -> (x,y,z+1)
00161                 spring2.initpos = (topology->getDx()-topology->getDz()).norm();
00162                 spring2.ks = this->quadsStiffness.getValue() / spring2.initpos;
00163                 spring2.kd = this->quadsDamping.getValue() / spring2.initpos;
00164                 for (int z=0; z<nz-1; z++)
00165                     for (int y=0; y<ny; y++)
00166                         for (int x=0; x<nx-1; x++)
00167                         {
00168                             spring1.m1 = topology->point(x,y,z);
00169                             spring1.m2 = topology->point(x+1,y,z+1);
00170                                                         this->addSpringForce(m_potentialEnergy,f1,x1,v1,f2,x2,v2, index++, spring1);
00171                             spring2.m1 = topology->point(x+1,y,z);
00172                             spring2.m2 = topology->point(x,y,z+1);
00173                                                         this->addSpringForce(m_potentialEnergy,f1,x1,v1,f2,x2,v2, index++, spring2);
00174                         }
00175                 // quads along YZ plane
00176                 // lines (x,y,z) -> (x,y+1,z+1)
00177                 spring1.initpos = (topology->getDy()+topology->getDz()).norm();
00178                 spring1.ks = this->quadsStiffness.getValue() / spring1.initpos;
00179                 spring1.kd = this->quadsDamping.getValue() / spring1.initpos;
00180                 // lines (x,y+1,z) -> (x,y,z+1)
00181                 spring2.initpos = (topology->getDy()-topology->getDz()).norm();
00182                 spring2.ks = this->quadsStiffness.getValue() / spring1.initpos;
00183                 spring2.kd = this->quadsDamping.getValue() / spring1.initpos;
00184                 for (int z=0; z<nz-1; z++)
00185                     for (int y=0; y<ny-1; y++)
00186                         for (int x=0; x<nx; x++)
00187                         {
00188                             spring1.m1 = topology->point(x,y,z);
00189                             spring1.m2 = topology->point(x,y+1,z+1);
00190                                                         this->addSpringForce(m_potentialEnergy,f1,x1,v1,f2,x2,v2, index++, spring1);
00191                             spring2.m1 = topology->point(x,y+1,z);
00192                             spring2.m2 = topology->point(x,y,z+1);
00193                                                         this->addSpringForce(m_potentialEnergy,f1,x1,v1,f2,x2,v2, index++, spring2);
00194                         }
00195             }
00196             if (this->cubesStiffness.getValue() != 0.0 || this->cubesDamping.getValue() != 0.0)
00197             {
00198                 typename RegularGridSpringForceField<DataTypes>::Spring spring1;
00199                 typename RegularGridSpringForceField<DataTypes>::Spring spring2;
00200                 typename RegularGridSpringForceField<DataTypes>::Spring spring3;
00201                 typename RegularGridSpringForceField<DataTypes>::Spring spring4;
00202                 // lines (x,y,z) -> (x+1,y+1,z+1)
00203                 spring1.initpos = (topology->getDx()+topology->getDy()+topology->getDz()).norm();
00204                 spring1.ks = this->cubesStiffness.getValue() / spring1.initpos;
00205                 spring1.kd = this->cubesDamping.getValue() / spring1.initpos;
00206                 // lines (x+1,y,z) -> (x,y+1,z+1)
00207                 spring2.initpos = (-topology->getDx()+topology->getDy()+topology->getDz()).norm();
00208                 spring2.ks = this->cubesStiffness.getValue() / spring2.initpos;
00209                 spring2.kd = this->cubesDamping.getValue() / spring2.initpos;
00210                 // lines (x,y+1,z) -> (x+1,y,z+1)
00211                 spring3.initpos = (topology->getDx()-topology->getDy()+topology->getDz()).norm();
00212                 spring3.ks = this->cubesStiffness.getValue() / spring3.initpos;
00213                 spring3.kd = this->cubesDamping.getValue() / spring3.initpos;
00214                 // lines (x,y,z+1) -> (x+1,y+1,z)
00215                 spring4.initpos = (topology->getDx()+topology->getDy()-topology->getDz()).norm();
00216                 spring4.ks = this->cubesStiffness.getValue() / spring4.initpos;
00217                 spring4.kd = this->cubesDamping.getValue() / spring4.initpos;
00218                 for (int z=0; z<nz-1; z++)
00219                     for (int y=0; y<ny-1; y++)
00220                         for (int x=0; x<nx-1; x++)
00221                         {
00222                             spring1.m1 = topology->point(x,y,z);
00223                             spring1.m2 = topology->point(x+1,y+1,z+1);
00224                                                         this->addSpringForce(m_potentialEnergy,f1,x1,v1,f2,x2,v2, index++, spring1);
00225                             spring2.m1 = topology->point(x+1,y,z);
00226                             spring2.m2 = topology->point(x,y+1,z+1);
00227                                                         this->addSpringForce(m_potentialEnergy,f1,x1,v1,f2,x2,v2, index++, spring2);
00228                             spring3.m1 = topology->point(x,y+1,z);
00229                             spring3.m2 = topology->point(x+1,y,z+1);
00230                                                         this->addSpringForce(m_potentialEnergy,f1,x1,v1,f2,x2,v2, index++, spring3);
00231                             spring4.m1 = topology->point(x,y,z+1);
00232                             spring4.m2 = topology->point(x+1,y+1,z);
00233                                                         this->addSpringForce(m_potentialEnergy,f1,x1,v1,f2,x2,v2, index++, spring4);
00234                         }
00235             }
00236         }
00237     }
00238 }
00239 
00240 template<class DataTypes>
00241 void RegularGridSpringForceField<DataTypes>::addDForce(VecDeriv& df1, VecDeriv& df2, const VecDeriv& dx1, const VecDeriv& dx2, double kFactor, double bFactor)
00242 {
00243     // Calc any custom springs
00244     this->StiffSpringForceField<DataTypes>::addDForce(df1, df2, dx1, dx2, kFactor, bFactor);
00245     // Compute topological springs
00246         const helper::vector<Spring>& springs = this->springs.getValue();
00247         if (this->mstate1==this->mstate2)
00248     {
00249         if (topology != NULL)
00250         {
00251             const int nx = topology->getNx();
00252             const int ny = topology->getNy();
00253             const int nz = topology->getNz();
00254             int index = springs.size();
00255             if (this->linesStiffness.getValue() != 0.0 || this->linesDamping.getValue() != 0.0)
00256             {
00257                 typename RegularGridSpringForceField<DataTypes>::Spring spring;
00258                 // lines along X
00259                 spring.initpos = topology->getDx().norm();
00260                 spring.ks = this->linesStiffness.getValue() / spring.initpos;
00261                 spring.kd = this->linesDamping.getValue() / spring.initpos;
00262                 for (int z=0; z<nz; z++)
00263                     for (int y=0; y<ny; y++)
00264                         for (int x=0; x<nx-1; x++)
00265                         {
00266                             spring.m1 = topology->point(x,y,z);
00267                             spring.m2 = topology->point(x+1,y,z);
00268                             this->addSpringDForce(df1,dx1,df2,dx2, index++, spring, kFactor, bFactor);
00269                         }
00270                 // lines along Y
00271                 spring.initpos = topology->getDy().norm();
00272                 spring.ks = this->linesStiffness.getValue() / spring.initpos;
00273                 spring.kd = this->linesDamping.getValue() / spring.initpos;
00274                 for (int z=0; z<nz; z++)
00275                     for (int y=0; y<ny-1; y++)
00276                         for (int x=0; x<nx; x++)
00277                         {
00278                             spring.m1 = topology->point(x,y,z);
00279                             spring.m2 = topology->point(x,y+1,z);
00280                             this->addSpringDForce(df1,dx1,df2,dx2, index++, spring, kFactor, bFactor);
00281                         }
00282                 // lines along Z
00283                 spring.initpos = topology->getDz().norm();
00284                 spring.ks = this->linesStiffness.getValue() / spring.initpos;
00285                 spring.kd = this->linesDamping.getValue() / spring.initpos;
00286                 for (int z=0; z<nz-1; z++)
00287                     for (int y=0; y<ny; y++)
00288                         for (int x=0; x<nx; x++)
00289                         {
00290                             spring.m1 = topology->point(x,y,z);
00291                             spring.m2 = topology->point(x,y,z+1);
00292                             this->addSpringDForce(df1,dx1,df2,dx2, index++, spring, kFactor, bFactor);
00293                         }
00294                 
00295             }
00296             if (this->quadsStiffness.getValue() != 0.0 || this->quadsDamping.getValue() != 0.0)
00297             {
00298                 typename RegularGridSpringForceField<DataTypes>::Spring spring1;
00299                 typename RegularGridSpringForceField<DataTypes>::Spring spring2;
00300                 // quads along XY plane
00301                 // lines (x,y,z) -> (x+1,y+1,z)
00302                 spring1.initpos = (topology->getDx()+topology->getDy()).norm();
00303                 spring1.ks = this->quadsStiffness.getValue() / spring1.initpos;
00304                 spring1.kd = this->quadsDamping.getValue() / spring1.initpos;
00305                 // lines (x+1,y,z) -> (x,y+1,z)
00306                 spring2.initpos = (topology->getDx()-topology->getDy()).norm();
00307                 spring2.ks = this->quadsStiffness.getValue() / spring2.initpos;
00308                 spring2.kd = this->quadsDamping.getValue() / spring2.initpos;
00309                 for (int z=0; z<nz; z++)
00310                     for (int y=0; y<ny-1; y++)
00311                         for (int x=0; x<nx-1; x++)
00312                         {
00313                             spring1.m1 = topology->point(x,y,z);
00314                             spring1.m2 = topology->point(x+1,y+1,z);
00315                             this->addSpringDForce(df1,dx1,df2,dx2, index++, spring1, kFactor, bFactor);
00316                             spring2.m1 = topology->point(x+1,y,z);
00317                             spring2.m2 = topology->point(x,y+1,z);
00318                             this->addSpringDForce(df1,dx1,df2,dx2, index++, spring2, kFactor, bFactor);
00319                         }
00320                 // quads along XZ plane
00321                 // lines (x,y,z) -> (x+1,y,z+1)
00322                 spring1.initpos = (topology->getDx()+topology->getDz()).norm();
00323                 spring1.ks = this->quadsStiffness.getValue() / spring1.initpos;
00324                 spring1.kd = this->quadsDamping.getValue() / spring1.initpos;
00325                 // lines (x+1,y,z) -> (x,y,z+1)
00326                 spring2.initpos = (topology->getDx()-topology->getDz()).norm();
00327                 spring2.ks = this->quadsStiffness.getValue() / spring2.initpos;
00328                 spring2.kd = this->quadsDamping.getValue() / spring2.initpos;
00329                 for (int z=0; z<nz-1; z++)
00330                     for (int y=0; y<ny; y++)
00331                         for (int x=0; x<nx-1; x++)
00332                         {
00333                             spring1.m1 = topology->point(x,y,z);
00334                             spring1.m2 = topology->point(x+1,y,z+1);
00335                             this->addSpringDForce(df1,dx1,df2,dx2, index++, spring1, kFactor, bFactor);
00336                             spring2.m1 = topology->point(x+1,y,z);
00337                             spring2.m2 = topology->point(x,y,z+1);
00338                             this->addSpringDForce(df1,dx1,df2,dx2, index++, spring2, kFactor, bFactor);
00339                         }
00340                 // quads along YZ plane
00341                 // lines (x,y,z) -> (x,y+1,z+1)
00342                 spring1.initpos = (topology->getDy()+topology->getDz()).norm();
00343                 spring1.ks = this->quadsStiffness.getValue() / spring1.initpos;
00344                 spring1.kd = this->quadsDamping.getValue() / spring1.initpos;
00345                 // lines (x,y+1,z) -> (x,y,z+1)
00346                 spring1.initpos = (topology->getDy()-topology->getDz()).norm();
00347                 spring1.ks = this->linesStiffness.getValue() / spring1.initpos;
00348                 spring1.kd = this->linesDamping.getValue() / spring1.initpos;
00349                 for (int z=0; z<nz-1; z++)
00350                     for (int y=0; y<ny-1; y++)
00351                         for (int x=0; x<nx; x++)
00352                         {
00353                             spring1.m1 = topology->point(x,y,z);
00354                             spring1.m2 = topology->point(x,y+1,z+1);
00355                             this->addSpringDForce(df1,dx1,df2,dx2, index++, spring1, kFactor, bFactor);
00356                             spring2.m1 = topology->point(x,y+1,z);
00357                             spring2.m2 = topology->point(x,y,z+1);
00358                             this->addSpringDForce(df1,dx1,df2,dx2, index++, spring2, kFactor, bFactor);
00359                         }
00360             }
00361             if (this->cubesStiffness.getValue() != 0.0 || this->cubesDamping.getValue() != 0.0)
00362             {
00363                 typename RegularGridSpringForceField<DataTypes>::Spring spring1;
00364                 typename RegularGridSpringForceField<DataTypes>::Spring spring2;
00365                 typename RegularGridSpringForceField<DataTypes>::Spring spring3;
00366                 typename RegularGridSpringForceField<DataTypes>::Spring spring4;
00367                 // lines (x,y,z) -> (x+1,y+1,z+1)
00368                 spring1.initpos = (topology->getDx()+topology->getDy()+topology->getDz()).norm();
00369                 spring1.ks = this->cubesStiffness.getValue() / spring1.initpos;
00370                 spring1.kd = this->cubesDamping.getValue() / spring1.initpos;
00371                 // lines (x+1,y,z) -> (x,y+1,z+1)
00372                 spring2.initpos = (-topology->getDx()+topology->getDy()+topology->getDz()).norm();
00373                 spring2.ks = this->cubesStiffness.getValue() / spring2.initpos;
00374                 spring2.kd = this->cubesDamping.getValue() / spring2.initpos;
00375                 // lines (x,y+1,z) -> (x+1,y,z+1)
00376                 spring3.initpos = (topology->getDx()-topology->getDy()+topology->getDz()).norm();
00377                 spring3.ks = this->cubesStiffness.getValue() / spring3.initpos;
00378                 spring3.kd = this->cubesDamping.getValue() / spring3.initpos;
00379                 // lines (x,y,z+1) -> (x+1,y+1,z)
00380                 spring4.initpos = (topology->getDx()+topology->getDy()-topology->getDz()).norm();
00381                 spring4.ks = this->cubesStiffness.getValue() / spring4.initpos;
00382                 spring4.kd = this->cubesDamping.getValue() / spring4.initpos;
00383                 for (int z=0; z<nz-1; z++)
00384                     for (int y=0; y<ny-1; y++)
00385                         for (int x=0; x<nx-1; x++)
00386                         {
00387                             spring1.m1 = topology->point(x,y,z);
00388                             spring1.m2 = topology->point(x+1,y+1,z+1);
00389                             this->addSpringDForce(df1,dx1,df2,dx2, index++, spring1, kFactor, bFactor);
00390                             spring2.m1 = topology->point(x+1,y,z);
00391                             spring2.m2 = topology->point(x,y+1,z+1);
00392                             this->addSpringDForce(df1,dx1,df2,dx2, index++, spring2, kFactor, bFactor);
00393                             spring3.m1 = topology->point(x,y+1,z);
00394                             spring3.m2 = topology->point(x+1,y,z+1);
00395                             this->addSpringDForce(df1,dx1,df2,dx2, index++, spring3, kFactor, bFactor);
00396                             spring4.m1 = topology->point(x,y,z+1);
00397                             spring4.m2 = topology->point(x+1,y+1,z);
00398                             this->addSpringDForce(df1,dx1,df2,dx2, index++, spring4, kFactor, bFactor);
00399                         }
00400             }
00401         }
00402     }
00403 }
00404 
00405 
00406 
00407 template<class DataTypes>
00408 void RegularGridSpringForceField<DataTypes>::draw()
00409 {
00410     if (!((this->mstate1 == this->mstate2)?this->getContext()->getShowForceFields():this->getContext()->getShowInteractionForceFields())) return;
00411     assert(this->mstate1);
00412     assert(this->mstate2);
00413     // Draw any custom springs
00414     this->StiffSpringForceField<DataTypes>::draw();
00415     // Compute topological springs
00416     const VecCoord& p1 = *this->mstate1->getX();
00417     const VecCoord& p2 = *this->mstate2->getX();
00418     glDisable(GL_LIGHTING);
00419     glColor4f(0.5,0.5,0.5,1);
00420     glBegin(GL_LINES);
00421     if (this->mstate1==this->mstate2)
00422     {
00423         if (topology != NULL)
00424         {
00425             const int nx = topology->getNx();
00426             const int ny = topology->getNy();
00427             const int nz = topology->getNz();
00428 
00429             if (this->linesStiffness.getValue() != 0.0 || this->linesDamping.getValue() != 0.0)
00430             {
00431                 typename RegularGridSpringForceField<DataTypes>::Spring spring;
00432                 // lines along X
00433                 spring.initpos = topology->getDx().norm();
00434                 spring.ks = this->linesStiffness.getValue() / spring.initpos;
00435                 spring.kd = this->linesDamping.getValue() / spring.initpos;
00436                 for (int z=0; z<nz; z++)
00437                     for (int y=0; y<ny; y++)
00438                         for (int x=0; x<nx-1; x++)
00439                         {
00440                             spring.m1 = topology->point(x,y,z);
00441                             spring.m2 = topology->point(x+1,y,z);
00442                             helper::gl::glVertexT(p1[spring.m1]);
00443                             helper::gl::glVertexT(p2[spring.m2]);
00444                         }
00445                 // lines along Y
00446                 spring.initpos = topology->getDy().norm();
00447                 spring.ks = this->linesStiffness.getValue() / spring.initpos;
00448                 spring.kd = this->linesDamping.getValue() / spring.initpos;
00449                 for (int z=0; z<nz; z++)
00450                     for (int y=0; y<ny-1; y++)
00451                         for (int x=0; x<nx; x++)
00452                         {
00453                             spring.m1 = topology->point(x,y,z);
00454                             spring.m2 = topology->point(x,y+1,z);
00455                             helper::gl::glVertexT(p1[spring.m1]);
00456                             helper::gl::glVertexT(p2[spring.m2]);
00457                         }
00458                 // lines along Z
00459                 spring.initpos = topology->getDz().norm();
00460                 spring.ks = this->linesStiffness.getValue() / spring.initpos;
00461                 spring.kd = this->linesDamping.getValue() / spring.initpos;
00462                 for (int z=0; z<nz-1; z++)
00463                     for (int y=0; y<ny; y++)
00464                         for (int x=0; x<nx; x++)
00465                         {
00466                             spring.m1 = topology->point(x,y,z);
00467                             spring.m2 = topology->point(x,y,z+1);
00468                             helper::gl::glVertexT(p1[spring.m1]);
00469                             helper::gl::glVertexT(p2[spring.m2]);
00470                         }
00471                 
00472             }
00473 #if 0
00474             if (this->quadsStiffness.getValue() != 0.0 || this->quadsDamping.getValue() != 0.0)
00475             {
00476                 typename RegularGridSpringForceField<DataTypes>::Spring spring1;
00477                 typename RegularGridSpringForceField<DataTypes>::Spring spring2;
00478                 // quads along XY plane
00479                 // lines (x,y,z) -> (x+1,y+1,z)
00480                 spring1.initpos = (topology->getDx()+topology->getDy()).norm();
00481                 spring1.ks = this->linesStiffness.getValue() / spring1.initpos;
00482                 spring1.kd = this->linesDamping.getValue() / spring1.initpos;
00483                 // lines (x+1,y,z) -> (x,y+1,z)
00484                 spring2.initpos = (topology->getDx()-topology->getDy()).norm();
00485                 spring2.ks = this->linesStiffness.getValue() / spring2.initpos;
00486                 spring2.kd = this->linesDamping.getValue() / spring2.initpos;
00487                 for (int z=0; z<nz; z++)
00488                     for (int y=0; y<ny-1; y++)
00489                         for (int x=0; x<nx-1; x++)
00490                         {
00491                             spring1.m1 = topology->point(x,y,z);
00492                             spring1.m2 = topology->point(x+1,y+1,z);
00493                             helper::gl::glVertexT(p1[spring1.m1]);
00494                             helper::gl::glVertexT(p2[spring1.m2]);
00495                             spring2.m1 = topology->point(x+1,y,z);
00496                             spring2.m2 = topology->point(x,y+1,z);
00497                             helper::gl::glVertexT(p1[spring2.m1]);
00498                             helper::gl::glVertexT(p2[spring2.m2]);
00499                         }
00500                 // quads along XZ plane
00501                 // lines (x,y,z) -> (x+1,y,z+1)
00502                 spring1.initpos = (topology->getDx()+topology->getDz()).norm();
00503                 spring1.ks = this->linesStiffness.getValue() / spring1.initpos;
00504                 spring1.kd = this->linesDamping.getValue() / spring1.initpos;
00505                 // lines (x+1,y,z) -> (x,y,z+1)
00506                 spring2.initpos = (topology->getDx()-topology->getDz()).norm();
00507                 spring2.ks = this->linesStiffness.getValue() / spring2.initpos;
00508                 spring2.kd = this->linesDamping.getValue() / spring2.initpos;
00509                 for (int z=0; z<nz-1; z++)
00510                     for (int y=0; y<ny; y++)
00511                         for (int x=0; x<nx-1; x++)
00512                         {
00513                             spring1.m1 = topology->point(x,y,z);
00514                             spring1.m2 = topology->point(x+1,y,z+1);
00515                             helper::gl::glVertexT(p1[spring1.m1]);
00516                             helper::gl::glVertexT(p2[spring1.m2]);
00517                             spring2.m1 = topology->point(x+1,y,z);
00518                             spring2.m2 = topology->point(x,y,z+1);
00519                             helper::gl::glVertexT(p1[spring2.m1]);
00520                             helper::gl::glVertexT(p2[spring2.m2]);
00521                         }
00522                 // quads along YZ plane
00523                 // lines (x,y,z) -> (x,y+1,z+1)
00524                 spring1.initpos = (topology->getDy()+topology->getDz()).norm();
00525                 spring1.ks = this->linesStiffness.getValue() / spring1.initpos;
00526                 spring1.kd = this->linesDamping.getValue() / spring1.initpos;
00527                 // lines (x,y+1,z) -> (x,y,z+1)
00528                 spring1.initpos = (topology->getDy()-topology->getDz()).norm();
00529                 spring1.ks = this->linesStiffness.getValue() / spring1.initpos;
00530                 spring1.kd = this->linesDamping.getValue() / spring1.initpos;
00531                 for (int z=0; z<nz-1; z++)
00532                     for (int y=0; y<ny-1; y++)
00533                         for (int x=0; x<nx; x++)
00534                         {
00535                             spring1.m1 = topology->point(x,y,z);
00536                             spring1.m2 = topology->point(x,y+1,z+1);
00537                             helper::gl::glVertexT(p1[spring1.m1]);
00538                             helper::gl::glVertexT(p2[spring1.m2]);
00539                             spring2.m1 = topology->point(x,y+1,z);
00540                             spring2.m2 = topology->point(x,y,z+1);
00541                             helper::gl::glVertexT(p1[spring2.m1]);
00542                             helper::gl::glVertexT(p2[spring2.m2]);
00543                         }
00544             }
00545             if (this->quadsStiffness.getValue() != 0.0 || this->quadsDamping.getValue() != 0.0)
00546             {
00547                 typename RegularGridSpringForceField<DataTypes>::Spring spring1;
00548                 typename RegularGridSpringForceField<DataTypes>::Spring spring2;
00549                 typename RegularGridSpringForceField<DataTypes>::Spring spring3;
00550                 typename RegularGridSpringForceField<DataTypes>::Spring spring4;
00551                 // lines (x,y,z) -> (x+1,y+1,z+1)
00552                 spring1.initpos = (topology->getDx()+topology->getDy()+topology->getDz()).norm();
00553                 spring1.ks = this->linesStiffness.getValue() / spring1.initpos;
00554                 spring1.kd = this->linesDamping.getValue() / spring1.initpos;
00555                 // lines (x+1,y,z) -> (x,y+1,z+1)
00556                 spring2.initpos = (-topology->getDx()+topology->getDy()+topology->getDz()).norm();
00557                 spring2.ks = this->linesStiffness.getValue() / spring2.initpos;
00558                 spring2.kd = this->linesDamping.getValue() / spring2.initpos;
00559                 // lines (x,y+1,z) -> (x+1,y,z+1)
00560                 spring3.initpos = (topology->getDx()-topology->getDy()+topology->getDz()).norm();
00561                 spring3.ks = this->linesStiffness.getValue() / spring3.initpos;
00562                 spring3.kd = this->linesDamping.getValue() / spring3.initpos;
00563                 // lines (x,y,z+1) -> (x+1,y+1,z)
00564                 spring4.initpos = (topology->getDx()+topology->getDy()-topology->getDz()).norm();
00565                 spring4.ks = this->linesStiffness.getValue() / spring4.initpos;
00566                 spring4.kd = this->linesDamping.getValue() / spring4.initpos;
00567                 for (int z=0; z<nz-1; z++)
00568                     for (int y=0; y<ny-1; y++)
00569                         for (int x=0; x<nx-1; x++)
00570                         {
00571                             spring1.m1 = topology->point(x,y,z);
00572                             spring1.m2 = topology->point(x+1,y+1,z+1);
00573                             helper::gl::glVertexT(p1[spring1.m1]);
00574                             helper::gl::glVertexT(p2[spring1.m2]);
00575                             spring2.m1 = topology->point(x+1,y,z);
00576                             spring2.m2 = topology->point(x,y+1,z+1);
00577                             helper::gl::glVertexT(p1[spring2.m1]);
00578                             helper::gl::glVertexT(p2[spring2.m2]);
00579                             spring3.m1 = topology->point(x,y+1,z);
00580                             spring3.m2 = topology->point(x+1,y,z+1);
00581                             helper::gl::glVertexT(p1[spring3.m1]);
00582                             helper::gl::glVertexT(p2[spring3.m2]);
00583                             spring4.m1 = topology->point(x,y,z+1);
00584                             spring4.m2 = topology->point(x+1,y+1,z);
00585                             helper::gl::glVertexT(p1[spring4.m1]);
00586                             helper::gl::glVertexT(p2[spring4.m2]);
00587                         }
00588             }
00589 #endif
00590         }
00591     }
00592     glEnd();
00593 }
00594 
00595 } // namespace forcefield
00596 
00597 } // namespace component
00598 
00599 } // namespace sofa
00600 
00601 #endif

Generated on Sun Nov 23 03:04:54 2008 for Sofa by  doxygen 1.5.7.1