|
1 | 1 | # ITK AI Agent Guide |
2 | 2 |
|
3 | | -ITK (Insight Toolkit) is a cross-platform, open-source toolkit for N-dimensional scientific image processing, segmentation, and registration. This guide helps AI agents navigate ITK's unique architecture and development workflows. |
| 3 | +The Insight Toolkit (ITK) is a cross-platform, open-source C++ toolkit for |
| 4 | +N-dimensional scientific image processing, segmentation, and registration. |
| 5 | +Apache 2.0 licensed. Build tool: **Pixi** (wraps CMake + Ninja). |
4 | 6 |
|
5 | | -## Architecture Overview |
| 7 | +## Context to Load on Demand |
6 | 8 |
|
7 | | -### Modular Structure |
8 | | -ITK uses a **module-based architecture** where each module is a self-contained unit with dependencies. Modules are organized in: |
9 | | -- `Modules/Core/` - Essential classes (Image, Region, Point, Vector, pipeline infrastructure) |
10 | | -- `Modules/Filtering/` - Image processing filters |
11 | | -- `Modules/IO/` - Image, mesh, and transform I/O for various formats (JPEG, PNG, NIFTI, DICOM, etc.) |
12 | | -- `Modules/Registration/` - Image registration algorithms |
13 | | -- `Modules/Segmentation/` - Segmentation algorithms |
14 | | -- `Modules/Numerics/` - Optimization and numerical methods |
15 | | -- `Modules/ThirdParty/` - Vendored dependencies (HDF5, JPEG, TIFF, VNL, Eigen3) |
16 | | -- `Modules/Bridge/` - Bridges to VTK, NumPy |
17 | | -- `Modules/Remote/` - External modules fetched during build |
| 9 | +Load only what your task requires: |
18 | 10 |
|
19 | | -Each module has an `itk-module.cmake` file declaring dependencies: |
20 | | -```cmake |
21 | | -itk_module(ITKCommon |
22 | | - DEPENDS ITKEigen3 ITKVNL |
23 | | - PRIVATE_DEPENDS ITKDoubleConversion |
24 | | - COMPILE_DEPENDS ITKKWSys |
25 | | - TEST_DEPENDS ITKTestKernel ITKMesh |
26 | | - DESCRIPTION "Core ITK classes..." |
27 | | -) |
28 | | -``` |
| 11 | +| Task | Read | |
| 12 | +|------|------| |
| 13 | +| Understanding the codebase layout | [./Documentation/AI/architecture.md](./Documentation/AI/architecture.md) | |
| 14 | +| Building or configuring ITK | [./Documentation/AI/building.md](./Documentation/AI/building.md) | |
| 15 | +| Writing or running tests | [./Documentation/AI/testing.md](./Documentation/AI/testing.md) | |
| 16 | +| Code style, naming conventions | [./Documentation/AI/style.md](./Documentation/AI/style.md) | |
| 17 | +| Writing ITK C++ classes, CMake build configuration, and Python wrapping configuration | [./Documentation/AI/conventions.md](./Documentation/AI/conventions.md) | |
| 18 | +| Creating a git commit | [./Documentation/AI/git-commits.md](./Documentation/AI/git-commits.md) | |
| 19 | +| Opening or updating a pull request | [./Documentation/AI/pull-requests.md](./Documentation/AI/pull-requests.md) | |
29 | 20 |
|
30 | | -### Template-Heavy C++ Design |
31 | | -ITK extensively uses **C++ templates** and **generic programming** for: |
32 | | -- Dimension-agnostic code (2D, 3D, nD images) |
33 | | -- Type flexibility (pixel types: unsigned char, float, RGB, etc.) |
34 | | -- Compile-time polymorphism via traits and policy classes |
| 21 | +## Critical Pitfalls |
35 | 22 |
|
36 | | -Example: `itk::Image<TPixel, VImageDimension>` where both parameters are compile-time constants. |
37 | | - |
38 | | -### Pipeline Architecture |
39 | | -ITK uses a **data pipeline** pattern: |
40 | | -- **Process objects** (`itk::ProcessObject`, e.g. most filters) perform computations |
41 | | -- **Data objects** (`itk::DataObject`, e.g., `itk::Image`) store data |
42 | | -- Filters connect via `SetInput()` / `GetOutput()` and execute lazily with `Update()` |
43 | | -- Smart pointers (`itk::SmartPointer<T>`) handle memory management |
44 | | - |
45 | | -### Python Wrapping |
46 | | -ITK provides **Python wrappers** via SWIG: |
47 | | -- Wrapping configs in `Wrapping/` directory |
48 | | -- Explicit template instantiations in `wrapping/` subdirectories of modules |
49 | | -- Python package structure mirrors C++ modules |
50 | | -- Snake_case functions available (e.g., `itk.median_image_filter()`) |
51 | | -- Install via `pip install itk` or build with `ITK_WRAP_PYTHON=ON` |
52 | | - |
53 | | -## Build System |
54 | | - |
55 | | -### CMake Configuration |
56 | | -ITK requires CMake 3.22.1+. Key build options: |
57 | | - |
58 | | -**Essential:** |
59 | | -```bash |
60 | | -cmake -B build -S . \ |
61 | | - -DCMAKE_BUILD_TYPE=Release \ |
62 | | - -DITK_BUILD_DEFAULT_MODULES=ON \ |
63 | | - -DBUILD_TESTING=ON \ |
64 | | - -DBUILD_EXAMPLES=OFF |
65 | | -``` |
66 | | - |
67 | | -**Python wrapping:** |
68 | | -```bash |
69 | | -cmake -B build-python -S . \ |
70 | | - -DITK_WRAP_PYTHON=ON \ |
71 | | - -DITK_WRAP_unsigned_short=ON \ |
72 | | - -DITK_WRAP_IMAGE_DIMS="2;3;4" |
73 | | -``` |
74 | | - |
75 | | -**Module selection:** |
76 | | -- `Module_<ModuleName>=ON` to enable specific modules |
77 | | -- `ITK_BUILD_DEFAULT_MODULES=ON` builds standard modules |
78 | | -- Use `find_package(ITK COMPONENTS ITKCommon ITKIOImageBase)` in external projects |
79 | | - |
80 | | -### Building |
81 | | -```bash |
82 | | -# Build via Pixi (recommended for development) |
83 | | -pixi run --as-is build # Build C++ tests |
84 | | -pixi run --as-is build-python # Build Python tests |
85 | | - |
86 | | -# Run tests via Pixi (recommended for development) |
87 | | -pixi run --as-is test # C++ tests |
88 | | -pixi run --as-is test-python # Python tests |
89 | | -``` |
90 | | - |
91 | | -For an interactive shell with ITK environment: |
92 | | -```bash |
93 | | -pixi shell -e cxx # C++ development |
94 | | -pixi shell -e python # Python development |
95 | | -``` |
96 | | - |
97 | | -### Module Dependencies |
98 | | -ITK automatically resolves module dependencies via CMake. The module DAG is loaded from `itk-module.cmake` files. To find required modules for code: |
99 | | -```bash |
100 | | -python Utilities/Maintenance/WhatModulesITK.py /path/to/ITK/source file1.cxx file2.h |
101 | | -``` |
102 | | - |
103 | | -## Testing |
104 | | - |
105 | | -### CTest Integration |
106 | | -Tests use **CTest** framework: |
107 | | -```bash |
108 | | -cd build |
109 | | -ctest -j8 # Run all tests in parallel |
110 | | -ctest -R ImageFilter # Run tests matching regex |
111 | | -ctest -L REQUIRES_GPU # Run tests with label |
112 | | -ctest --rerun-failed # Rerun only failed tests |
113 | | -``` |
114 | | - |
115 | | -### Test Organization |
116 | | -Each module has a `test/` directory with: |
117 | | -- **CTest tests**: Defined via `itk_add_test()` macro |
118 | | -- **GTest tests (preferred)**: Modern C++ tests using `creategoogletestdriver()` |
119 | | -- **Baseline images**: Stored via `ExternalData` (downloaded on demand) |
120 | | - |
121 | | -Example test definition: |
122 | | -```cmake |
123 | | -itk_add_test(NAME itkImageTest |
124 | | - COMMAND ITKCommonTestDriver itkImageTest |
125 | | - DATA{Input/image.png} |
126 | | - ${ITK_TEST_OUTPUT_DIR}/output.png |
127 | | -) |
128 | | -``` |
129 | | - |
130 | | -### ExternalData System |
131 | | -Large test data is **not stored in Git**. Instead, ITK uses `ExternalData` with content hashes: |
132 | | -- `DATA{path/to/file.png}` references data by hash |
133 | | -- Data downloaded from `https` resources during build |
134 | | -- Test data uploaded separately for new tests |
135 | | - |
136 | | -## Development Workflow |
137 | | - |
138 | | -### Setup for Development |
139 | | -**First time only:** |
140 | | -```bash |
141 | | -./Utilities/SetupForDevelopment.sh |
142 | | -``` |
143 | | -This configures: |
144 | | -- Git hooks (pre-commit, commit-msg) |
145 | | -- clang-format integration (auto-formats C++ on commit) |
146 | | -- KWStyle configuration |
147 | | -- GitHub remote setup |
148 | | - |
149 | | -### Code Style |
150 | | - |
151 | | -**Automatic C++ formatting:** |
152 | | -ITK enforces `.clang-format` style automatically via pre-commit hook. To format manually: |
153 | | -```bash |
154 | | -Utilities/Maintenance/clang-format.bash --modified # Format modified files |
155 | | -``` |
156 | | - |
157 | | -**Key style rules:** |
158 | | -- Use `clang-format` 19.1.7 (enforced by pre-commit) |
159 | | -- `constexpr` instead of `#define` for constants |
160 | | -- Smart pointers for all ITK objects: `auto image = ImageType::New();` |
161 | | -- American English spelling |
162 | | -- Doxygen comments with `\` (backslash) style: `\class`, `\brief` |
163 | | -- No `using namespace` in headers |
164 | | - |
165 | | -**Naming conventions:** |
166 | | -- Classes: `PascalCase` (e.g., `MedianImageFilter`) |
167 | | -- Variables: `camelCase` or `lowercase` with no underscores |
168 | | -- Member variables: `m_MemberVariable` prefix |
169 | | -- Template parameters: `TInputImage`, `TOutputImage` prefix |
170 | | -- Macros: `ITK_UPPERCASE_MACRO` |
171 | | - |
172 | | -### Commit Guidelines |
173 | | -**Required format** (enforced by `kw-commit-msg.py` hook): |
174 | | -``` |
175 | | -PREFIX: Brief description (≤78 chars) |
176 | | -
|
177 | | -Longer explanation if needed. Reference issues or features. |
178 | | -``` |
179 | | - |
180 | | -Common prefixes: `ENH:`, `BUG:`, `COMP:`, `DOC:`, `STYLE:`, `PERF:`, `WIP:` |
181 | | - |
182 | | -### CI/CD |
183 | | -ITK uses: |
184 | | -- **Azure Pipelines** for Linux, Windows, macOS builds (C++ and Python) |
185 | | -- **GitHub Actions** for Pixi builds and Apple Silicon |
186 | | -- **CDash** dashboards at https://open.cdash.org/index.php?project=Insight |
187 | | - |
188 | | -## Key Conventions |
189 | | - |
190 | | -### ITK-Specific Patterns |
191 | | - |
192 | | -**Object creation:** |
193 | | -```cpp |
194 | | -auto filter = FilterType::New(); // Factory method, returns SmartPointer |
195 | | -filter->SetInput(image); |
196 | | -filter->Update(); // Lazy evaluation |
197 | | -auto output = filter->GetOutput(); |
198 | | -``` |
199 | | -
|
200 | | -**Image iteration:** |
201 | | -Use **iterators** (not raw buffers) for image traversal: |
202 | | -```cpp |
203 | | -itk::ImageRegionIterator<ImageType> it(image, region); |
204 | | -for (; !it.IsAtEnd(); ++it) { |
205 | | - it.Set(it.Get() * 2); |
206 | | -} |
207 | | -``` |
208 | | - |
209 | | -**Macros for class boilerplate:** |
210 | | -```cpp |
211 | | -using Self = MyClass; |
212 | | -using Superclass = BaseClass; |
213 | | -using Pointer = SmartPointer<Self>; |
214 | | -itkNewMacro(Self); // Provides New() method |
215 | | -itkTypeMacro(Self, Superclass); // RTTI support |
216 | | -itkSetMacro(Radius, unsigned int); // Generates SetRadius() |
217 | | -itkGetConstMacro(Radius, unsigned int); // Generates GetRadius() |
218 | | -``` |
219 | | -
|
220 | | -### File Organization |
221 | | -Each module follows: |
222 | | -``` |
223 | | -ModuleName/ |
224 | | -├── itk-module.cmake # Module metadata |
225 | | -├── include/ # Public headers (.h) |
226 | | -│ ├── itkClassName.h |
227 | | -│ └── itkClassName.hxx # Template implementations |
228 | | -├── src/ # Non-template implementations (.cxx) |
229 | | -│ └── itkClassName.cxx # Non-template implementations |
230 | | -├── test/ # Tests |
231 | | -│ ├── CMakeLists.txt |
232 | | -│ └── itkClassNameTest.cxx |
233 | | -└── wrapping/ # Python wrapping (if applicable) |
234 | | - └── itkClassName.wrap |
235 | | -``` |
236 | | -
|
237 | | -**Header structure:** |
238 | | -- `.h` files: Class declarations |
239 | | -- `.hxx` files: Template method implementations (included at end of `.h`) |
240 | | -- `.cxx` files: Non-template implementations compiled into libraries |
241 | | -
|
242 | | -### Third-Party Code |
243 | | -Third-party libraries in `Modules/ThirdParty/` are **subtrees**, not submodules: |
244 | | -- Updated via `git subtree pull` |
245 | | -- Maintained upstream separately |
246 | | -- Wrapped with ITK CMake logic |
247 | | -
|
248 | | -## External Module Development |
249 | | -
|
250 | | -To create a remote module: |
251 | | -1. Create `itk-module.cmake` with dependencies |
252 | | -2. Create `MyModule.remote.cmake` in ITK's `Modules/Remote/` |
253 | | -3. Use `itk_module_impl()` macro in module's CMakeLists.txt |
254 | | -4. Test as external build: |
255 | | -```bash |
256 | | -cmake -B build-external -S path/to/module \ |
257 | | - -DITK_DIR=/path/to/ITK-build |
258 | | -``` |
259 | | - |
260 | | -## Common Pitfalls |
261 | | - |
262 | | -1. **Template compilation errors**: ITK's heavy template use causes verbose errors. Focus on the **first error** in the output. |
263 | | - |
264 | | -2. **Python wrapping**: Not all C++ types are wrapped. Check `wrapping/` directories for available instantiations. Common wrapped types: `F` (float), `D` (double), `UC` (unsigned char), `US` (unsigned short). |
265 | | - |
266 | | -3. **Memory management**: Always use `SmartPointer`. Never `delete` ITK objects manually. |
267 | | - |
268 | | -4. **Update() calls**: Filters don't execute until `Update()` is called. Changes to filter parameters after `Update()` require another `Update()`. |
269 | | - |
270 | | -5. **Module dependencies**: If code fails to link, check `itk-module.cmake` dependencies. Use `DEPENDS` for public deps, `PRIVATE_DEPENDS` for implementation deps. |
| 23 | +1. **Template errors are verbose** — focus on the *first* error only. |
| 24 | +2. **Python wrapping is incomplete** — check `wrapping/` dirs for available types before assuming a class is wrapped. |
| 25 | +3. **Never `delete` ITK objects** — always use `SmartPointer` (`auto filter = FilterType::New()`). |
| 26 | +4. **`Update()` is required** — filters don't execute until called; parameter changes after `Update()` need another call. |
| 27 | +5. **Link errors → check `itk-module.cmake`** — missing `DEPENDS` or `PRIVATE_DEPENDS` is the usual cause. |
| 28 | +6. **Licensing** — verify AI output does not reproduce third-party code in conflict with Apache 2.0. |
271 | 29 |
|
272 | 30 | ## Resources |
273 | 31 |
|
274 | | -- Main docs: https://docs.itk.org/ |
275 | | -- Discourse forum: https://discourse.itk.org/ |
| 32 | +- Docs: https://docs.itk.org/ |
| 33 | +- Contribution guide: https://docs.itk.org/en/latest/contributing/ |
| 34 | +- Discourse: https://discourse.itk.org/ |
276 | 35 | - Software Guide: https://itk.org/ItkSoftwareGuide.pdf |
277 | 36 | - Examples: https://examples.itk.org/ |
278 | 37 | - Doxygen API: https://itk.org/Doxygen/html/ |
279 | | -- Contribution guide: https://docs.itk.org/en/latest/contributing/ |
280 | | - |
281 | | -## Quick Reference |
282 | | - |
283 | | -**Find class documentation:** |
284 | | -```bash |
285 | | -# Search Doxygen locally after building docs |
286 | | -cmake -DITK_BUILD_DOCUMENTATION=ON .. |
287 | | -``` |
288 | | - |
289 | | -**Test a single module:** |
290 | | -```bash |
291 | | -ctest -L ITKCommon |
292 | | -``` |
293 | | - |
294 | | -**Build only specific modules:** |
295 | | -```cmake |
296 | | -set(Module_ITKCommon ON) |
297 | | -set(Module_ITKIOImageBase ON) |
298 | | -set(ITK_BUILD_DEFAULT_MODULES OFF) |
299 | | -``` |
300 | | - |
301 | | -**Python quick test:** |
302 | | -```python |
303 | | -import itk |
304 | | -image = itk.imread('input.png') |
305 | | -smoothed = itk.median_image_filter(image, radius=2) |
306 | | -itk.imwrite(smoothed, 'output.png') |
307 | | -``` |
| 38 | +- CDash: https://open.cdash.org/index.php?project=Insight |
0 commit comments