54 lines
1.6 KiB
Python
54 lines
1.6 KiB
Python
|
|
from numpy import *
|
|
from FuncDesigner import *
|
|
|
|
class Sketch(object):
|
|
def __init__(self):
|
|
self.shapes = []
|
|
self.constraints = []
|
|
|
|
def __iter__(self):
|
|
return self.shapes.__iter__()
|
|
|
|
def __contains__(self, item):
|
|
return self.shapes.__contains__(item)
|
|
|
|
def solve(self):
|
|
shape_vars = {}
|
|
varid_to_shape_var = []
|
|
varid = 0
|
|
equations = []
|
|
for c in self.constraints:
|
|
equations += c.toEqu()
|
|
for e in equations:
|
|
coeffs = e[0]
|
|
for c in coeffs:
|
|
shape_var = c[1]
|
|
if not shape_var in shape_vars:
|
|
shape_vars[shape_var] = varid
|
|
varid += 1
|
|
varid_to_shape_var.append(shape_var)
|
|
dim = len(equations)
|
|
if len(shape_vars) > dim:
|
|
dim = len(shape_vars)
|
|
equs = []
|
|
variables = oovars(dim)
|
|
for e in equations:
|
|
coeffs = e[0]
|
|
equ = -e[1]
|
|
for c in coeffs:
|
|
equ = c[0] * variables[shape_vars[c[1]]] + equ
|
|
equs.append(equ)
|
|
if len(equs) < dim:
|
|
print 'TODO: pad equation list: %d equations, %d vars' % \
|
|
(len(equs), len(variables))
|
|
s = sle(equs)
|
|
r = s.solve()
|
|
if r.ff != float('inf'):
|
|
print "SOLVED: ", r(variables)
|
|
for i in range(len(variables)):
|
|
varid_to_shape_var[i][0].setVar(varid_to_shape_var[i][1],
|
|
r(variables[i]))
|
|
for s in self.shapes:
|
|
print 'shape:', repr(s.vars)
|