简体   繁体   English

如何在保留纵横比的同时重新缩放顶点以适应特定范围?

[英]How to re-scale vertices to fit within certain range while preserving aspect ratio?

I'm trying to normalize the vertices of a mesh to fit within a bounding box that ranges from -0.5 to +0.5.我正在尝试标准化网格的顶点以适应范围从 -0.5 到 +0.5 的边界框。 Using the following logic, I've accomplished that:使用以下逻辑,我已经完成了:

# Calculate max and min values for each axis to
# get existing bounding box
x_max = np.max(vertices[:, 0])
y_max = np.max(vertices[:, 1])
z_max = np.max(vertices[:, 2])

x_min = np.min(vertices[:, 0])
y_min = np.min(vertices[:, 1])
z_min = np.min(vertices[:, 2])

# Calculate normalized vertices
normalized_x = 1 * (vertices[:, 0] - x_min) / (x_max - x_min) - 0.5
normalized_y = 1 * (vertices[:, 1] - y_min) / (y_max - y_min) - 0.5
normalized_z = 1 * (vertices[:, 2] - z_min) / (z_max - z_min) - 0.5

normalized_vertices = np.column_stack((normalized_x, normalized_y, normalized_z))

However, my problem is that while my mesh vertices are now in the proper range, the aspect ratio isn't preserved (so like the longer dimension is completely squashed).然而,我的问题是,虽然我的网格顶点现在在适当的范围内,但纵横比没有保留(所以更长的维度被完全压扁了)。 How would I scale this properly so that my points still range within the max of -0.5 to +0.5, but the original aspect ratio is preserved?我将如何正确缩放,以便我的点仍然在 -0.5 到 +0.5 的最大值范围内,但保留原始纵横比?

Use the greatest value of:使用最大值:

(x_max - x_min)
(y_max - y_min)
(z_max - z_min)

and divide by this value, let say it is max_value :并除以这个值,假设它是max_value

normalized_x = 1 * (vertices[:, 0] - x_min) / max_value - 0.5
normalized_y = 1 * (vertices[:, 1] - y_min) / max_value - 0.5
normalized_z = 1 * (vertices[:, 2] - z_min) / max_value - 0.5

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM