VDB Data value visualize: 结论从houdini得知.

API常用文字:

interior:内部

Narrow-band:窄带

background:窄带外

SDF: XY plane Data visualize:

{

  (1)用法:vdb sdf levelset球,采样其体素值到对应的点位置的颜色观察。houdini节点vdb from polygons(参数上Exterior band voxels:3,Interior band voxels:3)没有开启Fill interior

  则形成体素值:

    interior是-0.3 (这个值是由Interior band voxels:3 得到)

    Narrow-band: 从interior到narrow-band方向:-0.3 到 0

      background:从narrow-band到background方向:0.3 (Exterior band voxels:3)

  (2)用法:

  假如要让interior成为一个梯度值,而不是恒定值,houdini做了一个牛逼的按钮,Fill interior.

  interior里的体素值立马成为梯度的,大概是从-3.73过渡到narrow-band

}

FOG Volume: XY plane Data visualize:

{

  (1) 普通不勾选fill interior:

  interior是1

  Narrow-band: 从interior到narrow-band方向:1 到 0

    background:从narrow-band到background方向:0

  (2) 勾选fill interior:

  interior到narrow-band方向从1->0.3有个过渡

  Narrow-band: 从interior到narrow-band方向:0.3 到 0

    background:从narrow-band到background方向:0

}

==================================================================START======================================================

<1> ,make vdb sphere,and convert to volume

  1. #include <openvdb/openvdb.h>
  2. #include <openvdb/tools/LevelSetSphere.h>
  3. using namespace std;
  4.  
  5. // Populate the given grid with a narrow-band level set representation of a sphere.
  6. // The width of the narrow band is determined by the grid's background value.
  7. // (Example code only; use tools::createSphereSDF() in production.)
  8. template<class GridType>
  9. void
  10. static makeSphere(GridType& grid, float radius, const openvdb::Vec3f& c)
  11. {
  12. typedef typename GridType::ValueType ValueT;
  13. // Distance value for the constant region exterior to the narrow band
  14. const ValueT outside = grid.background();
  15. // Distance value for the constant region interior to the narrow band
  16. // (by convention, the signed distance is negative in the interior of
  17. // a level set)
  18. const ValueT inside = -outside;
  19. // Use the background value as the width in voxels of the narrow band.
  20. // (The narrow band is centered on the surface of the sphere, which
  21. // has distance 0.)
  22. int padding = int(openvdb::math::RoundUp(openvdb::math::Abs(outside)));
  23. // The bounding box of the narrow band is 2*dim voxels on a side.
  24. int dim = int(radius + padding);
  25. // Get a voxel accessor.
  26. typename GridType::Accessor accessor = grid.getAccessor();
  27. // Compute the signed distance from the surface of the sphere of each
  28. // voxel within the bounding box and insert the value into the grid
  29. // if it is smaller in magnitude than the background value.
  30. openvdb::Coord ijk;
  31. int &i = ijk[];
  32. int &j = ijk[];
  33. int &k = ijk[];
  34. for (i = c[] - dim; i < c[] + dim; ++i) {
  35. const float x2 = openvdb::math::Pow2(i - c[]);
  36. for (j = c[] - dim; j < c[] + dim; ++j) {
  37. const float x2y2 = openvdb::math::Pow2(j - c[]) + x2;
  38. for (k = c[] - dim; k < c[] + dim; ++k) {
  39. // The distance from the sphere surface in voxels
  40. const float dist = openvdb::math::Sqrt(x2y2
  41. + openvdb::math::Pow2(k - c[])) - radius;
  42. // Convert the floating-point distance to the grid's value type.
  43. ValueT val = ValueT(dist);
  44. // Only insert distances that are smaller in magnitude than
  45. // the background value.
  46. if (val < inside || outside < val) continue;
  47. // Set the distance for voxel (i,j,k).
  48. accessor.setValue(ijk, val);
  49. }
  50. }
  51. }
  52. // Propagate the outside/inside sign information from the narrow band
  53. // throughout the grid.
  54. openvdb::tools::signedFloodFill(grid.tree());
  55.  
  56. }
  57.  
  58. template <class T>
  59. static void convertToVolume(T &grid)
  60. {
  61.  
  62. // Convert the level set sphere to a narrow-band fog volume, in which
  63. // interior voxels have value 1, exterior voxels have value 0, and
  64. // narrow-band voxels have values varying linearly from 0 to 1.
  65. const float outside = grid->background();
  66. const float width = 2.0 * outside;
  67. // Visit and update all of the grid's active values, which correspond to
  68. // voxels on the narrow band.
  69. for (openvdb::FloatGrid::ValueOnIter iter = grid->beginValueOn(); iter; ++iter) {
  70. float dist = iter.getValue();
  71. iter.setValue((outside - dist) / width);
  72. }
  73. // Visit all of the grid's inactive tile and voxel values and update the values
  74. // that correspond to the interior region.
  75. for (openvdb::FloatGrid::ValueOffIter iter = grid->beginValueOff(); iter; ++iter) {
  76. if (iter.getValue() < 0.0) {
  77. iter.setValue(1.0);
  78. iter.setValueOff();
  79. } else{
  80. iter.setValue();
  81. iter.setValueOff();
  82. }
  83. }
  84.  
  85. }
  86.  
  87. int main()
  88. {
  89.  
  90. openvdb::initialize();
  91. // Create a shared pointer to a newly-allocated grid of a built-in type:
  92. // in this case, a FloatGrid, which stores one single-precision floating point
  93. // value per voxel. Other built-in grid types include BoolGrid, DoubleGrid,
  94. // Int32Grid and Vec3SGrid (see openvdb.h for the complete list).
  95. // The grid comprises a sparse tree representation of voxel data,
  96. // user-supplied metadata and a voxel space to world space transform,
  97. // which defaults to the identity transform.
  98. openvdb::FloatGrid::Ptr grid =
  99. openvdb::FloatGrid::create(/*background value=*/2.0);
  100. // Populate the grid with a sparse, narrow-band level set representation
  101. // of a sphere with radius 50 voxels, located at (1.5, 2, 3) in index space.
  102. makeSphere(*grid, /*radius=*/50.0, /*center=*/openvdb::Vec3f(1.5, , ));
  103. // Associate some metadata with the grid.
  104. grid->insertMeta("radius", openvdb::FloatMetadata(50.0));
  105. // Associate a scaling transform with the grid that sets the voxel size
  106. // to 0.5 units in world space.
  107. grid->setTransform(
  108. openvdb::math::Transform::createLinearTransform(/*voxel size=*/0.5));
  109. // Identify the grid as a level set.
  110. grid->setGridClass(openvdb::GRID_LEVEL_SET);
  111. //grid->setGridClass(openvdb::GRID_FOG_VOLUME);
  112. // Name the grid "LevelSetSphere".
  113. grid->setName("LevelSetSphere");
  114. // Create a VDB file object.
  115. openvdb::io::File file("mygrids.vdb");
  116. // Add the grid pointer to a container.
  117. openvdb::GridPtrVec grids;
  118.  
  119. // Write out the contents of the container.
  120.  
  121. grids.push_back(grid);
  122. file.write(grids);
  123. file.close();
  124.  
  125. // ============================================================= convert level set sphere to a fog volume sphere =====================================================
  126. std::cout << "read a new sdf volume and change it to fog\n";
  127. openvdb::io::File readFile("mygrids.vdb");
  128. readFile.open();
  129. openvdb::GridBase::Ptr readGrid;
  130. for(openvdb::io::File::NameIterator nameIter = readFile.beginName();nameIter!=readFile.endName();++nameIter)
  131. {
  132. if(nameIter.gridName() == "LevelSetSphere")
  133. {
  134. readGrid = readFile.readGrid(nameIter.gridName());
  135. } else
  136. {
  137. std::cout << "skip other grid modifile " << nameIter.gridName() <<std::endl;
  138. }
  139. }
  140. openvdb::FloatGrid::Ptr cast_grid = openvdb::gridPtrCast<openvdb::FloatGrid>(readGrid);
  141. cast_grid->setGridClass(openvdb::GRID_FOG_VOLUME);
  142. readFile.close();
  143. convertToVolume(cast_grid);
  144. openvdb::GridPtrVec WM_grids;
  145. WM_grids.push_back(cast_grid);
  146. openvdb::io::File WM_file("mygrids_convertToVolume.vdb");
  147. WM_file.write(WM_grids);
  148. WM_file.close();
  149.  
  150. return ;
  151. }

