12 vector<Vec3d> CGraphGenerator::m_Vertices;
13 vector<Face> CGraphGenerator::m_Faces;
18 initBasePolyhedron(nBaseType);
19 projectToSphere(fRadius);
21 for (
int L = 0 ; L < nLevels ; L++)
24 projectToSphere(fRadius);
27 buildNodesAndEdges(pPlainGraph);
38 generateTetrahedron();
46 generateIcosahedron();
50 printf(
"Wrong base type\n");
55 void CGraphGenerator::generateTetrahedron()
58 float fSqrt3 =
sqrt(3.0);
61 Math3d::SetVec(
v[0], fSqrt3, fSqrt3, fSqrt3);
62 Math3d::SetVec(
v[1], -fSqrt3, -fSqrt3, fSqrt3);
63 Math3d::SetVec(
v[2], -fSqrt3, fSqrt3, -fSqrt3);
64 Math3d::SetVec(
v[3], fSqrt3, -fSqrt3, -fSqrt3);
66 for (
int i = 0 ; i < 4 ; i++)
68 m_Vertices.push_back(
v[i]);
78 for (
int i = 0 ; i < 4 ; i++)
83 m_Faces.push_back(f[i]);
87 void CGraphGenerator::generateOctahedron()
91 Math3d::SetVec(
v[0], 1, 0, 0);
92 Math3d::SetVec(
v[1], -1, 0, 0);
93 Math3d::SetVec(
v[2], 0, 1, 0);
94 Math3d::SetVec(
v[3], 0, -1, 0);
95 Math3d::SetVec(
v[4], 0, 0, 1);
96 Math3d::SetVec(
v[5], 0, 0, -1);
98 for (
int i = 0 ; i < 6 ; i++)
100 m_Vertices.push_back(
v[i]);
114 for (
int i = 0 ; i < 8 ; i++)
119 m_Faces.push_back(f[i]);
123 void CGraphGenerator::generateIcosahedron()
126 float tau = 0.8506508084;
127 float one = 0.5257311121;
131 Math3d::SetVec(
v[0], tau, one, 0);
132 Math3d::SetVec(
v[1], -tau, one, 0);
133 Math3d::SetVec(
v[2], -tau, -one, 0);
134 Math3d::SetVec(
v[3], tau, -one, 0);
135 Math3d::SetVec(
v[4], one, 0, tau);
136 Math3d::SetVec(
v[5], one, 0, -tau);
137 Math3d::SetVec(
v[6], -one, 0, -tau);
138 Math3d::SetVec(
v[7], -one, 0, tau);
139 Math3d::SetVec(
v[8], 0, tau, one);
140 Math3d::SetVec(
v[9], 0, -tau, one);
141 Math3d::SetVec(
v[10], 0, -tau, -one);
142 Math3d::SetVec(
v[11], 0, tau, -one);
144 for (
int i = 0 ; i < 12 ; i++)
146 m_Vertices.push_back(
v[i]);
162 f[10].
set(10, 4, 11);
163 f[11].
set(10, 11, 3);
173 for (
int i = 0 ; i < 20 ; i++)
178 m_Faces.push_back(f[i]);
182 void CGraphGenerator::subDivide()
188 for (
int f = 0 ; f < int(m_Faces.size()) ; f++)
191 int nA = m_Faces.at(f).m_n1;
192 int nB = m_Faces.at(f).m_n2;
193 int nC = m_Faces.at(f).m_n3;
196 Vec3d a = m_Vertices.at(nA);
197 Vec3d b = m_Vertices.at(nB);
198 Vec3d c = m_Vertices.at(nC);
202 Math3d::AddVecVec(
a, b, a_m);
203 Math3d::MulVecScalar(a_m, 0.5, a_m);
204 Math3d::AddVecVec(b,
c, b_m);
205 Math3d::MulVecScalar(b_m, 0.5, b_m);
206 Math3d::AddVecVec(
c,
a, c_m);
207 Math3d::MulVecScalar(c_m, 0.5, c_m);
217 int nA_m, nB_m, nC_m;
220 nIndex = findVertex(a_m, 0.01);
224 m_Vertices.push_back(a_m);
225 nA_m = m_Vertices.size() - 1;
232 nIndex = findVertex(b_m, 0.01);
236 m_Vertices.push_back(b_m);
237 nB_m = m_Vertices.size() - 1;
244 nIndex = findVertex(c_m, 0.01);
248 m_Vertices.push_back(c_m);
249 nC_m = m_Vertices.size() - 1;
258 f[0].
set(nA, nA_m, nC_m);
259 f[1].
set(nA_m, nB, nB_m);
260 f[2].
set(nC_m, nB_m, nC);
261 f[3].
set(nA_m, nB_m, nC_m);
263 for (
int i = 0 ; i < 4 ; i++)
265 faces.push_back(f[i]);
273 void CGraphGenerator::projectToSphere(
float fRadius)
279 for (
int v = 0 ;
v < int(m_Vertices.size()) ;
v++)
281 float X = m_Vertices.at(
v).x;
282 float Y = m_Vertices.at(
v).y;
283 float Z = m_Vertices.at(
v).z;
288 float theta = atan2(Y, X);
289 float phi = atan2(
sqrt(X * X + Y * Y), Z);
292 x = fRadius * sin(phi) * cos(theta);
293 y = fRadius * sin(phi) * sin(theta);
294 z = fRadius * cos(phi);
296 m_Vertices.at(
v).x = x;
297 m_Vertices.at(
v).y = y;
298 m_Vertices.at(
v).z = z;
307 for (
int v = 0 ;
v < int(m_Vertices.size()) ;
v++)
318 for (
int f = 0 ; f < int(m_Faces.size()) ; f++)
321 pGraph->
addEdge(m_Faces.at(f).m_n1, m_Faces.at(f).m_n2);
322 pGraph->
addEdge(m_Faces.at(f).m_n2, m_Faces.at(f).m_n3);
323 pGraph->
addEdge(m_Faces.at(f).m_n3, m_Faces.at(f).m_n1);
327 int CGraphGenerator::findVertex(
Vec3d position,
float fEpsilon)
330 float fMinDistance = FLT_MAX;
333 for (
int v = 0 ;
v < int(m_Vertices.size()) ;
v++)
335 fDistance = Math3d::Angle(position, m_Vertices.at(
v));
337 if (fDistance < fMinDistance)
339 fMinDistance = fDistance;
344 if (fMinDistance > fEpsilon)