File: symmetry.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 (257 lines) | stat: -rw-r--r-- 8,841 bytes parent folder | download | duplicates (3)
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/****************************************************************************
* VCGLib                                                            o o     *
* Visual and Computer Graphics Library                            o     o   *
*                                                                _   O  _   *
* Copyright(C) 2004-2016                                           \/)\/    *
* 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_SYMMETRY_H
#define VCG_SYMMETRY_H

#include <vcg/space/plane3.h>
#include <vcg/space/index/grid_static_ptr.h>
#include <vcg/complex/algorithms/closest.h>
#include <vcg/complex/algorithms/create/platonic.h>
#include <vcg/complex/algorithms/point_sampling.h>

namespace vcg {
namespace tri {

//class SphereEdge;
//class SphereFace;
//class SphereVertex;

template <class TriMeshType>
class ExtrinsicPlaneSymmetry
{
    typedef typename TriMeshType::VertexType VertexType;
    typedef typename TriMeshType::FaceType FaceType;
    typedef typename TriMeshType::CoordType CoordType;
    typedef typename TriMeshType::ScalarType ScalarType;


    TriMeshType &tri_mesh;

    CoordType AlignZeroTr;

    std::vector<std::vector< ScalarType > > Weight;
    std::vector<std::vector<std::pair<CoordType,CoordType>  > > VotingPos;

    std::vector<ScalarType> Votes;

    TriMeshType *sphere;

    typename vcg::GridStaticPtr<FaceType,ScalarType> GridSph;

    ScalarType RadiusInterval;
    ScalarType MaxRadius;
    int radiusSph;

    std::vector<std::pair<ScalarType,int> > SortedPlanes;
    std::vector<vcg::Plane3<ScalarType> > SymmetricPlanes;

    int Bucket(const vcg::Plane3<ScalarType> &Pl)
    {
        ScalarType Offset=Pl.Offset();
        CoordType Direction=Pl.Direction();
        Direction.Normalize();
        ///get the offset interval
        int OffsetI=floor((Offset/RadiusInterval)+0.5);
        assert(OffsetI<radiusSph);
        ///then get the closest face
        ScalarType MaxD=sphere->bbox.Diag();
        ScalarType MinD;
        CoordType ClosePt;
        FaceType *choosen=NULL;
        choosen=vcg::tri::GetClosestFaceBase(*sphere,GridSph,Direction,MaxD,MinD,ClosePt);
        assert(choosen!=NULL);
        int IndexF=choosen-&(sphere->face[0]);
        ///compose the final index
        int OffsetRadius=OffsetI * (sphere->face.size());
        return(OffsetRadius+IndexF);
    }

    void Elect(CoordType p0,CoordType p1)
    {
        CoordType AvP=(p0+p1)/2.0;
        AvP-=AlignZeroTr;
        CoordType Direction=p0-p1;
        Direction.Normalize();
        vcg::Plane3<ScalarType> Pl;
        Pl.Init(AvP,Direction);
        //invert if needed
        if (Pl.Offset()<0)
        {
            ScalarType Off=Pl.Offset();
            CoordType Dir=Pl.Direction();
            Pl.Set(-Dir,-Off);
        }
        int index=Bucket(Pl);
        assert(index>=0);
        assert(index<(int)Votes.size());
        ScalarType VoteValue=1;
        Votes[index]+=VoteValue;
        Weight[index].push_back(VoteValue);
        VotingPos[index].push_back(std::pair<CoordType,CoordType> (p0,p1));
    }

    vcg::Plane3<ScalarType> GetInterpolatedPlane(int &Index)
    {
        ///then fit the plane
        CoordType AvDir=CoordType(0,0,0);
        ScalarType WSum=0;
        ScalarType AvOffset=0;
        for (size_t i=0;i<VotingPos[Index].size();i++)
        {
            CoordType p0=VotingPos[Index][i].first;
            CoordType p1=VotingPos[Index][i].second;
            ScalarType W=Weight[Index][i];
            CoordType Dir=(p0-p1);
            Dir.Normalize();

            CoordType AvP=(p0+p1)/2.0;
            ScalarType Offset=AvP*Dir;

            //invert if needed
            if (Offset<0)
            {
                Offset=-Offset;
                Dir=-Dir;
            }

            AvDir+=(Dir*W);
            AvOffset+=(Offset*W);
            WSum+=W;
        }
        AvDir.Normalize();
        AvOffset/=WSum;
        vcg::Plane3<ScalarType> Pl;
        Pl.Set(AvDir,AvOffset);
        return Pl;
    }

    vcg::Plane3<ScalarType> GetBasePlane(int &Index)
    {
        ///get offset value
        int OffsetIndex=Index/sphere->face.size();
        ScalarType OffsetVal=OffsetIndex*RadiusInterval;
        int DirectionIndex=Index % sphere->face.size();
        CoordType PlaneDirection=(sphere->face[DirectionIndex].P(0)+
                                  sphere->face[DirectionIndex].P(1)+
                                  sphere->face[DirectionIndex].P(2))/3.0;
        PlaneDirection.Normalize();
        CoordType CenterPl=PlaneDirection*OffsetVal;
        CenterPl+=AlignZeroTr;
        vcg::Plane3<ScalarType> RetPl;
        RetPl.Init(CenterPl,PlaneDirection);
        return RetPl;
    }

    void InitSymmetricPlanes(const int SubN=4)
    {
        assert(SortedPlanes.size()>0);
        SymmetricPlanes.clear();
        int BestN=pow((ScalarType)2,(ScalarType)SubN);
        if (BestN>=(int)SortedPlanes.size())BestN=SortedPlanes.size()-1;
        for (size_t j=SortedPlanes.size()-1;j>SortedPlanes.size()-1-SubN;j--)
        {
            int Index=SortedPlanes[j].second;
            SymmetricPlanes.push_back(GetInterpolatedPlane(Index));
        }
    }


public:

    void GetPlanes(std::vector<vcg::Plane3<ScalarType> > &Planes,int Num)
    {
        if (SymmetricPlanes.size()==0)return;

        for (int i=0;i<Num;i++)
            Planes.push_back(SymmetricPlanes[i]);
    }

    void Init(bool OnlyBorder=false,
              int SubDirections=4,
              int NumberBestPlanes=16)
    {
        if (OnlyBorder)
        {
            vcg::tri::UpdateTopology<TriMeshType>::FaceFace(tri_mesh);
            vcg::tri::UpdateFlags<TriMeshType>::FaceBorderFromFF(tri_mesh);
            vcg::tri::UpdateFlags<TriMeshType>::VertexBorderFromFaceBorder(tri_mesh);
        }
        AlignZeroTr=tri_mesh.bbox.Center();

        ///initialize the mesh
        if (sphere!=NULL)
            sphere->Clear();
        else
            sphere=new TriMeshType();

        //create the sphere
        vcg::tri::Sphere<TriMeshType>(*sphere,SubDirections);
        vcg::tri::UpdateBounding<TriMeshType>::Box(*sphere);
        //sphere->face.EnableMark();

        ///initialize grid
        GridSph.Set(sphere->face.begin(),sphere->face.end());

        ///then get radius division steps
        ScalarType MaxRadius=tri_mesh.bbox.Diag()/2;
        int AreaSph=sphere->fn;
        radiusSph=ceil(sqrt((ScalarType)AreaSph/4.0*M_PI));
        RadiusInterval=MaxRadius/(ScalarType)radiusSph;

        ///and finally allocate space for votes
        Votes.resize(radiusSph*sphere->fn,0);

        Weight.resize(radiusSph*sphere->fn);
        VotingPos.resize(radiusSph*sphere->fn);

        ///then count votes
        for (size_t i=0;i<tri_mesh.vert.size()-1;i++)
            for (size_t j=i+1;j<tri_mesh.vert.size();j++)
            {
                VertexType *v0=&tri_mesh.vert[i];
                VertexType *v1=&tri_mesh.vert[j];
                if ((OnlyBorder)&&(!((v0->IsB())&&(v1->IsB()))))continue;
                Elect(v0->P(),v1->P());
            }
        SortedPlanes.resize(Votes.size());
        for (size_t i=0;i<Votes.size();i++)
            SortedPlanes[i]=std::pair<ScalarType,int>(Votes[i],i);

        std::sort(SortedPlanes.begin(),SortedPlanes.end());

        InitSymmetricPlanes(NumberBestPlanes);

    }

    ExtrinsicPlaneSymmetry(TriMeshType &_tri_mesh):tri_mesh(_tri_mesh)
    {
        sphere=NULL;
        RadiusInterval=-1;
        radiusSph=-1;
    }

};

}///end namespace vcg
}///end namespace tri
#endif