2013-07-17 32 views
2

我必須用更小的均勻分佈的球製作球體。我認爲最好的方法是建立一個基於三角形的測地球,並使用頂點作爲我的球的中點。但我沒有寫出生成頂點的算法。 在C++或僞代碼中的答案會更好。測地球的測地球的算法

實施例:http://i.stack.imgur.com/iNQfP.png

回答

6

使用@Muckle_ewe給我的鏈接,我可以到以下算法代碼: 外main()

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()然後:

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()一次,並將結果用於要繪製的每個球體。

1

有公知的算法來三角測量表面。如果您不想自己編寫其中一個,您應該可以使用GNU Triangulated Surface Library生成合適的網格。

1

我做這個之前,圖形項目,我所使用的算法是詳盡這個網站

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

上不理會任何OpenGL繪圖調用和唯一的代碼了,與創建處理的部件實際頂點

+0

使用你給我的鏈接,我能夠編碼算法(請參閱我的答案)。非常感謝。 –

0

它取決於您想要球體所具有的三角形的數量。您可以擁有無​​限的解析度。

首先關注創建一個圓頂,稍後您可以通過採用上部圓頂的負座標來加倍。您將通過互鎖三角形的行來生成球體。 你的三角形是等邊的,所以決定一個長度。 將2(pi)r除以想要在圓頂的最下面一排上的三角形的數量。 這將是每個三角形每邊的長度。

接下來,您需要創建一個與球體表面相交的同心圓。 在這個圓和圓頂的基地之間將是您的第一排。 您將需要找到每個三角形傾斜的角度。 (我會在稍後發佈的時候發帖)

重複每個同心圓(產生行)的過程,直到行的高度*行數近似等於你開始的2(pi)r。

如果我有機會,我會盡量編程它。你也可以嘗試在數學論壇上發帖。