dune-spgrid 2.8.0
Loading...
Searching...
No Matches
cube.hh
Go to the documentation of this file.
1#ifndef DUNE_SPGRID_CUBE_HH
2#define DUNE_SPGRID_CUBE_HH
3
4#include <dune/common/fvector.hh>
5
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 >
81 {
82 for( int i = 0; i < dimension; ++i )
83 origin_[ i ] = width_[ i ] = 0;
84 }
85
86
87 template< class ct, int 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 &
102 {
103 return origin_;
104 }
105
106
107 template< class ct, int dim >
108 inline const typename SPCube< ct, dim >::GlobalVector &
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
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
Dune::SPMultiIndex< dim > min(const Dune::SPMultiIndex< dim > &a, const Dune::SPMultiIndex< dim > &b)
Definition: multiindex.hh:296
Dune::SPMultiIndex< dim > max(const Dune::SPMultiIndex< dim > &a, const Dune::SPMultiIndex< dim > &b)
Definition: multiindex.hh:307
Definition: iostream.hh:7
std::basic_ostream< char_type, traits > & operator<<(std::basic_ostream< char_type, traits > &out, const SPCube< ct, dim > &cube)
Definition: cube.hh:148
std::basic_istream< char_type, traits > & operator>>(std::basic_istream< char_type, traits > &in, SPCube< ct, dim > &cube)
Definition: cube.hh:162
iostream::Match< typename iostream::MatchTraits< T >::Type > match(const T &value)
Definition: iostream.hh:87
Definition: cube.hh:16
const GlobalVector & origin() const
obtain lower left corner
Definition: cube.hh:101
bool contains(const GlobalVector &x) const
determine whether the cube contains a point x
Definition: cube.hh:116
static This unitCube()
obtain a domain modelling the unit cube
Definition: cube.hh:130
const GlobalVector & width() const
obtain width
Definition: cube.hh:109
ct ctype
coordinate type
Definition: cube.hh:21
FieldVector< ctype, dimension > GlobalVector
type of global vectors, i.e., vectors within the domain
Definition: cube.hh:27
static const int dimension
dimension of the domain
Definition: cube.hh:24
SPCube()
default constructor
Definition: cube.hh:80