static int clipper(dglGraph_s * pgraph, dglSPClipInput_s * pIn, dglSPClipOutput_s * pOut, void *pvarg /* caller's pointer */ ) { ClipperContext_s *pclip = (ClipperContext_s *) pvarg; #if 0 dglInt32_t *pnFromXYZ = (dglInt32_t *) dglNodeGet_Attr(pgraph, pIn->pnNodeFrom); dglInt32_t *pnToXYZ = (dglInt32_t *) dglNodeGet_Attr(pgraph, pIn->pnNodeTo); printf("clipper called:\n"); printf(" from node: %ld - attributes x=%ld y=%ld z=%ld\n", dglNodeGet_Id(pgraph, pIn->pnNodeFrom), pnFromXYZ[0], pnFromXYZ[1], pnFromXYZ[2]); printf(" to node: %ld - attributes x=%ld y=%ld z=%ld\n", dglNodeGet_Id(pgraph, pIn->pnNodeTo), pnToXYZ[0], pnToXYZ[1], pnToXYZ[2]); printf(" edge : %ld\n", dglEdgeGet_Id(pgraph, pIn->pnEdge)); #endif if (pclip) { if (dglNodeGet_Id(pgraph, pIn->pnNodeTo) == pclip->node_to_discard) { /* printf( " discarder.\n" ); */ return 1; } } /* printf( " accepted.\n" ); */ return 0; }
static int clipper(dglGraph_s * pgraph, dglSPClipInput_s * pargIn, dglSPClipOutput_s * pargOut, void *pvarg) { /* caller's pointer */ dglInt32_t cost; dglInt32_t from; G_debug(3, "Net: clipper()"); from = dglNodeGet_Id(pgraph, pargIn->pnNodeFrom); G_debug(3, " Edge = %d NodeFrom = %d NodeTo = %d edge cost = %d", (int)dglEdgeGet_Id(pgraph, pargIn->pnEdge), (int)from, (int)dglNodeGet_Id(pgraph, pargIn->pnNodeTo), (int)pargOut->nEdgeCost); if (from != From_node) { /* do not clip first */ if (dglGet_NodeAttrSize(pgraph) > 0) { memcpy(&cost, dglNodeGet_Attr(pgraph, pargIn->pnNodeFrom), sizeof(cost)); if (cost == -1) { /* closed, cannot go from this node except it is 'from' node */ G_debug(3, " closed node"); return 1; } else { G_debug(3, " EdgeCost += %d (node)", (int)cost); pargOut->nEdgeCost += cost; } } } else { G_debug(3, " don't clip first node"); } return 0; }
static int _print_node(dglGraph_s * pgraph, dglInt32_t * pnode, void *pvarg) { FILE *f = (FILE *) pvarg; dglInt32_t *pedgeset; dglInt32_t *pedge; dglInt32_t *ptonode; dglInt32_t *pnattr; int iAttr, cAttr; int role; int i; dglEdgesetTraverser_s edgeaT; role = 0; if (dglNodeGet_Status(pgraph, pnode) & DGL_NS_HEAD) { role |= 1; } if (dglNodeGet_Status(pgraph, pnode) & DGL_NS_TAIL) { role |= 2; } fprintf(f, "HEAD %-8ld - %-s", dglNodeGet_Id(pgraph, pnode), (role > 2) ? "'H/T'" : (role == 2) ? "'T '" : (role == 1) ? "'H '" : "'A '"); if ((cAttr = dglGet_NodeAttrSize(pgraph)) > 0) { pnattr = dglNodeGet_Attr(pgraph, pnode); fprintf(f, " - HEAD ATTR ["); for (iAttr = 0; iAttr < cAttr; iAttr++) { if (iAttr && !(iAttr % 4)) fprintf(f, " "); fprintf(f, "%02x", ((unsigned char *)pnattr)[iAttr]); } fprintf(f, "]\n"); } else { fprintf(f, "\n"); } if (role & 1) { pedgeset = dglNodeGet_OutEdgeset(pgraph, pnode); dglEdgeset_T_Initialize(&edgeaT, pgraph, pedgeset); for (i = 0, pedge = dglEdgeset_T_First(&edgeaT); pedge; i++, pedge = dglEdgeset_T_Next(&edgeaT) ) { ptonode = dglEdgeGet_Tail(pgraph, pedge); if (ptonode) { role = 0; if (dglNodeGet_Status(pgraph, ptonode) & DGL_NS_HEAD) { role |= 1; } if (dglNodeGet_Status(pgraph, ptonode) & DGL_NS_TAIL) { role |= 2; } fprintf(f, "EDGE #%-8d: TAIL %-8ld - %-s - COST %-8ld - ID %-8ld", i, dglNodeGet_Id(pgraph, ptonode), (role > 2) ? "'H/T'" : (role == 2) ? "'T '" : (role == 1) ? "'H '" : "'A '", dglEdgeGet_Cost(pgraph, pedge), dglEdgeGet_Id(pgraph, pedge) ); if ((cAttr = dglGet_NodeAttrSize(pgraph)) > 0) { pnattr = dglNodeGet_Attr(pgraph, ptonode); fprintf(f, " - TAIL ATTR ["); for (iAttr = 0; iAttr < cAttr; iAttr++) { if (iAttr && !(iAttr % 4)) fprintf(f, " "); fprintf(f, "%02x", ((unsigned char *)pnattr)[iAttr]); } fprintf(f, "]"); } if ((cAttr = dglGet_EdgeAttrSize(pgraph)) > 0) { pnattr = dglEdgeGet_Attr(pgraph, pedge); fprintf(f, " - EDGE ATTR ["); for (iAttr = 0; iAttr < cAttr; iAttr++) { if (iAttr && !(iAttr % 4)) fprintf(f, " "); fprintf(f, "%02x", ((unsigned char *)pnattr)[iAttr]); } fprintf(f, "]\n"); } else { fprintf(f, "\n"); } } } dglEdgeset_T_Release(&edgeaT); } return 0; }
/*! \brief Computes weakly connected components \param graph input graph \param[out] component array of component ids \return number of components \return -1 on failure */ int NetA_weakly_connected_components(dglGraph_s * graph, int *component) { int nnodes, i; dglInt32_t *stack; int stack_size, components; dglInt32_t *cur_node; dglNodeTraverser_s nt; int have_node_costs; dglInt32_t ncost; if (graph->Version < 2) { G_warning("Directed graph must be version 2 or 3 for NetA_weakly_connected_components()"); return -1; } components = 0; nnodes = dglGet_NodeCount(graph); stack = (dglInt32_t *) G_calloc(nnodes + 1, sizeof(dglInt32_t)); if (!stack) { G_fatal_error(_("Out of memory")); return -1; } for (i = 1; i <= nnodes; i++) component[i] = 0; ncost = 0; have_node_costs = dglGet_NodeAttrSize(graph); dglNode_T_Initialize(&nt, graph); for (cur_node = dglNode_T_First(&nt); cur_node; cur_node = dglNode_T_Next(&nt)) { dglInt32_t cur_node_id = dglNodeGet_Id(graph, cur_node); if (!component[cur_node_id]) { stack[0] = cur_node_id; stack_size = 1; component[cur_node_id] = ++components; while (stack_size) { dglInt32_t *node, *edgeset, *edge; dglEdgesetTraverser_s et; node = dglGetNode(graph, stack[--stack_size]); edgeset = dglNodeGet_OutEdgeset(graph, node); dglEdgeset_T_Initialize(&et, graph, edgeset); for (edge = dglEdgeset_T_First(&et); edge; edge = dglEdgeset_T_Next(&et)) { dglInt32_t to; to = dglNodeGet_Id(graph, dglEdgeGet_Tail(graph, edge)); if (!component[to]) { component[to] = components; /* do not go through closed nodes */ if (have_node_costs) { memcpy(&ncost, dglNodeGet_Attr(graph, dglEdgeGet_Tail(graph, edge)), sizeof(ncost)); } if (ncost >= 0) stack[stack_size++] = to; } } dglEdgeset_T_Release(&et); edgeset = dglNodeGet_InEdgeset(graph, node); dglEdgeset_T_Initialize(&et, graph, edgeset); for (edge = dglEdgeset_T_First(&et); edge; edge = dglEdgeset_T_Next(&et)) { dglInt32_t to; to = dglNodeGet_Id(graph, dglEdgeGet_Head(graph, edge)); if (!component[to]) { component[to] = components; /* do not go through closed nodes */ if (have_node_costs) { memcpy(&ncost, dglNodeGet_Attr(graph, dglEdgeGet_Tail(graph, edge)), sizeof(ncost)); } if (ncost >= 0) stack[stack_size++] = to; } } dglEdgeset_T_Release(&et); } } } dglNode_T_Release(&nt); G_free(stack); return components; }
/*! \brief Computes strongly connected components with Kosaraju's two-pass algorithm \param graph input graph \param[out] component array of component ids \return number of components \return -1 on failure */ int NetA_strongly_connected_components(dglGraph_s * graph, int *component) { int nnodes, i; dglInt32_t *stack, *order; int *processed; int stack_size, order_size, components; dglInt32_t *cur_node; dglNodeTraverser_s nt; int have_node_costs; dglInt32_t ncost; if (graph->Version < 2) { G_warning("Directed graph must be version 2 or 3 for NetA_strongly_connected_components()"); return -1; } components = 0; nnodes = dglGet_NodeCount(graph); stack = (dglInt32_t *) G_calloc(nnodes + 1, sizeof(dglInt32_t)); order = (dglInt32_t *) G_calloc(nnodes + 1, sizeof(dglInt32_t)); processed = (int *)G_calloc(nnodes + 1, sizeof(int)); if (!stack || !order || !processed) { G_fatal_error(_("Out of memory")); return -1; } for (i = 1; i <= nnodes; i++) { component[i] = 0; } ncost = 0; have_node_costs = dglGet_NodeAttrSize(graph); order_size = 0; dglNode_T_Initialize(&nt, graph); for (cur_node = dglNode_T_First(&nt); cur_node; cur_node = dglNode_T_Next(&nt)) { dglInt32_t cur_node_id = dglNodeGet_Id(graph, cur_node); if (!component[cur_node_id]) { component[cur_node_id] = --components; stack[0] = cur_node_id; stack_size = 1; while (stack_size) { dglInt32_t *node, *edgeset, *edge; dglEdgesetTraverser_s et; dglInt32_t node_id = stack[stack_size - 1]; if (processed[node_id]) { stack_size--; order[order_size++] = node_id; continue; } processed[node_id] = 1; node = dglGetNode(graph, node_id); edgeset = dglNodeGet_OutEdgeset(graph, node); dglEdgeset_T_Initialize(&et, graph, edgeset); for (edge = dglEdgeset_T_First(&et); edge; edge = dglEdgeset_T_Next(&et)) { dglInt32_t to; to = dglNodeGet_Id(graph, dglEdgeGet_Tail(graph, edge)); if (!component[to]) { component[to] = components; /* do not go through closed nodes */ if (have_node_costs) { memcpy(&ncost, dglNodeGet_Attr(graph, dglEdgeGet_Tail(graph, edge)), sizeof(ncost)); } if (ncost < 0) processed[to] = 1; stack[stack_size++] = to; } } dglEdgeset_T_Release(&et); } } } dglNode_T_Release(&nt); components = 0; dglNode_T_Initialize(&nt, graph); while (order_size) { dglInt32_t cur_node_id = order[--order_size]; int cur_comp = component[cur_node_id]; if (cur_comp < 1) { component[cur_node_id] = ++components; stack[0] = cur_node_id; stack_size = 1; while (stack_size) { dglInt32_t *node, *edgeset, *edge; dglEdgesetTraverser_s et; dglInt32_t node_id = stack[--stack_size]; node = dglGetNode(graph, node_id); edgeset = dglNodeGet_InEdgeset(graph, node); dglEdgeset_T_Initialize(&et, graph, edgeset); for (edge = dglEdgeset_T_First(&et); edge; edge = dglEdgeset_T_Next(&et)) { dglInt32_t to; to = dglNodeGet_Id(graph, dglEdgeGet_Head(graph, edge)); if (component[to] == cur_comp) { component[to] = components; /* do not go through closed nodes */ if (have_node_costs) { memcpy(&ncost, dglNodeGet_Attr(graph, dglEdgeGet_Head(graph, edge)), sizeof(ncost)); } if (ncost >= 0) stack[stack_size++] = to; } } dglEdgeset_T_Release(&et); } } } dglNode_T_Release(&nt); G_free(stack); G_free(order); G_free(processed); return components; }