Skip to content

Commit

Permalink
Added new function to build Scalar Field from Voxels
Browse files Browse the repository at this point in the history
A new constructor for ScalarField allows you to build a new scalar field for all "surface/inside" voxels, using a default value. This is useful to build, for example, a for density domain for simulation exchange, for all voxels making up the fluid.

Requires PicoGK Runtime 1.6

Added usage example to Ex_SimulationSetup.cs
  • Loading branch information
LinKayser committed May 23, 2024
1 parent 5475390 commit f78a400
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
9 changes: 7 additions & 2 deletions Examples/Ex_SimulationSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,14 +179,19 @@ VectorField oInletField

Library.oViewer().SetGroupVisible(10, false);

// Create a new scalar field, which contains the density of water
// at all voxels which are in the fluid domain (997 kg/m³)
ScalarField oFluidDensity = new(voxFluid, 997.0f);

OpenVdbFile oFile = new();

voxFluid.m_oMetadata.SetValue("Simulation.Material.Name", "Water");
voxFluid.m_oMetadata.SetValue("Simulation.Material.Type", "Liquid");
oFile.nAdd(voxFluid, "Simulation.FluidDomain");

oFile.nAdd(voxManifold, "Simulation.SolidDomain");
oFile.nAdd(oFlowSpeed, "Simulation.FlowSpeed");
oFile.nAdd(voxManifold, "Simulation.SolidDomain");
oFile.nAdd(oFlowSpeed, "Simulation.FlowSpeed");
oFile.nAdd(oFluidDensity, "Simulation.FluidDensity");

string strVdbFile = Path.Combine(Utils.strDocumentsFolder(), "Sim.vdb");
oFile.SaveToFile(strVdbFile);
Expand Down
13 changes: 12 additions & 1 deletion PicoGK_ScalarField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,25 @@ public ScalarField(in ScalarField oSource)

/// <summary>
/// Creates a scalar field from an existing voxel field
/// the scalar field contains the signed distance values
/// setting the voxels inside the object to the specified value
/// of the voxels
/// </summary>
/// <param name="oVoxels">Voxels to create SDF from</param>
/// <param name="fValue">Value to set the scalar field to</param>"
/// <param name="fSdThreshold">The threshold of the signed distance field
/// to be used for the definition of "inside" - usually 0.5 is a good
/// value - the surface is at exactly 0 and a value of
/// 1.0 means you are 1 voxel outside from the surface.</param>
public ScalarField(Voxels oVoxels)
: this(_hCreateFromVoxels(oVoxels.m_hThis))
{ }

public ScalarField( Voxels oVoxels,
float fValue,
float fSdThreshold = 0.5f)
: this(_hBuildFromVoxels(oVoxels.m_hThis, fValue, fSdThreshold))
{}

/// <summary>
/// Sets the value at the specified position in mm
/// When you set a value, the position gets "activated"
Expand Down
3 changes: 3 additions & 0 deletions PicoGK__Interop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,9 @@ public partial class ScalarField : IDisposable
[DllImport(Config.strPicoGKLib, CallingConvention = CallingConvention.Cdecl, EntryPoint = "ScalarField_hCreateFromVoxels")]
public static extern IntPtr _hCreateFromVoxels(IntPtr hVoxels);

[DllImport(Config.strPicoGKLib, CallingConvention = CallingConvention.Cdecl, EntryPoint = "ScalarField_hBuildFromVoxels")]
public static extern IntPtr _hBuildFromVoxels(IntPtr hVoxels, float fScalarValue, float fSdThreshold);

[DllImport(Config.strPicoGKLib, CallingConvention = CallingConvention.Cdecl, EntryPoint = "ScalarField_bIsValid")]
private static extern bool _bIsValid(IntPtr hThis);

Expand Down

0 comments on commit f78a400

Please sign in to comment.