1
+ from pathlib import Path
2
+ import numpy as np
3
+ import matplotlib .pyplot as plt
4
+ import cv2
5
+
6
+ # Define cone parameters
7
+ height = 2.5e-1
8
+ radius = np .tan (np .pi / 6 ) * height
9
+ num_points = 30 # Resolution of the cone
10
+
11
+ # mesh grid in polar coordinates
12
+ theta = np .linspace (0 , 2 * np .pi , num_points )
13
+ z_cone = np .linspace (0 , - height , num_points )
14
+ theta , z_cone = np .meshgrid (theta , z_cone )
15
+
16
+ # Convert to Cartesian coordinates
17
+ r = radius * z_cone / height # Linear increase of radius
18
+ x_cone = r * np .cos (theta )
19
+ y_cone = r * np .sin (theta )
20
+
21
+ # Create a VideoWriter object
22
+ video_dir = Path ("./video" )
23
+ video_dir .mkdir (exist_ok = True )
24
+
25
+ video_filename = video_dir .joinpath ("video.mp4" )
26
+ frame_size = (640 , 480 )
27
+ fps = 2
28
+ fourcc = cv2 .VideoWriter_fourcc (* "mp4v" )
29
+ out = cv2 .VideoWriter (str (video_filename ), fourcc , fps , frame_size )
30
+
31
+ for i , file in enumerate (sorted (Path ("." ).glob ("out/*.csv" ))):
32
+ data = np .loadtxt (file , float , delimiter = "," , skiprows = 1 )
33
+ x = data [:, 0 ]
34
+ y = data [:, 1 ]
35
+ z = data [:, 2 ]
36
+
37
+ fig = plt .figure (figsize = (6 , 6 ))
38
+ ax = fig .add_subplot (projection = "3d" )
39
+ fig .patch .set_alpha (0 )
40
+
41
+ ax .plot_surface (x_cone , y_cone , z_cone , alpha = 0.3 )
42
+
43
+ # Axis
44
+ ax .plot (
45
+ [0 , radius * 1.1 ], [0 , 0 ], [0 , 0 ], "b" , linewidth = 2 , label = "X-axis"
46
+ ) # X-axis
47
+ ax .plot (
48
+ [0 , 0 ], [0 , radius * 1.1 ], [0 , 0 ], "g" , linewidth = 2 , label = "Y-axis"
49
+ ) # Y-axis
50
+ ax .plot (
51
+ [0 , 0 ], [0 , 0 ], [0 , - height / 2 ], "r" , linewidth = 2 , label = "Z-axis"
52
+ ) # Z-axis
53
+
54
+ ax .set_xlabel ("X-axis" )
55
+ ax .set_ylabel ("Y-axis" )
56
+ ax .set_zlabel ("Z-axis" )
57
+
58
+ ax .scatter (x , y , z , c = "b" , marker = "o" )
59
+ ax .set_xlim ([- radius * 1.2 , radius * 1.2 ])
60
+ ax .set_ylim ([- radius * 1.2 , radius * 1.2 ])
61
+ ax .set_zlim ([- height * 1.2 , 0 ])
62
+
63
+ image_path = video_dir .joinpath (f"frame_{ i } .png" )
64
+ plt .savefig (image_path )
65
+ plt .close ()
66
+
67
+ frame = cv2 .imread (image_path ) # Read the frame/image
68
+ frame_resized = cv2 .resize (
69
+ frame , frame_size
70
+ ) # Resize to match the video resolution
71
+ out .write (frame_resized ) # Write the frame to the video
72
+
73
+ out .release ()
74
+ #cv2.destroyAllWindows()
0 commit comments