#ifndef lint
static char rcsid [] = "$RCSfile$ $Revision$ $State$";
#endif
/****************************************************************************
*   File: rtmp_bound.c                                                      *
*                                                                           *
*       Copyright 1994 by Loral Advanced Distributed Simulation, Inc.       *
*                                                                           *
*               Loral Advanced Distributed Simulation, Inc.                 *
*               10 Moulton Street                                           *
*               Cambridge, MA 02238                                         *
*               617-873-1850                                                *
*                                                                           *
*       This software was developed by Loral under U. S. Government contracts*
*       and may be reproduced by or for the U. S. Government pursuant to    *
*       the copyright license under the clause at DFARS 252.227-7013        *
*       (OCT 1988).                                                         *
*                                                                           *
*       Contents: Code to manage boundary structures                        *
*       Created: Thu Mar 17 1994                                            *
*       Author: jesmith                                                     *
*       Remarks:                                                            *
*                                                                           *
****************************************************************************/

#include "librtmp_local.h"
#include <math.h>
#include <stdalloc.h>
#include <stdext.h>

/* Translate a list of points into a boundary. */
ROUTEMAP_BOUNDARY_PTR routemap_create_boundary(points)
    ROUTE_POINTS *points;
{
    int32 i;
    ROUTEMAP_BOUNDARY_PTR result;

    if (points->num_pts < 2)
      return NULL;

    result = (ROUTEMAP_BOUNDARY_PTR)
      STDALLOC(sizeof(struct routemap_boundary));
    
    result->n_verts = points->num_pts;
    result->verts = (struct routemap_obstacle_vertex *)
      STDALLOC(sizeof(struct routemap_obstacle_vertex) * result->n_verts);

    for (i=0;i<result->n_verts;i++)
    {
	result->verts[i].seg.x = points->points[i].point[X];
	result->verts[i].seg.y = points->points[i].point[Y];
    }
    for (i=0;i<result->n_verts-1;i++)
    {
	result->verts[i].seg.dx =
	  result->verts[i+1].seg.x - result->verts[i].seg.x;
	result->verts[i].seg.dy =
	  result->verts[i+1].seg.y - result->verts[i].seg.y;
	result->verts[i].len_sq =
	  ((result->verts[i].seg.dx * result->verts[i].seg.dx)+
	   (result->verts[i].seg.dy * result->verts[i].seg.dy));
    }
    result->verts[i].seg.dx = result->verts[i].seg.dy = 0.0;
    result->verts[i].len_sq = 0.0;

    return result;
}

/* Free the memory associated with a routemap boundary. */
void routemap_free_boundary(boundary)
    ROUTEMAP_BOUNDARY_PTR boundary;
{
    if (boundary)
    {
	if (boundary->verts)
	{
	    STDDEALLOC(boundary->verts);
	    boundary->verts = NULL;
	}
	STDDEALLOC(boundary);
    }
}
