openigtlink / OpenIGTLink

Free, open-source network communication library for image-guided therapy
http://openigtlink.org
BSD 3-Clause "New" or "Revised" License
102 stars 184 forks source link

Is this a bug? #258

Closed itSeez closed 1 year ago

itSeez commented 1 year ago

I could be misunderstanding things here, and would appreciate being corrected.

Say I have an array of 3 dimensions and 300 elements on each dimension with elements represented as 4 byte floats. I would expect the size, in bytes, to be 300 * 3 * 4 = 3600.

I want to use the ndarray message to send my array over IGTL. Now lets examine the code for igtl_ndarray_alloc_info(), for example. In this case info->dim == 3 and info->size == {300, 300, 300} which, because of the product on line 83, lead to len == 27,000,000. The function then goes on to allocate 108,000,000 bytes.

Am I misinterpreting how the info->size array is meant to be used? or is this a bug?

leochan2009 commented 1 year ago

300 x 3 is only two dimension, please check the definition of dimension

itSeez commented 1 year ago

Is this the definition of dimension you are referring to?

/** NDARRAY is a data type, which is designed to transfer N-dimensional numerical array.
 *  The message body consists of N-D array header, size table, and N-D array body. */
typedef struct {
  igtl_uint8     type;   /* Scalar type (2:int8 3:uint8 4:int16 5:uint16 6:int32
                            7:uint32 10:float32 11:float64 13:complex) */
  igtl_uint8     dim;    /* Dimension of array */
  igtl_uint16 *  size;   /* Array size */
  void *         array;
} igtl_ndarray_info;

After more thought I've realized my mistake. To correct my example: info->dim == 2 and info->size == {300, 3} which leads to len == 900 and the correct amount of memory. A couple of comments:

The documentation simply says "Dimension of array" which is not very descriptive. I know this may obvious to those in the know, but the documentation would help a lot more people if it included an example. Like the one proposed in PR #259. Even the example igtlNDArrayMessage.cxx, which provides a couple of wrapper classes and methods, doesn't actually use them to send data between nodes.

leochan2009 commented 1 year ago

What I meant about “definition of dimension” is the mathematical definition of dimension. https://en.m.wikipedia.org/wiki/Dimension . For example, when talking about dimension of three, you expect {x, y, z} coordinates, when talking about dimension of four, you will expect {x,y,z,t}. This is such a fundamental knowledge that doesn’t need explanation.

“ Say I have an array of 3 dimensions and 300 elements on each dimension with elements represented as 4 byte floats. I would expect the size, in bytes, to be 300 3 4 = 3600.”

I don’t think most people would expect the size as mentioned above

itSeez commented 1 year ago

You are probably right. I confused myself because I kept thinking about the dimensionality of x,y,z rather than the dimensions of the array itself. That said, Others might make the same mistake.

leochan2009 commented 1 year ago

Dimension has the same definition across mathematics, physics and computer sciences. https://en.wikipedia.org/wiki/Array_(data_structure)