MeshKit  1.0
CESets.cpp
Go to the documentation of this file.
00001 #include "meshkit/CESets.hpp"
00002 
00003 #include <cassert>
00004 #include <cstdlib>
00005 #include <cstring>
00006 #include <iMesh_extensions.h>
00007 
00008 #include <meshkit/Error.hpp>
00009 
00010 namespace MeshKit {
00011 
00012 CESets::~CESets()
00013 {
00014   std::vector<tag_data>::iterator i;
00015   for (i = tags_.begin(); i != tags_.end(); ++i)
00016     free(i->value);
00017 }
00018 
00019 void CESets::add_tag(iMesh::TagHandle tag_handle, const char *value)
00020 {
00021   assert(tag_handle != NULL);
00022   char *tmp = NULL;
00023 
00024   if (value) {
00025     int tag_size;
00026     IBERRCHK(mesh_->getTagSizeBytes(tag_handle, tag_size), *mesh_);
00027 
00028     tmp = static_cast<char*>(malloc(tag_size));
00029     memcpy(tmp, value, tag_size);
00030   }
00031 
00032   tags_.push_back(tag_data(tag_handle, tmp));
00033 }
00034 
00035 void CESets::add_tag(const std::string &tag_name, const char *value)
00036 {
00037   iMesh::TagHandle tag_handle;
00038   IBERRCHK(mesh_->getTagHandle(tag_name.c_str(), tag_handle), *mesh_);
00039   add_tag(tag_handle, value);
00040 }
00041 
00042 void CESets::update_tagged_sets()
00043 {
00044   int err;
00045   iMesh::EntitySetHandle root_set = mesh_->getRootSet();
00046 
00047   std::vector<tag_data>::const_iterator tag;
00048   for (tag = tags_.begin(); tag != tags_.end(); ++tag) {
00049     iMesh::EntitySetHandle *tmp_sets = NULL;
00050     int tmp_sets_alloc = 0, tmp_sets_size;
00051     iMesh_getEntSetsByTagsRec(mesh_->instance(), root_set, &tag->tag,
00052                               (tag->value ? &tag->value : NULL), 1, false,
00053                               &tmp_sets, &tmp_sets_alloc, &tmp_sets_size, &err);
00054     IBERRCHK(err, *mesh_);
00055     sets_.insert(tmp_sets, tmp_sets+tmp_sets_size);
00056     free(tmp_sets);
00057   }
00058 }
00059 
00060 void link_expand_sets(const CESets &ce_sets, iMesh::TagHandle local_tag)
00061 {
00062   CESets::set_iterator set;
00063   for (set = ce_sets.sbegin(); set != ce_sets.send(); ++set) {
00064     IBERRCHK(ce_sets.imesh_instance()->setEntSetESHData(*set, local_tag, *set),
00065              *ce_sets.imesh_instance());
00066   }
00067 }
00068 
00078 static
00079 void get_copied_ents(iMesh *mesh, iMesh::EntitySetHandle set,
00080                      iMesh::TagHandle local_tag,
00081                      std::vector<iMesh::EntityHandle> &ents)
00082 {
00083   std::vector<iMesh::EntityHandle> tmp_ents;
00084   IBERRCHK(mesh->getEntities(set, iBase_ALL_TYPES, iMesh_ALL_TOPOLOGIES, 
00085                              tmp_ents), *mesh);
00086 
00087   ents.reserve(tmp_ents.size());
00088 
00089   // get copied entities
00090   for (size_t i = 0; i < tmp_ents.size(); i++) {
00091     iMesh::EntityHandle eh_tag;
00092     iMesh::Error err = mesh->getEHData(tmp_ents[i], local_tag, eh_tag);
00093     if (err == iBase_SUCCESS && eh_tag)
00094       ents.push_back(eh_tag);
00095   }
00096 }
00097 
00107 static
00108 void get_dest_set(iMesh *mesh, iMesh::TagHandle local_tag,
00109                   iMesh::EntitySetHandle src, iMesh::EntitySetHandle &dest)
00110 {
00111   iMesh::Error err;
00112 
00113   err = mesh->getEntSetESHData(src, local_tag, dest);
00114 
00115   if (err != iBase_SUCCESS) {
00116     IBERRCHK(mesh->createEntSet(false, dest), *mesh);
00117     IBERRCHK(mesh->setEntSetESHData(src, local_tag, dest), *mesh);
00118   }
00119 }
00120 
00133 static
00134 void process_ce_subsets(iMesh *mesh, iMesh::EntitySetHandle src,
00135                         iMesh::EntitySetHandle current,
00136                         const std::set<iMesh::EntitySetHandle> &cesets,
00137                         iMesh::TagHandle local_tag) 
00138 {
00139   iMesh::EntitySetHandle dest;
00140 
00141   // First, add entities directly contained in this set.
00142   std::vector<iMesh::EntityHandle> tmp_tags;
00143   get_copied_ents(mesh, current, local_tag, tmp_tags);
00144 
00145   if (!tmp_tags.empty()) {
00146     get_dest_set(mesh, local_tag, src, dest);
00147     IBERRCHK(mesh->addEntArrToSet(&tmp_tags[0], tmp_tags.size(), dest), *mesh);
00148   }
00149 
00150   // Next, start looking at children.
00151   std::vector<iMesh::EntitySetHandle> children;
00152   IBERRCHK(mesh->getEntSets(current, 0, children), *mesh);
00153 
00154   for (size_t i = 0; i < children.size(); i++) {
00155 
00156     // If this child set is one of our cesets, add just the set...
00157     if (cesets.find(children[i]) != cesets.end()) {
00158       get_dest_set(mesh, local_tag, src, dest);
00159       if (src == dest) continue;
00160 
00161       IBERRCHK(mesh->addEntSet(children[i], dest), *mesh);
00162     }
00163 
00164     // ... otherwise, add the entities and recurse into the next level of
00165     // children.
00166     else {
00167       process_ce_subsets(mesh, src, children[i], cesets, local_tag);
00168     }
00169   }
00170 }
00171 
00172 // xxx - still need to do unique tags
00173 // - for each unique tag
00174 //   . if this set has the tag, add/append to get unique value
00175 void process_ce_sets(iMesh *mesh,
00176                      const std::set<iMesh::EntitySetHandle> &cesets,
00177                      iMesh::TagHandle local_tag) 
00178 {
00179   std::set<iMesh::EntitySetHandle>::const_iterator src;
00180   for (src = cesets.begin(); src != cesets.end(); ++src)
00181     process_ce_subsets(mesh, *src, *src, cesets, local_tag);
00182 }
00183 
00184 void tag_copy_sets(iMesh *mesh, iMesh::TagHandle copyTag,
00185                    const std::set<iMesh::EntitySetHandle> &copySets,
00186                    iMesh::TagHandle tag, const char *tag_val)
00187 {
00188   iMesh::Error err;
00189 
00190   int tag_size;
00191   IBERRCHK(mesh->getTagSizeBytes(tag, tag_size), *mesh);
00192 
00193   // allocate temp space for tag value
00194   std::vector<char> value(tag_size);
00195   
00196   // for each orig copy set with this tag, copy it to its copy
00197   std::set<iMesh::EntitySetHandle>::iterator set;
00198   for (set = copySets.begin(); set != copySets.end(); ++set) {
00199     // get the tag value
00200     err = mesh->getEntSetData(*set, tag, &value[0]);
00201     if (err == iBase_TAG_NOT_FOUND)
00202       continue;
00203     IBERRCHK(err, *mesh);
00204 
00205     // compare to tag value if necessary
00206     if (tag_val && strncmp(tag_val, &value[0], tag_size))
00207       continue;
00208       
00209     // if we got here, we should set the tag on the copy; get the copy
00210     iMesh::EntitySetHandle copy_set;
00211     err = mesh->getEntSetESHData(*set, copyTag, copy_set);
00212     if (err == iBase_TAG_NOT_FOUND)
00213       continue;
00214     IBERRCHK(err, *mesh);
00215 
00216     if (copy_set != *set)
00217       IBERRCHK(mesh->setEntSetData(copy_set, tag, &value[0]), *mesh);
00218   }
00219 }
00220 
00221 void tag_copy_sets(const CESets &ce_sets, iMesh::TagHandle local_tag,
00222                    iMesh::TagHandle copy_tag)
00223 {
00224   // set the copy tag on all copied sets
00225   for (CESets::const_set_iterator set = ce_sets.sbegin(); set != ce_sets.send();
00226        ++set) {
00227     iMesh::Error err;
00228     iMesh::EntityHandle eh;
00229     err = ce_sets.imesh_instance()->getEntSetEHData(*set, local_tag, eh);
00230     if (err == iBase_SUCCESS) {
00231       IBERRCHK(ce_sets.imesh_instance()->setEntSetEHData(*set, copy_tag, eh),
00232                *ce_sets.imesh_instance());
00233     }
00234   }
00235 
00236   // tag the newly-created sets
00237   for (CESets::const_tag_iterator tag = ce_sets.tbegin();
00238        tag != ce_sets.tend(); ++tag) {
00239     tag_copy_sets(ce_sets.imesh_instance(), copy_tag, ce_sets.sets(),
00240                   tag->tag, tag->value);
00241   }
00242 }
00243 
00244 } // namespace MeshKit
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines