contrib/brl/bbas/imesh/imesh_face.cxx
Go to the documentation of this file.
00001 // This is brl/bbas/imesh/imesh_face.cxx
00002 #include "imesh_face.h"
00003 //:
00004 // \file
00005 
00006 
00007 //: Return the group name for a given face index
00008 vcl_string imesh_face_array_base::group_name(unsigned int f) const
00009 {
00010   if (groups_.empty())
00011     return "";
00012 
00013   unsigned int i=0;
00014   for (; i<groups_.size() && groups_[i].second<f; ++i)
00015     ;
00016 
00017   if (i>=groups_.size())
00018     return "";
00019 
00020   return groups_[i].first;
00021 }
00022 
00023 
00024 //: Return a set of all faces in a group
00025 vcl_set<unsigned int>
00026 imesh_face_array_base::group_face_set(const vcl_string& name) const
00027 {
00028   vcl_set<unsigned int> face_set;
00029   unsigned int start = 0, end;
00030   for (unsigned int g=0; g<groups_.size(); ++g) {
00031     end = groups_[g].second;
00032     if (groups_[g].first == name) {
00033       for (unsigned int i=start; i<end; ++i)
00034         face_set.insert(i);
00035     }
00036     start = end;
00037   }
00038   return face_set;
00039 }
00040 
00041 
00042 //: Assign a group name to all faces currently unnamed
00043 //  Return the number of faces in the new group
00044 unsigned int imesh_face_array_base::make_group(const vcl_string& name)
00045 {
00046   unsigned int start_idx = 0;
00047   if (!groups_.empty())
00048     start_idx = groups_.back().second;
00049 
00050   if (start_idx < this->size())
00051     groups_.push_back(vcl_pair<vcl_string,unsigned int>(name,this->size()));
00052 
00053   return this->size() - start_idx;
00054 }
00055 
00056 
00057 //: Append this array of faces (must be the same type)
00058 //  Optionally shift the indices in \param other by \param ind_shift
00059 void imesh_face_array_base::append(const imesh_face_array_base& other,
00060                                    unsigned int )
00061 {
00062   if (this->has_normals() && other.has_normals())
00063     normals_.insert(normals_.end(), other.normals_.begin(), other.normals_.end());
00064   else
00065     normals_.clear();
00066 
00067   if (other.has_groups()) {
00068     // group any ungrouped faces in this array
00069     this->make_group("ungrouped");
00070     unsigned int offset = this->size();
00071     for (unsigned int g=0; g<other.groups_.size(); ++g) {
00072       groups_.push_back(other.groups_[g]);
00073       groups_.back().second += offset;
00074     }
00075   }
00076 }
00077 
00078 
00079 //: Append this array of faces
00080 //  Optionally shift the indices in \param other by \param ind_shift
00081 void imesh_face_array::append(const imesh_face_array_base& other,
00082                               unsigned int ind_shift)
00083 {
00084   imesh_face_array_base::append(other,ind_shift);
00085 
00086   const unsigned int new_begin = faces_.size();
00087 
00088   if (other.regularity() == 0) {
00089     const imesh_face_array& fs = static_cast<const imesh_face_array&>(other);
00090     faces_.insert(faces_.end(), fs.faces_.begin(), fs.faces_.end());
00091 
00092     if (ind_shift > 0) {
00093       for (unsigned int i=new_begin; i<faces_.size(); ++i) {
00094         vcl_vector<unsigned int>& f = faces_[i];
00095         for (unsigned int j=0; j<f.size(); ++j)
00096           f[j] += ind_shift;
00097       }
00098     }
00099   }
00100   else {
00101     for (unsigned int i=0; i<other.size(); ++i) {
00102       vcl_vector<unsigned int> f(other.num_verts(i));
00103       for (unsigned int j=0; j<other.num_verts(i); ++j)
00104         f[j] = other(i,j) + ind_shift;
00105       faces_.push_back(f);
00106     }
00107   }
00108 }
00109 
00110 
00111 //: Merge the two face arrays
00112 vcl_auto_ptr<imesh_face_array_base>
00113 imesh_merge(const imesh_face_array_base& f1,
00114             const imesh_face_array_base& f2,
00115             unsigned int ind_shift)
00116 {
00117   vcl_auto_ptr<imesh_face_array_base> f;
00118   // if both face sets are regular with the same number of vertices per face
00119   if (f1.regularity() == f2.regularity() || f1.regularity() == 0) {
00120     f.reset(f1.clone());
00121   }
00122   else {
00123     f.reset(new imesh_face_array(f1));
00124   }
00125   f->append(f2,ind_shift);
00126   return f;
00127 }