# how to draw parametric surfaces with equations?

**URL:** <https://discourse.paraview.org/t/how-to-draw-parametric-surfaces-with-equations/8178>\
**Category:** Development\
**Tags:** vtk, proposal\
**Created:** [October 13, 2021, 10:39am UTC](https://discourse.paraview.org/t/how-to-draw-parametric-surfaces-with-equations/8178 "2021-10-13T10:39:23Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![joaocqmiranda](https://discourse.paraview.org/user_avatar/discourse.paraview.org/joaocqmiranda/32/6641_2.png) [@joaocqmiranda](https://discourse.paraview.org/u/joaocqmiranda)\
**Post date:** [October 13, 2021, 10:39am UTC](https://discourse.paraview.org/t/how-to-draw-parametric-surfaces-with-equations/8178/1 "2021-10-13T10:39:23Z")

</div>

for example you type the equations for x,y,z and get the respective surface

---

<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:** [October 13, 2021, 11:06am UTC](https://discourse.paraview.org/t/how-to-draw-parametric-surfaces-with-equations/8178/2 "2021-10-13T11:06:27Z")

</div>

The closest that exist is an example plugin called ParametricSurfaces that lets you do something like that:

 ![a](https://discourse.paraview.org/uploads/default/original/2X/1/11ce33de6a97ae5a9cdeeb8b59c515ec0efa2458.png)

Here is the plugin:  
[ParametricSource.xml](https://discourse.paraview.org/uploads/short-url/2Ek29NqKT2ldapa5jaWFRQhSsGm.xml) (1.6 KB)

The rest could be done with a programmable filter though, eg see this:

[https://docs.paraview.org/en/latest/ReferenceManual/pythonProgrammableFilter.html#helix-source](https://docs.paraview.org/en/latest/ReferenceManual/pythonProgrammableFilter.html#helix-source)

---

<div class="post-metadata">

**Author:** ![joaocqmiranda](https://discourse.paraview.org/user_avatar/discourse.paraview.org/joaocqmiranda/32/6641_2.png) [@joaocqmiranda](https://discourse.paraview.org/u/joaocqmiranda)\
**Post date:** [October 13, 2021, 2:31pm UTC](https://discourse.paraview.org/t/how-to-draw-parametric-surfaces-with-equations/8178/3 "2021-10-13T14:31:41Z")

</div>

I Tried the programmable to create a supershape. but the only thing i got it was lines (1st code).  
Then I tried based on mathplotlib but I couldn’t got any results either

```auto
# Code for 'Script'

#This script generates a helix curve.
#This is intended as the script of a 'Programmable Source'
import math
import numpy as np
from vtk.numpy_interface import algorithms as algs
from vtk.numpy_interface import dataset_adapter as dsa

numPts = 80 # Points along Helix
length = 8.0 # Length of Helix
rounds = 3.0 # Number of times around
a=1
b=1
m=4
n1=12
n2=15
n3=15

def r(fi):
 return (((abs(np.cos(m*fi/4)/a))**(n2))+((abs(np.sin(m*fi/4)/b))**(n3)))**(-1/n1)

# Compute the point coordinates for the helix.

index = np.arange(-numPts/2, numPts/2, dtype=np.int32)
scalars = index * rounds * 2 * math.pi / numPts
scalars2 = index * rounds * math.pi / numPts

x = r(scalars)*np.cos(scalars)*r(scalars2)*np.cos(scalars2);
y = r(scalars)*np.sin(scalars)*r(scalars2)*np.cos(scalars2)
z = r(scalars2)*np.sin(scalars2)

# Create a (x,y,z) coordinates array and associate that with
# points to pass to the output dataset.
coordinates = algs.make_vector(x, y, z)
pts = vtk.vtkPoints()
pts.SetData(dsa.numpyTovtkDataArray(coordinates, 'Points'))
output.SetPoints(pts)

# Add scalars to the output point data.
output.PointData.append(index, 'Index')
output.PointData.append(scalars, 'Scalars')
output.PointData.append(scalars2, 'Scalars2')

# Next, we need to define the topology i.e.
# cell information. This helix will be a single
# polyline connecting all the points in order.
ptIds = vtk.vtkIdList()
ptIds.SetNumberOfIds(numPts)
for i in range(numPts):
   #Add the points to the line. The first value indicates
   #the order of the point on the line. The second value
   #is a reference to a point in a vtkPoints object. Depends
   #on the order that Points were added to vtkPoints object.
   #Note that this will not be associated with actual points
   #until it is added to a vtkPolyData object which holds a
   #vtkPoints object.
   ptIds.SetId(i, i)

# Allocate the number of 'cells' that will be added. We are just
# adding one vtkPolyLine 'cell' to the vtkPolyData object.
output.Allocate(1, 1)

# Add the poly line 'cell' to the vtkPolyData object.
output.InsertNextCell(vtk.VTK_POLY_LINE, ptIds)

```

```auto
# Code for 'Script'

#This script generates a helix curve.
#This is intended as the script of a 'Programmable Source'
import math
import numpy as np
from vtk.numpy_interface import algorithms as algs
from vtk.numpy_interface import dataset_adapter as dsa

numPts = 32 # Points along Helix
length = 8.0 # Length of Helix
rounds = 3.0 # Number of times around
a=1
b=1
m=4
n1=12
n2=15
n3=15

def r(fi):
 return (((abs(np.cos(m*fi/4)/a))**(n2))+((abs(np.sin(m*fi/4)/b))**(n3)))**(-1/n1)

# Compute the point coordinates for the helix.

#index = np.arange(-numPts/2, numPts/2, dtype=np.int32)
angle = np.linspace(-np.pi, np.pi, numPts)
angle2 = np.linspace(-np.pi/2, np.pi/2, numPts)
scalars, scalars2 = np.meshgrid(angle, angle2)

x = r(scalars)*np.cos(scalars)*r(scalars2)*np.cos(scalars2);
y = r(scalars)*np.sin(scalars)*r(scalars2)*np.cos(scalars2)
z = r(scalars2)*np.sin(scalars2)
print(x[1])
# Create a (x,y,z) coordinates array and associate that with
# points to pass to the output dataset.
coordinates = algs.make_vector(x, y, z)
pts = vtk.vtkPoints()
pts.SetData(dsa.numpyTovtkDataArray(coordinates, 'Points'))
output.SetPoints(pts)

# Add scalars to the output point data.
output.PointData.append(angle, 'Angle')
output.PointData.append(angle2, 'Angle2')
output.PointData.append(scalars, 'Scalars')
output.PointData.append(scalars2, 'Scalars2')

# Next, we need to define the topology i.e.
# cell information. This helix will be a single
# polyline connecting all the points in order.
ptIds = vtk.vtkIdList()
ptIds.SetNumberOfIds(numPts)
for i in range(numPts):
   #Add the points to the line. The first value indicates
   #the order of the point on the line. The second value
   #is a reference to a point in a vtkPoints object. Depends
   #on the order that Points were added to vtkPoints object.
   #Note that this will not be associated with actual points
   #until it is added to a vtkPolyData object which holds a
   #vtkPoints object.
   ptIds.SetId(i, i)

# Allocate the number of 'cells' that will be added. We are just
# adding one vtkPolyLine 'cell' to the vtkPolyData object.
output.Allocate(1, 1)

# Add the poly line 'cell' to the vtkPolyData object.
output.InsertNextCell(vtk.VTK_POLY_LINE, ptIds)

```

---

<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:** [October 13, 2021, 5:38pm UTC](https://discourse.paraview.org/t/how-to-draw-parametric-surfaces-with-equations/8178/4 "2021-10-13T17:38:30Z")

</div>

> [@joaocqmiranda](#):
>
> `VTK_POLY_LINE`

You are adding lines, so you get lines I suppose.

---

<div class="post-metadata">

**Author:** ![joaocqmiranda](https://discourse.paraview.org/user_avatar/discourse.paraview.org/joaocqmiranda/32/6641_2.png) [@joaocqmiranda](https://discourse.paraview.org/u/joaocqmiranda)\
**Post date:** [October 13, 2021, 5:44pm UTC](https://discourse.paraview.org/t/how-to-draw-parametric-surfaces-with-equations/8178/5 "2021-10-13T17:44:32Z")

</div>

> [@joaocqmiranda](#):
>
> `VTK_POLY_LINE`

ok how can i get a surface?

---

<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:** [October 13, 2021, 5:46pm UTC](https://discourse.paraview.org/t/how-to-draw-parametric-surfaces-with-equations/8178/6 "2021-10-13T17:46:50Z")

</div>

VTK\_QUAD ?

You’ll need to learn about VTK I’m afraid: [https://kitware.github.io/vtk-examples/site/Python/GeometricObjects/Quad/](https://kitware.github.io/vtk-examples/site/Python/GeometricObjects/Quad/)

---

<div class="post-metadata">

**Author:** ![joaocqmiranda](https://discourse.paraview.org/user_avatar/discourse.paraview.org/joaocqmiranda/32/6641_2.png) [@joaocqmiranda](https://discourse.paraview.org/u/joaocqmiranda)\
**Post date:** [October 13, 2021, 6:13pm UTC](https://discourse.paraview.org/t/how-to-draw-parametric-surfaces-with-equations/8178/7 "2021-10-13T18:13:26Z")

</div>

Ok it drew a shape but for the shape i want i need before the definition of x,y,z I need to add a comand like meshgrid.

```auto
Output mensage:
Traceback (most recent call last):
  File "<string>", line 20, in <module>
  File "<string>", line 43, in RequestData
  File "C:\Program Files\ParaView 5.9.1-Windows-Python3.8-msvc2017-64bit\bin\Lib\site-packages\vtkmodules\numpy_interface\algorithms.py", line 861, in make_vector
    return algs.make_vector(arrayx, arrayy, arrayz)
  File "C:\Program Files\ParaView 5.9.1-Windows-Python3.8-msvc2017-64bit\bin\Lib\site-packages\vtkmodules\numpy_interface\internal_algorithms.py", line 559, in make_vector
    raise ValueError("Can only merge 1D arrays")
ValueError: Can only merge 1D arrays

```

---

<div class="post-metadata">

**Author:** ![joaocqmiranda](https://discourse.paraview.org/user_avatar/discourse.paraview.org/joaocqmiranda/32/6641_2.png) [@joaocqmiranda](https://discourse.paraview.org/u/joaocqmiranda)\
**Post date:** [October 26, 2021, 3:53pm UTC](https://discourse.paraview.org/t/how-to-draw-parametric-surfaces-with-equations/8178/8 "2021-10-26T15:53:49Z")

</div>

When I put VTK\_QUAD it drew a shape that should be closed. There is any way to use a np.meshgrid?  
I found this code but it didnt drew any thing.

```auto
import vtk
import numpy as np
from vtk.numpy_interface import dataset_adapter as dsa
iD=vtk.vtkImageData()
dims=[31,31,31]
iD.SetSpacing(1,1,1)
iD.SetOrigin(0,0,0)
iD.SetDimensions(dims)
xaxis = np.linspace(-0.5,1,dims[0])
yaxis = np.linspace(-1,1,dims[1])
zaxis = np.linspace(-1,0.5,dims[2])
[xc,yc,zc] = np.meshgrid(xaxis,yaxis,zaxis)
data = np.sqrt(xc **2+yc** 2+zc**2)
image=dsa.WrapDataObject(iD)
image.PointData.append(data.ravel(),"scalar")

```
