ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 23 Apr 2020 04:47:39 +0200In Sage is there support for non-convex polyhedra?https://ask.sagemath.org/question/50929/in-sage-is-there-support-for-non-convex-polyhedra/ Which reference page gives help or examples? Can they be triangulated? I want to triangulate them in order to help turn them into stl files.Wed, 22 Apr 2020 22:46:11 +0200https://ask.sagemath.org/question/50929/in-sage-is-there-support-for-non-convex-polyhedra/Answer by slelievre for <p>Which reference page gives help or examples? Can they be triangulated? I want to triangulate them in order to help turn them into stl files.</p>
https://ask.sagemath.org/question/50929/in-sage-is-there-support-for-non-convex-polyhedra/?answer=50933#post-id-50933### Export SageMath nonconvex polyhedron to STL
We construct a non-convex polyhedron and export it to STL.
Define a list of vertices given as triples for points in $\mathbb{R}^3$.
v = [( 1, 0, 0), ( 2, 0, -1), ( 3, 0, 0), ( 2, 0, 1),
( 0, 1, 0), ( 0, 2, -1), ( 0, 3, 0), ( 0, 2, 1),
(-1, 0, 0), (-2, 0, -1), (-3, 0, 0), (-2, 0, 1),
( 0, -1, 0), ( 0, -2, -1), ( 0, -3, 0), ( 0, -2, 1)]
Define a list of faces: each face is a tuple of indices
where each index refers to a vertex in the list of vertices.
f = [(4*k + j, 4*((k+1)%4) + j, 4*((k+1)%4) + (j+1)%4, 4*k + (j+1)%4)
for k in range(4) for j in range(4)]
Now define a polyhedron using the function `polygons3d`,
which takes as arguments a list of vertices and a list of faces
as above. In addition, use `threejs_flat_shading=True` for
correct shading in the Three.js rendering.
torus = polygons3d(points=v, faces=f, threejs_flat_shading=True)
View the polyhedron.
torus.show(frame=False)
Save it in STL format (this saves to binary STL).
torus.save('diamond_torus.stl')
To get the ascii STL:
torus_ascii_stl = torus.stl_ascii_string()
print(torus_ascii_stl)
To save the polyhedron to ascii STL, write that ascii string to a file:
with open('diamond_torus_ascii.stl', 'w') as f:
f.write(torus_ascii_stl)
Note also that Blender can be made to use SageMath's Python,
so Python scripting in Blender can use all the power of Sage.
-----
Edit (2021-04-29): support for polyhedral complexes is coming:
- [Sage Trac ticket 31748: PolyhedralComplex](https://trac.sagemath.org/ticket/31748)Wed, 22 Apr 2020 23:43:33 +0200https://ask.sagemath.org/question/50929/in-sage-is-there-support-for-non-convex-polyhedra/?answer=50933#post-id-50933Comment by dart2163 for <h3>Export SageMath nonconvex polyhedron to STL</h3>
<p>We construct a non-convex polyhedron and export it to STL.</p>
<p>Define a list of vertices given as triples for points in $\mathbb{R}^3$.</p>
<pre><code>v = [( 1, 0, 0), ( 2, 0, -1), ( 3, 0, 0), ( 2, 0, 1),
( 0, 1, 0), ( 0, 2, -1), ( 0, 3, 0), ( 0, 2, 1),
(-1, 0, 0), (-2, 0, -1), (-3, 0, 0), (-2, 0, 1),
( 0, -1, 0), ( 0, -2, -1), ( 0, -3, 0), ( 0, -2, 1)]
</code></pre>
<p>Define a list of faces: each face is a tuple of indices
where each index refers to a vertex in the list of vertices.</p>
<pre><code>f = [(4*k + j, 4*((k+1)%4) + j, 4*((k+1)%4) + (j+1)%4, 4*k + (j+1)%4)
for k in range(4) for j in range(4)]
</code></pre>
<p>Now define a polyhedron using the function <code>polygons3d</code>,
which takes as arguments a list of vertices and a list of faces
as above. In addition, use <code>threejs_flat_shading=True</code> for
correct shading in the Three.js rendering.</p>
<pre><code>torus = polygons3d(points=v, faces=f, threejs_flat_shading=True)
</code></pre>
<p>View the polyhedron.</p>
<pre><code>torus.show(frame=False)
</code></pre>
<p>Save it in STL format (this saves to binary STL).</p>
<pre><code>torus.save('diamond_torus.stl')
</code></pre>
<p>To get the ascii STL:</p>
<pre><code>torus_ascii_stl = torus.stl_ascii_string()
print(torus_ascii_stl)
</code></pre>
<p>To save the polyhedron to ascii STL, write that ascii string to a file:</p>
<pre><code>with open('diamond_torus_ascii.stl', 'w') as f:
f.write(torus_ascii_stl)
</code></pre>
<p>Note also that Blender can be made to use SageMath's Python,
so Python scripting in Blender can use all the power of Sage.</p>
<hr>
<p>Edit (2021-04-29): support for polyhedral complexes is coming:</p>
<ul>
<li><a href="https://trac.sagemath.org/ticket/31748">Sage Trac ticket 31748: PolyhedralComplex</a></li>
</ul>
https://ask.sagemath.org/question/50929/in-sage-is-there-support-for-non-convex-polyhedra/?comment=50939#post-id-50939Thank you - this was very helpful.
I did not know I could directly save it in stl format. In my convex examples I was using the function/attribute
"triangulate" and then using a surface_to_stl routine to generate the stl. I notice that this polygon3d does not have a triangulate attribute. Also that the saved stl file is binary - is there a way to get the stl file in text format?Thu, 23 Apr 2020 04:06:05 +0200https://ask.sagemath.org/question/50929/in-sage-is-there-support-for-non-convex-polyhedra/?comment=50939#post-id-50939Comment by slelievre for <h3>Export SageMath nonconvex polyhedron to STL</h3>
<p>We construct a non-convex polyhedron and export it to STL.</p>
<p>Define a list of vertices given as triples for points in $\mathbb{R}^3$.</p>
<pre><code>v = [( 1, 0, 0), ( 2, 0, -1), ( 3, 0, 0), ( 2, 0, 1),
( 0, 1, 0), ( 0, 2, -1), ( 0, 3, 0), ( 0, 2, 1),
(-1, 0, 0), (-2, 0, -1), (-3, 0, 0), (-2, 0, 1),
( 0, -1, 0), ( 0, -2, -1), ( 0, -3, 0), ( 0, -2, 1)]
</code></pre>
<p>Define a list of faces: each face is a tuple of indices
where each index refers to a vertex in the list of vertices.</p>
<pre><code>f = [(4*k + j, 4*((k+1)%4) + j, 4*((k+1)%4) + (j+1)%4, 4*k + (j+1)%4)
for k in range(4) for j in range(4)]
</code></pre>
<p>Now define a polyhedron using the function <code>polygons3d</code>,
which takes as arguments a list of vertices and a list of faces
as above. In addition, use <code>threejs_flat_shading=True</code> for
correct shading in the Three.js rendering.</p>
<pre><code>torus = polygons3d(points=v, faces=f, threejs_flat_shading=True)
</code></pre>
<p>View the polyhedron.</p>
<pre><code>torus.show(frame=False)
</code></pre>
<p>Save it in STL format (this saves to binary STL).</p>
<pre><code>torus.save('diamond_torus.stl')
</code></pre>
<p>To get the ascii STL:</p>
<pre><code>torus_ascii_stl = torus.stl_ascii_string()
print(torus_ascii_stl)
</code></pre>
<p>To save the polyhedron to ascii STL, write that ascii string to a file:</p>
<pre><code>with open('diamond_torus_ascii.stl', 'w') as f:
f.write(torus_ascii_stl)
</code></pre>
<p>Note also that Blender can be made to use SageMath's Python,
so Python scripting in Blender can use all the power of Sage.</p>
<hr>
<p>Edit (2021-04-29): support for polyhedral complexes is coming:</p>
<ul>
<li><a href="https://trac.sagemath.org/ticket/31748">Sage Trac ticket 31748: PolyhedralComplex</a></li>
</ul>
https://ask.sagemath.org/question/50929/in-sage-is-there-support-for-non-convex-polyhedra/?comment=50941#post-id-50941Once the polyhedron has a name (here `torus`), explore its methods with
`torus.<TAB>` where `<TAB>` means hitting the TAB key. Or if you want only
methods starting in stl, type `torus.stl<TAB>`. This reveals the two methods
`.stl_binary` and `.stl_ascii_string`. You can then check their documentation
with `torus.stl_ascii_string?` and their source code with `torus.stl_ascii_string??`.Thu, 23 Apr 2020 04:47:39 +0200https://ask.sagemath.org/question/50929/in-sage-is-there-support-for-non-convex-polyhedra/?comment=50941#post-id-50941