Esempio n. 1
0
static int           _removeOldAIS(void)
{
    _ais_t  *ais = NULL;
    GTimeVal now;

    if (NULL == _ais_list) {
        g_print("s52ais:_getAIS() no AIS list\n");
        return FALSE;
    }

    g_get_current_time(&now);

    unsigned int i = 0;
    for (i=0; i<_ais_list->len; ++i) {
        ais = &g_array_index(_ais_list, _ais_t, i);
        // AIS report older then 10 min
        if (now.tv_sec > (ais->lastUpdate.tv_sec + AIS_SILENCE_MAX)) {
            _setAISDel(ais);

             g_array_remove_index_fast(_ais_list, i);

            return TRUE;
        }
    }

    return FALSE;
}
Esempio n. 2
0
int oh_remove_domain_from_handler(unsigned int h_id,
                                  SaHpiDomainIdT did)
{
    gint i;
    struct oh_handler *h;

    data_access_lock();
    h = oh_lookup_handler(h_id);
    if (h == NULL) {
        data_access_unlock();
        return -1;
    }
    for (i = 0; ; i++) {
        if (g_array_index(h->dids, SaHpiDomainIdT, i) == 0) {
            break;
        }
        if (g_array_index(h->dids, SaHpiDomainIdT, i) == did) {
            h->dids = g_array_remove_index_fast(h->dids, i);
            data_access_unlock();
            return 0;
        }
    }
    data_access_unlock();
    return -1;
}
Esempio n. 3
0
gboolean
ibus_hotkey_profile_remove_hotkey_by_event (IBusHotkeyProfile *profile,
                                            GQuark             event)
{
    IBusHotkeyProfilePrivate *priv;
    priv = IBUS_HOTKEY_PROFILE_GET_PRIVATE (profile);

    gint i;
    IBusHotkeyEvent *p = NULL;
    for ( i = 0; i < priv->events->len; i++) {
        p = &g_array_index (priv->events, IBusHotkeyEvent, i);
        if (p->event == event)
            break;
    }

    if (p == NULL || p->event != event)
        return FALSE;

    GList *list;
    for (list = p->hotkeys; list != NULL; list = list->next) {
        g_tree_remove (priv->hotkeys, (IBusHotkey *)list->data);
    }

    g_list_free (p->hotkeys);
    g_array_remove_index_fast (priv->events, i);

    return TRUE;
}
Esempio n. 4
0
static int key_press (Viewer *viewer, EventHandler *ehandler,
        const GdkEventKey *event)
{
    RendererGoal *self = (RendererGoal*) ehandler->user;

    if (event->keyval == 'g' || event->keyval == 'G') {
        if (viewer_picking(viewer))
            return 0;
    
        viewer_request_pick(viewer, ehandler);
        if (!ehandler->picking)
            return 0;

        return 1;
    }

    if ((event->keyval == GDK_Delete || event->keyval == GDK_BackSpace) && ehandler->picking) {
        ehandler->picking = 0;
        int idx = self->edit_goal - &g_array_index (self->my_goals, lcmtypes_goal_t, 0);
        g_array_remove_index_fast (self->my_goals, idx);
        self->edit_goal = NULL;
        return 1;
    }
    
    if (event->keyval == GDK_Escape && ehandler->picking) {
        ehandler->picking = 0;
        self->edit_goal = NULL;
        return 1;
    }
    
    return 0;
}
Esempio n. 5
0
GByteArray* g_byte_array_remove_index_fast (GByteArray *array,
					    guint       index_)
{
  g_array_remove_index_fast ((GArray*) array, index_);

  return array;
}
Esempio n. 6
0
static void
_cleanup(struct grid_task_queue_s *gtq)
{
	while (gtq->tasks->len > 0) {
		struct grid_task_s *task = &g_array_index(gtq->tasks, struct grid_task_s, 0);
		if (task->cleanup)
			task->cleanup(task->u);
		g_array_remove_index_fast(gtq->tasks, 0);
	}
}
Esempio n. 7
0
static int           _updateTimeTag(void)
// then update time tag of AIS
{
    if (NULL == _ais_list)
        return FALSE;

    /*
    {   // check global time - update time tag of all AIS each sec
        // FIXME: should be 0.5 sec
        GTimeVal now;
        g_get_current_time(&now);
        if ((now.tv_sec-_timeTick.tv_sec) < 1)
            return FALSE;

        g_get_current_time(&_timeTick);
    }
    */

    // keep removing old AIS
    while (TRUE == _removeOldAIS())
        _dumpAIS();

    GTimeVal now;
    g_get_current_time(&now);
    for (guint i=0; i<_ais_list->len; ++i) {
        gchar    str[127+1] = {'\0'};
        _ais_t  *ais = &g_array_index(_ais_list, _ais_t, i);

        if (-1.0 != ais->course) {
#ifdef S52_USE_SOCK
            g_snprintf(str, 127, "%s %lis%s%03.f deg / %3.1f kt", ais->name, (now.tv_sec - ais->lastUpdate.tv_sec),      NL, ais->course, ais->speed);
#else
            g_snprintf(str, 127, "%s %lis%c%03.f deg / %3.1f kt", ais->name, (now.tv_sec - ais->lastUpdate.tv_sec), (int)NL, ais->course, ais->speed);
#endif
        } else {
            g_snprintf(str, 127, "%s %lis", ais->name, now.tv_sec - ais->lastUpdate.tv_sec);
        }


#ifdef S52_USE_SOCK
        _encodeNsend("S52_setVESSELlabel", "%lu,\"%s\"", ais->vesselH, str);
#else
        if (FALSE == S52_setVESSELlabel(ais->vesselH, str)) {
            g_print("s52ais:_updateTimeTag(): FAIL setVESSELlabel = %s\n", str);
            _setAISDel(ais);
            g_array_remove_index_fast(_ais_list, i);

            //g_assert(0);
        }
#endif

    }

    return TRUE;
}
Esempio n. 8
0
static int           _delAIS    (unsigned int mmsi)
{
    for (guint i=0; i<_ais_list->len; ++i) {
        _ais_t *ais = &g_array_index(_ais_list, _ais_t, i);
        if (mmsi == ais->mmsi) {
            g_array_remove_index_fast(_ais_list, i);
            return TRUE;
        }
    }

    return FALSE;
}
void Clavier_touche_relacher( Scene* scene, char* nomTouche )
{
    int i = 0;

    for( i = 0; i < scene->clavier->nbTouche; i++ )
    {
        if( strcmp( g_array_index( scene->clavier->tTouche, char*, i ), nomTouche) == 0 )
        {
            g_array_remove_index_fast( scene->clavier->tTouche, i );
            scene->clavier->nbTouche--;
        }
    }
}
Esempio n. 10
0
gint
is_permutation(a, b)
{
    GArray *array_a, *array_b;
    gint i = 0;

    array_a = g_array_new(FALSE, FALSE, sizeof (gint));
    array_b = g_array_new(FALSE, FALSE, sizeof (gint));

    i = a % 10;
    g_array_prepend_val(array_a, i);
    i = b % 10;
    g_array_prepend_val(array_b, i);
    while(a > 0)
    {
        i = a % 10;
        g_array_prepend_val(array_a, i);
        a /= 10;
    }
    i = 0;
    while(b > 0)
    {
        i = b % 10;
        g_array_prepend_val(array_b, i);
        b /= 10;
    }
    g_array_remove_index_fast(array_a, array_a->len-1);
    g_array_remove_index_fast(array_b, array_b->len-1);

    g_array_sort(array_a, compare);
    g_array_sort(array_b, compare);

    print_array(array_a);
    print_array(array_b);

    return 0;
}
Esempio n. 11
0
void qio_ev_trigger(struct qio_ev *ev, struct qio *qio, const json_t *json)
{
	guint i;
	struct qio_ev_sub *sub;

	for (i = 0; i < ev->subs->len; i++) {
		sub = &g_array_index(ev->subs, struct qio_ev_sub, i);
		sub->cb(qio, ev->ev_path, json, sub->data);

		if (sub->once) {
			g_array_remove_index_fast(ev->subs, i);
			i--;
		}
	}
}
Esempio n. 12
0
void qio_ev_rm(struct qio_ev *ev, qio_ev_cb_fn cb, void *data)
{
	guint i;
	struct qio_ev_sub *sub;

	if (cb == NULL) {
		g_array_set_size(ev->subs, 0);
	} else {
		for (i = 0; i < ev->subs->len; i++) {
			sub = &g_array_index(ev->subs, struct qio_ev_sub, i);
			if (sub->cb == cb && data != NULL ? sub->data == data : TRUE) {
				g_array_remove_index_fast(ev->subs, i);
				i--;
			}
		}
	}
}
Esempio n. 13
0
int main(int argc, char** argv) {
 GArray* a = g_array_new(FALSE, FALSE, sizeof(int));
 int x[6] = {1,2,3,4,5,6};
 g_array_append_vals(a, &x, 6);
 prt(a);
 printf("Removing the first item\n");
 g_array_remove_index(a, 0);
 prt(a);
 printf("Removing the first two items\n");
 g_array_remove_range(a, 0, 2);
 prt(a);
 printf("Removing the first item very quickly\n");
 g_array_remove_index_fast(a, 0);
 prt(a);
 g_array_free(a, FALSE);
 return 0;
}
Esempio n. 14
0
gboolean
ibus_hotkey_profile_remove_hotkey (IBusHotkeyProfile *profile,
                                   guint              keyval,
                                   guint              modifiers)
{
    IBusHotkeyProfilePrivate *priv;
    priv = IBUS_HOTKEY_PROFILE_GET_PRIVATE (profile);

    IBusHotkey hotkey = {
        .keyval = keyval,
        .modifiers = modifiers
    };

    IBusHotkey *p1;
    GQuark event;
    gboolean retval;

    retval = g_tree_lookup_extended (priv->hotkeys,
                                     &hotkey,
                                     (gpointer)&p1,
                                     (gpointer)&event);

    if (!retval)
        return FALSE;

    gint i;
    IBusHotkeyEvent *p2 = NULL;
    for ( i = 0; i < priv->events->len; i++) {
        p2 = &g_array_index (priv->events, IBusHotkeyEvent, i);
        if (p2->event == event)
            break;
    }

    g_assert (p2->event == event);

    p2->hotkeys = g_list_remove (p2->hotkeys, p1);
    if (p2->hotkeys == NULL) {
        g_array_remove_index_fast (priv->events, i);
    }

    g_tree_remove (priv->hotkeys, p1);

    return TRUE;
}
Esempio n. 15
0
/**
 * gst_caps_features_remove_id:
 * @features: a #GstCapsFeatures.
 * @feature: a feature.
 *
 * Removes @feature from @features.
 *
 * Since: 1.2
 */
