-
Notifications
You must be signed in to change notification settings - Fork 3
/
hf_optimize_3d.py
168 lines (157 loc) · 6.37 KB
/
hf_optimize_3d.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import hou
import math
import numpy as np
import numpy.random as random
node = hou.pwd()
geo = node.geometry()
boundary_node = node.inputs()[1]
boundaries = boundary_node.geometry().pointGroups()
edge_boundaries = boundary_node.geometry().edgeGroups()
uv_node = hou.node(hou.parent().path() + "/uv_viewer")
cameras_info = hou.session.cameras_info
alpha = hou.session.find_parm(hou.parent(), "alpha")
is_alpha = bool(hou.session.find_parm(hou.parent(), "isAlpha"))
if is_alpha:
print("Current Alpha: " + str(alpha))
'''
4. Optimize Camera View Proportions by:
- Ensuring equitable locality knowledge in vertical and horizontal axes
'''
pix = geo.findPointAttrib("pix")
path_name = hou.hipFile.name().split(".")[0]
if (pix):
resolutions_x = []
resolutions_y = []
for i in range(1, len(boundaries)):
boundary = boundaries[i]
edge_boundary = edge_boundaries[i]
points = boundary.points()
edges = edge_boundary.edges()
min_x = float('inf')
max_x = 0
min_y = float('inf')
max_y = 0
pix_pos = []
for point in points:
pix_attrib = point.attribValue(pix)
curr_x = pix_attrib[(i-1) * 3]
curr_y = pix_attrib[(i-1) * 3 + 1]
curr_z = pix_attrib[(i-1) * 3 + 2]
pix_pos.append((point.number(), (curr_x, curr_y, curr_z)))
min_x = min(min_x, curr_x)
max_x = max(max_x, curr_x)
min_y = min(min_y, curr_y)
max_y = max(max_y, curr_y)
pix_pos = dict(pix_pos)
x_prop_res = max_x - min_x
y_prop_res = max_y - min_y
'''
5. Optimize Camera Resolution by:
- Maximizes retainment of 3D information in 2D
- Minimizes rendering time
We approx S Salamanca,P Merchan,A Adan,E Perez,C Cerrada[2008],
resolution = n * m
where
n = number of width pixels = x_prop_res/q
m = number of height pixels = y_prop_res/q
q = alpha * d
where
d = mesh edge average length (Take this locally)
alpha = 0.87, predetermined by finding Optimal Occupation Ratio,
optimal(Oi) = max(Oi) = max(SUMi_1,n SUMj_1,m Vij / n*m)
where
| 1 iff pixel pij taken up optimally
Vij = | 0 otherwise
'''
d = 0
for edge in edges:
edge_points = edge.points()
edge_pix_0 = pix_pos.get(edge_points[0].number())
edge_pix_1 = pix_pos.get(edge_points[1].number())
d += math.sqrt(math.pow(edge_pix_1[0] - edge_pix_0[0], 2) + math.pow(edge_pix_1[1] - edge_pix_0[1], 2))
d /= len(edges)
q = alpha * d
x_true_res = x_prop_res / q
y_true_res = y_prop_res / q
# NOTE: Houdini Non-Commercial limited to 1280 x 720, so scale res down
downscale = 1
if (x_true_res > 1280 or y_true_res > 720):
x_downscale = x_true_res / 1280
y_downscale = y_true_res / 720
downscale = max(x_downscale, y_downscale)
x_true_res = math.ceil(x_true_res / downscale)
y_true_res = math.ceil(y_true_res / downscale)
camera = hou.node('/obj/oz_camera_' + str(i))
camera.parm('resx').set(x_true_res)
camera.parm('resy').set(y_true_res)
resolutions_x.append(x_true_res)
resolutions_y.append(y_true_res)
'''
6. Camera is zoomed to minimum zoom where all boundary points are visible.
While not all boundary points are viewable, We zoom the camera
to a value within the zoom range (converging on ideal values),
re-unwrap and check again
'''
uv_node.parm("campath").set(camera.path())
boundary_center = cameras_info["centers"][i-1]
rotation_x = cameras_info["rotationsx"][i-1]
rotation_y = cameras_info["rotationsy"][i-1]
plane_normal = cameras_info["normals"][i-1]
zoom_out = cameras_info["zooms"][i-1]
zoom_range_max = zoom_out
best_zoom_out = zoom_out
best_visible_points = 0
max_visible_points = len(points)
tries = 0
sample_size = 10
max_tries = 10
better_zoom_findable = True
while ((best_visible_points != max_visible_points or better_zoom_findable) and tries < max_tries):
visible_points_samples = []
zoom_out_samples = []
for sample in range(sample_size):
if sample == 0:
zoom_out = best_zoom_out
else:
zoom_out = random.uniform(0, zoom_range_max)
visible_points = 0
camera_normal = plane_normal * zoom_out
new_translation = hou.Matrix4((1, 0, 0, boundary_center[0] + camera_normal[0],
0, 1, 0, boundary_center[1] + camera_normal[1],
0, 0, 1, boundary_center[2] + camera_normal[2],
0, 0, 0, 1)).transposed()
hou.session.reset_camera(camera)
camera.setWorldTransform(rotation_x * rotation_y * new_translation)
for point in points:
uv_coord = point.attribValue("uv")
if (uv_coord[0] >= 0 and uv_coord[0] <= 1 and uv_coord[1] >= 0 and uv_coord[1] <= 1 and not all(v == 0 for v in uv_coord)):
visible_points+= 1
visible_points_samples.append(visible_points)
zoom_out_samples.append(zoom_out)
best_visible_points = max(visible_points_samples)
viable_zoom_outs = []
for sample in range(sample_size):
if (visible_points_samples[sample] == best_visible_points):
viable_zoom_outs.append(zoom_out_samples[sample])
best_zoom_out = min(viable_zoom_outs)
if (best_visible_points == max_visible_points):
if (zoom_range_max == best_zoom_out):
better_zoom_findable = False
zoom_range_max = best_zoom_out
tries += 1
if (best_visible_points != max_visible_points):
print("WARNING: not all points could be fit to camera " + str(i))
camera_normal = plane_normal * best_zoom_out
new_translation = hou.Matrix4((1, 0, 0, boundary_center[0] + camera_normal[0],
0, 1, 0, boundary_center[1] + camera_normal[1],
0, 0, 1, boundary_center[2] + camera_normal[2],
0, 0, 0, 1)).transposed()
hou.session.reset_camera(camera)
camera.setWorldTransform(rotation_x * rotation_y * new_translation)
if (not(geo.findGlobalAttrib("resolutionsx_new") or geo.findGlobalAttrib("resolutionsy_new"))):
geo.addAttrib(hou.attribType.Global, "resolutionsx_new", resolutions_x)
geo.addAttrib(hou.attribType.Global, "resolutionsy_new", resolutions_y)
else:
geo.setGlobalAttribValue("resolutionsx_new", resolutions_x)
geo.setGlobalAttribValue("resolutionsy_new", resolutions_y)
node.bypass(True)