2
votes

I'm looking at the code in three.js, specifically the THREE.SphereGeometry method to create a sphere: https://github.com/mrdoob/three.js/blob/master/src/extras/geometries/SphereGeometry.js

There are 2 sets of loops - I'm looking at the second one.

My question is this: There's an array of arrays that's created, called vertices. Into this array are added arrays of objects.

Later, a single object is retrieved using indices, specifically:

var v1 = vertices[ y ][ x + 1 ];

Then, just below this, it appears the object is again referenced but by this syntax:

var n1 = this.vertices[ v1 ].clone().normalize();

Try as I may, this seems like a bug to me .. wouldn't this.vertices[v1] return undefined?

2

2 Answers

3
votes

I think what makes this confusing is this.vertices vs vertices. They are in fact two different structures.

// first loop
for (...) {
    /* ... */

    // this.verticies will have every vertex
    this.vertices.push( vertex );

    verticesRow.push( this.vertices.length - 1 );
    /* ... */
}

// notice we pushed a row of vertices to `vertices` not `this.verticies`
vertices.push( verticesRow );


// second loop
for (...)  for (...) {
    // grab the vertex from the local list
    var v1 = vertices[ y ][ x + 1 ];

    // use it to grab something from the object's list
    var n1 = this.vertices[ v1 ].clone().normalize();
}
0
votes

I don't know about that specific example, but here is a contrived data structure that would be valid, and not necessarily an error.

x = 1
y = 2

vertices = ["","",["","x",3],"the third element"]

var v1 = vertices[y][x+1] // v1 is `3`

var n1 = this.vertices[v1]

alert(n1)
// alerts `the third element`