//
// General purpose utility functions
// 
//
//
//
//
//
#include <stdlib.h>
#include <GL/gl.h>


#define MIN(x,y) ((x < y) ? x : y)




/* 
 *
 * drawBox:
 *
 * draws a rectangular box with the given x, y, and z ranges.  
 * The box is axis-aligned.
 *
 * from OpenGL red book's libaux
 *
 */
void 
drawBox( GLdouble x0, GLdouble x1, GLdouble y0, GLdouble y1,
	 GLdouble z0, GLdouble z1, GLenum type)
{
    static GLdouble n[6][3] = {
        {-1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}, {1.0, 0.0, 0.0},
        {0.0, -1.0, 0.0}, {0.0, 0.0, 1.0}, {0.0, 0.0, -1.0}
    };
    static GLint faces[6][4] = {
        { 0, 1, 2, 3 }, { 3, 2, 6, 7 }, { 7, 6, 5, 4 },
        { 4, 5, 1, 0 }, { 5, 6, 2, 1 }, { 7, 4, 0, 3 }
    };
    GLdouble v[8][3], tmp;
    GLint i;

    if (x0 > x1) {
        tmp = x0; x0 = x1; x1 = tmp;
    }
    if (y0 > y1) {
        tmp = y0; y0 = y1; y1 = tmp; 
    }
    if (z0 > z1) {
        tmp = z0; z0 = z1; z1 = tmp; 
    }
    v[0][0] = v[1][0] = v[2][0] = v[3][0] = x0;
    v[4][0] = v[5][0] = v[6][0] = v[7][0] = x1;
    v[0][1] = v[1][1] = v[4][1] = v[5][1] = y0;
    v[2][1] = v[3][1] = v[6][1] = v[7][1] = y1;
    v[0][2] = v[3][2] = v[4][2] = v[7][2] = z0;
    v[1][2] = v[2][2] = v[5][2] = v[6][2] = z1;

    for (i = 0; i < 6; i++) {
        glBegin(type);
        glNormal3dv(&n[i][0]);
        glVertex3dv(&v[faces[i][0]][0]);
        glNormal3dv(&n[i][0]);
        glVertex3dv(&v[faces[i][1]][0]);
        glNormal3dv(&n[i][0]);
        glVertex3dv(&v[faces[i][2]][0]);
        glNormal3dv(&n[i][0]);
        glVertex3dv(&v[faces[i][3]][0]);
        glEnd();
    }
}

void
drawSegment ( float *p, float *q )
{
float r[3];

  int lon;
  if ( lon = glIsEnabled (GL_LIGHTING) )
	glDisable( GL_LIGHTING ); 
  glLineWidth( 2 );
  glBegin( GL_LINES );
  glColor3f( 1.f, 0.5f, 0.f ); // orange
  glVertex3fv( p );
  glColor3f( 0.5f, 0.5f, 0.5f ); // grey
  r[0] = p[0] + 10 * (q[0] - p[0]);
  r[1] = p[1] + 10 * (q[1] - p[1]);
  r[2] = p[2] + 10 * (q[2] - p[2]);
  glVertex3fv( r );
  glEnd();
  glLineWidth( 1 ) ;
  if ( lon )
	glEnable( GL_LIGHTING ); 
}
