utils.py file
kevin-thankyou-lin opened this issue · 2 comments
kevin-thankyou-lin commented
Hi,
Did you have the utils.py file somewhere?
camera_positions, camera_quats = utils.generate_new_cameras(self.radius_of_vis_cam,
center_of_movement, lookat_pos, height=0.0, jitter_z=True, jitter_amount=0.08)
It'd be really helpful!
Thanks,
Kevin
YunchuZhang commented
I found the function somewhere else:
def generate_new_cameras(radius, center, lookat_vector, height, jitter_z=False, num_pts=50,
jitter_amount=None):
# generate points on the circle
angle = np.linspace(0, 2*np.pi, num=num_pts) # in radians
# angle = np.asarray([0, np.pi/2., np.pi, 3*np.pi/2.])
xy_pts = circle_pts(radius, angle)
plt.scatter(xy_pts[:, 0], xy_pts[:, 1], c='b')
# plt.show()
# xyz_points
xyz_points = np.c_[xy_pts[:, 0], xy_pts[:, 1], height*np.ones(len(xy_pts))]
xyz_points[:, 0] += center[0]
xyz_points[:, 1] += center[1]
if jitter_z:
if jitter_amount is not None:
xyz_points[:, 2] += (jitter_amount*np.random.normal(size=num_pts))
else:
xyz_points[:, 2] += (0.02*np.random.normal(size=num_pts))
# generate the z-axis for each of these
z_vector = xyz_points - lookat_vector
z_axis = z_vector / np.linalg.norm(z_vector, axis=1).reshape(-1, 1)
# now from this I will also generate the other two axis and quaternion
_, quat = get_quaternion(z_axis, world_up=np.asarray([0., 0., 1.]))
return xyz_points, quat
kevin-thankyou-lin commented
fantastic, thanks!
do you have the get_quaternion function too?
EDIT: found here: https://github.com/YunchuZhang/Visually-Grounded-Library-of-Behaviors/blob/945e3eb925189e6f9d3ccca0f1ac5b00f6207c32/pcp_utils/cameras.py#L8
def get_quaternion(z_axis, world_up):
"""
z_axis = numpy.ndarray(n_pts, 3)
world_up = axis representing the y axis
"""
world_up = np.tile(world_up, len(z_axis)).reshape(len(z_axis), 3)
side_axis = np.cross(world_up, z_axis)
side_axis = side_axis / np.linalg.norm(side_axis, axis=1).reshape(-1, 1)
cam_locs_to_remove = np.where(np.isnan(np.linalg.norm(side_axis, axis=1)))
cam_locs_to_take = np.ones(len(world_up)).astype(np.int)
cam_locs_to_take[cam_locs_to_remove] = 0
world_up = world_up[cam_locs_to_take.astype(np.bool)]
side_axis = side_axis[cam_locs_to_take.astype(np.bool)]
z_axis = z_axis[cam_locs_to_take.astype(np.bool)]
up_axis = np.cross(z_axis, side_axis)
# TODO: find a better way to do this
rot_mat = np.zeros((len(z_axis), 4, 4))
quats = list()
for i in range(len(rot_mat)):
rot_mat[i, :3, 0] = side_axis[i]
rot_mat[i, :3, 1] = up_axis[i]
rot_mat[i, :3, 2] = z_axis[i]
rot_mat[i, 3, 3] = 1
if np.isnan(np.sum(rot_mat)):
print('in the nan of utils while generating quaternions')
from IPython import embed; embed()
rot_mat[i] = sym(rot_mat[i])
quats.append(transformations.quaternion_from_matrix(rot_mat[i]))
return cam_locs_to_take, np.stack(quats)