Android Question 2D Float Array Java to B4A

walterf25

Expert
Licensed User
Longtime User
Hi all, i could probably figure this out myself, but i've been working on an AR library which I will releasing soon to the forums, but I want to wrap as much as I can before i release it, and i'm trying to port a lot of the classes being used to B4A so that we can have full control of the entire AR SDK, i'm currently trying to figure out how to load a 2D Float array like this;
B4X:
            float cube_vertices[][] = {
                /* +z */{1.0f / 2, 1.0f / 2, 0.01f / 2}, {1.0f / 2, -1.0f / 2, 0.01f / 2}, {-1.0f / 2, -1.0f / 2, 0.01f / 2}, {-1.0f / 2, 1.0f / 2, 0.01f / 2},
                /* -z */{1.0f / 2, 1.0f / 2, -0.01f / 2}, {1.0f / 2, -1.0f / 2, -0.01f / 2}, {-1.0f / 2, -1.0f / 2, -0.01f / 2}, {-1.0f / 2, 1.0f / 2, -0.01f / 2}
            };

I know in B4A I can do this
B4X:
Dim cube_vertices_buffer(,) As Float
But how can I actually load the array elements like in the Java code above, I found some function that @DonManfred posted to load this type of array, but that doesn't seem to work for this purpose, does anybody have a way of doing this?

Thanks.
Walter
 

sorex

Expert
Licensed User
Longtime User
a custom type is an option but I guess it's easier to pass a csv string and fill the array like that.

B4X:
    Type vertice(x As Float,y As Float,z As Float)

    Dim cube_vertices(2) As vertice=Array As vertice(newVertice(1,2,3),newVertice(2,3,1))
 Log(cube_vertices(0).x)

Sub newVertice(x As Float,y As Float,z As Float) As vertice
    Dim v As vertice
    v.Initialize
    v.x=x
    v.y=y
    v.z=z
    Return v
End Sub
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
a custom type is an option but I guess it's easier to pass a csv string and fill the array like that.

B4X:
    Type vertice(x As Float,y As Float,z As Float)

    Dim cube_vertices(2) As vertice=Array As vertice(newVertice(1,2,3),newVertice(2,3,1))
 Log(cube_vertices(0).x)

Sub newVertice(x As Float,y As Float,z As Float) As vertice
    Dim v As vertice
    v.Initialize
    v.x=x
    v.y=y
    v.z=z
    Return v
End Sub
Thanks, I think this may work, although I really wish B4X had this type of methods without having to create extra functions.

Regards,
Walter
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
a custom type is an option but I guess it's easier to pass a csv string and fill the array like that.

B4X:
    Type vertice(x As Float,y As Float,z As Float)

    Dim cube_vertices(2) As vertice=Array As vertice(newVertice(1,2,3),newVertice(2,3,1))
 Log(cube_vertices(0).x)

Sub newVertice(x As Float,y As Float,z As Float) As vertice
    Dim v As vertice
    v.Initialize
    v.x=x
    v.y=y
    v.z=z
    Return v
End Sub
I also came out with this other solution although i have to admit is not as fancy as your suggestion, but it seems to work good as well.
B4X:
    Dim cube_vertices As List
    cube_vertices.Initialize
    cube_vertices.Add(Array As Float(1.0f / 2, 1.0f / 2, 0.01f / 2))
    cube_vertices.Add(Array As Float(1.0f / 2, -1.0f / 2, 0.01f / 2))
    cube_vertices.Add(Array As Float(-1.0f / 2, -1.0f / 2, 0.01f / 2))
    cube_vertices.Add(Array As Float(-1.0f / 2, 1.0f / 2, 0.01f / 2))
    cube_vertices.Add(Array As Float(1.0f / 2, 1.0f / 2, -0.01f / 2))
    cube_vertices.Add(Array As Float(1.0f / 2, -1.0f / 2, -0.01f / 2))
    cube_vertices.Add(Array As Float(-1.0f / 2, -1.0f / 2, -0.01f / 2))
    cube_vertices.Add(Array As Float(-1.0f / 2, 1.0f / 2, -0.01f / 2))
    
    For i = 0 To cube_vertices.Size - 1
        Dim l() As Float
        l = cube_vertices.Get(i)
        For j = 0 To l.Length - 1
            LogColor("array at ["&i&"]" & "["&j&"]" & l(j), Colors.Magenta)
        Next
    Next

It logs them like this:
array at [0][0]0.5
array at [0][1]0.5
array at [0][2]0.004999999888241291
array at [1][0]0.5
array at [1][1]-0.5
array at [1][2]0.004999999888241291
array at [2][0]-0.5
array at [2][1]-0.5
array at [2][2]0.004999999888241291
array at [3][0]-0.5
array at [3][1]0.5
array at [3][2]0.004999999888241291
array at [4][0]0.5
array at [4][1]0.5
array at [4][2]-0.004999999888241291
array at [5][0]0.5
array at [5][1]-0.5
array at [5][2]-0.004999999888241291
array at [6][0]-0.5
array at [6][1]-0.5
array at [6][2]-0.004999999888241291
array at [7][0]-0.5
array at [7][1]0.5
array at [7][2]-0.004999999888241291

Walter
 
Upvote 0
Top