├── CMakeLists.txt ├── README.md ├── vdbvolume.cpp └── vdbvolume.h /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | CMAKE_MINIMUM_REQUIRED( VERSION 2.8.3 FATAL_ERROR ) 2 | 3 | PROJECT( vdbvolume ) 4 | 5 | # OpenVDB 6 | FIND_PATH( OPENVDB_DIR REQUIRED ) 7 | SET( OPENVDB_INCLUDE_DIR "${OPENVDB_DIR}/include" ) 8 | SET( OPENVDB_LIBRARY_DIR "${OPENVDB_DIR}/lib" ) 9 | 10 | # TBB 11 | FIND_PATH( TBB_DIR REQUIRED ) 12 | SET( TBB_INCLUDE_DIR "${TBB_DIR}/include" ) 13 | SET( TBB_LIBRARY_DIR "${TBB_DIR}/lib" ) 14 | 15 | # Mitsuba & Dependencies 16 | FIND_PATH( MITSUBA_DIR REQUIRED ) 17 | SET( MITSUBA_SDK_INCLUDE_DIR "${MITSUBA_DIR}/dist/sdk/include" ) 18 | SET( MITSUBA_SDK_LIBRARY_DIR "${MITSUBA_DIR}/dist/sdk/lib" ) 19 | SET( MITSUBA_DEPENDENCIES_INCLUDE_DIR "${MITSUBA_DIR}/dependencies/include" ) 20 | SET( MITSUBA_DEPENDENCIES_LIBRARY_DIR "${MITSUBA_DIR}/dependencies/lib/x64_vc10" ) 21 | 22 | INCLUDE_DIRECTORIES( ${MITSUBA_SDK_INCLUDE_DIR} ${MITSUBA_DEPENDENCIES_INCLUDE_DIR} ${OPENVDB_INCLUDE_DIR} ${TBB_INCLUDE_DIR} ) 23 | LINK_DIRECTORIES( ${MITSUBA_SDK_LIBRARY_DIR} ${MITSUBA_DEPENDENCIES_LIBRARY_DIR} ${OPENVDB_LIBRARY_DIR} ${TBB_LIBRARY_DIR} ) 24 | 25 | ADD_DEFINITIONS( -DSINGLE_PRECISION -DSPECTRUM_SAMPLES=3 -DMTS_BUILD_MODULE=2 ) 26 | ADD_LIBRARY( vdbvolume SHARED vdbvolume.cpp vdbvolume.h ) 27 | TARGET_LINK_LIBRARIES( vdbvolume openvdb tbb mitsuba-core mitsuba-render ) 28 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | mitsuba-vdb 2 | =========== 3 | 4 | OpenVDB Support for Mitsuba 5 | 6 | To compile this plugin for Mitsuba, you have to prepare 7 | * boost 1.53 8 | * tbb 9 | * IlmBase 10 | * OpenVDB 11 | -------------------------------------------------------------------------------- /vdbvolume.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * OpenVDB Data Source for Mitsuba Render 3 | * Copyright (C) 2014 Bo Zhou 4 | 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | */ 18 | 19 | #include "vdbvolume.h" 20 | 21 | #include 22 | #include 23 | #include 24 | #include 25 | 26 | struct VdbGridSet 27 | { 28 | std::map m_grids; 29 | }; 30 | 31 | struct VdbGridPool 32 | { 33 | std::map m_gridSets; 34 | }; 35 | 36 | static VdbGridPool s_vdbGridPool ; 37 | 38 | MTS_NAMESPACE_BEGIN 39 | 40 | VdbDataSource::VdbDataSource(const Properties &props) 41 | : 42 | VolumeDataSource(props), 43 | m_customStepSize(0.0f) { 44 | openvdb::initialize(); 45 | 46 | // 47 | m_filename = props.getString("filename"); 48 | m_fieldname = props.getString("fieldname"); 49 | if( props.hasProperty("customStepSize") ) { 50 | m_customStepSize = props.getFloat("customStepSize"); 51 | } 52 | 53 | std::map::iterator gridSetItr = s_vdbGridPool.m_gridSets.find(m_filename); 54 | if (gridSetItr == s_vdbGridPool.m_gridSets.end()) { 55 | if (open()) { 56 | gridSetItr = s_vdbGridPool.m_gridSets.find(m_filename); 57 | 58 | std::map::iterator gridItr = gridSetItr->second.m_grids.find(m_fieldname); 59 | if (gridItr == gridSetItr->second.m_grids.end()) { 60 | Log(EError, "Can't get the specific field [%s] from [%s] to read.", m_fieldname.c_str(), m_filename.c_str()); 61 | } 62 | } 63 | else { 64 | Log(EError, "Can't open the file [%s].", m_filename.c_str()); 65 | } 66 | } 67 | else if (gridSetItr->second.m_grids.find(m_fieldname) == gridSetItr->second.m_grids.end()) { 68 | Log(EError, "Opened the file [%s] but can't get the field [%s].", m_filename.c_str(), m_fieldname.c_str()); 69 | } 70 | } 71 | 72 | VdbDataSource::VdbDataSource(Stream *stream, InstanceManager *manager) 73 | : 74 | VolumeDataSource(stream, manager) { 75 | m_filename = stream->readString(); 76 | m_fieldname = stream->readString(); 77 | m_customStepSize = stream->readFloat(); 78 | } 79 | 80 | VdbDataSource::~VdbDataSource() { 81 | } 82 | 83 | bool VdbDataSource::supportsFloatLookups() const { 84 | return true; 85 | } 86 | 87 | Float VdbDataSource::lookupFloat(const Point &p) const { 88 | Float data = 0.0f; 89 | 90 | std::map::const_iterator gridSetItr = s_vdbGridPool.m_gridSets.find(m_filename); 91 | if (gridSetItr != s_vdbGridPool.m_gridSets.end()) { 92 | std::map::const_iterator gridItr = gridSetItr->second.m_grids.find(m_fieldname); 93 | if (gridItr != gridSetItr->second.m_grids.end()) { 94 | const openvdb::FloatGrid::Ptr &floatGrid = openvdb::gridPtrCast(gridItr->second); 95 | openvdb::tools::GridSampler interpolator(floatGrid->constTree() , floatGrid->transform()); 96 | data = interpolator.wsSample(openvdb::math::Vec3d(p.x, p.y, p.z)); 97 | } 98 | } 99 | 100 | return data; 101 | } 102 | 103 | bool VdbDataSource::supportsVectorLookups() const { 104 | return false; 105 | } 106 | 107 | void VdbDataSource::serialize(Stream *stream, InstanceManager *manager) const { 108 | VolumeDataSource::serialize(stream, manager); 109 | 110 | stream->writeString(m_filename); 111 | stream->writeString(m_fieldname); 112 | stream->writeFloat(m_customStepSize); 113 | } 114 | 115 | Float VdbDataSource::getStepSize() const { 116 | Float stepSize = 1.0f; 117 | if ( m_customStepSize > 0.0f ) { 118 | stepSize = m_customStepSize; 119 | } 120 | std::map::const_iterator gridSetItr = s_vdbGridPool.m_gridSets.find(m_filename); 121 | if (gridSetItr != s_vdbGridPool.m_gridSets.end()) { 122 | std::map::const_iterator gridItr = gridSetItr->second.m_grids.find(m_fieldname); 123 | if (gridItr != gridSetItr->second.m_grids.end()) { 124 | const openvdb::math::Vec3d &voxelSize = gridItr->second->constTransform().voxelSize(); 125 | stepSize = std::min(std::min(voxelSize.x(), voxelSize.y()) , voxelSize.z()) * 0.5; 126 | } 127 | } 128 | return stepSize; 129 | } 130 | 131 | Float VdbDataSource::getMaximumFloatValue() const { 132 | Float valMax = - 1.0f; 133 | 134 | std::map::const_iterator gridSetItr = s_vdbGridPool.m_gridSets.find(m_filename); 135 | if (gridSetItr != s_vdbGridPool.m_gridSets.end()) { 136 | std::map::const_iterator gridItr = gridSetItr->second.m_grids.find(m_fieldname); 137 | if (gridItr != gridSetItr->second.m_grids.end()) { 138 | const openvdb::FloatGrid::Ptr &floatGrid = openvdb::gridPtrCast(gridItr->second); 139 | Float valMin = 0.0f; 140 | floatGrid->evalMinMax(valMin, valMax); 141 | } 142 | } 143 | 144 | return valMax; 145 | } 146 | 147 | bool VdbDataSource::open() { 148 | bool success = false ; 149 | 150 | try { 151 | boost::scoped_ptr< openvdb::io::File > file(new openvdb::io::File(m_filename)); 152 | if (file->open()) { 153 | s_vdbGridPool.m_gridSets.insert(std::make_pair(m_filename, VdbGridSet())); 154 | std::map::iterator gridSetItr = s_vdbGridPool.m_gridSets.find(m_filename); 155 | 156 | // 157 | openvdb::math::Vec3d bbMin = openvdb::math::Vec3d( std::numeric_limits< double >::max()); 158 | openvdb::math::Vec3d bbMax = openvdb::math::Vec3d(- std::numeric_limits< double >::max()); 159 | openvdb::GridPtrVecPtr grids = file->readAllGridMetadata() ; 160 | for (openvdb::GridPtrVec::const_iterator itr = grids->begin(); itr != grids->end(); ++ itr) { 161 | const openvdb::Vec3i & bbMinI = (*itr)->metaValue(openvdb::GridBase::META_FILE_BBOX_MIN); 162 | const openvdb::Vec3i & bbMaxI = (*itr)->metaValue(openvdb::GridBase::META_FILE_BBOX_MAX); 163 | bbMin = openvdb::math::minComponent(bbMin , (*itr)->indexToWorld(bbMinI)); 164 | bbMax = openvdb::math::maxComponent(bbMax , (*itr)->indexToWorld(bbMaxI)); 165 | const std::string &fieldname = (*itr)->getName(); 166 | gridSetItr->second.m_grids.insert(std::make_pair(fieldname, file->readGrid((*itr)->getName()))); 167 | } 168 | 169 | m_aabb.min = Point(bbMin.x(), bbMin.y(), bbMin.z()); 170 | m_aabb.max = Point(bbMax.x(), bbMax.y(), bbMax.z()); 171 | 172 | success = true; 173 | } 174 | } 175 | catch(const std::exception &e) { 176 | std::cerr << e.what() << std::endl; 177 | } 178 | catch(...) { 179 | } 180 | 181 | return success ; 182 | } 183 | 184 | MTS_NAMESPACE_END 185 | -------------------------------------------------------------------------------- /vdbvolume.h: -------------------------------------------------------------------------------- 1 | /* 2 | * OpenVDB Data Source for Mitsuba Render 3 | * Copyright (C) 2014 Bo Zhou 4 | 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | */ 18 | 19 | #pragma once 20 | 21 | #include 22 | #include 23 | 24 | MTS_NAMESPACE_BEGIN 25 | 26 | class MTS_EXPORT_RENDER VdbDataSource : public VolumeDataSource 27 | { 28 | public : 29 | 30 | VdbDataSource(const Properties &props); 31 | 32 | VdbDataSource(Stream *stream, InstanceManager *manager); 33 | 34 | virtual ~VdbDataSource(); 35 | 36 | virtual bool supportsFloatLookups() const; 37 | 38 | virtual Float lookupFloat(const Point &p) const; 39 | 40 | virtual bool supportsVectorLookups() const; 41 | 42 | virtual void serialize(Stream *stream, InstanceManager *manager) const; 43 | 44 | virtual Float getStepSize() const; 45 | 46 | virtual Float getMaximumFloatValue() const; 47 | 48 | MTS_DECLARE_CLASS() 49 | 50 | private : 51 | 52 | bool open(); 53 | 54 | std::string m_filename; 55 | std::string m_fieldname; 56 | Float m_customStepSize; 57 | } ; 58 | 59 | MTS_IMPLEMENT_CLASS_S(VdbDataSource, false, VolumeDataSource); 60 | MTS_EXPORT_PLUGIN(VdbDataSource, "OpenVDB data source"); 61 | MTS_NAMESPACE_END 62 | --------------------------------------------------------------------------------