Medical Imaging Interaction Toolkit  2023.04.00
Medical Imaging Interaction Toolkit
How to create a new MITK Module

Create a Folder for your Module

First, create a folder for your module within /Modules e.g. 'NewModule'. You need to add the new folder to the ModuleList.cmake file in the Module directory as well. Open /Modules/ModuleList.cmake, append the folder to the MITK_MODULES list.

set(MITK_MODULES
...
NewModule
)

A simple example module is provided in the MITK/Examples/FirstSteps/NewModule directory, it includes a new data type (more information at How to create a new custom data type) and adds a MiniApp for that data type (more information at How to create a MiniApp command line tool).

Within the module we recommend using a Unix like directory structure. This helps others finding their way around your code. Depending on your use case you might not need every directory.

NewModule/
autoload/
cmdapps/
doc/
include/
resource/
src/

Subsequently a quick description of what each directory contains.

autoload

This directory should not directly contain files. Instead it contains further directories each of which is its own module.

These modules provide functionality which should be available at application start, but will not be included by other modules. Examples would be a module registering readers/writers or providing an interface for specific hardware.

For an example of an autoload module take a look at NewModule/autoload/IO.

cmdapps

This directory contains all cmdapps (command line tools) related to your module. For more information see How to create a MiniApp command line tool.

doc

This directory contains the documentation for your module.

include

This directory contains all header files which might be included by other modules.

resource

This directory contains resources needed by your module, such as xmls, images or similar.

src

This directory contains source and header files which should not be included by other modules. Further subdivision can help keeping it organized. (e.g. src/DataManagement src/Algorithms)

test

This directory contains all tests for your module.

Create CMakeLists.txt

Within your module create a CMakeLists.txt using the MITK_CREATE_MODULE macro:

An example:

MITK_CREATE_MODULE(
INCLUDE_DIRS
PUBLIC ${MITK_BINARY_DIR}
PRIVATE src/DataManagement
DEPENDS PUBLIC MitkCore
)
add_subdirectory(autoload/IO)
add_subdirectory(cmdapps)
add_subdirectory(test)

If you do not choose a module name, one will be generated based on the folder name (In this case the resulting name will be MitkNewModule). This name has to be unique across the entire project in order to avoid collisions. It should only contain Letters (both upper- and lowercase), no numbers, no underscores etc.

An example of an autoload module that sets its own name is:

MITK_CREATE_MODULE( NewModuleIO
INCLUDE_DIRS
PRIVATE src/IO
DEPENDS PUBLIC MitkNewModule MitkSceneSerialization
PACKAGE_DEPENDS
PRIVATE tinyxml2
AUTOLOAD_WITH MitkCore
)

the resulting name is MitkNewModuleIO.

Note
For more information about the parameters of the new module macro see mitkFunctionCreateModule.cmake

Create files.cmake

Next, create a new file and name it files.cmake, containing the files of your module.

An example:

set(CPP_FILES
mitkExampleDataStructureReaderService.cpp
mitkExampleDataStructureReaderService.h
mitkExampleDataStructureSerializer.cpp
mitkExampleDataStructureSerializer.h
mitkExampleDataStructureWriterService.cpp
mitkExampleDataStructureWriterService.h
mitkExampleIOMimeTypes.cpp
mitkExampleIOMimeTypes.h
mitkNewModuleIOActivator.cpp
)

If you do not add a source file here it will not be compiled, unless it is included elsewhere.

Set up the Test environment

Providing tests for your code is a very good way to save yourself a lot of debugging time and ensure consistency. An example of a small test environment is provided in the NewModule example.

Again you need a CMakeLists.txt, e.g.:

MITK_CREATE_MODULE_TESTS()

a files.cmake, e.g.:

set(MODULE_TESTS
mitkExampleDataStructureReaderWriterTest.cpp
mitkExampleDataStructureTest.cpp
)
SET(MODULE_CUSTOM_TESTS
)

and an actual test, e.g.:

/*============================================================================
The Medical Imaging Interaction Toolkit (MITK)
Copyright (c) German Cancer Research Center (DKFZ)
All rights reserved.
Use of this source code is governed by a 3-clause BSD license that can be
found in the LICENSE file.
============================================================================*/
// Testing
// std includes
#include <string>
// MITK includes
// VTK includes
#include <vtkDebugLeaks.h>
class mitkExampleDataStructureTestSuite : public mitk::TestFixture
{
CPPUNIT_TEST_SUITE(mitkExampleDataStructureTestSuite);
// Test the append method
MITK_TEST(Append_ExampleString_AddsExampleStringToData);
CPPUNIT_TEST_SUITE_END();
private:
std::string m_DefaultDataString;
public:
void setUp() override
{
m_DefaultDataString = "This is the example data content\nAnd a second line\n";
m_Data->SetData(m_DefaultDataString);
}
void tearDown() override
{
m_DefaultDataString = "";
m_Data = nullptr;
}
void Append_ExampleString_AddsExampleStringToData()
{
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)

For more information regarding tests please refer to Testing.

That's it! Enjoy your new module!

mitkExampleDataStructure.h
itk::SmartPointer< Self >
MITK_TEST
#define MITK_TEST(TESTMETHOD)
Adds a test to the current test suite.
Definition: mitkTestingMacros.h:295
test
Follow Up Storage - Class to facilitate loading/accessing structured follow-up data.
Definition: testcase.h:28
mitkTestingMacros.h
mitk::TestFixture
Test fixture for parameterized tests.
Definition: mitkTestFixture.h:86
mitk::ExampleDataStructure::New
static Pointer New()
mitkTestFixture.h
tinyxml2
Definition: mitkXMLSerializable.h:20
MITK_TEST_SUITE_REGISTRATION
#define MITK_TEST_SUITE_REGISTRATION(TESTSUITE_NAME)
Registers the given test suite.
Definition: mitkTestingMacros.h:267