GithubHelp home page GithubHelp logo

sondro / convhull_3d Goto Github PK

View Code? Open in Web Editor NEW

This project forked from leomccormack/convhull_3d

0.0 1.0 0.0 21.65 MB

A header-only C implementation of the Quickhull algorithm for building N-dimensional Convex Hulls and Delaunay meshes

License: MIT License

C 99.25% MATLAB 0.75%

convhull_3d's Introduction

convhull_3d

A header only C implementation of the 3-D Quickhull algorithm for building Convex Hulls. The code is also compliant with MSVC and C++ compilers.

New! As of 28.05.2021: Added support for building N-dimensional Convex hulls and Delaunay meshes.

Getting Started

To use this 3-D Convex Hull implementation in a '.c' or '.cpp' file, first add the following:

#define CONVHULL_3D_ENABLE
#include "convhull_3d.h"

Then specify the vertices, which can be optionally extracted from an '.obj' file using the following code:

ch_vertex* vertices = NULL;
int nVertices;
extractVerticesFromObjFile(OBJ_FILE_NAME, &vertices, &nVertices);
/* Where 'vertices' is a vector of vertices [nVertices].x, .y, .z  */

where 'OBJ_FILE_NAME' is the '.obj' file path (without the extension).

Or they may be defined manually, for example: a random distribution of points on the unit sphere:

int n = 936;
ch_vertex* vertices;
vertices = (ch_vertex*)malloc(n*sizeof(ch_vertex));
for (i = 0; i < n; i++) {
    float elev = rand()/(float)RAND_MAX * M_PI * 2.0;
    float azi = rand()/(float)RAND_MAX * M_PI * 2.0;
    vertices[i].x = cos(azi) * cos(elev) * rand()/(float)RAND_MAX;
    vertices[i].y = sin(azi) * cos(elev) * rand()/(float)RAND_MAX;
    vertices[i].z = sin(elev);
}

The Convex Hull may then be built and subsequently exported (including face normals) as an '.obj' file, using this code:

int* faceIndices = NULL;
int nFaces;
convhull_3d_build(vertices, nVertices, &faceIndices, &nFaces);
/* Where 'faceIndices' is a flat 2D matrix [nFaces x 3] */
convhull_3d_export_obj(vertices, nVertices, faceIndices, nFaces, 1, OUTPUT_OBJ_FILE_NAME);
free(vertices);
free(faceIndices);

where 'OUTPUT_OBJ_FILE_NAME' is the output '.obj' file path (without the extension).

Additional options

By default, the implementation uses double floating point precision to build the hull, while still exporting the results in single floating point precision. However, one may configure convhull_3d to use single precision to build the hull (which is less accurate and reliable, but quicker) by adding the following:

#define CONVHULL_3D_USE_SINGLE_PRECISION /* (optional) */
#define CONVHULL_3D_ENABLE
#include "convhull_3d.h"

If your project has CBLAS linked, then you may also speed up the matrix multiplications by adding:

#define CONVHULL_3D_USE_CBLAS /* (optional) */
#define CONVHULL_3D_USE_SINGLE_PRECISION /* (optional) */
#define CONVHULL_3D_ENABLE
#include "convhull_3d.h"

Test

This repository contains files: 'test/test_convhull_3d.c' and 'test/test_script.m'. The former can be used to generate Convex Hulls of the '.obj' files located in the 'test/obj_files' folder, which can be subsequently verified in MatLab using the latter file; where the 'convhull_3d.h' implementation is compared with MatLab's built-in 'convhull' function, side-by-side. Furthermore, Visual Studio 2017 and Xcode project files have been included in the 'test' folder for convenience.

Examples

The 'test/test_convhull_3d.c' file may also serve as example usage of the convhull_3d implementation. The following images are of the original 'obj' files (left) and the corresponding Convex Hulls (right), depicted using Tim Maxwell's OBJ Viewer:

Convex Hulls of uniformly distributed points on a sphere (180, 840, 5100 points left-to-right):

License

The code is distributed under the MIT license, but contains code that was originally written for MatLab by George Papazafeiropoulos (c) 2014; which was distributed under the BSD (2-clause) license and can be found here.

Contact

If you have any questions, or encounter any bugs, please email: [email protected]

convhull_3d's People

Contributors

leomccormack 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.