From 480b57e97fba637b77f632f240f097703d536ff3 Mon Sep 17 00:00:00 2001
From: "Joseph F. Boudreau" <boudreau@pitt.edu>
Date: Sun, 2 Feb 2020 12:48:11 -0500
Subject: [PATCH] new command line tool called gmcat

---
 GMCAT/CMakeLists.txt |  27 +++++++
 GMCAT/src/gmcat.cxx  | 166 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 193 insertions(+)
 create mode 100644 GMCAT/CMakeLists.txt
 create mode 100644 GMCAT/src/gmcat.cxx

diff --git a/GMCAT/CMakeLists.txt b/GMCAT/CMakeLists.txt
new file mode 100644
index 0000000..67fb628
--- /dev/null
+++ b/GMCAT/CMakeLists.txt
@@ -0,0 +1,27 @@
+
+set(MYLIB_VERSION_MAJOR 1)
+set(MYLIB_VERSION_MINOR 0)
+set(MYLIB_VERSION_PATCH 0)
+
+set( CMAKE_BUILD_TYPE DEBUG )
+set(CMAKE_CXX_FLAGS "-fPIC -O0 -g -gdwarf-2" )
+
+project ( "gmcat" VERSION ${MYLIB_VERSION_MAJOR}.${MYLIB_VERSION_MINOR}.${MYLIB_VERSION_PATCH} LANGUAGES CXX )
+
+find_package( GeoModelCore REQUIRED )
+find_package( GeoModelIO REQUIRED )
+find_package( Eigen3 REQUIRED )
+file( GLOB SOURCES src/*.cxx )
+
+include_directories ("${PROJECT_SOURCE_DIR}")
+include_directories ("${PROJECT_SOURCE_DIR}/src")
+add_executable ( gmcat ${SOURCES} ${HEADERS}  )
+
+# External dependencies:
+
+target_link_libraries (gmcat GeoModelCore::GeoModelKernel GeoModelIO::GeoModelRead GeoModelIO::GeoModelWrite )
+install(TARGETS gmcat DESTINATION bin)
+
+
+
+
diff --git a/GMCAT/src/gmcat.cxx b/GMCAT/src/gmcat.cxx
new file mode 100644
index 0000000..abf2634
--- /dev/null
+++ b/GMCAT/src/gmcat.cxx
@@ -0,0 +1,166 @@
+#include "GeoModelKernel/GeoVGeometryPlugin.h"
+#include "GeoModelDBManager/GMDBManager.h"
+#include "GeoModelRead/ReadGeoModel.h"
+#include "GeoModelWrite/WriteGeoModel.h"
+
+#include "GeoModelKernel/GeoGeometryPluginLoader.h"
+#include "GeoModelKernel/GeoVolumeCursor.h"
+#include "GeoModelKernel/GeoMaterial.h"
+#include "GeoModelKernel/GeoElement.h"
+#include "GeoModelKernel/GeoPVLink.h"
+
+
+#include "GeoModelKernel/GeoBox.h"
+#include "GeoModelKernel/GeoCountVolAction.h"
+#include "GeoModelKernel/GeoAccessVolumeAction.h"
+#include "GeoModelKernel/GeoNameTag.h"
+
+#include <iostream>
+#include <string>
+#include <vector>
+#ifdef __APPLE__
+const std::string shared_obj_extension=".dylib";
+#else
+const std::string shared_obj_extension=".so";
+#endif
+
+#define SYSTEM_OF_UNITS GeoModelKernelUnits // --> 'GeoModelKernelUnits::cm'
+
+int main(int argc, char ** argv) {
+  //
+  // Usage message:
+  //
+  
+  std::string gmcat= argv[0];
+  std::string usage= "usage: " + gmcat + " [plugin1"+shared_obj_extension
+    + "] [plugin2" + shared_obj_extension
+    + "] ...[file1.db] [file2.db].. -o outputFile]"; 
+  //
+  // Print usage message if no args given:
+  // 
+  if (argc==1) {
+    std::cerr << usage << std::endl;
+    return 0;
+  }
+  //
+  // Parse the command line:
+  // 
+  std::vector<std::string> inputFiles;
+  std::vector<std::string> inputPlugins;
+  std::string outputFile;
+  for (int argi=1;argi<argc;argi++) {
+    std::string argument=argv[argi];
+    if (argument.find("-o")!=std::string::npos) {
+      argi++;
+      if (argi>=argc) {
+	std::cerr << usage << std::endl;
+	return 1;
+      }
+      outputFile=argv[argi];
+    }
+    else if (argument.find(shared_obj_extension)!=std::string::npos) {
+      inputPlugins.push_back(argument);
+    }
+    else if (argument.find(".db")!=std::string::npos) {
+      inputFiles.push_back(argument);
+    }
+    else {
+      std::cerr << "Unrecognized argument " << argument << std::endl;
+      std::cerr << usage << std::endl;
+      return 2;
+    }
+  }
+
+  // if (((inputPlugins.size()+inputFiles.size())<1) || outputFile=="") {
+  //   std::cout << usage << std::endl;
+  //   return 3;
+  // }
+  //
+  // Create a huge world volume made of Air:
+  //
+  const double  gr =   SYSTEM_OF_UNITS::gram;
+  const double  mole = SYSTEM_OF_UNITS::mole;
+  const double  cm3 =  SYSTEM_OF_UNITS::cm3;
+  
+  // Define the chemical elements
+  GeoElement*  Nitrogen = new GeoElement ("Nitrogen" ,"N"  ,  7.0 ,  14.0067 *gr/mole);
+  GeoElement*  Oxygen   = new GeoElement ("Oxygen"   ,"O"  ,  8.0 ,  15.9995 *gr/mole);
+  GeoElement*  Argon    = new GeoElement ("Argon"    ,"Ar" , 18.0 ,  39.948  *gr/mole);
+  GeoElement*  Hydrogen = new GeoElement ("Hydrogen" ,"H"  ,  1.0 ,  1.00797 *gr/mole);
+  
+  double densityOfAir=0.001214 *gr/cm3;
+  GeoMaterial *air = new GeoMaterial("Air", densityOfAir);
+  air->add(Nitrogen  , 0.7494);
+  air->add(Oxygen, 0.2369);
+  air->add(Argon, 0.0129);
+  air->add(Hydrogen, 0.0008);
+  air->lock();
+  
+  const GeoBox* worldBox = new GeoBox(2000*SYSTEM_OF_UNITS::cm, 2000*SYSTEM_OF_UNITS::cm, 2000*SYSTEM_OF_UNITS::cm);
+  const GeoLogVol* worldLog = new GeoLogVol("WorldLog", worldBox, air);
+  GeoPhysVol *world=new GeoPhysVol(worldLog);
+  world->ref();
+  //
+  // Loop over plugins, create the geometry and put it under the world:
+  //
+  for (const std::string & plugin : inputPlugins) {
+    GeoGeometryPluginLoader loader;
+    GeoVGeometryPlugin *factory=loader.load(plugin);
+    if (!factory) {
+      std::cerr << "Could not load plugin " << plugin << std::endl;
+      return 5;
+    }
+    factory->create(world);
+  }
+  
+  //
+  // Loop over files, create the geometry and put it under the world:
+  //
+  for (const std::string & file : inputFiles) {
+    GMDBManager* db = new GMDBManager(file.c_str());
+    if (!db->isOpen()){
+      std::cerr << "Error opening input file " << file << std::endl;
+      return 6;
+    }
+      
+    /* set the GeoModel reader */
+    GeoModelIO::ReadGeoModel readInGeo = GeoModelIO::ReadGeoModel(db);
+      
+    /* build the GeoModel geometry */
+    GeoPhysVol* dbPhys = readInGeo.buildGeoModel(); // builds the whole GeoModel tree in memory
+
+    //world->add(dbPhys);
+    GeoVolumeCursor aV(dbPhys);
+	
+    while (!aV.atEnd()) {
+      GeoNameTag *nameTag=new GeoNameTag(aV.getName());
+      GeoTransform *transform= new GeoTransform(aV.getTransform());
+      world->add(nameTag);
+      world->add(transform);
+      world->add((GeoVPhysVol *) &*aV.getVolume());
+      aV.next();
+    }
+    delete db;
+  }
+
+  //
+  // Open a new database:
+  //
+  GMDBManager db(outputFile.c_str());
+  GeoModelIO::WriteGeoModel dumpGeoModelGraph(db);
+   
+  // check the DB connection
+  if (!db.isOpen()) {
+    std::cerr << "Error opening output file " << outputFile << std::endl;
+    return 4;
+  }
+
+  world->exec(&dumpGeoModelGraph);
+  dumpGeoModelGraph.saveToDB();
+   
+  world->unref();
+  
+  
+  return 0;
+}
+
-- 
GitLab