Go to the documentation of this file.00001
00002 #ifndef imesh_face_h_
00003 #define imesh_face_h_
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include <vcl_vector.h>
00016 #include <vcl_set.h>
00017 #include <vcl_cassert.h>
00018 #include <vcl_memory.h>
00019 #include <vcl_utility.h>
00020 #include <vcl_string.h>
00021 #include <vcl_algorithm.h>
00022 #include <vgl/vgl_vector_3d.h>
00023
00024 #define imesh_invalid_idx (static_cast<unsigned int>(-1))
00025
00026
00027
00028 template <unsigned s>
00029 class imesh_regular_face
00030 {
00031 public:
00032
00033 imesh_regular_face() { for (unsigned i=0; i<s; ++i) verts_[i]=imesh_invalid_idx; }
00034
00035
00036 imesh_regular_face(const vcl_vector<unsigned int>& verts)
00037 {assert(verts.size()==s); for (unsigned i=0; i<s; ++i) verts_[i]=verts[i];}
00038
00039
00040 unsigned int num_verts() const { return s; }
00041
00042 void flip_orientation()
00043 {
00044 vcl_reverse(verts_,verts_+s);
00045 }
00046
00047
00048 unsigned int operator[] (unsigned int i) const { return verts_[i]; }
00049 unsigned int& operator[] (unsigned int i) { return verts_[i]; }
00050 protected:
00051 unsigned int verts_[s];
00052 };
00053
00054
00055
00056 class imesh_tri : public imesh_regular_face<3>
00057 {
00058 public:
00059 imesh_tri(unsigned int a, unsigned int b, unsigned int c)
00060 {
00061 verts_[0] = a;
00062 verts_[1] = b;
00063 verts_[2] = c;
00064 }
00065 };
00066
00067
00068 class imesh_quad : public imesh_regular_face<4>
00069 {
00070 public:
00071 imesh_quad(unsigned int a, unsigned int b,
00072 unsigned int c, unsigned int d)
00073 {
00074 verts_[0] = a;
00075 verts_[1] = b;
00076 verts_[2] = c;
00077 verts_[3] = d;
00078 }
00079 };
00080
00081
00082
00083 class imesh_face_array_base
00084 {
00085 public:
00086
00087 virtual ~imesh_face_array_base() {}
00088
00089
00090 virtual unsigned int regularity() const = 0;
00091
00092
00093 virtual unsigned int size() const = 0;
00094
00095
00096 virtual unsigned int num_verts(unsigned int f) const = 0;
00097
00098
00099 virtual unsigned int operator() (unsigned int f, unsigned int i) const = 0;
00100
00101
00102 virtual void flip_orientation (unsigned int f)
00103 {
00104 if (has_normals())
00105 normals_[f] *= -1;
00106 }
00107
00108
00109 virtual imesh_face_array_base* clone() const = 0;
00110
00111
00112
00113 virtual void append(const imesh_face_array_base& other,
00114 unsigned int ind_shift=0);
00115
00116
00117 bool has_normals() const { return !normals_.empty(); }
00118
00119
00120 void set_normals(const vcl_vector<vgl_vector_3d<double> >& n)
00121 { assert(n.size() == this->size()); normals_ = n; }
00122
00123
00124 vgl_vector_3d<double>& normal(unsigned int f) { return normals_[f]; }
00125 const vgl_vector_3d<double>& normal(unsigned int f) const { return normals_[f]; }
00126
00127
00128 const vcl_vector<vgl_vector_3d<double> >& normals() const { return normals_; }
00129
00130
00131 bool has_groups() const { return !groups_.empty(); }
00132
00133
00134 vcl_string group_name(unsigned int f) const;
00135
00136
00137
00138 unsigned int make_group(const vcl_string& name);
00139
00140
00141 vcl_set<unsigned int> group_face_set(const vcl_string& name) const;
00142
00143
00144 const vcl_vector<vcl_pair<vcl_string,unsigned int> >& groups() const { return groups_; }
00145
00146 protected:
00147
00148
00149 vcl_vector<vcl_pair<vcl_string,unsigned int> > groups_;
00150
00151
00152 vcl_vector<vgl_vector_3d<double> > normals_;
00153 };
00154
00155
00156
00157 class imesh_face_array : public imesh_face_array_base
00158 {
00159 vcl_vector<vcl_vector<unsigned int> > faces_;
00160
00161 public:
00162
00163 imesh_face_array() {}
00164
00165
00166 imesh_face_array(unsigned int size) : faces_(size) {}
00167
00168
00169 imesh_face_array(const vcl_vector<vcl_vector<unsigned int> >& faces)
00170 : faces_(faces) {}
00171
00172
00173 imesh_face_array(const imesh_face_array& other)
00174 : imesh_face_array_base(other), faces_(other.faces_) {}
00175
00176
00177 explicit imesh_face_array(const imesh_face_array_base& fb)
00178 : imesh_face_array_base(fb), faces_(fb.size())
00179 {
00180 for (unsigned int i=0; i<fb.size(); ++i)
00181 for (unsigned int j=0; j<fb.num_verts(i); ++j)
00182 faces_[i].push_back(fb(i,j));
00183 }
00184
00185
00186 virtual unsigned int regularity() const { return 0; }
00187
00188
00189 virtual unsigned int size() const { return faces_.size(); }
00190
00191
00192 virtual unsigned int num_verts(unsigned int f) const { return faces_[f].size(); }
00193
00194
00195 virtual unsigned int operator() (unsigned int f, unsigned int i) const { return faces_[f][i]; }
00196
00197
00198 virtual void flip_orientation (unsigned int f)
00199 {
00200 vcl_reverse(faces_[f].begin(),faces_[f].end());
00201 imesh_face_array_base::flip_orientation(f);
00202 }
00203
00204
00205 virtual imesh_face_array_base* clone() const
00206 {
00207 return new imesh_face_array(*this);
00208 }
00209
00210
00211
00212 virtual void append(const imesh_face_array_base& other,
00213 unsigned int ind_shift=0);
00214
00215
00216 void push_back(const vcl_vector<unsigned int>& f) { faces_.push_back(f); }
00217
00218
00219 template <unsigned int s>
00220 void push_back(const imesh_regular_face<s>& f)
00221 {
00222 vcl_vector<unsigned int> f2(s);
00223 for (unsigned int i=0; i<s; ++i)
00224 f2[i] = f[i];
00225 this->push_back(f2);
00226 }
00227
00228
00229 vcl_vector<unsigned int>& operator[] (unsigned int f) { return faces_[f]; }
00230 const vcl_vector<unsigned int>& operator[] (unsigned int f) const { return faces_[f]; }
00231 };
00232
00233
00234
00235 template <unsigned int s>
00236 class imesh_regular_face_array : public imesh_face_array_base
00237 {
00238 vcl_vector<imesh_regular_face<s> > faces_;
00239
00240 public:
00241
00242 imesh_regular_face_array<s>() {}
00243
00244
00245 imesh_regular_face_array<s>(unsigned int size) : faces_(size) {}
00246
00247
00248 imesh_regular_face_array<s>(const vcl_vector<imesh_regular_face<s> >& faces) : faces_(faces) {}
00249
00250
00251
00252 virtual unsigned int regularity() const { return s; }
00253
00254
00255 virtual unsigned int size() const { return faces_.size(); }
00256
00257
00258 virtual unsigned int num_verts(unsigned int ) const { return s; }
00259
00260
00261 virtual unsigned int operator() (unsigned int f, unsigned int i) const { return faces_[f][i]; }
00262
00263
00264 virtual void flip_orientation (unsigned int f)
00265 {
00266 faces_[f].flip_orientation();
00267 imesh_face_array_base::flip_orientation(f);
00268 }
00269
00270
00271 virtual imesh_face_array_base* clone() const
00272 {
00273 return new imesh_regular_face_array<s>(*this);
00274 }
00275
00276
00277
00278 virtual void append(const imesh_face_array_base& other,
00279 unsigned int ind_shift=0)
00280 {
00281 imesh_face_array_base::append(other,ind_shift);
00282 assert(other.regularity() == s);
00283 const imesh_regular_face_array<s>& fs =
00284 static_cast<const imesh_regular_face_array<s>&>(other);
00285 const unsigned int new_begin = faces_.size();
00286 faces_.insert(faces_.end(), fs.faces_.begin(), fs.faces_.end());
00287 if (ind_shift > 0) {
00288 for (unsigned int i=new_begin; i<faces_.size(); ++i) {
00289 imesh_regular_face<s>& f = faces_[i];
00290 for (unsigned int j=0; j<s; ++j)
00291 f[j] += ind_shift;
00292 }
00293 }
00294 }
00295
00296
00297 void push_back(const imesh_regular_face<s>& f) { faces_.push_back(f); }
00298
00299
00300 imesh_regular_face<s>& operator[] (unsigned int f) { return faces_[f]; }
00301 const imesh_regular_face<s>& operator[] (unsigned int f) const { return faces_[f]; }
00302
00303
00304
00305 typedef typename vcl_vector<imesh_regular_face<s> >::iterator iterator;
00306 typedef typename vcl_vector<imesh_regular_face<s> >::const_iterator const_iterator;
00307
00308 iterator begin() { return faces_.begin(); }
00309 const_iterator begin() const { return faces_.begin(); }
00310
00311 iterator end() { return faces_.end(); }
00312 const_iterator end() const { return faces_.end(); }
00313 };
00314
00315
00316
00317
00318 vcl_auto_ptr<imesh_face_array_base>
00319 imesh_merge(const imesh_face_array_base& f1,
00320 const imesh_face_array_base& f2,
00321 unsigned int ind_shift=0);
00322
00323 #endif // imesh_face_h_