+ def _doProjection(self, mesh, projector):
+ """Apply Viewing and Projection tranformations.
+ """
+
+ for v in mesh.verts:
+ p = projector.doProjection(v.co[:])
+ v.co[0] = p[0]
+ v.co[1] = p[1]
+ v.co[2] = p[2]
+
+ #mesh.recalcNormals()
+ #mesh.update()
+
+ # We could reeset Camera matrix, since now
+ # we are in Normalized Viewing Coordinates,
+ # but doung that would affect World Coordinate
+ # processing for other objects
+
+ #self.cameraObj.data.type = 1
+ #self.cameraObj.data.scale = 2.0
+ #m = Matrix().identity()
+ #self.cameraObj.setMatrix(m)
+
+ def _doViewFrustumClipping(self, mesh):
+ """Clip faces against the View Frustum.
+ """
+
+ # HSR routines
+ def __simpleDepthSort(self, mesh):
+ """Sort faces by the furthest vertex.
+
+ This simple mesthod is known also as the painter algorithm, and it
+ solves HSR correctly only for convex meshes.
+ """
+
+ global progress
+ # The sorting requires circa n*log(n) steps
+ n = len(mesh.faces)
+ progress.setActivity("HSR: Painter", n*log(n))
+
+
+ by_furthest_z = (lambda f1, f2: progress.update() and
+ cmp(max([v.co[2] for v in f1]), max([v.co[2] for v in f2]))
+ )
+
+ # FIXME: using NMesh to sort faces. We should avoid that!
+ nmesh = NMesh.GetRaw(mesh.name)
+
+ # remember that _higher_ z values mean further points
+ nmesh.faces.sort(by_furthest_z)
+ nmesh.faces.reverse()
+
+ nmesh.update()
+
+ def __topologicalDepthSort(self, mesh):
+ """Occlusion based on topological occlusion.
+
+ Build the occlusion graph of the mesh,
+ and then do topological sort on that graph
+ """
+ return
+
+ def __newellDepthSort(self, mesh):
+ """Newell's depth sorting.
+
+ """
+ by_furthest_z = (lambda f1, f2:
+ cmp(max([v.co[2] for v in f1]), max([v.co[2] for v in f2]))
+ )
+
+ def Distance(point, face):
+ """ Calculate the distance between a point and a face.
+
+ An alternative but more expensive method can be:
+
+ ip = Intersect(Vector(face[0]), Vector(face[1]), Vector(face[2]),
+ Vector(face.no), Vector(point), 0)
+
+ d = Vector(ip - point).length
+ """
+
+ plNormal = Vector(face.no)
+ plVert0 = Vector(face[0])
+
+ #d = abs( (point * plNormal ) - (plVert0 * plNormal) )
+ d = (point * plNormal ) - (plVert0 * plNormal)
+ debug("d: "+ str(d) + "\n")
+
+ return d
+
+
+ # FIXME: using NMesh to sort faces. We should avoid that!
+ nmesh = NMesh.GetRaw(mesh.name)
+
+ # remember that _higher_ z values mean further points
+ nmesh.faces.sort(by_furthest_z)
+ nmesh.faces.reverse()
+
+
+ # Begin depth sort tests
+
+ # use the smooth flag to set marked faces
+ for f in nmesh.faces:
+ f.smooth = 0
+
+ facelist = nmesh.faces[:]
+ maplist = []
+
+ EPS = 10e-7
+
+ global progress
+ progress.setActivity("HSR: Newell", len(facelist))
+
+ while len(facelist):
+ P = facelist[0]
+
+ pSign = 1
+ if P.sel == 0:
+ pSign = -1
+
+ for Q in facelist[1:]:
+
+ debug("P.smooth: " + str(P.smooth) + "\n")
+ debug("Q.smooth: " + str(Q.smooth) + "\n")
+ debug("\n")
+
+ qSign = 1
+ if Q.sel == 0:
+ qSign = -1
+
+ # We need to test only those Qs whose furthest vertex
+ # is closer to the observer than the closest vertex of P.
+
+ zP = [v.co[2] for v in P.v]
+ zQ = [v.co[2] for v in Q.v]
+ ZOverlap = min(zP) < max(zQ)
+
+ if not ZOverlap:
+ if not Q.smooth:
+ # We can safely print P
+ break
+ else:
+ continue
+
+ # Test 1: X extent overlapping
+ xP = [v.co[0] for v in P.v]
+ xQ = [v.co[0] for v in Q.v]
+ notXOverlap = (max(xP) < min(xQ)) or (max(xQ) < min(xP))
+
+ if notXOverlap:
+ continue
+
+ # Test 2: Y extent Overlapping
+ yP = [v.co[1] for v in P.v]
+ yQ = [v.co[1] for v in Q.v]
+ notYOverlap = (max(yP) < min(yQ)) or (max(yQ) < min(yP))
+
+ if notYOverlap:
+ continue
+
+
+ # Test 3: P vertices are all behind the plane of Q
+ n = 0
+ for Pi in P:
+ d = qSign * Distance(Vector(Pi), Q)
+ if d < EPS:
+ n += 1
+ pVerticesBehindPlaneQ = (n == len(P))
+
+ if pVerticesBehindPlaneQ:
+ debug("\nTest 3\n")
+ debug("P BEHIND Q!\n")
+ continue
+
+
+ # Test 4: Q vertices in front of the plane of P
+ n = 0
+ for Qi in Q:
+ d = pSign * Distance(Vector(Qi), P)
+ if d >= EPS:
+ n += 1
+ qVerticesInFrontPlaneP = (n == len(Q))
+
+ if qVerticesInFrontPlaneP:
+ debug("\nTest 4\n")
+ debug("Q IN FRONT OF P!\n")
+ continue
+
+ # Test 5: Line Intersections... TODO
+
+
+ # We do not know if P obscures Q.
+ if Q.smooth == 1:
+ # Split P or Q, TODO
+ debug("Split here!!\n")
+ continue
+
+
+ # The question now is: Does Q obscure P?
+
+ # Test 3bis: Q vertices are all behind the plane of P
+ n = 0
+ for Qi in Q:
+ d = pSign * Distance(Vector(Qi), P)
+ if d < EPS:
+ n += 1
+ qVerticesBehindPlaneP = (n == len(Q))
+
+
+ # Test 4bis: P vertices in front of the plane of Q
+ n = 0
+ for Pi in P:
+ d = qSign * Distance(Vector(Pi), Q)
+ if d >= EPS:
+ n += 1
+ pVerticesInFrontPlaneQ = (n == len(P))
+
+
+ """
+ import intersection
+
+ if not qVerticesBehindPlaneP and not pVerticesInFrontPlaneQ:
+ # Split P or Q, TODO
+ print "Test 3bis or 4bis failed"
+ print "Split here!!2\n"
+
+ newfaces = intersection.splitOn(nmesh, P, Q, 0)
+ facelist.remove(Q)
+ for nf in newfaces:
+ if nf:
+ nf.col = Q.col
+ facelist.append(nf)
+
+ break
+
+ # We do not know
+ if Q.smooth:
+ # split P or Q
+ print "Split here!!\n"
+ newfaces = intersection.splitOn(nmesh, P, Q, 0)
+ facelist.remove(Q)
+ for nf in newfaces:
+ if nf:
+ nf.col = Q.col
+ facelist.append(nf)
+
+ break
+ """
+
+ Q.smooth = 1
+ facelist.remove(Q)
+ facelist.insert(0, Q)
+
+ # Write P!
+ facelist.remove(P)
+ maplist.append(P)
+
+ progress.update()
+
+
+ nmesh.faces = maplist
+
+ for f in nmesh.faces:
+ f.sel = 1
+ nmesh.update()
+
+ def _doHiddenSurfaceRemoval(self, mesh):
+ """Do HSR for the given mesh.
+ """
+ if len(mesh.faces) == 0:
+ return
+
+ if config.polygons['HSR'] == 'PAINTER':
+ print "\n\nUsing the Painter algorithm for HSR.\n"
+ self.__simpleDepthSort(mesh)
+
+ elif config.polygons['HSR'] == 'NEWELL':
+ print "\n\nUsing the Newell's algorithm for HSR.\n"
+ self.__newellDepthSort(mesh)
+
+