<2> vdb from particles

主要观察粒子的density,velocity

#include <openvdb/openvdb.h>

#include <openvdb/tools/ParticlesToLevelSet.h>

#include <iostream>
#include <vector>

  1. struct MyParticle
  2. {
  3. openvdb::Vec3R p, v;
  4. openvdb::Real r;
  5. };
  6.  
  7. class MyParticleList
  8. {
  9.  
  10. // change protected to the public,direct find the mRadiusScale,mVelocityScale
  11. public:
  12.  
  13. openvdb::Real mRadiusScale;
  14. openvdb::Real mVelocityScale;
  15. std::vector<MyParticle> mParticleList;
  16.  
  17. public:
  18. std::vector<MyParticle> &get_mPartcileList()
  19. {
  20. return mParticleList;
  21. }
  22.  
  23. public:
  24.  
  25. typedef openvdb::Vec3R PosType;
  26.  
  27. MyParticleList(openvdb::Real rScale=, openvdb::Real vScale=)
  28. : mRadiusScale(rScale), mVelocityScale(vScale) {}
  29. void add(const openvdb::Vec3R &p, const openvdb::Real &r,
  30. const openvdb::Vec3R &v=openvdb::Vec3R(,,))
  31. {
  32. MyParticle pa;
  33. pa.p = p;
  34. pa.r = r;
  35. pa.v = v;
  36. mParticleList.push_back(pa);
  37. }
  38. /// @return coordinate bbox in the space of the specified transfrom
  39. openvdb::CoordBBox getBBox(const openvdb::GridBase& grid) {
  40. openvdb::CoordBBox bbox;
  41. openvdb::Coord &min= bbox.min(), &max = bbox.max();
  42. openvdb::Vec3R pos;
  43. openvdb::Real rad, invDx = /grid.voxelSize()[];
  44. for (size_t n=, e=this->size(); n<e; ++n) {
  45. this->getPosRad(n, pos, rad);
  46. const openvdb::Vec3d xyz = grid.worldToIndex(pos);
  47. const openvdb::Real r = rad * invDx;
  48. for (int i=; i<; ++i) {
  49. min[i] = openvdb::math::Min(min[i], openvdb::math::Floor(xyz[i] - r));
  50. max[i] = openvdb::math::Max(max[i], openvdb::math::Ceil( xyz[i] + r));
  51. }
  52. }
  53. return bbox;
  54. }
  55. //typedef int AttributeType;
  56. // The methods below are only required for the unit-tests
  57. openvdb::Vec3R pos(int n) const {return mParticleList[n].p;}
  58. openvdb::Vec3R vel(int n) const {return mVelocityScale*mParticleList[n].v;}
  59. openvdb::Real radius(int n) const {return mRadiusScale*mParticleList[n].r;}
  60.  
  61. //////////////////////////////////////////////////////////////////////////////
  62. /// The methods below are the only ones required by tools::ParticleToLevelSet
  63. /// @note We return by value since the radius and velocities are modified
  64. /// by the scaling factors! Also these methods are all assumed to
  65. /// be thread-safe.
  66.  
  67. /// Return the total number of particles in list.
  68. /// Always required!
  69. size_t size() const { return mParticleList.size(); }
  70.  
  71. /// Get the world space position of n'th particle.
  72. /// Required by ParticledToLevelSet::rasterizeSphere(*this,radius).
  73. void getPos(size_t n, openvdb::Vec3R&pos) const { pos = mParticleList[n].p; }
  74.  
  75. void getPosRad(size_t n, openvdb::Vec3R& pos, openvdb::Real& rad) const {
  76. pos = mParticleList[n].p;
  77. rad = mRadiusScale*mParticleList[n].r;
  78. }
  79. void getPosRadVel(size_t n, openvdb::Vec3R& pos, openvdb::Real& rad, openvdb::Vec3R& vel) const {
  80. pos = mParticleList[n].p;
  81. rad = mRadiusScale*mParticleList[n].r;
  82. vel = mVelocityScale*mParticleList[n].v;
  83. }
  84. // The method below is only required for attribute transfer
  85. void getAtt(size_t n, openvdb::Index32& att) const { att = openvdb::Index32(n); }
  86. };

Particles IO

Buiding the density,and write it out.

  1. int main()
  2. {
  3.  
  4. const float voxelSize = 0.2f, halfWidth = 2.0f;
  5. openvdb::FloatGrid::Ptr density_grid = openvdb::createLevelSet<openvdb::FloatGrid>(voxelSize, halfWidth);
  6.  
  7. MyParticleList pa(,); // this multiply is radius scale , velocity scale
  8.  
  9. // This particle radius = 1 < 1.5 i.e. it's below the Nyquist frequency and hence ignored
  10. pa.add(openvdb::Vec3R( , , ), , openvdb::Vec3R( , , ));
  11. pa.add(openvdb::Vec3R( , , ), , openvdb::Vec3R( , , ));
  12. pa.add(openvdb::Vec3R( , , ), , openvdb::Vec3R( , , ));
  13. pa.add(openvdb::Vec3R( , , ), , openvdb::Vec3R( , , ));
  14. pa.add(openvdb::Vec3R( , , ), , openvdb::Vec3R( , , ));
  15.  
  16. openvdb::tools::ParticlesToLevelSet<openvdb::FloatGrid> raster(*density_grid);
  17. raster.rasterizeTrails(pa, 0.75);//scale offset between two instances
  18.  
  19. // always prune to produce a valid narrow-band level set.
  20. raster.finalize(true);
  21.  
  22. density_grid->setGridClass(openvdb::GRID_LEVEL_SET);
  23. density_grid->setName("density");
  24. convertToVolume(density_grid);
  25.  
  26. // Create a VDB file object.
  27. openvdb::io::File file("mygrids.vdb");
  28. // Add the grid pointer to a container.
  29. openvdb::GridPtrVec grids;
  30. grids.push_back(density_grid);
  31.  
  32. // Write out the contents of the container.
  33. file.write(grids);
  34. file.close();
  35.  
  36. }

particles to volume

