home *** CD-ROM | disk | FTP | other *** search
/ PCNET 2006 October - Disc 2 / PCNET_CD_2006_10_2.iso / apps / k3d-all-in-one-setup-0.5.14.0.exe / k3d-setup-0.5.14.0.exe / share / scripts / plot_function.py < prev    next >
Encoding:
Python Source  |  2006-01-24  |  2.4 KB  |  109 lines

  1. #python
  2.  
  3. import pygtk
  4. pygtk.require('2.0')
  5.  
  6. import gtk
  7. import k3d
  8. import math
  9. import sys
  10.  
  11. def mix(a, b, amount):
  12.     return (a * (1 - amount)) + (b * amount)
  13.  
  14. x1 = -5
  15. x2 = 5
  16. xcount = 29
  17.  
  18. y1 = -5
  19. y2 = 5
  20. ycount = 29
  21.  
  22. function = "-2 * math.sqrt(math.fabs(x * y))"
  23.  
  24. doc = Document
  25.  
  26. x1_entry = gtk.Entry()
  27. x1_entry.set_text(str(x1))
  28.  
  29. x2_entry = gtk.Entry()
  30. x2_entry.set_text(str(x2))
  31.  
  32. xcount_entry = gtk.Entry()
  33. xcount_entry.set_text(str(xcount))
  34.  
  35. y1_entry = gtk.Entry()
  36. y1_entry.set_text(str(y1))
  37.  
  38. y2_entry = gtk.Entry()
  39. y2_entry.set_text(str(y2))
  40.  
  41. ycount_entry = gtk.Entry()
  42. ycount_entry.set_text(str(ycount))
  43.  
  44. function_entry = gtk.Entry()
  45. function_entry.set_text(str(function))
  46.  
  47. table = gtk.Table(7, 2)
  48.  
  49. table.attach(gtk.Label("x1"), 0, 1, 0, 1)
  50. table.attach(x1_entry, 1, 2, 0, 1)
  51.  
  52. table.attach(gtk.Label("x2"), 0, 1, 1, 2)
  53. table.attach(x2_entry, 1, 2, 1, 2)
  54.  
  55. table.attach(gtk.Label("xcount"), 0, 1, 2, 3)
  56. table.attach(xcount_entry, 1, 2, 2, 3)
  57.  
  58. table.attach(gtk.Label("y1"), 0, 1, 3, 4)
  59. table.attach(y1_entry, 1, 2, 3, 4)
  60.  
  61. table.attach(gtk.Label("y2"), 0, 1, 4, 5)
  62. table.attach(y2_entry, 1, 2, 4, 5)
  63.  
  64. table.attach(gtk.Label("ycount"), 0, 1, 5, 6)
  65. table.attach(ycount_entry, 1, 2, 5, 6)
  66.  
  67. table.attach(gtk.Label("function"), 0, 1, 6, 7)
  68. table.attach(function_entry, 1, 2, 6, 7)
  69.  
  70. dialog = gtk.Dialog("Plot Function", None, gtk.DIALOG_MODAL | gtk.DIALOG_NO_SEPARATOR, (gtk.STOCK_OK, gtk.RESPONSE_ACCEPT, gtk.STOCK_CANCEL, gtk.RESPONSE_REJECT))
  71. dialog.vbox.pack_start(table)
  72. dialog.show_all()
  73. result = dialog.run()
  74. dialog.hide()
  75.  
  76. if result == gtk.RESPONSE_ACCEPT:
  77.     x1 = float(x1_entry.get_text())
  78.     x2 = float(x2_entry.get_text())
  79.     xcount = int(xcount_entry.get_text())
  80.     y1 = float(y1_entry.get_text())
  81.     y2 = float(y2_entry.get_text())
  82.     ycount = int(ycount_entry.get_text())
  83.     function = function_entry.get_text()
  84.  
  85.     doc.start_change_set()
  86.     try:
  87.         frozen_mesh = doc.new_node("FrozenMesh")
  88.         frozen_mesh.name = function
  89.  
  90.         mesh = frozen_mesh.new_mesh()
  91.         for xi in range(0, xcount):
  92.             for yi in range(0, ycount):
  93.                 x = mix(x1, x2, float(xi) / (xcount - 1))
  94.                 y = mix(y1, y2, float(yi) / (ycount - 1))
  95.                 z = eval(function)
  96.                 mesh.new_point([x, y, z])
  97.  
  98.         mesh_instance = doc.new_node("MeshInstance")
  99.         mesh_instance.name = function + " Instance"
  100.         doc.set_dependency(mesh_instance.get_property("input_mesh"), frozen_mesh.get_property("output_mesh"))
  101.  
  102.         doc.finish_change_set("Plot " + function)
  103.     except:
  104.         doc.cancel_change_set()
  105.         raise
  106.  
  107. sys.stdout.flush()
  108. sys.stderr.flush()
  109.