Commits

Jan Brohl  committed c1c4d2b

using metaballs for example, moved density-functions to seperate module

  • Participants
  • Parent commits 16fabf2

Comments (0)

Files changed (2)

File src/density.py

+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

File src/tetrahedrons.py

-from panda3d.core import Vec3, MeshDrawer # @UnresolvedImport
+from panda3d.core import Vec3, MeshDrawer  # @UnresolvedImport
+from density import Metaball, Composite
 
-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 Gridcell:  # struct GRIDCELL
+class Gridcell(object):  # struct GRIDCELL
     def __init__(self, p, n, val):
         self.p = p  # p=[8]
         self.n = n  # n=[8]
         self.val = val  # val=[8]
 
 
-class Triangle:  # struct TRIANGLE
+class Triangle(object):  # struct TRIANGLE
     def __init__(self, p1, p2, p3):
         self.p = [p1, p2, p3]  # vertices
 
 
     return triangles
 
-
 if __name__ == "__main__":
-    data = readdata(lobes, 5, 41)
+    data = readdata(Composite([Metaball(Vec3(0, 0, 0), 2), (lambda x, y, z:-Metaball(Vec3(1, 0, 0), 2)(x, y, z))]), 5, 41)
     isolevel = 0.1
     dr = lambda x, y, z: data[x][y][z]
     xmin = 0
     mdNode = md.getRoot()
     mdNode.setTwoSided(True)
     mdNode.reparentTo(render)  # @UndefinedVariable
+    #mdNode.setRenderModeWireframe(True)
     md.setBudget(xmax * ymax * zmax * 1) #actually 12 instead of 1 in general
     md.begin(base.cam, render)  # @UndefinedVariable
     c = (1, 1, 1, 0.5)