diff --git a/Examples/CMakeLists.txt b/Examples/CMakeLists.txt index 960535e42b..5f55f7d253 100644 --- a/Examples/CMakeLists.txt +++ b/Examples/CMakeLists.txt @@ -1,63 +1,64 @@ set(MITK_DEFAULT_SUBPROJECTS MITK-Examples) #----------------------------------------------------------------------------- # Set-up example plugins #----------------------------------------------------------------------------- if(MITK_USE_BLUEBERRY) # Specify which plug-ins belong to this project macro(GetMyTargetLibraries all_target_libraries varname) set(re_ctkplugin_mitk "^org_mitk_example_[a-zA-Z0-9_]+$") set(_tmp_list) list(APPEND _tmp_list ${all_target_libraries}) ctkMacroListFilter(_tmp_list re_ctkplugin_mitk OUTPUT_VARIABLE ${varname}) endmacro() set(MITK_EXAMPLE_PLUGIN_TARGETS ) foreach(mitk_example_plugin ${MITK_EXAMPLE_PLUGINS}) ctkFunctionExtractOptionNameAndValue(${mitk_example_plugin} plugin_name plugin_value) string(REPLACE "." "_" _plugin_target ${plugin_name}) list(APPEND MITK_EXAMPLE_PLUGIN_TARGETS ${_plugin_target}) mark_as_advanced(${${_plugin_target}_option_name}) endforeach() endif() #----------------------------------------------------------------------------- # Add example executables #----------------------------------------------------------------------------- set(MITK_DIR ${PROJECT_BINARY_DIR}) set(MITK_EXPORTS_FILE_INCLUDED 1) set(_example_dirs Dump MbiLog QtFreeRender Tutorial + FirstSteps # Overlays# depends on MitkQtWidgetsExt.. ) # some examples depend on Qt 5 if (MITK_USE_Qt5) list(APPEND _example_dirs QuickRender ) elseif (MITK_USE_Qt4) list(APPEND _example_dirs QtAppExample DumpDir ) endif() if(MITK_USE_BLUEBERRY) list(APPEND _example_dirs BlueBerryExampleLauncher ) endif() foreach(_example_dir ${_example_dirs}) add_subdirectory(${_example_dir}) endforeach() diff --git a/Examples/FirstSteps/CMakeLists.txt b/Examples/FirstSteps/CMakeLists.txt new file mode 100644 index 0000000000..1f6579406d --- /dev/null +++ b/Examples/FirstSteps/CMakeLists.txt @@ -0,0 +1 @@ +add_subdirectory(NewModule) \ No newline at end of file diff --git a/Examples/FirstSteps/NewModule/CMakeLists.txt b/Examples/FirstSteps/NewModule/CMakeLists.txt new file mode 100644 index 0000000000..dc2f2c9e12 --- /dev/null +++ b/Examples/FirstSteps/NewModule/CMakeLists.txt @@ -0,0 +1,12 @@ +MITK_CREATE_MODULE( + INCLUDE_DIRS + PUBLIC ${MITK_BINARY_DIR} + PRIVATE src/DataManagement + DEPENDS PUBLIC MitkCore + WARNINGS_AS_ERRORS + SUBPROJECTS MITK-Examples +) + +add_subdirectory(autoload/IO) +add_subdirectory(MiniApps) +add_subdirectory(test) diff --git a/Examples/FirstSteps/NewModule/MiniApps/CMakeLists.txt b/Examples/FirstSteps/NewModule/MiniApps/CMakeLists.txt new file mode 100644 index 0000000000..b6219118e7 --- /dev/null +++ b/Examples/FirstSteps/NewModule/MiniApps/CMakeLists.txt @@ -0,0 +1,76 @@ +option(BUILD_NewModuleMiniApps "Build commandline tools for the example module" OFF) + +if(BUILD_NewModuleMiniApps) + + # needed include directories + include_directories( + ${CMAKE_CURRENT_SOURCE_DIR} + ${CMAKE_CURRENT_BINARY_DIR} + ) + # list of miniapps + # if an app requires additional dependencies + # they are added after a "^^" and separated by "_" + set( miniapps + ExampleToUpperCaseMiniApp^^ + ) + + foreach(miniapp ${miniapps}) + # extract mini app name and dependencies + string(REPLACE "^^" "\\;" miniapp_info ${miniapp}) + set(miniapp_info_list ${miniapp_info}) + list(GET miniapp_info_list 0 appname) + list(GET miniapp_info_list 1 raw_dependencies) + string(REPLACE "_" "\\;" dependencies "${raw_dependencies}") + set(dependencies_list ${dependencies}) + + mitk_create_executable(${appname} + DEPENDS MitkNewModule MitkCommandLine ${dependencies_list} + PACKAGE_DEPENDS ITK + CPP_FILES ${appname}.cpp + ) + + if(EXECUTABLE_IS_ENABLED) + + # On Linux, create a shell script to start a relocatable application + if(UNIX AND NOT APPLE) + install(PROGRAMS "${MITK_SOURCE_DIR}/CMake/RunInstalledApp.sh" DESTINATION "." RENAME ${EXECUTABLE_TARGET}.sh) + endif() + + get_target_property(_is_bundle ${EXECUTABLE_TARGET} MACOSX_BUNDLE) + + if(APPLE) + if(_is_bundle) + set(_target_locations ${EXECUTABLE_TARGET}.app) + set(${_target_locations}_qt_plugins_install_dir ${EXECUTABLE_TARGET}.app/Contents/MacOS) + set(_bundle_dest_dir ${EXECUTABLE_TARGET}.app/Contents/MacOS) + set(_qt_plugins_for_current_bundle ${EXECUTABLE_TARGET}.app/Contents/MacOS) + set(_qt_conf_install_dirs ${EXECUTABLE_TARGET}.app/Contents/Resources) + install(TARGETS ${EXECUTABLE_TARGET} BUNDLE DESTINATION . ) + else() + if(NOT MACOSX_BUNDLE_NAMES) + set(_qt_conf_install_dirs bin) + set(_target_locations bin/${EXECUTABLE_TARGET}) + set(${_target_locations}_qt_plugins_install_dir bin) + install(TARGETS ${EXECUTABLE_TARGET} RUNTIME DESTINATION bin) + else() + foreach(bundle_name ${MACOSX_BUNDLE_NAMES}) + list(APPEND _qt_conf_install_dirs ${bundle_name}.app/Contents/Resources) + set(_current_target_location ${bundle_name}.app/Contents/MacOS/${EXECUTABLE_TARGET}) + list(APPEND _target_locations ${_current_target_location}) + set(${_current_target_location}_qt_plugins_install_dir ${bundle_name}.app/Contents/MacOS) + message( " set(${_current_target_location}_qt_plugins_install_dir ${bundle_name}.app/Contents/MacOS) ") + + install(TARGETS ${EXECUTABLE_TARGET} RUNTIME DESTINATION ${bundle_name}.app/Contents/MacOS/) + endforeach() + endif() + endif() + else() + set(_target_locations bin/${EXECUTABLE_TARGET}${CMAKE_EXECUTABLE_SUFFIX}) + set(${_target_locations}_qt_plugins_install_dir bin) + set(_qt_conf_install_dirs bin) + install(TARGETS ${EXECUTABLE_TARGET} RUNTIME DESTINATION bin) + endif() + endif() + endforeach() + +endif() diff --git a/Examples/FirstSteps/NewModule/MiniApps/ExampleToUpperCaseMiniApp.cpp b/Examples/FirstSteps/NewModule/MiniApps/ExampleToUpperCaseMiniApp.cpp new file mode 100644 index 0000000000..09fd150ecf --- /dev/null +++ b/Examples/FirstSteps/NewModule/MiniApps/ExampleToUpperCaseMiniApp.cpp @@ -0,0 +1,108 @@ +/*=================================================================== + +The Medical Imaging Interaction Toolkit (MITK) + +Copyright (c) German Cancer Research Center, +Division of Medical and Biological Informatics. +All rights reserved. + +This software is distributed WITHOUT ANY WARRANTY; without +even the implied warranty of MERCHANTABILITY or FITNESS FOR +A PARTICULAR PURPOSE. + +See LICENSE.txt or http://www.mitk.org for details. + +===================================================================*/ + +// std includes +#include +#include + +// CTK includes +#include "mitkCommandLineParser.h" + +// MITK includes +#include "mitkExampleDataStructure.h" +#include + +int main( int argc, char* argv[] ) +{ + mitkCommandLineParser parser; + + parser.setTitle( "To Upper Case" ); + parser.setCategory( "MITK-Examples" ); + parser.setDescription( "" ); + parser.setContributor( "MBI" ); + + parser.setArgumentPrefix( "--", "-" ); + parser.addArgument( + "input", "i", mitkCommandLineParser::InputFile, "Input file", "input file (.txt/.example)", us::Any(), false ); + parser.addArgument( "output", + "o", + mitkCommandLineParser::OutputFile, + "Output file", + "where to save the output (.txt/.example)", + us::Any(), + false ); + + parser.addArgument( + "verbose", "v", mitkCommandLineParser::Bool, "Verbose Output", "Whether to produce verbose output" ); + + map parsedArgs = parser.parseArguments( argc, argv ); + if ( parsedArgs.size() == 0 ) + return EXIT_FAILURE; + + // default values + bool verbose( false ); + + // parse command line arguments + std::string inFilename = us::any_cast( parsedArgs[ "input" ] ); + std::string outFileName = us::any_cast( parsedArgs[ "output" ] ); + + // if optional flag is present + if ( parsedArgs.count( "verbose" ) ) + verbose = us::any_cast( parsedArgs[ "verbose" ] ); + + try + { + // load file + std::vector inVector = mitk::IOUtil::Load( inFilename ); + if ( inVector.empty() ) + { + std::string errorMessage = "File at " + inFilename + " could not be read. Aborting."; + MITK_ERROR << errorMessage; + return EXIT_FAILURE; + } + mitk::BaseData* inBaseData = inVector.at( 0 ); + mitk::ExampleDataStructure* inExample = dynamic_cast( inBaseData ); + + // do creation + mitk::ExampleDataStructure::Pointer outExample = mitk::ExampleDataStructure::New(); + std::string data = inExample->GetData(); + std::transform( data.begin(), data.end(), data.begin(), ::toupper ); + outExample->SetData( data ); + + std::cout << "searching writer"; + + mitk::IOUtil::SaveBaseData( outExample.GetPointer(), outFileName ); + + return EXIT_SUCCESS; + } + catch ( itk::ExceptionObject e ) + { + std::cout << e; + return EXIT_FAILURE; + } + catch ( std::exception e ) + { + std::cout << e.what(); + return EXIT_FAILURE; + } + catch ( ... ) + { + std::cout << "ERROR!?!"; + return EXIT_FAILURE; + } + std::cout << "DONE"; + return EXIT_SUCCESS; +} diff --git a/Examples/FirstSteps/NewModule/autoload/IO/CMakeLists.txt b/Examples/FirstSteps/NewModule/autoload/IO/CMakeLists.txt new file mode 100644 index 0000000000..d5ca58f7ce --- /dev/null +++ b/Examples/FirstSteps/NewModule/autoload/IO/CMakeLists.txt @@ -0,0 +1,10 @@ +MITK_CREATE_MODULE( NewModuleIO + INCLUDE_DIRS + PRIVATE src/IO + DEPENDS PUBLIC MitkNewModule MitkSceneSerialization + PACKAGE_DEPENDS + PRIVATE tinyxml + AUTOLOAD_WITH MitkCore + WARNINGS_AS_ERRORS + SUBPROJECTS MITK-Examples +) \ No newline at end of file diff --git a/Examples/FirstSteps/NewModule/autoload/IO/files.cmake b/Examples/FirstSteps/NewModule/autoload/IO/files.cmake new file mode 100644 index 0000000000..6fdfa62128 --- /dev/null +++ b/Examples/FirstSteps/NewModule/autoload/IO/files.cmake @@ -0,0 +1,12 @@ +set(CPP_FILES + mitkExampleDataStructureReaderService.cpp + mitkExampleDataStructureReaderService.h + mitkExampleDataStructureSerializer.cpp + mitkExampleDataStructureSerializer.h + mitkExampleDataStructureWriterService.cpp + mitkExampleDataStructureWriterService.h + mitkExampleIOMimeTypes.cpp + mitkExampleIOMimeTypes.h + mitkNewModuleIOActivator.cpp +) + diff --git a/Examples/FirstSteps/NewModule/autoload/IO/mitkExampleDataStructureReaderService.cpp b/Examples/FirstSteps/NewModule/autoload/IO/mitkExampleDataStructureReaderService.cpp new file mode 100644 index 0000000000..e910d119ca --- /dev/null +++ b/Examples/FirstSteps/NewModule/autoload/IO/mitkExampleDataStructureReaderService.cpp @@ -0,0 +1,99 @@ +/*=================================================================== + +The Medical Imaging Interaction Toolkit (MITK) + +Copyright (c) German Cancer Research Center, +Division of Medical and Biological Informatics. +All rights reserved. + +This software is distributed WITHOUT ANY WARRANTY; without +even the implied warranty of MERCHANTABILITY or FITNESS FOR +A PARTICULAR PURPOSE. + +See LICENSE.txt or http://www.mitk.org for details. + +===================================================================*/ + +#include "mitkExampleDataStructureReaderService.h" + +// mitk includes +#include "mitkGeometry3D.h" +#include +#include "mitkExampleIOMimeTypes.h" + +// itk includes +#include "itksys/SystemTools.hxx" + +namespace mitk +{ + + ExampleDataStructureReaderService::ExampleDataStructureReaderService(const ExampleDataStructureReaderService& other) + : mitk::AbstractFileReader(other) + { + } + + ExampleDataStructureReaderService::ExampleDataStructureReaderService() + : mitk::AbstractFileReader( CustomMimeType( mitk::ExampleIOMimeTypes::EXAMPLE_MIMETYPE() ), "Default reader for the example data structure" ) + { + m_ServiceReg = this->RegisterService(); + } + + ExampleDataStructureReaderService::~ExampleDataStructureReaderService() + { + } + + std::vector > ExampleDataStructureReaderService::Read() + { + std::vector > result; + std::string location = GetInputLocation(); + + std::string ext = itksys::SystemTools::GetFilenameLastExtension(location); + ext = itksys::SystemTools::LowerCase(ext); + + if ( location == "") + { + MITK_ERROR << "No file name specified."; + } + try + { + std::ifstream file(location); + std::string content(""); + std::string line(""); + if (file.is_open()) + { + while (getline(file, line)) + { + content += line; + content += "\n"; + } + } + else + { + mitkThrow() << "Could not open file " << this->GetInputLocation() << " for reading."; + } + + mitk::ExampleDataStructure::Pointer outputData = mitk::ExampleDataStructure::New(); + outputData->SetData(content); + result.push_back(outputData.GetPointer()); + MITK_INFO << "Example file read"; + } + + catch (mitk::Exception e) + { + MITK_ERROR << e.GetDescription(); + } + catch(...) + { + MITK_ERROR << "Unknown error occurred while trying to read file."; + } + + return result; + } + + +} //namespace MITK + +mitk::ExampleDataStructureReaderService* mitk::ExampleDataStructureReaderService::Clone() const +{ + return new ExampleDataStructureReaderService(*this); +} diff --git a/Examples/FirstSteps/NewModule/autoload/IO/mitkExampleDataStructureReaderService.h b/Examples/FirstSteps/NewModule/autoload/IO/mitkExampleDataStructureReaderService.h new file mode 100644 index 0000000000..563387da58 --- /dev/null +++ b/Examples/FirstSteps/NewModule/autoload/IO/mitkExampleDataStructureReaderService.h @@ -0,0 +1,53 @@ +/*=================================================================== + +The Medical Imaging Interaction Toolkit (MITK) + +Copyright (c) German Cancer Research Center, +Division of Medical and Biological Informatics. +All rights reserved. + +This software is distributed WITHOUT ANY WARRANTY; without +even the implied warranty of MERCHANTABILITY or FITNESS FOR +A PARTICULAR PURPOSE. + +See LICENSE.txt or http://www.mitk.org for details. + +===================================================================*/ + +#ifndef __mitkExampleDataStructureReaderService_h +#define __mitkExampleDataStructureReaderService_h + +#include "mitkCommon.h" +#include +#include +#include +#include "mitkExampleDataStructure.h" + +namespace mitk +{ + + /** \brief The reader service for the MITK example data type + */ + + class ExampleDataStructureReaderService : public mitk::AbstractFileReader + { + public: + + typedef mitk::ExampleDataStructure OutputType; + + ExampleDataStructureReaderService(const ExampleDataStructureReaderService& other); + ExampleDataStructureReaderService(); + virtual ~ExampleDataStructureReaderService(); + + using AbstractFileReader::Read; + virtual std::vector > Read() override; + + private: + ExampleDataStructureReaderService* Clone() const override; + + us::ServiceRegistration m_ServiceReg; + }; + +} //namespace MITK + +#endif // __mitkExampleDataStructureReaderService_h diff --git a/Examples/FirstSteps/NewModule/autoload/IO/mitkExampleDataStructureSerializer.cpp b/Examples/FirstSteps/NewModule/autoload/IO/mitkExampleDataStructureSerializer.cpp new file mode 100644 index 0000000000..3853425192 --- /dev/null +++ b/Examples/FirstSteps/NewModule/autoload/IO/mitkExampleDataStructureSerializer.cpp @@ -0,0 +1,71 @@ +/*=================================================================== + +The Medical Imaging Interaction Toolkit (MITK) + +Copyright (c) German Cancer Research Center, +Division of Medical and Biological Informatics. +All rights reserved. + +This software is distributed WITHOUT ANY WARRANTY; without +even the implied warranty of MERCHANTABILITY or FITNESS FOR +A PARTICULAR PURPOSE. + +See LICENSE.txt or http://www.mitk.org for details. + +===================================================================*/ + +#include "mitkExampleDataStructureSerializer.h" +#include "mitkExampleDataStructure.h" +#include "mitkIOUtil.h" + +#include + + +MITK_REGISTER_SERIALIZER(ExampleDataStructureSerializer) + + +mitk::ExampleDataStructureSerializer::ExampleDataStructureSerializer() +{ +} + + +mitk::ExampleDataStructureSerializer::~ExampleDataStructureSerializer() +{ +} + + +std::string mitk::ExampleDataStructureSerializer::Serialize() +{ + const ExampleDataStructure* exData = dynamic_cast( m_Data.GetPointer() ); + if (exData == NULL) + { + MITK_ERROR << " Object at " << (const void*) this->m_Data + << " is not an mitk::ExampleDataStructure. Cannot serialize as ExampleDataStructure."; + return ""; + } + + std::string filename( this->GetUniqueFilenameInWorkingDirectory() ); + filename += "_"; + filename += m_FilenameHint; + filename += ".txt"; + + std::string fullname(m_WorkingDirectory); + fullname += "/"; + fullname += itksys::SystemTools::ConvertToOutputPath(filename.c_str()); + + try + { + mitk::IOUtil::Save(exData, fullname); + } + catch (std::exception& e) + { + MITK_ERROR << " Error serializing object at " << (const void*) this->m_Data + << " to " + << fullname + << ": " + << e.what(); + return ""; + } + return filename; +} + diff --git a/Examples/FirstSteps/NewModule/autoload/IO/mitkExampleDataStructureSerializer.h b/Examples/FirstSteps/NewModule/autoload/IO/mitkExampleDataStructureSerializer.h new file mode 100644 index 0000000000..1e76b67071 --- /dev/null +++ b/Examples/FirstSteps/NewModule/autoload/IO/mitkExampleDataStructureSerializer.h @@ -0,0 +1,39 @@ +/*=================================================================== + +The Medical Imaging Interaction Toolkit (MITK) + +Copyright (c) German Cancer Research Center, +Division of Medical and Biological Informatics. +All rights reserved. + +This software is distributed WITHOUT ANY WARRANTY; without +even the implied warranty of MERCHANTABILITY or FITNESS FOR +A PARTICULAR PURPOSE. + +See LICENSE.txt or http://www.mitk.org for details. + +===================================================================*/ + +#ifndef mitkExampleDataStructureSerializer_h_included +#define mitkExampleDataStructureSerializer_h_included + +#include "mitkBaseDataSerializer.h" + +namespace mitk +{ +/** + \brief Serializes mitk::ExampleDataStructure for mitk::SceneIO +*/ +class ExampleDataStructureSerializer : public BaseDataSerializer +{ + public: + mitkClassMacro( ExampleDataStructureSerializer, BaseDataSerializer ); + itkFactorylessNewMacro(Self) + itkCloneMacro(Self) + virtual std::string Serialize() override; + protected: + ExampleDataStructureSerializer(); + virtual ~ExampleDataStructureSerializer(); +}; +} // namespace +#endif diff --git a/Examples/FirstSteps/NewModule/autoload/IO/mitkExampleDataStructureWriterService.cpp b/Examples/FirstSteps/NewModule/autoload/IO/mitkExampleDataStructureWriterService.cpp new file mode 100644 index 0000000000..a42c23fd86 --- /dev/null +++ b/Examples/FirstSteps/NewModule/autoload/IO/mitkExampleDataStructureWriterService.cpp @@ -0,0 +1,95 @@ +/*=================================================================== + +The Medical Imaging Interaction Toolkit (MITK) + +Copyright (c) German Cancer Research Center, +Division of Medical and Biological Informatics. +All rights reserved. + +This software is distributed WITHOUT ANY WARRANTY; without +even the implied warranty of MERCHANTABILITY or FITNESS FOR +A PARTICULAR PURPOSE. + +See LICENSE.txt or http://www.mitk.org for details. + +===================================================================*/ + +#include "mitkExampleDataStructureWriterService.h" + +// itk includes +#include "itksys/SystemTools.hxx" + +// mitk includes +#include "mitkExampleIOMimeTypes.h" + +mitk::ExampleDataStructureWriterService::ExampleDataStructureWriterService() + : AbstractFileWriter(mitk::ExampleDataStructure::GetStaticNameOfClass(), CustomMimeType(mitk::ExampleIOMimeTypes::EXAMPLE_MIMETYPE()), "Default writer for the example data structure") +{ + RegisterService(); +} + +mitk::ExampleDataStructureWriterService::ExampleDataStructureWriterService(const mitk::ExampleDataStructureWriterService& other) + : AbstractFileWriter(other) +{ +} + + +mitk::ExampleDataStructureWriterService::~ExampleDataStructureWriterService() +{} + +mitk::ExampleDataStructureWriterService* mitk::ExampleDataStructureWriterService::Clone() const +{ + return new ExampleDataStructureWriterService(*this); +} + +void mitk::ExampleDataStructureWriterService::Write() +{ + MITK_INFO << "Writing ExampleDataStructure"; + InputType::ConstPointer input = dynamic_cast(this->GetInput()); + if (input.IsNull() ) + { + MITK_ERROR <<"Sorry, input to ExampleDataStructureWriterService is NULL!"; + return; + } + if ( this->GetOutputLocation().empty() ) + { + MITK_ERROR << "Sorry, filename has not been set!" ; + return ; + } + + std::string ext = itksys::SystemTools::GetFilenameLastExtension(this->GetOutputLocation()); + ext = itksys::SystemTools::LowerCase(ext); + + // default extension is .txt + if(ext == "") + { + ext = ".txt"; + this->SetOutputLocation(this->GetOutputLocation() + ext); + } + + try + { + std::ofstream file(this->GetOutputLocation()); + + if ( file.is_open() ) + { + file << input->GetData(); + } + else + { + mitkThrow() << "Could not open file " << this->GetOutputLocation() << " for writing."; + } + + MITK_INFO << "Example Data Structure has been written"; + } + + catch (mitk::Exception e) + { + MITK_ERROR << e.GetDescription(); + } + catch (...) + { + MITK_ERROR << "Unknown error occurred while trying to write file."; + } + +} diff --git a/Examples/FirstSteps/NewModule/autoload/IO/mitkExampleDataStructureWriterService.h b/Examples/FirstSteps/NewModule/autoload/IO/mitkExampleDataStructureWriterService.h new file mode 100644 index 0000000000..1e571c2b21 --- /dev/null +++ b/Examples/FirstSteps/NewModule/autoload/IO/mitkExampleDataStructureWriterService.h @@ -0,0 +1,51 @@ +/*=================================================================== + +The Medical Imaging Interaction Toolkit (MITK) + +Copyright (c) German Cancer Research Center, +Division of Medical and Biological Informatics. +All rights reserved. + +This software is distributed WITHOUT ANY WARRANTY; without +even the implied warranty of MERCHANTABILITY or FITNESS FOR +A PARTICULAR PURPOSE. + +See LICENSE.txt or http://www.mitk.org for details. + +===================================================================*/ + +#ifndef __mitkExampleDataStructureWriterService_h +#define __mitkExampleDataStructureWriterService_h + +#include +#include + +namespace mitk +{ + + /** + * Writes example data strucutres to a file + * @ingroup Process + */ + class ExampleDataStructureWriterService : public mitk::AbstractFileWriter + { + public: + typedef mitk::ExampleDataStructure InputType; + + ExampleDataStructureWriterService(); + virtual ~ExampleDataStructureWriterService(); + + using AbstractFileWriter::Write; + virtual void Write() override; + + protected: + + ExampleDataStructureWriterService(const ExampleDataStructureWriterService& other); + virtual mitk::ExampleDataStructureWriterService* Clone() const override; + + }; + + +} // end of namespace mitk + +#endif //__mitkExampleDataStructureWriterService_h diff --git a/Examples/FirstSteps/NewModule/autoload/IO/mitkExampleIOMimeTypes.cpp b/Examples/FirstSteps/NewModule/autoload/IO/mitkExampleIOMimeTypes.cpp new file mode 100644 index 0000000000..7ac4701984 --- /dev/null +++ b/Examples/FirstSteps/NewModule/autoload/IO/mitkExampleIOMimeTypes.cpp @@ -0,0 +1,116 @@ +/*=================================================================== + +The Medical Imaging Interaction Toolkit (MITK) + +Copyright (c) German Cancer Research Center, +Division of Medical and Biological Informatics. +All rights reserved. + +This software is distributed WITHOUT ANY WARRANTY; without +even the implied warranty of MERCHANTABILITY or FITNESS FOR +A PARTICULAR PURPOSE. + +See LICENSE.txt or http://www.mitk.org for details. + +===================================================================*/ + +#include "mitkExampleIOMimeTypes.h" +#include "mitkIOMimeTypes.h" +#include +#include + +namespace mitk +{ + +std::vector ExampleIOMimeTypes::Get() +{ + std::vector mimeTypes; + + // order matters here (descending rank for mime types) + + mimeTypes.push_back(EXAMPLE_MIMETYPE().Clone()); + mimeTypes.push_back(EXAMPLE_TWO_MIMETYPE().Clone()); + + return mimeTypes; +} + +// Mime Types + +ExampleIOMimeTypes::ExampleDataStructureMimeType::ExampleDataStructureMimeType() + : CustomMimeType(EXAMPLE_MIMETYPE_NAME()) +{ + std::string category = "Example and Tutorial Data"; + this->SetCategory(category); + this->SetComment("Example data structure containing just a string"); + + this->AddExtension("example"); + this->AddExtension("txt"); +} + +bool ExampleIOMimeTypes::ExampleDataStructureMimeType::AppliesTo(const std::string &path) const +{ + bool canRead( CustomMimeType::AppliesTo(path) ); + + // fix for bug 18572 + // Currently this function is called for writing as well as reading, in that case + // the image information can of course not be read + // This is a bug, this function should only be called for reading. + if( ! itksys::SystemTools::FileExists( path.c_str() ) ) + { + return canRead; + } + //end fix for bug 18572 + + std::string ext = this->GetExtension( path ); + ext = itksys::SystemTools::LowerCase( ext ); + + if( ext == ".txt" ) + { + // we could test something here + if( false ) + { + MITK_WARN << "Can not read .txt file as ExampleDataStructure"; + canRead = false; + } + } + + return canRead; +} + +ExampleIOMimeTypes::ExampleDataStructureMimeType* ExampleIOMimeTypes::ExampleDataStructureMimeType::Clone() const +{ + return new ExampleDataStructureMimeType(*this); +} + + +ExampleIOMimeTypes::ExampleDataStructureMimeType ExampleIOMimeTypes::EXAMPLE_MIMETYPE() +{ + return ExampleDataStructureMimeType(); +} + +CustomMimeType ExampleIOMimeTypes::EXAMPLE_TWO_MIMETYPE() +{ + CustomMimeType mimeType(EXAMPLE_TWO_MIMETYPE_NAME()); + std::string category = "Example and Tutorial Data"; + mimeType.SetComment("Simpler Example Data MimeType"); + mimeType.SetCategory(category); + mimeType.AddExtension("example2"); + return mimeType; +} + +// Names +std::string ExampleIOMimeTypes::EXAMPLE_MIMETYPE_NAME() +{ + // create a unique and sensible name for this mime type + static std::string name = IOMimeTypes::DEFAULT_BASE_NAME() + ".examples.example-data-structure"; + return name; +} + +std::string ExampleIOMimeTypes::EXAMPLE_TWO_MIMETYPE_NAME() +{ + // create a unique and sensible name for this mime type + static std::string name = IOMimeTypes::DEFAULT_BASE_NAME() + ".examples.example-data-structure-the-second"; + return name; +} + +} diff --git a/Examples/FirstSteps/NewModule/autoload/IO/mitkExampleIOMimeTypes.h b/Examples/FirstSteps/NewModule/autoload/IO/mitkExampleIOMimeTypes.h new file mode 100644 index 0000000000..f24c89bd33 --- /dev/null +++ b/Examples/FirstSteps/NewModule/autoload/IO/mitkExampleIOMimeTypes.h @@ -0,0 +1,58 @@ +/*=================================================================== + +The Medical Imaging Interaction Toolkit (MITK) + +Copyright (c) German Cancer Research Center, +Division of Medical and Biological Informatics. +All rights reserved. + +This software is distributed WITHOUT ANY WARRANTY; without +even the implied warranty of MERCHANTABILITY or FITNESS FOR +A PARTICULAR PURPOSE. + +See LICENSE.txt or http://www.mitk.org for details. + +===================================================================*/ + +#ifndef MITKEXAMPLEIOMIMETYPES_H +#define MITKEXAMPLEIOMIMETYPES_H + +#include "mitkCustomMimeType.h" + +#include + +namespace mitk { + +class ExampleIOMimeTypes +{ +public: + + // Deriving your own MimeType will probably be overkill in most situations. + class ExampleDataStructureMimeType : public CustomMimeType + { + public: + ExampleDataStructureMimeType(); + virtual bool AppliesTo(const std::string &path) const override; + virtual ExampleDataStructureMimeType* Clone() const override; + }; + + static ExampleDataStructureMimeType EXAMPLE_MIMETYPE(); + static std::string EXAMPLE_MIMETYPE_NAME(); + + // Simpler method of creating a new MimeType + static CustomMimeType EXAMPLE_TWO_MIMETYPE(); + static std::string EXAMPLE_TWO_MIMETYPE_NAME(); + + // Get all example Mime Types + static std::vector Get(); + +private: + + // purposely not implemented + ExampleIOMimeTypes(); + ExampleIOMimeTypes(const ExampleIOMimeTypes&); +}; + +} + +#endif // MITKEXAMPLEIOMIMETYPES_H diff --git a/Examples/FirstSteps/NewModule/autoload/IO/mitkNewModuleIOActivator.cpp b/Examples/FirstSteps/NewModule/autoload/IO/mitkNewModuleIOActivator.cpp new file mode 100644 index 0000000000..f8093aeaa2 --- /dev/null +++ b/Examples/FirstSteps/NewModule/autoload/IO/mitkNewModuleIOActivator.cpp @@ -0,0 +1,77 @@ +/*=================================================================== + +The Medical Imaging Interaction Toolkit (MITK) + +Copyright (c) German Cancer Research Center, +Division of Medical and Biological Informatics. +All rights reserved. + +This software is distributed WITHOUT ANY WARRANTY; without +even the implied warranty of MERCHANTABILITY or FITNESS FOR +A PARTICULAR PURPOSE. + +See LICENSE.txt or http://www.mitk.org for details. + +===================================================================*/ +#include +#include + +#include + +#include + +#include "mitkExampleIOMimeTypes.h" + +namespace mitk +{ + /** + \brief Registers services for example module. + */ + class NewModuleIOActivator : public us::ModuleActivator + { + public: + + void Load(us::ModuleContext* context) override + { + // We can register our read/write services with a custom service ranking + // services with a higher ranking are prioritized, default us 0 + us::ServiceProperties props; + props[ us::ServiceConstants::SERVICE_RANKING() ] = 10; + + m_MimeTypes = mitk::ExampleIOMimeTypes::Get(); + for (std::vector::const_iterator mimeTypeIter = m_MimeTypes.begin(), + iterEnd = m_MimeTypes.end(); mimeTypeIter != iterEnd; ++mimeTypeIter) + { + context->RegisterService(*mimeTypeIter, props); + } + + m_ExampleDataStructureReaderService = new ExampleDataStructureReaderService(); + + m_ExampleDataStructureWriterService = new ExampleDataStructureWriterService(); + } + + void Unload(us::ModuleContext*) override + { + for (unsigned int loop(0); loop < m_MimeTypes.size(); ++loop) + { + delete m_MimeTypes.at(loop); + } + + delete m_ExampleDataStructureReaderService; + + delete m_ExampleDataStructureWriterService; + + } + + private: + + ExampleDataStructureReaderService * m_ExampleDataStructureReaderService; + + ExampleDataStructureWriterService * m_ExampleDataStructureWriterService; + + std::vector m_MimeTypes; + + }; +} + +US_EXPORT_MODULE_ACTIVATOR(mitk::NewModuleIOActivator) diff --git a/Examples/FirstSteps/NewModule/doc/Readme.txt b/Examples/FirstSteps/NewModule/doc/Readme.txt new file mode 100644 index 0000000000..0a9980df7e --- /dev/null +++ b/Examples/FirstSteps/NewModule/doc/Readme.txt @@ -0,0 +1 @@ +This directory should contain documentation for your module. \ No newline at end of file diff --git a/Examples/FirstSteps/NewModule/files.cmake b/Examples/FirstSteps/NewModule/files.cmake new file mode 100644 index 0000000000..7bf639d033 --- /dev/null +++ b/Examples/FirstSteps/NewModule/files.cmake @@ -0,0 +1,6 @@ +file(GLOB_RECURSE H_FILES RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}/include/*") + +set(CPP_FILES + # DataStructures + DataManagement/mitkExampleDataStructure.cpp +) diff --git a/Examples/FirstSteps/NewModule/include/mitkExampleDataStructure.h b/Examples/FirstSteps/NewModule/include/mitkExampleDataStructure.h new file mode 100644 index 0000000000..aee319fe34 --- /dev/null +++ b/Examples/FirstSteps/NewModule/include/mitkExampleDataStructure.h @@ -0,0 +1,87 @@ + +/*=================================================================== + +The Medical Imaging Interaction Toolkit (MITK) + +Copyright (c) German Cancer Research Center, +Division of Medical and Biological Informatics. +All rights reserved. + +This software is distributed WITHOUT ANY WARRANTY; without +even the implied warranty of MERCHANTABILITY or FITNESS FOR +A PARTICULAR PURPOSE. + +See LICENSE.txt or http://www.mitk.org for details. + +===================================================================*/ + +#ifndef _MITK_ExampleDataStructure_H +#define _MITK_ExampleDataStructure_H + +#include + +#include "mitkBaseData.h" + +namespace mitk +{ +/** +* \brief Example Data Structure +* +* This class is an example of deriving your own data structure based on mitk::BaseData . +*/ +class MITKNEWMODULE_EXPORT ExampleDataStructure : public BaseData +{ +public: + // virtual methods that need to be implemented + virtual void UpdateOutputInformation() override; + virtual void SetRequestedRegionToLargestPossibleRegion() override; + virtual bool RequestedRegionIsOutsideOfTheBufferedRegion() override; + virtual bool VerifyRequestedRegion() override; + virtual void SetRequestedRegion( const itk::DataObject* ) override; + + // Macros + mitkClassMacro( ExampleDataStructure, BaseData ); + itkFactorylessNewMacro( Self ) itkCloneMacro( Self ) + + ////////////////// Interface /////////////////// + + // Get macros + itkGetMacro( Data, std::string ); + itkGetConstMacro( Data, std::string ); + + // Set macros + itkSetMacro( Data, std::string ); + + /** + * \brief Append a string to the data string + * + * Takes a string that is appended to the data string. + * + * \param input string to be appended + */ + void AppendAString( const std::string input ); + +protected: + ExampleDataStructure(); + virtual ~ExampleDataStructure(); + + // this string is the data stored in this example data structure + std::string m_Data; + +private: +}; + +/** +* \brief Returns true if the example data structures are considered equal. +* +* They are considered equal if their string is equal +*/ + +MITKNEWMODULE_EXPORT bool Equal( mitk::ExampleDataStructure* leftHandSide, + mitk::ExampleDataStructure* rightHandSide, + mitk::ScalarType eps, + bool verbose ); + +} // namespace mitk + +#endif /* _MITK_ExampleDataStructure_H */ diff --git a/Examples/FirstSteps/NewModule/resource/Readme.txt b/Examples/FirstSteps/NewModule/resource/Readme.txt new file mode 100644 index 0000000000..2643a3523e --- /dev/null +++ b/Examples/FirstSteps/NewModule/resource/Readme.txt @@ -0,0 +1 @@ +This directory should contain resources, such as images, presets, xmls or similar. \ No newline at end of file diff --git a/Examples/FirstSteps/NewModule/src/DataManagement/mitkExampleDataStructure.cpp b/Examples/FirstSteps/NewModule/src/DataManagement/mitkExampleDataStructure.cpp new file mode 100644 index 0000000000..f165242fc0 --- /dev/null +++ b/Examples/FirstSteps/NewModule/src/DataManagement/mitkExampleDataStructure.cpp @@ -0,0 +1,98 @@ + +/*=================================================================== + +The Medical Imaging Interaction Toolkit (MITK) + +Copyright (c) German Cancer Research Center, +Division of Medical and Biological Informatics. +All rights reserved. + +This software is distributed WITHOUT ANY WARRANTY; without +even the implied warranty of MERCHANTABILITY or FITNESS FOR +A PARTICULAR PURPOSE. + +See LICENSE.txt or http://www.mitk.org for details. + +===================================================================*/ + +#include "mitkExampleDataStructure.h" +#include "mitkGeometry3D.h" + +// implementation of virtual methods + +void mitk::ExampleDataStructure::UpdateOutputInformation() +{ + +} + +void mitk::ExampleDataStructure::SetRequestedRegionToLargestPossibleRegion() +{ + +} + +bool mitk::ExampleDataStructure::RequestedRegionIsOutsideOfTheBufferedRegion() +{ + return false; +} + +bool mitk::ExampleDataStructure::VerifyRequestedRegion() +{ + return true; +} + +void mitk::ExampleDataStructure::SetRequestedRegion(const itk::DataObject *) +{ + +} + +/* Constructor and Destructor */ +mitk::ExampleDataStructure::ExampleDataStructure() +: m_Data("Initialized") +{ + this->SetGeometry(mitk::Geometry3D::New()); +} + +mitk::ExampleDataStructure::~ExampleDataStructure() +{ +} + +void mitk::ExampleDataStructure::AppendAString(const std::string input) +{ + m_Data.append( input ); +} + + +bool mitk::Equal( mitk::ExampleDataStructure* leftHandSide, mitk::ExampleDataStructure* rightHandSide, mitk::ScalarType eps, bool verbose ) +{ + bool noDifferenceFound = true; + + if( rightHandSide == NULL ) + { + if(verbose) + { + MITK_INFO << "[Equal( ExampleDataStructure*, ExampleDataStructure* )] rightHandSide NULL."; + } + return false; + } + + if( leftHandSide == NULL ) + { + if(verbose) + { + MITK_INFO << "[Equal( ExampleDataStructure*, ExampleDataStructure* )] leftHandSide NULL."; + } + return false; + } + + if (!(leftHandSide->GetData() == rightHandSide->GetData()) ) + { + if(verbose) + { + MITK_INFO << "[Equal( ExampleDataStructure*, ExampleDataStructure* )] Data not equal. "; + MITK_INFO << leftHandSide->GetData() << " != " << rightHandSide->GetData(); + } + noDifferenceFound = false; + } + + return noDifferenceFound; +} diff --git a/Examples/FirstSteps/NewModule/test/CMakeLists.txt b/Examples/FirstSteps/NewModule/test/CMakeLists.txt new file mode 100644 index 0000000000..153cd81e2e --- /dev/null +++ b/Examples/FirstSteps/NewModule/test/CMakeLists.txt @@ -0,0 +1 @@ +MITK_CREATE_MODULE_TESTS() diff --git a/Examples/FirstSteps/NewModule/test/files.cmake b/Examples/FirstSteps/NewModule/test/files.cmake new file mode 100644 index 0000000000..f365da9ce1 --- /dev/null +++ b/Examples/FirstSteps/NewModule/test/files.cmake @@ -0,0 +1,7 @@ +set(MODULE_TESTS + mitkExampleDataStructureReaderWriterTest.cpp + mitkExampleDataStructureTest.cpp +) + +SET(MODULE_CUSTOM_TESTS +) diff --git a/Examples/FirstSteps/NewModule/test/mitkExampleDataStructureReaderWriterTest.cpp b/Examples/FirstSteps/NewModule/test/mitkExampleDataStructureReaderWriterTest.cpp new file mode 100644 index 0000000000..782d7fa41d --- /dev/null +++ b/Examples/FirstSteps/NewModule/test/mitkExampleDataStructureReaderWriterTest.cpp @@ -0,0 +1,74 @@ +/*=================================================================== + +The Medical Imaging Interaction Toolkit (MITK) + +Copyright (c) German Cancer Research Center, +Division of Medical and Biological Informatics. +All rights reserved. + +This software is distributed WITHOUT ANY WARRANTY; without +even the implied warranty of MERCHANTABILITY or FITNESS FOR +A PARTICULAR PURPOSE. + +See LICENSE.txt or http://www.mitk.org for details. + +===================================================================*/ + +// Testing +#include "mitkTestingMacros.h" +#include "mitkTestFixture.h" + +// std includes +#include + +// MITK includes +#include "mitkExampleDataStructure.h" +#include "mitkIOUtil.h" + +// VTK includes +#include + +class mitkExampleDataStructureReaderWriterTestSuite : public mitk::TestFixture +{ + CPPUNIT_TEST_SUITE(mitkExampleDataStructureReaderWriterTestSuite); + + // Test saving/loading + MITK_TEST(ExampleDataInOutput); + CPPUNIT_TEST_SUITE_END(); + +private: + + mitk::ExampleDataStructure::Pointer m_Data; + std::string m_DefaultDataString; + +public: + + /** + * @brief Setup Always call this method before each Test-case to ensure correct and new intialization of the used members for a new test case. (If the members are not used in a test, the method does not need to be called). + */ + void setUp() override + { + m_DefaultDataString = "This is the example data content\nAnd a second line\n"; + m_Data = mitk::ExampleDataStructure::New(); + m_Data->SetData(m_DefaultDataString); + } + + void tearDown() override + { + m_DefaultDataString = ""; + m_Data = nullptr; + } + + void ExampleDataInOutput() + { + std::string path = mitk::IOUtil::GetTempPath() + "ExampleDataOutput.txt"; + mitk::IOUtil::SaveBaseData(m_Data, path); + mitk::ExampleDataStructure::Pointer loadedData = dynamic_cast(mitk::IOUtil::LoadBaseData(path).GetPointer()); + + itksys::SystemTools::RemoveFile(path); + + CPPUNIT_ASSERT_MESSAGE("Comparing created and loaded example data.", mitk::Equal(m_Data, loadedData, mitk::eps, true)); + } +}; + +MITK_TEST_SUITE_REGISTRATION(mitkExampleDataStructureReaderWriter) diff --git a/Examples/FirstSteps/NewModule/test/mitkExampleDataStructureTest.cpp b/Examples/FirstSteps/NewModule/test/mitkExampleDataStructureTest.cpp new file mode 100644 index 0000000000..dd18227ada --- /dev/null +++ b/Examples/FirstSteps/NewModule/test/mitkExampleDataStructureTest.cpp @@ -0,0 +1,71 @@ +/*=================================================================== + +The Medical Imaging Interaction Toolkit (MITK) + +Copyright (c) German Cancer Research Center, +Division of Medical and Biological Informatics. +All rights reserved. + +This software is distributed WITHOUT ANY WARRANTY; without +even the implied warranty of MERCHANTABILITY or FITNESS FOR +A PARTICULAR PURPOSE. + +See LICENSE.txt or http://www.mitk.org for details. + +===================================================================*/ + +// Testing +#include "mitkTestingMacros.h" +#include "mitkTestFixture.h" + +// std includes +#include + +// MITK includes +#include "mitkExampleDataStructure.h" + +// VTK includes +#include + +class mitkExampleDataStructureTestSuite : public mitk::TestFixture +{ + CPPUNIT_TEST_SUITE(mitkExampleDataStructureTestSuite); + + // Test the append method + MITK_TEST(ExampleDataAppend); + CPPUNIT_TEST_SUITE_END(); + +private: + + mitk::ExampleDataStructure::Pointer m_Data; + std::string m_DefaultDataString; + +public: + + /** + * @brief Setup Always call this method before each Test-case to ensure correct and new intialization of the used members for a new test case. (If the members are not used in a test, the method does not need to be called). + */ + void setUp() override + { + m_DefaultDataString = "This is the example data content\nAnd a second line\n"; + m_Data = mitk::ExampleDataStructure::New(); + m_Data->SetData(m_DefaultDataString); + } + + void tearDown() override + { + m_DefaultDataString = ""; + m_Data = nullptr; + } + + void ExampleDataAppend() + { + std::string appendedString = "And a third line\n"; + std::string wholeString = m_DefaultDataString + appendedString; + m_Data->AppendAString(appendedString); + + CPPUNIT_ASSERT_MESSAGE("Checking whether string was correctly appended.", m_Data->GetData() == wholeString); + } +}; + +MITK_TEST_SUITE_REGISTRATION(mitkExampleDataStructure)