[Bf-extensions-cvs] SVN commit: /data/svn/bf-extensions [848] branches/ivygen/ivy_test.py: - commented out adhesion2

Florian Meyer florianfelix at web.de
Mon Jul 26 20:15:14 CEST 2010


Revision: 848
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-extensions&revision=848
Author:   testscreenings
Date:     2010-07-26 20:15:14 +0200 (Mon, 26 Jul 2010)

Log Message:
-----------
- commented out adhesion2
- started on adhesion3
- i think this will be famore efficent, albeit not as precise. But i think we don't need that ultrprecision.

Modified Paths:
--------------
    branches/ivygen/ivy_test.py

Modified: branches/ivygen/ivy_test.py
===================================================================
--- branches/ivygen/ivy_test.py	2010-07-26 08:04:24 UTC (rev 847)
+++ branches/ivygen/ivy_test.py	2010-07-26 18:15:14 UTC (rev 848)
@@ -5,6 +5,51 @@
 #from ivy_functions import *
 import math
 
+def adhesion2(old_pos, ob, data, props):
+    targetBoundingSphere = data[0]
+    minimum_dist = targetBoundingSphere[1] * props.ivyMaxAdhesionDist
+    distances = []
+
+    adhesionVector = mathutils.Vector((0,0,0))
+    #print('minimum_dist', minimum_dist)
+    for i, face in enumerate(ob.data.faces):
+        n = face.normal
+        c = face.center + ob.location
+
+        plane_dist = (n.dot(c - old_pos))
+        #print('plane_dist', plane_dist)
+    # up to here this works
+    # plane_dist is the distance to the plane defined by the face.center and face.normal
+        pointOnPlane = old_pos - n * plane_dist
+        #print('pointOnPlane', pointOnPlane)
+        # now find the closest distance to the poly
+        # test if pointOnPlane is already inside the poligon
+        # else get closest point on the closest edge
+        
+        if plane_dist < minimum_dist:
+            if isInside(ob, face, pointOnPlane):
+                #print('isInside')
+                #print('plane_dist', plane_dist)
+                if not distances:
+                    distances.append(positive(plane_dist))
+                #print('distances [0]', distances[0])
+                if plane_dist < distances[0]:
+                    distances.append(positive(plane_dist))
+                    distances.sort()
+                    #print('shortest', distances[0])
+                    adhesionVector = mathutils.Vector((old_pos - pointOnPlane)).normalize()
+                    
+            #else:
+                #print('is Outside')
+    #print('distance', distances[0])
+    #print('\nADHESION', adhesionVector)
+    return adhesionVector
+
+def positive(val):
+    if val < 0:
+        val *= -1
+    return val
+
 def isInside(ob, face, point):
     #tests if the point is always on the same side of the edges
     #seems to work reasonably well, except:
@@ -195,8 +240,8 @@
             primaryVector = prevIvy.primaryVector
 
             # ADHESION VECTOR
