Interpreting binary decimal data using JavaScript

The Challenge

Received data from an API contains binary information about colored points in 3D space. The goal is to decode these points and utilize them in a buffergeometry within Three.js.

Working with binary formatted data is proving to be beyond my current expertise, so I am seeking assistance to tackle this task.


Description of the binary data obtained from the API:

The binary data commences with a header structured as follows:

HTTP/1.1 200 OK
Content-Length: 13632 // varies based on query

Following the header is a blank line, succeeded by the representation of points in this layout:

Every 32 bytes:

  • X: double (8 byte)
  • Y: double (8 byte)
  • Z: double (8 byte)
  • R: unsigned char (1 byte)
  • G: unsigned char (1 byte)
  • B: unsigned char (1 byte)
  • 5 placeholders to fill remaining space

My Progress So Far:

Assuming that the binaryPoints variable holds the raw binary data fetched from the API. Initial steps involve adding the data to an arrayBuffer sized at 32 bytes per point, followed by creating a DataView object for manipulation.

// create a 32-byte arrayBuffer (each point occupies 32 bytes)
const buffer = await binaryPoints.arrayBuffer(32);

// establish a dataview of the buffer for reading the points
const data = new DataView(buffer);

Currently, I am considering incorporating an offset to account for the header size and aiming to extract the number of points from the header.

// assigning temporary values
const numPoints = 1000;
const offsetFromHeader = 100; // bits

Based on examples and references, it seems like iterating through the buffer 32 bytes at a time will facilitate extraction of xyzrgb values:

// increment offset by 256 each iteration (32*8)
for (var i=0, offset=offsetFromHeader; i<numPoints; i++, offset += 256) {
    // Extract xyz values using Float64Array (double)
    const xFloat64View = data.getFloat64Array(offset);
    const yFloat64View = data.getFloat64Array(offset + 64);
    const zFloat64View = data.getFloat64Array(offset + 128);

    // Fetch rgb values using Uint8Array (1 byte unsigned int)
    const rUint8View = data.getUint8Array(offset + 192);
    const gUint8View = data.getUint8Array(offset + 200);
    const bUint8View = data.getUint8Array(offset + 208);

    // Ignore excess bits 
}

Despite reaching this point, I'm uncertain about how to proceed further with this information. It seems existing samples focus on single-type data rather than the multi-dimensional nature seen here.

Should I construct a plain javascript array of points and input values accordingly?

// pseudocode
var points = [];

// inside loop
points[i].x = data.getFloat64Array(offset);

This approach might not be suitable as eventual integration into Three.js bufferGeometries aims to eliminate additional overhead associated with maintaining separate JS arrays.


Next Objective: Integrating Points into Three.js bufferGeometry

The rendered pointcloud in Three.js bufferGeometry relies on the outcome desired from the previous phases.

A potential implementation could resemble:

const bufferGeometry = new THREE.BufferGeometry();


let vertices = new Float32Array(); // xyz
let colors = new Float32Array(); // rgb

bufferGeometry.addAttribute('position', new THREE.BufferAttribute(vertices, 3));
bufferGeometry.addAttribute('color', new THREE.BufferAttribute(colors, 3));

const points = new THREE.Points(bufferGeometry, material);
scene.add(points);

Although progress remains tentative, detailing each step aids in refining my thought process amid this challenging endeavor.

Answer №1

Looks like you have a double gUint8View issue where the second one should be bUint8View.

For this situation, consider using the DataView api instead...

var x = view.getFloat64(0); 
var y = view.getFloat64(8); 
var z = view.getFloat64(16);
var r = view.getUInt8(24); 
var g = view.getUInt8(25); 
var v = view.getUInt8(26); 

Also, could there be confusion between "100 bit" and "100 byte" header? Starting at a non-8-bit boundary might complicate parsing significantly.

The THREE part of your code seems to be mostly accurate.

You're making progress! I recently worked on a similar project involving parsing the HYG star catalog for creating a point cloud of stars...

Keep up the good work!

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

Seams between polygons in the Three.js canvas renderer are noticeable

Although I've become comfortable with using this library, there's one task that has left me feeling exhausted: When attempting to display a textureless mesh exported from Blender as .obj (with triangulation and smoothing groups enabled), the Web ...

Sending image id as a parameter to a MySQL query in PHP using jQuery

