fix normal

This commit is contained in:
hofee
2024-10-19 18:23:34 +08:00
parent 1f8c017a01
commit 7d25777983
11 changed files with 147 additions and 48 deletions

View File

@@ -4,6 +4,7 @@ import bmesh
from collections import defaultdict
from scipy.spatial.transform import Rotation as R
from utils.pose import PoseUtil
from utils.pts import PtsUtil
import random
class ViewSampleUtil:
@@ -71,7 +72,7 @@ class ViewSampleUtil:
normals.append(normal)
for _ in range(pertube_repeat):
perturb_angle = np.radians(np.random.uniform(0, 30))
perturb_angle = np.radians(np.random.uniform(0, 10))
perturb_axis = np.random.normal(size=3)
perturb_axis /= np.linalg.norm(perturb_axis)
rotation_matrix = R.from_rotvec(perturb_angle * perturb_axis).as_matrix()
@@ -127,12 +128,27 @@ class ViewSampleUtil:
up_vector = np.array([0, 0, 1])
right_vector = np.cross(up_vector, forward_vector)
right_vector /= np.linalg.norm(right_vector)
dot_product = np.dot(forward_vector, up_vector)
angle = np.degrees(np.arccos(dot_product))
print(angle)
if angle < 110:
target_angle = np.random.uniform(110, 150)
angle_difference = np.radians(target_angle - angle)
corrected_up_vector = np.cross(forward_vector, right_vector)
rotation_matrix = np.array([right_vector, corrected_up_vector, forward_vector]).T
rotation_axis = np.cross(forward_vector, up_vector)
rotation_axis /= np.linalg.norm(rotation_axis)
rotation_matrix = PoseUtil.rotation_matrix_from_axis_angle(rotation_axis, angle_difference)
new_cam_position_world = np.dot(rotation_matrix, cam_position_world - look_at_point_world) + look_at_point_world
cam_position_world = new_cam_position_world
forward_vector = cam_position_world - look_at_point_world
forward_vector /= np.linalg.norm(forward_vector)
right_vector = np.cross(up_vector, forward_vector)
right_vector /= np.linalg.norm(right_vector)
corrected_up_vector = np.cross(forward_vector, right_vector)
rotation_matrix = np.array([right_vector, corrected_up_vector, forward_vector]).T
else:
rotation_matrix = np.array([right_vector, up_vector, forward_vector]).T
cam_pose = np.eye(4)
cam_pose[:3, :3] = rotation_matrix
cam_pose[:3, 3] = cam_position_world
@@ -146,16 +162,17 @@ class ViewSampleUtil:
cos_angle = np.dot(direction_vector, horizontal_normal) / (np.linalg.norm(direction_vector) * np.linalg.norm(horizontal_normal))
angle = np.arccos(np.clip(cos_angle, -1.0, 1.0))
angle_degree = np.degrees(angle)
if angle_degree < 90 - min_cam_table_included_degree:
if angle_degree < 90 + min_cam_table_included_degree:
filtered_cam_poses.append(cam_pose)
if random.random() < random_view_ratio:
pertube_pose = PoseUtil.get_uniform_pose([0.1, 0.1, 0.1], [3, 3, 3], 0, 180, "cm")
filtered_cam_poses.append(pertube_pose @ cam_pose)
if len(filtered_cam_poses) > max_views:
indices = np.random.choice(len(filtered_cam_poses), max_views, replace=False)
cam_points = np.array([cam_pose[:3, 3] for cam_pose in filtered_cam_poses])
_, indices = PtsUtil.fps_downsample_point_cloud(cam_points, max_views, require_idx=True)
filtered_cam_poses = [filtered_cam_poses[i] for i in indices]
return np.array(filtered_cam_poses)
@staticmethod