Exploring Quadrics with Marching Cubes in Three.js

I have been attempting to create an applet that displays various types of space quadrics using the Marching Cubes library from three.js to render implicit surfaces. However, the shapes that are being generated do not appear as expected, leading me to believe that I may not be utilizing the properties of THREE.MarchingCubes correctly. The main part of the applet includes the following code snippet:

        quadricData = {
            a11: 1,
            a22: 1,
            a33: 1,
            a12: 0,
            a13: 0,
            a23: 0,
            a1: 0,
            a2: 0,
            a3: 0,
            a: -1
        };

        quadValue = function (data, x, y, z) {
            return data.a11*x*x+
                   data.a22*y*y+
                   data.a33*z*z+
                   data.a12*2*x*y+
                   data.a13*2*x*z+
                   data.a23*2*y*z+
                   data.a1*x+
                   data.a2*y+
                   data.a3*z+
                   data.a;
        }

        var res = 50;
        var material = new THREE.MeshPhongMaterial( {
            color: '#1565C0', // blue 800
            transparent: true,
            opacity: 0.8,
            side: THREE.DoubleSide,
            flatShading: true
        } );

        quadric = new THREE.MarchingCubes( res, material );
        quadric.isolation = 0;

        for ( var k = 0 ; k < res ; k++ ) {
            for ( var j = 0 ; j < res ; j++ ) {
                for ( var i = 0 ; i < res ; i++ ) {

                var x = 8*(i-res/2)/res;
                var y = 8*(j-res/2)/res;
                var z = 8*(k-res/2)/res;

                quadric.field[ i + j*res + k*res*res ] = quadValue(quadricData,x,y,z);

                }
            }
        }

For the complete code, you can visit here. It seems that the 'field' attribute needs to be populated with values from a function, but this may not be the correct approach. Odd effects occur, like an ellipsoid expanding when a11 is increased instead of shrinking. The 'marching cubes grid' also appears to expand, which is quite amusing.

https://i.sstatic.net/y1pja.gif

Several other configurations do not produce the expected results. What is the proper way to utilize THREE.MarchingCubes?

Answer №1

To implement THREE.MarchingCubes:

  • Firstly, import MarchingCubes.js using

    <script src="https://threejs.org/examples/js/MarchingCubes.js"></script>

  • Create the volume with resolution R, which will result in a voxel cube with dimensions R x R x R, by using

    V = new THREE.MarchingCubes( R, material )

  • Assign the voxel values, which form a linear array of float values. The formula to find the index for a voxel at position [X,Y,Z] is (X + (Y * R) + ( Z * R * R)). Set the values using V.field[ i + j*R + k*R*R ] = f

  • Define the isolation value. The volume displayed will consist of all voxels less than this value, essentially known as the iso-surface value. Define the value by using V.isolation = s.

It appears that your code is functioning correctly. Increasing a11 does indeed decrease the volume in the x dimension as expected.

https://i.sstatic.net/moSg2.gif

The only discrepancy I noticed is the unnecessary multiplication of a12, a13, and a23 by 2. If a quadric equation is represented as:

Ax^2 + By^2 + Cz^2 + Dxy + Exz + Fyz + Gz + Hy + Iz + J = 0

And the calculation is:

data.a11*x*x + data.a22*y*y + data.a33*z*z + data.a12*2*x*y + data.a13*2*x*z + data.a23*2*y*z + data.a1*x + data.a2*y + data.a3*z + data.a
, then it seems redundant to multiply D, E, and F by two?

Have you resolved any bugs since posting this question? Can you provide more specific details about the issue?

Bug report: There is a bug report number 14060 filed against three.js here. As of May 2018, with three.js version r92, the Marching Cubes library seems to be functioning correctly in (1) Firefox on Windows and MacOS, and (2) Safari on MacOS. However, it shows inconsistencies in Firefox on Linux and Chrome on any platform. Disabling flat shading might help alleviate the problem.

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

What is the best way to transfer functions connected to an Object over to Object.prototype?

Imagine having this: var exampleObject = {age: 25, name: 'John'}; If you do this: Object.keys(exampleObject); // it will return ['age', 'name'] Now, what if you want to add this functionality to the object prototype? You c ...

The load event in React's iframe is failing to fire after the src attribute is modified using state

In the process of creating a registration form for a React application, we encountered the need to incorporate an HTML legal agreement as an iframe. This legal document is available in various languages, one of which can be selected using a drop-down menu; ...

Encountering challenges with CORS implementation in a test application

