Image Component Library (ICL)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
DataSegmentBase.h
Go to the documentation of this file.
00001 /********************************************************************
00002 **                Image Component Library (ICL)                    **
00003 **                                                                 **
00004 ** Copyright (C) 2006-2013 CITEC, University of Bielefeld          **
00005 **                         Neuroinformatics Group                  **
00006 ** Website: www.iclcv.org and                                      **
00007 **          http://opensource.cit-ec.de/projects/icl               **
00008 **                                                                 **
00009 ** File   : ICLGeom/src/ICLGeom/DataSegmentBase.h                  **
00010 ** Module : ICLGeom                                                **
00011 ** Authors: Christof Elbrechter, Patrick Nobou                     **
00012 **                                                                 **
00013 **                                                                 **
00014 ** GNU LESSER GENERAL PUBLIC LICENSE                               **
00015 ** This file may be used under the terms of the GNU Lesser General **
00016 ** Public License version 3.0 as published by the                  **
00017 **                                                                 **
00018 ** Free Software Foundation and appearing in the file LICENSE.GPL  **
00019 ** included in the packaging of this file.  Please review the      **
00020 ** following information to ensure the license requirements will   **
00021 ** be met: http://www.gnu.org/licenses/lgpl-3.0.txt                **
00022 **                                                                 **
00023 ** The development of this software was supported by the           **
00024 ** Excellence Cluster EXC 277 Cognitive Interaction Technology.    **
00025 ** The Excellence Cluster EXC 277 is a grant of the Deutsche       **
00026 ** Forschungsgemeinschaft (DFG) in the context of the German       **
00027 ** Excellence Initiative.                                          **
00028 **                                                                 **
00029 ********************************************************************/
00030 
00031 #pragma once
00032 
00033 #include <ICLUtils/BasicTypes.h>
00034 #include <ICLUtils/Exception.h>
00035 #include <algorithm>
00036 
00037 namespace icl{
00038   namespace geom{
00039     
00041     template<class T,int N> struct DataSegment;
00044 
00045 
00094     struct DataSegmentBase{
00096       friend class PointCloudObjectBase;
00097       
00098     protected:
00100       static inline int getSizeOf(core::depth d){
00101         static const int lens[] = { 1, 2, 4, 4, 8 };
00102         return lens[(int)d];
00103       }
00104   
00106       icl8u *data;
00107       
00109 
00115       size_t stride;   
00116       
00118       size_t numElements;
00119       
00121       icl32s organizedWidth; // -1 if unorganized 
00122   
00124       core::depth dataDepth;
00125       
00127       size_t elemDim;     // number of elements (in Type units)
00128   
00129     public:
00131       inline bool isOrganized() const {
00132         return organizedWidth > 0;
00133       }
00134       
00136       inline utils::Size getSize() const {
00137         if(!isOrganized()) return utils::Size::null;
00138         else return utils::Size(organizedWidth, numElements/organizedWidth);
00139       }
00140       
00142       inline int getStride() const { 
00143         return stride;
00144       }
00145       
00147 
00148       inline int getDim() const{
00149         return numElements;
00150       }    
00151       
00153       inline core::depth getDepth() const{
00154         return dataDepth;
00155       }
00156       
00158       inline int getElemDim() const{
00159         return elemDim;
00160       }
00161       
00163 
00164       icl8u *getDataPointer() { return data; }
00165 
00167 
00168       const icl8u *getDataPointer() const { return data; }
00169   
00171       inline DataSegmentBase(void *data=0, size_t stride=0, 
00172                              size_t numElements=0, icl32s organizedWidth=-1, 
00173                              core::depth dataDepth=core::depth8u, size_t elemDim=0):
00174         data((icl8u*)data),stride(stride),numElements(numElements),
00175         organizedWidth(organizedWidth),dataDepth(dataDepth),elemDim(elemDim){}
00176       
00178 
00185       template<class T, int N>
00186       const DataSegment<T,N> &as() const;
00187       
00189       class Bytes{
00190         icl8u *data; 
00191         int len;     
00192 
00193         Bytes(icl8u *data=0, int len=0):data(data),len(len){}
00195         Bytes(const Bytes &other){} 
00196         friend class DataSegmentBase; 
00197         public:
00198         
00200         inline int getDim() const{ return len; }
00201         
00203         icl8u &operator[](int idx) { return data[idx]; }
00204   
00206         const icl8u operator[](int idx) const { return data[idx]; }
00207         
00209 
00210         inline Bytes &operator=(const Bytes &other) throw (utils::ICLException){
00211           if(len != other.len){
00212             throw utils::ICLException("unable to assign DataSegmentBase::Bytes: lengths differ!");
00213           }
00214           std::copy(other.data,other.data+len,data);
00215           return *this;
00216         }
00217         
00219         icl8u *begin() { return data; }
00220   
00222         icl8u *end() { return data+len; }
00223   
00225         const icl8u *begin() const { return data; }
00226   
00228         const icl8u *end() const { return data+len; }
00229   
00230       };
00231       
00233       inline Bytes operator[](int idx) {
00234         return Bytes(data+idx*stride*getSizeOf(dataDepth), elemDim*getSizeOf(dataDepth));
00235       }
00236   
00238       inline const Bytes operator[](int idx) const{
00239         return const_cast<DataSegmentBase*>(this)->operator[](idx); 
00240       }
00241   
00243       inline Bytes operator()(int x, int y) {
00244        return operator[](x + organizedWidth * y );
00245       }
00246       
00248       inline const Bytes operator()(int x, int y) const{
00249         return operator[](x + organizedWidth * y );
00250       }
00251     };
00252   
00253   } // namespace geom
00254 }
00255 
00256 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines