1 /****************************************************************************
2 * VCGLib                                                            o o     *
3 * Visual and Computer Graphics Library                            o     o   *
4 *                                                                _   O  _   *
5 * Copyright(C) 2004-2017                                           \/)\/    *
6 * Visual Computing Lab                                            /\/|      *
7 * ISTI - Italian National Research Council                           |      *
8 *                                                                    \      *
9 * All rights reserved.                                                      *
10 *                                                                           *
11 * This program is free software; you can redistribute it and/or modify      *
12 * it under the terms of the GNU General Public License as published by      *
13 * the Free Software Foundation; either version 2 of the License, or         *
14 * (at your option) any later version.                                       *
15 *                                                                           *
16 * This program is distributed in the hope that it will be useful,           *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of            *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
19 * GNU General Public License (http://www.gnu.org/licenses/gpl.txt)          *
20 * for more details.                                                         *
21 *                                                                           *
22 ****************************************************************************/
23 
24 #ifndef VCG__FOREACH_H
25 #define VCG__FOREACH_H
26 
27 #ifndef __VCG_MESH
28 #error "This file should not be included alone. It is automatically included by complex.h"
29 #endif
30 
31 namespace vcg {
32 namespace tri {
33 /** \addtogroup trimesh
34 @{
35 */
36 
37 template <class MeshType>
ForEachFacePos(MeshType & m,std::function<void (typename face::Pos<typename MeshType::FaceType> &)> action)38 inline void ForEachFacePos(MeshType &m, std::function<void (typename face::Pos<typename MeshType::FaceType>  &)> action)
39 {
40   typedef typename face::Pos<typename MeshType::FaceType> PosType;
41 
42     for(auto fi=m.face.begin();fi!=m.face.end();++fi)
43         if(!(*fi).IsD())
44         {
45             for(int i=0;i<3;++i)
46             {
47                 PosType pi(&*fi,i);
48                 action(pi);
49             }
50         }
51 }
52 
53 /**
54  * ForEachFace Helper
55  * to traverse all the faces of a mesh you can simply write something like:
56  *
57  *      ForEachFace(m, [&](const FaceType &f){
58  *         MakeSomethingWithFace(f);
59  *      });
60  *
61  */
62 
63 template <class MeshType>
ForEachFace(const MeshType & m,std::function<void (const typename MeshType::FaceType &)> action)64 inline void ForEachFace(const MeshType &m, std::function<void (const typename MeshType::FaceType &)> action)
65 {
66   if(m.fn == (int) m.face.size())
67   {
68     for(auto fi=m.face.begin();fi!=m.face.end();++fi) {
69       action(*fi);
70     }
71   }
72   else
73   {
74     for(auto fi=m.face.begin();fi!=m.face.end();++fi)
75       if(!(*fi).IsD())
76       {
77         action(*fi);
78       }
79   }
80 }
81 
82 template <class MeshType>
ForEachFace(MeshType & m,std::function<void (typename MeshType::FaceType &)> action)83 inline void ForEachFace(MeshType &m, std::function<void (typename MeshType::FaceType &)> action)
84 {
85   if(m.fn == (int) m.face.size())
86   {
87     for(auto fi=m.face.begin();fi!=m.face.end();++fi) {
88       action(*fi);
89     }
90   }
91   else
92   {
93     for(auto fi=m.face.begin();fi!=m.face.end();++fi)
94       if(!(*fi).IsD())
95       {
96         action(*fi);
97       }
98   }
99 }
100 
101 /**
102  * ForEachVertex Helper
103  * to traverse all the vertexes of a mesh you can simply write something like:
104  *
105  *      ForEachVertex(m, [&](const VertexType &v){
106  *         MakeSomethingWithVertex(v);
107  *      });
108  *
109  */
110 
111 template <class MeshType>
ForEachVertex(MeshType & m,std::function<void (typename MeshType::VertexType &)> action)112 inline void ForEachVertex(MeshType &m, std::function<void (typename MeshType::VertexType &)> action)
113 {
114   if(m.vn == (int) m.vert.size())
115   {
116     for(auto vi=m.vert.begin();vi!=m.vert.end();++vi) {
117       action(*vi);
118     }
119   }
120   else
121   {
122     for(auto vi=m.vert.begin();vi!=m.vert.end();++vi)
123       if(!(*vi).IsD())
124       {
125         action(*vi);
126       }
127   }
128 }
129 
130 /**
131  * ForEachEdge Helper
132  * to traverse all the vertexes of a mesh you can simply write something like:
133  *
134  *      ForEachEdge(m, [&](const EdgeType &e){
135  *         MakeSomethingWithEdge(e);
136  *      });
137  *
138  */
139 
140 template <class MeshType>
ForEachEdge(MeshType & m,std::function<void (typename MeshType::EdgeType &)> action)141 inline void ForEachEdge(MeshType &m, std::function<void (typename MeshType::EdgeType &)> action)
142 {
143   if(m.en == (int) m.edge.size())
144   {
145     for(auto ei=m.edge.begin();ei!=m.edge.end();++ei) {
146       action(*ei);
147     }
148   }
149   else
150   {
151     for(auto ei=m.edge.begin();ei!=m.edge.end();++ei)
152       if(!(*ei).IsD())
153       {
154         action(*ei);
155       }
156   }
157 }
158 
159 /**
160  * ForEachTetra Helper
161  * to traverse all the tetras of a mesh you can simply write something like:
162  *
163  *      ForEachTetra(m, [&](const TetraType &t){
164  *         MakeSomethingWithTetra(t);
165  *      });
166  *
167  */
168 
169 template <class MeshType>
ForEachTetra(const MeshType & m,std::function<void (const typename MeshType::TetraType &)> action)170 inline void ForEachTetra(const MeshType &m, std::function<void (const typename MeshType::TetraType &)> action)
171 {
172   if(m.tn == (int) m.tetra.size())
173   {
174     for(auto ti = m.tetra.begin(); ti != m.tetra.end(); ++ti) {
175       action(*ti);
176     }
177   }
178   else
179   {
180     for(auto ti = m.tetra.begin(); ti != m.tetra.end(); ++ti)
181       if(!(*ti).IsD())
182       {
183         action(*ti);
184       }
185   }
186 }
187 
188 template <class MeshType>
ForEachTetra(MeshType & m,std::function<void (typename MeshType::TetraType &)> action)189 inline void ForEachTetra(MeshType &m, std::function<void (typename MeshType::TetraType &)> action)
190 {
191   if(m.tn == (int) m.tetra.size())
192   {
193     for(auto ti = m.tetra.begin(); ti != m.tetra.end(); ++ti) {
194       action(*ti);
195     }
196   }
197   else
198   {
199     for(auto ti = m.tetra.begin(); ti != m.tetra.end(); ++ti)
200       if(!(*ti).IsD())
201       {
202         action(*ti);
203       }
204   }
205 }
206 
207 /** @} */ // end doxygen group trimesh
208 } // end namespace tri
209 } // end namespace vcg
210 
211 #endif // VCG__FOREACH_H
212