I am struggling with an issue while trying to send a request to a website for ping. I have tried using jsonp, but it's not working as expected. I attempted to implement cors, but I keep getting the error message "No 'Access-Control-Allow-Origin&a ...

Transferring information between two components in separate Angular 4 modules

Within my application, I have defined two modules named AppModule and UserModule. I am currently encountering an issue with data sharing between the AppComponent and the LoginComponent (which belongs to the UserModule). Below is a snippet of app.componen ...

Creating a hollow cylinder in Three.js - Tips and Tricks

I'm facing some challenges building a hollow cylinder in my Three.js project. Do you think it's better to create it using CSG or by connecting the vertices together? ...

What is the significance of the any type in Typescript?

As I delve into learning Typescript, a question arises in my mind. I find myself pondering the purpose of the any type. It seems redundant to specify it when every variable essentially acts as an "any" type by default. Consider this scenario where the out ...

jQuery load fails to fill select dropdown

Upon page load, my code executes a call to mysql to populate a select element with data. This process works smoothly on the initial load or when the page is refreshed. However, I am encountering an issue when attempting to update or repopulate the select e ...

Reorganizing asynchronous code into a nested synchronous structure in Meteor/React

Recently, I've been experimenting with running data fetching code in a container component and passing it to the display component in order to streamline my use of hooks and reduce load time. I've tested both await/sync and Meteor's wrapAsyn ...

Retrieving a specific Project ID from Asana Task API using Node.js and parsing the JSON response

Utilizing the Asana Task API, we have the capability to retrieve a task's associated projects along with their GID and Notes (description text). The main objective Our aim is to extract the GID of the project containing #websiteprojecttemplate in its ...

Running cy.task after all test suites can be done by adding the task in a

I need some guidance on running cy.task after executing all test suites. I have a file generated at the start of the tests that I would like to remove once they are completed. Regardless of whether any tests passed or failed, I want to trigger cy.task im ...

Does the image alter based on the selection in the dropdown menu?

When I utilize the Templating Example from the select2 library, everything seems to work for the first dropdown value change. The correct image is previewed as expected. However, when trying to change the value a second time, a second image is appended but ...

How can I turn off Angular Grid's virtualization feature, where Angular generates div elements for the grid based on height and width positions?

Currently, I am working with an Angular grid (ag-grid) that dynamically creates div elements in the DOM to display data as the user scrolls or views different sections. As part of my testing process using Selenium WebDriver, I need to retrieve this data fr ...

How can I pass the value of a variable from one Vue.js 2 component to another?

Here is how I have structured my view: <div class="row"> <div class="col-md-3"> <search-filter-view ...></search-filter-view> </div> <div class="col-md-9"> <search-result-view ...></ ...

Unable to append item to document object model

Within my component, I have a snippet of code: isLoaded($event) { console.log($event); this.visible = $event; console.log(this.visible); this.onClick(); } onClick() { this.listImage = this.imageService.getImage(); let span = docu ...

What is the process for obtaining a compilation of JavaScript files that are run when an event is triggered?

How can I generate a list of all the JavaScript files that are triggered when a button is clicked or an input field is selected in a complex system? Is it possible to achieve this with Chrome DevTools, or are there alternative solutions available? If Chro ...

Providing real-time results as numbers are added together

I need assistance in calculating a price inclusive of VAT based on a user-entered VAT rate. Is it possible to show the result in an input box dynamically as the user inputs the VAT rate and price, without requiring them to click a button or resubmit the fo ...

Creating a callback in C code with Emscripten for JavaScript integration

In this challenge, the goal is to incorporate a JavaScript function as a callback to display progress during a while-loop operation. For example: var my_js_fn = function(curstate, maxstate){//int variables console.log(curstate.toString() + " of " + maxsta ...

Creating a custom inline style button in Froala Editor

As a UX Designer with experience in frontend development, I am currently working on an online editor proof of concept for a usability study. One of the key findings so far is that users are requesting a button that can automatically apply the corporate fon ...

CSS transition fails to revert

My standard opacity animation is not working in reverse order. Here is a link to the JSFiddle example. According to the documentation, it should work automatically. Since I am new to JavaScript, I am unsure if this issue lies in my code or if the CSS anima ...

PHP fails to return response to jQuery AJAX request

I am encountering issues with my jQuery AJAX requests in PHP. What could be wrong with my simple example? index.php - includes loading JS, PHP files, and defining a button and a paragraph. <html> <head> <script src='jquery ...