void
gst_caps_features_remove_id (GstCapsFeatures * features, GQuark feature)
{
  guint i, n;

  g_return_if_fail (features != NULL);
  g_return_if_fail (IS_MUTABLE (features));
  g_return_if_fail (feature != 0);

  n = features->array->len;
  for (i = 0; i < n; i++) {
    GQuark quark = gst_caps_features_get_nth_id (features, i);

    if (quark == feature) {
      g_array_remove_index_fast (features->array, i);
      return;
    }
  }
}
Esempio n. 16
0
static void
remmina_plugin_nx_remove_window_id (Window window_id)
{
    gint i;
    gboolean found = FALSE;

    pthread_mutex_lock (&remmina_nx_init_mutex);
    for (i = 0; i < remmina_nx_window_id_array->len; i++)
    {
        if (g_array_index (remmina_nx_window_id_array, Window, i) == window_id)
        {
            found = TRUE;
            break;
        }
    }
    if (found)
    {
        g_array_remove_index_fast (remmina_nx_window_id_array, i);
    }
    pthread_mutex_unlock (&remmina_nx_init_mutex);
}
Esempio n. 17
0
void vfs_file_monitor_remove( VFSFileMonitor * fm,
                              VFSFileMonitorCallback cb,
                              gpointer user_data )
{
    int i;
    VFSFileMonitorCallbackEntry* callbacks;

//printf( "vfs_file_monitor_remove\n" );
if ( !fm )
    printf( "    fm == NULL\n");
    if ( cb && fm && fm->callbacks )
    {
        callbacks = ( VFSFileMonitorCallbackEntry* ) fm->callbacks->data;
        for ( i = 0; i < fm->callbacks->len; ++i )
        {
            if ( callbacks[ i ].callback == cb && callbacks[ i ].user_data == user_data )
            {
                fm->callbacks = g_array_remove_index_fast ( fm->callbacks, i );
                break;
            }
        }
    }

    if ( fm && g_atomic_int_dec_and_test( &fm->n_ref ) )  //MOD added "fm &&"
    {
#ifdef USE_INOTIFY /* Linux inotify */
        inotify_rm_watch ( inotify_fd, fm->wd );
#else /*  Use FAM|gamin */
        FAMCancelMonitor( &fam, &fm->request );
#endif

        g_hash_table_remove( monitor_hash, fm->path );
        g_free( fm->path );
        g_array_free( fm->callbacks, TRUE );
        g_slice_free( VFSFileMonitor, fm );
    }
//printf( "vfs_file_monitor_remove   DONE\n" );
}
static CK_SESSION_HANDLE
pop_session_table (GP11ModulePrivate *pv, CK_SLOT_ID slot, gulong flags)
{
	CK_SESSION_HANDLE result = 0;
	SessionPool *pool;
	GArray **array;

	g_return_val_if_fail (pv, 0);

	if (!pv->open_sessions)
		return 0;
	
	pool = g_hash_table_lookup (pv->open_sessions, &slot);
	if (pool == NULL) 
		return 0;
	
	if (flags & CKF_RW_SESSION)
		array = &pool->rw_sessions;
	else
		array = &pool->ro_sessions;
	
	if (*array == NULL)
		return 0;
	
	g_assert ((*array)->len > 0);
	result = g_array_index (*array, CK_SESSION_HANDLE, (*array)->len - 1);
	g_assert (result != 0);
	g_array_remove_index_fast (*array, (*array)->len - 1);
	
	if (!(*array)->len) {
		g_array_free (*array, TRUE);
		*array = NULL;
		if (!pool->rw_sessions && !pool->ro_sessions)
			g_hash_table_remove (pv->open_sessions, &slot);
	}

	return result;
}
Esempio n. 19
0
static void remmina_rdp_event_release_key(RemminaProtocolWidget* gp, gint scancode)
{
	gint i, k;
	rfContext* rfi;
	RemminaPluginRdpEvent rdp_event = { 0 };

	rfi = GET_DATA(gp);
	rdp_event.type = REMMINA_RDP_EVENT_TYPE_SCANCODE;

	if (scancode == 0)
	{
		/* Send all release key events for previously pressed keys */
		rdp_event.key_event.up = True;

		for (i = 0; i < rfi->pressed_keys->len; i++)
		{
			rdp_event.key_event.key_code = g_array_index(rfi->pressed_keys, gint, i);
			remmina_rdp_event_event_push(gp, &rdp_event);
		}

		g_array_set_size(rfi->pressed_keys, 0);
	}
	else
	{
		/* Unregister the keycode only */
		for (i = 0; i < rfi->pressed_keys->len; i++)
		{
			k = g_array_index(rfi->pressed_keys, gint, i);

			if (k == scancode)
			{
				g_array_remove_index_fast(rfi->pressed_keys, i);
				break;
			}
		}
	}
}
Esempio n. 20
0
void
_cogl_poll_renderer_remove_fd (CoglRenderer *renderer, int fd)
{
  int i = find_pollfd (renderer, fd);
  GList *l;

  if (i < 0)
    return;

  g_array_remove_index_fast (renderer->poll_fds, i);
  renderer->poll_fds_age++;

  for (l = renderer->poll_sources; l; l = l->next)
    {
      CoglPollSource *source = l->data;
      if (source->fd == fd)
        {
          renderer->poll_sources =
            g_list_delete_link (renderer->poll_sources, l);
          g_slice_free (CoglPollSource, source);
          break;
        }
    }
}
Esempio n. 21
0
void game_set_initial_configuration_position(game_t *game)
{
    random_set_seed(time(NULL));
    
    int n = game->graph->n;
    GArray *players = g_array_new(FALSE, FALSE, sizeof(int));
    
    int i;
    for(i = 0; i < n; ++i)
    {
        game->initial_config[i] = 0;
        game->current_config[i] = 0;
        
        g_array_append_val(players, i);
    }
    
    mpq_t e;
    mpq_init(e);
    mpq_set_si(e, game->graph->n, 1);
    mpq_mul(e, game->p_c, e);
    int m = ROUND(mpq_get_d(e));
    mpq_clear(e);
    for(i = 0; i < m; ++i)
    {
        int j = random_integer(n);
        int x = g_array_index(players, int, j);
        
        game->initial_config[x] = 1;
        game->current_config[x] = 1;
        
        g_array_remove_index_fast(players, j);
        n--;
    }
    
    g_array_free(players, TRUE);
}
Esempio n. 22
0
// chain function; this function does the actual processing
static GstFlowReturn
gst_surf_tracker_chain(GstPad *pad, GstBuffer *buf) {
    GstSURFTracker *filter;
    GstClockTime    timestamp;

    // sanity checks
    g_return_val_if_fail(pad != NULL, GST_FLOW_ERROR);
    g_return_val_if_fail(buf != NULL, GST_FLOW_ERROR);

    filter = GST_SURF_TRACKER(GST_OBJECT_PARENT(pad));
    filter->image->imageData = (char*) GST_BUFFER_DATA(buf);

    // Create the gray image for the surf 'features' search process
    cvCvtColor(filter->image, filter->gray, CV_BGR2GRAY);
    ++filter->frames_processed;
    timestamp = GST_BUFFER_TIMESTAMP(buf);

    // If exist stored_objects: search matching, update, cleaning
    if ((filter->stored_objects != NULL) && (filter->stored_objects->len > 0)) {
        CvMemStorage *surf_image_mem_storage;
        CvSeq        *surf_image_keypoints, *surf_image_descriptors;
        guint         i;
        gint          j;

        // Update the match set 'features' for each object
        surf_image_mem_storage = cvCreateMemStorage(0);

        // Search 'features' in full image
        surf_image_keypoints = surf_image_descriptors = NULL;
        cvExtractSURF(filter->gray, NULL, &surf_image_keypoints, &surf_image_descriptors,
                      surf_image_mem_storage, filter->params, 0);

        for (i = 0; i < filter->stored_objects->len; ++i) {
            InstanceObject *object;
            GArray         *pairs;

            object = &g_array_index(filter->stored_objects, InstanceObject, i);
            pairs  = g_array_new(FALSE, FALSE, sizeof(IntPair));

            findPairs(object->surf_object_keypoints, object->surf_object_descriptors,
                      surf_image_keypoints, surf_image_descriptors, pairs);

            // if match, update object
            if (pairs->len && (float) pairs->len / object->surf_object_descriptors->total >= MIN_MATCH_OBJECT) {
                object->range_viewed++;
                object->last_frame_viewed = filter->frames_processed;
                object->timestamp         = timestamp;

                if (object->surf_object_keypoints_last_match != NULL)
                    cvClearSeq(object->surf_object_keypoints_last_match);
                object->surf_object_keypoints_last_match = getMatchPoints(surf_image_keypoints, pairs, 1, object->mem_storage);

                if (object->surf_object_descriptors_last_match != NULL)
                    cvClearSeq(object->surf_object_descriptors_last_match);
                object->surf_object_descriptors_last_match = getMatchPoints(surf_image_descriptors, pairs, 1, object->mem_storage);

                // Estimate rect of objects localized
                object->rect_estimated = rectDisplacement(object->surf_object_keypoints, surf_image_keypoints, pairs, object->rect, PAIRS_PERC_CONSIDERATE);
            }

            g_array_free(pairs, TRUE);
        }

        if (surf_image_keypoints != NULL) cvClearSeq(surf_image_keypoints);
        if (surf_image_descriptors != NULL) cvClearSeq(surf_image_descriptors);
        cvReleaseMemStorage(&surf_image_mem_storage);

        // Clean old objects
        for (j = filter->stored_objects->len - 1; j >= 0; --j) {
            InstanceObject *object;

            object = &g_array_index(filter->stored_objects, InstanceObject, j);
            if ((filter->frames_processed - object->last_frame_viewed > DELOBJ_NFRAMES_IS_OLD) ||
                (filter->frames_processed != object->last_frame_viewed && object->range_viewed < DELOBJ_COMBOFRAMES_IS_IRRELEVANT)) {
                if (object->surf_object_keypoints != NULL) cvClearSeq(object->surf_object_keypoints);
                if (object->surf_object_descriptors != NULL) cvClearSeq(object->surf_object_descriptors);
                if (object->surf_object_keypoints_last_match != NULL) cvClearSeq(object->surf_object_keypoints_last_match);
                if (object->surf_object_descriptors_last_match != NULL) cvClearSeq(object->surf_object_descriptors_last_match);
                cvReleaseMemStorage(&object->mem_storage);
                g_array_remove_index_fast(filter->stored_objects, j);
            }
        }

    } // if any object exist

    // Process all haar rects
    if ((filter->rect_array != NULL) && (filter->rect_array->len > 0)) {
        guint i, j;

        for (i = 0; i < filter->rect_array->len; ++i) {
            CvRect rect = g_array_index(filter->rect_array, CvRect, i);

            // If already exist in 'stored_objects', update features. Else save
            // as new.
            for (j = 0; j < filter->stored_objects->len; ++j) {
                InstanceObject *object;

                object = &g_array_index(filter->stored_objects, InstanceObject, j);

                // It is considered equal if the "centroid match features" is inner
                // haar rect AND max area deviation is PERC_RECT_TO_SAME_OBJECT
                if (pointIntoRect(rect, (object->surf_object_keypoints_last_match != NULL) ? surfCentroid(object->surf_object_keypoints_last_match, cvPoint(0, 0)) : surfCentroid(object->surf_object_keypoints, cvPoint(0, 0))) &&
                    ((float) MIN((object->rect.width * object->rect.height), (rect.width * rect.height)) / (float) MAX((object->rect.width * object->rect.height), (rect.width * rect.height)) >= PERC_RECT_TO_SAME_OBJECT)) {

                    // Update the object features secound the new body rect
                    cvSetImageROI(filter->gray, rect);
                    cvExtractSURF(filter->gray, NULL, &object->surf_object_keypoints, &object->surf_object_descriptors,
                                  object->mem_storage, filter->params, 0);
                    cvResetImageROI(filter->gray);
                    object->rect = object->rect_estimated = rect;
                    object->last_body_identify_timestamp = timestamp;

                    break;
                }
            }

            // If new, create object and append in stored_objects
            if (j >= filter->stored_objects->len) {
                InstanceObject object;

                object.surf_object_keypoints   = 0;
                object.surf_object_descriptors = 0;
                object.mem_storage             = cvCreateMemStorage(0);

                cvSetImageROI(filter->gray, rect);
                cvExtractSURF(filter->gray, NULL, &object.surf_object_keypoints, &object.surf_object_descriptors,
                              object.mem_storage, filter->params, 0);
                cvResetImageROI(filter->gray);

                if (object.surf_object_descriptors && object.surf_object_descriptors->total > 0) {
                    object.id                                 = filter->static_count_objects++;
                    object.last_frame_viewed                  = filter->frames_processed;
                    object.range_viewed                       = 1;
                    object.rect                               = object.rect_estimated               = rect;
                    object.timestamp                          = object.last_body_identify_timestamp = timestamp;
                    object.surf_object_keypoints_last_match   = NULL;
                    object.surf_object_descriptors_last_match = NULL;

                    g_array_append_val(filter->stored_objects, object);
                }
            } // new
        }
    }

    // Put the objects found in the frame in gstreamer pad
    if ((filter->stored_objects != NULL) && (filter->stored_objects->len > 0)) {
        guint i;

        for (i = 0; i < filter->stored_objects->len; ++i) {
            InstanceObject object = g_array_index(filter->stored_objects, InstanceObject, i);

            // 'Continue' whether the object is not found in this frame
            if (object.timestamp == timestamp) {
                TrackedObject *tracked_object;
                GstEvent      *event;
                CvRect         rect;

                rect = ((object.last_body_identify_timestamp == timestamp) ? object.rect : object.rect_estimated);

                if (filter->verbose) {
                    GST_INFO("[object #%d rect] x: %d, y: %d, width: %d, height: %d\n", object.id, rect.x, rect.y, rect.width, rect.height);
                    // drawSurfPoints(object.surf_object_keypoints, cvPoint(object.rect.x, object.rect.y), filter->image, PRINT_COLOR, 0);
                    // drawSurfPoints(object.surf_object_keypoints_last_match, cvPoint(object.rect.x, object.rect.y), filter->image, PRINT_COLOR, 1);
                }

                if (filter->display_features) {
                    drawSurfPoints(object.surf_object_keypoints_last_match, cvPoint(0, 0), filter->image, PRINT_COLOR, 1);
                }

                if (filter->display) {
                    char *label;
                    float font_scaling;

                    font_scaling = ((filter->image->width * filter->image->height) > (320 * 240)) ? 0.5f : 0.3f;

                    cvRectangle(filter->image, cvPoint(rect.x, rect.y), cvPoint(rect.x + rect.width, rect.y + rect.height),
                                PRINT_COLOR, ((object.last_body_identify_timestamp == timestamp) ? 2 : 1), 8, 0);
                    label = g_strdup_printf("OBJ#%i", object.id);
                    printText(filter->image, cvPoint(rect.x + (rect.width / 2), rect.y + (rect.height / 2)), label, PRINT_COLOR, font_scaling, 1);
                    g_free(label);
                }

                // allocate and initialize 'TrackedObject' structure
                tracked_object = tracked_object_new();
                tracked_object->id        = g_strdup_printf("PERSON#%d", object.id);
                tracked_object->type      = TRACKED_OBJECT_DYNAMIC;
                tracked_object->height    = rect.height;
                tracked_object->timestamp = timestamp;

                // add the points that the define the lower part of the object (i.e,
                // the lower horizontal segment of the rectangle) as the objects perimeter
                tracked_object_add_point(tracked_object, rect.x, rect.y + rect.height);
                tracked_object_add_point(tracked_object, rect.x + rect.width, rect.y + rect.height);

                // send downstream event
                event = gst_event_new_custom(GST_EVENT_CUSTOM_DOWNSTREAM,
                                             tracked_object_to_structure(tracked_object, "tracked-object"));
                gst_pad_push_event(filter->srcpad, event);
            }
        }
    }

    // Clean body rects
    g_array_free(filter->rect_array, TRUE);
    filter->rect_array = g_array_sized_new(FALSE, FALSE, sizeof(CvRect), 1);

    // Draw number of objects stored
    if (filter->display) {
        char *label = g_strdup_printf("N_STORED_OBJS: %3i", filter->stored_objects->len);
        printText(filter->image, cvPoint(0, 0), label, PRINT_COLOR, .5, 1);
        g_free(label);
    }

    gst_buffer_set_data(buf, (guint8*) filter->image->imageData, (guint) filter->image->imageSize);
    return gst_pad_push(filter->srcpad, buf);
}
FullTree* qsearch_make_fulltree(QSearchTree *clt, const gsl_matrix *dm) {
    
    int i,j; 
    int node_count = clt->total_node_count;
    int leaf_count = (node_count + 2)/2;
    
    FullTree *tree = malloc(sizeof(FullTree));
    tree->node_count = node_count;
    
    tree->data = malloc(sizeof(Misc));
    
    ((Misc *)tree->data)->nodes = malloc(sizeof(FullNode) * node_count);
    ((Misc *)tree->data)->tmpA  = g_array_sized_new(FALSE, FALSE, sizeof(guint32), node_count);
    ((Misc *)tree->data)->tmpB  = g_array_sized_new(FALSE, FALSE, sizeof(guint32), node_count);

    FullNode *map = get_nodes(tree);
    
    GArray *todo = g_array_sized_new(FALSE, FALSE, sizeof(guint32), node_count - leaf_count);
    g_array_set_size(todo, node_count-leaf_count);

    for (i = 0; i < node_count; ++i) {
        FullNode *node = (map + i);
        
        for (j = 0; j < 3; ++j) {
            map[i].connections[j] = -1;
            map[i].leaf_count[j] = 0;
            map[i].dist[j] = 0;
        }
        
        guint8 *node_branch = malloc(sizeof(guint8) * node_count);
        node->node_branch = node_branch;

        if (i < leaf_count) {
            for (j=0; j < node_count; ++j) node_branch[j] = 0;
            node->leaf_count[0] = leaf_count - 1;
        } else {
            for (j = 0; j < node_count; ++j) node_branch[j] = -1;
            g_array_index(todo, guint32, i - leaf_count) = i;
        } 
    }
     
    // set connections (construct tree)
    for (i = 0; i < node_count; ++i) {

        QSearchNeighborList *cln = g_ptr_array_index(clt->n, i);
        // add to connected nodes
        for (j = 0; j < cln->n->len; ++j) {
            int node = g_array_index(cln->n, guint32, j);
        
            // find unfilled branch
            int branch = find_branch(map[node].connections, -1);
            map[node].connections[branch] = i; // set connection
            
            if (i < leaf_count) {
                map[node].leaf_count[branch] = 1; // set leaf
            }
            map[node].node_branch[i] = branch; // leaf can be found in branch

            // set connection back to this node
            branch = find_branch(map[i].connections, -1);
            map[i].connections[branch] = node;
            map[i].node_branch[node] = branch;
        }
    }
    
    // every iteration, this loop progresses by at least finishing one node. Worst case is n^3 (for 'linear' trees)
    while (todo->len > 0) {
        for (i = 0; i < todo->len; ++i) {
            
            int this_node = g_array_index(todo, guint32, i); 
                        
            for (j = 0; j < 3; ++j) {
                int connected_node = map[this_node].connections[j];
                int branch = find_branch(map[connected_node].connections, this_node);

                if (map[connected_node].leaf_count[branch] == 0) {
                    int first = (3 + j-1) % 3;
                    int second = (j + 1)  % 3;
                    if (map[this_node].leaf_count[first] != 0 && map[this_node].leaf_count[second] != 0) {
                        map[connected_node].leaf_count[branch] = map[this_node].leaf_count[first] + map[this_node].leaf_count[second];
                        
                        // set the node_branch information
                        int k;
                        for (k = 0; k < node_count; ++k) {
                            // node present in one of the two branches pointing away from this
                            int node_present = k==this_node || map[this_node].node_branch[k] == first || map[this_node].node_branch[k] == second;
                            
                            if (node_present) map[connected_node].node_branch[k] = branch;
                        }
                    }
                }
            }
        }
        
        for (i = 0; i < todo->len; ++i) {
            
            int this_node = g_array_index(todo, guint32, i);
             
            // we are done when all entries are set for this node, AND there are no pending assignments to neighbours

            // are all entries set for this node?
            for (j = 0; j < 3; ++j) {
                if (map[this_node].leaf_count[j] == 0) { 
                    break;
                }
            }

            // are there pending assignments? (we could do the assignments here, but that would duplicate code)
            int done = 1;
            for (j = 0; j < 3; ++j) {
                if (this_node < leaf_count) continue;
                int connected_node = map[this_node].connections[j];
                // find connection
                int branch = find_branch(map[connected_node].connections, this_node);
                if (map[connected_node].leaf_count[branch] == 0) {
                    done = 0;
                    break;
                }
            }
            
            if (done) {
                //printf("Removing node %d, counts %d %d %d\n", i, map[this_node].leaf_count[0], map[this_node].leaf_count[1], map[this_node].leaf_count[2]);
                g_array_remove_index_fast(todo, i);
                --i;
            }
        }
    }
    
    g_array_free(todo, TRUE); 
    
    int k; 
    // now fill in the distances
    for (i=leaf_count; i < node_count; ++i) {
        for (j = 0; j < leaf_count; ++j) {
            for (k = j+1; k < leaf_count; ++k) {

                int b1 = map[i].node_branch[j];
                int b2 = map[i].node_branch[k];
                
                if (b1 == b2) continue;

                int b3 = 3 - b1 - b2;

                map[i].dist[b3] += gsl_matrix_get(dm, j, k);
            }
        }
    }
    
    set_score(tree);
    
    return tree;
}
Esempio n. 24
0
/**
 * @brief Process Key Input and return the status
 *
 * @param keycode keycode from XKeyEvent
 * @param state state from XKeyEvent
 * @param count count from XKeyEvent
 * @return INPUT_RETURN_VALUE
 **/
