- #if (isVisible(face)):
- # print "face visible"
-
- # backface culling
- a = []
- a.append(OBJmesh.faces[face][0][0])
- a.append(OBJmesh.faces[face][0][1])
- a.append(OBJmesh.faces[face][0][2])
- a = RotatePoint(a[0], a[1], a[2], obj.RotX, obj.RotY, obj.RotZ)
- a[0] += obj.LocX - camera.LocX
- a[1] += obj.LocY - camera.LocY
- a[2] += obj.LocZ - camera.LocZ
- b = []
- b.append(OBJmesh.faces[face][1][0])
- b.append(OBJmesh.faces[face][1][1])
- b.append(OBJmesh.faces[face][1][2])
- b = RotatePoint(b[0], b[1], b[2], obj.RotX, obj.RotY, obj.RotZ)
- b[0] += obj.LocX - camera.LocX
- b[1] += obj.LocY - camera.LocY
- b[2] += obj.LocZ - camera.LocZ
- c = []
- c.append(OBJmesh.faces[face][numvert-1][0])
- c.append(OBJmesh.faces[face][numvert-1][1])
- c.append(OBJmesh.faces[face][numvert-1][2])
- c = RotatePoint(c[0], c[1], c[2], obj.RotX, obj.RotY, obj.RotZ)
- c[0] += obj.LocX - camera.LocX
- c[1] += obj.LocY - camera.LocY
- c[2] += obj.LocZ - camera.LocZ
-
- norm = [0,0,0]
- norm[0] = (b[1] - a[1])*(c[2] - a[2]) - (c[1] - a[1])*(b[2] - a[2])
- norm[1] = -((b[0] - a[0])*(c[2] - a[2]) - (c[0] - a[0])*(b[2] - a[2]))
- norm[2] = (b[0] - a[0])*(c[1] - a[1]) - (c[0] - a[0])*(b[1] - a[1])
-
- d = norm[0]*a[0] + norm[1]*a[1] + norm[2]*a[2]
-
- # if the face is visible flatten it on the "picture plane"
- if d < 0:
- file.write("<polygon points=\"")
- for vert in range(numvert):
-
- vertxyz = []
-
- if vert != 0: file.write(", ")
-
- vertxyz.append(OBJmesh.faces[face][vert][0])
- vertxyz.append(OBJmesh.faces[face][vert][1])
- vertxyz.append(OBJmesh.faces[face][vert][2])
-
- # rotate object
- vertxyz = RotatePoint(vertxyz[0], vertxyz[1], vertxyz[2], obj.RotX, obj.RotY, obj.RotZ)
-
- # original setting for translate
- vertxyz[0] += (obj.LocX - camera.LocX)
- vertxyz[1] += (obj.LocY - camera.LocY)
- vertxyz[2] += (obj.LocZ - camera.LocZ)
-
- # rotate camera
- vertxyz = RotatePoint(vertxyz[0], vertxyz[1], vertxyz[2], -camera.RotX, -camera.RotY, -camera.RotZ)
-
- #dist = Distance(vertxyz[0], vertxyz[1], vertxyz[2])
- xy = flatern(vertxyz[0], vertxyz[1], vertxyz[2])
- px = int(xy[0])
- py = int(xy[1])
- # add/sorting in Z' direction
- #Dodaj(px,py,Distance(vertxyz[0], vertxyz[1], vertxyz[2]))
- file.write(`px` + ", " + `py`)
-
- # per face color calculation
- ambient = -200
- #svetlo = [1, 1, -1] #original
- svetlo = [1, 1, -0.3]
- vektori = (norm[0]*svetlo[0]+norm[1]*svetlo[1]+norm[2]*svetlo[2])
- vduzine = fabs(sqrt(pow(norm[0],2)+pow(norm[1],2)+pow(norm[2],2))*sqrt(pow(svetlo[0],2)+pow(svetlo[1],2)+pow(svetlo[2],2)))
- intensity = floor(ambient + 255 * acos(vektori/vduzine))
- if intensity < 0:
- intensity = 0
-
- wireframe = True
- if wireframe:
- stroke_width=1
- else:
- stroke_width=0
- file.write("\"\n style=\"fill:rgb("+str(intensity)+","+str(intensity)+","+str(intensity)+");stroke:rgb(0,0,0);stroke-width:"+str(stroke_width)+"\"/>\n")
- if animation:
- file.write("<animate attributeName=\"visibility\" begin=\""+str(f*0.08)+"s\" dur=\"0.08s\" fill=\"remove\" to=\"visible\">\n")
- file.write("</animate>\n")
- file.write("</g>\n")
-
-file.write("</svg>")
-file.close()
-DrawProgressBar (1.0,"Finished.")
-print "Finished\n"
+ # 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:
+ 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))
+
+ 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:
+ print P.col[0]
+ 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:
+ print Q.col[0]
+ 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
+ # Check if polygons effectively overlap each other, not only
+ # boundig boxes as dome before.
+ # Since we We are working in normalized projection coordinates
+ # we kust check if polygons intersect.
+
+ def projectionsOverlap(P, Q):
+
+ for i in range(0, len(P.v)):
+
+ v1 = Vector(P.v[i-1])
+ v1[2] = 0
+ v2 = Vector(P.v[i])
+ v2[2] = 0
+
+ for j in range(0, len(Q.v)):
+ v3 = Vector(Q.v[j-1])
+ v3[2] = 0
+ v4 = Vector(Q.v[j])
+ v4[2] = 0
+
+ ret = LineIntersect(v1, v2, v3, v4)
+ # if line v1-v2 and v3-v4 intersect both return
+ # values are the same.
+ if ret and ret[0] == ret[1] and isOnSegment(v1, v2,
+ ret[0]) and isOnSegment(v3, v4, ret[1]):
+ debug("Projections OVERLAP!!\n")
+ debug("line1:"+
+ " M "+ str(v1[0])+','+str(v1[1]) + ' L ' + str(v2[0])+','+str(v2[1]) + '\n' +
+ " M "+ str(v3[0])+','+str(v3[1]) + ' L ' + str(v4[0])+','+str(v4[1]) + '\n' +
+ "\n")
+ debug("return: "+ str(ret)+"\n")
+ return True
+
+ return False
+
+ if not projectionsOverlap(P, Q):
+ debug("\nTest 5\n")
+ debug("Projections do not overlap!\n")
+ continue
+
+
+ # We do not know if P obscures Q.
+ if Q.smooth == 1:
+ # Split P or Q, TODO
+ debug("Cycle detected!\n")
+ 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:
+ print Q.col[0]
+ d = pSign * 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:
+ print P.col[0]
+ d = qSign * 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")
+
+
+ import intersection
+
+ if not qVerticesBehindPlaneP and not pVerticesInFrontPlaneQ:
+ debug("\nSimple Intersection?\n")
+ # Split P or Q, TODO
+ print "Test 3bis or 4bis failed"
+ print "Split here!!2\n"
+
+ """newfaces = intersection.splitOn(P, Q, 0)
+ print newfaces
+ 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(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!
+ P = facelist[0]
+ 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 "\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)
+
+
+ def _doEdgesStyle(self, mesh, edgestyleSelect):
+ """Process Mesh Edges accroding to a given selection style.
+
+ Examples of algorithms:
+
+ Contours:
+ given an edge if its adjacent faces have the same normal (that is
+ they are complanar), than deselect it.
+
+ Silhouettes:
+ given an edge if one its adjacent faces is frontfacing and the
+ other is backfacing, than select it, else deselect.
+ """
+
+ Mesh.Mode(Mesh.SelectModes['EDGE'])
+
+ edge_cache = MeshUtils.buildEdgeFaceUsersCache(mesh)
+
+ for i,edge_faces in enumerate(edge_cache):
+ mesh.edges[i].sel = 0
+ if edgestyleSelect(edge_faces):
+ mesh.edges[i].sel = 1
+
+ """
+ for e in mesh.edges:
+
+ e.sel = 0
+ if edgestyleSelect(e, mesh):
+ e.sel = 1
+ """
+
+
+
+# ---------------------------------------------------------------------
+#
+## GUI Class and Main Program
+#
+# ---------------------------------------------------------------------
+
+
+from Blender import BGL, Draw
+from Blender.BGL import *
+
+class GUI:
+
+ def _init():
+
+ # Output Format menu
+ output_format = config.output['FORMAT']
+ default_value = outputWriters.keys().index(output_format)+1
+ GUI.outFormatMenu = Draw.Create(default_value)
+ GUI.evtOutFormatMenu = 0
+
+ # Animation toggle button
+ GUI.animToggle = Draw.Create(config.output['ANIMATION'])
+ GUI.evtAnimToggle = 1
+
+ # Join Objects toggle button
+ GUI.joinObjsToggle = Draw.Create(config.output['JOIN_OBJECTS'])
+ GUI.evtJoinObjsToggle = 2
+
+ # Render filled polygons
+ GUI.polygonsToggle = Draw.Create(config.polygons['SHOW'])
+
+ # Shading Style menu
+ shading_style = config.polygons['SHADING']
+ default_value = shadingStyles.keys().index(shading_style)+1
+ GUI.shadingStyleMenu = Draw.Create(default_value)
+ GUI.evtShadingStyleMenu = 21
+
+ GUI.evtPolygonsToggle = 3
+ # We hide the config.polygons['EXPANSION_TRICK'], for now
+
+ # Render polygon edges
+ GUI.showEdgesToggle = Draw.Create(config.edges['SHOW'])
+ GUI.evtShowEdgesToggle = 4
+
+ # Render hidden edges
+ GUI.showHiddenEdgesToggle = Draw.Create(config.edges['SHOW_HIDDEN'])
+ GUI.evtShowHiddenEdgesToggle = 5
+
+ # Edge Style menu
+ edge_style = config.edges['STYLE']
+ default_value = edgeStyles.keys().index(edge_style)+1
+ GUI.edgeStyleMenu = Draw.Create(default_value)
+ GUI.evtEdgeStyleMenu = 6
+
+ # Edge Width slider
+ GUI.edgeWidthSlider = Draw.Create(config.edges['WIDTH'])
+ GUI.evtEdgeWidthSlider = 7
+
+ # Edge Color Picker
+ c = config.edges['COLOR']
+ GUI.edgeColorPicker = Draw.Create(c[0]/255.0, c[1]/255.0, c[2]/255.0)
+ GUI.evtEdgeColorPicker = 71
+
+ # Render Button
+ GUI.evtRenderButton = 8
+
+ # Exit Button
+ GUI.evtExitButton = 9
+
+ def draw():
+
+ # initialize static members
+ GUI._init()
+
+ glClear(GL_COLOR_BUFFER_BIT)
+ glColor3f(0.0, 0.0, 0.0)
+ glRasterPos2i(10, 350)
+ Draw.Text("VRM: Vector Rendering Method script. Version %s." %
+ __version__)
+ glRasterPos2i(10, 335)
+ Draw.Text("Press Q or ESC to quit.")
+
+ # Build the output format menu
+ glRasterPos2i(10, 310)
+ Draw.Text("Select the output Format:")
+ outMenuStruct = "Output Format %t"
+ for t in outputWriters.keys():
+ outMenuStruct = outMenuStruct + "|%s" % t
+ GUI.outFormatMenu = Draw.Menu(outMenuStruct, GUI.evtOutFormatMenu,
+ 10, 285, 160, 18, GUI.outFormatMenu.val, "Choose the Output Format")
+
+ # Animation toggle
+ GUI.animToggle = Draw.Toggle("Animation", GUI.evtAnimToggle,
+ 10, 260, 160, 18, GUI.animToggle.val,
+ "Toggle rendering of animations")
+
+ # Join Objects toggle
+ GUI.joinObjsToggle = Draw.Toggle("Join objects", GUI.evtJoinObjsToggle,
+ 10, 235, 160, 18, GUI.joinObjsToggle.val,
+ "Join objects in the rendered file")
+
+ # Render Button
+ Draw.Button("Render", GUI.evtRenderButton, 10, 210-25, 75, 25+18,
+ "Start Rendering")
+ Draw.Button("Exit", GUI.evtExitButton, 95, 210-25, 75, 25+18, "Exit!")
+
+ # Rendering Styles
+ glRasterPos2i(200, 310)
+ Draw.Text("Rendering Style:")
+
+ # Render Polygons
+ GUI.polygonsToggle = Draw.Toggle("Filled Polygons", GUI.evtPolygonsToggle,
+ 200, 285, 160, 18, GUI.polygonsToggle.val,
+ "Render filled polygons")
+
+ if GUI.polygonsToggle.val == 1:
+
+ # Polygon Shading Style
+ shadingStyleMenuStruct = "Shading Style %t"
+ for t in shadingStyles.keys():
+ shadingStyleMenuStruct = shadingStyleMenuStruct + "|%s" % t.lower()
+ GUI.shadingStyleMenu = Draw.Menu(shadingStyleMenuStruct, GUI.evtShadingStyleMenu,
+ 200, 260, 160, 18, GUI.shadingStyleMenu.val,
+ "Choose the shading style")
+
+
+ # Render Edges
+ GUI.showEdgesToggle = Draw.Toggle("Show Edges", GUI.evtShowEdgesToggle,
+ 200, 235, 160, 18, GUI.showEdgesToggle.val,
+ "Render polygon edges")
+
+ if GUI.showEdgesToggle.val == 1:
+
+ # Edge Style
+ edgeStyleMenuStruct = "Edge Style %t"
+ for t in edgeStyles.keys():
+ edgeStyleMenuStruct = edgeStyleMenuStruct + "|%s" % t.lower()
+ GUI.edgeStyleMenu = Draw.Menu(edgeStyleMenuStruct, GUI.evtEdgeStyleMenu,
+ 200, 210, 160, 18, GUI.edgeStyleMenu.val,
+ "Choose the edge style")
+
+ # Edge size
+ GUI.edgeWidthSlider = Draw.Slider("Width: ", GUI.evtEdgeWidthSlider,
+ 200, 185, 140, 18, GUI.edgeWidthSlider.val,
+ 0.0, 10.0, 0, "Change Edge Width")
+
+ # Edge Color
+ GUI.edgeColorPicker = Draw.ColorPicker(GUI.evtEdgeColorPicker,
+ 342, 185, 18, 18, GUI.edgeColorPicker.val, "Choose Edge Color")
+
+ # Show Hidden Edges
+ GUI.showHiddenEdgesToggle = Draw.Toggle("Show Hidden Edges",
+ GUI.evtShowHiddenEdgesToggle,
+ 200, 160, 160, 18, GUI.showHiddenEdgesToggle.val,
+ "Render hidden edges as dashed lines")
+
+ glRasterPos2i(10, 160)
+ Draw.Text("%s (c) 2006" % __author__)
+
+ def event(evt, val):
+
+ if evt == Draw.ESCKEY or evt == Draw.QKEY:
+ Draw.Exit()
+ else:
+ return
+
+ Draw.Redraw(1)
+
+ def button_event(evt):
+
+ if evt == GUI.evtExitButton:
+ Draw.Exit()
+
+ elif evt == GUI.evtOutFormatMenu:
+ i = GUI.outFormatMenu.val - 1
+ config.output['FORMAT']= outputWriters.keys()[i]
+
+ elif evt == GUI.evtAnimToggle:
+ config.output['ANIMATION'] = bool(GUI.animToggle.val)
+
+ elif evt == GUI.evtJoinObjsToggle:
+ config.output['JOIN_OBJECTS'] = bool(GUI.joinObjsToggle.val)
+
+ elif evt == GUI.evtPolygonsToggle:
+ config.polygons['SHOW'] = bool(GUI.polygonsToggle.val)
+
+ elif evt == GUI.evtShadingStyleMenu:
+ i = GUI.shadingStyleMenu.val - 1
+ config.polygons['SHADING'] = shadingStyles.keys()[i]
+
+ elif evt == GUI.evtShowEdgesToggle:
+ config.edges['SHOW'] = bool(GUI.showEdgesToggle.val)
+
+ elif evt == GUI.evtShowHiddenEdgesToggle:
+ config.edges['SHOW_HIDDEN'] = bool(GUI.showHiddenEdgesToggle.val)
+
+ elif evt == GUI.evtEdgeStyleMenu:
+ i = GUI.edgeStyleMenu.val - 1
+ config.edges['STYLE'] = edgeStyles.keys()[i]
+
+ elif evt == GUI.evtEdgeWidthSlider:
+ config.edges['WIDTH'] = float(GUI.edgeWidthSlider.val)
+
+ elif evt == GUI.evtEdgeColorPicker:
+ config.edges['COLOR'] = [int(c*255.0) for c in GUI.edgeColorPicker.val]
+
+ elif evt == GUI.evtRenderButton:
+ label = "Save %s" % config.output['FORMAT']
+ # Show the File Selector
+ global outputfile
+ Blender.Window.FileSelector(vectorize, label, outputfile)
+
+ else:
+ print "Event: %d not handled!" % evt
+
+ if evt:
+ Draw.Redraw(1)
+ #GUI.conf_debug()
+
+ def conf_debug():
+ from pprint import pprint
+ print "\nConfig"
+ pprint(config.output)
+ pprint(config.polygons)
+ pprint(config.edges)
+
+ _init = staticmethod(_init)
+ draw = staticmethod(draw)
+ event = staticmethod(event)
+ button_event = staticmethod(button_event)
+ conf_debug = staticmethod(conf_debug)
+
+# A wrapper function for the vectorizing process
+def vectorize(filename):
+ """The vectorizing process is as follows:
+
+ - Instanciate the writer and the renderer
+ - Render!
+ """
+
+ if filename == "":
+ print "\nERROR: invalid file name!"
+ return
+
+ from Blender import Window
+ editmode = Window.EditMode()
+ if editmode: Window.EditMode(0)
+
+ actualWriter = outputWriters[config.output['FORMAT']]
+ writer = actualWriter(filename)
+
+ renderer = Renderer()
+ renderer.doRendering(writer, config.output['ANIMATION'])
+
+ if editmode: Window.EditMode(1)
+
+# We use a global progress Indicator Object
+progress = None
+
+# Here the main
+if __name__ == "__main__":
+
+ global progress
+
+ outputfile = ""
+ basename = Blender.sys.basename(Blender.Get('filename'))
+ if basename != "":
+ outputfile = Blender.sys.splitext(basename)[0] + "." + str(config.output['FORMAT']).lower()
+
+ if Blender.mode == 'background':
+ progress = ConsoleProgressIndicator()
+ vectorize(outputfile)
+ else:
+ progress = GraphicalProgressIndicator()
+ Draw.Register(GUI.draw, GUI.event, GUI.button_event)