different large refactorings (subdirectores, removed obsolete stuff) and

bug fixes
This commit is contained in:
2020-08-31 21:25:41 +02:00
parent 7aeae6fc55
commit ffcb5ed744
2250 changed files with 764 additions and 142723 deletions

View File

@ -0,0 +1,194 @@
#!/usr/bin/env python3
# Generate Apollonian Gaskets -- the math part.
# Copyright (c) 2014 Ludger Sandig
# This file is part of apollon.
# Apollon is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# Apollon is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with Apollon. If not, see <http://www.gnu.org/licenses/>.
from cmath import *
import random
class Circle(object):
"""
A circle represented by center point as complex number and radius.
"""
def __init__ ( self, mx, my, r ):
"""
@param mx: x center coordinate
@type mx: int or float
@param my: y center coordinate
@type my: int or float
@param r: radius
@type r: int or float
"""
self.r = r
self.m = (mx +my*1j)
def __repr__ ( self ):
"""
Pretty printing
"""
return "Circle( self, %s, %s, %s )" % (self.m.real, self.m.imag, self.r)
def __str__ ( self ):
"""
Pretty printing
"""
return "Circle x:%.3f y:%.3f r:%.3f [cur:%.3f]" % (self.m.real, self.m.imag, self.r.real, self.curvature().real)
def curvature (self):
"""
Get circle's curvature.
@rtype: float
@return: Curvature of the circle.
"""
return 1/self.r
def outerTangentCircle( circle1, circle2, circle3 ):
"""
Takes three externally tangent circles and calculates the fourth one enclosing them.
@param circle1: first circle
@param circle2: second circle
@param circle3: third circle
@type circle1: L{Circle}
@type circle2: L{Circle}
@type circle3: L{Circle}
@return: The enclosing circle
@rtype: L{Circle}
"""
cur1 = circle1.curvature()
cur2 = circle2.curvature()
cur3 = circle3.curvature()
m1 = circle1.m
m2 = circle2.m
m3 = circle3.m
cur4 = -2 * sqrt( cur1*cur2 + cur2*cur3 + cur1 * cur3 ) + cur1 + cur2 + cur3
m4 = ( -2 * sqrt( cur1*m1*cur2*m2 + cur2*m2*cur3*m3 + cur1*m1*cur3*m3 ) + cur1*m1 + cur2*m2 + cur3*m3 ) / cur4
circle4 = Circle( m4.real, m4.imag, 1/cur4 )
return circle4
def tangentCirclesFromRadii( r2, r3, r4 ):
"""
Takes three radii and calculates the corresponding externally
tangent circles as well as a fourth one enclosing them. The enclosing
circle is the first one.
@param r2, r3, r4: Radii of the circles to calculate
@type r2: int or float
@type r3: int or float
@type r4: int or float
@return: The four circles, where the first one is the enclosing one.
@rtype: (L{Circle}, L{Circle}, L{Circle}, L{Circle})
"""
circle2 = Circle( 0, 0, r2 )
circle3 = Circle( r2 + r3, 0, r3 )
m4x = (r2*r2 + r2*r4 + r2*r3 - r3*r4) / (r2 + r3)
m4y = sqrt( (r2 + r4) * (r2 + r4) - m4x*m4x )
circle4 = Circle( m4x, m4y, r4 )
circle1 = outerTangentCircle( circle2, circle3, circle4 )
return ( circle1, circle2, circle3, circle4 )
def secondSolution( fixed, c1, c2, c3 ):
"""
If given four tangent circles, calculate the other one that is tangent
to the last three.
@param fixed: The fixed circle touches the other three, but not
the one to be calculated.
@param c1, c2, c3: Three circles to which the other tangent circle
is to be calculated.
@type fixed: L{Circle}
@type c1: L{Circle}
@type c2: L{Circle}
@type c3: L{Circle}
@return: The circle.
@rtype: L{Circle}
"""
curf = fixed.curvature()
cur1 = c1.curvature()
cur2 = c2.curvature()
cur3 = c3.curvature()
curn = 2 * (cur1 + cur2 + cur3) - curf
mn = (2 * (cur1*c1.m + cur2*c2.m + cur3*c3.m) - curf*fixed.m ) / curn
return Circle( mn.real, mn.imag, 1/curn )
class ApollonianGasket(object):
"""
Container for an Apollonian Gasket.
"""
def __init__(self, c1, c2, c3):
"""
Creates a basic apollonian Gasket with four circles.
@param c1, c2, c3: The curvatures of the three inner circles of the
starting set (i.e. depth 0 of the recursion). The fourth,
enclosing circle will be calculated from them.
@type c1: int or float
@type c2: int or float
@type c3: int or float
"""
self.start = tangentCirclesFromRadii( 1/c1, 1/c2, 1/c3 )
self.genCircles = list(self.start)
def recurse(self, circles, depth, maxDepth):
"""Recursively calculate the smaller circles of the AG up to the
given depth. Note that for depth n we get 2*3^{n+1} circles.
@param maxDepth: Maximal depth of the recursion.
@type maxDepth: int
@param circles: 4-Tuple of circles for which the second
solutions are calculated
@type circles: (L{Circle}, L{Circle}, L{Circle}, L{Circle})
@param depth: Current depth
@type depth: int
"""
if( depth == maxDepth ):
return
(c1, c2, c3, c4) = circles
if( depth == 0 ):
# First recursive step, this is the only time we need to
# calculate 4 new circles.
del self.genCircles[4:]
cspecial = secondSolution( c1, c2, c3, c4 )
self.genCircles.append( cspecial )
self.recurse( (cspecial, c2, c3, c4), 1, maxDepth )
cn2 = secondSolution( c2, c1, c3, c4 )
self.genCircles.append( cn2 )
cn3 = secondSolution( c3, c1, c2, c4 )
self.genCircles.append( cn3 )
cn4 = secondSolution( c4, c1, c2, c3 )
self.genCircles.append( cn4 )
self.recurse( (cn2, c1, c3, c4), depth+1, maxDepth )
self.recurse( (cn3, c1, c2, c4), depth+1, maxDepth )
self.recurse( (cn4, c1, c2, c3), depth+1, maxDepth )
def generate(self, depth):
"""
Wrapper for the recurse function. Generate the AG,
@param depth: Recursion depth of the Gasket
@type depth: int
"""
self.recurse(self.start, 0, depth)

