Improve documentation

This commit is contained in:
Elod Csirmaz 2024-12-02 19:39:58 +00:00
parent c4c4b941e9
commit a28b85cca5
3 changed files with 141 additions and 44 deletions

View file

@ -40,21 +40,32 @@ hljs.highlightAll();
<span>(</span><span>size: list | <a title="openscad_py.point.Point" href="point.html#openscad_py.point.Point">Point</a>,<br>center: bool = False)</span>
</code></dt>
<dd>
<div class="desc"><p>A 3D primitive, cube.
Creates a cube in the first octant. When center is true, the cube is centered on the origin.</p>
<p>See <a href="https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/The_OpenSCAD_Language#cube">https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/The_OpenSCAD_Language#cube</a></p></div>
<div class="desc"><p>A 3D primitive, cube.</p>
<p>See <a href="https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/The_OpenSCAD_Language#cube">https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/The_OpenSCAD_Language#cube</a></p>
<p>Creates a cube in the first octant. When <code>center</code> is True, the cube is centered on the origin.</p>
<h2 id="arguments">Arguments</h2>
<ul>
<li>size: a Point object or a list of <code>x, y, z</code> sizes</li>
<li>center: if True, the cube is centered on the origin</li>
</ul></div>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">class Cube(Object):
&#34;&#34;&#34;A 3D primitive, cube.
Creates a cube in the first octant. When center is true, the cube is centered on the origin.
See https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/The_OpenSCAD_Language#cube
&#34;&#34;&#34;
def __init__(self, size: TUnion[list, Point], center: bool = False):
&#34;&#34;&#34;
Creates a cube in the first octant. When `center` is True, the cube is centered on the origin.
Arguments:
- size: a Point object or a list of `x, y, z` sizes
- center: if True, the cube is centered on the origin
&#34;&#34;&#34;
self.size = Point.c(size)
self.center = center

View file

