/* Remove the repetitive matches that appear in different simulations and retain only one */
void unique_match1(matchingslist &seg_in, matchingslist &seg_out, vector< vector <float> > &Minfoall_in, vector< vector <float> > &Minfoall_out)
{
  int i_in, i_out;
  float x1_in, x2_in, y1_in, y2_in, x1_out, x2_out, y1_out, y2_out;
  int flag_unique;
  float d1, d2;
  int Th2 = 2;

  seg_out.push_back(seg_in[0]);
  Minfoall_out.push_back(Minfoall_in[0]);

  /* For other matches */
  if ( seg_in.size() > 1 )
    {
      /* check if a match is unique. if yes, copy */
		matchingslist::iterator ptr_in = seg_in.begin();
		for ( i_in = 1; i_in < (int) seg_in.size(); i_in++, ptr_in++ )
			{
			  x1_in = ptr_in->first.x;
			  y1_in = ptr_in->first.y;
			  x2_in = ptr_in->second.x;
			  y2_in = ptr_in->second.y;

			  flag_unique = 1;

			  matchingslist::iterator ptr_out = seg_out.begin();
			  for ( i_out = 0; i_out < (int) seg_out.size(); i_out++, ptr_out++ )
				{
					x1_out = ptr_out->first.x;
					y1_out = ptr_out->first.y;
					x2_out = ptr_out->second.x;
					y2_out = ptr_out->second.y;

					d1 = (x1_in - x1_out)*(x1_in - x1_out) + (y1_in - y1_out)*(y1_in - y1_out);
					d2 = (x2_in - x2_out)*(x2_in - x2_out) + (y2_in - y2_out)*(y2_in - y2_out);


					if ( ( d1 <= Th2) && ( d2 <= Th2) )
					 {
					   flag_unique = 0;
					   continue;
					 }
				}

			  if ( flag_unique == 1 )
				{
					seg_out.push_back(seg_in[i_in]);
					Minfoall_out.push_back(Minfoall_in[i_in]);
				}
		}
    }
}
/* Remove the ALL multiple-to-one matches */
void clean_match2(matchingslist &seg_in, matchingslist &seg_out, vector< vector <float> > &Minfoall_in, vector< vector <float> > &Minfoall_out)
{
	int i1, i2;
	float x1_in, x2_in, y1_in, y2_in, x1_out, x2_out, y1_out, y2_out;

  // Guoshen Yu, 2010.09.22, Windows version
  // int flag_unique[seg_in.size()];
  int tmp_size = seg_in.size();
  int *flag_unique = new int[tmp_size];

	int sum_flag=0;
	float d1, d2;
	int Th1 = 1;
	int Th2 = 4;

	for ( i1 = 0; i1 < (int) seg_in.size(); i1++ )
    {
      flag_unique[i1] = 1;
    }

	/* Set the flag of redundant matches to 0. */
	matchingslist::iterator ptr_in = seg_in.begin();
	for ( i1 = 0; i1 < (int) seg_in.size() - 1; i1++, ptr_in++ )
	{
		x1_in = ptr_in->first.x;
		y1_in = ptr_in->first.y;
		x2_in = ptr_in->second.x;
		y2_in = ptr_in->second.y;

		matchingslist::iterator ptr_out = ptr_in+1;
		for ( i2 = i1 + 1; i2 < (int) seg_in.size(); i2++, ptr_out++ )
		{

		  x1_out = ptr_out->first.x;
		  y1_out = ptr_out->first.y;
		  x2_out = ptr_out->second.x;
		  y2_out = ptr_out->second.y;

		  d1 = (x1_in - x1_out)*(x1_in - x1_out) + (y1_in - y1_out)*(y1_in - y1_out);
		  d2 = (x2_in - x2_out)*(x2_in - x2_out) + (y2_in - y2_out)*(y2_in - y2_out);


		  /* If redundant, set flags of both elements to 0.*/
		  if ( ( d1 > Th2) && ( d2 <= Th1) )
			{
			  flag_unique[i1] = 0;
			  flag_unique[i2] = 0;
			}
		}
	}

  for ( i1 = 0; i1 < (int) seg_in.size(); i1++ )
  {
	  sum_flag += flag_unique[i1];
  }

  /* Copy the matches that are not redundant */
  if ( sum_flag > 0 )
  {
	  for (  i1 = 0; i1 < (int) seg_in.size(); i1++ )
	  {
		  if ( flag_unique[i1] == 1 )
		  {
				seg_out.push_back(seg_in[i1]);
				Minfoall_out.push_back(Minfoall_in[i1]);
		  }
	  }
  }
  else
  {
      printf("Warning: all matches are redundant and are thus removed! This step of match cleaning is short circuited. (Normally this should not happen...)\n");
  }

  // Guoshen Yu, 2010.09.22, Windows version
  delete [] flag_unique;
}