visionTrack.c 7.33 KB
Newer Older
Timothy Stack's avatar
 
Timothy Stack committed
1 2 3 4 5 6 7 8 9 10 11

#include "config.h"

#include <math.h>
#include <float.h>
#include <assert.h>

#include "log.h"

#include "visionTrack.h"

Timothy Stack's avatar
Timothy Stack committed
12 13 14 15 16 17 18 19 20
/**
 * Find the track in a list that is the minimum distance from a given track.
 *
 * @param vt The track to compare against the other tracks in the list.
 * @param curr The track in a list where the search should start.  The function
 * will continue the search until it reaches the end of the list.
 * @param distance_out Reference to a float where the minimum distance found
 * should be stored.
 */
Timothy Stack's avatar
 
Timothy Stack committed
21
static struct vision_track *vtFindMin(struct vision_track *vt,
Timothy Stack's avatar
Timothy Stack committed
22
				      struct vision_track *curr,
Timothy Stack's avatar
 
Timothy Stack committed
23 24
				      float *distance_out)
{
Timothy Stack's avatar
Timothy Stack committed
25
    struct vision_track *retval = NULL;
Timothy Stack's avatar
 
Timothy Stack committed
26 27

    assert(vt != NULL);
Timothy Stack's avatar
Timothy Stack committed
28
    assert(curr != NULL);
Timothy Stack's avatar
 
Timothy Stack committed
29 30 31 32 33 34 35 36 37
    assert(distance_out != NULL);

    *distance_out = FLT_MAX;

    while (curr->vt_link.ln_Succ != NULL) {
	float distance;

	distance = hypotf(vt->vt_position.x - curr->vt_position.x,
			  vt->vt_position.y - curr->vt_position.y);
Timothy Stack's avatar
Timothy Stack committed
38 39 40 41 42 43
	printf("  min %f %f - %f %f = %f\n",
	       vt->vt_position.x,
	       vt->vt_position.y,
	       curr->vt_position.x,
	       curr->vt_position.y,
	       distance);
Timothy Stack's avatar
 
Timothy Stack committed
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
	if (distance < *distance_out) {
	    retval = curr;
	    *distance_out = distance;
	}
	
	curr = (struct vision_track *)curr->vt_link.ln_Succ;
    }
    
    return retval;
}

int vtUpdate(struct lnMinList *now,
	     struct vmc_client *vc,
	     struct mtp_packet *mp,
	     struct lnMinList *pool)
{
    int retval = 0;
    
    assert(now != NULL);
    assert(mp != NULL);

    switch (mp->data.opcode) {
    case MTP_CONTROL_ERROR:
	{
	    struct mtp_control *mc;

	    mc = &mp->data.mtp_payload_u.error;
	    
	    retval = (mc->code == MTP_POSITION_STATUS_CYCLE_COMPLETE);
	}
	break;
    case MTP_UPDATE_POSITION:
	{
	    struct mtp_update_position *mup;
	    struct vision_track *vt;
	    
	    mup = &mp->data.mtp_payload_u.update_position;
	    vt = (struct vision_track *)lnRemHead(pool);
	    assert(vt != NULL);

	    vt->vt_position = mup->position;
Timothy Stack's avatar
 
Timothy Stack committed
85
	    vt->vt_client = vc;
Timothy Stack's avatar
 
Timothy Stack committed
86 87 88 89
	    vt->vt_age = 0;
	    vt->vt_userdata = NULL;
	    lnAddTail(now, &vt->vt_link);

Timothy Stack's avatar
Timothy Stack committed
90
	    /* Adjust the cameras viewable area based on this track. */
Timothy Stack's avatar
 
Timothy Stack committed
91 92 93 94 95 96 97 98
	    if (mup->position.x < vc->vc_left)
		vc->vc_left = mup->position.x;
	    if (mup->position.x > vc->vc_right)
		vc->vc_right = mup->position.x;
	    if (mup->position.y < vc->vc_top)
		vc->vc_top = mup->position.y;
	    if (mup->position.y > vc->vc_bottom)
		vc->vc_bottom = mup->position.y;
Timothy Stack's avatar
 
Timothy Stack committed
99 100 101 102 103 104 105 106

	    
	    printf("vc %p %f %f %f %f\n",
		   vc,
		   vc->vc_left,
		   vc->vc_right,
		   vc->vc_top,
		   vc->vc_bottom);
Timothy Stack's avatar
 
Timothy Stack committed
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
	    
	    retval = (mup->status == MTP_POSITION_STATUS_CYCLE_COMPLETE);
	}
	break;
    default:
	error("unhandled vmc-client packet %d\n", mp->data.opcode);
	break;
    }
    
