# Fastest way to create 3D topography surface (with holes)

**URL:** https://discourse.paraview.org/t/fastest-way-to-create-3d-topography-surface-with-holes/3287
**Category:** Development
**Tags:** python
**Created:** [January 7, 2020, 6:54pm UTC](https://discourse.paraview.org/t/fastest-way-to-create-3d-topography-surface-with-holes/3287 "2020-01-07T18:54:02Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![MBG](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/m/7ab992/32.png) [@MBG](https://discourse.paraview.org/u/MBG)
#### Post date: [January 7, 2020, 6:54pm UTC](https://discourse.paraview.org/t/fastest-way-to-create-3d-topography-surface-with-holes/3287/1 "2020-01-07T18:54:02Z")

</div>

In Python plugin I use the code below to create 3D polydata from reqular 2D grid with holes (NoData values) but it’s works too slow. Maybe should I use some other data type or processing instead?

```
# Define points and triangles for mesh
vtk_points = vtkPoints()
vtk_cells = vtkCellArray()

# Build the meshgrid manually
count = 0
# iterate array
for j in range(0,len(ys)-2):
	for i in range(0,len(xs)-2):
		# check area
		if not mask[j,i] or not mask[j+1,i] or not mask[j,i+1] or not mask[j+1,i+1]:
			continue

		# Triangle 1
		vtk_points.InsertNextPoint(xs[i], ys[j], values[j,i])
		vtk_points.InsertNextPoint(xs[i], ys[j+1], values[j+1,i])
		vtk_points.InsertNextPoint(xs[i+1], ys[j], values[j,i+1])

		triangle = vtkTriangle()
		triangle.GetPointIds().SetId(0, count)
		triangle.GetPointIds().SetId(1, count + 1)
		triangle.GetPointIds().SetId(2, count + 2)

		vtk_cells.InsertNextCell(triangle)

		# Triangle 2
		vtk_points.InsertNextPoint(xs[i], ys[j+1], values[j+1,i])
		vtk_points.InsertNextPoint(xs[i+1], ys[j+1], values[j+1,i+1])
		vtk_points.InsertNextPoint(xs[i+1], ys[j], values[j,i+1])

		triangle = vtkTriangle()
		triangle.GetPointIds().SetId(0, count + 3)
		triangle.GetPointIds().SetId(1, count + 4)
		triangle.GetPointIds().SetId(2, count + 5)

		vtk_cells.InsertNextCell(triangle)

		count += 6

# Create a polydata object
trianglePolyData = vtkPolyData()

# Add the geometry and topology to the polydata
trianglePolyData.SetPoints(vtk_points)
#trianglePolyData.GetPointData().SetScalars(colors)
trianglePolyData.SetPolys(vtk_cells)
```

---

<div class="post-metadata">

### Author: ![mwestphal](https://discourse.paraview.org/user_avatar/discourse.paraview.org/mwestphal/32/17_2.png) [@mwestphal](https://discourse.paraview.org/u/mwestphal)
#### Post date: [January 10, 2020, 9:16am UTC](https://discourse.paraview.org/t/fastest-way-to-create-3d-topography-surface-with-holes/3287/2 "2020-01-10T09:16:59Z")

</div>

Can you share an input data and the desired output ?

---

<div class="post-metadata">

### Author: ![MBG](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/m/7ab992/32.png) [@MBG](https://discourse.paraview.org/u/MBG)
#### Post date: [January 10, 2020, 1:51pm UTC](https://discourse.paraview.org/t/fastest-way-to-create-3d-topography-surface-with-holes/3287/3 "2020-01-10T13:51:40Z")

</div>

Source datasets:

[Download the global GEBCO\_2019 Grid - in netCDF format (11 Gbytes, 12 Gbytes uncompressed)](https://www.bodc.ac.uk/data/open_download/gebco/GEBCO_15SEC/zip/)

[Admin 0 – Countries - Download without boundary lakes](https://www.naturalearthdata.com/http//www.naturalearthdata.com/download/50m/cultural/ne_50m_admin_0_countries_lakes.zip)

Actual plugin code to generate vtkUnstructuredGrid with Z coordinate for coloring:

> <https://github.com/mobigroup/ParaView-plugins/blob/41959e73294368e737b1a649b0d99ff2bbd33b4f/NCube/NCubeParaViewPlugin.py#L235-L256>

  
And to add attribute to it (like to country name for coloring and labeling):  

> <https://github.com/mobigroup/ParaView-plugins/blob/41959e73294368e737b1a649b0d99ff2bbd33b4f/NCube/NCubeParaViewPlugin.py#L360-L364>

This is the output:

 ![Screenshot 2020-01-07 at 21.19.59](https://discourse.paraview.org/uploads/default/original/2X/f/f78534d195842edb4d9db73bd542a6560b5594ac.jpeg)

---

<div class="post-metadata">

### Author: ![MBG](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/m/7ab992/32.png) [@MBG](https://discourse.paraview.org/u/MBG)
#### Post date: [January 10, 2020, 1:55pm UTC](https://discourse.paraview.org/t/fastest-way-to-create-3d-topography-surface-with-holes/3287/4 "2020-01-10T13:55:54Z")

</div>

For now, I don’t know more optimal way to get produce this visualization. Is it the optimal datatype for the task? We have Z coordinate in the mesh geometries and as the separate attribute - could we exclude it but still have the ability for mesh coloring by Z coordinate? Could we save additional attribute like to country name by more optimal way (for now it’s saved separately for every cell as I see)?

---

<div class="post-metadata">

### Author: ![yohanlty\_total](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/y/ecd19e/32.png) [@yohanlty\_total](https://discourse.paraview.org/u/yohanlty_total)
#### Post date: [March 4, 2020, 5:16pm UTC](https://discourse.paraview.org/t/fastest-way-to-create-3d-topography-surface-with-holes/3287/5 "2020-03-04T17:16:52Z")

</div>

Hi,  
I am trying to learn what is efficient and I came across this [https://vtk.org/Wiki/VTK/Tutorials/DataArrays](https://vtk.org/Wiki/VTK/Tutorials/DataArrays)  
It mainly deals with the loss of performances related to the generic API.

From what I have read but not tested in the source code, using iterator + a second class to parallelize with multithreading seems the way to go (I am currently looking at _ParaView-v5.7.0/VTK/Imaging/Core/vtkImageBlend.cxx_).

The next thing would be to reduce cache misses on the 2D array **values** by adding a Morton Index ([https://en.wikipedia.org/wiki/Z-order\_curve](https://en.wikipedia.org/wiki/Z-order_curve))

Hopefully this would help.  
Best regards
