Exit on EOF
[ctsim.git] / tools / ctsimtext.cpp
index 0414cc94abc06ea84be1338527861bd1562525e7..e1695b127f666d8ab34a22ce2e811a1c05b6957a 100644 (file)
@@ -7,9 +7,7 @@
 **   Date Started:  Jan 2001
 **
 **  This is part of the CTSim program
-**  Copyright (C) 1983-2000 Kevin Rosenberg
-**
-**  $Id: ctsimtext.cpp,v 1.18 2001/03/31 16:43:20 kevin Exp $
+**  Copyright (C) 1983-2009 Kevin Rosenberg
 **
 **  This program is free software; you can redistribute it and/or modify
 **  it under the terms of the GNU General Public License (version 2) as
@@ -38,14 +36,14 @@ extern "C" {
 #include <readline/history.h>
 };
 #endif
-
+#include <exception>
 
 // Master shell for all command-line tools
 // If called as ctsimtext, program will look to next token on command-line as the function name
 // If linked to ctsimtext, but executed as another name, eg pjrec, then program will use that
 // linked name as name of function.
 
-static const char* const g_szIdStr = "$Id: ctsimtext.cpp,v 1.18 2001/03/31 16:43:20 kevin Exp $";
+static const char* const g_szIdStr = "$Id$";
 static const char* const s_szProgramName = "ctsimtext";
 static const char* const s_szProgramName2 = "ctsimtext.exe";
 static const char* const s_szProgramName3 = "ctsimtext-lam";
@@ -56,19 +54,26 @@ extern int ifexport_main (int argc, char* const argv[]);
 extern int ifinfo_main (int argc, char* const argv[]);
 extern int phm2if_main (int argc, char* const argv[]);
 extern int phm2pj_main (int argc, char* const argv[]);
+extern int phm2helix_main (int argc, char* const argv[]);
+extern int pjHinterp_main (int argc, char* const argv[]);
 extern int pj2if_main (int argc, char* const argv[]);
 extern int pjinfo_main (int argc, char* const argv[]);
 extern int pjrec_main (int argc, char* const argv[]);
+extern int linogram_main (int argc, char* const argv[]);
 
 static int processCommand (int argc, char* const argv[]);
 static void convertStringToArgcv (char* szLine, int* piArgc, char*** pppArgv);
 
 
-void 
+void
 ctsimtext_usage (const char *program)
 {
   std::cout << "usage: " << fileBasename(program) << " ctsim-function-name ctstim-function-parameters...\n";
-  std::cout << "CTSim text shell\n\n";
+  std::cout << "CTSim text shell";
+#ifdef VERSION
+  std::cout << ", Version " <<VERSION;
+#endif
+  std::cout << "\n\n";
   std::cout << "  if1           Single image file conversion\n";
   std::cout << "  if2           Dual image file conversions\n";
   std::cout << "  ifexport      Export an imagefile to a graphics file\n";
@@ -78,9 +83,12 @@ ctsimtext_usage (const char *program)
   std::cout << "  pjrec         Projection reconstruction\n";
   std::cout << "  phm2if        Convert a geometric phantom into an imagefile\n";
   std::cout << "  phm2pj        Take projections of a phantom object\n";
+  std::cout << "  phm2helix     Take projections of a phantom object\n";
+  std::cout << "  pjHinterp     Interpolate helical projections of a phantom object\n";
+  std::cout << "  linogram      Print linogram sampling\n";
 }
 
-void 
+void
 interactive_usage ()
 {
   std::cout << "usage: function-name parameters...\n";
@@ -91,31 +99,38 @@ interactive_usage ()
   std::cout << "  if2           Dual image file conversions\n";
   std::cout << "  phm2if        Convert a geometric phantom into an imagefile\n";
   std::cout << "  phm2pj        Take projections of a phantom object\n";
+  std::cout << "  phm2helix     Take projections of a phantom object\n";
   std::cout << "  pjinfo        Projection file information\n";
   std::cout << "  pj2if         Convert an projection file into an imagefile\n";
+  std::cout << "  pjHinterp     Interpolate helical projections of a phantom object\n";
   std::cout << "  pjrec         Projection reconstruction\n";
   std::cout << "  quit          Quits shell\n";
+  std::cout << "  linogram      Display linogram sampling\n";
   std::cout << "All functions accept --help as parameter for online help\n\n";
 }
 
 static bool s_bInteractive = false;
 
