+ 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.
+ """
+
+ # The Canonical View Volume, 8 vertices, and 6 faces,
+ # We consider its face normals pointing outside
+
+ v1 = NMesh.Vert(1, 1, -1)
+ v2 = NMesh.Vert(1, -1, -1)
+ v3 = NMesh.Vert(-1, -1, -1)
+ v4 = NMesh.Vert(-1, 1, -1)
+ v5 = NMesh.Vert(1, 1, 1)
+ v6 = NMesh.Vert(1, -1, 1)
+ v7 = NMesh.Vert(-1, -1, 1)
+ v8 = NMesh.Vert(-1, 1, 1)
+
+ cvv = []
+ f1 = NMesh.Face([v1, v4, v3, v2])
+ cvv.append(f1)
+ f2 = NMesh.Face([v5, v6, v7, v8])
+ cvv.append(f2)
+ f3 = NMesh.Face([v1, v2, v6, v5])
+ cvv.append(f3)
+ f4 = NMesh.Face([v2, v3, v7, v6])
+ cvv.append(f4)
+ f5 = NMesh.Face([v3, v4, v8, v7])
+ cvv.append(f5)
+ f6 = NMesh.Face([v4, v1, v5, v8])
+ cvv.append(f6)
+
+ nmesh = NMesh.GetRaw(mesh.name)
+ clippedfaces = nmesh.faces[:]
+ facelist = clippedfaces[:]
+
+ for clipface in cvv:
+
+ clippedfaces = []
+
+ for f in facelist:
+
+ #newfaces = HSR.splitOn(clipface, f, return_positive_faces=False)
+ newfaces = None
+
+ if not newfaces:
+ # Check if the face is all outside the view frustum
+ # TODO: Do this test before, it is more efficient
+ points_outside = 0
+ for v in f:
+ if abs(v[0]) > (1 - EPS) or abs(v[1]) > (1 - EPS) or abs(v[2]) > (1 - EPS):
+ points_outside += 1
+
+ if points_outside != len(f):
+ clippedfaces.append(f)
+ else:
+ for nf in newfaces:
+ for v in nf:
+ nmesh.verts.append(v)
+
+ nf.mat = f.mat
+ nf.sel = f.sel
+ nf.col = [f.col[0]] * len(nf.v)
+
+ clippedfaces.append(nf)
+ facelist = clippedfaces[:]
+
+ nmesh.faces = facelist
+ nmesh.update()
+
+ # 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]) + EPS)
+ )
+
+ # 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 __newellDepthSort(self, mesh):
+ """Newell's depth sorting.
+
+ """
+
+ #global progress
+
+ # Find non planar quads and convert them to triangle
+ #for f in mesh.faces:
+ # f.sel = 0
+ # if is_nonplanar_quad(f.v):
+ # print "NON QUAD??"
+ # f.sel = 1
+
+ # Now reselect all faces
+ for f in mesh.faces:
+ f.sel = 1
+ mesh.quadToTriangle()
+
+ # 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 = []
+
+ # The steps are _at_least_ equal to len(facelist), we do not count the
+ # feces coming out from splitting!!
+ progress.setActivity("HSR: Newell", len(facelist))
+ #progress.setQuiet(True)
+
+ while len(facelist):
+ debug("\n----------------------\n")
+ debug("len(facelits): %d\n" % len(facelist))
+ P = facelist[0]
+
+ pSign = sign(P.normal[2])
+
+ # We can discard faces parallel to the view vector
+ #if P.normal[2] == 0:
+ # facelist.remove(P)
+ # continue
+
+ split_done = 0
+ face_marked = 0
+
+ for Q in facelist[1:]:
+
+ debug("P.smooth: " + str(P.smooth) + "\n")
+ debug("Q.smooth: " + str(Q.smooth) + "\n")
+ debug("\n")
+
+ qSign = sign(Q.normal[2])
+ # TODO: check also if Q is parallel??
+
+ # Test 0: 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]
+ notZOverlap = min(zP) > max(zQ) + EPS
+
+ if notZOverlap:
+ debug("\nTest 0\n")
+ debug("NOT Z OVERLAP!\n")
+ if Q.smooth == 0:
+ # If Q is not marked then we can safely print P
+ break
+ else:
+ debug("met a marked face\n")
+ 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))
+ notXOverlap = min(xQ) >= (max(xP) - EPS) or min(xP) >= (max(xQ) - EPS)
+
+ if notXOverlap:
+ debug("\nTest 1\n")
+ debug("NOT X OVERLAP!\n")
+ 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)
+ notYOverlap = min(yQ) >= (max(yP) - EPS) or min(yP) >= (max(yQ) - EPS)
+
+ if notYOverlap:
+ debug("\nTest 2\n")
+ debug("NOT Y OVERLAP!\n")
+ continue
+
+ # Test 3: P vertices are all behind the plane of Q
+ n = 0
+ for Pi in P:
+ d = qSign * HSR.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 * HSR.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: Check if projections of polygons effectively overlap,
+ # in previous tests we checked only bounding boxes.
+
+ #if not projectionsOverlap(P, Q):
+ if not (HSR.projectionsOverlap(P, Q) or HSR.projectionsOverlap(Q, P)):
+ debug("\nTest 5\n")
+ debug("Projections do not overlap!\n")
+ continue
+
+ # We still can't say if P obscures Q.
+
+ # But if Q is marked we do a face-split trying to resolve a
+ # difficulty (maybe a visibility cycle).
+ if Q.smooth == 1:
+ # Split P or Q
+ debug("Possibly a cycle detected!\n")
+ debug("Split here!!\n")
+
+ facelist = HSR.facesplit(P, Q, facelist, nmesh)
+ split_done = 1
+ break
+
+ # 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 * HSR.Distance(Vector(Qi), P)
+ if d <= EPS:
+ n += 1
+ qVerticesBehindPlaneP = (n == len(Q))
+
+ if qVerticesBehindPlaneP:
+ debug("\nTest 3bis\n")
+ debug("Q BEHIND P!\n")
+
+ # Test 4bis: P vertices in front of the plane of Q
+ n = 0
+ for Pi in P:
+ d = qSign * HSR.Distance(Vector(Pi), Q)
+ if d >= -EPS:
+ n += 1
+ pVerticesInFrontPlaneQ = (n == len(P))
+
+ if pVerticesInFrontPlaneQ:
+ debug("\nTest 4bis\n")
+ debug("P IN FRONT OF Q!\n")
+
+ # We don't even know if Q does obscure P, so they should
+ # intersect each other, split one of them in two parts.
+ if not qVerticesBehindPlaneP and not pVerticesInFrontPlaneQ:
+ debug("\nSimple Intersection?\n")
+ debug("Test 3bis or 4bis failed\n")
+ debug("Split here!!2\n")
+
+ facelist = HSR.facesplit(P, Q, facelist, nmesh)
+ split_done = 1
+ break
+
+ facelist.remove(Q)
+ facelist.insert(0, Q)
+ Q.smooth = 1
+ face_marked = 1
+ debug("Q marked!\n")
+ break
+
+ # Write P!
+ if split_done == 0 and face_marked == 0:
+ facelist.remove(P)
+ maplist.append(P)
+ dumpfaces(maplist, "dump" + str(len(maplist)).zfill(4) + ".svg")
+
+ progress.update()
+
+ if len(facelist) == 870:
+ dumpfaces([P, Q], "loopdebug.svg")
+
+ #if facelist == None:
+ # maplist = [P, Q]
+ # print [v.co for v in P]
+ # print [v.co for v in Q]
+ # break
+
+ # end of while len(facelist)
+
+ 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 "\nUsing the Painter algorithm for HSR."
+ self.__simpleDepthSort(mesh)
+
+ elif config.polygons['HSR'] == 'NEWELL':
+ print "\nUsing the Newell's algorithm for HSR."
+ self.__newellDepthSort(mesh)
+