SP3近似杂化轨道3d stl模型python生成

技术与数学详解
1. 引言
SP3杂化轨道是化学中描述碳原子成键的重要概念。碳原子的sp³杂化形成四个等价的杂化轨道,空间呈四面体对称分布,轨道夹角为109.5°。每个sp³轨道呈哑铃形,一端较大(指向成键方向),另一端较小(与原子核相连)。
本文详细介绍如何使用Python生成sp³杂化轨道的三维几何模型,包括数学公式、代码实现和模型导出。
2. 数学原理
2.1 四面体几何
sp³杂化轨道的核心是四面体对称性。四个轨道指向四面体的四个顶点:
方向向量=13(1,1,1), 13(1,−1,−1), 13(−1,1,−1), 13(−1,−1,1)方向向量 = \frac{1}{\sqrt{3}}(1, 1, 1),\ \frac{1}{\sqrt{3}}(1, -1, -1),\ \frac{1}{\sqrt{3}}(-1, 1, -1),\ \frac{1}{\sqrt{3}}(-1, -1, 1)方向向量=31(1,1,1), 31(1,−1,−1), 31(−1,1,−1), 31(−1,−1,1)
这四个方向两两之间的夹角为109.5°,完美符合sp³杂化轨道的空间分布。
2.2 轨道形状建模
sp³轨道形状可以分解为两部分:
- 主体部分:从原子核出发,半径逐渐增大
- 末端封口:半球形封盖
2.2.1 主体半径曲线
使用正弦曲线实现平滑的半径增长:
r(t)=rmax⋅sin(ttend⋅π2)r(t) = r_{max} \cdot \sin\left(\frac{t}{t_{end}} \cdot \frac{\pi}{2}\right)r(t)=rmax⋅sin(tendt⋅2π)
其中:
- t∈[0,1]t \in [0, 1]t∈[0,1] 为归一化位置参数
- rmaxr_{max}rmax 为最大半径
- tend=0.85t_{end} = 0.85tend=0.85 为主体结束位置
2.2.2 半球封口
主体末端使用完整的半球面封口,球心位于主体末端:
Psphere(θ,ϕ)=C+rmax⋅(sinϕ⋅cosθ⋅u⃗1+sinϕ⋅sinθ⋅u⃗2+cosϕ⋅d⃗)P_{sphere}(\theta, \phi) = C + r_{max} \cdot (\sin\phi \cdot \cos\theta \cdot \vec{u}_1 + \sin\phi \cdot \sin\theta \cdot \vec{u}_2 + \cos\phi \cdot \vec{d})Psphere(θ,ϕ)=C+rmax⋅(sinϕ⋅cosθ⋅u1+sinϕ⋅sinθ⋅u2+cosϕ⋅d)
其中 d⃗\vec{d}d 为轨道方向向量,u⃗1,u⃗2\vec{u}_1, \vec{u}_2u1,u2 为与 d⃗\vec{d}d 垂直的正交基向量。
3. 代码实现
3.1 基础网格生成
import numpy as np
from stl import mesh
import math
def create_orbital_mesh(start_point, direction, length=2.2, max_radius=0.55, n_lat=20, n_lon=32):
"""生成单个轨道网格"""
vertices = []
faces = []
direction = np.array(direction) / np.linalg.norm(direction)
# 构建正交基
if abs(direction[2]) < 0.9:
v1 = np.cross(direction, (0, 0, 1))
else:
v1 = np.cross(direction, (1, 0, 0))
v1 = v1 / np.linalg.norm(v1)
v2 = np.cross(direction, v1)
v2 = v2 / np.linalg.norm(v2)
3.2 顶点计算
body_end_t = 0.85
# 主体部分
for i in range(int(n_lat * body_end_t) + 1):
t = i / n_lat
radius = max_radius * math.sin(t / body_end_t * math.pi / 2)
pos = start_point + direction * (t * length)
for j in range(n_lon):
theta = 2 * math.pi * j / n_lon
offset = v1 * (radius * math.cos(theta)) + v2 * (radius * math.sin(theta))
vertex = pos + offset
vertices.append(vertex.tolist())
3.3 半球封口
# 半球封口
sphere_center = start_point + direction * (body_end_t * length)
n_hemi = 12
for i in range(n_hemi + 1):
phi = math.pi * i / (2 * n_hemi)
sphere_r = max_radius * math.sin(phi)
z_offset = max_radius * math.cos(phi)
pos = sphere_center + direction * z_offset
for j in range(n_lon):
theta = 2 * math.pi * j / n_lon
offset = v1 * (sphere_r * math.cos(theta)) + v2 * (sphere_r * math.sin(theta))
vertices.append((pos + offset).tolist())
3.4 面索引生成
# 主体侧面
for i in range(int(n_lat * body_end_t)):
for j in range(n_lon):
next_j = (j + 1) % n_lon
v0 = i * n_lon + j
v1 = i * n_lon + next_j
v2 = (i + 1) * n_lon + next_j
v3 = (i + 1) * n_lon + j
faces.append([v0, v1, v2])
faces.append([v0, v2, v3])
# 半球封口连接到主体
# ... 类似的三角面生成逻辑 ...
return np.array(vertices), np.array(faces)
3.5 四面体构建
# 四个方向向量(四面体)
directions = [(1, 1, 1), (1, -1, -1), (-1, 1, -1), (-1, -1, 1)]
directions = [np.array(d) / np.linalg.norm(d) for d in directions]
all_vertices, all_faces = [], []
vertex_offset = 0
# 生成四个sp³轨道
for direction in directions:
v, f = create_orbital_mesh((0, 0, 0), direction, length=2.2, max_radius=0.55)
all_vertices.extend(v)
all_faces.extend(f + vertex_offset)
vertex_offset += len(v)
# 添加中心原子(碳原子)
v, f = create_sphere((0, 0, 0), 0.35)
all_vertices.extend(v)
all_faces.extend(f + vertex_offset)
3.6 STL导出
# 合并所有顶点
vertices = np.array(all_vertices)
faces = np.array(all_faces)
# 创建STL网格
stl_mesh = mesh.Mesh(np.zeros(faces.shape[0], dtype=mesh.Mesh.dtype))
for i, f in enumerate(faces):
for j in range(3):
stl_mesh.vectors[i][j] = vertices[f[j]]
stl_mesh.normals
stl_mesh.save('sp3_orbital.stl')
4. 进阶:镜像小轨道
如果要生成对称的小轨道(反方向,大小30%):
small_scale = 0.3
for direction in directions:
# 反方向
opposite_dir = -np.array(direction)
v, f = create_orbital_mesh(
(0, 0, 0),
opposite_dir,
length=2.2*small_scale,
max_radius=0.55*small_scale
)
all_vertices.extend(v)
all_faces.extend(f + vertex_offset)
vertex_offset += len(v)
5. 模型参数
| 参数 | 值 | 说明 |
|---|---|---|
| 大轨道长度 | 2.2 | 主轨道总长度 |
| 大轨道最大半径 | 0.55 | 轨道最粗处半径 |
| 小轨道比例 | 30% | 镜像轨道相对于大轨道的尺寸 |
| 中心原子半径 | 0.35 | 碳原子球体半径 |
| 主体结束位置 | 85% | 主体占总长度的比例 |
| 纬线数量 | 20 | 主体网格密度 |
| 经线数量 | 32 | 环向网格密度 |
6. 可视化
使用matplotlib生成预览图:
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
fig = plt.figure(figsize=(10, 10))
ax = fig.add_subplot(111, projection='3d')
triangles = [vector for vector in stl_mesh.vectors]
poly3d = Poly3DCollection(triangles, alpha=0.85, edgecolor='k', linewidth=0.05)
poly3d.set_facecolor('#44dd88')
ax.add_collection3d(poly3d)
ax.set_xlim([-3, 3])
ax.set_ylim([-3, 3])
ax.set_zlim([-3, 3])
ax.view_init(elev=20, azim=35)
plt.savefig('preview.png', dpi=150)
7. 总结
本文详细介绍了sp³杂化轨道三维模型的生成方法,涵盖:
- 四面体几何:四个方向向量的构建
- 水滴形主体:使用正弦曲线实现平滑半径增长
- 半球封口:使用球面公式实现圆润的末端封盖
- 镜像对称:可扩展生成反向的小轨道
该方法生成的模型可直接用于3D打印、科学可视化或分子模拟展示。
由Zengtudor的助手rxr与zengtudor一同完成 时间:2026-03-02
更多推荐


所有评论(0)