-int 
+int
 ctsimtext_main (int argc, char * argv[])
 {
-  int iReturn = 0;  
-  
+  int iReturn = 0;
+
   if (argc > 1 && (strcmp(s_szProgramName, fileBasename (argv[0])) == 0 || strcmp(s_szProgramName2, fileBasename (argv[0])) == 0 || strcmp(s_szProgramName3, fileBasename (argv[0])) == 0)) {
     argv++;
     argc--;
     iReturn = processCommand (argc, argv);
-  } else if (argc >=  1) {
+  } else if (argc >= 1 && ! (strcmp(s_szProgramName, fileBasename (argv[0])) == 0 || strcmp(s_szProgramName2, fileBasename (argv[0])) == 0 || strcmp(s_szProgramName3, fileBasename (argv[0])) == 0)) {
     iReturn = processCommand (argc, argv);
   } else {
     s_bInteractive = true;
     char szPrompt[] = "CTSim> ";
-    std::cout << "CTSim Text Shell (Type \"quit\" to end)\n\n";
-    
+    std::cout << "CTSim Text Shell";
+#ifdef VERSION
+    std::cout << ", Version " << VERSION;
+#endif
+    std::cout << " (Type \"quit\" to end)\n\n";
+
     while (1) {
 #ifdef HAVE_READLINE
       char* pszInputLine = readline (szPrompt);
@@ -123,36 +138,40 @@ ctsimtext_main (int argc, char * argv[])
         break;
       if (*pszInputLine != EOS)
         add_history (pszInputLine);
-      
+
 #else  // DONT_HAVE_READLINE
-      
+
       static const int s_MaxLineLength = 1024;
       char* pszInputLine = new char [s_MaxLineLength+1];
       std::cout << szPrompt;
       std::cin.getline (pszInputLine, s_MaxLineLength);
-      
+      if (std::cin.eof()) {
+        std::cout << "\n";
+        break;
+      }
+
 #ifdef DEBUG
       std::cout << "#" << pszInputLine << "#\n";
 #endif
-      
+
       std::cout << std::flush;
       std::cout << "\n";
 #endif  // DONT_HAVE_READLINE
-      
-      if (strncasecmp (pszInputLine, "quit", 4) == 0) 
+
+      if (strncasecmp (pszInputLine, "quit", 4) == 0)
         break;
-      
+
       convertStringToArgcv (pszInputLine, &argc, &argv);
 #ifdef DEBUG
-      for (unsigned int i = 0; i < argc; i++)
+      for (int i = 0; i < argc; i++)
         std::cout << "Token " << i << ": " << argv[i] << "\n";
 #endif
       iReturn = processCommand (argc, argv);
-      
+
       delete pszInputLine;
     }
   }
-  
+
   return iReturn;
 }
 
@@ -162,12 +181,12 @@ convertStringToArgcv (char* pszLine, int* piArgc, char*** pppArgv)
   char* pCurrentPos = pszLine;
   int nTokens = 0;
   std::vector<char*> vecpszToken;
-  
+
   // Process line
   bool bInDoubleQuote = false;
   bool bInSingleQuote = false;
   bool bInToken = false;
-  
+
   while (*pCurrentPos) {
     if (isspace (*pCurrentPos)) {
       if (! bInToken)
@@ -208,10 +227,10 @@ convertStringToArgcv (char* pszLine, int* piArgc, char*** pppArgv)
       nTokens++;
       vecpszToken.push_back (pCurrentPos);
     }
-    
+
     pCurrentPos++;
   }
-  
+
   *piArgc = nTokens;
   if (nTokens > 0) {
     *pppArgv = new char* [nTokens];
@@ -221,15 +240,15 @@ convertStringToArgcv (char* pszLine, int* piArgc, char*** pppArgv)
     *pppArgv = NULL;
 }
 
-static int 
+static int
 processCommand (int argc, char* const argv[])
 {
   if (argc < 1)
     return 1;
-  
+
   const char* const pszFunction = fileBasename (argv[0]);
-  
-  try {  
+
+  try {
     if (strcasecmp (pszFunction, "if1") == 0)
       return if1_main (argc, argv);
     else if (strcasecmp (pszFunction, "if2") == 0)
@@ -242,12 +261,18 @@ processCommand (int argc, char* const argv[])
       return phm2if_main (argc, argv);
     else if (strcasecmp (pszFunction, "phm2pj") == 0)
       return phm2pj_main (argc, argv);
+    else if (strcasecmp (pszFunction, "phm2helix") == 0)
+      return phm2helix_main (argc, argv);
+    else if (strcasecmp (pszFunction, "pjHinterp") == 0)
+      return pjHinterp_main (argc, argv);
     else if (strcasecmp (pszFunction, "pj2if") == 0)
       return pj2if_main (argc, argv);
     else if (strcasecmp (pszFunction, "pjinfo") == 0)
       return pjinfo_main (argc, argv);
     else if (strcasecmp (pszFunction, "pjrec") == 0)
       return pjrec_main (argc, argv);
+    else if (strcasecmp (pszFunction, "linogram") == 0)
+      return linogram_main (argc, argv);
     else {
       std::cout << "Unknown function name: " << pszFunction << "\n";
       if (s_bInteractive)
@@ -256,22 +281,53 @@ processCommand (int argc, char* const argv[])
         ctsimtext_usage (s_szProgramName);
       return 1;
     }
-  } catch (exception e) {
+  } catch (std::exception e) {
     std::cerr << "Exception: " << e.what() << std::endl;
   } catch (...) {
     std::cerr << "Unknown exception caught\n";
-  }  
+  }
 
   return 1;
 }
 
-int 
+int
 main (int argc, char* argv[])
 {
-  int retval = 1;
-  
-  retval = ctsimtext_main(argc, argv);
-  
+#if defined(HAVE_FFTW) && defined(HAVE_GETENV)
+  const char* const pszHome = getenv("HOME");
+  char* pszWisdom = NULL;
+
+  if (pszHome) {
+    const char szFileBase[] = ".fftw3-wisdom";
+    int nHome = strlen(pszHome);
+    int nBase = strlen(szFileBase);
+    int len = nHome + nBase + 1;
+    pszWisdom = new char [ len + 1 ];
+    strcpy (pszWisdom, pszHome);
+    pszWisdom[nHome] = '/';
+    strcpy(pszWisdom+nHome+1,szFileBase);
+    pszWisdom[nHome+nBase+2] = 0;
+
+    FILE *wisdom = fopen(pszWisdom,"r");
+    if (wisdom) {
+      fftw_import_wisdom_from_file(wisdom);
+      fclose(wisdom);
+    }
+  }
+#endif
+
+  int retval = ctsimtext_main(argc, argv);
+
+#if defined(HAVE_FFTW) && defined(HAVE_GETENV)
+  if (pszWisdom) {
+    FILE* wisdom = fopen(pszWisdom,"w+");
+    if (wisdom) {
+      fftw_export_wisdom_to_file(wisdom);
+      fclose(wisdom);
+      delete [] pszWisdom;
+    }
+  }
+#endif
+
   return retval;
 }
-