# How to convert a model into a volume and then compress it into a plane (volume) in a direction?

**URL:** <https://discourse.slicer.org/t/how-to-convert-a-model-into-a-volume-and-then-compress-it-into-a-plane-volume-in-a-direction/28983>\
**Category:** Development\
**Tags:** segmentation, python, 3d-model\
**Created:** [April 18, 2023, 12:57pm UTC](https://discourse.slicer.org/t/how-to-convert-a-model-into-a-volume-and-then-compress-it-into-a-plane-volume-in-a-direction/28983 "2023-04-18T12:57:18Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jumbojing](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jumbojing/32/10811_2.png) [@jumbojing](https://discourse.slicer.org/u/jumbojing)\
**Post date:** [April 18, 2023, 12:57pm UTC](https://discourse.slicer.org/t/how-to-convert-a-model-into-a-volume-and-then-compress-it-into-a-plane-volume-in-a-direction/28983/1 "2023-04-18T12:57:18Z")

</div>

How to convert a model into a volume and then compress it into a plane (a volume with one slice) in a direction?

@lassoan@pieper@Juicy@jamesobutler @jcfr

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [April 18, 2023, 2:02pm UTC](https://discourse.slicer.org/t/how-to-convert-a-model-into-a-volume-and-then-compress-it-into-a-plane-volume-in-a-direction/28983/2 "2023-04-18T14:02:05Z")

</div>

You can load it as a segmentation and then get the numpy array so you can use [sum](https://numpy.org/doc/stable/reference/generated/numpy.sum.html) or similar.

---

<div class="post-metadata">

**Author:** ![jumbojing](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jumbojing/32/10811_2.png) [@jumbojing](https://discourse.slicer.org/u/jumbojing)\
**Post date:** [April 18, 2023, 11:08pm UTC](https://discourse.slicer.org/t/how-to-convert-a-model-into-a-volume-and-then-compress-it-into-a-plane-volume-in-a-direction/28983/4 "2023-04-18T23:08:22Z")

</div>

At 1St, convert a model into a segmentation…as👇…

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/a/3/a331a0f431ed878046f147ce937c11c8bd08900d.jpeg)

👆🏻the model

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/a/2/a270ca1ce8e4a941f68ca835ba9484468e351357.jpeg)

👆🏻the segmentation

The segmentation converted is not the desired result on the two-dimensional slice…  
👇 This is the attachment:

[test.mrb](https://drive.google.com/file/d/1h9v8fWfetG2iAab8ZVABcSEaYzvQMitk/view?usp=share_link)

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [April 19, 2023, 2:40pm UTC](https://discourse.slicer.org/t/how-to-convert-a-model-into-a-volume-and-then-compress-it-into-a-plane-volume-in-a-direction/28983/5 "2023-04-19T14:40:18Z")

</div>

The stl is not a closed surface so the rasterization fails. Probably you can fix it in some cad tool or your could try rotating it so that the open ends align with the rasterization planes.

---

<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:** [April 20, 2023, 4:23am UTC](https://discourse.slicer.org/t/how-to-convert-a-model-into-a-volume-and-then-compress-it-into-a-plane-volume-in-a-direction/28983/6 "2023-04-20T04:23:36Z")

</div>

You can project a model node to the current slice plane by setting in Models module: Display / Slice display / Mode → Projection. Also enable Visibility and reduce Opacity a bit in that section.

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/4/4/4432f3954b90abe087112ef3c301229c2757ea4b.png)

Distance encoded projection option may be useful, too:

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/b/8/b8a148465598a970bb83a564e1ea6040fb05c3b1.jpeg)

Note that these projection modes can be also used for visualizing the pedical screws.

---

<div class="post-metadata">

**Author:** ![jumbojing](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jumbojing/32/10811_2.png) [@jumbojing](https://discourse.slicer.org/u/jumbojing)\
**Post date:** [April 26, 2023, 4:30am UTC](https://discourse.slicer.org/t/how-to-convert-a-model-into-a-volume-and-then-compress-it-into-a-plane-volume-in-a-direction/28983/7 "2023-04-26T04:30:06Z")

</div>

Another solution: I attempted to project this model onto a plane in one direction, resulting in an array containing all coordinate points Then, project these point coordinates onto an empty volume. However, the current problem is how to generate a `schalar volume` (or `binary labelmap`) from a set of point clouds located on a certain plane in 3D space?

👇My failed code, got a empty volume:

```auto
def ps2vol(ps, modName='vol'):
    ps = np.asarray(ps)
    # psX轴的最大值-最小值
    l = len(ps)
    norm = Helper.p3Nor(ps[0], ps[int(l/3)], ps[int(l/3*2)])
    dimX = int(ps[:, 0].max() - ps[:, 0].min())+1
    dimY = int(ps[:, 1].max() - ps[:, 1].min())+1
    dimZ = int(ps[:, 2].max() - ps[:, 2].min())+1
    # 创建vtkImageData数据
    imageSize = [dimX, dimY, dimZ]
    voxelType=vtk.VTK_UNSIGNED_CHAR
    imageOrigin = [ps[:, 0].min(), ps[:, 1].min(), ps[:, 2].min()]
    imageSpacing = [1.0, 1.0, 1.0]
    imageDirections = np.diag(norm)
    fillVoxelValue = 0
    # Create an empty image volume, filled with fillVoxelValue
    imageData = vtk.vtkImageData()
    imageData.SetDimensions(imageSize)
    imageData.AllocateScalars(voxelType, 1)
    imageData.GetPointData().GetScalars().Fill(fillVoxelValue)
    # 将有点的位置设置为1
    for point in ps:
        i, j, k = int(point[0]), int(point[1]), int(point[2])
        imageData.SetScalarComponentFromDouble(i, j, k, 0, 1) #
    imageData.Modified()
# writer = vtk.vtkXMLImageDataWriter()
# writer.SetInputData(imageData)
# writer.SetFileName(modName+'.vti')
# writer.Write()
# # Create volume node
    volumeNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLScalarVolumeNode", modName)
    volumeNode.SetOrigin(imageOrigin)
    volumeNode.SetSpacing(imageSpacing)
    volumeNode.SetIJKToRASDirections(imageDirections)
    volumeNode.SetAndObserveImageData(imageData)
    volumeNode.CreateDefaultDisplayNodes()
    volumeNode.CreateDefaultStorageNode()
    return volumeNode

```

[Point cloud files: pjArr.npy](https://drive.google.com/file/d/13T8Mswa18OOogUkBl_Bw_T_wHCEpV3Tg/view?usp=sharing)

pointCloud with markPoint:👇

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/1/e/1e0bcb6ae88a958631ea25a1311599acb2dd5b9d.jpeg)

---

<div class="post-metadata">

**Author:** ![jumbojing](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jumbojing/32/10811_2.png) [@jumbojing](https://discourse.slicer.org/u/jumbojing)\
**Post date:** [May 3, 2023, 4:07am UTC](https://discourse.slicer.org/t/how-to-convert-a-model-into-a-volume-and-then-compress-it-into-a-plane-volume-in-a-direction/28983/8 "2023-05-03T04:07:43Z")

</div>

???👆🏻

what’s wrong?
