1 /* 2 ----------------------------------------------------------------------------- 3 This source file is part of OGRE 4 (Object-oriented Graphics Rendering Engine) 5 For the latest info, see http://www.ogre3d.org 6 7 Copyright (c) 2000-2013 Torus Knot Software Ltd 8 Permission is hereby granted, free of charge, to any person obtaining a copy 9 of this software and associated documentation files (the "Software"), to deal 10 in the Software without restriction, including without limitation the rights 11 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 copies of the Software, and to permit persons to whom the Software is 13 furnished to do so, subject to the following conditions: 14 15 The above copyright notice and this permission notice shall be included in 16 all copies or substantial portions of the Software. 17 18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 THE SOFTWARE. 25 ----------------------------------------------------------------------------- 26 */ 27 #include "OgreVolumeHalfFloatGridSource.h" 28 #include "OgreRoot.h" 29 #include "OgreDeflate.h" 30 #include "OgreStreamSerialiser.h" 31 #include "OgreBitwise.h" 32 #include "OgreMemoryAllocatorConfig.h" 33 #include "OgreLogManager.h" 34 #include "OgreTimer.h" 35 36 namespace Ogre { 37 namespace Volume { 38 getVolumeGridValue(size_t x,size_t y,size_t z) const39 float HalfFloatGridSource::getVolumeGridValue(size_t x, size_t y, size_t z) const 40 { 41 x = x >= mWidth ? mWidth - 1 : x; 42 y = y >= mHeight ? mHeight - 1 : y; 43 z = z >= mDepth ? mDepth - 1 : z; 44 return Bitwise::halfToFloat(mData[(mDepth - z - 1) * mDepthTimesHeight + x * mHeight + y]); 45 } 46 47 //----------------------------------------------------------------------- 48 setVolumeGridValue(int x,int y,int z,float value)49 void HalfFloatGridSource::setVolumeGridValue(int x, int y, int z, float value) 50 { 51 52 // Clamp if wanted. 53 if (mMaxClampedAbsoluteDensity != (Real)0.0 && Math::Abs(value) > mMaxClampedAbsoluteDensity) 54 { 55 value = mMaxClampedAbsoluteDensity; 56 } 57 58 mData[(mDepth - z - 1) * mDepthTimesHeight + x * mHeight + y] = Bitwise::floatToHalf(value); 59 } 60 61 //----------------------------------------------------------------------- 62 HalfFloatGridSource(const String & serializedVolumeFile,const bool trilinearValue,const bool trilinearGradient,const bool sobelGradient)63 HalfFloatGridSource::HalfFloatGridSource(const String &serializedVolumeFile, const bool trilinearValue, const bool trilinearGradient, const bool sobelGradient) : 64 GridSource(trilinearValue, trilinearGradient, sobelGradient) 65 { 66 67 Timer t; 68 DataStreamPtr streamRead = Root::getSingleton().openFileStream(serializedVolumeFile); 69 #if OGRE_NO_ZIP_ARCHIVE == 0 70 DataStreamPtr uncompressStream(OGRE_NEW DeflateStream(serializedVolumeFile, streamRead)); 71 StreamSerialiser ser(uncompressStream); 72 #else 73 StreamSerialiser ser(streamRead); 74 #endif 75 if (!ser.readChunkBegin(VOLUME_CHUNK_ID, VOLUME_CHUNK_VERSION)) 76 { 77 OGRE_EXCEPT(Exception::ERR_INVALID_STATE, 78 "Invalid volume file given!", 79 __FUNCTION__); 80 } 81 82 // Read header 83 Vector3 readFrom, readTo; 84 ser.read(&readFrom); 85 ser.read(&readTo); 86 float voxelWidth; 87 ser.read<float>(&voxelWidth); 88 size_t width, height, depth; 89 ser.read<size_t>(&width); 90 ser.read<size_t>(&height); 91 ser.read<size_t>(&depth); 92 mWidth = static_cast<int>(width); 93 mHeight = static_cast<int>(height); 94 mDepth = static_cast<int>(depth); 95 mDepthTimesHeight = static_cast<int>(mDepth * mHeight); 96 97 Vector3 worldDimension = readTo - readFrom; 98 mPosXScale = (Real)1.0 / (Real)worldDimension.x * (Real)mWidth; 99 mPosYScale = (Real)1.0 / (Real)worldDimension.y * (Real)mHeight; 100 mPosZScale = (Real)1.0 / (Real)worldDimension.z * (Real)mDepth; 101 102 mVolumeSpaceToWorldSpaceFactor = (Real)worldDimension.x * (Real)mWidth; 103 mMaxClampedAbsoluteDensity = 0; 104 105 // Read data 106 size_t elementCount = mWidth * mHeight * mDepth; 107 mData = OGRE_ALLOC_T(uint16, elementCount, MEMCATEGORY_GENERAL); 108 ser.read(mData, elementCount); 109 110 ser.readChunkEnd(VOLUME_CHUNK_ID); 111 112 LogManager::getSingleton().stream() << "Processed serialization in " << t.getMilliseconds() << "ms."; 113 } 114 115 //----------------------------------------------------------------------- 116 setMaxClampedAbsoluteDensity(Real maxClampedAbsoluteDensity)117 void HalfFloatGridSource::setMaxClampedAbsoluteDensity(Real maxClampedAbsoluteDensity) 118 { 119 mMaxClampedAbsoluteDensity = maxClampedAbsoluteDensity; 120 } 121 122 //----------------------------------------------------------------------- 123 124 getMaxClampedAbsoluteDensity(void) const125 Real HalfFloatGridSource::getMaxClampedAbsoluteDensity(void) const 126 { 127 return mMaxClampedAbsoluteDensity; 128 } 129 130 //----------------------------------------------------------------------- 131 ~HalfFloatGridSource(void)132 HalfFloatGridSource::~HalfFloatGridSource(void) 133 { 134 OGRE_FREE(mData, MEMCATEGORY_GENERAL); 135 } 136 } 137 }