Coder Social home page Coder Social logo

cymesh's Introduction

CyMesh

A Half-Edge mesh implemented in cython. The mesh is intended for dynamic manipulation. Splitting edges, fliping edges and calculating normals and curvatures are all supported.

Currently only supports tri-meshes.

TODO:

  • support n-gon meshes and have a tri-mesh subclass

INSTALL

Requires cython and pygame/pyopengl for viewing.

git clone https://github.com/Sloth6/cymesh
cd cymesh
python setup.py build_ext --inplace
python demo.py

To install locally.

python setup.py install

Demo

Here is all the code in demo.py for creating a mesh from an .obj file and viewing it. Mesh's can also be created from a list of polygons.

from random import random
from cymesh.mesh import Mesh
from cymesh.view import Viewer

mesh = Mesh.from_obj('triangulated_sphere_2.obj')

# Add noise to mesh.
for vert in mesh.verts:
    vert.p[0] += random() * .1
    vert.p[1] += random() * .1
    vert.p[2] += random() * .1

# If not given a max length, all edges are split.
mesh.splitEdges()

# Flip edges which will become shorter.
mesh.shortenEdges()

# Normals and curvature updates must be called after making changes to mesh.
mesh.calculateNormals()
mesh.calculateCurvature()

# We can write our new object to a file.
mesh.writeObj('my_mesh.obj')

# We can also export the mesh to a dict of numpy arrays.
export = mesh.export()
print(export.keys())
# > dict_keys(['faces', 'vertice_normals', 'face_normals', 'vert_data', 'vertices', 'curvature', 'edges'])

# Each vert has a dictionary 'data' attribute to add additional information.
# Colors can be passed to the viewer by adding a color to data.
for vert in mesh.verts:
    vert.data['color'] = (random(), random(), random())

# View the mesh with pyopengl.
view = Viewer()
view.startDraw()
view.drawMesh(mesh, edges=True)
view.endDraw()
view.mainLoop()

Interface

Mesh

Attributes:
    readonly list verts
    readonly list faces
    readonly list edges
    readonly list halfs

Methods:
    void shortenEdges()
    int splitEdges(double max_edge_length=0.0)
    double volume()
    void calculateNormals()
    void calculateCurvature()
    list getNearby(Vert v, int n)
    Vert splitEdge(Edge e)
    tuple boundingBox(self)

Vert

Attributes:
	readonly unsigned int id
	public double p[3]
	readonly double normal[3]
	readonly double curvature
	readonly HalfEdge he
	public dict data

Methods:
	list faces()
	list neighbors()

Edge

Attributes:
    readonly unsigned int id

Methods:
    double length()
    tuple vertices()
    bint isBoundary()
    void flip()

Face

Attributes:
    readonly unsigned int id
    readonly double normal[3]

Methods:
    list vertices()
    list edges()

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.