def draw_gui(self, camera, data, timeline): io = imgui.get_io() if not io.want_capture_mouse: x = imgui.get_mouse_pos().x y = camera.viewport()[1] - imgui.get_mouse_pos().y p = camera.unproject(x, y) i = timeline.intersect(p[0]) if i[0]: date = timeline.index_date(i[1]) if date is not None: df = data[data.Date == date] wp = x + 20, camera.viewport()[1] - y - 100 imgui.set_next_window_position(wp[0], wp[1]) imgui.set_next_window_bg_alpha(0.8) imgui.begin( "pointer", False, imgui.WINDOW_NO_MOVE | imgui.WINDOW_NO_TITLE_BAR | imgui.WINDOW_NO_RESIZE | imgui.WINDOW_ALWAYS_AUTO_RESIZE) imgui.text("Date: " + str(date)) for index, row in df.iterrows(): imgui.separator() imgui.text(row["Pattern"] + ": " + str(row["CandleTime"])) imgui.end() pass
def draw(self, c: OrthographicCamera): if self.price_data is None: return self.candlestick_model.draw(self.price_data, c, self.timeline) # measures sp = imgui.get_mouse_pos().x, c.viewport()[1] - imgui.get_mouse_pos().y self.measures.end = c.unproject(sp[0], sp[1]) self.measures.draw(c)
def draw_gui(self, c: OrthographicCamera): if self.price_data is None: return # compute mouse screen position sp = imgui.get_mouse_pos().x, c.viewport()[1] - imgui.get_mouse_pos().y # compute mouse world position wp = c.unproject(sp[0], sp[1]) self.measures.draw_gui(sp, wp, c) if not self.measures.dragging: draw_gui(c, self.price_data, self.timeline)
def mouse_in(x, y, w, h): """判断鼠标是否在指定的位置""" px, py = imgui.get_mouse_pos() if px < x or px > x + w: return False if py < y or py > y + h: return False return True
def draw(self): imgui.begin(self.title) #imgui.text(str(imgui.is_mouse_down(0))) imgui.label_text(str(imgui.get_mouse_pos()), "position") imgui.label_text(str(imgui.is_mouse_down(0)), "left button") imgui.label_text(str(imgui.is_mouse_down(1)), "right button") imgui.label_text(str(imgui.is_mouse_down(2)), "middle button") imgui.end()
def draw_gui(camera, data, timeline): x = imgui.get_mouse_pos().x y = camera.viewport()[1] - imgui.get_mouse_pos().y p = camera.unproject(x, y) r = intersect(p[0], p[1], data, timeline) if r is not None: wp = x + 20, camera.viewport()[1] - y - 100 imgui.set_next_window_position(wp[0], wp[1]) imgui.set_next_window_bg_alpha(0.8) imgui.begin( "pointer", False, imgui.WINDOW_NO_MOVE | imgui.WINDOW_NO_TITLE_BAR | imgui.WINDOW_NO_RESIZE | imgui.WINDOW_ALWAYS_AUTO_RESIZE) imgui.text("Date: " + str(r.date)) imgui.separator() imgui.text("Low: %.2f" % float(r.min_value)) imgui.text("Max: %.2f" % float(r.max_value)) imgui.text("Open: %.2f" % float(r.open_value)) imgui.text("Close: %.2f" % float(r.close_value)) imgui.end()
def zoomtip(imtex, imdim, mag=1.0): if imgui.is_item_hovered(): w, h = imgui.get_window_size() h = imdim[0] * w / imdim[1] rectmin = imgui.get_item_rect_min() mousepos = imgui.get_mouse_pos() u = float(mousepos[0] - rectmin[0]) / w v = float(mousepos[1] - rectmin[1]) / h imgui.begin_tooltip() tw = 32. / imdim[1] / mag th = 32. / imdim[0] / mag imgui.image(imtex, 64, 64, uv0=(u - tw, v - th), uv1=(u + tw, v + th)) dl = imgui.get_window_draw_list() rm = imgui.get_item_rect_min() col = imgui.get_color_u32_rgba(1, 1, 0, 1) dl.add_line(rm[0], rm[1] + 32, rm[0] + 64, rm[1] + 32, col) dl.add_line(rm[0] + 32, rm[1], rm[0] + 32, rm[1] + 64, col) imgui.end()
def main(): useLiveCamera = True #gc.disable() # # transform to convert the image to tensor # transform = transforms.Compose([ # transforms.ToTensor() # ]) # # initialize the model # model = torchvision.models.detection.keypointrcnn_resnet50_fpn(pretrained=True, # num_keypoints=17) # # set the computation device # device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') # # load the modle on to the computation device and set to eval mode # model.to(device).eval() # initialize glfw if not glfw.init(): return glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 4) glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 3) #creating the window window = glfw.create_window(1600, 900, "PyGLFusion", None, None) if not window: glfw.terminate() return glfw.make_context_current(window) imgui.create_context() impl = GlfwRenderer(window) # rendering glClearColor(0.2, 0.3, 0.2, 1.0) # positions texture coords quad = [ -1.0, -1.0, 0.0, 0.0, 0.0, 1.0, -1.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0, 1.0, -1.0, 1.0, 0.0, 0.0, 1.0 ] quad = np.array(quad, dtype=np.float32) indices = [0, 1, 2, 2, 3, 0] indices = np.array(indices, dtype=np.uint32) screenVertex_shader = (Path(__file__).parent / 'shaders/ScreenQuad.vert').read_text() screenFragment_shader = (Path(__file__).parent / 'shaders/ScreenQuad.frag').read_text() renderShader = OpenGL.GL.shaders.compileProgram( OpenGL.GL.shaders.compileShader(screenVertex_shader, GL_VERTEX_SHADER), OpenGL.GL.shaders.compileShader(screenFragment_shader, GL_FRAGMENT_SHADER)) # set up VAO and VBO for full screen quad drawing calls VAO = glGenVertexArrays(1) glBindVertexArray(VAO) VBO = glGenBuffers(1) glBindBuffer(GL_ARRAY_BUFFER, VBO) glBufferData(GL_ARRAY_BUFFER, 80, quad, GL_STATIC_DRAW) EBO = glGenBuffers(1) glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO) glBufferData(GL_ELEMENT_ARRAY_BUFFER, 24, indices, GL_STATIC_DRAW) glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 20, ctypes.c_void_p(0)) glEnableVertexAttribArray(0) glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 20, ctypes.c_void_p(12)) glEnableVertexAttribArray(1) # shaders bilateralFilter_shader = (Path(__file__).parent / 'shaders/bilateralFilter.comp').read_text() bilateralFilterShader = OpenGL.GL.shaders.compileProgram( OpenGL.GL.shaders.compileShader(bilateralFilter_shader, GL_COMPUTE_SHADER)) alignDepthColor_shader = (Path(__file__).parent / 'shaders/alignDepthColor.comp').read_text() alignDepthColorShader = OpenGL.GL.shaders.compileProgram( OpenGL.GL.shaders.compileShader(alignDepthColor_shader, GL_COMPUTE_SHADER)) depthToVertex_shader = (Path(__file__).parent / 'shaders/depthToVertex.comp').read_text() depthToVertexShader = OpenGL.GL.shaders.compileProgram( OpenGL.GL.shaders.compileShader(depthToVertex_shader, GL_COMPUTE_SHADER)) vertexToNormal_shader = (Path(__file__).parent / 'shaders/vertexToNormal.comp').read_text() vertexToNormalShader = OpenGL.GL.shaders.compileProgram( OpenGL.GL.shaders.compileShader(vertexToNormal_shader, GL_COMPUTE_SHADER)) raycast_shader = (Path(__file__).parent / 'shaders/raycast.comp').read_text() raycastShader = OpenGL.GL.shaders.compileProgram( OpenGL.GL.shaders.compileShader(raycast_shader, GL_COMPUTE_SHADER)) integrate_shader = (Path(__file__).parent / 'shaders/integrate.comp').read_text() integrateShader = OpenGL.GL.shaders.compileProgram( OpenGL.GL.shaders.compileShader(integrate_shader, GL_COMPUTE_SHADER)) trackP2P_shader = (Path(__file__).parent / 'shaders/p2pTrack.comp').read_text() trackP2PShader = OpenGL.GL.shaders.compileProgram( OpenGL.GL.shaders.compileShader(trackP2P_shader, GL_COMPUTE_SHADER)) reduceP2P_shader = (Path(__file__).parent / 'shaders/p2pReduce.comp').read_text() reduceP2PShader = OpenGL.GL.shaders.compileProgram( OpenGL.GL.shaders.compileShader(reduceP2P_shader, GL_COMPUTE_SHADER)) trackP2V_shader = (Path(__file__).parent / 'shaders/p2vTrack.comp').read_text() trackP2VShader = OpenGL.GL.shaders.compileProgram( OpenGL.GL.shaders.compileShader(trackP2V_shader, GL_COMPUTE_SHADER)) reduceP2V_shader = (Path(__file__).parent / 'shaders/p2vReduce.comp').read_text() reduceP2VShader = OpenGL.GL.shaders.compileProgram( OpenGL.GL.shaders.compileShader(reduceP2V_shader, GL_COMPUTE_SHADER)) LDLT_shader = (Path(__file__).parent / 'shaders/LDLT.comp').read_text() LDLTShader = OpenGL.GL.shaders.compileProgram( OpenGL.GL.shaders.compileShader(LDLT_shader, GL_COMPUTE_SHADER)) # Splatter globalMapUpdate_shader = (Path(__file__).parent / 'shaders/GlobalMapUpdate.comp').read_text() globalMapUpdateShader = OpenGL.GL.shaders.compileProgram( OpenGL.GL.shaders.compileShader(globalMapUpdate_shader, GL_COMPUTE_SHADER)) indexMapGenVert_shader = (Path(__file__).parent / 'shaders/IndexMapGeneration.vert').read_text() indexMapGenFrag_shader = (Path(__file__).parent / 'shaders/IndexMapGeneration.frag').read_text() IndexMapGenerationShader = OpenGL.GL.shaders.compileProgram( OpenGL.GL.shaders.compileShader(indexMapGenVert_shader, GL_VERTEX_SHADER), OpenGL.GL.shaders.compileShader(indexMapGenFrag_shader, GL_FRAGMENT_SHADER)) SurfaceSplattingVert_shader = ( Path(__file__).parent / 'shaders/SurfaceSplatting.vert').read_text() SurfaceSplattingFrag_shader = ( Path(__file__).parent / 'shaders/SurfaceSplatting.frag').read_text() SurfaceSplattingShader = OpenGL.GL.shaders.compileProgram( OpenGL.GL.shaders.compileShader(SurfaceSplattingVert_shader, GL_VERTEX_SHADER), OpenGL.GL.shaders.compileShader(SurfaceSplattingFrag_shader, GL_FRAGMENT_SHADER)) UnnecessaryPointRemoval_shader = ( Path(__file__).parent / 'shaders/UnnecessaryPointRemoval.comp').read_text() UnnecessaryPointRemovalShader = OpenGL.GL.shaders.compileProgram( OpenGL.GL.shaders.compileShader(UnnecessaryPointRemoval_shader, GL_COMPUTE_SHADER)) # P2V expm_shader = (Path(__file__).parent / 'shaders/expm.comp').read_text() expmShader = OpenGL.GL.shaders.compileProgram( OpenGL.GL.shaders.compileShader(expm_shader, GL_COMPUTE_SHADER)) d2c, c2d, K, invK, colK = camera.start(useLiveCamera) shaderDict = { 'renderShader': renderShader, 'bilateralFilterShader': bilateralFilterShader, 'alignDepthColorShader': alignDepthColorShader, 'depthToVertexShader': depthToVertexShader, 'vertexToNormalShader': vertexToNormalShader, 'raycastVolumeShader': raycastShader, 'integrateVolumeShader': integrateShader, 'trackP2PShader': trackP2PShader, 'reduceP2PShader': reduceP2PShader, 'trackP2VShader': trackP2VShader, 'reduceP2VShader': reduceP2VShader, 'LDLTShader': LDLTShader, 'globalMapUpdate': globalMapUpdateShader, 'indexMapGeneration': IndexMapGenerationShader, 'surfaceSplatting': SurfaceSplattingShader, 'unnecessaryPointRemoval': UnnecessaryPointRemovalShader, 'expm': expmShader } bufferDict = { 'p2pReduction': -1, 'p2pRedOut': -1, 'p2vReduction': -1, 'p2vRedOut': -1, 'test': -1, 'outBuf': -1, 'poseBuffer': -1, 'globalMap0': -1, 'globalMap1': -1, 'atomic0': -1, 'atomic1': -1 } textureDict = { 'rawColor': -1, 'lastColor': -1, 'nextColor': -1, 'rawDepth': -1, 'filteredDepth': -1, 'lastDepth': -1, 'nextDepth': -1, 'refVertex': -1, 'refNormal': -1, 'virtualVertex': -1, 'virtualNormal': -1, 'virtualDepth': -1, 'virtualColor': -1, 'mappingC2D': -1, 'mappingD2C': -1, 'xyLUT': -1, 'tracking': -1, 'volume': -1, 'indexMap': -1 } fboDict = {'indexMap': -1, 'virtualFrame': -1} # 'iters' : (2, 5, 10), fusionConfig = { 'volSize': (128, 128, 128), 'volDim': (1.0, 1.0, 1.0), 'iters': (2, 2, 2), 'initOffset': (0, 0, 0), 'maxWeight': 100.0, 'distThresh': 0.05, 'normThresh': 0.9, 'nearPlane': 0.1, 'farPlane': 4.0, 'maxMapSize': 5000000, 'c_stable': 10.0, 'sigma': 0.6 } cameraConfig = { 'depthWidth': 640, 'depthHeight': 576, 'colorWidth': 1920, 'colorHeight': 1080, 'd2c': d2c, 'c2d': c2d, 'depthScale': 0.001, 'K': K, 'invK': invK, 'colK': colK } textureDict = frame.generateTextures(textureDict, cameraConfig, fusionConfig) bufferDict = frame.generateBuffers(bufferDict, cameraConfig, fusionConfig) colorMat = np.zeros( (cameraConfig['colorHeight'], cameraConfig['colorWidth'], 3), dtype="uint8") useColorMat = False integrateFlag = True resetFlag = True initPose = glm.mat4() initPose[3, 0] = fusionConfig['volDim'][0] / 2.0 initPose[3, 1] = fusionConfig['volDim'][1] / 2.0 initPose[3, 2] = 0 blankResult = np.array([0, 0, 0, 0, 0, 0], dtype='float32') glBindBuffer(GL_SHADER_STORAGE_BUFFER, bufferDict['poseBuffer']) glBufferSubData(GL_SHADER_STORAGE_BUFFER, 0, 16 * 4, glm.value_ptr(initPose)) glBufferSubData(GL_SHADER_STORAGE_BUFFER, 16 * 4, 16 * 4, glm.value_ptr(glm.inverse(initPose))) glBufferSubData(GL_SHADER_STORAGE_BUFFER, 16 * 4 * 2, 16 * 4, glm.value_ptr(glm.mat4(1.0))) glBufferSubData(GL_SHADER_STORAGE_BUFFER, 16 * 4 * 3, 16 * 4, glm.value_ptr(glm.mat4(1.0))) glBufferSubData(GL_SHADER_STORAGE_BUFFER, 16 * 4 * 4, 6 * 4, blankResult) glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0) mouseX, mouseY = 0, 0 clickedPoint3D = glm.vec4(fusionConfig['volDim'][0] / 2.0, fusionConfig['volDim'][1] / 2.0, 0, 0) sliderDim = fusionConfig['volDim'][0] #[32 64 128 256 512] currentSize = math.log2(fusionConfig['volSize'][0]) - 5 volumeStatsChanged = False currPose = initPose # splatter stuff frameCount = 0 fboDict = frame.generateFrameBuffers(fboDict, textureDict, cameraConfig) initAtomicCount = np.array([0], dtype='uint32') mapSize = np.array([0], dtype='uint32') glBindBuffer(GL_ATOMIC_COUNTER_BUFFER, bufferDict['atomic0']) glBufferSubData(GL_ATOMIC_COUNTER_BUFFER, 0, 4, initAtomicCount) glBindBuffer(GL_ATOMIC_COUNTER_BUFFER, 0) glBindBuffer(GL_ATOMIC_COUNTER_BUFFER, bufferDict['atomic1']) glBufferSubData(GL_ATOMIC_COUNTER_BUFFER, 0, 4, initAtomicCount) glBindBuffer(GL_ATOMIC_COUNTER_BUFFER, 0) # aa = torch.tensor([0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0], dtype=torch.float32, device=torch.device('cuda')) # bb = torch.tensor([0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], dtype=torch.float32, device=torch.device('cuda')) # #setup pycuda gl interop needs to be after openGL is init # import pycuda.gl.autoinit # import pycuda.gl # cuda_gl = pycuda.gl # cuda_driver = pycuda.driver # from pycuda.compiler import SourceModule # import pycuda # pycuda_source_ssbo = cuda_gl.RegisteredBuffer(int(bufferDict['test']), cuda_gl.graphics_map_flags.NONE) # sm = SourceModule(""" # __global__ void simpleCopy(float *inputArray, float *outputArray) { # unsigned int x = blockIdx.x*blockDim.x + threadIdx.x; # outputArray[x] = inputArray[x]; # inputArray[x] = 8008.135f; # } # """) # cuda_function = sm.get_function("simpleCopy") # mappingObj = pycuda_source_ssbo.map() # data, size = mappingObj.device_ptr_and_size() # cuda_function(np.intp(aa.data_ptr()), np.intp(data), block=(8, 1, 1)) # mappingObj.unmap() # glBindBuffer(GL_SHADER_STORAGE_BUFFER, bufferDict['test']) # tee = glGetBufferSubData(GL_SHADER_STORAGE_BUFFER, 0, 32) # glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0) # teeData = np.frombuffer(tee, dtype=np.float32) # print(teeData) # modTensor = aa.cpu().data.numpy() # print(modTensor) #fusionConfig['initOffset'] = (initPose[3,0], initPose[3,1], initPose[3,2]) # LUTs #createXYLUT(k4a, textureDict, cameraConfig) <-- bug in this person.init() while not glfw.window_should_close(window): glfw.poll_events() impl.process_inputs() imgui.new_frame() sTime = time.perf_counter() try: capture = camera.getFrames(useLiveCamera) if capture.color is not None: #if useLiveCamera == False: #if k4a.configuration["color_format"] == ImageFormat.COLOR_MJPG: # colorMat = cv2.imdecode(capture.color, cv2.IMREAD_COLOR) # useColorMat = True glActiveTexture(GL_TEXTURE0) glBindTexture(GL_TEXTURE_2D, textureDict['rawColor']) glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, int(cameraConfig['colorWidth']), int(cameraConfig['colorHeight']), (GL_RGB, GL_RGBA)[useLiveCamera], GL_UNSIGNED_BYTE, (capture.color, colorMat)[useColorMat]) if capture.depth is not None: glActiveTexture(GL_TEXTURE1) glBindTexture(GL_TEXTURE_2D, textureDict['rawDepth']) glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, int(cameraConfig['depthWidth']), int(cameraConfig['depthHeight']), GL_RED, GL_UNSIGNED_SHORT, capture.depth) except EOFError: break # #smallMat = cv2.pyrDown(colorMat) # start_time = time.time() # rotMat = cv2.flip(colorMat, 0) # pil_image = Image.fromarray(rotMat).convert('RGB') # image = transform(pil_image) # image = image.unsqueeze(0).to(device) # end_time = time.time() # print((end_time - start_time) * 1000.0) # with torch.no_grad(): # outputs = model(image) # output_image = utils.draw_keypoints(outputs, rotMat) # cv2.imshow('Face detection frame', output_image) # if cv2.waitKey(1) & 0xFF == ord('q'): # break person.getPose(textureDict, cameraConfig, capture.color) frame.bilateralFilter(shaderDict, textureDict, cameraConfig) frame.depthToVertex(shaderDict, textureDict, cameraConfig, fusionConfig) frame.alignDepthColor(shaderDict, textureDict, cameraConfig, fusionConfig) frame.vertexToNormal(shaderDict, textureDict, cameraConfig) frame.mipmapTextures(textureDict) #currPose = track.runP2P(shaderDict, textureDict, bufferDict, cameraConfig, fusionConfig, currPose, integrateFlag, resetFlag) currPose = track.runP2V(shaderDict, textureDict, bufferDict, cameraConfig, fusionConfig, currPose, integrateFlag, resetFlag) #mapSize = track.runSplatter(shaderDict, textureDict, bufferDict, fboDict, cameraConfig, fusionConfig, mapSize, frameCount, integrateFlag, resetFlag) frameCount += 1 if resetFlag == True: resetFlag = False integrateFlag = True imgui.begin("Menu", True) if imgui.button("Reset"): fusionConfig['volSize'] = (1 << (currentSize + 5), 1 << (currentSize + 5), 1 << (currentSize + 5)) fusionConfig['volDim'] = (sliderDim, sliderDim, sliderDim) currPose, integrateFlag, resetFlag = track.reset( textureDict, bufferDict, cameraConfig, fusionConfig, clickedPoint3D) volumeStatsChanged = False if imgui.button("Integrate"): integrateFlag = not integrateFlag imgui.same_line() imgui.checkbox("", integrateFlag) changedDim, sliderDim = imgui.slider_float("dim", sliderDim, min_value=0.01, max_value=5.0) clickedSize, currentSize = imgui.combo( "size", currentSize, ["32", "64", "128", "256", "512"]) if imgui.is_mouse_clicked(): if not imgui.is_any_item_active(): mouseX, mouseY = imgui.get_mouse_pos() w, h = glfw.get_framebuffer_size(window) xPos = ((mouseX % int(w / 3)) / (w / 3) * cameraConfig['depthWidth']) yPos = (mouseY / (h)) * cameraConfig['depthHeight'] clickedDepth = capture.depth[ int(yPos + 0.5), int(xPos + 0.5)] * cameraConfig['depthScale'] clickedPoint3D = clickedDepth * ( cameraConfig['invK'] * glm.vec4(xPos, yPos, 1.0, 0.0)) volumeStatsChanged = True if changedDim or clickedSize: volumeStatsChanged = True imgui.end() graphics.render(VAO, window, shaderDict, textureDict) imgui.render() impl.render(imgui.get_draw_data()) eTime = time.perf_counter() #print((eTime-sTime) * 1000, mapSize[0]) glfw.swap_buffers(window) glfw.terminate() if useLiveCamera == True: camera.stop()
def main(): global width_library global width_shematic global width_context global height_window global previous_key_callback global selected_link global selected_node global iggraph global node_library global debug_is_mouse_dragging global show_debug_window # states ------------------------- scrolling = imgui.Vec2(0, 0) iggraph = IGGraph(node_library) # iggraph.reset() node_hovered_in_scene = -1 parameter_link_start = None selected_parameter = None io_hovered = None io_anchors_width_not_hovered = 10 io_anchors_width_hovered = 15 right_splitter_is_active = False left_splitter_is_active = False image_width = 0 image_height = 0 image_texture = None # states ------------------------- imgui.create_context() window = impl_glfw_init() impl = GlfwRenderer(window) io = imgui.get_io() previous_key_callback = glfw.set_key_callback(window,key_event) init_textures() assign_parameter_colors(node_library) while not glfw.window_should_close(window): glfw.poll_events() impl.process_inputs() imgui.new_frame() if imgui.begin_main_menu_bar(): if imgui.begin_menu("File", True): clicked_new, selected_new = imgui.menu_item( "New", 'Cmd+N', False, True ) if clicked_new: iggraph = IGGraph(node_library) clicked_load, selected_load = imgui.menu_item( "Load", 'Cmd+L', False, True ) if clicked_load: root = tk.Tk() root.withdraw() filename = filedialog.askopenfilename() if filename : f=open(filename) iggraph.from_json(json.load(f)) f.close() clicked_save, selected_save = imgui.menu_item( "Save", 'Cmd+S', False, True ) if clicked_save: iggraph.reset() graph_json = iggraph.to_json() text2save = json.dumps(graph_json, indent=4, sort_keys=True) root = tk.Tk() root.withdraw() f = filedialog.asksaveasfile(mode='w', defaultextension=".json") if f is None: # asksaveasfile return `None` if dialog closed with "cancel". return f.write(text2save) f.close() clicked_quit, selected_quit = imgui.menu_item( "Quit", 'Cmd+Q', False, True ) if clicked_quit: exit(0) imgui.end_menu() if imgui.begin_menu("Debug", True): show_debug_window_clicked, show_debug_window_selected = imgui.menu_item( "Show Debug window", 'Cmd+D', show_debug_window, True ) if show_debug_window_clicked: show_debug_window = not show_debug_window catch_exceptions_clicked, catch_exceptions_selected = imgui.menu_item( "Catch Exceptions", '', iggraph.catch_exceptions, True ) if catch_exceptions_clicked: iggraph.catch_exceptions = not iggraph.catch_exceptions imgui.separator() imgui.menu_item( "Examples", '', False, False ) show_example_mosaic_clicked, show_example_mosaic_selected = imgui.menu_item( "Mosaic", '', False, True ) if show_example_mosaic_clicked: example_mosaic(iggraph) imgui.end_menu() imgui.end_main_menu_bar() height_window = io.display_size.y - 18 # imgui.get_cursor_pos_y() imgui.push_style_var(imgui.STYLE_ITEM_SPACING, imgui.Vec2(0,0)) imgui.set_next_window_size(io.display_size.x, height_window) imgui.set_next_window_position(0, 18) imgui.push_style_var(imgui.STYLE_WINDOW_ROUNDING, 0) imgui.begin("Splitter test", False, imgui.WINDOW_NO_TITLE_BAR | imgui.WINDOW_NO_RESIZE | imgui.WINDOW_NO_MOVE | imgui.WINDOW_NO_BRING_TO_FRONT_ON_FOCUS) imgui.pop_style_var() imgui.pop_style_var() width_shematic = io.display_size.x - separator_width - width_context - separator_width - width_library # ============================================================================== # Library # ============================================================================== imgui.push_style_var(imgui.STYLE_CHILD_BORDERSIZE, 0) imgui.begin_child("Library", width_library, 0, True) for node_name in node_library.nodes: if imgui.button(node_name, width_library): iggraph.create_node(node_name) imgui.end_child() imgui.pop_style_var() imgui.same_line() imgui.button("left_splitter", separator_width, height_window - 20) left_splitter_is_active = imgui.is_item_active() if (left_splitter_is_active): width_library += io.mouse_delta.x scrolling = imgui.Vec2(scrolling.x - io.mouse_delta.x, scrolling.y) if (imgui.is_item_hovered()): imgui.set_mouse_cursor(imgui.MOUSE_CURSOR_RESIZE_EW) imgui.same_line() # ============================================================================== # Shematic # ============================================================================== imgui.push_style_var(imgui.STYLE_CHILD_BORDERSIZE, 0) imgui.begin_child("shematic", width_shematic, 0, True) if show_inputs_popup(iggraph): imgui.open_popup("Outputs") show_outputs_popup(iggraph) # create our child canvas if iggraph.get_state() == iggraph.STATE_IDLE: imgui.text("status: edit | ") elif iggraph.get_state() == iggraph.STATE_RUNNING: imgui.text("status: run | ") imgui.same_line() if iggraph.get_state() == iggraph.STATE_IDLE: if imgui.button("run"): #TODO if not running iggraph.set_state(iggraph.STATE_RUNNING) iggraph.prepare_to_run() imgui.open_popup("User Input") imgui.same_line() if imgui.button("run one step"): iggraph.set_state(iggraph.STATE_RUNNING) iggraph.prepare_to_run() iggraph.run_one_step() elif iggraph.get_state() == iggraph.STATE_RUNNING: if imgui.button("stop running"): iggraph.reset() iggraph.set_state(iggraph.STATE_IDLE) imgui.same_line() if imgui.button("run one step"): iggraph.set_state(iggraph.STATE_RUNNING) iggraph.run_one_step() # imgui.same_line(imgui.get_window_width() - 100) imgui.push_style_var(imgui.STYLE_FRAME_PADDING, imgui.Vec2(1, 1)) imgui.push_style_var(imgui.STYLE_WINDOW_PADDING, imgui.Vec2(0, 0)) imgui.begin_child("scrolling_region", 0, 0, True, imgui.WINDOW_NO_SCROLLBAR | imgui.WINDOW_NO_MOVE) imgui.pop_style_var() imgui.pop_style_var() imgui.push_item_width(120.0) offset = add(imgui.get_cursor_screen_pos(), scrolling) draw_list = imgui.get_window_draw_list() # Display links draw_list.channels_split(2) draw_list.channels_set_current(0) for link in iggraph.links: draw_link_param_to_param(draw_list, offset, link.output_parameter, link.input_parameter, selected_link == link) # Display nodes parameter_link_end = None one_parameter_hovered = False one_node_moving_active = False for node in iggraph.nodes: imgui.push_id(str(node.id)) node_rect_min = add(offset, node.pos) draw_list.channels_set_current(1) # foreground old_any_active = imgui.is_any_item_active() #display node content first # todo test = add(node_rect_min, NODE_WINDOW_PADDING) imgui.set_cursor_screen_position(add(node_rect_min, NODE_WINDOW_PADDING)) imgui.begin_group() imgui.text("") imgui.text(node.name) imgui.text("") imgui.end_group() # save size node_widgets_active = False # (not old_any_active and imgui.is_any_item_active()) node.size = add( add( imgui.get_item_rect_size(), NODE_WINDOW_PADDING) , NODE_WINDOW_PADDING) node_rect_max = add(node.size, node_rect_min) #display node box draw_list.channels_set_current(0) # background imgui.set_cursor_screen_position(node_rect_min) imgui.invisible_button(str(node.id), node.size.x, node.size.y) if imgui.is_item_hovered(): node_hovered_in_scene = node.id else: node_hovered_in_scene = None node_moving_active = imgui.is_item_active() use_hovered_color = node_hovered_in_scene or selected_node == node draw_list.add_rect_filled(node_rect_min.x, node_rect_min.y, node_rect_max.x, node_rect_max.y, get_node_color(node, iggraph, use_hovered_color), 5) if node_hovered_in_scene and iggraph.is_error(node): imgui.begin_tooltip() imgui.text(iggraph.error_nodes[node]) imgui.end_tooltip() # input parameters for parameter_name in node.inputs: parameter = node.inputs[parameter_name] center = node.get_intput_slot_pos(parameter) center_with_offset = add(offset, center) if io_hovered == parameter: io_anchors_width = io_anchors_width_hovered else: io_anchors_width = io_anchors_width_not_hovered imgui.set_cursor_pos(imgui.Vec2(center.x-io_anchors_width/2, center.y-io_anchors_width/2)) imgui.push_id(str(str(node.id) + "input" + parameter.id)) if (imgui.invisible_button("input", io_anchors_width, io_anchors_width)): selected_parameter = parameter # imgui.is_item_hovered() does not work when dragging is_hovering = ((io.mouse_pos.x-offset.x>center.x-io_anchors_width/2) and (io.mouse_pos.x-offset.x<center.x+io_anchors_width/2) and (io.mouse_pos.y-offset.y>center.y-io_anchors_width/2) and (io.mouse_pos.y-offset.y<center.y+io_anchors_width/2)) if is_hovering: io_hovered = parameter one_parameter_hovered = True imgui.begin_tooltip() imgui.text(parameter_name) imgui.end_tooltip() if is_hovering and imgui.is_mouse_released(0): parameter_link_end = parameter imgui.pop_id() draw_list.add_circle_filled(center_with_offset.x, center_with_offset.y, io_anchors_width/2, get_parameter_color(parameter)) # output parameters for parameter_name in node.outputs: parameter = node.outputs[parameter_name] center = node.get_output_slot_pos(parameter) center_with_offset = add(offset, center) if io_hovered == parameter: io_anchors_width = io_anchors_width_hovered else: io_anchors_width = io_anchors_width_not_hovered imgui.set_cursor_pos(imgui.Vec2(center.x-io_anchors_width/2, center.y-io_anchors_width/2)) imgui.push_id(str(str(node.id) + "output" + parameter.id)) if (imgui.invisible_button("output", io_anchors_width, io_anchors_width)): selected_parameter = parameter is_hovering = ((io.mouse_pos.x-offset.x>center.x-io_anchors_width/2) and (io.mouse_pos.x-offset.x<center.x+io_anchors_width/2) and (io.mouse_pos.y-offset.y>center.y-io_anchors_width/2) and (io.mouse_pos.y-offset.y<center.y+io_anchors_width/2)) if is_hovering: io_hovered = parameter one_parameter_hovered = True imgui.begin_tooltip() imgui.text(parameter_name) imgui.end_tooltip() draw_list.add_circle_filled(center_with_offset.x, center_with_offset.y, io_anchors_width/2, get_parameter_color(parameter)) imgui.pop_id() # cannot use imgui.is_item_active, seems buggy with the scroll if is_hovering and imgui.is_mouse_down(0): parameter_link_start = parameter if node_widgets_active or node_moving_active: selected_node = node one_node_moving_active = True if node_moving_active and imgui.is_mouse_dragging(0) and node.id==selected_node.id: node.pos = add(node.pos, io.mouse_delta) debug_is_mouse_dragging = imgui.is_mouse_dragging(0) imgui.pop_id() draw_list.channels_merge() if not one_parameter_hovered: io_hovered = None # scrolling mouse_is_in_schematic = (io.mouse_pos.x > width_library) and\ (io.mouse_pos.x < (width_library + width_shematic)) and\ (io.mouse_pos.y < height_window) and\ (io.mouse_pos.y > 18) if not one_node_moving_active and\ not parameter_link_start and\ imgui.is_mouse_dragging(0) and\ mouse_is_in_schematic and\ not left_splitter_is_active and\ not right_splitter_is_active and\ imgui.is_window_focused(): scroll_offset = imgui.Vec2(io.mouse_delta.x, io.mouse_delta.y) scrolling = add(scrolling, scroll_offset) # link creation if parameter_link_start and parameter_link_end: iggraph.add_link(parameter_link_start, parameter_link_end) # creating link elif parameter_link_start and imgui.is_mouse_dragging(0): draw_link_param_to_point(draw_list, offset, parameter_link_start, io.mouse_pos.x, io.mouse_pos.y, True) # mouse release if imgui.is_mouse_released(0): parameter_link_start = None imgui.pop_item_width() imgui.end_child() # mouse click on the scene if imgui.is_mouse_clicked(0): mouse_pos = imgui.get_mouse_pos() local_mouse_pos = imgui.Vec2(mouse_pos.x - offset.x, mouse_pos.y - offset.y) selected_link = None for link in iggraph.links: start_node = link.output_parameter.owner start_pos = start_node.get_output_slot_pos(link.output_parameter) end_node = link.input_parameter.owner end_pos = end_node.get_intput_slot_pos(link.input_parameter) distance_mouse_start = math.sqrt(((local_mouse_pos.x-start_pos.x)**2) + ((local_mouse_pos.y-start_pos.y)**2)) distance_mouse_end = math.sqrt(((local_mouse_pos.x-end_pos.x)**2) + ((local_mouse_pos.y-end_pos.y)**2)) distance_start_end = math.sqrt(((start_pos.x-end_pos.x)**2) + ((start_pos.y-end_pos.y)**2)) if ((distance_mouse_start + distance_mouse_end) - distance_start_end) < 0.1: selected_link = link imgui.end_child() imgui.pop_style_var() imgui.same_line() imgui.button("right_splitter", separator_width, height_window - 20) right_splitter_is_active = imgui.is_item_active() if (right_splitter_is_active): width_context -= io.mouse_delta.x if (imgui.is_item_hovered()): imgui.set_mouse_cursor(imgui.MOUSE_CURSOR_RESIZE_EW) # ============================================================================== # Context # ============================================================================== imgui.same_line() imgui.push_style_var(imgui.STYLE_CHILD_BORDERSIZE, 0) imgui.begin_child("child3", width_context, 0, True); if selected_node: if selected_node.handle_dynamic_parameters(): if imgui.button("add parameter"): selected_node.add_dynamic_parameter() if imgui.tree_node("Inputs"): for parameter_name in selected_node.inputs: parameter = selected_node.inputs[parameter_name] if imgui.tree_node(parameter.id): display_parameter(parameter, True) imgui.tree_pop() imgui.tree_pop() if imgui.tree_node("Output"): for parameter_name in selected_node.outputs: parameter = selected_node.outputs[parameter_name] if imgui.tree_node(parameter.id): display_parameter(parameter, False) imgui.tree_pop() imgui.tree_pop() imgui.end_child() imgui.pop_style_var() # imgui.end() # ============================================================================== # Debug Window # ============================================================================== if show_debug_window: debug_window_expanded, show_debug_window = imgui.begin("Debug", True) if parameter_link_start: imgui.text("parameter_link_start: " + parameter_link_start.id) else: imgui.text("parameter_link_start: " + "None") if selected_parameter: imgui.text("selected_parameter: " + selected_parameter.id) else: imgui.text("selected_parameter: " + "None") imgui.text("is_mouse_dragging: " + str(debug_is_mouse_dragging)) imgui.text("mouse: (" + str(io.mouse_pos.x) + ", " + str(io.mouse_pos.y) + ")") imgui.end() gl.glClearColor(1., 1., 1., 1) gl.glClear(gl.GL_COLOR_BUFFER_BIT) imgui.render() impl.render(imgui.get_draw_data()) glfw.swap_buffers(window) impl.shutdown() glfw.terminate()
def on_frame(): global cur_state, last_frame_x, last_frame_y, got_hold_last_frame imgui.set_next_window_position(0, 0) io = imgui.get_io() imgui.set_next_window_size(io.display_size.x, io.display_size.y) imgui.begin("Map", flags=imgui.WINDOW_NO_TITLE_BAR | imgui.WINDOW_NO_RESIZE | imgui.WINDOW_NO_COLLAPSE | imgui.WINDOW_NO_BRING_TO_FRONT_ON_FOCUS) map_hovered = imgui.is_window_hovered() draw_map() imgui.end() if imgui.begin_main_menu_bar(): if imgui.begin_menu("File", True): clicked_quit, selected_quit = imgui.menu_item( "Quit", 'Cmd+Q', False, True) if clicked_quit: exit(1) clicked_load, selected_load = imgui.menu_item( "Load", "", False, True) if selected_load: load_map() clicked_save, selected_save = imgui.menu_item( "Save", "", False, True) if selected_save: save_map() clicked_export, selected_export = imgui.menu_item( "Export", "", False, True) if selected_export: export_map() clicked_reset, selected_reset = imgui.menu_item( "Reset", "", False, True) if selected_reset: cur_state = State() imgui.end_menu() imgui.end_main_menu_bar() imgui.begin("Tools", False) tools_hovered = imgui.is_window_hovered() draw_mode() left_button_clicked = imgui.is_mouse_clicked(button=0) right_button_clicked = imgui.is_mouse_clicked(button=1) mouse_button_clicked = (left_button_clicked or right_button_clicked) left_button_down = imgui.is_mouse_down(button=0) if mouse_button_clicked: print("clicked left {} right {}".format(left_button_clicked, right_button_clicked)) #else: # print("clearing down_x and down_y") # start_down_x = None # start_down_y = None if got_hold_last_frame: cur_x, cur_y = imgui.get_mouse_pos() moved_cam_x = last_frame_x - cur_x moved_cam_y = last_frame_y - cur_y cur_state.camera_x += moved_cam_x cur_state.camera_y += moved_cam_y if map_hovered and not tools_hovered and mouse_button_clicked: x, y = imgui.get_mouse_pos() print("interpreting click at {},{}".format(x, y)) interpret_click(x + cur_state.camera_x, y + cur_state.camera_y, LEFT_BUTTON if left_button_clicked else RIGHT_BUTTON) elif map_hovered and left_button_down: got_hold_last_frame = True last_frame_x, last_frame_y = imgui.get_mouse_pos() elif not left_button_down: got_hold_last_frame = False imgui.end()
def render_editor(self): if imgui.button("optimize track"): self.start_optimization() imgui.same_line() _, ks = imgui.slider_float2("kcurv, kdist", self.kcurv, self.kdist, min_value=0, max_value=100.0, power=2) self.kcurv, self.kdist = ks _, self.lanewidth = imgui.slider_float("lane width", self.lanewidth, min_value=10, max_value=1000, power=1.5) # use a child region just to keep appearing/disappearing widgets at the # top from shifting stuff around imgui.begin_child("region", 0, 25, border=False) if (self.editmode == 'turn' and self.selectedpt is not None and self.selectedpt < len(self.pts['turn'])): i = self.selectedpt T = self.pts['turn'] _, T[i][2] = imgui.slider_float("turn %d radius" % (i + 1), T[i][2], min_value=-1000., max_value=1000., power=1.2) if self.editmode == 'home': _, self.homeangle = imgui.slider_float("home position angle", self.homeangle, -3.141, 3.141) imgui.end_child() w, h = imgui.get_window_size() h = self.mapim.shape[0] * w / self.mapim.shape[1] imgui.image_button(self.maptex, w, h, frame_padding=0) rectmin = imgui.get_item_rect_min() if imgui.is_item_clicked(0): mxy = imgui.get_mouse_pos() u = (mxy[0] - rectmin[0]) * self.mapim.shape[1] / w v = (mxy[1] - rectmin[1]) * self.mapim.shape[1] / w if imgui.is_mouse_double_clicked(0): self.add_point(u, v) else: self.select_point(u, v) if (imgui.is_item_hovered() and self.selectedpt is not None and imgui.is_mouse_dragging(0)): mxy = imgui.get_mouse_pos() u = (mxy[0] - rectmin[0]) * self.mapim.shape[1] / w v = (mxy[1] - rectmin[1]) * self.mapim.shape[1] / w self.move_point(u, v) scale = w / self.mapim.shape[1] dl = imgui.get_window_draw_list() self.render_ptlist(dl, self.pts['cone'], rectmin, scale, imgui.get_color_u32_rgba(1, 0.7, 0, 1), 4, self.editmode == 'cone') self.render_ptlist(dl, self.pts['turn'], rectmin, scale, imgui.get_color_u32_rgba(0, 0.3, 1, 1), 3, self.editmode == 'turn') self.render_ptlist(dl, self.pts['home'], rectmin, scale, imgui.get_color_u32_rgba(1, 1, 1, 1), 10, self.editmode == 'home') # render home position angle if len(self.pts['home']) > 0: h = self.pts['home'][0] S = 100 * np.sin(self.homeangle) C = 100 * np.cos(self.homeangle) dl.add_line(rectmin[0] + h[0] * scale, rectmin[1] + h[1] * scale, rectmin[0] + h[0] * scale + C, rectmin[1] + h[1] * scale - S, imgui.get_color_u32_rgba(1, 1, 1, 1)) self.render_turns(scale, rectmin) self.render_opttrack(scale, rectmin) if self.editmode == "cone": zoomtip(self.maptex, self.mapim.shape, 2)