|
- #include <cmath>
- #include <vector>
- struct Point3D {
- double x, y, z;
- };
- // 初始十二个顶点坐标,它们是单位半径球面上正十二面体的顶点
- std::vector<Point3D> initialVertices = {
- { 0.0, 0.0, 1.0 }, // North pole
- { 0.8944271909999159, 0.0, -0.4472135954999579 }, // Equatorial vertices
- {-0.8944271909999159, 0.0, -0.4472135954999579 },
- { 0.2763932022500212, 0.8506508083520399, -0.4472135954999579 },
- { 0.2763932022500212, -0.8506508083520399, -0.4472135954999579 },
- {-0.7236067977499788, 0.5257311121191336, -0.4472135954999579 },
- {-0.7236067977499788, -0.5257311121191336, -0.4472135954999579 },
- { 0.7236067977499788, 0.5257311121191336, 0.4472135954999579 },
- { 0.7236067977499788, -0.5257311121191336, 0.4472135954999579 },
- {-0.2763932022500212, 0.8506508083520399, 0.4472135954999579 },
- {-0.2763932022500212, -0.8506508083520399, 0.4472135954999579 }
- };
- void subdivide(std::vector<Point3D>& vertices, std::vector<std::array<int, 3>>& faces) {
- std::vector<Point3D> newVertices;
- std::vector<std::array<int, 3>> newFaces;
- for (const auto& face : faces) {
- // 中心点索引
- int centerIndex = vertices.size();
- // 计算三角形中心点
- Point3D center = {
- (vertices[face[0]].x + vertices[face[1]].x + vertices[face[2]].x) / 3.0,
- (vertices[face[0]].y + vertices[face[1]].y + vertices[face[2]].y) / 3.0,
- (vertices[face[0]].z + vertices[face[1]].z + vertices[face[2]].z) / 3.0
- };
- // 投影到单位球面上
- double length = std::sqrt(center.x * center.x + center.y * center.y + center.z * center.z);
- if (length > 1e-6) {
- center.x /= length;
- center.y /= length;
- center.z /= length;
- }
- // 添加新的顶点
- vertices.push采用back(center);
- // 创建四个新的面
- newFaces.push采用back({ face[0], centerIndex, face[2] });
- newFaces.push采用back({ face[1], centerIndex, face[0] });
- newFaces.push采用back({ face[2], centerIndex, face[1] });
- newFaces.push采用back({ centerIndex, face[1], face[2] });
- // 移除旧的面
- }
-
- // 替换旧的面和顶点数据
- faces.swap(newFaces);
- }
- int main() {
- // 初始化基本的正十二面体顶点和面片
- std::vector<Point3D> vertices(initialVertices.begin(), initialVertices.end());
- std::vector<std::array<int, 3>> faces; // 填充正十二面体的各个面
- // 对初始网格进行多次细分以得到更细密的网格
- for (int i = 0; i < desiredSubdivisionLevels; ++i) {
- subdivide(vertices, faces);
- }
- // 输出最终的网格顶点和面信息
- // ...
- return 0;
- }
复制代码 |
|