__EXPORT_API
INPUT_RETURN_VALUE FcitxLibpinyinDoInput(void* arg, FcitxKeySym sym, unsigned int state)
{
    FcitxLibpinyin* libpinyin = (FcitxLibpinyin*) arg;
    FcitxLibpinyinConfig* config = &libpinyin->owner->config;
    FcitxInputState* input = FcitxInstanceGetInputState(libpinyin->owner->owner);

    if (FcitxHotkeyIsHotKeySimple(sym, state))
    {
        /* there is some special case that ';' is used */
        if (FcitxHotkeyIsHotKeyLAZ(sym, state)
            || sym == '\''
            || (FcitxHotkeyIsHotKey(sym, state, FCITX_SEMICOLON) && libpinyin->type == LPT_Shuangpin && (config->spScheme == FCITX_SHUANG_PIN_MS || config->spScheme == FCITX_SHUANG_PIN_ZIGUANG))
            || (libpinyin->type == LPT_Zhuyin && LibpinyinCheckZhuyinKey(sym, config->zhuyinLayout, config->useTone))
        )
        {
            if (strlen(libpinyin->buf) == 0 && (sym == '\'' || sym == ';'))
                return IRV_TO_PROCESS;

            if (strlen(libpinyin->buf) < MAX_PINYIN_INPUT)
            {
                size_t len = strlen(libpinyin->buf);
                if (libpinyin->buf[libpinyin->cursor_pos] != 0)
                {
                    memmove(libpinyin->buf + libpinyin->cursor_pos + 1, libpinyin->buf + libpinyin->cursor_pos, len - libpinyin->cursor_pos);
                }
                libpinyin->buf[len + 1] = 0;
                libpinyin->buf[libpinyin->cursor_pos] = (char) (sym & 0xff);
                libpinyin->cursor_pos ++;

                size_t parselen = FcitxLibpinyinParse(libpinyin, libpinyin->buf);

                if (parselen == 0 && strlen(libpinyin->buf) == 1 && libpinyin->type != LPT_Shuangpin
                    && !(libpinyin->type == LPT_Pinyin && !libpinyin->owner->config.incomplete)
                    && !(libpinyin->type == LPT_Zhuyin && !libpinyin->owner->config.chewingIncomplete))
                {
                    FcitxLibpinyinReset(libpinyin);
                    return IRV_TO_PROCESS;
                }
                return IRV_DISPLAY_CANDWORDS;
            }
            else
                return IRV_DO_NOTHING;
        }
    }

    if (FcitxHotkeyIsHotKey(sym, state, FCITX_SPACE)
        || (libpinyin->type == LPT_Zhuyin && FcitxHotkeyIsHotKey(sym, state, FCITX_ENTER)))
    {
        size_t len = strlen(libpinyin->buf);
        if (len == 0)
            return IRV_TO_PROCESS;

        return FcitxCandidateWordChooseByIndex(FcitxInputStateGetCandidateList(input), 0);
    }

    if (FcitxHotkeyIsHotKey(sym, state, FCITX_LIBPINYIN_SHIFT_ENTER)) {
        size_t len = strlen(libpinyin->buf);
        if (len == 0)
            return IRV_TO_PROCESS;

        strcpy(FcitxInputStateGetOutputString(input), libpinyin->buf);

        return IRV_COMMIT_STRING;
    }

    if (FcitxHotkeyIsHotKey(sym, state, FCITX_BACKSPACE) || FcitxHotkeyIsHotKey(sym, state, FCITX_DELETE))
    {
        if (strlen(libpinyin->buf) > 0)
        {
            int offset = LibpinyinGetOffset(libpinyin);
            if (offset != 0 && FcitxHotkeyIsHotKey(sym, state, FCITX_BACKSPACE))
            {
                g_array_remove_index_fast(libpinyin->fixed_string, libpinyin->fixed_string->len - 1);
                pinyin_clear_constraint(libpinyin->inst, LibpinyinGetOffset(libpinyin));
            }
            else
            {
                if (FcitxHotkeyIsHotKey(sym, state, FCITX_BACKSPACE))
                {
                    if (libpinyin->cursor_pos > 0)
                        libpinyin->cursor_pos -- ;
                    else
                        return IRV_DO_NOTHING;
                }
                size_t len = strlen(libpinyin->buf);
                if (libpinyin->cursor_pos == (int)len)
                    return IRV_DO_NOTHING;
                memmove(libpinyin->buf + libpinyin->cursor_pos, libpinyin->buf + libpinyin->cursor_pos + 1, len - libpinyin->cursor_pos - 1);
                libpinyin->buf[strlen(libpinyin->buf) - 1] = 0;
                if (libpinyin->buf[0] == '\0')
                    return IRV_CLEAN;
                else
                    FcitxLibpinyinParse(libpinyin, libpinyin->buf);
            }
            return IRV_DISPLAY_CANDWORDS;
        }
        else
            return IRV_TO_PROCESS;
    }
    else
    {
        if (strlen(libpinyin->buf) > 0)
        {
            if (FcitxHotkeyIsHotKey(sym, state, FCITX_LEFT))
            {
                if (libpinyin->cursor_pos > 0)
                {
                    if ( libpinyin->cursor_pos == LibpinyinGetPinyinOffset(libpinyin))
                    {
                        g_array_remove_index_fast(libpinyin->fixed_string, libpinyin->fixed_string->len - 1);
                        pinyin_clear_constraint(libpinyin->inst, LibpinyinGetOffset(libpinyin));
                        return IRV_DISPLAY_CANDWORDS;
                    }
                    else
                    {
                        libpinyin->cursor_pos--;
                        return IRV_DISPLAY_CANDWORDS;
                    }
                }

                return IRV_DO_NOTHING;
            }
            else if (FcitxHotkeyIsHotKey(sym, state, FCITX_RIGHT))
            {
                size_t len = strlen(libpinyin->buf);
                if (libpinyin->cursor_pos < (int) len)
                {
                    libpinyin->cursor_pos ++ ;
                    return IRV_DISPLAY_CANDWORDS;
                }
                return IRV_DO_NOTHING;
            }
            else if (FcitxHotkeyIsHotKey(sym, state, FCITX_HOME))
            {
                int offset = LibpinyinGetPinyinOffset(libpinyin);
                if ( libpinyin->cursor_pos != offset)
                {
                    libpinyin->cursor_pos = offset;
                    return IRV_DISPLAY_CANDWORDS;
                }
                return IRV_DO_NOTHING;
            }
            else if (FcitxHotkeyIsHotKey(sym, state, FCITX_END))
            {
                size_t len = strlen(libpinyin->buf);
                if (libpinyin->cursor_pos != (int) len)
                {
                    libpinyin->cursor_pos = len ;
                    return IRV_DISPLAY_CANDWORDS;
                }
                return IRV_DO_NOTHING;
            }
        }
        else {
            return IRV_TO_PROCESS;
        }
    }
    return IRV_TO_PROCESS;
}
Esempio n. 25
0
gboolean
p2tr_pslg_visibility_check (P2trPSLG    *pslg,
                            P2trVector2 *point,
                            P2trPSLG    *polygon)
{
    P2trPSLG *known_blocks;
    GArray   *second_points;
    gboolean  found_visibility_path = FALSE;

    /* W <- Some point in T (for example, center of weight) */
    P2trVector2 W;
    find_point_in_polygon (polygon, &W);

    /* KnownBlocks <- {} */
    known_blocks = p2tr_pslg_new ();

    /* SecondPoint <- {W} */
    second_points   = g_array_new (FALSE, FALSE, sizeof(P2trVector2));
    g_array_append_val (second_points, W);

    while ((! found_visibility_path) && second_points->len > 0)
    {
        P2trVector2 S;
        P2trBoundedLine PS;
        P2trVector2 poly_intersection;

        /* S <- Some point from SecondPoint */
        p2tr_vector2_copy (&S, &g_array_index(second_points, P2trVector2, 0));
        /* SecondPoint <- SecondPoint \ {S} */
        g_array_remove_index_fast (second_points, 0);

        /* PS <- The infinite line going through P and S */
        p2tr_bounded_line_init (&PS, &S, point);

        /* IF PS intersects @Poly */
        if (find_closest_intersection (polygon, &PS.infinite, point, &poly_intersection))
        {
            P2trBoundedLine PS_exact, *B;

            /* IF there is an edge B=(u,v) (from E) that intersects PS */
            p2tr_bounded_line_init (&PS_exact, point, &poly_intersection);
            B = pslg_line_intersection (pslg, &PS_exact);

            if (B != NULL)
            {
                /* IF B is not in KnownBlocks: */
                if (! p2tr_pslg_contains_line (known_blocks, B))
                {
                    /* SecondPoint <- SecondPoint + {u,v} */
                    g_array_append_val (second_points, B->start);
                    g_array_append_val (second_points, B->end);
                    /* KnownBlocks <- KnownBlocks + {B} */
                    p2tr_pslg_add_existing_line (known_blocks, B);
                }
            }
            else
            {
                found_visibility_path = TRUE;
            }
        }
    }

    g_array_free (second_points, TRUE);
    p2tr_pslg_free (known_blocks);

    return found_visibility_path;
}
Esempio n. 26
0
/**
 * Clean up any finished TID request processes
 *
 * This is called by the main process after forking each TID request. If you want to be
 * sure finished processes are cleaned up promptly even during a lull in TID requests,
 * this can be called from the main thread of the main process. It is not thread-safe,
 * so should not be used from sub-threads. It should not be called by child processes -
 * this would probably be harmless but ineffective.
 *
 * @param tids
 */
