File: GeoMatcher.cc

package info (click to toggle)
madlib 1.3.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,196 kB
  • sloc: cpp: 39,851; sh: 10,041; makefile: 473
file content (233 lines) | stat: -rw-r--r-- 8,170 bytes parent folder | download | duplicates (6)
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
// -------------------------------------------------------------------
// MAdLib - Copyright (C) 2008-2009 Universite catholique de Louvain
//
// See the Copyright.txt and License.txt files for license information. 
// You should have received a copy of these files along with MAdLib. 
// If not, see <http://www.madlib.be/license/>
//
// Please report all bugs and problems to <contrib@madlib.be>
//
// Authors: Gaetan Compere, Jean-Francois Remacle
// -------------------------------------------------------------------

#include "GeoMatcher.h"
#include "CallbackManager.h"
#include "MathUtils.h"
#include "ModelInterface.h"
#include "MAdMessage.h"
#include "AdaptInterface.h"

namespace MAd {

  // -------------------------------------------------------------------
  void GeoMatcherCBFunction (pPList before, pPList after, void * data,
                             operationType type , pEntity ppp) 
  {
#ifdef _HAVE_GMSH_
    geoMatcher * gm = (geoMatcher *)(data);

    switch (type) {
    case MAd_ESPLIT: { 

      if ( gm->relocationsComputed() ) {
        MAdMsgSgl::instance().error(__LINE__,__FILE__,
                                    "Applying an edge split during a geometry snapping procedure");
      }

      // if new point is on a line or surface, you need to find its 
      // projection on the geometrical entity and impose its new 
      // location as a Dirichlet BC in the elastic model

      pGEntity pGE = V_whatIn( (pVertex)ppp );
      int dimBnd = GEN_type(pGE);
      if ( dimBnd < 3 ) {

        double xyz[3]; // current position of the new point
        V_coord( (pVertex)ppp, xyz );
//         printVec(xyz,"Original location");

        double u,v;
        if ( !V_params((pVertex)ppp,&u,&v) ) {
          V_info((pVertex)ppp);
          MAdMsgSgl::instance().error(__LINE__,__FILE__,
                                      "Not a parametric point");
        }

        double tgt[3]; // the target location
        switch (dimBnd) {
        case 2: { // on a surface
          GF_xyz((pGFace)pGE, u, v, tgt);
          break;
        }
        case 1: { // on a line
          GE_xyz((pGEdge)pGE, u, tgt);
          break;
        }
        default: {
          MAdMsgSgl::instance().error(__LINE__,__FILE__,
                                      "Unexpected geometric entity dimension for a new vertex: %d",
                                      dimBnd);
        }
        }
//         printVec(tgt,"Target location");

        // compute the difference with the current position
        double dx[3];
        diffVec(tgt,xyz,dx);
//         printVec(dx,"DISPLACEMENT");
      
        // impose the motion as a Dirichlet BC in the elastic model
        smallVector dxVec(3);
        for (int i=0; i<3; i++) dxVec(i) = dx[i];
        gm->addDirichlet( (pVertex)ppp, dxVec );
      }
      break;
    } 
    case MAd_ECOLLAPSE: {
      gm->delDirichlet( (pVertex)ppp );
      gm->delRelocation( (pVertex)ppp );
      break;
    }
    case MAd_FSWAP:
    case MAd_ESWAP: {
      break;
    }
    case MAd_RREMOVE: {
      throw; // some nodes could become parametric: to be checked
      void * temp = NULL;
      while ( pEntity pE = PList_next(before,&temp) ) {
        if ( EN_type(pE) == 0 ) {
          gm->delDirichlet( (pVertex)pE );
        }
      }
      break;
    }
    default: {
      MAdMsgSgl::instance().error(__LINE__,__FILE__,
                                  "Callback function not implemented for mesh modification %d",
                                  type);
    }
    }
#endif
  }

  // -------------------------------------------------------------------
  geoMatcher::geoMatcher(pMesh _mesh, MeshAdapter * _ma):
    MAdElasticityOp(_mesh), force(false), strictChecking(false),
    adapter(_ma), sliverFOp(NULL), sliverROp(NULL)
  {
    CallBackManagerSgl::instance().registerCallBack(GeoMatcherCBFunction,this);
  }

