def apply(ctx_ref, data_size, data, plugin_data): instance = ctypes.cast(data, ctypes.POINTER(ctypes.c_uint32))[0] if instance not in Molecule.plugin_instances: egs.printf("molecule plugin instance not found", egs.WARNING) else: Context.call_apply(ctx_ref, Molecule.plugin_instances[instance].spheres) Context.call_apply(ctx_ref, Molecule.plugin_instances[instance].cylinder)
def __init__(self, filename, bonds_param=1.5): with open(filename, "r") as f: data = f.read().splitlines() num_atoms = int(data[0].strip()) positions = np.zeros((num_atoms, 3), np.float) colors = np.zeros(num_atoms, np.long) for i, line in enumerate(data[2:]): atom, x, y, z = line.strip().split() atom = atom.upper() if atom not in Molecule.atoms: print("element not found: {} (at ({}, {}, {}))".format( atom, x, y, z)) positions[i] = float(x), float(y), float(z) colors[i] = Molecule.atoms[atom][1] start = ctypes.POINTER(ctypes.c_float)() end = ctypes.POINTER(ctypes.c_float)() bond_lib = ctypes.CDLL( os.path.join(os.environ.get('EGS_PATH'), 'plugins', 'molecule_plugin', LIB_NAME)) bond_lib.calc_bonds.argtypes = [ ctypes.POINTER(ctypes.c_float), ctypes.POINTER(ctypes.c_float), ctypes.POINTER(ctypes.c_float), ctypes.c_int, ctypes.c_float, ctypes.POINTER(ctypes.POINTER(ctypes.c_float)), ctypes.POINTER(ctypes.POINTER(ctypes.c_float)) ] bond_lib.calc_bonds.restype = ctypes.c_int num_bonds = bond_lib.calc_bonds( (ctypes.c_float * num_atoms)(*positions[:, 0]), (ctypes.c_float * num_atoms)(*positions[:, 1]), (ctypes.c_float * num_atoms)(*positions[:, 2]), num_atoms, bonds_param, ctypes.byref(start), ctypes.byref(end)) bonds_start = np.ctypeslib.as_array( (ctypes.c_float * 3 * num_bonds).from_address( ctypes.addressof(start.contents))) bonds_end = np.ctypeslib.as_array( (ctypes.c_float * 3 * num_bonds).from_address( ctypes.addressof(end.contents))) bonds = [ bonds_start.reshape(num_bonds, 3), bonds_end.reshape(num_bonds, 3) ] self._data = PluginWrapper() self._data.plugin_name = Molecule.plugin_name self.spheres = Sphere(positions, [0.31] * (len(positions)), colors).display_list_elem_ref self.cylinder = Cylinder(bonds[0], bonds[1], [0.046629] * len(bonds[0]), 0xffffffff).display_list_elem_ref self._data.data_length = ctypes.sizeof(ctypes.c_uint32) self._data.data = (ctypes.c_uint8 * self._data.data_length)( Molecule.plugin_instance_ctr) Molecule.plugin_instances[Molecule.plugin_instance_ctr] = self Molecule.plugin_instance_ctr += 1 self._molecule_ref = self._data.get_display_list_elem() egs.printf("created molecule".encode('utf-8'), egs.DEBUG)
def __init__(self, width, height, rgb_data): self._data = PluginWrapper() self._data.plugin_name = Image.plugin_name self.width = width self.height = height self.initialised = False self.rgb_data = np.array(rgb_data, dtype=np.uint8) self._data.data_length = ctypes.sizeof(ctypes.c_uint32) self._data.data = (ctypes.c_uint8 * self._data.data_length)(Image.plugin_instance_ctr) Image.plugin_instances[Image.plugin_instance_ctr] = self Image.plugin_instance_ctr += 1 self._image_ref = self._data.get_display_list_elem() egs.printf("created image", egs.DEBUG)
def from_image_file(filename): try: im = PIL.Image.open(filename) except NameError: egs.printf("PIL module not loaded.", egs.WARNING) return Image(width=0, height=0, rgb_data=[]) w, h = im.size rgba = list(im.getdata()) im.close() rgb = [] for elem in rgba: if len(elem) == 3: rgb += [elem[0], elem[1], elem[2], 255] else: rgb += [elem[0], elem[1], elem[2], elem[3]] return Image(width=w, height=h, rgb_data=rgb)
def apply(ctx_ref, data_size, data, plugin_data): instance = ctypes.cast(data, ctypes.POINTER(ctypes.c_uint32))[0] if instance not in Image.plugin_instances: egs.printf("Plugin instance not found", egs.WARNING) else: if not Image.plugin_instances[instance].initialised: Image.plugin_instances[instance].texture = OpenGL.glGenTextures(1) OpenGL.glBindTexture(OpenGL.GL_TEXTURE_2D, Image.plugin_instances[instance].texture) OpenGL.glTexParameterf(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_WRAP_S, OpenGL.GL_REPEAT) OpenGL.glTexParameterf(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_WRAP_T, OpenGL.GL_CLAMP_TO_EDGE) OpenGL.glTexParameteri(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MAG_FILTER, OpenGL.GL_LINEAR) OpenGL.glTexParameteri(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MIN_FILTER, OpenGL.GL_LINEAR_MIPMAP_LINEAR) width = Image.plugin_instances[instance].width height = Image.plugin_instances[instance].height OpenGL.glTexImage2D(OpenGL.GL_TEXTURE_2D, 0, OpenGL.GL_RGB, width, height, 0, OpenGL.GL_RGBA, OpenGL.GL_UNSIGNED_BYTE, Image.plugin_instances[instance].rgb_data) OpenGL.glGenerateMipmap(OpenGL.GL_TEXTURE_2D) Image.plugin_instances[instance].initialised = True OpenGL.glActiveTexture(OpenGL.GL_TEXTURE0) OpenGL.glBindTexture(OpenGL.GL_TEXTURE_2D, Image.plugin_instances[instance].texture)
def terminate(data_size, data, plugin_data): instance = ctypes.cast(data, ctypes.POINTER(ctypes.c_uint32))[0] if instance not in Molecule.plugin_instances: egs.printf("molecule plugin instance not found", egs.WARNING) else: egs.printf("molecule destructor: calling sphere destructor", egs.DEBUG) Context.call_terminate(Molecule.plugin_instances[instance].spheres) egs.printf("molecule destructor: calling cylinder destructor", egs.DEBUG) Context.call_terminate( Molecule.plugin_instances[instance].cylinder) del instance egs.printf("molecule destructor: done", egs.DEBUG)
def terminate(data_size, data, plugin_data): egs.printf("image destructor", egs.DEBUG)
def image_plugin_terminate_plugin(): egs.printf("Image plugin terminated")
from __future__ import print_function import ctypes import sys import os import numpy as np lib_path = os.path.abspath(os.path.join('..', '..')) sys.path.append(lib_path) from egs import DisplayListElem, PluginWrapper, Context, SerializedDisplayListElem import egs try: import PIL.Image except ImportError: egs.printf("Could not import PIL.", egs.WARNING) import egs.glip as OpenGL class Image: apply_fun = None plugin_name = ctypes.c_char_p("image_plugin") plugin_instances = {} plugin_instance_ctr = 0 def __init__(self, width, height, rgb_data): self._data = PluginWrapper() self._data.plugin_name = Image.plugin_name self.width = width self.height = height self.initialised = False
def molecule_plugin_terminate_plugin(): egs.printf("Molecule plugin terminated")