File: type.hh

package info (click to toggle)
dune-geometry 2.11.0-1~exp2
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 2,316 kB
  • sloc: cpp: 15,164; python: 262; makefile: 6
file content (232 lines) | stat: -rw-r--r-- 9,276 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
// SPDX-FileCopyrightInfo: Copyright © DUNE Project contributors, see file LICENSE.md in module root
// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
#ifndef DUNE_PYTHON_GEOMETRY_TYPE_HH
#define DUNE_PYTHON_GEOMETRY_TYPE_HH

#include <cassert>

#include <dune/common/exceptions.hh>

#include <dune/geometry/type.hh>
#include <dune/geometry/typeindex.hh>

#include <dune/python/pybind11/operators.h>
#include <dune/python/pybind11/pybind11.h>

namespace Dune
{

  // to_string for GeometryType
  // --------------------------

  inline static std::string to_string ( const GeometryType &type )
  {
    if( type.isNone() )
      return "none(" + std::to_string( type.dim() ) + ")";
    switch( type.dim() )
    {
    case 0:
      return "vertex";
    case 1:
      return "line";
    case 2:
      return (type.isSimplex() ? "triangle" : "quadrilateral");
    case 3:
      if( type.isSimplex() )
        return "tetrahedron";
      else if( type.isHexahedron() )
        return "hexahedron";
      else if( type.isPyramid() )
        return "pyramid";
      else if( type.isPrism() )
        return "prism";
    default:
      if( type.isSimplex() )
        return "simplex(" + std::to_string( type.dim() ) + ")";
      else if( type.isCube() )
        return "cube(" + std::to_string( type.dim() ) + ")";
      else
        return "general(" + std::to_string( type.id() ) + ", " + std::to_string( type.dim() ) + ")";
    }
  }



  // geometryTypeFromString
  // ----------------------

  inline static GeometryType geometryTypeFromString ( const std::string &s )
  {
    typedef GeometryType (*Constructor) ( const std::vector< std::string > & );
    static const char *constructorNames[] = {
        "cube",
        "general",
        "hexahedron",
        "line",
        "none",
        "prism",
        "pyramid",
        "quadrilateral",
        "simplex",
        "tetrahedron",
        "triangle",
        "vertex"
      };
    static const Constructor constructors[]
      = {
          // cube
          [] ( const std::vector< std::string > &args ) {
              if( args.size() != 1 )
                DUNE_THROW( Exception, "GeometryType 'cube' requires integer argument for dimension." );
              return GeometryTypes::cube( std::stoul( args[ 0 ] ) );
            },
          // general
          [] ( const std::vector< std::string > &args ) {
              if( args.size() != 2 )
                DUNE_THROW( Exception, "GeometryType 'general' requires two integer arguments, topologyId and dimension." );
              const auto id = std::stoul( args[ 0 ] );
              const auto dim = std::stoul( args[ 1 ] );
              if( id >= Dune::Impl::numTopologies( dim ) )
                DUNE_THROW( Exception, "Topology id " << id << " too large for dimension " << dim << "." );
              return GeometryType( id, dim );
            },
          // hexahedron
          [] ( const std::vector< std::string > &args ) {
              if( !args.empty() )
                DUNE_THROW( Exception, "GeometryType 'hexahedron' does not require arguments." );
              return GeometryTypes::hexahedron;
            },
          // line
          [] ( const std::vector< std::string > &args ) {
              if( !args.empty() )
                DUNE_THROW( Exception, "GeometryType 'line' does not require arguments." );
              return GeometryTypes::line;
            },
          // none
          [] ( const std::vector< std::string > &args ) {
              if( args.size() != 1 )
                DUNE_THROW( Exception, "GeometryType 'none' requires integer argument for dimension." );
              return GeometryTypes::none( std::stoul( args[ 0 ] ) );
            },
          // prism
          [] ( const std::vector< std::string > &args ) {
              if( !args.empty() )
                DUNE_THROW( Exception, "GeometryType 'prism' does not require arguments." );
              return GeometryTypes::prism;
            },
          // pyramid
          [] ( const std::vector< std::string > &args ) {
              if( !args.empty() )
                DUNE_THROW( Exception, "GeometryType 'pyramid' does not require arguments." );
              return GeometryTypes::pyramid;
            },
          // quadrilateral
          [] ( const std::vector< std::string > &args ) {
              if( !args.empty() )
                DUNE_THROW( Exception, "GeometryType 'quadrilateral' does not require arguments." );
              return GeometryTypes::quadrilateral;
            },
          // simplex
          [] ( const std::vector< std::string > &args ) {
              if( args.size() != 1 )
                DUNE_THROW( Exception, "GeometryType 'simplex' requires integer argument for dimension." );
              return GeometryTypes::simplex( std::stoul( args[ 0 ] ) );
            },
          // tetrahedron
          [] ( const std::vector< std::string > &args ) {
              if( !args.empty() )
                DUNE_THROW( Exception, "GeometryType 'tetrahedron' does not require arguments." );
              return GeometryTypes::tetrahedron;
            },
          // triangle
          [] ( const std::vector< std::string > &args ) {
              if( !args.empty() )
                DUNE_THROW( Exception, "GeometryType 'triangle' does not require arguments." );
              return GeometryTypes::triangle;
            },
          // vertex
          [] ( const std::vector< std::string > &args ) {
              if( !args.empty() )
                DUNE_THROW( Exception, "GeometryType 'vertex' does not require arguments." );
              return GeometryTypes::vertex;
            }
        };
    const std::size_t numConstructors = sizeof( constructorNames ) / sizeof( const char * );

    // find constructor index
    std::size_t n = s.find_first_of( '(' );
    const std::string cName = s.substr( 0, n );
    const std::size_t cIdx = std::lower_bound( constructorNames, constructorNames + numConstructors, cName ) - constructorNames;
    if( (cIdx == numConstructors) || (constructorNames[ cIdx ] != cName) )
      DUNE_THROW( Exception, "No DUNE geometry type constructor named '" << cName << "'." );

    // obtain argument vector
    std::vector< std::string > args;
    if( n != std::string::npos )
    {
      while( s[ n ] != ')' )
      {
        // skip leading spaces
        const std::size_t m = s.find_first_not_of( ' ', n+1 );
        if( m == std::string::npos )
          DUNE_THROW( Exception, "Invalid argument list." );

        // find end of argument
        n = s.find_first_of( ",)", m );
        if( (n == std::string::npos) || (n == m) )
          DUNE_THROW( Exception, "Invalid argument list." );

        // remove trailing spaces from argument
        const std::size_t k = s.find_last_not_of( ' ', n-1 );
        assert( (k != std::string::npos) && (k >= m) );

        args.push_back( s.substr( m, k-m+1 ) );
      }
    }

    // call constructor
    return constructors[ cIdx ]( args );
  }



