NPL
Neurological Programs and Libraries
slicer.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright 2014 Micah C Chambers (micahc.vt@gmail.com)
3  *
4  * NPL is free software: you can redistribute it and/or modify it under the
5  * terms of the BSD 2-Clause License available in LICENSE or at
6  * http://opensource.org/licenses/BSD-2-Clause
7  *
8  * @file slicer.h
9  *
10  *****************************************************************************/
11 
12 #ifndef SLICER_H
13 #define SLICER_H
14 
15 #include <vector>
16 #include <cstdint>
17 #include <cstddef>
18 #include <cassert>
19 
20 using namespace std;
21 
22 namespace npl {
23 
31 class Slicer
32 {
33 public:
34 
35  /****************************************
36  *
37  * Constructors
38  *
39  ****************************************/
40 
44  Slicer();
45 
53  Slicer(size_t ndim, const size_t* dim);
54 
62  void setDim(size_t ndim, const size_t* dim);
63 
64  /****************************************
65  *
66  * Query Location
67  *
68  ****************************************/
69 
75  bool isBegin() const { return !isEnd() && m_linpos==m_linfirst; };
76 
83  bool isEnd() const { return m_end; };
84 
91  bool eof() const { return m_end; };
92 
93  /*************************************
94  * Movement
95  ***********************************/
96 
103  Slicer& operator++();
104 
111  Slicer& operator--();
112 
117  void goBegin();
118 
123  void goEnd();
124 
133  void goIndex(size_t len, int64_t* newpos);
134 
140  void goIndex(std::vector<int64_t> newpos);
141 
142  /****************************************
143  *
144  * Actually get the linear location
145  *
146  ***************************************/
147 
154  inline int64_t operator*() const { return m_linpos; };
155 
166  void index(size_t len, int64_t* index) const;
167 
178  void index(size_t len, int* index) const;
179 
190  void index(size_t len, double* index) const;
191 
201  inline void index(std::vector<int64_t>& ind) const
202  {
203  index(ind.size(), ind.data());
204  }
205 
215  inline void index(std::vector<int>& ind) const
216  {
217  index(ind.size(), ind.data());
218  };
219 
230  inline void index(std::vector<double>& ind) const
231  {
232  index(ind.size(), ind.data());
233  };
234 
240  inline int64_t index(size_t dd) const
241  {
242  assert(dd<m_ndim);
243  return m_pos[dd];
244  };
245 
251  inline size_t linIndex() const
252  {
253  return m_linpos;
254  }
255 
261  inline int64_t getStride(size_t dd)
262  {
263  assert(dd < m_ndim); return m_strides[dd];
264  };
265 
266  /***********************************************
267  *
268  * Modification
269  *
270  **********************************************/
271 
285  void setROI(size_t len, const size_t* roisize, const int64_t* roistart = NULL);
286 
294  void setROI(const std::vector<std::pair<int64_t, int64_t>>& roi);
295 
306  void setOrder(const std::vector<size_t>& order, bool revorder = false);
307 
318  void setOrder(std::initializer_list<size_t> order, bool revorder = false);
319 
324  void setOrder();
325 
334  const std::vector<size_t>& getOrder() const { return m_order; } ;
335 
336 protected:
337 
338  /******************************************
339  *
340  * Offset, useful to kernel processing
341  *
342  ******************************************/
343 
344  size_t m_linpos;
345  size_t m_linfirst;
346  std::vector<int64_t> m_pos;
347  bool m_end;
348 
349  size_t m_ndim;
350  std::vector<size_t> m_order;
351  std::vector<std::pair<int64_t,int64_t>> m_roi;
352  std::vector<size_t> m_dim;
353  std::vector<size_t> m_strides;
354 };
355 
379 {
380 public:
381 
382  /****************************************
383  *
384  * Constructors
385  *
386  ****************************************/
387 
391  ChunkSlicer();
392 
400  ChunkSlicer(size_t ndim, const size_t* dim);
401 
410  void setDim(size_t ndim, const size_t* dim);
411 
412  /****************************************
413  *
414  * Query Location
415  *
416  ****************************************/
417 
423  bool isBegin() const { return !isEnd() && m_linpos==m_linfirst; };
424 
431  bool isEnd() const { return m_end; };
432 
439  bool eof() const { return m_end; };
440 
446  bool isChunkBegin() const { return m_linpos==m_chunkfirst; };
447 
453  bool isChunkEnd() const { return m_chunkend; };
454 
460  bool eoc() const { return m_chunkend; };
461 
462  /*************************************
463  * Movement
464  ***********************************/
465 
472  ChunkSlicer& operator++();
473 
480  ChunkSlicer& operator--();
481 
487  ChunkSlicer& nextChunk();
488 
494  ChunkSlicer& prevChunk();
495 
500  void goChunkBegin();
501 
506  void goChunkEnd();
507 
512  void goBegin();
513 
518  void goEnd();
519 
528  void goIndex(size_t len, int64_t* newpos);
529 
535  void goIndex(std::vector<int64_t> newpos);
536 
537  /****************************************
538  *
539  * Actually get the linear location
540  *
541  ***************************************/
542 
549  inline int64_t operator*() const { return m_linpos; };
550 
561  void index(size_t len, int64_t* index) const;
562 
573  void index(size_t len, int* index) const;
574 
585  void index(size_t len, double* index) const;
586 
596  inline void index(std::vector<int64_t>& ind) const
597  {
598  index(ind.size(), ind.data());
599  };
600 
610  inline void index(std::vector<int>& ind) const
611  {
612  index(ind.size(), ind.data());
613  };
614 
624  inline void index(std::vector<double>& ind) const
625  {
626  index(ind.size(), ind.data());
627  };
628 
634  inline int64_t index(size_t dd) const
635  {
636  assert(dd<m_ndim);
637  return m_pos[dd];
638  };
639 
645  inline size_t linIndex() const
646  {
647  return m_linpos;
648  }
649 
655  inline int64_t getStride(size_t dd)
656  {
657  assert(dd < m_ndim);
658  return m_strides[dd];
659  };
660 
661  /***********************************************
662  *
663  * Modification
664  *
665  **********************************************/
666 
679  void setROI(size_t len, const size_t* roisize, const int64_t* roistart = NULL);
680 
688  void setROI(const std::vector<std::pair<int64_t, int64_t>>& roi);
689 
710  void setChunkSize(size_t len, const int64_t* sizes, bool defunity = false);
711 
732  void setChunkSize(size_t len, const size_t* sizes, bool defunity = false);
733 
750  void setLineChunk(size_t dir);
751 
762  void setOrder(const std::vector<size_t>& order, bool revorder = false);
763 
774  void setOrder(std::initializer_list<size_t> order, bool revorder = false);
775 
784  const std::vector<size_t>& getOrder() const { return m_order; } ;
785 
786 protected:
787 
788  /******************************************
789  *
790  * Offset, useful to kernel processing
791  *
792  ******************************************/
793 
794  // used to determine begin/end status
795  size_t m_linfirst;
796  size_t m_chunkfirst;
797  bool m_end;
799 
800  // position
801  size_t m_linpos;
802  std::vector<int64_t> m_pos;
803  std::vector<std::pair<int64_t,int64_t>> m_chunk;
804 
805  // relatively static
806  std::vector<size_t> m_order;
807  std::vector<std::pair<int64_t,int64_t>> m_roi;
808  std::vector<int64_t> m_chunksizes;
809 
810  size_t m_ndim;
811  std::vector<size_t> m_dim;
812  std::vector<size_t> m_strides;
813 };
814 
836 class KSlicer
837 {
838 public:
839 
840  /****************************************
841  *
842  * Constructors
843  *
844  ****************************************/
845 
849  KSlicer();
850 
857  KSlicer(size_t ndim, const size_t* dim);
858 
859  /***************************************************
860  *
861  * Basic Settings
862  *
863  ***************************************************/
864 
871  void setROI(const std::vector<std::pair<int64_t, int64_t>> roi = {});
872 
885  void setROI(size_t len, const size_t* roisize, const int64_t* roistart = NULL);
886 
899  void setOrder(const std::vector<size_t> order = {}, bool revorder = false);
900 
909  const std::vector<size_t>& getOrder() const { return m_order; } ;
910 
923  void setRadius(std::vector<size_t> kradius = {});
924 
936  void setRadius(size_t kradius);
937 
952  void setWindow(const std::vector<std::pair<int64_t, int64_t>>& krange);
953 
954  /****************************************
955  *
956  * Query Location
957  *
958  ****************************************/
959 
965  bool isBegin() const { return m_linpos[m_center] == m_begin; };
966 
973  bool isEnd() const { return m_end; };
974 
981  bool eof() const { return m_end; };
982 
988  inline int64_t linIndex() const
989  {
990  return m_linpos[m_center];
991  }
992 
998  inline int64_t getStride(size_t dd)
999  {
1000  assert(dd < m_ndim);
1001  return m_strides[dd];
1002  };
1003 
1004  /*************************************
1005  * Movement
1006  ***********************************/
1007 
1014  KSlicer& operator++();
1015 
1022  KSlicer& operator--();
1023 
1028  void goBegin();
1029 
1034  void goEnd();
1035 
1041  void goIndex(const std::vector<int64_t>& newpos);
1042 
1043  /****************************************
1044  *
1045  * Actually get the linear location
1046  *
1047  ***************************************/
1048 
1054  inline int64_t getC() const
1055  {
1056  assert(!m_end);
1057  return m_linpos[m_center];
1058  };
1059 
1066  inline int64_t operator*() const
1067  {
1068  assert(!m_end);
1069  return m_linpos[m_center];
1070  };
1071 
1072 
1084  void indexC(size_t len, int64_t* index) const;
1085 
1093  inline int64_t getK(int64_t kit) const
1094  {
1095  assert(!m_end);
1096  assert(kit < m_numoffs);
1097  assert(m_linpos[kit] >= 0);
1098  return m_linpos[kit];
1099  };
1100 
1108  inline int64_t operator[](int64_t kit) const
1109  {
1110  assert(!m_end);
1111  assert(kit < m_numoffs);
1112  assert(m_linpos[kit] >= 0);
1113  return m_linpos[kit];
1114  };
1115 
1130  void indexK(size_t kit, size_t len, int64_t* index, bool bound = true) const;
1131 
1143  int64_t offsetK(size_t kit, size_t dim);
1144 
1154  bool insideK(size_t k);
1155 
1167  void offsetK(size_t kit, size_t len, int64_t* dindex) const;
1168 
1174  size_t ksize() const
1175  {
1176  return m_numoffs;
1177  };
1178 
1185  void setDim(size_t ndim, const size_t* dim);
1186 
1187 protected:
1188 
1189  // order of traversal, constructor initializes
1190  size_t m_ndim; // constructor
1191  std::vector<size_t> m_size; // constructor
1192  std::vector<size_t> m_strides; //constructor
1193 
1194  // setOrder
1195  std::vector<size_t> m_order;
1196 
1197  // setRadius/setWindow
1198  // for each of the neighbors we need to know
1199  size_t m_numoffs; // setRadius/setWindow/
1200 
1201  // NDIM*OFFSET, D0O0, D1O0, D2O0, D0O1, ...
1202  std::vector<int64_t*> m_offs; // setRadius/setWindow
1203  std::vector<int64_t> m_offs_raw; // setRadius/setWindow
1204  size_t m_center; // setRadius/setWindow
1205  int64_t m_fradius; //forward radius, should be positive
1206  int64_t m_rradius; //reverse radius, should be positive
1207 
1208  // setROI
1209  std::vector<std::pair<int64_t,int64_t>> m_roi;
1210  size_t m_begin;
1211 
1212  // goBegin/goEnd/goIndex
1213  bool m_end;
1214  std::vector<int64_t*> m_pos;
1215  std::vector<int64_t> m_pos_raw;
1216  std::vector<int64_t> m_linpos;
1217 
1218 };
1219 
1220 } // npl
1221 
1222 #endif //SLICER_H
1223 
std::vector< size_t > m_strides
Definition: slicer.h:353
const std::vector< size_t > & getOrder() const
Returns the array giving the order of dimension being traversed. So 3,2,1,0 would mean that the next ...
Definition: slicer.h:909
void index(std::vector< int64_t > &ind) const
Places the first len dimension in the given array. If the number of dimensions exceed the len then th...
Definition: slicer.h:596
Definition: accessors.h:29
int64_t operator*() const
Get image linear index of center. Identitcal to center() just more confusing.
Definition: slicer.h:1066
std::vector< int64_t > m_pos
Definition: slicer.h:802
bool isBegin() const
Are we at the begining of iteration?
Definition: slicer.h:75
std::vector< std::pair< int64_t, int64_t > > m_chunk
Definition: slicer.h:803
std::vector< int64_t * > m_offs
Definition: slicer.h:1202
size_t m_linfirst
Definition: slicer.h:345
std::vector< int64_t > m_linpos
Definition: slicer.h:1216
std::vector< size_t > m_dim
Definition: slicer.h:811
std::vector< std::pair< int64_t, int64_t > > m_roi
Definition: slicer.h:351
bool isBegin() const
Are we at the begining of iteration?
Definition: slicer.h:965
void index(std::vector< double > &ind) const
Places the first len dimension in the given array. If the number of dimensions exceed the len then th...
Definition: slicer.h:624
This class is used to step through an ND array in order of dimensions, but unlike Slicer it breaks th...
Definition: slicer.h:378
STL namespace.
void index(std::vector< int64_t > &ind) const
Places the first len dimension in the given array. If the number of dimensions exceed the len then th...
Definition: slicer.h:201
std::vector< int64_t > m_pos_raw
Definition: slicer.h:1215
int64_t linIndex() const
get linear position of current point
Definition: slicer.h:988
void index(std::vector< double > &ind) const
Places the first len dimension in the given array. If the number of dimensions exceed the len then th...
Definition: slicer.h:230
bool m_end
Definition: slicer.h:1213
std::vector< int64_t > m_chunksizes
Definition: slicer.h:808
size_t m_ndim
Definition: slicer.h:1190
int64_t index(size_t dd) const
Returns the index in the specified dimension.
Definition: slicer.h:240
std::vector< int64_t > m_pos
Definition: slicer.h:346
int64_t getC() const
Get image linear index of center.
Definition: slicer.h:1054
size_t m_chunkfirst
Definition: slicer.h:796
int64_t operator*() const
dereference operator. Returns the linear position in the array given the n-dimensional position...
Definition: slicer.h:549
std::vector< int64_t > m_offs_raw
Definition: slicer.h:1203
bool isEnd() const
Are we at the end of iteration? Note that this will be 1 past the end, as typically is done in c++...
Definition: slicer.h:973
size_t linIndex() const
get linear position of current point
Definition: slicer.h:645
bool isEnd() const
Are we at the end of iteration? Note that this will be 1 past the end, as typically is done in c++...
Definition: slicer.h:431
This class is used to slice an image in along a dimension, and to step an arbitrary direction in an i...
Definition: slicer.h:836
bool eof() const
Are we at the end of iteration? Note that this will be 1 past the end, as typically is done in c++...
Definition: slicer.h:981
int64_t getStride(size_t dd)
get stride in specified dimension
Definition: slicer.h:261
size_t m_ndim
Definition: slicer.h:810
std::vector< std::pair< int64_t, int64_t > > m_roi
Definition: slicer.h:1209
bool isChunkBegin() const
Returns true when we have reached the end of the chunk.
Definition: slicer.h:446
std::vector< size_t > m_strides
Definition: slicer.h:1192
bool isBegin() const
Are we at the begining of iteration?
Definition: slicer.h:423
bool eof() const
Are we at the end of iteration? Note that this will be 1 past the end, as typically is done in c++...
Definition: slicer.h:439
std::vector< std::pair< int64_t, int64_t > > m_roi
Definition: slicer.h:807
bool isChunkEnd() const
Returns true when we have reached the end of the chunk.
Definition: slicer.h:453
int64_t index(size_t dd) const
Returns the index in the specified dimension.
Definition: slicer.h:634
bool eof() const
Are we at the end of iteration? Note that this will be 1 past the end, as typically is done in c++...
Definition: slicer.h:91
bool isEnd() const
Are we at the end of iteration? Note that this will be 1 past the end, as typically is done in c++...
Definition: slicer.h:83
void index(std::vector< int > &ind) const
Places the first len dimension in the given array. If the number of dimensions exceed the len then th...
Definition: slicer.h:215
void index(std::vector< int > &ind) const
Places the first len dimension in the given array. If the number of dimensions exceed the len then th...
Definition: slicer.h:610
int64_t m_rradius
Definition: slicer.h:1206
std::vector< size_t > m_order
Definition: slicer.h:350
bool m_end
Definition: slicer.h:347
int64_t getStride(size_t dd)
get stride in specified dimension
Definition: slicer.h:655
bool m_chunkend
Definition: slicer.h:798
size_t ksize() const
Get linear position.
Definition: slicer.h:1174
std::vector< size_t > m_order
Definition: slicer.h:1195
bool eoc() const
Returns true when we have reached the end of the chunk.
Definition: slicer.h:460
int64_t m_fradius
Definition: slicer.h:1205
size_t linIndex() const
get linear position of current point
Definition: slicer.h:251
size_t m_linpos
Definition: slicer.h:801
size_t m_numoffs
Definition: slicer.h:1199
This class is used to step through an ND array in order of dimensions. Order may be any size from 0 t...
Definition: slicer.h:31
std::vector< size_t > m_size
Definition: slicer.h:1191
std::vector< size_t > m_strides
Definition: slicer.h:812
size_t m_center
Definition: slicer.h:1204
std::vector< int64_t * > m_pos
Definition: slicer.h:1214
size_t m_begin
Definition: slicer.h:1210
std::vector< size_t > m_order
Definition: slicer.h:806
int64_t getK(int64_t kit) const
Get index of i'th kernel (center-offset) element. Note that values outside the image will not be retu...
Definition: slicer.h:1093
int64_t operator[](int64_t kit) const
Same as offset(int64_t kit). Note that values outside the image will not be returned, the nearest inside position will be.
Definition: slicer.h:1108
std::vector< size_t > m_dim
Definition: slicer.h:352
size_t m_ndim
Definition: slicer.h:349
int64_t operator*() const
dereference operator. Returns the linear position in the array given the n-dimensional position...
Definition: slicer.h:154
int64_t getStride(size_t dd)
get stride in specified dimension
Definition: slicer.h:998