1. Wrapping Functions When Matrix is Involved
- Posted by Icy_Viking May 26, 2019
- 1744 views
Hello,
I know a matrix is an array of numbers. However I'm not sure how one would wrap them when making a wrapper. For example.
C Code
http://newtondynamics.com/wiki/index.php5?title=NewtonCreateBox --Documentation in case needed NewtonCollision* NewtonCreateBox( const NewtonWorld* newtonWorld, dFloat dx, dFloat dy, dFloat dz, int shapeID, const dFloat *offsetMatrix)
Eu Code
public constant xNewtonCreateBox = define_c_func(nd,"NewtonCreateBox",{C_POINTER,C_FLOAT,C_FLOAT,C_FLOAT,C_INT,C_POINTER},C_POINTER) public function NewtonCreateBox(atom world,atom x,atom y,atom z,atom id,atom matrix) return c_func(xNewtonCreateBox,{world,x,y,z,id,matrix}) end function
The code above appears correct, but I feel I am wrapping part of it wrong, perhaps? I know the matrix is a pointer float.
2. Re: Wrapping Functions When Matrix is Involved
- Posted by ghaberek (admin) May 26, 2019
- 1721 views
You just need to allocate the space for the floats, poke them into that space, and then pass the pointer to the function.
Not sure why we don't have peek/poke float functions in the standard library. But this is how you'd do it:
procedure poke_float32( atom ptr, object value ) if atom( value ) then value = {value} end if for i = 1 to length( value ) do poke( ptr, atom_to_float32(value[i]) ) ptr += sizeof(C_FLOAT) end for end procedure public constant xNewtonCreateBox = define_c_func( nd,"NewtonCreateBox", {C_POINTER,C_FLOAT,C_FLOAT,C_FLOAT,C_INT,C_POINTER}, C_POINTER ) public function NewtonCreateBox( atom world, atom x, atom y, atom z, atom id, sequence matrix ) -- we assume matrix is of non-zero length atom p_matrix = allocate_data( sizeof(C_FLOAT) * length(matrix), TRUE ) -- cleanup = TRUE poke_float32( p_matrix, matrix ) return c_func( xNewtonCreateBox, {world,x,y,z,id,p_matrix} ) end function
-Greg
3. Re: Wrapping Functions When Matrix is Involved
- Posted by Icy_Viking May 27, 2019
- 1721 views
You just need to allocate the space for the floats, poke them into that space, and then pass the pointer to the function.
Not sure why we don't have peek/poke float functions in the standard library. But this is how you'd do it:
procedure poke_float32( atom ptr, object value ) if atom( value ) then value = {value} end if for i = 1 to length( value ) do poke( ptr, atom_to_float32(value[i]) ) ptr += sizeof(C_FLOAT) end for end procedure public constant xNewtonCreateBox = define_c_func( nd,"NewtonCreateBox", {C_POINTER,C_FLOAT,C_FLOAT,C_FLOAT,C_INT,C_POINTER}, C_POINTER ) public function NewtonCreateBox( atom world, atom x, atom y, atom z, atom id, sequence matrix ) -- we assume matrix is of non-zero length atom p_matrix = allocate_data( sizeof(C_FLOAT) * length(matrix), TRUE ) -- cleanup = TRUE poke_float32( p_matrix, matrix ) return c_func( xNewtonCreateBox, {world,x,y,z,id,p_matrix} ) end function
-Greg
Thanks, this works, except for I don't think Euphoria has a sizeof function. I briefed through the manual, but didn't find anything. Unless allocate() can be subsistuted for sizeof?
4. Re: Wrapping Functions When Matrix is Involved
- Posted by irv May 27, 2019
- 1677 views
include std/dll.e ? sizeof(C_FLOAT)
4
Euphoria 4.1 beta 2
8.41.2.5
sizeof
<built-in> function sizeof( atom data_type )
Parameters:
data_type A C data type constant
Returns the size, in bytes of the specified data type.
5. Re: Wrapping Functions When Matrix is Involved
- Posted by Icy_Viking May 27, 2019
- 1643 views
include std/dll.e ? sizeof(C_FLOAT)
4
Euphoria 4.1 beta 2
8.41.2.5
sizeof
<built-in> function sizeof( atom data_type )
Parameters:
data_type A C data type constant
Returns the size, in bytes of the specified data type.
I see. I'm still using 4.0.5. I'm now switched to using Eu 4.1.0 Beta 2 and its working quite well and of course sizeof works now!
6. Re: Wrapping Functions When Matrix is Involved
- Posted by irv May 27, 2019
- 1607 views
I see. I'm still using 4.0.5
The more people who use 4.1, the sooner any bugs will be found, and the sooner they can be fixed.
I think that EuGTK is complex enough to be a fair test of Eu 4.1, and so far, only one "sort of" bug has shown up. It could also simply be documented as an "illegal operation", without inconveniencing anyone significantly. Don't let that stop you from using 4.1 b 2.
7. Re: Wrapping Functions When Matrix is Involved
- Posted by Icy_Viking May 27, 2019
- 1614 views
I see. I'm still using 4.0.5
The more people who use 4.1, the sooner any bugs will be found, and the sooner they can be fixed.
I think that EuGTK is complex enough to be a fair test of Eu 4.1, and so far, only one "sort of" bug has shown up. It could also simply be documented as an "illegal operation", without inconveniencing anyone significantly. Don't let that stop you from using 4.1 b 2.
I just edited my post. I just switched to 4.1.0 Beta 2 and so far everything is working. Also, that sizeof function is handy. Now all we need is a useful way to easily wrap structs, or if Eu had a struct function of its own.
8. Re: Wrapping Functions When Matrix is Involved
- Posted by ghaberek (admin) May 27, 2019
- 1607 views
Now all we need is a useful way to easily wrap structs, or if Eu had a struct function of its own.
There several options available in The Archive. I wrap all my structs by hand without much difficulty. See this comment.
I'd like to get Matt's memstruct* changes merged it after we get a proper 4.1 release out. It seems ripe for being a "version 4.2" feature.
(*Although I'm strongly considering changing "memstruct" to just "struct" -- but I suppose we'll burn that bridge when we get to it.)
So how is that 4.1 release going, anyway? I'm still struggling with the spaghetti soup that is the monolithic Makefile.
It's slow-going and I've been putting more effort into Euphoria MVC because I think more work is need to refresh the website.
If anything, rebuilding the website will act as a good proof-of-concept that 4.1 is viable for full release.
-Greg
9. Re: Wrapping Functions When Matrix is Involved
- Posted by Icy_Viking May 28, 2019
- 1563 views
Now all we need is a useful way to easily wrap structs, or if Eu had a struct function of its own.
There several options available in The Archive. I wrap all my structs by hand without much difficulty. See this comment.
I'd like to get Matt's memstruct* changes merged it after we get a proper 4.1 release out. It seems ripe for being a "version 4.2" feature.
(*Although I'm strongly considering changing "memstruct" to just "struct" -- but I suppose we'll burn that bridge when we get to it.)
So how is that 4.1 release going, anyway? I'm still struggling with the spaghetti soup that is the monolithic Makefile.
It's slow-going and I've been putting more effort into Euphoria MVC because I think more work is need to refresh the website.
If anything, rebuilding the website will act as a good proof-of-concept that 4.1 is viable for full release.
-Greg
4.1.0 beta 2 is going well for me. I'll take a look at that comment. Also, adding/changing memstruct to struct would be a good idea I think and would be a good idea to include as a feature in 4.2. Also, a new re-built website would also be nice as well.