-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdendnode.cpp
More file actions
91 lines (61 loc) · 1.9 KB
/
dendnode.cpp
File metadata and controls
91 lines (61 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
Created by: Jason Carlisle Mann (on2valhalla | jcm2207@columbia.edu)
Dendnode source
*/
#include "dendnode.h"
std::ostream& operator<< (std::ostream &o, const DendNode &n)
{
o << "{ ";
for (std::vector<int>::const_iterator it = n.idxs.begin(); it != n.idxs.end(); it++)
o << *it << ", ";
o << "} d:" << n.distance;
return o;
}
void DendNode::toJson(std::string fileName)
{
std::ofstream outputFile;
std::stringstream links;
std::cout << "Writing JSON to: " << fileName << std::endl;
outputFile.open(fileName.c_str(), std::ofstream::trunc | std::ofstream::out);
outputFile << "{\"nodes\":[\n";
links << "\"links\":[\n";
int group = 0;
float distTrack = 0;
recursiveWriter(this, outputFile, links, &group, &distTrack);
//join the nodes and the links
//get rid of final commas
long pos = outputFile.tellp();
outputFile.seekp(pos-3);
pos = links.tellp();
links.seekp(pos-3);
links << "\n";
std::string linkStr = links.str();
outputFile << "\n],\n" << linkStr << "]}";
outputFile.close();
}
int DendNode::recursiveWriter(DendNode *node, std::ofstream &file, std::stringstream &links, int *group, float *distTrack)
{
// leaf node terminating condition
if (node->distance == -1)
{
int idx = node->idxs[0] +1;
std::string addr;
if(idx< 10)
addr = ", \"img\":\"http://0.0.0.0:8000/img/i0";
else
addr = ", \"img\":\"http://0.0.0.0:8000/img/i";
file << "\t{\"name\":" << "\"" << idx << "\", \"group\":" << *group
<< addr << idx << ".jpg\"}, \n";
return idx -1;
}
if (fabs(*distTrack - node->distance) > .05)
{
*distTrack = node->distance;
(*group)++;
}
int left = recursiveWriter(node->left, file, links, group, distTrack);
int right = recursiveWriter(node->right, file, links, group, distTrack);
links << "\t{\"source\":" << left << ", \"target\":" << right
<< ", \"value\":" << (1-node->distance) * VALUE << "}, \n";
return (left > right) ? left : right;
}