1 /*=========================================================================
2  *
3  *  Copyright Insight Software Consortium
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *         http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  *=========================================================================*/
18 
19 #include "itkQuadEdgeMeshEulerOperatorFlipEdgeFunction.h"
20 #include "itkQuadEdgeMeshEulerOperatorsTestHelper.h"
21 
itkQuadEdgeMeshEulerOperatorFlipTest(int,char * [])22 int itkQuadEdgeMeshEulerOperatorFlipTest( int , char * [] )
23 {
24 
25   using MeshType = itk::QuadEdgeMesh< double, 3 >;
26   using MeshPointer = MeshType::Pointer;
27   using QEType = MeshType::QEType;
28 
29   using FlipEdge =
30       itk::QuadEdgeMeshEulerOperatorFlipEdgeFunction< MeshType, QEType>;
31 
32   MeshPointer  mesh = MeshType::New();
33   CreateSquareTriangularMesh<MeshType>( mesh );
34   FlipEdge::Pointer flipEdge = FlipEdge::New( );
35   std::cout << flipEdge << std::endl;
36 
37 #ifndef NDEBUG
38   if( flipEdge->Evaluate( (QEType*)1 ) )
39     {
40     std::cout << "FAILED." << std::endl;
41     return EXIT_FAILURE;
42     }
43   std::cout << "OK" << std::endl;
44 #endif
45 
46   std::cout << flipEdge->GetNameOfClass() << std::endl;
47 
48   flipEdge->SetInput( mesh );
49 
50 #ifndef NDEBUG
51   std::cout << "     " << "Test QE Input not internal";
52   QEType* dummy = new QEType;
53   if( flipEdge->Evaluate( dummy ) )
54     {
55     std::cout << "FAILED." << std::endl;
56     return EXIT_FAILURE;
57     }
58   delete dummy;
59   std::cout << "OK" << std::endl;
60   std::cout << "     " << "Test No QE Input";
61   if( flipEdge->Evaluate( (QEType*)nullptr ) )
62     {
63     std::cout << "FAILED." << std::endl;
64     return EXIT_FAILURE;
65     }
66   std::cout << "OK" << std::endl;
67 #endif
68 
69   mesh->LightWeightDeleteEdge( mesh->FindEdge( 12, 18 ) );
70   mesh->AddFace( mesh->FindEdge( 17 ,12 ) );
71   std::cout << "     " << "Flip an edge with a polygonal face (impossible)";
72   QEType* tempFlippedEdge = flipEdge->Evaluate( mesh->FindEdge( 12 , 17 ) );
73   if( tempFlippedEdge )
74     {
75     std::cout << "FAILED." << std::endl;
76     return EXIT_FAILURE;
77     }
78 
79   CreateSquareTriangularMesh<MeshType>( mesh );
80   std::cout << "     " << "Flip an edge (possible)";
81   tempFlippedEdge = flipEdge->Evaluate( mesh->FindEdge( 12 , 6 ) );
82   if( !tempFlippedEdge )
83     {
84     std::cout << "FAILED." << std::endl;
85     return EXIT_FAILURE;
86     }
87   // The number of edges and faces must be unchanged:
88   if( ! AssertTopologicalInvariants< MeshType >
89           ( mesh, 25, 56, 32, 1, 0 ) )
90     {
91     std::cout << "FAILED." << std::endl;
92     return EXIT_FAILURE;
93     }
94   if ( mesh->GetPoint( 12 ).GetValence( ) != 5 )
95     {
96     std::cout << "FAILED [wrong valence of "
97                << mesh->GetPoint( 12 ).GetValence( )
98                << " for vertex 12 ]." << std::endl;
99     return EXIT_FAILURE;
100     }
101   if ( mesh->GetPoint( 6 ).GetValence( ) != 5 )
102     {
103     std::cout << "FAILED [wrong valence of "
104               << mesh->GetPoint( 6 ).GetValence( )
105               << " for vertex 6 ]." << std::endl;
106     return EXIT_FAILURE;
107     }
108   if ( mesh->GetPoint( 11 ).GetValence( ) != 7 )
109     {
110     std::cout << "FAILED [wrong valence of "
111               << mesh->GetPoint( 11 ).GetValence( )
112               << " for vertex 11 ]." << std::endl;
113     return EXIT_FAILURE;
114     }
115   if ( mesh->GetPoint( 7 ).GetValence( ) != 7 )
116     {
117     std::cout << "FAILED [wrong valence of "
118               << mesh->GetPoint( 7 ).GetValence( )
119               << " for vertex 7 ]." << std::endl;
120     return EXIT_FAILURE;
121     }
122   std::cout << ".OK" << std::endl;
123    // Checking invariance (i.e. FlipEdge is it's own inverse):
124   std::cout << "     " << "Check FlipEdge(FlipEdge()) invariance (possible for triangles).";
125   if( !flipEdge->Evaluate( tempFlippedEdge ) )
126     {
127     std::cout << "FAILED." << std::endl;
128     return EXIT_FAILURE;
129     }
130   // The number of edges and faces must be unchanged:
131   if( ! AssertTopologicalInvariants< MeshType >
132           ( mesh, 25, 56, 32, 1, 0 ) )
133     {
134     std::cout << "FAILED." << std::endl;
135     return EXIT_FAILURE;
136     }
137   if ( mesh->GetPoint( 12 ).GetValence( ) != 6 )
138     {
139     std::cout << "FAILED [wrong valence of "
140               << mesh->GetPoint( 12 ).GetValence( )
141               << " for vertex 12 ]." << std::endl;
142     return EXIT_FAILURE;
143     }
144   if ( mesh->GetPoint( 6 ).GetValence( ) != 6 )
145     {
146     std::cout << "FAILED [wrong valence of "
147               << mesh->GetPoint( 6 ).GetValence( )
148               << " for vertex 6 ]." << std::endl;
149     return EXIT_FAILURE;
150     }
151   if ( mesh->GetPoint( 11 ).GetValence( ) != 6 )
152     {
153     std::cout << "FAILED [wrong valence of "
154               << mesh->GetPoint( 11 ).GetValence( )
155               << " for vertex 11 ]." << std::endl;
156     return EXIT_FAILURE;
157     }
158   if ( mesh->GetPoint( 7 ).GetValence( ) != 6 )
159     {
160     std::cout << "FAILED [wrong valence of "
161               << mesh->GetPoint( 7 ).GetValence( )
162               << " for vertex 7 ]." << std::endl;
163     return EXIT_FAILURE;
164     }
165   std::cout << "OK" << std::endl;
166   std::cout << "Checking FlipEdge." << "OK" << std::endl << std::endl;
167 
168   return EXIT_SUCCESS;
169 }
170