|
| 1 | +using PicoGK; |
| 2 | + |
| 3 | +namespace PicoGKExamples |
| 4 | +{ |
| 5 | + class OpenVdbExample |
| 6 | + { |
| 7 | + /// <summary> |
| 8 | + /// his function comprehensively explains how to deal with OpenVDB |
| 9 | + /// (.vdb) files. These files can contain many different fields and |
| 10 | + /// each field be of a number of types. |
| 11 | + /// |
| 12 | + /// PicoGK Voxels are stored in GRID_LEVEL_SET data structures |
| 13 | + /// PicoGK doesn't support any other data types at the moment. |
| 14 | + /// |
| 15 | + /// If you want to make sure you deal with all of these cases |
| 16 | + /// this detailed function is for you |
| 17 | + /// |
| 18 | + /// If you have control over the files and just want to load/save |
| 19 | + /// voxel data in PicoGK, you can use the helper functions |
| 20 | + /// implemented in PicoGK_VoxelsIo.cs |
| 21 | + /// |
| 22 | + /// Basically you do: |
| 23 | + /// |
| 24 | + /// vox.SaveToVdbFile("Hello.vdb") |
| 25 | + /// |
| 26 | + /// and |
| 27 | + /// |
| 28 | + /// Voxels vox = Voxels.voxFromVdbFile("Hello.vdb") |
| 29 | + /// |
| 30 | + /// Check out the end of the function below, for an example |
| 31 | + /// |
| 32 | + /// </summary> |
| 33 | + public static void Task() |
| 34 | + { |
| 35 | + // Create a mesh from an existing STL file |
| 36 | + Mesh msh = Mesh.mshFromStlFile( |
| 37 | + Path.Combine( Utils.strPicoGKSourceCodeFolder(), |
| 38 | + "Examples/Testfiles/Teapot.stl")); |
| 39 | + |
| 40 | + // Create Voxels from the mesh |
| 41 | + Voxels vox = new Voxels(msh); |
| 42 | + |
| 43 | + // Create an empty VDB file object |
| 44 | + OpenVdbFile oFileCreated = new OpenVdbFile(); |
| 45 | + |
| 46 | + // Add the voxels to the VDB file object |
| 47 | + // You can add multiple fields to an object |
| 48 | + // The name is optional |
| 49 | + oFileCreated.nAdd(vox, "Teapot"); |
| 50 | + |
| 51 | + // Let's add another field, Vdb supports multiple |
| 52 | + oFileCreated.nAdd(vox, "Another One"); |
| 53 | + |
| 54 | + // Let's add a third field |
| 55 | + oFileCreated.nAdd(vox, "Three"); |
| 56 | + |
| 57 | + // Let's add a fourth field with an auto generated name |
| 58 | + oFileCreated.nAdd(vox); |
| 59 | + |
| 60 | + // Note, internally copies are made of the voxel fields, so |
| 61 | + // after you add a voxel field, any changes made to it are not |
| 62 | + // reflected when you save it |
| 63 | + |
| 64 | + // Filename to use |
| 65 | + string strVdbFileName = Path.Combine( Library.strLogFolder, |
| 66 | + "Teapot.vdb"); |
| 67 | + |
| 68 | + Library.Log($"In memory VdbFile object contains {oFileCreated.nFieldCount()} fields"); |
| 69 | + |
| 70 | + for (int nField = 0; nField < oFileCreated.nFieldCount(); nField++) |
| 71 | + { |
| 72 | + Library.Log($"- Field {nField} has type {oFileCreated.strFieldType(nField)} and name '{oFileCreated.strFieldName(nField)}'"); |
| 73 | + } |
| 74 | + |
| 75 | + // Save the VdbFile object to an actual file on disk |
| 76 | + oFileCreated.SaveToFile(strVdbFileName); |
| 77 | + |
| 78 | + // Load the saved VdbFile from disk |
| 79 | + // If this fails, an exception is thrown |
| 80 | + OpenVdbFile oFileLoad = new OpenVdbFile(strVdbFileName); |
| 81 | + |
| 82 | + Library.Log($"Loaded VdbFile {strVdbFileName}"); |
| 83 | + Library.Log($"VdbFile contains {oFileLoad.nFieldCount()} fields"); |
| 84 | + |
| 85 | + for (int nField = 0; nField <oFileLoad.nFieldCount(); nField++) |
| 86 | + { |
| 87 | + Library.Log($"- Field {nField} has type {oFileLoad.strFieldType(nField)} and name '{oFileLoad.strFieldName(nField)}'"); |
| 88 | + } |
| 89 | + |
| 90 | + // Extract field number with the name Teapot (case insensitive) |
| 91 | + // .vdb files support several fields of various types, which |
| 92 | + // are unsupported in PicoGK. |
| 93 | + // Also be aware that the order of which these fields are |
| 94 | + // written to the file may often not be the same order that we |
| 95 | + // added it to the object, so don't rely on the third added |
| 96 | + // field to be the third field in the file |
| 97 | + // The safest way to get exactly what you want is to search by |
| 98 | + // name. |
| 99 | + |
| 100 | + Voxels voxRead = oFileLoad.voxGet("Teapot"); |
| 101 | + |
| 102 | + // Show what we loaded |
| 103 | + Library.oViewer().Add(voxRead); |
| 104 | + |
| 105 | + // Lastly, let's demonstrate the simple helper functions |
| 106 | + // which you can use when you have full control over |
| 107 | + // the origin VDB files (no surprises) |
| 108 | + |
| 109 | + // Create a Voxel field from the first compatible field in a VDB |
| 110 | + // file. If there are multiple fields, all others are ignored |
| 111 | + // If no compatible field is found, an exception is thrown |
| 112 | + Voxels voxReadSimple = Voxels.voxFromVdbFile(strVdbFileName); |
| 113 | + |
| 114 | + // Now lets save the Voxels to a new file |
| 115 | + voxReadSimple.SaveToVdbFile(Path.Combine( Library.strLogFolder, |
| 116 | + "Simple.vdb")); |
| 117 | + |
| 118 | + // This file now contains exactly one field, |
| 119 | + // and uses an auto-generated field name |
| 120 | + } |
| 121 | + } |
| 122 | +} |
| 123 | + |
0 commit comments