]> Creatis software - cpMesh.git/blob - appli/examples/example_InternalForceSimplexMesh.cxx
First commit
[cpMesh.git] / appli / examples / example_InternalForceSimplexMesh.cxx
1 #include <iostream>
2 #include <string>
3 #include <utility>
4
5 #include <vtkActor.h>
6 #include <vtkInteractorStyleSwitch.h>
7 #include <vtkProperty.h>
8 #include <vtkRenderer.h>
9 #include <vtkRenderWindow.h>
10 #include <vtkRenderWindowInteractor.h>
11 #include <vtkSmartPointer.h>
12
13 #include <cpm/DataStructures/QuadEdgeMesh.h>
14 #include <cpm/DataStructures/SimplexMesh.h>
15 #include <cpm/Algorithms/QuadEdge/MeshToDualFilter.h>
16 #include <cpm/Algorithms/Simplex/DeformationFilter.h>
17 #include <cpm/Algorithms/Simplex/InternalForceFunction.h>
18 #include <cpm/IO/MeshReader.h>
19 #include <cpm/VTK/MeshMapper.h>
20 #include <cpm/VTK/ThreadedDeformation.h>
21
22 // -------------------------------------------------------------------------
23 const unsigned int Dim = 3;
24 const unsigned int Order = 2;
25
26 typedef float TScalar;
27 typedef cpm::DataStructures::QuadEdgeMesh< TScalar, Dim >       TTriangulation;
28 typedef cpm::DataStructures::SimplexMesh< TScalar, Order, Dim > TSimplex;
29
30 typedef cpm::IO::MeshReader< TTriangulation > TTriangulationReader;
31 typedef cpm::VTK::MeshMapper< TSimplex >      TSimplexMapper;
32 typedef
33 cpm::Algorithms::QuadEdge::MeshToDualFilter< TTriangulation, TSimplex >
34 TTriangulationToSimplexFilter;
35 typedef
36 cpm::Algorithms::Simplex::DeformationFilter< TSimplex >
37 TDeformationFilter;
38 typedef
39 cpm::Algorithms::Simplex::InternalForceFunction< TSimplex >
40 TInternalForce;
41 typedef
42 cpm::VTK::ThreadedDeformation< TDeformationFilter >
43 TThreadedExecution;
44
45 // -------------------------------------------------------------------------
46 int main( int argc, char* argv[] )
47 {
48   // WARNING: Always call this static method FIRST!!!
49   TThreadedExecution::InitThreadSupport( );
50
51   // Input parameters
52   if( argc < 2 )
53   {
54     std::cerr
55       << "Usage: " << argv[ 0 ]
56       << " input_triangulation"
57       << std::endl;
58     return( 1 );
59
60   } // fi
61   std::string input_triangulation_fn = argv[ 1 ];
62
63   // Read triangulation
64   TTriangulationReader::Pointer input_triangulation_reader =
65     TTriangulationReader::New( );
66   input_triangulation_reader->SetFileName( input_triangulation_fn );
67
68   // Compute the simplex (i.e. the triangulation's dual) mesh
69   TTriangulationToSimplexFilter::Pointer triangl2simplex =
70     TTriangulationToSimplexFilter::New( );
71   triangl2simplex->SetInput( input_triangulation_reader->GetOutput( ) );
72   triangl2simplex->Update( );
73   TSimplex::Pointer simplex = triangl2simplex->GetOutput( );
74   simplex->DisconnectPipeline( );
75
76   // Map it to VTK
77   vtkSmartPointer< TSimplexMapper > simplex_mapper =
78     vtkSmartPointer< TSimplexMapper >::New( );
79   simplex_mapper->SetInputData( simplex );
80
81   vtkSmartPointer< vtkActor > simplex_actor =
82     vtkSmartPointer< vtkActor >::New( );
83   simplex_actor->SetMapper( simplex_mapper );
84   simplex_actor->GetProperty( )->SetColor( 1, 0, 0 );
85   simplex_actor->GetProperty( )->SetOpacity( 1 );
86   simplex_actor->GetProperty( )->SetLineWidth( 2 );
87   simplex_actor->GetProperty( )->SetRepresentationToWireframe( );
88
89   // Create visualization stuff
90   vtkSmartPointer< vtkRenderer > renderer =
91     vtkSmartPointer< vtkRenderer >::New( );
92   vtkSmartPointer< vtkRenderWindow > window =
93     vtkSmartPointer< vtkRenderWindow >::New( );
94   vtkSmartPointer< vtkRenderWindowInteractor > interactor =
95     vtkSmartPointer< vtkRenderWindowInteractor >::New( );
96   vtkInteractorStyleSwitch* iswitch =
97     dynamic_cast< vtkInteractorStyleSwitch* >(
98       interactor->GetInteractorStyle( )
99       );
100   renderer->SetBackground( 0.1, 0.3, 0.5 );
101   window->AddRenderer( renderer );
102   window->SetSize( 800, 800 );
103   window->SetInteractor( interactor );
104   if( iswitch != NULL )
105     iswitch->SetCurrentStyleToTrackballCamera( );
106
107   // Associate actors
108   renderer->AddActor( simplex_actor );
109
110   // Interact for a while
111   renderer->ResetCamera( );
112   window->Render( );
113   interactor->Start( );
114
115   // Ok, now configure deformation objects...
116   TInternalForce::Pointer int_force = TInternalForce::New( );
117   int_force->SetPhiConstraintToC0( );
118
119   TDeformationFilter::Pointer deform = TDeformationFilter::New( );
120   deform->AddForce( int_force, TScalar( 1 ) );
121   deform->SetDamping( TScalar( 1 ) );
122   deform->SetInput( simplex );
123
124   // Threaded execution...
125   vtkSmartPointer< TThreadedExecution > thread =
126     vtkSmartPointer< TThreadedExecution >::New( );
127   int thId = thread->ExecuteFilter( deform, window );
128
129   // ... with active interaction
130   interactor->Start( );
131
132   std::cout << "-----> " << thread->IsThreadActive( thId ) << std::endl;
133   return( 0 );
134 }
135
136 // eof - $RCSfile$