一个更好的方法Buiding the density.and write it out

  1. // Note densityGrid allocation memory in this function and transform same as densityGrid
  2. void buildingDensityGrid(openvdb::FloatGrid::Ptr &densityGrid,
  3. openvdb::math::Transform::Ptr &transform,MyParticleList &pa,bool isRasterToSphere = true)
  4. {
  5. float backGround = 0.1f;
  6. float voxelSize = 0.1;
  7. transform = openvdb::math::Transform::createLinearTransform(voxelSize);
  8. densityGrid = openvdb::FloatGrid::create(backGround);
  9. std::cout << "set density grid class type\n";
  10. densityGrid->setGridClass(openvdb::GRID_LEVEL_SET);
  11. densityGrid->setTransform(transform);
  12. openvdb::tools::ParticlesToLevelSet<openvdb::FloatGrid> raster(*densityGrid);
  13. if(isRasterToSphere)
  14. {
  15. raster.rasterizeSpheres(pa,pa.mRadiusScale);
  16. } else{
  17. raster.rasterizeTrails(pa,0.75);
  18. }
  19. raster.finalize(true);
  20. std::cout << "raster to end\n";
  21. }
  22.  
  23. int main()
  24. {
  25. MyParticleList pa(,);
  26. pa.add(openvdb::Vec3R( , , ), , openvdb::Vec3R( , , ));
  27. pa.add(openvdb::Vec3R( , , ), , openvdb::Vec3R( , , ));
  28. pa.add(openvdb::Vec3R( , , ), , openvdb::Vec3R( , , ));
  29. pa.add(openvdb::Vec3R( , , ), , openvdb::Vec3R( , , ));
  30. pa.add(openvdb::Vec3R( , , ), , openvdb::Vec3R( , , ));
  31.  
  32. openvdb::FloatGrid::Ptr densityGrid;
  33. openvdb::math::Transform::Ptr transform;
  34. buildingDensityGrid(densityGrid,transform,pa);
  35. densityGrid->setName("density");
  36. convertToVolume(densityGrid);
  37.  
  38. // Create a VDB file object.
  39. std::cout << "write vdb \n";
  40. openvdb::io::File file("mygrids.vdb");
  41. // Add the grid pointer to a container.
  42. openvdb::GridPtrVec grids;
  43. grids.push_back(densityGrid);
  44. // Write out the contents of the container.
  45. file.write(grids);
  46. file.close();
  47.  
  48. }

接下来VelocityBuilding,直接写了个包裹库,并且在houdini测试了下openvdb::Vec3sGrid的运动模糊。

GLY_OpenVdbWrapper.h

  1. //
  2. // Created by gearslogy on 4/13/17.
  3. //
  4.  
  5. #ifndef ARNOLDVDBPOINTS_GLY_OPENVDBWAPPER_H
  6. #define ARNOLDVDBPOINTS_GLY_OPENVDBWAPPER_H
  7.  
  8. #include <memory>
  9. #include <openvdb/openvdb.h>
  10. #include <openvdb/tools/ParticlesToLevelSet.h>
  11. #include <openvdb/tools/LevelSetUtil.h>
  12. #include <openvdb/tools/TopologyToLevelSet.h>
  13.  
  14. // DEFINE OUR PARTICLES STRUCT
  15. namespace TopVertex
  16. {
  17.  
  18. struct MyParticle
  19. {
  20. openvdb::Vec3R p, v;
  21. openvdb::Real r; // per particle has own radius
  22. };
  23.  
  24. class MyParticleList
  25. {
  26. // change protected to the public,direct find the mRadiusScale,mVelocityScale
  27. public:
  28. openvdb::Real mRadiusScale;
  29. openvdb::Real mVelocityScale;
  30. std::vector<MyParticle> mParticleList;
  31. typedef openvdb::Vec3R PosType;
  32. MyParticleList(openvdb::Real rScale=, openvdb::Real vScale=)
  33. : mRadiusScale(rScale), mVelocityScale(vScale) {}
  34. void add(const openvdb::Vec3R &p, const openvdb::Real &r,
  35. const openvdb::Vec3R &v=openvdb::Vec3R(,,))
  36. {
  37. MyParticle pa;
  38. pa.p = p;
  39. pa.r = r;
  40. pa.v = v;
  41. mParticleList.push_back(pa);
  42. }
  43. /// @return coordinate bbox in the space of the specified transfrom
  44. openvdb::CoordBBox getBBox(const openvdb::GridBase& grid) {
  45. openvdb::CoordBBox bbox;
  46. openvdb::Coord &min= bbox.min(), &max = bbox.max();
  47. openvdb::Vec3R pos;
  48. openvdb::Real rad, invDx = /grid.voxelSize()[];
  49. for (size_t n=, e=this->size(); n<e; ++n) {
  50. this->getPosRad(n, pos, rad);
  51. const openvdb::Vec3d xyz = grid.worldToIndex(pos);
  52. const openvdb::Real r = rad * invDx;
  53. for (int i=; i<; ++i) {
  54. min[i] = openvdb::math::Min(min[i], openvdb::math::Floor(xyz[i] - r));
  55. max[i] = openvdb::math::Max(max[i], openvdb::math::Ceil( xyz[i] + r));
  56. }
  57. }
  58. return bbox;
  59. }
  60. //typedef int AttributeType;
  61. // The methods below are only required for the unit-tests
  62. openvdb::Vec3R pos(int n) const {return mParticleList[n].p;}
  63. openvdb::Vec3R vel(int n) const {return mVelocityScale*mParticleList[n].v;}
  64. openvdb::Real radius(int n) const {return mRadiusScale*mParticleList[n].r;}
  65.  
  66. //////////////////////////////////////////////////////////////////////////////
  67. /// The methods below are the only ones required by tools::ParticleToLevelSet
  68. /// @note We return by value since the radius and velocities are modified
  69. /// by the scaling factors! Also these methods are all assumed to
  70. /// be thread-safe.
  71.  
  72. /// Return the total number of particles in list.
  73. /// Always required!
  74. size_t size() const { return mParticleList.size(); }
  75.  
  76. /// Get the world space position of n'th particle.
  77. /// Required by ParticledToLevelSet::rasterizeSphere(*this,radius).
  78. void getPos(size_t n, openvdb::Vec3R&pos) const { pos = mParticleList[n].p; }
  79.  
  80. void getPosRad(size_t n, openvdb::Vec3R& pos, openvdb::Real& rad) const {
  81. pos = mParticleList[n].p;
  82. rad = mRadiusScale*mParticleList[n].r;
  83. }
  84. void getPosRadVel(size_t n, openvdb::Vec3R& pos, openvdb::Real& rad, openvdb::Vec3R& vel) const {
  85. pos = mParticleList[n].p;
  86. rad = mRadiusScale*mParticleList[n].r;
  87. vel = mVelocityScale*mParticleList[n].v;
  88. }
  89. // The method below is only required for attribute transfer
  90. void getAtt(size_t n, openvdb::Index32& att) const { att = openvdb::Index32(n); }
  91. };
  92.  
  93. }
  94.  
  95. //
  96. namespace TopVertex
  97. {
  98. class GLY_OpenVdbWrapper
  99. {
  100. public:
  101. // Rater point parm
  102. struct RasterParms
  103. {
  104. float backGround;
  105. float voxelSize;
  106. float halfWidth;
  107. };
  108.  
  109. // define some variable type
  110. using Ptr = std::shared_ptr<GLY_OpenVdbWrapper>;
  111. using RasterT = openvdb::tools::ParticlesToLevelSet<openvdb::FloatGrid, openvdb::Index32>;
  112. enum POINT_RASTER_TYPE{RS_Sphere=0x0,RS_trailer=0x1};
  113.  
  114. // define a static pointer to our class
  115. static GLY_OpenVdbWrapper* creator();
  116.  
  117. //
  118. GLY_OpenVdbWrapper();
  119. ~GLY_OpenVdbWrapper();
  120.  
  121. // SAMPLE POINTS API
  122. void samplePointsSetPoints(const std::vector<openvdb::Vec3R> &posList);
  123. void samplePointsSetPoints(double *array,int rawSize);
  124. void samplePointsSetRadius(double *array,int rawSize);
  125. void samplePointsSetRadius(const std::vector<openvdb::Real> &radiusList);
  126. void samplePointsSetVelocity(const std::vector<openvdb::Vec3R> &vel);
  127. void samplePointsSetVelocity(double *array,int rawsize);
  128. void samplePointsSetRadiusScale(double radius);
  129. void samplePointsSetVelocityScale(double v);
  130. void samplePointsAppendPoint(openvdb::Vec3R p,openvdb::Vec3R v,openvdb::Real radius);
  131. void samplePointsRasterDensityGrid(POINT_RASTER_TYPE type,openvdb::FloatGrid::Ptr &gridPtr,RasterParms &rasterParm);
  132. void samplePointsRasterVelocityGrid(openvdb::math::Transform::Ptr &density_transform,openvdb::Vec3SGrid::Ptr &velocityGrid);
  133.  
  134. private:
  135. class SamplePoints;
  136. std::unique_ptr<SamplePoints> mPimplSamplePoints;
  137. };
  138. }
  139.  
  140. #endif //ARNOLDVDBPOINTS_GLY_OPENVDBWAPPER_H

