Graphviz 14.1.2~dev.20260118.0226
Loading...
Searching...
No Matches
nearest_neighbor_graph_ann.cpp
Go to the documentation of this file.
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
13#include <ANN/ANN.h> // ANN declarations
15#include <utility>
16#include <vector>
17
18static const int dim = 4; // dimension
19
20static void sortPtsX(int n, ANNpointArray pts){
21 /* sort so that edges always go from left to right in x-doordinate */
22 for (int i = 0; i < n; i++){
23 ANNpoint p = pts[i];
24 if (p[0] < p[2] || (p[0] == p[2] && p[1] < p[3])) continue;
25 std::swap(p[0], p[2]);
26 std::swap(p[1], p[3]);
27 }
28}
29
30static void sortPtsY(int n, ANNpointArray pts){
31 /* sort so that edges always go from left to right in x-doordinate */
32 for (int i = 0; i < n; i++){
33 ANNpoint p = pts[i];
34 if (p[1] < p[3] || (p[1] == p[3] && p[0] < p[2])) continue;
35 std::swap(p[0], p[2]);
36 std::swap(p[1], p[3]);
37 }
38}
39
40void nearest_neighbor_graph_ann(int nPts, int k, const std::vector<double> &x,
41 int &nz0, std::vector<int> &irn,
42 std::vector<int> &jcn,
43 std::vector<double> &val) {
44
45 /* Gives a nearest neighbor graph is a list of dim-dimendional points. The connectivity is in irn/jcn, and the distance in val.
46
47 nPts: number of points
48 dim: dimension
49 k: number of neighbors needed
50 x: nPts*dim vector. The i-th point is x[i*dim : i*dim + dim - 1]
51 nz: number of entries in the connectivity matrix irn/jcn/val
52 irn, jcn: the connectivity
53 val: the distance
54
55 note that there could be repeates
56 */
57
58 // error tolerance
59 const double eps = 0;
60
61 ANNpointArray dataPts = annAllocPts(nPts, dim); // allocate data points
62 std::vector<ANNidx> nnIdx(k); // allocate near neighbor indices
63 std::vector<ANNdist> dists(k); // allocate near neighbor dists
64
65 for (int i = 0; i < nPts; i++){
66 double *xx = dataPts[i];
67 for (int j = 0; j < dim; j++) xx[j] = x[i*dim + j];
68 }
69
70 //========= graph when sort based on x ========
71 int nz = 0;
72 sortPtsX(nPts, dataPts);
73 ANNkd_tree xTree( // build search structure
74 dataPts, // the data points
75 nPts, // number of points
76 dim); // dimension of space
77 for (int ip = 0; ip < nPts; ip++){
78 xTree.annkSearch( // search
79 dataPts[ip], // query point
80 k, // number of near neighbors
81 nnIdx.data(), // nearest neighbors (returned)
82 dists.data(), // distance (returned)
83 eps); // error bound
84
85 for (int i = 0; i < k; i++) { // print summary
86 if (nnIdx[i] == ip) continue;
87 val[nz] = dists[i];
88 irn[nz] = ip;
89 jcn[nz++] = nnIdx[i];
90 }
91 }
92
93
94 //========= graph when sort based on y ========
95 sortPtsY(nPts, dataPts);
96 ANNkd_tree yTree( // build search structure
97 dataPts, // the data points
98 nPts, // number of points
99 dim); // dimension of space
100 for (int ip = 0; ip < nPts; ip++){
101 yTree.annkSearch( // search
102 dataPts[ip], // query point
103 k, // number of near neighbors
104 nnIdx.data(), // nearest neighbors (returned)
105 dists.data(), // distance (returned)
106 eps); // error bound
107
108 for (int i = 0; i < k; i++) { // print summary
109 if (nnIdx[i] == ip) continue;
110 val[nz] = dists[i];
111 irn[nz] = ip;
112 jcn[nz++] = nnIdx[i];
113 }
114 }
115
116 nz0 = nz;
117
118 annDeallocPts(dataPts);
119 annClose(); // done with ANN
120
121
122
123}
static void sortPtsY(int n, ANNpointArray pts)
static void sortPtsX(int n, ANNpointArray pts)
void nearest_neighbor_graph_ann(int nPts, int k, const std::vector< double > &x, int &nz0, std::vector< int > &irn, std::vector< int > &jcn, std::vector< double > &val)
static const int dim