diff --git a/CMakeLists.txt b/CMakeLists.txt index c2eda1d..187bcc2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -146,7 +146,8 @@ else() RenderingFreeType RenderingGL2PSOpenGL2 RenderingOpenGL2 - RenderingVolumeOpenGL2) + RenderingVolumeOpenGL2 + IOGeometry) endif() set(CMAKE_REQUIRED_INCLUDES CMAKE_REQUIRED_INCLUDES math.h) @@ -204,8 +205,8 @@ add_subdirectory(${SRC_DIR}/Core) include_directories(${SRC_DIR}/Math) add_subdirectory(${SRC_DIR}/Math) -include_directories(${SRC_DIR}/Detectors) -add_subdirectory(${SRC_DIR}/Detectors) +include_directories(${SRC_DIR}/HEP) +add_subdirectory(${SRC_DIR}/HEP) include_directories(${SRC_DIR}/Root) add_subdirectory(${SRC_DIR}/Root) diff --git a/src/Core/CMakeLists.txt b/src/Core/CMakeLists.txt index d337c97..98de62a 100644 --- a/src/Core/CMakeLists.txt +++ b/src/Core/CMakeLists.txt @@ -49,7 +49,7 @@ endif() target_link_libraries(${libname} ${LIBRARIES}) install(TARGETS ${libname} - EXPORT "${PROJECT_NAME}Targets" + EXPORT "uLibTargets" RUNTIME DESTINATION ${INSTALL_BIN_DIR} COMPONENT bin LIBRARY DESTINATION ${INSTALL_LIB_DIR} COMPONENT lib) diff --git a/src/Detectors/CMakeLists.txt b/src/Detectors/CMakeLists.txt deleted file mode 100644 index 0022084..0000000 --- a/src/Detectors/CMakeLists.txt +++ /dev/null @@ -1,12 +0,0 @@ -set(HEADERS MuonScatter.h MuonError.h MuonEvent.h) - -set(ULIB_SELECTED_MODULES ${ULIB_SELECTED_MODULES} Detectors PARENT_SCOPE) - - -install(FILES ${HEADERS} - DESTINATION ${INSTALL_INC_DIR}/Detectors) - -if(BUILD_TESTING) - include(uLibTargetMacros) - add_subdirectory(testing) -endif() diff --git a/src/HEP/CMakeLists.txt b/src/HEP/CMakeLists.txt new file mode 100644 index 0000000..58f2afc --- /dev/null +++ b/src/HEP/CMakeLists.txt @@ -0,0 +1,12 @@ + +################################################################################ +##### HEP - High Energy Physics modules ######################################## +################################################################################ + +include_directories(${SRC_DIR}/HEP) + +add_subdirectory(Detectors) +add_subdirectory(Geant) + +set(ULIB_SHARED_LIBRARIES ${ULIB_SHARED_LIBRARIES} PARENT_SCOPE) +set(ULIB_SELECTED_MODULES ${ULIB_SELECTED_MODULES} PARENT_SCOPE) diff --git a/src/HEP/Detectors/CMakeLists.txt b/src/HEP/Detectors/CMakeLists.txt new file mode 100644 index 0000000..f793ae5 --- /dev/null +++ b/src/HEP/Detectors/CMakeLists.txt @@ -0,0 +1,34 @@ +set(HEADERS + ChamberHitEvent.h + DetectorChamber.h + ExperimentFitEvent.h + HierarchicalEncoding.h + Hit.h + HitMC.h + LinearFit.h + MuonError.h + MuonEvent.h + MuonScatter.h +) + +set(libname ${PACKAGE_LIBPREFIX}Detectors) +set(ULIB_SHARED_LIBRARIES ${ULIB_SHARED_LIBRARIES} ${libname} PARENT_SCOPE) +set(ULIB_SELECTED_MODULES ${ULIB_SELECTED_MODULES} Detectors PARENT_SCOPE) + +## Headers-only INTERFACE library +add_library(${libname} INTERFACE) +target_include_directories(${libname} INTERFACE + $ + $ +) + +install(TARGETS ${libname} + EXPORT "uLibTargets") + +install(FILES ${HEADERS} + DESTINATION ${INSTALL_INC_DIR}/HEP/Detectors) + +if(BUILD_TESTING) + include(uLibTargetMacros) + add_subdirectory(testing) +endif() diff --git a/src/Detectors/ChamberHitEvent.h b/src/HEP/Detectors/ChamberHitEvent.h similarity index 100% rename from src/Detectors/ChamberHitEvent.h rename to src/HEP/Detectors/ChamberHitEvent.h diff --git a/src/Detectors/DetectorChamber.h b/src/HEP/Detectors/DetectorChamber.h similarity index 100% rename from src/Detectors/DetectorChamber.h rename to src/HEP/Detectors/DetectorChamber.h diff --git a/src/Detectors/ExperimentFitEvent.h b/src/HEP/Detectors/ExperimentFitEvent.h similarity index 100% rename from src/Detectors/ExperimentFitEvent.h rename to src/HEP/Detectors/ExperimentFitEvent.h diff --git a/src/Detectors/HierarchicalEncoding.h b/src/HEP/Detectors/HierarchicalEncoding.h similarity index 100% rename from src/Detectors/HierarchicalEncoding.h rename to src/HEP/Detectors/HierarchicalEncoding.h diff --git a/src/Detectors/Hit.h b/src/HEP/Detectors/Hit.h similarity index 100% rename from src/Detectors/Hit.h rename to src/HEP/Detectors/Hit.h diff --git a/src/Detectors/HitMC.h b/src/HEP/Detectors/HitMC.h similarity index 100% rename from src/Detectors/HitMC.h rename to src/HEP/Detectors/HitMC.h diff --git a/src/Detectors/LinearFit.h b/src/HEP/Detectors/LinearFit.h similarity index 100% rename from src/Detectors/LinearFit.h rename to src/HEP/Detectors/LinearFit.h diff --git a/src/Detectors/Makefile.am b/src/HEP/Detectors/Makefile.am similarity index 100% rename from src/Detectors/Makefile.am rename to src/HEP/Detectors/Makefile.am diff --git a/src/Detectors/MuonError.h b/src/HEP/Detectors/MuonError.h similarity index 100% rename from src/Detectors/MuonError.h rename to src/HEP/Detectors/MuonError.h diff --git a/src/Detectors/MuonEvent.h b/src/HEP/Detectors/MuonEvent.h similarity index 100% rename from src/Detectors/MuonEvent.h rename to src/HEP/Detectors/MuonEvent.h diff --git a/src/Detectors/MuonScatter.h b/src/HEP/Detectors/MuonScatter.h similarity index 100% rename from src/Detectors/MuonScatter.h rename to src/HEP/Detectors/MuonScatter.h diff --git a/src/Detectors/testing/CMakeLists.txt b/src/HEP/Detectors/testing/CMakeLists.txt similarity index 100% rename from src/Detectors/testing/CMakeLists.txt rename to src/HEP/Detectors/testing/CMakeLists.txt diff --git a/src/Detectors/testing/GDMLSolidTest.cpp b/src/HEP/Detectors/testing/GDMLSolidTest.cpp similarity index 100% rename from src/Detectors/testing/GDMLSolidTest.cpp rename to src/HEP/Detectors/testing/GDMLSolidTest.cpp diff --git a/src/Detectors/testing/HierarchicalEncodingTest.cpp b/src/HEP/Detectors/testing/HierarchicalEncodingTest.cpp similarity index 100% rename from src/Detectors/testing/HierarchicalEncodingTest.cpp rename to src/HEP/Detectors/testing/HierarchicalEncodingTest.cpp diff --git a/src/Detectors/testing/Makefile.am b/src/HEP/Detectors/testing/Makefile.am similarity index 100% rename from src/Detectors/testing/Makefile.am rename to src/HEP/Detectors/testing/Makefile.am diff --git a/src/Detectors/testing/testing-prototype.h b/src/HEP/Detectors/testing/testing-prototype.h similarity index 100% rename from src/Detectors/testing/testing-prototype.h rename to src/HEP/Detectors/testing/testing-prototype.h diff --git a/src/HEP/Geant/CMakeLists.txt b/src/HEP/Geant/CMakeLists.txt new file mode 100644 index 0000000..92a2a36 --- /dev/null +++ b/src/HEP/Geant/CMakeLists.txt @@ -0,0 +1,52 @@ + +################################################################################ +##### HEP/Geant - Geant4 integration library ################################### +################################################################################ + +find_package(Geant4 QUIET) + +if(NOT Geant4_FOUND) + message(STATUS "Geant4 not found - skipping mutomGeant library") + return() +endif() + +message(STATUS "Geant4 found: ${Geant4_VERSION}") +include(${Geant4_USE_FILE}) + +set(HEADERS + GeantEvent.h + Matter.h + Scene.h + Solid.h +) + +set(SOURCES + Scene.cpp + Solid.cpp +) + +set(libname ${PACKAGE_LIBPREFIX}Geant) +set(ULIB_SHARED_LIBRARIES ${ULIB_SHARED_LIBRARIES} ${libname} PARENT_SCOPE) +set(ULIB_SELECTED_MODULES ${ULIB_SELECTED_MODULES} Geant PARENT_SCOPE) + +add_library(${libname} SHARED ${SOURCES}) +set_target_properties(${libname} PROPERTIES + VERSION ${PROJECT_VERSION} + SOVERSION ${PROJECT_SOVERSION}) + +target_include_directories(${libname} PRIVATE ${Geant4_INCLUDE_DIRS}) + +target_link_libraries(${libname} + ${PACKAGE_LIBPREFIX}Core + ${PACKAGE_LIBPREFIX}Math + ${PACKAGE_LIBPREFIX}Detectors + ${Geant4_LIBRARIES} +) + +install(TARGETS ${libname} + EXPORT "uLibTargets" + RUNTIME DESTINATION ${INSTALL_BIN_DIR} COMPONENT bin + LIBRARY DESTINATION ${INSTALL_LIB_DIR} COMPONENT lib) + +install(FILES ${HEADERS} + DESTINATION ${INSTALL_INC_DIR}/HEP/Geant) diff --git a/src/Detectors/GeantEvent.h b/src/HEP/Geant/GeantEvent.h similarity index 70% rename from src/Detectors/GeantEvent.h rename to src/HEP/Geant/GeantEvent.h index 33d26aa..c57f55c 100644 --- a/src/Detectors/GeantEvent.h +++ b/src/HEP/Geant/GeantEvent.h @@ -23,8 +23,6 @@ //////////////////////////////////////////////////////////////////////////////*/ - - #ifndef U_GEANTEVENT_H #define U_GEANTEVENT_H @@ -38,32 +36,25 @@ namespace uLib { class GeantEventData { public: - uLibGetMacro (EventID, Id_t ) - uLibGetMacro (Momentum,Scalarf ) - uLibConstRefMacro (GenPos, Vector3f) - uLibConstRefMacro (GenDir, Vector3f) - uLibConstRefMacro (ChEvents,Vector) +uLibGetMacro(EventID, Id_t) uLibGetMacro(Momentum, Scalarf) + uLibConstRefMacro(GenPos, Vector3f) uLibConstRefMacro(GenDir, Vector3f) + uLibConstRefMacro(ChEvents, Vector) -private: - friend class GeantEvent; - Id_t m_EventID; - Scalarf m_Momentum; - Vector3f m_GenPos; - Vector3f m_GenDir; - Vector m_ChEvents; + private : friend class GeantEvent; + Id_t m_EventID; + Scalarf m_Momentum; + Vector3f m_GenPos; + Vector3f m_GenDir; + Vector m_ChEvents; }; class GeantEvent { public: - uLibSetMacro (EventID, Id_t ) - uLibSetMacro (Momentum,Scalarf ) - uLibRefMacro (GenPos, Vector3f) - uLibRefMacro (GenDir, Vector3f) - uLibRefMacro (ChEvents,Vector) + uLibSetMacro(EventID, Id_t) uLibSetMacro(Momentum, Scalarf) + uLibRefMacro(GenPos, Vector3f) uLibRefMacro(GenDir, Vector3f) + uLibRefMacro(ChEvents, Vector) }; - -} - +} // namespace uLib #endif // GEANTEVENT_H diff --git a/src/Detectors/Matter.h b/src/HEP/Geant/Matter.h similarity index 100% rename from src/Detectors/Matter.h rename to src/HEP/Geant/Matter.h diff --git a/src/Detectors/Scene.cpp b/src/HEP/Geant/Scene.cpp similarity index 100% rename from src/Detectors/Scene.cpp rename to src/HEP/Geant/Scene.cpp diff --git a/src/Detectors/Scene.h b/src/HEP/Geant/Scene.h similarity index 100% rename from src/Detectors/Scene.h rename to src/HEP/Geant/Scene.h diff --git a/src/Detectors/Solid.cpp b/src/HEP/Geant/Solid.cpp similarity index 100% rename from src/Detectors/Solid.cpp rename to src/HEP/Geant/Solid.cpp diff --git a/src/Detectors/Solid.h b/src/HEP/Geant/Solid.h similarity index 100% rename from src/Detectors/Solid.h rename to src/HEP/Geant/Solid.h diff --git a/src/ParticlePhysics/MuonTomography/DetectorChamber.h b/src/HEP/MuonTomography/DetectorChamber.h similarity index 100% rename from src/ParticlePhysics/MuonTomography/DetectorChamber.h rename to src/HEP/MuonTomography/DetectorChamber.h diff --git a/src/ParticlePhysics/MuonTomography/DriftDetector.h b/src/HEP/MuonTomography/DriftDetector.h similarity index 100% rename from src/ParticlePhysics/MuonTomography/DriftDetector.h rename to src/HEP/MuonTomography/DriftDetector.h diff --git a/src/ParticlePhysics/MuonTomography/Hit.h b/src/HEP/MuonTomography/Hit.h similarity index 100% rename from src/ParticlePhysics/MuonTomography/Hit.h rename to src/HEP/MuonTomography/Hit.h diff --git a/src/ParticlePhysics/MuonTomography/Makefile.am b/src/HEP/MuonTomography/Makefile.am similarity index 100% rename from src/ParticlePhysics/MuonTomography/Makefile.am rename to src/HEP/MuonTomography/Makefile.am diff --git a/src/ParticlePhysics/MuonTomography/MuonScatter.h b/src/HEP/MuonTomography/MuonScatter.h similarity index 100% rename from src/ParticlePhysics/MuonTomography/MuonScatter.h rename to src/HEP/MuonTomography/MuonScatter.h diff --git a/src/Math/CMakeLists.txt b/src/Math/CMakeLists.txt index 5e6fb13..8e3ff8e 100644 --- a/src/Math/CMakeLists.txt +++ b/src/Math/CMakeLists.txt @@ -54,7 +54,7 @@ endif() install(TARGETS ${libname} - EXPORT "${PROJECT_NAME}Targets" + EXPORT "uLibTargets" RUNTIME DESTINATION ${INSTALL_BIN_DIR} COMPONENT bin LIBRARY DESTINATION ${INSTALL_LIB_DIR} COMPONENT lib) diff --git a/src/Math/testing/VoxRaytracerTestExtended.cpp b/src/Math/testing/VoxRaytracerTestExtended.cpp index 9696c97..fe29181 100644 --- a/src/Math/testing/VoxRaytracerTestExtended.cpp +++ b/src/Math/testing/VoxRaytracerTestExtended.cpp @@ -48,7 +48,7 @@ int main() { Raytracer rt(img); - const size_t NUM_RAYS = 1000000; + const size_t NUM_RAYS = 100000; std::cout << "Generating " << NUM_RAYS << " random ray pairs across a 100x100x100 grid...\n"; diff --git a/src/ParticlePhysics/Geant/Makefile.am b/src/ParticlePhysics/Geant/Makefile.am deleted file mode 100644 index 70c690d..0000000 --- a/src/ParticlePhysics/Geant/Makefile.am +++ /dev/null @@ -1,14 +0,0 @@ -include $(top_srcdir)/Common.am - -library_includedir = $(includedir)/libmutom-${PACKAGE_VERSION}/ParticlePhysics/Geant - -library_include_HEADERS = - -_PPGEANT_SOURCES = - - - -noinst_LTLIBRARIES = libmutomppgeant.la -libmutomppgeant_la_SOURCES = ${_PPGEANT_SOURCES} - - diff --git a/src/Python/CMakeLists.txt b/src/Python/CMakeLists.txt index 5ca99bf..fa5dec2 100644 --- a/src/Python/CMakeLists.txt +++ b/src/Python/CMakeLists.txt @@ -25,7 +25,7 @@ target_include_directories(uLib_python PRIVATE # Install uLib_python within the uLib install target install(TARGETS uLib_python - EXPORT "${PROJECT_NAME}Targets" + EXPORT "uLibTargets" RUNTIME DESTINATION ${INSTALL_BIN_DIR} COMPONENT bin LIBRARY DESTINATION ${INSTALL_LIB_DIR} COMPONENT lib ARCHIVE DESTINATION ${INSTALL_LIB_DIR} COMPONENT lib diff --git a/src/Root/CMakeLists.txt b/src/Root/CMakeLists.txt index 91efbf3..37143ff 100644 --- a/src/Root/CMakeLists.txt +++ b/src/Root/CMakeLists.txt @@ -56,7 +56,7 @@ set_target_properties(${libname} PROPERTIES target_link_libraries(${libname} ${LIBRARIES}) install(TARGETS ${libname} - EXPORT "${PROJECT_NAME}Targets" + EXPORT "uLibTargets" RUNTIME DESTINATION ${INSTALL_BIN_DIR} COMPONENT bin LIBRARY DESTINATION ${INSTALL_LIB_DIR} COMPONENT lib) diff --git a/src/Vtk/CMakeLists.txt b/src/Vtk/CMakeLists.txt index 8654e45..4ba3acd 100644 --- a/src/Vtk/CMakeLists.txt +++ b/src/Vtk/CMakeLists.txt @@ -1,23 +1,31 @@ set(HEADERS uLibVtkInterface.h uLibVtkViewer.h - vtkContainerBox.h - vtkMuonScatter.h - vtkStructuredGrid.h - vtkVoxRaytracerRepresentation.h - vtkVoxImage.h) + vtkContainerBox.h) set(SOURCES uLibVtkInterface.cxx uLibVtkViewer.cpp - vtkContainerBox.cpp - vtkMuonScatter.cxx - vtkStructuredGrid.cpp - vtkVoxRaytracerRepresentation.cpp - vtkVoxImage.cpp) + vtkContainerBox.cpp) + +## Pull in Math VTK wrappers (sets MATH_SOURCES / MATH_HEADERS) +add_subdirectory(Math) +list(APPEND SOURCES ${MATH_SOURCES}) +list(APPEND HEADERS ${MATH_HEADERS}) + +## Pull in HEP/Detectors VTK wrappers (sets HEP_DETECTORS_SOURCES / HEADERS) +add_subdirectory(HEP/Detectors) +list(APPEND SOURCES ${HEP_DETECTORS_SOURCES}) +list(APPEND HEADERS ${HEP_DETECTORS_HEADERS}) + +## Pull in HEP/MuonTomography VTK wrappers (sets HEP_MUONTOMOGRAPHY_SOURCES / HEADERS) +add_subdirectory(HEP/MuonTomography) +list(APPEND SOURCES ${HEP_MUONTOMOGRAPHY_SOURCES}) +list(APPEND HEADERS ${HEP_MUONTOMOGRAPHY_HEADERS}) set(LIBRARIES Eigen3::Eigen ${ROOT_LIBRARIES} ${VTK_LIBRARIES} - ${PACKAGE_LIBPREFIX}Math) + ${PACKAGE_LIBPREFIX}Math + ${PACKAGE_LIBPREFIX}Detectors) if(USE_CUDA) find_package(CUDAToolkit REQUIRED) @@ -35,11 +43,14 @@ set_target_properties(${libname} PROPERTIES target_link_libraries(${libname} ${LIBRARIES}) install(TARGETS ${libname} - EXPORT "${PROJECT_NAME}Targets" + EXPORT "uLibTargets" RUNTIME DESTINATION ${INSTALL_BIN_DIR} COMPONENT bin LIBRARY DESTINATION ${INSTALL_LIB_DIR} COMPONENT lib) install(FILES ${HEADERS} DESTINATION ${INSTALL_INC_DIR}/Vtk) +install(FILES ${MATH_HEADERS} DESTINATION ${INSTALL_INC_DIR}/Vtk/Math) +install(FILES ${HEP_DETECTORS_HEADERS} DESTINATION ${INSTALL_INC_DIR}/Vtk/HEP/Detectors) +install(FILES ${HEP_MUONTOMOGRAPHY_HEADERS} DESTINATION ${INSTALL_INC_DIR}/Vtk/HEP/MuonTomography) if(BUILD_TESTING) include(uLibTargetMacros) diff --git a/src/Vtk/HEP/Detectors/CMakeLists.txt b/src/Vtk/HEP/Detectors/CMakeLists.txt new file mode 100644 index 0000000..1937a8f --- /dev/null +++ b/src/Vtk/HEP/Detectors/CMakeLists.txt @@ -0,0 +1,22 @@ + +################################################################################ +##### Vtk/HEP/Detectors - VTK wrappers for HEP Detectors objects ############## +################################################################################ + +## Sources and headers are exported to parent scope so they get compiled +## into the single mutomVtk shared library. + +set(HEP_DETECTORS_SOURCES + ${CMAKE_CURRENT_SOURCE_DIR}/vtkMuonScatter.cxx + ${CMAKE_CURRENT_SOURCE_DIR}/vtkMuonEvent.cxx + PARENT_SCOPE) + +set(HEP_DETECTORS_HEADERS + ${CMAKE_CURRENT_SOURCE_DIR}/vtkMuonScatter.h + ${CMAKE_CURRENT_SOURCE_DIR}/vtkMuonEvent.h + PARENT_SCOPE) + +if(BUILD_TESTING) + include(uLibTargetMacros) + add_subdirectory(testing) +endif() diff --git a/src/Vtk/HEP/Detectors/testing/CMakeLists.txt b/src/Vtk/HEP/Detectors/testing/CMakeLists.txt new file mode 100644 index 0000000..2e36c4b --- /dev/null +++ b/src/Vtk/HEP/Detectors/testing/CMakeLists.txt @@ -0,0 +1,14 @@ +# TESTS +set(TESTS + vtkMuonScatterTest +) + +set(LIBRARIES + ${PACKAGE_LIBPREFIX}Core + ${PACKAGE_LIBPREFIX}Detectors + ${PACKAGE_LIBPREFIX}Vtk + ${VTK_LIBRARIES} + Boost::unit_test_framework +) + +uLib_add_tests(VtkDetectors) diff --git a/src/Vtk/HEP/Detectors/testing/vtkMuonScatterTest.cpp b/src/Vtk/HEP/Detectors/testing/vtkMuonScatterTest.cpp new file mode 100644 index 0000000..e90835f --- /dev/null +++ b/src/Vtk/HEP/Detectors/testing/vtkMuonScatterTest.cpp @@ -0,0 +1,62 @@ +/*////////////////////////////////////////////////////////////////////////////// +// CMT Cosmic Muon Tomography project ////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// + + Copyright (c) 2014, Universita' degli Studi di Padova, INFN sez. di Padova + All rights reserved + + Authors: Andrea Rigoni Garola < andrea.rigoni@pd.infn.it > + + ------------------------------------------------------------------ + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 3.0 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library. + +//////////////////////////////////////////////////////////////////////////////*/ + +#include "Vtk/HEP/Detectors/vtkMuonScatter.h" +#include "HEP/Detectors/MuonScatter.h" + +#include "Vtk/uLibVtkViewer.h" + +#define BOOST_TEST_MODULE VtkMuonScatterTest +#include + +using namespace uLib; + +BOOST_AUTO_TEST_CASE(vtkMuonScatterTest) { + MuonScatter event; + event.LineIn().direction << 0, -1, 1, 0; + event.LineIn().origin << 0, 1, -1, 1; + + event.LineOut().direction << 0, -1, 0, 0; + event.LineOut().origin << 0, -1, 0, 1; + + Vtk::vtkMuonScatter v_event(event); + v_event.AddPocaPoint(HPoint3f(0, 0, 0)); + + v_event.SaveToXMLFile("vtk_testing_muonevent.vtp"); + + if (std::getenv("CTEST_PROJECT_NAME") == nullptr) { + Vtk::Viewer viewer; + + // Vtk::Tie tms; + // tms.DoAction(); + // Vtk::Tie vms; + // vms.DoAction(); + + viewer.AddPuppet(v_event); + viewer.Start(); + } + + BOOST_CHECK(true); // reached here without crash +} diff --git a/src/Vtk/HEP/Detectors/vtkMuonEvent.cxx b/src/Vtk/HEP/Detectors/vtkMuonEvent.cxx new file mode 100644 index 0000000..9a4025d --- /dev/null +++ b/src/Vtk/HEP/Detectors/vtkMuonEvent.cxx @@ -0,0 +1,145 @@ +/*////////////////////////////////////////////////////////////////////////////// +// CMT Cosmic Muon Tomography project ////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// + + Copyright (c) 2014, Universita' degli Studi di Padova, INFN sez. di Padova + All rights reserved + + Authors: Andrea Rigoni Garola < andrea.rigoni@pd.infn.it > + + ------------------------------------------------------------------ + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 3.0 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library. + +//////////////////////////////////////////////////////////////////////////////*/ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "Math/Dense.h" +#include "Vtk/HEP/Detectors/vtkMuonEvent.h" + +namespace uLib { +namespace Vtk { + +///// CALLBACK ///////////////////////////////////////////////////////////////// + +class vtkWidgetCallback : public vtkCommand { +public: + static vtkWidgetCallback *New() { return new vtkWidgetCallback; } + void SetParent(uLib::Vtk::vtkMuonEvent *parent) { this->parent = parent; } + virtual void Execute(vtkObject *caller, unsigned long, void *) { + vtkSmartPointer t = vtkSmartPointer::New(); + vtkBoxWidget *widget = reinterpret_cast(caller); + widget->GetTransform(t); + // parent->SetTransform(t); + // std::cout << "event\n"; + } + +private: + uLib::Vtk::vtkMuonEvent *parent; +}; + +//////////////////////////////////////////////////////////////////////////////// +///// VTK MUON EVENT ///////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// + +vtkMuonEvent::vtkMuonEvent(MuonEventData &content) + : m_PolyData(NULL), m_Appender(vtkAppendPolyData::New()), + content(&content) { + InstallPipe(); +} + +vtkMuonEvent::vtkMuonEvent(const MuonEventData &content) + : m_PolyData(NULL), m_Appender(vtkAppendPolyData::New()), + content(const_cast(&content)) { + InstallPipe(); +} + +vtkMuonEvent::~vtkMuonEvent() {} + +vtkMuonEvent::Content &vtkMuonEvent::GetContent() { return *content; } + +void vtkMuonEvent::PrintSelf(std::ostream &o) const { + o << "..:: MuonEvent ::..\n" + "\t[in] Origin > " + << content->LineIn().origin.transpose() << "\n" + << "\t[in] Direction > " << content->LineIn().direction.transpose() << "\n" + << "\t[out] Origin > " << content->LineOut().origin.transpose() << "\n" + << "\t[out] Direction > " << content->LineOut().direction.transpose() + << "\n" + << "\tMomentum > " << content->GetMomentum() << "\n" + << "...................\n"; +} + +void vtkMuonEvent::InstallPipe() { + + vtkAppendPolyData *appender = m_Appender; + vtkSmartPointer mapper = + vtkSmartPointer::New(); + + if (content) { + vtkSmartPointer line_in = + vtkSmartPointer::New(); + vtkSmartPointer line_out = + vtkSmartPointer::New(); + + float distance = + (content->LineIn().origin - content->LineOut().origin).norm() / 10; + + HPoint3f pt; + pt = content->LineIn().origin; + line_in->SetPoint1(pt(0), pt(1), pt(2)); + pt = content->LineIn().origin + content->LineIn().direction * distance; + line_in->SetPoint2(pt(0), pt(1), pt(2)); + pt = content->LineOut().origin; + line_out->SetPoint1(pt(0), pt(1), pt(2)); + pt = content->LineOut().origin + content->LineOut().direction * distance; + line_out->SetPoint2(pt(0), pt(1), pt(2)); + + appender->AddInputConnection(line_in->GetOutputPort()); + appender->AddInputConnection(line_out->GetOutputPort()); + } + + appender->Update(); + mapper->SetInputConnection(appender->GetOutputPort()); + mapper->Update(); + + vtkSmartPointer actor = vtkSmartPointer::New(); + actor->SetMapper(mapper); + this->SetProp(actor); +} + +vtkPolyData *vtkMuonEvent::GetPolyData() const { + return m_Appender->GetOutput(); +} + +void vtkMuonEvent::AddPocaPoint(HPoint3f poca) { + m_Poca = poca; + vtkSmartPointer sphere = + vtkSmartPointer::New(); + float size = + (content->LineIn().origin - content->LineOut().origin).head(3).norm(); + size /= 100; + sphere->SetRadius(size); + sphere->SetCenter(poca(0), poca(1), poca(2)); + sphere->Update(); + m_Appender->AddInputConnection(sphere->GetOutputPort()); + m_Appender->Update(); +} + +HPoint3f vtkMuonEvent::GetPocaPoint() { return m_Poca; } + +} // namespace Vtk +} // namespace uLib diff --git a/src/Vtk/vtkMuonEvent.h b/src/Vtk/HEP/Detectors/vtkMuonEvent.h similarity index 66% rename from src/Vtk/vtkMuonEvent.h rename to src/Vtk/HEP/Detectors/vtkMuonEvent.h index 58a996c..9f6d329 100644 --- a/src/Vtk/vtkMuonEvent.h +++ b/src/Vtk/HEP/Detectors/vtkMuonEvent.h @@ -23,17 +23,15 @@ //////////////////////////////////////////////////////////////////////////////*/ - - #ifndef VTKMUONEVENT_H #define VTKMUONEVENT_H +#include +#include +#include +#include #include #include -#include -#include -#include -#include #include #include @@ -45,50 +43,45 @@ #include "Math/Dense.h" -#include "uLibVtkInterface.h" -#include "Detectors/MuonEvent.h" - - - +#include "HEP/Detectors/MuonEvent.h" +#include "Vtk/uLibVtkInterface.h" namespace uLib { +namespace Vtk { + +class vtkMuonEvent : public Puppet, public Polydata { + typedef MuonEventData Content; -class vtkMuonEvent : public Abstract::uLibVtkPolydata { - typedef MuonEventData Content; public: - vtkMuonEvent(const MuonEventData &content); - vtkMuonEvent(MuonEventData &content); - ~vtkMuonEvent(); + vtkMuonEvent(const MuonEventData &content); + vtkMuonEvent(MuonEventData &content); + ~vtkMuonEvent(); - Content& GetContent(); + Content &GetContent(); - void PrintSelf(std::ostream &o) const; + void PrintSelf(std::ostream &o) const; - virtual vtkProp *GetProp(); + virtual vtkPolyData *GetPolyData() const; - virtual vtkPolyData* GetPolyData() const; + void AddPocaPoint(HPoint3f poca); - void AddPocaPoint(HPoint3f poca); - - HPoint3f GetPocaPoint(); - - void vtkStartInteractive(); + HPoint3f GetPocaPoint(); + void vtkStartInteractive(); private: - void InstallPipe(); + void InstallPipe(); - vtkMuonEvent::Content *content; - vtkPolyData *m_PolyData; - vtkActor *m_Prop; + vtkMuonEvent::Content *content; + vtkPolyData *m_PolyData; - vtkAppendPolyData *m_Appender; - vtkBoxWidget *m_WidgetIN; - vtkBoxWidget *m_WidgetOUT; - HPoint3f m_Poca; + vtkAppendPolyData *m_Appender; + vtkBoxWidget *m_WidgetIN; + vtkBoxWidget *m_WidgetOUT; + HPoint3f m_Poca; }; +} // namespace Vtk +} // namespace uLib -} - -#endif // VTKMUONSCATTER_H +#endif // VTKMUONEVENT_H diff --git a/src/Vtk/HEP/Detectors/vtkMuonScatter.cxx b/src/Vtk/HEP/Detectors/vtkMuonScatter.cxx new file mode 100644 index 0000000..1d1c2bd --- /dev/null +++ b/src/Vtk/HEP/Detectors/vtkMuonScatter.cxx @@ -0,0 +1,144 @@ +/*////////////////////////////////////////////////////////////////////////////// +// CMT Cosmic Muon Tomography project ////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// + + Copyright (c) 2014, Universita' degli Studi di Padova, INFN sez. di Padova + All rights reserved + + Authors: Andrea Rigoni Garola < andrea.rigoni@pd.infn.it > + + ------------------------------------------------------------------ + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 3.0 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library. + +//////////////////////////////////////////////////////////////////////////////*/ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "Math/Dense.h" +#include "Vtk/HEP/Detectors/vtkMuonScatter.h" + +namespace uLib { +namespace Vtk { + +//////////////////////////////////////////////////////////////////////////////// +///// VTK MUON Scatter /////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// + +vtkMuonScatter::vtkMuonScatter(MuonScatter &content) + : m_Content(&content), m_LineIn(vtkLineSource::New()), + m_LineOut(vtkLineSource::New()), m_PolyData(vtkPolyData::New()), + m_SpherePoca(NULL) { + InstallPipe(); +} + +vtkMuonScatter::vtkMuonScatter(const MuonScatter &content) + : m_Content(const_cast(&content)), + m_LineIn(vtkLineSource::New()), m_LineOut(vtkLineSource::New()), + m_PolyData(vtkPolyData::New()), m_SpherePoca(NULL) { + InstallPipe(); +} + +vtkMuonScatter::~vtkMuonScatter() { + m_LineIn->Delete(); + m_LineOut->Delete(); + if (m_SpherePoca) + m_SpherePoca->Delete(); +} + +vtkMuonScatter::Content &vtkMuonScatter::GetContent() { return *m_Content; } + +void vtkMuonScatter::PrintSelf(std::ostream &o) const {} + +void vtkMuonScatter::InstallPipe() { + if (m_Content) { + vtkLineSource *line_in = m_LineIn; + vtkLineSource *line_out = m_LineOut; + + float distance = + (m_Content->LineIn().origin - m_Content->LineOut().origin).norm() / 10; + + HPoint3f pt; + pt = m_Content->LineIn().origin; + line_in->SetPoint1(pt(0), pt(1), pt(2)); + pt = m_Content->LineIn().origin + m_Content->LineIn().direction * distance; + line_in->SetPoint2(pt(0), pt(1), pt(2)); + pt = m_Content->LineOut().origin; + line_out->SetPoint1(pt(0), pt(1), pt(2)); + pt = + m_Content->LineOut().origin + m_Content->LineOut().direction * distance; + line_out->SetPoint2(pt(0), pt(1), pt(2)); + } + + vtkSmartPointer mapper = + vtkSmartPointer::New(); + mapper->SetInputConnection(m_LineIn->GetOutputPort()); + vtkSmartPointer actor = vtkSmartPointer::New(); + actor->SetMapper(mapper); + this->SetProp(actor); + + mapper = vtkSmartPointer::New(); + mapper->SetInputConnection(m_LineOut->GetOutputPort()); + actor = vtkSmartPointer::New(); + actor->SetMapper(mapper); + this->SetProp(actor); +} + +vtkPolyData *vtkMuonScatter::GetPolyData() const { + vtkSmartPointer append = + vtkSmartPointer::New(); + append->AddInputConnection(m_LineIn->GetOutputPort()); + append->AddInputConnection(m_LineOut->GetOutputPort()); + if (m_SpherePoca) + append->AddInputConnection(m_SpherePoca->GetOutputPort()); + append->Update(); + m_PolyData->DeepCopy(append->GetOutput()); + return m_PolyData; +} + +void vtkMuonScatter::AddPocaPoint(HPoint3f poca) { + vtkSphereSource *sphere = vtkSphereSource::New(); + float size = + (m_Content->LineIn().origin - m_Content->LineOut().origin).head(3).norm(); + size /= 100; + sphere->SetRadius(size); + sphere->SetCenter(poca(0), poca(1), poca(2)); + sphere->Update(); + m_SpherePoca = sphere; + + vtkSmartPointer mapper = + vtkSmartPointer::New(); + mapper->SetInputConnection(m_SpherePoca->GetOutputPort()); + vtkSmartPointer actor = vtkSmartPointer::New(); + actor->SetMapper(mapper); + this->SetProp(actor); +} + +HPoint3f vtkMuonScatter::GetPocaPoint() { + double center[3]; + if (m_SpherePoca) { + m_SpherePoca->GetCenter(center); + return HPoint3f(center[0], center[1], center[2]); + } else { + return HPoint3f(0, 0, 0); + } +} + +void vtkMuonScatter::ConnectInteractor(vtkRenderWindowInteractor *interactor) { + // TODO +} + +} // namespace Vtk +} // namespace uLib diff --git a/src/Vtk/vtkMuonScatter.h b/src/Vtk/HEP/Detectors/vtkMuonScatter.h similarity index 70% rename from src/Vtk/vtkMuonScatter.h rename to src/Vtk/HEP/Detectors/vtkMuonScatter.h index 90b0c39..10393a1 100644 --- a/src/Vtk/vtkMuonScatter.h +++ b/src/Vtk/HEP/Detectors/vtkMuonScatter.h @@ -23,18 +23,16 @@ //////////////////////////////////////////////////////////////////////////////*/ - - #ifndef VTKMUONSCATTER_H #define VTKMUONSCATTER_H -#include +#include +#include +#include +#include #include #include -#include -#include -#include -#include +#include #include #include @@ -46,8 +44,8 @@ #include "Math/Dense.h" -#include "uLibVtkInterface.h" -#include "Detectors/MuonScatter.h" +#include "HEP/Detectors/MuonScatter.h" +#include "Vtk/uLibVtkInterface.h" class vtkRenderWindowInteractor; @@ -55,40 +53,40 @@ namespace uLib { namespace Vtk { class vtkMuonScatter : public Puppet, public Polydata { - typedef MuonScatter Content; + typedef MuonScatter Content; + public: + vtkMuonScatter(const MuonScatter &content); + vtkMuonScatter(MuonScatter &content); - vtkMuonScatter(const MuonScatter &content); - vtkMuonScatter(MuonScatter &content); + ~vtkMuonScatter(); - ~vtkMuonScatter(); + Content &GetContent(); - Content &GetContent(); + void PrintSelf(std::ostream &o) const; - void PrintSelf(std::ostream &o) const; + virtual vtkPolyData *GetPolyData() const; - virtual vtkPolyData* GetPolyData() const; + void AddPocaPoint(HPoint3f poca); - void AddPocaPoint(HPoint3f poca); + HPoint3f GetPocaPoint(); - HPoint3f GetPocaPoint(); - - void vtkStartInteractive(); + void vtkStartInteractive(); protected: - void ConnectInteractor(vtkRenderWindowInteractor *interactor); + void ConnectInteractor(vtkRenderWindowInteractor *interactor); private: - void InstallPipe(); + void InstallPipe(); - vtkMuonScatter::Content *m_Content; - vtkLineSource *m_LineIn; - vtkLineSource *m_LineOut; - vtkSphereSource *m_SpherePoca; - vtkPolyData *m_PolyData; + vtkMuonScatter::Content *m_Content; + vtkLineSource *m_LineIn; + vtkLineSource *m_LineOut; + vtkSphereSource *m_SpherePoca; + vtkPolyData *m_PolyData; }; -} // vtk -} // uLib +} // namespace Vtk +} // namespace uLib #endif // VTKMUONSCATTER_H diff --git a/src/Vtk/HEP/MuonTomography/CMakeLists.txt b/src/Vtk/HEP/MuonTomography/CMakeLists.txt new file mode 100644 index 0000000..12adfb1 --- /dev/null +++ b/src/Vtk/HEP/MuonTomography/CMakeLists.txt @@ -0,0 +1,17 @@ + +################################################################################ +##### Vtk/HEP/MuonTomography - VTK wrappers for MuonTomography objects ######## +################################################################################ + +set(HEP_MUONTOMOGRAPHY_SOURCES + ${CMAKE_CURRENT_SOURCE_DIR}/vtkVoxRaytracerRepresentation.cpp + PARENT_SCOPE) + +set(HEP_MUONTOMOGRAPHY_HEADERS + ${CMAKE_CURRENT_SOURCE_DIR}/vtkVoxRaytracerRepresentation.h + PARENT_SCOPE) + +if(BUILD_TESTING) + include(uLibTargetMacros) + add_subdirectory(testing) +endif() diff --git a/src/Vtk/HEP/MuonTomography/testing/CMakeLists.txt b/src/Vtk/HEP/MuonTomography/testing/CMakeLists.txt new file mode 100644 index 0000000..ba81a0c --- /dev/null +++ b/src/Vtk/HEP/MuonTomography/testing/CMakeLists.txt @@ -0,0 +1,15 @@ +# TESTS +set(TESTS + vtkVoxRaytracerTest +) + +set(LIBRARIES + ${PACKAGE_LIBPREFIX}Core + ${PACKAGE_LIBPREFIX}Math + ${PACKAGE_LIBPREFIX}Detectors + ${PACKAGE_LIBPREFIX}Vtk + ${VTK_LIBRARIES} + Boost::unit_test_framework +) + +uLib_add_tests(VtkMuonTomography) diff --git a/src/Vtk/testing/vtkVoxRaytracerTest.cpp b/src/Vtk/HEP/MuonTomography/testing/vtkVoxRaytracerTest.cpp similarity index 52% rename from src/Vtk/testing/vtkVoxRaytracerTest.cpp rename to src/Vtk/HEP/MuonTomography/testing/vtkVoxRaytracerTest.cpp index aa1d264..04bc919 100644 --- a/src/Vtk/testing/vtkVoxRaytracerTest.cpp +++ b/src/Vtk/HEP/MuonTomography/testing/vtkVoxRaytracerTest.cpp @@ -23,69 +23,55 @@ //////////////////////////////////////////////////////////////////////////////*/ - - - #include -#include #include +#include - -#include -#include - -#include "Vtk/vtkMuonScatter.h" -#include "Vtk/vtkStructuredGrid.h" -#include "Vtk/vtkVoxRaytracerRepresentation.h" -#include "Vtk/uLibVtkViewer.h" - -// remove #include -#include "testing-prototype.h" +#include +#include + +#include "Vtk/HEP/Detectors/vtkMuonScatter.h" +#include "Vtk/HEP/MuonTomography/vtkVoxRaytracerRepresentation.h" +#include "Vtk/Math/vtkStructuredGrid.h" +#include "Vtk/uLibVtkViewer.h" + +#define BOOST_TEST_MODULE VtkVoxRaytracerTest +#include using namespace uLib; - - -class vtkWidgetCallback : public vtkCommand -{ +class vtkWidgetCallback : public vtkCommand { public: static vtkWidgetCallback *New() { return new vtkWidgetCallback; } - void SetTracer(Vtk::vtkVoxRaytracerRepresentation *parent) - { - this->vtk_raytr = parent; + void SetTracer(Vtk::vtkVoxRaytracerRepresentation *parent) { + this->vtk_raytr = parent; } - void SetMuon (uLib::MuonScatter *muon) { this->muon = muon; } + void SetMuon(uLib::MuonScatter *muon) { this->muon = muon; } - void SetAnnotation ( vtkCornerAnnotation *annotation) { - this->annotation = annotation; + void SetAnnotation(vtkCornerAnnotation *annotation) { + this->annotation = annotation; } - virtual void Execute(vtkObject *caller, unsigned long, void*) - { - char str[40]; - vtk_raytr->SetMuon(*muon); - if(annotation) - { - sprintf(str,"total length = %f",vtk_raytr->GetRay().TotalLength()); - annotation->SetText(1,str); - for(int i=0; iGetRay().Data().size(); ++i) - { - std::cout << "L[" << i << "] = " - << vtk_raytr->GetRay().Data().at(i).L << "\n"; - } - std::cout << "\n"; + virtual void Execute(vtkObject *caller, unsigned long, void *) { + char str[40]; + vtk_raytr->SetMuon(*muon); + if (annotation) { + sprintf(str, "total length = %f", vtk_raytr->GetRay().TotalLength()); + annotation->SetText(1, str); + for (int i = 0; i < vtk_raytr->GetRay().Data().size(); ++i) { + std::cout << "L[" << i << "] = " << vtk_raytr->GetRay().Data().at(i).L + << "\n"; } + std::cout << "\n"; + } } private: - vtkWidgetCallback() : - vtk_raytr(NULL), - muon(NULL), - annotation(NULL) {} + vtkWidgetCallback() : vtk_raytr(NULL), muon(NULL), annotation(NULL) {} uLib::VoxRaytracer *raytracer; Vtk::vtkVoxRaytracerRepresentation *vtk_raytr; @@ -93,50 +79,40 @@ private: vtkCornerAnnotation *annotation; }; +BOOST_AUTO_TEST_CASE(vtkVoxRaytracerRepresentationTest) { + // muon scatter // + MuonScatter muon; + muon.LineIn().origin << -6, 12, -6, 1; + muon.LineIn().direction << 1, -1, 1, 0; + muon.LineOut().origin << 6, -4, 6, 1; + muon.LineOut().direction << 1, -1, 1, 0; + Vtk::vtkMuonScatter v_muon(muon); + // structured grid // + StructuredGrid grid(Vector3i(12, 10, 12)); + grid.SetSpacing(Vector3f(1, 1, 1)); + grid.SetPosition(Vector3f(0, 0, 0)); -int main() -{ - BEGIN_TESTING(vtk VoxRaytracer); + Vtk::vtkStructuredGrid v_grid(grid); + // voxraytracer // + VoxRaytracer rt(grid); + HPoint3f pt; + rt.GetEntryPoint(muon.LineIn(), pt); + std::cout << pt.transpose() << "\n"; - // muon scatter // - MuonScatter muon; - muon.LineIn().origin << -6, 12, -6, 1; - muon.LineIn().direction << 1 , -1 , 1 , 0; - muon.LineOut().origin << 6 , -4 , 6 , 1; - muon.LineOut().direction << 1 , -1 , 1 , 0; + Vtk::vtkVoxRaytracerRepresentation v_rt(rt); + v_rt.SetMuon(muon); + v_rt.SetRayColor(Vector4f(1, 0, 0, 1)); - Vtk::vtkMuonScatter v_muon(muon); - - - // structured grid // - StructuredGrid grid(Vector3i(12,10,12)); - grid.SetSpacing(Vector3f(1,1,1)); - grid.SetPosition(Vector3f(0,0,0)); - - - Vtk::vtkStructuredGrid v_grid(grid); - - - // voxraytracer // - VoxRaytracer rt(grid); - HPoint3f pt; - rt.GetEntryPoint(muon.LineIn(),pt); - std::cout << pt.transpose() << "\n"; - - Vtk::vtkVoxRaytracerRepresentation v_rt(rt); - v_rt.SetMuon(muon); - v_rt.SetRayColor(Vector4f(1,0,0,1)); - - // // renderer // + if (std::getenv("CTEST_PROJECT_NAME") == nullptr) { + // renderer // Vtk::Viewer viewer; // widget // vtkBoxWidget *widget = v_grid.GetWidget(); - vtkWidgetCallback *cbk = vtkWidgetCallback::New(); cbk->SetTracer(&v_rt); cbk->SetMuon(&muon); @@ -150,7 +126,7 @@ int main() viewer.AddPuppet(v_rt); viewer.AddPuppet(v_muon); viewer.Start(); + } - - END_TESTING; + BOOST_CHECK(v_rt.GetRay().Count() > 0); } diff --git a/src/Vtk/vtkVoxRaytracerRepresentation.cpp b/src/Vtk/HEP/MuonTomography/vtkVoxRaytracerRepresentation.cpp similarity index 93% rename from src/Vtk/vtkVoxRaytracerRepresentation.cpp rename to src/Vtk/HEP/MuonTomography/vtkVoxRaytracerRepresentation.cpp index d4ffc8d..7e5293b 100644 --- a/src/Vtk/vtkVoxRaytracerRepresentation.cpp +++ b/src/Vtk/HEP/MuonTomography/vtkVoxRaytracerRepresentation.cpp @@ -27,12 +27,12 @@ #include "config.h" #endif -#include "vtkVoxRaytracerRepresentation.h" +#include "Vtk/HEP/MuonTomography/vtkVoxRaytracerRepresentation.h" #include "Math/VoxRaytracer.h" -// #include "vtkMuonEvent.h" -#include "vtkMuonScatter.h" +// #include "Vtk/HEP/Detectors/vtkMuonEvent.h" +#include "Vtk/HEP/Detectors/vtkMuonScatter.h" namespace uLib { namespace Vtk { @@ -114,12 +114,10 @@ void vtkVoxRaytracerRepresentation::SetMuon(MuonScatter &muon) { m_Line3->SetPoint1(pt1(0), pt1(1), pt1(2)); m_Line3->SetPoint2(pt2(0), pt2(1), pt2(2)); - // Create a vtkPoints object and store the points in it vtkSmartPointer points = vtkSmartPointer::New(); points->InsertNextPoint(pt1(0), pt1(1), pt1(2)); points->InsertNextPoint(pt2(0), pt2(1), pt2(2)); - // Create a cell array to store the lines in and add the lines to it vtkSmartPointer lines = vtkSmartPointer::New(); vtkSmartPointer line = vtkSmartPointer::New(); @@ -127,19 +125,16 @@ void vtkVoxRaytracerRepresentation::SetMuon(MuonScatter &muon) { line->GetPointIds()->SetId(1, 1); lines->InsertNextCell(line); - // Create a polydata to store everything in vtkSmartPointer linesPolyData = vtkSmartPointer::New(); - - // Add the points to the dataset linesPolyData->SetPoints(points); - - // Add the lines to the dataset linesPolyData->SetLines(lines); m_RayLine->RemoveAllInputs(); #if VTK_MAJOR_VERSION <= 5 m_RayLine->AddInputConnection(linesPolyData->GetProducerPort()); +#else + m_RayLine->AddInputData(linesPolyData); #endif m_RayLine->AddInputConnection(m_Line1->GetOutputPort()); m_RayLine->AddInputConnection(m_Sphere1->GetOutputPort()); @@ -177,15 +172,12 @@ void vtkVoxRaytracerRepresentation::SetMuon(MuonScatter &muon, HPoint3f poca) { m_Line3->SetPoint1(pt1(0), pt1(1), pt1(2)); m_Line3->SetPoint2(pt2(0), pt2(1), pt2(2)); - // Create a vtkPoints object and store the points in it vtkSmartPointer points = vtkSmartPointer::New(); points->InsertNextPoint(pt1(0), pt1(1), pt1(2)); points->InsertNextPoint(poca(0), poca(1), poca(2)); points->InsertNextPoint(pt2(0), pt2(1), pt2(2)); - // Create a cell array to store the lines in and add the lines to it vtkSmartPointer lines = vtkSmartPointer::New(); - for (unsigned int i = 0; i < 2; i++) { vtkSmartPointer line = vtkSmartPointer::New(); line->GetPointIds()->SetId(0, i); @@ -193,19 +185,16 @@ void vtkVoxRaytracerRepresentation::SetMuon(MuonScatter &muon, HPoint3f poca) { lines->InsertNextCell(line); } - // Create a polydata to store everything in vtkSmartPointer linesPolyData = vtkSmartPointer::New(); - - // Add the points to the dataset linesPolyData->SetPoints(points); - - // Add the lines to the dataset linesPolyData->SetLines(lines); m_RayLine->RemoveAllInputs(); #if VTK_MAJOR_VERSION <= 5 m_RayLine->AddInputConnection(linesPolyData->GetProducerPort()); +#else + m_RayLine->AddInputData(linesPolyData); #endif m_RayLine->AddInputConnection(m_Line1->GetOutputPort()); m_RayLine->AddInputConnection(m_Sphere1->GetOutputPort()); @@ -260,11 +249,12 @@ void vtkVoxRaytracerRepresentation::SetRay(VoxRaytracer::RayData *ray) { Vector3i idv = m_Content->GetImage()->UnMap(id); vtkSmartPointer cube = vtkSmartPointer::New(); cube->SetBounds(idv(0), idv(0) + 1, idv(1), idv(1) + 1, idv(2), idv(2) + 1); - cube->Update(); #if VTK_MAJOR_VERSION <= 5 + cube->Update(); appender->AddInput(cube->GetOutput()); +#else + appender->AddInputData(cube->GetOutput()); #endif - appender->Update(); } } @@ -294,21 +284,18 @@ void vtkVoxRaytracerRepresentation::InstallPipe() { append->AddInputConnection(m_Line1->GetOutputPort()); append->AddInputConnection(m_Line2->GetOutputPort()); - append->Update(); vtkSmartPointer mapper = vtkSmartPointer::New(); mapper->SetInputConnection(append->GetOutputPort()); - mapper->Update(); - vtkSmartPointer actor = vtkSmartPointer::New(); + vtkSmartPointer actor = vtkActor::New(); actor->SetMapper(mapper); actor->GetProperty()->SetColor(0.6, 0.6, 1); this->SetProp(actor); mapper = vtkSmartPointer::New(); mapper->SetInputConnection(m_RayLine->GetOutputPort()); - mapper->Update(); m_RayLineActor->SetMapper(mapper); m_RayLineActor->GetProperty()->SetColor(1, 0, 0); @@ -322,7 +309,6 @@ void vtkVoxRaytracerRepresentation::InstallPipe() { mapper = vtkSmartPointer::New(); mapper->SetInputConnection(polyfilter->GetOutputPort()); - mapper->Update(); vtkActor *vra = m_RayRepresentationActor; vra->SetMapper(mapper); diff --git a/src/Vtk/vtkVoxRaytracerRepresentation.h b/src/Vtk/HEP/MuonTomography/vtkVoxRaytracerRepresentation.h similarity index 53% rename from src/Vtk/vtkVoxRaytracerRepresentation.h rename to src/Vtk/HEP/MuonTomography/vtkVoxRaytracerRepresentation.h index d36246a..5d0024a 100644 --- a/src/Vtk/vtkVoxRaytracerRepresentation.h +++ b/src/Vtk/HEP/MuonTomography/vtkVoxRaytracerRepresentation.h @@ -23,110 +23,101 @@ //////////////////////////////////////////////////////////////////////////////*/ - - #ifndef U_VTKVOXRAYTRACERREPRESENTATION_H #define U_VTKVOXRAYTRACERREPRESENTATION_H -#include #include +#include -#include -#include +#include +#include +#include #include #include -#include +#include +#include #include -#include -#include -#include #include #include -#include +#include +#include #include #include #include "Math/Dense.h" -#include "uLibVtkInterface.h" +#include "Vtk/uLibVtkInterface.h" -#include "Detectors/MuonScatter.h" +#include "HEP/Detectors/MuonScatter.h" #include "Math/StructuredGrid.h" #include "Math/VoxRaytracer.h" - class vtkActor; namespace uLib { namespace Vtk { class vtkVoxRaytracerRepresentation : public Puppet { - typedef VoxRaytracer Content; + typedef VoxRaytracer Content; + public: - vtkVoxRaytracerRepresentation(Content &content); - ~vtkVoxRaytracerRepresentation(); + vtkVoxRaytracerRepresentation(Content &content); + ~vtkVoxRaytracerRepresentation(); - uLib::VoxRaytracer *GetRaytracerAlgorithm(); + uLib::VoxRaytracer *GetRaytracerAlgorithm(); - vtkProp *GetProp(); + vtkProp *GetProp(); - vtkPolyData *GetPolyData() const; + vtkPolyData *GetPolyData() const; - enum RepresentationElements { - RayElements, - VoxelsElements - }; - void SetRepresentationElements(enum RepresentationElements el); + enum RepresentationElements { RayElements, VoxelsElements }; + void SetRepresentationElements(enum RepresentationElements el); - void SetMuon(MuonScatter &muon); + void SetMuon(MuonScatter &muon); - void SetMuon(MuonScatter &muon, HPoint3f poca); + void SetMuon(MuonScatter &muon, HPoint3f poca); - void SetMuon(class vtkMuonEvent &muon); + void SetMuon(class vtkMuonEvent &muon); - void SetMuon(class vtkMuonScatter &muon); + void SetMuon(class vtkMuonScatter &muon); - VoxRaytracer::RayData GetRay(); + VoxRaytracer::RayData GetRay(); - void SetRay(HPoint3f in, HPoint3f out); + void SetRay(HPoint3f in, HPoint3f out); - void SetRay(HPoint3f in, HPoint3f mid, HPoint3f out); - - void SetRay(VoxRaytracer::RayData *ray); - - void SetVoxelsColor(Vector4f rgba); - void SetRayColor(Vector4f rgba); + void SetRay(HPoint3f in, HPoint3f mid, HPoint3f out); + void SetRay(VoxRaytracer::RayData *ray); + void SetVoxelsColor(Vector4f rgba); + void SetRayColor(Vector4f rgba); private: - void InstallPipe(); + void InstallPipe(); - void SetColor(vtkActor *actor, Vector4f rgba); + void SetColor(vtkActor *actor, Vector4f rgba); - VoxRaytracer *m_Content; + VoxRaytracer *m_Content; - Scalarf default_radius; - vtkAssembly *m_Assembly; - vtkAppendPolyData *m_RayLine; - vtkActor *m_RayLineActor; - vtkActor *m_RayRepresentationActor; - vtkSmartPointer m_Transform; - VoxRaytracer::RayData m_Ray; - vtkSmartPointer m_Sphere1; - vtkSmartPointer m_Sphere2; - vtkSmartPointer m_Line1; - vtkSmartPointer m_Line2; - vtkSmartPointer m_Line3; - vtkSmartPointer m_RayRepresentation; + Scalarf default_radius; + vtkAssembly *m_Assembly; + vtkAppendPolyData *m_RayLine; + vtkActor *m_RayLineActor; + vtkActor *m_RayRepresentationActor; + vtkSmartPointer m_Transform; + VoxRaytracer::RayData m_Ray; + vtkSmartPointer m_Sphere1; + vtkSmartPointer m_Sphere2; + vtkSmartPointer m_Line1; + vtkSmartPointer m_Line2; + vtkSmartPointer m_Line3; + vtkSmartPointer m_RayRepresentation; - vtkAppendPolyData *m_SelectedElement; + vtkAppendPolyData *m_SelectedElement; }; - -} // vtk -} // uLib - +} // namespace Vtk +} // namespace uLib #endif // VTKVOXRAYTRACERREPRESENTATION_H diff --git a/src/Vtk/Math/CMakeLists.txt b/src/Vtk/Math/CMakeLists.txt new file mode 100644 index 0000000..810a8a8 --- /dev/null +++ b/src/Vtk/Math/CMakeLists.txt @@ -0,0 +1,22 @@ + +################################################################################ +##### Vtk/Math - VTK wrappers for Math module objects ######################### +################################################################################ + +set(MATH_SOURCES + ${CMAKE_CURRENT_SOURCE_DIR}/vtkStructuredGrid.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/vtkTriangleMesh.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/vtkVoxImage.cpp + PARENT_SCOPE) + +set(MATH_HEADERS + ${CMAKE_CURRENT_SOURCE_DIR}/vtkHLineRepresentation.h + ${CMAKE_CURRENT_SOURCE_DIR}/vtkStructuredGrid.h + ${CMAKE_CURRENT_SOURCE_DIR}/vtkTriangleMesh.h + ${CMAKE_CURRENT_SOURCE_DIR}/vtkVoxImage.h + PARENT_SCOPE) + +if(BUILD_TESTING) + include(uLibTargetMacros) + add_subdirectory(testing) +endif() diff --git a/src/Vtk/Math/testing/CMakeLists.txt b/src/Vtk/Math/testing/CMakeLists.txt new file mode 100644 index 0000000..f77d692 --- /dev/null +++ b/src/Vtk/Math/testing/CMakeLists.txt @@ -0,0 +1,16 @@ +# TESTS +set(TESTS + vtkStructuredGridTest + vtkTriangleMeshTest + vtkVoxImageTest +) + +set(LIBRARIES + ${PACKAGE_LIBPREFIX}Core + ${PACKAGE_LIBPREFIX}Math + ${PACKAGE_LIBPREFIX}Vtk + ${VTK_LIBRARIES} + Boost::unit_test_framework +) + +uLib_add_tests(VtkMath) diff --git a/src/Vtk/testing/vtkStructuredGridTest.cpp b/src/Vtk/Math/testing/vtkStructuredGridTest.cpp similarity index 77% rename from src/Vtk/testing/vtkStructuredGridTest.cpp rename to src/Vtk/Math/testing/vtkStructuredGridTest.cpp index ffbc89b..7062cea 100644 --- a/src/Vtk/testing/vtkStructuredGridTest.cpp +++ b/src/Vtk/Math/testing/vtkStructuredGridTest.cpp @@ -23,29 +23,26 @@ //////////////////////////////////////////////////////////////////////////////*/ - - - +#include "Vtk/Math/vtkStructuredGrid.h" #include "Math/StructuredGrid.h" - -#include "Vtk/vtkStructuredGrid.h" #include "Vtk/uLibVtkViewer.h" -#include "testing-prototype.h" +#define BOOST_TEST_MODULE VtkStructuredGridTest +#include using namespace uLib; -int main() -{ +BOOST_AUTO_TEST_CASE(vtkStructuredGridTest) { + StructuredGrid grid(Vector3i(10, 10, 100)); + grid.SetSpacing(Vector3f(3, 1, 1)); - StructuredGrid grid(Vector3i(10,10,100)); - grid.SetSpacing(Vector3f(3,1,1)); - - Vtk::vtkStructuredGrid grid_viewer(grid); + Vtk::vtkStructuredGrid grid_viewer(grid); + if (std::getenv("CTEST_PROJECT_NAME") == nullptr) { Vtk::Viewer viewer; viewer.AddPuppet(grid_viewer); viewer.Start(); + } - return 0; + BOOST_CHECK(grid_viewer.GetWidget() != nullptr); } diff --git a/src/Vtk/testing/vtkMuonScatter.cpp b/src/Vtk/Math/testing/vtkTriangleMeshTest.cpp similarity index 65% rename from src/Vtk/testing/vtkMuonScatter.cpp rename to src/Vtk/Math/testing/vtkTriangleMeshTest.cpp index 2c9e7ca..b0caac8 100644 --- a/src/Vtk/testing/vtkMuonScatter.cpp +++ b/src/Vtk/Math/testing/vtkTriangleMeshTest.cpp @@ -23,40 +23,31 @@ //////////////////////////////////////////////////////////////////////////////*/ +#include "Vtk/Math/vtkTriangleMesh.h" +#include "Math/TriangleMesh.h" - -#include "Detectors/MuonScatter.h" -#include "Vtk/vtkMuonScatter.h" - -#include "Vtk/uLibVtkViewer.h" - -#include "testing-prototype.h" +#define BOOST_TEST_MODULE VtkTriangleMeshTest +#include using namespace uLib; -int main() -{ - MuonScatter event; - event.LineIn().direction << 0, -1, 1, 0; - event.LineIn().origin << 0, 1, -1, 1; +BOOST_AUTO_TEST_CASE(vtkTriangleMeshConstruction) { + TriangleMesh mesh; - event.LineOut().direction << 0, -1, 0, 0; - event.LineOut().origin << 0, -1, 0, 1; + mesh.AddPoint(Vector3f(0, 0, 0)); + mesh.AddPoint(Vector3f(0, 1, 0)); + mesh.AddPoint(Vector3f(1, 0, 0)); + mesh.AddTriangle(Vector3i(0, 1, 2)); - Vtk::vtkMuonScatter v_event(event); - v_event.AddPocaPoint(HPoint3f(0,0,0)); - - v_event.SaveToXMLFile("vtk_testing_muonevent.vtp"); + Vtk::vtkTriangleMesh v_mesh(mesh); + v_mesh.Update(); + if (std::getenv("CTEST_PROJECT_NAME") == nullptr) { Vtk::Viewer viewer; - -// Vtk::Tie tms; -// tms.DoAction(); -// Vtk::Tie vms; -// vms.DoAction(); - - viewer.AddPuppet(v_event); + viewer.AddPuppet(v_mesh); viewer.Start(); + } - return 0; + BOOST_CHECK_EQUAL(mesh.Points().size(), 3u); + BOOST_CHECK_EQUAL(mesh.Triangles().size(), 1u); } diff --git a/src/Vtk/testing/vtkTriangleMeshTest.cpp b/src/Vtk/Math/testing/vtkVoxImageTest.cpp similarity index 61% rename from src/Vtk/testing/vtkTriangleMeshTest.cpp rename to src/Vtk/Math/testing/vtkVoxImageTest.cpp index 5d12e6d..08c253c 100644 --- a/src/Vtk/testing/vtkTriangleMeshTest.cpp +++ b/src/Vtk/Math/testing/vtkVoxImageTest.cpp @@ -23,53 +23,36 @@ //////////////////////////////////////////////////////////////////////////////*/ +#include "Vtk/Math/vtkVoxImage.h" +#include "Math/VoxImage.h" - - -#include "Math/TriangleMesh.h" -#include "Vtk/vtkTriangleMesh.h" -#include "testing-prototype.h" +#define BOOST_TEST_MODULE VtkVoxImageTest +#include using namespace uLib; +struct TestVoxel { + Scalarf Value; + unsigned int Count; +}; -int main() -{ - BEGIN_TESTING(Vtk Triangle Mesh); +BOOST_AUTO_TEST_CASE(vtkVoxImageConstruction) { + TestVoxel zero = {0, 0}; + TestVoxel nonzero = {5.5f * 1e-6f, 100}; -// { // SIMPLE TESTS // + VoxImage img(Vector3i(10, 10, 10)); + img.SetSpacing(Vector3f(3, 3, 3)); + img.InitVoxels(zero); + img[Vector3i(3, 3, 3)] = nonzero; + Vtk::vtkVoxImage vtk_img(img); + vtk_img.SaveToXMLFile("test_vtkvoximage.vti"); -// TriangleMesh mesh; + if (std::getenv("CTEST_PROJECT_NAME") == nullptr) { + Vtk::Viewer viewer; + viewer.AddPuppet(vtk_img); + viewer.Start(); + } -// mesh.AddPoint(Vector3f(0,0,0)); -// mesh.AddPoint(Vector3f(0,1,0)); -// mesh.AddPoint(Vector3f(1,0,0)); - -// mesh.AddTriangle(Vector3i(0,1,2)); - -// mesh.PrintSelf(std::cout); - - -// vtkTriangleMesh v_mesh(mesh); -// v_mesh.Update(); - -// TestingRenderWidow(&v_mesh); -// } - - { // SIMPLE TESTS // - - TriangleMesh mesh; - - vtkTriangleMesh v_mesh(mesh); - - v_mesh.ReadFromStlFile("prova.stl"); - - mesh.PrintSelf(std::cout); - - TestingRenderWidow(&v_mesh); - } - - - END_TESTING; + BOOST_CHECK(vtk_img.GetImageData() != nullptr); } diff --git a/src/Vtk/vtkHLineRepresentation.h b/src/Vtk/Math/vtkHLineRepresentation.h similarity index 96% rename from src/Vtk/vtkHLineRepresentation.h rename to src/Vtk/Math/vtkHLineRepresentation.h index c69ea14..59fc7f7 100644 --- a/src/Vtk/vtkHLineRepresentation.h +++ b/src/Vtk/Math/vtkHLineRepresentation.h @@ -23,30 +23,20 @@ //////////////////////////////////////////////////////////////////////////////*/ - - #ifndef VTKHLINEREPRESENTATION_H #define VTKHLINEREPRESENTATION_H - - class vtkProp; class vtkPolyData; - namespace uLib { class vtkHLineRepresentationDefault { public: - vtkHLineRepresentationDefault(); - - + vtkHLineRepresentationDefault(); }; - -} - - +} // namespace uLib #endif // VTKHLINEREPRESENTATION_H diff --git a/src/Vtk/Math/vtkStructuredGrid.cpp b/src/Vtk/Math/vtkStructuredGrid.cpp new file mode 100644 index 0000000..3cf9898 --- /dev/null +++ b/src/Vtk/Math/vtkStructuredGrid.cpp @@ -0,0 +1,114 @@ +/*////////////////////////////////////////////////////////////////////////////// +// CMT Cosmic Muon Tomography project ////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// + + Copyright (c) 2014, Universita' degli Studi di Padova, INFN sez. di Padova + All rights reserved + + Authors: Andrea Rigoni Garola < andrea.rigoni@pd.infn.it > + + ------------------------------------------------------------------ + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 3.0 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library. + +//////////////////////////////////////////////////////////////////////////////*/ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "Math/StructuredGrid.h" +#include "Vtk/Math/vtkStructuredGrid.h" + +namespace uLib { +namespace Vtk { + +//////////////////////////////////////////////////////////////////////////////// +////// VTK STRUCTURED GRID ///////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// + +vtkStructuredGrid::vtkStructuredGrid(Content &content) + : m_Content(&content), m_Actor(vtkActor::New()), + m_Widget(vtkBoxWidget::New()), m_Transform(vtkTransform::New()) { + vtkSmartPointer callback = + vtkSmartPointer::New(); + callback->SetGrid(this); + m_Widget->AddObserver(vtkCommand::InteractionEvent, callback); + + this->InstallPipe(); +} + +vtkStructuredGrid::~vtkStructuredGrid() { + m_Actor->Delete(); + m_Widget->Delete(); + m_Transform->Delete(); +} + +void vtkStructuredGrid::SetTransform(vtkTransform *t) { + vtkMatrix4x4 *vmat = t->GetMatrix(); + Matrix4f mat; + for (int i = 0; i < 4; ++i) + for (int j = 0; j < 4; ++j) + mat(i, j) = vmat->GetElement(i, j); + m_Content->SetMatrix(mat); + + vtkSmartPointer vmat2 = vtkSmartPointer::New(); + mat = m_Content->GetWorldMatrix(); + for (int i = 0; i < 4; ++i) + for (int j = 0; j < 4; ++j) + vmat2->SetElement(i, j, mat(i, j)); + m_Transform->SetMatrix(vmat2); + m_Transform->Update(); + this->Update(); +} + +vtkBoxWidget *vtkStructuredGrid::GetWidget() { return m_Widget; } + +void vtkStructuredGrid::Update() { m_Actor->GetMapper()->Update(); } + +void vtkStructuredGrid::InstallPipe() { + vtkSmartPointer cube = vtkSmartPointer::New(); + vtkSmartPointer filter = + vtkSmartPointer::New(); + + vtkSmartPointer vmat = vtkSmartPointer::New(); + Matrix4f mat = m_Content->GetWorldMatrix(); + for (int i = 0; i < 4; ++i) + for (int j = 0; j < 4; ++j) + vmat->SetElement(i, j, mat(i, j)); + m_Transform->SetMatrix(vmat); + filter->SetTransform(m_Transform); + filter->SetInputConnection(cube->GetOutputPort()); + + Vector3i dims = m_Content->GetDims(); + cube->SetBounds(0, dims(0), 0, dims(1), 0, dims(2)); + cube->Update(); + filter->Update(); + + vtkSmartPointer mapper = + vtkSmartPointer::New(); + mapper->SetInputConnection(filter->GetOutputPort()); + + m_Actor->SetMapper(mapper); + m_Actor->GetProperty()->SetRepresentationToSurface(); + m_Actor->GetProperty()->SetEdgeVisibility(true); + m_Actor->GetProperty()->SetOpacity(0.4); + m_Actor->GetProperty()->SetAmbient(0.7); + this->Update(); + m_Widget->SetProp3D(m_Actor); + + this->SetProp(m_Actor); +} + +} // namespace Vtk +} // namespace uLib diff --git a/src/Vtk/vtkStructuredGrid.h b/src/Vtk/Math/vtkStructuredGrid.h similarity index 63% rename from src/Vtk/vtkStructuredGrid.h rename to src/Vtk/Math/vtkStructuredGrid.h index faa936a..0921a09 100644 --- a/src/Vtk/vtkStructuredGrid.h +++ b/src/Vtk/Math/vtkStructuredGrid.h @@ -23,77 +23,70 @@ //////////////////////////////////////////////////////////////////////////////*/ - - #ifndef U_VTKSTRUCTUREDGRID_H #define U_VTKSTRUCTUREDGRID_H -#include +#include #include #include -#include +#include #include -#include #include +#include -#include -#include #include #include +#include +#include #include "Math/Dense.h" -#include "uLibVtkInterface.h" #include "Math/StructuredGrid.h" - +#include "Vtk/uLibVtkInterface.h" namespace uLib { namespace Vtk { class vtkStructuredGrid : public Puppet { - typedef StructuredGrid Content; + typedef StructuredGrid Content; + public: + vtkStructuredGrid(Content &content); + ~vtkStructuredGrid(); - vtkStructuredGrid(Content &content); - ~vtkStructuredGrid(); + void SetTransform(class vtkTransform *t); - void SetTransform(class vtkTransform *t); - - class vtkBoxWidget * GetWidget(); - - void Update(); + class vtkBoxWidget *GetWidget(); + void Update(); private: - void InstallPipe(); + void InstallPipe(); - class vtkWidgetCallback : public vtkCommand - { - public: - static vtkWidgetCallback *New() { return new vtkWidgetCallback; } + class vtkWidgetCallback : public vtkCommand { + public: + static vtkWidgetCallback *New() { return new vtkWidgetCallback; } - void SetGrid(uLib::Vtk::vtkStructuredGrid *grid) { this->grid = grid; } + void SetGrid(uLib::Vtk::vtkStructuredGrid *grid) { this->grid = grid; } - virtual void Execute(vtkObject *caller, unsigned long, void*) - { - vtkSmartPointer t = vtkSmartPointer::New(); - vtkBoxWidget *widget = reinterpret_cast(caller); - widget->GetTransform(t); - grid->SetTransform(t); - } - private: - uLib::Vtk::vtkStructuredGrid *grid; - }; + virtual void Execute(vtkObject *caller, unsigned long, void *) { + vtkSmartPointer t = vtkSmartPointer::New(); + vtkBoxWidget *widget = reinterpret_cast(caller); + widget->GetTransform(t); + grid->SetTransform(t); + } - vtkActor *m_Actor; - vtkBoxWidget *m_Widget; - StructuredGrid *m_Content; - vtkTransform *m_Transform; + private: + uLib::Vtk::vtkStructuredGrid *grid; + }; + + vtkActor *m_Actor; + vtkBoxWidget *m_Widget; + StructuredGrid *m_Content; + vtkTransform *m_Transform; }; - -} // vtk -} // uLib - +} // namespace Vtk +} // namespace uLib #endif // VTKSTRUCTUREDGRID_H diff --git a/src/Vtk/Math/vtkTriangleMesh.cpp b/src/Vtk/Math/vtkTriangleMesh.cpp new file mode 100644 index 0000000..8893308 --- /dev/null +++ b/src/Vtk/Math/vtkTriangleMesh.cpp @@ -0,0 +1,166 @@ +/*////////////////////////////////////////////////////////////////////////////// +// CMT Cosmic Muon Tomography project ////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// + + Copyright (c) 2014, Universita' degli Studi di Padova, INFN sez. di Padova + All rights reserved + + Authors: Andrea Rigoni Garola < andrea.rigoni@pd.infn.it > + + ------------------------------------------------------------------ + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 3.0 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library. + +//////////////////////////////////////////////////////////////////////////////*/ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include + +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include "Vtk/Math/vtkTriangleMesh.h" +#include + +namespace uLib { +namespace Vtk { + +void vtkTriangleMesh::vtk2uLib_update() { + vtkIdType number_of_points = m_Poly->GetNumberOfPoints(); + vtkIdType number_of_triangles = m_Poly->GetNumberOfPolys(); + + std::cout << "//////\n" + << "number of points = " << number_of_points << "\n" + << "number of polys = " << number_of_triangles << "\n" + << "//////\n"; + + m_content.Points().resize(number_of_points); + for (int i = 0; i < number_of_points; ++i) { + double *point = m_Poly->GetPoint(i); + m_content.Points()[i](0) = point[0]; + m_content.Points()[i](1) = point[1]; + m_content.Points()[i](2) = point[2]; + } + + m_content.Triangles().resize(number_of_triangles); + m_Poly->GetPolys()->InitTraversal(); + vtkSmartPointer idList = vtkSmartPointer::New(); + for (int i = 0; i < number_of_triangles; ++i) { + m_Poly->GetPolys()->GetNextCell(idList); + m_content.Triangles()[i](0) = idList->GetId(0); + m_content.Triangles()[i](1) = idList->GetId(1); + m_content.Triangles()[i](2) = idList->GetId(2); + } + m_Poly->Modified(); + m_Actor->GetMapper()->Update(); +} + +void vtkTriangleMesh::uLib2vtk_update() { + vtkIdType number_of_points = m_content.Points().size(); + vtkIdType number_of_triangles = m_content.Triangles().size(); + + vtkSmartPointer points = vtkSmartPointer::New(); + points->SetNumberOfPoints(number_of_points); + for (vtkIdType i = 0; i < number_of_points; i++) { + double x, y, z; + x = m_content.Points().at(i)(0); + y = m_content.Points().at(i)(1); + z = m_content.Points().at(i)(2); + points->SetPoint(i, x, y, z); + } + + vtkSmartPointer polys = vtkSmartPointer::New(); + for (vtkIdType i = 0; i < number_of_triangles; i++) { + vtkIdType a, b, c; + a = m_content.Triangles().at(i)(0); + b = m_content.Triangles().at(i)(1); + c = m_content.Triangles().at(i)(2); + polys->InsertNextCell(3); + polys->InsertCellPoint(a); + polys->InsertCellPoint(b); + polys->InsertCellPoint(c); + } + + m_Poly->SetPoints(points); + m_Poly->SetPolys(polys); + m_Poly->Modified(); + m_Actor->GetMapper()->Update(); +} + +// -------------------------------------------------------------------------- // + +vtkTriangleMesh::vtkTriangleMesh(vtkTriangleMesh::Content &content) + : m_content(content), m_Poly(vtkPolyData::New()), m_Actor(vtkActor::New()) { + vtkSmartPointer mapper = + vtkSmartPointer::New(); + mapper->SetInputData(m_Poly); + m_Actor->SetMapper(mapper); + this->SetProp(m_Actor); +} + +vtkTriangleMesh::~vtkTriangleMesh() { + m_Poly->Delete(); + m_Actor->Delete(); +} + +void vtkTriangleMesh::ReadFromFile(const char *filename) { + vtkSmartPointer reader = + vtkSmartPointer::New(); + reader->SetFileName(filename); + reader->Update(); + m_Poly->DeepCopy(reader->GetOutput()); + vtk2uLib_update(); +} + +void vtkTriangleMesh::ReadFromXMLFile(const char *filename) { + vtkSmartPointer reader = + vtkSmartPointer::New(); + reader->SetFileName(filename); + reader->Update(); + m_Poly->DeepCopy(reader->GetOutput()); + vtk2uLib_update(); +} + +void vtkTriangleMesh::ReadFromObjFile(const char *filename) { + vtkSmartPointer reader = vtkSmartPointer::New(); + reader->SetFileName(filename); + reader->Update(); + m_Poly->DeepCopy(reader->GetOutput()); + vtk2uLib_update(); +} + +void vtkTriangleMesh::ReadFromStlFile(const char *filename) { + vtkSmartPointer reader = vtkSmartPointer::New(); + reader->SetFileName(filename); + reader->Update(); + m_Poly->DeepCopy(reader->GetOutput()); + vtk2uLib_update(); +} + +vtkPolyData *vtkTriangleMesh::GetPolyData() const { return m_Poly; } + +void vtkTriangleMesh::Update() { uLib2vtk_update(); } + +} // namespace Vtk +} // namespace uLib diff --git a/src/Vtk/vtkTriangleMesh.h b/src/Vtk/Math/vtkTriangleMesh.h similarity index 68% rename from src/Vtk/vtkTriangleMesh.h rename to src/Vtk/Math/vtkTriangleMesh.h index 53da896..8899838 100644 --- a/src/Vtk/vtkTriangleMesh.h +++ b/src/Vtk/Math/vtkTriangleMesh.h @@ -23,51 +23,47 @@ //////////////////////////////////////////////////////////////////////////////*/ - - #ifndef VTKTRIANGLEMESH_H #define VTKTRIANGLEMESH_H - -#include "uLibVtkInterface.h" #include "Math/TriangleMesh.h" +#include "Vtk/uLibVtkInterface.h" class vtkPolyData; +class vtkActor; namespace uLib { +namespace Vtk { +class vtkTriangleMesh : public Puppet, public Polydata { + typedef TriangleMesh Content; -class vtkTriangleMesh : public Abstract::uLibVtkPolydata { - typedef TriangleMesh Content; public: - vtkTriangleMesh(Content &content); - ~vtkTriangleMesh(); + vtkTriangleMesh(Content &content); + ~vtkTriangleMesh(); - void ReadFromFile(const char *filename); + void ReadFromFile(const char *filename); - void ReadFromXMLFile(const char *filename); + void ReadFromXMLFile(const char *filename); - void ReadFromObjFile(const char *filename); + void ReadFromObjFile(const char *filename); - void ReadFromStlFile(const char *filename); + void ReadFromStlFile(const char *filename); - virtual class vtkProp *GetProp(); + virtual class vtkPolyData *GetPolyData() const; - virtual class vtkPolyData *GetPolyData() const; - - void Update(); + void Update(); private: - void vtk2uLib_update(); - void uLib2vtk_update(); + void vtk2uLib_update(); + void uLib2vtk_update(); - TriangleMesh &m_content; - vtkPolyData *m_Poly; - vtkActor *m_Actor; + TriangleMesh &m_content; + vtkPolyData *m_Poly; + vtkActor *m_Actor; }; - - -} +} // namespace Vtk +} // namespace uLib #endif // VTKTRIANGLEMESH_H diff --git a/src/Vtk/Math/vtkVoxImage.cpp b/src/Vtk/Math/vtkVoxImage.cpp new file mode 100644 index 0000000..27f97ac --- /dev/null +++ b/src/Vtk/Math/vtkVoxImage.cpp @@ -0,0 +1,286 @@ +/*////////////////////////////////////////////////////////////////////////////// +// CMT Cosmic Muon Tomography project ////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// + + Copyright (c) 2014, Universita' degli Studi di Padova, INFN sez. di Padova + All rights reserved + + Authors: Andrea Rigoni Garola < andrea.rigoni@pd.infn.it > + + ------------------------------------------------------------------ + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 3.0 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library. + +//////////////////////////////////////////////////////////////////////////////*/ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include +#include +#include + +#include +#include + +#include +#include + +#include +#include +#include +#include + +#include +#include +#include + +#include + +#include "Vtk/Math/vtkVoxImage.h" + +namespace uLib { +namespace Vtk { + +void vtkVoxImage::GetContent() { + const int *dims = static_cast(m_Content.GetDims().data()); + m_Image->SetDimensions(dims); + + float *spacing = m_Content.GetSpacing().data(); + m_Image->SetSpacing(spacing[0], spacing[1], spacing[2]); + + float *pos = m_Content.GetPosition().data(); + m_Image->SetOrigin(pos[0], pos[1], pos[2]); + + vtkFloatArray *array = + vtkFloatArray::SafeDownCast(m_Image->GetPointData()->GetScalars()); + array->SetNumberOfTuples(m_Content.GetDims().prod()); + Vector3i index(0, 0, 0); + int i = 0; + for (int zv = 0; zv < dims[2]; ++zv) { + for (int yv = 0; yv < dims[1]; ++yv) { + for (int xv = 0; xv < dims[0]; ++xv) { + index << xv, yv, zv; + array->SetValue(i++, m_Content.GetValue(index)); + } + } + } + m_Image->GetPointData()->SetScalars(array); +} + +void vtkVoxImage::SetContent() { + int *ext = m_Image->GetExtent(); + int dims[3] = {ext[1] - ext[0] + 1, ext[3] - ext[2] + 1, ext[5] - ext[4] + 1}; + m_Content.SetDims(Vector3i(dims[0], dims[1], dims[2])); + + double *spacing = m_Image->GetSpacing(); + m_Content.SetSpacing(Vector3f(spacing[0], spacing[1], spacing[2])); + + double *pos = m_Image->GetOrigin(); + m_Content.SetPosition(Vector3f(pos[0], pos[1], pos[2])); + + vtkFloatArray *array = + vtkFloatArray::SafeDownCast(m_Image->GetPointData()->GetScalars()); + if (array) { + Vector3i index(0, 0, 0); + int i = 0; + for (int zv = 0; zv < dims[2]; ++zv) { + for (int yv = 0; yv < dims[1]; ++yv) { + for (int xv = 0; xv < dims[0]; ++xv) { + index << xv, yv, zv; + m_Content.SetValue(index, array->GetValue(i++)); + } + } + } + } else { + std::cerr << "Error reading array Value Data\n"; + } +} + +//////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +// VTK VOXIMAGE + +vtkVoxImage::vtkVoxImage(Content &content) + : m_Content(content), m_Actor(vtkVolume::New()), + m_Image(vtkImageData::New()), m_Outline(vtkCubeSource::New()), + m_Reader(NULL), m_Writer(NULL), writer_factor(1.E6) { + GetContent(); + InstallPipe(); +} + +vtkVoxImage::~vtkVoxImage() { + m_Image->Delete(); + m_Actor->Delete(); + m_Outline->Delete(); +} + +vtkImageData *vtkVoxImage::GetImageData() { + GetContent(); + return m_Image; +} + +void vtkVoxImage::SaveToXMLFile(const char *fname) { + vtkSmartPointer writer = + vtkSmartPointer::New(); + writer->SetFileName(fname); + GetContent(); + vtkSmartPointer vtkscale = + vtkSmartPointer::New(); + +#if VTK_MAJOR_VERSION <= 5 + vtkscale->SetInputConnection(m_Image->GetProducerPort()); +#else + vtkscale->SetInputData(m_Image); +#endif + vtkscale->SetScale(writer_factor); + vtkscale->Update(); + writer->SetInputConnection(vtkscale->GetOutputPort()); + writer->Update(); + writer->Write(); +} + +void vtkVoxImage::ReadFromVKTFile(const char *fname) { + vtkSmartPointer reader = + vtkSmartPointer::New(); + reader->SetFileName(fname); + reader->Update(); + if (reader->IsFileStructuredPoints()) { + vtkSmartPointer vtkscale = + vtkSmartPointer::New(); + vtkscale->SetInputConnection(reader->GetOutputPort()); + vtkscale->SetScale(1 / writer_factor); + vtkscale->Update(); + + m_Image->DeepCopy(vtkscale->GetOutput()); + SetContent(); + } else { + std::cerr << "Error: file does not contain structured points\n"; + } + m_Actor->Update(); +} + +void vtkVoxImage::ReadFromXMLFile(const char *fname) { + vtkSmartPointer reader = + vtkSmartPointer::New(); + reader->SetFileName(fname); + reader->Update(); + vtkSmartPointer vtkscale = + vtkSmartPointer::New(); + vtkscale->SetInputConnection(reader->GetOutputPort()); + vtkscale->SetScale(1 / writer_factor); + vtkscale->Update(); + + m_Image->DeepCopy(vtkscale->GetOutput()); + SetContent(); +} + +void vtkVoxImage::setShadingPreset(int blendType) { + vtkSmartVolumeMapper *mapper = (vtkSmartVolumeMapper *)m_Actor->GetMapper(); + vtkVolumeProperty *property = m_Actor->GetProperty(); + + static vtkColorTransferFunction *colorFun = vtkColorTransferFunction::New(); + static vtkPiecewiseFunction *opacityFun = vtkPiecewiseFunction::New(); + + float window = 40 / writer_factor; + float level = 20 / writer_factor; + + property->SetColor(colorFun); + property->SetScalarOpacity(opacityFun); + property->SetInterpolationTypeToLinear(); + + if (blendType != 6) { + colorFun->RemoveAllPoints(); + opacityFun->RemoveAllPoints(); + } + + switch (blendType) { + case 0: + colorFun->AddRGBSegment(0.0, 1.0, 1.0, 1.0, 255.0, 1.0, 1.0, 1.0); + opacityFun->AddSegment(level - 0.5 * window, 0.0, level + 0.5 * window, + 1.0); + mapper->SetBlendModeToMaximumIntensity(); + break; + case 1: + colorFun->AddRGBSegment(level - 0.5 * window, 0.0, 0.0, 0.0, + level + 0.5 * window, 1.0, 1.0, 1.0); + opacityFun->AddSegment(level - 0.5 * window, 0.0, level + 0.5 * window, + 1.0); + mapper->SetBlendModeToComposite(); + property->ShadeOff(); + break; + case 2: + colorFun->AddRGBSegment(0.0, 1.0, 1.0, 1.0, 255.0, 1.0, 1.0, 1.0); + opacityFun->AddSegment(level - 0.5 * window, 0.0, level + 0.5 * window, + 1.0); + mapper->SetBlendModeToComposite(); + property->ShadeOn(); + break; + case 3: + colorFun->AddRGBPoint(-3024, 0, 0, 0, 0.5, 0.0); + colorFun->AddRGBPoint(-1000, .62, .36, .18, 0.5, 0.0); + colorFun->AddRGBPoint(-500, .88, .60, .29, 0.33, 0.45); + colorFun->AddRGBPoint(3071, .83, .66, 1, 0.5, 0.0); + opacityFun->AddPoint(-3024, 0, 0.5, 0.0); + opacityFun->AddPoint(-1000, 0, 0.5, 0.0); + opacityFun->AddPoint(-500, 1.0, 0.33, 0.45); + opacityFun->AddPoint(3071, 1.0, 0.5, 0.0); + mapper->SetBlendModeToComposite(); + property->ShadeOn(); + property->SetAmbient(0.1); + property->SetDiffuse(0.9); + property->SetSpecular(0.2); + property->SetSpecularPower(10.0); + property->SetScalarOpacityUnitDistance(0.8919); + break; + default: + vtkGenericWarningMacro("Unknown blend type."); + break; + } +} + +void vtkVoxImage::Update() { + m_Actor->Update(); + m_Outline->SetBounds(m_Image->GetBounds()); + m_Outline->Update(); +} + +void vtkVoxImage::InstallPipe() { + vtkSmartPointer mapper = + vtkSmartPointer::New(); +#if VTK_MAJOR_VERSION <= 5 + mapper->SetInputConnection(m_Image->GetProducerPort()); +#else + mapper->SetInputData(m_Image); +#endif + mapper->Update(); + + m_Actor->SetMapper(mapper); + this->setShadingPreset(0); + mapper->Update(); + + m_Outline->SetBounds(m_Image->GetBounds()); + vtkSmartPointer mmapper = + vtkSmartPointer::New(); + mmapper->SetInputConnection(m_Outline->GetOutputPort()); + vtkSmartPointer actor = vtkSmartPointer::New(); + actor->SetMapper(mmapper); + actor->GetProperty()->SetRepresentationToWireframe(); + actor->GetProperty()->SetAmbient(0.7); + + this->SetProp(m_Actor); +} + +} // namespace Vtk +} // namespace uLib diff --git a/src/Vtk/vtkVoxImage.h b/src/Vtk/Math/vtkVoxImage.h similarity index 65% rename from src/Vtk/vtkVoxImage.h rename to src/Vtk/Math/vtkVoxImage.h index f94a334..5cfd65c 100644 --- a/src/Vtk/vtkVoxImage.h +++ b/src/Vtk/Math/vtkVoxImage.h @@ -23,73 +23,67 @@ //////////////////////////////////////////////////////////////////////////////*/ - - #ifndef U_VTKVOXIMAGE_H #define U_VTKVOXIMAGE_H -#include +#include #include +#include #include #include -#include #include -#include "uLibVtkInterface.h" - +#include "Vtk/uLibVtkInterface.h" class vtkImageData; namespace uLib { namespace Vtk { -class vtkVoxImage : public Puppet -{ +class vtkVoxImage : public Puppet { public: - typedef Abstract::VoxImage Content; + typedef Abstract::VoxImage Content; - vtkVoxImage(Content &content); + vtkVoxImage(Content &content); - ~vtkVoxImage(); + ~vtkVoxImage(); - void GetContent(); + void GetContent(); - void SetContent(); + void SetContent(); - vtkImageData * GetImageData(); + vtkImageData *GetImageData(); - void SaveToXMLFile(const char *fname); + void SaveToXMLFile(const char *fname); - void ReadFromVKTFile(const char *fname); + void ReadFromVKTFile(const char *fname); - void ReadFromXMLFile(const char *fname); + void ReadFromXMLFile(const char *fname); - void setShadingPreset(int blendType = 2); + void setShadingPreset(int blendType = 2); - void Update(); + void Update(); protected: - void InstallPipe(); - float writer_factor; + void InstallPipe(); + float writer_factor; private: - vtkVolume *m_Actor; - vtkImageData *m_Image; - vtkCubeSource *m_Outline; + vtkVolume *m_Actor; + vtkImageData *m_Image; + vtkCubeSource *m_Outline; - vtkXMLImageDataReader *m_Reader; - vtkXMLImageDataWriter *m_Writer; + vtkXMLImageDataReader *m_Reader; + vtkXMLImageDataWriter *m_Writer; - vtkVoxImage::Content &m_Content; - - float m_Window; - float m_Level; + vtkVoxImage::Content &m_Content; + float m_Window; + float m_Level; }; -} // vtk -} // uLib - +} // namespace Vtk +} // namespace uLib #endif // VTKVOXIMAGE_H diff --git a/src/Vtk/testing/CMakeLists.txt b/src/Vtk/testing/CMakeLists.txt index 99968ca..e5c9e48 100644 --- a/src/Vtk/testing/CMakeLists.txt +++ b/src/Vtk/testing/CMakeLists.txt @@ -2,9 +2,6 @@ set( TESTS vtkViewerTest vtkContainerBoxTest - vtkMuonScatter - vtkStructuredGridTest - vtkVoxRaytracerTest # vtkVoxImageTest # vtkTriangleMeshTest ) diff --git a/src/Vtk/testing/vtkVoxImageTest.cpp b/src/Vtk/testing/vtkVoxImageTest.cpp deleted file mode 100644 index 579bf62..0000000 --- a/src/Vtk/testing/vtkVoxImageTest.cpp +++ /dev/null @@ -1,102 +0,0 @@ -/*////////////////////////////////////////////////////////////////////////////// -// CMT Cosmic Muon Tomography project ////////////////////////////////////////// -//////////////////////////////////////////////////////////////////////////////// - - Copyright (c) 2014, Universita' degli Studi di Padova, INFN sez. di Padova - All rights reserved - - Authors: Andrea Rigoni Garola < andrea.rigoni@pd.infn.it > - - ------------------------------------------------------------------ - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 3.0 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this library. - -//////////////////////////////////////////////////////////////////////////////*/ - - - - -#include "Math/VoxImage.h" -#include "Vtk/vtkVoxImage.h" -#include "Vtk/uLibVtkViewer.h" - -#include "testing-prototype.h" - -using namespace uLib; - -struct TestVoxel { - Scalarf Value; - unsigned int Count; -}; - -int main() -{ - BEGIN_TESTING(Vtk Vox Image); - - TestVoxel zero = {0,0}; - TestVoxel nonzero = {5.5*1E-6,100}; - -// { // SIMPLE TESTS // -// VoxImage img(Vector3i(10,10,10)); -// img.SetSpacing(Vector3f(3,3,3)); - -// img.InitVoxels(zero); -// img[Vector3i(3,3,3)] = nonzero; - -// Vtk::vtkVoxImage vtk_img(img); - -// vtk_img.SaveToXMLFile("test.vti"); -// vtk_img.setShadingPreset(0); - -// // vtk_img.ReadFromVKTFile("error.vtk"); - -// // VoxImage img2 = img; - -// // Vtk::vtkVoxImage vtk_img2(img2); - -// // img2.ExportToVtk("error_saved.vtk",0); - -// // vtk_img2.SaveToXMLFile("error_saved.vti"); - -// Vtk::Viewer viewer; -// viewer.AddPuppet(vtk_img); -// viewer.Start(); -// } - - { // SIMPLE TESTS // - VoxImage img(Vector3i(10,10,1)); - img.SetSpacing(Vector3f(3,3,3)); - - img.InitVoxels(zero); - img[Vector3i(3,3,0)] = nonzero; - - Vtk::vtkVoxImage vtk_img(img); - - vtk_img.ReadFromVKTFile("test.vtk"); - vtk_img.Update(); - - // vtk_img.SaveToXMLFile("test.vti"); - // vtk_img.setShadingPreset(0); - - // Vtk::vtkVoxImage vtk_img2(img2); - // img2.ExportToVtk("error_saved.vtk",0); - // vtk_img2.SaveToXMLFile("error_saved.vti"); - - Vtk::Viewer viewer; - viewer.AddPuppet(vtk_img); - viewer.Start(); - } - - - END_TESTING; -} diff --git a/src/Vtk/vtkMuonEvent.cxx b/src/Vtk/vtkMuonEvent.cxx deleted file mode 100644 index e81368c..0000000 --- a/src/Vtk/vtkMuonEvent.cxx +++ /dev/null @@ -1,170 +0,0 @@ -/*////////////////////////////////////////////////////////////////////////////// -// CMT Cosmic Muon Tomography project ////////////////////////////////////////// -//////////////////////////////////////////////////////////////////////////////// - - Copyright (c) 2014, Universita' degli Studi di Padova, INFN sez. di Padova - All rights reserved - - Authors: Andrea Rigoni Garola < andrea.rigoni@pd.infn.it > - - ------------------------------------------------------------------ - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 3.0 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this library. - -//////////////////////////////////////////////////////////////////////////////*/ - - - -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif - -#include "vtkMuonEvent.h" -#include "Math/Dense.h" - -namespace uLib { - -///// CALLBACK ///////////////////////////////////////////////////////////////// - -class vtkWidgetCallback : public vtkCommand -{ -public: - static vtkWidgetCallback *New() - { return new vtkWidgetCallback; } - void SetParent(uLib::vtkMuonEvent *parent) { this->parent = parent; } - virtual void Execute(vtkObject *caller, unsigned long, void*) - { - vtkSmartPointer t = - vtkSmartPointer::New(); - vtkBoxWidget *widget = reinterpret_cast(caller); - widget->GetTransform(t); - //parent->SetTransform(t); - //std::cout << "event\n"; - } -private: - uLib::vtkMuonEvent *parent; -}; - - -//////////////////////////////////////////////////////////////////////////////// -///// VTK MUON EVENT ///////////////////////////////////////////////////////// -//////////////////////////////////////////////////////////////////////////////// - - -vtkMuonEvent::vtkMuonEvent(MuonEventData &content) : - m_Prop(vtkActor::New()), - m_PolyData(NULL), - m_Appender(vtkAppendPolyData::New()), - content(&content) -{ - InstallPipe(); -} - -vtkMuonEvent::vtkMuonEvent(const MuonEventData &content) : - m_Prop(vtkActor::New()), - m_PolyData(NULL), - m_Appender(vtkAppendPolyData::New()), - content(const_cast(&content)) -{ - InstallPipe(); -} - - -vtkMuonEvent::~vtkMuonEvent() -{ - if (m_Prop) m_Prop->Delete(); -} - -vtkMuonEvent::Content &vtkMuonEvent::GetContent() -{ - return *content; -} - -void vtkMuonEvent::PrintSelf(std::ostream &o) const -{ - o << "..:: MuonEvent ::..\n" \ - "\t[in] Origin > " << content->LineIn().origin.transpose() << "\n" << - "\t[in] Direction > " << content->LineIn().direction.transpose() << "\n" << - "\t[out] Origin > " << content->LineOut().origin.transpose() << "\n" << - "\t[out] Direction > " << content->LineOut().direction.transpose()<< "\n" << - "\tMomentum > " << content->GetMomentum() << "\n" << - "...................\n"; -} - -vtkProp *vtkMuonEvent::GetProp() -{ - return m_Prop; -} - -void vtkMuonEvent::InstallPipe() -{ - - - vtkAppendPolyData *appender = m_Appender; - vtkSmartPointer mapper = vtkSmartPointer::New(); - - if(content) { - vtkSmartPointer line_in = vtkSmartPointer::New(); - vtkSmartPointer line_out = vtkSmartPointer::New(); - - float distance = (content->LineIn().origin - content->LineOut().origin).norm() / 10; - - HPoint3f pt; - pt = content->LineIn().origin; - line_in->SetPoint1(pt(0),pt(1),pt(2)); - pt= content->LineIn().origin + content->LineIn().direction * distance; - line_in->SetPoint2(pt(0),pt(1),pt(2)); - pt = content->LineOut().origin; - line_out->SetPoint1(pt(0),pt(1),pt(2)); - pt = content->LineOut().origin + content->LineOut().direction * distance; - line_out->SetPoint2(pt(0),pt(1),pt(2)); - - appender->AddInputConnection(line_in->GetOutputPort()); - appender->AddInputConnection(line_out->GetOutputPort()); - } - - appender->Update(); - mapper->SetInputConnection(appender->GetOutputPort()); - mapper->Update(); - m_Prop->SetMapper(mapper); -} - -vtkPolyData *vtkMuonEvent::GetPolyData() const -{ - return m_Appender->GetOutput(); -} - -void vtkMuonEvent::AddPocaPoint(HPoint3f poca) -{ - m_Poca = poca; - vtkSmartPointer sphere = - vtkSmartPointer::New(); - float size = (content->LineIn().origin - content->LineOut().origin).head(3).norm(); - size /= 100; - sphere->SetRadius(size); - sphere->SetCenter(poca(0),poca(1),poca(2)); - sphere->Update(); - m_Appender->AddInputConnection(sphere->GetOutputPort()); - m_Appender->Update(); -} - -HPoint3f vtkMuonEvent::GetPocaPoint() -{ - return m_Poca; -} - - - - - -} diff --git a/src/Vtk/vtkMuonScatter.cxx b/src/Vtk/vtkMuonScatter.cxx deleted file mode 100644 index 5832c7f..0000000 --- a/src/Vtk/vtkMuonScatter.cxx +++ /dev/null @@ -1,189 +0,0 @@ -/*////////////////////////////////////////////////////////////////////////////// -// CMT Cosmic Muon Tomography project ////////////////////////////////////////// -//////////////////////////////////////////////////////////////////////////////// - - Copyright (c) 2014, Universita' degli Studi di Padova, INFN sez. di Padova - All rights reserved - - Authors: Andrea Rigoni Garola < andrea.rigoni@pd.infn.it > - - ------------------------------------------------------------------ - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 3.0 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this library. - -//////////////////////////////////////////////////////////////////////////////*/ - - - - -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif - -#include "vtkMuonScatter.h" -#include "Math/Dense.h" - -namespace uLib { -namespace Vtk { - -///// CALLBACK ///////////////////////////////////////////////////////////////// - -//namespace { - -//class vtkWidgetCallback : public vtkCommand -//{ -//public: -// static vtkWidgetCallback *New() -// { return new vtkWidgetCallback; } -// void SetParent(uLib::vtkMuonScatter *parent) { this->parent = parent; } -// virtual void Execute(vtkObject *caller, unsigned long, void*) -// { -// vtkSmartPointer t = -// vtkSmartPointer::New(); -// vtkBoxWidget *widget = reinterpret_cast(caller); -// widget->GetTransform(t); -// //parent->SetTransform(t); -// //std::cout << "Scatter\n"; -// } -//private: -// uLib::vtkMuonScatter *parent; -//}; - -//} - - -//////////////////////////////////////////////////////////////////////////////// -///// VTK MUON Scatter ///////////////////////////////////////////////////////// -//////////////////////////////////////////////////////////////////////////////// - - -vtkMuonScatter::vtkMuonScatter(MuonScatter &content) : - m_Content(&content), - m_LineIn(vtkLineSource::New()), - m_LineOut(vtkLineSource::New()), - m_PolyData(vtkPolyData::New()), - m_SpherePoca(NULL) -{ - InstallPipe(); -} - -vtkMuonScatter::vtkMuonScatter(const MuonScatter &content) : - m_Content(const_cast(&content)), - m_LineIn(vtkLineSource::New()), - m_LineOut(vtkLineSource::New()), - m_PolyData(vtkPolyData::New()), - m_SpherePoca(NULL) -{ - InstallPipe(); -} - - -vtkMuonScatter::~vtkMuonScatter() -{ - m_LineIn->Delete(); - m_LineOut->Delete(); - if(m_SpherePoca) m_SpherePoca->Delete(); -} - -vtkMuonScatter::Content &vtkMuonScatter::GetContent() -{ - return *m_Content; -} - -void vtkMuonScatter::PrintSelf(std::ostream &o) const -{} - - -void vtkMuonScatter::InstallPipe() -{ - if(m_Content) { - vtkLineSource *line_in = m_LineIn; - vtkLineSource *line_out = m_LineOut; - - float distance = (m_Content->LineIn().origin - m_Content->LineOut().origin).norm() / 10; - - HPoint3f pt; - pt = m_Content->LineIn().origin; - line_in->SetPoint1(pt(0),pt(1),pt(2)); - pt= m_Content->LineIn().origin + m_Content->LineIn().direction * distance; - line_in->SetPoint2(pt(0),pt(1),pt(2)); - pt = m_Content->LineOut().origin; - line_out->SetPoint1(pt(0),pt(1),pt(2)); - pt = m_Content->LineOut().origin + m_Content->LineOut().direction * distance; - line_out->SetPoint2(pt(0),pt(1),pt(2)); - } - - vtkSmartPointer mapper = vtkSmartPointer::New(); - mapper->SetInputConnection(m_LineIn->GetOutputPort()); - vtkSmartPointer actor = vtkSmartPointer::New(); - actor->SetMapper(mapper); - this->SetProp(actor); - - mapper = vtkSmartPointer::New(); - mapper->SetInputConnection(m_LineOut->GetOutputPort()); - actor = vtkSmartPointer::New(); - actor->SetMapper(mapper); - this->SetProp(actor); -} - -vtkPolyData *vtkMuonScatter::GetPolyData() const -{ - vtkSmartPointer append = vtkSmartPointer::New(); - append->AddInputConnection(m_LineIn->GetOutputPort()); - append->AddInputConnection(m_LineOut->GetOutputPort()); - if(m_SpherePoca) append->AddInputConnection(m_SpherePoca->GetOutputPort()); - append->Update(); - m_PolyData->DeepCopy(append->GetOutput()); - return m_PolyData; -} - -void vtkMuonScatter::AddPocaPoint(HPoint3f poca) -{ - vtkSphereSource *sphere = vtkSphereSource::New(); - float size = (m_Content->LineIn().origin - m_Content->LineOut().origin).head(3).norm(); - size /= 100; - sphere->SetRadius(size); - sphere->SetCenter(poca(0),poca(1),poca(2)); - sphere->Update(); - m_SpherePoca = sphere; - - vtkSmartPointer mapper = vtkSmartPointer::New(); - mapper->SetInputConnection(m_SpherePoca->GetOutputPort()); - vtkSmartPointer actor = vtkSmartPointer::New(); - actor->SetMapper(mapper); - this->SetProp(actor); -} - -HPoint3f vtkMuonScatter::GetPocaPoint() -{ - double center[3]; - if(m_SpherePoca) { - m_SpherePoca->GetCenter(center); - return HPoint3f(center[0],center[1],center[2]); - } - else { - return HPoint3f(0,0,0); - } -} - - -void vtkMuonScatter::ConnectInteractor(vtkRenderWindowInteractor *interactor) -{ - // TODO -} - - - - -} // vtk -} // uLib diff --git a/src/Vtk/vtkStructuredGrid.cpp b/src/Vtk/vtkStructuredGrid.cpp deleted file mode 100644 index a122c43..0000000 --- a/src/Vtk/vtkStructuredGrid.cpp +++ /dev/null @@ -1,135 +0,0 @@ -/*////////////////////////////////////////////////////////////////////////////// -// CMT Cosmic Muon Tomography project ////////////////////////////////////////// -//////////////////////////////////////////////////////////////////////////////// - - Copyright (c) 2014, Universita' degli Studi di Padova, INFN sez. di Padova - All rights reserved - - Authors: Andrea Rigoni Garola < andrea.rigoni@pd.infn.it > - - ------------------------------------------------------------------ - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 3.0 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this library. - -//////////////////////////////////////////////////////////////////////////////*/ - - - -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif - -#include "Math/StructuredGrid.h" -#include "Vtk/vtkStructuredGrid.h" - -namespace uLib { -namespace Vtk { - - -//////////////////////////////////////////////////////////////////////////////// -////// VTK STRUCTURED GRID ///////////////////////////////////////////////////// -//////////////////////////////////////////////////////////////////////////////// - - - -vtkStructuredGrid::vtkStructuredGrid(Content &content) : - m_Content(&content), - m_Actor(vtkActor::New()), - m_Widget(vtkBoxWidget::New()), - m_Transform(vtkTransform::New()) -{ - vtkSmartPointer callback = - vtkSmartPointer::New(); - callback->SetGrid(this); - m_Widget->AddObserver(vtkCommand::InteractionEvent, callback); - - this->InstallPipe(); -} - -vtkStructuredGrid::~vtkStructuredGrid() -{ - m_Actor->Delete(); - m_Widget->Delete(); - m_Transform->Delete(); -} - - -void vtkStructuredGrid::SetTransform(vtkTransform *t) -{ - vtkMatrix4x4 *vmat = t->GetMatrix(); - Matrix4f mat; - for(int i=0; i<4; ++i) - for(int j=0; j<4; ++j) - mat(i,j) = vmat->GetElement(i,j); - m_Content->SetMatrix(mat); - - vtkSmartPointer vmat2 = vtkSmartPointer::New(); - mat = m_Content->GetWorldMatrix(); - for(int i=0; i<4; ++i) - for(int j=0; j<4; ++j) - vmat2->SetElement(i,j,mat(i,j)); - m_Transform->SetMatrix(vmat2); - m_Transform->Update(); - this->Update(); -} - -vtkBoxWidget *vtkStructuredGrid::GetWidget() -{ - return m_Widget; -} - -void vtkStructuredGrid::Update() -{ - m_Actor->GetMapper()->Update(); -} - -void vtkStructuredGrid::InstallPipe() -{ - vtkSmartPointer cube = - vtkSmartPointer::New(); - vtkSmartPointer filter = - vtkSmartPointer::New(); - - vtkSmartPointer vmat = vtkSmartPointer::New(); - Matrix4f mat = m_Content->GetWorldMatrix(); - for(int i=0; i<4; ++i) - for(int j=0; j<4; ++j) - vmat->SetElement(i,j,mat(i,j)); - m_Transform->SetMatrix(vmat); - filter->SetTransform(m_Transform); - filter->SetInputConnection(cube->GetOutputPort()); - - Vector3i dims = m_Content->GetDims(); - cube->SetBounds(0,dims(0),0,dims(1),0,dims(2)); - cube->Update(); - filter->Update(); - - vtkSmartPointer mapper = - vtkSmartPointer::New(); - mapper->SetInputConnection(filter->GetOutputPort()); - - m_Actor->SetMapper(mapper); - m_Actor->GetProperty()->SetRepresentationToSurface(); - m_Actor->GetProperty()->SetEdgeVisibility(true); - m_Actor->GetProperty()->SetOpacity(0.4); - m_Actor->GetProperty()->SetAmbient(0.7); - // set content transform to actor // - this->Update(); - m_Widget->SetProp3D(m_Actor); - - this->SetProp(m_Actor); -} - - -} // vtk -} // uLib diff --git a/src/Vtk/vtkTriangleMesh.cpp b/src/Vtk/vtkTriangleMesh.cpp deleted file mode 100644 index e03bad6..0000000 --- a/src/Vtk/vtkTriangleMesh.cpp +++ /dev/null @@ -1,206 +0,0 @@ -/*////////////////////////////////////////////////////////////////////////////// -// CMT Cosmic Muon Tomography project ////////////////////////////////////////// -//////////////////////////////////////////////////////////////////////////////// - - Copyright (c) 2014, Universita' degli Studi di Padova, INFN sez. di Padova - All rights reserved - - Authors: Andrea Rigoni Garola < andrea.rigoni@pd.infn.it > - - ------------------------------------------------------------------ - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 3.0 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this library. - -//////////////////////////////////////////////////////////////////////////////*/ - - - -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif - -#include - -#include -#include -#include -#include - -#include -#include -#include -#include -#include - -#include "Vtk/vtkTriangleMesh.h" - - -namespace uLib { - - -void vtkTriangleMesh::vtk2uLib_update() -{ - // Assumes that Polys are Triangles !!! // - vtkIdType number_of_points = m_Poly->GetNumberOfPoints(); - vtkIdType number_of_triangles = m_Poly->GetNumberOfPolys(); - - std::cout << "//////\n" << - "number of points = " << number_of_points << "\n" << - "muiber of polys = " << number_of_triangles << "\n" << - "//////\n"; - - - - - - m_content.Points().resize(number_of_points); - for (int i=0; iGetPoint(i); - m_content.Points()[i](0) = point[0]; - m_content.Points()[i](1) = point[1]; - m_content.Points()[i](2) = point[2]; -// std::cout << "pt" << i << " = " << m_content.Points()[i].transpose() << "\n"; - } - - m_content.Triangles().resize(number_of_triangles); - m_Poly->GetPolys()->InitTraversal(); - vtkSmartPointer idList = vtkSmartPointer::New(); - for (int i=0; iGetPolys()->GetNextCell(idList); - m_content.Triangles()[i](0) = idList->GetId(0); - m_content.Triangles()[i](1) = idList->GetId(1); - m_content.Triangles()[i](2) = idList->GetId(2); - } - m_Poly->Update(); - m_Actor->GetMapper()->Update(); -} - -void vtkTriangleMesh::uLib2vtk_update() -{ - vtkIdType number_of_points = m_content.Points().size(); - vtkIdType number_of_triangles = m_content.Triangles().size(); - - vtkSmartPointer points - = vtkSmartPointer::New(); - points->SetNumberOfPoints(number_of_points); - for (vtkIdType i = 0; i < number_of_points; i++) - { - double x, y, z; - x = m_content.Points().at(i)(0); - y = m_content.Points().at(i)(1); - z = m_content.Points().at(i)(2); - points->SetPoint(i, x, y, z); - } - - vtkSmartPointer polys - = vtkSmartPointer::New(); - for (vtkIdType i = 0; i < number_of_triangles; i++) - { - vtkIdType a, b, c; - a = m_content.Triangles().at(i)(0); - b = m_content.Triangles().at(i)(1); - c = m_content.Triangles().at(i)(2); - polys->InsertNextCell(3); - polys->InsertCellPoint(a); - polys->InsertCellPoint(b); - polys->InsertCellPoint(c); - } - - m_Poly->SetPoints(points); - m_Poly->SetPolys(polys); - m_Poly->Update(); - m_Actor->GetMapper()->Update(); -} - - -// -------------------------------------------------------------------------- // - - - - - -vtkTriangleMesh::vtkTriangleMesh(vtkTriangleMesh::Content &content) : - m_content(content), - m_Poly(vtkPolyData::New()), - m_Actor(vtkActor::New()) -{ - vtkSmartPointer mapper = - vtkSmartPointer::New(); - mapper->SetInputConnection(m_Poly->GetProducerPort()); - m_Actor->SetMapper(mapper); -} - -vtkTriangleMesh::~vtkTriangleMesh() -{ - m_Poly->Delete(); - m_Actor->Delete(); -} - -void vtkTriangleMesh::ReadFromFile(const char *filename) -{ - vtkSmartPointer reader = - vtkSmartPointer::New(); - reader->SetFileName(filename); - reader->Update(); - m_Poly->DeepCopy(reader->GetOutput()); - vtk2uLib_update(); -} - -void vtkTriangleMesh::ReadFromXMLFile(const char *filename) -{ - vtkSmartPointer reader = - vtkSmartPointer::New(); - reader->SetFileName(filename); - reader->Update(); - m_Poly->DeepCopy(reader->GetOutput()); - vtk2uLib_update(); -} - -void vtkTriangleMesh::ReadFromObjFile(const char *filename) -{ - vtkSmartPointer reader = - vtkSmartPointer::New(); - reader->SetFileName(filename); - reader->Update(); - m_Poly->DeepCopy(reader->GetOutput()); - vtk2uLib_update(); -} - -void vtkTriangleMesh::ReadFromStlFile(const char *filename) -{ - vtkSmartPointer reader = - vtkSmartPointer::New(); - reader->SetFileName(filename); - reader->Update(); - m_Poly->DeepCopy(reader->GetOutput()); - vtk2uLib_update(); -} - -vtkProp *vtkTriangleMesh::GetProp() -{ - return m_Actor; -} - -vtkPolyData *vtkTriangleMesh::GetPolyData() const -{ - return m_Poly; -} - -void vtkTriangleMesh::Update() -{ - uLib2vtk_update(); -} - -} diff --git a/src/Vtk/vtkVoxImage.cpp b/src/Vtk/vtkVoxImage.cpp deleted file mode 100644 index aad7465..0000000 --- a/src/Vtk/vtkVoxImage.cpp +++ /dev/null @@ -1,340 +0,0 @@ -/*////////////////////////////////////////////////////////////////////////////// -// CMT Cosmic Muon Tomography project ////////////////////////////////////////// -//////////////////////////////////////////////////////////////////////////////// - - Copyright (c) 2014, Universita' degli Studi di Padova, INFN sez. di Padova - All rights reserved - - Authors: Andrea Rigoni Garola < andrea.rigoni@pd.infn.it > - - ------------------------------------------------------------------ - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 3.0 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this library. - -//////////////////////////////////////////////////////////////////////////////*/ - - - -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif - - -#include -#include -#include - -#include -#include - -#include -#include - -#include -#include -#include -#include - -#include -#include -#include - - -#include - -#include "vtkVoxImage.h" - - -namespace uLib { -namespace Vtk { - - -void vtkVoxImage::GetContent() -{ - // ULIB -> VTK // - const int *dims = static_cast(m_Content.GetDims().data()); - m_Image->SetDimensions(dims); - - float *spacing = m_Content.GetSpacing().data(); - m_Image->SetSpacing(spacing[0],spacing[1],spacing[2]); - - // Warning .. TODO: Read complete Transform matrix // - float *pos = m_Content.GetPosition().data(); - m_Image->SetOrigin(pos[0],pos[1],pos[2]); - - vtkFloatArray *array = vtkFloatArray::SafeDownCast - (m_Image->GetPointData()->GetScalars()); - array->SetNumberOfTuples(m_Content.GetDims().prod()); - Vector3i index(0,0,0); - int i=0; - for (int zv = 0; zv < dims[2]; ++zv) { - for (int yv = 0; yv < dims[1]; ++yv) { - for (int xv = 0; xv < dims[0]; ++xv) { - index << xv,yv,zv; - array->SetValue(i++,m_Content.GetValue(index)); - } - } - } - m_Image->GetPointData()->SetScalars(array); - // m_Image->Update(); -} - -void vtkVoxImage::SetContent() -{ - // VTK -> ULIB // - int *ext = m_Image->GetExtent(); - int dims[3] = { ext[1]-ext[0] + 1, ext[3]-ext[2] + 1, ext[5]-ext[4] + 1 }; - m_Content.SetDims(Vector3i(dims[0],dims[1],dims[2])); - - double *spacing = m_Image->GetSpacing(); - m_Content.SetSpacing(Vector3f(spacing[0],spacing[1],spacing[2])); - - // Warning .. TODO: Read complete Transform matrix // - double *pos = m_Image->GetOrigin(); - m_Content.SetPosition(Vector3f(pos[0],pos[1],pos[2])); - - vtkFloatArray *array = vtkFloatArray::SafeDownCast - (m_Image->GetPointData()->GetScalars()); - if(array) { - Vector3i index(0,0,0); - int i=0; - for (int zv = 0; zv < dims[2]; ++zv) { - for (int yv = 0; yv < dims[1]; ++yv) { - for (int xv = 0; xv < dims[0]; ++xv) { - index << xv,yv,zv; - m_Content.SetValue(index,array->GetValue(i++)); - } - } - } - } - else { - std::cerr << "Error reading array Value Data\n"; - } -} - - - - -//////////////////////////////////////////////////////////////////////////////// -//////////////////////////////////////////////////////////////////////////////// -//////////////////////////////////////////////////////////////////////////////// -// VTK VOXIMAGE - -vtkVoxImage::vtkVoxImage(Content &content) : - m_Content(content), - m_Actor(vtkVolume::New()), - m_Image(vtkImageData::New()), - m_Outline(vtkCubeSource::New()), - m_Reader(NULL), - m_Writer(NULL), - writer_factor(1.E6) -{ - GetContent(); - InstallPipe(); -} - -vtkVoxImage::~vtkVoxImage() -{ - m_Image->Delete(); - m_Actor->Delete(); - m_Outline->Delete(); -} - -vtkImageData *vtkVoxImage::GetImageData() -{ - GetContent(); - return m_Image; -} - -void vtkVoxImage::SaveToXMLFile(const char *fname) -{ - vtkSmartPointer writer = - vtkSmartPointer::New(); - writer->SetFileName(fname); - GetContent(); - vtkSmartPointer vtkscale = - vtkSmartPointer::New(); - -# if VTK_MAJOR_VERSION <= 5 - vtkscale->SetInputConnection(m_Image->GetProducerPort()); -# else - vtkscale->SetInputData(m_Image); -# endif - vtkscale->SetScale(writer_factor); - vtkscale->Update(); - writer->SetInputConnection(vtkscale->GetOutputPort()); - writer->Update(); - writer->Write(); -} - -void vtkVoxImage::ReadFromVKTFile(const char *fname) -{ - vtkSmartPointer reader = - vtkSmartPointer::New(); - reader->SetFileName(fname); - reader->Update(); - if(reader->IsFileStructuredPoints()) - { - vtkSmartPointer vtkscale = - vtkSmartPointer::New(); - vtkscale->SetInputConnection(reader->GetOutputPort()); - vtkscale->SetScale(1/writer_factor); - vtkscale->Update(); - - m_Image->DeepCopy(vtkscale->GetOutput()); // FIX! (pipe connection) - SetContent(); - } - else { - std::cerr << "Error: file does not contain structured points\n"; - } - m_Actor->Update(); -} - -void vtkVoxImage::ReadFromXMLFile(const char *fname) -{ - vtkSmartPointer reader = - vtkSmartPointer::New(); - reader->SetFileName(fname); - reader->Update(); - vtkSmartPointer vtkscale = - vtkSmartPointer::New(); - vtkscale->SetInputConnection(reader->GetOutputPort()); - vtkscale->SetScale(1/writer_factor); - vtkscale->Update(); - - m_Image->DeepCopy(vtkscale->GetOutput()); - SetContent(); -} - - -void vtkVoxImage::setShadingPreset(int blendType) -{ - vtkSmartVolumeMapper *mapper = - (vtkSmartVolumeMapper *)m_Actor->GetMapper(); - vtkVolumeProperty *property = m_Actor->GetProperty(); - - static vtkColorTransferFunction *colorFun = vtkColorTransferFunction::New(); - static vtkPiecewiseFunction *opacityFun = vtkPiecewiseFunction::New(); - - float window = 40 / writer_factor; - float level = 20 / writer_factor; - - property->SetColor(colorFun); - property->SetScalarOpacity(opacityFun); - property->SetInterpolationTypeToLinear(); - - if(blendType != 6) { - colorFun->RemoveAllPoints(); - opacityFun->RemoveAllPoints(); - } - - switch (blendType) { - // MIP - // Create an opacity ramp from the window and level values. - // Color is white. Blending is MIP. - case 0: - colorFun->AddRGBSegment(0.0, 1.0, 1.0, 1.0, 255.0, 1.0, 1.0, 1.0); - opacityFun->AddSegment(level - 0.5 * window, 0.0, - level + 0.5 * window, 1.0); - mapper->SetBlendModeToMaximumIntensity(); - break; - - // CompositeRamp - // Create a ramp from the window and level values. Use compositing - // without shading. Color is a ramp from black to white. - case 1: - colorFun->AddRGBSegment(level - 0.5 * window, 0.0, 0.0, 0.0, - level + 0.5 * window, 1.0, 1.0, 1.0); - opacityFun->AddSegment(level - 0.5 * window, 0.0, - level + 0.5 * window, 1.0); - mapper->SetBlendModeToComposite(); - property->ShadeOff(); - break; - - // CompositeShadeRamp - // Create a ramp from the window and level values. Use compositing - // with shading. Color is white. - case 2: - colorFun->AddRGBSegment(0.0, 1.0, 1.0, 1.0, 255.0, 1.0, 1.0, 1.0); - opacityFun->AddSegment(level - 0.5 * window, 0.0, - level + 0.5 * window, 1.0); - mapper->SetBlendModeToComposite(); - property->ShadeOn(); - break; - - // CT_Skin - // Use compositing and functions set to highlight skin in CT data - // Not for use on RGB data - case 3: - colorFun->AddRGBPoint(-3024, 0, 0, 0, 0.5, 0.0); - colorFun->AddRGBPoint(-1000, .62, .36, .18, 0.5, 0.0); - colorFun->AddRGBPoint(-500, .88, .60, .29, 0.33, 0.45); - colorFun->AddRGBPoint(3071, .83, .66, 1, 0.5, 0.0); - - opacityFun->AddPoint(-3024, 0, 0.5, 0.0); - opacityFun->AddPoint(-1000, 0, 0.5, 0.0); - opacityFun->AddPoint(-500, 1.0, 0.33, 0.45); - opacityFun->AddPoint(3071, 1.0, 0.5, 0.0); - - mapper->SetBlendModeToComposite(); - property->ShadeOn(); - property->SetAmbient(0.1); - property->SetDiffuse(0.9); - property->SetSpecular(0.2); - property->SetSpecularPower(10.0); - property->SetScalarOpacityUnitDistance(0.8919); - break; - - - default: - vtkGenericWarningMacro("Unknown blend type."); - break; - } -} - -void vtkVoxImage::Update() { - m_Actor->Update(); - m_Outline->SetBounds(m_Image->GetBounds()); - m_Outline->Update(); -} - -void vtkVoxImage::InstallPipe() -{ - vtkSmartPointer mapper = - vtkSmartPointer::New(); -# if VTK_MAJOR_VERSION <= 5 - mapper->SetInputConnection(m_Image->GetProducerPort()); -# else - mapper->SetInputData(m_Image); -# endif - mapper->Update(); - - m_Actor->SetMapper(mapper); - this->setShadingPreset(0); - mapper->Update(); - - m_Outline->SetBounds(m_Image->GetBounds()); - vtkSmartPointer mmapper = vtkSmartPointer::New(); - mmapper->SetInputConnection(m_Outline->GetOutputPort()); - vtkSmartPointer actor = vtkSmartPointer::New(); - actor->SetMapper(mmapper); - actor->GetProperty()->SetRepresentationToWireframe(); - actor->GetProperty()->SetAmbient(0.7); - - // this->SetProp(actor); - this->SetProp(m_Actor); -} - - -} // vtk -} // uLib