]> Creatis software - clitk.git/blobdiff - tools/clitkMeshViewer.cxx
Ensure compatibility with newer version of VTK9
[clitk.git] / tools / clitkMeshViewer.cxx
index a56275a5bce4a861a89b0ad4cacc0bdd1151a380..c21bc4e43f31cf7e9d64936076b1411ad4562d48 100644 (file)
@@ -30,6 +30,9 @@
 #include "vtkProperty.h"
 #include "vtkInteractorStyle.h"
 
+#include "clitkCommon.h"
+#include "clitkMeshViewer_ggo.h"
+
 #include <string>
 #include <iostream>
 #include <vector>
@@ -39,13 +42,17 @@ typedef vtkSmartPointer<vtkPolyDataMapper> MapperType;
 typedef vtkSmartPointer<vtkActor> ActorType;
 
 
-void run(int argc, char** argv);
+long run(const args_info_clitkMeshViewer& argsInfo);
 
 // Adapted from vtkAnimationCue example...
 class CueAnimator
 {
 public:
-  CueAnimator(std::vector<ActorType>& rActors) : m_Fps(1), m_LastTick(0), m_CurrentActor(0), m_rActors(rActors) {
+  CueAnimator(std::vector<ActorType>& rActors) : m_Fps(1), m_CurrentActor(0), m_rActors(rActors) {
+    m_rActors[0]->SetVisibility(1);
+    for (unsigned int i = 1; i < m_rActors.size(); i++) {
+      m_rActors[i]->SetVisibility(0);
+    }
   }
   
   ~CueAnimator() {
@@ -58,45 +65,37 @@ public:
   void StartCue(vtkAnimationCue::AnimationCueInfo *vtkNotUsed(info),
                 vtkRenderer *ren)
     {
-      std::cout << "StartCue" << std::endl;
-      m_LastTick = 0;
-      m_rActors[0]->SetVisibility(1);
-      
-      for (unsigned int i = 1; i < m_rActors.size(); i++) {
-        m_rActors[i]->SetVisibility(0);
-      }
-
+      //std::cout << "StartCue" << std::endl;
     }
   
   void Tick(vtkAnimationCue::AnimationCueInfo *info,
             vtkRenderer *ren)
     {
-      std::cout << "Tick AT:" << info->AnimationTime << " DT:" << info->DeltaTime << " CT:" << info->ClockTime << std::endl;
-      if (info->AnimationTime - m_LastTick < 1/m_Fps)
-        return;
-      
-      m_LastTick = info->AnimationTime;
+      //std::cout << "Tick AT:" << info->AnimationTime << " DT:" << info->DeltaTime << " CT:" << info->ClockTime << std::endl;
       
       m_rActors[m_CurrentActor]->SetVisibility(0);
       
-      ++m_CurrentActor;
-      m_CurrentActor %= m_rActors.size();
-
+      int step = lrint(m_Fps * info->AnimationTime);
+      int actor = step % m_rActors.size();
+      
+      //if (actor != m_CurrentActor) std::cout << "Showing frame: " << m_CurrentActor << std::endl;
+      m_CurrentActor = actor;
       m_rActors[m_CurrentActor]->SetVisibility(1);
       
       ren->Render();
     }
   
-  void EndCue(vtkAnimationCue::AnimationCueInfo *vtkNotUsed(info),
+  void EndCue(vtkAnimationCue::AnimationCueInfo *info,
               vtkRenderer *ren)
     {
-      std::cout << "EndCue" << std::endl;
+      //std::cout << "EndCue" << std::endl;
     }
   
 protected:
   
   double m_Fps;
-  double m_LastTick;
+  clock_t m_LastTick;
+  double m_TotalTicks;
   int m_CurrentActor;
   std::vector<ActorType>& m_rActors;
 };
@@ -162,13 +161,13 @@ public:
                        void *calldata)
     {
       vtkRenderWindowInteractor *isi = dynamic_cast<vtkRenderWindowInteractor *>(caller);
-      std::cout << "Execute" << std::endl;
+      //std::cout << "Execute" << std::endl;
       switch (event)
       {
         case vtkCommand::KeyPressEvent:
         {
           std::string key = isi->GetKeySym();
-          std::cout << key[0] << std::endl;
+          //std::cout << key[0] << std::endl;
           switch (key[0])
           {
             case 'P':
@@ -204,30 +203,34 @@ public:
 
 int main(int argc, char** argv)
 {
-  if (argc == 0)
-    std::cout << "Usage: clitkMeshViewer FILE1 FILE2 ..." << std::endl;
+  GGO(clitkMeshViewer, args_info);
 
-  run(argc, argv);
-  
-  return EXIT_SUCCESS;
+  return run(args_info);
 }
 
-void run(int argc, char** argv)
+long run(const args_info_clitkMeshViewer& argsInfo)
 {
   std::vector<ObjReaderType> objs;
   std::vector<MapperType> mappers;
   std::vector<ActorType> actors;
   
-  int nfiles = argc;
-  std::string animate = argv[argc-1];
-  if (animate == "--animate")
-    nfiles = argc-1;
+  bool verbose = argsInfo.verbose_flag;
+
+  int nfiles = argsInfo.inputs_num;
+  if (nfiles == 0)
+  {
+    std::cout << "At leas one mesh (.OBJ) file must be given. See clitkMeshViewer -h." << std::endl;
+    return -1;
+  }
+  
+  if (verbose) 
+    std::cout << nfiles << " file(s) to be loaded..." << std::endl;
   
   vtkSmartPointer<vtkRenderer> aRenderer = vtkRenderer::New();
-  for (int i = 1; i < nfiles; i++) {    
-    std::string file = argv[i];
-    
-    std::cout << "Reading " << file << std::endl;
+  for (int i = 0; i < nfiles; i++) {    
+    std::string file = argsInfo.inputs[i];
+    if (verbose)
+      std::cout << "Reading " << file << std::endl;
     
     vtkSmartPointer<vtkOBJReader> preader = vtkOBJReader::New();
     preader->SetFileName(file.c_str());
@@ -272,10 +275,11 @@ void run(int argc, char** argv)
   vtkSmartPointer<vtkWindowObserver> window_observer;
   CueAnimator animator(actors);
   
-  double fps = 5;
-  animator.SetFps(fps);
+  bool animate = argsInfo.animate_flag;
+  if (animate) {
+    double fps = argsInfo.fps_arg;
+    animator.SetFps(fps);
 
-  if (animate == "--animate") {
     // Create an Animation Scene
     scene=vtkAnimationScene::New();
     scene->SetModeToRealTime();
@@ -308,6 +312,7 @@ void run(int argc, char** argv)
   }
 
   iren->Start(); 
+  return 0;
 }