0

Sorry for the lengthy code but I didn't manage to make a minimal example reproducing the problem. Each cell of the two arrays returned by the following function contains a number, as expected:

function VNtorus(R, r, nx, ny) {
    var Vertices = new Array(nx);
    var Normals = new Array(nx);
    for (var i = 0; i < nx; i++) {
        Vertices[i] = new Array(ny);
        Normals[i] = new Array(ny);
        var u = i / nx * 2 * Math.PI;
        var cos_u = Math.cos(u);
        var sin_u = Math.sin(u);
        var cx = R * cos_u;
        var cy = R * sin_u;
        for (var j = 0; j < ny; j++) {
            var v = j / ny * 2 * Math.PI;
            var rcos_v = r * Math.cos(v);
            var rsin_v = r * Math.sin(v);
            Vertices[i][j] = new THREE.Vector3(
                cx + rcos_v * cos_u,
                cy + rcos_v * sin_u,
                rsin_v
            );
            Normals[i][j] = new THREE.Vector3(
                rcos_v * cos_u,
                rcos_v * sin_u,
                rsin_v
            );
        }
    }
    return {
        vertices : Vertices,
        normals : Normals
    }
}

If you want, the context of this code is here: Cannot render a mesh with THREE js

However, when I call this function as below, the first array is empty, for the two resulting arrays of arrays:

var a = 3; var c = 0.7; var mu = 1.7;
var b = Math.sqrt(a * a - c * c);
var bb = b * Math.sqrt(mu * mu - c * c);
var omega = (a * mu + bb) / c;
var Omega = new THREE.Vector3(omega, 0, 0)
var inversion = function (M) {
    var OmegaM = Omega.sub(M);
    var k = OmegaM.dot(OmegaM);
    return Omega.addScaledVector(OmegaM, k);
}
var d = (a - c) * (mu - c) + bb;
var r = c * c * (mu - c) / ((a + c) * (mu - c) + bb) / d;
var R = c * c * (a - c) / ((a - c) * (mu + c) + bb) / d;
var omegaT = omega - (b * b * (omega - c)) /
    ((a - c) * (mu + omega) - b * b) / ((a + c) * (omega - c) + b * b);
var tmesh = VNtorus(10, 3, 64, 32);
var tvertices = tmesh.vertices;
var tnormals = tmesh.normals;
console.log(tvertices)
// ...

0: Array(32)
length: 32
__proto__: Array(0)
1: Array(32)
0: p {x: 0.008700136926854488, y: 0.0008568886953253847, z: 0}
1: p {x: 0.008532966235351477, y: 0.0008404238193190658, z: 0.0017055250840917766}
....

I'm really lost here. I am not expert in Javascript and I possibly make a novice error somewhere.

2
  • the same code prints array of 64 items for me. and Each Item contains 32 Objects of Vector3('x':somevalue,'y':somevalue,'z':'somevalue') type. Commented Nov 1, 2018 at 5:06
  • E.g. if I try to console tvertices[63][31] , it prints Vector3 {x: 12.880034860328578, y: -1.2685727086829575, z: -0.5852709660483861} Commented Nov 1, 2018 at 5:08

1 Answer 1

0

I have created a code snippet for you using jquery.mon.js and three.js and on button click I am calling your code and it is producing the array with 64 elements for me.

You might want to verify your three.js CDN.

(64) [Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32), Array(32)]

function myFunction(elmnt, clr) {
            var a = 3; var c = 0.7; var mu = 1.7;
            var b = Math.sqrt(a * a - c * c);
            var bb = b * Math.sqrt(mu * mu - c * c);
            var omega = (a * mu + bb) / c;
            var Omega = new THREE.Vector3(omega, 0, 0)
            var inversion = function (M) {
                var OmegaM = Omega.sub(M);
                var k = OmegaM.dot(OmegaM);
                return Omega.addScaledVector(OmegaM, k);
            }
            var d = (a - c) * (mu - c) + bb;
            var r = c * c * (mu - c) / ((a + c) * (mu - c) + bb) / d;
            var R = c * c * (a - c) / ((a - c) * (mu + c) + bb) / d;
            var omegaT = omega - (b * b * (omega - c)) /
                ((a - c) * (mu + omega) - b * b) / ((a + c) * (omega - c) + b * b);
            var tmesh = VNtorus(10, 3, 64, 32);
            var tvertices = tmesh.vertices;
            var tnormals = tmesh.normals;
            console.log(tvertices)
        }

        function VNtorus(R, r, nx, ny) {
            var Vertices = new Array(nx);
            var Normals = new Array(nx);
            for (var i = 0; i < nx; i++) {
                Vertices[i] = new Array(ny);
                Normals[i] = new Array(ny);
                var u = i / nx * 2 * Math.PI;
                var cos_u = Math.cos(u);
                var sin_u = Math.sin(u);
                var cx = R * cos_u;
                var cy = R * sin_u;
                for (var j = 0; j < ny; j++) {
                    var v = j / ny * 2 * Math.PI;
                    var rcos_v = r * Math.cos(v);
                    var rsin_v = r * Math.sin(v);
                    Vertices[i][j] = new THREE.Vector3(
                        cx + rcos_v * cos_u,
                        cy + rcos_v * sin_u,
                        rsin_v
                    );
                    Normals[i][j] = new THREE.Vector3(
                        rcos_v * cos_u,
                        rcos_v * sin_u,
                        rsin_v
                    );
                }
            }
            return {
                vertices: Vertices,
                normals: Normals
            }
        }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
 <script src="https://threejs.org/build/three.js"></script>
 <button onclick="myFunction()">Click me</button </body> </body> </html>

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. I've tried with your version of three js and that still does'nt work. I'm going to restart the computer.
can you try console.log(tvertices.length) , once your computer is up :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.