# commontk/ctk architecture

> Architecture diagram of the GitHub repository commontk/ctk, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-07-24). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/commontk/ctk
- Repository: https://github.com/commontk/ctk

## Overview

##### Project type and purpose
- CTK is a large C++ “meta-toolkit” / library suite for medical imaging (DICOM), plugin hosting (OSGi‐style), Qt widgets, CLI modules, scripting and visualization.
- It is not a web or full-stack app, but a monorepo containing reusable libraries (Libs/…), example Applications, Plugins/, and CMake-based build orchestration (SuperBuild.cmake, CMakeExternals).

##### File structure
- Top-level directories:
- Applications/ – sample and test apps that exercise CTK modules
- Libs/ – core libraries subdivided by domain (Core, DICOM, CommandLineModules, PluginFramework, Widgets, Visualization, etc.)
- Plugins/ – OSGi-style service bundles built atop PluginFramework and CTK services
- CMake/, CMakeExternals/ – build scripts, helpers, ExternalProject recipes
- Utilities/ – helper CMake Find modules, graphing tools
- Each Libs subfolder follows an MVC-style layering:
- Core foundation → domain-specific services (DICOM, PluginFramework, Scripting) → frontends (Qt GUI, WebKit, Python) → application examples.
- Build configuration: CMakeLists.txt at root, SuperBuild.cmake drives dependency fetch & build.

##### README insights
- CTK covers five main topics: DICOM, DICOM Application Hosting, Widgets, Plugin Framework, Command Line Interfaces.
- External dependencies: Qt5, VTK, DCMTK, PythonQt. Built via CMake ExternalProject (Artichoke).
- Emphasis on service-oriented plugin architecture and separation of frontends vs backends.

##### Main components
a. External Dependencies (third-party):
- Qt5
- VTK (ChartsCore, GUISupportQt, ViewsQt)
- DCMTK
- PythonQt
b. Build System:
- CMake SuperBuild (fetch & configure externals, orchestrate build)
c. CTK Libraries (runtime modules):
- Core – low-level utilities, threading, file I/O
- CommandLineModules – CLI backend/frontends
- PluginFramework – dynamic service registry & module loader
- Widgets – Qt widget collection (UI components, event translators)
- DICOM – PACS query/retrieve, database, thumbnail generation
- Visualization – VTK-based charts & 3D views
- Scripting – Python embedding and console
- Other domains (ImageProcessing, XNAT, QtTesting, etc.)
d. Frontends/Applications:
- Qt GUI Apps (ctkDICOMHost, ctkCommandLineModuleExplorer, ctkPluginBrowser, etc.)
- CLI test apps
- Script shells (ctkSimplePythonShell)
e. Plugins:
- Bundled OSGi plugins (org.commontk.*) that register CTK services

##### Relationships & interactions
- CTK Core underpins every other Lib; PluginFramework depends on Core, others register services there.
- Domain libraries (e.g. DICOM) depend on Core, may depend on PluginFramework & Qt.
- Widgets and Visualization depend on Core + PluginFramework + Qt + VTK.
- CommandLineModules backends depend on Core; frontends depend on backends + Qt.
- Applications consume CTK Libraries and Plugins; Plugins are loaded at runtime by PluginFramework.

