This site requires JavaScript, please enable it in your browser!
Greenfoot back
ihjufaeshiju
ihjufaeshiju wrote ...

2019/12/31

How to initialize a Vector containing objects containing float values?

ihjufaeshiju ihjufaeshiju

2019/12/31

#
I have got 3 classes:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//class1
public class vec3d 
{
    float x, y, z;
}
 
//class2
public class triangle 
{
    vec3d[] p = new vec3d[3];
}
 
//class3
import java.util.Vector;
 
public class mesh 
{
    Vector<triangle> tris;
}
In C++ a mesh for a cube was created like this:
1
2
3
4
5
6
mesh meshCube;
 
meshCube.tris = {
    {0.0f, 0.0f, 0.0f,    0.0f, 1.0f, 0.0f,    1.0f, 1.0f, 0.0f},
    {0.0f, 0.0f, 0.0f,    1.0f, 1.0f, 0.0f,    1.0f, 0.0f, 0.0f}, //and so on...
};
How can I initialize all the different triangles and vec3d's in java?
danpost danpost

2019/12/31

#
It would help if you had constructors for you classes. For example:
1
2
3
4
5
6
7
8
9
public class triangle()
{
    vec3d[] p;
     
    public triangle(vec3d a, vec3d b, vec3d c)
    {
        p = new vec3d[] { a, b, c };
    }
}
and
1
2
3
4
5
6
7
8
9
10
11
public class vec3d()
{
    float x, y, z;
     
    public vec3d(float a, float b, float c)
    {
        x = a;
        y = b;
        z = c;
    }
}
Then a triangle can be created with:
1
triangle triA = new triangle(new vec3d(0.0f, 0.0f, 0.0f), new vec3d(0.0f, 1.0f, 0.0f), new vec3d(1.0f, 1.0f, 0.0f));
ihjufaeshiju ihjufaeshiju

2019/12/31

#
So in order to create the whole mesh, could I do this?
1
mesh cubeMesh = new mesh(new triangle(new vec3d(0.0f, 0.0f, 0.0f), new vec3d(0.0f, 1.0f, 0.0f), new vec3d(1.0f, 1.0f, 0.0f)), new triangle(new vec3d(0.0f, 1.0f, 1.0f), new vec3d(0.0f, 1.0f, 0.0f), new vec3d(1.0f, 1.0f, 0.0f)))
danpost danpost

2019/12/31

#
ihjufaeshiju wrote...
So in order to create the whole mesh, could I do this? << Code Omitted >>
Yes -- provided you had the appropriate constructor for a mesh instance.
ihjufaeshiju ihjufaeshiju

2019/12/31

#
I now have the constructor added.
1
2
3
4
5
6
7
8
9
10
11
public class mesh 
{
    Vector<triangle> tris;
     
    public mesh(triangle south1, triangle south2, triangle east1, triangle east2) {
        tris.add(south1);
        tris.add(south2);
        tris.add(east1);
        tris.add(east2);
    }
}
I also create a new mesh like this:
1
2
3
4
5
6
7
public void OnUserCreate() {
    mesh meshCube = new mesh(
    new triangle(new vec3d(0.0f, 0.0f, 0.0f), new vec3d(0.0f, 1.0f, 0.0f), new vec3d(1.0f, 1.0f, 0.0f)),
    new triangle(new vec3d(0.0f, 0.0f, 0.0f), new vec3d(1.0f, 1.0f, 0.0f), new vec3d(1.0f, 0.0f, 0.0f)),
    new triangle(new vec3d(1.0f, 0.0f, 0.0f), new vec3d(1.0f, 1.0f, 0.0f), new vec3d(1.0f, 1.0f, 1.0f)),
    new triangle(new vec3d(1.0f, 0.0f, 0.0f), new vec3d(1.0f, 1.0f, 1.0f), new vec3d(1.0f, 0.0f, 1.0f)));
}
Is there any way for the size of the mesh (I.e. how many triangles) not to be limited by the constructor? Even the simplest cube would consist of 12 triangles. Thats 12 times tris.add(triangle) and 116 lines for a very simple spaceship.
danpost danpost

2019/12/31

#
ihjufaeshiju wrote...
Is there any way for the size of the mesh (I.e. how many triangles) not to be limited by the constructor? Even the simplest cube would consist of 12 triangles. Thats 12 times tris.add(triangle) and 116 lines for a very simple spaceship.
You should be able to use something like the following:
1
2
3
4
public mesh(triangle... triangles)
{
    tris = new Vector<triangle>(triangles);
}
(not tested) There may be a conversion problem which would then require you to iterate through the array (from parameter argument) and place the triangles into the vector via a loop. However, after looking at the Vector class API documentation of this constructor, I do not thing that will be the case. If line 3 errors, try this:
1
tris = new Vector<triangle>(java.util.Arrays.asList(triangles));
ihjufaeshiju ihjufaeshiju

2019/12/31

#
The first method results in a syntax error and the 2nd method prints this to the log: java.lang.ClassCastException: class java.util.Arrays$ArrayList cannot be cast to class java.util.Vector (java.util.Arrays$ArrayList and java.util.Vector are in module java.base of loader 'bootstrap') any alternatives?
danpost danpost

2020/1/1

#
ihjufaeshiju wrote...
The first method results in a syntax error and the 2nd method prints this to the log: java.lang.ClassCastException: class java.util.Arrays$ArrayList cannot be cast to class java.util.Vector (java.util.Arrays$ArrayList and java.util.Vector are in module java.base of loader 'bootstrap') any alternatives?
Alternative has already been provided:
There may be a conversion problem which would then require you to iterate through the array (from parameter argument) and place the triangles into the vector via a loop.
You need to login to post a reply.