Cylinder

Qualified name: algan.mobs.shapes\_3d.Cylinder

class Cylinder(radius=1, height=1, direction=tensor([0., 1., 0.]), v_range=(0, 360), closed=False, resolution=None, *args, **kwargs)[source]

Bases: Surface

A cylinder, tessellated from a Surface.

Only the curved side is built by default; closed adds the two end discs as children and as Scene actors (add_bases() does the same after construction).

Parameters:
  • radius – Radius in world units. Defaults to 1.

  • height – Length along direction, in world units. Defaults to 1.

  • direction – Axis the cylinder runs along, shape (*, 3); it need not be normalized. Defaults to UP (the +y axis).

  • v_range – Azimuthal sweep around the cylinder’s axis, in degrees. The sweep starts on the side the cylinder’s forward direction points at (INWARD, for the default direction=UP) and turns toward its left, so (0, 180) builds the LEFT half of the tube. Defaults to (0, 360), the closed tube. The extent along the axis comes from height.

  • closed – Whether to close both ends with flat discs – lit triangle meshes carrying the tube’s own mesh identity, so each rim is an interior edge of one surface. Defaults to False: the tube is open at both ends. The discs are whole circles even when v_range is partial, matching Manim. Each disc samples its rim more finely than the tube samples its rings – it has to, since a flat disc’s boundary cannot be refined at render time the way the curved tube’s is – so a capped cylinder carries more triangles than the tube alone suggests.

  • resolution – Manim-style grid resolution as (u_patches, v_patches), or one int for both; each value becomes grid_width/grid_height plus one, since Manim counts patches and Algan counts vertices. Defaults to None, meaning Algan sizes the grid itself from geometry_tolerance.

  • *args – Passed to Surface – notably color, and color_texture for the two-tone styling Manim spells checkerboard_colors: color_texture=get_checkerboard((BLUE, BLUE_E)).

  • **kwargs – Passed to Surface – notably color, and color_texture for the two-tone styling Manim spells checkerboard_colors: color_texture=get_checkerboard((BLUE, BLUE_E)).

Notes

A partial v_range builds an open half-pipe, not a solid: the cut runs the length of the tube and the inside of the surface shows through it. The shape is re-tessellated from scratch, and since the tessellation search only promises geometry_tolerance, a partial sweep can end up with a denser grid than the closed tube.

Examples

A capped cylinder lying along the screen’s x axis:

Example: Example1Cylinder

../_images/Example1Cylinder-1.png
from algan import *

Cylinder(radius=0.4, height=1.6, direction=RIGHT, closed=True).spawn()

Scene.save_video()

Half a tube, cut along its length and turned so the shell faces the camera (unrotated, the cut runs straight through the line of sight and the half reads as a flat panel):

Example: Example2Cylinder

../_images/Example2Cylinder-1.png
from algan import *

Cylinder(radius=0.7, height=1.4, v_range=(0, PI),
         color=BLUE).rotate(-90, UP).spawn()

Scene.save_video()

Methods

add_bases

coord_function

Map the surface's (u, v) parameters to positions in space.

move_between_points

set_direction

set_end_point

set_start_point

Attributes

animation_manager

This mob's scene-owned animation manager.

basis

The Mob's orientation and scale, as a flattened 3x3 matrix of shape (*, 9).

casts_shadows

Whether this Mob's geometry blocks light on its way from a light source to another surface -- whether it casts a shadow.

children

The Mobs attached below this one, in attachment order.

closed_shell

Whether this Mob's triangles form a CLOSED shell -- every camera ray that enters the geometry crosses a second time on its way out.

color_texture

An image painted across the surface, as an [W, H, 5] RGBA+glow tensor.

draws_descendants

Whether get_render_primitives returns geometry belonging to this Mob's DESCENDANTS as well as its own.

forward

Get the direction the Mob is facing.

geometry_tolerance

How far this surface's mesh may sit from the exact shape, in world units.

lifespan

This mob's [spawn, despawn) interval on its Scene timeline (a Lifespan).

location

The Mob's position in world space, shape (*, 3).

normalized_basis

The Mob's orientation with scale divided out, shape (*, 9).

parents

The Mobs this one is attached to, in attachment order.

receives_shadows

Whether this Mob's surfaces are darkened by shadows cast onto them.

render_tolerance_pixels

How far a drawn triangle may sit from the true surface, in pixels.

right

Get the Mob's own rightward direction.

scale_coefficient

The Mob's scale along its own right, up and forward axes, shape (*, 3).

two_sided

Whether this Mob's geometry should be lit from whichever side the ray arrives on.

up

Get the Mob's own upward direction.

vertices

The surface's vertex positions, shape (*, grid_width * grid_height, 3).

x

The Mob's x coordinate in world units, shape (*, 1).

xy

The Mob's x and y coordinates in world units, shape (*, 2).

y

The Mob's y coordinate in world units, shape (*, 1).

z

The Mob's z coordinate in world units, shape (*, 1).

coord_function(uv)[source]

Map the surface’s (u, v) parameters to positions in space.

This is what defines the surface’s shape, and what each 3-D shape class overrides: Sphere maps the unit square onto a sphere, Torus onto a torus, and so on. The base implementation gives a flat plane spanning [-1, 1] on both axes.

Parameters:

uv – Parameter coordinates to map, shape (*, 2), with both components in [0, 1].

Returns:

Positions relative to the surface’s location, shape (*, 3).

Return type:

torch.Tensor

two_sided = False

Whether this Mob’s geometry should be lit from whichever side the ray arrives on. True (the default) is for geometry with no meaningful outside – a 2-D shape, Text, a parametric Surface, an imported mesh whose winding nobody has checked – where a back-facing hit is shaded with its normal flipped toward the viewer, so the surface is lit from behind instead of coming out black.

The built-in solids set it False: their normals face out (see tests/unit_tests/test_normal_orientation.py), so a back-facing hit is genuinely the inside of the solid and is shaded as such. That is what stops a half-transparent solid’s far shell from being lit like a second front shell – the bright and dark “planes” through a fading Octahedron. Set it True on an instance to get the old two-sided lighting back (an open Cone you want lit inside, say); it must be set before the Mob is spawned, since the render primitive reads it once.