moab
|
00001 00016 //------------------------------------------------------------------------- 00017 // Filename : Util.cpp 00018 // 00019 // Purpose : This file contains utility functions that can be used 00020 // with MB 00021 // 00022 // Special Notes : This is a pure virtual class, to prevent instantiation. 00023 // All functions are static, called like this: 00024 // Util::function_name(); 00025 // 00026 // Creator : Ray J. Meyers 00027 // 00028 // Date : 09/01/02 00029 // 00030 // Owner : Ray J. meyers 00031 //------------------------------------------------------------------------- 00032 00033 #include "moab/Util.hpp" 00034 #include "moab/Interface.hpp" 00035 #include <assert.h> 00036 #include <math.h> 00037 #include <algorithm> 00038 #include <limits> 00039 00040 namespace moab { 00041 00044 00045 void Util::normal(Interface* MB, EntityHandle handle, double& x, double& y, double& z) 00046 { 00047 // get connectivity 00048 const EntityHandle *connectivity; 00049 int number_nodes = 0; 00050 MB->get_connectivity(handle, connectivity, number_nodes, true); 00051 assert(number_nodes >= 3); 00052 00053 // get_coordinates 00054 double coords[3][3]; 00055 MB->get_coords(&(connectivity[0]), 1, coords[0]); 00056 MB->get_coords(&(connectivity[1]), 1, coords[1]); 00057 MB->get_coords(&(connectivity[2]), 1, coords[2]); 00058 00059 double vecs[2][3]; 00060 vecs[0][0] = coords[1][0] - coords[0][0]; 00061 vecs[0][1] = coords[1][1] - coords[0][1]; 00062 vecs[0][2] = coords[1][2] - coords[0][2]; 00063 vecs[1][0] = coords[2][0] - coords[0][0]; 00064 vecs[1][1] = coords[2][1] - coords[0][1]; 00065 vecs[1][2] = coords[2][2] - coords[0][2]; 00066 00067 x = vecs[0][1] * vecs[1][2] - vecs[0][2] * vecs[1][1]; 00068 y = vecs[0][2] * vecs[1][0] - vecs[0][0] * vecs[1][2]; 00069 z = vecs[0][0] * vecs[1][1] - vecs[0][1] * vecs[1][0]; 00070 00071 double mag = sqrt(x*x + y*y + z*z); 00072 if(mag != std::numeric_limits<double>::epsilon()) 00073 { 00074 x /= mag; 00075 y /= mag; 00076 z /= mag; 00077 } 00078 } 00079 00080 void Util::centroid(Interface *MB, EntityHandle handle, Coord &coord) 00081 { 00082 const EntityHandle *connectivity; 00083 int number_nodes = 0; 00084 MB->get_connectivity(handle, connectivity, number_nodes,true); 00085 00086 coord.x=0.0; 00087 coord.y=0.0; 00088 coord.z=0.0; 00089 00090 for(int i = 0; i< number_nodes; i++) 00091 { 00092 double node_coords[3]; 00093 MB->get_coords(&(connectivity[i]), 1, node_coords); 00094 00095 coord.x+=node_coords[0]; 00096 coord.y+=node_coords[1]; 00097 coord.z+=node_coords[2]; 00098 } 00099 00100 coord.x/=(double)number_nodes; 00101 coord.y/=(double)number_nodes; 00102 coord.z/=(double)number_nodes; 00103 } 00104 00105 /*//This function calculates the coordinates for the centers of each edges of the entity specified by handle. The coordinates are returned in the list coords_list 00106 void Util::edge_centers(Interface *MB, EntityHandle handle, std::vector<Coord> &coords_list) 00107 { 00108 MB canon_tool(MB); 00109 EntityType type; 00110 int i = 0; 00111 int number_nodes = 0; 00112 double coords[2][3]; 00113 const EntityHandle *connectivity; 00114 00115 MB->get_connectivity(handle, connectivity, number_nodes,true); 00116 00117 MB->type_from_handle(handle,type); 00118 00119 const struct MBCN::ConnMap* conn_map = &(canon_tool.mConnectivityMap[type][0]); //get edge sub_elements 00120 00121 coords_list.resize(conn_map->num_sub_elements); 00122 00123 for(i = 0; i<conn_map->num_sub_elements; i++) 00124 { 00125 00126 MB->get_coords(connectivity[conn_map->conn[i][0]], coords[0]); 00127 MB->get_coords(connectivity[conn_map->conn[i][1]], coords[1]); 00128 00129 coords_list[i].x = (coords[0][0] + coords[1][0])/2.0; 00130 coords_list[i].y = (coords[0][1] + coords[1][1])/2.0; 00131 coords_list[i].z = (coords[0][2] + coords[1][2])/2.0; 00132 } 00133 } 00134 */ 00135 00136 /* 00137 void Util::face_centers(Interface *MB, EntityHandle handle, std::vector<Coord> &coords_list) 00138 { 00139 MB canon_tool(MB); 00140 EntityType type; 00141 int i = 0; 00142 int number_nodes = 0; 00143 double node_coords[3]; 00144 const EntityHandle *connectivity; 00145 00146 MB->get_connectivity(handle, connectivity, number_nodes,true); 00147 00148 MB->type_from_handle(handle,type); 00149 00150 const struct MBCN::ConnMap* conn_map = &(canon_tool.mConnectivityMap[type][1]); //get face 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 } // namespace moab