@ -37,44 +37,47 @@ hljs.highlightAll();
<dl>
<dt id="openscad_py.point.Point"><code class="flex name class">
<span>class <span class="ident">Point</span></span>
<span>(</span><span>coords)</span>
<span>(</span><span>coords: List[float])</span>
</code></dt>
<dd>
<div class="desc"><p>Represents a point or vector in arbitrary dimensions</p></div>
<div class="desc"><p>Represents a point or vector in arbitrary number of dimensions</p></div>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">class Point:
&#34;&#34;&#34;Represents a point or vector in arbitrary dimensions&#34;&#34;&#34;
&#34;&#34;&#34;Represents a point or vector in arbitrary number of dimensions&#34;&#34;&#34;
def __init__(self, coords):
def __init__(self, coords: List[float]):
self.c = np.array(coords, dtype=NP_TYPE)
@classmethod
def c(cls, coords: TUnion[list, &#39;Point&#39;]) -&gt; &#39;Point&#39;:
&#34;&#34;&#34;Ensure coords is an instance of Point (idempotent)&#34;&#34;&#34;
def c(cls, coords: TUnion[list[float], &#39;Point&#39;]) -&gt; &#39;Point&#39;:
&#34;&#34;&#34;Ensure `coords` is an instance of Point (idempotent)&#34;&#34;&#34;
if isinstance(coords, Point):
return coords
return Point(coords)
def render(self) -&gt; str:
&#34;&#34;&#34;Render the object into OpenSCAD code&#34;&#34;&#34;
&#34;&#34;&#34;Render the point / vector into OpenSCAD code&#34;&#34;&#34;
return &#34;[&#34; + (&#34;,&#34;.join([str(c) for c in self.c])) + &#34;]&#34;
def render_stl(self) -&gt; str:
&#34;&#34;&#34;Render the point / vector into STL&#34;&#34;&#34;
return &#34; &#34;.join([str(c) for c in self.c])
def scale(self, x: float) -&gt; &#39;Point&#39;:
&#34;&#34;&#34;Scale the current vector/point by a scalar&#34;&#34;&#34;
&#34;&#34;&#34;Scale the current point / vector by the scalar `x`&#34;&#34;&#34;
return self.__class__(self.c * x)
def add(self, p: &#39;Point&#39;) -&gt; &#39;Point&#39;:
&#34;&#34;&#34;Add another point / vector `p` to the current one&#34;&#34;&#34;
assert isinstance(p, Point)
assert self.dim() == p.dim()
return self.__class__(self.c + p.c)
def sub(self, p: &#39;Point&#39;) -&gt; &#39;Point&#39;:
&#34;&#34;&#34;Subtract another point / vector `p` from the current one&#34;&#34;&#34;
assert isinstance(p, Point)
assert self.dim() == p.dim()
return self.__class__(self.c - p.c)
@ -92,17 +95,18 @@ hljs.highlightAll();
return np.sqrt(np.square(self.c).sum())
def norm(self) -&gt; &#39;Point&#39;:
&#34;&#34;&#34;Return a normalized version of the vector (scaled to length 1)&#34;&#34;&#34;
l = self.length()
if l == 0:
raise Exception(&#34;normalising 0 vector&#34;)
raise Exception(&#34;Attempted to normalise 0 vector&#34;)
return self.__class__(self.c / self.length())
def dot(self, p: &#39;Point&#39;) -&gt; float:
&#34;&#34;&#34;Return the dot product&#34;&#34;&#34;
&#34;&#34;&#34;Return the dot product of the current vector and `p`&#34;&#34;&#34;
return np.dot(self.c, p.c)
def cross(self, p: &#39;Point&#39;) -&gt; &#39;Point&#39;:
&#34;&#34;&#34;Return the cross product&#34;&#34;&#34;
&#34;&#34;&#34;Return the cross product of the current vector and `p`&#34;&#34;&#34;
assert self.dim() == 3
assert p.dim() == 3
return Point([
@ -113,25 +117,36 @@ hljs.highlightAll();
])
def eq(self, p: &#39;Point&#39;) -&gt; bool:
&#34;&#34;&#34;Return whether the current point / vector and `p` are equal&#34;&#34;&#34;
return (self.c == p.c).all()
def lt(self, p: &#39;Point&#39;) -&gt; bool:
&#34;&#34;&#34;Return whether the current vector is smaller than `p` in each dimension&#34;&#34;&#34;
return (self.c &lt; p.c).all()
def le(self, p: &#39;Point&#39;) -&gt; bool:
&#34;&#34;&#34;Return whether the current vector is smaller or equal to `p` in each dimension&#34;&#34;&#34;
return (self.c &lt;= p.c).all()
def gt(self, p: &#39;Point&#39;) -&gt; bool:
&#34;&#34;&#34;Return whether the current vector is greater than `p` in each dimension&#34;&#34;&#34;
return (self.c &gt; p.c).all()
def ge(self, p: &#39;Point&#39;) -&gt; bool:
&#34;&#34;&#34;Return whether the current vector is greater or equal to `p` in each dimension&#34;&#34;&#34;
return (self.c &gt;= p.c).all()
def allclose(self, p: &#39;Point&#39;) -&gt; bool:
&#34;&#34;&#34;Return whether the current point / vector and `p` are close to each other&#34;&#34;&#34;
return self.c.shape == p.c.shape and np.allclose(self.c, p.c)
def angle(self, p: &#39;Point&#39;, mode: str = &#34;deg&#34;) -&gt; float:
&#34;&#34;&#34;Return the angle between two vectors in degrees or radians&#34;&#34;&#34;
&#34;&#34;&#34;Return the angle between two vectors in degrees or radians
Arguments:
- p: a Point object
- mode: &#34;deg&#34; | &#34;rad&#34;
&#34;&#34;&#34;
r = self.dot(p)
r = r / self.length() / p.length()
r = math.acos(r)
@ -142,7 +157,11 @@ hljs.highlightAll();
raise ValueError(&#34;Unknown mode&#34;)
def z_slope(self, mode: str = &#34;deg&#34;) -&gt; float:
&#34;&#34;&#34;Return the slope of a vector in degrees or radians&#34;&#34;&#34;
&#34;&#34;&#34;Return the slope of a vector in degrees or radians
Arguments:
- mode: &#34;deg&#34; | &#34;rad&#34;
&#34;&#34;&#34;
r = self.c[2] / self.length()
r = math.asin(r)
if mode == &#34;rad&#34;:
@ -152,7 +171,12 @@ hljs.highlightAll();
raise ValueError(&#34;Unknown mode&#34;)
def rotate(self, coords, angle: float) -&gt; &#39;Point&#39;:
&#34;&#34;&#34;Rotate. coords is a list of 2 coordinate indices that we rotate&#34;&#34;&#34;
&#34;&#34;&#34;Rotate the current vector
Arguments:
- coords: A list of 2 coordinate indices to rotate
- angle: the angle to rotate by, in degrees
&#34;&#34;&#34;
assert len(coords) == 2
ca, cb = coords
s = np.sin(angle / 180. * np.pi)
@ -165,26 +189,33 @@ hljs.highlightAll();
# Operator overloading
def __add__(self, other):
&#34;&#34;&#34;Use `p1 + p2` to add two vectors&#34;&#34;&#34;
return self.add(other)
def __radd__(self, other):
&#34;&#34;&#34;Use `p1 + p2` to add two vectors&#34;&#34;&#34;
assert isinstance(other, Point)
return other.add(self)
def __sub__(self, other):
&#34;&#34;&#34;Use `p1 - p2` to subtract two vectors&#34;&#34;&#34;
return self.sub(other)
def __rsub__(self, other):
&#34;&#34;&#34;Use `p1 - p2` to subtract two vectors&#34;&#34;&#34;
assert isinstance(other, Point)
return other.sub(self)
def __mul__(self, other):
&#34;&#34;&#34;Use `p * x` to scale a vector&#34;&#34;&#34;
return self.scale(other)
def __rmul__(self, other):
&#34;&#34;&#34;Use `x * p` to scale a vector&#34;&#34;&#34;
return self.scale(other)
def __neg__(self):
&#34;&#34;&#34;Use `-p` to negate a vector&#34;&#34;&#34;
return self.scale(-1.)</code></pre>
</details>
<h3>Static methods</h3>
@ -193,34 +224,69 @@ hljs.highlightAll();
<span>def <span class="ident">c</span></span>(<span>coords: list | ForwardRef('<a title="openscad_py.point.Point" href="#openscad_py.point.Point">Point</a>')) > <a title="openscad_py.point.Point" href="#openscad_py.point.Point">Point</a></span>
</code></dt>
<dd>
<div class="desc"><p>Ensure coords is an instance of Point (idempotent)</p></div>
<div class="desc"><p>Ensure <code>coords</code> is an instance of Point (idempotent)</p></div>
</dd>
</dl>
<h3>Methods</h3>
<dl>
<dt id="openscad_py.point.Point.__add__"><code class="name flex">
<span>def <span class="ident">__add__</span></span>(<span>self, other)</span>
</code></dt>
<dd>
<div class="desc"><p>Use <code>p1 + p2</code> to add two vectors</p></div>
</dd>
<dt id="openscad_py.point.Point.__mul__"><code class="name flex">
<span>def <span class="ident">__mul__</span></span>(<span>self, other)</span>
</code></dt>
<dd>
<div class="desc"><p>Use <code>p * x</code> to scale a vector</p></div>
</dd>
<dt id="openscad_py.point.Point.__neg__"><code class="name flex">
<span>def <span class="ident">__neg__</span></span>(<span>self)</span>
</code></dt>
<dd>
<div class="desc"><p>Use <code>-p</code> to negate a vector</p></div>
</dd>
<dt id="openscad_py.point.Point.__rmul__"><code class="name flex">
<span>def <span class="ident">__rmul__</span></span>(<span>self, other)</span>
</code></dt>
<dd>
<div class="desc"><p>Use <code>x * p</code> to scale a vector</p></div>
</dd>
<dt id="openscad_py.point.Point.__sub__"><code class="name flex">
<span>def <span class="ident">__sub__</span></span>(<span>self, other)</span>
</code></dt>
<dd>
<div class="desc"><p>Use <code>p1 - p2</code> to subtract two vectors</p></div>
</dd>
<dt id="openscad_py.point.Point.add"><code class="name flex">
<span>def <span class="ident">add</span></span>(<span>self,<br>p: <a title="openscad_py.point.Point" href="#openscad_py.point.Point">Point</a>) > <a title="openscad_py.point.Point" href="#openscad_py.point.Point">Point</a></span>
</code></dt>
<dd>
<div class="desc"></div>
<div class="desc"><p>Add another point / vector <code>p</code> to the current one</p></div>
</dd>
<dt id="openscad_py.point.Point.allclose"><code class="name flex">
<span>def <span class="ident">allclose</span></span>(<span>self,<br>p: <a title="openscad_py.point.Point" href="#openscad_py.point.Point">Point</a>) > bool</span>
</code></dt>
<dd>
<div class="desc"></div>
<div class="desc"><p>Return whether the current point / vector and <code>p</code> are close to each other</p></div>
</dd>
<dt id="openscad_py.point.Point.angle"><code class="name flex">
<span>def <span class="ident">angle</span></span>(<span>self,<br>p: <a title="openscad_py.point.Point" href="#openscad_py.point.Point">Point</a>,<br>mode: str = 'deg') > float</span>
</code></dt>
<dd>
<div class="desc"><p>Return the angle between two vectors in degrees or radians</p></div>
<div class="desc"><p>Return the angle between two vectors in degrees or radians</p>
<h2 id="arguments">Arguments</h2>
<ul>
<li>p: a Point object</li>
<li>mode: "deg" | "rad"</li>
</ul></div>
</dd>
<dt id="openscad_py.point.Point.cross"><code class="name flex">
<span>def <span class="ident">cross</span></span>(<span>self,<br>p: <a title="openscad_py.point.Point" href="#openscad_py.point.Point">Point</a>) > <a title="openscad_py.point.Point" href="#openscad_py.point.Point">Point</a></span>
</code></dt>
<dd>
<div class="desc"><p>Return the cross product</p></div>
<div class="desc"><p>Return the cross product of the current vector and <code>p</code></p></div>
</dd>
<dt id="openscad_py.point.Point.dim"><code class="name flex">
<span>def <span class="ident">dim</span></span>(<span>self) > int</span>
@ -232,25 +298,25 @@ hljs.highlightAll();
<span>def <span class="ident">dot</span></span>(<span>self,<br>p: <a title="openscad_py.point.Point" href="#openscad_py.point.Point">Point</a>) > float</span>
</code></dt>
<dd>
<div class="desc"><p>Return the dot product</p></div>
<div class="desc"><p>Return the dot product of the current vector and <code>p</code></p></div>
</dd>
<dt id="openscad_py.point.Point.eq"><code class="name flex">
<span>def <span class="ident">eq</span></span>(<span>self,<br>p: <a title="openscad_py.point.Point" href="#openscad_py.point.Point">Point</a>) > bool</span>
</code></dt>
<dd>
<div class="desc"></div>
<div class="desc"><p>Return whether the current point / vector and <code>p</code> are equal</p></div>
</dd>
<dt id="openscad_py.point.Point.ge"><code class="name flex">
<span>def <span class="ident">ge</span></span>(<span>self,<br>p: <a title="openscad_py.point.Point" href="#openscad_py.point.Point">Point</a>) > bool</span>
</code></dt>
<dd>
<div class="desc"></div>
<div class="desc"><p>Return whether the current vector is greater or equal to <code>p</code> in each dimension</p></div>
</dd>
<dt id="openscad_py.point.Point.gt"><code class="name flex">
<span>def <span class="ident">gt</span></span>(<span>self,<br>p: <a title="openscad_py.point.Point" href="#openscad_py.point.Point">Point</a>) > bool</span>
</code></dt>
<dd>
<div class="desc"></div>
<div class="desc"><p>Return whether the current vector is greater than <code>p</code> in each dimension</p></div>
</dd>
<dt id="openscad_py.point.Point.is_zero"><code class="name flex">
<span>def <span class="ident">is_zero</span></span>(<span>self) > bool</span>
@ -262,7 +328,7 @@ hljs.highlightAll();
<span>def <span class="ident">le</span></span>(<span>self,<br>p: <a title="openscad_py.point.Point" href="#openscad_py.point.Point">Point</a>) > bool</span>
</code></dt>
<dd>
<div class="desc"></div>
<div class="desc"><p>Return whether the current vector is smaller or equal to <code>p</code> in each dimension</p></div>
</dd>
<dt id="openscad_py.point.Point.length"><code class="name flex">
<span>def <span class="ident">length</span></span>(<span>self) > float</span>
@ -274,49 +340,58 @@ hljs.highlightAll();
<span>def <span class="ident">lt</span></span>(<span>self,<br>p: <a title="openscad_py.point.Point" href="#openscad_py.point.Point">Point</a>) > bool</span>
</code></dt>
<dd>
<div class="desc"></div>
<div class="desc"><p>Return whether the current vector is smaller than <code>p</code> in each dimension</p></div>
</dd>
<dt id="openscad_py.point.Point.norm"><code class="name flex">
<span>def <span class="ident">norm</span></span>(<span>self) > <a title="openscad_py.point.Point" href="#openscad_py.point.Point">Point</a></span>
</code></dt>
<dd>
<div class="desc"></div>
<div class="desc"><p>Return a normalized version of the vector (scaled to length 1)</p></div>
</dd>
<dt id="openscad_py.point.Point.render"><code class="name flex">
<span>def <span class="ident">render</span></span>(<span>self) > str</span>
</code></dt>
<dd>
<div class="desc"><p>Render the object into OpenSCAD code</p></div>
<div class="desc"><p>Render the point / vector into OpenSCAD code</p></div>
</dd>
<dt id="openscad_py.point.Point.render_stl"><code class="name flex">
<span>def <span class="ident">render_stl</span></span>(<span>self) > str</span>
</code></dt>
<dd>
<div class="desc"></div>
<div class="desc"><p>Render the point / vector into STL</p></div>
</dd>
<dt id="openscad_py.point.Point.rotate"><code class="name flex">
<span>def <span class="ident">rotate</span></span>(<span>self, coords, angle: float) > <a title="openscad_py.point.Point" href="#openscad_py.point.Point">Point</a></span>
</code></dt>
<dd>
<div class="desc"><p>Rotate. coords is a list of 2 coordinate indices that we rotate</p></div>
<div class="desc"><p>Rotate the current vector</p>
<h2 id="arguments">Arguments</h2>
<ul>
<li>coords: A list of 2 coordinate indices to rotate</li>
<li>angle: the angle to rotate by, in degrees</li>
</ul></div>
</dd>
<dt id="openscad_py.point.Point.scale"><code class="name flex">
<span>def <span class="ident">scale</span></span>(<span>self, x: float) > <a title="openscad_py.point.Point" href="#openscad_py.point.Point">Point</a></span>
</code></dt>
<dd>
<div class="desc"><p>Scale the current vector/point by a scalar</p></div>
<div class="desc"><p>Scale the current point / vector by the scalar <code>x</code></p></div>
</dd>
<dt id="openscad_py.point.Point.sub"><code class="name flex">
<span>def <span class="ident">sub</span></span>(<span>self,<br>p: <a title="openscad_py.point.Point" href="#openscad_py.point.Point">Point</a>) > <a title="openscad_py.point.Point" href="#openscad_py.point.Point">Point</a></span>
</code></dt>
<dd>
<div class="desc"></div>
<div class="desc"><p>Subtract another point / vector <code>p</code> from the current one</p></div>
</dd>
<dt id="openscad_py.point.Point.z_slope"><code class="name flex">
<span>def <span class="ident">z_slope</span></span>(<span>self, mode: str = 'deg') > float</span>
</code></dt>
<dd>
<div class="desc"><p>Return the slope of a vector in degrees or radians</p></div>
<div class="desc"><p>Return the slope of a vector in degrees or radians</p>
<h2 id="arguments">Arguments</h2>
<ul>
<li>mode: "deg" | "rad"</li>
</ul></div>
</dd>
</dl>
</dd>
@ -338,6 +413,11 @@ hljs.highlightAll();
<li>
<h4><code><a title="openscad_py.point.Point" href="#openscad_py.point.Point">Point</a></code></h4>
<ul class="two-column">
<li><code><a title="openscad_py.point.Point.__add__" href="#openscad_py.point.Point.__add__">__add__</a></code></li>
<li><code><a title="openscad_py.point.Point.__mul__" href="#openscad_py.point.Point.__mul__">__mul__</a></code></li>
<li><code><a title="openscad_py.point.Point.__neg__" href="#openscad_py.point.Point.__neg__">__neg__</a></code></li>
<li><code><a title="openscad_py.point.Point.__rmul__" href="#openscad_py.point.Point.__rmul__">__rmul__</a></code></li>
<li><code><a title="openscad_py.point.Point.__sub__" href="#openscad_py.point.Point.__sub__">__sub__</a></code></li>
<li><code><a title="openscad_py.point.Point.add" href="#openscad_py.point.Point.add">add</a></code></li>
<li><code><a title="openscad_py.point.Point.allclose" href="#openscad_py.point.Point.allclose">allclose</a></code></li>
<li><code><a title="openscad_py.point.Point.angle" href="#openscad_py.point.Point.angle">angle</a></code></li>

View file

@ -41,24 +41,30 @@ hljs.highlightAll();
</code></dt>
<dd>
<div class="desc"><p>A 3D primitive, a polyhedron defined by a list of points and faces.
Faces are defined by lists of point indices. The points of a face must be listed clockwise when
looking at the face from the outside inward.
Nonplanar faces should be triangulated by OpenSCAD.</p>
<p>See <a href="https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/The_OpenSCAD_Language#polyhedron">https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/The_OpenSCAD_Language#polyhedron</a></p></div>
Nonplanar faces will be triangulated by OpenSCAD.</p>
<p>See <a href="https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/The_OpenSCAD_Language#polyhedron">https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/The_OpenSCAD_Language#polyhedron</a></p>
<h2 id="arguments">Arguments</h2>
<ul>
<li>points: a list of Point objects or coordinate tuples defining the vertices</li>
<li>faces: defines the faces as a list of lists of vertex indices. The points of a face must be listed clockwise when looking at the face from the outside inward.</li>
</ul></div>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">class Polyhedron(Object):
&#34;&#34;&#34;A 3D primitive, a polyhedron defined by a list of points and faces.
Faces are defined by lists of point indices. The points of a face must be listed clockwise when
looking at the face from the outside inward.
Nonplanar faces should be triangulated by OpenSCAD.
Nonplanar faces will be triangulated by OpenSCAD.
See https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/The_OpenSCAD_Language#polyhedron
&#34;&#34;&#34;
def __init__(self, points: List[TUnion[list, Point]], faces: List[list], convexity: int = 10):
&#34;&#34;&#34;
Arguments:
- points: a list of Point objects or coordinate tuples defining the vertices
- faces: defines the faces as a list of lists of vertex indices. The points of a face must be listed clockwise when looking at the face from the outside inward.
&#34;&#34;&#34;
self.points = [Point.c(p) for p in points]
self.faces = faces
self.convexity = convexity