|
5 | 5 |
|
6 | 6 |
|
7 | 7 | def make_basic_shapes(): |
8 | | - gmeshes = [] |
9 | | - |
10 | | - # cube is the root, shifted -1 in x and rotated a bit |
11 | | - cube_mesh = pv.Cube( |
12 | | - x_length=2.0, |
13 | | - y_length=1.0, |
14 | | - z_length=4.0, |
15 | | - ) |
16 | | - |
17 | | - # add material |
18 | | - cube_gm = GMesh( |
19 | | - name="cube", |
20 | | - mesh=cube_mesh, |
21 | | - material="G4_AIR", |
22 | | - color="lightblue", |
23 | | - opacity=0.4, |
24 | | - position=(-1.0, 0.0, 0.0), # translation in world frame |
25 | | - rotation=(0.0, 0.0, 10.0), # rotate cube 30° around Z |
26 | | - ) |
27 | | - gmeshes.append(cube_gm) |
28 | | - |
29 | | - # cylinder is defined at its own local origin (0,0,0), unrotated locally |
30 | | - cyl_mesh = pv.Cylinder( |
31 | | - radius=0.5, |
32 | | - height=1.0, |
33 | | - resolution=64, |
34 | | - ) |
35 | | - |
36 | | - cyl_gm = GMesh( |
37 | | - name="cylinder", |
38 | | - mesh=cyl_mesh, |
39 | | - mother="cube", # inside |
40 | | - material="G4_WATER", |
41 | | - color="lightgreen", |
42 | | - position=(0.0, 0.2, 0.0), # 1 unit along mother's +Y |
43 | | - rotation=(0.0, 10.0, 0.0), # rotate 10° around mother's Y |
44 | | - ) |
45 | | - gmeshes.append(cyl_gm) |
46 | | - |
47 | | - return gmeshes |
| 8 | + gmeshes = [] |
| 9 | + |
| 10 | + # --- Box: world-like container, slightly transparent --- |
| 11 | + box_mesh = pv.Cube(x_length=4.0, y_length=4.0, z_length=6.0) |
| 12 | + box_gm = GMesh( |
| 13 | + name="box", |
| 14 | + mesh=box_mesh, |
| 15 | + material="G4_AIR", |
| 16 | + color="ghostwhite", |
| 17 | + opacity=0.15, |
| 18 | + position=(0.0, 0.0, 0.0), |
| 19 | + rotation=(0.0, 0.0, 0.0), |
| 20 | + ) |
| 21 | + gmeshes.append(box_gm) |
| 22 | + |
| 23 | + # --- Cylinder: solid, inside the box --- |
| 24 | + cyl_mesh = pv.Cylinder(radius=0.6, height=2.0, resolution=64, direction=(0, 0, 1)) |
| 25 | + cyl_gm = GMesh( |
| 26 | + name="cylinder", |
| 27 | + mesh=cyl_mesh, |
| 28 | + mother="box", |
| 29 | + material="G4_WATER", |
| 30 | + color="steelblue", |
| 31 | + opacity=1.0, |
| 32 | + position=(0.0, 0.0, -1.5), |
| 33 | + rotation=(0.0, 0.0, 0.0), |
| 34 | + ) |
| 35 | + gmeshes.append(cyl_gm) |
| 36 | + |
| 37 | + # --- Sphere: inside the box, demonstrates G4Sphere --- |
| 38 | + sphere_mesh = pv.Sphere(radius=0.7, theta_resolution=32, phi_resolution=32) |
| 39 | + sphere_gm = GMesh( |
| 40 | + name="sphere", |
| 41 | + mesh=sphere_mesh, |
| 42 | + mother="box", |
| 43 | + material="G4_Pb", |
| 44 | + color="tomato", |
| 45 | + opacity=0.85, |
| 46 | + position=(0.0, 0.0, 1.2), |
| 47 | + rotation=(0.0, 0.0, 0.0), |
| 48 | + ) |
| 49 | + gmeshes.append(sphere_gm) |
| 50 | + |
| 51 | + # --- Small rotated box: demonstrates rotation parameter --- |
| 52 | + rotbox_mesh = pv.Cube(x_length=1.2, y_length=0.4, z_length=1.2) |
| 53 | + rotbox_gm = GMesh( |
| 54 | + name="rotated_box", |
| 55 | + mesh=rotbox_mesh, |
| 56 | + mother="box", |
| 57 | + material="G4_Al", |
| 58 | + color="gold", |
| 59 | + opacity=0.9, |
| 60 | + position=(0.8, 0.8, 0.0), |
| 61 | + rotation=(0.0, 0.0, 35.0), # 35 degrees around Z |
| 62 | + ) |
| 63 | + gmeshes.append(rotbox_gm) |
| 64 | + |
| 65 | + return gmeshes |
48 | 66 |
|
49 | 67 |
|
50 | 68 | if __name__ == "__main__": |
| 69 | + gmeshes = make_basic_shapes() |
| 70 | + pvmeshes = pvmeshes_from_gmeshes(gmeshes) |
51 | 71 |
|
52 | | - gmeshes = make_basic_shapes() |
53 | | - pvmeshes = pvmeshes_from_gmeshes(gmeshes) |
54 | | - |
55 | | - p = pv.Plotter() |
56 | | - |
57 | | - # add world-space meshes |
58 | | - for mesh, color, opacity in pvmeshes: |
59 | | - p.add_mesh(mesh, |
60 | | - color=color, |
61 | | - opacity=opacity, |
62 | | - show_edges=True) |
63 | | - |
64 | | - # visual reference axes at world origin |
65 | | - p.add_axes_at_origin(xlabel="X", ylabel="Y", zlabel="Z") |
66 | | - p.show_axes() |
67 | | - |
68 | | - set_yz_view_x_into_screen(p, distance=10.0) |
| 72 | + p = pv.Plotter() |
69 | 73 |
|
70 | | - p.show() |
| 74 | + for mesh, color, opacity in pvmeshes: |
| 75 | + p.add_mesh(mesh, color=color, opacity=opacity, show_edges=True) |
71 | 76 |
|
| 77 | + p.add_axes_at_origin(xlabel="X", ylabel="Y", zlabel="Z") |
| 78 | + p.show_axes() |
| 79 | + set_yz_view_x_into_screen(p, distance=10.0) |
| 80 | + p.show() |
0 commit comments