|
| 1 | +# Lint as: python3 |
| 2 | +# |
| 3 | +# Copyright 2019 The dm_control Authors. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +# ============================================================================ |
| 17 | + |
| 18 | +"""Cameras for recording soccer videos.""" |
| 19 | + |
| 20 | +from dm_control.mujoco import engine |
| 21 | +import numpy as np |
| 22 | + |
| 23 | + |
| 24 | +class MultiplayerTrackingCamera(object): |
| 25 | + """Camera that smoothly tracks multiple entities.""" |
| 26 | + |
| 27 | + def __init__( |
| 28 | + self, |
| 29 | + min_distance, |
| 30 | + distance_factor, |
| 31 | + smoothing_update_speed, |
| 32 | + azimuth=90, |
| 33 | + elevation=-45, |
| 34 | + width=1920, |
| 35 | + height=1080, |
| 36 | + ): |
| 37 | + """Construct a new MultiplayerTrackingcamera. |
| 38 | +
|
| 39 | + The target lookat point is the centroid of all tracked entities. |
| 40 | + Target camera distance is set to min_distance + distance_factor * d_max, |
| 41 | + where d_max is the maximum distance of any entity to the lookat point. |
| 42 | +
|
| 43 | + Args: |
| 44 | + min_distance: minimum camera distance. |
| 45 | + distance_factor: camera distance multiplier (see above). |
| 46 | + smoothing_update_speed: exponential filter parameter to smooth camera |
| 47 | + movement. 1 means no filter; smaller values mean less change per step. |
| 48 | + azimuth: constant azimuth to use for camera. |
| 49 | + elevation: constant elevation to use for camera. |
| 50 | + width: width to use for rendered video. |
| 51 | + height: height to use for rendered video. |
| 52 | + """ |
| 53 | + self._min_distance = min_distance |
| 54 | + self._distance_factor = distance_factor |
| 55 | + if smoothing_update_speed < 0 or smoothing_update_speed > 1: |
| 56 | + raise ValueError("Filter speed must be in range [0, 1].") |
| 57 | + self._smoothing_update_speed = smoothing_update_speed |
| 58 | + self._azimuth = azimuth |
| 59 | + self._elevation = elevation |
| 60 | + self._width = width |
| 61 | + self._height = height |
| 62 | + self._camera = None |
| 63 | + |
| 64 | + @property |
| 65 | + def camera(self): |
| 66 | + return self._camera |
| 67 | + |
| 68 | + def render(self): |
| 69 | + """Render the current frame.""" |
| 70 | + if self._camera is None: |
| 71 | + raise ValueError( |
| 72 | + "Camera has not been initialized yet." |
| 73 | + " render can only be called after physics has been compiled." |
| 74 | + ) |
| 75 | + return self._camera.render() |
| 76 | + |
| 77 | + def after_compile(self, physics): |
| 78 | + """Instantiate the camera and ensure rendering buffer is large enough.""" |
| 79 | + buffer_height = max(self._height, physics.model.vis.global_.offheight) |
| 80 | + buffer_width = max(self._height, physics.model.vis.global_.offwidth) |
| 81 | + physics.model.vis.global_.offheight = buffer_height |
| 82 | + physics.model.vis.global_.offwidth = buffer_width |
| 83 | + self._camera = engine.MovableCamera( |
| 84 | + physics, height=self._height, width=self._width) |
| 85 | + |
| 86 | + def _get_target_camera_pose(self, entity_positions): |
| 87 | + """Returns the pose that the camera should be pulled toward. |
| 88 | +
|
| 89 | + Args: |
| 90 | + entity_positions: list of numpy arrays representing current positions of |
| 91 | + the entities to be tracked. |
| 92 | + Returns: mujoco.engine.Pose representing the target camera pose. |
| 93 | + """ |
| 94 | + stacked_positions = np.stack(entity_positions) |
| 95 | + centroid = np.mean(stacked_positions, axis=0) |
| 96 | + radii = np.linalg.norm(stacked_positions - centroid, axis=1) |
| 97 | + assert len(radii) == len(entity_positions) |
| 98 | + camera_distance = self._min_distance + self._distance_factor * np.max(radii) |
| 99 | + return engine.Pose( |
| 100 | + lookat=centroid, |
| 101 | + distance=camera_distance, |
| 102 | + azimuth=self._azimuth, |
| 103 | + elevation=self._elevation, |
| 104 | + ) |
| 105 | + |
| 106 | + def initialize_episode(self, entity_positions): |
| 107 | + """Begin the episode with the camera set to its target pose.""" |
| 108 | + target_pose = self._get_target_camera_pose(entity_positions) |
| 109 | + self._camera.set_pose(*target_pose) |
| 110 | + |
| 111 | + def after_step(self, entity_positions): |
| 112 | + """Move camera toward its target poses.""" |
| 113 | + target_pose = self._get_target_camera_pose(entity_positions) |
| 114 | + cur_pose = self._camera.get_pose() |
| 115 | + smoothing_update_speed = self._smoothing_update_speed |
| 116 | + filtered_pose = [ |
| 117 | + target_val * smoothing_update_speed + \ |
| 118 | + current_val * (1 - smoothing_update_speed) |
| 119 | + for target_val, current_val in zip(target_pose, cur_pose) |
| 120 | + ] |
| 121 | + self._camera.set_pose(*filtered_pose) |
0 commit comments