MOAB: Mesh Oriented datABase  (version 5.4.1)
Util.cpp
Go to the documentation of this file.
00001 /**
00002  * MOAB, a Mesh-Oriented datABase, is a software component for creating,
00003  * storing and accessing finite element mesh data.
00004  *
00005  * Copyright 2004 Sandia Corporation.  Under the terms of Contract
00006  * DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government
00007  * retains certain rights in this software.
00008  *
00009  * This library is free software; you can redistribute it and/or
00010  * modify it under the terms of the GNU Lesser General Public
00011  * License as published by the Free Software Foundation; either
00012  * version 2.1 of the License, or (at your option) any later version.
00013  *
00014  */
00015 
00016 //-------------------------------------------------------------------------
00017 // Purpose       : This file contains utility functions for use in MOAB
00018 //
00019 // Special Notes : This is a pure virtual class, to prevent instantiation.
00020 //                 All functions are static, called like this:
00021 //                 Util::function_name();
00022 //-------------------------------------------------------------------------
00023 #include "moab/Util.hpp"
00024 #include "moab/Interface.hpp"
00025 #include <cassert>
00026 #include <algorithm>
00027 #include <limits>
00028 #if defined( _MSC_VER ) || defined( __MINGW32__ )
00029 #include <float.h>
00030 #define finite( A ) _finite( A )
00031 #ifndef MOAB_HAVE_FINITE
00032 #define MOAB_HAVE_FINITE
00033 #endif
00034 #endif
00035 
00036 namespace moab
00037 {
00038 
00039 //! temporary normal function for MBEntities.  This should be moved to
00040 //! an appropriate MB algorithms file
00041 
00042 void Util::normal( Interface* MB, EntityHandle handle, double& x, double& y, double& z )
00043 {
00044     // get connectivity
00045     const EntityHandle* connectivity = NULL;
00046     int number_nodes                 = 0;
00047     // TODO make the return value nonvoid
00048     ErrorCode rval = MB->get_connectivity( handle, connectivity, number_nodes, true );MB_CHK_SET_ERR_RET( rval, "can't get_connectivity" );
00049     assert( number_nodes >= 3 );
00050 
00051     // get_coordinates
00052     double coords[3][3];
00053     MB->get_coords( &( connectivity[0] ), 1, coords[0] );
00054     MB->get_coords( &( connectivity[1] ), 1, coords[1] );
00055     MB->get_coords( &( connectivity[2] ), 1, coords[2] );
00056 
00057     double vecs[2][3];
00058     vecs[0][0] = coords[1][0] - coords[0][0];
00059     vecs[0][1] = coords[1][1] - coords[0][1];
00060     vecs[0][2] = coords[1][2] - coords[0][2];
00061     vecs[1][0] = coords[2][0] - coords[0][0];
00062     vecs[1][1] = coords[2][1] - coords[0][1];
00063     vecs[1][2] = coords[2][2] - coords[0][2];
00064 
00065     x = vecs[0][1] * vecs[1][2] - vecs[0][2] * vecs[1][1];
00066     y = vecs[0][2] * vecs[1][0] - vecs[0][0] * vecs[1][2];
00067     z = vecs[0][0] * vecs[1][1] - vecs[0][1] * vecs[1][0];
00068 
00069     double mag = sqrt( x * x + y * y + z * z );
00070     if( mag > std::numeric_limits< double >::epsilon() )
00071     {
00072         x /= mag;
00073         y /= mag;
00074         z /= mag;
00075     }
00076 }
00077 
00078 void Util::centroid( Interface* MB, EntityHandle handle, CartVect& coord )
00079 {
00080     const EntityHandle* connectivity = NULL;
00081     int number_nodes                 = 0;
00082     // TODO make the return value nonvoid
00083     ErrorCode rval = MB->get_connectivity( handle, connectivity, number_nodes, true );MB_CHK_SET_ERR_RET( rval, "can't get_connectivity" );
00084 
00085     coord[0] = coord[1] = coord[2] = 0.0;
00086 
00087     for( int i = 0; i < number_nodes; i++ )
00088     {
00089         double node_coords[3];
00090         MB->get_coords( &( connectivity[i] ), 1, node_coords );
00091 
00092         coord[0] += node_coords[0];
00093         coord[1] += node_coords[1];
00094         coord[2] += node_coords[2];
00095     }
00096 
00097     coord[0] /= (double)number_nodes;
00098     coord[1] /= (double)number_nodes;
00099     coord[2] /= (double)number_nodes;
00100 }
00101 
00102 /*//This function calculates the coordinates for the centers of each edges of the entity specified
00103 by handle. The coordinates are returned in the list coords_list void Util::edge_centers(Interface
00104 *MB, EntityHandle handle, std::vector<Coord> &coords_list)
00105 {
00106   MB canon_tool(MB);
00107   EntityType type;
00108   int i = 0;
00109   int number_nodes = 0;
00110   double coords[2][3];
00111   const EntityHandle *connectivity;
00112 
00113   MB->get_connectivity(handle, connectivity, number_nodes,true);
00114 
00115   MB->type_from_handle(handle,type);
00116 
00117   const struct MBCN::ConnMap* conn_map = &(canon_tool.mConnectivityMap[type][0]); //get edge
00118 sub_elements
00119 
00120   coords_list.resize(conn_map->num_sub_elements);
00121 
00122   for(i = 0; i<conn_map->num_sub_elements; i++)
00123   {
00124 
00125     MB->get_coords(connectivity[conn_map->conn[i][0]], coords[0]);
00126     MB->get_coords(connectivity[conn_map->conn[i][1]], coords[1]);
00127 
00128     coords_list[i].x = (coords[0][0] + coords[1][0])/2.0;
00129     coords_list[i].y = (coords[0][1] + coords[1][1])/2.0;
00130     coords_list[i].z = (coords[0][2] + coords[1][2])/2.0;
00131   }
00132 }
00133 */
00134 
00135 /*
00136 void Util::face_centers(Interface *MB, EntityHandle handle, std::vector<Coord> &coords_list)
00137 {
00138   MB canon_tool(MB);
00139   EntityType type;
00140   int i = 0;
00141   int number_nodes = 0;
00142   double node_coords[3];
00143   const EntityHandle *connectivity;
00144 
00145   MB->get_connectivity(handle, connectivity, number_nodes,true);
00146 
00147   MB->type_from_handle(handle,type);
00148 
00149   const struct MBCN::ConnMap* conn_map = &(canon_tool.mConnectivityMap[type][1]); //get face
00150 sub_elements
00151 
00152   coords_list.resize(conn_map->num_sub_elements);
00153 
00154   for(i = 0; i<conn_map->num_sub_elements;i++)
00155   {
00156     int number_nodes_per_element = conn_map->num_nodes_per_sub_element[i];
00157 
00158     for(int j = 0; j<number_nodes_per_element; j++)
00159     {
00160       MB->get_coords(connectivity[conn_map->conn[i][j]], node_coords);
00161 
00162       coords_list[i].x+=node_coords[0];
00163       coords_list[i].y+=node_coords[1];
00164       coords_list[i].z+=node_coords[2];
00165     }
00166 
00167     coords_list[i].x/=(double)number_nodes_per_element;
00168     coords_list[i].y/=(double)number_nodes_per_element;
00169     coords_list[i].z/=(double)number_nodes_per_element;
00170   }
00171 }
00172 */
00173 
00174 // Explicit template specializations
00175 template bool Util::is_finite< double >( double value );
00176 
00177 }  // namespace moab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines