-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsnapshot.cpp
180 lines (142 loc) · 5.25 KB
/
snapshot.cpp
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include "snapshot.h"
#include "util.h"
#include "types.h"
#include <itkImageFileWriter.h>
using namespace std;
typedef itk::ImageFileWriter<InputImageType> WriterType;
string s_snapshot_basename;
ImageType::Pointer g_snapshot_image;
void setup_snapshot_image(string basename, ImageType::Pointer model)
{
ImageType::SpacingType spacing = model->GetSpacing();
spacing[0] /= constants::SnapshotImageZoom;
spacing[1] /= constants::SnapshotImageZoom;
spacing[2] /= constants::SnapshotImageZoom;
g_snapshot_image = ImageType::New();
g_snapshot_image->SetSpacing( spacing );
g_snapshot_image->SetOrigin( model->GetOrigin() );
ImageType::RegionType const& region = model->GetLargestPossibleRegion();
ImageType::RegionType::SizeType size = region.GetSize();
// size is in pixels
ImageType::SizeType doubled_size(size);
doubled_size[0] *= constants::SnapshotImageZoom;
doubled_size[1] *= constants::SnapshotImageZoom;
doubled_size[2] *= constants::SnapshotImageZoom;
g_snapshot_image->SetRegions( doubled_size );
g_snapshot_image->Allocate();
s_snapshot_basename = basename;
}
void add_seeds_to_snapshot(Seeds const& seeds,
ImageType::Pointer original_image,
double seeds_emph_factor)
{
for (Seeds::const_iterator it = seeds.begin(), end = seeds.end();
it != end; ++it) {
double seed_density = original_image->GetPixel(*it);
PointType physPoint;
original_image->TransformIndexToPhysicalPoint(*it, physPoint);
ImageType::IndexType index;
g_snapshot_image->TransformPhysicalPointToIndex(physPoint, index);
g_snapshot_image->SetPixel(index, seed_density * seeds_emph_factor);
}
}
// <basename> == eg, 1AGW
string beta_output_name(string basename,
double beta_thickness,
double thickness_flex,
double sigma,
int gaussian_support,
double beta_falloff_factor,
double point_sep
)
{
ostringstream oss;
set_nice_numeric_format(oss);
// &&& Maybe resolution as well, from the file itself.
oss << basename
<< "-bt=" << beta_thickness
<< "-brange=" << thickness_flex
<< "-sig=" << sigma
<< "-supp=" << gaussian_support
<< "-bfal=" << beta_falloff_factor
<< "-sep=" << point_sep
;
return oss.str();
}
void snapshot_beta_points(Nodes const& nodes)
{
// clear it
g_snapshot_image->FillBuffer(0);
for (Nodes::const_iterator it = nodes.begin(), end = nodes.end();
it != end; ++it) {
// &&& Hmm, maybe we can do better than this; make it truly sparse.
ImageType::IndexType index;
g_snapshot_image->TransformPhysicalPointToIndex((*it)->pos(), index);
PixelType density = g_snapshot_image->GetPixel(index);
// There is no natural beta intensity (except maybe beta-like-ness,
// which we don't keep)
density += constants::BetaPointDisplayFakeDensity;
g_snapshot_image->SetPixel(index, density);
}
}
void write_snapshot_image(string fname)
{
WriterType::Pointer writer = WriterType::New();
writer->SetFileName(fname.c_str());
writer->SetInput(g_snapshot_image);
try {
writer->Update();
give_wide_permissions(fname.c_str());
}
catch (itk::ExceptionObject &err) {
std::cout << "ExceptionObject caught !" << std::endl;
std::cout << err << std::endl;
assert(false);
}
}
void maybe_snap_image(unsigned n_betas, Nodes const& nodes)
{
static unsigned s_iSeries = 0;
static unsigned s_snap_at = constants::SnapshotIntervalBase;
static time_t s_then = ::time(0);
if (n_betas == s_snap_at) {
++s_iSeries;
ostringstream oss;
oss << s_snapshot_basename << "." << n_betas << ".vtk";
cout << "==============================================================\n"
<< "DUMPING IMAGE: " << s_snap_at << endl;
snapshot_beta_points(nodes);
write_snapshot_image(s_snapshot_basename + ".vtk");
s_snap_at = constants::SnapshotIntervalBase * unsigned(::pow(constants::SnapshotIntervalPower, s_iSeries));
// If <FinalSnapshot> == 0 indicates infinite
if (constants::FinalSnapshot and constants::FinalSnapshot <= s_iSeries) {
LongEnoughException long_enough;
throw long_enough;
}
}
if (constants::MaxPoints and constants::MaxPoints <= n_betas) {
// ostringstream oss;
// oss << s_snapshot_basename << "." << n_betas << ".vtk";
// write_beta_point_image(nodes, oss.str());
LongEnoughException long_enough;
throw long_enough;
}
if ((n_betas % 1000) == 0) {
time_t now = ::time(0);
time_t elapsed = now - s_then;
g_log << "Progress: " << n_betas << " / " << constants::MaxPoints << "; "
<< elapsed << " secs" << endl;
s_then = now;
}
}
void write_vertices(Nodes const& nodes, string vertex_filename)
{
ofstream of(vertex_filename.c_str());
give_wide_permissions(vertex_filename.c_str());
for (Nodes::const_iterator it = nodes.begin(), end = nodes.end();
it != end; ++it) {
of << (*it)->pos()[0] << " "
<< (*it)->pos()[1] << " "
<< (*it)->pos()[2] << endl;
}
}