Source

isosurfacesforp3d / src / density.py

Full commit
from panda3d.core import Vec3  # @UnresolvedImport
from math import cos, exp, atan2


def lobes(x, y, z):
    try:
        theta = atan2(x, y)         # sin t = 0
    except:
        theta = 0
    try:
        phi = atan2(z, y)
    except:
        phi = 0
    r = x * x + y * y + z * z
    ct = cos(theta)
    cp = cos(phi)
    return ct * ct * cp * cp * exp(-r / 10)

class Composite(object):
    def __init__(self, density_callables):
        self.density_callables = list(density_callables)
    def __add__(self, other):
        return Composite(self.density_callables + [other])
    def __sub__(self, other):
        return Composite(self.density_callables + [(lambda x, y, z:-other(x, y, z))])
    def __call__(self, x, y, z):
        return sum(df(x, y, z) for df in self.density_callables)

class Metaball(object):
    def __init__(self, center, density):
        self.center = center
        self.density = float(density)

    def __call__(self, x, y, z):
        dist = (self.center - Vec3(x, y, z)).lengthSquared()
        if dist == 0.0:
            return self.density * float("+inf")
        return self.density / dist