ITK 6.0.0
Insight Toolkit
 
Loading...
Searching...
No Matches
itkTrxStreamlineData.h
Go to the documentation of this file.
1/*=========================================================================
2 *
3 * Copyright NumFOCUS
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0.txt
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 *=========================================================================*/
18#ifndef itkTrxStreamlineData_h
19#define itkTrxStreamlineData_h
20
21#include "TractographyTRXExport.h"
22
23#include "itkDataObject.h"
24#include "itkIntTypes.h"
25#include "itkFixedArray.h"
26#include "itkMatrix.h"
27#include "itkPoint.h"
28#include "itkTrxGroup.h"
29
30#include "itk_eigen.h"
31#include ITK_EIGEN(Core)
32#include "vnl/vnl_matrix.h"
33#include <array>
34#include <cstddef>
35#include <functional>
36#include <iterator>
37#include <map>
38#include <memory>
39#include <string>
40#include <variant>
41#include <vector>
42
43namespace itk
44{
45class TrxHandleBase;
46class TrxStreamWriter;
61class TractographyTRX_EXPORT TrxStreamlineData : public DataObject
62{
63public:
64 ITK_DISALLOW_COPY_AND_MOVE(TrxStreamlineData);
65
70
71 itkNewMacro(Self);
72 itkOverrideGetNameOfClassMacro(TrxStreamlineData);
73
80
82 {
83 RAS, // TRX native space
84 LPS // ITK-preferred space
85 };
86
87 using OffsetType = uint64_t;
92 using StreamlineType = std::vector<PointType>;
93 using AabbType = std::array<double, 6>;
94
102
103 class TractographyTRX_EXPORT StreamlinePointRange
104 {
105 public:
106 class TractographyTRX_EXPORT Iterator
107 {
108 public:
109 using iterator_category = std::forward_iterator_tag;
111 using difference_type = std::ptrdiff_t;
112 using pointer = void;
114
115 Iterator() = default;
117
119 operator*() const;
120
121 Iterator &
123
124 bool
125 operator==(const Iterator & other) const;
126
127 bool
128 operator!=(const Iterator & other) const;
129
130 private:
131 const TrxStreamlineData * m_Data{ nullptr };
133 };
134
137
139 begin() const;
140
142 end() const;
143
144 private:
145 const TrxStreamlineData * m_Data{ nullptr };
148 };
149
150 std::vector<PointType>
151 GetStreamline(SizeValueType streamlineIndex) const;
152
154 GetStreamlineRange(SizeValueType streamlineIndex) const;
155
157 bool
158 GetStreamlineView(SizeValueType streamlineIndex, StreamlineView & view) const;
159
165 void
167 const std::function<void(SizeValueType, const void *, SizeValueType, CoordinateType, CoordinateSystem)> & fn,
168 CoordinateSystem requestedSystem = CoordinateSystem::LPS) const;
169
170 void
171 SetTrxHandle(const std::shared_ptr<TrxHandleBase> & trxHandle);
172
173 bool
175
176 void
177 Save(const std::string & filename, bool useCompression) const;
178
181
182 bool
184
185 bool
187
188 bool
190
191 const std::vector<Eigen::half> *
193
194 const std::vector<float> *
196
197 const std::vector<double> *
199
200 std::vector<Eigen::half> *
202
203 std::vector<float> *
205
206 std::vector<double> *
208
209 const std::vector<OffsetType> &
210 GetOffsets() const;
211
212 void
213 SetOffsets(std::vector<OffsetType> && offsets);
214
217
220
221 Pointer
222 SubsetStreamlines(const std::vector<uint32_t> & streamlineIds, bool buildCacheForResult = false) const;
223
228 Pointer
229 SubsetStreamlinesLazy(const std::vector<uint32_t> & streamlineIds, bool buildCacheForResult = false) const;
230
231 Pointer
232 QueryAabb(const PointType & minCornerLps,
233 const PointType & maxCornerLps,
234 bool buildCacheForResult = false) const;
235
236 Pointer
237 QueryAabb(const PointType & minCornerLps,
238 const PointType & maxCornerLps,
239 bool buildCacheForResult,
240 size_t maxStreamlines,
241 uint32_t rngSeed) const;
242
243 const std::vector<AabbType> &
245
246 void
248
249 void
251
252 const MatrixType &
254
255 bool
257
258 void
260
261 const MatrixType &
263
264 bool
266
267 void
269
270 const DimensionsType &
272
273 bool
275
276 void
278
281
282 void
284
285 template <typename TTransform>
286 void
287 TransformInPlace(const TTransform * transform);
288
289 template <typename TTransform>
290 void
291 TransformInPlaceChunked(const TTransform * transform, SizeValueType chunkPoints);
292
293 template <typename TTransform>
294 void
295 TransformToWriterChunked(const TTransform * transform,
296 class TrxStreamWriter * writer,
297 SizeValueType reservePoints = 0) const;
298
299 template <typename TTransform>
300 void
301 TransformToWriterChunkedReuseBuffer(const TTransform * transform,
302 class TrxStreamWriter * writer,
303 StreamlineType & buffer,
304 SizeValueType reservePoints = 0) const;
305
307 template <typename TTransform>
308 void
309 TransformToWriterChunkedReuseVnlBuffer(const TTransform * transform,
310 class TrxStreamWriter * writer,
311 vnl_matrix<double> & buffer) const;
312
314 void
315 Graft(const DataObject * data) override;
316
317 // -----------------------------------------------------------------------
318 // Group API
319 // -----------------------------------------------------------------------
320
322 bool
323 HasGroups() const;
324
326 std::vector<std::string>
328
334 GetGroup(const std::string & name) const;
335
338 GetGroupStreamlineCount(const std::string & name) const;
339
340 // -----------------------------------------------------------------------
341 // DPS / DPV field enumeration
342 // -----------------------------------------------------------------------
343
345 std::vector<std::string>
347
349 std::vector<std::string>
351
352 // -----------------------------------------------------------------------
353 // DPS / DPV value retrieval (widened to float)
354 // -----------------------------------------------------------------------
355
361 std::vector<float>
362 GetDpsField(const std::string & name) const;
363
369 std::vector<float>
370 GetDpvField(const std::string & name) const;
371
379 {
381 std::vector<std::string> groupNames;
388 vnl_matrix<double> matrix;
394 vnl_matrix<double> streamlineCounts;
395 };
396
407 ComputeGroupConnectivity(const std::string & dpsFieldName = "") const;
408
409protected:
410 TrxStreamlineData() = default;
411 ~TrxStreamlineData() override = default;
412
413 void
414 PrintSelf(std::ostream & os, Indent indent) const override;
415
416
417private:
418 using PositionStorageType = std::variant<std::vector<Eigen::half>, std::vector<float>, std::vector<double>>;
419
420 void
422
423 void
425
428
430 std::vector<OffsetType> m_Offsets;
433 std::shared_ptr<TrxHandleBase> m_TrxHandle;
434 mutable bool m_PositionsLoaded{ true };
435 mutable std::vector<AabbType> m_AabbCache;
436 mutable bool m_AabbCacheValid{ false };
437
441 bool m_HasVoxelToRas{ false };
442 bool m_HasVoxelToLps{ false };
443 bool m_HasDimensions{ false };
445
446 mutable std::map<std::string, TrxGroup::Pointer> m_GroupCache;
447 std::vector<std::string> m_GroupNames;
448 std::vector<std::string> m_DpsFieldNames;
449 std::vector<std::string> m_DpvFieldNames;
450};
451
452std::shared_ptr<TrxHandleBase>
453LoadTrxHandle(const std::string & path);
454
455} // end namespace itk
456
457#ifndef ITK_MANUAL_INSTANTIATION
458# include "itkTrxStreamlineData.hxx"
459#endif
460
461#endif
Simulate a standard C array with copy semantics.
Control indentation during Print() invocation.
Definition itkIndent.h:53
A templated class holding a M x N size Matrix.
Definition itkMatrix.h:71
A templated class holding a geometric point in n-Dimensional space.
Definition itkPoint.h:54
Implements transparent reference counting.
SmartPointer< Self > Pointer
Streaming TRX writer that ingests ITK streamlines and metadata.
bool operator!=(const Iterator &other) const
bool operator==(const Iterator &other) const
Iterator(const TrxStreamlineData *data, SizeValueType index)
StreamlinePointRange(const TrxStreamlineData *data, SizeValueType start, SizeValueType end)
const std::vector< Eigen::half > * GetFloat16Positions() const
std::vector< PointType > StreamlineType
itk::SizeValueType SizeValueType
std::array< double, 6 > AabbType
Pointer QueryAabb(const PointType &minCornerLps, const PointType &maxCornerLps, bool buildCacheForResult=false) const
SmartPointer< Self > Pointer
void SetVoxelToRasMatrix(const MatrixType &matrix)
SizeValueType GetNumberOfStreamlines() const
std::vector< float > * GetFloat32Positions()
bool HasVoxelToRasMatrix() const
TrxGroup::Pointer GetGroup(const std::string &name) const
void SetCoordinateSystem(CoordinateSystem system)
void TransformInPlace(const TTransform *transform)
bool HasVoxelToLpsMatrix() const
Pointer SubsetStreamlinesLazy(const std::vector< uint32_t > &streamlineIds, bool buildCacheForResult=false) const
bool HasFloat32Positions() const
bool GetStreamlineView(SizeValueType streamlineIndex, StreamlineView &view) const
std::vector< std::string > m_DpsFieldNames
std::vector< std::string > GetDpvFieldNames() const
void Save(const std::string &filename, bool useCompression) const
void SetDimensions(const DimensionsType &dims)
std::variant< std::vector< Eigen::half >, std::vector< float >, std::vector< double > > PositionStorageType
void TransformToWriterChunkedReuseBuffer(const TTransform *transform, class TrxStreamWriter *writer, StreamlineType &buffer, SizeValueType reservePoints=0) const
bool HasFloat64Positions() const
PointType GetPointLpsAtIndex(SizeValueType index) const
FixedArray< uint16_t, 3 > DimensionsType
std::vector< OffsetType > m_Offsets
void SetTrxHandle(const std::shared_ptr< TrxHandleBase > &trxHandle)
void ForEachStreamlineChunked(const std::function< void(SizeValueType, const void *, SizeValueType, CoordinateType, CoordinateSystem)> &fn, CoordinateSystem requestedSystem=CoordinateSystem::LPS) const
SizeValueType GetGroupStreamlineCount(const std::string &name) const
std::vector< std::string > GetGroupNames() const
void TransformToWriterChunkedReuseVnlBuffer(const TTransform *transform, class TrxStreamWriter *writer, vnl_matrix< double > &buffer) const
std::map< std::string, TrxGroup::Pointer > m_GroupCache
void SetVoxelToLpsMatrix(const MatrixType &matrix)
void TransformInPlaceChunked(const TTransform *transform, SizeValueType chunkPoints)
void TransformToWriterChunked(const TTransform *transform, class TrxStreamWriter *writer, SizeValueType reservePoints=0) const
std::vector< PointType > GetStreamline(SizeValueType streamlineIndex) const
Pointer SubsetStreamlines(const std::vector< uint32_t > &streamlineIds, bool buildCacheForResult=false) const
CoordinateType GetCoordinateType() const
bool HasTrxHandle() const
CoordinateSystem GetCoordinateSystem() const
std::vector< std::string > m_GroupNames
const MatrixType & GetVoxelToLpsMatrix() const
SizeValueType GetNumberOfVertices() const
std::vector< Eigen::half > * GetFloat16Positions()
SmartPointer< const Self > ConstPointer
std::vector< float > GetDpsField(const std::string &name) const
bool HasFloat16Positions() const
void PrintSelf(std::ostream &os, Indent indent) const override
const MatrixType & GetVoxelToRasMatrix() const
std::shared_ptr< TrxHandleBase > m_TrxHandle
std::vector< std::string > m_DpvFieldNames
const std::vector< OffsetType > & GetOffsets() const
itk::Point< double, 3 > PointType
std::vector< float > GetDpvField(const std::string &name) const
std::vector< AabbType > m_AabbCache
void InvalidateAabbCache() const
CoordinateSystem m_CoordinateSystem
void Graft(const DataObject *data) override
void EnsurePositionsLoaded() const
bool HasGroups() const
const std::vector< AabbType > & GetOrBuildStreamlineAabbs() const
std::vector< std::string > GetDpsFieldNames() const
void SetOffsets(std::vector< OffsetType > &&offsets)
std::vector< double > * GetFloat64Positions()
~TrxStreamlineData() override=default
GroupConnectivityResult ComputeGroupConnectivity(const std::string &dpsFieldName="") const
PositionStorageType m_Positions
Pointer QueryAabb(const PointType &minCornerLps, const PointType &maxCornerLps, bool buildCacheForResult, vcl_size_t maxStreamlines, uint32_t rngSeed) const
const DimensionsType & GetDimensions() const
bool HasDimensions() const
StreamlinePointRange GetStreamlineRange(SizeValueType streamlineIndex) const
const std::vector< double > * GetFloat64Positions() const
const std::vector< float > * GetFloat32Positions() const
Matrix< double, 4, 4 > MatrixType
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
unsigned long SizeValueType
Definition itkIntTypes.h:82
std::shared_ptr< TrxHandleBase > LoadTrxHandle(const std::string &path)