GLY_OpenVdbWrapper.cpp

  1. //
  2. // Created by gearslogy on 4/13/17.
  3. //
  4.  
  5. #include "GLY_OpenVdbWrapper.h"
  6. #include <assert.h>
  7. #include <algorithm>
  8. using namespace TopVertex;
  9.  
  10. //=======================================SamplePoints details=================================
  11. //
  12. class GLY_OpenVdbWrapper::SamplePoints
  13. {
  14. public:
  15. SamplePoints():mParticleListPtr(new MyParticleList(,))
  16. {
  17. }
  18. ~SamplePoints()
  19. {
  20. std::cout << "Release SamplePoints memory\n";
  21. }
  22. void setRadiusScale(double radius)
  23. {
  24. mParticleListPtr->mRadiusScale = radius;
  25. }
  26. void setVelocityScale(double v)
  27. {
  28. mParticleListPtr->mVelocityScale = v;
  29. }
  30.  
  31. void setPoints(const std::vector<openvdb::Vec3R> &posList)
  32. {
  33. auto &pa = mParticleListPtr->mParticleList;
  34. pa.resize(posList.size());
  35. for(int i=;i<posList.size();i++)
  36. {
  37. pa[i].p = posList[i];
  38. }
  39. }
  40. void setPoints(double *array,int rawsize)
  41. {
  42. assert(rawsize%==);
  43. auto &pa = mParticleListPtr->mParticleList;
  44. pa.resize(rawsize/);
  45. for(int i=;i<rawsize/;i++)
  46. {
  47. double x = array[i];
  48. double y = array[i+];
  49. double z = array[i+];
  50. auto t = openvdb::Vec3R(x,y,z);
  51. pa[i].p = t;
  52. }
  53. }
  54. void setRadius(const std::vector<openvdb::Real> &radiusList)
  55. {
  56. assert(radiusList.size()==mParticleListPtr->mParticleList.size());
  57. auto &pa = mParticleListPtr->mParticleList;
  58. for(int i=;i<pa.size();i++)
  59. {
  60. pa[i].r = radiusList[i];
  61. }
  62. }
  63. void setRadius(double *array,int pointsNum)
  64. {
  65. assert(pointsNum==mParticleListPtr->mParticleList.size());
  66. auto &pa = mParticleListPtr->mParticleList;
  67. for(int i=;i<pa.size();i++)
  68. {
  69. pa[i].r = array[i];
  70. }
  71. }
  72. void setVelocity(const std::vector<openvdb::Vec3R> &vel)
  73. {
  74. assert(vel.size() == mParticleListPtr->mParticleList.size());
  75. auto &pa = mParticleListPtr->mParticleList;
  76. for(int i=;i<vel.size();i++)
  77. {
  78. pa[i].v = vel[i];
  79. }
  80. }
  81. void setVelocity(double *array,int rawsize)
  82. {
  83. assert(rawsize%==);
  84. auto &pa = mParticleListPtr->mParticleList;
  85. for(int i=;i<rawsize/;i++)
  86. {
  87. double x = array[i];
  88. double y = array[i+];
  89. double z = array[i+];
  90. auto t = openvdb::Vec3R(x,y,z);
  91. pa[i].v = t;
  92. }
  93. }
  94. void appendPoint(openvdb::Vec3R p,openvdb::Vec3R v,openvdb::Real radius)
  95. {
  96. mParticleListPtr->add(p,radius,v);
  97. }
  98. void clearPoints(){mParticleListPtr->mParticleList.clear();}
  99. void rasterDensity(POINT_RASTER_TYPE type,openvdb::FloatGrid::Ptr &gridPtr,RasterParms &rasterParm)
  100. {
  101.  
  102. std::cout << "Start process samplePoints Raster Density\n";
  103. float backGround = rasterParm.backGround;
  104. float voxelSize = rasterParm.voxelSize;
  105. float halfWidth = rasterParm.halfWidth;
  106. openvdb::math::Transform::Ptr transform = openvdb::math::Transform::createLinearTransform(voxelSize);
  107. //gridPtr = openvdb::FloatGrid::create(backGround); //this is simple and can work
  108. gridPtr = openvdb::createLevelSet<openvdb::FloatGrid>(voxelSize, halfWidth);
  109.  
  110. gridPtr->setGridClass(openvdb::GRID_LEVEL_SET);
  111. gridPtr->setTransform(transform);
  112. openvdb::tools::ParticlesToLevelSet<openvdb::FloatGrid,openvdb::Index> raster(*gridPtr);
  113. if(type==0x0) // RS_Sphere
  114. {
  115. raster.rasterizeSpheres(*mParticleListPtr);
  116. } else
  117. {
  118. raster.rasterizeTrails(*mParticleListPtr,0.75);
  119. }
  120. raster.finalize(true);
  121. openvdb::tools::sdfToFogVolume(*gridPtr);
  122. gridPtr->setName("density");
  123. mId=raster.attributeGrid();
  124. }
  125.  
  126. void rasterVelocityGrid(openvdb::math::Transform::Ptr &density_transform,openvdb::Vec3SGrid::Ptr &gridPtr)
  127. {
  128. typedef typename openvdb::Int32Grid::TreeType::ValueConverter<openvdb::Vec3s >::Type TreeTypeWarpVec;
  129. typedef typename openvdb::Grid<TreeTypeWarpVec> GridType;
  130. typename TreeTypeWarpVec::Ptr tree(
  131. new TreeTypeWarpVec(mId->tree(), openvdb::Vec3s(,,) , openvdb::TopologyCopy()));
  132. //typename GridType::Ptr velocity_grid(GridType::create(tree)); //为grid开辟内存*/
  133.  
  134. gridPtr = openvdb::Vec3SGrid::create(tree);
  135. gridPtr->setVectorType(openvdb::VecType());
  136. // MY Method
  137. openvdb::Coord ijk;
  138. openvdb::Vec3SGrid::Accessor vel_accessor = gridPtr->getAccessor();
  139. for(auto iter = mId->beginValueOn();iter.test();++iter)
  140. {
  141. auto d = *iter;
  142. //std::cout << " D:..." <<d <<std::endl;
  143. ijk = iter.getCoord();
  144. openvdb::math::Vec3s vel = mParticleListPtr->vel(d);
  145. vel*=;
  146. vel_accessor.setValue(ijk,vel);
  147.  
  148. }
  149. gridPtr->setName("vel");
  150. gridPtr->setTransform(density_transform);
  151. }
  152.  
  153. private:
  154. std::unique_ptr<MyParticleList > mParticleListPtr;
  155. RasterT::AttGridType::Ptr mId; //remeber the id of point in voxel
  156. };
  157.  
  158. //
  159.  
  160. //============================================GLY_OpenVdbWrapper==================================================
  161. // GLY_OpenVdbWrapper Details
  162.  
  163. GLY_OpenVdbWrapper::GLY_OpenVdbWrapper():mPimplSamplePoints(new GLY_OpenVdbWrapper::SamplePoints())
  164. {
  165. }
  166. GLY_OpenVdbWrapper::~GLY_OpenVdbWrapper()
  167. {
  168. std::cout << "Release Wrapper memory\n";
  169. }
  170. void GLY_OpenVdbWrapper::samplePointsSetPoints(const std::vector<openvdb::Vec3R> &posList)
  171. {
  172. mPimplSamplePoints->setPoints(posList);
  173. }
  174. void GLY_OpenVdbWrapper::samplePointsSetPoints(double *array,int rawSize)
  175. {
  176. mPimplSamplePoints->setPoints(array,rawSize);
  177. }
  178. void GLY_OpenVdbWrapper::samplePointsSetRadius(double *array, int rawSize) {
  179. mPimplSamplePoints->setRadius(array,rawSize);
  180. }
  181. void GLY_OpenVdbWrapper::samplePointsSetRadius(const std::vector<openvdb::Real> &radiusList) {
  182. mPimplSamplePoints->setRadius(radiusList);
  183. }
  184. void GLY_OpenVdbWrapper::samplePointsSetVelocity(const std::vector<openvdb::Vec3R> &vel) {
  185. mPimplSamplePoints->setVelocity(vel);
  186. }
  187. void GLY_OpenVdbWrapper::samplePointsSetVelocity(double *array, int rawsize) {
  188. mPimplSamplePoints->setVelocity(array,rawsize);
  189. }
  190. void GLY_OpenVdbWrapper::samplePointsSetRadiusScale(double radius) {
  191. mPimplSamplePoints->setRadiusScale(radius);
  192. }
  193. void GLY_OpenVdbWrapper::samplePointsSetVelocityScale(double v) {
  194. mPimplSamplePoints->setVelocityScale(v);
  195. }
  196. void GLY_OpenVdbWrapper::samplePointsAppendPoint(openvdb::Vec3R p, openvdb::Vec3R v, openvdb::Real radius)
  197. {
  198. mPimplSamplePoints->appendPoint(p,v,radius);
  199. }
  200. GLY_OpenVdbWrapper* GLY_OpenVdbWrapper::creator() {
  201. return new GLY_OpenVdbWrapper;
  202. }
  203. void GLY_OpenVdbWrapper::samplePointsRasterDensityGrid(POINT_RASTER_TYPE type, openvdb::FloatGrid::Ptr &gridPtr,
  204. RasterParms &rasterParm) {
  205. mPimplSamplePoints->rasterDensity(type,gridPtr,rasterParm);
  206. }
  207. void GLY_OpenVdbWrapper::samplePointsRasterVelocityGrid(openvdb::math::Transform::Ptr &density_transform,
  208. openvdb::Vec3SGrid::Ptr &velocityGrid)
  209. {
  210. mPimplSamplePoints->rasterVelocityGrid(density_transform,velocityGrid);
  211. }

