5 #ifndef BALL_DATATYPE_HASHGRID_H
6 #define BALL_DATATYPE_HASHGRID_H
12 #ifndef BALL_CONCEPT_FORWARDITERATOR_H
16 #ifndef BALL_CONCEPT_VISITOR_H
20 #ifndef BALL_CONCEPT_PROCESSOR_H
24 #ifndef BALL_MATHS_VECTOR3_H
28 #ifdef BALL_HAS_GNU_SLIST
58 template <
typename Item>
100 Item*
find(
const Item &item);
103 const Item*
find(
const Item& item)
const;
113 void insert(
const Item& item);
120 bool remove(
const Item& item);
151 bool has(
const Item& item)
const;
166 void dump(std::ostream& s = std::cout,
Size depth = 0)
const;
204 bound_->getIndices(x_, y_, z_);
208 : bound_(traits.bound_),
209 position_(traits.position_)
225 return (bound_ == 0);
240 return (position_ == traits.position_);
245 return (position_ != traits.position_);
250 return (bound_ != 0 && position_ < 27);
267 return (position_ == 0);
278 return (position_ == 27);
295 while ( position_ < 27
354 #ifdef BALL_HAS_GNU_SLIST
378 position_(bound_->
data.begin())
383 : bound_(traits.bound_),
384 position_(traits.position_)
390 bound_ = traits.bound_;
391 position_ = traits.position_;
407 return (bound_ == 0);
422 return (position_ == traits.position_);
427 return (position_ != traits.position_);
432 return (bound_ != 0 && position_ != bound_->data.end());
438 position_ = bound_->data.end();
443 position_ = bound_->data.begin();
448 return (position_ == bound_->data.begin());
453 position_ = bound_->data.end();
458 return (position_ == bound_->data.end());
532 template<
typename Item>
538 template<
typename Item>
544 template<
typename Item>
551 template<
typename Item>
557 template<
typename Item>
561 parent->getIndices(*
this, x, y, z);
564 template<
typename Item>
567 typename DataContainer::iterator found = std::find(data.begin(), data.end(), item);
569 if (found != data.end())
577 template<
typename Item>
584 template<
typename Item>
590 template<
typename Item>
594 data.push_front(item);
597 template<
typename Item>
600 #ifdef BALL_HAS_GNU_SLIST
606 if(*data.begin() == item)
614 for(++pos; pos != data.end(); ++pos)
618 data.erase_after(prev);
628 if (pos != data.end())
639 template<
typename Item>
647 template <
typename Item>
651 visitor.visit(*
this);
654 template<
typename Item>
657 return (data == box.
data);
660 template<
typename Item>
664 return !(*
this == box);
667 template<
typename Item>
671 return (std::find(data.begin(), data.end(), item) != data.end());
674 template<
typename Item>
681 template<
typename Item>
688 template<
typename Item>
696 s <<
" size: " << getSize() << std::endl;
699 s <<
" data:" << std::endl;
700 for (
typename DataContainer::const_iterator d_it = data.begin(); d_it != data.end(); ++d_it)
703 s <<
" " << *d_it << std::endl;
709 template <
typename Item>
712 if (processor.
start() ==
false)
719 for (
typename DataContainer::iterator d_it = data.begin(); d_it != data.end(); ++d_it)
721 result = processor(*d_it);
729 return processor.
finish();
732 template <
typename Item>
735 if (processor.start() ==
false)
744 result = processor(*it);
752 return processor.finish();
759 template <
typename Item>
784 Size dimension_z,
float spacing_x,
float spacing_y,
float spacing_z);
790 Size dimension_z,
float spacing);
801 HashGrid3(const
Vector3& origin, const
Vector3& size,
float spacing);
804 HashGrid3(const HashGrid3& grid,
bool deep = true);
807 virtual ~HashGrid3();
810 virtual
void clear();
816 void clear(const
Vector3 &vector);
825 void destroy(const
Vector3& vector);
840 void set(const HashGrid3& grid,
bool deep = true);
843 const HashGrid3& operator = (const HashGrid3& grid);
849 void get(HashGrid3& grid,
bool deep = true) const;
866 const
Vector3& getOrigin() const;
872 const
Vector3& getUnit() const;
925 bool operator == (const HashGrid3& grid) const;
928 bool operator != (const HashGrid3& grid) const;
942 virtual
void dump(std::ostream& s = std::cout,
Size depth = 0) const;
982 virtual ~BoxIteratorTraits() {}
997 : bound_(traits.bound_),
998 position_(traits.position_)
1004 bound_ = traits.bound_;
1005 position_ = traits.position_;
1021 return (bound_ == 0);
1036 return (position_ == traits.position_);
1041 return (position_ != traits.position_);
1046 return (bound_ != 0 && position_ < bound_->
getSize());
1052 position_ = bound_->getSize()+1;
1062 return (position_ == 0);
1067 position_ = bound_->getSize();
1072 return (position_ == bound_->getSize());
1077 return bound_->box_[position_];
1082 return bound_->box_[position_];
1153 vector<HashGridBox3<Item> > box_;
1160 template <
typename Item>
1170 template <
typename Item>
1174 float spacing_x,
float spacing_y,
float spacing_z)
1175 : origin_(originvector),
1176 unit_(spacing_x, spacing_y, spacing_z),
1177 dimension_x_(dimension_x),
1178 dimension_y_(dimension_y),
1179 dimension_z_(dimension_z),
1184 template <
typename Item>
1187 Size dimension_x,
Size dimension_y,
Size dimension_z,
float spacing)
1189 unit_(spacing, spacing, spacing),
1190 dimension_x_(dimension_x),
1191 dimension_y_(dimension_y),
1192 dimension_z_(dimension_z),
1198 template <
typename Item>
1202 unit_(spacing, spacing, spacing),
1203 dimension_x_((
Size)(size.x / spacing + 1.0)),
1204 dimension_y_((
Size)(size.y / spacing + 1.0)),
1205 dimension_z_((
Size)(size.z / spacing + 1.0)),
1206 box_(dimension_x_ * dimension_y_ * dimension_z_,
HashGridBox3<Item>(this))
1210 template <
typename Item>
1216 template <
typename Item>
1221 template <
typename Item>
1224 Size size = dimension_x_ * dimension_y_ * dimension_z_;
1228 box_[index].clear();
1232 template <
typename Item>
1244 template <
typename Item>
1256 template <
typename Item>
1263 template <
typename Item>
1270 template <
typename Item>
1277 template <
typename Item>
1282 origin_.set(origin);
1284 dimension_x_ = dimension_x;
1285 dimension_y_ = dimension_y;
1286 dimension_z_ = dimension_z;
1290 template <
typename Item>
1293 origin_.set(origin);
1294 unit_.set(unit, unit, unit);
1295 dimension_x_ = size;
1296 dimension_y_ = size;
1297 dimension_z_ = size;
1301 template <
typename Item>
1304 origin_.set(grid.origin_);
1305 unit_.set(grid.unit_);
1306 dimension_x_ = grid.dimension_x_;
1307 dimension_y_ = grid.dimension_y_;
1308 dimension_z_ = grid.dimension_z_;
1311 for(
Position i = 0; i < box_.size(); ++i)
1313 box_[i].setParent(
this);
1317 template <
typename Item>
1326 template <
typename Item>
1329 Size& dimension_x,
Size& dimension_y,
Size& dimension_z)
const
1331 origin.
set(origin_);
1333 dimension_x = dimension_x_;
1334 dimension_y = dimension_y_;
1335 dimension_z = dimension_z_;
1338 template <
typename Item>
1342 grid.
set(*
this, deep);
1345 template <
typename Item>
1353 if (!box_[i].isEmpty())
1360 template <
typename Item>
1364 return (dimension_x_ * dimension_y_ * dimension_z_);
1367 template <
typename Item>
1374 template <
typename Item>
1381 template <
typename Item>
1388 template <
typename Item>
1395 template <
typename Item>
1399 return dimension_x_;
1402 template <
typename Item>
1406 return dimension_y_;
1409 template <
typename Item>
1413 return dimension_z_;
1416 template <
typename Item>
1423 getIndices(*box, x, y, z);
1425 const Item* item = 0;
1426 float distance = FLT_MAX;
1431 const Index xn = x + xi;
1434 const Index yn = y + yi;
1439 if (box_ptr != 0 && !box_ptr->
isEmpty())
1442 for (;hit != box_ptr->
endData(); hit++)
1444 const float new_dist = ((*hit)->getPosition() - point).getSquareLength();
1445 if (new_dist < distance)
1448 distance = new_dist;
1459 template <
typename Item>
1466 return pow((
double)((size.
x * size.
y * size.
z) / nr_boxes), (
double)(1.0 / 3.0));
1469 template <
typename Item>
1481 return &(box_[x * dimension_y_ * dimension_z_ + y * dimension_z_ + z]);
1485 template <
typename Item>
1489 return ((
HashGrid3*)
this)->getBox(x, y, z);
1492 template <
typename Item>
1496 float x = (vector.
x - origin_.x) / unit_.x;
1497 float y = (vector.
y - origin_.y) / unit_.y;
1498 float z = (vector.
z - origin_.z) / unit_.z;
1505 return getBox(x1, y1, z1);
1508 template <
typename Item>
1512 return ((
HashGrid3 *)
this)->getBox(vector);
1515 template <
typename Item>
1521 Index index = getIndex_(box);
1530 x = index / (dimension_y_ * dimension_z_);
1531 index -= x * dimension_y_ * dimension_z_;
1532 y = index / dimension_z_;
1533 z = index - y * dimension_z_;
1538 template <
typename Item>
1551 template <
typename Item>
1563 template <
typename Item>
1571 return box->
remove(item);
1577 template <
typename Item>
1585 return box->
remove(item);
1591 template <
typename Item>
1595 visitor.visit(*
this);
1598 template <
typename Item>
1602 if (getSize() != grid.
getSize()
1603 || origin_ != grid.origin_
1604 || unit_ != grid.unit_
1605 || dimension_x_ != grid.dimension_x_
1606 || dimension_y_ != grid.dimension_y_
1607 || dimension_z_ != grid.dimension_z_)
1612 return box_ == grid.box_;
1615 template <
typename Item>
1619 return !(*
this == grid);
1622 template <
typename Item>
1626 return (getSize() == 0);
1629 template <
typename Item>
1632 Size size = getSize();
1636 if (box_[index].isValid() ==
false)
1645 template <
typename Item>
1653 s <<
" origin: " << origin_ << std::endl;
1656 s <<
" unit: " << unit_.z << std::endl;
1659 s <<
" dimension: " << dimension_x_ <<
" "
1660 << dimension_y_ <<
" "
1661 << dimension_z_ << std::endl;
1663 Size size = getSize();
1665 s <<
" size: " << size << std::endl;
1668 s <<
" non empty boxes: " << countNonEmptyBoxes() << std::endl;
1671 s <<
" boxes:" << std::endl;
1676 getIndices(box_[index], x, y, z);
1677 s <<
" " << index <<
". box: ("
1678 << x <<
',' << y <<
',' << z <<
')' << std::endl;
1679 box_[index].dump(s, 1);
1683 s <<
" non-empty boxes:" << std::endl;
1687 if (!box_[i].isEmpty())
1688 s <<
" " << getIndex_(box_[i]) << std::endl;
1693 template <
typename Item>
1696 if (processor.
start() ==
false)
1707 result = processor(*item);
1716 return processor->
finish();
1719 template <
typename Item>
1722 if (processor.start() ==
false)
1732 result = processor(*box);
1740 return processor->finish();
1743 template <
typename Item>
1747 if ((&box < &box_[0]) || (&box - &box_[0] >= getSize()))
1753 return (
Index)(&box - &box_[0]);
1758 #endif // BALL_DATATYPE_HASHGRID_H
std::list< Item > DataContainer
const HashGridBox3 * getContainer() const
const HashGrid3 * getContainer() const
bool operator!=(const DataIteratorTraits &traits) const
ForwardIterator< HashGrid3< Item >, HashGridBox3< Item >, BoxIteratorPosition, BoxIteratorTraits > BoxIterator
HashGridBox3< Item > * getBox(Position x, Position y, Position z)
Return the HashGridBox3 at position (x, y, z)
#define BALL_CREATE(name)
bool apply(UnaryProcessor< Item > &processor)
const BoxIteratorPosition & getPosition() const
Position BoxIteratorPosition
ConstBoxIterator beginBox() const
Size countNonEmptyBoxes() const
Counts the non-empty boxes of a grid.
void set(const Vector3 &origin, const Vector3 &unit, Size dimension_x, Size dimension_y, Size dimension_z)
assigns the content of a hash grid (obsolete)
ConstBoxIterator endBox() const
ConstForwardIterator< HashGridBox3< Item >, HashGridBox3< Item >, BoxIteratorPosition, BoxIteratorTraits > ConstBoxIterator
This is the const version of BoxIterator .
ConstDataIterator beginData() const
bool operator==(const BoxIteratorTraits &traits) const
friend class BoxIteratorTraits
Size getSizeY() const
Get the y dimension of the grid.
ConstForwardIterator< HashGridBox3< Item >, Item, DataIteratorPosition, DataIteratorTraits > ConstDataIterator
BoxIteratorTraits(const HashGridBox3 &box)
void clear()
Clears the grid box.
bool operator!=(const BoxIteratorTraits &traits) const
Size getSizeZ() const
Get the z dimension of the grid.
virtual void clear()
Clears the whole grid.
ConstDataIterator endData() const
HashGridBox3 * getContainer()
bool has(const Item &item) const
const BoxIteratorPosition & getPosition() const
bool getIndices(const HashGridBox3< Item > &box, Position &x, Position &y, Position &z) const
Get the position indices of a HashGridBox3.
Vector3 & getUnit()
Returns the unit of the grid.
bool operator==(const DataIteratorTraits &traits) const
DataContainer::iterator DataIteratorPosition
BoxIteratorPosition & getPosition()
bool operator==(const HashGridBox3 &box) const
Equality operator.
BoxIteratorPosition & getPosition()
HashGridBox3 * getContainer()
static ConstForwardIterator end(const Container &container)
const DataIteratorPosition & getPosition() const
bool apply(UnaryProcessor< Item > &processor)
const Item & getData() const
bool remove(const Item &item)
DataIteratorTraits(const DataIteratorTraits &traits, bool=true)
const HashGridBox3< Item > & getData() const
friend class DataIteratorTraits
void host(Visitor< HashGrid3 > &visitor)
const HashGrid3 & operator=(const HashGrid3 &grid)
Assignment operator.
DataIteratorPosition & getPosition()
virtual bool isValid() const
Validity check.
BoxIteratorTraits(const BoxIteratorTraits &traits, bool=true)
bool removeAll(const Item &item)
ForwardIterator< HashGridBox3< Item >, Item, DataIteratorPosition, DataIteratorTraits > DataIterator
Size getSizeX() const
Get the x dimension of the grid.
virtual ~HashGrid3()
Destructor.
virtual void dump(std::ostream &s=std::cout, Size depth=0) const
Dump the contents of a HashGrid3 to a stream.
Vector3 & getOrigin()
Returns the origin of the grid.
void dump(std::ostream &s=std::cout, Size depth=0) const
bool operator!=(const HashGridBox3 &box) const
Inequality operator.
void get(Vector3 &origin, Vector3 &unit, Size &dimension_x, Size &dimension_y, Size &dimension_z) const
HashGrid3< Item > * parent
const HashGridBox3< Item > & getData() const
#define BALL_CREATE_DEEP(name)
static float calculateMinSpacing(LongIndex memory, const Vector3 &size)
const char BALL_EXPORT neighbour_table_[27][3]
BALL_ULONG64_TYPE LongSize
friend class BoxIteratorTraits
ConstBoxIterator endBox() const
get the last box
static const Position INVALID_POSITION
BALL_LONG64_TYPE LongIndex
const HashGridBox3 * getContainer() const
const Item * getClosestItem(const Vector3 &point, Size distance) const
HashGridBox3(HashGrid3< Item > *parent)
Default constructor.
const DataIteratorTraits & operator=(const DataIteratorTraits &traits)
static ForwardIterator end(const Container &container)
Position BoxIteratorPosition
void destroy()
Destroys the grid (obsolete, only calls clear())
bool remove(Position x, Position y, Position z, const Item &item)
Remove an item from position (x, y ,z)
Size getSize() const
Returns the size of a grid, i. e. ?????
ConstForwardIterator< HashGrid3< Item >, HashGridBox3< Item >, BoxIteratorPosition, BoxIteratorTraits > ConstBoxIterator
HashGrid3()
Default constructor.
DataIteratorTraits(const HashGridBox3 &box)
HashGridBox3< Item > & getData()
void insert(Position x, Position y, Position z, const Item &item)
Insert an item at position (x, y, z)
ForwardIterator< HashGridBox3< Item >, HashGridBox3< Item >, BoxIteratorPosition, BoxIteratorTraits > BoxIterator
#define BALL_DUMP_STREAM_PREFIX(os)
HashGridBox3< Item > & getData()
static ConstForwardIterator begin(const Container &container)
BoxIterator beginBox()
get the first box
void insert(const Item &item)
bool operator==(const HashGrid3 &grid) const
Equality operator.
bool isEmpty() const
Tests, whether this is empty.
void host(Visitor< HashGridBox3 > &visitor)
Host method.
Item * find(const Item &item)
#define BALL_DUMP_STREAM_SUFFIX(os)
#define BALL_DUMP_DEPTH(os, depth)
static ForwardIterator begin(const Container &container)
bool operator!=(const HashGrid3 &grid) const
Inequality operator.
void setParent(HashGrid3< Item > *p)
void getIndices(Position &x, Position &y, Position &z)
BoxIteratorTraits(const BoxIteratorTraits &traits, bool=true)
BoxIterator endBox()
get the last box
BoxIteratorTraits(const HashGrid3 &grid)
HashGrid3 * getContainer()
ConstBoxIterator beginBox() const
get the first box
static const Index INVALID_INDEX