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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
/* *****************************************************************
    MESQUITE -- The Mesh Quality Improvement Toolkit

    Copyright 2004 Sandia Corporation and Argonne National
    Laboratory.  Under the terms of Contract DE-AC04-94AL85000
    with Sandia Corporation, the U.S. Government retains certain
    rights in this software.

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library 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
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    (lgpl.txt) along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

    [email protected], [email protected], [email protected],
    [email protected], [email protected], [email protected]

  ***************************************************************** */
// -*- Mode : c++; tab-width: 2; c-tab-always-indent: t; indent-tabs-mode: nil; c-basic-offset: 2
// -*-
//
//   SUMMARY:
//     USAGE:
//
//    AUTHOR: Michael Brewer
//       ORG: Sandia National Labs
//    E-MAIL: [email protected]
//
// ORIG-DATE: Jan. 29, 2003
//  LAST-MOD: 25-Feb-04 at 10:49:32 by Thomas Leurent
//
// DESCRIPTION:
// ============
/*! \file PlanarGeometryTest.cpp

Regression testing using the planar geometry capabilities in
SimplifiedGeometryEngine.
 */
// DESCRIP-END.
//

#include "meshfiles.h"

#include "PatchDataInstances.hpp"
#include "cppunit/extensions/HelperMacros.h"
#include <cmath>

#include "Mesquite.hpp"
#include "MsqError.hpp"
#include "InstructionQueue.hpp"
#include "PatchData.hpp"
//#include "StoppingCriterion.hpp"
#include "QualityAssessor.hpp"

#include "ConditionNumberQualityMetric.hpp"
#include "LPtoPTemplate.hpp"
#include "EdgeLengthQualityMetric.hpp"
#include "LaplacianSmoother.hpp"
#include "LInfTemplate.hpp"
#include "SteepestDescent.hpp"
#include "ConjugateGradient.hpp"
#include "AspectRatioGammaQualityMetric.hpp"
#include "UntangleBetaQualityMetric.hpp"
#include "MultiplyQualityMetric.hpp"
#include "PlanarDomain.hpp"

#include "MsqFPE.hpp"

#include "UnitUtil.hpp"

#include "MeshImpl.hpp"
#include <iostream>
using std::cout;
using std::endl;

using namespace MBMesquite;

class PlanarGeometryTest : public CppUnit::TestFixture
{
  private:
    CPPUNIT_TEST_SUITE( PlanarGeometryTest );
    // run steepest descent on the tangled_tri.vtk mesh
    CPPUNIT_TEST( test_plane_tri_tangled );
    // run cg on tangled_quad.vtk mesh
    CPPUNIT_TEST( test_plane_quad_tangled );
    // run cg with asm metric on tri mesh in y=-5 plane
    CPPUNIT_TEST( test_plane_tri_xz );
    // test fit plane
    CPPUNIT_TEST( test_fit_plane );
    CPPUNIT_TEST_SUITE_END();

  private:
    double qualTol;  // double used for double comparisons
    int pF;          // PRINT_FLAG
  public:
    void setUp()
    {
        // pF=1;//PRINT_FLAG IS ON
        pF = 0;  // PRINT_FLAG IS OFF
                 // tolerance double
        qualTol = MSQ_MIN;
    }

    void tearDown() {}

  public:
    PlanarGeometryTest() {}<--- Member variable 'PlanarGeometryTest::qualTol' is not initialized in the constructor.<--- Member variable 'PlanarGeometryTest::pF' is not initialized in the constructor.

