<jupyter_start><jupyter_text>Get file paths<jupyter_code>msh_file = "../examples/mumax_mesh.vtu"
mag_file = "../examples/mumax_mag.csv"
# scale for the points
scale = 1e9<jupyter_output><empty_output><jupyter_text>Generate raytracing - skip if generated<jupyter_code># get the mesh, scale the points to nm
msh = Mesh.from_file(msh_file, scale=scale)<jupyter_output><empty_output><jupyter_text>Make sure that the projection vector is correct and that the structure is oriented well<jupyter_code># get the projection vector
p = get_projection_vector(90, 0) # direction of xrays
n = [0, 1, 0] # normal to the projection plane
x0 = [-100, 0, 0] # point on the projection plane

# prepare raytracing object
raytr = RayTracing(msh, p, n=n, x0=x0)
struct = raytr.struct
struct_projected = raytr.struct_projected
vis = MeshVisualizer(struct, struct_projected)
vis.set_camera(dist=2e5)
vis.show()<jupyter_output><empty_output><jupyter_text>If raytracing file generated - skip if not<jupyter_code># load raytracing if exists
raytr = np.load("raytracing.npy", allow_pickle=True).item()
struct = raytr.struct
struct_projected = raytr.struct_projected<jupyter_output><empty_output><jupyter_text>Generate and save raytracing<jupyter_code>from trimesh import triangles as triangles_mod
from xmcd_projection.raytracing import *
self = raytr
struct_projected = raytr.struct_projected
triangles_mesh = self.mesh.triangles
tol = 1e-3

triangles_normal = triangles_mod.normals(triangles_mesh)[0]
tree = triangles_mod.bounds_tree(triangles_mesh)

pnew = p[np.newaxis, :]
# ray_origins = points

triangles = self.struct_projected.triangles
a = np.linalg.norm(triangles[:, 1, :] -
                   triangles[:, 2, :], axis=1)[:, None]
b = np.linalg.norm(triangles[:, 2, :] -
                   triangles[:, 0, :], axis=1)[:, None]
c = np.linalg.norm(triangles[:, 1, :] -
                   triangles[:, 0, :], axis=1)[:, None]
ray_origins = (a * triangles[:, 0, :] + b * triangles[:,
                                                      1, :] + c * triangles[:, 2, :]) / (a + b + c)

def ray_piercing_fun(orig): return ray_triangle_id(
    triangles_mesh, orig[np.newaxis, :], pnew, triangles_normal=triangles_normal, tree=tree)

