1 /****************************************************************************
2 * MeshLab                                                           o o     *
3 * A versatile mesh processing toolbox                             o     o   *
4 *                                                                _   O  _   *
5 * Copyright(C) 2005                                                \/)\/    *
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 FILTER_IMG_PATCH_PARAM_PLUGIN__DOMINANCYCLASSIFIER_H
25 #define FILTER_IMG_PATCH_PARAM_PLUGIN__DOMINANCYCLASSIFIER_H
26 
27 
28 
29 
30 #include <common/meshmodel.h>
31 #include "GPU/GPU.h"
32 #include "OOCRaster.h"
33 
34 
35 typedef QVector<CFaceO*> FaceVec;
36 typedef QVector<vcg::Point3f> WeightVec;
37 
38 struct Patch
39 {
40     OOCRaster   *ref;
41     FaceVec     faces;
42     FaceVec     boundary;
43     WeightVec   bWeight;
44 };
45 
46 typedef QMap<OOCRaster*,FaceVec> RasterFaceMap;
47 
48 
49 class DominancyClassifier
50 {
51 public:
52     enum WeightMask
53     {
54         W_ORIENTATION   = 0x01,
55         W_DISTANCE      = 0x02,
56         W_IMG_BORDER    = 0x04,
57         W_SILHOUETTE    = 0x08,
58         W_ALL           = 0x0F,
59     };
60 
61     struct VDominancy
62     {
63         float       weight1;
64         float       weight2;
65         OOCRaster   *dominant1;
66         OOCRaster   *dominant2;
67 
VDominancyVDominancy68         inline VDominancy() :
69         weight1(-1.0f),
70         weight2(-1.0f),
71         dominant1(NULL),
72         dominant2(NULL)
73         {}
74 
isOnBoundaryVDominancy75         inline bool isOnBoundary() const    { return dominant1 && dominant2 && borderWeight()<=1.0f; }
borderWeightVDominancy76         inline float borderWeight() const   { return (std::sqrt(weight1/weight2) - 1.0f) / 0.2; }
77     };
78 
79 private:
80     typedef GPU::VBO<GPU::InterlacedBuffers,
81                      GPU::Vertex3f         ,
82                      GPU::Normal3f         ,
83                      GPU::TexCoord1i       ,
84                      GPU::Indexui          > MyVBO;
85 
86     typedef std::vector<VDominancy> VertDomVector;
87 
88 
89 
90     CMeshO                      &m_Mesh;
91     QList<OOCRaster>            &m_RasterList;
92 
93     MyVBO                       m_MeshVBO;
94 
95     int                         m_WeightMask;
96     GPU::Shader                 m_WeightShader;
97     GPU::Shader                 m_VisCheckShader;
98 
99     float                       m_DepthMin;
100     float                       m_DepthMax;
101 
102     GPU::Texture2D              m_WeightMap;
103     GPU::Texture2D              m_WeightMapSilh;
104     GPU::Texture2D              m_ShadowMap;
105     vcg::Matrix44f              m_Proj;
106     vcg::Matrix44f              m_Pose;
107     vcg::Matrix44f              m_TexProj;
108 
109     VertDomVector               m_VertexDom;
110 
111 
112     void                        updateDepthRange();
113     void                        updateMeshVBO();
114     bool                        initShaders();
115     void                        generateWeightsAndShadowMap( OOCRaster &rr );
116     void                        projectiveTexMatrices( OOCRaster &rr );
117     void                        setupShadowTexture( OOCRaster &rr );
118     void                        checkDominancy( OOCRaster &rr );
119     void                        releaseAll();
120 
id(const CVertexO & v)121     inline int                  id( const CVertexO& v ) const                       { return &v - &m_Mesh.vert[0]; }
122 
123 public:
124     DominancyClassifier( CMeshO &mesh, QList<OOCRaster> &rasterList, int weightMask );
125 
126     inline const VDominancy&    operator[]( const int v ) const                     { return m_VertexDom[v];      }
127     inline VDominancy&          operator[]( const int v )                           { return m_VertexDom[v];      }
128     inline const VDominancy&    operator[]( const CVertexO& v ) const               { return m_VertexDom[id(v)];  }
129     inline VDominancy&          operator[]( const CVertexO& v )                     { return m_VertexDom[id(v)];  }
130     inline const VDominancy&    operator[]( const CVertexO* v ) const               { return m_VertexDom[id(*v)]; }
131     inline VDominancy&          operator[]( const CVertexO* v )                     { return m_VertexDom[id(*v)]; }
132     inline const VDominancy&    operator[]( const CMeshO::VertexIterator& v ) const { return m_VertexDom[id(*v)]; }
133     inline VDominancy&          operator[]( const CMeshO::VertexIterator& v )       { return m_VertexDom[id(*v)]; }
134 
135     void                        dominancyCoverage( RasterFaceMap &rpatches ) const;
136 };
137 
138 
139 
140 
141 #endif // FILTER_IMG_PATCH_PARAM_PLUGIN__DOMINANCYCLASSIFIER_H
142