Developer documentation
Version 3.0.3-105-gd3941f44
particlegrid.h
Go to the documentation of this file.
1/* Copyright (c) 2008-2022 the MRtrix3 contributors.
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 *
7 * Covered Software is provided under this License on an "as is"
8 * basis, without warranty of any kind, either expressed, implied, or
9 * statutory, including, without limitation, warranties that the
10 * Covered Software is free of defects, merchantable, fit for a
11 * particular purpose or non-infringing.
12 * See the Mozilla Public License v. 2.0 for more details.
13 *
14 * For more details, see http://www.mrtrix.org/.
15 */
16
17#ifndef __gt_particlegrid_h__
18#define __gt_particlegrid_h__
19
20#include <mutex>
21
22#include "header.h"
23#include "transform.h"
25#include "math/rng.h"
26
29
30
31namespace MR {
32 namespace DWI {
33 namespace Tractography {
34 namespace GT {
35
41 public:
42
43 using ParticleVectorType = vector<Particle*>;
44
45 template <class HeaderType>
46 ParticleGrid(const HeaderType& image)
47 {
48 DEBUG("Initialise particle grid.");
49 dims[0] = Math::ceil<size_t>( image.size(0) * image.spacing(0) / (2.0*Particle::L) );
50 dims[1] = Math::ceil<size_t>( image.size(1) * image.spacing(1) / (2.0*Particle::L) );
51 dims[2] = Math::ceil<size_t>( image.size(2) * image.spacing(2) / (2.0*Particle::L) );
52 grid.resize(dims[0]*dims[1]*dims[2]);
53
54 // Initialise scanner-to-grid transform
55 Eigen::DiagonalMatrix<default_type, 3> newspacing (2.0*Particle::L, 2.0*Particle::L, 2.0*Particle::L);
56 Eigen::Vector3d shift (image.spacing(0)/2.0 - Particle::L,
57 image.spacing(1)/2.0 - Particle::L,
58 image.spacing(2)/2.0 - Particle::L);
59 T_s2g = image.transform() * newspacing;
60 T_s2g = T_s2g.inverse().translate(shift);
61 }
62
63 ParticleGrid(const ParticleGrid&) = delete;
64 ParticleGrid& operator=(const ParticleGrid&) = delete;
65
67 clear();
68 }
69
70 inline unsigned int getTotalCount() const {
71 return pool.size();
72 }
73
74 void add(const Point_t& pos, const Point_t& dir);
75
76 void shift(Particle* p, const Point_t& pos, const Point_t& dir);
77
78 void remove(Particle* p);
79
80 void clear();
81
82 const ParticleVectorType* at(const ssize_t x, const ssize_t y, const ssize_t z) const;
83
84 inline Particle* getRandom() {
85 return pool.random();
86 }
87
88 void exportTracks(Tractography::Writer<float>& writer);
89
90
91 protected:
92 std::mutex mutex;
97 size_t dims[3];
98
99
100 inline size_t pos2idx(const Point_t& pos) const
101 {
102 size_t x, y, z;
103 pos2xyz(pos, x, y, z);
104 return xyz2idx(x, y, z);
105 }
106
107 public:
108 inline void pos2xyz(const Point_t& pos, size_t& x, size_t& y, size_t& z) const
109 {
110 Point_t gpos = T_s2g.cast<float>() * pos;
111 assert (gpos[0]>=-0.5 && gpos[1]>=-0.5 && gpos[2]>=-0.5);
112 x = Math::round<size_t>(gpos[0]);
113 y = Math::round<size_t>(gpos[1]);
114 z = Math::round<size_t>(gpos[2]);
115 }
116
117 protected:
118 inline size_t xyz2idx(const size_t x, const size_t y, const size_t z) const
119 {
120 return z + dims[2] * (y + dims[1] * x);
121 }
122
123 };
124
125 }
126 }
127 }
128}
129
130#endif // __gt_particlegrid_h__
size_t xyz2idx(const size_t x, const size_t y, const size_t z) const
Definition: particlegrid.h:118
size_t pos2idx(const Point_t &pos) const
Definition: particlegrid.h:100
void pos2xyz(const Point_t &pos, size_t &x, size_t &y, size_t &z) const
Definition: particlegrid.h:108
vector< ParticleVectorType > grid
Definition: particlegrid.h:94
ParticlePool manages creation and deletion of particles, minimizing the no. calls to new/delete.
Definition: particlepool.h:38
class to handle writing tracks to file, with RAM buffer
Definition: file.h:353
random number generator
Definition: rng.h:45
#define DEBUG(msg)
Definition: exception.h:75
Eigen::Vector3f Point_t
Definition: particle.h:29
Definition: base.h:24
Eigen::Transform< default_type, 3, Eigen::AffineCompact > transform_type
the type for the affine transform of an image:
Definition: types.h:234
#define MEMALIGN(...)
Definition: types.h:185