View File

@ -0,0 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>
<inkscape-extension xmlns="http://www.inkscape.org/namespace/inkscape/extension">
<name>Apollonian Gasket</name>
<id>fablabchemnitz.de.apollonian</id>
<param name="active_tab" type="notebook">
<page name="title" gui-text="Settings">
<param name="depth" type="int" min="2" max="7" gui-text="Depth">3</param>
<param name="c1" type="float" min="0.1" max="10.0" precision="2" gui-text="c1">2.0</param>
<param name="c2" type="float" min="0.1" max="10.0" precision="2" gui-text="c2">3.0</param>
<param name="c3" type="float" min="0.1" max="10.0" precision="2" gui-text="c3">3.0</param>
<param name="shrink" type="bool" gui-text="shrink circles for cutting">true</param>
</page>
<page name="Usage1" gui-text="Usage">
<param name="use1" type="description" xml:space="preserve">
Make an apollonian gasket:
Depth = depth in search tree
c1,c2,c3 = curvatures of first 3 osculating circles
See https://en.wikipedia.org/wiki/Apollonian_gasket
for details of construction.
</param>
</page>
</param>
<effect>
<object-type>all</object-type>
<effects-menu>
<submenu name="FabLab Chemnitz">
<submenu name="Shape/Pattern from Generator"/>
</submenu>
</effects-menu>
</effect>
<script>
<command location="inx" interpreter="python">fablabchemnitz_apollonian.py</command>
</script>
</inkscape-extension>

View File

@ -0,0 +1,96 @@
#!/usr/bin/env python3
import inkex
import fablabchemnitz_apolloniangasket_func
from lxml import etree
__version__ = '0.0'
def cplxs2pts(zs):
tt = []
for z in zs:
tt.extend([z.real,z.imag])
return tt
def draw_SVG_circle(parent, r, cx, cy, name):
" structre an SVG circle entity under parent "
circ_attribs = { 'cx': str(cx), 'cy': str(cy),
'r': str(r),
inkex.addNS('label','inkscape'): name}
circle = etree.SubElement(parent, inkex.addNS('circle','svg'), circ_attribs )
class Gasket(inkex.Effect): # choose a better name
def __init__(self):
" define how the options are mapped from the inx file "
inkex.Effect.__init__(self) # initialize the super class
# list of parameters defined in the .inx file
self.arg_parser.add_argument("--depth",type=int, default=3, help="command line help")
self.arg_parser.add_argument("--c1", type=float, default=2.0, help="command line help")
self.arg_parser.add_argument("--c2", type=float, default=3.0, help="command line help")
self.arg_parser.add_argument("--c3", type=float, default=3.0, help="command line help")
self.arg_parser.add_argument("--shrink", type=inkex.Boolean, default=True, help="command line help")
self.arg_parser.add_argument("--active_tab", default='title', help="Active tab.")
def calc_unit_factor(self):
unit_factor = self.svg.unittouu(str(1.0) + self.options.units)
return unit_factor
### -------------------------------------------------------------------
### Main function and is called when the extension is run.
def effect(self):
#set up path styles
path_stroke = '#DD0000' # take color from tab3
path_fill = 'none' # no fill - just a line
path_stroke_width = self.svg.unittouu(str(0.1) + "mm")
page_id = self.options.active_tab # sometimes wrong the very first time
style_curve = { 'stroke': path_stroke,
'fill': 'none',
'stroke-width': path_stroke_width }
# This finds center of current view in inkscape
t = 'translate(%s,%s)' % (self.svg.namedview.center[0], self.svg.namedview.center[1] )
# add a group to the document's current layer
#all the circles inherit style from this group
g_attribs = { inkex.addNS('label','inkscape'): 'zengon' + "_%d"%(self.options.depth),
inkex.addNS('transform-center-x','inkscape'): str(0),
inkex.addNS('transform-center-y','inkscape'): str(0),
'transform': t,
'style' : str(inkex.Style((style_curve))),
'info':'N: '}
topgroup = etree.SubElement(self.svg.get_current_layer(), 'g', g_attribs )
circles = fablabchemnitz_apolloniangasket_func.main(c1=self.options.c1,
c2=self.options.c2,
c3=self.options.c3,
depth=self.options.depth)
#shrink the circles so they don't touch
#useful for laser cutting
if self.options.shrink:
circles = circles[1:]
for cc in circles:
cc.r = abs(cc.r)
if cc.r >.5:
cc.r -= .1
else:
cc.r *= .9
scale_factor = 200
for c in circles:
cx, cy, r = c.m.real, c.m.imag, abs(c.r)
#rescale and add circle to document
cx, cy, r = scale_factor*cx , scale_factor*cy, scale_factor*r
draw_SVG_circle(topgroup,r,cx,cy,'apo')
if __name__ == '__main__':
Gasket().run()

