Add trait SignificantlyDifferent

This commit is contained in:
Maximilian Ammann 2022-04-07 19:48:11 +02:00
parent 192297f286
commit 1a5440822e
4 changed files with 33 additions and 21 deletions

View File

@ -10,6 +10,7 @@ use cgmath::{AbsDiffEq, Matrix4, Point3, Vector3};
use style_spec::source::TileAddressingScheme;
use crate::util::math::{div_floor, Aabb2};
use crate::util::SignificantlyDifferent;
pub const EXTENT_UINT: u32 = 4096;
pub const EXTENT_SINT: i32 = EXTENT_UINT as i32;
@ -96,10 +97,11 @@ impl Zoom {
}
}
impl Eq for Zoom {}
impl PartialEq for Zoom {
fn eq(&self, other: &Self) -> bool {
self.0.abs_diff_eq(&other.0, 0.05)
impl SignificantlyDifferent for Zoom {
type Epsilon = f64;
fn ne(&self, other: &Self, epsilon: Self::Epsilon) -> bool {
self.0.abs_diff_eq(&other.0, epsilon)
}
}

View File

@ -3,6 +3,7 @@ use cgmath::{AbsDiffEq, Matrix4, Point2, Point3, Vector2, Vector3, Vector4};
use crate::render::shaders::ShaderCamera;
use crate::util::math::{bounds_from_points, Aabb2, Aabb3, Plane};
use crate::util::SignificantlyDifferent;
#[rustfmt::skip]
pub const OPENGL_TO_WGPU_MATRIX: cgmath::Matrix4<f64> = cgmath::Matrix4::new(
@ -73,12 +74,13 @@ pub struct Camera {
pub height: f64,
}
impl Eq for Camera {}
impl PartialEq for Camera {
fn eq(&self, other: &Self) -> bool {
self.position.abs_diff_eq(&other.position, 0.05)
&& self.yaw.abs_diff_eq(&other.yaw, 0.05)
&& self.pitch.abs_diff_eq(&other.pitch, 0.05)
impl SignificantlyDifferent for Camera {
type Epsilon = f64;
fn ne(&self, other: &Self, epsilon: Self::Epsilon) -> bool {
self.position.abs_diff_ne(&other.position, epsilon)
|| self.yaw.abs_diff_ne(&other.yaw, epsilon)
|| self.pitch.abs_diff_ne(&other.pitch, epsilon)
}
}

View File

@ -581,7 +581,7 @@ impl RenderState {
// TODO: Could we draw inspiration from StagingBelt (https://docs.rs/wgpu/latest/wgpu/util/struct.StagingBelt.html)?
// TODO: What is StagingBelt for?
if self.camera.did_change() || self.zoom.did_change() || self.try_failed {
if self.camera.did_change(0.05) || self.zoom.did_change(0.05) || self.try_failed {
if let Some(view_region) = &view_region {
// FIXME: We also need to request tiles from layers above if we are over the maximum zoom level
self.try_failed = self.request_tiles_in_view(view_region, scheduler);
@ -597,8 +597,8 @@ impl RenderState {
);
}
self.camera.finished_observing();
self.zoom.finished_observing();
self.camera.update_reference();
self.zoom.update_reference();
}
#[tracing::instrument(skip_all)]

View File

@ -51,31 +51,39 @@ impl MinMaxBoundingBox {
}
}
pub trait SignificantlyDifferent<Rhs: ?Sized = Self> {
type Epsilon;
/// This method tests for `self` and `other` values to be significantly different
#[must_use]
fn ne(&self, other: &Rhs, epsilon: Self::Epsilon) -> bool;
}
pub struct ChangeObserver<T> {
inner: T,
last_value: Option<T>,
reference_value: Option<T>,
}
impl<T> ChangeObserver<T> {
pub fn new(value: T) -> Self {
Self {
inner: value,
last_value: None,
reference_value: None,
}
}
}
impl<T> ChangeObserver<T>
where
T: Clone + Eq,
T: Clone + SignificantlyDifferent,
{
pub fn finished_observing(&mut self) {
self.last_value = Some(self.inner.clone());
pub fn update_reference(&mut self) {
self.reference_value = Some(self.inner.clone());
}
pub fn did_change(&self) -> bool {
if let Some(last_value) = &self.last_value {
if !last_value.eq(&self.inner) {
pub fn did_change(&self, epsilon: T::Epsilon) -> bool {
if let Some(reference_value) = &self.reference_value {
if reference_value.ne(&self.inner, epsilon) {
true
} else {
false