home *** CD-ROM | disk | FTP | other *** search
/ Hackers Magazine 57 / CdHackersMagazineNr57.iso / Software / Multimedia / k3d-setup-0.7.11.0.exe / share / k3d / scripts / plot_function.py < prev    next >
Encoding:
Python Source  |  2008-07-21  |  3.3 KB  |  138 lines

  1. #python
  2.  
  3. import k3d
  4.  
  5. import pygtk
  6. pygtk.require('2.0')
  7.  
  8. import gtk
  9. import math
  10. import sys
  11.  
  12. def mix(a, b, amount):
  13.     return (a * (1 - amount)) + (b * amount)
  14.  
  15. x1 = -5
  16. x2 = 5
  17. xcount = 29
  18.  
  19. y1 = -5
  20. y2 = 5
  21. ycount = 29
  22.  
  23. function = "-2 * math.sqrt(math.fabs(x * y))"
  24.  
  25. doc = Document
  26.  
  27. x1_entry = gtk.Entry()
  28. x1_entry.set_text(str(x1))
  29.  
  30. x2_entry = gtk.Entry()
  31. x2_entry.set_text(str(x2))
  32.  
  33. xcount_entry = gtk.Entry()
  34. xcount_entry.set_text(str(xcount))
  35.  
  36. y1_entry = gtk.Entry()
  37. y1_entry.set_text(str(y1))
  38.  
  39. y2_entry = gtk.Entry()
  40. y2_entry.set_text(str(y2))
  41.  
  42. ycount_entry = gtk.Entry()
  43. ycount_entry.set_text(str(ycount))
  44.  
  45. function_entry = gtk.Entry()
  46. function_entry.set_text(str(function))
  47.  
  48. table = gtk.Table(7, 2)
  49.  
  50. table.attach(gtk.Label("x1"), 0, 1, 0, 1)
  51. table.attach(x1_entry, 1, 2, 0, 1)
  52.  
  53. table.attach(gtk.Label("x2"), 0, 1, 1, 2)
  54. table.attach(x2_entry, 1, 2, 1, 2)
  55.  
  56. table.attach(gtk.Label("xcount"), 0, 1, 2, 3)
  57. table.attach(xcount_entry, 1, 2, 2, 3)
  58.  
  59. table.attach(gtk.Label("y1"), 0, 1, 3, 4)
  60. table.attach(y1_entry, 1, 2, 3, 4)
  61.  
  62. table.attach(gtk.Label("y2"), 0, 1, 4, 5)
  63. table.attach(y2_entry, 1, 2, 4, 5)
  64.  
  65. table.attach(gtk.Label("ycount"), 0, 1, 5, 6)
  66. table.attach(ycount_entry, 1, 2, 5, 6)
  67.  
  68. table.attach(gtk.Label("function"), 0, 1, 6, 7)
  69. table.attach(function_entry, 1, 2, 6, 7)
  70.  
  71. 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))
  72. dialog.vbox.pack_start(table)
  73. dialog.show_all()
  74.  
  75. result = gtk.RESPONSE_ACCEPT
  76.  
  77. if not k3d.batch_mode():
  78.     result = dialog.run()
  79.  
  80. dialog.hide()
  81.  
  82. if result == gtk.RESPONSE_ACCEPT:
  83.     x1 = float(x1_entry.get_text())
  84.     x2 = float(x2_entry.get_text())
  85.     xcount = int(xcount_entry.get_text())
  86.     y1 = float(y1_entry.get_text())
  87.     y2 = float(y2_entry.get_text())
  88.     ycount = int(ycount_entry.get_text())
  89.     function = function_entry.get_text()
  90.  
  91.     doc.start_change_set()
  92.     try:
  93.         frozen_mesh = doc.new_node("FrozenMesh")
  94.         frozen_mesh.name = function
  95.  
  96.         mesh = k3d.dynamic_cast(frozen_mesh, "imesh_storage").reset_mesh()
  97.  
  98.         points = mesh.create_points()
  99.         point_selection = mesh.create_point_selection()
  100.         point_groups = mesh.create_point_groups()
  101.         first_points = point_groups.create_first_points()
  102.         point_counts = point_groups.create_point_counts()
  103.         materials = point_groups.create_materials()
  104.         constantwidth = point_groups.writable_constant_data().create_array("constantwidth", "k3d::double_t")
  105.         group_points = point_groups.create_points()
  106.  
  107.         for xi in range(0, xcount):
  108.             for yi in range(0, ycount):
  109.                 x = mix(x1, x2, float(xi) / (xcount - 1))
  110.                 y = mix(y1, y2, float(yi) / (ycount - 1))
  111.                 z = eval(function)
  112.                 point = k3d.point3(x, y, z)
  113.                 points.append(point)
  114.                 point_selection.append(0.0)
  115.  
  116.         first_points.append(len(group_points))
  117.         point_counts.append(len(points))
  118.         materials.append(None)
  119.         constantwidth.append(0.2)
  120.  
  121.         for i in range(len(points)):
  122.                 group_points.append(i)
  123.  
  124.         mesh_instance = doc.new_node("MeshInstance")
  125.         mesh_instance.name = function + " Instance"
  126.         mesh_instance.gl_painter = doc.get_node("GL Default Painter")
  127.         mesh_instance.ri_painter = doc.get_node("RenderMan Default Painter")
  128.         doc.set_dependency(mesh_instance.get_property("input_mesh"), frozen_mesh.get_property("output_mesh"))
  129.  
  130.         doc.finish_change_set("Plot " + function)
  131.     except:
  132.         doc.cancel_change_set()
  133.         raise
  134.  
  135. sys.stdout.flush()
  136. sys.stderr.flush()
  137.  
  138.