Having trouble passing image ids to mysql queries. I've tried the following code but keep getting an Undefined index x error. I've checked multiple Stack Overflow answers without success. echo "<img src=http://localhost/ss/img/".$p['pat ...

Changing the host in every URL within a string using node.js

I am working with a string that contains URLs with IP addresses: { "auth" : { "login" : "http://123.123.11.22:85/auth/signin", "resetpass" : "http://123.123.22.33:85/auth/resetpass", "profile" : "http://123.123.33.44:85/auth/profile" ...

Ways to determine the total number of pages in a PDF using a stream

Utilizing a library such as pdf-parse enables us to extract the number of pages (numpages) from a PDF buffer. The challenge I am facing is handling large buffers that cannot be stored in memory, prompting me to consider streaming it instead: const response ...

Troubleshooting issue with JQuery AJAX loading Bootstrap Dropdowns

I have a website on GitHub Pages that uses Bootstrap, but I'm having issues with the drop-down menu in the navbar. Sometimes it works and sometimes it doesn't. I am using $("#nav").load("url"); to load the navbar so that I can make changes and ha ...

Subdomain for an Ajax request

Can we use ajax requests to extract data from and fetch information from pages like ? It appears that JavaScript still does not permit subdomain requests. ...

Connecting AngularFirebaseAuth: Running server API immediately following Firebase authentication?

My authentication process relies on two key factors: Using Firebase auth (email and password) Making a server API call to retrieve the complete customer entity from the database based on the firebaseID. The user must exist in both places for successful a ...

Having trouble displaying values from nested JSON in a datatable

Response from server : ["{\"CLIENT\":[{\"tranche\":\"1-4\",\"prix\":\"65.96\",\"currency\":\"E\"}],\"DISTRIBUTEUR\":[{\"tranche\":\"1-4\",\"prix\ ...

unique navbar color transition effect specifically for the homepage

I have successfully created a function called changeColour in my code that changes the color of the navbar when scrolling past a certain point. However, I am encountering an issue where I only want this effect to be applied on the Home page. I would like a ...

I'm having trouble with my carousel. I suspect it's the "link-rel" in the head tag that's causing the issue

Having trouble with my carousel - it's not navigating properly. When I check the console, it shows: Failed to find a valid digest in the 'integrity' attribute for resource '' with computed SHA-256 integrity 'YLGeXaa ...

Nested ng-repeat in AngularJS by numeric value

Looking to create a sliding carousel layout for displaying a bunch of data with 8 points per slide. The desired structure is as follows: Slide datapoint 1 datapoint 2 datapoint 3 datapoint 4 datapoint 5 datapoint 6 datapoint 7 ...

Successive Node.js Post Requests

Is it possible to make consecutive POST requests in separate route files and redirect to a different EJS page with each request? Here is my file structure: Routes: index.js - users.js - users1.js Views: index.ejs - users.ejs - users1.ejs This is my ...

Addon for Three.js in Blender not appearing on Addons screen for Windows 7 and Windows Vista users

Is there anyone who has successfully been able to make the three.js import export addon in Blender work on either Windows 7 or Windows Vista? I have attempted this on two different computers. For Windows 7, I placed the files in the following directory ( ...

What are some strategies for establishing communication between sibling components in Vue?

Currently, my Vue app has a structure that includes a "blackout" component for displaying modals and a router-view for various sub-menus. These components are siblings at the same level. <blackout v-if="this.popup.currentPopup"></blacko ...

Is there a way to adjust the quantity of items in the shopping cart without the need to reload the webpage?

Currently, I am working on a test project using React & Redux. The project involves implementing a basket feature where I receive an array of products structured like this: products: [ { name: "Product one", count: 1, ...

Basic application - angular has not been declared

I'm currently diving into the realm of javascript and angularjs, following along with the tutorials on . After setting up a basic IntelliJ javascript project, I created two essential files: index.html <!DOCTYPE html> <html lang="en"> ...

Tips for extracting only a portion of the JavaScript timestamp

I have a JavaScript timestamp that reads Tue Sep 30 2014 12:02:50 GMT-0400 (EDT). When I use the .getTime() method, I get 1412092970.768 Typically, this timestamp represents a specific time of today. My question is whether it's possible to extract o ...

What is the process of incorporating a video into a react.js project through the use of the HTML

I'm experiencing an issue where my video player loads, but the video itself does not. Can anyone shed light on why this might be happening? class App extends Component { render() { return ( <div className="App& ...

Check if a specific number appears exactly once in an array and output either True or False

I am facing a challenge with comparing two arrays Array1 = [1,1,1,2,2,2,3,3] Array2 =[1,1,2,1] When comparing both arrays, the desired result is True if the number of occurrences of Integer 1 are the same. Array2 = [1,1,2] //Expecting False For the ab ...

What sets apart 'hasClass' from 'is'? Additionally, is there a substitute to retrieve a Jquery element instead?

Similar Question: jQuery .hasClass() vs .is() Sample HTML code: <div class="results"> <div class="table"> <form class="tr" action="#" method="post"> <div class="td"> <input class="dat ...