Reference documentation for deal.II version 8.5.1
cell_id.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 1998 - 2016 by the deal.II authors
4 //
5 // This file is part of the deal.II library.
6 //
7 // The deal.II library is free software; you can use it, redistribute
8 // it, and/or modify it under the terms of the GNU Lesser General
9 // Public License as published by the Free Software Foundation; either
10 // version 2.1 of the License, or (at your option) any later version.
11 // The full text of the license can be found in the file LICENSE at
12 // the top level of the deal.II distribution.
13 //
14 // ---------------------------------------------------------------------
15 
16 #ifndef dealii__cell_id_h
17 #define dealii__cell_id_h
18 
19 #include <deal.II/base/config.h>
20 #include <deal.II/base/exceptions.h>
21 #include <deal.II/base/std_cxx11/array.h>
22 
23 #include <vector>
24 #include <iostream>
25 
26 #ifdef DEAL_II_WITH_P4EST
27 #include <deal.II/distributed/p4est_wrappers.h>
28 #endif
29 
30 DEAL_II_NAMESPACE_OPEN
31 
32 template <int, int> class Triangulation;
33 
56 class CellId
57 {
58 public:
66  typedef std_cxx11::array<unsigned int, 4> binary_type;
67 
77  CellId(const unsigned int coarse_cell_id,
78  const std::vector<unsigned char> &child_indices);
79 
90  CellId(const unsigned int coarse_cell_id,
91  const unsigned int n_child_indices,
92  const unsigned char *child_indices);
93 
98  CellId(const binary_type &binary_representation);
99 
103  CellId();
104 
108  std::string to_string() const;
109 
113  template<int dim>
114  binary_type to_binary() const;
115 
119  template<int dim, int spacedim>
121  to_cell(const Triangulation<dim,spacedim> &tria) const;
122 
126  bool operator== (const CellId &other) const;
127 
131  bool operator!= (const CellId &other) const;
132 
138  bool operator<(const CellId &other) const;
139 
140 private:
145  unsigned int coarse_cell_id;
146 
151  unsigned int n_child_indices;
152 
162 #ifdef DEAL_II_WITH_P4EST
163  std_cxx11::array<char,internal::p4est::functions<2>::max_level> child_indices;
164 #else
165  std_cxx11::array<char,30> child_indices;
166 #endif
167 
168  friend std::istream &operator>> (std::istream &is, CellId &cid);
169  friend std::ostream &operator<< (std::ostream &os, const CellId &cid);
170 };
171 
172 
173 
174 
178 inline
179 std::ostream &operator<< (std::ostream &os,
180  const CellId &cid)
181 {
182  os << cid.coarse_cell_id << '_' << cid.n_child_indices << ':';
183  for (unsigned int i=0; i<cid.n_child_indices; ++i)
184  // write the child indices. because they are between 0 and 2^dim-1, they all
185  // just have one digit, so we could write them as integers. it's
186  // probably clearer to write them as one-digit characters starting
187  // at '0'
188  os << static_cast<unsigned char>(cid.child_indices[i] + '0');
189  return os;
190 }
191 
192 
193 
197 inline
198 std::istream &operator>> (std::istream &is,
199  CellId &cid)
200 {
201  unsigned int cellid;
202  is >> cellid;
203  if (is.eof())
204  return is;
205 
206  cid.coarse_cell_id = cellid;
207  char dummy;
208  is >> dummy;
209  Assert(dummy=='_', ExcMessage("invalid CellId"));
210  is >> cid.n_child_indices;
211  is >> dummy;
212  Assert(dummy==':', ExcMessage("invalid CellId"));
213 
214  char value;
215  for (unsigned int i=0; i<cid.n_child_indices; ++i)
216  {
217  // read the one-digit child index (as an integer number) and
218  // convert it back into unsigned char
219  is >> value;
220  cid.child_indices[i] = value-'0';
221  }
222  return is;
223 }
224 
225 
226 
227 inline bool
228 CellId::operator== (const CellId &other) const
229 {
230  if (this->coarse_cell_id != other.coarse_cell_id)
231  return false;
232  if (n_child_indices != other.n_child_indices)
233  return false;
234 
235  for (unsigned int i=0; i<n_child_indices; ++i)
236  if (child_indices[i] != other.child_indices[i])
237  return false;
238 
239  return true;
240 }
241 
242 
243 
244 inline bool
245 CellId::operator!= (const CellId &other) const
246 {
247  return !(*this == other);
248 }
249 
250 
251 
252 inline
253 bool CellId::operator<(const CellId &other) const
254 {
255  if (this->coarse_cell_id != other.coarse_cell_id)
256  return this->coarse_cell_id < other.coarse_cell_id;
257 
258  unsigned int idx = 0;
259  while (idx < n_child_indices)
260  {
261  if (idx>=other.n_child_indices)
262  return false;
263 
264  if (child_indices[idx] != other.child_indices[idx])
265  return child_indices[idx] < other.child_indices[idx];
266 
267  ++idx;
268  }
269 
270  if (n_child_indices == other.n_child_indices)
271  return false;
272  return true; // other.id is longer
273 }
274 
275 DEAL_II_NAMESPACE_CLOSE
276 
277 #endif
Triangulation< dim, spacedim >::cell_iterator to_cell(const Triangulation< dim, spacedim > &tria) const
Definition: cell_id.cc:163
binary_type to_binary() const
Definition: cell_id.cc:107
std_cxx11::array< char, internal::p4est::functions< 2 >::max_level > child_indices
Definition: cell_id.h:163
CellId()
Definition: cell_id.cc:25
unsigned int coarse_cell_id
Definition: cell_id.h:145
friend std::ostream & operator<<(std::ostream &os, const CellId &cid)
Definition: cell_id.h:179
static ::ExceptionBase & ExcMessage(std::string arg1)
#define Assert(cond, exc)
Definition: exceptions.h:313
bool operator<(const CellId &other) const
Definition: cell_id.h:253
bool operator!=(const CellId &other) const
Definition: cell_id.h:245
friend std::istream & operator>>(std::istream &is, CellId &cid)
Definition: cell_id.h:198
bool operator==(const CellId &other) const
Definition: cell_id.h:228
std_cxx11::array< unsigned int, 4 > binary_type
Definition: cell_id.h:66
std::string to_string() const
Definition: cell_id.cc:152
Definition: cell_id.h:56
OutputOperator< VectorType > & operator<<(OutputOperator< VectorType > &out, unsigned int step)
Definition: operator.h:154
unsigned int n_child_indices
Definition: cell_id.h:151
std::istream & operator>>(std::istream &in, Point< dim, Number > &p)
Definition: point.h:551