Skip to content

Commit

Permalink
factorize code by handling reversed_longitude directly when projectin…
Browse files Browse the repository at this point in the history
…g. Should fix #115
  • Loading branch information
bmatthieu3 committed Sep 27, 2023
1 parent 0f61915 commit 9137eff
Show file tree
Hide file tree
Showing 15 changed files with 245 additions and 283 deletions.
5 changes: 3 additions & 2 deletions src/core/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,9 @@ impl App {
// When it will be supported nearly everywhere, we will need to uncomment this line to
// enable it
//gl.enable(WebGl2RenderingContext::SCISSOR_TEST);
gl.enable(WebGl2RenderingContext::CULL_FACE);
gl.cull_face(WebGl2RenderingContext::BACK);

//gl.enable(WebGl2RenderingContext::CULL_FACE);
//gl.cull_face(WebGl2RenderingContext::BACK);

// The tile buffer responsible for the tile requests
let downloader = Downloader::new();
Expand Down
30 changes: 12 additions & 18 deletions src/core/src/camera/viewport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ pub struct CameraViewPort {
// The rotation of the camera
rotation_center_angle: Angle<f64>,
w2m_rot: Rotation<f64>,
final_rot: Rotation<f64>,

w2m: Matrix4<f64>,
m2w: Matrix4<f64>,
Expand Down Expand Up @@ -103,7 +102,6 @@ impl CameraViewPort {
let zoomed = false;

let w2m_rot = Rotation::zero();
let final_rot = Rotation::zero();

// Get the initial size of the window
let window = web_sys::window().unwrap_abort();
Expand Down Expand Up @@ -147,7 +145,6 @@ impl CameraViewPort {
m2w,

dpi,
final_rot,
rotation_center_angle,
// The width over height ratio
aspect,
Expand Down Expand Up @@ -511,10 +508,11 @@ impl CameraViewPort {

pub fn set_longitude_reversed(&mut self, reversed_longitude: bool, proj: &ProjectionType) {
if self.reversed_longitude != reversed_longitude {
self.reversed_longitude = reversed_longitude;

self.rotation_center_angle = -self.rotation_center_angle;
self.update_rot_matrices(proj);
}
self.reversed_longitude = reversed_longitude;

// The camera is reversed => it has moved
self.moved = true;
Expand All @@ -526,18 +524,6 @@ impl CameraViewPort {
}

// Accessors
pub fn get_rotation(&self) -> &Rotation<f64> {
&self.w2m_rot
}

// This rotation is the final rotation, i.e. a composite of
// two rotations:
// - The current rotation of the sphere
// - The rotation around the center axis of a specific angle
pub fn get_final_rotation(&self) -> &Rotation<f64> {
&self.final_rot
}

pub fn get_w2m(&self) -> &cgmath::Matrix4<f64> {
&self.w2m
}
Expand Down Expand Up @@ -654,6 +640,11 @@ impl CameraViewPort {
}

fn update_center(&mut self) {
// Longitude reversed identity matrix
const ID_R: &Matrix4<f64> = &Matrix4::new(
-1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0,
);

// The center position is on the 3rd column of the w2m matrix
self.center = self.w2m.z;

Expand All @@ -662,9 +653,12 @@ impl CameraViewPort {

// Re-update the model matrix to take into account the rotation
// by theta around the center axis
self.final_rot = center_rot * self.w2m_rot;
let final_rot = center_rot * self.w2m_rot;
self.w2m = (&final_rot).into();
if self.reversed_longitude {
self.w2m = self.w2m * ID_R;
}

self.w2m = (&self.final_rot).into();
self.m2w = self.w2m.transpose();
}
}
Expand Down
29 changes: 15 additions & 14 deletions src/core/src/grid/label.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@


use crate::math::HALF_PI;
use crate::math::PI;
use cgmath::Vector3;
use crate::ProjectionType;
use crate::CameraViewPort;
use crate::LonLatT;
use crate::ProjectionType;
use cgmath::InnerSpace;
use cgmath::Vector3;

use crate::math::angle::SerializeFmt;
use crate::math::TWICE_PI;
use crate::grid::XYScreen;
use crate::math::angle::SerializeFmt;
use crate::math::lonlat::LonLat;
use crate::math::TWICE_PI;

use crate::math::angle::ToAngle;
use core::ops::Range;
use cgmath::Vector2;
use core::ops::Range;

const OFF_TANGENT: f64 = 35.0;
const OFF_BI_TANGENT: f64 = 5.0;
Expand All @@ -40,7 +39,7 @@ impl Label {
options: LabelOptions,
camera: &CameraViewPort,
projection: &ProjectionType,
fmt: &SerializeFmt
fmt: &SerializeFmt,
) -> Option<Self> {
let fov = camera.get_field_of_view();
let d = if fov.contains_north_pole() {
Expand All @@ -60,17 +59,15 @@ impl Label {

LonLatT::new(lon.to_angle(), lat.to_angle())
}
LabelOptions::OnSide => LonLatT::new(lon.to_angle(), lat.start.to_angle())
LabelOptions::OnSide => LonLatT::new(lon.to_angle(), lat.start.to_angle()),
};

let m1: Vector3<_> = lonlat.vector();
let m2 = (m1 + d * 1e-3).normalize();

//let s1 = projection.model_to_screen_space(&(system.to_icrs_j2000::<f64>() * m1), camera, reversed_longitude)?;
let d1 = projection.model_to_screen_space(&m1.extend(1.0), camera)?;
let d2 = projection.model_to_screen_space(&m2.extend(1.0), camera)?;

//let s2 = projection.model_to_screen_space(&(system.to_icrs_j2000::<f64>() * m2), camera, reversed_longitude)?;
let dt = (d2 - d1).normalize();
let db = Vector2::new(dt.y.abs(), dt.x.abs());

Expand Down Expand Up @@ -108,7 +105,7 @@ impl Label {
let lon = camera.get_center().lon();
LonLatT::new(lon, lat.to_angle())
}
LabelOptions::OnSide => LonLatT::new(lon.start.to_angle(), lat.to_angle())
LabelOptions::OnSide => LonLatT::new(lon.start.to_angle(), lat.to_angle()),
};

let m1: Vector3<_> = lonlat.vector();
Expand Down Expand Up @@ -141,13 +138,17 @@ impl Label {
};

// rot is between -PI and +PI
let rot = dt.y.signum() * dt.x.acos() + PI;
let mut angle = dt.y.signum() * dt.x.acos();

// Detect if the label is upside-down fix the angle by adding PI
if angle.abs() >= HALF_PI {
angle += PI;
}

Some(Label {
position,
content,
rot,
rot: angle,
})
}
}
90 changes: 26 additions & 64 deletions src/core/src/math/projection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub mod domain;

use domain::{basic, cod::Cod, full::FullScreen, hpx::Hpx, par::Par};

/* S <-> NDC space conversion methods */
pub fn screen_to_ndc_space(
pos_screen_space: &Vector2<f64>,
camera: &CameraViewPort,
Expand Down Expand Up @@ -55,6 +56,7 @@ pub fn ndc_to_screen_space(
pos_screen_space / dpi
}

/* NDC <-> CLIP space conversion methods */
pub fn clip_to_ndc_space(pos_clip_space: &Vector2<f64>, camera: &CameraViewPort) -> Vector2<f64> {
let ndc_to_clip = camera.get_ndc_to_clip();
let clip_zoom_factor = camera.get_clip_zoom_factor();
Expand All @@ -65,6 +67,20 @@ pub fn clip_to_ndc_space(pos_clip_space: &Vector2<f64>, camera: &CameraViewPort)
)
}

pub fn ndc_to_clip_space(
pos_normalized_device: &Vector2<f64>,
camera: &CameraViewPort,
) -> Vector2<f64> {
let ndc_to_clip = camera.get_ndc_to_clip();
let clip_zoom_factor = camera.get_clip_zoom_factor();

Vector2::new(
pos_normalized_device.x * ndc_to_clip.x * clip_zoom_factor,
pos_normalized_device.y * ndc_to_clip.y * clip_zoom_factor,
)
}

/* S <-> CLIP space conversion methods */
pub fn clip_to_screen_space(
pos_clip_space: &Vector2<f64>,
camera: &CameraViewPort,
Expand All @@ -81,19 +97,6 @@ pub fn screen_to_clip_space(
ndc_to_clip_space(&pos_normalized_device, camera)
}

pub fn ndc_to_clip_space(
pos_normalized_device: &Vector2<f64>,
camera: &CameraViewPort,
) -> Vector2<f64> {
let ndc_to_clip = camera.get_ndc_to_clip();
let clip_zoom_factor = camera.get_clip_zoom_factor();

Vector2::new(
pos_normalized_device.x * ndc_to_clip.x * clip_zoom_factor,
pos_normalized_device.y * ndc_to_clip.y * clip_zoom_factor,
)
}

use al_api::coo_system::CooSystem;
use cgmath::InnerSpace;

Expand Down Expand Up @@ -167,21 +170,15 @@ impl ProjectionType {
let pos_screen_space = *pos_screen_space;
let pos_normalized_device = screen_to_ndc_space(&pos_screen_space, camera);

let ndc_to_clip = camera.get_ndc_to_clip();
let clip_zoom_factor = camera.get_clip_zoom_factor();

let pos_clip_space = Vector2::new(
pos_normalized_device.x * ndc_to_clip.x * clip_zoom_factor,
pos_normalized_device.y * ndc_to_clip.y * clip_zoom_factor,
);
let pos_clip_space = ndc_to_clip_space(&pos_normalized_device, camera);
self.clip_to_world_space(&pos_clip_space)
.map(|mut pos_world_space| {
if camera.get_longitude_reversed() {
pos_world_space.x = -pos_world_space.x;
}
/*.map(|mut pos_world_space| {
if camera.get_longitude_reversed() {
pos_world_space.x = -pos_world_space.x;
}
pos_world_space.normalize()
})
pos_world_space.normalize()
})*/
}

/// Screen to model space deprojection
Expand All @@ -198,10 +195,7 @@ impl ProjectionType {
camera: &CameraViewPort,
) -> Option<Vector4<f64>> {
self.screen_to_world_space(pos_screen_space, camera)
.map(|world_pos| {
let r = camera.get_final_rotation();
r.rotate(&world_pos)
})
.map(|world_pos| camera.get_w2m() * world_pos)
}

pub fn normalized_device_to_model_space(
Expand All @@ -210,10 +204,7 @@ impl ProjectionType {
camera: &CameraViewPort,
) -> Option<XYZWModel> {
self.normalized_device_to_world_space(ndc_pos, camera)
.map(|world_pos| {
let r = camera.get_final_rotation();
r.rotate(&world_pos)
})
.map(|world_pos| camera.get_w2m() * world_pos)
}

pub fn model_to_screen_space(
Expand Down Expand Up @@ -295,18 +286,7 @@ impl ProjectionType {
camera: &CameraViewPort,
) -> Option<Vector2<f64>> {
self.world_to_clip_space(pos_world_space)
.map(|mut pos_clip_space| {
if camera.get_longitude_reversed() {
pos_clip_space.x = -pos_clip_space.x;
}
let ndc_to_clip = camera.get_ndc_to_clip();
let clip_zoom_factor = camera.get_clip_zoom_factor();

Vector2::new(
pos_clip_space.x / (ndc_to_clip.x * clip_zoom_factor),
pos_clip_space.y / (ndc_to_clip.y * clip_zoom_factor),
)
})
.map(|pos_clip_space| clip_to_ndc_space(&pos_clip_space, camera))
}

pub fn normalized_device_to_world_space(
Expand All @@ -318,24 +298,6 @@ impl ProjectionType {
self.clip_to_world_space(&clip_pos)
}

/*pub fn world_to_normalized_device_space_unchecked(
&self,
pos_world_space: &Vector4<f64>,
camera: &CameraViewPort,
) -> Vector2<f64> {
let mut pos_clip_space = self.world_to_clip_space_unchecked(pos_world_space);
if camera.get_longitude_reversed() {
pos_clip_space.x = -pos_clip_space.x;
}
let ndc_to_clip = camera.get_ndc_to_clip();
let clip_zoom_factor = camera.get_clip_zoom_factor();
Vector2::new(
pos_clip_space.x / (ndc_to_clip.x * clip_zoom_factor),
pos_clip_space.y / (ndc_to_clip.y * clip_zoom_factor),
)
}*/

pub fn world_to_screen_space(
&self,
pos_world_space: &Vector4<f64>,
Expand Down
3 changes: 1 addition & 2 deletions src/core/src/renderable/coverage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@ pub mod moc;

use crate::renderable::line::RasterizedLineRenderer;

use super::utils::triangle::Triangle;
use wasm_bindgen::JsValue;

use hierarchy::MOCHierarchy;

use super::utils::Triangle;

use al_api::coo_system::CooSystem;

use al_api::moc::MOC as Cfg;
Expand Down
Loading

0 comments on commit 9137eff

Please sign in to comment.