sumit
11/16/2019 - 1:31 PM

array glib cpp

// NOTE: always free the array, the code of freeing is given in end of file 

#include <gst/gst.h>
#include <glib.h>

// initialize array
struct safe_zone {
  double theta_center;
  int min_theta_point[3]; // {angle, x, y}
  int max_theta_point[3]; // {angle, x, y}
  int flag_direction;
  gboolean flag_positive_speed;
};
GArray *arr_safe_zone; /* this array will contain bbox struct at every frame for each tracked object */
arr_safe_zone = g_array_new (FALSE, FALSE, sizeof (struct safe_zone));

 //append array
struct safe_zone current_safe_zone = { theta_center, 
                                       {min_theta_point[0], min_theta_point[1], min_theta_point[2]},
                                       {max_theta_point[0], max_theta_point[1], max_theta_point[2]}, 
                                       flag_direction, 
                                       flag_positive_speed };
g_array_append_val(arr_safe_zone, current_safe_zone);
// Get array Length                                                     
int arr_length = arr_safe_zone->len

// Sort array 
double safe_zone_comparator(gconstpointer zone1, gconstpointer zone2) {
  double theta1 = ((struct safe_zone *)zone1)->theta_center;
  double theta2 = ((struct safe_zone *)zone2)->theta_center;
  return theta1 - theta2;
}
g_array_sort(arr_safe_zone, (GCompareFunc)safe_zone_comparator);

// FREE array
g_array_free (arr_safe_zone, TRUE);