View Full Version : Using MAT on an array within a Union
dcromley
08-04-2009, 20:44
I will accept suggestions for a more appropriate board to post this question :)
I don't NEED to do this, but I wanted to refer to a elements of a 3-dimension point either by
point(1),point(2),point(3) or by point.x, point.y, and point.z
so I put both in a Union. Should I be able to say
MAT regular_array = union.array ? My code says no.
uses "math"
type zVector
x as double
y as double
z as double
end type
union zUnion
v as zVector
a(3) as double
end union
dim UnionOne as zUnion, aOne(3) as double
UnionOne.v.x = 10
msgbox 0, UnionOne.v.x ' gives 10, as it should
msgbox 0, UnionOne.a(1) ' gives 10, as it should
' ----------------------- should this statement work?
MAT aOne = UnionOne.a ' gives error
' --------------------------------------------------
MAT aOne = UnionOne ' no error, but no results
msgbox 0, aOne(1) ' want this to be 10, is 0
Petr Schreiber
08-04-2009, 21:21
Hi,
in theory it should work, I think.
Here is workaround:
uses "math", "console"
type zVector
x as double
y as double
z as double
end type
union zUnion
v as zVector
a(3) as double
end union
dim UnionOne as zUnion
UnionOne.v.x = 10
printl UnionOne.v.x ' gives 10, as it should
printl UnionOne.a(1) ' gives 10, as it should
' -- The following is memory overlay - you change something in aOne and it
' -- is reflected in union
dim aOne(3) as double at varptr(UnionOne)
printl aOne(1) ' want this to be 10, is 10
' -- The following is copy of memory blocks ( independent copy )
dim aTwo(3) as double
' -- POKE$ copies something TO specified address
' -- PEEK$ copies something FROM specified address
' -- VARPTR returns pointer to numeric variable
poke$ ( varptr(aTwo(1)), peek$( varptr(UnionOne), sizeof(double)*3 ))
printl aTwo(1) ' want this to be 10, is 10
waitkey
Petr
ErosOlmi
08-04-2009, 21:36
I'm sorry but MAT statement works only on regular arrays/matrix.
Petr suggestion to overimpose logical structure to memory areas of the UNION or TYPE using DIM ... AT statement is one solution to have a work around.
uses "math"
type zVector
x as double
y as double
z as double
end type
union zUnion
v as zVector
a(3) as double
end union
dim UnionOne as zUnion, aOne(3) as double
UnionOne.v.x = 10
msgbox 0, UnionOne.v.x ' gives 10, as it should
msgbox 0, UnionOne.a(1) ' gives 10, as it should
'---Over Impose a logical structure (DummyA) to the same memory area of UnionOne
'---in order to let MAT statement think it is dealing with a standard array.
dim DummyA(3) as DOUBLE at varptr(UnionOne)
' ----------------------- should this statement work?
MAT aOne = DummyA ' gives error
' --------------------------------------------------
MAT aOne = DummyA ' no error, but no results
msgbox 0, aOne(1) ' want this to be 10, is 0
MAT statement is quite complex. Do not know if I will be able to improve it allowing all possibile kind of matrix even inside complex structures. But if really needed I can have a look.
Ciao
Eros
dcromley
09-04-2009, 03:27
Thank you again guys.
Petr,
Your "printl" is better than "msgbox".
I know about PEEKs and POKEs from my deep past.
Eros,
>But if really needed I can ..
Not at all. I was just trying out ideas. Lots of other approaches.
Nice to know about all this.
uses "math", "console"
type zVector
x as double
y as double
z as double
end type
dim VectorOne as zVector
VectorOne.x = 10
' One could do this
dim ArrayOne(3) as double
call VectorToArray(VectorOne, ArrayOne)
printl ArrayOne(1) ' want this to be 10, is 10
' Or one could do this
dim ArrayTwo(3) as double at varptr(VectorOne)
printl ArrayTwo(1) ' want this to be 10, is 10
WaitKey
Sub VectorToArray(pv as zVector, pa() as double)
pa(1) = pv.x
pa(2) = pv.y
pa(3) = pv.z
End Function
I LOVE this stuff! Dave
ErosOlmi
09-04-2009, 07:24
Ok, Dave. Thanks.
DIM ... AT ... is a very very powerful statement because it will let you manage even complex situation in a very simple way.
You can over impose any logical structure to any memory area already allocated.
_________________________________________________
A little last note on this forum capability: we have implemented a syntax highlighter to be used to colorize source code when posted. It is called geShi.
As you can see from previous posts, it is much nicer to have code colorized instead of just black text.
To activate syntax coloring in code, during posting use the drop down combo box called "Syntax highlight (geShi)" and choose the programming language of your code.
You will pass from:
uses "math", "console"
type zVector
x as double
y as double
z as double
end type
to this:
uses "math", "console"
type zVector
x as double
y as double
z as double
end type
Ciao
Eros
dcromley
09-04-2009, 11:51
Thank YOU.
And, of course, there is
ARRAY ASSIGN ArrayOne() = VectorOne.x, VectorOne.y, VectorOne.z