Developer documentation
Version 3.0.3-105-gd3941f44
spatiallock.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_spatiallock_h__
18#define __gt_spatiallock_h__
19
20#include <Eigen/Dense>
21#include <mutex>
22
23#include "types.h"
24
25
26namespace MR {
27 namespace DWI {
28 namespace Tractography {
29 namespace GT {
30
34 template <typename T = float >
37 public:
38 using value_type = T;
39 using point_type = Eigen::Matrix<value_type, 3, 1>;
40
41 SpatialLock() : _tx(0), _ty(0), _tz(0) { }
42 SpatialLock(const value_type t) : _tx(t), _ty(t), _tz(t) { }
43 SpatialLock(const value_type tx, const value_type ty, const value_type tz) : _tx(tx), _ty(ty), _tz(tz) { }
44
45 ~SpatialLock() {
46 lockcentres.clear();
47 }
48
49 void setThreshold(const value_type t) {
50 _tx = _ty = _tz = t;
51 }
52
53 void setThreshold(const value_type tx, const value_type ty, const value_type tz) {
54 _tx = tx;
55 _ty = ty;
56 _tz = tz;
57 }
58
59
60 struct Guard
62 public:
63 Guard(SpatialLock& l) : lock(l), idx(-1) { }
64
65 ~Guard() {
66 if (idx >= 0)
67 lock.unlock(idx);
68 }
69
70 bool try_lock(const point_type& pos) {
71 return lock.try_lock(pos, idx);
72 }
73
74 bool operator!() const {
75 return (idx == -1);
76 }
77
78 private:
79 SpatialLock& lock;
80 ssize_t idx;
81
82 };
83
84
85 protected:
86 std::mutex mutex;
89
90 bool try_lock(const point_type& pos, ssize_t& idx) {
91 std::lock_guard<std::mutex> lock (mutex);
92 idx = -1;
93 ssize_t i = 0;
94 point_type d;
95 for (auto& x : lockcentres) {
96 if (x.second) {
97 d = x.first - pos;
98 if ((std::fabs(d[0]) < _tx) && (std::fabs(d[1]) < _ty) && (std::fabs(d[2]) < _tz))
99 return false;
100 } else {
101 idx = i;
102 }
103 i++;
104 }
105 if (idx == -1) {
106 idx = lockcentres.size();
107 lockcentres.emplace_back(pos, true);
108 } else {
109 lockcentres[idx].first = pos;
110 lockcentres[idx].second = true;
111 }
112 return true;
113 }
114
115 void unlock(const size_t idx) {
116 lockcentres[idx].second = false;
117 }
118
119
120 };
121
122
123 }
124 }
125 }
126}
127
128#endif // __gt_spatiallock_h__
SpatialLock manages a mutex lock on n positions in 3D space.
Definition: spatiallock.h:36
bool try_lock(const point_type &pos, ssize_t &idx)
Definition: spatiallock.h:90
vector< std::pair< point_type, bool > > lockcentres
Definition: spatiallock.h:87
#define NOMEMALIGN
Definition: memory.h:22
MR::default_type value_type
Definition: typedefs.h:33
Definition: base.h:24
#define MEMALIGN(...)
Definition: types.h:185