Flow123d  release_2.2.0-914-gf1a3a4f
simplex.hh
Go to the documentation of this file.
1 #ifndef SIMPLEX_H_
2 #define SIMPLEX_H_
3 
4 #include <armadillo>
5 #include "system/system.hh"
6 
7 
8 
9 /**
10  * Simplex<N> represents N-dimensional simplex,
11  * Simplex<0> = pointer to 3D point
12  * Simplex<1> = abscissa
13  * Simplex<2> = triangle
14  * Simplex<3> = tetrahedron
15  * Sub - simplices are made in lexicographical order
16  *
17  * Simplex<3> with 4 points 0,1,2,3 creates:
18  * |
19  * -----------------------------------------------------------------------------------------------------------------------------------------------
20  * | | | |
21  * S<2>(0,1,2) S<2>(0,1,3) S<2>(0,2,3) S<2>(1,2,3)
22  * | | | |
23  * ------------------------------- ------------------------------- ------------------------------- -------------------------------
24  * | | | | | | | | | | | |
25  * S<1>(0,1) S<1>(0,2) S<1>(1,2) S<1>(0,1) S<1>(0,3) S<1>(1,3) S<1>(0,2) S<1>(0,3) S<1>(2,3) S<1>(1,2) S<1>(1,3) S<1>(2,3)
26  * | | | | | | | | | | | |
27  * ------- ------- ------- ------- ------- ------- ------- ------- ------- ------- ------- -------
28  * | | | | | | | | | | | | | | | | | | | | | | | |
29  * S<0>(0) S<0>(1) S<0>(0) S<0>(2) S<0>(1) S<0>(2) S<0>(0) S<0>(1) S<0>(0) S<0>(3) S<0>(1) S<0>(3) S<0>(0) S<0>(2) S<0>(0) S<0>(3) S<0>(2) S<0>(3) S<0>(1) S<0>(2) S<0>(1) S<0>(3) S<0>(2) S<0>(3)
30  *
31  * Simplex<0> has pointer to 3D point, because 3D point will be only once in memory
32  *
33  * https://en.wikipedia.org/wiki/Simplex
34  */
35 template<unsigned int N> class Simplex;
36 
37 /// Operator for printing Simplex<N>.
38 template<unsigned int N> std::ostream& operator<<(std::ostream& os, const Simplex<N>& s);
39 
40 /**
41  * Simplex<0> represents a point in 3D
42  * it has a pointer to coordinates of point in 3D because of mesh implementation
43  */
44 template<> class Simplex<0> {
45 private:
46  arma::vec3* coords_; ///< Point coordinates.
47 public:
48  Simplex(); /// Default constructor. Does not set coordinates pointer.
49 
50  /** Constuctor that sets the point coordinates.
51  * @param field - array of pointers to point coordinates;
52  * it takes just the first element of the input array for case of Simplex<0>
53  */
54  Simplex(arma::vec3 **field);
55 
56  ~Simplex(); ///< Destructor.
57 
58  /**
59  * Setter for point coordinates
60  */
61  void set_simplices(arma::vec3 **field);
62 
63  /// Returns the point coordinates.
64  arma::vec3 &point_coordinates();
65 
66  /// Friend output operator.
67  friend std::ostream& operator<< <>(std::ostream& os, const Simplex<0>& s);
68 };
69 
70 template<unsigned int N> class Simplex {
71 private:
72  /**
73  * Every simplex<N> has (N+1) simplices of dimension (N-1)
74  * TODO: (idea - when used in new mesh) replace with references
75  */
76  Simplex<N - 1> simplices_[N + 1];
77 public:
78  Simplex(); ///< Default (empty) constructor.
79 
80  /** Constuctor that sets the coordinates of all vertices.
81  * @param field - array of pointers to point coordinates of the vertices
82  * array must contain (N+1) elements for case Simplex<N>
83  */
84  Simplex(arma::vec3 **field_of_pointers_to_coordinates);
85 
86  ~Simplex(); ///< Destructor.
87 
88  /// Creating sub-simplices in lexicografic order
89  void set_simplices(arma::vec3 **field_of_pointers_to_coordinates);
90 
91  /// Returns subsimplex of index @p idx.
92  Simplex<N - 1> &operator[](unsigned int idx);
93 
94  /// Get simplex of abscissa from different simplices - if it has own implementation in .cpp file
95  Simplex<1> &abscissa(unsigned int idx);
96 
97  Simplex<0> &node(unsigned int idx);
98 
99  /// Friend output operator.
100  friend std::ostream& operator<< <>(std::ostream& os, const Simplex<N>& s);
101 };
102 
103 
104 
105 /********************************************* IMPLEMENTATION ***********************************************/
106 
108 { coords_ = nullptr; }
109 
111 { ASSERT_DBG(field != nullptr);
112  coords_ = field[0]; }
113 
115 
117 { ASSERT_DBG(field != nullptr);
118  coords_ = field[0]; }
119 
121 { ASSERT_DBG(coords_ != nullptr);
122  return *coords_; }
123 
124 
125 
126 template<unsigned int N> Simplex<N>::Simplex(){}
127 
128 template<unsigned int N> Simplex<N>::Simplex(arma::vec3 **field_of_pointers_to_coordinates)
129 { set_simplices(field_of_pointers_to_coordinates); }
130 
131 template<unsigned int N> Simplex<N>::~Simplex(){}
132 
133 template<unsigned int N> Simplex<N-1>& Simplex<N>::operator[](unsigned int idx)
134 { ASSERT_DBG(idx < N+1);
135  return simplices_[idx]; }
136 
137 
138 #endif /* SIMPLEX_H_ */
void set_simplices(arma::vec3 **field_of_pointers_to_coordinates)
Creating sub-simplices in lexicografic order.
Definition: simplex.cc:10
arma::vec3 * coords_
Point coordinates.
Definition: simplex.hh:46
Simplex< 0 > & node(unsigned int idx)
Simplex< 1 > & abscissa(unsigned int idx)
Get simplex of abscissa from different simplices - if it has own implementation in ...
~Simplex()
Destructor.
Definition: simplex.hh:131
Simplex< N-1 > & operator[](unsigned int idx)
Returns subsimplex of index idx.
Definition: simplex.hh:133
Simplex()
Default (empty) constructor.
Definition: simplex.hh:126
#define ASSERT_DBG(expr)
Definition: asserts.hh:349
Simplex< N-1 > simplices_[N+1]
Definition: simplex.hh:76