# Give the prism a high importance to ensure adequate sampling prism.material.importance = 9 rgb = RGBPipeline2D() rgb.display_sensitivity = 2.0 sampler = RGBAdaptiveSampler2D(rgb, min_samples=500) # create and setup the camera camera = PinholeCamera((1920, 1080), fov=45, parent=world, pipelines=[rgb], frame_sampler=sampler) camera.transform = translate(0, 0.075, -0.05) * rotate( 180, -45, 0) * translate(0, 0, -0.75) camera.ray_importance_sampling = True camera.ray_important_path_weight = 0.75 camera.ray_max_depth = 500 camera.ray_extinction_prob = 0.01 camera.spectral_bins = 32 camera.spectral_rays = 32 camera.pixel_samples = 250 # start ray tracing plt.ion() for p in range(0, 1000): print("Rendering pass {}".format(p + 1)) camera.observe()
transform=rotate(-35.5, 0, 0) * translate(0.10, 0, 0) * rotate(90, 0, 0)) # background light source top_light = Sphere(0.5, parent=world, transform=translate(0, 2, -1), material=UniformSurfaceEmitter(d65_white, scale=2)) # Give the prism a high importance to ensure adequate sampling prism.material.importance = 9 rgb = RGBPipeline2D() # create and setup the camera camera = PinholeCamera((512, 256), fov=45, parent=world, pipelines=[rgb]) camera.transform = translate(0, 0.05, -0.05) * rotate(180, -65, 0) * translate(0, 0, -0.75) camera.ray_importance_sampling = True camera.ray_important_path_weight = 0.75 camera.ray_max_depth = 500 camera.ray_extinction_prob = 0.01 camera.spectral_bins = 32 camera.spectral_rays = 32 camera.pixel_samples = 100 # start ray tracing plt.ion() for p in range(0, 1000): print("Rendering pass {}".format(p+1)) camera.observe() rgb.save("prisms_{}.png".format(p+1))
plt.colorbar() plt.axis('equal') plt.xlabel('r axis') plt.ylabel('z axis') plt.title("Neutral Density profile in r-z plane") plt.figure() xrange = np.linspace(0, 4, 200) yrange = np.linspace(-2, 2, 200) d_alpha_rz_intensity = np.zeros((200, 200)) direction = Vector3D(0, 1, 0) for i, x in enumerate(xrange): for j, y in enumerate(yrange): emission = d_alpha_excit.emission(Point3D(x, 0.0, y), direction, Spectrum(650, 660, 1)) d_alpha_rz_intensity[j, i] = emission.total() plt.imshow(d_alpha_rz_intensity, extent=[0, 4, -2, 2], origin='lower') plt.colorbar() plt.xlabel('r axis') plt.ylabel('z axis') plt.title("D-alpha emission in R-Z") camera = PinholeCamera((256, 256), pipelines=[PowerPipeline2D()], parent=world) camera.transform = translate(2.5, -4.5, 0)*rotate_basis(Vector3D(0, 1, 0), Vector3D(0, 0, 1)) camera.pixel_samples = 1 plt.ion() camera.observe() plt.ioff() plt.show()
def make_cherab_image(self): """ run cherab to generate the synthetic spectral cube :return: """ if self.radiance is not NotImplemented: self.radiance.close() if self.spectral_radiance is not NotImplemented: self.spectral_radiance.close() import_mastu_mesh(self.world, ) # first, define camera, calculate view vectors and calculate ray lengths pipeline_spectral = SpectralPowerPipeline2D() pipeline_spectral_rad = SpectralRadiancePipeline2D() pipelines = [pipeline_spectral, pipeline_spectral_rad, ] camera = PinholeCamera(self.sensor_format_ds, fov=self.fov, pipelines=pipelines, parent=self.world) # orient and position the camera init_view_vector, init_up_vector = Vector3D(0, 0, 1), Vector3D(0, 1, 0) axle_1 = init_view_vector.cross(self.view_vector) angle = init_view_vector.angle(self.view_vector) t_1 = rotate_vector(angle, axle_1) final_up_vector = rotate_vector(-90, axle_1) * self.view_vector intermediate_up_vector = t_1 * init_up_vector angle_between = intermediate_up_vector.angle(final_up_vector) t_2 = rotate_vector(-angle_between, self.view_vector) camera.transform = translate(self.pupil_point[0], self.pupil_point[1], self.pupil_point[2], ) * t_2 * t_1 vector_xyz = np.arange(3) vector_xyz = xr.DataArray(vector_xyz, coords=(vector_xyz, ), dims=('vector_xyz',), name='vector_xyz', ) # calculating the pixel view directions view_vectors = xr.combine_nested( [xr.zeros_like(self.x_pixel_ds + self.y_pixel_ds) + self.view_vector[i] for i in [0, 1, 2, ]], concat_dim=(vector_xyz,), ) view_vectors = view_vectors.rename('view_vectors') def v3d2da(v3d): """ raysect Vector3D to xarray DataArray :param v3d: :return: """ da = np.array([v3d.x, v3d.y, v3d.z, ]) da = xr.DataArray(da, coords=(np.arange(3),), dims=('vector_xyz',), ) return da # basis unit vectors defining camera view -- v_z is forward and v_y is up v_y = final_up_vector.normalise() v_x = self.view_vector.cross(v_y).normalise() v_z = self.view_vector.normalise() v_x, v_y, v_z = [v3d2da(i) for i in [v_x, v_y, v_z, ]] # FOV defines the widest view, with pixels defined as square. sensor_aspect = self.sensor_format[1] / self.sensor_format[0] if sensor_aspect > 1: fov_v = self.fov fov_h = self.fov / sensor_aspect elif sensor_aspect == 1: fov_v = fov_h = self.fov elif sensor_aspect < 1: fov_h = self.fov fov_v = self.fov * sensor_aspect else: raise Exception() pixel_projection = 2 * np.tan(fov_h * np.pi / 360) / self.sensor_format[0] view_vectors = view_vectors + (v_x * (self.x_pixel_ds - self.sensor_format[0] / 2 + 0.5) * pixel_projection) + \ (v_y * (self.y_pixel_ds - self.sensor_format[1] / 2 + 0.5) * pixel_projection) if self.verbose: print('--status: calculating ray lengths') # TODO there has to be a better way of doing this?! ray_lengths = xr.DataArray(np.zeros(self.sensor_format_ds), dims=('x', 'y', ), coords=(self.x_ds, self.y_ds, )) for idx_x, x_pixel in enumerate(self.x_pixel_ds.values): if self.verbose and idx_x % 10 == 0: print('x =', str(x_pixel)) for idx_y, y_pixel in enumerate(self.y_pixel_ds.values): direction = Vector3D(*list(view_vectors.isel(x=idx_x, y=idx_y, ).values)) intersections = [] for p in self.world.primitives: intersection = p.hit(CoreRay(self.pupil_point, direction, )) if intersection is not None: intersections.append(intersection) # find the intersection corresponding to the shortest ray length no_intersections = len(intersections) if no_intersections == 0: ray_lengths.values[idx_x, idx_y] = 3 else: ray_lengths.values[idx_x, idx_y] = min([i.ray_distance for i in intersections if i.primitive.name != 'Plasma Geometry']) camera.spectral_bins = 40 camera.pixel_samples = 10 camera.min_wavelength = self.wl_min_nm camera.max_wavelength = self.wl_max_nm camera.quiet = not self.verbose camera.observe() # output to netCDF via xarray wl = pipeline_spectral.wavelengths wl = xr.DataArray(wl, coords=(wl, ), dims=('wavelength', )) * 1e-9 # ( m ) spec_power_ds = pipeline_spectral.frame.mean * 1e9 # converting units from (W/nm) --> (W/m) spec_radiance_ds = pipeline_spectral_rad.frame.mean * 1e9 coords = (self.x_ds, self.y_ds, wl, ) dims = ('x', 'y', 'wavelength', ) name = 'spec_power' attrs = {'units': 'W/m^2/str/m'} spec_power_ds = xr.DataArray(np.flip(spec_power_ds, axis=1), coords=coords, dims=dims, name=name, attrs=attrs, ) spec_radiance_ds = xr.DataArray(np.flip(spec_radiance_ds, axis=1, ), coords=coords, dims=dims, name=name, attrs=attrs, ) # calculate the centre-of-mass wavelength radiance_ds = spec_power_ds.integrate(dim='wavelength').assign_attrs({'units': 'W/m^2/str', }) ds_ds = xr.Dataset({'spectral_radiance_ds': spec_radiance_ds, 'radiance_ds': radiance_ds, 'view_vectors_ds': view_vectors, 'ray_lengths_ds': ray_lengths }) x_p_y = self.x + self.y spec_power = spec_power_ds.interp_like(x_p_y) / self.cherab_down_sample # to conserve power ds = xr.Dataset({'spectral_radiance': spec_power, }) ds_ds.to_netcdf(self.fpath_ds, mode='w', ) ds.to_netcdf(self.fpath, mode='w', )
yrange = np.linspace(-2, 2, 200) d_alpha_rz_intensity = np.zeros((200, 200)) direction = Vector3D(0, 1, 0) for i, x in enumerate(xrange): for j, y in enumerate(yrange): emission = d_alpha_excit.emission(Point3D(x, y, 0.0), direction, Spectrum(650, 660, 1)) d_alpha_rz_intensity[j, i] = emission.total() plt.imshow(d_alpha_rz_intensity, extent=[-2, 2, -2, 2], origin='lower') plt.colorbar() plt.xlabel('x axis') plt.ylabel('y axis') plt.title("D-alpha emission in x-y") camera = PinholeCamera((256, 256), pipelines=[PowerPipeline2D()], parent=world) camera.transform = translate(-3, 0, 0) * rotate_basis(Vector3D(1, 0, 0), Vector3D(0, 0, 1)) camera.pixel_samples = 1 plt.ion() camera.observe() plt.ioff() plt.show() # this code can be used to plot the mesh, but it's quite slow # for tri_index in range(triangles.shape[0]): # v1, v2, v3 = triangles[tri_index] # plt.plot([vertex_coords[v1, 0], vertex_coords[v2, 0], vertex_coords[v3, 0], vertex_coords[v1, 0]], # [vertex_coords[v1, 1], vertex_coords[v2, 1], vertex_coords[v3, 1], vertex_coords[v1, 1]], 'k') # plt.plot([vertex_coords[v1, 0], vertex_coords[v2, 0], vertex_coords[v3, 0], vertex_coords[v1, 0]], # [vertex_coords[v1, 1], vertex_coords[v2, 1], vertex_coords[v3, 1], vertex_coords[v1, 1]], '.b')