Image Component Library (ICL)
DataSegment.h
Go to the documentation of this file.
1 /********************************************************************
2 ** Image Component Library (ICL) **
3 ** **
4 ** Copyright (C) 2006-2013 CITEC, University of Bielefeld **
5 ** Neuroinformatics Group **
6 ** Website: www.iclcv.org and **
7 ** http://opensource.cit-ec.de/projects/icl **
8 ** **
9 ** File : ICLGeom/src/ICLCore/DataSegment.h **
10 ** Module : ICLGeom **
11 ** Authors: Christof Elbrechter, Patrick Nobou **
12 ** **
13 ** **
14 ** GNU LESSER GENERAL PUBLIC LICENSE **
15 ** This file may be used under the terms of the GNU Lesser General **
16 ** Public License version 3.0 as published by the **
17 ** **
18 ** Free Software Foundation and appearing in the file LICENSE.LGPL **
19 ** included in the packaging of this file. Please review the **
20 ** following information to ensure the license requirements will **
21 ** be met: http://www.gnu.org/licenses/lgpl-3.0.txt **
22 ** **
23 ** The development of this software was supported by the **
24 ** Excellence Cluster EXC 277 Cognitive Interaction Technology. **
25 ** The Excellence Cluster EXC 277 is a grant of the Deutsche **
26 ** Forschungsgemeinschaft (DFG) in the context of the German **
27 ** Excellence Initiative. **
28 ** **
29 ********************************************************************/
30 
31 #pragma once
32 
33 #include <ICLUtils/CompatMacros.h>
34 #include <ICLMath/FixedVector.h>
35 #include <ICLCore/CoreFunctions.h>
37 #include <ICLUtils/ClippedCast.h>
38 
39 namespace icl{
40  namespace core{
41 
42 
44 
146  template<class T,int N>
147  struct DataSegment : public DataSegmentBase{
150 
152  inline DataSegment(T *data=0, size_t stride=0, size_t numElements=0, icl32s organizedWidth=-1):
154 
157  return *reinterpret_cast<math::FixedColVector<T,N>*>(data +idx*stride);
158  }
159 
161  inline const math::FixedColVector<T,N> &operator[](int idx) const{
162  return const_cast<DataSegment<T,N> *>(this)->operator[](idx);
163  }
164 
166  inline math::FixedColVector<T,N> &operator()(int x, int y) {
167  return operator[](x + organizedWidth * y );
168  }
169 
171  inline const math::FixedColVector<T,N> &operator()(int x, int y) const{
172  return operator[](x + organizedWidth * y );
173  }
174 
176  template<class OtherT>
177  inline void deepCopy(DataSegment<OtherT,N> dst) const ;
178 
180  inline bool isPacked() const{
181  return stride == sizeof(T)*N;
182  }
183 
185 
187  template<class OtherT>
188  inline bool equals(DataSegment<OtherT,N> dst, float tollerance = 1.0e-5) const {
189  if(getDim() != dst.getDim()) return false;
190  if(isOrganized() != dst.isOrganized()) return false;
191  if(isOrganized() && getSize() != dst.getSize()) return false;
192  const int dim = getDim();
193  for(int i=0;i<dim;++i){
194  for(int j=0;j<N;++j){
195  if(fabs((float)(operator[](i)[j] - dst[i][j])) > tollerance) return false;
196  }
197  }
198  return true;
199  }
200 
202 
209  template<class Fill>
210  inline void fillScalar(Fill scalarValue){
211  const int dim = getDim();
212  for(int i=0;i<dim;++i){
213  for(int j=0;j<N;++j){
214  operator[](i)[j] = scalarValue;
215  }
216  }
217  }
218 
220 
222  template<class Fill>
223  inline void fill(Fill vecValue){
224  const int dim = getDim();
225  for(int i=0;i<dim;++i){
226  operator[](i) = vecValue;
227  }
228  }
229 
231 
233  static DataSegment<T,N> fromVector(std::vector<T> &v){
234  return DataSegment<T,N>(v.data(), N*sizeof(T), v.size()/N);
235  }
236 
238 
240  static const DataSegment<T,N> fromVector(const std::vector<T> &v){
241  return DataSegment<T,N>(v.data(), N*sizeof(T), v.size()/N);
242  }
243 
244  };
245 
246 
248  template<class T, class OtherT, int N>
249  struct DataSegmentDeepCopyUtil{
250  static void copy(const DataSegment<T,N> &src, DataSegment<OtherT,N> &dst){
251  const int dim = src.getDim();
252  const bool sp = src.isPacked(), dp = dst.isPacked();
253  if(sp && dp){
254  const T *s = (const T*)src.begin();
255  T *d = (T*)dst.begin();
256  std::transform(s,s+dim*N,d,icl::utils::clipped_cast<T,OtherT>);
257  }else if(sp){
258  const math::FixedColVector<T,N> *srcpacked = &src[0];
259  for(int i=0;i<dim;++i){
260  dst[i] = srcpacked[i];
261  }
262  }else if(dp){
263  math::FixedColVector<OtherT,N> *dstpacked = &dst[0];
264  for(int i=0;i<dim;++i){
265  dstpacked[i] = src[i];
266  }
267  }else{
268  for(int i=0;i<dim;++i){
269  dst[i] = src[i];
270  }
271  }
272  }
273  };
274 
275  template<class T, int N>
276  struct DataSegmentDeepCopyUtil<T,T,N>{
277  static void copy(const DataSegment<T,N> &src, DataSegment<T,N> &dst){
278  const int dim = src.getDim();
279  const bool sp = src.isPacked(), dp = dst.isPacked();
280  if(sp && dp){
281  memcpy(dst.getDataPointer(),src.getDataPointer(),dim*sizeof(T)*N);
282  }else if(sp){
283  const math::FixedColVector<T,N> *srcpacked = &src[0];
284  for(int i=0;i<dim;++i){
285  dst[i] = srcpacked[i];
286  }
287  }else if(dp){
288  math::FixedColVector<T,N> *dstpacked = &dst[0];
289  for(int i=0;i<dim;++i){
290  dstpacked[i] = src[i];
291  }
292  }else{
293  for(int i=0;i<dim;++i){
294  dst[i] = src[i];
295  }
296  }
297  }
298  };
299 
300  template<class T, int N> template<class OtherT>
301  inline void DataSegment<T,N>::deepCopy(DataSegment<OtherT,N> dst) const {
302  ICLASSERT_THROW(getDim() == dst.getDim(),
303  utils::ICLException("error in DataSegment::deepCopy "
304  "(source and destination dim differ)"));
306  }
307 
308 
311 
316  template<class T>
317  struct DataSegment<T,1> : public DataSegmentBase{
319  typedef T VectorType;
320 
322  inline DataSegment(T *data=0, size_t stride=0, size_t numElements=0, icl32s organizedWidth=-1):
324 
326  inline T &operator[](int idx) {
327  return *reinterpret_cast<T*>(data +idx*stride);
328  }
329 
331  inline const T &operator[](int idx) const{
332  return const_cast<DataSegment<T,1>*>(this)->operator[](idx);
333  }
334 
336  inline T &operator()(int x, int y) {
337  return operator[](x + organizedWidth * y );
338  }
339 
341  inline const T &operator()(int x, int y) const{
342  return operator[](x + organizedWidth * y );
343  }
344 
346  template<class OtherT>
347  inline void deepCopy(DataSegment<OtherT,1> dst) const {
348  ICLASSERT_THROW(getDim() == dst.getDim(),
349  utils::ICLException("error in DataSegment::deepCopy "
350  "(source and destination dim differ)"));
351  const int dim = getDim();
352  for(int i=0;i<dim;++i){
353  dst[i] = operator[](i);
354  }
355  }
356  template<class OtherT>
357  inline bool equals(DataSegment<OtherT,1> dst, float tollerance = 1.0e-5) const {
358  if(getDim() != dst.getDim()) return false;
359  if(isOrganized() != dst.isOrganized()) return false;
360  if(isOrganized() && getSize() != dst.getSize()) return false;
361  const int dim = getDim();
362  for(int i=0;i<dim;++i){
363  if(fabs((float)(operator[](i) - dst[i])) > tollerance) return false;
364  }
365  return true;
366  }
367 
369 
370  template<class Fill>
371  inline void fillScalar(Fill scalarValue){
372  const int dim = getDim();
373  for(int i=0;i<dim;++i){
374  operator[](i) = scalarValue;
375  }
376  }
377 
379  template<class Fill>
380  inline void fill(Fill vecValue){
381  const int dim = getDim();
382  for(int i=0;i<dim;++i){
383  operator[](i) = vecValue;
384  }
385  }
386  };
387 
389  template<class T, int N>
390  const DataSegment<T,N> &DataSegmentBase::as() const{
391  if(dataDepth != icl::core::getDepth<T>()) throw utils::ICLException("invalid cast of data segment (core::depth is wrong)");
392  if(elemDim != N) throw utils::ICLException("invalid cast of data segment (dimension is wrong)");
393  return (DataSegment<T,N> &)(*this);
394  }
395 
400  } // namespace core
401 }
402 
static DataSegment< T, N > fromVector(std::vector< T > &v)
wrapps a data-segment around an std::vector
Definition: DataSegment.h:233
const math::FixedColVector< T, N > & operator[](int idx) const
linear index operator (const)
Definition: DataSegment.h:161
void fillScalar(Fill scalarValue)
fills each scalar value of each entry with given value
Definition: DataSegment.h:210
T & operator[](int idx)
linear index operator (specialized to return a T& directly)
Definition: DataSegment.h:326
undocument this line if you encounter any issues!
Definition: Any.h:37
DataSegment(T *data=0, size_t stride=0, size_t numElements=0, icl32s organizedWidth=-1)
Constructor (basically passes all parameters to the Base class)
Definition: DataSegment.h:152
bool equals(DataSegment< OtherT, N > dst, float tollerance=1.0e-5) const
compares two data segments element wise given given maximun tollerance
Definition: DataSegment.h:188
icl32s organizedWidth
if > 0 , the data is 2D-organized
Definition: DataSegmentBase.h:126
T VectorType
vector typedef
Definition: DataSegment.h:319
core::depth dataDepth
underlying data depth
Definition: DataSegmentBase.h:129
size_t numElements
number of vector elements contained
Definition: DataSegmentBase.h:123
bool equals(DataSegment< OtherT, 1 > dst, float tollerance=1.0e-5) const
Definition: DataSegment.h:357
static const DataSegment< T, N > fromVector(const std::vector< T > &v)
wrapps a data-segment around an std::vector (const version)
Definition: DataSegment.h:240
Abstract data segment class.
Definition: DataSegmentBase.h:99
core::depth getDepth() const
returns the actual core::depth (data type) of the entries
Definition: DataSegmentBase.h:158
math::FixedColVector< T, N > & operator[](int idx)
linear index operator
Definition: DataSegment.h:156
const DataSegment< T, N > & as() const
shallow and save cast from data segment base to special data segment version
Ipp32s icl32s
32bit signed integer type for the ICL
Definition: BasicTypes.h:58
math::FixedColVector< T, N > & operator()(int x, int y)
2D-index operator (only for organized data segments)
Definition: DataSegment.h:166
void fill(Fill vecValue)
fills each vector entry with given value
Definition: DataSegment.h:223
void fill(Fill vecValue)
fills each vector entry with given value
Definition: DataSegment.h:380
bool isPacked() const
returns whether the data is packed in memory (stride is sizeof(T)*N)
Definition: DataSegment.h:180
size_t stride
stride between elements
Definition: DataSegmentBase.h:120
void deepCopy(DataSegment< OtherT, 1 > dst) const
copies the data segment to into another element-wise
Definition: DataSegment.h:347
T & operator()(int x, int y)
2D-index operator (only for organized data segments, specialized to return a T& directly)
Definition: DataSegment.h:336
void deepCopy(DataSegment< OtherT, N > dst) const
copies the data segment to into another element-wise
utils::Size getSize() const
returns the ordred size of the segment of utils::Size::null if it's not organized
Definition: DataSegmentBase.h:141
template specialization for data-segments, where each entry is just 1D
Definition: DataSegment.h:317
DataSegment(T *data=0, size_t stride=0, size_t numElements=0, icl32s organizedWidth=-1)
Constructor (basically passes all parameters to the Base class)
Definition: DataSegment.h:322
Definition: FixedVector.h:40
Base class for Exception handling in the ICL.
Definition: Exception.h:42
size_t elemDim
vector element dim
Definition: DataSegmentBase.h:132
void fillScalar(Fill scalarValue)
fills each scalar value of each entry with given value
Definition: DataSegment.h:371
const T & operator[](int idx) const
linear index operator (specialized to return a T& directly, const)
Definition: DataSegment.h:331
bool isOrganized() const
returns, whether the segment is 2D-organized
Definition: DataSegmentBase.h:136
math::FixedColVector< T, N > VectorType
vector typedef
Definition: DataSegment.h:149
const T & operator()(int x, int y) const
2D-index operator (only for organized data segments, specialized to return a T& directly,...
Definition: DataSegment.h:341
void copy(const T *src, const T *srcEnd, T *dst)
moves data from source to destination array (no casting possible)
Definition: CoreFunctions.h:216
icl8u * data
data pointer orign
Definition: DataSegmentBase.h:111
The DataSegment class defines a strided data segment (or 1D or 2D ordred array of vectors)
Definition: DataSegment.h:147
const math::FixedColVector< T, N > & operator()(int x, int y) const
2D-index operator (only for organized data segments, const)
Definition: DataSegment.h:171
#define ICLASSERT_THROW(X, OBJ)
Definition: Macros.h:155
int getDim() const
returns the number of elements
Definition: DataSegmentBase.h:153