Graphviz  2.29.20120524.0446
lib/pathplan/util.c
Go to the documentation of this file.
00001 /* $Id$ $Revision$ */
00002 /* vim:set shiftwidth=4 ts=8: */
00003 
00004 /*************************************************************************
00005  * Copyright (c) 2011 AT&T Intellectual Property 
00006  * All rights reserved. This program and the accompanying materials
00007  * are made available under the terms of the Eclipse Public License v1.0
00008  * which accompanies this distribution, and is available at
00009  * http://www.eclipse.org/legal/epl-v10.html
00010  *
00011  * Contributors: See CVS logs. Details at http://www.graphviz.org/
00012  *************************************************************************/
00013 
00014 
00015 #include <assert.h>
00016 #include <stdlib.h>
00017 #include "pathutil.h"
00018 
00019 #ifdef DMALLOC
00020 #include "dmalloc.h"
00021 #endif
00022 
00023 #define ALLOC(size,ptr,type) (ptr? (type*)realloc(ptr,(size)*sizeof(type)):(type*)malloc((size)*sizeof(type)))
00024 
00025 Ppoly_t copypoly(Ppoly_t argpoly)
00026 {
00027     Ppoly_t rv;
00028     int i;
00029 
00030     rv.pn = argpoly.pn;
00031     rv.ps = malloc(sizeof(Ppoint_t) * argpoly.pn);
00032     for (i = 0; i < argpoly.pn; i++)
00033         rv.ps[i] = argpoly.ps[i];
00034     return rv;
00035 }
00036 
00037 void freepoly(Ppoly_t argpoly)
00038 {
00039     free(argpoly.ps);
00040 }
00041 
00042 int Ppolybarriers(Ppoly_t ** polys, int npolys, Pedge_t ** barriers,
00043                   int *n_barriers)
00044 {
00045     Ppoly_t pp;
00046     int i, j, k, n, b;
00047     Pedge_t *bar;
00048 
00049     n = 0;
00050     for (i = 0; i < npolys; i++)
00051         n = n + polys[i]->pn;
00052 
00053     bar = malloc(n * sizeof(Pedge_t));
00054 
00055     b = 0;
00056     for (i = 0; i < npolys; i++) {
00057         pp = *polys[i];
00058         for (j = 0; j < pp.pn; j++) {
00059             k = j + 1;
00060             if (k >= pp.pn)
00061                 k = 0;
00062             bar[b].a = pp.ps[j];
00063             bar[b].b = pp.ps[k];
00064             b++;
00065         }
00066     }
00067     assert(b == n);
00068     *barriers = bar;
00069     *n_barriers = n;
00070     return 1;
00071 }
00072 
00073 /* make_polyline:
00074  */
00075 void
00076 make_polyline(Ppolyline_t line, Ppolyline_t* sline)
00077 {
00078     static int isz = 0;
00079     static Ppoint_t* ispline = 0;
00080     int i, j;
00081     int npts = 4 + 3*(line.pn-2);
00082 
00083     if (npts > isz) {
00084         ispline = ALLOC(npts, ispline, Ppoint_t); 
00085         isz = npts;
00086     }
00087 
00088     j = i = 0;
00089     ispline[j+1] = ispline[j] = line.ps[i];
00090     j += 2;
00091     i++;
00092     for (; i < line.pn-1; i++) {
00093         ispline[j+2] = ispline[j+1] = ispline[j] = line.ps[i];
00094         j += 3;
00095     }
00096     ispline[j+1] = ispline[j] = line.ps[i];
00097 
00098     sline->pn = npts;
00099     sline->ps = ispline;
00100 }
00101