/
SliceModel.h
160 lines (154 loc) · 5.16 KB
/
SliceModel.h
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
#ifndef SliceModelH
#define SliceModelH
#include <future>
#include "zlib.h"
#include "TIPL/tipl.hpp"
// ---------------------------------------------------------------------------
class fib_data;
class SliceModel {
public:
fib_data* handle = nullptr;
uint32_t view_id = 0;
bool is_diffusion_space = true;
tipl::matrix<4,4> to_dif,to_slice;
tipl::shape<3> dim;
tipl::vector<3> vs;
tipl::matrix<4,4> trans_to_mni;
public:
bool is_overlay = false;
bool stay = false;
public:
// for directx
tipl::vector<3,int> slice_pos;
bool slice_visible[3];
public:
SliceModel(fib_data* new_handle,uint32_t view_id_);
virtual ~SliceModel(void){}
public:
std::pair<float,float> get_value_range(void) const;
std::pair<float,float> get_contrast_range(void) const;
std::pair<unsigned int,unsigned int> get_contrast_color(void) const;
void set_contrast_range(float min_v,float max_v);
void set_contrast_color(unsigned int min_c,unsigned int max_c);
virtual void get_slice(tipl::color_image& image,
unsigned char cur_dim,int pos,
const std::vector<std::shared_ptr<SliceModel> >& overlay_slices) const;
virtual void get_high_reso_slice(tipl::color_image& image,unsigned char cur_dim,int pos,
const std::vector<std::shared_ptr<SliceModel> >& overlay_slices) const;
virtual tipl::const_pointer_image<3> get_source(void) const;
std::string get_name(void) const;
public:
auto toDiffusionSpace(unsigned char cur_dim,int x,int y) const
{
auto v = tipl::slice2space<tipl::vector<3> >(cur_dim, x, y, slice_pos[cur_dim]);
if(!is_diffusion_space)
{
v.to(to_dif);
v.round();
}
return v;
}
auto toOtherSlice(std::shared_ptr<SliceModel> other_slice,
unsigned char cur_dim,int x,int y) const
{
auto v = tipl::slice2space<tipl::vector<3> >(cur_dim, x, y, slice_pos[cur_dim]);
if(!is_diffusion_space)
v.to(to_dif);
if(!other_slice->is_diffusion_space)
v.to(other_slice->to_slice);
return v;
}
template<typename U,typename V>
auto to3DSpace(unsigned char cur_dim,V x, V y) const
{
return tipl::slice2space<U>(cur_dim, x, y, slice_pos[cur_dim]);
}
public:
void get_other_slice_pos(unsigned char cur_dim,int& x, int& y) const {
x = slice_pos[(cur_dim + 1) % 3];
y = slice_pos[(cur_dim + 2) % 3];
if (cur_dim == 1)
std::swap(x, y);
}
bool set_slice_pos(int x,int y,int z)
{
if(!dim.is_valid(x,y,z))
return false;
bool has_updated = false;
if(slice_pos[0] != x)
{
slice_pos[0] = x;
has_updated = true;
}
if(slice_pos[1] != y)
{
slice_pos[1] = y;
has_updated = true;
}
if(slice_pos[2] != z)
{
slice_pos[2] = z;
has_updated = true;
}
return has_updated;
}
void get_slice_positions(unsigned int cur_dim,std::vector<tipl::vector<3,float> >& points)
{
points.resize(4);
tipl::get_slice_positions(cur_dim, slice_pos[cur_dim],dim,points);
if(!is_diffusion_space)
for(auto& p : points)
p.to(to_dif);
}
void apply_overlay(tipl::color_image& show_image,
unsigned char dim,
std::shared_ptr<SliceModel> other_slice) const;
};
class CustomSliceModel : public SliceModel {
public:
std::string source_file_name,name,error_msg = "unknown error";
public:
std::shared_ptr<std::thread> thread;
tipl::affine_transform<float> arg_min;
bool terminated = true;
bool running = false;
void terminate(void);
void wait(void);
void argmin(void);
void run_registration(void);
void update_transform(void);
public:
CustomSliceModel(fib_data* new_handle);
~CustomSliceModel(void)
{
terminate();
}
bool save_mapping(const char* file_name);
bool load_mapping(const char* file_name);
public:
bool is_mni = false;
tipl::image<3> source_images;
tipl::image<3> skull_removed_images;
tipl::color_image picture,high_reso_picture;
void update_image(void);
virtual void get_slice(tipl::color_image& image,
unsigned char,int,
const std::vector<std::shared_ptr<SliceModel> >& overlay_slices) const;
virtual void get_high_reso_slice(tipl::color_image& image,unsigned char cur_dim,int pos,
const std::vector<std::shared_ptr<SliceModel> >& overlay_slices) const;
virtual tipl::const_pointer_image<3> get_source(void) const;
public:
bool load_slices(const std::vector<std::string>& files,bool is_mni = false);
bool load_slices(const std::string& file,bool is_mni = false)
{
if(!std::filesystem::exists(file))
{
error_msg = "file not exist";
return false;
}
std::vector<std::string> files;
files.push_back(file);
return load_slices(files,is_mni);
}
};
#endif