  // -------------------------------------------------------------------
  geoMatcher::geoMatcher(const geoMatcher& gm):
    MAdElasticityOp(gm), force(gm.force), strictChecking(gm.strictChecking),
    adapter(gm.adapter), sliverFOp(gm.sliverFOp), sliverROp(gm.sliverROp)
  {
    CallBackManagerSgl::instance().registerCallBack(GeoMatcherCBFunction,this);
  }

  // -------------------------------------------------------------------
  geoMatcher::~geoMatcher()
  {
    CallBackManagerSgl::instance().unregisterCallBack(GeoMatcherCBFunction,this);
  }
  
  // -------------------------------------------------------------------
  bool geoMatcher::snap()
  {
    bool res = true;

    // --- prepare the node motion ---
    buildCavity();
    setDirichletBC();
    compute();
    
    // --- perform the node motion ---
    if ( force ) forceRelocation();
    else
      {
        double ratio = 0.;
        int achieved = -1;
        while ( achieved != 2 )
          {
            achieved = advance(&ratio,1.e-12);
          
            if ( achieved == 0 ) {
              MAdMsgSgl::instance().warning(-1,__FILE__,
                                            "Could not advance snapping, achieved: %d, ratio: %f",
                                            achieved,ratio);
            }
            else {
              MAdMsgSgl::instance().info(-1,__FILE__,
                                         "Advance snapping, achieved: %d, ratio: %f",
                                         achieved,ratio);
            }
        
            if ( achieved <= 1 ) {

              // Apply the operations that do not add vertices or manage their 
              // target locations (complicated!)
              bool slivR, slivF;
              if ( sliverROp ) slivR = sliverROp->getNewVertexPermission();
              if ( sliverFOp ) slivF = sliverFOp->getNewVertexPermission();
              if ( sliverROp ) sliverROp->newVertexPermission(false);
              if ( sliverFOp ) sliverFOp->newVertexPermission(false);

              int nbBefore, nbAfter;
              if (M_dim(mesh) == 3) sliverROp->removeSliverRegions(&nbBefore,&nbAfter);
              else                  sliverFOp->removeSliverFaces  (&nbBefore,&nbAfter);
              std::cout << "Removed slivers " << nbBefore << " -> " << nbAfter <<"\n";

              if ( !( nbBefore - nbAfter ) )
                {
                  if ( !adapter->optimiseElementShape() ) {
                    //                 string filename = outPrefix + "dirichlet.txt";
                    //                 std::ofstream dirichletOut(filename.c_str());
                    //                 geoTracker->printDirichlet(dirichletOut);
                    //                 dirichletOut.close();
                    //                 filename = outPrefix + "relocations.txt";
                    //                 std::ofstream relocationsOut(filename.c_str());
                    //                 printRelocations(relocationsOut);
                    //                 relocationsOut.close();
                    MAdMsgSgl::instance().warning(__LINE__,__FILE__,
                                                  "Could not snap vertices, ratio reached: %f",
                                                  ratio);
                    reportFailure(ratio);
                    break;
                  }
                }
              if ( sliverROp ) sliverROp->newVertexPermission( slivR );
              if ( sliverFOp ) sliverFOp->newVertexPermission( slivF );
            }
          }

        if ( strictChecking && achieved != 2 ) { res = false; }
      }
    
    clear();

    return res;
  }
  
  // -------------------------------------------------------------------
  void geoMatcher::printFailures(std::ostream& out) const
  {
    if ( failures.size() ) {
      out << "No failure reported in the GeoMatcher\n";
    }
    else {
      out << failures.size() << " failures reported in the GeoMatcher:\n\n";
      int i = 0;
      std::list<double>::const_iterator it = failures.begin();
      for (; it != failures.end(); it++)
        {
          out << i << "  ratio: " << *it << "\n";
          i++;
        }
    }
  }

  // -------------------------------------------------------------------

}