def get_piercings_item(orig):
    tri_id, _, locs = ray_piercing_fun(orig)
    try:
        return get_piercings_frompt_lengths(locs, tri_id // 4)
    except ValueError as e:
        warn(
            str(e) + ' If this happens rarely, it could be a numerical artefact.')
        # running this again, if it crashes a second time, it's not an artefact!
        tri_id, _, locs = ray_piercing_fun(orig + tol)
        return get_piercings_frompt_lengths(locs, tri_id // 4)

for orig in ray_origins:
    get_piercings_item(orig)
piercings_list = [get_piercings_item(orig) for orig in ray_origins]

tri_id, _, locs = ray_piercing_fun(orig)
orig
tri_id
locs[tri_id//4 == 364]
orig
triangles_mesh[1457, :, :]
move = np.random.rand(2)
move
import matplotlib.pyplot as plt
plt.plot(triangles_mesh[1456, :, 0], triangles_mesh[1456, :, 2], color="blue")
plt.plot(triangles_mesh[1456, [0, 2], 0], triangles_mesh[1456, [0, 2], 2], color="blue")
plt.plot(triangles_mesh[1457, :, 0], triangles_mesh[1457, :, 2], color="red")
plt.plot(triangles_mesh[1457, [0, 2], 0], triangles_mesh[1457, [0, 2], 2], color="red")
plt.plot(triangles_mesh[1458, :, 0], triangles_mesh[1458, :, 2], color="green")
plt.plot(triangles_mesh[1458, [0, 2], 0], triangles_mesh[1458, [0, 2], 2], color="green")
move = np.random.rand(2)
plt.scatter(orig[0]+1, orig[2]+1)
triangles_mesh[1456, :, :]
triangles_mesh[1458, :, :]
triangles = struct_projected.triangles
triangles[0,:,:]
a = np.linalg.norm(triangles[:, 1, :] - triangles[:, 2, :], axis=1)[:, None]
b = np.linalg.norm(triangles[:, 2, :] - triangles[:, 0, :], axis=1)[:, None]
c = np.linalg.norm(triangles[:, 1, :] - triangles[:, 0, :], axis=1)[:, None]
triangle_centres = (a*triangles[:, 0, :]+b*triangles[:, 1, :]+c*triangles[:, 2, :])/(a+b+c)
i = 12
import matplotlib.pyplot as plt
plt.plot(triangles[i, :, 2], triangles[i, :, 0])
plt.scatter(triangle_centres[i, 2], triangle_centres[i, 0])
plt.axis('equal')
get_piercings_frompt_lengths(locs, tri_id // 4)
np.max(tri_areas)
self = raytr
points = self.struct_projected.triangles_center
self._piercings = raytracing.get_points_piercings(
    points, self.p, self.mesh.triangles, tol=self.tol)
import xmcd_projection
deep_reload(xmcd_projection)
from xmcd_projection import *
raytr.get_piercings()
# np.save("raytracing.npy", raytr, allow_pickle=True)<jupyter_output>0%|                                                                                         | 0/3188 [00:00<?, ?it/s][A[A[A


  0%|                                                                                 | 1/3188 [00:01<58:38,  1.10s/it][A[A[A


  2%|█▉                                                                              | 78/3188 [00:01<40:04,  1.29it/s][A[A[A


  5%|███▌                                                                           | 145/3188 [00:01<27:28,  1.85it/s][A[A[A


  6%|█████▏                                                                         | 207/3188 [00:01<18:51,  2.63it/s][A[A[A


  9%|███████▍                                                                       | 302/3188 [00:01<12:47,  3.76it/s][A[A[A


 12%|█████████▌                                                                     | 388/3188 [00:01<08:42,  5.36it/s][A[A[A


 15%|████████████▏                                                              [...]<jupyter_text>Get the xmcd  Get magnetisation, fix vertex shufflingNote: sometimes if the mesh file has multiple parts, the paraview export and the original mesh coordinates are not in the same order. I add a function to fix that when necessary<jupyter_code>magnetisation, mag_points = load_mesh_magnetisation(mag_file, scale=scale)
shuffle_file = "shuffle_indx.npy"
try:
    shuffle_indx = np.load(shuffle_file)
except FileNotFoundError:
    print('File not found. Generating shuffle indx')
    shuffle_indx = msh.get_shuffle_indx(mag_points)
    np.save(shuffle_file, shuffle_indx)
magnetisation = magnetisation[shuffle_indx, :]<jupyter_output><empty_output><jupyter_text>Get the colours and XMCD values<jupyter_code>xmcd_value = raytr.get_xmcd(magnetisation)
mag_colors = get_struct_face_mag_color(struct, magnetisation)
azi=90
center_struct = [0, 0, 0]
dist_struct = 1e4
center_peem = [100, -200, 0]
dist_peem = 8e4

vis = MeshVisualizer(struct, struct_projected, projected_xmcd=xmcd_value, struct_colors=mag_colors)
vis.show(azi=azi, center=center_peem, dist=dist_peem)
# Image.fromarray(vis.get_image_np())<jupyter_output><empty_output><jupyter_text>View different parts of the image separately  Both<jupyter_code>vis.update_colors(xmcd_value, mag_colors)
vis.view_both(azi=azi, center=center_peem, dist=dist_peem)
Image.fromarray(vis.get_image_np())<jupyter_output><empty_output><jupyter_text>Projection<jupyter_code>vis.view_projection(azi=azi, center=center_peem, dist=dist_peem)
Image.fromarray(vis.get_image_np())<jupyter_output><empty_output><jupyter_text>Structure<jupyter_code>center_struct = [75, 50, 0]
dist_struct = 1e4
vis.view_struct(azi=azi, center=center_struct, dist=dist_struct)
Image.fromarray(vis.get_image_np())<jupyter_output><empty_output><jupyter_text>Blurred image<jupyter_code>vis.view_projection(azi=azi, center=center_peem, dist=dist_peem)
Image.fromarray((vis.get_blurred_image(desired_background=0.7)*255).astype(np.uint8))<jupyter_output><empty_output><jupyter_text>Saving one render<jupyter_code>vis.view_both(azi=azi, center=center_peem, dist=dist_peem)
vis.save_render('mumax_shadow.png')
vis.view_projection()
blurred = vis.get_blurred_image(desired_background=0.7)
imsave('mumax_shadow_blurred.png', (blurred*255).astype(np.uint8), check_contrast=False)

vis.view_struct(azi=azi, center=center_struct, dist=dist_struct)
vis.save_render('mumax_structure_view.png')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14052902093518868, 'fraction_numerical': 0.038611006644101796, 'mean_word_length': 3.820543806646526, 'pattern_counts': {'":': 0, '<': 55, '<?xml version=': 0, '>': 48, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 27, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '647675', 'license': 'MIT', 'n_tokens_mistral': 3176, 'n_tokens_neox': 2922, 'n_words': 532, 'path': 'testing/mumax_projection.ipynb', 'repo_name': 'Skoricius/xmcd-projection', 'size': '7977'}