# Adding volume in C++

**URL:** <https://discourse.slicer.org/t/adding-volume-in-c/19174>\
**Category:** Development\
**Created:** [August 12, 2021, 6:26pm UTC](https://discourse.slicer.org/t/adding-volume-in-c/19174 "2021-08-12T18:26:18Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![keri](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/keri/32/11618_2.png) [@keri](https://discourse.slicer.org/u/keri)\
**Post date:** [August 12, 2021, 6:26pm UTC](https://discourse.slicer.org/t/adding-volume-in-c/19174/1 "2021-08-12T18:26:18Z")

</div>

I’m working on custom slicer app and now I need to understand how to add volume in C++.

For example I have `Eigen::MatrixXf` that can be represented as `vtkImageData`.  
To display my data I guess I need to call `GetScene()` (or `GetMRMLScene()` I don’t know exactly) and then call `AddNode(myVtkImage)`.

If so I cannot understand how to call `GetScene()` from mainwindow class? What header contains this `GetScene`?

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [August 12, 2021, 6:47pm UTC](https://discourse.slicer.org/t/adding-volume-in-c/19174/2 "2021-08-12T18:47:32Z")

</div>

When your module is instantiated the scene is passed to both the module logic and widget classes and they can be accessed as `this->GetMRMLScene()` and `this->mrmlScene()`, respectively.

All other classes should get the scene from these classes (e.g., the module widget sets the scene in its child widgets), but if you need any quick hack anywhere then you can access the application singleton, get the application logic from it, and from that you can get the scene. It is better to avoid getting the scene from the application though, because it adds unnecessary dependencies and assumptions, which make the class less reusable.

---

<div class="post-metadata">

**Author:** ![keri](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/keri/32/11618_2.png) [@keri](https://discourse.slicer.org/u/keri)\
**Post date:** [August 12, 2021, 9:51pm UTC](https://discourse.slicer.org/t/adding-volume-in-c/19174/3 "2021-08-12T21:51:09Z")

</div>

Thank you!

One another related question that has now appeared:

If my module depends on my SlicerCAT library wich is added via `slicerMacroBuildAppLibrary(NAME qColada ...)` and thus should be called `qColada`, how can I `find_package(qColada REQUIRED)` from my loadable module’s CMakeLists? As I can see `slicerMacroBuildAppLibrary()` doesn’t produce something like `qColadaConfig.cmake` inside `Slicer-build`. (in contrast to `SlicerConfig.cmake` wich resides in this folder and thus can be easily found).

I guess I need somehow pass `-DqColada_DIR=/path/to/ColadaConfig.cmake` when configuring my module…

I forgot to notice that I’m trying to build module externally

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [August 13, 2021, 4:39am UTC](https://discourse.slicer.org/t/adding-volume-in-c/19174/4 "2021-08-13T04:39:43Z")

</div>

`find_package` is for external packages. Within Slicer, the module libraries and headers are available as CMake variables. See for example, [here](https://github.com/SlicerIGT/SlicerIGT/blob/master/PathExplorer/CMakeLists.txt) how libraries of Subject Hierarchy and Markups modules are used in a SlicerIGT module.

---

<div class="post-metadata">

**Author:** ![keri](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/keri/32/11618_2.png) [@keri](https://discourse.slicer.org/u/keri)\
**Post date:** [August 13, 2021, 10:23am UTC](https://discourse.slicer.org/t/adding-volume-in-c/19174/5 "2021-08-13T10:23:00Z")

</div>

I think now I see how this works. Variables marked `mark_as_superbuild(...)` are kept in `SlicerConfig.cmake` and thus I can use them to find my dependencies.

Thank you!

---

<div class="post-metadata">

**Author:** ![keri](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/keri/32/11618_2.png) [@keri](https://discourse.slicer.org/u/keri)\
**Post date:** [August 13, 2021, 1:43pm UTC](https://discourse.slicer.org/t/adding-volume-in-c/19174/6 "2021-08-13T13:43:00Z")

</div>

Also I can see now that the target `qColadaApp` is defined as:

```cmake
if (TARGET qColadaApp)
  message("qColadaApp EXIST")
endif()

```

prints: `qColadaApp EXIST`

But when I link this target `qColadaApp` to my module:

```cmake
set(MODULE_TARGET_LIBRARIES
  vtkSlicer${MODULE_NAME}ModuleLogic
  qSlicer${MODULE_NAME}ModuleWidgets
  qColadaApp # my custom Slicer library
  )

#-----------------------------------------------------------------------------
slicerMacroBuildLoadableModule(
  NAME ${MODULE_NAME}
  TITLE ${MODULE_TITLE}
  EXPORT_DIRECTIVE ${MODULE_EXPORT_DIRECTIVE}
  INCLUDE_DIRECTORIES ${MODULE_INCLUDE_DIRECTORIES}
  SRCS ${MODULE_SRCS}
  MOC_SRCS ${MODULE_MOC_SRCS}
  UI_SRCS ${MODULE_UI_SRCS}
  TARGET_LIBRARIES ${MODULE_TARGET_LIBRARIES}
  RESOURCES ${MODULE_RESOURCES}
  WITH_GENERIC_TESTS
  )

```

I see that IDE can’t include headers from that target. Picture below. Probably some target properties are missing?

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/8/6/861900b7c396cb62a326aca712534be35fef5439.png)

---

<div class="post-metadata">

**Author:** ![keri](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/keri/32/11618_2.png) [@keri](https://discourse.slicer.org/u/keri)\
**Post date:** [August 13, 2021, 4:42pm UTC](https://discourse.slicer.org/t/adding-volume-in-c/19174/7 "2021-08-13T16:42:25Z")

</div>

I guess I have found a solution. I should have manually added include folders to my `ColadaApp` headers (and export header wich is in `Slicer-build/Applications/ColadaApp`:

```auto
# Current_{source,binary} and Slicer_{Libs,Base} already included
set(MODULE_INCLUDE_DIRECTORIES
  ${CMAKE_CURRENT_SOURCE_DIR}/Logic
  ${CMAKE_CURRENT_BINARY_DIR}/Logic
  ${CMAKE_CURRENT_SOURCE_DIR}/Widgets
  ${CMAKE_CURRENT_BINARY_DIR}/Widgets

  # These two are my dirs
  ${CMAKE_CURRENT_SOURCE_DIR}/../../../Applications/ColadaApp
  ${Slicer_BINARY_DIR}/Applications/ColadaApp
  )

slicerMacroBuildLoadableModule(
...
  INCLUDE_DIRECTORIES ${MODULE_INCLUDE_DIRECTORIES}
...
  )

```

I hope the number of my questions grows slower than the answers on them 🙂

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [August 16, 2021, 10:21pm UTC](https://discourse.slicer.org/t/adding-volume-in-c/19174/8 "2021-08-16T22:21:30Z")

</div>

A post was split to a new topic: [Change memory layout of images](https://discourse.slicer.org/t/change-memory-layout-of-images/19217)
