0

I have an array of x/y/z positions from a BufferGeometry, there are nearly 60000 points (18000 values),

[3, 2, 1, 3, 2, 1, 3, 2, 1, ...]

I need to shuffle those positions and then get the 30000 first to get random points. I am thinking of converting this array to an array of Vector 3 before shuffling to not lose all the "trio" values.

    [new THREE.Vector3(3, 2, 1), new THREE.Vector3(3, 2, 1), new THREE.Vector3(3, 2, 1), ...]

I need vocabulary help. Is there specific names for those two arrays please ? (that would help me found out about the next questions).

Is there specific methods to convert an array to another ?

And is there a best way to shuffle the raw positions array ?

WestLangley
  • 102,557
  • 10
  • 276
  • 276
Nephelococcygia
  • 726
  • 5
  • 11
  • Regarding the shuffle: https://stackoverflow.com/questions/6274339/how-can-i-shuffle-an-array . About the "conversion": what are you exactly talking about? It's quite hard to understand what you want to do to me. You likely don't want to convert an array, you just want to **elaborate** it to get something out of it, either a new array or whatever. – briosheje Aug 21 '17 at 16:37
  • **I need to shuffle those positions and then get the 30000 first to get random points.** please elaborate, i dont understand how random points fit here – pailhead Aug 21 '17 at 18:13

1 Answers1

1

The short answer is: you don't need to convert to THREE.Vector3 in order to extract n random points from the array. There is no specific name for both arrays.

Below I provide the functions to execute the operations you would like to perform:

And is there a best way to shuffle the raw positions array ? (yes, see below)

var points = [5, 6, 7, 3, 2, 1, 5, 6, 7, 3, 2, 1, 5, 6, 7]

// Assuming there are more points than the number of points you need to get back
function getRandomPoints (points, numberOfPoints) {
  var resultPoints = []
  for (var i = 0; i < numberOfPoints; i++) {
    // Get a random index
    var randomIndex = Math.floor(Math.random() * points.length);
    var index = (randomIndex - randomIndex % 3)
    resultPoints.push(points[index])
    resultPoints.push(points[index + 1])
    resultPoints.push(points[index + 2])
    points.splice(index, index + 3);
  }

  return resultPoints;
}

var randomPoints = getRandomPoints(points, 2);

Is there specific methods to convert an array to another ? (yes, see below)

var points = [5, 6, 7, 3, 2, 1, 5, 6, 7, 3, 2, 1, 5, 6, 7]

function convertToVector (points) {
  var resultPoints = []
  for (var i = 0; i < points.length; i = i + 3) {
    var vectorPoint = new THREE.Vector3(points[i], points[i + 1], points[i + 2])
    resultPoints.push(vectorPoint)
  }
  return resultPoints
}

var convertedPoints = convertToVector(points)
Alex P.
  • 551
  • 1
  • 5
  • 21