36 #ifndef particlegrid_h 37 #define particlegrid_h 48 template<
class Po
int >
56 template <
class Po
int>
90 int giveIndex (
const IntArray &pos )
const;
96 void givePosition (
IntArray &pos,
int index )
const;
131 int getNumberOfPoints()
const;
133 bool isEmpty()
const;
145 return this->res ( i );
153 return this->dx ( i );
167 void getGridCoord (
FloatArray &answer,
int ind )
const;
175 bool getPoint ( Point *&answer,
const IntArray &pos )
const;
176 bool getPoint ( Point *&answer,
int ind )
const;
201 void setPoint ( Point *point,
const IntArray &pos );
202 void setPoint ( Point *point,
int ind );
208 void clearPosition (
const IntArray &pos );
209 void clearPosition (
int ind );
212 void appendAllPoints ( std::list<Point*> &answer );
230 std :: string
errorInfo(
const char *func)
const {
return std :: string(
"ParticleGrid :: ") + func; }
235 template <
class Po
int>
238 this->init ( res, bb0, bb1 );
241 template <
class Po
int>
244 this->init ( old->
res, old->
bb0, old->
bb1 );
247 template <
class Po
int>
255 this->dx.resize ( this->n );
257 this->res_prod.resize ( this->n );
258 this->res_prod ( 0 ) = 1;
260 for (
int i = 0; i < this->n; ++i ) {
261 this->dx ( i ) = ( this->bb1 ( i ) - this->bb0 ( i ) ) / ( this->res ( i )-1 );
263 this->total *= this->res ( i );
265 this->res_prod ( i ) = 1;
266 for (
int j = 0; j < i; ++j ) {
267 this->res_prod ( i ) *= this->res ( j );
272 for (
int i = 0; i < this->total; ++i ) {
273 this->data[i] = NULL;
277 template <
class Po
int>
281 for (
int i = 0; i < this->total; ++i ) {
282 this->clearPosition ( i );
287 template <
class Po
int>
292 for (
int i = 0; i < this->total; ++i ) {
298 c += p->
subgrid->getNumberOfPoints();
305 template <
class Po
int>
309 for (
int i = 0; i < this->total; ++i ) {
315 if ( !p->
subgrid->isEmpty() ) {
324 template <
class Po
int>
328 for (
int i = 0; i < this->n; ++i ) {
329 index += this->res_prod ( i ) *pos ( i );
334 template <
class Po
int>
338 for (
int i = 0; i < this->n; ++i ) {
339 pos ( i ) = ( index/res_prod ( i ) ) % res ( i );
343 template <
class Po
int>
346 answer.
resize ( this->n );
347 for (
int i = 0; i < this->n; ++i ) {
348 answer ( i ) = pos ( i ) *this->dx ( i ) + this->bb0 ( i );
352 template <
class Po
int>
356 this->givePosition ( pos, ind );
357 this->getGridCoord ( answer, pos );
360 template <
class Po
int>
365 for (
int i = 0; i < this->n; ++i ) {
366 ind0 ( i ) =
max ( (
int ) floor ( ( x0 ( i ) - this->bb0 ( i ) ) /this->dx ( i ) ), 0 );
367 ind1 ( i ) =
min ( (
int ) ceil ( ( x1 ( i ) - this->bb0 ( i ) ) /this->dx ( i ) ), this->res ( i ) );
371 template <
class Po
int>
375 for (
int i = 0; i < this->n; ++i ) {
377 pos ( i ) = ( int ) floor ( ( x ( i ) - this->bb0 ( i ) ) /this->dx ( i ) + 0.5 );
378 pos ( i ) =
min (
max ( pos ( i ),0 ),this->res ( i )-1 );
382 template <
class Po
int>
389 template <
class Po
int>
393 this->getBoundingBox ( x0, x1, ind0, ind1 );
398 template <
class Po
int>
405 this->getBoundingBox ( x0, x1, p0, p1 );
409 if ( this->n == 2 ) {
410 for (
int x = p0 ( 0 ); x < p1 ( 0 ); x++ ) {
412 for (
int y = p0 ( 1 ); y < p1 ( 1 ); y++ ) {
417 rp->
subgrid->appendAllPoints ( answer );
419 answer.push_front ( rp->
point );
424 }
else if ( this->n == 3 ) {
429 template <
class Po
int>
432 for (
int i = 0; i < this->total; ++i ) {
436 rp->
subgrid->appendAllPoints ( answer );
437 }
else if ( rp->
point ) {
438 answer.push_front ( rp->
point );
440 OOFEM_ERROR (
"Refined particle does not contain subgrid or point." );
446 template <
class Po
int>
449 return this->getPoint ( answer, this->giveIndex ( pos ) );
452 template <
class Po
int>
455 this->setPoint ( point, this->giveIndex ( pos ) );
458 template <
class Po
int>
476 template <
class Po
int>
479 this->clearPosition ( ind );
481 this->data[ind]->
point = point;
482 this->data[ind]->subgrid = NULL;
485 template <
class Po
int>
488 return this->clearPosition ( this->giveIndex ( pos ) );
491 template <
class Po
int>
504 this->data[ind] = NULL;
507 template <
class Po
int>
525 template <
class Po
int>
528 return this->getSubGrid ( subgrid, this->giveIndex ( pos ) );
531 template <
class Po
int>
536 this->getGridCoord ( grid_point, pos );
538 sub_bb0 = grid_point;
539 sub_bb0.
add ( -0.5, dx );
540 sub_bb1 = grid_point;
541 sub_bb1.
add ( 0.5, dx );
544 int ind = this->giveIndex ( pos );
545 this->clearPosition ( ind );
547 this->data[ind]->
point = NULL;
548 this->data[ind]->subgrid = subgrid;
554 template <
class Po
int>
569 index ( 0 ), endind ( 0 ), sub_it ( NULL ), grid ( NULL ), ind0(), ind1(), limited ( false ) {}
572 index ( 0 ), endind ( g->total ), sub_it ( NULL ), grid ( g ), ind0(), ind1(), limited ( false ) {
573 if ( this->grid->
data[this->index] && this->grid->data[this->index]->subgrid ) {
579 index ( 0 ), sub_it ( NULL ), grid ( g ), ind0 ( ind0 ), ind1 ( ind1 ), limited ( true ) {
582 this->endind = this->grid->
giveIndex ( end ) + 1;
584 this->index = this->grid->
giveIndex ( ind0 );
585 if ( this->grid->
data[this->index] && this->grid->data[this->index]->subgrid ) {
591 index ( x.index ), sub_it ( x.sub_it ), grid ( x.grid ), ind0 ( x.ind0 ), ind1 ( x.ind1 ), limited ( x.limited ) { }
594 if ( this->sub_it ) {
600 return this->index >= this->endind;
607 if ( this->subGridActive() ) {
610 if ( this->grid->
data[this->index] != NULL ) {
611 return this->grid->
data[this->index]->point;
622 OOFEM_ERROR (
"Can't set element in outside grid" );
624 if ( this->subGridActive() ) {
627 this->grid->
setPoint ( p, this->index );
631 return this->sub_it != NULL;
635 if ( this->subGridActive() ) {
644 if ( this->subGridActive() ) {
646 if ( !this->sub_it->
end() ) {
654 if ( this->limited ) {
656 for (
int i = 0; i < this->pos.
giveSize() - 1; ++i ) {
657 if ( this->pos ( i ) >= this->ind1 ( i ) ) {
658 this->pos ( i ) = this->ind0 ( i );
659 this->pos ( i+1 ) ++;
666 if ( !this->end() && this->grid->
data[this->index] && this->grid->data[this->index]->subgrid ) {
671 std :: string
errorInfo(
const char *func)
const {
return std :: string(
"ParticleGridIterator :: ") + func; }
677 #endif // particlegrid_h int getResolution(int i)
Returns the resolution in given dimension.
bool getSubGrid(ParticleGrid< Point > *&subgrid, const IntArray &pos) const
Gives the sub-grid at the position.
void getBoundingBox(const FloatArray &x0, const FloatArray &x1, IntArray &ind0, IntArray &ind1) const
Finds the indices [ind0, ind1) of the bounding box [x0,x1).
A recursive iterator for a grid with refinements.
int max(int i, int j)
Returns bigger value form two given decimals.
ParticleGridIterator(ParticleGrid< Point > *g, IntArray ind0, IntArray ind1)
Constructor.
void getGridPoint(FloatArray &x)
IntArray res_prod
Helper for index <-> position, the cumulative product of res.
ParticleGridIterator()
Constructor.
FloatArray dx
Grid increments.
int getTotal() const
Total number potential points.
bool subGridActive() const
int getNumberOfPoints() const
Number of active points.
RefinedParticlePoint ** data
Particle data structure.
Particle grid data structure for n-D grids.
Class implementing an array of integers.
void appendAllPoints(std::list< Point * > &answer)
double getGridStep(int i) const
Returns the grid increment.
int giveDimensions()
Returns Number of dimensions of grid.
void createSubGrid(ParticleGrid< Point > *&subgrid, const IntArray &res, const IntArray &pos)
Creates a 2 by 2 sub-grid at the given position.
ParticleGridIterator(ParticleGrid< Point > *g)
Constructor.
ParticleGridIterator< Point > iterator
List iterator type.
int n
Number of dimensions of the grid.
ParticleGridIterator(const ParticleGridIterator< Point > &x)
Copy constructor.
iterator begin()
Returns an iterator over the entire grid (with subgrids)
Recursive data structure for.
void resize(int n)
Checks size of receiver towards requested bounds.
std::string errorInfo(const char *func) const
void add(int val)
Adds given scalar to all values of receiver.
Class representing vector of real numbers.
void givePosition(IntArray &pos, int index) const
Translates from total position to n-D position.
void operator++()
Lets the iterator step forward to the next element.
FloatArray bb0
Bounding boxes.
void getPosition(const FloatArray &x, IntArray &pos) const
Finds the position closest to coordinate x.
int giveIndex(const IntArray &pos) const
Translates from n-D position to total position.
IntArray res
Resolution in each dimension.
~ParticleGrid()
Destructor.
void setPoint(Point *point, const IntArray &pos)
Sets a point in the grid.
std::string errorInfo(const char *func) const
void getGridCoord(FloatArray &answer, const IntArray &pos) const
Gives the real coordinates for the given identifier.
void getPointsWithin(std::list< Point * > &answer, const FloatArray &x0, const FloatArray &x1)
int total
Total grid points.
int min(int i, int j)
Returns smaller value from two given decimals.
void init(const IntArray &res, const FloatArray &bb0, const FloatArray &bb1)
Initiation of structure, used by constructors.
ParticleGrid< Point > * grid
the oofem namespace is to define a context or scope in which all oofem names are defined.
ParticleGrid< Point > * subgrid
bool isEmpty() const
Returns true if the entire grid is empty.
void add(const FloatArray &src)
Adds array src to receiver.
bool getPoint(Point *&answer, const IntArray &pos) const
Gives the point at the position.
iterator beginAt(const FloatArray &x0, const FloatArray &x1)
Returns an iterator over specified region.
void clearPosition(const IntArray &pos)
Deletes any preexisting data at position.
ParticleGrid(const IntArray &res, const FloatArray &bb0, const FloatArray &bb1)
Creates a new empty particle grid.
void resize(int s)
Resizes receiver towards requested size.
ParticleGridIterator< Point > * sub_it
~ParticleGridIterator()
Destructor.