Giter VIP home page Giter VIP logo

marching-cubes's Introduction

Marching Cubes

Overview

Python OpenGL program to generate triangle meshes of various scalar fields using the marching cubes algorithm.

Requirements

The program requires pyopengl, glfw, pyglm, and numpy. For help setting up a new Python environment and downloading the packages, check out my beginner PyOpenGL tutorial, Hello Triangle!

Using the Program

The program is divided into two modules to optimize performance:

marching_cubes.py

Generates triangle meshes and saves the data to a PLY file

render.py

Opens the triangle mesh data from a PLY file and renders the result

Generating Triangle Meshes

Triangle meshes are generated with the marching cubes algorithm. To generate a triangle mesh of a scalar field, execute

python marching_cubes.py filename scalar_field isovalue volume_min volume_max
stepsize

Parameters

filename
Filename of the PLY file to write triangle mesh data to.
scalar_field
Name of the scalar field defining the object to generate a triangle mesh for.
Options:
 wavy : Scalar field f(x, y, z) = y − sin (x) ⋅ cos (z)
 hyperboloid : Scalar field f(x, y, z) = x2 − y2 − z2 − z
isovalue
Boundary value used to define which points are considered to be inside of the object.
  If f(x, y, z) < isovalue, then the point (x, y, z) is considered to be inside of the object. If f(x, y, z) > isovalue, then the point (x, y, z) is considered to be outside of the object.
volume_min
Minimum value of x, y, and z to use in the algorithm.
volume_max
Maximum value of x, y, and z to use in the algorithm.
stepsize
Side length of each cube in the algorithm. A smaller stepsize will result in more vertices in the triangle mesh.

Rendering Triangle Meshes

To render a saved triangle mesh, execute

python render.py filename volume_min volume_max colour

Parameters

filename
Filepath to the PLY file containing the triangle mesh's attributes.
volume_min
Minimum value of x, y, and z in the marching volume. For best results, this should match the value used to generate the triangle mesh.
volume_max
Maximum value of x, y, and z in the marching volume. For best results, this should match the value used to generate the triangle mesh.
colour
Name of the object's base colour. Used as the diffuse component of the lighting model.
Options: turquoise, lime, orange, purple, fuchsia
 If not specified, turquoise is used by default.

To render the object, the program uses a Phong-like shader which combines the the ambient, diffuse, and specular components of light to compute the object's colour. A box is drawn around the marching volume, with the x, y, and z axes indicated in red, green, and blue respectively. Once the scene is rendered, the cube can be rotated in place by clicking and dragging the mouse. The camera can be zoomed in and out using the up and down arrow keys respectively.

Examples

Example PLY files that can be used to experiment with the program have been provided in the directory Example-Files. The parameters used to generate each triangle mesh are included as a comment in the file. The following images show each triangle mesh rendered. The code to generate and render each triangle mesh is included in the caption.

Rendered example of the wavy scalar field

Texture mesh generated of the wavy scalar field.
 Generated using: python marching_cubes.py Example-Files/wavy.ply wavy 0 -5 5 0.05
 Rendered using: python render.py Example-Files/wavy.ply -5 5 turquoise

marching-cubes's People

Contributors

joelenehales avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.