static VAttrList VGraphEncodeAttrMethod (VPointer value, size_t *lengthp) { VGraph graph = value; VAttrList list; size_t len, nadj; int i, slong, sfloat, spriv, nnodes; VNode n; VAdjacency adj; /* Compute the file space needed for the Graph's binary data: */ len = 0; slong = VRepnPrecision (VLongRepn) / 8; sfloat = VRepnPrecision (VFloatRepn) / 8; spriv = graph->nfields * VRepnPrecision (graph->node_repn) / 8; nnodes = 0; for (i = 1; i <= graph->size; i++) { n = VGraphGetNode(graph, i); if (n == 0) continue; ++nnodes; /* Count the number of adjacencies : */ for (adj = n->base.head, nadj = 0; adj; adj = adj->next) nadj++; /* each node contains: * an index and the number of adjacencies * the private data area * the list of adjacencies * optionally reserve space for weights */ len += 2 * slong + nadj * slong + spriv; if (graph->useWeights) len += (nadj+1) * sfloat; }; *lengthp = len; assert(nnodes == graph->nnodes); /* for debugging */ graph->nnodes = nnodes; /* just to be safe for now... */ /* Temporarily prepend several attributes to the graph's list: */ if ((list = VGraphAttrList (graph)) == NULL) list = VGraphAttrList (graph) = VCreateAttrList (); VPrependAttr (list, VRepnAttr, VNumericRepnDict, VLongRepn, (VLong) graph->node_repn); VPrependAttr (list, VNNodeFieldsAttr, NULL, VLongRepn, (VLong) graph->nfields); VPrependAttr (list, VNGraphSizeAttr, NULL, VLongRepn, (VLong) graph->size); VPrependAttr (list, VNNodeWeightsAttr, NULL, VLongRepn, (VLong) graph->useWeights); return list; }
VGraph VCopyGraph (VGraph src) { VGraph dst; int i; dst = VCreateGraph (src->size, src->nfields, src->node_repn, src->useWeights); /* copy each used node in table */ for (i = 1; i <= src->size; i++) dst->table[i-1] = VCopyNodeDeep(src, VGraphGetNode(src, i)); dst->nnodes = src->nnodes; dst->lastUsed = src->lastUsed; if (VGraphAttrList (dst)) VDestroyAttrList (VGraphAttrList (dst)); if (VGraphAttrList (src)) VGraphAttrList (dst) = VCopyAttrList (VGraphAttrList (src)); return dst; }
VGraph VGraphExtractNodes (VGraph src) { VGraph dst; VAdjacency adj; VNode n; int i, j; /* create a destination graph much like src */ dst = VCreateGraph(src->size, src->nfields, src->node_repn, src->useWeights); /* copy selected nodes from src */ for (i = j = 1; i <= src->lastUsed; i++) { n = VGraphGetNode(src, i); if (n && n->base.hops) dst->table[j] = VCopyNodeShallow(src, n); }; /* set number of nodes used */ dst->nnodes = j-1; dst->lastUsed = j; /* now link nodes in new graph */ for (i = 1; i <= dst->lastUsed; i++) { n = VGraphGetNode(dst, i); if (n == 0) continue; j = VGraphLookupNode(src, n); if (j == 0) continue; n = VGraphGetNode(src, j); for (adj = n->base.head; adj; adj = adj->next) { n = VGraphGetNode(src, adj->id); j = VGraphLookupNode(dst, n); if (j) VGraphLinkNodes(dst, i, j); }; }; if (VGraphAttrList (dst)) VDestroyAttrList (VGraphAttrList (dst)); if (VGraphAttrList (src)) VGraphAttrList (dst) = VCopyAttrList (VGraphAttrList (src)); return dst; }
VGraph VGraphHemi(VGraph src,VLong hemi) { VString str; VGraph dest=NULL; int i,j,n,ncols,half=0; float x,y,z,val=0; VNode node,node1; int *table; half = 80; /* standard coordinate for inter-hemisperic cleft */ if (VGetAttr (VGraphAttrList (src), "ca", NULL,VStringRepn, (VPointer) & str) == VAttrFound) { sscanf(str,"%f %f %f",&x,&y,&z); ncols = (int) x; half = ncols/2; } table = (int *) VMalloc(sizeof(int) * (src)->lastUsed + 1); for (i=0; i<= src->lastUsed; i++) table[i] = 0; n = (src)->lastUsed/2; dest = VCreateGraph(n,VGraphNFields(src),VNodeRepn(src),src->useWeights); for (i=1; i<=(src)->lastUsed; i++) { node = (VNode) VGraphGetNode (src,i); if (node == 0) continue; VReadNode((src),node,&x,&y,&z,&val); if ((hemi == 0 && x <= half) || (hemi == 1 && x > half)) { n = VGraphAddNode(dest,(VNode) node); table[i] = n; } } for (i=1; i<=(src)->lastUsed; i++) { node = (VNode) VGraphGetNode (src,i); if (node == 0) continue; for (j=1; j<=(src)->lastUsed; j++) { node1 = (VNode) VGraphGetNode (src,j); if (node1 == 0) continue; if (table[i] == 0 || table[j] == 0) continue; if (VGraphIsAdjacent(src,i,j) == TRUE) { VGraphLinkNodes(dest,table[i],table[j]); } } } VFree(table); return dest; }
int main(int argc, char *argv[]) { static VLong objnr = -1; static VString name = ""; static VString value = ""; static VOptionDescRec options[] = { { "obj", VLongRepn, 1, (VPointer) &objnr, VOptionalOpt, NULL, "object number, all objects (-1)" }, { "name", VStringRepn, 1, (VPointer) &name, VRequiredOpt, NULL, "attribute name" }, { "value", VStringRepn, 1, (VPointer) &value, VRequiredOpt, NULL, "attribute value" } }; FILE *in_file, *out_file; VAttrList list, olist; VAttrListPosn posn; VImage isrc; VGraph gsrc; Volumes vsrc; VString buf; int nobj; char prg_name[100]; char ver[100]; getLipsiaVersion(ver, sizeof(ver)); sprintf(prg_name, "vattredit V%s", ver); fprintf(stderr, "%s\n", prg_name); /* Parse command line arguments and identify files: */ VParseFilterCmd(VNumber(options), options, argc, argv, &in_file, & out_file); /* Read source image(s): */ if(!(list = VReadFile(in_file, NULL))) exit(1); /* Process each image: */ nobj = 0; for(VFirstAttr(list, & posn); VAttrExists(& posn); VNextAttr(& posn)) { olist = NULL; if(nobj == objnr || objnr < 0) { if(VGetAttrRepn(& posn) == VImageRepn) { VGetAttrValue(& posn, NULL, VImageRepn, & isrc); olist = VImageAttrList(isrc); } else if(VGetAttrRepn(& posn) == VGraphRepn) { VGetAttrValue(& posn, NULL, VGraphRepn, & gsrc); olist = VGraphAttrList(gsrc); } else if(VGetAttrRepn(& posn) == VolumesRepn) { VGetAttrValue(& posn, NULL, VolumesRepn, & vsrc); olist = VolumesAttrList(vsrc); } if(olist != NULL) { if(VGetAttr(olist, name, NULL, VStringRepn, &buf) == VAttrFound) fprintf(stderr, " object %3d, old value: %s\n", nobj, buf); VSetAttr(olist, name, NULL, VStringRepn, value); fprintf(stderr, " object %3d, new value: %s\n", nobj, value); } } nobj++; } /* Make History */ VHistory(VNumber(options), options, prg_name, &list, &list); /* Write out the results: */ if(! VWriteFile(out_file, list)) exit(1); fprintf(stderr, "%s: done.\n", argv[0]); return 0; }
static VPointer VGraphDecodeMethod (VStringConst name, VBundle b) { VGraph graph; VLong size, nfields, node_repn, useWeights; VAttrList list; VLong idx, nadj; int length; size_t len; VNode n; VPointer p, ptr; VAdjacency adj; #define Extract(name, dict, locn, required) \ VExtractAttr (b->list, name, dict, VLongRepn, & locn, required) /* Extract the required attribute values for Graph. */ if (!Extract (VRepnAttr, VNumericRepnDict, node_repn, TRUE) || !Extract (VNNodeFieldsAttr, NULL, nfields, TRUE) || !Extract (VNNodeWeightsAttr, NULL, useWeights, TRUE)) return NULL; /* Look for size attribute, if not present, look for nnodes (for backward compatibility */ if (Extract (VNGraphSizeAttr, NULL, size, TRUE) == FALSE && Extract (VNGraphNodesAttr, NULL, size, TRUE) == FALSE) return NULL; if (size <= 0 || nfields <= 0) { VWarning ("VGraphReadDataMethod: Bad Graph file attributes"); return NULL; } /* Create the Graph data structure. */ graph = VCreateGraph ((int)size, (int) nfields, (VRepnKind) node_repn, (int) useWeights); if (! graph) return NULL; /* Give it whatever attributes remain: */ list = VGraphAttrList (graph); VGraphAttrList (graph) = b->list; b->list = list; length = b->length; if (length == 0) return graph; p = b->data; #define unpack(repn, cnt, dest) \ ptr = dest; \ if (VUnpackData(repn, cnt, p, VMsbFirst, & len, & ptr, 0) == 0) return 0; \ p = (char *) p + len; length -= len; len = length; \ if (length < 0) goto Fail; len = length; while (length > 0) { /* Get the index : */ unpack(VLongRepn, 1, &idx); graph->table[idx-1] = n = VCalloc(1, VNodeSize(graph)); if (idx > graph->lastUsed) graph->lastUsed = idx; graph->nnodes++; /* Get the number of adjacencies : */ unpack(VLongRepn, 1, &nadj); /* Unpack the adjacencies : */ while (nadj--) { adj = VMalloc(sizeof(VAdjRec)); unpack(VLongRepn, 1, &adj->id); if (graph->useWeights) { unpack(VFloatRepn, 1, &adj->weight); } else adj->weight = 0.0; adj->next = n->base.head; n->base.head = adj; }; /* Unpack the node itself: */ if (graph->useWeights) { unpack(VFloatRepn, 1, &(n->base.weight)); } else n->base.weight = 0.0; unpack(graph->node_repn, graph->nfields, n->data); } return graph; Fail: VWarning ("VGraphDecodeMethod: %s graph has wrong data length", name); VDestroyGraph (graph); return NULL; #undef Extract }