This repository was archived by the owner on Feb 13, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtest.py
129 lines (106 loc) · 5.63 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# SPDX-FileCopyrightText: Copyright (c) 2022-2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: LicenseRef-NvidiaProprietary
#
# NVIDIA CORPORATION, its affiliates and licensors retain all intellectual
# property and proprietary rights in and to this material, related
# documentation and any modifications thereto. Any use, reproduction,
# disclosure or distribution of this material and related documentation
# without an express license agreement from NVIDIA CORPORATION or
# its affiliates is strictly prohibited.
import os, io, sys, subprocess
import pathlib
import argparse
import struct
parser = argparse.ArgumentParser(description='Runs tests for micromesh_toolkit python bindings')
parser.add_argument('--moduleDir', required=True, type=pathlib.Path, help='Directory containing the .so (linux) or .pyd (windows) module')
parser.add_argument('--resultsDir', required=True, type=pathlib.Path, help='Path to write results to')
parser.add_argument('--meshDir', required=True, type=pathlib.Path, help='Asset directory for test scenes')
args = parser.parse_args()
sys.path.insert(0, str(args.moduleDir))
print("Entering " + os.path.dirname(os.path.realpath('__file__')), file=sys.stderr)
subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'numpy'])
import micromesh_python as pymm
import numpy as np
print("micromesh_python module objects:", file=sys.stderr)
for name in dir(pymm):
print(" " + name, file=sys.stderr)
settings = pymm.BakerSettings()
settings.level = 3
settings.maxTraceLength = 0.0
settings.enableCompression = False
settings.minPSNR = 50.0
settings.subdivMethod = pymm.SubdivMethod.Uniform
settings.uniDirectional = True
baseMesh = pymm.Mesh()
baseMesh.vertexPositions = np.array([[0,0,0],[1,0,0],[0,1,0]], dtype=float)
baseMesh.vertexNormals = np.array([[0,0,1], [0,0,1], [0,0,1]], dtype=float)
baseMesh.vertexDirectionBounds = np.array([[0,1], [0,1], [0,1]], dtype=float)
baseMesh.vertexDirections = np.array([[0,0,1], [0,0,1], [0,0,1]], dtype=float)
baseMesh.vertexTexcoords0 = np.array([[0,0],[1,0],[0,1]], dtype=float)
baseMesh.triangleVertices = np.array([[0,1,2]], dtype=np.uint)
baseMeshTransform = np.array([[1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1]], dtype=float)
referenceMesh = pymm.Mesh()
# Same vertices as the baseMesh except the last is raised up (+Z) by 1
referenceMesh.vertexPositions = np.array([[0,0,0],[1,0,0],[0,1,1]], dtype=float)
referenceMesh.vertexNormals = np.array([[0,0,1], [0,0,1], [0,0,1]], dtype=float)
referenceMesh.vertexDirectionBounds = np.array([[0,1], [0,1], [0,1]], dtype=float)
referenceMesh.vertexDirections = np.array([[0,0,1], [0,0,1], [0,0,1]], dtype=float)
referenceMesh.vertexTexcoords0 = np.array([[0,0],[1,0],[0,1]], dtype=float)
referenceMesh.triangleVertices = np.array([[0,1,2]], dtype=np.uint)
referenceMeshTransform = np.array([[1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1]], dtype=float)
meshDir = pathlib.Path(args.meshDir).resolve()
resultsDir = pathlib.Path(args.resultsDir).resolve()
bakeInput = pymm.BakerInput()
bakeInput.settings = settings
bakeInput.baseMesh = baseMesh
bakeInput.baseMeshTransform = baseMeshTransform
bakeInput.referenceMesh = referenceMesh
bakeInput.referenceMeshTransform = referenceMeshTransform
bakeInput.heightmap.filepath = str(meshDir / "cracks" / "height.png")
bakeInput.heightmap.bias = 0.0
bakeInput.heightmap.scale = 1.0
bakeInput.quaternionMapFilepath = str(resultsDir / "quaternionMap.png")
bakeInput.quaternionMapResolution = 32
bakeInput.offsetMapFilepath = str(resultsDir / "offsetMap.png")
bakeInput.offsetMapResolution = 32
textureInputs = ["height.png", "height.png"]
textureOutputs = ["outHeight.png", "outHeight2.png"]
resampledWidth = 256
resampledHeight = 256
resamplerInputs = []
for inputTexture, outputTexture in zip(textureInputs, textureOutputs):
resamplerInput = pymm.ResamplerInput()
resamplerInput.input.filepath = str(meshDir / "cracks" / inputTexture)
resamplerInput.input.type = pymm.TextureType.Generic
resamplerInput.output.filepath = str(resultsDir / outputTexture)
resamplerInput.output.width = resampledWidth
resamplerInput.output.height = resampledHeight
resamplerInputs.append(resamplerInput)
if len(resamplerInputs) > 0:
bakeInput.resamplerInput = resamplerInputs
context = pymm.createContext(verbosity=pymm.Verbosity.Info)
bakeOutput = pymm.bakeMicromesh(context, bakeInput)
print("Bake Finished")
displacementBytes = bakeOutput.values.tobytes()
displacements = struct.unpack('{}f'.format(bakeOutput.valueCount), displacementBytes)
print(displacements)
assert abs(displacements[2] - 1.0) < 0.01, "third vertex should have a displacement of 1.0"
for resamplerInput in resamplerInputs:
assert os.path.exists(resamplerInput.output.filepath), f"resampled texture {resamplerInput.output.filepath} does not exist"
size = os.path.getsize(resamplerInput.output.filepath)
with io.open(resamplerInput.output.filepath, "rb") as file:
data = file.read(26)
# Ensure it's a (modern) PNG file
assert (size >= 24) and data.startswith(b'\211PNG\r\n\032\n') and (data[12:16] == b'IHDR'), f"resampled texture isn't a .png"
# Unpack the resolution from the magic header
w, h = struct.unpack(">LL", data[16:24])
assert (w == resampledWidth) and (h == resampledHeight), f"resampled texture {resamplerInput.output.filepath} size isn't correct"
sys.stdout.flush()
print("Leaving " + os.path.dirname(os.path.realpath('__file__')), file=sys.stderr)
context = None