    void test_plane_tri_tangled()
    {
        MBMesquite::MsqPrintError err( cout );
        MBMesquite::MeshImpl mesh;

        // This test doesn't use InstructionQueue, so
        // we need to set up trapping of floating-point
        // exceptions ourself.
        MsqFPE fpe_trap( true );

        mesh.read_vtk( MESH_FILES_DIR "2D/vtk/tris/tangled/tangled_tri.vtk", err );
        CPPUNIT_ASSERT( !err );

        // create geometry: plane z=5, normal (0,0,1)
        Vector3D pnt( 0, 0, 5 );
        Vector3D s_norm( 0, 0, 1 );
        MBMesquite::PlanarDomain msq_geom( s_norm, pnt );

        // creates an intruction queue
        InstructionQueue queue1, queue2;

        // creates a mean ratio quality metric ...
        ConditionNumberQualityMetric shape;
        UntangleBetaQualityMetric untan( .1 );

        // ... and builds an objective function with it (untangle)
        LInfTemplate untan_func( &untan );
        LPtoPTemplate shape_func( &shape, 2, err );
        // Make sure no errors
        CPPUNIT_ASSERT( !err );
        // creates the steepest descent optimization procedures
        SteepestDescent pass1( &untan_func );
        SteepestDescent pass2( &shape_func );
        pass1.use_element_on_vertex_patch();
        pass2.use_global_patch();
        // Make sure no errors
        CPPUNIT_ASSERT( !err );
        QualityAssessor stop_qa = QualityAssessor( &untan );
        QualityAssessor qa      = QualityAssessor( &shape );
        if( pF == 0 )
        {
            stop_qa.disable_printing_results();
            qa.disable_printing_results();
        }
        //**********Set stopping criterion  untangle ver small ********
        // StoppingCriterion sc_qa(&stop_qa,-100,MSQ_MIN);
        // pass1->set_stopping_criterion(&sc_qa);
        TerminationCriterion sc_of, sc_inner;
        sc_of.add_iteration_limit( 10 );
        pass1.set_outer_termination_criterion( &sc_of );
        sc_inner.add_iteration_limit( 6 );
        // sc_inner.add_absolute_gradient_L2_norm( 0.01 );
        pass1.set_inner_termination_criterion( &sc_inner );

        //**********Set stopping criterion  5 iterates ****************
        // StoppingCriterion sc5(StoppingCriterion::NUMBER_OF_PASSES,5);
        // pass2->set_stopping_criterion(&sc5);
        TerminationCriterion sc5;
        sc5.add_iteration_limit( 5 );
        pass2.set_inner_termination_criterion( &sc5 );
        // TerminationCriterion sc_inner;
        // sc_inner.add_iteration_limit( 5 );
        // pass2->set_inner_termination_criterion(&sc_inner);
        // pass2->set_maximum_iteration(5);

        queue1.set_master_quality_improver( &pass1, err );
        CPPUNIT_ASSERT( !err );
        queue2.set_master_quality_improver( &pass2, err );
        CPPUNIT_ASSERT( !err );
        //********************UNTANGLE*******************************
        // Make sure no errors
        CPPUNIT_ASSERT( !err );
        // launches optimization on mesh_set1
        MeshDomainAssoc mesh_and_domain = MeshDomainAssoc( &mesh, &msq_geom );
        double orig_qa_val              = stop_qa.loop_over_mesh( &mesh_and_domain, 0, err );
        // Make sure no errors
        CPPUNIT_ASSERT( !err );
        queue1.run_instructions( &mesh_and_domain, err );
        CPPUNIT_ASSERT( !err );
        // Make sure no errors
        CPPUNIT_ASSERT( !err );
        double fin_qa_val = stop_qa.loop_over_mesh( &mesh_and_domain, 0, err );
        // Make sure no errors
        CPPUNIT_ASSERT( !err );
        // make sure 'quality' improved
        CPPUNIT_ASSERT( ( fin_qa_val - orig_qa_val ) <= 0.0 );
        // make sure sc_qa really was satisfied
        CPPUNIT_ASSERT( fin_qa_val <= MSQ_MIN );

        //********************SMOOTH*******************************
        // Make sure no errors
        CPPUNIT_ASSERT( !err );
        // launches optimization on mesh_set1
        orig_qa_val = qa.loop_over_mesh( &mesh_and_domain, 0, err );
        // Make sure no errors
        CPPUNIT_ASSERT( !err );
        queue2.run_instructions( &mesh_and_domain, err );
        CPPUNIT_ASSERT( !err );
        // Make sure no errors
        CPPUNIT_ASSERT( !err );
        fin_qa_val = qa.loop_over_mesh( &mesh_and_domain, 0, err );
        // Make sure no errors
        CPPUNIT_ASSERT( !err );
        // make sure 'quality' improved
        CPPUNIT_ASSERT( ( fin_qa_val - orig_qa_val ) <= 0.0 );
        print_timing_diagnostics( cout );
    }

