Skip to content

Commit

Permalink
fix some ui, line thickness, add resizeChanged and projectionChanged …
Browse files Browse the repository at this point in the history
…callbacks
  • Loading branch information
bmatthieu3 committed Jul 15, 2024
1 parent e9fbafc commit 10e57aa
Show file tree
Hide file tree
Showing 20 changed files with 191 additions and 50 deletions.
8 changes: 8 additions & 0 deletions examples/al-event-listeners.html
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@
$('#infoDiv').html(msg);
});

aladin.on('resizeChanged', function() {
console.log("resize")
});

aladin.on('projectionChanged', function(proj) {
console.log(proj)
});

cat.sources[0].actionClicked();
});
</script>
Expand Down
1 change: 1 addition & 0 deletions examples/al-init-custom-options.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
showFrame: true,
showZoomControl:true,
showSettingsControl:true,
showCooGrid: true,
}
);

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"homepage": "https://aladin.u-strasbg.fr/",
"name": "aladin-lite",
"type": "module",
"version": "3.4.3-beta",
"version": "3.4.4-beta",
"description": "An astronomical HiPS visualizer in the browser",
"author": "Thomas Boch and Matthieu Baumann",
"license": "GPL-3",
Expand Down
9 changes: 7 additions & 2 deletions src/core/src/camera/viewport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,12 +470,17 @@ impl CameraViewPort {
self.update_rot_matrices(proj);
}

