1 /****************************************************************************
2 * VCGLib                                                            o o     *
3 * Visual and Computer Graphics Library                            o     o   *
4 *                                                                _   O  _   *
5 * Copyright(C) 2004-2016                                           \/)\/    *
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 #ifndef __VCG_MESH
24 #error "This file should not be included alone. It is automatically included by complex.h"
25 #endif
26 #ifndef __VCG_FACE_PLUS
27 #define __VCG_FACE_PLUS
28 
29 namespace vcg {
30 
31 /*------------------------------------------------------------------*/
32 /*
33 The base class of all the recusive definition chain. It is just a container of the typenames of the various simplexes.
34 These typenames must be known form all the derived classes.
35 */
36 
37 template <class UserTypes>
38                 class FaceTypeHolder: public UserTypes {
39   public:
40 
41     template <class LeftF>
ImportData(const LeftF &)42     void ImportData(const LeftF & ){}
Name(std::vector<std::string> &)43     static void Name(std::vector<std::string> & /* name */){}
44 
45 
46  // prot
VN()47         inline int VN()  const { return 3;}
Prev(const int & i)48         inline int Prev(const int & i) const { return (i+(3-1))%3;}
Next(const int & i)49         inline int Next(const int & i) const { return (i+1)%3;}
Alloc(const int &)50     inline void Alloc(const int & ){}
Dealloc()51     inline void Dealloc(){}
52 };
53 
54 /* The base class form which we start to add our components.
55 it has the empty definition for all the standard members (coords, color flags)
56 Note:
57 in order to avoid both virtual classes and ambiguous definitions all
58 the subsequent overrides must be done in a sequence of derivation.
59 
60 In other words we cannot derive and add in a single derivation step
61 (with multiple ancestor), both the real (non-empty) normal and color but
62 we have to build the type a step a time (deriving from a single ancestor at a time).
63 
64 
65 */
66 template <class UserTypes>
67 class FaceBase: public
68             face::EmptyCore< FaceTypeHolder <UserTypes> > {
69 };
70 
71 
72 /* The Real Big Face class;
73 
74 The class __FaceArityMax__ is the one that is the Last to be derived,
75 and therefore is the only one to know the real members
76 (after the many overrides) so all the functions with common behaviour
77 using the members defined in the various Empty/nonEmpty component classes
78 MUST be defined here.
79 
80 I.e. IsD() that uses the overridden Flags() member must be defined here.
81 
82 */
83 
84 template < class UserTypes,
85           template <typename> class A, template <typename> class B,
86           template <typename> class C, template <typename> class D,
87           template <typename> class E, template <typename> class F,
88           template <typename> class G, template <typename> class H,
89           template <typename> class I, template <typename> class J,
90           template <typename> class K, template <typename> class L >
91                     class FaceArityMax: public L<Arity11<FaceBase<UserTypes>, A, B, C, D, E, F, G, H, I, J, K> > {
92 
93 public:
94     typedef typename  FaceArityMax::ScalarType ScalarType;
95 // ----- Flags stuff -----
96 
97     enum {
98 
99         DELETED     = 0x00000001,		// Face is deleted from the mesh
100         NOTREAD     = 0x00000002,		// Face of the mesh is not readable
101         NOTWRITE    = 0x00000004,		// Face of the mesh is not writable
102     VISITED     = 0x00000010,		// Face has been visited. Usualy this is a per-algorithm used bit.
103         SELECTED    = 0x00000020,		// Face is selected. Algorithms should try to work only on selected face (if explicitly requested)
104         // Border _flags, it is assumed that BORDERi = BORDER0<<i
105         BORDER0     = 0x00000040,
106         BORDER1     = 0x00000080,
107         BORDER2     = 0x00000100,
108         BORDER012     = BORDER0 | BORDER1 | BORDER2 ,
109         // Face Orientation Flags, used efficiently compute point face distance
110         NORMX				= 0x00000200,
111         NORMY				= 0x00000400,
112         NORMZ				= 0x00000800,
113         // Crease _flags,  it is assumed that CREASEi = CREASE0<<i
114         CREASE0    = 0x00008000,
115         CREASE1    = 0x00010000,
116         CREASE2    = 0x00020000,
117         // Faux edges. (semantics: when a mesh is polygonal, edges which are inside a polygonal face are "faux"
118         FAUX0       = 0x00040000,
119         FAUX1       = 0x00080000,
120         FAUX2       = 0x00100000,
121         FAUX012     = FAUX0 | FAUX1 | FAUX2 ,
122         // First user bit
123         USER0       = 0x00200000
124             };
125 
126 
127     ///  checks if the Face is deleted
IsD()128     bool IsD() const {return (this->cFlags() & DELETED) != 0;}
129     ///  checks if the Face is readable
IsR()130     bool IsR() const {return (this->cFlags() & NOTREAD) == 0;}
131     ///  checks if the Face is modifiable
IsW()132     bool IsW() const {return (this->cFlags() & NOTWRITE)== 0;}
133     /// This funcion checks whether the Face is both readable and modifiable
IsRW()134     bool IsRW() const {return (this->cFlags() & (NOTREAD | NOTWRITE)) == 0;}
135     ///  checks if the Face is Modified
IsS()136     bool IsS() const {return (this->cFlags() & SELECTED) != 0;}
137     ///  checks if the Face is Modified
IsV()138     bool IsV() const {return (this->cFlags() & VISITED) != 0;}
139 
140     /** Set the flag value
141         @param flagp Valore da inserire nel flag
142     */
SetFlags(int flagp)143     void SetFlags(int flagp) {this->Flags()=flagp;}
144 
145     /** Set the flag value
146         @param flagp Valore da inserire nel flag
147     */
ClearFlags()148     void ClearFlags() {this->Flags()=0;}
149 
150     ///  deletes the Face from the mesh
SetD()151     void SetD() {this->Flags() |=DELETED;}
152     ///  un-delete a Face
ClearD()153     void ClearD() {this->Flags() &=(~DELETED);}
154     ///  marks the Face as readable
SetR()155     void SetR() {this->Flags() &=(~NOTREAD);}
156     ///  marks the Face as not readable
ClearR()157     void ClearR() {this->Flags() |=NOTREAD;}
158     ///  marks the Face as writable
SetW()159     void SetW() {this->Flags() &=(~NOTWRITE);}
160     ///  marks the Face as notwritable
ClearW()161     void ClearW() {this->Flags() |=NOTWRITE;}
162     ///  select the Face
SetS()163     void SetS()		{this->Flags() |=SELECTED;}
164     /// Un-select a Face
ClearS()165   void ClearS()	{this->Flags() &= ~SELECTED;}
166     ///  select the Face
SetV()167     void SetV()		{this->Flags() |=VISITED;}
168     /// Un-select a Face
ClearV()169   void ClearV()	{this->Flags() &= ~VISITED;}
170 
171     /// This function checks if the face is selected
IsB(int i)172     bool IsB(int i) const {return (this->cFlags() & (BORDER0<<i)) != 0;}
173     /// This function select the face
SetB(int i)174   void SetB(int i)		{this->Flags() |=(BORDER0<<i);}
175     /// This funcion execute the inverse operation of SetS()
ClearB(int i)176     void ClearB(int i)	{this->Flags() &= (~(BORDER0<<i));}
177 
178     /// This function checks if the face is selected
IsCrease(int i)179     bool IsCrease(int i) const {return (this->cFlags() & (CREASE0<<i)) != 0;}
180     /// This function select the face
SetCrease(int i)181     void SetCrease(int i){this->Flags() |=(CREASE0<<i);}
182     /// This funcion execute the inverse operation of SetS()
ClearCrease(int i)183     void ClearCrease(int i)	{this->Flags() &= (~(CREASE0<<i));}
184 
185     /// This function checks if a given side of the face is a feature/internal edge
186     /// it is used by some importer to mark internal
187     /// edges of polygonal faces that have been triangulated
IsF(int i)188     bool IsF(int i) const {return (this->cFlags() & (FAUX0<<i) ) != 0;}
IsAnyF()189     bool IsAnyF() const {return (this->cFlags() & (FAUX0|FAUX1|FAUX2)) != 0;}
190     /// This function select the face
SetF(int i)191     void SetF(int i)		{this->Flags() |=(FAUX0<<i);}
192     /// This funcion execute the inverse operation of SetS()
ClearF(int i)193     void ClearF(int i)	{this->Flags() &= (~(FAUX0<<i));}
ClearAllF()194     void ClearAllF() { this->Flags() &= (~(FAUX0|FAUX1|FAUX2)); }
195 
196     ///  Return the first bit that is not still used
FirstUnusedBitFlag()197     static int &FirstUnusedBitFlag()
198     {
199       static int b =USER0;
200       return b;
201     }
202 
203     /// Allocate a bit among the flags that can be used by user. It updates the FirstUnusedBitFlag.
NewBitFlag()204     static inline int NewBitFlag()
205     {
206       int bitForTheUser = FirstUnusedBitFlag();
207       FirstUnusedBitFlag()=FirstUnusedBitFlag()<<1;
208       return bitForTheUser;
209     }
210 
211     /// De-allocate a pre allocated bit. It updates the FirstUnusedBitFlag.
212     // Note you must deallocate bit in the inverse order of the allocation (as in a stack)
DeleteBitFlag(int bitval)213     static inline bool DeleteBitFlag(int bitval)
214     {
215       if(FirstUnusedBitFlag()>>1==bitval) {
216         FirstUnusedBitFlag() = FirstUnusedBitFlag()>>1;
217         return true;
218       }
219       assert(0);
220       return false;
221     }
222 
223     /// This function checks if the given user bit is true
IsUserBit(int userBit)224     bool IsUserBit(int userBit){return (this->Flags() & userBit) != 0;}
225 
226     /// This function set the given user bit
SetUserBit(int userBit)227     void SetUserBit(int userBit){this->Flags() |=userBit;}
228 
229     /// This function clear the given user bit
ClearUserBit(int userBit)230     void ClearUserBit(int userBit){this->Flags() &= (~userBit);}
231 
232 
GetBBox(Box3<ScalarType> & bb)233   void GetBBox(Box3<ScalarType>& bb ) const
234   {
235     if(this->IsD()) {
236         bb.SetNull();
237         return;
238       }
239         bb.Set(this->cP(0));
240         bb.Add(this->cP(1));
241         bb.Add(this->cP(2));
242   }
243 
244 
245 };
246 
247 
248 /*
249 
250 These are the three main classes that are used by the library user to define its own Facees.
251 The user MUST specify the names of all the type involved in a generic complex.
252 so for example when defining a Face of a trimesh you must know the name of the type of the edge and of the face.
253 Typical usage example:
254 
255 A Face with coords, flags and normal for use in a standard trimesh:
256 
257 class MyFaceNf   : public FaceSimp2< VertProto, EdgeProto, MyFaceNf, face::Flag, face::Normal3f  > {};
258 
259 
260 A Face with coords, and normal for use in a tetrahedral mesh AND in a standard trimesh:
261 
262 class TetraFace   : public FaceSimp3< VertProto, EdgeProto, TetraFace, TetraProto, face::Coord3d, face::Normal3f  > {};
263 
264 
265 A summary of the components that can be added to a face (see components.h for details):
266 
267 VertexRef
268 NormalFromVert, WedgeNormal
269 Normal3s, Normal3f, Normal3d
270 WedgeTexCoord2s, WedgeTexCoord2f, WedgeTexCoord2d
271 BitFlags
272 WedgeColor, Color4b
273 Qualitys, Qualityf, Qualityd
274 Mark                                            //Incremental mark (int)
275 VFAdj                                           //Topology vertex face adjacency
276                                                  (pointers to next face in the ring of the vertex
277 FFAdj                                           //topology: face face adj
278                                                   pointers to adjacent faces
279 
280 */
281 
282 template <class UserTypes,
283           template <typename> class A = DefaultDeriver, template <typename> class B = DefaultDeriver,
284           template <typename> class C = DefaultDeriver, template <typename> class D = DefaultDeriver,
285           template <typename> class E = DefaultDeriver, template <typename> class F = DefaultDeriver,
286           template <typename> class G = DefaultDeriver, template <typename> class H = DefaultDeriver,
287           template <typename> class I = DefaultDeriver, template <typename> class J = DefaultDeriver,
288           template <typename> class K = DefaultDeriver, template <typename> class L = DefaultDeriver >
289                             class Face: public FaceArityMax<UserTypes, A, B, C, D, E, F, G, H, I, J, K, L>  {
290                             public: typedef AllTypes::AFaceType IAm; typedef UserTypes TypesPool;};
291 
292 
293 }// end namespace
294 #endif
295 
296