Coder Social home page Coder Social logo

csg's Introduction

Introduction

This is a Constructive Solid Geometry and Quick Hull implemention in golang.

The CSG implementation is based upon https://github.com/jscad/csg.js/

The QuickHull implementation is based upon https://www.cs.ubc.ca/~lloyd/java/quickhull3d.html

You should also investigate SDFs as a better alternative to traditional CSG methods, see https://github.com/deadsy/sdfx

Basic usage

CSG

s1 := NewCube(&CubeOptions{Size: &Vector{2, 2, 2}})
s2 := NewSphere(&SphereOptions{Center: &Vector{1, 1, 1}, Radius: 1.2, Slices: 15, Stacks: 15})
    
c:=s1.Subtract(s2)

out, err := os.Create("v.stl")
if err != nil {
    panic(err)
}
c.MarshalToASCIISTL(out)
out.Close()

will result in:

Image of resulting subtraction

Hulling shapes:

h := &Hull{}

s1 := csg.NewSphere(&csg.SphereOptions{Center: &csg.Vector{0, 0, 0}})
s2 := csg.NewSphere(&csg.SphereOptions{Center: &csg.Vector{4, 0, -4}})
s3 := csg.NewSphere(&csg.SphereOptions{Center: &csg.Vector{4, 0, 4}})

err := h.BuildFromCSG([]*csg.CSG{s1, s2, s3})
if err != nil {
	panic(err)
}

c := h.ToCSG()
out, err := os.Create("v.stl")
if err != nil {
	panic(err)
}
c.MarshalToASCIISTL(out)
out.Close()

will result in:

Image of resulting hull

Why?

I do a lot of OpenSCAD programming and I was hoping to find a way to build a more preformant and automatable tool-chain for 3D programming, plus I wanted to get my 3D math and understanding to a better place. Anyways while building this library I discovered SDFs (Signed Distance Functions), which is where I'll re-focus my efforts because it they seem like ultimately a more flexible approach to CSG rather then a mesh or polygon approach, I recommend seeing https://github.com/deadsy/sdfx as it's where I'm going to refocus my efforts.

csg's People

Contributors

celer avatar everclear avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

csg's Issues

Incorrect output

Hi, This looks very interesting, but it doesn't seem to work. The output is jumble of triangles. Could you confirm the code in the repo is correct. Thanks.

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.