MOAB: Mesh Oriented datABase  (version 5.4.1)
TagVertexMeshTest.cpp
Go to the documentation of this file.
00001 /* *****************************************************************
00002     MESQUITE -- The Mesh Quality Improvement Toolkit
00003 
00004     Copyright 2006 Lawrence Livermore National Laboratory.  Under
00005     the terms of Contract B545069 with the University of Wisconsin --
00006     Madison, Lawrence Livermore National Laboratory retains certain
00007     rights in this software.
00008 
00009     This library is free software; you can redistribute it and/or
00010     modify it under the terms of the GNU Lesser General Public
00011     License as published by the Free Software Foundation; either
00012     version 2.1 of the License, or (at your option) any later version.
00013 
00014     This library is distributed in the hope that it will be useful,
00015     but WITHOUT ANY WARRANTY; without even the implied warranty of
00016     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017     Lesser General Public License for more details.
00018 
00019     You should have received a copy of the GNU Lesser General Public License
00020     (lgpl.txt) along with this library; if not, write to the Free Software
00021     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022 
00023     (2006) [email protected]
00024 
00025   ***************************************************************** */
00026 
00027 /** \file TagVertexMeshTest.cpp
00028  *  \brief unit tests for TagVertexMesh class
00029  *  \author Jason Kraftcheck
00030  */
00031 
00032 #include "Mesquite.hpp"
00033 #include "TagVertexMesh.hpp"
00034 #include "MsqError.hpp"
00035 #include "UnitUtil.hpp"
00036 #include "MeshImpl.hpp"
00037 #include "MsqVertex.hpp"
00038 #include "InstructionQueue.hpp"
00039 #include <cppunit/extensions/HelperMacros.h>
00040 #include <cstdio>
00041 
00042 using namespace MBMesquite;
00043 
00044 const char TEMP_FILE_NAME[] = "TagVertexMesh.vtk";
00045 
00046 class TagVertexMeshTest : public CppUnit::TestFixture
00047 {
00048   private:
00049     CPPUNIT_TEST_SUITE( TagVertexMeshTest );
00050     CPPUNIT_TEST( test_vertex_coordinates );
00051     CPPUNIT_TEST( test_save_coordinates );
00052     CPPUNIT_TEST( test_cleanup );
00053     CPPUNIT_TEST( test_alternate_name );
00054     CPPUNIT_TEST( test_reference_mesh );
00055     CPPUNIT_TEST_SUITE_END();
00056 
00057     MeshImpl* realMesh;
00058 
00059   public:
00060     TagVertexMeshTest() : realMesh( 0 ) {}
00061 
00062     void setUp();
00063     void tearDown();
00064 
00065     void test_vertex_coordinates();
00066     void test_save_coordinates();
00067     void test_cleanup();
00068     void test_alternate_name();
00069     void test_reference_mesh();
00070 };
00071 
00072 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( TagVertexMeshTest, "TagVertexMeshTest" );
00073 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( TagVertexMeshTest, "Unit" );
00074 
00075 void TagVertexMeshTest::setUp()
00076 {
00077     const char vtk_data[] = "# vtk DataFile Version 2.0\n"
00078                             "test mesh\n"
00079                             "ASCII\n"
00080                             "DATASET UNSTRUCTURED_GRID\n"
00081                             "POINTS 3 float\n"
00082                             "0 0 0\n"
00083                             "1 0 0\n"
00084                             "0 1 0\n"
00085                             "CELLS 1 4\n"
00086                             "3 0 1 2\n"
00087                             "CELL_TYPES 1\n"
00088                             "5\n";
00089 
00090     FILE* file = fopen( TEMP_FILE_NAME, "w" );
00091     CPPUNIT_ASSERT( !!file );
00092     size_t r = fwrite( vtk_data, sizeof( vtk_data ) - 1, 1, file );
00093     fclose( file );
00094     CPPUNIT_ASSERT( r == 1 );
00095 
00096     MsqPrintError err( std::cerr );
00097     realMesh = new MeshImpl;
00098     realMesh->read_vtk( TEMP_FILE_NAME, err );
00099     remove( TEMP_FILE_NAME );
00100     ASSERT_NO_ERROR( err );
00101 }
00102 
00103 void TagVertexMeshTest::tearDown()
00104 {
00105     delete realMesh;
00106     realMesh = 0;
00107 }
00108 
00109 void TagVertexMeshTest::test_vertex_coordinates()
00110 {
00111     MsqPrintError err( std::cerr );
00112     TagVertexMesh tag_mesh( err, realMesh, true );
00113     ASSERT_NO_ERROR( err );
00114 
00115     std::vector< Mesh::VertexHandle > vertices;
00116     realMesh->get_all_vertices( vertices, err );
00117     ASSERT_NO_ERROR( err );
00118 
00119     // Check that initial position for vertex matches that of real mesh
00120     Mesh::VertexHandle vertex = vertices[0];
00121     MsqVertex get_coords;
00122     Vector3D orig_coords, real_coords, tag_coords;
00123     realMesh->vertices_get_coordinates( &vertex, &get_coords, 1, err );
00124     ASSERT_NO_ERROR( err );
00125     orig_coords = get_coords;
00126     tag_mesh.vertices_get_coordinates( &vertex, &get_coords, 1, err );
00127     ASSERT_NO_ERROR( err );
00128     tag_coords = get_coords;
00129     CPPUNIT_ASSERT_VECTORS_EQUAL( orig_coords, tag_coords, DBL_EPSILON );
00130 
00131     // Check that modified vertex coords show up in tag mesh but not
00132     // real mesh.
00133     Vector3D new_coords( 5, 5, 5 );
00134     tag_mesh.vertex_set_coordinates( vertex, new_coords, err );
00135     ASSERT_NO_ERROR( err );
00136     tag_mesh.vertices_get_coordinates( &vertex, &get_coords, 1, err );
00137     ASSERT_NO_ERROR( err );
00138     tag_coords = get_coords;
00139     CPPUNIT_ASSERT_VECTORS_EQUAL( new_coords, tag_coords, DBL_EPSILON );
00140     realMesh->vertices_get_coordinates( &vertex, &get_coords, 1, err );
00141     ASSERT_NO_ERROR( err );
00142     real_coords = get_coords;
00143     CPPUNIT_ASSERT_VECTORS_EQUAL( orig_coords, real_coords, DBL_EPSILON );
00144 }
00145 
00146 void TagVertexMeshTest::test_save_coordinates()
00147 {
00148     MsqPrintError err( std::cerr );
00149     Vector3D new_coords( 5, 5, 5 );
00150     MsqVertex get_coords;
00151 
00152     std::vector< Mesh::VertexHandle > vertices;
00153     realMesh->get_all_vertices( vertices, err );
00154     ASSERT_NO_ERROR( err );
00155     Mesh::VertexHandle vertex = vertices[0];
00156 
00157     realMesh->vertices_get_coordinates( &vertex, &get_coords, 1, err );
00158     ASSERT_NO_ERROR( err );
00159     Vector3D orig_coords = get_coords;
00160 
00161     // modify a vertex in the tag interface
00162     {
00163         TagVertexMesh tag_mesh( err, realMesh, false );
00164         ASSERT_NO_ERROR( err );
00165         tag_mesh.vertex_set_coordinates( vertex, new_coords, err );
00166         ASSERT_NO_ERROR( err );
00167     }
00168 
00169     // check that it exists in a new TagVertexMesh
00170     {
00171         TagVertexMesh tag_mesh( err, realMesh, false );
00172         ASSERT_NO_ERROR( err );
00173         tag_mesh.vertices_get_coordinates( &vertex, &get_coords, 1, err );
00174         ASSERT_NO_ERROR( err );
00175         CPPUNIT_ASSERT_VECTORS_EQUAL( new_coords, get_coords, DBL_EPSILON );
00176     }
00177 }
00178 
00179 void TagVertexMeshTest::test_cleanup()
00180 {
00181     MsqPrintError err( std::cerr );
00182     Vector3D new_coords( 5, 5, 5 );
00183     MsqVertex get_coords;
00184 
00185     std::vector< Mesh::VertexHandle > vertices;
00186     realMesh->get_all_vertices( vertices, err );
00187     ASSERT_NO_ERROR( err );
00188     Mesh::VertexHandle vertex = vertices[0];
00189 
00190     realMesh->vertices_get_coordinates( &vertex, &get_coords, 1, err );
00191     ASSERT_NO_ERROR( err );
00192     Vector3D orig_coords = get_coords;
00193 
00194     // modify a vertex in the tag interface
00195     {
00196         TagVertexMesh tag_mesh( err, realMesh, true );
00197         ASSERT_NO_ERROR( err );
00198         tag_mesh.vertex_set_coordinates( vertex, new_coords, err );
00199         ASSERT_NO_ERROR( err );
00200     }
00201 
00202     // check that values were cleaned up when previous instance was destroyed
00203     {
00204         TagVertexMesh tag_mesh( err, realMesh, false );
00205         ASSERT_NO_ERROR( err );
00206         tag_mesh.vertices_get_coordinates( &vertex, &get_coords, 1, err );
00207         ASSERT_NO_ERROR( err );
00208         CPPUNIT_ASSERT_VECTORS_EQUAL( orig_coords, get_coords, DBL_EPSILON );
00209     }
00210 }
00211 
00212 void TagVertexMeshTest::test_alternate_name()
00213 {
00214     MsqPrintError err( std::cerr );
00215     Vector3D new_coords( 5, 5, 5 );
00216     MsqVertex get_coords;
00217 
00218     std::vector< Mesh::VertexHandle > vertices;
00219     realMesh->get_all_vertices( vertices, err );
00220     ASSERT_NO_ERROR( err );
00221     Mesh::VertexHandle vertex = vertices[0];
00222 
00223     realMesh->vertices_get_coordinates( &vertex, &get_coords, 1, err );
00224     ASSERT_NO_ERROR( err );
00225     Vector3D orig_coords = get_coords;
00226 
00227     // modify a vertex in the tag interface and save it
00228     {
00229         TagVertexMesh tag_mesh( err, realMesh, false, "foobar" );
00230         ASSERT_NO_ERROR( err );
00231         tag_mesh.vertex_set_coordinates( vertex, new_coords, err );
00232         ASSERT_NO_ERROR( err );
00233     }
00234 
00235     // verify that it is modified in the new interface
00236     {
00237         TagVertexMesh tag_mesh( err, realMesh, false, "foobar" );
00238         ASSERT_NO_ERROR( err );
00239         tag_mesh.vertices_get_coordinates( &vertex, &get_coords, 1, err );
00240         ASSERT_NO_ERROR( err );
00241         CPPUNIT_ASSERT_VECTORS_EQUAL( new_coords, get_coords, DBL_EPSILON );
00242     }
00243 }
00244 
00245 void TagVertexMeshTest::test_reference_mesh()
00246 {
00247     MsqPrintError err( std::cerr );
00248     TagVertexMesh tag_mesh( err, realMesh, true );
00249     ASSERT_NO_ERROR( err );
00250 
00251     std::vector< Mesh::VertexHandle > vertices;
00252     realMesh->get_all_vertices( vertices, err );
00253     ASSERT_NO_ERROR( err );
00254 
00255     // copy real mesh coordinates into tag data in TagVertexMesh
00256     InstructionQueue q;
00257     q.add_tag_vertex_mesh( &tag_mesh, err );
00258     ASSERT_NO_ERROR( err );
00259     q.run_instructions( realMesh, err );
00260     ASSERT_NO_ERROR( err );
00261 
00262     // Check that initial position for vertex matches that of real mesh
00263     Mesh::VertexHandle vertex = vertices[0];
00264     MsqVertex get_coords;
00265     Vector3D orig_coords, real_coords, tag_coords;
00266     realMesh->vertices_get_coordinates( &vertex, &get_coords, 1, err );
00267     ASSERT_NO_ERROR( err );
00268     orig_coords = get_coords;
00269     tag_mesh.vertices_get_coordinates( &vertex, &get_coords, 1, err );
00270     ASSERT_NO_ERROR( err );
00271     tag_coords = get_coords;
00272     CPPUNIT_ASSERT_VECTORS_EQUAL( orig_coords, tag_coords, DBL_EPSILON );
00273 
00274     // Check that modified vertex coords show up in real mesh but not
00275     // tag mesh.
00276     realMesh->vertices_get_coordinates( &vertex, &get_coords, 1, err );
00277     ASSERT_NO_ERROR( err );
00278     orig_coords = get_coords;
00279     Vector3D new_coords( 5, 5, 5 );
00280     realMesh->vertex_set_coordinates( vertex, new_coords, err );
00281     ASSERT_NO_ERROR( err );
00282     tag_mesh.vertices_get_coordinates( &vertex, &get_coords, 1, err );
00283     ASSERT_NO_ERROR( err );
00284     tag_coords = get_coords;
00285     CPPUNIT_ASSERT_VECTORS_EQUAL( orig_coords, tag_coords, DBL_EPSILON );
00286     // restore realMesh to initial state
00287     realMesh->vertex_set_coordinates( vertex, orig_coords, err );
00288     ASSERT_NO_ERROR( err );
00289 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines