CiftiLib
A C++ library for CIFTI-2 and CIFTI-1 files
VolumeSpace.h
1 #ifndef __VOLUME_SPACE_H__
2 #define __VOLUME_SPACE_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 "Common/Vector3D.h"
32 
33 #include "Common/XmlAdapter.h"
34 
35 #include "stdint.h"
36 #include <vector>
37 
38 namespace cifti
39 {
40 
42  {
43  int64_t m_dims[3];
44  std::vector<std::vector<float> > m_sform, m_inverse;
45  void computeInverse();
46  public:
47  enum OrientTypes
48  {
49  LEFT_TO_RIGHT = 0,
50  RIGHT_TO_LEFT = 4,
51  POSTERIOR_TO_ANTERIOR = 1,
52  ANTERIOR_TO_POSTERIOR = 5,
53  INFERIOR_TO_SUPERIOR = 2,
54  SUPERIOR_TO_INFERIOR = 6
55  };
56  VolumeSpace();
57  VolumeSpace(const int64_t dims[3], const std::vector<std::vector<float> >& sform);
58  VolumeSpace(const int64_t dims[3], const float sform[12]);
59  void setSpace(const int64_t dims[3], const std::vector<std::vector<float> >& sform);
60  void setSpace(const int64_t dims[3], const float sform[12]);
61  const int64_t* getDims() const { return m_dims; }
62  const std::vector<std::vector<float> >& getSform() const { return m_sform; }
63  void getSpacingVectors(Vector3D& iStep, Vector3D& jStep, Vector3D& kStep, Vector3D& origin) const;
64  bool matchesVolumeSpace(const VolumeSpace& right) const;//allows slight mismatches
65  bool operator==(const VolumeSpace& right) const;//requires that it be exact
66  bool operator!=(const VolumeSpace& right) const { return !(*this == right); }
67 
69  bool isPlumb() const;
70 
72  void getOrientAndSpacingForPlumb(OrientTypes* orientOut, float* spacingOut, float* originOut) const;
73 
75  void getOrientation(OrientTypes orientOut[3]) const;
76 
78  template <typename T>
79  inline void indexToSpace(const T* indexIn, float* coordOut) const
80  { indexToSpace<T>(indexIn[0], indexIn[1], indexIn[2], coordOut[0], coordOut[1], coordOut[2]); }
81 
83  template <typename T>
84  inline void indexToSpace(const T& indexIn1, const T& indexIn2, const T& indexIn3, float* coordOut) const
85  { indexToSpace<T>(indexIn1, indexIn2, indexIn3, coordOut[0], coordOut[1], coordOut[2]); }
86 
88  template <typename T>
89  inline void indexToSpace(const T* indexIn, float& coordOut1, float& coordOut2, float& coordOut3) const
90  { indexToSpace<T>(indexIn[0], indexIn[1], indexIn[2], coordOut1, coordOut2, coordOut3); }
91 
93  template <typename T>
94  void indexToSpace(const T& indexIn1, const T& indexIn2, const T& indexIn3, float& coordOut1, float& coordOut2, float& coordOut3) const;
95 
97  inline void spaceToIndex(const float* coordIn, float* indexOut) const { spaceToIndex(coordIn[0], coordIn[1], coordIn[2], indexOut[0], indexOut[1], indexOut[2]); }
99  inline void spaceToIndex(const float& coordIn1, const float& coordIn2, const float& coordIn3, float* indexOut) const { spaceToIndex(coordIn1, coordIn2, coordIn3, indexOut[0], indexOut[1], indexOut[2]); }
101  inline void spaceToIndex(const float* coordIn, float& indexOut1, float& indexOut2, float& indexOut3) const { spaceToIndex(coordIn[0], coordIn[1], coordIn[2], indexOut1, indexOut2, indexOut3); }
103  void spaceToIndex(const float& coordIn1, const float& coordIn2, const float& coordIn3, float& indexOut1, float& indexOut2, float& indexOut3) const;
104 
106  inline void enclosingVoxel(const float* coordIn, int64_t* indexOut) const { enclosingVoxel(coordIn[0], coordIn[1], coordIn[2], indexOut[0], indexOut[1], indexOut[2]); }
108  inline void enclosingVoxel(const float& coordIn1, const float& coordIn2, const float& coordIn3, int64_t* indexOut) const { enclosingVoxel(coordIn1, coordIn2, coordIn3, indexOut[0], indexOut[1], indexOut[2]); }
110  inline void enclosingVoxel(const float* coordIn, int64_t& indexOut1, int64_t& indexOut2, int64_t& indexOut3) const { enclosingVoxel(coordIn[0], coordIn[1], coordIn[2], indexOut1, indexOut2, indexOut3); }
112  void enclosingVoxel(const float& coordIn1, const float& coordIn2, const float& coordIn3, int64_t& indexOut1, int64_t& indexOut2, int64_t& indexOut3) const;
113 
114  template <typename T>
115  inline bool indexValid(const T* indexIn) const
116  {
117  return indexValid(indexIn[0], indexIn[1], indexIn[2]);//implicit cast to int64_t
118  }
119 
121  inline bool indexValid(const int64_t& indexIn1, const int64_t& indexIn2, const int64_t& indexIn3) const
122  {
123  if (indexIn1 < 0 || indexIn1 >= m_dims[0]) return false;
124  if (indexIn2 < 0 || indexIn2 >= m_dims[1]) return false;
125  if (indexIn3 < 0 || indexIn3 >= m_dims[2]) return false;
126  return true;
127  }
128 
129  inline int64_t getIndex(const int64_t& indexIn1, const int64_t& indexIn2, const int64_t& indexIn3) const
130  {
131  return indexIn1 + m_dims[0] * (indexIn2 + m_dims[1] * indexIn3);
132  }
133 
134  template <typename T>
135  inline int64_t getIndex(const T* indexIn) const
136  {
137  return getIndex(indexIn[0], indexIn[1], indexIn[2]);//implicit cast to int64_t
138  }
139 
140  void readCiftiXML1(XmlReader& xml);//xml functions
141  void readCiftiXML2(XmlReader& xml);
142  void writeCiftiXML1(XmlWriter& xml) const;
143  void writeCiftiXML2(XmlWriter& xml) const;
144  };
145 
146  template <typename T>
147  void VolumeSpace::indexToSpace(const T& indexIn1, const T& indexIn2, const T& indexIn3, float& coordOut1, float& coordOut2, float& coordOut3) const
148  {
149  coordOut1 = indexIn1 * m_sform[0][0] + indexIn2 * m_sform[0][1] + indexIn3 * m_sform[0][2] + m_sform[0][3];
150  coordOut2 = indexIn1 * m_sform[1][0] + indexIn2 * m_sform[1][1] + indexIn3 * m_sform[1][2] + m_sform[1][3];
151  coordOut3 = indexIn1 * m_sform[2][0] + indexIn2 * m_sform[2][1] + indexIn3 * m_sform[2][2] + m_sform[2][3];
152  }
153 
154 }
155 
156 #endif //__VOLUME_SPACE_H__
Definition: Vector3D.h:37
Definition: VolumeSpace.h:42
bool isPlumb() const
returns true if volume space is not skew, and each axis and index is separate
Definition: VolumeSpace.cxx:291
void indexToSpace(const T *indexIn, float &coordOut1, float &coordOut2, float &coordOut3) const
returns three coordinates of an index triplet
Definition: VolumeSpace.h:89
void enclosingVoxel(const float *coordIn, int64_t &indexOut1, int64_t &indexOut2, int64_t &indexOut3) const
returns integer indexes of voxel whose center is closest to the coordinate triplet
Definition: VolumeSpace.h:110
void spaceToIndex(const float *coordIn, float &indexOut1, float &indexOut2, float &indexOut3) const
returns three floating point indexes of a given coordinate triplet
Definition: VolumeSpace.h:101
void enclosingVoxel(const float &coordIn1, const float &coordIn2, const float &coordIn3, int64_t *indexOut) const
returns integer index triplet of voxel whose center is closest to the three coordinates
Definition: VolumeSpace.h:108
void getOrientAndSpacingForPlumb(OrientTypes *orientOut, float *spacingOut, float *originOut) const
returns orientation, spacing, and center (spacing/center can be negative, spacing/center is LPI rearr...
Definition: VolumeSpace.cxx:187
void getOrientation(OrientTypes orientOut[3]) const
get just orientation, even for non-plumb volumes
Definition: VolumeSpace.cxx:226
bool indexValid(const int64_t &indexIn1, const int64_t &indexIn2, const int64_t &indexIn3) const
checks if an index is within array dimensions
Definition: VolumeSpace.h:121
void enclosingVoxel(const float *coordIn, int64_t *indexOut) const
returns integer index triplet of voxel whose center is closest to the coordinate triplet
Definition: VolumeSpace.h:106
void spaceToIndex(const float *coordIn, float *indexOut) const
returns floating point index triplet of a given coordinate triplet
Definition: VolumeSpace.h:97
void indexToSpace(const T *indexIn, float *coordOut) const
returns coordinate triplet of an index triplet
Definition: VolumeSpace.h:79
void spaceToIndex(const float &coordIn1, const float &coordIn2, const float &coordIn3, float *indexOut) const
returns floating point index triplet of three given coordinates
Definition: VolumeSpace.h:99
void indexToSpace(const T &indexIn1, const T &indexIn2, const T &indexIn3, float *coordOut) const
returns coordinate triplet of three indices
Definition: VolumeSpace.h:84
namespace for all CiftiLib functionality
Definition: CiftiBrainModelsMap.h:42