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;
108 Size getSize()
const;
113 void insert(
const Item& item);
120 bool remove(
const Item& item);
127 bool removeAll(
const Item& item);
151 bool has(
const Item& item)
const;
156 bool isEmpty()
const;
164 bool isValid()
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
308 BoxIteratorPosition position_;
312 friend class BoxIteratorTraits;
326 return BoxIterator::begin(*
this);
332 return BoxIterator::end(*
this);
338 <HashGridBox3<Item>, HashGridBox3<Item>,
339 BoxIteratorPosition, BoxIteratorTraits>
345 return ConstBoxIterator::begin(*
this);
351 return ConstBoxIterator::end(*
this);
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());
478 HashGridBox3<Item>* bound_;
479 DataIteratorPosition position_;
482 friend class DataIteratorTraits;
495 return DataIterator::begin(*
this);
501 return DataIterator::end(*
this);
516 return ConstDataIterator::begin(*
this);
522 return ConstDataIterator::end(*
this);
532 template<
typename Item>
538 template<
typename Item>
544 template<
typename Item>
551 template<
typename Item>
557 template<
typename Item>
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);
723 if (result <= Processor::BREAK)
725 return (result == Processor::BREAK) ? true :
false;
729 return processor.
finish();
732 template <
typename Item>
735 if (processor.start() ==
false)
744 result = processor(*it);
746 if (result <= Processor::BREAK)
748 return (result == Processor::BREAK) ? true :
false;
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);
810 virtual void clear();
816 void clear(
const Vector3 &vector);
825 void destroy(
const Vector3& vector);
837 void set(
const Vector3& origin,
float unit,
Size size);
840 void set(
const HashGrid3& grid,
bool deep =
true);
849 void get(
HashGrid3& grid,
bool deep =
true)
const;
857 Size countNonEmptyBoxes()
const;
860 Size getSize()
const;
866 const Vector3& getOrigin()
const;
872 const Vector3& getUnit()
const;
875 Size getSizeX()
const;
878 Size getSizeY()
const;
881 Size getSizeZ()
const;
903 void insert(
const Vector3& vector,
const Item& item);
909 bool remove(
const Vector3& vector,
const Item& item);
931 bool isEmpty()
const;
939 virtual bool isValid()
const;
942 virtual void dump(std::ostream& s = std::cout,
Size depth = 0)
const;
959 const Item* getClosestItem(
const Vector3& point,
Size distance)
const;
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_];
1093 BoxIteratorPosition position_;
1096 friend class BoxIteratorTraits;
1107 return BoxIterator::begin(*
this);
1113 return BoxIterator::end(*
this);
1126 return ConstBoxIterator::begin(*
this);
1132 return ConstBoxIterator::end(*
this);
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>
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>
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);
1709 if (result <= Processor::BREAK)
1711 return (result == Processor::BREAK) ? true :
false;
1716 return processor->
finish();
1719 template <
typename Item>
1722 if (processor.start() ==
false)
1732 result = processor(*box);
1734 if (result <= Processor::BREAK)
1736 return (result == Processor::BREAK) ? true :
false;
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< BALL::Atom * > DataContainer
ForwardIterator< HashGrid3< Item >, HashGridBox3< Item >, BoxIteratorPosition, BoxIteratorTraits > BoxIterator
ConstBoxIterator beginBox() const
get the first box
HashGridBox3< Item > * getBox(Position x, Position y, Position z)
Return the HashGridBox3 at position (x, y, z)
#define BALL_CREATE(name)
Position BoxIteratorPosition
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)
ConstForwardIterator< HashGridBox3< Item >, HashGridBox3< Item >, BoxIteratorPosition, BoxIteratorTraits > ConstBoxIterator
This is the const version of BoxIterator .
const BoxIteratorPosition & getPosition() const
ConstForwardIterator< HashGridBox3< Item >, Item, DataIteratorPosition, DataIteratorTraits > ConstDataIterator
BoxIteratorTraits(const HashGridBox3 &box)
void clear()
Clears the grid box.
const BoxIteratorPosition & getPosition() const
const signed char BALL_EXPORT neighbour_table_[27][3]
const HashGridBox3 * getContainer() const
HashGridBox3 * getContainer()
const DataIteratorPosition & getPosition() const
DataContainer::iterator DataIteratorPosition
BoxIteratorPosition & getPosition()
BoxIteratorPosition & getPosition()
HashGridBox3 * getContainer()
bool remove(const Item &item)
DataIteratorTraits(const DataIteratorTraits &traits, bool=true)
const Item & getData() const
DataIteratorPosition & getPosition()
BoxIteratorTraits(const BoxIteratorTraits &traits, bool=true)
ForwardIterator< HashGridBox3< Item >, Item, DataIteratorPosition, DataIteratorTraits > DataIterator
HashGrid3< Item > * parent
BALL_EXPORT bool operator!=(const String &s1, const String &s2)
ConstBoxIterator beginBox() const
#define BALL_CREATE_DEEP(name)
const HashGridBox3< Item > & getData() const
BALL_ULONG64_TYPE LongSize
const HashGrid3 * getContainer() const
static const Position INVALID_POSITION
BALL_LONG64_TYPE LongIndex
HashGridBox3(HashGrid3< Item > *parent)
Default constructor.
BALL_EXPORT bool operator==(const String &s1, const String &s2)
Position BoxIteratorPosition
ConstDataIterator endData() const
ConstForwardIterator< HashGrid3< Item >, HashGridBox3< Item >, BoxIteratorPosition, BoxIteratorTraits > ConstBoxIterator
-*- Mode: C++; tab-width: 2; -*-
DataIteratorTraits(const HashGridBox3 &box)
HashGridBox3< Item > & getData()
ConstBoxIterator endBox() const
ConstDataIterator beginData() const
ForwardIterator< HashGridBox3< Item >, HashGridBox3< Item >, BoxIteratorPosition, BoxIteratorTraits > BoxIterator
#define BALL_DUMP_STREAM_PREFIX(os)
HashGridBox3< Item > & getData()
const HashGridBox3< Item > & getData() const
bool getIndices(const HashGridBox3< Item > &box, Position &x, Position &y, Position &z) const
Get the position indices of a HashGridBox3.
BoxIterator beginBox()
get the first box
void insert(const Item &item)
ConstBoxIterator endBox() const
get the last box
#define BALL_DUMP_STREAM_SUFFIX(os)
#define BALL_DUMP_DEPTH(os, depth)
Size getSize() const
Returns the size of a grid, i. e. ?????
BoxIteratorTraits(const BoxIteratorTraits &traits, bool=true)
BoxIterator endBox()
get the last box
BoxIteratorTraits(const HashGrid3 &grid)
HashGrid3 * getContainer()
const HashGridBox3 * getContainer() const
static const Index INVALID_INDEX