Mesh Oriented datABase  (version 5.6.0)
An array-based unstructured mesh library
Util.cpp
Go to the documentation of this file.
1 /**
2  * MOAB, a Mesh-Oriented datABase, is a software component for creating,
3  * storing and accessing finite element mesh data.
4  *
5  * Copyright 2004 Sandia Corporation. Under the terms of Contract
6  * DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government
7  * retains certain rights in this software.
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  */
15 
16 //-------------------------------------------------------------------------
17 // Purpose : This file contains utility functions for use in MOAB
18 //
19 // Special Notes : This is a pure virtual class, to prevent instantiation.
20 // All functions are static, called like this:
21 // Util::function_name();
22 //-------------------------------------------------------------------------
23 #include "moab/Util.hpp"
24 #include "moab/Interface.hpp"
25 #include <cassert>
26 #include <algorithm>
27 #include <limits>
28 #if defined( _MSC_VER ) || defined( __MINGW32__ )
29 #include <float.h>
30 #define finite( A ) _finite( A )
31 #ifndef MOAB_HAVE_FINITE
32 #define MOAB_HAVE_FINITE
33 #endif
34 #endif
35 
36 namespace moab
37 {
38 
39 //! temporary normal function for MBEntities. This should be moved to
40 //! an appropriate MB algorithms file
41 
42 void Util::normal( Interface* MB, EntityHandle handle, double& x, double& y, double& z )
43 {
44  // get connectivity
45  const EntityHandle* connectivity = NULL;
46  int number_nodes = 0;
47  // TODO make the return value nonvoid
48  ErrorCode rval = MB->get_connectivity( handle, connectivity, number_nodes, true );
49  MB_CHK_SET_ERR_RET( rval, "can't get_connectivity" );
50  assert( number_nodes >= 3 );
51 
52  // get_coordinates
53  double coords[3][3];
54  MB->get_coords( &( connectivity[0] ), 1, coords[0] );
55  MB->get_coords( &( connectivity[1] ), 1, coords[1] );
56  MB->get_coords( &( connectivity[2] ), 1, coords[2] );
57 
58  double vecs[2][3];
59  vecs[0][0] = coords[1][0] - coords[0][0];
60  vecs[0][1] = coords[1][1] - coords[0][1];
61  vecs[0][2] = coords[1][2] - coords[0][2];
62  vecs[1][0] = coords[2][0] - coords[0][0];
63  vecs[1][1] = coords[2][1] - coords[0][1];
64  vecs[1][2] = coords[2][2] - coords[0][2];
65 
66  x = vecs[0][1] * vecs[1][2] - vecs[0][2] * vecs[1][1];
67  y = vecs[0][2] * vecs[1][0] - vecs[0][0] * vecs[1][2];
68  z = vecs[0][0] * vecs[1][1] - vecs[0][1] * vecs[1][0];
69 
70  double mag = sqrt( x * x + y * y + z * z );
71  if( mag > std::numeric_limits< double >::epsilon() )
72  {
73  x /= mag;
74  y /= mag;
75  z /= mag;
76  }
77 }
78 
79 void Util::centroid( Interface* MB, EntityHandle handle, CartVect& coord )
80 {
81  const EntityHandle* connectivity = NULL;
82  int number_nodes = 0;
83  // TODO make the return value nonvoid
84  ErrorCode rval = MB->get_connectivity( handle, connectivity, number_nodes, true );
85  MB_CHK_SET_ERR_RET( rval, "can't get_connectivity" );
86 
87  coord[0] = coord[1] = coord[2] = 0.0;
88 
89  for( int i = 0; i < number_nodes; i++ )
90  {
91  double node_coords[3];
92  MB->get_coords( &( connectivity[i] ), 1, node_coords );
93 
94  coord[0] += node_coords[0];
95  coord[1] += node_coords[1];
96  coord[2] += node_coords[2];
97  }
98 
99  coord[0] /= (double)number_nodes;
100  coord[1] /= (double)number_nodes;
101  coord[2] /= (double)number_nodes;
102 }
103 
104 /*//This function calculates the coordinates for the centers of each edges of the entity specified
105 by handle. The coordinates are returned in the list coords_list void Util::edge_centers(Interface
106 *MB, EntityHandle handle, std::vector<Coord> &coords_list)
107 {
108  MB canon_tool(MB);
109  EntityType type;
110  int i = 0;
111  int number_nodes = 0;
112  double coords[2][3];
113  const EntityHandle *connectivity;
114 
115  MB->get_connectivity(handle, connectivity, number_nodes,true);
116 
117  MB->type_from_handle(handle,type);
118 
119  const struct MBCN::ConnMap* conn_map = &(canon_tool.mConnectivityMap[type][0]); //get edge
120 sub_elements
121 
122  coords_list.resize(conn_map->num_sub_elements);
123 
124  for(i = 0; i<conn_map->num_sub_elements; i++)
125  {
126 
127  MB->get_coords(connectivity[conn_map->conn[i][0]], coords[0]);
128  MB->get_coords(connectivity[conn_map->conn[i][1]], coords[1]);
129 
130  coords_list[i].x = (coords[0][0] + coords[1][0])/2.0;
131  coords_list[i].y = (coords[0][1] + coords[1][1])/2.0;
132  coords_list[i].z = (coords[0][2] + coords[1][2])/2.0;
133  }
134 }
135 */
136 
137 /*
138 void Util::face_centers(Interface *MB, EntityHandle handle, std::vector<Coord> &coords_list)
139 {
140  MB canon_tool(MB);
141  EntityType type;
142  int i = 0;
143  int number_nodes = 0;
144  double node_coords[3];
145  const EntityHandle *connectivity;
146 
147  MB->get_connectivity(handle, connectivity, number_nodes,true);
148 
149  MB->type_from_handle(handle,type);
150 
151  const struct MBCN::ConnMap* conn_map = &(canon_tool.mConnectivityMap[type][1]); //get face
152 sub_elements
153 
154  coords_list.resize(conn_map->num_sub_elements);
155 
156  for(i = 0; i<conn_map->num_sub_elements;i++)
157  {
158  int number_nodes_per_element = conn_map->num_nodes_per_sub_element[i];
159 
160  for(int j = 0; j<number_nodes_per_element; j++)
161  {
162  MB->get_coords(connectivity[conn_map->conn[i][j]], node_coords);
163 
164  coords_list[i].x+=node_coords[0];
165  coords_list[i].y+=node_coords[1];
166  coords_list[i].z+=node_coords[2];
167  }
168 
169  coords_list[i].x/=(double)number_nodes_per_element;
170  coords_list[i].y/=(double)number_nodes_per_element;
171  coords_list[i].z/=(double)number_nodes_per_element;
172  }
173 }
174 */
175 
176 // Explicit template specializations
177 template bool Util::is_finite< double >( double value );
178 
179 } // namespace moab