Debian Codegraph @

lib/neatogen/call_tri.c

raw
1 /*************************************************************************
2 * Copyright (c) 2011 AT&T Intellectual Property
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * https://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors: Details at https://graphviz.org
9 *************************************************************************/
10
11 #include "config.h"
12 #include <sparse/SparseMatrix.h>
13 #include <neatogen/call_tri.h>
14 #include <neatogen/delaunay.h>
15 #include <stddef.h>
16 #include <stdbool.h>
17 #include <util/alloc.h>
18
19 SparseMatrix call_tri(int n, double *x) {
20 double one = 1;
21 int i, ii, jj;
22 SparseMatrix A;
23 SparseMatrix B;
24 int* edgelist = NULL;
25 double* xv = gv_calloc(n, sizeof(double));
26 double* yv = gv_calloc(n, sizeof(double));
27 int numberofedges = 0;
28
29 for (i = 0; i < n; i++) {
30 xv[i] = x[i * 2];
31 yv[i] = x[i * 2 + 1];
32 }
33
34 if (n > 2) {
35 edgelist = delaunay_tri (xv, yv, n, &numberofedges);
36 }
37
38 A = SparseMatrix_new(n, n, 1, MATRIX_TYPE_REAL, FORMAT_COORD);
39 for (i = 0; i < numberofedges; i++) {
40 ii = edgelist[i * 2];
41 jj = edgelist[i * 2 + 1];
42 SparseMatrix_coordinate_form_add_entry(A, ii, jj, &one);
43 }
44 if (n == 2) { /* if two points, add edge i->j */
45 ii = 0;
46 jj = 1;
47 SparseMatrix_coordinate_form_add_entry(A, ii, jj, &one);
48 }
49 for (i = 0; i < n; i++) {
50 SparseMatrix_coordinate_form_add_entry(A, i, i, &one);
51 }
52 B = SparseMatrix_from_coordinate_format(A);
53 SparseMatrix_delete(A);
54 A = SparseMatrix_symmetrize(B, false);
55 SparseMatrix_delete(B);
56 B = A;
57
58 free (edgelist);
59 free (xv);
60 free (yv);
61 return B;
62 }
63
64 SparseMatrix call_tri2(int n, int dim, double * xx)
65 {
66 v_data *delaunay;
67 int i, j;
68 SparseMatrix A;
69 SparseMatrix B;
70 double one = 1;
71 double *x = gv_calloc(n, sizeof(double));
72 double *y = gv_calloc(n, sizeof(double));
73
74 for (i = 0; i < n; i++) {
75 x[i] = xx[dim * i];
76 y[i] = xx[dim * i + 1];
77 }
78
79 delaunay = UG_graph(x, y, n);
80
81 A = SparseMatrix_new(n, n, 1, MATRIX_TYPE_REAL, FORMAT_COORD);
82
83 for (i = 0; i < n; i++) {
84 for (j = 1; j < delaunay[i].nedges; j++) {
85 SparseMatrix_coordinate_form_add_entry(A, i, delaunay[i].edges[j], &one);
86 }
87 }
88 for (i = 0; i < n; i++) {
89 SparseMatrix_coordinate_form_add_entry(A, i, i, &one);
90 }
91 B = SparseMatrix_from_coordinate_format(A);
92 {
93 SparseMatrix tmp = SparseMatrix_symmetrize(B, false);
94 SparseMatrix_delete(B);
95 B = tmp;
96 }
97 SparseMatrix_delete(A);
98
99 free (x);
100 free (y);
101 freeGraph (delaunay);
102
103 return B;
104
105 }
106