简体   繁体   中英

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. 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?

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 :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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