    void test_plane_quad_tangled()
    {
        MBMesquite::MeshImpl mesh;
        MsqPrintError err( cout );
        mesh.read_vtk( MESH_FILES_DIR "2D/vtk/quads/tangled/tangled_quad.vtk", err );
        CPPUNIT_ASSERT( !err );

        // create geometry: plane z=5, normal (0,0,1)
        Vector3D pnt( 0, 0, 5 );
        Vector3D s_norm( 0, 0, 1 );
        MBMesquite::PlanarDomain msq_geom( s_norm, pnt );

        // creates an intruction queue
        InstructionQueue queue1, queue2;

        // creates a mean ratio quality metric ...
        ConditionNumberQualityMetric shape;
        UntangleBetaQualityMetric untan( .1 );

        // ... and builds an objective function with it (untangle)
        LInfTemplate untan_func( &untan );
        LPtoPTemplate shape_func( &shape, 2, err );
        // Make sure no errors
        CPPUNIT_ASSERT( !err );
        // creates the cg optimization procedures
        ConjugateGradient pass1( &untan_func, err );
        ConjugateGradient pass2( &shape_func, err );
        pass1.use_element_on_vertex_patch();
        pass2.use_global_patch();
        // Make sure no errors
        CPPUNIT_ASSERT( !err );
        QualityAssessor stop_qa = QualityAssessor( &untan );
        QualityAssessor qa      = QualityAssessor( &shape );
        // turn off printing if print flag not set.
        if( pF == 0 )
        {
            stop_qa.disable_printing_results();
            qa.disable_printing_results();
        }
        //**********Set stopping criterion  untangle ver small ********
        // StoppingCriterion sc_qa(&stop_qa,-100,MSQ_MIN);
        // pass1->set_stopping_criterion(&sc_qa);
        TerminationCriterion sc_of;
        sc_of.add_iteration_limit( 10 );
        pass1.set_outer_termination_criterion( &sc_of );

        //**********Set stopping criterion  5 iterates ****************
        // StoppingCriterion sc5(StoppingCriterion::NUMBER_OF_PASSES,5);
        // pass2->set_stopping_criterion(&sc5);
        TerminationCriterion sc5;
        sc5.add_iteration_limit( 5 );
        pass2.set_inner_termination_criterion( &sc5 );
        // pass2->set_maximum_iteration(5);
        // TerminationCriterion sc_inner;
        // sc_inner.add_iteration_limit( 5 );
        // pass2->set_inner_termination_criterion(&sc_inner);
        queue1.set_master_quality_improver( &pass1, err );
        CPPUNIT_ASSERT( !err );
        queue2.set_master_quality_improver( &pass2, err );
        CPPUNIT_ASSERT( !err );
        //********************UNTANGLE*******************************
        // Make sure no errors
        CPPUNIT_ASSERT( !err );
        // launches optimization on mesh_set1
        MeshDomainAssoc mesh_and_domain = MeshDomainAssoc( &mesh, &msq_geom );
        double orig_qa_val              = stop_qa.loop_over_mesh( &mesh_and_domain, 0, err );
        // Make sure no errors
        CPPUNIT_ASSERT( !err );
        queue1.run_instructions( &mesh_and_domain, err );
        CPPUNIT_ASSERT( !err );
        // Make sure no errors
        CPPUNIT_ASSERT( !err );
        double fin_qa_val = stop_qa.loop_over_mesh( &mesh_and_domain, 0, err );
        // Make sure no errors
        CPPUNIT_ASSERT( !err );
        // make sure 'quality' improved
        CPPUNIT_ASSERT( ( fin_qa_val - orig_qa_val ) <= 0.0 );
        // make sure sc_qa really was satisfied
        CPPUNIT_ASSERT( fin_qa_val <= MSQ_MIN );

        //********************SMOOTH*******************************
        // Make sure no errors
        CPPUNIT_ASSERT( !err );
        // launches optimization on mesh_set1
        orig_qa_val = qa.loop_over_mesh( &mesh_and_domain, 0, err );
        // Make sure no errors
        CPPUNIT_ASSERT( !err );
        queue2.run_instructions( &mesh_and_domain, err );
        CPPUNIT_ASSERT( !err );
        // Make sure no errors
        CPPUNIT_ASSERT( !err );
        fin_qa_val = qa.loop_over_mesh( &mesh_and_domain, 0, err );
        // Make sure no errors
        CPPUNIT_ASSERT( !err );
        // make sure 'quality' improved
        CPPUNIT_ASSERT( ( fin_qa_val - orig_qa_val ) <= 0.0 );
        print_timing_diagnostics( cout );
    }