-            adhesionVector = adhesion2(prevIvy.position, self.ivyCollider, data, props)
-            
+            #adhesionVector = adhesion2(prevIvy.position, self.ivyCollider, data, props)
+            adhesionVector = mathutils.Vector((0,0,0))
 
             # GROW VECTOR
             growVector = (randomVector * props.ivyRandomWeight +
@@ -289,50 +334,111 @@
                 #print('newRootsLength', len(self.ivyRoots[-1].ivyNodes))
                 pass # seed new root
 
-def adhesion2(old_pos, ob, data, props):
-    targetBoundingSphere = data[0]
-    minimum_dist = targetBoundingSphere[1] * props.ivyMaxAdhesionDist
-    distances = []
+def generateSamples(faces, mesh):
+    samplePoints = []
+    for face in faces:
+        print('face', face.index)
+        # adding the vertcoordinates --> doesn't account for shared verts
+        samplePoints += ([mesh.verts[i].co.copy() for i in face.verts])
+        #print('samplePoints - Verts', len(samplePoints))
+    
+        # ATM this only works for quads, hence:
+        if len(face.verts) == 3:
+            print('skipping Triangle')
+            continue
 
-    adhesionVector = mathutils.Vector((0,0,0))
-    #print('minimum_dist', minimum_dist)
-    for i, face in enumerate(ob.data.faces):
-        n = face.normal
-        c = face.center + ob.location
+        start0 = mesh.verts[face.verts[0]].co
+        start1 = mesh.verts[face.verts[1]].co
+        start2 = mesh.verts[face.verts[2]].co
+        start3 = mesh.verts[face.verts[3]].co
+        V0 = mesh.verts[face.verts[0]].co - mesh.verts[face.verts[1]].co
+        V1 = mesh.verts[face.verts[1]].co - mesh.verts[face.verts[2]].co
+        V2 = mesh.verts[face.verts[2]].co - mesh.verts[face.verts[3]].co
+        V3 = mesh.verts[face.verts[3]].co - mesh.verts[face.verts[0]].co
 
-        plane_dist = (n.dot(c - old_pos))
-        #print('plane_dist', plane_dist)
-    # up to here this works
-    # plane_dist is the distance to the plane defined by the face.center and face.normal
-        pointOnPlane = old_pos - n * plane_dist
-        #print('pointOnPlane', pointOnPlane)
-        # now find the closest distance to the poly
-        # test if pointOnPlane is already inside the poligon
-        # else get closest point on the closest edge
+        # add edgesamples (3 each)
+        a = 0.0
+        samp = 3
+        for i in range(samp):
+            a += 1 / (samp + 1)
+            #print('edge a', a)
+            #print('i', i, 'a', a)
+            esample0 = start0 + V0 * a
+            esample1 = start1 + V1 * a
+            esample2 = start2 + V2 * a
+            esample3 = start3 + V3 * a
+            samplePoints.append(esample0)
+            samplePoints.append(esample1)
+            samplePoints.append(esample2)
+            samplePoints.append(esample3)
+
+        print('samplePoints - Edge', len(samplePoints))
+
+        # add facesamples (12)
+        # maybe add a test here if the face is facing the point
+
+
+        # get the two triangles with the smallest angle
+
+        angle0 = (V0).angle(-V1)
+        print('vert 0',angle0)
+
+        angle1 = (V1).angle(-V2)
+        print('vert 1',angle1)
+
+        angle2 = (V2).angle(-V3)
+        print('vert 2',angle2)
+
+        angle3 = (V3).angle(-V0)
+        print('vert 3',angle3)
+
+        angle = angle0
+        tri_1 = [V0, -V1]
+        tri_2 = [V2, -V3]
+
+        if angle1 < angle:
+            angle = angle1
+            tri_1 = [V1, -V2]
+            tri_2 = [V3, -V0]
+
+        if angle2 < angle:
+            angle = angle2
+            tri_1 = [V2, -V3]
+            tri_2 = [V0, -V1]
+
+        if angle3 < angle:
+            angle = angle3
+            tri_1 = [V3, -V0]
+            tri_2 = [V1, -V2]
+
+    
+        print('smallest',angle)
+
+
         
-        if plane_dist < minimum_dist:
-            if isInside(ob, face, pointOnPlane):
-                #print('isInside')
-                #print('plane_dist', plane_dist)
-                if not distances:
-                    distances.append(positive(plane_dist))
-                #print('distances [0]', distances[0])
-                if plane_dist < distances[0]:
-                    distances.append(positive(plane_dist))
-                    distances.sort()
-                    #print('shortest', distances[0])
-                    adhesionVector = mathutils.Vector((old_pos - pointOnPlane)).normalize()
-                    
-            #else:
-                #print('is Outside')
-    #print('distance', distances[0])
-    #print('\nADHESION', adhesionVector)
+
+
+
+    return samplePoints
+
+
+def adhesion3(NodePoint, mesh):
+    #get the closest faces
+    #1. naive search
+    print('\nsorting faces')
+    sortetFaces = sorted(mesh.faces, key=lambda face: (face.center - NodePoint).length)
+
+    # now generate oversampling points for the closest faces
+    samplePoints = generateSamples(sortetFaces[0:1], mesh)
+
+    sortetSample = sorted(samplePoints, key=lambda point: (point - NodePoint).length)
+
+    try:
+        adhesionVector = NodePoint - sortetSample[0]
+    except:
+        adhesionVector = mathutils.Vector((0,0,0))
+
     return adhesionVector
-    
-def positive(val):
-    if val < 0:
-        val *= -1
-    return val
 
 def collision(ob, root, new_pos):
     prevIvy = root.ivyNodes[-1]
@@ -371,18 +477,20 @@
 
     try:
         obj = context.active_object
-        print('context.active_object', context.active_object.name)
+        #print('context.active_object', context.active_object.name)
     except:
         obj = None
 
     #create main IvyObject stores all the IvyRoots.IvyNodes
     IVY = Ivy()
 
-    print('preLoop collider is', IVY.ivyCollider)
+    adhesion3(seedPoint, obj.data)
 
+    #print('preLoop collider is', IVY.ivyCollider)
+
     # set collider object
     IVY.ivyCollider = collider
-    print('setting collider to', IVY.ivyCollider.name)
+    #print('setting collider to', IVY.ivyCollider.name)
 
     #empty the roots list
     IVY.ivyRoots = []
@@ -414,7 +522,7 @@
                             default=0)
     ivyIter = IntProperty(name='Iterations',
                             min=0, soft_min=0,
-                            default=19)
+                            default=29)
     ivyGravityWeight = FloatProperty(name='Gravity Weight',
                             min=0,soft_min=0,
                             max=1,soft_max=1,




More information about the Bf-extensions-cvs mailing list