main.cpp:

  1. //
  2. // Created by gearslogy on 4/14/17.
  3. //
  4.  
  5. #include "GLY_OpenVdbWrapper.h"
  6. using namespace std;
  7. using namespace TopVertex;
  8. int main()
  9. {
  10. GLY_OpenVdbWrapper::Ptr wrapper(GLY_OpenVdbWrapper::creator());
  11. wrapper->samplePointsAppendPoint(openvdb::Vec3R(, , ), openvdb::Vec3R( , , ) ,);
  12. wrapper->samplePointsAppendPoint(openvdb::Vec3R(, , ), openvdb::Vec3R( , , ) ,1.5);
  13. wrapper->samplePointsAppendPoint(openvdb::Vec3R(, , ), openvdb::Vec3R( , , ) ,2.0);
  14. wrapper->samplePointsAppendPoint(openvdb::Vec3R(, , ), openvdb::Vec3R( , , ) ,2.5);
  15. wrapper->samplePointsAppendPoint(openvdb::Vec3R(, , ), openvdb::Vec3R( , , ) ,3.0);
  16.  
  17. // create grid named "density"
  18. openvdb::FloatGrid::Ptr densityGrid;
  19. GLY_OpenVdbWrapper::RasterParms parms;
  20. parms.backGround = 0.1;
  21. parms.voxelSize = 0.1;
  22. parms.halfWidth = 0.5;
  23. wrapper->samplePointsRasterDensityGrid(GLY_OpenVdbWrapper::RS_Sphere,densityGrid,parms);
  24.  
  25. // Create velocity Grid "velocity"
  26. openvdb::Vec3SGrid::Ptr velocityGrid;
  27. auto densityTransform = densityGrid->transformPtr();
  28. wrapper->samplePointsRasterVelocityGrid(densityTransform,velocityGrid);
  29.  
  30. // IO Operator
  31. openvdb::io::File file("mygrids.vdb");
  32. openvdb::GridPtrVec grids;
  33. grids.push_back(densityGrid);
  34. grids.push_back(velocityGrid);
  35. file.write(grids);
  36. file.close();
  37.  
  38. }