    void test_plane_tri_xz()
    {
        MsqPrintError err( cout );
        MBMesquite::MeshImpl mesh;
        mesh.read_vtk( MESH_FILES_DIR "2D/vtk/tris/untangled/tri_5_xz.vtk", err );
        CPPUNIT_ASSERT( !err );

        // create geometry: plane y=5, normal (0,1,0)
        Vector3D pnt( 0, -5, 0 );
        Vector3D s_norm( 0, -1, 0 );
        MBMesquite::PlanarDomain msq_geom( s_norm, pnt );

        // creates an intruction queue
        InstructionQueue queue1;

        // creates a asm quality metric ...
        ConditionNumberQualityMetric smooth;

        // ... and builds an objective function with it (untangle)
        LPtoPTemplate smooth_func( &smooth, 1, err );
        // Make sure no errors
        CPPUNIT_ASSERT( !err );
        // creates the cg optimization procedures
        ConjugateGradient pass1( &smooth_func, err );
        // pass1->set_patch_type(PatchData::ELEMENTS_ON_VERTEX_PATCH, err,1 ,1);
        pass1.use_global_patch();
        pass1.set_debugging_level( 1 );
        // Make sure no errors
        CPPUNIT_ASSERT( !err );
        QualityAssessor qa = QualityAssessor( &smooth );

        //**********Set stopping criterion  5 iterates ****************
        TerminationCriterion sc5;
        sc5.add_iteration_limit( 5 );
        pass1.set_inner_termination_criterion( &sc5 );
        // StoppingCriterion sc5(StoppingCriterion::NUMBER_OF_PASSES,5);
        // pass1->set_stopping_criterion(&sc5);
        TerminationCriterion sc_inner;
        sc_inner.add_iteration_limit( 5 );
        pass1.set_inner_termination_criterion( &sc_inner );
        // pass1->set_maximum_iteration(5);

        queue1.set_master_quality_improver( &pass1, err );
        CPPUNIT_ASSERT( !err );
        //********************UNTANGLE*******************************
        // Make sure no errors
        CPPUNIT_ASSERT( !err );
        // launches optimization on mesh_set1
        MeshDomainAssoc mesh_and_domain = MeshDomainAssoc( &mesh, &msq_geom );
        double orig_qa_val              = qa.loop_over_mesh( &mesh_and_domain, 0, err );
        // Make sure no errors
        CPPUNIT_ASSERT( !err );
        queue1.run_instructions( &mesh_and_domain, err );
        CPPUNIT_ASSERT( !err );
        // Make sure no errors
        CPPUNIT_ASSERT( !err );
        double fin_qa_val = qa.loop_over_mesh( &mesh_and_domain, 0, err );
        // Make sure no errors
        CPPUNIT_ASSERT( !err );
        // make sure 'quality' improved
        CPPUNIT_ASSERT( ( fin_qa_val - orig_qa_val ) <= 0.0 );
        print_timing_diagnostics( cout );
    }

    void test_fit_plane();
};

CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( PlanarGeometryTest, "PlanarGeometryTest" );
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( PlanarGeometryTest, "Regression" );

void PlanarGeometryTest::test_fit_plane()
{
    MsqPrintError err( std::cerr );
    PlanarDomain plane( PlanarDomain::XY, -1 );
    const double epsilon = 1e-8;

    MeshImpl mesh1;
    mesh1.read_vtk( MESH_FILES_DIR "2D/vtk/tris/untangled/bad_circle_tri.vtk", err );
    ASSERT_NO_ERROR( err );
    plane.fit_vertices( &mesh1, err, epsilon );
    ASSERT_NO_ERROR( err );
    CPPUNIT_ASSERT_VECTORS_EQUAL( Vector3D( 0, 0, 1 ), plane.get_normal(), epsilon );
    CPPUNIT_ASSERT_DOUBLES_EQUAL( 5, plane.get_coeff(), epsilon );

    MeshImpl mesh2;
    mesh2.read_vtk( MESH_FILES_DIR "2D/vtk/tris/untangled/equil_tri.vtk", err );
    ASSERT_NO_ERROR( err );
    plane.fit_vertices( &mesh2, err, epsilon );
    ASSERT_NO_ERROR( err );
    CPPUNIT_ASSERT_VECTORS_EQUAL( Vector3D( 0, 0, 1 ), plane.get_normal(), epsilon );
    CPPUNIT_ASSERT_DOUBLES_EQUAL( 0, plane.get_coeff(), epsilon );

    MeshImpl mesh3;
    mesh3.read_vtk( MESH_FILES_DIR "2D/vtk/quads/untangled/quads_4by2.vtk", err );
    ASSERT_NO_ERROR( err );
    plane.fit_vertices( &mesh3, err, epsilon );
    ASSERT_NO_ERROR( err );
    CPPUNIT_ASSERT_VECTORS_EQUAL( Vector3D( 0, 0, 1 ), plane.get_normal(), epsilon );
    CPPUNIT_ASSERT_DOUBLES_EQUAL( -2, plane.get_coeff(), epsilon );

    MeshImpl mesh4;
    mesh4.read_vtk( MESH_FILES_DIR "2D/vtk/tris/untangled/tri_5_xz.vtk", err );
    ASSERT_NO_ERROR( err );
    plane.fit_vertices( &mesh4, err, epsilon );
    ASSERT_NO_ERROR( err );
    CPPUNIT_ASSERT_VECTORS_EQUAL( Vector3D( 0, -1, 0 ), plane.get_normal(), epsilon );
    CPPUNIT_ASSERT_DOUBLES_EQUAL( -5, plane.get_coeff(), epsilon );
}