Dune Core Modules (2.9.0)

cube.hh
1#ifndef DUNE_SPGRID_CUBE_HH
2#define DUNE_SPGRID_CUBE_HH
3
5
6#include <dune/common/iostream.hh>
7
8namespace Dune
9{
10
11 // SPCube
12 // ------
13
14 template< class ct, int dim >
15 class SPCube
16 {
17 typedef SPCube< ct, dim > This;
18
19 public:
21 typedef ct ctype;
22
24 static const int dimension = dim;
25
27 typedef FieldVector< ctype, dimension > GlobalVector;
28
30 SPCube ();
31
42 SPCube ( const GlobalVector &a, const GlobalVector &b );
43
48 const GlobalVector &origin () const;
49
54 const GlobalVector &width () const;
55
62 bool contains ( const GlobalVector &x ) const;
63
68 static This unitCube ();
69
70 private:
71 GlobalVector origin_, width_;
72 };
73
74
75
76 // Implementation of SPCube
77 // ------------------------
78
79 template< class ct, int dim >
80 inline SPCube< ct, dim >::SPCube ()
81 {
82 for( int i = 0; i < dimension; ++i )
83 origin_[ i ] = width_[ i ] = 0;
84 }
85
86
87 template< class ct, int dim >
88 inline SPCube< ct, dim >
89 ::SPCube ( const GlobalVector &a, const GlobalVector &b )
90 {
91 for( int i = 0; i < dimension; ++i )
92 {
93 origin_[ i ] = std::min( a[ i ], b[ i ] );
94 width_[ i ] = std::max( a[ i ], b[ i ] ) - origin_[ i ];
95 }
96 }
97
98
99 template< class ct, int dim >
100 inline const typename SPCube< ct, dim >::GlobalVector &
101 SPCube< ct, dim >::origin () const
102 {
103 return origin_;
104 }
105
106
107 template< class ct, int dim >
108 inline const typename SPCube< ct, dim >::GlobalVector &
109 SPCube< ct, dim >::width () const
110 {
111 return width_;
112 }
113
114
115 template< class ct, int dim >
116 inline bool SPCube< ct, dim >::contains ( const GlobalVector &x ) const
117 {
118 bool contains = true;
119 for( int i = 0; i < dimension; ++i )
120 {
121 const ctype y = x[ i ] - origin()[ i ];
122 contains &= ((y >= 0) && (y <= width()[ i ]));
123 }
124 return contains;
125 }
126
127
128 template< class ct, int dim >
129 inline typename SPCube< ct, dim >::This
130 SPCube< ct, dim >::unitCube ()
131 {
132 GlobalVector a, b;
133 for( int i = 0; i < dimension; ++i )
134 {
135 a = ctype( 0 );
136 b = ctype( 1 );
137 }
138 return This( a, b );
139 }
140
141
142
143 // Auxilliary Functions for SPCube
144 // -------------------------------
145
146 template< class char_type, class traits, class ct, int dim >
147 inline std::basic_ostream< char_type, traits > &
148 operator<< ( std::basic_ostream< char_type, traits > &out,
149 const SPCube< ct, dim > &cube )
150 {
151 typedef SPCube< ct, dim > Cube;
152 typename Cube::GlobalVector a = cube.origin();
153 typename Cube::GlobalVector b = a + cube.width();
154 for( int i = 0; i < Cube::dimension; ++i )
155 out << (i > 0 ? "x[" : "[") << a[ i ] << "," << b[ i ] << "]";
156 return out;
157 }
158
159
160 template< class char_type, class traits, class ct, int dim >
161 inline std::basic_istream< char_type, traits > &
162 operator>> ( std::basic_istream< char_type, traits > &in,
163 SPCube< ct, dim > &cube )
164 {
165 typedef SPCube< ct, dim > Cube;
166 typename Cube::GlobalVector a;
167 typename Cube::GlobalVector b;
168 for( int i = 0; i < Cube::dimension; ++i )
169 {
170 if( i > 0 )
171 in >> match( 'x' );
172 in >> match( '[' ) >> a[ i ] >> match( ',' ) >> b[ i ] >> match( ']' );
173 }
174 if( !in.fail() )
175 cube = Cube( a, b );
176 return in;
177 }
178
179} // namespace Dune
180
181#endif // #ifndef DUNE_SPGRID_CUBE_HH
Implements a vector constructed from a given type representing a field and a compile-time given size.
Stream & operator>>(Stream &stream, std::tuple< Ts... > &t)
Read a std::tuple.
Definition: streamoperators.hh:43
Dune namespace.
Definition: alignedallocator.hh:13
@ cube
use only cube elements (i.e., quadrilaterals or hexahedra)
Definition: declaration.hh:19
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Jul 15, 22:36, 2024)