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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#include "meshkit/MeshImprove.hpp"
#include <iostream>
#include <math.h>
#include <map>

//#include "SweepWrapper.hpp"
#include "UntangleWrapper.hpp"
#include "ShapeImprover.hpp"
#include "SmartLaplacianSmoother.hpp"
#include "SweepWrapper.hpp"
#include "SmartLaplaceWrapper.hpp"
#include "ShapeImprovementWrapper.hpp"

#include "IdealWeightInverseMeanRatio.hpp"
#include "QualityAssessor.hpp"
#include "InstructionQueue.hpp"
#include "TerminationCriterion.hpp"

#include "LaplaceWrapper.hpp"
#include "SizeAdaptShapeWrapper.hpp"
#include "PaverMinEdgeLengthWrapper.hpp"
#include "DeformingDomainWrapper.hpp"
#include <MsqError.hpp>
#include <ShapeImprovementWrapper.hpp>
#include <MsqIMesh.hpp>
#include <MsqIGeom.hpp>
#ifdef HAVE_FBIGEOM
#include "meshkit/MsqFBiGeom.hpp"
#endif
using namespace Mesquite;

namespace MeshKit {

MeshImprove::MeshImprove(MKCore* core, bool isLaplacian, bool isUntangle,
    bool isShapeImprove, bool isSizeAdapt, iGeom * ig_inst)
{
  mk_core = core;
  IsLaplacian = isLaplacian;
  IsUntangle = isUntangle;
  IsShapeImprove = isShapeImprove;
  IsSizeAdapt = isSizeAdapt;
  if (ig_inst)
    igeom_inst = ig_inst;
  else
    igeom_inst = mk_core->igeom_instance();// it will pick the first one
}

void MeshImprove::SurfMeshImprove(iBase_EntityHandle surface,
    iBase_EntitySetHandle surfMesh, iBase_EntityType entity_type)
{

  MsqError mError;
  const char* VERTEX_FIXED_TAG_NAME = "MesquiteVertexFixed";

  iBase_TagHandle fixed_tag = 0;
  iMesh::Error m_err = mk_core->imesh_instance()->getTagHandle(
      VERTEX_FIXED_TAG_NAME, fixed_tag);
  if (m_err) {
    m_err = mk_core->imesh_instance()->createTag(VERTEX_FIXED_TAG_NAME, 1,
        iBase_INTEGER, fixed_tag);
    IBERRCHK(m_err, "Trouble create the tag handle.");
  }

  MsqIMesh mesh_adapter(mk_core->imesh_instance()->instance(), surfMesh,
      entity_type, mError, &fixed_tag);
  cout << "error =" << mError << endl;
  if (mError)
    throw mError;

  //get all the vertices in surface mesh: entity_handles_out---quads     adj_entity_handles_out---vertices
  std::vector<iBase_EntityHandle> entity_handles_out, adj_entity_handles_out;
  std::vector<int> offsets_out, adj_entity_indices_out;

  m_err = mk_core->imesh_instance()->getAdjEntIndices(surfMesh, entity_type,
      iMesh_ALL_TOPOLOGIES, iBase_VERTEX, entity_handles_out,
      adj_entity_handles_out, adj_entity_indices_out, offsets_out);
  IBERRCHK(m_err, "Trouble get the adjacent entity indices.");

  cout << "number of faces is " << entity_handles_out.size() << endl;
  cout << "number of vertices is " << adj_entity_handles_out.size() << endl;

  //set fixed flag on all vertices
  std::vector<int> tag_data(adj_entity_handles_out.size(), 1);
  m_err = mk_core->imesh_instance()->setIntArrData(&adj_entity_handles_out[0],
      adj_entity_handles_out.size(), fixed_tag, &tag_data[0]);
  IBERRCHK(m_err, "Trouble set an array of int data for a list of vertices.");

  //clear fixed flag for vertices contained directly in set
  int count = -1;
  m_err
      = mk_core->imesh_instance()->getNumOfType(surfMesh, iBase_VERTEX, count);
  IBERRCHK(m_err, "Trouble get the number of vertices in the set.");

  adj_entity_handles_out.clear();

  cout << "Num of Vertices on the target surface is " << count << endl;

  m_err = mk_core->imesh_instance()->getEntities(surfMesh, iBase_VERTEX,
      iMesh_ALL_TOPOLOGIES, adj_entity_handles_out);
  IBERRCHK(m_err, "Trouble get the nodes from the mesh entity set.");

  tag_data.clear();
  tag_data.resize(adj_entity_handles_out.size(), 0);

  m_err = mk_core->imesh_instance()->setIntArrData(&adj_entity_handles_out[0],
      adj_entity_handles_out.size(), fixed_tag, &tag_data[0]);
  IBERRCHK(m_err, "Trouble set an array of int data for mesh nodes.");

  //Finally smooth the mesh

  //SweepWrapper smoother( 1e-6,  "COORDINATES_MAP");
  if (IsUntangle) {
    UntangleWrapper smoother;
    //smoother.set_cpu_time_limit(300);
    smoother.set_vertex_movement_limit_factor(0.001);

    if (surface) {
#ifdef HAVE_FBIGEOM
      if (this->igeom_inst->isFBiGeom())
      {
        MsqFBiGeom fbgeom_adapter((FBiGeom *)igeom_inst, surface);
        smoother.run_instructions(&mesh_adapter, &fbgeom_adapter, mError);
        cout << "Mesquite error in fb surface mesh smoothing=" << mError << endl;
      }
      else
      {
#endif
      MsqIGeom geom_adapter(mk_core->igeom_instance()->instance(), surface);
      smoother.run_instructions(&mesh_adapter, &geom_adapter, mError);
      cout << "Mesquite error in surface mesh smoothing=" << mError << endl;
#ifdef HAVE_FBIGEOM
    }
#endif
    } else {
      smoother.run_instructions(&mesh_adapter, mError);
      cout << "Mesquite error in surface mesh smoothing=" << mError << endl;
    }
  }

  //use the SmartLaplaceWrapper class the smooth the target surface mesh
  if (IsLaplacian) {
    LaplaceWrapper sl_smooth;
    TerminationCriterion terminate;
    terminate.write_iterations("mesquite.gpt", mError);
    if (surface) {
#ifdef HAVE_FBIGEOM
      if (this->igeom_inst->isFBiGeom())
      {
        MsqFBiGeom fbgeom_adapter((FBiGeom *)igeom_inst, surface);
        sl_smooth.run_instructions(&mesh_adapter, &fbgeom_adapter, mError);
        cout << "Mesquite error in fb smart Laplacian surface mesh smoothing with the geometry domain=" << mError << endl;
      }
      else
      {
#endif
      MsqIGeom geom_adapter(mk_core->igeom_instance()->instance(), surface);
      sl_smooth.run_instructions(&mesh_adapter, &geom_adapter, mError);
      cout
          << "Mesquite error in the smart Laplacian surface mesh smoothing with the geometry domain="
          << mError << endl;
#ifdef HAVE_FBIGEOM
    }
#endif
    } else {
      sl_smooth.run_instructions(&mesh_adapter, mError);
      cout
          << "Mesquite error in the smart Laplacian surface mesh smoothing without the geometry domain="
          << mError << endl;
    }
  }

  //use the ShapeImprover class to smooth the target surface mesh
  if (IsShapeImprove) {
    IdealWeightInverseMeanRatio extra_metric;
    ShapeImprovementWrapper smoother1;
    smoother1.quality_assessor().add_quality_assessment(&extra_metric);
    //smoother1.set_vertex_movement_limit_factor(0.01);
    //smoother1.set_cpu_time_limit(300);
    if (surface) {
#ifdef HAVE_FBIGEOM
      if (this->igeom_inst->isFBiGeom())
      {
        MsqFBiGeom fbgeom_adapter((FBiGeom *)igeom_inst, surface);
        smoother1.run_instructions(&mesh_adapter, &fbgeom_adapter, mError);
        cout << "Mesquite error in ShapeImprover fb surface mesh smoothing=" << mError << endl;
      }
      else
      {
#endif
      MsqIGeom geom_adapter(mk_core->igeom_instance()->instance(), surface);
      smoother1.run_instructions(&mesh_adapter, &geom_adapter, mError);
      cout << "Mesquite error in the ShapeImprover surface mesh smoothing="
          << mError << endl;
#ifdef HAVE_FBIGEOM
    }
#endif
    } else {
      smoother1.run_instructions(&mesh_adapter, mError);
      cout << "Mesquite error in the ShapeImprover surface mesh smoothing="
          << mError << endl;
    }
  }

  //Use the Minimum Edge-Length Improvement

  if (IsSizeAdapt) {
    SizeAdaptShapeWrapper Smooth_SizeAdapt(1.0e-2);
    if (surface) {
#ifdef HAVE_FBIGEOM
      if (this->igeom_inst->isFBiGeom())
      {
        MsqFBiGeom fbgeom_adapter((FBiGeom *)igeom_inst, surface);
        Smooth_SizeAdapt.run_instructions(&mesh_adapter, &fbgeom_adapter, mError);
        cout << "Mesquite error in SizeAdaptShape fb surface mesh smoothing=" << mError << endl;
      }
      else
      {
#endif
      MsqIGeom geom_adapter(mk_core->igeom_instance()->instance(), surface);
      Smooth_SizeAdapt.run_instructions(&mesh_adapter, &geom_adapter, mError);
      cout << "Mesquite error in the SizeAdaptShape surface mesh smoothing="
          << mError << endl;
#ifdef HAVE_FBIGEOM
    }
#endif

    } else {
      Smooth_SizeAdapt.run_instructions(&mesh_adapter, mError);
      cout << "Mesquite error in the SizeAdaptShape surface mesh smoothing="
          << mError << endl;
    }
  }

}

void MeshImprove::VolumeMeshImprove(iBase_EntitySetHandle volMesh,<--- The function 'VolumeMeshImprove' is never used.
    iBase_EntityType entity_type)
{

  cout << "Volume smoothing is starting..." << endl;

  MsqError mError;
  const char* VERTEX_FIXED_TAG_NAME = "MesquiteVertexFixed";

  iBase_TagHandle fixed_tag = 0;

  iMesh::Error m_err = mk_core->imesh_instance()->getTagHandle(
      VERTEX_FIXED_TAG_NAME, fixed_tag);
  if (m_err) {
    m_err = mk_core->imesh_instance()->createTag(VERTEX_FIXED_TAG_NAME, 1,
        iBase_INTEGER, fixed_tag);
    IBERRCHK(m_err, "Trouble create a taghandle.");
  }

  MsqIMesh mesh_adapter(mk_core->imesh_instance()->instance(), volMesh,
      entity_type, mError, &fixed_tag);
  cout << "error =" << mError << endl;
  if (mError)
    throw mError;

  //get all the vertices in volume mesh
  int num_vtx, count;
  std::vector<iBase_EntityHandle> faces, verts;
  std::vector<int> indices, offsets;

  m_err = mk_core->imesh_instance()->getAdjEntIndices(volMesh, entity_type,
      iMesh_ALL_TOPOLOGIES, iBase_VERTEX, faces, verts, indices, offsets);
  IBERRCHK(m_err, "Trouble get the quads and nodes on the target surface.");
  num_vtx = verts.size();

  cout << "number of faces is " << faces.size() << endl;
  cout << "number of vertices is " << verts.size() << endl;

  //set fixed flag on all vertices
  vector<int> tag_data(num_vtx, 1);
  m_err = mk_core->imesh_instance()->setIntArrData(&verts[0], verts.size(),
      fixed_tag, &tag_data[0]);
  IBERRCHK(m_err, "Trouble set an array of int data for nodes on the target surface.");

  //clear fixed flag for vertices contained directly in set
  m_err = mk_core->imesh_instance()->getNumOfType(volMesh, iBase_VERTEX, count);
  IBERRCHK(m_err, "Trouble get the number of interior nodes on the target surface.");

  //get the interior mesh nodes on the target surface
  verts.clear();
  cout << "Num of Vertices on the target surface is " << count << endl;

  m_err = mk_core->imesh_instance()->getEntities(volMesh, iBase_VERTEX,
      iMesh_ALL_TOPOLOGIES, verts);
  IBERRCHK(m_err, "Trouble get the number of interior nodes on the target surface.");

  tag_data.clear();
  tag_data.resize(verts.size(), 0);
  m_err = mk_core->imesh_instance()->setIntArrData(&verts[0], verts.size(),
      fixed_tag, &tag_data[0]);
  IBERRCHK(m_err, "Trouble set the int data for interior nodes on the target surface.");

  //using the UntangleWrapper class to smooth the mesh with the inverted elements.
  UntangleWrapper smoother1;

  smoother1.set_cpu_time_limit(1000);
  smoother1.set_vertex_movement_limit_factor(0.001);

  smoother1.run_instructions(&mesh_adapter, mError);

  //Finally smooth the mesh
  //Use the ShapeImprover class to smooth the target surface mesh
  ShapeImprover smoother2;
  smoother2.set_cpu_time_limit(1000);
  smoother2.set_vertex_movement_limit_factor(0.001);
  smoother2.run_instructions(&mesh_adapter, mError);
  if (mError)
    cout << "Mesquite error in volume mesh smoothing is as follows\n" << mError
        << std::endl;
  /*

   SweepWrapper smoother3( 1e-6,  "COORDINATES_MAP");
   ShapeImprovementWrapper smoother2(mError);


   smoother2.run_instructions(&mesh_adapter, mError);

   if (mError)
   cout << "Mesquite error in volume mesh smoothing=" << mError << endl;
   */

}

MeshImprove::~MeshImprove()
{
  cout << "It is over now in smoothing" << endl;
}

}