PointIterator.hpp 3.95 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
/*
 * PointIterator.hpp
 *
 *  Created on: Jan 3, 2017
 *      Author: i-bird
 */

#ifndef OPENFPM_NUMERICS_SRC_DRAW_POINTITERATOR_HPP_
#define OPENFPM_NUMERICS_SRC_DRAW_POINTITERATOR_HPP_


/*! \brief this class draw particles on subset of grid-like position
 *
 \verbatim
                  Point B
                    |                 (0.6,0.6)
  +   +   +   +   + |  +   +   +   +
    +---------------+
  + | +   +   +   + | +   +   +   +
    |               |
  + | +   +   +   + | +   +   +   +
    |               |
  + | +   +   +   + | +   +   +   +
    |               |
  + | +   +   +   + | +   +   +   +
    |               |
  + | +   +   +   + | +   +   +   +
    |               |
  + | +   +   +   + | +   +   +   +
    |               |
  + | +   +   +   + | +   +   +   +
    +---------------+
  +   +   +   +   + ^ +   +   +   +
(-1.0,-1.0)         |
                    |
                   (Point A)


 \endverbatim
 *
 *
 *  Suppose we have a grid 9x9 from (-1.0,-1.0 to 0.6,0.6)
 *
 *  Defined a Box (-0.9,-0.9 to -0.1,0.5)
 *
 *  This class will return the points
 *
 *  (-0.8 -0.8)
 *  (-0.6 -0.8)
 *  (-0.4 -0.8)
 *  ...
 *  (-0.1,-0.8) Point A
 *  ...
 *  (-0.1,0.5) Point B
 *
 */
template<unsigned int dim, typename T, typename Decomposition>
class PointIterator: protected grid_dist_id_iterator_dec<Decomposition>
{
	//! Actual point
	Point<dim,T> ap;

	//! sub_domain
	Box<dim,T> sub_domain;

incardon's avatar
incardon committed
66 67 68
	//! domain
	Box<dim,T> domain;

69 70 71
	//! Spacing
	T sp[dim];

72
	static grid_key_dx<dim> getStart(size_t (& gs)[dim], const Box<dim,T> & dom, Box<dim,T> & sub_dom, T (& sp)[dim])
73 74
	{
		for (size_t i = 0 ; i < dim ; i++)
incardon's avatar
incardon committed
75
			sp[i] = (dom.getHigh(i) - dom.getLow(i)) / (gs[i] -1);
76 77 78 79 80 81 82 83 84

		grid_key_dx<dim> pkey;

		for (size_t i = 0 ; i < dim ; i++)
			pkey.set_d(i,std::ceil( (sub_dom.getLow(i) - dom.getLow(i)) / sp[i]));

		return pkey;
	}

85
	static grid_key_dx<dim> getStop(size_t (& gs)[dim], const Box<dim,T> & dom, Box<dim,T> & sub_dom, T (& sp)[dim])
86 87
	{
		for (size_t i = 0 ; i < dim ; i++)
incardon's avatar
incardon committed
88
			sp[i] = (dom.getHigh(i) - dom.getLow(i)) / (gs[i] - 1);
89 90 91 92

		grid_key_dx<dim> pkey;

		for (size_t i = 0 ; i < dim ; i++)
incardon's avatar
incardon committed
93
			pkey.set_d(i,std::floor( (sub_dom.getHigh(i) - dom.getLow(i)) / sp[i]));
94 95 96 97

		return pkey;
	}

incardon's avatar
incardon committed
98 99
	void calculateAp()
	{
100 101 102
		if (grid_dist_id_iterator_dec<Decomposition>::isNext() == false)
			return;

incardon's avatar
incardon committed
103 104 105 106 107 108
		grid_key_dx<dim> key = grid_dist_id_iterator_dec<Decomposition>::get();

		for (size_t i = 0 ; i < dim ; i++)
			ap.get(i) = key.get(i) * sp[i] + domain.getLow(i);
	}

109 110 111 112 113 114 115
public:

	/*! \brief Draw Particles
	 *
	 * \param sp grid spacing
	 *
	 */
116
	PointIterator( Decomposition & dec, size_t (& sz)[dim], const Box<dim,T> & domain, Box<dim,T> & sub_domain)
incardon's avatar
incardon committed
117
	:grid_dist_id_iterator_dec<Decomposition>(dec, sz, getStart(sz,domain,sub_domain,sp), getStop(sz,domain,sub_domain,sp)),sub_domain(sub_domain),domain(domain)
118
	{
incardon's avatar
incardon committed
119
		calculateAp();
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
	}

	/*! \Return the actual point
	 *
	 *
	 */
	Point<dim,T> & get()
	{
		return ap;
	}

	/*! \brief Next point
	 *
	 * \return itself
	 *
	 */
	PointIterator & operator++()
	{
		grid_dist_id_iterator_dec<Decomposition>::operator++();
139

incardon's avatar
incardon committed
140
		calculateAp();
141 142 143 144 145 146 147 148

		return *this;
	}

	bool isNext()
	{
		return grid_dist_id_iterator_dec<Decomposition>::isNext();
	}
incardon's avatar
incardon committed
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173

	/*! \brief Return the real Margin of the box
	 *
	 * For example consider to have a domain (0.0,0.0) to (1.0,1.0)
	 * 11 points in each direction (spacing 0.1). if we have a sub-domain
	 * (0.15,0.15) to (0.55,0.55) getBoxMargins return the box (0.2,0.2) to
	 * (0.5,0.5). the smallest box that enclose the points that the point
	 * iterator is going to give
	 *
	 */
	Box<dim,T> getBoxMargins()
	{
		Box<dim,T> box;

		grid_key_dx<dim> start = grid_dist_id_iterator_dec<Decomposition>::getStart();
		grid_key_dx<dim> stop = grid_dist_id_iterator_dec<Decomposition>::getStop();

		for (size_t i = 0 ; i < dim ; i++)
		{
			box.setLow(i, start.get(i)*sp[i] + domain.getLow(i));
			box.setHigh(i, stop.get(i)*sp[i] + domain.getLow(i));
		}

		return box;
	}
174 175 176 177
};


#endif /* OPENFPM_NUMERICS_SRC_DRAW_POINTITERATOR_HPP_ */