Skip to content

Commit 6f2ca85

Browse files
authored
Merge pull request #2162 from MRtrix3/remove_copy_ptr
Remove copy_ptr and fix handling of masks when more than 3D
2 parents 5800036 + 1b1882b commit 6f2ca85

File tree

4 files changed

+63
-63
lines changed

4 files changed

+63
-63
lines changed

cmd/afdconnectivity.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ class AFDConnectivity : public DWI::Tractography::SIFT::ModelBase<Fixel>
143143
bool all_fixels;
144144
DWI::Tractography::Mapping::TrackMapperBase mapper;
145145
Image<value_type> v_fod;
146-
copy_ptr<DWI::FMLS::Segmenter> fmls;
146+
std::unique_ptr<DWI::FMLS::Segmenter> fmls;
147147

148148
using Fixel_map<Fixel>::accessor;
149149

cmd/dwi2tensor.cpp

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ template <class MASKType, class B0Type, class DKTType, class PredictType>
111111
class Processor { MEMALIGN(Processor)
112112
public:
113113
Processor (const Eigen::MatrixXd& b, const bool ols, const int iter,
114-
MASKType* mask_image, B0Type* b0_image, DKTType* dkt_image, PredictType* predict_image) :
114+
const MASKType& mask_image, const B0Type& b0_image, const DKTType& dkt_image, const PredictType& predict_image) :
115115
mask_image (mask_image),
116116
b0_image (b0_image),
117117
dkt_image (dkt_image),
@@ -128,9 +128,9 @@ class Processor { MEMALIGN(Processor)
128128
template <class DWIType, class DTType>
129129
void operator() (DWIType& dwi_image, DTType& dt_image)
130130
{
131-
if (mask_image) {
132-
assign_pos_of (dwi_image, 0, 3).to (*mask_image);
133-
if (!mask_image->value())
131+
if (mask_image.valid()) {
132+
assign_pos_of (dwi_image, 0, 3).to (mask_image);
133+
if (!mask_image.value())
134134
return;
135135
}
136136

@@ -157,34 +157,34 @@ class Processor { MEMALIGN(Processor)
157157
dt_image.value() = p[dt_image.index(3)];
158158
}
159159

160-
if (b0_image) {
161-
assign_pos_of (dwi_image, 0, 3).to (*b0_image);
162-
b0_image->value() = exp(p[6]);
160+
if (b0_image.valid()) {
161+
assign_pos_of (dwi_image, 0, 3).to (b0_image);
162+
b0_image.value() = exp(p[6]);
163163
}
164164

165-
if (dkt_image) {
166-
assign_pos_of (dwi_image, 0, 3).to (*dkt_image);
165+
if (dkt_image.valid()) {
166+
assign_pos_of (dwi_image, 0, 3).to (dkt_image);
167167
double adc_sq = (p[0]+p[1]+p[2])*(p[0]+p[1]+p[2])/9.0;
168-
for (auto l = Loop(3)(*dkt_image); l; ++l) {
169-
dkt_image->value() = p[dkt_image->index(3)+7]/adc_sq;
168+
for (auto l = Loop(3)(dkt_image); l; ++l) {
169+
dkt_image.value() = p[dkt_image.index(3)+7]/adc_sq;
170170
}
171171
}
172172

173-
if (predict_image) {
174-
assign_pos_of (dwi_image, 0, 3).to (*predict_image);
173+
if (predict_image.valid()) {
174+
assign_pos_of (dwi_image, 0, 3).to (predict_image);
175175
dwi = (b*p).array().exp();
176-
for (auto l = Loop(3)(*predict_image); l; ++l) {
177-
predict_image->value() = dwi[predict_image->index(3)];
176+
for (auto l = Loop(3)(predict_image); l; ++l) {
177+
predict_image.value() = dwi[predict_image.index(3)];
178178
}
179179
}
180180

181181
}
182182

183183
private:
184-
copy_ptr<MASKType> mask_image;
185-
copy_ptr<B0Type> b0_image;
186-
copy_ptr<DKTType> dkt_image;
187-
copy_ptr<PredictType> predict_image;
184+
MASKType mask_image;
185+
B0Type b0_image;
186+
DKTType dkt_image;
187+
PredictType predict_image;
188188
Eigen::VectorXd dwi;
189189
Eigen::VectorXd p;
190190
Eigen::VectorXd w;
@@ -196,7 +196,7 @@ class Processor { MEMALIGN(Processor)
196196
};
197197

198198
template <class MASKType, class B0Type, class DKTType, class PredictType>
199-
inline Processor<MASKType, B0Type, DKTType, PredictType> processor (const Eigen::MatrixXd& b, const bool ols, const int iter, MASKType* mask_image, B0Type* b0_image, DKTType* dkt_image, PredictType* predict_image) {
199+
inline Processor<MASKType, B0Type, DKTType, PredictType> processor (const Eigen::MatrixXd& b, const bool ols, const int iter, const MASKType& mask_image, const B0Type& b0_image, const DKTType& dkt_image, const PredictType& predict_image) {
200200
return { b, ols, iter, mask_image, b0_image, dkt_image, predict_image };
201201
}
202202

@@ -205,11 +205,11 @@ void run ()
205205
auto dwi = Header::open (argument[0]).get_image<value_type>();
206206
auto grad = DWI::get_DW_scheme (dwi);
207207

208-
Image<bool>* mask = nullptr;
208+
Image<bool> mask;
209209
auto opt = get_options ("mask");
210210
if (opt.size()) {
211-
mask = new Image<bool> (Image<bool>::open (opt[0][0]));
212-
check_dimensions (dwi, *mask, 0, 3);
211+
mask = Image<bool>::open (opt[0][0]);
212+
check_dimensions (dwi, mask, 0, 3);
213213
}
214214

215215
bool ols = get_options ("ols").size();
@@ -223,27 +223,27 @@ void run ()
223223
DWI::stash_DW_scheme (header, grad);
224224
PhaseEncoding::clear_scheme (header);
225225

226-
Image<value_type>* predict = nullptr;
226+
Image<value_type> predict;
227227
opt = get_options ("predicted_signal");
228228
if (opt.size())
229-
predict = new Image<value_type> (Image<value_type>::create (opt[0][0], header));
229+
predict = Image<value_type>::create (opt[0][0], header);
230230

231231
header.size(3) = 6;
232232
auto dt = Image<value_type>::create (argument[1], header);
233233

234-
Image<value_type>* b0 = nullptr;
234+
Image<value_type> b0;
235235
opt = get_options ("b0");
236236
if (opt.size()) {
237237
header.ndim() = 3;
238-
b0 = new Image<value_type> (Image<value_type>::create (opt[0][0], header));
238+
b0 = Image<value_type>::create (opt[0][0], header);
239239
}
240240

241-
Image<value_type>* dkt = nullptr;
241+
Image<value_type> dkt;
242242
opt = get_options ("dkt");
243243
if (opt.size()) {
244244
header.ndim() = 4;
245245
header.size(3) = 15;
246-
dkt = new Image<value_type> (Image<value_type>::create (opt[0][0], header));
246+
dkt = Image<value_type>::create (opt[0][0], header);
247247
}
248248

249249
Eigen::MatrixXd b = -DWI::grad2bmatrix<double> (grad, opt.size()>0);

cmd/dwidenoise.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ class DenoisingFunctor {
143143
{
144144
// Process voxels in mask only
145145
if (mask.valid()) {
146-
assign_pos_of (dwi).to (mask);
146+
assign_pos_of (dwi, 0, 3).to (mask);
147147
if (!mask.value())
148148
return;
149149
}
@@ -196,7 +196,7 @@ class DenoisingFunctor {
196196

197197
// store noise map if requested:
198198
if (noise.valid()) {
199-
assign_pos_of(dwi).to(noise);
199+
assign_pos_of(dwi, 0, 3).to(noise);
200200
noise.value() = real_type (std::sqrt(sigma2));
201201
}
202202
}

cmd/sh2peaks.cpp

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,9 @@ class Item { MEMALIGN(Item)
122122
class DataLoader { MEMALIGN(DataLoader)
123123
public:
124124
DataLoader (Image<value_type>& sh_data,
125-
Image<bool>* mask_data) :
125+
const Image<bool>& mask_data) :
126126
sh (sh_data),
127+
mask (mask_data),
127128
loop (Loop("estimating peak directions", 0, 3) (sh)) { }
128129

129130
bool operator() (Item& item) {
@@ -133,12 +134,11 @@ class DataLoader { MEMALIGN(DataLoader)
133134
item.pos[1] = sh.index(1);
134135
item.pos[2] = sh.index(2);
135136

136-
if (mask) {
137-
assign_pos_of(sh).to(*mask);
138-
if (!mask->value()) {
139-
for (auto l = Loop(3) (sh); l; ++l)
140-
item.data[sh.index(3)] = NaN;
141-
}
137+
if (mask.valid())
138+
assign_pos_of(sh, 0, 3).to(mask);
139+
if (mask.valid() && !mask.value()) {
140+
for (auto l = Loop(3) (sh); l; ++l)
141+
item.data[sh.index(3)] = NaN;
142142
} else {
143143
// iterates over SH coefficients
144144
for (auto l = Loop(3) (sh); l; ++l)
@@ -154,7 +154,7 @@ class DataLoader { MEMALIGN(DataLoader)
154154

155155
private:
156156
Image<value_type> sh;
157-
std::unique_ptr<Image<bool> > mask;
157+
Image<bool> mask;
158158
LoopAlongAxisRangeProgress::Run<Image<value_type> > loop;
159159
};
160160

@@ -169,7 +169,7 @@ class Processor { MEMALIGN(Processor)
169169
int npeaks,
170170
vector<Direction> true_peaks,
171171
value_type threshold,
172-
Image<value_type>* ipeaks_data,
172+
Image<value_type> ipeaks_data,
173173
bool use_precomputer) :
174174
dirs_vox (dirs_data),
175175
dirs (directions),
@@ -210,17 +210,17 @@ class Processor { MEMALIGN(Processor)
210210
all_peaks.push_back (p);
211211
}
212212

213-
if (ipeaks_vox) {
214-
ipeaks_vox->index(0) = item.pos[0];
215-
ipeaks_vox->index(1) = item.pos[1];
216-
ipeaks_vox->index(2) = item.pos[2];
213+
if (ipeaks_vox.valid()) {
214+
ipeaks_vox.index(0) = item.pos[0];
215+
ipeaks_vox.index(1) = item.pos[1];
216+
ipeaks_vox.index(2) = item.pos[2];
217217

218218
for (int i = 0; i < npeaks; i++) {
219219
Eigen::Vector3f p;
220-
ipeaks_vox->index(3) = 3*i;
220+
ipeaks_vox.index(3) = 3*i;
221221
for (int n = 0; n < 3; n++) {
222-
p[n] = ipeaks_vox->value();
223-
ipeaks_vox->index(3)++;
222+
p[n] = ipeaks_vox.value();
223+
ipeaks_vox.index(3)++;
224224
}
225225
p.normalize();
226226

@@ -270,16 +270,16 @@ class Processor { MEMALIGN(Processor)
270270
vector<Direction> true_peaks;
271271
value_type threshold;
272272
vector<Direction> peaks_out;
273-
copy_ptr<Image<value_type> > ipeaks_vox;
273+
Image<value_type> ipeaks_vox;
274274
Math::SH::PrecomputedAL<value_type>* precomputer;
275275

276276
bool check_input (const Item& item) {
277-
if (ipeaks_vox) {
278-
ipeaks_vox->index(0) = item.pos[0];
279-
ipeaks_vox->index(1) = item.pos[1];
280-
ipeaks_vox->index(2) = item.pos[2];
281-
ipeaks_vox->index(3) = 0;
282-
if (std::isnan (value_type (ipeaks_vox->value())))
277+
if (ipeaks_vox.valid()) {
278+
ipeaks_vox.index(0) = item.pos[0];
279+
ipeaks_vox.index(1) = item.pos[1];
280+
ipeaks_vox.index(2) = item.pos[2];
281+
ipeaks_vox.index(3) = 0;
282+
if (std::isnan (value_type (ipeaks_vox.value())))
283283
return true;
284284
}
285285

@@ -308,9 +308,9 @@ void run ()
308308

309309
auto opt = get_options ("mask");
310310

311-
std::unique_ptr<Image<bool> > mask_data;
311+
Image<bool> mask_data;
312312
if (opt.size())
313-
mask_data.reset (new Image<bool>(Image<bool>::open (opt[0][0])));
313+
mask_data = Image<bool>::open (opt[0][0]);
314314

315315
opt = get_options ("seeds");
316316
Eigen::Matrix<value_type, Eigen::Dynamic, 2> dirs;
@@ -339,22 +339,22 @@ void run ()
339339
header.datatype() = DataType::Float32;
340340

341341
opt = get_options ("peaks");
342-
std::unique_ptr<Image<value_type> > ipeaks_data;
342+
Image<value_type> ipeaks_data;
343343
if (opt.size()) {
344344
if (true_peaks.size())
345345
throw Exception ("you can't specify both a peaks file and orientations to be estimated at the same time");
346346
if (opt.size())
347-
ipeaks_data.reset (new Image<value_type> (Image<value_type>::open(opt[0][0])));
347+
ipeaks_data = Image<value_type>::open(opt[0][0]);
348348

349-
check_dimensions (SH_data, *ipeaks_data, 0, 3);
350-
npeaks = ipeaks_data->size (3) / 3;
349+
check_dimensions (SH_data, ipeaks_data, 0, 3);
350+
npeaks = ipeaks_data.size (3) / 3;
351351
}
352352
header.size(3) = 3 * npeaks;
353353
auto peaks = Image<value_type>::create (argument[1], header);
354354

355-
DataLoader loader (SH_data, mask_data.get());
355+
DataLoader loader (SH_data, mask_data);
356356
Processor processor (peaks, dirs, Math::SH::LforN (SH_data.size (3)),
357-
npeaks, true_peaks, threshold, ipeaks_data.get(), get_options("fast").size());
357+
npeaks, true_peaks, threshold, ipeaks_data, get_options("fast").size());
358358

359359
Thread::run_queue (loader, Thread::batch (Item()), Thread::multi (processor));
360360
}

0 commit comments

Comments
 (0)