
/* Generated by Interface Builder */

#import "CubeView.h"
#import "dotAt.h"
#import <stdlib.h>
#import <strings.h>
#import <stdio.h>
#import <dpsclient/wraps.h>
#import <dpsclient/dpsNeXT.h>
#import <appkit/NXBitmapImageRep.h>
#import <appkit/Control.h>
#import <appkit/Form.h>
#import <appkit/OpenPanel.h>
#import <appkit/Pasteboard.h>
#import <appkit/Application.h>
#import <streams/streams.h>
#import <math.h>
#define MAXLINE 10000
#define MAXNUM  10000
#define rad_to_deg  (180./M_PI)
#define TRUE 0
#define FALSE !TRUE
#define defRadius 0.01
extern id NXApp;

@implementation CubeView

- initFrame:(const NXRect *) frameRect
{
  self = [super initFrame: frameRect];
  [self initialize];
	[self setCube:YES];
	[self setAxes:NO];
  return self;
}

- initialize
{
  [self setDrawSize:(NXCoord) 2:(NXCoord) 2];
  [self setDrawOrigin:(NXCoord) -1:(NXCoord) -1];
  PSonly = NO;
  vm = [AzimuthMat new];
				// establish the ops array and
				// bounding box for DPSDoUserPath()
  ops[0] = dps_moveto; ops[1] = 32 + 15; ops[2] = dps_lineto;
  boundingBox[0] = bounds.origin.x;
  boundingBox[1] = bounds.origin.y;
  boundingBox[2] = bounds.origin.x + bounds.size.width;
  boundingBox[3] = bounds.origin.y + bounds.size.height;
/*
  [self setAutosizing:NX_WIDTHSIZABLE || NX_HEIGHTSIZABLE];
*/
/*
  [self reScale:self];
*/
  return self;
}

- showError:(char *)errorMessage
{
  NXRunAlertPanel("Error", errorMessage, "OK", NULL, NULL);
  return self;
}

- clear:sender
{
  [self reScale:self];
  [self Reset:self];
  [self display];
  return self;

}

- Reset:sender
{
  [vm setTheta:0.];
  [vm setPhi:0.];
  [vm setdist:2.0];
  [self display];
  return self;
}

- printPSCode:sender
{
  PSonly = YES;
  return self;
}

- setTheta:(float)floatValue
{
  [vm setTheta:floatValue];
  [self display];
  return self;
}

- setPhi:(float)floatValue
{
  [vm setPhi:-floatValue];
  [self display];
  return self;
}

- setdist:(float)floatValue
{
  [vm setdist:floatValue];
  [self display];
  return self;
}

- setCube:(int)intValue
{
	showCube = intValue;
  [self display];
	return self;
}

- setAxes:(int)intValue
{
	showAxes = intValue;
  [self display];
	return self;
}

- toggleCube:sender
{
  showCube = showCube ? NO : YES;
  [self display];
  return self;
}

- toggleAxes:sender
{
  showAxes = showAxes ? NO : YES;
  [self display];
  return self;
}

- reScale:sender
{
  float maxval[3], minval[3], limits[6], *fpt;
  datapoints **all_lines = toshow, *thisline;
  short int i, count;

  if (*all_lines == (datapoints *) NULL) { /* no data - set to defaults */
    limits[0] = limits[1] = limits[2] = 0.;
    limits[3] = limits[4] = limits[5] = 1.;
    [self setlimits:limits];
    return self;
  }
  for(i = 0; i < 3; i++)
    minval[i] = maxval[i] = *((*all_lines)->displayed[i]);
  while (thisline = *all_lines++) {
    for(i = 0; i < 3; i++) {
      count = thisline->npts;
      fpt = thisline->displayed[i];
      while (count--) {
	if (*fpt < minval[i]) minval[i] = *fpt;
	if (*fpt > maxval[i]) maxval[i] = *fpt;
	fpt++;
      }
    }
  }
  for(i = 0; i < 3; i++) {
    if (minval[i] >= maxval[i]) minval[i] = maxval[i] - 1;
    limits[i] = minval[i] - 0.035*(maxval[i] - minval[i]);
    limits[i + 3] = maxval[i] + 0.035*(maxval[i] - minval[i]);
  }
  [self setlimits:limits];
  return self;
}



static int x_seq[] = {0,3,3,0,0,0,3,3,0,0,3,3,3,3,0,0};
static int y_seq[] = {1,1,4,4,1,1,1,4,4,1,1,1,4,4,4,4};
static int z_seq[] = {2,2,2,2,2,5,5,5,5,5,5,2,2,5,5,2};