  namespace Python
  {

    inline pybind11::class_< GeometryType > registerGeometryType ( pybind11::module scope )
    {
      pybind11::class_< GeometryType > cls( scope, "GeometryType" );

      cls.def( pybind11::init( [] ( const std::string &s ) { return new GeometryType( geometryTypeFromString( s ) ); } ) );

      cls.def_property_readonly( "isVertex", [] ( const GeometryType &self ) { return self.isVertex(); } );
      cls.def_property_readonly( "isLine", [] ( const GeometryType &self ) { return self.isLine(); } );
      cls.def_property_readonly( "isTriangle", [] ( const GeometryType &self ) { return self.isTriangle(); } );
      cls.def_property_readonly( "isQuadrilateral", [] ( const GeometryType &self ) { return self.isQuadrilateral(); } );
      cls.def_property_readonly( "isTetrahedron",[] ( const GeometryType &self ) { return self.isTetrahedron(); } );
      cls.def_property_readonly( "isPyramid",[] ( const GeometryType &self ) { return self.isPyramid(); } );
      cls.def_property_readonly( "isPrism", [] ( const GeometryType &self ) { return self.isPrism(); } );
      cls.def_property_readonly( "isHexahedron", [] ( const GeometryType &self ) { return self.isHexahedron(); } );
      cls.def_property_readonly( "isSimplex", [] ( const GeometryType &self ) { return self.isSimplex(); } );
      cls.def_property_readonly( "isCube", [] ( const GeometryType &self ) { return self.isCube(); } );
      cls.def_property_readonly( "isNone", [] ( const GeometryType &self ) { return self.isNone(); } );

      cls.def( pybind11::self == pybind11::self );
      cls.def( pybind11::self != pybind11::self );
      cls.def( "__hash__", [] ( const GeometryType &self ) { return GlobalGeometryTypeIndex::index( self ); } );

      cls.def( "__str__", [] ( const GeometryType &self ) { return to_string( self ); } );

      cls.def_property_readonly( "dim", [] ( const GeometryType &self ) { return self.dim(); } );
      cls.def_property_readonly( "dimension", [] ( const GeometryType &self ) { return self.dim(); } );

      return cls;
    }

  } // namespace Python

} // namespace Dune

#endif // ifndef DUNE_PYTHON_GEOMETRY_TYPE_HH