00001 /*======================================================================= 00002 *** THE CONTENT OF THIS WORK IS PROPRIETARY TO FEI S.A.S, (FEI S.A.S.), *** 00003 *** AND IS DISTRIBUTED UNDER A LICENSE AGREEMENT. *** 00004 *** *** 00005 *** REPRODUCTION, DISCLOSURE, OR USE, IN WHOLE OR IN PART, OTHER THAN AS *** 00006 *** SPECIFIED IN THE LICENSE ARE NOT TO BE UNDERTAKEN EXCEPT WITH PRIOR *** 00007 *** WRITTEN AUTHORIZATION OF FEI S.A.S. *** 00008 *** *** 00009 *** RESTRICTED RIGHTS LEGEND *** 00010 *** USE, DUPLICATION, OR DISCLOSURE BY THE GOVERNMENT OF THE CONTENT OF THIS *** 00011 *** WORK OR RELATED DOCUMENTATION IS SUBJECT TO RESTRICTIONS AS SET FORTH IN *** 00012 *** SUBPARAGRAPH (C)(1) OF THE COMMERCIAL COMPUTER SOFTWARE RESTRICTED RIGHT *** 00013 *** CLAUSE AT FAR 52.227-19 OR SUBPARAGRAPH (C)(1)(II) OF THE RIGHTS IN *** 00014 *** TECHNICAL DATA AND COMPUTER SOFTWARE CLAUSE AT DFARS 52.227-7013. *** 00015 *** *** 00016 *** COPYRIGHT (C) 1996-2019 BY FEI S.A.S, *** 00017 *** BORDEAUX, FRANCE *** 00018 *** ALL RIGHTS RESERVED *** 00019 **=======================================================================*/ 00020 /*======================================================================= 00021 ** Author : Julien Sallanne (Feb 2014) 00022 **=======================================================================*/ 00023 00024 #ifndef _SB_IMAGEDATAVOXEL_H_ 00025 #define _SB_IMAGEDATAVOXEL_H_ 00026 00027 #include <ImageViz/SoImageViz.h> 00028 #include <ImageViz/SbImageDataType.h> 00029 #include <Inventor/devices/SoCpuBufferObject.h> 00030 00031 #include <Inventor/errors/SbException.h> 00032 00046 class SbImageDataVoxel 00047 { 00048 public: 00053 SbImageDataVoxel(const SbImageDataType& channelType, SoBufferObject* buffer, size_t offset); 00054 00056 ~SbImageDataVoxel(); 00057 00059 SbImageDataVoxel(const SbImageDataVoxel&); 00060 00062 const SbImageDataVoxel& operator=(const SbImageDataVoxel& right); 00063 00065 inline const SbImageDataType& getImageDataType() const { return m_imageDataType; } 00066 00072 template <typename TypeOut> TypeOut castValue(size_t channelIndex) const; 00073 00079 template <typename TypeOut> TypeOut& getValue(size_t channelIndex); 00080 00081 template <typename TypeOut> const TypeOut& getValue(size_t channelIndex) const; 00082 00083 private: 00084 00086 void* getDataPtr() const; 00087 00088 private: 00089 00090 SbImageDataType m_imageDataType; 00091 // Used to keep constructor buffer. It is mapped in SbImageDataVoxel constructor and 00092 // unmapped in destructor. 00093 SoRef<SoBufferObject> m_buffer; 00094 // Offset use to map m_buffer to m_cpuBuffer. 00095 size_t m_offset; 00096 // CPU buffer containing data. It is mapped in SbImageDataVoxel constructor and 00097 // unmapped in destructor. Basically, m_cpuBuffer = m_buffer->map() + offset; 00098 SoRef<SoCpuBufferObject> m_cpuBuffer; 00099 // Pointer to channels. Basically, m_ptr = m_cpuBuffer->map(); 00100 void* m_ptr; 00101 }; 00102 00103 00104 //----------------------------------------------------------------------------- 00105 template <typename TypeOut> 00106 TypeOut 00107 SbImageDataVoxel::castValue(size_t i) const 00108 { 00109 if ( i >= getImageDataType().getNumChannel() ) 00110 throw SbException("out of range"); 00111 00112 TypeOut dataValue; 00113 00114 switch ( m_imageDataType.getDataType() ) 00115 { 00116 case SbDataType::UNSIGNED_BYTE : dataValue = TypeOut(((unsigned char*) m_ptr)[i]); break; 00117 case SbDataType::UNSIGNED_SHORT: dataValue = TypeOut(((unsigned short*) m_ptr)[i]); break; 00118 case SbDataType::UNSIGNED_INT32: dataValue = TypeOut(((unsigned int*) m_ptr)[i]); break; 00119 case SbDataType::SIGNED_BYTE : dataValue = TypeOut(((signed char*) m_ptr)[i]); break; 00120 case SbDataType::SIGNED_SHORT : dataValue = TypeOut(((signed short*) m_ptr)[i]); break; 00121 case SbDataType::SIGNED_INT32 : dataValue = TypeOut(((signed int*) m_ptr)[i]); break; 00122 case SbDataType::FLOAT : dataValue = TypeOut(((float*) m_ptr)[i]); break; 00123 case SbDataType::DOUBLE : dataValue = TypeOut(((double*) m_ptr)[i]); break; 00124 default : SoDebugError::postWarning("SbImageDataVoxel::castValue()", "Unknown data type %s", m_imageDataType.getDataType().getString().toLatin1()); dataValue = 0; break; 00125 } 00126 return dataValue; 00127 } 00128 00129 //----------------------------------------------------------------------------- 00130 template <typename TypeOut> 00131 const TypeOut& 00132 SbImageDataVoxel::getValue(size_t i) const 00133 { 00134 if ( i >= getImageDataType().getNumChannel() ) 00135 throw SbException("out of range"); 00136 00137 TypeOut returnValue; 00138 const SbDataType dataTypeOut = SbDataType::getTemplateType(returnValue); 00139 00140 if ( dataTypeOut != getImageDataType().getDataType() ) 00141 throw SbException("inconsistent dataType"); 00142 00143 return ((TypeOut*) m_ptr)[i]; 00144 } 00145 00146 //----------------------------------------------------------------------------- 00147 template <typename TypeOut> 00148 TypeOut& 00149 SbImageDataVoxel::getValue(size_t i) 00150 { 00151 if ( i >= getImageDataType().getNumChannel() ) 00152 throw SbException("out of range"); 00153 00154 TypeOut returnValue; 00155 const SbDataType dataTypeOut = SbDataType::getTemplateType(returnValue); 00156 00157 if ( dataTypeOut != getImageDataType().getDataType() ) 00158 throw SbException("inconsistent dataType"); 00159 00160 return ((TypeOut*) m_ptr)[i]; 00161 } 00162 00163 #endif // _SB_IMAGEDATAVOXEL_H_ 00164 00165