CylinderGridTopology.cpp

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 #include <sofa/component/topology/CylinderGridTopology.h>
00026 #include <sofa/core/ObjectFactory.h>
00027 #include <sofa/helper/rmath.h>
00028 
00029 namespace sofa
00030 {
00031 
00032 namespace component
00033 {
00034 
00035 namespace topology
00036 {
00037 
00038 using namespace sofa::defaulttype;
00039 using std::cout;
00040 using std::endl;
00041 
00042 SOFA_DECL_CLASS(CylinderGridTopology)
00043 
00044 int CylinderGridTopologyClass = core::RegisterObject("Cylinder grid in 3D")
00045 .addAlias("CylinderGrid")
00046 .add< CylinderGridTopology >()
00047 ;
00048 
00049 CylinderGridTopology::CylinderGridTopology(int nx, int ny, int nz)
00050   : GridTopology(nx, ny, nz),
00051     center(initData(&center,Vector3(0.0f,0.0f,0.0f),"center", "Center of the cylinder")),
00052     axis(initData(&axis,Vector3(0.0f,0.0f,1.0f),"axis", "Main direction of the cylinder")),
00053     radius(initData(&radius,(SReal)1.0,"radius", "Radius of the cylinder")),
00054     length(initData(&length,(SReal)1.0,"length", "Length of the cylinder along its axis"))
00055 {
00056 }
00057 
00058 CylinderGridTopology::CylinderGridTopology()
00059   : center(initData(&center,Vector3(0.0f,0.0f,0.0f),"center", "Center of the cylinder")),
00060     axis(initData(&axis,Vector3(0.0f,0.0f,1.0f),"axis", "Main direction of the cylinder")),
00061     radius(initData(&radius,(SReal)1.0,"radius", "Radius of the cylinder")),
00062     length(initData(&length,(SReal)1.0,"length", "Length of the cylinder along its axis"))
00063 {
00064 }
00065 
00066 unsigned CylinderGridTopology::getIndex( int i, int j, int k ) const
00067 {
00068     return n.getValue()[0]* ( n.getValue()[1]*k + j ) + i;
00069 }
00070 
00071 Vector3 CylinderGridTopology::getPoint(int i) const
00072 {
00073     int x = i%n.getValue()[0]; i/=n.getValue()[0];
00074     int y = i%n.getValue()[1]; i/=n.getValue()[1];
00075     int z = i;
00076     return getPoint(x,y,z);
00077 }
00078 
00079 Vector3 CylinderGridTopology::getPoint(int x, int y, int z) const
00080 {
00081     //return p0+dx*x+dy*y+dz*z;
00082     SReal r = radius.getValue();
00083     SReal l = length.getValue();
00084     Vector3 axisZ = axis.getValue();
00085     axisZ.normalize();
00086     Vector3 axisX = ((axisZ-Vector3(1,0,0)).norm() < 0.000001 ? Vector3(0,1,0) : Vector3(1,0,0));
00087     Vector3 axisY = cross(axisZ,axisX);
00088     axisX = cross(axisY,axisZ);
00089     axisX.normalize();
00090     axisY.normalize();
00091     axisZ.normalize();
00092     int nx = getNx();
00093     int ny = getNy();
00094     int nz = getNz();
00095     // coordonate on a square
00096     Vector3 p(x*2*r/(nx-1) - r, y*2*r/(ny-1) - r, 0);
00097     // scale it to be on a circle
00098     if (p.norm() > 0.0000001)
00099         p *= helper::rmax(helper::rabs(p[0]),helper::rabs(p[1]))/p.norm();
00100     if (nz>1)
00101         p[2] = z*l/(nz-1);
00102     return center.getValue()+axisX*p[0] + axisY*p[1] + axisZ * p[2];
00103 }
00104 
00105 } // namespace topology
00106 
00107 } // namespace component
00108 
00109 } // namespace sofa
00110 

Generated on Sun Nov 23 03:42:46 2008 for Sofa by  doxygen 1.5.7.1