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/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(¢er,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(¢er,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
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
00096 Vector3 p(x*2*r/(nx-1) - r, y*2*r/(ny-1) - r, 0);
00097
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 }
00106
00107 }
00108
00109 }
00110