Flow123d  release_3.0.0-1150-g11be08a
node_accessor.hh
Go to the documentation of this file.
1 /*!
2  *
3  * Copyright (C) 2015 Technical University of Liberec. All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU General Public License version 3 as published by the
7  * Free Software Foundation. (http://www.gnu.org/licenses/gpl-3.0.en.html)
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12  *
13  *
14  * @file node_accessor.hh
15  * @brief
16  */
17 
18 #ifndef NODE_ACCESSOR_HH_
19 #define NODE_ACCESSOR_HH_
20 
21 #include "mesh/nodes.hh"
22 #include "mesh/mesh.h"
23 
24 /**
25  * Node accessor templated just by dimension of the embedding space, used for access to nodes out of Mesh.
26  * This should allow algorithms over nodes.
27  */
28 template <int spacedim>
29 class NodeAccessor {
30 public:
31  /**
32  * Default invalid accessor.
33  */
35  : mesh_(NULL)
36  {}
37 
38  /**
39  * Node accessor.
40  */
41  NodeAccessor(const Mesh *mesh, unsigned int idx)
42  : mesh_(mesh), node_idx_(idx)
43  {}
44 
45  inline bool is_valid() const {
46  return mesh_ != NULL;
47  }
48 
49  inline const Node * node() const {
50  return &(mesh_->node_vec_[node_idx_]);
51  }
52 
53  inline unsigned int idx() const {
54  return node_idx_;
55  }
56 
57  inline unsigned int index() const {
58  return (unsigned int)mesh_->find_node_id(node_idx_);
59  }
60 
61  inline void inc() {
62  ASSERT(is_valid()).error("Do not call inc() for invalid accessor!");
63  node_idx_++;
64  }
65 
66  bool operator==(const NodeAccessor<spacedim>& other) {
67  return (node_idx_ == other.node_idx_);
68  }
69 
70  /**
71  * -> dereference operator
72  *
73  * Allow simplify calling of node() method. Example:
74  @code
75  NodeAccessor<3> node_ac(mesh, index);
76  arma::vec centre;
77  centre = node_ac.node()->point(); // full format of access to node
78  centre = node_ac->point(); // short format with dereference operator
79  @endcode
80  */
81  inline const Node * operator ->() const {
82  return &(mesh_->node_vec_[node_idx_]);
83  }
84 
85 private:
86  /// Pointer to the mesh owning the node.
87  const Mesh *mesh_;
88 
89  /// Index into Mesh::node_vec_ array.
90  unsigned int node_idx_;
91 };
92 
93 
94 #endif /* NODE_ACCESSOR_HH_ */
const Node * node() const
Definition: nodes.hh:31
Nodes of a mesh.
NodeAccessor(const Mesh *mesh, unsigned int idx)
unsigned int node_idx_
Index into Mesh::node_vec_ array.
Definition: mesh.h:76
#define ASSERT(expr)
Allow use shorter versions of macro names if these names is not used with external library...
Definition: asserts.hh:346
bool operator==(const NodeAccessor< spacedim > &other)
const Node * operator->() const
const Mesh * mesh_
Pointer to the mesh owning the node.
vector< Node > node_vec_
Definition: mesh.h:525
bool is_valid() const
unsigned int index() const
unsigned int idx() const
int find_node_id(unsigned int pos) const
Return node id (in GMSH file) of node of given position in node vector.
Definition: mesh.h:373