def __init__(self, context): StellarSystem.__init__( self, 'Universe', orbit=FixedOrbit(frame=AbsoluteReferenceFrame()), rotation=FixedRotation(LQuaterniond(), frame=AbsoluteReferenceFrame()), description='Universe') self.visible = True self.resolved = True self.octree_width = 100000.0 * units.Ly abs_mag = app_to_abs_mag(6.0, self.octree_width * sqrt(3)) self.octree = OctreeNode( 0, LPoint3d(10 * units.Ly, 10 * units.Ly, 10 * units.Ly), self.octree_width, abs_mag) self.update_id = 0 self.previous_leaves = [] self.to_update_leaves = [] self.to_update = [] self.to_update_extra = [] self.to_remove = [] self.nb_cells = 0 self.nb_leaves = 0 self.nb_leaves_in_cells = 0 self.dump_octree = False self.dump_octree_stats = False #TODO: Temporary until non physical objects, like cockpit and annotations are properly managed self.scene_position = LPoint3d(0, 0, 0) self.scene_orientation = LQuaterniond() self.scene_scale_factor = 1.0 self.vector_to_star = LVector3d.forward()
def get_frame_rotation_at(self, time): angle = (time - self.epoch ) * self.body.orbit.get_mean_motion() + self.meridian_angle local = LQuaterniond() local.setFromAxisAngleRad(angle, LVector3d.unitZ()) rotation = local * self.get_frame_equatorial_orientation_at(time) return rotation
def LQuaternionromAxisAngle(axis, angle, angle_units=units.Rad): if isinstance(axis, list): axis = LVector3d(*axis) axis.normalize() rot = LQuaterniond() rot.setFromAxisAngleRad(angle * angle_units, axis) return rot
def __init__(self, names, orbit=None, rotation=None, body_class=None, point_color=None, description=''): LabelledObject.__init__(self, names) self.description = description self.system = None self.body_class = body_class if orbit is None: orbit = FixedOrbit() self.orbit = orbit if rotation is None: rotation = UnknownRotation() self.rotation = rotation if point_color is None: point_color = LColor(1.0, 1.0, 1.0, 1.0) self.point_color = srgb_to_linear(point_color) self.abs_magnitude = 99.0 self.oid = None self.oid_color = None #Flags self.visible = False self.resolved = False self.in_view = False self.selected = False self.update_id = 0 self.visibility_override = False #Cached values self._position = LPoint3d() self._global_position = LPoint3d() self._local_position = LPoint3d() self._orientation = LQuaterniond() self._equatorial = LQuaterniond() self._app_magnitude = None self._extend = 0.0 #Scene parameters self.rel_position = None self.distance_to_obs = None self.vector_to_obs = None self.distance_to_star = None self.vector_to_star = None self.height_under = 0.0 self.star = None self.light_color = (1.0, 1.0, 1.0, 1.0) self.visible_size = 0.0 self.scene_position = None self.scene_orientation = None self.scene_scale_factor = None self.world_body_center_offset = LVector3d() self.model_body_center_offset = LVector3d() self.projected_world_body_center_offset = LVector3d() #Components self.orbit_object = None self.rotation_axis = None self.reference_axis = None self.init_annotations = False self.init_components = False self.update_frozen = False #TODO: Should be done properly self.orbit.body = self self.rotation.body = self objectsDB.add(self)
def do_move_and_rot(self, step): rot = LQuaterniond(*self.fake.getQuat()) rot.normalize() self.ship.set_frame_rot(rot) position = self.end_pos * step + self.start_pos * (1.0 - step) self.ship.set_frame_pos(position) if step == 1.0: self.current_interval = None
class CelestiaBodyFixedReferenceFrame(RelativeReferenceFrame): rotY180 = LQuaterniond() rotY180.set_from_axis_angle(180, LVector3d(0, 1, 0)) rotZ90 = LQuaterniond() rotZ90.set_from_axis_angle(-90, LVector3d(0, 0, 1)) def get_orientation(self): rot = self.body.get_sync_rotation() return rot
def relative_rotation(rotation, axis, angle): axis.normalize() rel_axis = rotation.xform(axis) delta_quat = LQuaterniond() try: delta_quat.setFromAxisAngleRad(angle, rel_axis) except AssertionError: print("invalid axis", axis, axis.length(), rel_axis, rel_axis.length()) return rotation * delta_quat
def get_rotation_at(self, time): if self.sync: self.period, self.mean_motion = self.body.orbit.getPeriod() if self.period == 0: return LQuaterniond() angle = (time - self.epoch) * self.mean_motion + self.meridian_angle local = LQuaterniond() local.setFromAxisAngleRad(angle, LVector3d.unitZ()) rotation = local * self.get_equatorial_rotation_at(time) return rotation
def __init__(self, position=None, right_asc=0.0, right_asc_unit=units.Deg, declination=0.0, declination_unit=units.Deg, distance=0.0, distance_unit=units.Ly, frame=None): Orbit.__init__(self, frame) if position is None: self.right_asc = right_asc * right_asc_unit self.declination = declination * declination_unit distance = distance * distance_unit else: self.right_asc = None self.declination = None if not isinstance(position, LPoint3d): position = LPoint3d(*position) if position is None: inclination = pi / 2 - self.declination ascending_node = self.right_asc + pi / 2 inclination_quat = LQuaterniond() inclination_quat.setFromAxisAngleRad(inclination, LVector3d.unitX()) ascending_node_quat = LQuaterniond() ascending_node_quat.setFromAxisAngleRad(ascending_node, LVector3d.unitZ()) self.orientation = inclination_quat * ascending_node_quat * J2000EquatorialReferenceFrame.orientation position = self.orientation.xform(LVector3d(0, 0, distance)) self.global_position = position self.position = LPoint3d() self.rotation = LQuaterniond()
def align_on_equatorial(self, duration=None): if duration is None: duration = settings.fast_move ecliptic_normal = self.ship._frame_rotation.conjugate().xform(J2000EquatorialReferenceFrame.orientation.xform(LVector3d.up())) angle = acos(ecliptic_normal.dot(LVector3d.right())) direction = ecliptic_normal.cross(LVector3d.right()).dot(LVector3d.forward()) if direction < 0: angle = 2 * pi - angle rot=LQuaterniond() rot.setFromAxisAngleRad(pi / 2 - angle, LVector3d.forward()) self.ship.step_turn(rot, absolute=False)
def do_orbit(self, delta, axis, rate): target = self.ui.selected center = target.get_rel_position_to(self.ship._global_position) center = self.ship.get_rel_position_of(center) relative_pos = self.ship.get_frame_pos() - center rot=LQuaterniond() rot.setFromAxisAngleRad(rate * delta, axis) rot2 = self.ship._frame_rotation.conjugate() * rot * self.ship._frame_rotation rot2.normalize() new_pos = rot2.conjugate().xform(relative_pos) self.ship.set_frame_pos(new_pos + center) self.ship.turn(self.ship._frame_rotation * rot2, absolute=False)
def go_to_surface(self, duration = None, height=1.001): if not self.ui.selected: return target = self.ui.selected if duration is None: duration = settings.slow_move print("Go to surface", target.get_name()) self.ui.sync_selected() center = target.get_rel_position_to(self.ship._global_position) direction = self.ship.get_pos() - center new_orientation = LQuaterniond() lookAt(new_orientation, direction) height = target.get_height_under(self.ship.get_pos()) + 10 * units.m new_position = center + new_orientation.xform(LVector3d(0, height, 0)) self.move_and_rotate_to(new_position, new_orientation, duration=duration)
def __init__(self, names, orbit=None, rotation=None, body_class=None, point_color=None, description=''): LabelledObject.__init__(self, names) self.description = description self.system = None self.body_class = body_class self.visible = True self.resolved = True self.in_view = True if orbit is None: orbit = FixedOrbit() self.orbit = orbit if rotation is None: rotation = FixedRotation() self.rotation = rotation if point_color is None: point_color = LColor(1.0, 1.0, 1.0, 1.0) self.point_color = point_color self.position = LPoint3d() self.orbit_position = LPoint3d() self.orbit_rotation = LQuaterniond() self.orientation = LQuaterniond() self.equatorial = LQuaterniond() self.abs_magnitude = 99.0 self.cached_app_magnitude = None self.rel_position = None self.distance_to_obs = None self.vector_to_obs = None self.distance_to_star = None self.vector_to_star = None self.height_under = 0.0 self.light_color = (1.0, 1.0, 1.0, 1.0) self.visible_size = 0.0 self.scene_position = None self.scene_orientation = None self.scene_scale_factor = None self.orbit_object = None self.rotation_axis = None self.reference_axis = None self.star = None self.init_annotations = False self.init_components = False #TODO: Should be done properly self.orbit.body = self self.rotation.body = self self.selected = False objectsDB.add(self) self.global_position = self.orbit.get_global_position_at(0) self.cast_shadows = False self.world_body_center_offset = LVector3d() self.model_body_center_offset = LVector3d()
def calc_orientation_from_incl_an(inclination, ascending_node, flipped=False): inclination_quat = LQuaterniond() if flipped: inclination += pi inclination_quat.setFromAxisAngleRad(inclination, LVector3d.unitX()) ascending_node_quat = LQuaterniond() ascending_node_quat.setFromAxisAngleRad(ascending_node, LVector3d.unitZ()) return inclination_quat * ascending_node_quat
def __init__(self, position=None, global_position=True, right_asc=0.0, right_asc_unit=units.Deg, declination=0.0, declination_unit=units.Deg, distance=0.0, distance_unit=units.Ly, frame=None): Orbit.__init__(self, frame) if position is None: self.right_asc = right_asc * right_asc_unit self.declination = declination * declination_unit distance = distance * distance_unit else: self.right_asc = None self.declination = None if not isinstance(position, LPoint3d): position = LPoint3d(*position) if position is None: self.orientation = calc_orientation(self.right_asc, self.declination) position = self.orientation.xform(LVector3d(0, 0, distance)) if global_position: self.global_position = position self.position = LPoint3d() else: self.global_position = LPoint3d() self.position = position self.rotation = LQuaterniond()
def __init__(self, name): VisibleObject.__init__(self, name) self.camera_modes = [CameraController.FIXED] self.frame = AbsoluteReferenceFrame() self._frame_position = LPoint3d() self._frame_rotation = LQuaterniond() self._position = LPoint3d() self._global_position = LPoint3d() self._local_position = LPoint3d() self._orientation = LQuaterniond() self.camera_distance = 0.0 self.camera_pos = LPoint3d() self.camera_rot = LQuaterniond()
def __init__(self, position=LPoint3d(0, 0, 0), rotation=LQuaterniond(), frame=None): Orbit.__init__(self, frame) self.position = position self.rotation = rotation
class J2000EquatorialReferenceFrame(BodyReferenceFrame): orientation = LQuaterniond() orientation.setFromAxisAngleRad(-units.J2000_Obliquity / 180.0 * pi, LVector3d.unitX()) def get_orientation(self): return self.orientation
def __init__(self, model, offset=None, rotation=None, scale=None, auto_scale_mesh=True, flatten=True, panda=False, attribution=None, context=defaultDirContext): Shape.__init__(self) self.model = model self.attribution = attribution self.context = context if offset is None: offset = LPoint3d() self.offset = offset if rotation is None: rotation = LQuaterniond() if scale is None and not auto_scale_mesh: scale = LVector3d(1, 1, 1) self.scale_factor = scale self.rotation = rotation self.auto_scale_mesh = auto_scale_mesh self.flatten = flatten self.panda = panda self.mesh = None self.callback = None self.cb_args = None
def decode(self, data): if 'angle' in data: angle = float(data['angle']) axis = data.get("axis", LVector3d.up()) rot = utils.LQuaternionromAxisAngle(axis, angle, units.Deg) else: rot = LQuaterniond() rotation = FixedRotation(rot) return rotation
def do_drag(self, z_angle, x_angle, move=False, rotate=True): zRotation = LQuaterniond() zRotation.setFromAxisAngleRad(z_angle, self.dragZAxis) xRotation = LQuaterniond() xRotation.setFromAxisAngleRad(x_angle, self.dragXAxis) combined = xRotation * zRotation if move: delta = combined.xform(-self.dragDir) self.observer.set_frame_camera_pos(delta + self.dragCenter) self.observer.set_frame_camera_rot(self.dragOrientation * combined) else: self.observer.set_camera_rot(self.dragOrientation * combined)
def go_to(self, target, duration, position, direction, up): if up is None: up = LVector3d.up() frame = CelestiaBodyFixedReferenceFrame(target) up = frame.get_orientation().xform(up) if isclose(abs(up.dot(direction)), 1.0): print("Warning: lookat vector identical to up vector") orientation = LQuaterniond() lookAt(orientation, direction, up) self.move_and_rotate_camera_to(position, orientation, duration=duration)
def __init__(self, right_asc=0.0, right_asc_unit=units.Deg, declination=0.0, declination_unit=units.Deg, frame=None): Orbit.__init__(self, frame) self.right_asc = right_asc * right_asc_unit self.declination = declination * declination_unit self.orientation = calc_orientation(self.right_asc, self.declination) self.position = LPoint3d() self.rotation = LQuaterniond()
def decode(self, data): name = data.get('name') radius = data.get('radius', 10) radius_units = data.get('radius-units', units.m) radius *= radius_units camera_distance = data.get('camera-distance', None) camera_pos = data.get('camera-position', None) camera_pos_units = data.get('camera-position-units', units.m) camera_rot_data = data.get('camera-rotation', None) if camera_rot_data is not None: if len(camera_rot_data) == 3: camera_rot = LQuaterniond() camera_rot.set_hpr(LVector3d(*camera_rot_data)) else: camera_rot = LQuaterniond(*camera_rot_data) else: camera_rot = LQuaterniond() shape = data.get('shape') appearance = data.get('appearance') lighting_model = data.get('lighting-model') shape, extra = ShapeYamlParser.decode(shape) if appearance is None: if isinstance(shape, MeshShape): appearance = 'model' else: appearance = 'textures' appearance = AppearanceYamlParser.decode(appearance) lighting_model = LightingModelYamlParser.decode( lighting_model, appearance) shader = BasicShader( lighting_model=lighting_model, use_model_texcoord=not extra.get('create-uv', False)) ship_object = ShapeObject('ship', shape=shape, appearance=appearance, shader=shader) if camera_distance is None: if camera_pos is None: camera_distance = 5.0 camera_pos = LPoint3d(0, -camera_distance * radius, 0) else: camera_pos = LPoint3d(*camera_pos) * camera_pos_units camera_distance = camera_pos.length() / radius else: camera_pos = LPoint3d(0, -camera_distance * radius, 0) ship = VisibleShip(name, ship_object, radius) ship.set_camera_hints(camera_distance, camera_pos, camera_rot) for mode in self.camera_modes: ship.add_camera_mode(mode) self.app.add_ship(ship)
def __init__(self, cam, lens): CameraBase.__init__(self, cam, lens) #Global position of the camera, i.e. the center of the current system self.camera_global_pos = LPoint3d() #Local position of the camera within the system in the current camera reference frame #Initialize not at zero to avoid null vector when starting to move self.camera_pos = LPoint3d(0, -1, 0) #Camera orientation in the current camera reference frame self.camera_rot = LQuaterniond() #Camera reference frame self.camera_frame = AbsoluteReferenceFrame() #Direction of sight of the camera self.camera_vector = LVector3d.forward()
def go_to(self, target, duration, position, direction, up): if up is None: up = LVector3d.up() frame = SynchroneReferenceFrame(target) up = frame.get_orientation().xform(up) if isclose(abs(up.dot(direction)), 1.0): print("Warning: lookat vector identical to up vector") else: # Make the up vector orthogonal to the direction using Gram-Schmidt up = up - direction * up.dot(direction) orientation = LQuaterniond() lookAt(orientation, direction, up) self.move_and_rotate_to(position, orientation, duration=duration)
def do_drag(self, z_angle, x_angle, move=False, rotate=True): zRotation = LQuaterniond() xRotation = LQuaterniond() try: zRotation.setFromAxisAngleRad(z_angle, self.dragZAxis) xRotation.setFromAxisAngleRad(x_angle, self.dragXAxis) except AssertionError as e: print("Wrong drag axis :", e) combined = xRotation * zRotation if move: delta = combined.xform(-self.dragDir) self.ship.set_frame_pos(delta + self.dragCenter) self.ship.set_frame_rot(self.dragOrientation * combined) else: self.ship.set_rot(self.dragOrientation * combined)
def __init__(self, right_asc=0.0, right_asc_unit=units.Deg, declination=0.0, declination_unit=units.Deg, frame=None): Orbit.__init__(self, frame) self.right_asc = right_asc * right_asc_unit self.declination = declination * declination_unit inclination = pi / 2 - self.declination ascending_node = self.right_asc + pi / 2 inclination_quat = LQuaterniond() inclination_quat.setFromAxisAngleRad(inclination, LVector3d.unitX()) ascending_node_quat = LQuaterniond() ascending_node_quat.setFromAxisAngleRad(ascending_node, LVector3d.unitZ()) self.orientation = inclination_quat * ascending_node_quat * J2000EquatorialReferenceFrame.orientation self.position = LPoint3d() self.rotation = LQuaterniond()
def update(self, observer): if self.instance is None: return False local_position = LPoint3d(*self.physics_instance.get_pos()) local_rotation = LQuaterniond(*self.physics_instance.get_quat()) if settings.camera_at_origin: scene_rel_position = local_position - observer._local_position else: scene_rel_position = local_position self.instance.set_pos(*scene_rel_position) self.instance.set_quat(LQuaternion(*local_rotation)) if (local_position - observer._local_position).length() > self.limit: print("Object is too far, removing it") self.remove_instance() return False else: return True
def decode(self, data, frame): if 'angle' in data: angle = float(data['angle']) axis = data.get("axis", LVector3d.up()) rot = utils.LQuaternionromAxisAngle(axis, angle, units.Deg) elif 'ra' in data: right_ascension = data.get('ra', None) ra_units = AngleUnitsYamlParser.decode(data.get('ra-units', 'Deg')) declination = data.get('de', 0.0) decl_units = AngleUnitsYamlParser.decode(data.get('de-units', 'Deg')) rot = EquatorialReferenceAxis(right_ascension * ra_units, declination * decl_units, False) else: rot = LQuaterniond() if data.get('frame') is not None or frame is None: frame = FrameYamlParser.decode(data.get('frame', 'J2000Equatorial')) rotation = FixedRotation(rot, frame) return rotation