def _closest_guideline_point(self, point): currd = None closest = None is_junction = False for element in self.elements: for pin in element.pins(): p = pin.position + element.bounding_box.topLeft() d = QVector2D(p - point).lengthSquared() if (currd is None or d < currd) and d < 2500: currd = d closest = p is_junction = True for wire in self.wires: for p in (wire.p1(), wire.p2()): d = QVector2D(p - point).lengthSquared() if (currd is None or d < currd) and d < 2500: currd = d closest = p is_junction = True for line in self.guidelines: p = _closest_point(line, point) d = QVector2D(p - point).lengthSquared() if not _is_point_on_line(line, p): continue if self._wire_start is not None: delta = p - self._wire_start if delta.x() != 0 and delta.y() != 0: continue if (currd is None or ((not is_junction and d < currd) or (is_junction and abs(d - currd) > 100))) and d < 2500: currd = d closest = p return closest
def calculate(self): self.rays = [] cone_normal_top, cone_norma_bottom = self.light.cone_normals() angle_top = math.atan2(cone_normal_top.y(), cone_norma_bottom.x()) angle_bottom = math.atan2(cone_norma_bottom.y(), cone_norma_bottom.x()) # get angle between vectors total_angle = math.acos( QVector2D.dotProduct(cone_normal_top, cone_norma_bottom)) part = total_angle / self.segments for i in range(self.segments): rad = math.atan2(self.light.dir.y(), self.light.dir.x()) angle_i = rad - total_angle / 2.0 + (part * i) vector_i = QVector2D(math.cos(angle_i), math.sin(angle_i)) ray_i = Ray(self.light.get_focal_point_worldspace(), vector_i) first_i = True self.rays.append(ray_i) for line in self.lines: intersect_v = self.does_ray_intersect(ray_i, line) if intersect_v: length_i = (intersect_v - ray_i.pos).length() if first_i: ray_i.length = length_i first_i = False if length_i < ray_i.length: ray_i.length = length_i
def __init__(self, engine): self.engine = engine self.static = False self.radius = 10.0 self.pos = QVector2D() self.velocity = QVector2D() self.mass = 100.000
def paint(self, painter): r = self.runner.player.rect pos = self.pos_to_screen(self.runner.player.pos) pos = self.pos_to_screen(QVector2D(r.x(), r.y())) size = self.pos_to_screen(QVector2D(r.width(), r.height())) # painter.drawRect(pos.x(), pos.y(), 20, 20) self.paint_gameobject(painter, self.runner.player) self.paint_obstacles(painter)
def pins(self): bb = self.bounding_box yield Pin(self.descriptor.get_pin('in'), QVector2D(-1, 0), QPoint(0, bb.height() / 2)) yield Pin(self.descriptor.get_pin('out'), QVector2D(1, 0), QPoint(bb.width(), bb.height() / 2))
def paint(self, painter): if DEBUG: pen = QPen(QColor(50, 40, 230)) painter.setPen(pen) painter.drawLine(self.pos.toPoint(), (self.pos + self.dir * 20).toPoint()) start = (self.pos - QVector2D(-2, -2)).toPoint() end = (self.pos - QVector2D(2, 2)).toPoint() r = QtCore.QRect(start, end) painter.fillRect(r, QtGui.QColor(255, 255, 255))
def create_window_rect(self): v0 = QVector2D(0, 0) v1 = QVector2D(self.width(), 0) v2 = QVector2D(0, self.height()) v3 = QVector2D(self.width(), self.height()) return [ Line(v0, v1), Line(v0, v2), Line(v0, v1), Line(v1, v3), Line(v2, v3) ]
def mouseMoveEvent(self, event): if self.current_mouse_button == QtCore.Qt.LeftButton: mouse_pos = event.pos() self.light.pos = QVector2D(mouse_pos.x(), mouse_pos.y()) self.volumelight.calculate() self.update() if self.current_mouse_button == QtCore.Qt.RightButton: mouse = QVector2D(event.pos().x(), event.pos().y()) mouse_offset = mouse - self.light.pos mouse_offset.normalize() self.light.dir = mouse_offset self.volumelight.calculate() self.update()
def uvCenterOffset(self): """ Get UV offset corresponding to the camera principal point. """ if not self.solvedIntrinsics: return None pp = self.solvedIntrinsics["principalPoint"] # compute principal point offset in UV space uvPP = QVector2D(float(pp[0]) / self.imageSize.width(), float(pp[1]) / self.imageSize.height()) # convert to offset offset = uvPP - QVector2D(0.5, 0.5) # apply orientation to principal point correction if self.orientation == 6: offset = QVector2D(-offset.y(), offset.x()) elif self.orientation == 8: offset = QVector2D(offset.y(), -offset.x()) return offset
def mouseMoveEvent(self, event): if self.current_mouse_button == QtCore.Qt.LeftButton: mouse_pos = event.pos() self.ray.pos = QVector2D(mouse_pos.x(), mouse_pos.y()) self.pathtracer.reset() self.pathtracer.start() self.update() if self.current_mouse_button == QtCore.Qt.RightButton: mouse = QVector2D(event.pos().x(), event.pos().y()) mouse_offset = mouse - self.ray.pos mouse_offset.normalize() self.ray.dir = mouse_offset self.pathtracer.reset() self.pathtracer.start() self.update()
def mouseMoveEvent(self, e): if e.buttons() == Qt.LeftButton: currPos = QVector2D(e.localPos()) diff = currPos - self.pressLoc diff *= 0.5 self.pressLoc = currPos self.scene.getCamera().rotate(diff.x(), diff.y())
def reset(self): self.obstacle_tick = 0 self.obstacles = [] self.player.pos = QVector2D() self.prev_time = time.time() self.stopped = False self.total_ticks_alive = 0
def __init__(self, scene, updateFpsDisplay, renderSettings=Rendersettings(), parent=None): View.__init__(self, scene) QOpenGLWidget.__init__(self, parent) self.renderImage = QImage() self.renderSettings = renderSettings self.updateFpsDisplay = updateFpsDisplay self.shaderProgram = QOpenGLShaderProgram() self.viewPortShaderProgram = QOpenGLShaderProgram() self.lightShaderProgram = QOpenGLShaderProgram() self.eyeLoc = QVector3D(0.0, 0.0, 5.0) self.pressLoc = QVector2D() self.isRendering = False self.viewportPlane = ViewportPlane() self.viewportTexture = None self.timer = QTimer() self.timer.setSingleShot(True) self.timer.timeout.connect(self.checkRender) self.scene.registerView(self, [ UpdateType.MATERIAL_CHANGE, UpdateType.MATERIAL_CREATE, UpdateType.MATERIAL_DELETE, UpdateType.OBJECT_CREATE, UpdateType.OBJECT_DELETE, UpdateType.OBJECT_TRANSFORM, UpdateType.CAMERA_CHANGE, UpdateType.LIGHT_CHANGE, UpdateType.SCENE_LOAD ]) self.timestamps = None self.fpsWindow = 10 self.renderer = None self.renderStartTime = None
def mouseMoveEvent(self, event): """ :param QMouseEvent event: :return: """ SENSITIVITY = 1.0 if self._is_mouse_pressed: mouse_delta = QVector2D(event.pos() - self._last_screen_pos).length() if mouse_delta > SENSITIVITY: self._is_dragging = True pos = self.mapToScene(event.pos()) self.viewport().setCursor(Qt.ClosedHandCursor) delta = (pos.x() - self._last_coords[0], pos.y() - self._last_coords[1]) self.translate(*delta) self._save_last_coords(event) event.accept() super().mouseMoveEvent(event)
def get_next_pos(self): pos_copy = QVector2D(self.pos) v = self.create_random_vector2d() if random.random() < 0.01: pos_copy += v * random.randint(20, 40) else: pos_copy += v * random.randint(1, 5) return pos_copy
def generate(self): self.points = [] self.colors = [] for i in range(self.total_points): v = QVector2D(random.random(), random.random()) self.points.append(v) c = QColor(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)) self.colors.append(c)
def __init__(self): super(PathTraceBouncerDemoWidget, self).__init__() self.resize(QtCore.QSize(300, 300)) self.current_mouse_button = None self.setWindowTitle("PathTraceBouncer") self.points = [] self.ray = Ray(QVector2D(self.width() / 3, self.height() / 2), QVector2D(0.5, 0.5).normalized()) self.pathtracer = PathTracer() self.pathtracer.lines += self.create_circle( QVector2D(self.width() / 2, self.height() / 2), self.height() / 2, 8) self.pathtracer.lines += self.create_circle( QVector2D(self.width() / 2, self.height() / 2), self.height() / 9, 8) self.pathtracer.rays.append(self.ray)
def does_ray_intersect(ray, line): v0 = line.v0 - ray.pos v1 = line.v1 - ray.pos angle = math.atan2(ray.dir.y(), ray.dir.x()) v0_angle = math.atan2(v0.y(), v0.x()) - angle v1_angle = math.atan2(v1.y(), v1.x()) - angle local_v0 = QVector2D(math.cos(v0_angle), math.sin(v0_angle)) * v0.length() local_v1 = QVector2D(math.cos(v1_angle), math.sin(v1_angle)) * v1.length() res = False # if the point is behind no hit if local_v0.x() < 0 and local_v1.x() < 0: return False # points need to cross the axis if local_v0.y() < 0 and local_v1.y() > 0: res = True if local_v0.y() > 0 and local_v1.y() < 0: res = True if not res: return None slope = VolumeLight.line_slope_x(local_v0, local_v1) intercept_x = local_v0.x() - slope * local_v0.y() intercept_y = local_v0.y() - slope * local_v0.x() if intercept_x <= 0: return None local_intersect_point = QVector2D(intercept_x, 0) new_angle = math.atan2(local_intersect_point.y(), local_intersect_point.x()) + angle world_intersect = QVector2D( math.cos(new_angle), math.sin(new_angle)) * local_intersect_point.length() return world_intersect + ray.pos
def _closest_guideline_point(self, point): currd = None closest = None for line in self.guidelines: p = self._closest_point(line, point) d = QVector2D(p - point).lengthSquared() if (currd is None or d < currd) and d < 2500: currd = d closest = p return closest
def does_ray_intersect(ray, line): p0 = line.p0 - ray.pos p1 = line.p1 - ray.pos angle = math.atan2(ray.dir.y(), ray.dir.x()) p0_angle = math.atan2(p0.y(), p0.x()) - angle p1_angle = math.atan2(p1.y(), p1.x()) - angle local_p0 = QVector2D(math.cos(p0_angle), math.sin(p0_angle)) * p0.length() local_p1 = QVector2D(math.cos(p1_angle), math.sin(p1_angle)) * p1.length() res = False # if the point is behind no hit if local_p0.x() < 0 and local_p1.x() < 0: return False # points need to cross the axis if local_p0.y() < 0 and local_p1.y() > 0: res = True if local_p0.y() > 0 and local_p1.y() < 0: res = True if not res: return None slope = PathTracer.line_slope_x(local_p0, local_p1) intercept_x = local_p0.x() - slope * local_p0.y() intercept_y = local_p0.y() - slope * local_p0.x() if intercept_x <= 0: return None local_intersect_point = QVector2D(intercept_x, 0) new_angle = math.atan2(local_intersect_point.y(), local_intersect_point.x()) + angle world_intersect = QVector2D(math.cos(new_angle), math.sin( new_angle)) * local_intersect_point.length() return world_intersect + ray.pos
def pointFromWorldToScreen(self, point, camera, windowSize): """ Compute the Screen point corresponding to a World Point. Args: point (QVector4D): point in world coordinates camera (QCamera): camera viewing the scene windowSize (QSize): size of the Scene3D window Returns: QVector2D: point in screen coordinates """ # Transform the point from World Coord to Normalized Device Coord viewMatrix = camera.transform().matrix().inverted() projectedPoint = (camera.projectionMatrix() * viewMatrix[0]).map(point) projectedPoint2D = QVector2D(projectedPoint.x() / projectedPoint.w(), projectedPoint.y() / projectedPoint.w()) # Transform the point from Normalized Device Coord to Screen Coord screenPoint2D = QVector2D( int((projectedPoint2D.x() + 1) * windowSize.width() / 2), int((projectedPoint2D.y() - 1) * windowSize.height() / -2)) return screenPoint2D
def paint(self, painter): chunk_x = float(self.rect.width()) / float(self.width) chunk_y = float(self.rect.height()) / float(self.height) size_vector = QVector2D(self.width, self.height) for y in range(self.height): for x in range(self.width): v = QVector2D(x, y) index = self.get_closest_point(v) c = self.colors[index] draw_x = math.floor(chunk_x * x) dray_y = math.floor(chunk_y * y) painter.setPen(QPen(c)) painter.fillRect(draw_x, dray_y, math.ceil(chunk_x), math.ceil(chunk_y), c) painter.setPen(QPen(QColor(100, 100, 100))) for v in self.points: painter.drawRect(v.x() * size_vector.x() * chunk_x, v.y() * size_vector.y() * chunk_y, 2, 2)
def __init__(self): super(LightVolumesDemoWidget, self).__init__() self.resize(QtCore.QSize(300, 300)) self.setWindowTitle("LightVolumes") self.current_mouse_button = None self.light = Light() self.light.dir = QVector2D(1, 0) self.light.pos.setX(self.width() / 3) self.light.pos.setY(self.height() / 2) self.lines = [] for i in range(20): circle = self.create_circle( QVector2D(random.random() * self.width(), random.random() * self.height()), 10 + random.random() * 10, 16) self.lines += circle self.lines += self.create_window_rect() self.volumelight = VolumeLight(self.light, self.lines) self.volumelight.calculate()
def projectToTrackball(self, screenCoords): sx = screenCoords.x() sy = self._windowSize.height() - screenCoords.y() p2d = QVector2D(sx / self._windowSize.width() - 0.5, sy / self._windowSize.height() - 0.5) z = 0.0 r2 = pow(self._trackballSize, 2) lengthSquared = p2d.lengthSquared() if lengthSquared <= r2 * 0.5: z = sqrt(r2 - lengthSquared) else: z = r2 * 0.5 / p2d.length() return QVector3D(p2d.x(), p2d.y(), z)
def get_option(self): choice = random.randint(0, 3) pos_copy = QVector2D(self.pos) if choice == self.MOVE_UP: pos_copy += self.up_vector elif choice == self.MOVE_DOWN: pos_copy += self.down_vector elif choice == self.MOVE_LEFT: pos_copy += self.left_vector elif choice == self.MOVE_RIGHT: pos_copy += self.right_vector return pos_copy
def shoot_ray(self, ray, ignore_line=None): intersecting_lines = [] nearest_length = sys.float_info.max nearest_line = None nearest_point = None for line in self.lines: if line == ray.line: continue if line == ignore_line: continue intersect_point = self.does_ray_intersect(ray, line) if not intersect_point: continue l = (intersect_point - ray.pos).length() if l < nearest_length: nearest_length = l nearest_line = line nearest_point = intersect_point intersecting_lines.append(line) # self.debug_points.append(intersect_point) if not nearest_point: return if nearest_point: self.debug_points.append(nearest_point) angle = math.atan2(nearest_line.normal.y(), nearest_line.normal.x()) roughness = math.pi / 8.0 for i in range(self.scatter_per_bounce): new_strength = ray.strength - random.random() * 0.28 if new_strength < 0: continue angle_random = roughness / 2.0 - (random.random() * roughness) new_angle = angle + angle_random new_normal = QVector2D(math.cos(new_angle), math.sin(new_angle)) bounce_dir = self.reflect(ray.dir, new_normal) bounce_ray = Ray(nearest_point, bounce_dir) self.all_rays.append(bounce_ray) bounce_ray.parent = ray bounce_ray.strength = new_strength bounce_ray.line = nearest_line self.bounce_rays.append(bounce_ray) if not intersecting_lines: return
def __init__(self): super(RandomWalkerDemoWidget, self).__init__() self.setWindowTitle("RandomWalker") self.setCursor(QtCore.Qt.BlankCursor) self.is_paused = True self.tick_timer = QtCore.QTimer() self.tick_timer.setInterval(1) self.tick_timer.timeout.connect(self.tick) self.random_walker = RandomWalker() self.random_walker.rect = self.rect() self.random_walker.resolution = QSize(50, 50) self.random_walker.start_pos = QVector2D( self.random_walker.resolution.width() / 2, self.random_walker.resolution.height() / 2)
def __init__(self): super(LevyFlightDemoWidget, self).__init__() self.setWindowTitle("LevyFlight") self.setCursor(QtCore.Qt.BlankCursor) self.is_paused = True self.tick_timer = QtCore.QTimer() self.tick_timer.setInterval(1) self.tick_timer.timeout.connect(self.tick) self.levy_flight = LevyFlight() self.levy_flight.rect = self.rect() self.levy_flight.resolution = QSize(300, 300) self.levy_flight.start_pos = QVector2D( self.levy_flight.resolution.width() / 2, self.levy_flight.resolution.height() / 2)
def create_circle(self, c, r, s): lines = [] points = [] pi2 = math.pi * 2 for i in range(s): j = (pi2 / s) * i v = QVector2D(math.cos(j), math.sin(j)) p = c + v * r points.append(p) for i in range(0, s - 1, 1): line = Line(points[i], points[i + 1]) lines.append(line) lines.append(Line(points[-1], points[0])) return lines
def tick(self, dt=1.0): if self.static: return force = self.calculate_force() acceleration = QVector2D(force.x(), force.y()) self.velocity.setX(self.velocity.x() + acceleration.x() * dt) self.velocity.setY(self.velocity.y() + acceleration.y() * dt) self.pos += self.velocity * dt for obj in self.engine.objects: if obj == self: continue if self.intersects(obj): self.resolve_collision(obj)