Skip to content

printingin3d/JavaCSG

 
 

Repository files navigation

JavaCSG

Build Status

Java implementation of BSP based CSG (Constructive Solid Geometry). It is the only simple and free Java implementation I am aware of. This implementation uses the same CSG algorithm as csg.js (see CSG and Node classes). Thanks to the author for creating the csg.js library.

In addition to CSG this library provides the following features:

  • extrusion of concave, non-intersecting polygons. Uses Poly2Tri for triangulation.
  • Transformations (Scale, Rotation, Translation and Mirror)
  • STL import and export (STLLoader from Fiji)

How to Build JavaCSG

Requirements

  • Java >= 1.7
  • Internet connection (dependencies are downloaded automatically)
  • IDE: Gradle Plugin (not necessary for command line usage) Eclipse doesn't need plugin

IDE

Open the JavaCSG Gradle project in your favourite IDE (tested with NetBeans 7.4) and build it by calling the assemble task.

Eclipse

You can run the eclipse task in the command line and import the project as an existing project into Eclipse. No plugin is needed in Eclipse.

Command Line

Navigate to the Gradle project (e.g., path/to/JavaCSG) and enter the following command

Bash (Linux/OS X/Cygwin/other Unix-like shell)

sh gradlew assemble

Windows (CMD)

gradlew assemble

Code Sample:

// we use cube and sphere as base geometries
CSG cube = new Cube(2).toCSG();
CSG sphere = new Sphere(1.25).toCSG();

// perform union, difference and intersection
CSG cubePlusSphere = cube.union(sphere);
CSG cubeMinusSphere = cube.difference(sphere);
CSG cubeIntersectSphere = cube.intersect(sphere);
        
// translate geometries to prevent overlapping 
CSG union = cube.
        union(sphere.transformed(Transform.unity().translateX(3))).
        union(cubePlusSphere.transformed(Transform.unity().translateX(6))).
        union(cubeMinusSphere.transformed(Transform.unity().translateX(9))).
        union(cubeIntersectSphere.transformed(Transform.unity().translateX(12)));
        
// save union as stl
try {
    FileUtil.write(
            Paths.get("sample.stl"),
            union.toStlString()
    );
} catch (IOException ex) {
    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages

  • Java 98.1%
  • Shell 1.8%
  • Groovy 0.1%