|
| 1 | +import argparse |
| 2 | +import os |
| 3 | +from pathlib import Path |
| 4 | + |
| 5 | + |
| 6 | +def open_file(file_path): |
| 7 | + """ |
| 8 | + Opens a file. If utf-8 decoding fails, try windows-1252. |
| 9 | +
|
| 10 | + Args: |
| 11 | + file_path: Path of the file to open. |
| 12 | +
|
| 13 | + Returns: |
| 14 | + The content of the file in an arbitrary format. |
| 15 | + """ |
| 16 | + try: |
| 17 | + with open(file_path, "r", encoding="utf-8", errors="replace") as file: |
| 18 | + return file.read() |
| 19 | + except UnicodeDecodeError: |
| 20 | + with open(file_path, "r", encoding="windows-1252", errors="replace") as file: |
| 21 | + return file.read() |
| 22 | + |
| 23 | + |
| 24 | +def write_file(file_path, content): |
| 25 | + """ |
| 26 | + Writes a file. If utf-8 decoding fails, try windows-1252. |
| 27 | +
|
| 28 | + Args: |
| 29 | + file_path: Path of the file to open. |
| 30 | +
|
| 31 | + Returns: |
| 32 | + None. |
| 33 | + """ |
| 34 | + try: |
| 35 | + with open(file_path, "w", encoding="utf-8", errors="replace") as file: |
| 36 | + file.write(content) |
| 37 | + except UnicodeDecodeError: |
| 38 | + with open(file_path, "w", encoding="windows-1252", errors="replace") as file: |
| 39 | + file.write(content) |
| 40 | + |
| 41 | + |
| 42 | +def update_files(dir_path): |
| 43 | + """ |
| 44 | + Updates the content of files ending in .h, .cuh, .cc, .cu, and .cpp |
| 45 | + to call the appropriate API as we update NanoVDB from version 32.6 to |
| 46 | + version 32.7. This includes changes in namespaces, function names, and |
| 47 | + include directories. |
| 48 | +
|
| 49 | + Args: |
| 50 | + Directory path: will include files in downstream directories. |
| 51 | +
|
| 52 | + Returns: |
| 53 | + None. Writes the contents of the file. |
| 54 | + """ |
| 55 | + |
| 56 | + # List of file extensions to search for |
| 57 | + file_extensions = [".h", ".cuh", ".cc", ".cu", ".cpp"] |
| 58 | + |
| 59 | + nspace_dic = { |
| 60 | + "math": [ |
| 61 | + "Ray", |
| 62 | + "DDA<", |
| 63 | + "HDDA", |
| 64 | + "Vec3<", |
| 65 | + "Vec4<", |
| 66 | + "BBox<", |
| 67 | + "ZeroCrossing", |
| 68 | + "TreeMarcher", |
| 69 | + "PointTreeMarcher", |
| 70 | + "BoxStencil<", |
| 71 | + "CurvatureStencil<", |
| 72 | + "GradStencil<", |
| 73 | + "WenoStencil<", |
| 74 | + "AlignUp", |
| 75 | + "Min", |
| 76 | + "Max", |
| 77 | + "Abs", |
| 78 | + "Clamp", |
| 79 | + "Sqrt", |
| 80 | + "Sign", |
| 81 | + "Maximum<", |
| 82 | + "Delta<", |
| 83 | + "RoundDown<", |
| 84 | + "pi<", |
| 85 | + "isApproxZero<", |
| 86 | + "Round<", |
| 87 | + "createSampler", |
| 88 | + "SampleFromVoxels<", |
| 89 | + ], |
| 90 | + "tools": [ |
| 91 | + "createNanoGrid", |
| 92 | + "StatsMode", |
| 93 | + "createLevelSetSphere", |
| 94 | + "createFogVolumeSphere", |
| 95 | + "createFogVolumeSphere createFogVolumeSphere", |
| 96 | + "createFogVolumeTorus", |
| 97 | + "createLevelSetBox", |
| 98 | + "CreateNanoGrid", |
| 99 | + "updateGridStats", |
| 100 | + "evalChecksum", |
| 101 | + "validateChecksum", |
| 102 | + "checkGrid", |
| 103 | + "Extrema", |
| 104 | + ], |
| 105 | + "util": [ |
| 106 | + "is_floating_point", |
| 107 | + "findLowestOn", |
| 108 | + "findHighestOn", |
| 109 | + "Range", |
| 110 | + "streq", |
| 111 | + "strcpy", |
| 112 | + "strcat", |
| 113 | + "empty(", |
| 114 | + "Split", |
| 115 | + "invoke", |
| 116 | + "forEach", |
| 117 | + "reduce", |
| 118 | + "prefixSum", |
| 119 | + "is_same", |
| 120 | + "is_specialization", |
| 121 | + "PtrAdd", |
| 122 | + "PtrDiff", |
| 123 | + ], |
| 124 | + } |
| 125 | + |
| 126 | + rename_dic = { |
| 127 | + # list from func4 in updateFiles.sh |
| 128 | + "nanovdb::build::": "nanovdb::tools::build::", |
| 129 | + "nanovdb::BBoxR": "nanovdb::Vec3dBBox", |
| 130 | + "nanovdb::BBox<nanovdb::Vec3d>": "nanovdb::Vec3dBbox", |
| 131 | + # scope and rename, i.e. list from func2 in updateFiles.sh |
| 132 | + "nanovdb::cudaCreateNodeManager": "nanovdb::cuda::createNodeManager", |
| 133 | + "nanovdb::cudaVoxelsToGrid": "nanovdb::cuda::voxelsToGrid", |
| 134 | + "nanovdb::cudaPointsToGrid": "nanovdb::cuda::pointsToGrid", |
| 135 | + "nanovdb::DitherLUT": "nanovdb::math::DitherLUT", |
| 136 | + "nanovdb::PackedRGBA8": "nanovdb::math::Rgba8", |
| 137 | + "nanovdb::Rgba8": "nanovdb::math::Rgba8", |
| 138 | + "nanovdb::CpuTimer": "nanovdb::util::Timer", |
| 139 | + "nanovdb::GpuTimer": "nanovdb::util::cuda::Timer", |
| 140 | + "nanovdb::CountOn": "nanovdb::util::countOn", |
| 141 | + } |
| 142 | + |
| 143 | + movdir_dic = { |
| 144 | + # list comes from func3 calls on updateFiles.sh |
| 145 | + "util/GridHandle.h": "GridHandle.h", |
| 146 | + "util/BuildGrid.h": "tools/GridBuilder.h", |
| 147 | + "util/GridBuilder.h": "tools/GridBuilder.h", |
| 148 | + "util/IO.h": "io/IO.h", |
| 149 | + "util/CSampleFromVoxels.h": "math/CSampleFromVoxels.h", |
| 150 | + "util/DitherLUT.h": "math/DitherLUT.h", |
| 151 | + "util/HDDA.h": "math/HDDA.h", |
| 152 | + "util/Ray.h": "math/Ray.h", |
| 153 | + "util/SampleFromVoxels.h": "math/SampleFromVoxels.h", |
| 154 | + "util/Stencils.h": "nanovdb/math/Stencils.h", |
| 155 | + "util/CreateNanoGrid.h": "tools/CreateNanoGrid.h", |
| 156 | + "util/Primitives.h": "tools/CreatePrimitives.h", |
| 157 | + "util/GridChecksum.h": "tools/GridChecksum.h", |
| 158 | + "util/GridStats.h": "tools/GridStats.h", |
| 159 | + "util/GridChecksum.h": "tools/GridChecksum.h", |
| 160 | + "util/GridValidator.h": "tools/GridValidator.h", |
| 161 | + "util/NanoToOpenVDB.h": "tools/NanoToOpenVDB.h", |
| 162 | + "util/cuda/CudaGridChecksum.cuh": "tools/cuda/CudaGridChecksum.cuh", |
| 163 | + "util/cuda/CudaGridStats.cuh": "tools/cuda/CudaGridStats.cuh", |
| 164 | + "util/cuda/CudaGridValidator.cuh": "tools/cuda/CudaGridValidator.cuh", |
| 165 | + "util/cuda/CudaIndexToGrid.cuh": "tools/cuda/CudaIndexToGrid.cuh", |
| 166 | + "util/cuda/CudaPointsToGrid.cuh": "tools/cuda/PointsToGrid.cuh", |
| 167 | + "util/cuda/CudaSignedFloodFill.cuh": "tools/cuda/CudaSignedFloodFill.cuh", |
| 168 | + "util/cuda/CudaDeviceBuffer.h": "cuda/DeviceBuffer.h", |
| 169 | + "util/cuda/CudaGridHandle.cuh": "cuda/GridHandle.cuh", |
| 170 | + "util/cuda/CudaUtils.h": "util/cuda/Util.h", |
| 171 | + "util/cuda/GpuTimer.h": "util/cuda/Timer.h", |
| 172 | + } |
| 173 | + |
| 174 | + # Iterate over files in the directory and its subdirectories |
| 175 | + for root, dirs, files in os.walk(dir_path): |
| 176 | + for file in files: |
| 177 | + if any(file.endswith(ext) for ext in file_extensions): |
| 178 | + file_path = os.path.join(root, file) |
| 179 | + print(f"Processing file: {file_path}") |
| 180 | + |
| 181 | + content = open_file(file_path) |
| 182 | + |
| 183 | + # Correspond to func1 $file in updateFiles.sh |
| 184 | + for key, vals in nspace_dic.items(): |
| 185 | + for val in vals: |
| 186 | + old_word = "nanovdb::" + val |
| 187 | + new_word = "nanovdb::" + key + "::" + val |
| 188 | + content = content.replace(old_word, new_word) |
| 189 | + |
| 190 | + # Correspond to func4 and func2 in updateFiles.sh |
| 191 | + for key, val in rename_dic.items(): |
| 192 | + content = content.replace(key, val) |
| 193 | + |
| 194 | + # Correspond to func3 in updateFiles.sh |
| 195 | + for key, val in movdir_dic.items(): |
| 196 | + old_path = "<nanovdb/" + key + ">" |
| 197 | + new_path = "<nanovdb/" + val + ">" |
| 198 | + content = content.replace(old_path, new_path) |
| 199 | + |
| 200 | + write_file(file_path, content) |
| 201 | + |
| 202 | +# Example use: |
| 203 | +# To update all the files using NanoVDB in the current directory (and directories downstream): |
| 204 | +# python ./nanovdb/nanovdb/cmd/updateFiles.py |
| 205 | +# To update all the files using NanoVDB in a directory called foo (and directories downstream): |
| 206 | +# python ./nanovdb/nanovdb/cmd/updateFiles.py -d /path/to/foo |
| 207 | +if __name__ == "__main__": |
| 208 | + parser = argparse.ArgumentParser(description="Synthetic Data Generation for USD") |
| 209 | + parser.add_argument( |
| 210 | + "-d", |
| 211 | + "--directory", |
| 212 | + type=str, |
| 213 | + default=None, |
| 214 | + help="Path to directory containing .h, .cc, and .cu files using NanoVDB.", |
| 215 | + ) |
| 216 | + |
| 217 | + args = parser.parse_args() |
| 218 | + dir_path = os.getcwd() if args.directory is None else Path(args.directory).resolve() |
| 219 | + |
| 220 | + update_files(dir_path) |
0 commit comments