Medical Imaging Interaction Toolkit  2024.06.00
Medical Imaging Interaction Toolkit
How to create a new custom data type

Prerequisites and further reading

We will use some concepts during this tutorial which we assume you are aware of, as well as taking advantage of infrastructure which needs to be set up. The following is a list of prerequisites which should be present to effectively use this tutorial.

Some concepts will only be briefly touched upon in this tutorial, for a more concise presentation of these concepts please refer to the following further reading.

Creating a new data type

A new data type needs to derive from mitk::BaseData in order to be handled by the mitk::DataStorage via mitk::DataNode. An example of a very simple data type is provided in the example module. This type encapsulates a string.

/*============================================================================
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.
============================================================================*/
#ifndef mitkExampleDataStructure_h
#define mitkExampleDataStructure_h
#include <MitkNewModuleExports.h>
#include "mitkBaseData.h"
namespace mitk
{
class MITKNEWMODULE_EXPORT ExampleDataStructure : public BaseData
{
public:
// virtual methods that need to be implemented
void UpdateOutputInformation() override;
void SetRequestedRegionToLargestPossibleRegion() override;
bool RequestedRegionIsOutsideOfTheBufferedRegion() override;
bool VerifyRequestedRegion() override;
void SetRequestedRegion(const itk::DataObject *) override;
// Macros
mitkClassMacro(ExampleDataStructure, BaseData);
itkFactorylessNewMacro(Self);
itkCloneMacro(Self);
// Get macros
itkGetMacro(Data, std::string);
itkGetConstMacro(Data, std::string);
// Set macros
itkSetMacro(Data, std::string);
void AppendAString(const std::string input);
protected:
ExampleDataStructure();
~ExampleDataStructure() override;
// this string is the data stored in this example data structure
std::string m_Data;
private:
};
MITKNEWMODULE_EXPORT bool Equal(mitk::ExampleDataStructure *leftHandSide,
bool verbose);
} // namespace mitk
#endif

Overloading mitk::Equal to work with your data type will enable you to write simpler, standardized tests.

Adding readers and writers

In order for your data type to be read from and written to disk, you need to implement readers and writers. In order for your readers/writers to be registered and available even if your module has not been loaded (usually after just starting the application), it is advisable to implement them separately in a autoload module. The default location for this is "YourModuleName/autoload/IO".

More information regarding implementing IO and MimeTypes is available at Reader and Writer. An example MimeType is implemented for the example data structure.

/*============================================================================
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.
============================================================================*/
#ifndef mitkExampleIOMimeTypes_h
#define mitkExampleIOMimeTypes_h
#include <string>
namespace mitk
{
class ExampleIOMimeTypes
{
public:
// Deriving your own MimeType will probably be overkill in most situations.
class ExampleDataStructureMimeType : public CustomMimeType
{
public:
bool AppliesTo(const std::string &path) 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<CustomMimeType *> Get();
private:
// purposely not implemented
ExampleIOMimeTypes();
ExampleIOMimeTypes(const ExampleIOMimeTypes &);
};
}
#endif
Note
{ You do not need to create your own class to manage your MimeTypes. Instead they can be defined within the Reader/Writer. }

Adding a mapper

If your data type needs a special way to render its data for the user, you need to implement a new mapper. More information can be found at Rendering Concept.

If you meet any difficulties during this How-To, don't hesitate to ask on the MITK mailing list mitk-.nosp@m.user.nosp@m.s@lis.nosp@m.ts.s.nosp@m.ource.nosp@m.forg.nosp@m.e.net! People there are kind and will try to help you. If you notice that there is an error, or that the code has changed so the way shown here is no longer correct, please open a bug report at https://phabricator.mitk.org/maniphest/ .

mitk::ExampleDataStructure
Example Data Structure.
Definition: mitkExampleDataStructure.h:27
mitk::eps
const MITKCORE_EXPORT ScalarType eps
mitk::ExampleIOMimeTypes::ExampleDataStructureMimeType::AppliesTo
bool AppliesTo(const std::string &path) const override
Checks if the MimeType can handle file at the given location.
mitkCustomMimeType.h
mitk::Equal
MITKNEWMODULE_EXPORT bool Equal(mitk::ExampleDataStructure *leftHandSide, mitk::ExampleDataStructure *rightHandSide, mitk::ScalarType eps, bool verbose)
Returns true if the example data structures are considered equal.
mitk::ExampleIOMimeTypes::EXAMPLE_MIMETYPE_NAME
static std::string EXAMPLE_MIMETYPE_NAME()
mitk
Find image slices visible on a given plane.
Definition: RenderingTests.dox:1
mitk::ExampleIOMimeTypes::ExampleDataStructureMimeType::Clone
ExampleDataStructureMimeType * Clone() const override
mitk::ExampleIOMimeTypes::EXAMPLE_TWO_MIMETYPE
static CustomMimeType EXAMPLE_TWO_MIMETYPE()
mitk::ExampleIOMimeTypes::ExampleDataStructureMimeType::ExampleDataStructureMimeType
ExampleDataStructureMimeType()
mitk::ExampleIOMimeTypes::EXAMPLE_TWO_MIMETYPE_NAME
static std::string EXAMPLE_TWO_MIMETYPE_NAME()
mitk::ExampleIOMimeTypes::Get
static std::vector< CustomMimeType * > Get()
mitk::ExampleIOMimeTypes::EXAMPLE_MIMETYPE
static ExampleDataStructureMimeType EXAMPLE_MIMETYPE()
mitkBaseData.h
mitkClassMacro
#define mitkClassMacro(className, SuperClassName)
Definition: mitkCommon.h:36
mitk::ScalarType
double ScalarType
Definition: mitkNumericConstants.h:20