freegeo.c

来自「算断裂的」· C语言 代码 · 共 337 行

C
337
字号

/*
// ------------------------------------------------------------------
// freegeo.c
//
// This file contains the function for deleting a geo.
// ------------------------------------------------------------------
// Author: Stephen A. Vavasis
// Copyright (c) 1998 by Cornell University.  All rights reserved.
// 
// See the accompanying file 'Copyright' for authorship information,
// the terms of the license governing this software, and disclaimers
// concerning this software.
// ------------------------------------------------------------------
// This file is part of the CPTC and QMG software.  
// Version 2.0 of QMG, release date RELDATE.
// ------------------------------------------------------------------
*/




#include <stdlib.h>
#include "GeoFmt.h"
#include <string.h>


#ifdef USE_DEBUG_MALLOC
#define malloc(x) debug_malloc(x,__LINE__,__FILE__)
#define free(x) debug_free(x,__LINE__,__FILE__)

extern void* debug_malloc();
extern void debug_free();
#endif



/* Free a property-value list for a topological face */

static void free_prop_or_val(count, array) 
int count;
char** array;
{
  int i;
  if (array == 0) return;
  for (i = 0; i < count; ++i)
    free(array[i]);
  free(array);
}


/* Free the whole geo */

void cptc_free_geo(geo)
Cptc_GeomDesc* geo;
{
  int i, j;
  if (geo == 0) return;
  if (geo -> props) {
    free_prop_or_val(geo -> num_properties,  geo -> props);
    free_prop_or_val(geo -> num_properties,  geo -> values);
  }

  if (geo -> control_points != 0)
    free(geo -> control_points);
  if (geo -> top_vertices) {
    for (i = 0; i < geo -> num_top_vertices; ++i) {
      free(geo -> top_vertices[i].id);
      free_prop_or_val(geo -> top_vertices[i].num_properties,
        geo -> top_vertices[i].props);
      free_prop_or_val(geo -> top_vertices[i].num_properties,
        geo -> top_vertices[i].values);
    }
    free(geo -> top_vertices);
  }
  if (geo -> top_edges) {
    for (i = 0; i < geo -> num_top_edges; ++i) {
      free(geo -> top_edges[i].id);
      free_prop_or_val(geo -> top_edges[i].num_properties,
        geo -> top_edges[i].props);
      free_prop_or_val(geo -> top_edges[i].num_properties,
        geo -> top_edges[i].values);
      if (geo -> top_edges[i].bound_vertices != 0)
        free(geo -> top_edges[i].bound_vertices);
      if (geo -> top_edges[i].curves != 0) {
        for (j = 0; j < geo -> top_edges[i].num_curves; ++j) {
          free(geo -> top_edges[i].curves[j].control_pnt_indx);
        }
        free (geo -> top_edges[i].curves);
        
      }
    } 
    free(geo -> top_edges);
  }
  if (geo -> top_surfaces) {
    for (i = 0; i < geo -> num_top_surfaces; ++i) {
      free(geo -> top_surfaces[i].id);
      free_prop_or_val(geo -> top_surfaces[i].num_properties,
        geo -> top_surfaces[i].props);
      free_prop_or_val(geo -> top_surfaces[i].num_properties,
        geo -> top_surfaces[i].values);
      if (geo -> top_surfaces[i].bound_vertices != 0)
        free(geo -> top_surfaces[i].bound_vertices);
      if (geo -> top_surfaces[i].bound_edges != 0)
        free(geo -> top_surfaces[i].bound_edges);
      if (geo -> top_surfaces[i].patches != 0) {
        for (j = 0; j < geo -> top_surfaces[i].num_patches; ++j) {
          free(geo -> top_surfaces[i].patches[j].control_pnt_indx);
        }
        free (geo -> top_surfaces[i].patches);
      }
    } 
    free(geo -> top_surfaces);
  }

  if (geo -> top_regions) {
   for (i = 0; i < geo -> num_top_regions; ++i) {
      free(geo -> top_regions[i].id);
      free_prop_or_val(geo -> top_regions[i].num_properties,
        geo -> top_regions[i].props);
      free_prop_or_val(geo -> top_regions[i].num_properties,
        geo -> top_regions[i].values);
      if (geo -> top_regions[i].bound_vertices != 0)
        free(geo -> top_regions[i].bound_vertices);
      if (geo -> top_regions[i].bound_edges != 0)
        free(geo -> top_regions[i].bound_edges);
      if (geo -> top_regions[i].bound_surfaces != 0)
        free(geo -> top_regions[i].bound_surfaces);
      if (geo -> top_regions[i].surface_orientation != 0)
        free(geo -> top_regions[i].surface_orientation);
    } 
    free(geo -> top_regions);
  }
  free(geo);
}



