Files
stt/demo/example_usage.py
2025-11-27 15:06:01 +08:00

138 lines
3.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
"""
STT Python绑定使用示例
这个脚本展示了如何使用STT的Python绑定来生成球面三角网格。
"""
import pystt as stt
import os
import sys
def basic_example():
"""基本使用示例"""
print("=== 基本使用示例 ===")
# 创建生成器
generator = stt.SttGenerator()
# 设置参数
generator.set_tree_depth(3, 6) # 最小深度3最大深度6
generator.set_reference_system("WGS84") # 使用WGS84参考系统
# 运行生成
result = generator.run("basic_output.msh")
print(f"生成结果: {'成功' if result == 0 else '失败'}")
print(f"输出文件: basic_output.msh")
print()
def quick_creation_example():
"""快速创建示例"""
print("=== 快速创建示例 ===")
# 使用便利函数快速创建
info = stt.create_stt(
min_depth=2,
max_depth=5,
reference_system="Earth",
output_file="quick_output.msh"
)
print("生成信息:")
for key, value in info.items():
print(f" {key}: {value}")
print()
def advanced_example():
"""高级使用示例"""
print("=== 高级使用示例 ===")
generator = stt.SttGenerator()
# 设置自定义参考系统(月球)
generator.set_pole_equator_radius(1738000.0, 1738000.0) # 月球半径
# 设置二十面体方向
generator.set_icosahedron_orient(0.0, 90.0) # 北极方向
# 使用完整参数运行
params = {
"output_msh": "advanced_output.msh",
"output_vertex": "vertices.txt",
"output_triangle_center": "triangle_centers.txt",
"output_neighbor": "neighbors.txt"
}
result = generator.run_full(params)
print(f"高级生成结果: {'成功' if result == 0 else '失败'}")
print("输出文件:")
for key, file in params.items():
if os.path.exists(file):
size = os.path.getsize(file)
print(f" {file}: {size} bytes")
else:
print(f" {file}: 未生成")
print()
def custom_reference_system_example():
"""自定义参考系统示例"""
print("=== 自定义参考系统示例 ===")
generator = stt.SttGenerator()
# 设置自定义椭球参数(例如火星)
# 火星: 赤道半径 3396.2 km, 极半径 3376.2 km
generator.set_pole_equator_radius(3396200.0, 3376200.0)
generator.set_tree_depth(3, 7)
result = generator.run("mars_grid.msh")
print(f"火星网格生成: {'成功' if result == 0 else '失败'}")
print()
def check_module_info():
"""检查模块信息"""
print("=== 模块信息 ===")
info = stt.get_info()
for key, value in info.items():
print(f"{key}: {value}")
print(f"可用常量:")
print(f" stt.WGS84: {stt.WGS84}")
print(f" stt.EARTH: {stt.EARTH}")
print(f" stt.MOON: {stt.MOON}")
print()
def main():
"""主函数"""
print("STT Python绑定使用示例")
print("=" * 30)
# 检查模块是否可用
try:
check_module_info()
except Exception as e:
print(f"错误: 无法加载STT模块 - {e}")
print("请确保已经正确安装STT Python绑定")
sys.exit(1)
# 运行各种示例
try:
basic_example()
quick_creation_example()
advanced_example()
custom_reference_system_example()
print("所有示例运行完成!")
print("检查生成的文件以查看结果。")
except Exception as e:
print(f"运行示例时出错: {e}")
import traceback
traceback.print_exc()
if __name__ == "__main__":
main()