Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Python_超人
宇宙模拟器
提交
e84ca521
宇宙模拟器
项目概览
Python_超人
/
宇宙模拟器
通知
19
Star
2
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
宇宙模拟器
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
e84ca521
编写于
4月 09, 2023
作者:
三月三net
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Python超人-宇宙模拟器
上级
95299c47
变更
25
显示空白变更内容
内联
并排
Showing
25 changed file
with
249 addition
and
215 deletion
+249
-215
bodies/asteroid.py
bodies/asteroid.py
+2
-2
bodies/asteroids.py
bodies/asteroids.py
+2
-2
bodies/body.py
bodies/body.py
+53
-52
bodies/dysen_sphere.py
bodies/dysen_sphere.py
+2
-2
bodies/fixed_stars/fixed_star.py
bodies/fixed_stars/fixed_star.py
+1
-1
bodies/moon.py
bodies/moon.py
+32
-12
bodies/saturn.py
bodies/saturn.py
+2
-2
bodies/sun.py
bodies/sun.py
+1
-1
common/color_utils.py
common/color_utils.py
+6
-6
common/func.py
common/func.py
+12
-12
common/image_utils.py
common/image_utils.py
+22
-22
common/system.py
common/system.py
+12
-12
sim_scenes/func.py
sim_scenes/func.py
+11
-11
sim_scenes/science/parabolic_curve.py
sim_scenes/science/parabolic_curve.py
+27
-14
sim_scenes/solar_system/solar_system_4.py
sim_scenes/solar_system/solar_system_4.py
+5
-5
simulators/func.py
simulators/func.py
+7
-7
simulators/mpl_simulator.py
simulators/mpl_simulator.py
+11
-11
simulators/simulator.py
simulators/simulator.py
+8
-8
simulators/ursina/entities/entity_utils.py
simulators/ursina/entities/entity_utils.py
+9
-9
simulators/ursina/ui/ui_panel.py
simulators/ursina/ui/ui_panel.py
+3
-3
simulators/ursina/ursina_mesh.py
simulators/ursina/ursina_mesh.py
+3
-3
simulators/ursina_simulator.py
simulators/ursina_simulator.py
+6
-6
simulators/views/body_view.py
simulators/views/body_view.py
+7
-7
simulators/views/mayavi_view.py
simulators/views/mayavi_view.py
+4
-4
simulators/views/ursina_view.py
simulators/views/ursina_view.py
+1
-1
未找到文件。
bodies/asteroid.py
浏览文件 @
e84ca521
...
...
@@ -50,8 +50,8 @@ class Asteroid(Body):
def
ignore_gravity
(
self
,
body
):
"""
是否忽略引力
:
param body:
:
return:
@
param body:
@
return:
"""
# 小行星只对恒星有引力,忽略其他行星的引力
if
body
.
is_fixed_star
:
...
...
bodies/asteroids.py
浏览文件 @
e84ca521
...
...
@@ -53,8 +53,8 @@ class Asteroids(Body):
def
ignore_gravity
(
self
,
body
):
"""
是否忽略引力
:
param body:
:
return:
@
param body:
@
return:
"""
# 小行星只对恒星有引力,忽略其他行星的引力
# if body.is_fixed_star:
...
...
bodies/body.py
浏览文件 @
e84ca521
...
...
@@ -26,20 +26,21 @@ class Body(metaclass=ABCMeta):
is_fixed_star
=
False
,
trail_color
=
None
,
show_name
=
False
):
"""
天体类
:param name: 天体名称
:param mass: 天体质量 (kg)
:param init_position: 初始位置 (km)
:param init_velocity: 初始速度 (km/s)
:param density: 平均密度 (kg/m³)
:param color: 天体颜色(纹理图片优先)
:param texture: 纹理图片
:param size_scale: 尺寸缩放
:param distance_scale: 距离缩放
:param rotation_speed: 自旋速度(度/小时)
:param parent: 天体的父对象
:param ignore_mass: 是否忽略质量(如果为True,则不计算引力)
:param is_fixed_star: 是否为恒星
:param trail_color: 天体拖尾颜色(默认天体颜色)
@param name: 天体名称
@param mass: 天体质量 (kg)
@param init_position: 初始位置 (km)
@param init_velocity: 初始速度 (km/s)
@param density: 平均密度 (kg/m³)
@param color: 天体颜色(纹理图片优先)
@param texture: 纹理图片
@param size_scale: 尺寸缩放
@param distance_scale: 距离缩放
@param rotation_speed: 自旋速度(度/小时)
@param parent: 天体的父对象
@param ignore_mass: 是否忽略质量(如果为True,则不计算引力)
@param is_fixed_star: 是否为恒星
@param trail_color: 天体拖尾颜色(默认天体颜色)
@param show_name: 是否显示天体名称
"""
self
.
__his_pos
=
[]
self
.
__his_vel
=
[]
...
...
@@ -130,7 +131,7 @@ class Body(metaclass=ABCMeta):
def
init_position
(
self
):
"""
获取天体的初始位置(单位:km)
:
return:
@
return:
"""
return
self
.
__init_position
...
...
@@ -138,8 +139,8 @@ class Body(metaclass=ABCMeta):
def
init_position
(
self
,
value
):
"""
设置天体的初始位置(单位:km)
:
param value:
:
return:
@
param value:
@
return:
"""
self
.
__init_position
=
np
.
array
(
value
,
dtype
=
'float32'
)
self
.
__position
=
copy
.
deepcopy
(
self
.
__init_position
)
...
...
@@ -148,7 +149,7 @@ class Body(metaclass=ABCMeta):
def
init_velocity
(
self
):
"""
获取天体的初始速度 (km/s)
:
return:
@
return:
"""
return
self
.
__init_velocity
...
...
@@ -156,8 +157,8 @@ class Body(metaclass=ABCMeta):
def
init_velocity
(
self
,
value
):
"""
设置天体的初始速度 (km/s)
:
param value:
:
return:
@
param value:
@
return:
"""
self
.
__init_velocity
=
np
.
array
(
value
,
dtype
=
'float32'
)
self
.
__velocity
=
copy
.
deepcopy
(
self
.
__init_velocity
)
...
...
@@ -166,7 +167,7 @@ class Body(metaclass=ABCMeta):
def
has_rings
(
self
):
"""
是否为带光环的天体(土星为 True)
:
return:
@
return:
"""
return
self
.
__has_rings
...
...
@@ -178,7 +179,7 @@ class Body(metaclass=ABCMeta):
def
is_fixed_star
(
self
):
"""
是否为恒星(太阳为 True)
:
return:
@
return:
"""
return
self
.
__is_fixed_star
...
...
@@ -190,7 +191,7 @@ class Body(metaclass=ABCMeta):
def
position
(
self
):
"""
获取天体的位置(单位:km)
:
return:
@
return:
"""
return
self
.
__position
...
...
@@ -198,8 +199,8 @@ class Body(metaclass=ABCMeta):
def
position
(
self
,
value
):
"""
设置天体的位置(单位:km)
:
param value:
:
return:
@
param value:
@
return:
"""
self
.
__position
=
value
self
.
__record_history
()
...
...
@@ -208,7 +209,7 @@ class Body(metaclass=ABCMeta):
def
acceleration
(
self
):
"""
获取天体的加速度(单位:km/s²)
:
return:
@
return:
"""
return
self
.
__acceleration
...
...
@@ -216,8 +217,8 @@ class Body(metaclass=ABCMeta):
def
acceleration
(
self
,
value
):
"""
设置天体的加速度(单位:km/s²)
:
param value:
:
return:
@
param value:
@
return:
"""
self
.
__acceleration
=
value
self
.
__record_history
()
...
...
@@ -226,7 +227,7 @@ class Body(metaclass=ABCMeta):
def
velocity
(
self
):
"""
获取天体的速度(单位:km/s)
:
return:
@
return:
"""
return
self
.
__velocity
...
...
@@ -234,8 +235,8 @@ class Body(metaclass=ABCMeta):
def
velocity
(
self
,
value
):
"""
设置天体的速度(单位:km/s)
:
param value:
:
return:
@
param value:
@
return:
"""
self
.
__velocity
=
value
self
.
__record_history
()
...
...
@@ -243,9 +244,9 @@ class Body(metaclass=ABCMeta):
def
__append_history
(
self
,
his_list
,
data
):
"""
追加每个位置时刻的历史数据
:
param his_list:
:
param data:
:
return:
@
param his_list:
@
param data:
@
return:
"""
# 如果历史记录为0 或者 新增数据和最后的历史数据不相同,则添加
if
len
(
his_list
)
==
0
or
\
...
...
@@ -255,7 +256,7 @@ class Body(metaclass=ABCMeta):
def
__record_history
(
self
):
"""
记录每个位置时刻的历史数据
:
return:
@
return:
"""
# 如果历史记录数超过了保留数量,则截断,只保留 __his_reserved_num 数量的历史
if
len
(
self
.
__his_pos
)
>
self
.
__his_reserved_num
:
...
...
@@ -272,21 +273,21 @@ class Body(metaclass=ABCMeta):
def
his_position
(
self
):
"""
历史位置
:
return:
@
return:
"""
return
self
.
__his_pos
def
his_velocity
(
self
):
"""
历史瞬时速度
:
return:
@
return:
"""
return
self
.
__his_vel
def
his_acceleration
(
self
):
"""
历史瞬时加速度
:
return:
@
return:
"""
return
self
.
__his_acc
...
...
@@ -294,7 +295,7 @@ class Body(metaclass=ABCMeta):
def
mass
(
self
):
"""
天体质量 (单位:kg)
:
return:
@
return:
"""
return
self
.
__mass
...
...
@@ -302,7 +303,7 @@ class Body(metaclass=ABCMeta):
def
rotation_speed
(
self
):
"""
自旋速度(度/小时)
:
return:
@
return:
"""
return
self
.
__rotation_speed
...
...
@@ -310,7 +311,7 @@ class Body(metaclass=ABCMeta):
def
rotation_speed
(
self
,
value
):
"""
自旋速度(度/小时)
:
return:
@
return:
"""
self
.
__rotation_speed
=
value
...
...
@@ -318,7 +319,7 @@ class Body(metaclass=ABCMeta):
def
density
(
self
):
"""
平均密度 (单位:kg/m³)
:
return:
@
return:
"""
return
self
.
__density
...
...
@@ -340,7 +341,7 @@ class Body(metaclass=ABCMeta):
def
raduis
(
self
):
"""
天体的半径(单位:km)
:
return:
@
return:
"""
# V = ⁴⁄₃πr³ -> r = pow((3V)/(4π),1/3)
return
pow
(
3
*
self
.
volume
/
(
4
*
math
.
pi
),
1
/
3
)
...
...
@@ -349,7 +350,7 @@ class Body(metaclass=ABCMeta):
def
diameter
(
self
):
"""
天体的直径(单位:km)
:
return:
@
return:
"""
return
self
.
raduis
*
2
...
...
@@ -361,8 +362,8 @@ class Body(metaclass=ABCMeta):
def
ignore_gravity
(
self
,
body
):
"""
是否忽略引力
:
param body:
:
return:
@
param body:
@
return:
"""
return
False
...
...
@@ -370,7 +371,7 @@ class Body(metaclass=ABCMeta):
def
position_au
(
self
):
"""
获取天体的位置(单位:天文单位 A.U.)
:
return:
@
return:
"""
pos
=
self
.
position
pos_au
=
pos
/
AU
...
...
@@ -385,7 +386,7 @@ class Body(metaclass=ABCMeta):
def
reset
(
self
):
"""
重新设置初始速度和初始位置
:
return:
@
return:
"""
self
.
position
=
copy
.
deepcopy
(
self
.
init_position
)
self
.
velocity
=
copy
.
deepcopy
(
self
.
init_velocity
)
...
...
@@ -405,8 +406,8 @@ class Body(metaclass=ABCMeta):
def
build_bodies_from_json
(
json_file
):
"""
JSON文件转为天体对象
:
param json_file:
:
return:
@
param json_file:
@
return:
"""
bodies
=
[]
params
=
{}
...
...
@@ -451,8 +452,8 @@ class Body(metaclass=ABCMeta):
def
exp
(
body_data
):
"""
进行表达式分析,将表达式改为eval执行后的结果
:
param body_data:
:
return:
@
param body_data:
@
return:
"""
#
for
k
in
body_data
.
keys
():
...
...
bodies/dysen_sphere.py
浏览文件 @
e84ca521
...
...
@@ -46,8 +46,8 @@ class DysenSphere(Body):
def
ignore_gravity
(
self
,
body
):
"""
是否忽略引力
:
param body:
:
return:
@
param body:
@
return:
"""
return
True
...
...
bodies/fixed_stars/fixed_star.py
浏览文件 @
e84ca521
...
...
@@ -88,7 +88,7 @@ class FixedStar(Body):
def
is_fixed_star
(
self
):
"""
恒星
:
return:
@
return:
"""
return
True
...
...
bodies/moon.py
浏览文件 @
e84ca521
...
...
@@ -26,7 +26,23 @@ class Moon(Body):
init_position
=
[
0
,
0
,
363104
+
1.12
*
AU
],
init_velocity
=
[
-
(
29.79
+
1.03
),
0
,
0
],
texture
=
"moon.jpg"
,
size_scale
=
1.0
,
distance_scale
=
1.0
,
rotation_speed
=
0.25
,
ignore_mass
=
False
,
trail_color
=
None
,
show_name
=
False
):
rotation_speed
=
0.25
,
ignore_mass
=
False
,
trail_color
=
None
,
show_name
=
False
,
gravity_only_for_earth
=
False
):
"""
@param name: 月球名称
@param mass: 月球质量 (kg)
@param init_position: 初始位置 (km)
@param init_velocity: 初始速度 (km/s)
@param texture: 纹理图片
@param size_scale: 尺寸缩放
@param distance_scale: 距离缩放
@param rotation_speed: 自旋速度(度/小时)
@param ignore_mass: 是否忽略质量(如果为True,则不计算引力)
@param trail_color:月球拖尾颜色(默认天体颜色)
@param show_name: 是否显示月球名称
@param gravity_only_for_earth: 如果为True,则仅适用于地球的重力,与其他天体之间的重力不会受到影响
"""
params
=
{
"name"
:
name
,
"mass"
:
mass
,
...
...
@@ -43,18 +59,22 @@ class Moon(Body):
"show_name"
:
show_name
}
super
().
__init__
(
**
params
)
self
.
gravity_only_for_earth
=
gravity_only_for_earth
def
ignore_gravity
(
self
,
body
):
"""
是否忽略引力
@param body:
@return:
"""
if
self
.
gravity_only_for_earth
:
# 月球只对地球有引力,忽略其他的引力
if
isinstance
(
body
,
Earth
):
return
False
else
:
return
False
# def ignore_gravity(self, body):
# """
# 是否忽略引力
# :param body:
# :return:
# """
# # 月球只对地球有引力,忽略其他的引力
# if isinstance(body, Earth):
# return False
#
# return True
return
True
if
__name__
==
'__main__'
:
...
...
bodies/saturn.py
浏览文件 @
e84ca521
...
...
@@ -49,7 +49,7 @@ class Saturn(Body):
def
has_rings
(
self
):
"""
土星带光环的天体
:
return:
@
return:
"""
return
True
...
...
@@ -57,7 +57,7 @@ class Saturn(Body):
def
rings_color
(
self
):
"""
土星光环的颜色
:
return:
@
return:
"""
return
173
,
121
,
92
...
...
bodies/sun.py
浏览文件 @
e84ca521
...
...
@@ -46,7 +46,7 @@ class Sun(FixedStar):
def
is_fixed_star
(
self
):
"""
太阳为恒星
:
return:
@
return:
"""
return
True
...
...
common/color_utils.py
浏览文件 @
e84ca521
...
...
@@ -11,9 +11,9 @@
def
conv_to_vec4_color
(
colour
:
tuple
,
alpha
=
1
):
"""
:
param colour:
:
param alpha:
:
return:
@
param colour:
@
param alpha:
@
return:
"""
from
ursina
import
Vec4
if
len
(
colour
)
==
3
:
...
...
@@ -26,9 +26,9 @@ def conv_to_vec4_color(colour: tuple, alpha=1):
def
adjust_brightness
(
color
,
target_brightness
:
float
=
0.6
):
"""
调整颜色的亮度到目标 target_brightness(确保亮度不超过 1.0)
:
param color:
:
param target_brightness:(确保亮度不超过 1.0)
:
return:
@
param color:
@
param target_brightness:(确保亮度不超过 1.0)
@
return:
"""
from
ursina
import
Vec4
# 获取颜色的亮度值
...
...
common/func.py
浏览文件 @
e84ca521
...
...
@@ -17,9 +17,9 @@ import math
def
get_dominant_colors
(
infile
,
resize
=
(
20
,
20
)):
"""
获取图片的主要颜色
:
param infile:
:
param resize:
:
return:
@
param infile:
@
param resize:
@
return:
"""
image
=
Image
.
open
(
infile
)
...
...
@@ -49,12 +49,12 @@ def get_positions_velocitys(angles, velocity=1, radius=1, radius_offset=None, ve
"""
以位置 (0, 0, 0)为中心,随机获取空间上的位置和公转方向的速度集合
(比如:获取大批小行星的位置)
:
param angles: 参考中心位置(0, 0, 0)的角度集合
:
param velocity: 速度
:
param radius: 半径(距离中心位置(0, 0, 0)的距离)
:
param radius_offset:在半径的基础上,随机偏移的值
:
param velocity_offset:在速度的基础上,随机偏移的值
:
return:
@
param angles: 参考中心位置(0, 0, 0)的角度集合
@
param velocity: 速度
@
param radius: 半径(距离中心位置(0, 0, 0)的距离)
@
param radius_offset:在半径的基础上,随机偏移的值
@
param velocity_offset:在速度的基础上,随机偏移的值
@
return:
"""
angles
=
np
.
array
(
angles
*
np
.
pi
)
...
...
@@ -99,9 +99,9 @@ def find_file(file_path, default_val=None, find_deep=5):
def
calculate_distance
(
pos1
,
pos2
=
[
0
,
0
,
0
]):
"""
计算两点间的距离
:
param pos1:
:
param pos2:
:
return:
@
param pos1:
@
param pos2:
@
return:
"""
d
=
pow
(
pow
(
np
.
array
(
pos1
[
0
])
-
np
.
array
(
pos2
[
0
]),
2
)
+
pow
(
np
.
array
(
pos1
[
1
])
-
np
.
array
(
pos2
[
1
]),
2
)
+
...
...
common/image_utils.py
浏览文件 @
e84ca521
...
...
@@ -19,13 +19,13 @@ hsv_to_rgb = np.vectorize(colorsys.hsv_to_rgb)
def
image_file_enhance
(
imageFilePath
,
bright
,
contrast
,
color
,
sharpness
,
saveFolderPath
):
"""
图像增强之亮度、对比度与饱和度调整
:
param imageFilePath: 图像文件路径
:
param bright: 亮度
:
param contrast: 对比度
:
param color: 饱和度
:
param sharpness: 清晰度
:
param saveFolderPath: 结果保存路径
:
return:
@
param imageFilePath: 图像文件路径
@
param bright: 亮度
@
param contrast: 对比度
@
param color: 饱和度
@
param sharpness: 清晰度
@
param saveFolderPath: 结果保存路径
@
return:
"""
imageFileName
=
os
.
path
.
basename
(
imageFilePath
)
imageOriginal
=
Image
.
open
(
imageFilePath
)
...
...
@@ -59,13 +59,13 @@ def image_file_enhance(imageFilePath, bright, contrast, color, sharpness, saveFo
def
image_enhance
(
imageOriginal
,
bright
=
0
,
contrast
=
0
,
color
=
0
,
sharpness
=
0
):
"""
图像增强之亮度、对比度与饱和度调整
:
param imageFilePath: 图像文件路径
:
param bright: 亮度
:
param contrast: 对比度
:
param color: 饱和度
:
param sharpness: 清晰度
:
param saveFolderPath: 结果保存路径
:
return:
@
param imageFilePath: 图像文件路径
@
param bright: 亮度
@
param contrast: 对比度
@
param color: 饱和度
@
param sharpness: 清晰度
@
param saveFolderPath: 结果保存路径
@
return:
"""
image
=
imageOriginal
if
bright
>
0
:
...
...
@@ -194,8 +194,8 @@ def find_texture_root_path():
def
find_texture
(
texture
):
"""
尝试在多个路径下寻找纹理图片
:
param texture: 纹理图片
:
return: 纹理图片的路径
@
param texture: 纹理图片
@
return: 纹理图片的路径
"""
if
os
.
path
.
exists
(
texture
):
return
texture
...
...
@@ -339,8 +339,8 @@ if __name__ == '__main__':
# """
# 自适应地调整图片的对比度和亮度。
#
#
:
param image_path: 图像路径。
#
:
return: 调整后的图像对象。
#
@
param image_path: 图像路径。
#
@
return: 调整后的图像对象。
# """
# if isinstance(src_img, str):
# image = Image.open(src_img)
...
...
@@ -382,8 +382,8 @@ if __name__ == '__main__':
# """
# 自适应地调整图片的对比度和亮度。
#
#
:
param image_path: 图像路径。
#
:
return: 调整后的图像对象。
#
@
param image_path: 图像路径。
#
@
return: 调整后的图像对象。
# """
# if isinstance(src_img, str):
# image = Image.open(src_img)
...
...
@@ -418,8 +418,8 @@ if __name__ == '__main__':
# """
# 自适应地调整图片的对比度和亮度。
#
#
:
param image_path: 图像路径。
#
:
return: 调整后的图像对象。
#
@
param image_path: 图像路径。
#
@
return: 调整后的图像对象。
# """
# if isinstance(src_img, str):
# image = Image.open(src_img)
...
...
common/system.py
浏览文件 @
e84ca521
...
...
@@ -21,8 +21,8 @@ class System(object):
def
__init__
(
self
,
bodies
,
max_distance
=
200
*
AU
):
"""
:
param bodies:
:
param max_distance:系统的最大范围,超出范围的天体就不显示了
@
param bodies:
@
param max_distance:系统的最大范围,超出范围的天体就不显示了
"""
self
.
bodies
=
bodies
# self.adjust_distance_and_velocity()
...
...
@@ -78,7 +78,7 @@ class System(object):
def
total_mass
(
self
):
"""
总质量
:
return:
@
return:
"""
total_mass
=
0.0
for
body
in
self
.
bodies
:
...
...
@@ -91,7 +91,7 @@ class System(object):
def
center_of_mass
(
self
):
"""
质心
:
return:
@
return:
"""
r
=
np
.
zeros
(
2
)
for
body
in
self
.
bodies
:
...
...
@@ -101,8 +101,8 @@ class System(object):
def
evolve
(
self
,
dt
):
"""
:
param dt:
:
return:
@
param dt:
@
return:
"""
self
.
calc_bodies_acceleration
()
...
...
@@ -115,9 +115,9 @@ class System(object):
def
save_to_json
(
self
,
json_file_name
,
params
=
None
):
"""
:
param json_file_name:
:
param params:
:
return:
@
param json_file_name:
@
param params:
@
return:
"""
import
json
import
os
...
...
@@ -145,14 +145,14 @@ class System(object):
def
calc_bodies_acceleration
(
self
):
"""
计算加速度
:
return:
@
return:
"""
def
valid_body
(
body
):
"""
判断是否为有效的天体
:
param body:
:
return:
@
param body:
@
return:
"""
if
not
body
.
appeared
:
# 不显示
return
False
...
...
sim_scenes/func.py
浏览文件 @
e84ca521
...
...
@@ -20,13 +20,13 @@ def mayavi_run(bodies, dt=SECONDS_PER_WEEK,
bgcolor
=
(
1
/
255
,
1
/
255
,
30
/
255
)):
"""
用 mayavi 查看运行效果
:
param bodies: 天体
:
param dt: 单位:秒,按时间差进行演变,值越小越精确,但演变速度会慢。
:
param view_azimuth: 观测方位角,可选,float类型(以度为单位,0-360),用x轴投影到x-y平面上的球体上的位置矢量所对的角度。
:
param view_distance: 观测距离,可选,float类型 or 'auto',一个正浮点数,表示距放置相机的焦点的距离。
:
param view_focalpoint: 观测焦点,可选,类型为一个由3个浮点数组成的数组 or 'auto',,代表观测相机的焦点
:
param bgcolor:
:
return:
@
param bodies: 天体
@
param dt: 单位:秒,按时间差进行演变,值越小越精确,但演变速度会慢。
@
param view_azimuth: 观测方位角,可选,float类型(以度为单位,0-360),用x轴投影到x-y平面上的球体上的位置矢量所对的角度。
@
param view_distance: 观测距离,可选,float类型 or 'auto',一个正浮点数,表示距放置相机的焦点的距离。
@
param view_focalpoint: 观测焦点,可选,类型为一个由3个浮点数组成的数组 or 'auto',,代表观测相机的焦点
@
param bgcolor:
@
return:
"""
from
mayavi
import
mlab
from
simulators.mayavi_simulator
import
MayaviSimulator
...
...
@@ -146,10 +146,10 @@ def ursina_run(bodies,
def
mpl_run
(
bodies
,
dt
=
SECONDS_PER_WEEK
,
gif_file_name
=
None
,
gif_max_frame
=
200
):
"""
:
param bodies: 天体
:
param dt: 单位:秒,按时间差进行演变,值越小越精确,但演变速度会慢。
:
param gif_file_name: 导出的 gif 文件名,如果为空,则显示动画
:
return:
@
param bodies: 天体
@
param dt: 单位:秒,按时间差进行演变,值越小越精确,但演变速度会慢。
@
param gif_file_name: 导出的 gif 文件名,如果为空,则显示动画
@
return:
"""
from
simulators.mpl_simulator
import
MplSimulator
body_sys
=
System
(
bodies
)
...
...
sim_scenes/science/parabolic_curve.py
浏览文件 @
e84ca521
...
...
@@ -10,26 +10,39 @@ from bodies import Moon, Earth, Body
from
common.consts
import
SECONDS_PER_HOUR
,
SECONDS_PER_MINUTE
from
sim_scenes.func
import
ursina_run
,
get_vector2d_velocity
if
__name__
==
'__main__'
:
def
create_ejected_object
(
velocity
,
raduis
,
trail_color
,
angle
=
10
):
"""
抛物线模拟
创建一个被抛的物体
@param velocity: 抛出去的速度
@param raduis: 物体地球中心的半径
@param trail_color: 轨迹颜色
@param angle: 抛出去的角度(地平线夹角,默认为10)
@return:
"""
# TODO: 修改抛出物体的速度
velocity
=
8
# 物体飞不出地球太远,就落地
# velocity = 10 # 物体能飞出地球很远,但还是无法摆脱地球引力
# velocity = 11.2 # 脱离地球引力直接飞出。速度11.2千米/秒为脱离地球引力的速度叫第二宇宙速度
# 根据速度、角度获取矢量速度(vx、vy) -> vx² + vy² = velocity²
vx
,
vy
=
get_vector2d_velocity
(
velocity
,
angle
=
10
)
vx
,
vy
=
get_vector2d_velocity
(
velocity
,
angle
=
angle
)
moon
=
Moon
(
name
=
f
'物体速度:
{
velocity
}
'
,
mass
=
500
,
size_scale
=
2e6
,
trail_color
=
trail_color
,
init_position
=
[
0
,
raduis
,
0
],
init_velocity
=
[
vx
,
vy
,
0
],
gravity_only_for_earth
=
True
)
# 仅适用于地球的重力,物体之间重力不要受到影响
return
moon
if
__name__
==
'__main__'
:
"""
抛物线模拟
"""
# 地球在中心位置
e
=
Earth
(
init_position
=
[
0
,
0
,
0
],
size_scale
=
1
,
texture
=
"earth_hd.jpg"
,
init_velocity
=
[
0
,
0
,
0
])
bodies
=
[
e
,
Moon
(
name
=
'小月球'
,
mass
=
500
,
size_scale
=
2e6
,
init_position
=
[
0
,
e
.
raduis
+
300
,
0
],
# 在地球表面上
init_velocity
=
[
vx
,
vy
,
0
]),
]
raduis
=
e
.
raduis
+
300
# 红色:velocity = 8 # 物体飞不出地球太远,就落地
obj1
=
create_ejected_object
(
velocity
=
8
,
raduis
=
raduis
,
trail_color
=
(
255
,
0
,
0
))
# 绿色:velocity = 10 # 物体能飞出地球很远,但还是无法摆脱地球引力
obj2
=
create_ejected_object
(
velocity
=
10
,
raduis
=
raduis
,
trail_color
=
(
0
,
255
,
0
))
# 蓝色:velocity = 11.2 # 脱离地球引力直接飞出。速度11.2千米/秒为脱离地球引力的速度叫第二宇宙速度
obj3
=
create_ejected_object
(
velocity
=
11.2
,
raduis
=
raduis
,
trail_color
=
(
0
,
0
,
255
))
bodies
=
[
e
,
obj1
,
obj2
,
obj3
]
# 使用 ursina 查看的运行效果
# 常用快捷键: P:运行和暂停 O:重新开始 I:显示天体轨迹
...
...
sim_scenes/solar_system/solar_system_4.py
浏览文件 @
e84ca521
...
...
@@ -16,11 +16,11 @@ from common.consts import G
def
get_velocity
(
mass
,
distance
,
velocity
,
target_distance
):
"""
根据天体的质量和天体原始距离、速度,计算出能稳定围绕太阳转的速度。
:
param mass: 天体质量,单位 kg
:
param distance: 天体原始距离,单位 km
:
param velocity: 天体原始速度,单位 km/s
:
param target_distance: 目标距离,即新轨道的距离,单位 km
:
return: 稳定围绕太阳转的速度,单位 km/s
@
param mass: 天体质量,单位 kg
@
param distance: 天体原始距离,单位 km
@
param velocity: 天体原始速度,单位 km/s
@
param target_distance: 目标距离,即新轨道的距离,单位 km
@
return: 稳定围绕太阳转的速度,单位 km/s
"""
v
=
velocity
*
pow
(
distance
/
target_distance
,
0.5
)
# # 计算原始速率
...
...
simulators/func.py
浏览文件 @
e84ca521
...
...
@@ -20,8 +20,8 @@ COSMIC_FORE_COLOR = "white"
def
get_default_colors
(
styles
=
{}):
"""
:
param styles:
:
return:
@
param styles:
@
return:
"""
bg_color
=
styles
[
"bg_color"
]
if
"bg_color"
in
styles
else
"white"
# COSMIC_BG_COLOR
fore_color
=
styles
[
"fore_color"
]
if
"fore_color"
in
styles
else
"black"
# COSMIC_FORE_COLOR
...
...
@@ -35,8 +35,8 @@ def get_default_colors(styles={}):
def
create_fig_ax
(
styles
=
{}):
"""
:
param styles:
:
return:
@
param styles:
@
return:
"""
bg_color
,
fore_color
=
get_default_colors
(
styles
)
...
...
@@ -69,9 +69,9 @@ def create_fig_ax(styles={}):
def
update_ax_styles
(
ax
,
styles
=
{}):
"""
:
param ax:
:
param styles:
:
return:
@
param ax:
@
param styles:
@
return:
"""
plt
.
cla
()
bg_color
,
fore_color
=
get_default_colors
(
styles
)
...
...
simulators/mpl_simulator.py
浏览文件 @
e84ca521
...
...
@@ -32,10 +32,10 @@ class MplSimulator(Simulator):
def
save_as_gif
(
self
,
dt
,
gif_max_frame
=
200
,
gif_file_name
=
'bodies_run.gif'
,
styles
=
{}):
"""
保存 GIF 文件
:
param dt: 单位:秒,按时间差进行演变,值越小越精确,但演变速度会慢。
:
param gif_max_frame: 导出的 gif 文件的画面帧数
:
param gif_file_name: 导出的 gif 文件名
:
return:
@
param dt: 单位:秒,按时间差进行演变,值越小越精确,但演变速度会慢。
@
param gif_max_frame: 导出的 gif 文件的画面帧数
@
param gif_file_name: 导出的 gif 文件名
@
return:
"""
fig
,
ax
=
create_fig_ax
()
...
...
@@ -57,11 +57,11 @@ class MplSimulator(Simulator):
def
run
(
self
,
dt
,
**
kwargs
):
"""
:
param dt: 单位:秒,按时间差进行演变,值越小越精确,但演变速度会慢。
:
param kwargs:
@
param dt: 单位:秒,按时间差进行演变,值越小越精确,但演变速度会慢。
@
param kwargs:
gif_file_name: 导出的 gif 文件名,如果为空,则显示动画
gif_max_frame: 导出的 gif 文件的画面帧数
:
return:
@
return:
"""
gif_file_name
=
kwargs
[
"gif_file_name"
]
if
"gif_file_name"
in
kwargs
else
None
gif_max_frame
=
kwargs
[
"gif_max_frame"
]
if
"gif_max_frame"
in
kwargs
else
None
...
...
@@ -84,10 +84,10 @@ class MplSimulator(Simulator):
def
show_figure
(
self
,
ax
,
bodies
,
pause
=
0.1
,
update_ax
=
None
,
styles
=
{}):
"""
:
param ax:
:
param bodies:
:
param pause:
:
return:
@
param ax:
@
param bodies:
@
param pause:
@
return:
"""
if
update_ax
is
not
None
:
# 更新 ax
...
...
simulators/simulator.py
浏览文件 @
e84ca521
...
...
@@ -18,8 +18,8 @@ class Simulator(metaclass=ABCMeta):
def
__init__
(
self
,
bodies_sys
:
System
,
viewer_type
:
type
):
"""
:
param bodies_sys: 天体系统
:
param viewer_type: BodyViewer类型
@
param bodies_sys: 天体系统
@
param viewer_type: BodyViewer类型
"""
self
.
body_views
=
[]
self
.
bodies_sys
=
bodies_sys
...
...
@@ -28,8 +28,8 @@ class Simulator(metaclass=ABCMeta):
def
init_views
(
self
,
viewer_type
:
type
):
"""
:
param viewer_type: BodyViewer类型
:
return:
@
param viewer_type: BodyViewer类型
@
return:
"""
for
body
in
self
.
bodies_sys
.
bodies
:
view
=
viewer_type
(
body
,
self
.
bodies_sys
)
...
...
@@ -38,8 +38,8 @@ class Simulator(metaclass=ABCMeta):
def
evolve
(
self
,
dt
:
int
):
"""
单位:秒,按时间差进行演变,值越小越精确,但演变速度会慢。
:
param dt: 时间差(秒)
:
return:
@
param dt: 时间差(秒)
@
return:
"""
self
.
bodies_sys
.
evolve
(
dt
)
for
idx
,
view
in
enumerate
(
self
.
body_views
):
...
...
@@ -71,7 +71,7 @@ class Simulator(metaclass=ABCMeta):
def
run
(
self
,
dt
:
int
,
**
kwargs
):
"""
按时间差运行,值越小越精确,但演变速度会慢。
:
param dt: 时间差(秒)
:
return:
@
param dt: 时间差(秒)
@
return:
"""
pass
simulators/ursina/entities/entity_utils.py
浏览文件 @
e84ca521
...
...
@@ -42,7 +42,7 @@ def create_name_text(parent):
def
trail_init
(
parent
):
"""
拖尾球体的初始化
:
return:
@
return:
"""
# 存放拖尾球体
parent
.
trails
=
{}
...
...
@@ -79,7 +79,7 @@ def distance_between_two_points(point_a: Vec3, point_b: Vec3) -> float:
def
create_trails
(
parent
):
"""
创建拖尾
:
return:
@
return:
"""
# 当前天体的位置
try
:
...
...
@@ -129,8 +129,8 @@ def create_trails(parent):
def
create_trail_sphere
(
parent
,
pos
):
"""
在天体当前的位置创建一个拖尾球体
:
param pos:
:
return:
@
param pos:
@
return:
"""
# sphere = create_sphere(1,6) diamond sphere
trail
=
BodyTrail
(
color
=
parent
.
trail_color
,
scale
=
parent
.
trail_scale
,
position
=
pos
)
...
...
@@ -154,8 +154,8 @@ def merge_vectors(vectors):
def
create_trail_line
(
parent
,
pos
):
"""
在天体当前的位置创建一个拖尾球体
:
param pos:
:
return:
@
param pos:
@
return:
"""
if
hasattr
(
parent
,
"trail_last_pos"
):
trail_last_pos
=
parent
.
trail_last_pos
...
...
@@ -174,7 +174,7 @@ def create_trail_line(parent, pos):
def
create_rings
(
self
):
"""
创建行星环(使用土星贴图)
:
return:
@
return:
"""
rings_texture
=
'textures/saturnRings.jpg'
rings_texture
=
find_file
(
rings_texture
)
...
...
@@ -213,8 +213,8 @@ def clear_trails(self):
def
create_fixed_star_lights
(
fixed_star
):
"""
创建恒星的发光的效果、并作为灯光源
:
param entity:
:
return:
@
param entity:
@
return:
"""
# 如果是恒星(如:太阳),自身会发光,则需要关闭灯光
...
...
simulators/ursina/ui/ui_panel.py
浏览文件 @
e84ca521
...
...
@@ -65,9 +65,9 @@ class UiPanel(WindowPanel):
def
show_message
(
self
,
message
,
close_time
=
3
):
"""
显示消息框
:
param message: 消息内容
:
param close_time: 定义显示消息框关闭时间
:
return:
@
param message: 消息内容
@
param close_time: 定义显示消息框关闭时间
@
return:
"""
# 创建消息框
message_box
=
Text
(
text
=
message
,
font
=
UrsinaConfig
.
CN_FONT
,
background
=
True
,
origin
=
(
0
,
0
),
y
=
.
25
)
...
...
simulators/ursina/ursina_mesh.py
浏览文件 @
e84ca521
...
...
@@ -18,9 +18,9 @@ from simulators.ursina.ursina_config import UrsinaConfig
def
create_sphere
(
radius
,
subdivisions
):
"""
创建一个球体
:
param radius:
:
param subdivisions:
:
return:
@
param radius:
@
param subdivisions:
@
return:
"""
# 生成球体的顶点、UV坐标uvs、法线tris和三角面
verts
=
[]
...
...
simulators/ursina_simulator.py
浏览文件 @
e84ca521
...
...
@@ -74,8 +74,8 @@ class UrsinaSimulator(Simulator):
def
get_bodies_max_distance
(
self
,
body_views
):
"""
算法优化
:
param body_views:
:
return:
@
param body_views:
@
return:
"""
max_distance
=
0
for
b1
,
b2
in
itertools
.
combinations
(
body_views
,
2
):
...
...
@@ -93,7 +93,7 @@ class UrsinaSimulator(Simulator):
def
adjust_system_motion_params
(
self
):
"""
调整天体系统运行的参数
:
return:
@
return:
"""
max_distance
=
self
.
get_bodies_max_distance
(
self
.
body_views
)
# 根据天体之间的距离,调整 application.time_scale(控制摄像头运动的速度)
...
...
@@ -116,7 +116,7 @@ class UrsinaSimulator(Simulator):
def
check_interval_expired
(
self
):
"""
检查时间间隔是否已过期
:
return:
@
return:
"""
now
=
datetime
.
datetime
.
now
()
elapsed_time
=
now
-
self
.
last_time
...
...
@@ -154,8 +154,8 @@ class UrsinaSimulator(Simulator):
def
cosmic_background
(
self
,
texture
=
'../textures/cosmic2.jpg'
):
"""
加入宇宙背景
:
param texture:
:
return:
@
param texture:
@
return:
"""
# Add skybox
...
...
simulators/views/body_view.py
浏览文件 @
e84ca521
...
...
@@ -51,8 +51,8 @@ class BodyView(metaclass=ABCMeta):
def
__find_texture
(
self
,
texture
):
"""
尝试在多个路径下寻找纹理图片
:
param texture: 纹理图片
:
return: 纹理图片的路径
@
param texture: 纹理图片
@
return: 纹理图片的路径
"""
return
find_texture
(
texture
)
# if os.path.exists(texture):
...
...
@@ -68,8 +68,8 @@ class BodyView(metaclass=ABCMeta):
def
__get_texture_main_color
(
self
,
texture
):
"""
获取纹理图片的主要颜色
:
param texture:
:
return:
@
param texture:
@
return:
"""
try
:
colors
=
get_dominant_colors
(
texture
)
...
...
@@ -83,14 +83,14 @@ class BodyView(metaclass=ABCMeta):
def
update
(
self
):
"""
更新天体信息和数据,比如:更新天体的位置
:
return:
@
return:
"""
pass
def
disappear
(
self
):
"""
天体消失的操作,比如:销毁天体视图对象
:
return:
@
return:
"""
pass
...
...
@@ -98,6 +98,6 @@ class BodyView(metaclass=ABCMeta):
def
appear
(
self
):
"""
天体显示的操作,比如:构建天体视图对象
:
return:
@
return:
"""
pass
simulators/views/mayavi_view.py
浏览文件 @
e84ca521
...
...
@@ -24,7 +24,7 @@ class MayaviView(BodyView):
def
update
(
self
):
"""
更新天体信息和数据,比如:更新天体的位置
:
return:
@
return:
"""
if
hasattr
(
self
.
sphere
,
"mlab_source"
):
# self.sphere.mlab_source.x 的位置是已经和 distance_scale 进行了相乘
...
...
@@ -83,7 +83,7 @@ class MayaviView(BodyView):
def
appear
(
self
):
"""
天体显示的操作,比如:构建天体视图对象
:
return:
@
return:
"""
if
hasattr
(
self
.
body
,
"torus_stars"
):
# 暂不支持环状小行星群
...
...
@@ -131,8 +131,8 @@ class MayaviView(BodyView):
def
__set_texture
(
self
,
image_file
):
"""
设置纹理图片到天体
:
param image_file:
:
return:
@
param image_file:
@
return:
"""
outfile
=
image_file
.
replace
(
'.jpg'
,
'_flipped.jpg'
).
replace
(
'.png'
,
'_flipped.jpg'
)
if
os
.
path
.
exists
(
outfile
):
...
...
simulators/views/ursina_view.py
浏览文件 @
e84ca521
...
...
@@ -29,7 +29,7 @@ class UrsinaView(BodyView):
def
update
(
self
):
"""
:
return:
@
return:
"""
self
.
planet
.
update
()
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录