// file: $USFS/util/error_eval/v6.1/error_proc_02.cc
//

// system include files
//
#include <string.h>

// isip include files
//
#include "error_eval.h"
#include "error_eval_constants.h"

// function: score_match_cc
//
// arguments:
//  int_4* line_ref: (input) pointer to the reference line data array
//  int_4 num_coord_ref: (input) number of coordinate data in the reference
//                       data array
//  FILE* fp_res: (input) pointer to the result file
//  int_4& count_correct: (output) number of match lines
//  int_4& count_error: (output) number of errors
//  int_4& num_res_lines: (output) number of lines in the result file
//
// return:
//  a logical_1 value indicating status
//
// this function searches in a detected line data file, finds the best
// match line for a given reference line, and scores that best match. if
// there is no match line found for the given reference line, it is a "no
// match error." if there is a match line but the match line displays
// significant discrepancy to the reference line in location, slope or
// length, it is a "match error."
//
logical_1 score_match_cc(int_4* line_ref_a, int_4 num_coord_ref_a,
			 FILE* fp_res_a, int_4& count_correct_a,
			 int_4& count_error_a, int_4& num_res_lines_a) {

  // declare local variables
  //
  int_4 count = 0, count_data = 0, n = 0;
  logical_1 end_of_loop = ISIP_FALSE;
  logical_1 found_match = ISIP_FALSE;

  char_1* str_res = new char_1[ISIP_MAX_STRING_LENGTH];
  memset(str_res, 0, ISIP_MAX_STRING_LENGTH);

  int_4* line_res = new int_4[EE_MAX_NUM_COORDINATES];
  memset(line_res, 0, sizeof(int_4) * EE_MAX_NUM_COORDINATES);

  float_8 dist = 0, angle = 0, len_dif = 0;

  num_res_lines_a = 0;

  // reset the file pointer to the beginning of the file
  //
  rewind(fp_res_a);
  
  // loop over the detected lines and compute various parameters
  //
  while (end_of_loop == ISIP_FALSE) {

    // check for the end of file
    //
    if ((n = fscanf(fp_res_a, "%s", (char*)str_res)) == 0) {
      fprintf(stdout, "score_match_cc: error reading data file\n");
      return ISIP_FALSE;
    }
    else if (n == EOF) {
      end_of_loop = ISIP_TRUE;
    }
      
    // check for valid data
    //
    if (count == 0) {

      // check for the "Class" label
      //
      if (strcmp((char*)str_res, "Class") == 0) {
	count++;
      }
    }
    else if (count == 1) {

      // check for the "longline" label
      //
      if (strcmp((char*)str_res, "longline") == 0) {
	count++;
        count_data = 0;
      }
      else {
        count = 0;
      }
    }
    else if (count == 2) {

      // check for the end of line data
      //
      if (strcmp((char*)str_res, "Class") == 0 || end_of_loop == ISIP_TRUE) {

        // end of this line
        //
        count = 1;

        if (count_data != 0) {

	  if (found_match == ISIP_FALSE) {
	    
	    // compute distance, angle and length
	    //
	    if (compu_param_cc(line_ref_a, num_coord_ref_a, line_res,
			       count_data, dist, angle, len_dif)
		== ISIP_FALSE) {
	      fprintf(stdout, "score_match_cc: error computing parameters\n");
	      return ISIP_FALSE;
	    }
	  
	    // check for the match line
	    //
	    if (dist != -1) {
	      if ((dist <= EE_MAX_DIST) && (angle <= EE_MAX_ANGLE) &&
		  (len_dif <= EE_MAX_LENGTH_DIF)) {

		// found match for the reference line
		//
		count_correct_a++;
		found_match = ISIP_TRUE;
	      }
	    }
	  }
	  
          // prepare for the next line
          //
          memset(line_res, 0, sizeof(int_4) * EE_MAX_NUM_COORDINATES);
	  num_res_lines_a++;
        }
      }
      else {

        // line data continues, record it, and adjust the data count
        //
        line_res[count_data] = (int_4)atoi((char*)str_res);
        count_data++;
      }	
    }

    // prepare memory for the next fetch
    //
    memset(str_res, 0, ISIP_MAX_STRING_LENGTH);
  }

  // if no match for this reference line
  //
  if (found_match == ISIP_FALSE) {
    count_error_a++;
  }
				  
  // release memory
  //
  delete [] str_res;
  delete [] line_res;
  
  // exit gracefully
  //
  return ISIP_TRUE;
}
