// Copyright (c) 1997  Todd Anthony Nathan  All Rights Reserved.
//
// Created: Sun Mar  9 02:16:13 GMT-0600 1997
// By:      Todd Anthony Nathan
// 
// You are free to do as you see fit with this software.  The author
// doesn't make any implied or non-implied guarentees to the fitness
// of the software.  Use it at your own risk.
///

#import "MiscCoordAdditions.h"

@implementation MiscCoord (MiscCoordAdditions)

// Simply replace the contents of one point with the supplied
- replacePoint:(NXPoint *)newPoint at:(int)index
{
	double	x, y, z;
	int		lastSlot;

	x = newPoint->x;
	y = newPoint->y;
	z = 0.0;

	// Expand point collection if necessary
	lastSlot = [self numPoints] - 1;
	if ( index > lastSlot ) {
		[self selectAndSetNumPoints:0 blockSize:(index + 1)];
	}
	[self selectExistingPoints:index blockSize:1];
	[self setCoord:x :y :z];

	return ( self );
}

// Expand the array by one, then move every
// point from index up one slot.  Then fill
// in the new slot.
/// 
- insertPoint:(NXPoint *)newPoint at:(int)index
{
	int		orig, dest;
	double	x, y, z;
	int		lastSlot;
	int		mySize;

	mySize = [self numPoints];
	lastSlot = mySize - 1;
	[self selectAndSetNumPoints:0 blockSize:(mySize + 1)];

	// Move the values up...
	for ( orig = lastSlot, dest = lastSlot + 1; orig >= index; orig--, dest-- ) {
		[self selectExistingPoints:orig blockSize:1];
		[self coord:&x :&y :&z];
		[self selectExistingPoints:dest blockSize:1];
		[self setCoord:x :y :z];
	}

	[self replacePoint:newPoint at:index];

	return ( self );
}


// Add it to the beginning
- prependPoint:(NXPoint *)newPoint
{
	[self insertPoint:(NXPoint *)newPoint at:0];
	return ( self );
}


// Add it to the end
- appendPoint:(NXPoint *)newPoint
{
	[self insertPoint:(NXPoint *)newPoint at:[self numPoints]];
	return ( self );
}


- swapPointsAt:(int)pointA :(int)pointB
{
	double	ptAX, ptAY, ptAZ;
	double	ptBX, ptBY, ptBZ;
	int		mySize, lastSlot;

	if ( pointA != pointB && pointA >= 0 && pointB >= 0 ) {
		mySize = [self numPoints];
		lastSlot = mySize - 1;
		if ( MAX(pointA, pointB) > lastSlot ) {
			[self selectAndSetNumPoints:0 blockSize:mySize + 1];
		}
		[self selectExistingPoints:pointA blockSize:1];
		[self coord:&ptAX :&ptAY :&ptAZ];
		[self selectExistingPoints:pointB blockSize:1];
		[self coord:&ptBX :&ptBY :&ptBZ];
		[self selectExistingPoints:pointB blockSize:1];
		[self setCoord:ptAX :ptAY :ptAZ];
		[self selectExistingPoints:pointA blockSize:1];
		[self setCoord:ptBX :ptBY :ptBZ];
	}

	return ( self );
}


// Reverse the values of the points
- reverse
{
	int		low, high;

	for ( low = 0, high = [self numPoints] - 1; low < high; low++, high-- ) {
		[self swapPointsAt:low :high];
	}

	return ( self );
}

- dumpPoints
{
	int	index;
	double	x, y, z;

	for ( index = 0; index < [self numPoints]; index++ ) {
		[self selectExistingPoints:index blockSize:1];
		[self coord:&x :&y :&z];
		printf("Point at  %4d:  x = %.2f, y = %.2f, z = %.2f\n", index, x, y, z);
	}

	return ( self );
}

@end
