#include "kdtreelog.h"
#include "logentry.h"
#include <GL/gl.h>
#include "bounds3d.h"
#include <stdio.h>
#include <iostream.h>
#include "kdtreefindpointvis.h"

void KDTreeFindPointVis::Display(const LogEntry* upto) const {
  KDTreeFindPoint_Entry *e = NULL;
  const LogEntry *temp = _start;
  glColor3f(0.0,1.0,0.0); // green
  while (temp) {
    if (temp == upto) break;

    e = (KDTreeFindPoint_Entry*)temp->_data;
    e->_node->_bounds.drawWireFrame();
    temp = temp->_next;
  }

  e = (KDTreeFindPoint_Entry*)upto->_data;
    
  //highlight current node
  GLfloat oldWidth;
  glGetFloatv(GL_LINE_WIDTH, &oldWidth);
  glLineWidth(oldWidth+2);
  glColor3f(1.0,0.6,0.0);  // orange
  e->_node->_bounds.drawWireFrame();
  glLineWidth(oldWidth);
  
  //draw point
  glColor3f(1.0,0.0,0.0);  // red
  float epsilon = 0.1;
  float x = e->_pt.x();
  float y = e->_pt.y();
  float z = e->_pt.z();
  glBegin(GL_LINES);
  glVertex3f(x-epsilon, y, z);
  glVertex3f(x+epsilon, y, z);
  glVertex3f(x, y-epsilon, z);
  glVertex3f(x, y+epsilon, z);
  glVertex3f(x, y, z-epsilon);
  glVertex3f(x, y, z+epsilon);
  glEnd();
}

char * KDTreeFindPointVis::StatusLine (const LogEntry *ours) const {
  //char *newline = new char [strlen (ours->_name) + 1]; 
  //strcpy (newline, ours->_name); 
  //return newline;
  KDTreeFindPoint_Entry *e = (KDTreeFindPoint_Entry*)ours->_data;

  char *newline = new char [150];
  char *specific = new char[50];
  switch (e->_ret) {
  case Parent:
    sprintf(specific, "looking in Parent");
    break;
  case Yes:
    sprintf(specific, "found");
    break;
  case Lower:
    sprintf(specific, "looking in Lower child");
    break;
  case Upper:
    sprintf(specific, "looking in Upper child");
    break;
  default:
    break;
  }
  sprintf(newline, "%s: point (%.2f,%.2f,%.2f) %s", 
	  ours->_name, e->_pt.x(), e->_pt.y(), e->_pt.z(),specific);
  return newline;
}

int KDTreeFindPointVis::IsOurs (const LogEntry *en) const {
  int answer = 0;
  const LogEntry *temp = _start;
  while (temp) {
    if (temp == en) {
      answer = 1;
      break;
    }
    if (temp == _end) break;
    temp = temp->_next;
  }
  return answer;
}

int KDTreeFindPointVis::isAparentofB(const LogEntry *A, const LogEntry *B) {
  switch (A->_event) {
  case KDTreeFindPoint:
    switch (B->_event) {
    case KDTreeFindPoint:
      KDTreeFindPoint_Entry *ea = (KDTreeFindPoint_Entry*)A->_data;
      KDTreeFindPoint_Entry *eb = (KDTreeFindPoint_Entry*)B->_prev->_data;
      if (ea->_node == eb->_node)
	return 1;
      break;
    default:
      break;
    }
    break;
  default:
    break;
  }
  return 0;
}