/// lonlat must be given in icrs frame
/// center lonlat must be given in icrs frame
pub fn set_center(&mut self, lonlat: &LonLatT<f64>, proj: &ProjectionType) {
let icrs_pos: Vector4<_> = lonlat.vector();

let view_pos = CooSystem::ICRS.to(self.get_coo_system()) * icrs_pos;
let rot = Rotation::from_sky_position(&view_pos);
let rot_to_center = Rotation::from_sky_position(&view_pos);

let phi = self.get_center_pos_angle();
let third_euler_rot = Rotation::from_axis_angle(&view_pos.truncate(), phi);

let rot = third_euler_rot * rot_to_center;

// Apply the rotation to the camera to go
// to the next lonlat
Expand Down
4 changes: 3 additions & 1 deletion src/core/src/renderable/grid/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,9 @@ impl ProjetedGrid {
crate::shader::get_shader(&self.gl, shaders, "line_inst_ndc.vert", "line_base.frag")?
.bind(&self.gl)
.attach_uniform("u_color", &self.color)
.attach_uniform("u_width", &self.thickness)
.attach_uniform("u_width", &(camera.get_width()))
.attach_uniform("u_height", &(camera.get_height()))
.attach_uniform("u_thickness", &self.thickness)
.bind_vertex_array_object_ref(&self.vao)
.draw_elements_instanced_with_i32(
WebGl2RenderingContext::TRIANGLES,
Expand Down
18 changes: 13 additions & 5 deletions src/core/src/renderable/image/grid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ use wcs::WCS;
pub fn get_grid_params(
xy_min: &(f64, f64),
xy_max: &(f64, f64),
max_tex_size: u64,
max_tex_size_x: u64,
max_tex_size_y: u64,
num_tri_per_tex_patch: u64,
) -> (
impl Iterator<Item = (u64, f32)> + Clone,
Expand All @@ -31,8 +32,8 @@ pub fn get_grid_params(
let step = (step_x.max(step_y)).max(1); // at least one pixel!

(
get_coord_uv_it(xmin, xmax, step, max_tex_size),
get_coord_uv_it(ymin, ymax, step, max_tex_size),
get_coord_uv_it(xmin, xmax, step, max_tex_size_x),
get_coord_uv_it(ymin, ymax, step, max_tex_size_y),
)
}

Expand Down Expand Up @@ -169,13 +170,20 @@ fn build_range_indices(it: impl Iterator<Item = (u64, f32)> + Clone) -> Vec<Rang
pub fn vertices(
xy_min: &(f64, f64),
xy_max: &(f64, f64),
max_tex_size: u64,
max_tex_size_x: u64,
max_tex_size_y: u64,
num_tri_per_tex_patch: u64,
camera: &CameraViewPort,
wcs: &WCS,
projection: &ProjectionType,
) -> (Vec<f32>, Vec<f32>, Vec<u16>, Vec<u32>) {
let (x_it, y_it) = get_grid_params(xy_min, xy_max, max_tex_size, num_tri_per_tex_patch);
let (x_it, y_it) = get_grid_params(
xy_min,
xy_max,
max_tex_size_x,
max_tex_size_y,
num_tri_per_tex_patch,
);

let idx_x_ranges = build_range_indices(x_it.clone());
let idx_y_ranges = build_range_indices(y_it.clone());
Expand Down
96 changes: 83 additions & 13 deletions src/core/src/renderable/image/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,23 @@ pub struct Image {
/// Texture indices that must be drawn
idx_tex: Vec<usize>,
/// The maximum webgl supported texture size
max_tex_size: usize,
max_tex_size_x: usize,
max_tex_size_y: usize,

reg: Region,
// The coo system in which the polygonal region has been defined
coo_sys: CooSystem,
}

use al_core::pixel::Pixel;
use al_core::texture::TEX_PARAMS;
use fitsrs::hdu::header::extension;
use fitsrs::hdu::AsyncHDU;
use futures::io::BufReader;
use futures::AsyncReadExt;
impl Image {
pub async fn from_reader_and_wcs<R, F>(
gl: &WebGlContext,
reader: R,
mut reader: R,
wcs: WCS,
mut scale: Option<f32>,
mut offset: Option<f32>,
Expand All @@ -94,20 +96,86 @@ impl Image {
WebGl2RenderingContext::get_parameter(gl, WebGl2RenderingContext::MAX_TEXTURE_SIZE)?
.as_f64()
.unwrap_or(4096.0) as usize;
let (textures, mut cuts) =
subdivide_texture::build::<F, R>(gl, width, height, reader, max_tex_size, blank)
.await?;

let mut max_tex_size_x = max_tex_size;
let mut max_tex_size_y = max_tex_size;

// apply bscale to the cuts
if F::NUM_CHANNELS == 1 {
offset = offset.or(Some(0.0));
scale = scale.or(Some(1.0));
blank = blank.or(Some(std::f32::NAN));
cuts = cuts.map(|cuts| {
let start = cuts.start * scale.unwrap() + offset.unwrap();
let end = cuts.end * scale.unwrap() + offset.unwrap();
start..end
});
}

let (textures, mut cuts) = if width <= max_tex_size as u64 && height <= max_tex_size as u64
{
max_tex_size_x = width as usize;
max_tex_size_y = height as usize;
// can fit in one texture

let num_pixels_to_read = (width as usize) * (height as usize);
let num_bytes_to_read =
num_pixels_to_read * std::mem::size_of::<<F::P as Pixel>::Item>() * F::NUM_CHANNELS;
let mut buf = vec![0; num_bytes_to_read];

let _ = reader.read_exact(&mut buf[..]).await.map_err(|e| {
JsValue::from_str("invalid data with respect to the NAXIS given in the WCS")
})?;

unsafe {
let slice = std::slice::from_raw_parts(
buf.as_mut_ptr() as *const <F::P as Pixel>::Item,
num_bytes_to_read / std::mem::size_of::<<F::P as Pixel>::Item>(),
);

let cuts = if F::NUM_CHANNELS == 1 {
let mut samples = slice
.iter()
.filter_map(|item| {
let t: f32 =
<<F::P as Pixel>::Item as al_core::convert::Cast<f32>>::cast(*item);
if t.is_nan() || t == blank.unwrap() {
None
} else {
Some(t)
}
})
.collect::<Vec<_>>();

let cuts = cuts::first_and_last_percent(&mut samples, 1, 99);
Some(cuts)
} else {
None
};

(
vec![Texture2D::create_from_raw_pixels::<F>(
gl,
width as i32,
height as i32,
TEX_PARAMS,
Some(slice),
)?],
cuts,
)
}
} else {
subdivide_texture::crop_image::<F, R>(
gl,
width,
height,
reader,
max_tex_size as u64,
blank,
)
.await?
};

if let Some(cuts) = cuts.as_mut() {
let start = cuts.start * scale.unwrap() + offset.unwrap();
let end = cuts.end * scale.unwrap() + offset.unwrap();

*cuts = start..end;
}

let num_indices = vec![];
Expand Down Expand Up @@ -213,7 +281,8 @@ impl Image {
channel: F::CHANNEL_TYPE,
textures,
cuts,
max_tex_size,
max_tex_size_x,
max_tex_size_y,
// Indices of textures that must be drawn
idx_tex,
// The polygonal region in the sky
Expand Down Expand Up @@ -484,7 +553,8 @@ impl Image {
let (pos, uv, indices, num_indices) = grid::vertices(
&(x_mesh_range.start, y_mesh_range.start),
&(x_mesh_range.end.ceil(), y_mesh_range.end.ceil()),
self.max_tex_size as u64,
self.max_tex_size_x as u64,
self.max_tex_size_y as u64,
num_vertices,
camera,
&self.wcs,
Expand Down
16 changes: 10 additions & 6 deletions src/core/src/renderable/image/subdivide_texture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,30 @@ use al_core::Texture2D;
use al_core::WebGlContext;
use std::ops::Range;

pub async fn build<'a, F, R>(
pub async fn crop_image<'a, F, R>(
gl: &WebGlContext,
width: u64,
height: u64,
mut reader: R,
max_tex_size: usize,
max_tex_size: u64,
blank: Option<f32>,
) -> Result<(Vec<Texture2D>, Option<Range<f32>>), JsValue>
where
F: ImageFormat,
R: AsyncReadExt + Unpin,
{
let mut buf =
vec![0; max_tex_size * std::mem::size_of::<<F::P as Pixel>::Item>() * F::NUM_CHANNELS];
let max_tex_size = max_tex_size as u64;
let mut tex_chunks = vec![];

// Subdivision
let num_textures = ((width / max_tex_size) + 1) * ((height / max_tex_size) + 1);

let mut tex_chunks = vec![];
let mut buf = vec![
0;
(max_tex_size as usize)
* std::mem::size_of::<<F::P as Pixel>::Item>()
* F::NUM_CHANNELS
];

for _ in 0..num_textures {
tex_chunks.push(Texture2D::create_from_raw_pixels::<F>(
gl,
Expand Down
8 changes: 6 additions & 2 deletions src/core/src/renderable/moc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,9 @@ impl MOCIntern {
.attach_uniforms_from(camera)
.attach_uniform("u_2world", &icrs2world)
.attach_uniform("u_color", &color)
.attach_uniform("u_width", &thickness)
.attach_uniform("u_width", &(camera.get_width()))
.attach_uniform("u_height", &(camera.get_height()))
.attach_uniform("u_thickness", &thickness)
.attach_uniform("u_proj", proj)
.bind_vertex_array_object_ref(&self.vao)
.draw_elements_instanced_with_i32(
Expand Down Expand Up @@ -487,7 +489,9 @@ impl MOCIntern {
.attach_uniforms_from(camera)
.attach_uniform("u_2world", &icrs2world)
.attach_uniform("u_color", &color)
.attach_uniform("u_width", &thickness)
.attach_uniform("u_width", &(camera.get_width()))
.attach_uniform("u_height", &(camera.get_height()))
.attach_uniform("u_thickness", &thickness)
.attach_uniform("u_proj", proj)
.bind_vertex_array_object_ref(&self.vao)
.draw_elements_instanced_with_i32(
Expand Down
8 changes: 4 additions & 4 deletions src/css/aladin.css
Original file line number Diff line number Diff line change
Expand Up @@ -328,10 +328,10 @@ canvas {
}

.aladin-btn.aladin-dark-theme.toggled {
border-color: greenyellow;
border-color: dodgerblue;
}
.aladin-btn.aladin-dark-theme:hover, .aladin-input-select.aladin-dark-theme:hover {
border-color: red;
border-color: greenyellow;
}

.aladin-btn.disabled {
Expand Down Expand Up @@ -1176,13 +1176,13 @@ canvas {
line-height: 1.7rem;
}

.aladin-fov .aladin-zoom-in {
.aladin-fov .aladin-zoom-out {
margin-right: 0;
border-right: none;
border-radius: 5px 0px 0px 5px;
}

.aladin-fov .aladin-zoom-out {
.aladin-fov .aladin-zoom-in {
border-radius: 0px 5px 5px 0px;
}

Expand Down
2 changes: 1 addition & 1 deletion src/glsl/webgl2/line/base.vert
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#version 300 es
precision lowp float;
precision highp float;
layout (location = 0) in vec2 ndc_pos;

out float l;
Expand Down
5 changes: 4 additions & 1 deletion src/glsl/webgl2/line/inst_lonlat.vert
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ uniform mat4 u_2world;
uniform vec2 ndc_to_clip;
uniform float czf;
uniform float u_width;
uniform float u_height;
uniform float u_thickness;

out float l;

Expand All @@ -34,6 +36,7 @@ void main() {
vec2 x_b = p_b_ndc - p_a_ndc;
vec2 y_b = normalize(vec2(-x_b.y, x_b.x));

vec2 p_ndc = p_a_ndc + x_b * vertex.x + y_b * u_width * 0.001 * vertex.y;
float ndc2pix = 2.0 / u_width;
vec2 p_ndc = p_a_ndc + x_b * vertex.x + u_thickness * y_b * vertex.y * vec2(1.0, u_width/u_height) * ndc2pix;
gl_Position = vec4(p_ndc, 0.f, 1.f);
}
8 changes: 6 additions & 2 deletions src/glsl/webgl2/line/inst_ndc.vert
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
#version 300 es
precision lowp float;
precision highp float;
layout (location = 0) in vec2 p_a;
layout (location = 1) in vec2 p_b;
layout (location = 2) in vec2 vertex;

out float l;

uniform float u_width;
uniform float u_height;
uniform float u_thickness;

void main() {
vec2 x_b = p_b - p_a;
vec2 y_b = normalize(vec2(-x_b.y, x_b.x));

vec2 p = p_a + x_b * vertex.x + y_b * u_width * 0.001 * vertex.y;
float ndc2pix = 2.0 / u_width;

vec2 p = p_a + x_b * vertex.x + u_thickness * y_b * vertex.y * vec2(1.0, u_width/u_height) * ndc2pix;
gl_Position = vec4(p, 0.f, 1.f);
}
2 changes: 1 addition & 1 deletion src/js/A.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ A.polygon = function (raDecArray, options) {
}

options = options || {};
//options.closed = true;
options.closed = true;

return new Polyline(raDecArray, options);
};
Expand Down
Loading

0 comments on commit 10e57aa

Please sign in to comment.