// file: $USFS/class/image_analysis/v6.1/imaga_seg_06.cc
//

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

// isip include files
//
#include "image_analysis.h"
#include <link_list.h>
#include <polygon.h>

#include "image_analysis_constants.h"

// method: is_inside_cc
//
// arguments:
//  int_4 x_val: (input) x coordinate for the pixel
//  int_4 y_val: (input) y coordinate for the pixel
//  char_1*& trans: (output) transcription of the polygon into which the
//                  pixel falls
//
// return:
//  a logical_1 value indicating if the given pixel is inside the prescribed
// region or not
//
// this method traverses the linked list of polygon_list_d to check if the
// given pixel falls into any of the polygons and to find out the corresponding
// transcription
//
logical_1 Image_analysis::is_inside_cc(int_4 x_val_a, int_4 y_val_a,
				       char_1*& trans_a) {

  // declare local variables
  //
  logical_1 inside = ISIP_FALSE;
  Link_node* curr_node = (Link_node*)NULL;
  Link_node* next_node = (Link_node*)NULL;
  Polygon* polygon = (Polygon*)NULL;
  char_1* trans = (char_1*)NULL;
  
  // loop over the linked list
  //
  for (curr_node = polygon_list_d->get_head_cc();
       curr_node != (Link_node*)NULL; curr_node = next_node) {
    next_node = curr_node->get_next_cc();
    polygon = (Polygon*)(curr_node->get_item_cc()); 
    
    // check if the pixel is inside the polygon
    //
    if (polygon->is_inside_cc(x_val_a, y_val_a) == ISIP_TRUE) {

      // get the transcription of the polygon
      //
      trans = polygon->get_trans_cc();

      // allocate memory for trans_a
      //
      if (trans_a != (char_1*)NULL) {
	delete [] trans_a;
      }
      trans_a = new char_1[strlen((char*)trans) + 1];
      memset((char*)trans_a, 0, strlen((char*)trans_a) + 1);

      // set trans_a
      //
      strcpy((char*)trans_a, (char*)trans);

      // set the flag and return
      //
      inside = ISIP_TRUE;
      return inside;
    }
  }
    
  // exit gracefully
  //
  return inside;
}