##### Architectural patterns & design principles
- Layered modular architecture: Core → Domain libraries → Frontends → Apps
- Service-oriented plugin pattern (OSGi-style): dynamic discovery, registration, activation
- Separation of concerns: backends vs frontends, CLI vs GUI vs scripting
- CMake ExternalProject for dependencies, self-contained SuperBuild

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph "External Dependencies"
        ExternalDeps["CMakeExternals"]:::external
        Qt5["Qt5"]:::external
        VTK["VTK"]:::external
        DCMTK["DCMTK"]:::external
        PythonQt["PythonQt"]:::external
    end

    subgraph "Build System"
        CMakeScripts["CMake/"]:::build
        SuperBuild["SuperBuild.cmake"]:::build
    end

    subgraph "Utilities"
        CMakeUtils["Utilities/CMake"]:::utility
        GraphingUtils["Utilities/DGraph"]:::utility
    end

    subgraph "CTK Libraries"
        CoreLib["Core"]:::core
        PluginFrameworkLib["PluginFramework"]:::plugin
        subgraph "Domain Libraries"
            CLMLib["CommandLineModules"]:::core
            WidgetsLib["Widgets"]:::core
            DICOMLib["DICOM"]:::core
            VizLib["Visualization"]:::core
            ScriptingLib["Scripting"]:::core
            QtTestingLib["QtTesting"]:::core
            XNATLib["XNAT"]:::core
            ImageProcessingLib["ImageProcessing"]:::core
        end
    end

    subgraph "Frontends/Applications"
        HostApp["ctkDICOMHost"]:::frontend
        PluginBrowserApp["ctkPluginBrowser"]:::frontend
        CLMExplorerApp["ctkCommandLineModuleExplorer"]:::frontend
        CLITestApps["CLI Test Apps"]:::frontend
        PythonShellApp["ctkSimplePythonShell"]:::frontend
    end

    subgraph "Plugins"
        PluginsRoot["Plugins"]:::plugin
    end

    %% External -> Build
    ExternalDeps -->|fetches| SuperBuild
    Qt5 -->|UI toolkit| WidgetsLib
    VTK -->|rendering| VizLib
    DCMTK -->|PACS support| DICOMLib
    PythonQt -->|scripting| ScriptingLib

    %% Utilities -> Build Scripts
    CMakeUtils --> CMakeScripts
    GraphingUtils --> CMakeScripts
    CMakeScripts --> SuperBuild

    %% Build -> Libraries & Apps & Plugins
    SuperBuild --> CoreLib
    SuperBuild --> CLMLib
    SuperBuild --> PluginFrameworkLib
    SuperBuild --> WidgetsLib
    SuperBuild --> DICOMLib
    SuperBuild --> VizLib
    SuperBuild --> ScriptingLib
    SuperBuild --> QtTestingLib
    SuperBuild --> XNATLib
    SuperBuild --> ImageProcessingLib
    SuperBuild --> HostApp
    SuperBuild --> PluginBrowserApp
    SuperBuild --> CLMExplorerApp
    SuperBuild --> CLITestApps
    SuperBuild --> PythonShellApp
    SuperBuild --> PluginsRoot

    %% Core -> Domain
    CoreLib --> CLMLib
    CoreLib --> WidgetsLib
    CoreLib --> DICOMLib
    CoreLib --> VizLib
    CoreLib --> ScriptingLib
    CoreLib --> QtTestingLib
    CoreLib --> XNATLib
    CoreLib --> ImageProcessingLib
    CoreLib --> PluginFrameworkLib

    %% Domain <-> PluginFramework
    CLMLib -->|"registers"| PluginFrameworkLib
    WidgetsLib -->|"registers"| PluginFrameworkLib
    DICOMLib -->|"registers"| PluginFrameworkLib
    VizLib -->|"registers"| PluginFrameworkLib
    ScriptingLib -->|"registers"| PluginFrameworkLib
    QtTestingLib -->|"registers"| PluginFrameworkLib
    XNATLib -->|"registers"| PluginFrameworkLib
    ImageProcessingLib -->|"registers"| PluginFrameworkLib

    PluginFrameworkLib -->|"service lookup"| CLMLib
    PluginFrameworkLib -->|"service lookup"| WidgetsLib
    PluginFrameworkLib -->|"service lookup"| DICOMLib
    PluginFrameworkLib -->|"service lookup"| VizLib
    PluginFrameworkLib -->|"service lookup"| ScriptingLib
    PluginFrameworkLib -->|"service lookup"| QtTestingLib
    PluginFrameworkLib -->|"service lookup"| XNATLib
    PluginFrameworkLib -->|"service lookup"| ImageProcessingLib

    %% Applications -> Libraries & Framework
    HostApp -->|uses| DICOMLib
    HostApp -->|uses| WidgetsLib
    HostApp -->|uses| PluginFrameworkLib

    PluginBrowserApp -->|uses| WidgetsLib
    PluginBrowserApp -->|uses| PluginFrameworkLib

    CLMExplorerApp -->|uses| CLMLib
    CLMExplorerApp -->|uses| PluginFrameworkLib

    CLITestApps -->|uses| CLMLib
    CLITestApps -->|uses| PluginFrameworkLib

    PythonShellApp -->|uses| ScriptingLib
    PythonShellApp -->|uses| PluginFrameworkLib

    %% Plugins <-> Framework
    PluginsRoot -->|loaded by| PluginFrameworkLib
    PluginFrameworkLib -->|discovers| PluginsRoot

    %% Click Events
    click ExternalDeps "https://github.com/commontk/ctk/tree/master/CMakeExternals/"
    click SuperBuild "https://github.com/commontk/ctk/blob/master/SuperBuild.cmake"
    click CMakeScripts "https://github.com/commontk/ctk/tree/master/CMake/"
    click CMakeUtils "https://github.com/commontk/ctk/tree/master/Utilities/CMake/"
    click GraphingUtils "https://github.com/commontk/ctk/tree/master/Utilities/DGraph/"
    click CoreLib "https://github.com/commontk/ctk/tree/master/Libs/Core/"
    click CLMLib "https://github.com/commontk/ctk/tree/master/Libs/CommandLineModules/"
    click PluginFrameworkLib "https://github.com/commontk/ctk/tree/master/Libs/PluginFramework/"
    click WidgetsLib "https://github.com/commontk/ctk/tree/master/Libs/Widgets/"
    click DICOMLib "https://github.com/commontk/ctk/tree/master/Libs/DICOM/"
    click VizLib "https://github.com/commontk/ctk/tree/master/Libs/Visualization/"
    click ScriptingLib "https://github.com/commontk/ctk/tree/master/Libs/Scripting/"
    click QtTestingLib "https://github.com/commontk/ctk/tree/master/Libs/QtTesting/"
    click XNATLib "https://github.com/commontk/ctk/tree/master/Libs/XNAT/"
    click ImageProcessingLib "https://github.com/commontk/ctk/tree/master/Libs/ImageProcessing/"
    click HostApp "https://github.com/commontk/ctk/tree/master/Applications/ctkDICOMHost/"
    click PluginBrowserApp "https://github.com/commontk/ctk/tree/master/Applications/ctkPluginBrowser/"
    click CLMExplorerApp "https://github.com/commontk/ctk/tree/master/Applications/ctkCommandLineModuleExplorer/"
    click CLITestApps "https://github.com/commontk/ctk/tree/master/Applications/Testing/Cpp/"
    click PythonShellApp "https://github.com/commontk/ctk/tree/master/Applications/ctkSimplePythonShell/"
    click PluginsRoot "https://github.com/commontk/ctk/tree/master/Plugins/"

    classDef external fill:#D8BFD8,stroke:#000
    classDef build fill:#D3D3D3,stroke:#000
    classDef utility fill:#F0E68C,stroke:#000
    classDef core fill:#98FB98,stroke:#000
    classDef plugin fill:#ADD8E6,stroke:#000
    classDef frontend fill:#FFA500,stroke:#000
```
