| 276 | } |
| 277 | |
| 278 | MR::Mesh makeTorusWithSelfIntersections( float primaryRadius, float secondaryRadius, int primaryResolution, int secondaryResolution, |
| 279 | std::vector<Vector3f>* pointsOut ) |
| 280 | { |
| 281 | int i, j, k; |
| 282 | float a, b; |
| 283 | |
| 284 | int vertexCount = primaryResolution * secondaryResolution; |
| 285 | std::vector<Vector3f> points( vertexCount ); |
| 286 | bool pSet = false; |
| 287 | if ( pointsOut ) |
| 288 | pointsOut->resize( primaryResolution ); |
| 289 | k = 0; |
| 290 | for ( i = 0; i < secondaryResolution; ++i ) |
| 291 | { |
| 292 | a = 2 * i * PI_F / secondaryResolution; |
| 293 | for ( j = 0; j < primaryResolution; ++j ) |
| 294 | { |
| 295 | b = 2 * j * PI_F / primaryResolution; |
| 296 | |
| 297 | points[k].x = ( primaryRadius - secondaryRadius * std::cos( a ) ) * std::cos( b ); |
| 298 | points[k].y = ( primaryRadius - secondaryRadius * std::cos( a ) ) * std::sin( b ); |
| 299 | points[k].z = secondaryRadius * std::sin( 2.f * a ); |
| 300 | ++k; |
| 301 | if ( pointsOut && !pSet ) |
| 302 | ( *pointsOut )[j] = {primaryRadius * std::cos( b ),primaryRadius * std::sin( b ),0.0f}; |
| 303 | } |
| 304 | pSet = true; |
| 305 | } |
| 306 | |
| 307 | Triangulation t; |
| 308 | |
| 309 | int triangleCount = 2 * vertexCount - 2 * primaryResolution; |
| 310 | t.reserve( triangleCount ); |
| 311 | |
| 312 | k = 0; |
| 313 | for ( i = 0; i < secondaryResolution; ++i ) |
| 314 | { |
| 315 | for ( j = 0; j < primaryResolution; ++j ) |
| 316 | { |
| 317 | t.push_back( { |
| 318 | VertId( i * primaryResolution + j ), |
| 319 | VertId( ( i + 1 ) % secondaryResolution * primaryResolution + j ), |
| 320 | VertId( i * primaryResolution + ( j + 1 ) % primaryResolution ) } ); |
| 321 | |
| 322 | t.push_back( { |
| 323 | VertId( i * primaryResolution + ( j + 1 ) % primaryResolution ), |
| 324 | VertId( ( i + 1 ) % secondaryResolution * primaryResolution + j ), |
| 325 | VertId( ( i + 1 ) % secondaryResolution * primaryResolution + ( j + 1 ) % primaryResolution ) } ); |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | return Mesh::fromTriangles( std::move(points), t ); |
| 330 | } |
| 331 | |
| 332 | } |