    return retval;
}

void vtCoalesce(struct lnMinList *extra,
		struct lnMinList *now,
		struct vmc_client *vc,
		unsigned int vc_len)
{
    struct vision_track *vt;

    assert(extra != NULL);
    assert(now != NULL);
    assert(vc != NULL);
    assert(vc_len > 0);

    vt = (struct vision_track *)now->lh_Head;
    while (vt->vt_link.ln_Succ != NULL) {
	int in_camera_count = 0, lpc;
Timothy Stack's avatar
Timothy Stack committed
134 135 136 137 138

	/*
	 * Figure out how many cameras this track might be viewable in so we
	 * can coalesce them.
	 */
Timothy Stack's avatar
 
Timothy Stack committed
139
	for (lpc = 0; lpc < vc_len; lpc++) {
Timothy Stack's avatar
 
Timothy Stack committed
140 141 142 143 144 145 146 147 148
#if 0
	    printf("vc2 %f %f -- %f %f %f %f\n",
		   vt->vt_position.x,
		   vt->vt_position.y,
		   vc[lpc].vc_left,
		   vc[lpc].vc_right,
		   vc[lpc].vc_top,
		   vc[lpc].vc_bottom);
#endif
Timothy Stack's avatar
 
Timothy Stack committed
149 150 151 152 153 154 155 156 157 158 159
	    if ((vt->vt_position.x >= vc[lpc].vc_left) &&
		(vt->vt_position.x <= vc[lpc].vc_right) &&
		(vt->vt_position.y >= vc[lpc].vc_top) &&
		(vt->vt_position.y <= vc[lpc].vc_bottom)) {
		in_camera_count += 1;
	    }
	}

	assert(in_camera_count > 0);

	if (in_camera_count > 1) {
Timothy Stack's avatar
Timothy Stack committed
160 161
	    struct vision_track *vt_extra;
	    float distance;
Timothy Stack's avatar
 
Timothy Stack committed
162
	    
Timothy Stack's avatar
Timothy Stack committed
163 164 165 166 167 168 169
	    while ((in_camera_count > 1) &&
		   ((vt_extra = vtFindMin(vt,
					  (struct vision_track *)
					  vt->vt_link.ln_Succ,
					  &distance)) != NULL) &&
		   (distance < 0.35)) {
		printf("coalesce %f %f  --  %f %f\n",
Timothy Stack's avatar
 
Timothy Stack committed
170
		       vt->vt_position.x, vt->vt_position.y,
Timothy Stack's avatar
Timothy Stack committed
171
		       vt_extra->vt_position.x, vt_extra->vt_position.y);
Timothy Stack's avatar
 
Timothy Stack committed
172
		
Timothy Stack's avatar
Timothy Stack committed
173 174 175 176
		lnRemove(&vt_extra->vt_link);
		lnAddHead(extra, &vt_extra->vt_link);

		in_camera_count -= 1;
Timothy Stack's avatar
 
Timothy Stack committed
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
	    }
	}

	vt = (struct vision_track *)vt->vt_link.ln_Succ;
    }
}

void vtMatch(struct lnMinList *pool,
	     struct lnMinList *prev,
	     struct lnMinList *now)
{
    struct vision_track *vt;

