diff --git a/scanner/scanner.c b/scanner/scanner.c index 2bc38ba..73b7bfc 100644 --- a/scanner/scanner.c +++ b/scanner/scanner.c @@ -14,9 +14,9 @@ typedef enum { } ClusterTypes; typedef struct Face { - long faceId; long double descriptor[128]; long int clusterId; + long faceId; ClusterTypes clusterType; double *distances; } Face; @@ -111,10 +111,10 @@ RangeQuery(DB, distFunc, Q, eps) { } #endif -FaceLink *RangeQuery(Face *pFaces, long int faceCount, Face *pQ, double eps) { +FaceLink *RangeQuery(Face **ppFaces, long int faceCount, Face *pQ, double eps) { FaceLink *pNeighbors = NULL; for (long int i = 0; i < faceCount; i++) { - Face *pFace = &pFaces[i]; + Face *pFace = ppFaces[i]; if (pFace->faceId == pQ->faceId) { continue; } @@ -147,15 +147,15 @@ long int chainLength(FaceLink *pLink) { return count; } -long int DBSCAN(Face *faces, long int faceCount, double eps, int minPts) { +long int DBSCAN(Face **ppFaces, long int faceCount, double eps, int minPts) { long int C = 0; for (long int i = 0; i < faceCount; i++) { - Face *pFace = &faces[i]; + Face *pFace = ppFaces[i]; if (pFace->clusterType != UNDEFINED) { continue; } - FaceLink *pNeighbors = RangeQuery(faces, faceCount, pFace, eps); + FaceLink *pNeighbors = RangeQuery(ppFaces, faceCount, pFace, eps); long neighborCount = chainLength(pNeighbors); if (neighborCount < minPts) { pFace->clusterType = NOISE; @@ -190,7 +190,7 @@ long int DBSCAN(Face *faces, long int faceCount, double eps, int minPts) { pQ->clusterId = C; pQ->clusterType = EDGE; - FaceLink *pSubNeighbors = RangeQuery(faces, faceCount, pQ, eps); + FaceLink *pSubNeighbors = RangeQuery(ppFaces, faceCount, pQ, eps); neighborCount = chainLength(pSubNeighbors); if (neighborCount >= minPts) { pQ->clusterType = CORE; @@ -241,19 +241,23 @@ int main(int argc, char *argv[]) { closedir(faceDir); } - Face *pFaces = malloc(sizeof(Face) * entries); - if (!pFaces) { + Face **ppFaces = malloc(sizeof(Face *) * entries); + if (!ppFaces) { fprintf(stderr, "Unable to allocate storage face descriptors."); return -1; } - memset(pFaces, 0, sizeof(Face) * entries); for (i = 0; i < entries; i++) { - pFaces[i].distances = malloc(sizeof(*pFaces[i].distances) * entries); - if (!pFaces[i].distances) { + ppFaces[i] = malloc(sizeof(Face)); + memset(ppFaces[i], 0, sizeof(Face)); + } + + for (i = 0; i < entries; i++) { + ppFaces[i]->distances = malloc(sizeof(*ppFaces[i]->distances) * entries); + if (!ppFaces[i]->distances) { fprintf(stderr, "Unable to allocate storage for distance dictionary."); return -1; } - memset(pFaces[i].distances, 0, sizeof(*pFaces[i].distances) * entries); + memset(ppFaces[i]->distances, 0, sizeof(*ppFaces[i]->distances) * entries); } long int count = 0; @@ -281,7 +285,7 @@ int main(int argc, char *argv[]) { char path[1028*2]; sprintf(path, "%s/%s", pathBuf, ent->d_name); maxId = maxId > id ? maxId : id; - if (!readFaceDescriptor(&pFaces[count], id, path)) { + if (!readFaceDescriptor(ppFaces[count], id, path)) { fprintf(stderr, "Unable to read %s.\n", path); continue; } @@ -297,9 +301,9 @@ int main(int argc, char *argv[]) { long double total = 0.0; for (long i = 0; i < entries; i++) { - Face *pLink = &pFaces[i]; + Face *pLink = ppFaces[i]; for (long j = 0; j < entries; j++) { - Face *pTarget = &pFaces[j]; + Face *pTarget = ppFaces[j]; if (i == j) { pLink->distances[i] = 0.0; pTarget->distances[j] = 0.0; @@ -319,10 +323,10 @@ int main(int argc, char *argv[]) { fprintf(stderr, "Average distance: %Lf\n", 1. * total / (entries * entries)); - long int clusters = DBSCAN(pFaces, entries, 0.44L, 2); + long int clusters = DBSCAN(ppFaces, entries, 0.44L, 2); long int undefined = 0, outlier = 0, core = 0, reachable = 0; for (i = 0; i < entries; i++) { - switch (pFaces[i].clusterType) { + switch (ppFaces[i]->clusterType) { case NOISE: outlier++; break; @@ -349,11 +353,11 @@ int main(int argc, char *argv[]) { long nodes = 0; fprintf(stdout, "/* %ld. */ [", i); for (long int j = 0; j < entries; j++) { - if (pFaces[j].clusterId == i) { + if (ppFaces[j]->clusterId == i) { if (nodes == 0) { - fprintf(stdout, "%ld", pFaces[j].faceId); + fprintf(stdout, "%ld", ppFaces[j]->faceId); } else { - fprintf(stdout, ",%ld", pFaces[j].faceId); + fprintf(stdout, ",%ld", ppFaces[j]->faceId); } nodes++; } @@ -396,10 +400,10 @@ int main(int argc, char *argv[]) { char sqlBuf[1024]; int sourceIndex = 0, lines = 0; for (long i = 0; i < entries; i++) { - Face *pLink = &pFaces[i]; + Face *pLink = ppFaces[i]; int targetIndex = 0; for (long j = 0; j < entries; j++) { - Face *pTarget = &pFaces[j]; + Face *pTarget = ppFaces[j]; if (i == j) { pLink->distances[i] = 0.0; pTarget->distances[j] = 0.0;