void tids_sweep_procs(TIDS_INSTANCE *tids)
{
  guint ii;
  struct tid_process tp = {0};
  char result[TIDS_MAX_MESSAGE_LEN] = {0};
  ssize_t result_len;
  int status;
  int wait_rc;

  /* loop backwards over the array so we can remove elements as we go */
  for (ii=tids->pids->len; ii > 0; ii--) {
    /* ii-1 is the current index - get our own copy, we may destroy the list's copy */
    tp = g_array_index(tids->pids, struct tid_process, ii-1);

    wait_rc = waitpid(tp.pid, &status, WNOHANG);
    if (wait_rc == 0)
      continue; /* process still running */

    if (wait_rc < 0) {
      /* invalid options will probably keep being invalid, report that condition */
      if(errno == EINVAL)
        tr_crit("tids_sweep_procs: waitpid called with invalid options");

      /* If we got ECHILD, that means the PID was invalid; we'll assume the process was
       * terminated and we missed it. For all other errors, move on
       * to the next PID to check. */
      if (errno != ECHILD)
        continue;

      tr_warning("tid_sweep_procs: TID process %d disappeared", tp.pid);
    }

    /* remove the item (we still have a copy of the data) */
    g_array_remove_index_fast(tids->pids, ii-1); /* disturbs only indices >= ii-1 which we've already handled */

    /* Report exit status unless we got ECHILD above or somehow waitpid returned the wrong pid */
    if (wait_rc == tp.pid) {
      if (WIFEXITED(status)) {
        tr_debug("tids_sweep_procs: TID process %d exited with status %d.", tp.pid, WTERMSIG(status));
      } else if (WIFSIGNALED(status)) {
        tr_debug("tids_sweep_procs: TID process %d terminated by signal %d.", tp.pid, WTERMSIG(status));
      }
    } else if (wait_rc > 0) {
      tr_err("tids_sweep_procs: waitpid returned pid %d, expected %d", wait_rc, tp.pid);
    }

    /* read the pipe - if the TID request worked, it will have written status before terminating */
    result_len = read(tp.read_fd, result, TIDS_MAX_MESSAGE_LEN);
    close(tp.read_fd);

    if ((result_len > 0) && (strcmp(result, TIDS_SUCCESS_MESSAGE) == 0)) {
      tids->req_count++;
      tr_info("tids_sweep_procs: TID process %d exited after successful request.", tp.pid);
    } else if ((result_len > 0) && (strcmp(result, TIDS_ERROR_MESSAGE) == 0)) {
      tids->req_error_count++;
      tr_info("tids_sweep_procs: TID process %d exited after unsuccessful request.", tp.pid);
    } else {
      tids->error_count++;
      tr_info("tids_sweep_procs: TID process %d exited with an error.", tp.pid);
    }
  }
}