2

I am making a 3D model of the volume generated by two functions: one as the base (in intersection of the x-axis), and the other as the height of the volume.This is achieved by approximating the volume of small parts, drawing that mesh, then adding all of the meshes together. However, I cannot figure out how to add the meshes together.

Here is my mesh creator:

 private TriangleMesh createVolume(double start, double end, double numSteps, PolynomialFunction base, PolynomialFunction height) {
    TriangleMesh m = new TriangleMesh();

    double stepSize = (end-start)/numSteps;

    for(double i = start; i < end; i += stepSize) {

        double x = i;
        double x2 = x+stepSize;
        double gx = height.value(x);
        double gx2 = height.value(x2);
        double fx = base.value(x);
        double fx2 = base.value(x2);


        TriangleMesh t = createVolumeSection(x,x2,gx,gx2,fx,fx2);
        m.getPoints().addAll(t.getPoints());
        m.getTexCoords().addAll(t.getTexCoords());
        m.getFaces().addAll(t.getFaces());
    }

    return m;
}


private TriangleMesh createVolumeSection(double xVal, double x2Val, double gxVal, double gx2Val,
                                         double fxVal, double fx2Val){
    TriangleMesh m = new TriangleMesh();
    float x = ((float)xVal) ;
    float x2 = ((float)x2Val);
    float gx = ((float)gxVal);
    float gx2 = ((float)gx2Val);
    float fx = ((float)fxVal);
    float fx2 = ((float)fx2Val);

    //create Points
    m.getPoints().addAll(
            x,  0,  0,      // A = 0
            x,  0,  gx,     // B = 1
            x2, 0,  0,      // C = 2
            x2, 0,  gx2,    // D = 3
            x,  fx, 0,      // E = 4
            x,  fx, gx,     // F = 5
            x2, fx2,0,      // G = 6
            x2, fx2,gx2     // H = 7
    );

    m.getTexCoords().addAll(0,0);

    m.getFaces().addAll(
            0 , 0 , 1 , 0 , 3 , 0 ,     // A-B-D
            0 , 0 , 3 , 0 , 2 , 0 ,     // A-D-C
            0 , 0 , 2 , 0 , 6 , 0 ,     // A-C-G
            0 , 0 , 6 , 0 , 4 , 0 ,     // A-G-E
            0 , 0 , 4 , 0 , 1 , 0 ,     // A-E-B
            1 , 0 , 4 , 0 , 5 , 0 ,     // B-E-F
            1 , 0 , 5 , 0 , 7 , 0 ,     // B-F-H
            1 , 0 , 7 , 0 , 3 , 0 ,     // B-H-D
            3 , 0 , 7 , 0 , 6 , 0 ,     // D-H-G
            3 , 0 , 6 , 0 , 2 , 0 ,     // D-G-C
            6 , 0 , 7 , 0 , 5 , 0 ,     // G-H-F
            6 , 0 , 5 , 0 , 4 , 0       // G-F-E
    );

    return m ;
}

This is supposed to create a series of right-trapezoidal prisms, but it ends up only drawing the first mesh in the series.

Can anyone help?

Thank You in Advance.

José Pereda
  • 44,311
  • 7
  • 104
  • 132

1 Answers1

0

Your approach is almost correct, but you are failing at updating the info of the faces array.

With every volume section you add, you add new vertices to the final mesh, so the vertices indices on the face array should be shifted accordingly. For that, keep a counter of the total points added for the n-1 volumes:

private int points=0;

private TriangleMesh createVolume(double start, double end, double numSteps, Function<Number,Number> base, Function<Number,Number> height) {
    ...
    TriangleMesh t = createVolumeSection(x,x2,gx,gx2,fx,fx2);
    m.getPoints().addAll(t.getPoints());
    points=m.getPoints().size()/3;
    ...
}

and then modify the faces array:

private TriangleMesh createVolumeSection(double xVal, double x2Val, double gxVal, double gx2Val,
                                         double fxVal, double fx2Val){
    TriangleMesh m = new TriangleMesh();
    ...
    m.getFaces().addAll(
            points+0 , 0 , points+1 , 0 , points+3 , 0 ,     // A-B-D
            points+0 , 0 , points+3 , 0 , points+2 , 0 ,     // A-D-C
            points+0 , 0 , points+2 , 0 , points+6 , 0 ,     // A-C-G
            points+0 , 0 , points+6 , 0 , points+4 , 0 ,     // A-G-E
            points+0 , 0 , points+4 , 0 , points+1 , 0 ,     // A-E-B
            points+1 , 0 , points+4 , 0 , points+5 , 0 ,     // B-E-F
            points+1 , 0 , points+5 , 0 , points+7 , 0 ,     // B-F-H
            points+1 , 0 , points+7 , 0 , points+3 , 0 ,     // B-H-D
            points+3 , 0 , points+7 , 0 , points+6 , 0 ,     // D-H-G
            points+3 , 0 , points+6 , 0 , points+2 , 0 ,     // D-G-C
            points+6 , 0 , points+7 , 0 , points+5 , 0 ,     // G-H-F
            points+6 , 0 , points+5 , 0 , points+4 , 0       // G-F-E
    );

    return m ;
}
José Pereda
  • 44,311
  • 7
  • 104
  • 132
  • I tried this, and on run, it gives me a warning that the values in the faces array must be within the range of ( 0 to points.length/3-1). The mesh doesn't get drawn at all either. – thblackhelmetman May 19 '15 at 21:59
  • I've tested it successfully: `TriangleMesh createVolume = createVolume(0, 10, 10, x->Math.sin(x.doubleValue()), x->Math.cos(x.doubleValue())); group.getChildren().addAll(new MeshView(createVolume));` – José Pereda May 19 '15 at 22:23
  • Would an option be to make the points have extra indices so that the warning doesn't appear, then add the points from a certain index. – thblackhelmetman May 20 '15 at 00:57
  • Or, should I remove the auxiliary method and transfer the code to the method with the loop? That would make it so that the whole points array gets updated, so the faces indices are also updated at the same time. – thblackhelmetman May 20 '15 at 01:05
  • Nevermind I figured out that I have to reset points to 0 each time the method is called. *Doh.* Thanks very much for your help. – thblackhelmetman May 20 '15 at 03:01