Search code examples
c++data-structuresrospoint-cloud-library

Member in a struct that isn't attributed to any type? and a struct to hold a lone variable


I'm trying to understand the code below, it is for a struct that holds XYZ and Intensity value in a pointcloud, The field PCL_ADD_POINT4D looks like a standalone statement which is not of any known type, what does that mean? is it a way to address the contents?

struct EIGEN_ALIGN16 _PointXYZI
   {
    PCL_ADD_POINT4D; // This adds the members x,y,z which can also be accessed using the point (which is float[4])
     union
     {
       struct
       {
         float intensity;
       };
       float data_c[4];
     };
     EIGEN_MAKE_ALIGNED_OPERATOR_NEW
   };



this is from http://docs.ros.org/hydro/api/pcl/html/point__types_8hpp_source.html#l00382

what is PCL_ADD_POINT4D and is there any obvious reason why intensity needs to be in a struct. Cheers.

EDIT:

As @Beta has pointed out PCL_ADD_POINT4D comes from here

Having intensity inside a struct is still strange. (I'll leave the question open in case there is a legit reason for this)


Solution

  • Part 1: PCL_ADD_POINT4D refers to a macro

    PCL_ADD_POINT4D refers to the following lines

     #define PCL_ADD_POINT4D \
       union EIGEN_ALIGN16 { \
         float data[4]; \
         struct { \
           float x; \
           float y; \
           float z; \
         }; \
       }; \
    

    here EIGEN_ALIGN16 is a SSE macro that help to optimise the point operations because they are usually applied over vectors (in our case point vectors/clouds) of types we define in these contexts.

    link to source file is here

    Part 2: Struct to house one variable

    I think this is more of a eye pleasing design pattern thing more than anything else, I got used to it as my eyes were getting familiar with the excellent PCL library more.

    In structs members are stored in adjacent memory locations in order, and in unions all members start at the same location and do have an overlapping nature (imagine combining different size(lengthwise) strings to form a long string [struct] vs hanging different length strings(or ropes) from one point [union], in the union case the length of the whole thing will be the length of the longest piece of string.

    we can exploit these features to give a nice compact look to our new types and that's exactly what the authors of PCL has done.

    struct point_xyz{
        union{
            float x;
            float y;
            float z;
            float padding; // redundant with data[4] already taking care of it
        };
        float data[4];
    };
    

    if we instantiate a point_xyz as e.g. point1 we can access it members through point1.data[0] or point1.x also we can pass the point as a whole using .data to functions that prefer it in that way.

    So how is this relevant to the original question ?

    I think it is because this point type structure definition appears very regularly with the PCL source and when declaring the intensity the same blueprint was used just to be easy on the eyes.