Test plugin for katana:

Update katana plugin:

升级了光线求交,直接快的飞起来

Arnold粒子体积渲染(Arnold particles volume rendering):

插件loading模式:AlembicAPI->OpenvdbAPI->ArnoldAPI

然后KatanaAPI再写个插件 读取这个ArnoldAPI写出来的proc,

update volume:

重要的事情不说两遍,static 关键字在一个so上被一个进程,2个instance调用,全局的static object内存地址是他妈一样的,也就是说是共享的地址。会导致你假如创建两个instance,你却希望有两份不一样的全局变量内容,结果,太感人了,确实是错的,是一样的。

如果独立进程,独立instance调用so上的全局变量,ok没问题。

Rendering the cd field:

VDB R&D的更多相关文章

  1. openstack搭建之-cinder配置(12)

    一. base节点配置 mysql -u root -proot CREATE DATABASE cinder; GRANT ALL PRIVILEGES ON cinder.* TO 'cinder ...

  2. openstack实验环境搭建

    Openstack实验文档 一.base节点 1.1配置网络 vim /etc/sysconfig/network-scripts/ifcfg-eth0 1.2关闭防火墙和selinux system ...

  3. S1_搭建分布式OpenStack集群_10 cinder 存储节点配置

    一.安装配置lvm2安装LVM包:# yum install -y lvm2 启动LVM元数据服务,并将其配置为在系统启动时启动:# systemctl enable lvm2-lvmetad.ser ...

  4. [原]CentOS7安装Rancher2.1并部署kubernetes (二)---部署kubernetes

    ##################    Rancher v2.1.7  +    Kubernetes 1.13.4  ################ ##################### ...

  5. 利用python进行数据分析2_数据采集与操作

    txt_filename = './files/python_baidu.txt' # 打开文件 file_obj = open(txt_filename, 'r', encoding='utf-8' ...

  6. Django项目:CRM(客户关系管理系统)--81--71PerfectCRM实现CRM项目首页

    {#portal.html#} {## ————————46PerfectCRM实现登陆后页面才能访问————————#} {#{% extends 'king_admin/table_index.h ...

  7. 【翻译】Awesome R资源大全中文版来了,全球最火的R工具包一网打尽,超过300+工具,还在等什么?

    0.前言 虽然很早就知道R被微软收购,也很早知道R在统计分析处理方面很强大,开始一直没有行动过...直到 直到12月初在微软技术大会,看到我软的工程师演示R的使用,我就震惊了,然后最近在网上到处了解和 ...

  8. 应用r.js来优化你的前端

    r.js是requireJS的优化(Optimizer)工具,可以实现前端文件的压缩与合并,在requireJS异步按需加载的基础上进一步提供前端优化,减小前端文件大小.减少对服务器的文件请求.要使用 ...

  9. 使用R画地图数据

    用R画地图数据 首先,从这里下载中国地图的GIS数据,这是一个压缩包,完全解压后包含三个文件(bou2_4p.dbf.bou2_4p.shp和bou2_4p.shx),将这三个文件解压到同一个目录下. ...

随机推荐

  1. BZOJ2815 拓扑排序 + LCA

    https://www.lydsy.com/JudgeOnline/problem.php?id=2815 作为一个DAG图,结点之间又有这么明显的等级之分,很容易想到的是拓扑排序. 但是不管是正向的 ...

  2. MySQL数据库优化_索引

    1.添加索引后减少查询需要的行数,提高查询性能 (1) 建表 CREATE TABLE `site_user` ( `id` ) NOT NULL AUTO_INCREMENT COMMENT '自增 ...

  3. Kafka各个版本差异汇总

    Kafka各个版本差异汇总   从0.8.x,0.9.x,0.10.0.x,0.10.1.x,0.10.2.x,0.11.0.x,1.0.x或1.1.x升级到2.0.0 Kafka 2.0.0引入了线 ...

  4. springMVC的全局拦截器

    先说说为什么要使用springMVC的全局拦截器,比如 当我们在访问接口的时候,我们一般都会先判断这个用户是否登陆,我们就要在每个接口的前面都要判断一下,想想是不是很蛋疼,那工作量... 这时候,我们 ...

  5. MySQL数据类型1

    1.float.double.decimal类型用法详解 三者的区别介绍 float:浮点型,含字节数为4,32bit,数值范围为-3.4E38~3.4E38(7个有效位) double:双精度实型, ...

  6. redis安全问题【原】

    前提 假设redis安装在 IP 地址为 192.168.0.123 的linux服务器 . 我的本机Win10操作系统 IP地址为 192.168.0.45 , 有一套java客户端代码可调用lin ...

  7. 让Windows Server 2008r2 IIS7.5 ASP.NET 支持10万并发请求

    由于之前使用的是默认配置,服务器最多只能处理5000个同时请求,今天下午由于某种情况造成同时请求超过5000,从而出现了上面的错误. 为了避免这样的错误,我们根据相关文档调整了设置,让服务器从设置上支 ...

  8. JAVA方法调用中的解析与分派

    JAVA方法调用中的解析与分派 本文算是<深入理解JVM>的读书笔记,参考书中的相关代码示例,从字节码指令角度看看解析与分派的区别. 方法调用,其实就是要回答一个问题:JVM在执行一个方法 ...

  9. C#基础_MD5

    MD5加密 1创建Md5 2.开始加密,需要将字符转换为字节数组 3.返回一个加密好的字节数组 4.将字节数组中每个元素按照指定的编码格式解析成字符串 1 static void Main(strin ...

  10. 【noip 2016】普及组

    T1.买铅笔 题目链接 #include<cstdio> #include<algorithm> #include<cstring> using namespace ...