Skip to content

Commit

Permalink
V1.2 adds .VDB file support
Browse files Browse the repository at this point in the history
Added OpenVDB .vdb file support
This requires runtime v1.2
  • Loading branch information
LinKayser committed Jan 22, 2024
1 parent 5b46472 commit 6301db6
Show file tree
Hide file tree
Showing 7 changed files with 560 additions and 19 deletions.
123 changes: 123 additions & 0 deletions Examples/Ex_VdbIo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
using PicoGK;

namespace PicoGKExamples
{
class OpenVdbExample
{
/// <summary>
/// his function comprehensively explains how to deal with OpenVDB
/// (.vdb) files. These files can contain many different fields and
/// each field be of a number of types.
///
/// PicoGK Voxels are stored in GRID_LEVEL_SET data structures
/// PicoGK doesn't support any other data types at the moment.
///
/// If you want to make sure you deal with all of these cases
/// this detailed function is for you
///
/// If you have control over the files and just want to load/save
/// voxel data in PicoGK, you can use the helper functions
/// implemented in PicoGK_VoxelsIo.cs
///
/// Basically you do:
///
/// vox.SaveToVdbFile("Hello.vdb")
///
/// and
///
/// Voxels vox = Voxels.voxFromVdbFile("Hello.vdb")
///
/// Check out the end of the function below, for an example
///
/// </summary>
public static void Task()
{
// Create a mesh from an existing STL file
Mesh msh = Mesh.mshFromStlFile(
Path.Combine( Utils.strPicoGKSourceCodeFolder(),
"Examples/Testfiles/Teapot.stl"));

// Create Voxels from the mesh
Voxels vox = new Voxels(msh);

// Create an empty VDB file object
OpenVdbFile oFileCreated = new OpenVdbFile();

// Add the voxels to the VDB file object
// You can add multiple fields to an object
// The name is optional
oFileCreated.nAdd(vox, "Teapot");

// Let's add another field, Vdb supports multiple
oFileCreated.nAdd(vox, "Another One");

// Let's add a third field
oFileCreated.nAdd(vox, "Three");

// Let's add a fourth field with an auto generated name
oFileCreated.nAdd(vox);

// Note, internally copies are made of the voxel fields, so
// after you add a voxel field, any changes made to it are not
// reflected when you save it

// Filename to use
string strVdbFileName = Path.Combine( Library.strLogFolder,
"Teapot.vdb");

Library.Log($"In memory VdbFile object contains {oFileCreated.nFieldCount()} fields");

for (int nField = 0; nField < oFileCreated.nFieldCount(); nField++)
{
Library.Log($"- Field {nField} has type {oFileCreated.strFieldType(nField)} and name '{oFileCreated.strFieldName(nField)}'");
}

// Save the VdbFile object to an actual file on disk
oFileCreated.SaveToFile(strVdbFileName);

// Load the saved VdbFile from disk
// If this fails, an exception is thrown
OpenVdbFile oFileLoad = new OpenVdbFile(strVdbFileName);

Library.Log($"Loaded VdbFile {strVdbFileName}");
Library.Log($"VdbFile contains {oFileLoad.nFieldCount()} fields");

for (int nField = 0; nField <oFileLoad.nFieldCount(); nField++)
{
Library.Log($"- Field {nField} has type {oFileLoad.strFieldType(nField)} and name '{oFileLoad.strFieldName(nField)}'");
}

// Extract field number with the name Teapot (case insensitive)
// .vdb files support several fields of various types, which
// are unsupported in PicoGK.
// Also be aware that the order of which these fields are
// written to the file may often not be the same order that we
// added it to the object, so don't rely on the third added
// field to be the third field in the file
// The safest way to get exactly what you want is to search by
// name.

Voxels voxRead = oFileLoad.voxGet("Teapot");

// Show what we loaded
Library.oViewer().Add(voxRead);

// Lastly, let's demonstrate the simple helper functions
// which you can use when you have full control over
// the origin VDB files (no surprises)

// Create a Voxel field from the first compatible field in a VDB
// file. If there are multiple fields, all others are ignored
// If no compatible field is found, an exception is thrown
Voxels voxReadSimple = Voxels.voxFromVdbFile(strVdbFileName);

// Now lets save the Voxels to a new file
voxReadSimple.SaveToVdbFile(Path.Combine( Library.strLogFolder,
"Simple.vdb"));

// This file now contains exactly one field,
// and uses an auto-generated field name
}
}
}

9 changes: 3 additions & 6 deletions PicoGK_Library.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ public partial class Library
/// <returns>The name of the dynamically loaded C++ library</returns>
public static string strName()
{
const int iLen = 255;
StringBuilder oBuilder = new StringBuilder(iLen);
StringBuilder oBuilder = new StringBuilder(Library.nStringLength);
_GetName(oBuilder);
return oBuilder.ToString();
}
Expand All @@ -60,8 +59,7 @@ public static string strName()
/// <returns>The library version of the C++ library</returns>
public static string strVersion()
{
const int iLen = 255;
StringBuilder oBuilder = new StringBuilder(iLen);
StringBuilder oBuilder = new StringBuilder(Library.nStringLength);
_GetVersion(oBuilder);
return oBuilder.ToString();
}
Expand All @@ -73,8 +71,7 @@ public static string strVersion()
/// <returns>Internal build info of the C++ library</returns>
public static string strBuildInfo()
{
const int iLen = 255;
StringBuilder oBuilder = new StringBuilder(iLen);
StringBuilder oBuilder = new StringBuilder(Library.nStringLength);
_GetBuildInfo(oBuilder);
return oBuilder.ToString();
}
Expand Down

0 comments on commit 6301db6

Please sign in to comment.