How to visualize 3D delaunay triangulation in Python?
I have a set of 3D points which I've used scipy.spatial.Delaunay to do the triangulation / tetrahedralization. I now have a set of unique faces of all of the tetrahedra, and would like to visualize these in 3D.
Are there any Python libraries (or libraries with a Python wrapper) that can do this?
python scipy vtk mayavi mplot3d
add a comment |
I have a set of 3D points which I've used scipy.spatial.Delaunay to do the triangulation / tetrahedralization. I now have a set of unique faces of all of the tetrahedra, and would like to visualize these in 3D.
Are there any Python libraries (or libraries with a Python wrapper) that can do this?
python scipy vtk mayavi mplot3d
Have you tried to do it using any of these libraries that you tagged? If so, can you show us what you have tried so far?
– aestrivex
Nov 18 '13 at 20:14
add a comment |
I have a set of 3D points which I've used scipy.spatial.Delaunay to do the triangulation / tetrahedralization. I now have a set of unique faces of all of the tetrahedra, and would like to visualize these in 3D.
Are there any Python libraries (or libraries with a Python wrapper) that can do this?
python scipy vtk mayavi mplot3d
I have a set of 3D points which I've used scipy.spatial.Delaunay to do the triangulation / tetrahedralization. I now have a set of unique faces of all of the tetrahedra, and would like to visualize these in 3D.
Are there any Python libraries (or libraries with a Python wrapper) that can do this?
python scipy vtk mayavi mplot3d
python scipy vtk mayavi mplot3d
asked Nov 17 '13 at 0:42
bardbard
99021537
99021537
Have you tried to do it using any of these libraries that you tagged? If so, can you show us what you have tried so far?
– aestrivex
Nov 18 '13 at 20:14
add a comment |
Have you tried to do it using any of these libraries that you tagged? If so, can you show us what you have tried so far?
– aestrivex
Nov 18 '13 at 20:14
Have you tried to do it using any of these libraries that you tagged? If so, can you show us what you have tried so far?
– aestrivex
Nov 18 '13 at 20:14
Have you tried to do it using any of these libraries that you tagged? If so, can you show us what you have tried so far?
– aestrivex
Nov 18 '13 at 20:14
add a comment |
2 Answers
2
active
oldest
votes
Try mayavi.mlab.triangular_mesh()
import numpy as np
from mayavi import mlab
vertices = np.array([[0, 1, 0, 0],[0, 0, 1, 0],[0, 0, 0, 1]])
faces = np.array([[0, 1, 0, 0],[1, 2, 1, 2],[2, 3, 3, 3]])
mlab.triangular_mesh(vertices[0,:], vertices[1,:], vertices[2,:], faces.T)
mlab.show()
add a comment |
It can also be done using the three-dimensional plotting of matplotlib (without the need for the mayavi package).
The following code is an initial simple implementation of such a function.
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
from scipy.spatial import Delaunay
def plot_tri_simple(ax, points, tri):
for tr in tri.simplices:
pts = points[tr, :]
ax.plot3D(pts[[0,1],0], pts[[0,1],1], pts[[0,1],2], color='g', lw='0.1')
ax.plot3D(pts[[0,2],0], pts[[0,2],1], pts[[0,2],2], color='g', lw='0.1')
ax.plot3D(pts[[0,3],0], pts[[0,3],1], pts[[0,3],2], color='g', lw='0.1')
ax.plot3D(pts[[1,2],0], pts[[1,2],1], pts[[1,2],2], color='g', lw='0.1')
ax.plot3D(pts[[1,3],0], pts[[1,3],1], pts[[1,3],2], color='g', lw='0.1')
ax.plot3D(pts[[2,3],0], pts[[2,3],1], pts[[2,3],2], color='g', lw='0.1')
ax.scatter(points[:,0], points[:,1], points[:,2], color='b')
The result of calling this function with the test code below results in the following figure:
np.random.seed(0)
x = 2.0 * np.random.rand(20) - 1.0
y = 2.0 * np.random.rand(20) - 1.0
z = 2.0 * np.random.rand(20) - 1.0
points = np.vstack([x, y, z]).T
tri = Delaunay(points)
fig = plt.figure()
ax = plt.axes(projection='3d')
plot_tri(ax, points, tri)
The code above is slow because the plot is done within the loop. Furthermore, it works on each simplex separately so edges are rendered more than once.
A more efficient implementation follows, which makes use of an auxiliary function collect_edges
to take each edge only once, and uses np.nan
values in the plot function to draw the edge segments in a single plot command.
The result of running the test code above with the new function gives an identical figure but the running time is improved by a factor of x80 on my machine (300 ms compared to 3.6 ms).
def plot_tri_2(ax, points, tri):
edges = collect_edges(tri)
x = np.array()
y = np.array()
z = np.array()
for (i,j) in edges:
x = np.append(x, [points[i, 0], points[j, 0], np.nan])
y = np.append(y, [points[i, 1], points[j, 1], np.nan])
z = np.append(z, [points[i, 2], points[j, 2], np.nan])
ax.plot3D(x, y, z, color='g', lw='0.1')
ax.scatter(points[:,0], points[:,1], points[:,2], color='b')
def collect_edges(tri):
edges = set()
def sorted_tuple(a,b):
return (a,b) if a < b else (b,a)
# Add edges of tetrahedron (sorted so we don't add an edge twice, even if it comes in reverse order).
for (i0, i1, i2, i3) in tri.simplices:
edges.add(sorted_tuple(i0,i1))
edges.add(sorted_tuple(i0,i2))
edges.add(sorted_tuple(i0,i3))
edges.add(sorted_tuple(i1,i2))
edges.add(sorted_tuple(i1,i3))
edges.add(sorted_tuple(i2,i3))
return edges
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f20025784%2fhow-to-visualize-3d-delaunay-triangulation-in-python%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Try mayavi.mlab.triangular_mesh()
import numpy as np
from mayavi import mlab
vertices = np.array([[0, 1, 0, 0],[0, 0, 1, 0],[0, 0, 0, 1]])
faces = np.array([[0, 1, 0, 0],[1, 2, 1, 2],[2, 3, 3, 3]])
mlab.triangular_mesh(vertices[0,:], vertices[1,:], vertices[2,:], faces.T)
mlab.show()
add a comment |
Try mayavi.mlab.triangular_mesh()
import numpy as np
from mayavi import mlab
vertices = np.array([[0, 1, 0, 0],[0, 0, 1, 0],[0, 0, 0, 1]])
faces = np.array([[0, 1, 0, 0],[1, 2, 1, 2],[2, 3, 3, 3]])
mlab.triangular_mesh(vertices[0,:], vertices[1,:], vertices[2,:], faces.T)
mlab.show()
add a comment |
Try mayavi.mlab.triangular_mesh()
import numpy as np
from mayavi import mlab
vertices = np.array([[0, 1, 0, 0],[0, 0, 1, 0],[0, 0, 0, 1]])
faces = np.array([[0, 1, 0, 0],[1, 2, 1, 2],[2, 3, 3, 3]])
mlab.triangular_mesh(vertices[0,:], vertices[1,:], vertices[2,:], faces.T)
mlab.show()
Try mayavi.mlab.triangular_mesh()
import numpy as np
from mayavi import mlab
vertices = np.array([[0, 1, 0, 0],[0, 0, 1, 0],[0, 0, 0, 1]])
faces = np.array([[0, 1, 0, 0],[1, 2, 1, 2],[2, 3, 3, 3]])
mlab.triangular_mesh(vertices[0,:], vertices[1,:], vertices[2,:], faces.T)
mlab.show()
edited Dec 22 '13 at 8:25
Baby Groot
3,984104465
3,984104465
answered Dec 22 '13 at 8:08
nullasnullas
1514
1514
add a comment |
add a comment |
It can also be done using the three-dimensional plotting of matplotlib (without the need for the mayavi package).
The following code is an initial simple implementation of such a function.
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
from scipy.spatial import Delaunay
def plot_tri_simple(ax, points, tri):
for tr in tri.simplices:
pts = points[tr, :]
ax.plot3D(pts[[0,1],0], pts[[0,1],1], pts[[0,1],2], color='g', lw='0.1')
ax.plot3D(pts[[0,2],0], pts[[0,2],1], pts[[0,2],2], color='g', lw='0.1')
ax.plot3D(pts[[0,3],0], pts[[0,3],1], pts[[0,3],2], color='g', lw='0.1')
ax.plot3D(pts[[1,2],0], pts[[1,2],1], pts[[1,2],2], color='g', lw='0.1')
ax.plot3D(pts[[1,3],0], pts[[1,3],1], pts[[1,3],2], color='g', lw='0.1')
ax.plot3D(pts[[2,3],0], pts[[2,3],1], pts[[2,3],2], color='g', lw='0.1')
ax.scatter(points[:,0], points[:,1], points[:,2], color='b')
The result of calling this function with the test code below results in the following figure:
np.random.seed(0)
x = 2.0 * np.random.rand(20) - 1.0
y = 2.0 * np.random.rand(20) - 1.0
z = 2.0 * np.random.rand(20) - 1.0
points = np.vstack([x, y, z]).T
tri = Delaunay(points)
fig = plt.figure()
ax = plt.axes(projection='3d')
plot_tri(ax, points, tri)
The code above is slow because the plot is done within the loop. Furthermore, it works on each simplex separately so edges are rendered more than once.
A more efficient implementation follows, which makes use of an auxiliary function collect_edges
to take each edge only once, and uses np.nan
values in the plot function to draw the edge segments in a single plot command.
The result of running the test code above with the new function gives an identical figure but the running time is improved by a factor of x80 on my machine (300 ms compared to 3.6 ms).
def plot_tri_2(ax, points, tri):
edges = collect_edges(tri)
x = np.array()
y = np.array()
z = np.array()
for (i,j) in edges:
x = np.append(x, [points[i, 0], points[j, 0], np.nan])
y = np.append(y, [points[i, 1], points[j, 1], np.nan])
z = np.append(z, [points[i, 2], points[j, 2], np.nan])
ax.plot3D(x, y, z, color='g', lw='0.1')
ax.scatter(points[:,0], points[:,1], points[:,2], color='b')
def collect_edges(tri):
edges = set()
def sorted_tuple(a,b):
return (a,b) if a < b else (b,a)
# Add edges of tetrahedron (sorted so we don't add an edge twice, even if it comes in reverse order).
for (i0, i1, i2, i3) in tri.simplices:
edges.add(sorted_tuple(i0,i1))
edges.add(sorted_tuple(i0,i2))
edges.add(sorted_tuple(i0,i3))
edges.add(sorted_tuple(i1,i2))
edges.add(sorted_tuple(i1,i3))
edges.add(sorted_tuple(i2,i3))
return edges
add a comment |
It can also be done using the three-dimensional plotting of matplotlib (without the need for the mayavi package).
The following code is an initial simple implementation of such a function.
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
from scipy.spatial import Delaunay
def plot_tri_simple(ax, points, tri):
for tr in tri.simplices:
pts = points[tr, :]
ax.plot3D(pts[[0,1],0], pts[[0,1],1], pts[[0,1],2], color='g', lw='0.1')
ax.plot3D(pts[[0,2],0], pts[[0,2],1], pts[[0,2],2], color='g', lw='0.1')
ax.plot3D(pts[[0,3],0], pts[[0,3],1], pts[[0,3],2], color='g', lw='0.1')
ax.plot3D(pts[[1,2],0], pts[[1,2],1], pts[[1,2],2], color='g', lw='0.1')
ax.plot3D(pts[[1,3],0], pts[[1,3],1], pts[[1,3],2], color='g', lw='0.1')
ax.plot3D(pts[[2,3],0], pts[[2,3],1], pts[[2,3],2], color='g', lw='0.1')
ax.scatter(points[:,0], points[:,1], points[:,2], color='b')
The result of calling this function with the test code below results in the following figure:
np.random.seed(0)
x = 2.0 * np.random.rand(20) - 1.0
y = 2.0 * np.random.rand(20) - 1.0
z = 2.0 * np.random.rand(20) - 1.0
points = np.vstack([x, y, z]).T
tri = Delaunay(points)
fig = plt.figure()
ax = plt.axes(projection='3d')
plot_tri(ax, points, tri)
The code above is slow because the plot is done within the loop. Furthermore, it works on each simplex separately so edges are rendered more than once.
A more efficient implementation follows, which makes use of an auxiliary function collect_edges
to take each edge only once, and uses np.nan
values in the plot function to draw the edge segments in a single plot command.
The result of running the test code above with the new function gives an identical figure but the running time is improved by a factor of x80 on my machine (300 ms compared to 3.6 ms).
def plot_tri_2(ax, points, tri):
edges = collect_edges(tri)
x = np.array()
y = np.array()
z = np.array()
for (i,j) in edges:
x = np.append(x, [points[i, 0], points[j, 0], np.nan])
y = np.append(y, [points[i, 1], points[j, 1], np.nan])
z = np.append(z, [points[i, 2], points[j, 2], np.nan])
ax.plot3D(x, y, z, color='g', lw='0.1')
ax.scatter(points[:,0], points[:,1], points[:,2], color='b')
def collect_edges(tri):
edges = set()
def sorted_tuple(a,b):
return (a,b) if a < b else (b,a)
# Add edges of tetrahedron (sorted so we don't add an edge twice, even if it comes in reverse order).
for (i0, i1, i2, i3) in tri.simplices:
edges.add(sorted_tuple(i0,i1))
edges.add(sorted_tuple(i0,i2))
edges.add(sorted_tuple(i0,i3))
edges.add(sorted_tuple(i1,i2))
edges.add(sorted_tuple(i1,i3))
edges.add(sorted_tuple(i2,i3))
return edges
add a comment |
It can also be done using the three-dimensional plotting of matplotlib (without the need for the mayavi package).
The following code is an initial simple implementation of such a function.
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
from scipy.spatial import Delaunay
def plot_tri_simple(ax, points, tri):
for tr in tri.simplices:
pts = points[tr, :]
ax.plot3D(pts[[0,1],0], pts[[0,1],1], pts[[0,1],2], color='g', lw='0.1')
ax.plot3D(pts[[0,2],0], pts[[0,2],1], pts[[0,2],2], color='g', lw='0.1')
ax.plot3D(pts[[0,3],0], pts[[0,3],1], pts[[0,3],2], color='g', lw='0.1')
ax.plot3D(pts[[1,2],0], pts[[1,2],1], pts[[1,2],2], color='g', lw='0.1')
ax.plot3D(pts[[1,3],0], pts[[1,3],1], pts[[1,3],2], color='g', lw='0.1')
ax.plot3D(pts[[2,3],0], pts[[2,3],1], pts[[2,3],2], color='g', lw='0.1')
ax.scatter(points[:,0], points[:,1], points[:,2], color='b')
The result of calling this function with the test code below results in the following figure:
np.random.seed(0)
x = 2.0 * np.random.rand(20) - 1.0
y = 2.0 * np.random.rand(20) - 1.0
z = 2.0 * np.random.rand(20) - 1.0
points = np.vstack([x, y, z]).T
tri = Delaunay(points)
fig = plt.figure()
ax = plt.axes(projection='3d')
plot_tri(ax, points, tri)
The code above is slow because the plot is done within the loop. Furthermore, it works on each simplex separately so edges are rendered more than once.
A more efficient implementation follows, which makes use of an auxiliary function collect_edges
to take each edge only once, and uses np.nan
values in the plot function to draw the edge segments in a single plot command.
The result of running the test code above with the new function gives an identical figure but the running time is improved by a factor of x80 on my machine (300 ms compared to 3.6 ms).
def plot_tri_2(ax, points, tri):
edges = collect_edges(tri)
x = np.array()
y = np.array()
z = np.array()
for (i,j) in edges:
x = np.append(x, [points[i, 0], points[j, 0], np.nan])
y = np.append(y, [points[i, 1], points[j, 1], np.nan])
z = np.append(z, [points[i, 2], points[j, 2], np.nan])
ax.plot3D(x, y, z, color='g', lw='0.1')
ax.scatter(points[:,0], points[:,1], points[:,2], color='b')
def collect_edges(tri):
edges = set()
def sorted_tuple(a,b):
return (a,b) if a < b else (b,a)
# Add edges of tetrahedron (sorted so we don't add an edge twice, even if it comes in reverse order).
for (i0, i1, i2, i3) in tri.simplices:
edges.add(sorted_tuple(i0,i1))
edges.add(sorted_tuple(i0,i2))
edges.add(sorted_tuple(i0,i3))
edges.add(sorted_tuple(i1,i2))
edges.add(sorted_tuple(i1,i3))
edges.add(sorted_tuple(i2,i3))
return edges
It can also be done using the three-dimensional plotting of matplotlib (without the need for the mayavi package).
The following code is an initial simple implementation of such a function.
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
from scipy.spatial import Delaunay
def plot_tri_simple(ax, points, tri):
for tr in tri.simplices:
pts = points[tr, :]
ax.plot3D(pts[[0,1],0], pts[[0,1],1], pts[[0,1],2], color='g', lw='0.1')
ax.plot3D(pts[[0,2],0], pts[[0,2],1], pts[[0,2],2], color='g', lw='0.1')
ax.plot3D(pts[[0,3],0], pts[[0,3],1], pts[[0,3],2], color='g', lw='0.1')
ax.plot3D(pts[[1,2],0], pts[[1,2],1], pts[[1,2],2], color='g', lw='0.1')
ax.plot3D(pts[[1,3],0], pts[[1,3],1], pts[[1,3],2], color='g', lw='0.1')
ax.plot3D(pts[[2,3],0], pts[[2,3],1], pts[[2,3],2], color='g', lw='0.1')
ax.scatter(points[:,0], points[:,1], points[:,2], color='b')
The result of calling this function with the test code below results in the following figure:
np.random.seed(0)
x = 2.0 * np.random.rand(20) - 1.0
y = 2.0 * np.random.rand(20) - 1.0
z = 2.0 * np.random.rand(20) - 1.0
points = np.vstack([x, y, z]).T
tri = Delaunay(points)
fig = plt.figure()
ax = plt.axes(projection='3d')
plot_tri(ax, points, tri)
The code above is slow because the plot is done within the loop. Furthermore, it works on each simplex separately so edges are rendered more than once.
A more efficient implementation follows, which makes use of an auxiliary function collect_edges
to take each edge only once, and uses np.nan
values in the plot function to draw the edge segments in a single plot command.
The result of running the test code above with the new function gives an identical figure but the running time is improved by a factor of x80 on my machine (300 ms compared to 3.6 ms).
def plot_tri_2(ax, points, tri):
edges = collect_edges(tri)
x = np.array()
y = np.array()
z = np.array()
for (i,j) in edges:
x = np.append(x, [points[i, 0], points[j, 0], np.nan])
y = np.append(y, [points[i, 1], points[j, 1], np.nan])
z = np.append(z, [points[i, 2], points[j, 2], np.nan])
ax.plot3D(x, y, z, color='g', lw='0.1')
ax.scatter(points[:,0], points[:,1], points[:,2], color='b')
def collect_edges(tri):
edges = set()
def sorted_tuple(a,b):
return (a,b) if a < b else (b,a)
# Add edges of tetrahedron (sorted so we don't add an edge twice, even if it comes in reverse order).
for (i0, i1, i2, i3) in tri.simplices:
edges.add(sorted_tuple(i0,i1))
edges.add(sorted_tuple(i0,i2))
edges.add(sorted_tuple(i0,i3))
edges.add(sorted_tuple(i1,i2))
edges.add(sorted_tuple(i1,i3))
edges.add(sorted_tuple(i2,i3))
return edges
edited Nov 13 '18 at 15:50
answered Nov 13 '18 at 15:45
Iddo HannielIddo Hanniel
3165
3165
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f20025784%2fhow-to-visualize-3d-delaunay-triangulation-in-python%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Have you tried to do it using any of these libraries that you tagged? If so, can you show us what you have tried so far?
– aestrivex
Nov 18 '13 at 20:14