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75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
import numpy as np
import torch
from PIL import Image
import argparse
import os
import json
from typing import List, Optional, Tuple
import scipy.spatial.transform as spt
from omegaconf import OmegaConf


from modeling.pipeline import VMemPipeline
from utils import load_img_and_K, transform_img_and_K, get_default_intrinsics





class Navigator:
    """
    Navigator class for moving through a 3D scene with a virtual camera.
    Provides methods to move forward and turn left/right, generating new camera poses
    and rendering frames using VMemPipeline.
    """
    def __init__(self, pipeline: VMemPipeline, step_size: float = 0.1, num_interpolation_frames: int = 4):
        """
        Initialize the Navigator.
        
        Args:
            pipeline: The VMemPipeline used for rendering frames
            step_size: The distance to move forward with each step
            num_interpolation_frames: Number of frames to generate for each movement
        """
        self.pipeline = pipeline
        self.step_size = step_size
        self.current_pose = None
        self.current_K = None
        self.frames = []
        self.num_interpolation_frames = num_interpolation_frames
        self.pose_history = []  # Store history of camera poses
        
    def initialize(self, image, initial_pose, initial_K):
        """
        Initialize the navigator with an image and camera parameters.
        Uses the pipeline's initialize method to set up the state.
        
        Args:
            image: Initial image tensor
            initial_pose: Initial camera pose (4x4 camera-to-world matrix)
            initial_K: Initial camera intrinsics matrix
            
        Returns:
            The initial frame as PIL Image
        """
        self.current_pose = initial_pose
        self.current_K = initial_K
        
        # Use the pipeline's initialize method
        initial_frame = self.pipeline.initialize(image, initial_pose, initial_K)
        self.frames = [initial_frame]
        
        # Save the initial pose
        self.pose_history.append({
            "file_path": f"images/frame_001.png",
            "transform_matrix": initial_pose.tolist() if isinstance(initial_pose, np.ndarray) else initial_pose
        })
        
        return initial_frame
        
    def initialize_pipeline(self, image_tensor, initial_pose, initial_K):
        """
        Initialize the pipeline with the first image and camera pose.
        Deprecated: Use initialize() instead.
        
        Args:
            image_tensor: Initial image tensor [1, C, H, W]
            initial_pose: Initial camera pose (4x4 camera-to-world matrix)
            initial_K: Initial camera intrinsics matrix
            
        Returns:
            The generated frame as PIL Image
        """
        return self.initialize(image_tensor, initial_pose, initial_K)
        
    def initialize_with_image(self, image, initial_pose, initial_K):
        """
        Initialize the navigator with an image and camera parameters.
        Deprecated: Use initialize() instead.
        
        Args:
            image: Initial image tensor
            initial_pose: Initial camera pose (4x4 camera-to-world matrix)
            initial_K: Initial camera intrinsics matrix
            
        Returns:
            The generated frame as PIL Image
        """
        return self.initialize(image, initial_pose, initial_K)
    
    def _interpolate_poses(self, start_pose, end_pose, num_frames):
        """
        Interpolate between two camera poses.
        
        Args:
            start_pose: Starting camera pose (4x4 matrix)
            end_pose: Ending camera pose (4x4 matrix)
            num_frames: Number of interpolation frames to generate (including end pose)
            
        Returns:
            List of interpolated camera poses
        """
        # Extract rotation matrices
        start_R = start_pose[:3, :3]
        end_R = end_pose[:3, :3]
        
        # Extract translation vectors
        start_t = start_pose[:3, 3]
        end_t = end_pose[:3, 3]
        
        # Convert rotation matrices to quaternions for smooth interpolation
        start_quat = spt.Rotation.from_matrix(start_R).as_quat()
        end_quat = spt.Rotation.from_matrix(end_R).as_quat()
        
        # Generate interpolated poses
        interpolated_poses = []
        for i in range(num_frames):
            # Interpolation factor (0 to 1)
            t = (i + 1) / num_frames
            
            # Interpolate translation
            interp_t = (1 - t) * start_t + t * end_t
            
            # Interpolate rotation (SLERP)
            interp_quat = spt.Slerp(
                np.array([0, 1]), 
                spt.Rotation.from_quat([start_quat, end_quat])
            )(t).as_matrix()
            
            # Create interpolated pose matrix
            interp_pose = np.eye(4)
            interp_pose[:3, :3] = interp_quat
            interp_pose[:3, 3] = interp_t
            
            interpolated_poses.append(interp_pose)
            
        return interpolated_poses
        
    def move_backward(self, num_steps: int = 1) -> List[Image.Image]:
        """
        Move the camera backward along its viewing direction with smooth interpolation.
        
        Args:
            num_steps: Number of steps to move forward
            
        Returns:
            List of generated frames as PIL Images
        """
        if self.current_pose is None:
            print("Navigator not initialized. Call initialize first.")
            return None
        
        # Get the current forward direction from the camera pose
        forward_dir = self.current_pose[:3, 2]
        
        # Create the target pose
        target_pose = self.current_pose.copy()
        target_pose[:3, 3] += forward_dir * self.step_size * num_steps
        
        # Interpolate between current pose and target pose
        interpolated_poses = self._interpolate_poses(
            self.current_pose, 
            target_pose, 
            self.num_interpolation_frames
        )
        
        # Create list of intrinsics (same for all frames)
        interpolated_Ks = [self.current_K] * len(interpolated_poses)
        
        # Generate frames for interpolated poses
        new_frames = self.pipeline.generate_trajectory_frames(interpolated_poses, 
                                                              interpolated_Ks,
                                                              use_non_maximum_suppression=False)
        
        # Update the current pose to the final pose
        self.current_pose = interpolated_poses[-1]
        self.frames.extend(new_frames)
        
        # Save the final pose
        self.pose_history.append({
            "file_path": f"images/frame_{len(self.pose_history) + 1:03d}.png",
            "transform_matrix": self.current_pose.tolist() if isinstance(self.current_pose, np.ndarray) else self.current_pose
        })
        
        return new_frames
    
        
    def move_forward(self, num_steps: int = 1) -> List[Image.Image]:
        """
        Move the camera forward along its viewing direction with smooth interpolation.
        
        Args:
            num_steps: Number of steps to move forward
            
        Returns:
            List of generated frames as PIL Images
        """
        if self.current_pose is None:
            print("Navigator not initialized. Call initialize first.")
            return None
        
        # Get the current forward direction from the camera pose
        forward_dir = self.current_pose[:3, 2]
        
        # Create the target pose
        target_pose = self.current_pose.copy()
        target_pose[:3, 3] -= forward_dir * self.step_size * num_steps
        
        # Interpolate between current pose and target pose
        interpolated_poses = self._interpolate_poses(
            self.current_pose, 
            target_pose, 
            self.num_interpolation_frames
        )
        
        # Create list of intrinsics (same for all frames)
        interpolated_Ks = [self.current_K] * len(interpolated_poses)
        
        # Generate frames for interpolated poses
        new_frames = self.pipeline.generate_trajectory_frames(interpolated_poses, 
                                                              interpolated_Ks,
                                                              use_non_maximum_suppression=False)
        
        # Update the current pose to the final pose
        self.current_pose = interpolated_poses[-1]
        self.frames.extend(new_frames)
        
        # Save the final pose
        self.pose_history.append({
            "file_path": f"images/frame_{len(self.pose_history) + 1:03d}.png",
            "transform_matrix": self.current_pose.tolist() if isinstance(self.current_pose, np.ndarray) else self.current_pose
        })
        
        return new_frames
    
    def turn_left(self, degrees: float = 3) -> List[Image.Image]:
        """
        Rotate the camera left around the up vector with smooth interpolation.
        
        Args:
            degrees: Rotation angle in degrees
            
        Returns:
            List of generated frames as PIL Images
        """
        return self._turn(degrees)
    
    def turn_right(self, degrees: float = 3) -> List[Image.Image]:
        """
        Rotate the camera right around the up vector with smooth interpolation.
        
        Args:
            degrees: Rotation angle in degrees
            
        Returns:
            List of generated frames as PIL Images
        """
        return self._turn(-degrees)
    
    def _turn(self, degrees: float) -> List[Image.Image]:
        """
        Helper method to turn the camera by the specified angle with smooth interpolation.
        Positive angles turn left, negative angles turn right.
        
        Args:
            degrees: Rotation angle in degrees
            
        Returns:
            List of generated frames as PIL Images
        """
        if self.current_pose is None:
            print("Navigator not initialized. Call initialize first.")
            return None
        
        # Convert degrees to radians
        angle_rad = np.radians(degrees)
        
        # Create rotation matrix around the up axis (assuming Y is up)
        rotation = np.array([
            [np.cos(angle_rad), 0, np.sin(angle_rad), 0],
            [0, 1, 0, 0],
            [-np.sin(angle_rad), 0, np.cos(angle_rad), 0],
            [0, 0, 0, 1]
        ])
        
        # Apply rotation to the current pose
        position = self.current_pose[:3, 3].copy()
        rotation_matrix = self.current_pose[:3, :3].copy()
        
        # Create the target pose
        target_pose = np.eye(4)
        target_pose[:3, :3] = rotation[:3, :3] @ rotation_matrix
        target_pose[:3, 3] = position
        
        # Interpolate between current pose and target pose
        interpolated_poses = self._interpolate_poses(
            self.current_pose, 
            target_pose, 
            self.num_interpolation_frames
        )
        
        # Create list of intrinsics (same for all frames)
    
        interpolated_Ks = [self.current_K] * len(interpolated_poses)
        
        # Generate frames for interpolated poses
        new_frames = self.pipeline.generate_trajectory_frames(interpolated_poses, interpolated_Ks)
        
        # Update the current pose to the final pose
        self.current_pose = interpolated_poses[-1]
        self.frames.extend(new_frames)
        
        # Save the final pose
        self.pose_history.append({
            "file_path": f"images/frame_{len(self.pose_history) + 1:03d}.png",
            "transform_matrix": self.current_pose.tolist() if isinstance(self.current_pose, np.ndarray) else self.current_pose
        })
        
        return new_frames
    
 
    
    def navigate(self, commands: List[str]) -> List[List[Image.Image]]:
        """
        Execute a series of navigation commands and return the generated frames.
        
        Args:
            commands: List of commands ('w', 'a', 'd', 'q')
            
        Returns:
            List of lists of generated frames, one list per command
        """
        if self.current_pose is None:
            print("Navigator not initialized. Call initialize first.")
            return []
        
        all_generated_frames = []
        
        for idx, cmd in enumerate(commands):
        
            if cmd == 'w':
                frames = self.move_forward()
                if frames:
                    all_generated_frames.extend(frames)
            elif cmd == 's':
                # self.pipeline.temporal_only = True
                frames = self.move_backward()
                if frames:
                    all_generated_frames.extend(frames)
            elif cmd == 'a':
                frames = self.turn_left(4)
                if frames:
                    all_generated_frames.extend(frames)
            elif cmd == 'd':
                frames = self.turn_right(4)
                if frames:
                    all_generated_frames.extend(frames)


        
        return all_generated_frames

    def undo(self) -> bool:
        """
        Undo the last navigation step by removing the most recent frames and poses.
        Uses the pipeline's undo_latest_move method to handle the frame removal.
        
        Returns:
            bool: True if undo was successful, False otherwise
        """
        # Check if we have enough poses to undo
        if len(self.pose_history) <= 1:
            print("Cannot undo: at initial position")
            return False
            
        # Use pipeline's undo function to remove the last batch of frames
        success = self.pipeline.undo_latest_move()
        
        if success:
            # Remove the last pose from history
            self.pose_history.pop()
            
            # Set current pose to the previous pose
            prev_pose_data = self.pose_history[-1]
            self.current_pose = np.array(prev_pose_data["transform_matrix"])
            
            # Remove frames from the frames list
            frames_to_remove = min(self.pipeline.config.model.target_num_frames, len(self.frames) - 1)
            for _ in range(frames_to_remove):
                if len(self.frames) > 1:  # Keep at least the initial frame
                    self.frames.pop()
            
            print(f"Successfully undid last movement. Now at position {len(self.pose_history)}")
            return True
        
        return False

    def save_camera_poses(self, output_path):
        """
        Save the camera pose history to a JSON file in the format
        required for NeRF training.
        
        Args:
            output_path: Path to save the JSON file
        """
        # Create the output directory if it doesn't exist
        os.makedirs(os.path.dirname(output_path), exist_ok=True)
        
        # Format the data as required
        transforms_data = {
            "frames": self.pose_history
        }
        
        # Save to JSON file
        with open(output_path, 'w') as f:
            json.dump(transforms_data, f, indent=4)
        
        print(f"Camera poses saved to {output_path}")