Arranging array positions in ThreeJS

My BufferGeometry contains an array of x/y/z positions with almost 60,000 points (18,000 values),

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

To obtain random points, I am considering shuffling these positions and then selecting the first 30,000. One idea is to first convert this array into an array of Vector 3 to retain the trio values.

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

I could use some help with terminology. Are there specific names for these two arrays? This information will assist me in tackling subsequent questions.

Are there established methods for converting one array into another?

Furthermore, what is the most efficient way to shuffle the original positions array?

Answer №1

In summary: No conversion to THREE.Vector3 is necessary to extract n random points from the array. There are no specific names assigned to the arrays.

Below, you will find the functions needed to carry out the requested operations:

Is there an optimal method to shuffle the raw positions array? (Yes, details below)

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

// Assuming more points exist than required for extraction
function getRandomPoints (points, numberOfPoints) {
  var resultPoints = []
  for (var i = 0; i < numberOfPoints; i++) {
    // Generate 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);

Are there specific techniques for converting one array to another? (Yes, explained 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)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Using a two-dimensional C array with Objective-C objects: a beginner's guide

I am working with a two-dimensional C array of Objective-C objects and I need to figure out how to access the members of those objects. Specifically, I have a Ball object stored in array[0][0] that I need to access the "size" member of. id array[5][5]; a ...

Reasons for dividing by 1 in JavaScript

While completing a basic programming assignment from my teacher, I encountered an interesting issue in Javascript. I found that when dividing a number by 1, it returns an unexpected value. Can anyone provide an explanation for this? I have created a jsfidd ...

Firebase (web) deploy encounters an issue due to stripe integration

I recently integrated Stripe into my Firebase function index.js: const stripe = require('stripe')('key'); and created a function for checkout sessions: exports.createCheckoutSession = functions.https.onCall(async(data, context) =&g ...

Error: The `Field` component encountered a failed prop type validation due to an invalid prop `component` after upgrading to MUI version

Encountered an error when migrating to Material ui v4 Failed prop type: Invalid prop component supplied to Field. in Field (created by TextField) This error points to the redux form field component export const TextField = props => ( <Field ...

Troubleshooting Tips: Investigating a Service Call in AngularJS 2 using ES6 Syntax

MY DILEMMA: I have recently started learning Angular2 and found myself wanting to debug a service call. The service appears to have been properly called, as evidenced by the view display. However, when trying to log the content of the variable holding t ...

Obtain the maximum or minimum value from an associative array using a function and provided parameters

Here is the code I have so far: <!DOCTYPE html> <html> <body> <button onclick="scanarray('a', 'max')">Test with a, max</button> <button onclick="scanarray('b', 'min')">Test with ...

Determine the distinct elements in an array using JavaScript/jQuery

I have incorporated Gridster widgets into my webpage, each with a button that turns the widget's color to red when clicked. Upon clicking the button, the parent element is also added to an array. My main goal I aim to have the parent element added t ...

Encountering a Nuxt error where properties of null are being attempted to be read, specifically the 'addEventListener' property. As a result, both the default

Currently, I am utilizing nuxt.js along with vuesax as my UI framework. I made particular adjustments to my default.vue file located in the /layouts directory by incorporating a basic navbar template example from vuesax. Subsequently, I employed @nuxtjs/ ...

What steps should I take to incorporate a timer into my bot similar to the timer used by other giveaway bots

I am looking to add a timer to my bot giveaway embed message that continues to update itself even when the bot is offline, without showing that the message was edited. Here's what I currently have in my embed: const embed = new MessageEmbed(); ...

It appears that the SignalR proxy is not defined

Why is $.connection.connectionhub showing as undefined? I am using webform. <script src="/scripts/jquery-1.6.4.min.js"></script> <!--Reference the SignalR library. --> <script src="/scripts/jquery.signalR-2.2.1.min.js">< ...

Divide the code into individual components within Angular 2 projects

I currently have 3 Angular 2 projects developed in TypeScript. Each project contains the same models and services. I would like to find a way to integrate these common elements at a global level and connect them with each individual project. Any suggesti ...

AngularJS ng-click incompatibility causing non-functioning popover content

I've gone through all the posts regarding this issue, but unfortunately, none of them proved to be helpful. It seems that the jsfiddle and plunker links provided are no longer functioning as expected. My objective is quite simple - I want to place a ...

Using wildcard in Angular app for MQTT observation

My curiosity lies in MQTT wildcards and how they function, specifically while utilizing the mosqitto broker. Let's say I have around 1-2k topics. In my frontend, I am observing them with a single-level wildcard using ngx-mqtt. Will there be a separat ...

Switch up your text display using JQuery and toggle between different texts

I have implemented a jQuery script to toggle the display of certain paragraphs when the "More" link is clicked. However, I am facing an issue where the link always displays "More" even after the content has been expanded. I want it to change to "Less" once ...

Retrieve a collection of JSON files using a single function

The format of JSON links is as follows: www.example.com/link-number-end-of-link.com.json I need to retrieve multiple JSON link files where the only variable changing is the number in the link. For example, it could range from 10 to 40, with the first ...

Having trouble with JavaScript function returning false?

I am trying to call a JavaScript function on an ASP.NET button client click event and want to prevent postback. The function is working, but it is not preventing the page from posting back. Here is my JavaScript code: function User2Check() { v ...

The error code 405 (Method Not Allowed) occurs in Ajax when the action field is empty or identical to the current page

Special thanks to @abc123 for sharing the code below in one of their posts: <!DOCTYPE html> <html> <head> <script src="http://code.jquery.com/jquery-1.9.1.js"></script> </head> <body> <form id="formoid" a ...

Instructions for correctly integrating the ng2-datepicker module into an Angular 2 application

Ng2-datepicker (1.0.6) Angular2 (2.0.0-rc.5) As I attempted to implement it in my HTML following the documentation, I encountered the following error: zone.js:484 Unhandled Promise rejection: Template parse errors: Can't bind to 'ngMode ...

Having difficulty interpreting the responseText

Currently experimenting with Redis Webdis Dart Here's my code snippet #import('dart:html'); #import('dart:json'); class ChatClient { XMLHttpRequest listener; int parsePosition = 0; void connect(){ this.listener = ne ...

Reactjs is having issues with Datatable functions

I am making use of the Datatable Library for creating tables easily. After fetching data with the Fetch API and rendering it to the table, everything seems to work smoothly. However, I am facing issues with DataTable Functions such as sorting, searching, ...