View File

@ -0,0 +1,112 @@
#!/usr/bin/env python3
# Command line program to create svg apollonian circles
# Copyright (c) 2014 Ludger Sandig
# This file is part of apollon.
# Apollon is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# Apollon is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with Apollon. If not, see <http://www.gnu.org/licenses/>.
import math
from fablabchemnitz_apollon import ApollonianGasket
def ag_to_svg(circles, colors, tresh=0.00005):
"""
Convert a list of circles to svg, optionally color them.
@param circles: A list of L{Circle}s
@param colors: A L{ColorMap} object
@param tresh: Only circles with a radius greater than the product of tresh and maximal radius are saved
"""
svg = []
tresh = .000005
print ('>>', tresh)
# Find the biggest circle, which hopefully is the enclosing one
# and has a negative radius because of this. Note that this does
# not have to be the case if we picked an unlucky set of radii at
# the start. If that was the case, we're screwed now.
big = min(circles, key=lambda c: c.r.real)
# Move biggest circle to front so it gets drawn first
circles.remove(big)
circles.insert(0, big)
if big.r.real < 0:
# Bounding box from biggest circle, lower left corner and two
# times the radius as width
corner = big.m - ( abs(big.r) + abs(big.r) * 1j )
vbwidth = abs(big.r)*2
width = 500 # Hardcoded!
# Line width independent of circle size
lw = (vbwidth/width)
svg.append('<svg xmlns="http://www.w3.org/2000/svg" width="%f" height="%f" viewBox="%f %f %f %f">\n' % (width, width, corner.real, corner.imag, vbwidth, vbwidth))
# Keep stroke width relative
svg.append('<g stroke-width="%f">\n' % lw)
# Iterate through circle list, circles with radius<radmin
# will not be saved because they are too small for printing.
radmin = tresh * abs(big.r)
print(radmin)
for c in circles:
if abs(c.r) > radmin:
fill = colors.color_for(abs(c.r))
svg.append(( '<circle cx="%f" cy="%f" r="%f" fill="%s" stroke="black"/>\n' % (c.m.real, c.m.imag, abs(c.r), fill)))
svg.append('</g>\n')
svg.append('</svg>\n')
return ''.join(svg)
def impossible_combination(c1, c2, c3):
# If any curvatures x, y, z satisfy the equation
# x = 2*sqrt(y*z) + y + z
# then no fourth enclosing circle can be genereated, because it
# would be a line.
# We need to see for c1, c2, c3 if they could be "x".
impossible = False
sets = [(c1,c2,c3), (c2,c3,c1), (c3,c1,c2)]
for (x, y, z) in sets:
if x == 2*math.sqrt(y*z) + y + z:
impossible = True
return impossible
def main(c1=3.,c2=2.,c3=2.,depth=5):
# Sanity checks
for c in [c1, c2,c3]:
if c == 0:
print("Error: curvature or radius can't be 0")
exit(1)
if impossible_combination(c1, c2, c3):
print("Error: no apollonian gasket possible for these curvatures")
exit(1)
ag = ApollonianGasket(c1, c2, c3)
ag.generate(depth)
# Get smallest and biggest radius
smallest = abs(min(ag.genCircles, key=lambda c: abs(c.r.real)).r.real)
biggest = abs(max(ag.genCircles, key=lambda c: abs(c.r.real)).r.real)
return ag.genCircles