MOAB: Mesh Oriented datABase  (version 5.2.1)
WeightReader.cpp
Go to the documentation of this file.
00001 /* *****************************************************************
00002     MESQUITE -- The Mesh Quality Improvement Toolkit
00003 
00004     Copyright 2006 Sandia National Laboratories.  Developed at the
00005     University of Wisconsin--Madison under SNL contract number
00006     624796.  The U.S. Government and the University of Wisconsin
00007     retain certain rights to 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     This library is distributed in the hope that it will be useful,
00015     but WITHOUT ANY WARRANTY; without even the implied warranty of
00016     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017     Lesser General Public License for more details.
00018 
00019     You should have received a copy of the GNU Lesser General Public License
00020     (lgpl.txt) along with this library; if not, write to the Free Software
00021     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022 
00023     (2006) kraftche@cae.wisc.edu
00024 
00025   ***************************************************************** */
00026 
00027 /** \file WeightReader.cpp
00028  *  \brief
00029  *  \author Jason Kraftcheck
00030  */
00031 
00032 #include "Mesquite.hpp"
00033 #include "WeightReader.hpp"
00034 #include "PatchData.hpp"
00035 #include "MsqError.hpp"
00036 #include "ElemSampleQM.hpp"
00037 #include <sstream>
00038 
00039 namespace MBMesquite
00040 {
00041 
00042 static TagHandle get_tag( Mesh* mesh, unsigned num_doubles, const std::string& base_name, MsqError& err )
00043 {
00044     std::ostringstream str;
00045     str << base_name << num_doubles;
00046 
00047     TagHandle handle = mesh->tag_get( str.str().c_str(), err );
00048     MSQ_ERRZERO( err );
00049 
00050     // double check tag type
00051     std::string temp_name;
00052     Mesh::TagType temp_type;
00053     unsigned temp_length;
00054     mesh->tag_properties( handle, temp_name, temp_type, temp_length, err );
00055     MSQ_ERRZERO( err );
00056 
00057     if( temp_type != Mesh::DOUBLE || temp_length != num_doubles )
00058     {
00059         MSQ_SETERR( err )
00060         ( MsqError::TAG_ALREADY_EXISTS, "Mismatched type or length for existing tag \"%s\"", str.str().c_str() );
00061     }
00062 
00063     return handle;
00064 }
00065 
00066 WeightReader::WeightReader( std::string name ) : tagBaseName( name ) {}
00067 
00068 WeightReader::~WeightReader() {}
00069 
00070 double WeightReader::get_weight( PatchData& pd, size_t element, Sample sample, MsqError& err )
00071 {
00072     WeightReaderData& data = get_data( pd );
00073 
00074     // calculate index of sample in array
00075     NodeSet all_samples = pd.get_samples( element );
00076     unsigned offset     = all_samples.num_before( sample );
00077 
00078     if( !data.weights.empty() && data.elementIndex == element )
00079     {
00080         assert( offset < data.weights.size() );
00081         return data.weights[offset];
00082     }
00083     const unsigned num_samples = all_samples.num_nodes();
00084     const unsigned handle_idx  = num_samples - 1;
00085 
00086     // get the tag handle
00087     const TagHandle INVALID_HANDLE = (TagHandle)-1;
00088     if( data.handles.size() <= handle_idx ) data.handles.resize( handle_idx + 1, INVALID_HANDLE );
00089     TagHandle& tag_handle = data.handles[handle_idx];
00090     if( tag_handle == INVALID_HANDLE )
00091     {
00092         tag_handle = get_tag( pd.get_mesh(), num_samples, tagBaseName.c_str(), err );
00093         MSQ_ERRZERO( err );
00094         assert( tag_handle != INVALID_HANDLE );
00095     }
00096 
00097     // get the tag data
00098     data.weights.resize( num_samples );
00099     pd.get_mesh()->tag_get_element_data( tag_handle, 1, pd.get_element_handles_array() + element, &data.weights[0],
00100                                          err );
00101     if( MSQ_CHKERR( err ) )
00102     {
00103         data.weights.clear();
00104         return false;
00105     }
00106 
00107     data.elementIndex = element;
00108 
00109     assert( offset < num_samples );
00110     return data.weights[offset];
00111 }
00112 
00113 void WeightReader::notify_patch_destroyed( WeightReaderData& data )
00114 {
00115     data.handles.clear();
00116     data.weights.clear();
00117 }
00118 
00119 void WeightReader::notify_new_patch( PatchData&, WeightReaderData& data )
00120 {
00121     data.weights.clear();
00122 }
00123 
00124 void WeightReader::notify_sub_patch( PatchData& /*pd*/, WeightReaderData& data, PatchData& subpatch, const size_t*,
00125                                      const size_t*, MsqError& /*err*/ )
00126 {
00127     WeightReaderData& other = get_data( subpatch );
00128     if( other.handles.empty() ) other.handles = data.handles;
00129 }
00130 
00131 }  // namespace MBMesquite
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines