Skip to content
/ jcsg Public
forked from NeuronRobotics/JCSG

Java implementation of BSP based CSG (Constructive Solid Geometry)

License

Notifications You must be signed in to change notification settings

breiler/jcsg

 
 

Repository files navigation

JCSG

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 an optimized CSG algorithm based on csg.js (see CSG and Node classes). Thanks to the author for creating the csg.js library.

This package is based on Kevin Harringtons work from https://github.com/NeuronRobotics/JCSG and Michael Hoffer from https://github.com/miho/JCSG with the goal to remove the dependency to JavaFX.

In addition to CSG this library provides the following features:

  • optimized difference() and union() operations (many thanks to Sebastian Reiter)
  • extrusion of concave, non-intersecting polygons (uses Poly2Tri for triangulation)
  • convex hull (uses QuickHull3D)
  • weighted transformations (Scale, Rotation, Translation and Mirror)
  • STL import and export (STLLoader from Fiji)
  • OBJ export including material information (see screenshot below)
  • 3d text support

Maven

<dependency>
  <groupId>com.breiler</groupId>
  <artifactId>jcsg-core</artifactId>
  <version>VERSION_FROM_BADGE</version>
</dependency>

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

Java implementation of BSP based CSG (Constructive Solid Geometry)

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Java 100.0%