CiftiLib
A C++ library for CIFTI-2 and CIFTI-1 files
CiftiBrainModelsMap.h
1 #ifndef __CIFTI_BRAIN_MODELS_MAP_H__
2 #define __CIFTI_BRAIN_MODELS_MAP_H__
3 
4 /*LICENSE_START*/
5 /*
6  * Copyright (c) 2014, Washington University School of Medicine
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without modification,
10  * are permitted provided that the following conditions are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include "CiftiMappingType.h"
32 
33 #include "Common/Compact3DLookup.h"
34 #include "StructureEnum.h"
35 #include "VolumeSpace.h"
36 
37 #include <map>
38 #include <utility>
39 #include <vector>
40 
41 namespace cifti
42 {
44  {
45  public:
46  enum ModelType
47  {
48  SURFACE,
49  VOXELS
50  };
51  struct SurfaceMap
52  {
53  int64_t m_ciftiIndex;
54  int64_t m_surfaceNode;
55  };
56  struct VolumeMap
57  {
58  int64_t m_ciftiIndex;
59  int64_t m_ijk[3];
60  };
61  struct ModelInfo
62  {
63  ModelType m_type;
64  StructureEnum::Enum m_structure;
65  int64_t m_indexStart, m_indexCount;//these are intended only for summary info, use getSurfaceMap, etc for the index to vertex/voxel mappings
66  };
67  struct IndexInfo
68  {
69  ModelType m_type;
70  StructureEnum::Enum m_structure;
71  int64_t m_surfaceNode;//only one of these two will be valid
72  int64_t m_ijk[3];
73  };
74  bool hasVolumeData() const;
75  bool hasVolumeData(const StructureEnum::Enum& structure) const;
76  bool hasSurfaceData(const StructureEnum::Enum& structure) const;
77  int64_t getIndexForNode(const int64_t& node, const StructureEnum::Enum& structure) const;
78  int64_t getIndexForVoxel(const int64_t* ijk, StructureEnum::Enum* structureOut = NULL) const;
79  int64_t getIndexForVoxel(const int64_t& i, const int64_t& j, const int64_t& k, StructureEnum::Enum* structureOut = NULL) const;
80  IndexInfo getInfoForIndex(const int64_t index) const;
81  std::vector<SurfaceMap> getSurfaceMap(const StructureEnum::Enum& structure) const;
82  std::vector<VolumeMap> getFullVolumeMap() const;
83  std::vector<VolumeMap> getVolumeStructureMap(const StructureEnum::Enum& structure) const;
84  const VolumeSpace& getVolumeSpace() const;
85  int64_t getSurfaceNumberOfNodes(const StructureEnum::Enum& structure) const;
86  std::vector<StructureEnum::Enum> getSurfaceStructureList() const;
87  std::vector<StructureEnum::Enum> getVolumeStructureList() const;
88  const std::vector<int64_t>& getNodeList(const StructureEnum::Enum& structure) const;//useful for copying mappings to a new dense mapping
89  const std::vector<int64_t>& getVoxelList(const StructureEnum::Enum& structure) const;
90  std::vector<ModelInfo> getModelInfo() const;
91 
92  CiftiBrainModelsMap() { m_haveVolumeSpace = false; m_ignoreVolSpace = false; }
93  void addSurfaceModel(const int64_t& numberOfNodes, const StructureEnum::Enum& structure, const float* roi = NULL);
94  void addSurfaceModel(const int64_t& numberOfNodes, const StructureEnum::Enum& structure, const std::vector<int64_t>& nodeList);
95  void addVolumeModel(const StructureEnum::Enum& structure, const std::vector<int64_t>& ijkList);
96  void setVolumeSpace(const VolumeSpace& space);
97  void clear();
98 
99  CiftiMappingType* clone() const { return new CiftiBrainModelsMap(*this); }
100  MappingType getType() const { return BRAIN_MODELS; }
101  int64_t getLength() const;
102  bool operator==(const CiftiMappingType& rhs) const;
103  bool approximateMatch(const CiftiMappingType& rhs, AString* explanation = NULL) const;
104  void readXML1(XmlReader& xml);
105  void readXML2(XmlReader& xml);
106  void writeXML1(XmlWriter& xml) const;
107  void writeXML2(XmlWriter& xml) const;
108  private:
109  struct BrainModelPriv
110  {
111  ModelType m_type;
112  StructureEnum::Enum m_brainStructure;
113  int64_t m_surfaceNumberOfNodes;
114  std::vector<int64_t> m_nodeIndices;
115  std::vector<int64_t> m_voxelIndicesIJK;
116 
117  int64_t m_modelStart, m_modelEnd;//stuff only needed for optimization - models are kept in sorted order by their index ranges
118  std::vector<int64_t> m_nodeToIndexLookup;
119  bool operator==(const BrainModelPriv& rhs) const;
120  bool operator!=(const BrainModelPriv& rhs) const { return !((*this) == rhs); }
121  void setupSurface(const int64_t& start);
122  };
123  VolumeSpace m_volSpace;
124  bool m_haveVolumeSpace, m_ignoreVolSpace;//second is needed for parsing cifti-1
125  std::vector<BrainModelPriv> m_modelsInfo;
126  std::map<StructureEnum::Enum, int> m_surfUsed, m_volUsed;
127  Compact3DLookup<std::pair<int64_t, StructureEnum::Enum> > m_voxelToIndexLookup;//make one unified lookup rather than separate lookups per volume structure
128  int64_t getNextStart() const;
129  struct ParseHelperModel
130  {//specifically to allow the parsed elements to be sorted before using addSurfaceModel/addVolumeModel
131  ModelType m_type;
132  StructureEnum::Enum m_brainStructure;
133  int64_t m_surfaceNumberOfNodes;
134  std::vector<int64_t> m_nodeIndices;
135  std::vector<int64_t> m_voxelIndicesIJK;
136  int64_t m_offset, m_count;
137  bool operator<(const ParseHelperModel& rhs) const
138  {
139  if (m_offset < rhs.m_offset) return true;
140  if (m_offset > rhs.m_offset) return false;//get the common cases first
141  if (m_count < rhs.m_count) return true;//in case we have a zero-length model - this shouldn't happen, usually
142  return false;
143  }
144  void parseBrainModel1(XmlReader& xml);
145  void parseBrainModel2(XmlReader& xml);
146  static std::vector<int64_t> readIndexArray(XmlReader& xml);
147  };
148  };
149 }
150 
151 #endif //__CIFTI_BRAIN_MODELS_MAP_H__
Definition: CiftiBrainModelsMap.h:44
Definition: CiftiMappingType.h:39
Enum
Definition: StructureEnum.h:49
Definition: VolumeSpace.h:42
namespace for all CiftiLib functionality
Definition: CiftiBrainModelsMap.h:42
Definition: CiftiBrainModelsMap.h:68
Definition: CiftiBrainModelsMap.h:62
Definition: CiftiBrainModelsMap.h:52
Definition: CiftiBrainModelsMap.h:57