Skip to content

Commit

Permalink
Merge pull request #164 from epernod/SphereGridTopology
Browse files Browse the repository at this point in the history
[GeneralTopology] Grid Topologies cleanup + new SphereGrid
  • Loading branch information
matthieu-nesme authored Mar 8, 2017
2 parents 689399b + b0f40e5 commit 8410f29
Show file tree
Hide file tree
Showing 23 changed files with 1,029 additions and 310 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@

### New features for users
* [SceneCreator]
* New methods to add basic 3D object: Cube, Cylinder, Plane. In rigid or deformable.
* New methods to add basic 3D object: Cube, Cylinder, Sphere and Plane. In rigid or deformable.

* [GeneralTopology]
* SphereGridTopology component to create sphere grids, similar to CylinderGridTopology.

- Adds a new orientedBox dataField in BoxROI so that we can use it to either defined AABoxes or OrientedBox

Expand Down
185 changes: 146 additions & 39 deletions SofaKernel/modules/SofaBaseTopology/GridTopology.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <SofaBaseTopology/GridTopology.h>
#include <sofa/core/visual/VisualParams.h>
#include <sofa/core/ObjectFactory.h>
#include <sofa/defaulttype/Vec.h>

namespace sofa
{
Expand All @@ -43,7 +44,7 @@ int GridTopologyClass = core::RegisterObject("Base class fo a regular grid in 3D
GridTopology::GridUpdate::GridUpdate(GridTopology *t):
topology(t)
{
addInput(&t->n);
addInput(&t->d_n);
addOutput(&t->seqEdges);
addOutput(&t->seqQuads);
addOutput(&t->seqHexahedra);
Expand All @@ -60,7 +61,7 @@ void GridTopology::GridUpdate::update()
void GridTopology::GridUpdate::updateEdges()
{
SeqEdges& edges = *topology->seqEdges.beginEdit();
const Vec3i& n = topology->n.getValue();
const Vec3i& n = topology->d_n.getValue();
edges.clear();
edges.reserve( (n[0]-1)*n[1]*n[2] +
n[0]*(n[1]-1)*n[2] +
Expand All @@ -86,7 +87,7 @@ void GridTopology::GridUpdate::updateEdges()
void GridTopology::GridUpdate::updateQuads()
{
SeqQuads& quads = *topology->seqQuads.beginEdit();
const Vec3i& n = topology->n.getValue();
const Vec3i& n = topology->d_n.getValue();
quads.clear();
quads.reserve((n[0]-1)*(n[1]-1)*n[2]+(n[0]-1)*n[1]*(n[2]-1)+n[0]*(n[1]-1)*(n[2]-1));
// quads along XY plane
Expand Down Expand Up @@ -120,7 +121,7 @@ void GridTopology::GridUpdate::updateQuads()
void GridTopology::GridUpdate::updateHexas()
{
SeqHexahedra& hexahedra = *topology->seqHexahedra.beginEdit();
const Vec3i& n = topology->n.getValue();
const Vec3i& n = topology->d_n.getValue();
hexahedra.clear();
hexahedra.reserve((n[0]-1)*(n[1]-1)*(n[2]-1));
for (int z=0; z<n[2]-1; z++)
Expand All @@ -141,67 +142,173 @@ void GridTopology::GridUpdate::updateHexas()
}

GridTopology::GridTopology()
: n(initData(&n,Vec3i(2,2,2),"n","grid resolution"))
, p_createTexCoords(initData(&p_createTexCoords, (bool)false, "createTexCoords", "If set to true, virtual texture coordinates will be generated using 3D interpolation."))
: d_n(initData(&d_n,Vec3i(2,2,2),"n","grid resolution"))
, d_computeHexaList(initData(&d_computeHexaList, true, "computeHexaList", "put true if the list of Hexahedra is needed during init"))
, d_computeQuadList(initData(&d_computeQuadList, true, "computeQuadList", "put true if the list of Quad is needed during init"))
, d_computeEdgeList(initData(&d_computeEdgeList, true, "computeEdgeList", "put true if the list of Lines is needed during init"))
, d_computePointList(initData(&d_computePointList, true, "computePointList", "put true if the list of Points is needed during init"))
, d_createTexCoords(initData(&d_createTexCoords, (bool)false, "createTexCoords", "If set to true, virtual texture coordinates will be generated using 3D interpolation."))
{
setSize();
setNbGridPoints();
GridUpdate::SPtr gridUpdate = sofa::core::objectmodel::New<GridUpdate>(this);
this->addSlave(gridUpdate);
}

GridTopology::GridTopology(int _nx, int _ny, int _nz)
: n(initData(&n,Vec3i(_nx,_ny,_nz),"n","grid resolution"))
, p_createTexCoords(initData(&p_createTexCoords, (bool)false, "createTexCoords", "If set to true, virtual texture coordinates will be generated using 3D interpolation."))
: d_n(initData(&d_n,Vec3i(_nx,_ny,_nz),"n","grid resolution"))
, d_computeHexaList(initData(&d_computeHexaList, true, "computeHexaList", "put true if the list of Hexahedra is needed during init"))
, d_computeQuadList(initData(&d_computeQuadList, true, "computeQuadList", "put true if the list of Quad is needed during init"))
, d_computeEdgeList(initData(&d_computeEdgeList, true, "computeEdgeList", "put true if the list of Lines is needed during init"))
, d_computePointList(initData(&d_computePointList, true, "computePointList", "put true if the list of Points is needed during init"))
, d_createTexCoords(initData(&d_createTexCoords, (bool)false, "createTexCoords", "If set to true, virtual texture coordinates will be generated using 3D interpolation."))
{
nbPoints = _nx*_ny*_nz;
this->n.setValue(Vec3i(_nx,_ny,_nz));
this->d_n.setValue(Vec3i(_nx,_ny,_nz));

checkGridResolution();
}

GridTopology::GridTopology( Vec3i np )
: n(initData(&n,np,"n","grid resolution"))
, p_createTexCoords(initData(&p_createTexCoords, (bool)false, "createTexCoords", "If set to true, virtual texture coordinates will be generated using 3D interpolation."))
: d_n(initData(&d_n,np,"n","grid resolution"))
, d_computeHexaList(initData(&d_computeHexaList, true, "computeHexaList", "put true if the list of Hexahedra is needed during init"))
, d_computeQuadList(initData(&d_computeQuadList, true, "computeQuadList", "put true if the list of Quad is needed during init"))
, d_computeEdgeList(initData(&d_computeEdgeList, true, "computeEdgeList", "put true if the list of Lines is needed during init"))
, d_computePointList(initData(&d_computePointList, true, "computePointList", "put true if the list of Points is needed during init"))
, d_createTexCoords(initData(&d_createTexCoords, (bool)false, "createTexCoords", "If set to true, virtual texture coordinates will be generated using 3D interpolation."))
{
nbPoints = np[0]*np[1]*np[2];
this->n.setValue(np);
this->d_n.setValue(np);

checkGridResolution();
}

void GridTopology::init()
{
if (p_createTexCoords.getValue())
// first check resolution
checkGridResolution();

if (d_createTexCoords.getValue())
this->createTexCoords();

this->reinit();
if (d_computeHexaList.getValue())
this->computeHexaList();

if (d_computeQuadList.getValue())
this->computeQuadList();

if (d_computeEdgeList.getValue())
this->computeEdgeList();

if (d_computePointList.getValue())
this->computePointList();

Inherit1::init();
}

void GridTopology::reinit()
{
checkGridResolution();
}

void GridTopology::setSize(int nx, int ny, int nz)
{
// std::cerr<<"GridTopology::setSize(int nx, int ny, int nz), n = "<< n.getValue() << std::endl;
if (nx == this->n.getValue()[0] && ny == this->n.getValue()[1] && nz == this->n.getValue()[2])
if (nx == this->d_n.getValue()[0] && ny == this->d_n.getValue()[1] && nz == this->d_n.getValue()[2])
return;
this->n.setValue(Vec3i(nx,ny,nz));
setSize();
this->d_n.setValue(Vec3i(nx,ny,nz));
setNbGridPoints();

checkGridResolution();
}

void GridTopology::setNumVertices(int nx, int ny, int nz)
void GridTopology::checkGridResolution()
{
setSize(nx,ny,nz);
const Vec3i& _n = d_n.getValue();
if (_n[0] < 2 || _n[1] < 2 || _n[2] < 2)
{
msg_warning(this) << "The grid resolution: ["<< _n[0] << " ; " << _n[1] << " ; " << _n[2] <<
"] is outside the validity range. At least a resolution of 2 is needed in each 3D direction."
" Continuing with default value=[2; 2; 2]."
" Set a valid grid resolution to remove this warning message.";
this->d_n.setValue(Vec3i(2,2,2));
setNbGridPoints();
computePointList();
}
}

void GridTopology::setNumVertices(Vec3i n)
void GridTopology::setSize(Vec3i n)
{
setSize(n[0],n[1],n[2]);
}

void GridTopology::setSize()
void GridTopology::setNbGridPoints()
{
this->setNbPoints(d_n.getValue()[0]*d_n.getValue()[1]*d_n.getValue()[2]);
}


void GridTopology::computeHexaList()
{
this->nbPoints = n.getValue()[0]*n.getValue()[1]*n.getValue()[2];
updateHexahedra();
}

void GridTopology::computeQuadList()
{
// updateQuads();
// const SeqQuads seq_quads= this->getQuads();
// sout<<"Init: Number of Quads ="<<seq_quads.size()<<sendl;
}

void GridTopology::computeEdgeList()
{
//updateEdges();
// const SeqLines seq_l=this->getLines();
// sout<<"Init: Number of Lines ="<<seq_l.size()<<sendl;
}

void GridTopology::computePointList()
{
int nbPoints= this->getNbPoints();
// put the result in seqPoints
SeqPoints& seq_P= *(seqPoints.beginEdit());
seq_P.resize(nbPoints);

for (int i=0; i<nbPoints; i++)
{
seq_P[i] = this->getPoint(i);
}

seqPoints.endEdit();
}

unsigned GridTopology::getIndex( int i, int j, int k ) const
{
return d_n.getValue()[0]* ( d_n.getValue()[1]*k + j ) + i;
}


sofa::defaulttype::Vector3 GridTopology::getPoint(int i) const
{
int x = i%d_n.getValue()[0]; i/=d_n.getValue()[0];
int y = i%d_n.getValue()[1]; i/=d_n.getValue()[1];
int z = i;

return getPointInGrid(x,y,z);
}

sofa::defaulttype::Vector3 GridTopology::getPointInGrid(int i, int j, int k) const
{
unsigned int id = this->getIndex(i, j, k);
if (id < seqPoints.getValue().size())
return seqPoints.getValue()[id];
else
return sofa::defaulttype::Vector3();
}


GridTopology::Hexa GridTopology::getHexaCopy(int i)
{
int x = i%(n.getValue()[0]-1); i/=(n.getValue()[0]-1);
int y = i%(n.getValue()[1]-1); i/=(n.getValue()[1]-1);
int x = i%(d_n.getValue()[0]-1); i/=(d_n.getValue()[0]-1);
int y = i%(d_n.getValue()[1]-1); i/=(d_n.getValue()[1]-1);
int z = i;
return getHexahedron(x,y,z);
}
Expand All @@ -223,38 +330,38 @@ GridTopology::Hexa GridTopology::getHexahedron(int x, int y, int z)

GridTopology::Quad GridTopology::getQuadCopy(int i)
{
if (n.getValue()[0] == 1)
if (d_n.getValue()[0] == 1)
{
int y = i%(n.getValue()[1]-1);
i/=(n.getValue()[1]-1);
int z = i%(n.getValue()[2]-1);
int y = i%(d_n.getValue()[1]-1);
i/=(d_n.getValue()[1]-1);
int z = i%(d_n.getValue()[2]-1);

return getQuad(1,y,z);
}
else if (n.getValue()[1] == 1)
else if (d_n.getValue()[1] == 1)
{
int x = i%(n.getValue()[0]-1);
i/=(n.getValue()[0]-1);
int z = i%(n.getValue()[2]-1);
int x = i%(d_n.getValue()[0]-1);
i/=(d_n.getValue()[0]-1);
int z = i%(d_n.getValue()[2]-1);

return getQuad(x,1,z);
}
else
{
int x = i%(n.getValue()[0]-1);
i/=(n.getValue()[0]-1);
int y = i%(n.getValue()[1]-1);
int x = i%(d_n.getValue()[0]-1);
i/=(d_n.getValue()[0]-1);
int y = i%(d_n.getValue()[1]-1);

return getQuad(x,y,1);
}
}

GridTopology::Quad GridTopology::getQuad(int x, int y, int z)
{
if (n.getValue()[2] == 1)
if (d_n.getValue()[2] == 1)
return Quad(point(x, y, 1), point(x+1, y, 1),
point(x+1, y+1, 1), point(x, y+1, 1));
else if (n.getValue()[1] == 1)
else if (d_n.getValue()[1] == 1)
return Quad(point(x, 1, z), point(x+1, 1, z),
point(x+1, 1, z+1), point(x, 1, z+1));
else
Expand Down
Loading

0 comments on commit 8410f29

Please sign in to comment.