/* clone a string */

static void clone_id(dest, src)
char**dest;
char* src;
{
  *dest = (char*) malloc(strlen(src) + 1);
  strcpy(*dest, src);
}

/* clone a property-value list */

static void clone_prop_vals(destnumprops, destprops,  destvals,
                            numprop, srcprops, srcvals) 
int* destnumprops;
char*** destprops; 
char*** destvals;
int numprop;
char** srcprops;
char** srcvals;
{
  int i;
  *destprops = (char**) malloc(numprop * sizeof(char*));
  *destvals = (char**) malloc(numprop * sizeof(char*));
  *destnumprops = numprop;
  for (i = 0; i < numprop; ++i) {
    clone_id(&((*destprops)[i]), srcprops[i]);
    clone_id(&((*destvals)[i]), srcvals[i]);
  }
}


/* clone a list of integers */

static void clone_int_list(destsize, destlist, srcsize, srclist)
int* destsize;
int** destlist;
int srcsize;
int* srclist;
{
  int i;
  *destsize = srcsize;
  *destlist = (int*) malloc(sizeof(int) * srcsize);
  for (i = 0; i < srcsize; ++i)
    (*destlist)[i] = srclist[i];
}
 



/* Clone (make a separate copy with all new pointers) an entire geo */

Cptc_GeomDesc* cptc_clone_geo(geo)
Cptc_GeomDesc* geo;
{
  int i, j, k, ncp, ntv, nte, ncurve, nts, ord, npatch, ntr, ou, ov;
  Cptc_GeomDesc* newgeo;
  if (geo == 0) return 0;
  newgeo = (Cptc_GeomDesc*) malloc(sizeof(Cptc_GeomDesc));
  *newgeo = *geo;

  if (geo -> props) {
    clone_prop_vals(&(newgeo -> num_properties),
      &(newgeo -> props), &(newgeo -> values),
      geo -> num_properties, geo -> props, geo -> values);
  }

  if (geo -> control_points) {
    ncp = geo -> num_control_pts;
    newgeo -> num_control_pts = ncp;
    newgeo -> control_points = (Cptc_GPoint*) malloc(sizeof(Cptc_GPoint) * ncp);
    for (i = 0; i < ncp; ++i)
      newgeo -> control_points[i] = geo -> control_points[i];
  }
  if (geo -> top_vertices) {
    ntv = geo -> num_top_vertices;
    newgeo -> num_top_vertices = ntv;
    newgeo -> top_vertices = (Cptc_TVertex*) malloc(sizeof(Cptc_TVertex) * ntv);
    for (i = 0; i < ntv; ++i) {
      newgeo -> top_vertices[i] = geo -> top_vertices[i];
      clone_id(&(newgeo -> top_vertices[i].id), geo -> top_vertices[i].id);
      clone_prop_vals(&(newgeo -> top_vertices[i].num_properties),
        &(newgeo -> top_vertices[i].props),
        &(newgeo -> top_vertices[i].values),
        geo -> top_vertices[i].num_properties,
        geo -> top_vertices[i].props,
        geo -> top_vertices[i].values);
    }
  }
  if (geo -> top_edges) {
    nte = geo -> num_top_edges;
    newgeo -> num_top_edges = nte;
    newgeo -> top_edges = (Cptc_TEdge*) malloc(sizeof(Cptc_TEdge) * nte);
    for (i = 0; i < nte; ++i) {
      newgeo -> top_edges[i] = geo -> top_edges[i];
      clone_id(&(newgeo -> top_edges[i].id), geo -> top_edges[i].id);
      ncurve = geo -> top_edges[i].num_curves;
      newgeo -> top_edges[i].curves = 
        (Cptc_GCurve*) malloc(sizeof(Cptc_GCurve) * ncurve);
      newgeo -> top_edges[i].num_curves = ncurve;
      for (j = 0; j < ncurve; ++j) {
        ord = geo -> top_edges[i].curves[j].order;
        newgeo -> top_edges[i].curves[j].order = ord;
        newgeo -> top_edges[i].curves[j].control_pnt_indx =
          (int*) malloc(sizeof(int) * (ord + 1));
        for (k = 0; k < ord + 1; ++k) {
          newgeo -> top_edges[i].curves[j].control_pnt_indx[k] =
            geo -> top_edges[i].curves[j].control_pnt_indx[k];
        }
      }
      clone_prop_vals(&(newgeo -> top_edges[i].num_properties),
        &(newgeo -> top_edges[i].props),
        &(newgeo -> top_edges[i].values),
        geo -> top_edges[i].num_properties,
        geo -> top_edges[i].props,
        geo -> top_edges[i].values);
      clone_int_list(&(newgeo -> top_edges[i].num_bound_vertices),
        &(newgeo -> top_edges[i].bound_vertices),
        geo -> top_edges[i].num_bound_vertices,
        geo -> top_edges[i].bound_vertices);

    }
  }
  
  if (geo -> top_surfaces) {
    nts = geo -> num_top_surfaces;
    newgeo -> top_surfaces = (Cptc_TSurface*) malloc(sizeof(Cptc_TSurface) * nts);
    newgeo -> num_top_surfaces = nts;
    for (i = 0; i < nts; ++i) {
      newgeo -> top_surfaces[i] = geo -> top_surfaces[i];
      clone_id(&(newgeo -> top_surfaces[i].id), geo -> top_surfaces[i].id);
      npatch = geo -> top_surfaces[i].num_patches;
      newgeo -> top_surfaces[i].patches = 
        (Cptc_GPatch*) malloc(sizeof(Cptc_GPatch) * npatch);
      newgeo -> top_surfaces[i].num_patches = npatch;
      for (j = 0; j < npatch; ++j) {
        newgeo -> top_surfaces[i].patches[j] = 
          geo -> top_surfaces[i].patches[j];
        ou = geo -> top_surfaces[i].patches[j].order_u;
        ov = geo -> top_surfaces[i].patches[j].order_v;
        ncp = (geo -> top_surfaces[i].patches[j].type == CPTC_PATCHTYPE_TRIANGLE)?
          (ou + 1) * (ou + 2) / 2 : (ou + 1) * (ov + 1);
        newgeo -> top_surfaces[i].patches[j].control_pnt_indx = 
          (int*) malloc(sizeof(int) * ncp);
        for (k = 0; k < ncp; ++k) {
          newgeo -> top_surfaces[i].patches[j].control_pnt_indx[k] =
            geo -> top_surfaces[i].patches[j].control_pnt_indx[k];
        }
      }
      clone_prop_vals(&(newgeo -> top_surfaces[i].num_properties),
        &(newgeo -> top_surfaces[i].props),
        &(newgeo -> top_surfaces[i].values),
        geo -> top_surfaces[i].num_properties,
        geo -> top_surfaces[i].props,
        geo -> top_surfaces[i].values);
      clone_int_list(&(newgeo -> top_surfaces[i].num_bound_vertices),
        &(newgeo -> top_surfaces[i].bound_vertices),
        geo -> top_surfaces[i].num_bound_vertices,
        geo -> top_surfaces[i].bound_vertices);
      clone_int_list(&(newgeo -> top_surfaces[i].num_bound_edges),
        &(newgeo -> top_surfaces[i].bound_edges),
        geo -> top_surfaces[i].num_bound_edges,
        geo -> top_surfaces[i].bound_edges);
    }
  }
  if (geo -> top_regions) {
    ntr = geo -> num_top_regions;
    newgeo -> top_regions = (Cptc_TRegion*)
      malloc(sizeof(Cptc_TRegion) * ntr);
    newgeo -> num_top_regions = ntr;
    for (i = 0; i < ntr; ++i) {
      clone_id(&(newgeo -> top_regions[i].id), geo -> top_regions[i].id);
      clone_prop_vals(&(newgeo -> top_regions[i].num_properties),
        &(newgeo -> top_regions[i].props),
        &(newgeo -> top_regions[i].values),
        geo -> top_regions[i].num_properties,
        geo -> top_regions[i].props,
        geo -> top_regions[i].values);
      clone_int_list(&(newgeo -> top_regions[i].num_bound_vertices),
        &(newgeo -> top_regions[i].bound_vertices),
        geo -> top_regions[i].num_bound_vertices,
        geo -> top_regions[i].bound_vertices);
      clone_int_list(&(newgeo -> top_regions[i].num_bound_edges),
        &(newgeo -> top_regions[i].bound_edges),
        geo -> top_regions[i].num_bound_edges,
        geo -> top_regions[i].bound_edges);
      clone_int_list(&(newgeo -> top_regions[i].num_bound_surfaces),
        &(newgeo -> top_regions[i].bound_surfaces),
        geo -> top_regions[i].num_bound_surfaces,
        geo -> top_regions[i].bound_surfaces);
      clone_int_list(&(newgeo -> top_regions[i].num_bound_surfaces),
        &(newgeo -> top_regions[i].surface_orientation),
        geo -> top_regions[i].num_bound_surfaces,
        geo -> top_regions[i].surface_orientation);
    }
  }
  return newgeo;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?