Exemplo n.º 1
0
/**
 *	@brief Handle nunchuk event.
 *
 *	@param nc		A pointer to a nunchuk_t structure.
 *	@param msg		The message specified in the event packet.
 */
void nunchuk_event(struct nunchuk_t* nc, byte* msg) {
	int i;

	/* decrypt data */
	for (i = 0; i < 6; ++i)
		msg[i] = (msg[i] ^ 0x17) + 0x17;

	/* get button states */
	nunchuk_pressed_buttons(nc, msg[5]);

	/* calculate joystick state */
	calc_joystick_state(&nc->js, msg[0], msg[1]);

	/* calculate orientation */
	nc->accel.x = msg[2];
	nc->accel.y = msg[3];
	nc->accel.z = msg[4];

	calculate_orientation(&nc->accel_calib, &nc->accel, &nc->orient, NUNCHUK_IS_FLAG_SET(nc, WIIUSE_SMOOTHING));
	calculate_gforce(&nc->accel_calib, &nc->accel, &nc->gforce);
}
// Update Motion History Image: Calculate motion features and orientation.
void motionDetection(IplImage* image, IplImage* destination_image, MotionInfo* motionInfo)
{
    double timestamp = (double)clock()/CLOCKS_PER_SEC; // get current time in seconds
    CvSize image_size = cvSize(image->width, image->height); // get current frame image_size
    int previous_frame_index = last_index, current_frame_index;
    
    initialize_images(image_size);
    
    cvCvtColor(image, image_buffer[last_index], CV_BGR2GRAY); // convert frame to grayscale
    
    current_frame_index = (last_index + 1) % N; // index of (last_index - (N-1))th frame
    last_index = current_frame_index;
    
    silhouette = image_buffer[current_frame_index];
    
    cvAbsDiff(image_buffer[previous_frame_index], image_buffer[current_frame_index], silhouette); // Get difference between frames
    cvThreshold(silhouette, silhouette, DIFFERENCE_THRESHOLD, 1, CV_THRESH_BINARY); // Add threshold
    //cvDilate(silhouette, silhouette, 0, 18);
    //cvErode(silhouette, silhouette, 0, 10);
    
    cvUpdateMotionHistory(silhouette, mhi, timestamp, MHI_DURATION); // Update MHI
    
    // Convert MHI to blue 8U image
    cvCvtScale(mhi, orientation_mask, 255./MHI_DURATION, (MHI_DURATION - timestamp)*255./MHI_DURATION);
    
    if (destination_image) {
      cvZero(destination_image);
      cvCvtPlaneToPix(orientation_mask, 0, 0, 0, destination_image);
    }
    
    // Calculate motion gradient orientation and valid orientation mask
    cvCalcMotionGradient(mhi, orientation_mask, orientation, MAX_TIME_DELTA, MIN_TIME_DELTA, 3);
    
    // motion_feature_sequence = extract_motion_features();
    if(!storage)
        storage = cvCreateMemStorage(0);
    else
        cvClearMemStorage(storage);
    
    CvSeq* motion_feature_sequence = cvSegmentMotion(mhi, segment_mask, storage, timestamp, MAX_TIME_DELTA);
    
    int SEGMENT_WIDTH = image_size.width / MAX_SEGMENTS_X;
    int SEGMENT_HEIGHT = image_size.height / MAX_SEGMENTS_Y;
    
    // Global motion
    CvRect global_motion_segment = cvRect(0, 0, image_size.width, image_size.height);
    motionInfo->global_angle = calculate_orientation(global_motion_segment, silhouette);
    
    if (destination_image)
      draw_orientation(destination_image, &global_motion_segment, motionInfo->global_angle, 100, CV_RGB(0, 255, 0), true);
    
    long area = 0;
    long totalArea = 0;
    int totalMovingSegments = 0;
    bool hasValidMovement = false;
    CvRect segmentRect;
    
    // Segmented motion
    for(int x = 0; x < MAX_SEGMENTS_X; x++)
    {
      for(int y = 0; y < MAX_SEGMENTS_Y; y++)
      {
        segmentRect = cvRect(x * SEGMENT_WIDTH, y * SEGMENT_HEIGHT, SEGMENT_WIDTH, SEGMENT_HEIGHT);
        area = calculate_motion(&segmentRect, motion_feature_sequence);
        hasValidMovement = (area > MIN_MOTION_FEATURE_AREA);
        
        motionInfo->segment_motion_areas[x][y] = area;
        motionInfo->segment_movements[x][y] = hasValidMovement;
        motionInfo->segment_angles[x][y] = calculate_orientation(segmentRect, silhouette);
        
        totalArea += area;
        totalMovingSegments += (area > MIN_MOTION_FEATURE_AREA);
        
        //printf("%i, ", area);
        //fflush(stdout);
        
        if (hasValidMovement)
          if (destination_image)
            draw_orientation(destination_image, &segmentRect, motionInfo->segment_angles[x][y], 20, CV_RGB(255, 0, 0), true);
      }
    }
    motionInfo->total_motion_area = totalArea;
    motionInfo->total_segments_with_movements = totalMovingSegments;
    motionInfo->SEGMENTS_X = MAX_SEGMENTS_X;
    motionInfo->SEGMENTS_Y = MAX_SEGMENTS_Y;
    
    printf("%i, %f\n", totalArea, (float)totalArea / (float)(image_size.width*image_size.height));
    //fflush(stdout);
}
Exemplo n.º 3
0
void cDlgOptions::create_hardpoints(HTREEITEM item)
{
	HTREEITEM fixed = utf->AddNewNode(tree, item, "Fixed");
	HTREEITEM revolute = utf->AddNewNode(tree, item, "Revolute");

	// iterate mesh list and create hardpoints
	list<MMESH *>::iterator i;
	for (i = meshList->begin(); i != meshList->end(); i ++)
	{
		MMESH * m = *i;
		char * hp_name = 0;

		// Hardpoint|Fixed|blah or Hardpoint|Revolute|blah
		if ( !_strnicmp(m->nname, "Hp", 2) )
		{
			HTREEITEM parent = 0;
			if ( !_strnicmp (m->nname + 3, "Fixed", 5) )
			{
				parent = fixed;
				hp_name = m->nname + 9;	// pointer to name Hp|Fixed|name
			}
			else if ( !_strnicmp (m->nname + 3, "Revolute", 8) )
			{
				parent = revolute;
				hp_name = m->nname + 12;	// pointer to name Hp|Revolute|name
			}
			else 
				continue;

 			HTREEITEM new_hp = utf->AddNewNode(tree, parent, hp_name);

				// only revolute hardpoints have these
				if (parent == revolute)
				{
					HTREEITEM axis = utf->AddNewNode(tree, new_hp, "Axis");
						float * axis_data = (float *) malloc (12 + 4);
						*(int *) axis_data = 12;
						axis_data[1] = 0.0f;
						axis_data[2] = 1.0f;
						axis_data[3] = 0.0f;
						tree->SetItemData(axis, (DWORD_PTR)axis_data);

					HTREEITEM max = utf->AddNewNode(tree, new_hp, "Max");
						float * max_data = (float *) malloc (8 + 4);
						*(int *) max_data = 8;
						max_data[1] = 0.785380f;
						max_data[2] = 0.0f;
						tree->SetItemData(max, (DWORD_PTR)max_data);

					HTREEITEM min = utf->AddNewNode(tree, new_hp, "Min");
						float * min_data = (float *) malloc (8 + 4);
						*(int *) min_data = 8;
						min_data[1] = -0.785400f;
						min_data[2] = 0.0f;
						tree->SetItemData(min, (DWORD_PTR)min_data);
				}

				// both fixed and revolute hardpoints have Orientation and Position
						float * position_data = (float *) malloc (12 + 4);
						*(int *) position_data = 12;
						calculate_position( position_data + 1, m);

						float * orientation_data = (float *) malloc (36 + 4);
						*(int *) orientation_data = 36;
						calculate_orientation( orientation_data + 1, position_data + 1, hp_name);

					HTREEITEM orientation = utf->AddNewNode(tree, new_hp, "Orientation");
						tree->SetItemData(orientation, (DWORD_PTR) orientation_data);

					HTREEITEM position = utf->AddNewNode(tree, new_hp, "Position");
						tree->SetItemData(position, (DWORD_PTR) position_data);
		}
	}
}