File: foreach.h

package info (click to toggle)
meshlab 2020.09%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 45,132 kB
  • sloc: cpp: 400,238; ansic: 31,952; javascript: 1,578; sh: 387; yacc: 238; lex: 139; python: 86; makefile: 30
file content (211 lines) | stat: -rw-r--r-- 5,781 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/****************************************************************************
* VCGLib                                                            o o     *
* Visual and Computer Graphics Library                            o     o   *
*                                                                _   O  _   *
* Copyright(C) 2004-2017                                           \/)\/    *
* Visual Computing Lab                                            /\/|      *
* ISTI - Italian National Research Council                           |      *
*                                                                    \      *
* All rights reserved.                                                      *
*                                                                           *
* This program is free software; you can redistribute it and/or modify      *
* it under the terms of the GNU General Public License as published by      *
* the Free Software Foundation; either version 2 of the License, or         *
* (at your option) any later version.                                       *
*                                                                           *
* This program is distributed in the hope that it will be useful,           *
* but WITHOUT ANY WARRANTY; without even the implied warranty of            *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt)          *
* for more details.                                                         *
*                                                                           *
****************************************************************************/

#ifndef VCG__FOREACH_H
#define VCG__FOREACH_H

#ifndef __VCG_MESH
#error "This file should not be included alone. It is automatically included by complex.h"
#endif

namespace vcg {
namespace tri {
/** \addtogroup trimesh
@{
*/

template <class MeshType>
inline void ForEachFacePos(MeshType &m, std::function<void (typename face::Pos<typename MeshType::FaceType>  &)> action)
{
  typedef typename face::Pos<typename MeshType::FaceType> PosType;
  
    for(auto fi=m.face.begin();fi!=m.face.end();++fi)
        if(!(*fi).IsD())
        {
            for(int i=0;i<3;++i)
            {
                PosType pi(&*fi,i);
                action(pi);
            }
        }
}

/**
 * ForEachFace Helper
 * to traverse all the faces of a mesh you can simply write something like:
 * 
 *      ForEachFace(m, [&](const FaceType &f){
 *         MakeSomethingWithFace(f);
 *      });
 *  
 */

template <class MeshType>
inline void ForEachFace(const MeshType &m, std::function<void (const typename MeshType::FaceType &)> action)
{
  if(m.fn == (int) m.face.size())
  {
    for(auto fi=m.face.begin();fi!=m.face.end();++fi) {
      action(*fi);
    }       
  }
  else
  {
    for(auto fi=m.face.begin();fi!=m.face.end();++fi)
      if(!(*fi).IsD())
      {
        action(*fi);
      }    
  }
}

template <class MeshType>
inline void ForEachFace(MeshType &m, std::function<void (typename MeshType::FaceType &)> action)
{
  if(m.fn == (int) m.face.size())
  {
    for(auto fi=m.face.begin();fi!=m.face.end();++fi) {
      action(*fi);
    }       
  }
  else
  {
    for(auto fi=m.face.begin();fi!=m.face.end();++fi)
      if(!(*fi).IsD())
      {
        action(*fi);
      }    
  }
}

/**
 * ForEachVertex Helper
 * to traverse all the vertexes of a mesh you can simply write something like:
 * 
 *      ForEachVertex(m, [&](const VertexType &v){
 *         MakeSomethingWithVertex(v);
 *      });
 *  
 */

template <class MeshType>
inline void ForEachVertex(MeshType &m, std::function<void (typename MeshType::VertexType &)> action)
{
  if(m.vn == (int) m.vert.size())
  {
    for(auto vi=m.vert.begin();vi!=m.vert.end();++vi) {
      action(*vi);
    }       
  }
  else
  {
    for(auto vi=m.vert.begin();vi!=m.vert.end();++vi)
      if(!(*vi).IsD())
      {
        action(*vi);
      }    
  }
}

/**
 * ForEachEdge Helper
 * to traverse all the vertexes of a mesh you can simply write something like:
 * 
 *      ForEachEdge(m, [&](const EdgeType &e){
 *         MakeSomethingWithEdge(e);
 *      });
 *  
 */

template <class MeshType>
inline void ForEachEdge(MeshType &m, std::function<void (typename MeshType::EdgeType &)> action)
{
  if(m.en == (int) m.edge.size())
  {
    for(auto ei=m.edge.begin();ei!=m.edge.end();++ei) {
      action(*ei);
    }       
  }
  else
  {
    for(auto ei=m.edge.begin();ei!=m.edge.end();++ei)
      if(!(*ei).IsD())
      {
        action(*ei);
      }    
  }
}

/**
 * ForEachTetra Helper
 * to traverse all the tetras of a mesh you can simply write something like:
 * 
 *      ForEachTetra(m, [&](const TetraType &t){
 *         MakeSomethingWithTetra(t);
 *      });
 *  
 */

template <class MeshType>
inline void ForEachTetra(const MeshType &m, std::function<void (const typename MeshType::TetraType &)> action)
{
  if(m.tn == (int) m.tetra.size())
  {
    for(auto ti = m.tetra.begin(); ti != m.tetra.end(); ++ti) {
      action(*ti);
    }       
  }
  else
  {
    for(auto ti = m.tetra.begin(); ti != m.tetra.end(); ++ti)
      if(!(*ti).IsD())
      {
        action(*ti);
      }    
  }
}

template <class MeshType>
inline void ForEachTetra(MeshType &m, std::function<void (typename MeshType::TetraType &)> action)
{
  if(m.tn == (int) m.tetra.size())
  {
    for(auto ti = m.tetra.begin(); ti != m.tetra.end(); ++ti) {
      action(*ti);
    }       
  }
  else
  {
    for(auto ti = m.tetra.begin(); ti != m.tetra.end(); ++ti)
      if(!(*ti).IsD())
      {
        action(*ti);
      }    
  }
}

/** @} */ // end doxygen group trimesh
} // end namespace tri
} // end namespace vcg

#endif // VCG__FOREACH_H