-setlimits:(float *)newlimits
{
  int i, n, tickdir;
  float tick, width;

  for (n = 0; n < 16; n++) {	/* create cube */
    cube[0][n] = newlimits[x_seq[n]];
    cube[1][n] = newlimits[y_seq[n]];
    cube[2][n] = newlimits[z_seq[n]];
  }
  for (n = 0; n < 54; n++) {	/* create axes */
    axes[0][n] = newlimits[0];
    axes[1][n] = newlimits[1];
    axes[2][n] = newlimits[2];
  }
  for (i = 0; i < 3; i++) {
    tickdir = (i == 0) ? 1 : 0;
    tick = 0.03 * (newlimits[tickdir + 3] - newlimits[tickdir]);
    width = newlimits[i + 3] - newlimits[i];
    n = 18 * i;
    axes[i][n+1] = axes[i][n+2] = axes[i][n+3] = axes[i][n+4] =
      newlimits[i] + 0.25 * width;
    axes[i][n+5] = axes[i][n+6] = axes[i][n+7] = axes[i][n+8] =
      newlimits[i] + 0.5 * width;
    axes[i][n+9] = axes[i][n+10] = axes[i][n+11] = axes[i][n+12] =
      newlimits[i] + 0.75 * width;
    axes[i][n+13] = axes[i][n+17] = newlimits[i] + 0.87 * width;
    axes[i][n+14] = axes[i][n+16] = newlimits[i] + 0.84 * width;
    axes[i][n+15] = newlimits[i] + width;
    axes[tickdir][n+2] = axes[tickdir][n+6] = axes[tickdir][n+10] =
      axes[tickdir][n+14] = newlimits[tickdir] + tick;
    axes[tickdir][n+3] = axes[tickdir][n+7] = axes[tickdir][n+11] =
      axes[tickdir][n+16] = newlimits[tickdir] - tick;
  }
  [vm setlimits:newlimits];
/*  [self Reset:self];
*/
  return self;
}

- getStruct:(datapoints ***)dptr:(int) max_p:(float **)p
{

	toshow = *dptr;
	max_path = max_p;
	path = *p;
	
/*
{
	datapoints **all_data, *thesedata;
	int count;
	
	all_data = toshow;
	count = 0;
	printf("CubeView: data structure toshow\n");
	while ((thesedata = *all_data++) != (datapoints *) NULL) {
		printf("count = %d, npts = %d, type = %d\n",
			count++, thesedata->npts, thesedata->type);
			if (count > 10) break;
			}

}
*/
	[self reScale:self];
	return self;
}

- drawSelf:(NXRect*)r :(int)c
{
  datapoints **all_lines = toshow, *thisline;
  float radius = defRadius;
  float	shade = NX_BLACK;
  
  NXEraseRect(&bounds);
/*
 *	We would set defaults, but overide if data is not null;
 *
*/
  PSsetgray(NX_BLACK);
  PSsetlinewidth(0.0);
  if (showCube) {
    ops[1] = 32 + 16 - 1;
    DPSDoUserPath ([vm as_DPSpath :16 :cube[0] :cube[1] :cube[2] :path],
		   32, dps_float, ops, 3, boundingBox, dps_ustroke);
  }
  if (showAxes) {
    ops[1] = 32 + 54 - 1;
    DPSDoUserPath ([vm as_DPSpath :54 :axes[0] :axes[1] :axes[2] :path],
		   108, dps_float, ops, 3, boundingBox, dps_ustroke);
  }
  while((thisline = *all_lines++) != (datapoints *) NULL) {
    if (thisline->type & 0x1) {	/* draw the line */
      ops[1] = thisline->npts + 32 - 1;
      DPSDoUserPath([vm as_DPSpath :thisline->npts
		     :(thisline->displayed)[0] :(thisline->displayed)[1]
		     :(thisline->displayed)[2] :path],
		    2*(thisline->npts), dps_float,
		    ops, 3, boundingBox, dps_ustroke);
    }
    if (thisline->type & 0x2) {	/* draw points */
/*
 *	Here we pull out thisline->radius and thisline->shade
 *  If they are not the default(?) then set them and pass
 *  Otherwise use default.
*/
	if (thisline->radius <= 0.) radius = defRadius;
		else radius = thisline->radius;
	if (thisline->shade < 0.) shade = NX_BLACK;
		else shade = thisline->shade;
		
	dotAtPSonly([vm as_DPSpath :thisline->npts
		     :(thisline->displayed)[0] :(thisline->displayed)[1]
		     :(thisline->displayed)[2] :path],
		    2 *(thisline->npts), radius, shade);
     }
  }
  PSonly = NO;
  return self;
}

@end
