cwant / tessagon

Tessellate your favorite 2D manifolds with triangles, hexagons, and other interesting patterns.
Apache License 2.0
213 stars 17 forks source link
blender hacktoberfest python

tessagon: tessellation / tiling with python

Tessellate your favorite 3D surfaces (technically, 2D manifolds) with triangles, hexagons, or a number of other curated tiling types!

Animation of tessellated torii

News

A nascent Inkscape extension to create tiling patterns: https://github.com/cwant/inkscape-tiling-extension

You can find a Blender (3.6) pull request to add support for Tessagon in the "XYZ function" addon here: https://projects.blender.org/blender/blender-addons/pulls/104726

Check out my presentation on the creation of Tessagon as an open source Python project here: https://cwant.github.io/python-os-presentation (or watch on YouTube)

A demo of using Tessagon with Inkscape Simple Scripting can be found here.

TL;DR

Check out the repository and look in the demo directory.

This software may also be installed via pip:

python3 -m pip install tessagon

or

pip3 install tessagon

How it works

Three things are needed to use tessagon to tessellate the surface of a 2D-manifold (or more accurately, a patch on a 2D-manifold in 3-space):

The reader should check out the demos, but here is some very basic usage using blender:

from tessagon.types.hex_tessagon import HexTessagon
from tessagon.adaptors.blender_adaptor import BlenderAdaptor

def my_func(u,v):
  return [u, v, u**2 + v**2]

options = {
    'function': my_func,
    'u_range': [0.0, 1.0],
    'v_range': [0.0, 1.0],
    'u_num': 8,
    'v_num': 20,
    'u_cyclic': False,
    'v_cyclic': False,
    'adaptor_class' : BlenderAdaptor
  }
tessagon = HexTessagon(**options)

bmesh = tessagon.create_mesh()

# Do something with the bmesh ...

Tessagon classes

Additional tessagon classes can be added by deconstructing how a tessellation fits within a rectangular patch in the plane (check out the ASCII art in each source file in tessagon.types). The current Tessagon subclasses include:

Regular tilings

Archimedean tilings

Laves tilings

Non-edge-to-edge tilings

Non-convex tilings

Usage and Options

Each tessagon class is initialized with number of keyword options, e.g.:

from tessagon.types.dodeca_tessagon import DodecaTessagon
from tessagon.adaptors.vtk_adaptor import VtkAdaptor
tessagon = DodecaTessagon(function=my_func,
                          u_range=[0.0, 1.0],
                          v_range=[0.0, 1.0],
                          u_num=8,
                          v_num=20,
                          u_cyclic=True,
                          v_cyclic=False,
                          adaptor_class=VtkAdaptor)
poly_data = tessagon.create_mesh()

The create_mesh() method creates a tessellated mesh using your provided function and the tile type corresponding to the tessagon class used. The chosen adaptor dictates the 3D data type the mesh will be (for the BlenderAdaptor the output is BMesh, for the VTKAdaptor the output is vtkPolyData).

Each Tessagon class may supply options that are specific to that class (they will be listed on the documentation page for each class).

Here are the options that are common to all Tessagon classes:

tiles

As you work with the software, keep in mind the difference between a face and a tile. A tile is not a face! A tile is a four-sided region (often a rectangle) that holds one or more faces that form a repeated pattern. A tile often shares faces with neighboring tiles:

tiles vs faces

The source code files for each tessagon class usually contain some ASCII art that illustrates how the pattern of faces is arranged on a tile.

corners

Writing your own tessellation classes

All tesselations are found in the types module, so check out the source code there for numerous examples. The source code documentation in hex_tessagon is more verbose than the others, which hopefully will aid understanding.

Each tessellation involve two classes: a tile class (a child of Tile) and a child of class Tessagon. The Tessagon subclass is easy (it just declares which tile class with be used), so writing the tile class will take most of your time. (The FloretTessagon is an exception because the math is more complex.)

There are five methods that you will want to write:

Metadata and Discovery

Each Tessagon subclass has some metadata attached to it (class TessagonMetadata) that describes the properties of the tiling. This metadata will be expanded as needs require, but currently consists of information about how many color patterns the tiling has, what type (regular, archimedean, laves, non_edge, non_convex), and what sorts of shapes are produced by instances of the class.

A nascent helper class tessagon.TessagonDiscovery exists that can help you search for tilings with certain properties (for example, this could be used to create a menu for an external application that categorizes the tilings). The methods in the class are intended to support chaining of operations (e.g., they results returned are also of type TessagonDiscovery, that can then be reified using the to_list method). Some examples:

find_all = TessagonDiscovery()
find_all.to_list() # a list of all of the tessagons
regular = find_all.with_classification('regular')
regular.to_list() # The three regular tilings (HexTessagon, SquareTessagon, TriTessagon)
regular.inverse().to_list() # All tilings except for the regular ones

Checkout the test suite for more example usage.

The TessagonDiscovery class also has a class method get_class for fetching tessagon classes without having to deal with importing that looks like this:

tessagon_cls = TessagonDiscovery.get_class('TriTessagon')
tessagon = tessagon_cls(function=my_func, ...)

wire_skin

Check out my other project wire_skin to add some interesting effects to the tessellated manifolds you create:

https://github.com/cwant/wire_skin

wire_skin

3D Printing on Shapeways

I primarily use Tessagon to create objects for 3D printing (on Shapeways). I tag my models with the keyword tessagon. If you also use Shapeways to print models made with Tessagon, feel free to use this tag -- I'd be very interested to see what you make!

https://www.shapeways.com/marketplace?q=tessagon

3D printed DodecaHexTriTessagon