File size: 4,946 Bytes
3cb1822
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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
"""
Helper script for taking screenshots from ParaView state files
"""
from paraview.simple import *
import os
import math

def take_screenshots_from_state(state_path, output_dir, prefix="", data_directory=None):
    """
    Load a ParaView state file and take 3 screenshots from different angles
    
    Args:
        state_path (str): Path to the .pvsm state file
        output_dir (str): Directory to save screenshots
        prefix (str): Prefix for screenshot filenames
        data_path (str): Directory of raw data file for state file
    
    Returns:
        list: List of screenshot file paths
    """
    if not os.path.exists(state_path):
        raise FileNotFoundError(f"State file not found: {state_path}")
    
    # Load state file
    if data_directory:
        LoadState(state_path, data_directory=data_directory)
    else:
        LoadState(state_path)
    
    # Create output directory
    os.makedirs(output_dir, exist_ok=True)
    
    # Get the active view
    renderView = GetActiveViewOrCreate('RenderView')
    
    # Reset camera to fit all data
    renderView.ResetCamera()
    
    # Get current camera position for reference
    camera = renderView.GetActiveCamera()
    original_position = camera.GetPosition()
    original_focal_point = camera.GetFocalPoint()
    
    # Calculate distance from focal point to position
    distance = math.sqrt(sum([(original_position[i] - original_focal_point[i])**2 for i in range(3)]))
    
    # Define three different camera angles
    angles = [
        {
            'name': 'front',
            'position': [original_focal_point[0], original_focal_point[1], original_focal_point[2] + distance],
            'up': [0, 1, 0]
        },
        {
            'name': 'side',
            'position': [original_focal_point[0] + distance, original_focal_point[1], original_focal_point[2]],
            'up': [0, 0, 1]
        },
        {
            'name': 'diagonal',
            'position': [original_focal_point[0] + distance*0.7, original_focal_point[1] + distance*0.7, original_focal_point[2] + distance*0.7],
            'up': [0, 0, 1]
        }
    ]
    
    screenshot_paths = []
    
    # Take screenshots from different angles
    for angle in angles:
        # Set camera position
        camera.SetPosition(angle['position'])
        camera.SetFocalPoint(original_focal_point)
        camera.SetViewUp(angle['up'])
        
        # Reset camera to ensure proper framing
        renderView.ResetCamera()
        
        # Render the view
        Render()
        
        # Save screenshot
        filename = f"{prefix}{angle['name']}_view.png" if prefix else f"{angle['name']}_view.png"
        screenshot_path = os.path.join(output_dir, filename)
        SaveScreenshot(screenshot_path, renderView, ImageResolution=[1920, 1080])
        screenshot_paths.append(screenshot_path)
    
    return screenshot_paths

def main():
    import argparse
    parser = argparse.ArgumentParser(description="Take 3 screenshots from ParaView state files.")
    parser.add_argument('--gs_state_path', type=str, help='Path to ground truth state file (.pvsm)')
    parser.add_argument('--gs_img_path', type=str, help='Directory to save ground truth screenshots')
    parser.add_argument('--result_state_path', type=str, help='Path to result state file (.pvsm)')
    parser.add_argument('--result_img_path', type=str, help='Directory to save result screenshots')
    parser.add_argument('--data_directory', type=str, default=None, help='Directory containing raw data files (optional)')
    args = parser.parse_args()

    # Validate argument pairs
    if args.gs_state_path and not args.gs_img_path:
        parser.error('If --gs_state_path is provided, --gs_img_path must also be provided.')
    if args.gs_img_path and not args.gs_state_path:
        parser.error('If --gs_img_path is provided, --gs_state_path must also be provided.')
    if args.result_state_path and not args.result_img_path:
        parser.error('If --result_state_path is provided, --result_img_path must also be provided.')
    if args.result_img_path and not args.result_state_path:
        parser.error('If --result_img_path is provided, --result_state_path must also be provided.')

    if args.gs_state_path and args.gs_img_path:
        os.makedirs(args.gs_img_path, exist_ok=True)
        print(f"Taking screenshots for ground truth: {args.gs_state_path} -> {args.gs_img_path}")
        take_screenshots_from_state(args.gs_state_path, args.gs_img_path, prefix="gs_", data_directory=args.data_directory)

    if args.result_state_path and args.result_img_path:
        os.makedirs(args.result_img_path, exist_ok=True)
        print(f"Taking screenshots for result: {args.result_state_path} -> {args.result_img_path}")
        take_screenshots_from_state(args.result_state_path, args.result_img_path, prefix="result_", data_directory=args.data_directory)

if __name__ == "__main__":
    main()