首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >测地球面的算法

测地球面的算法
EN

Stack Overflow用户
提问于 2013-07-18 00:45:06
回答 4查看 11.4K关注 0票数 6

我必须用更小的均匀分布的球做一个球体。我认为最好的方法是建立一个基于三角形的测地球体,并使用顶点作为我的球的中点。但是我写不出一个生成顶点的算法。用C++或伪代码回答会更好。

测地球体示例:http://i.stack.imgur.com/iNQfP.png

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2013-07-23 02:59:15

使用@Muckle_ewe给我的链接,我能够编写以下算法:在main()之外

代码语言:javascript
复制
class Vector3d {  // this is a pretty standard vector class
public:
    double x, y, z;
    ...
}

void subdivide(const Vector3d &v1, const Vector3d &v2, const Vector3d &v3, vector<Vector3d> &sphere_points, const unsigned int depth) {
    if(depth == 0) {
        sphere_points.push_back(v1);
        sphere_points.push_back(v2);
        sphere_points.push_back(v3);
        return;
    }
    const Vector3d v12 = (v1 + v2).norm();
    const Vector3d v23 = (v2 + v3).norm();
    const Vector3d v31 = (v3 + v1).norm();
    subdivide(v1, v12, v31, sphere_points, depth - 1);
    subdivide(v2, v23, v12, sphere_points, depth - 1);
    subdivide(v3, v31, v23, sphere_points, depth - 1);
    subdivide(v12, v23, v31, sphere_points, depth - 1);
}

void initialize_sphere(vector<Vector3d> &sphere_points, const unsigned int depth) {
    const double X = 0.525731112119133606;
    const double Z = 0.850650808352039932;
    const Vector3d vdata[12] = {
        {-X, 0.0, Z}, { X, 0.0, Z }, { -X, 0.0, -Z }, { X, 0.0, -Z },
        { 0.0, Z, X }, { 0.0, Z, -X }, { 0.0, -Z, X }, { 0.0, -Z, -X },
        { Z, X, 0.0 }, { -Z, X, 0.0 }, { Z, -X, 0.0 }, { -Z, -X, 0.0 }
    };
    int tindices[20][3] = {
        {0, 4, 1}, { 0, 9, 4 }, { 9, 5, 4 }, { 4, 5, 8 }, { 4, 8, 1 },
        { 8, 10, 1 }, { 8, 3, 10 }, { 5, 3, 8 }, { 5, 2, 3 }, { 2, 7, 3 },
        { 7, 10, 3 }, { 7, 6, 10 }, { 7, 11, 6 }, { 11, 0, 6 }, { 0, 1, 6 },
        { 6, 1, 10 }, { 9, 0, 11 }, { 9, 11, 2 }, { 9, 2, 5 }, { 7, 2, 11 }
    };
    for(int i = 0; i < 20; i++)
        subdivide(vdata[tindices[i][0]], vdata[tindices[i][1]], vdata[tindices[i][2]], sphere_points, depth);
}

然后在main()

代码语言:javascript
复制
vector<Vector3d> sphere_points;
initialize_sphere(sphere_points, DEPTH);  // where DEPTH should be the subdivision depth
for(const Vector3d &point : sphere_points)
    const Vector3d point_tmp = point * RADIUS + CENTER;  // Then for the sphere I want to draw, I  iterate over all the precomputed sphere points and with a linear function translate the sphere to its CENTER and chose the proper RADIUS

实际上,您只需使用initialize_sphere()一次,并将结果用于您想要绘制的每个球体。

票数 11
EN

Stack Overflow用户

发布于 2013-07-18 01:03:39

我以前在一个图形项目中这样做过,我使用的算法在这个网站上有详细的介绍

http://www.opengl.org.ru/docs/pg/0208.html

只需忽略任何openGL绘图调用,只编写处理创建实际顶点的部分

票数 2
EN

Stack Overflow用户

发布于 2013-07-18 00:58:37

有一些众所周知的算法来三角剖分曲面。如果你不想自己编写一个网格,你应该能够使用GNU Triangulated Surface Library生成一个合适的网格。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17705621

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档