    assert(pool != NULL);
    assert(prev != NULL);
    assert(now != NULL);

Timothy Stack's avatar
Timothy Stack committed
194 195 196 197
    /*
     * Walk through the tracks in the current frame trying to find tracks in
     * the previous frame, within some threshold.
     */
Timothy Stack's avatar
 
Timothy Stack committed
198 199 200 201 202
    vt = (struct vision_track *)now->lh_Head;
    while (vt->vt_link.ln_Succ != NULL) {
	struct vision_track *vt_prev;
	float distance;
	
Timothy Stack's avatar
Timothy Stack committed
203 204 205
	if ((vt_prev = vtFindMin(vt,
				 (struct vision_track *)prev->lh_Head,
				 &distance)) != NULL) {
Timothy Stack's avatar
 
Timothy Stack committed
206
	    if (distance < 0.30) {
Timothy Stack's avatar
 
Timothy Stack committed
207 208 209 210 211 212 213 214 215
		vt->vt_userdata = vt_prev->vt_userdata;
		lnRemove(&vt_prev->vt_link);
		lnAddHead(pool, &vt_prev->vt_link);
	    }
	}
	
	vt = (struct vision_track *)vt->vt_link.ln_Succ;
    }

Timothy Stack's avatar
Timothy Stack committed
216 217 218 219
    /*
     * Walk through the previous frame copying any young tracks to the current
     * frame.
     */
Timothy Stack's avatar
 
Timothy Stack committed
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
    vt = (struct vision_track *)prev->lh_Head;
    while (vt->vt_link.ln_Succ != NULL) {
	struct vision_track *vt_next;

	vt_next = (struct vision_track *)vt->vt_link.ln_Succ;
	if (vt->vt_age < 5) {
	    vt->vt_age += 1;
	    lnRemove(&vt->vt_link);
	    lnAddHead(now, &vt->vt_link);
	}

	vt = vt_next;
    }
}

void vtUnknownTracks(struct lnMinList *unknown, struct lnMinList *mixed)
{
    struct vision_track *vt;

    assert(unknown != NULL);
    assert(mixed != NULL);
    
    vt = (struct vision_track *)mixed->lh_Head;
    while (vt->vt_link.ln_Succ != NULL) {
	struct vision_track *vt_succ;
	
	vt_succ = (struct vision_track *)vt->vt_link.ln_Succ;
	
	if (vt->vt_userdata == NULL) {
	    lnRemove(&vt->vt_link);
	    lnAddTail(unknown, &vt->vt_link);
	}
	
	vt = vt_succ;
    }
}

struct vision_track *vtFindWiggle(struct lnMinList *start,
				  struct lnMinList *now)
{
    struct vision_track *vt, *retval = NULL;

    assert(start != NULL);
    assert(now != NULL);

Timothy Stack's avatar
Timothy Stack committed
265 266 267 268
    /*
     * Walk through the current frame searching for tracks that have a small
     * displacement from the start frame and large orientation change.
     */
Timothy Stack's avatar
 
Timothy Stack committed
269 270 271 272
    vt = (struct vision_track *)now->lh_Head;
    while ((vt->vt_link.ln_Succ != NULL) && (retval == NULL)) {
	struct vision_track *vt_start;
	float distance;
Timothy Stack's avatar
Timothy Stack committed
273 274 275 276 277 278 279 280

	printf("check %f %f %f\n",
	       vt->vt_position.x,
	       vt->vt_position.y,
	       vt->vt_position.theta);
	if ((vt_start = vtFindMin(vt,
				  (struct vision_track *)start->lh_Head,
				  &distance)) == NULL) {
Timothy Stack's avatar
 
Timothy Stack committed
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
	}
	else if (distance < 0.05) {
	    float diff;

	    diff = mtp_theta(mtp_theta(vt_start->vt_position.theta) -
			     mtp_theta(vt->vt_position.theta));
	    printf("diff = %f  %f - %f\n",
		   diff,
		   mtp_theta(vt_start->vt_position.theta),
		   mtp_theta(vt->vt_position.theta));
	    printf("diff2 = %f  %f - %f\n",
		   mtp_theta(vt_start->vt_position.theta) -
		   mtp_theta(vt->vt_position.theta),
		   vt_start->vt_position.theta,
		   vt->vt_position.theta);
	    if (diff > (M_PI - M_PI_4) || (diff < (-M_PI + M_PI_4))) {
		retval = vt;
Timothy Stack's avatar
 
Timothy Stack committed
298
		printf(" found it %p\n", retval);
Timothy Stack's avatar
 
Timothy Stack committed
299 300
	    }
	}
Timothy Stack's avatar
Timothy Stack committed
301 302
	printf("dist %p %f\n", vt_start, distance);
	
Timothy Stack's avatar
 
Timothy Stack committed
303 304 305 306 307
	vt = (struct vision_track *)vt->vt_link.ln_Succ;
    }
    
    return retval;
}