Code Monkey home page Code Monkey logo

Comments (3)

miho avatar miho commented on July 19, 2024

What are you trying to do with so many spheres? :)

To reduce the runtime you can enable optimization. This is still a work in progress. But it should work in your case. For me it reduced the runtime to about 10-20 seconds.

Here's an example:

import eu.mihosoft.vrl.v3d.CSG;
import eu.mihosoft.vrl.v3d.FileUtil;
import eu.mihosoft.vrl.v3d.Sphere;
import eu.mihosoft.vrl.v3d.Transform;
import java.io.IOException;
import java.nio.file.Paths;

/**
 *
 * @author Michael Hoffer <[email protected]>
 */
public class Spheres {

    public CSG toCSG() {

        double maxR = 10;

        double w = 30;
        double h = 30;
        double d = 30;

        // optimization reduces runtime dramatically
        CSG.setDefaultOptType(CSG.OptType.POLYGON_BOUND);

        CSG spheres = null;

        for(int i = 0;i<70;i++) {
            CSG s = new Sphere(Math.random()*maxR).toCSG().
                    transformed(
                            Transform.unity().
                                    translate(
                                            Math.random()*w,
                                            Math.random()*h,
                                            Math.random()*d));
            if (spheres == null) {
                spheres = s;
            } else {
                spheres = spheres.union(s);
            }
        }

        return spheres;
    }

    public static void main(String[] args) throws IOException {
        FileUtil.write(Paths.get("spheres.stl"), new Spheres().toCSG().toStlString());
    }
}

from jcsg.

danwinkler avatar danwinkler commented on July 19, 2024

Thanks so much for the quick response, and for the example code! I'm really enjoying using JCSG so far, thank you for creating it.

Your fix worked great, the polygons are much cleaner and the operations are much faster:
image

A small bug appeared when I changed the OptType, but it's easy to work around for now.

When I tried to subtract a cube from the spheres that wasn't intersecting with the spheres, I got this error:

Exception in thread "main" java.lang.RuntimeException: Please fix me! I don't know what to do?
    at eu.mihosoft.vrl.v3d.Node.invert(Node.java:133)
    at eu.mihosoft.vrl.v3d.CSG._differenceNoOpt(CSG.java:560)
    at eu.mihosoft.vrl.v3d.CSG._differencePolygonBoundsOpt(CSG.java:550)
    at eu.mihosoft.vrl.v3d.CSG.difference(CSG.java:517)
    at Spheres.toCSG(Spheres.java:50)
    at Spheres.main(Spheres.java:56)

I recreated the error by modifying your example code. Replace the Spheres.toCSG with:

    public CSG toCSG() {

        double maxR = 10;

        double w = 30;
        double h = 30;
        double d = 30;

        // optimization reduces runtime dramatically
        CSG.setDefaultOptType(CSG.OptType.POLYGON_BOUND);

        CSG spheres = null;

        for(int i = 0;i<20;i++) {
            System.out.println( "s " + i );
            CSG s = new Sphere(Math.random()*maxR).toCSG().
                transformed(
                    Transform.unity().
                        translate(
                            Math.random()*w - (w/2),
                            Math.random()*h - (h/2),
                            Math.random()*d - (d/2)
                        ));
            if (spheres == null) {
                spheres = s;
            } else {
                spheres = spheres.union(s);
            }
        }

        CSG box = new Cube( 100, 100, 100 ).toCSG().transformed( Transform.unity().translate( 0, 0, -110 ) );

        spheres = spheres.difference( box );

        return spheres;
    }

Like I said, pretty easy to work around for now. Thanks again!

from jcsg.

danwinkler avatar danwinkler commented on July 19, 2024

Oh, and the reason I'm trying to union so many spheres is for 3D Printing. I was trying to recreate what I did using OpenSCAD:
3DPrint

from jcsg.

Related Issues (20)

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.