From f2a34637cb771048e9e879584a048feb180388dd Mon Sep 17 00:00:00 2001 From: Mihai Calin Luca Date: Wed, 12 Jun 2024 12:29:21 +0200 Subject: [PATCH 01/20] initial impl and test --- .../tests/big_float_scenario_rs_test.rs | 13 +++++++++ .../base/src/types/managed/basic/big_float.rs | 29 +++++++++++++++++-- .../types/managed/wrapped/managed_decimal.rs | 2 +- .../scenario/tests/managed_decimal_test.rs | 4 +-- 4 files changed, 43 insertions(+), 5 deletions(-) diff --git a/contracts/feature-tests/big-float-features/tests/big_float_scenario_rs_test.rs b/contracts/feature-tests/big-float-features/tests/big_float_scenario_rs_test.rs index 66ca09ab86..9aa3dc14dd 100644 --- a/contracts/feature-tests/big-float-features/tests/big_float_scenario_rs_test.rs +++ b/contracts/feature-tests/big-float-features/tests/big_float_scenario_rs_test.rs @@ -56,6 +56,19 @@ fn big_float_overflow_test_rs() { assert_eq!(forth, &BigUint::from(2184473079534488064u64)); } +#[test] +fn big_float_ln_test_rs() { + let fixed = BigFloat::::from_frac(23i64, 2i64); + + let ln_fixed = fixed.ln(BigUint::from(10u64)); // precision of 10 decimal points + + println!("{ln_fixed:?}"); + assert_eq!( + ln_fixed, + BigFloat::from_frac(31355146488i64, 10_000_000_000i64) + ); +} + #[test] fn big_float_new_from_big_int_rs() { world().run("scenarios/big_float_new_from_big_int.scen.json"); diff --git a/framework/base/src/types/managed/basic/big_float.rs b/framework/base/src/types/managed/basic/big_float.rs index 934bd2c636..64c4515d4e 100644 --- a/framework/base/src/types/managed/basic/big_float.rs +++ b/framework/base/src/types/managed/basic/big_float.rs @@ -168,8 +168,33 @@ impl BigFloat { (self * denominator).trunc() } - pub fn to_managed_decimal(self, decimals: T) -> ManagedDecimal { - ManagedDecimal::::from_big_float(self, decimals) + pub fn to_managed_decimal(&self, decimals: T) -> ManagedDecimal { + ManagedDecimal::::from_big_float(&self, decimals) + } + + pub fn ln(&self, precision: BigUint) -> Self { + // find the highest power of 2 less than or equal to self + let mng_dec = self + .to_fixed_point(&BigFloat::from(precision)) + .into_big_uint() + .unwrap_or_sc_panic("can't calculate ln for this number"); + + let log2 = mng_dec.log2(); // most significant bit + let divisor = BigFloat::from(1 << log2); + let x = self / &divisor; // normalize to [1.0, 2.0] + + let ln_of_2 = BigFloat::from_frac(69314718i64, 100_000_000i64); // 0.69314718 8 decimals + let first = BigFloat::from_frac(17417939i64, 10_000_000i64); // 1.7417939, 7 decimals + let second = BigFloat::from_frac(28212026i64, 10_000_000i64); // 2.8212026, 7 decimals + let third = BigFloat::from_frac(14699568i64, 10_000_000i64); // 1.4699568, 7 decimals + let fourth = BigFloat::from_frac(44717955i64, 100_000_000i64); // 0.44717955, 8 decimals + let fifth = BigFloat::from_frac(56570851i64, 1_000_000_000i64); // 0.056570851, 9 decimals + + // approximating polynom to get the result + let result = + (((fourth - fifth * x.clone()) * x.clone() - third) * x.clone() + second) * x - first; + let add_member = BigFloat::from_big_uint(&BigUint::from(log2)) * ln_of_2; + result + add_member } } diff --git a/framework/base/src/types/managed/wrapped/managed_decimal.rs b/framework/base/src/types/managed/wrapped/managed_decimal.rs index 89a7c47ca9..fb31e80e58 100644 --- a/framework/base/src/types/managed/wrapped/managed_decimal.rs +++ b/framework/base/src/types/managed/wrapped/managed_decimal.rs @@ -126,7 +126,7 @@ impl ManagedDecimal { } pub fn from_big_float( - big_float: BigFloat, + big_float: &BigFloat, num_decimals: T, ) -> ManagedDecimal { let scaling_factor: &BigUint = &num_decimals.scaling_factor(); diff --git a/framework/scenario/tests/managed_decimal_test.rs b/framework/scenario/tests/managed_decimal_test.rs index cbe701c9cf..ea862953e7 100644 --- a/framework/scenario/tests/managed_decimal_test.rs +++ b/framework/scenario/tests/managed_decimal_test.rs @@ -64,10 +64,10 @@ pub fn test_managed_decimal() { let float_1 = BigFloat::::from_frac(3i64, 2i64); let fixed_float_1 = ManagedDecimal::>::from_big_float( - float_1.clone(), + &float_1, ConstDecimals::<1>, ); - let fixed_float_2 = ManagedDecimal::::from_big_float(float_1, 1usize); + let fixed_float_2 = ManagedDecimal::::from_big_float(&float_1, 1usize); assert_eq!( fixed_float_1, From fada5b36dd01bb2a6266f356cd48e8230ee3b290 Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Wed, 12 Jun 2024 18:32:39 +0300 Subject: [PATCH 02/20] ManagedDecimal - SCDisplay, Display, Debug --- .../types/managed/wrapped/managed_decimal.rs | 33 +++++++++++++++++-- .../scenario/tests/managed_decimal_test.rs | 6 ++-- 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/framework/base/src/types/managed/wrapped/managed_decimal.rs b/framework/base/src/types/managed/wrapped/managed_decimal.rs index fb31e80e58..2ca276ca9c 100644 --- a/framework/base/src/types/managed/wrapped/managed_decimal.rs +++ b/framework/base/src/types/managed/wrapped/managed_decimal.rs @@ -4,9 +4,11 @@ use crate::{ const_handles, use_raw_handle, BigFloatApiImpl, BigIntApiImpl, ManagedTypeApi, StaticVarApiImpl, }, + formatter::{FormatBuffer, FormatByteReceiver, SCDisplay}, types::{BigFloat, BigUint}, }; +use alloc::string::ToString; use multiversx_sc_codec::{ DecodeError, DecodeErrorHandler, EncodeErrorHandler, NestedDecode, NestedDecodeInput, NestedEncode, NestedEncodeOutput, TopDecode, TopDecodeInput, TopEncode, TopEncodeOutput, @@ -17,7 +19,7 @@ use core::{ ops::{Add, Deref, Div, Mul, Sub}, }; -use super::ManagedRef; +use super::{ManagedBufferCachedBuilder, ManagedRef}; fn scaling_factor( num_decimals: NumDecimals, @@ -71,7 +73,7 @@ impl Decimals for ConstDecimals { } } -#[derive(Debug, Clone)] +#[derive(Clone)] pub struct ManagedDecimal { data: BigUint, decimals: D, @@ -402,3 +404,30 @@ impl TypeAbi false } } +impl SCDisplay for ManagedDecimal { + fn fmt(&self, f: &mut F) { + let sf = self.decimals.scaling_factor(); + let temp = &self.data / sf.deref(); + temp.fmt(f); + f.append_bytes(b"."); + let temp = &self.data % sf.deref(); + temp.fmt(f); + } +} + +impl core::fmt::Display for ManagedDecimal { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + let mut result = ManagedBufferCachedBuilder::::new_from_slice(&[]); + result.append_display(self); + core::fmt::Display::fmt(&result.into_managed_buffer(), f) + } +} + +impl core::fmt::Debug for ManagedDecimal { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + f.debug_struct("ManagedDecimal") + .field("handle", &self.data.handle.clone()) + .field("number", &self.to_string()) + .finish() + } +} diff --git a/framework/scenario/tests/managed_decimal_test.rs b/framework/scenario/tests/managed_decimal_test.rs index ea862953e7..ec7d29fd9d 100644 --- a/framework/scenario/tests/managed_decimal_test.rs +++ b/framework/scenario/tests/managed_decimal_test.rs @@ -63,10 +63,8 @@ pub fn test_managed_decimal() { ); let float_1 = BigFloat::::from_frac(3i64, 2i64); - let fixed_float_1 = ManagedDecimal::>::from_big_float( - &float_1, - ConstDecimals::<1>, - ); + let fixed_float_1 = + ManagedDecimal::>::from_big_float(&float_1, ConstDecimals::<1>); let fixed_float_2 = ManagedDecimal::::from_big_float(&float_1, 1usize); assert_eq!( From 20acd9ecdd649378733d5f82f356cae3dca96ba6 Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Wed, 12 Jun 2024 18:33:28 +0300 Subject: [PATCH 03/20] BigFloat - ln fix --- .../tests/big_float_scenario_rs_test.rs | 16 ++++---- .../base/src/types/managed/basic/big_float.rs | 38 +++++++++++-------- .../src/types/managed/basic/big_float_cmp.rs | 7 ++++ 3 files changed, 36 insertions(+), 25 deletions(-) diff --git a/contracts/feature-tests/big-float-features/tests/big_float_scenario_rs_test.rs b/contracts/feature-tests/big-float-features/tests/big_float_scenario_rs_test.rs index 9aa3dc14dd..3879706111 100644 --- a/contracts/feature-tests/big-float-features/tests/big_float_scenario_rs_test.rs +++ b/contracts/feature-tests/big-float-features/tests/big_float_scenario_rs_test.rs @@ -58,15 +58,13 @@ fn big_float_overflow_test_rs() { #[test] fn big_float_ln_test_rs() { - let fixed = BigFloat::::from_frac(23i64, 2i64); - - let ln_fixed = fixed.ln(BigUint::from(10u64)); // precision of 10 decimal points - - println!("{ln_fixed:?}"); - assert_eq!( - ln_fixed, - BigFloat::from_frac(31355146488i64, 10_000_000_000i64) - ); + let x = BigFloat::::from(23i64); + let ln_x = x.ln(); + assert_eq!(ln_x.to_managed_decimal(9usize).to_string(), "3.135514648"); + assert!(ln_x.is_close( + &BigFloat::from_frac(3135514648, 1_000_000_000), // 3.135514648 + &BigFloat::from_frac(1, 1_000_000_000) + )); } #[test] diff --git a/framework/base/src/types/managed/basic/big_float.rs b/framework/base/src/types/managed/basic/big_float.rs index 64c4515d4e..4c363690aa 100644 --- a/framework/base/src/types/managed/basic/big_float.rs +++ b/framework/base/src/types/managed/basic/big_float.rs @@ -93,7 +93,6 @@ big_float_conv_num! {i16} big_float_conv_num! {i8} impl BigFloat { - #[inline] pub fn neg(&self) -> Self { let new_bf_handle: M::BigFloatHandle = use_raw_handle(M::static_var_api_impl().next_handle()); @@ -101,7 +100,13 @@ impl BigFloat { BigFloat::from_handle(new_bf_handle) } - #[inline] + pub fn abs(&self) -> Self { + let new_bf_handle: M::BigFloatHandle = + use_raw_handle(M::static_var_api_impl().next_handle()); + M::managed_type_impl().bf_abs(new_bf_handle.clone(), self.handle.clone()); + BigFloat::from_handle(new_bf_handle) + } + pub fn from_big_uint(big_uint: &BigUint) -> Self { let new_bf_handle: M::BigFloatHandle = use_raw_handle(M::static_var_api_impl().next_handle()); @@ -109,7 +114,6 @@ impl BigFloat { BigFloat::from_handle(new_bf_handle) } - #[inline] pub fn from_big_int(big_int: &BigInt) -> Self { let new_bf_handle: M::BigFloatHandle = use_raw_handle(M::static_var_api_impl().next_handle()); @@ -169,31 +173,33 @@ impl BigFloat { } pub fn to_managed_decimal(&self, decimals: T) -> ManagedDecimal { - ManagedDecimal::::from_big_float(&self, decimals) + ManagedDecimal::::from_big_float(self, decimals) } - pub fn ln(&self, precision: BigUint) -> Self { + pub fn ln(&self) -> Self { // find the highest power of 2 less than or equal to self - let mng_dec = self - .to_fixed_point(&BigFloat::from(precision)) + let trunc_val = self.trunc(); + let trunc_val_unsigned = trunc_val .into_big_uint() - .unwrap_or_sc_panic("can't calculate ln for this number"); - - let log2 = mng_dec.log2(); // most significant bit - let divisor = BigFloat::from(1 << log2); + .unwrap_or_sc_panic("log argument must be positive"); + let bit_log2 = trunc_val_unsigned.log2(); // aproximate, based on position of the most significant bit + let divisor = BigFloat::from(1 << bit_log2); let x = self / &divisor; // normalize to [1.0, 2.0] + debug_assert!(x >= 1); + debug_assert!(x <= 2); + let ln_of_2 = BigFloat::from_frac(69314718i64, 100_000_000i64); // 0.69314718 8 decimals - let first = BigFloat::from_frac(17417939i64, 10_000_000i64); // 1.7417939, 7 decimals + let first = BigFloat::from_frac(-17417939i64, 10_000_000i64); // -1.7417939, 7 decimals let second = BigFloat::from_frac(28212026i64, 10_000_000i64); // 2.8212026, 7 decimals - let third = BigFloat::from_frac(14699568i64, 10_000_000i64); // 1.4699568, 7 decimals + let third = BigFloat::from_frac(-14699568i64, 10_000_000i64); // -1.4699568, 7 decimals let fourth = BigFloat::from_frac(44717955i64, 100_000_000i64); // 0.44717955, 8 decimals - let fifth = BigFloat::from_frac(56570851i64, 1_000_000_000i64); // 0.056570851, 9 decimals + let fifth = BigFloat::from_frac(-56570851i64, 1_000_000_000i64); // -0.056570851, 9 decimals // approximating polynom to get the result let result = - (((fourth - fifth * x.clone()) * x.clone() - third) * x.clone() + second) * x - first; - let add_member = BigFloat::from_big_uint(&BigUint::from(log2)) * ln_of_2; + (((fourth + fifth * x.clone()) * x.clone() + third) * x.clone() + second) * x + first; + let add_member = BigFloat::from_big_uint(&BigUint::from(bit_log2)) * ln_of_2; result + add_member } } diff --git a/framework/base/src/types/managed/basic/big_float_cmp.rs b/framework/base/src/types/managed/basic/big_float_cmp.rs index d3958667e6..2b342bdb76 100644 --- a/framework/base/src/types/managed/basic/big_float_cmp.rs +++ b/framework/base/src/types/managed/basic/big_float_cmp.rs @@ -4,6 +4,13 @@ use crate::api::{use_raw_handle, BigFloatApiImpl, ManagedTypeApi, StaticVarApiIm use super::{BigFloat, BigInt}; +impl BigFloat { + pub fn is_close(&self, other: &Self, abs_tolerance: &Self) -> bool { + // TODO: temp handles + &(self - other).abs() <= abs_tolerance + } +} + impl PartialEq for BigFloat { #[inline] fn eq(&self, other: &Self) -> bool { From b86ac8e3b05d4c51ae493a0d1fe7e1a9eb0f28a1 Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Wed, 12 Jun 2024 19:34:06 +0300 Subject: [PATCH 04/20] BigFloat - ln optimization --- .../base/src/types/managed/basic/big_float.rs | 24 ++++++++++--------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/framework/base/src/types/managed/basic/big_float.rs b/framework/base/src/types/managed/basic/big_float.rs index 4c363690aa..c0a52c8eff 100644 --- a/framework/base/src/types/managed/basic/big_float.rs +++ b/framework/base/src/types/managed/basic/big_float.rs @@ -189,18 +189,20 @@ impl BigFloat { debug_assert!(x >= 1); debug_assert!(x <= 2); + let mut result = BigFloat::from_frac(-56570851i64, 1_000_000_000i64); // -0.056570851, 9 decimals; + result *= &x; + result += BigFloat::from_frac(44717955i64, 100_000_000i64); // 0.44717955, 8 decimals; + result *= &x; + result += BigFloat::from_frac(-14699568i64, 10_000_000i64); // -1.4699568, 7 decimals; + result *= &x; + result += BigFloat::from_frac(28212026i64, 10_000_000i64); // 2.8212026, 7 decimals; + result *= &x; + result += BigFloat::from_frac(-17417939i64, 10_000_000i64); // -1.7417939, 7 decimals; + let ln_of_2 = BigFloat::from_frac(69314718i64, 100_000_000i64); // 0.69314718 8 decimals - let first = BigFloat::from_frac(-17417939i64, 10_000_000i64); // -1.7417939, 7 decimals - let second = BigFloat::from_frac(28212026i64, 10_000_000i64); // 2.8212026, 7 decimals - let third = BigFloat::from_frac(-14699568i64, 10_000_000i64); // -1.4699568, 7 decimals - let fourth = BigFloat::from_frac(44717955i64, 100_000_000i64); // 0.44717955, 8 decimals - let fifth = BigFloat::from_frac(-56570851i64, 1_000_000_000i64); // -0.056570851, 9 decimals - - // approximating polynom to get the result - let result = - (((fourth + fifth * x.clone()) * x.clone() + third) * x.clone() + second) * x + first; - let add_member = BigFloat::from_big_uint(&BigUint::from(bit_log2)) * ln_of_2; - result + add_member + result += BigFloat::from(bit_log2 as i32) * ln_of_2; + + result } } From 0f0bff648fa21e4edd160a7188547ce9b02c5e58 Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Thu, 13 Jun 2024 08:04:14 +0300 Subject: [PATCH 05/20] BigFloat - ln optimization --- .../base/src/types/managed/basic/big_float.rs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/framework/base/src/types/managed/basic/big_float.rs b/framework/base/src/types/managed/basic/big_float.rs index c0a52c8eff..d618b7f1c7 100644 --- a/framework/base/src/types/managed/basic/big_float.rs +++ b/framework/base/src/types/managed/basic/big_float.rs @@ -189,17 +189,19 @@ impl BigFloat { debug_assert!(x >= 1); debug_assert!(x <= 2); - let mut result = BigFloat::from_frac(-56570851i64, 1_000_000_000i64); // -0.056570851, 9 decimals; + const DENOMINATOR: i64 = 1_000_000_000; + + let mut result = BigFloat::from_frac(-56570851, DENOMINATOR); // -0.056570851 result *= &x; - result += BigFloat::from_frac(44717955i64, 100_000_000i64); // 0.44717955, 8 decimals; + result += BigFloat::from_frac(447179550, DENOMINATOR); // 0.44717955 result *= &x; - result += BigFloat::from_frac(-14699568i64, 10_000_000i64); // -1.4699568, 7 decimals; + result += BigFloat::from_frac(-1469956800, DENOMINATOR); // -1.4699568 result *= &x; - result += BigFloat::from_frac(28212026i64, 10_000_000i64); // 2.8212026, 7 decimals; + result += BigFloat::from_frac(2821202600, DENOMINATOR); // 2.8212026 result *= &x; - result += BigFloat::from_frac(-17417939i64, 10_000_000i64); // -1.7417939, 7 decimals; + result += BigFloat::from_frac(-1741793900, DENOMINATOR); // -1.7417939 - let ln_of_2 = BigFloat::from_frac(69314718i64, 100_000_000i64); // 0.69314718 8 decimals + let ln_of_2 = BigFloat::from_frac(693147180, DENOMINATOR); // 0.69314718 result += BigFloat::from(bit_log2 as i32) * ln_of_2; result From 8540cc71eb284c6d959f5d75ead13a30f118b70a Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Thu, 13 Jun 2024 19:41:45 +0300 Subject: [PATCH 06/20] BigUint - ln --- .../basic-features/src/big_num_methods.rs | 2 +- .../base/src/types/managed/basic/big_uint.rs | 51 +++++++++++++++++-- framework/scenario/tests/big_uint_test.rs | 10 ++++ 3 files changed, 59 insertions(+), 4 deletions(-) create mode 100644 framework/scenario/tests/big_uint_test.rs diff --git a/contracts/feature-tests/basic-features/src/big_num_methods.rs b/contracts/feature-tests/basic-features/src/big_num_methods.rs index 086f89761b..c30b8cb56e 100644 --- a/contracts/feature-tests/basic-features/src/big_num_methods.rs +++ b/contracts/feature-tests/basic-features/src/big_num_methods.rs @@ -48,7 +48,7 @@ pub trait BigIntMethods { } #[endpoint] - fn biguint_overwrite_u64(&self, bu: BigUint, small: u64) -> BigUint { + fn biguint_overwrite_u64(&self, mut bu: BigUint, small: u64) -> BigUint { bu.overwrite_u64(small); bu } diff --git a/framework/base/src/types/managed/basic/big_uint.rs b/framework/base/src/types/managed/basic/big_uint.rs index 7f9f6bb17b..110a6b46d1 100644 --- a/framework/base/src/types/managed/basic/big_uint.rs +++ b/framework/base/src/types/managed/basic/big_uint.rs @@ -10,9 +10,12 @@ use crate::{ DecodeErrorHandler, EncodeErrorHandler, NestedDecode, NestedDecodeInput, NestedEncode, NestedEncodeOutput, TopDecode, TopDecodeInput, TopEncode, TopEncodeOutput, TryStaticCast, }, + contract_base::ErrorHelper, formatter::{hex_util::encode_bytes_as_hex, FormatBuffer, FormatByteReceiver, SCDisplay}, - proxy_imports::ManagedRef, - types::{heap::BoxedBytes, ManagedBuffer, ManagedBufferCachedBuilder, ManagedType}, + types::{ + heap::BoxedBytes, ConstDecimals, Decimals, ManagedBuffer, ManagedBufferCachedBuilder, + ManagedDecimal, ManagedRef, ManagedType, + }, }; use super::cast_to_i64::cast_to_i64; @@ -176,7 +179,7 @@ impl BigUint { } #[inline] - pub fn overwrite_u64(&self, value: u64) { + pub fn overwrite_u64(&mut self, value: u64) { Self::set_value(self.handle.clone(), value); } @@ -236,6 +239,48 @@ impl BigUint { let api = M::managed_type_impl(); api.bi_log2(self.handle.clone()) } + + pub fn ln(&self) -> ManagedDecimal> { + let bit_log2 = self.log2(); // aproximate, based on position of the most significant bit + let scaling_factor_9 = ConstDecimals::<9>.scaling_factor(); + let divisor = BigUint::from(1u64 << bit_log2); + let normalized = self * &*scaling_factor_9 / divisor; + + let x = normalized + .to_u64() + .unwrap_or_else(|| ErrorHelper::::signal_error_with_message("ln internal error")) + as i64; + + const DENOMINATOR: i64 = 1_000_000_000; + + // x normalized to [1.0, 2.0] + debug_assert!(x >= DENOMINATOR); + debug_assert!(x <= 2 * DENOMINATOR); + + let mut result: i64 = -56570851; // -0.056570851 + result *= x; + result /= DENOMINATOR; + result += 447179550; // 0.44717955 + result *= x; + result /= DENOMINATOR; + result += -1469956800; // -1.4699568 + result *= x; + result /= DENOMINATOR; + result += 2821202600; // 2.8212026 + result *= x; + result /= DENOMINATOR; + result += -1741793900; // -1.7417939 + + const LN_OF_2_SCALE_9: i64 = 693147180; // 0.69314718 + result += bit_log2 as i64 * LN_OF_2_SCALE_9; + + debug_assert!(result > 0); + + let mut result_bi = normalized; // reuse handle + result_bi.overwrite_u64(result as u64); + + ManagedDecimal::const_decimals_from_raw(result_bi) + } } impl Clone for BigUint { diff --git a/framework/scenario/tests/big_uint_test.rs b/framework/scenario/tests/big_uint_test.rs new file mode 100644 index 0000000000..d31eba9346 --- /dev/null +++ b/framework/scenario/tests/big_uint_test.rs @@ -0,0 +1,10 @@ +use multiversx_sc::types::BigUint; +use multiversx_sc_scenario::api::StaticApi; + +#[test] +fn test_big_uint_ln() { + // ln(23) = 3.1354942159291497 + let x = BigUint::::from(23u32); + let ln_x = x.ln(); + assert_eq!(ln_x.to_string(), "3.135514649"); // first 6 decimals are ok +} From f2648b76c668092f2604189addb3a6cf3c39d443 Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Thu, 13 Jun 2024 20:04:49 +0300 Subject: [PATCH 07/20] BigUint - ln - exploration via tests --- framework/scenario/tests/big_uint_test.rs | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/framework/scenario/tests/big_uint_test.rs b/framework/scenario/tests/big_uint_test.rs index d31eba9346..6c33032a36 100644 --- a/framework/scenario/tests/big_uint_test.rs +++ b/framework/scenario/tests/big_uint_test.rs @@ -1,10 +1,22 @@ use multiversx_sc::types::BigUint; use multiversx_sc_scenario::api::StaticApi; +fn assert_big_uint_ln(x: u32, ln_str: &str) { + let x = BigUint::::from(x); + let ln_x = x.ln(); + assert_eq!(ln_x.to_string(), ln_str); +} + #[test] fn test_big_uint_ln() { - // ln(23) = 3.1354942159291497 - let x = BigUint::::from(23u32); - let ln_x = x.ln(); - assert_eq!(ln_x.to_string(), "3.135514649"); // first 6 decimals are ok + assert_big_uint_ln(23, "3.135514649"); // vs. 3.1354942159291497 first 6 decimals are ok + + assert_big_uint_ln(1, "0.60599"); + assert_big_uint_ln(2, "0.693207779"); // vs. 0.6931471805599453 + assert_big_uint_ln(3, "1.98595430"); // vs. 1.0986122886681096 + assert_big_uint_ln(4, "1.386354959"); // vs. 1.3862943611198906 + assert_big_uint_ln(5, "1.609481340"); // vs. 1.6094379124341003 + assert_big_uint_ln(6, "1.791742610"); // vs. 1.791759469228055 + + assert_big_uint_ln(1000, "6.907784913"); // vs. 6.907755278982137 } From 449ef21ac6e73078fa710d1d879e5b9b22f4f089 Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Mon, 17 Jun 2024 20:38:37 +0300 Subject: [PATCH 08/20] test fix --- contracts/feature-tests/abi-tester/src/abi_test_type.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/contracts/feature-tests/abi-tester/src/abi_test_type.rs b/contracts/feature-tests/abi-tester/src/abi_test_type.rs index 2928e81c89..c70f1aec42 100644 --- a/contracts/feature-tests/abi-tester/src/abi_test_type.rs +++ b/contracts/feature-tests/abi-tester/src/abi_test_type.rs @@ -45,5 +45,6 @@ pub struct OnlyShowsUpInEsdtAttr { #[derive(TypeAbi)] pub struct ManagedDecimalWrapper { + #[allow(dead_code)] pub field: ManagedDecimal>, } From 33870173709bbb92a2ba0c4bde0feac8dc946ffb Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Mon, 17 Jun 2024 20:46:53 +0300 Subject: [PATCH 09/20] copied plotters-wasm-demo to repo --- Cargo.lock | 959 +++++++++++++++++++++++++--- Cargo.toml | 1 + tools/plotter/.gitignore | 3 + tools/plotter/Cargo.toml | 18 + tools/plotter/README.md | 54 ++ tools/plotter/src/func_plot.rs | 31 + tools/plotter/src/lib.rs | 58 ++ tools/plotter/src/mandelbrot.rs | 67 ++ tools/plotter/src/plot3d.rs | 42 ++ tools/plotter/start-server.bat | 8 + tools/plotter/start-server.sh | 23 + tools/plotter/www/.gitignore | 3 + tools/plotter/www/bootstrap.js | 21 + tools/plotter/www/index.html | 42 ++ tools/plotter/www/index.js | 103 +++ tools/plotter/www/package.json | 36 ++ tools/plotter/www/style.css | 55 ++ tools/plotter/www/webpack.config.js | 14 + 18 files changed, 1458 insertions(+), 80 deletions(-) create mode 100644 tools/plotter/.gitignore create mode 100644 tools/plotter/Cargo.toml create mode 100644 tools/plotter/README.md create mode 100644 tools/plotter/src/func_plot.rs create mode 100644 tools/plotter/src/lib.rs create mode 100644 tools/plotter/src/mandelbrot.rs create mode 100644 tools/plotter/src/plot3d.rs create mode 100644 tools/plotter/start-server.bat create mode 100755 tools/plotter/start-server.sh create mode 100644 tools/plotter/www/.gitignore create mode 100644 tools/plotter/www/bootstrap.js create mode 100644 tools/plotter/www/index.html create mode 100644 tools/plotter/www/index.js create mode 100644 tools/plotter/www/package.json create mode 100644 tools/plotter/www/style.css create mode 100644 tools/plotter/www/webpack.config.js diff --git a/Cargo.lock b/Cargo.lock index ec245b66b9..8cd3edc71d 100755 --- a/Cargo.lock +++ b/Cargo.lock @@ -66,7 +66,7 @@ version = "0.8.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e89da841a80418a9b391ebaea17f5c112ffaaa96f621d2c285b5174da76b9011" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "once_cell", "version_check", "zerocopy", @@ -98,6 +98,21 @@ dependencies = [ "multiversx-sc-meta-lib", ] +[[package]] +name = "android-tzdata" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e999941b234f3131b00bc13c22d06e8c5ff726d1b6318ac7eb276997bbb4fef0" + +[[package]] +name = "android_system_properties" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311" +dependencies = [ + "libc", +] + [[package]] name = "anstream" version = "0.6.14" @@ -130,9 +145,9 @@ dependencies = [ [[package]] name = "anstyle-query" -version = "1.0.3" +version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a64c907d4e79225ac72e2a354c9ce84d50ebb4586dee56c82b3ee73004f537f5" +checksum = "ad186efb764318d35165f1758e7dcef3b10628e26d41a44bc5550652e6804391" dependencies = [ "windows-sys 0.52.0", ] @@ -182,13 +197,13 @@ checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0" [[package]] name = "backtrace" -version = "0.3.72" +version = "0.3.73" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "17c6a35df3749d2e8bb1b7b21a976d82b15548788d2735b9d82f329268f71a11" +checksum = "5cc23269a4f8976d0a4d2e7109211a419fe30e8d88d677cd60b6bc79c5732e0a" dependencies = [ "addr2line", "cc", - "cfg-if", + "cfg-if 1.0.0", "libc", "miniz_oxide", "object", @@ -370,6 +385,12 @@ version = "3.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c" +[[package]] +name = "bytemuck" +version = "1.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78834c15cb5d5efe3452d58b1e8ba890dd62d21907f867f383358198e56ebca5" + [[package]] name = "byteorder" version = "1.5.0" @@ -384,9 +405,15 @@ checksum = "514de17de45fdb8dc022b1a7975556c53c86f9f0aa5f534b98977b171857c2c9" [[package]] name = "cc" -version = "1.0.98" +version = "1.0.99" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96c51067fd44124faa7f870b4b1c969379ad32b2ba805aa959430ceaa384f695" + +[[package]] +name = "cfg-if" +version = "0.1.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "41c270e7540d725e65ac7f1b212ac8ce349719624d7bcff99f8e2e488e8cf03f" +checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" [[package]] name = "cfg-if" @@ -429,11 +456,25 @@ dependencies = [ "multiversx-sc-meta-lib", ] +[[package]] +name = "chrono" +version = "0.4.38" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a21f936df1771bf62b77f047b726c4625ff2e8aa607c01ec06e5a05bd8463401" +dependencies = [ + "android-tzdata", + "iana-time-zone", + "js-sys", + "num-traits", + "wasm-bindgen", + "windows-targets 0.52.5", +] + [[package]] name = "clap" -version = "4.5.4" +version = "4.5.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "90bc066a67923782aa8515dbaea16946c5bcc5addbd668bb80af688e53e548a0" +checksum = "5db83dced34638ad474f39f250d7fea9598bdd239eaced1bdf45d597da0f433f" dependencies = [ "clap_builder", "clap_derive", @@ -441,9 +482,9 @@ dependencies = [ [[package]] name = "clap_builder" -version = "4.5.2" +version = "4.5.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ae129e2e766ae0ec03484e609954119f123cc1fe650337e155d03b022f24f7b4" +checksum = "f7e204572485eb3fbf28f871612191521df159bc3e15a9f5064c66dba3a8c05f" dependencies = [ "anstream", "anstyle", @@ -453,9 +494,9 @@ dependencies = [ [[package]] name = "clap_derive" -version = "4.5.4" +version = "4.5.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "528131438037fd55894f62d6e9f068b8f45ac57ffa77517819645d10aed04f64" +checksum = "c780290ccf4fb26629baa7a1081e68ced113f1d3ec302fa5948f1c381ebf06c6" dependencies = [ "heck", "proc-macro2", @@ -465,9 +506,15 @@ dependencies = [ [[package]] name = "clap_lex" -version = "0.7.0" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4b82cf0babdbd58558212896d1a4272303a57bdb245c2bf1147185fb45640e70" + +[[package]] +name = "color_quant" +version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "98cc8fbded0c607b7ba9dd60cd98df59af97e84d24e49c8557331cfc26d301ce" +checksum = "3d7b894f5411737b7867f4827955924d7c254fc9f4d91a6aad6b097804b1018b" [[package]] name = "colorchoice" @@ -543,6 +590,42 @@ version = "0.8.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "06ea2b9bc92be3c2baa9334a323ebca2d6f074ff852cd1d7b11064035cd3868f" +[[package]] +name = "core-graphics" +version = "0.23.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c07782be35f9e1140080c6b96f0d44b739e2278479f64e02fdab4e32dfd8b081" +dependencies = [ + "bitflags 1.3.2", + "core-foundation", + "core-graphics-types", + "foreign-types 0.5.0", + "libc", +] + +[[package]] +name = "core-graphics-types" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "45390e6114f68f718cc7a830514a96f903cccd70d02a8f6d9f643ac4ba45afaf" +dependencies = [ + "bitflags 1.3.2", + "core-foundation", + "libc", +] + +[[package]] +name = "core-text" +version = "20.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c9d2790b5c08465d49f8dc05c8bcae9fea467855947db39b0f8145c091aaced5" +dependencies = [ + "core-foundation", + "core-graphics", + "foreign-types 0.5.0", + "libc", +] + [[package]] name = "cpufeatures" version = "0.2.12" @@ -558,7 +641,7 @@ version = "1.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a97769d94ddab943e4510d138150169a2758b5ef3eb191a9ee688de3e23ef7b3" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", ] [[package]] @@ -664,6 +747,16 @@ dependencies = [ "multiversx-sc-meta-lib", ] +[[package]] +name = "cstr" +version = "0.2.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68523903c8ae5aacfa32a0d9ae60cadeb764e1da14ee0d26b1f3089f13a54636" +dependencies = [ + "proc-macro2", + "quote", +] + [[package]] name = "curve25519-dalek" version = "3.2.0" @@ -724,6 +817,27 @@ dependencies = [ "multiversx-sc-meta-lib", ] +[[package]] +name = "dirs-next" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b98cf8ebf19c3d1b223e151f99a4f9f0690dca41414773390fc824184ac833e1" +dependencies = [ + "cfg-if 1.0.0", + "dirs-sys-next", +] + +[[package]] +name = "dirs-sys-next" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ebda144c4fe02d1f7ea1a7d9641b6fc6b580adcfa024ae48797ecdeb6825b4d" +dependencies = [ + "libc", + "redox_users", + "winapi", +] + [[package]] name = "displaydoc" version = "0.2.4" @@ -735,6 +849,27 @@ dependencies = [ "syn", ] +[[package]] +name = "dlib" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "330c60081dcc4c72131f8eb70510f1ac07223e5d4163db481a04a0befcffa412" +dependencies = [ + "libloading", +] + +[[package]] +name = "dwrote" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "439a1c2ba5611ad3ed731280541d36d2e9c4ac5e7fb818a27b604bdc5a6aa65b" +dependencies = [ + "lazy_static", + "libc", + "winapi", + "wio", +] + [[package]] name = "ed25519" version = "1.5.3" @@ -787,7 +922,7 @@ version = "0.8.34" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b45de904aa0b010bce2ab45264d0631681847fa7b6f2eaa7dab7619943bc4f59" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", ] [[package]] @@ -1005,6 +1140,15 @@ version = "2.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9fc0510504f03c51ada170672ac806f1f105a88aa97a5281117e1ddc3368e51a" +[[package]] +name = "fdeflate" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4f9bfee30e4dedf0ab8b422f03af778d9612b63f502710fc500a334ebe2de645" +dependencies = [ + "simd-adler32", +] + [[package]] name = "first-contract" version = "0.0.0" @@ -1032,19 +1176,71 @@ dependencies = [ "miniz_oxide", ] +[[package]] +name = "float-ord" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ce81f49ae8a0482e4c55ea62ebbd7e5a686af544c00b9d090bba3ff9be97b3d" + [[package]] name = "fnv" version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" +[[package]] +name = "font-kit" +version = "0.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2845a73bbd781e691ab7c2a028c579727cd254942e8ced57ff73e0eafd60de87" +dependencies = [ + "bitflags 2.5.0", + "byteorder", + "core-foundation", + "core-graphics", + "core-text", + "dirs-next", + "dwrote", + "float-ord", + "freetype-sys", + "lazy_static", + "libc", + "log", + "pathfinder_geometry", + "pathfinder_simd", + "walkdir", + "winapi", + "yeslogic-fontconfig-sys", +] + [[package]] name = "foreign-types" version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f6f339eb8adc052cd2ca78910fda869aefa38d22d5cb648e6485e4d3fc06f3b1" dependencies = [ - "foreign-types-shared", + "foreign-types-shared 0.1.1", +] + +[[package]] +name = "foreign-types" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d737d9aa519fb7b749cbc3b962edcf310a8dd1f4b67c91c4f83975dbdd17d965" +dependencies = [ + "foreign-types-macros", + "foreign-types-shared 0.3.1", +] + +[[package]] +name = "foreign-types-macros" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a5c6c585bc94aaf2c7b51dd4c2ba22680844aba4c687be581871a6f518c5742" +dependencies = [ + "proc-macro2", + "quote", + "syn", ] [[package]] @@ -1053,6 +1249,12 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b" +[[package]] +name = "foreign-types-shared" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa9a19cbb55df58761df49b23516a86d432839add4af60fc256da840f66ed35b" + [[package]] name = "form_urlencoded" version = "1.2.1" @@ -1161,6 +1363,17 @@ dependencies = [ "multiversx-sc-meta-lib", ] +[[package]] +name = "freetype-sys" +version = "0.20.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e7edc5b9669349acfda99533e9e0bcf26a51862ab43b08ee7745c55d28eb134" +dependencies = [ + "cc", + "libc", + "pkg-config", +] + [[package]] name = "futures" version = "0.3.30" @@ -1266,7 +1479,7 @@ version = "0.1.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8fc3cb4d91f53b50155bdcfd23f6a4c39ae1969c2ae85982b135750cccaf5fce" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "libc", "wasi 0.9.0+wasi-snapshot-preview1", ] @@ -1277,13 +1490,23 @@ version = "0.2.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "js-sys", "libc", "wasi 0.11.0+wasi-snapshot-preview1", "wasm-bindgen", ] +[[package]] +name = "gif" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "80792593675e051cf94a4b111980da2ba60d4a83e43e0048c5693baab3977045" +dependencies = [ + "color_quant", + "weezl", +] + [[package]] name = "gimli" version = "0.29.0" @@ -1388,12 +1611,12 @@ dependencies = [ [[package]] name = "http-body-util" -version = "0.1.1" +version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0475f8b2ac86659c21b64320d5d653f9efe42acd2a4e560073ec61a155a34f1d" +checksum = "793429d76616a256bcb62c2a2ec2bed781c8307e797e2598c50010f2bee2544f" dependencies = [ "bytes", - "futures-core", + "futures-util", "http", "http-body", "pin-project-lite", @@ -1401,9 +1624,9 @@ dependencies = [ [[package]] name = "httparse" -version = "1.8.0" +version = "1.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d897f394bad6a705d5f4104762e116a75639e470d80901eed05a860a95cb1904" +checksum = "0fcc0b4a115bf80b728eb8ea024ad5bd707b615bfed49e0665b6e0f86fd082d9" [[package]] name = "humantime" @@ -1431,6 +1654,23 @@ dependencies = [ "want", ] +[[package]] +name = "hyper-rustls" +version = "0.27.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ee4be2c948921a1a5320b629c4193916ed787a7f7f293fd3f7f5a6c9de74155" +dependencies = [ + "futures-util", + "http", + "hyper", + "hyper-util", + "rustls", + "rustls-pki-types", + "tokio", + "tokio-rustls", + "tower-service", +] + [[package]] name = "hyper-tls" version = "0.6.0" @@ -1467,14 +1707,157 @@ dependencies = [ "tracing", ] +[[package]] +name = "iana-time-zone" +version = "0.1.60" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7ffbb5a1b541ea2561f8c41c087286cc091e21e556a4f09a8f6cbf17b69b141" +dependencies = [ + "android_system_properties", + "core-foundation-sys", + "iana-time-zone-haiku", + "js-sys", + "wasm-bindgen", + "windows-core", +] + +[[package]] +name = "iana-time-zone-haiku" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f" +dependencies = [ + "cc", +] + +[[package]] +name = "icu_collections" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db2fa452206ebee18c4b5c2274dbf1de17008e874b4dc4f0aea9d01ca79e4526" +dependencies = [ + "displaydoc", + "yoke", + "zerofrom", + "zerovec", +] + +[[package]] +name = "icu_locid" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "13acbb8371917fc971be86fc8057c41a64b521c184808a698c02acc242dbf637" +dependencies = [ + "displaydoc", + "litemap", + "tinystr", + "writeable", + "zerovec", +] + +[[package]] +name = "icu_locid_transform" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "01d11ac35de8e40fdeda00d9e1e9d92525f3f9d887cdd7aa81d727596788b54e" +dependencies = [ + "displaydoc", + "icu_locid", + "icu_locid_transform_data", + "icu_provider", + "tinystr", + "zerovec", +] + +[[package]] +name = "icu_locid_transform_data" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fdc8ff3388f852bede6b579ad4e978ab004f139284d7b28715f773507b946f6e" + +[[package]] +name = "icu_normalizer" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "19ce3e0da2ec68599d193c93d088142efd7f9c5d6fc9b803774855747dc6a84f" +dependencies = [ + "displaydoc", + "icu_collections", + "icu_normalizer_data", + "icu_properties", + "icu_provider", + "smallvec", + "utf16_iter", + "utf8_iter", + "write16", + "zerovec", +] + +[[package]] +name = "icu_normalizer_data" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8cafbf7aa791e9b22bec55a167906f9e1215fd475cd22adfcf660e03e989516" + +[[package]] +name = "icu_properties" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f8ac670d7422d7f76b32e17a5db556510825b29ec9154f235977c9caba61036" +dependencies = [ + "displaydoc", + "icu_collections", + "icu_locid_transform", + "icu_properties_data", + "icu_provider", + "tinystr", + "zerovec", +] + +[[package]] +name = "icu_properties_data" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67a8effbc3dd3e4ba1afa8ad918d5684b8868b3b26500753effea8d2eed19569" + +[[package]] +name = "icu_provider" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ed421c8a8ef78d3e2dbc98a973be2f3770cb42b606e3ab18d6237c4dfde68d9" +dependencies = [ + "displaydoc", + "icu_locid", + "icu_provider_macros", + "stable_deref_trait", + "tinystr", + "writeable", + "yoke", + "zerofrom", + "zerovec", +] + +[[package]] +name = "icu_provider_macros" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ec89e9337638ecdc08744df490b221a7399bf8d164eb52a665454e60e075ad6" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "idna" -version = "0.5.0" +version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "634d9b1461af396cad843f47fdba5597a4f9e6ddd4bfb6ff5d85028c25cb12f6" +checksum = "4716a3a0933a1d01c2f72450e89596eb51dd34ef3c211ccd875acdf1f8fe47ed" dependencies = [ - "unicode-bidi", - "unicode-normalization", + "icu_normalizer", + "icu_properties", + "smallvec", + "utf8_iter", ] [[package]] @@ -1493,6 +1876,20 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "image" +version = "0.24.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5690139d2f55868e080017335e4b94cb7414274c74f1669c84fb5feba2c9f69d" +dependencies = [ + "bytemuck", + "byteorder", + "color_quant", + "jpeg-decoder", + "num-traits", + "png", +] + [[package]] name = "indexmap" version = "2.2.6" @@ -1542,6 +1939,12 @@ version = "1.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" +[[package]] +name = "jpeg-decoder" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f5d4a7da358eff58addd2877a45865158f0d78c911d43a5784ceb7bbf52833b0" + [[package]] name = "js-sys" version = "0.3.69" @@ -1646,10 +2049,30 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] -name = "libc" -version = "0.2.155" +name = "libc" +version = "0.2.155" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c" + +[[package]] +name = "libloading" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c2a198fb6b0eada2a8df47933734e6d35d350665a33a3593d7164fa52c75c19" +dependencies = [ + "cfg-if 1.0.0", + "windows-targets 0.52.5", +] + +[[package]] +name = "libredox" +version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c" +checksum = "c0ff37bd590ca25063e35af745c343cb7a0271906fb7b37e4813e8f79f00268d" +dependencies = [ + "bitflags 2.5.0", + "libc", +] [[package]] name = "linked-list-repeat" @@ -1674,6 +2097,12 @@ version = "0.4.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "78b3ae25bc7c8c38cec158d1f2757ee79e9b3740fbc7ccf0e59e4b08d793fa89" +[[package]] +name = "litemap" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "643cb0b8d4fcc284004d5fd0d67ccf61dfffadb7f75e1e71bc420f4688a3a704" + [[package]] name = "local-esdt-and-nft" version = "0.0.0" @@ -1781,9 +2210,15 @@ dependencies = [ [[package]] name = "memchr" -version = "2.7.2" +version = "2.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" + +[[package]] +name = "memory_units" +version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6c8640c5d730cb13ebd907d8d04b52f55ac9a2eec55b440c8892f40d56c76c1d" +checksum = "8452105ba047068f40ff7093dd1d9da90898e63dd61736462e9cdda6a90ad3c3" [[package]] name = "mime" @@ -1798,6 +2233,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "87dfd01fe195c66b572b37921ad8803d010623c0aca821bea2302239d155cdae" dependencies = [ "adler", + "simd-adler32", ] [[package]] @@ -2234,9 +2670,9 @@ dependencies = [ [[package]] name = "object" -version = "0.35.0" +version = "0.36.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b8ec7ab813848ba4522158d5517a6093db1ded27575b070f4177b8d12b41db5e" +checksum = "576dfe1fc8f9df304abb159d767a29d0476f7750fbf8aa7ad07816004a207434" dependencies = [ "memchr", ] @@ -2260,8 +2696,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "95a0481286a310808298130d22dd1fef0fa571e05a8f44ec801801e84b216b1f" dependencies = [ "bitflags 2.5.0", - "cfg-if", - "foreign-types", + "cfg-if 1.0.0", + "foreign-types 0.3.2", "libc", "once_cell", "openssl-macros", @@ -2370,7 +2806,7 @@ version = "0.9.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1e401f977ab385c9e4e3ab30627d6f26d00e2c73eef317493c4ec6d468726cf8" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "libc", "redox_syscall", "smallvec", @@ -2383,6 +2819,25 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8835116a5c179084a830efb3adc117ab007512b535bc1a21c991d3b32a6b44dd" +[[package]] +name = "pathfinder_geometry" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b7b7e7b4ea703700ce73ebf128e1450eb69c3a8329199ffbfb9b2a0418e5ad3" +dependencies = [ + "log", + "pathfinder_simd", +] + +[[package]] +name = "pathfinder_simd" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ebf45976c56919841273f2a0fc684c28437e2f304e264557d9c72be5d5a718be" +dependencies = [ + "rustc_version", +] + [[package]] name = "payable-features" version = "0.0.0" @@ -2482,6 +2937,77 @@ version = "0.3.30" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d231b230927b5e4ad203db57bbcbee2802f6bce620b1e4a9024a07d94e2907ec" +[[package]] +name = "plotters" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a15b6eccb8484002195a3e44fe65a4ce8e93a625797a063735536fd59cb01cf3" +dependencies = [ + "chrono", + "font-kit", + "image", + "lazy_static", + "num-traits", + "pathfinder_geometry", + "plotters-backend", + "plotters-bitmap", + "plotters-svg", + "ttf-parser", + "wasm-bindgen", + "web-sys", +] + +[[package]] +name = "plotters-backend" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "414cec62c6634ae900ea1c56128dfe87cf63e7caece0852ec76aba307cebadb7" + +[[package]] +name = "plotters-bitmap" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f7e7f6fb8302456d7c264a94dada86f76d76e1a03e2294ee86ca7da92983b0a6" +dependencies = [ + "gif", + "image", + "plotters-backend", +] + +[[package]] +name = "plotters-canvas" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "498a82bf654581fa4df6c1bd73e56f8556bdeec5eca5a0989d170ca0728ccace" +dependencies = [ + "js-sys", + "plotters-backend", + "wasm-bindgen", + "web-sys", +] + +[[package]] +name = "plotters-svg" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "81b30686a7d9c3e010b84284bdd26a29f2138574f52f5eb6f794fc0ad924e705" +dependencies = [ + "plotters-backend", +] + +[[package]] +name = "png" +version = "0.17.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06e4b0d3d1312775e782c86c91a111aa1f910cbb65e1337f9975b5f9a554b5e1" +dependencies = [ + "bitflags 1.3.2", + "crc32fast", + "fdeflate", + "flate2", + "miniz_oxide", +] + [[package]] name = "ppv-lite86" version = "0.2.17" @@ -2704,18 +3230,29 @@ dependencies = [ [[package]] name = "redox_syscall" -version = "0.5.1" +version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "469052894dcb553421e483e4209ee581a45100d31b4018de03e5a7ad86374a7e" +checksum = "c82cf8cff14456045f55ec4241383baeff27af886adb72ffb2162f99911de0fd" dependencies = [ "bitflags 2.5.0", ] +[[package]] +name = "redox_users" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bd283d9651eeda4b2a83a43c1c91b266c40fd76ecd39a50a8c630ae69dc72891" +dependencies = [ + "getrandom 0.2.15", + "libredox", + "thiserror", +] + [[package]] name = "regex" -version = "1.10.4" +version = "1.10.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c117dbdfde9c8308975b6a18d71f3f385c89461f7b3fb054288ecf2a2058ba4c" +checksum = "b91213439dad192326a0d7c6ee3955910425f441d7038e0d6933b0aec5c4517f" dependencies = [ "aho-corasick", "memchr", @@ -2725,9 +3262,9 @@ dependencies = [ [[package]] name = "regex-automata" -version = "0.4.6" +version = "0.4.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "86b83b8b9847f9bf95ef68afb0b8e6cdb80f498442f5179a29fad448fcc1eaea" +checksum = "38caf58cc5ef2fed281f89292ef23f6365465ed9a41b7a7754eb4e26496c92df" dependencies = [ "aho-corasick", "memchr", @@ -2736,15 +3273,15 @@ dependencies = [ [[package]] name = "regex-syntax" -version = "0.8.3" +version = "0.8.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "adad44e29e4c806119491a7f06f03de4d1af22c3a680dd47f1e6e179439d1f56" +checksum = "7a66a03ae7c801facd77a29370b4faec201768915ac14a721ba36f20bc9c209b" [[package]] name = "reqwest" -version = "0.12.4" +version = "0.12.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "566cafdd92868e0939d3fb961bd0dc25fcfaaed179291093b3d43e6b3150ea10" +checksum = "c7d6d2a27d57148378eb5e111173f4276ad26340ecc5c49a4a2152167a2d6a37" dependencies = [ "base64", "bytes", @@ -2757,6 +3294,7 @@ dependencies = [ "http-body", "http-body-util", "hyper", + "hyper-rustls", "hyper-tls", "hyper-util", "ipnet", @@ -2800,6 +3338,21 @@ dependencies = [ "rewards-distribution", ] +[[package]] +name = "ring" +version = "0.17.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c17fa4cb658e3583423e915b9f3acc01cceaee1860e33d59ebae66adc3a2dc0d" +dependencies = [ + "cc", + "cfg-if 1.0.0", + "getrandom 0.2.15", + "libc", + "spin", + "untrusted", + "windows-sys 0.52.0", +] + [[package]] name = "ruplacer" version = "0.8.2" @@ -2881,6 +3434,19 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "rustls" +version = "0.23.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05cff451f60db80f490f3c182b77c35260baace73209e9cdbbe526bfe3a4d402" +dependencies = [ + "once_cell", + "rustls-pki-types", + "rustls-webpki", + "subtle", + "zeroize", +] + [[package]] name = "rustls-pemfile" version = "2.1.2" @@ -2897,6 +3463,17 @@ version = "1.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "976295e77ce332211c0d24d92c0e83e50f5c5f046d11082cea19f3df13a3562d" +[[package]] +name = "rustls-webpki" +version = "0.102.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff448f7e92e913c4b7d4c6d8e4540a1724b319b4152b8aef6d4cf8339712b33e" +dependencies = [ + "ring", + "rustls-pki-types", + "untrusted", +] + [[package]] name = "ryu" version = "1.0.18" @@ -3110,7 +3687,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4d58a1e1bf39749807d89cf2d98ac2dfa0ff1cb3faa38fbb64dd88ac8013d800" dependencies = [ "block-buffer 0.9.0", - "cfg-if", + "cfg-if 1.0.0", "cpufeatures", "digest 0.9.0", "opaque-debug", @@ -3122,7 +3699,7 @@ version = "0.10.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "793db75ad2bcafc3ffa7c68b215fee268f537982cd901d132f89c6343f3a3dc8" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "cpufeatures", "digest 0.10.7", ] @@ -3200,6 +3777,18 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "spin" +version = "0.9.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67" + +[[package]] +name = "stable_deref_trait" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3" + [[package]] name = "str-repeat" version = "0.0.0" @@ -3241,9 +3830,20 @@ dependencies = [ [[package]] name = "sync_wrapper" -version = "0.1.2" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7065abeca94b6a8a577f9bd45aa0867a2238b74e8eb67cf10d492bc39351394" + +[[package]] +name = "synstructure" +version = "0.13.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2047c6ded9c721764247e62cd3b03c09ffc529b2ba5b10ec482ae507a4a70160" +checksum = "c8af7666ab7b6390ab78131fb5b0fce11d6b7a6951602017c35fa82800708971" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] [[package]] name = "system-configuration" @@ -3272,7 +3872,7 @@ version = "3.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "85b77fafb263dd9d05cbeac119526425676db3784113aa9295c88498cbf8bff1" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "fastrand", "rustix", "windows-sys 0.52.0", @@ -3298,6 +3898,16 @@ dependencies = [ "syn", ] +[[package]] +name = "tinystr" +version = "0.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9117f5d4db391c1cf6927e7bea3db74b9a1c1add8f7eda9ffd5364f40f57b82f" +dependencies = [ + "displaydoc", + "zerovec", +] + [[package]] name = "tinyvec" version = "1.6.0" @@ -3331,9 +3941,9 @@ dependencies = [ [[package]] name = "tokio" -version = "1.37.0" +version = "1.38.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1adbebffeca75fcfd058afa480fb6c0b81e165a0323f9c9d39c9697e37c46787" +checksum = "ba4f4a02a7a80d6f274636f0aa95c7e383b912d41fe721a31f29e29698585a4a" dependencies = [ "backtrace", "bytes", @@ -3350,9 +3960,9 @@ dependencies = [ [[package]] name = "tokio-macros" -version = "2.2.0" +version = "2.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5b8a1e28f2deaa14e508979454cb3a223b10b938b45af148bc0986de36f1923b" +checksum = "5f5ae998a069d4b5aba8ee9dad856af7d520c3699e6159b185c2acd48155d39a" dependencies = [ "proc-macro2", "quote", @@ -3369,6 +3979,17 @@ dependencies = [ "tokio", ] +[[package]] +name = "tokio-rustls" +version = "0.26.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c7bc40d0e5a97695bb96e27995cd3a08538541b0a846f65bba7a359f36700d4" +dependencies = [ + "rustls", + "rustls-pki-types", + "tokio", +] + [[package]] name = "tokio-util" version = "0.7.11" @@ -3384,9 +4005,9 @@ dependencies = [ [[package]] name = "toml" -version = "0.8.13" +version = "0.8.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a4e43f8cc456c9704c851ae29c67e17ef65d2c30017c17a9765b89c382dc8bba" +checksum = "6f49eb2ab21d2f26bd6db7bf383edc527a7ebaee412d17af4d40fdccd442f335" dependencies = [ "indexmap", "serde", @@ -3406,9 +4027,9 @@ dependencies = [ [[package]] name = "toml_edit" -version = "0.22.13" +version = "0.22.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c127785850e8c20836d49732ae6abfa47616e60bf9d9f57c43c250361a9db96c" +checksum = "f21c7aaf97f1bd9ca9d4f9e73b0a6c74bd5afef56f2bc931943a6e1c37e04e38" dependencies = [ "indexmap", "serde", @@ -3488,16 +4109,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b" [[package]] -name = "typenum" -version = "1.17.0" +name = "ttf-parser" +version = "0.20.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825" +checksum = "17f77d76d837a7830fe1d4f12b7b4ba4192c1888001c7164257e4bc6d21d96b4" [[package]] -name = "unicode-bidi" -version = "0.3.15" +name = "typenum" +version = "1.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "08f95100a766bf4f8f28f90d77e0a5461bbdb219042e7679bebe79004fed8d75" +checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825" [[package]] name = "unicode-ident" @@ -3520,6 +4141,12 @@ version = "1.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d4c87d22b6e3f4a18d4d40ef354e97c90fcb14dd91d7dc0aa9d8a1172ebf7202" +[[package]] +name = "untrusted" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1" + [[package]] name = "unwrap-infallible" version = "0.1.5" @@ -3528,9 +4155,9 @@ checksum = "151ac09978d3c2862c4e39b557f4eceee2cc72150bc4cb4f16abf061b6e381fb" [[package]] name = "url" -version = "2.5.0" +version = "2.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "31e6302e3bb753d46e83516cae55ae196fc0c309407cf11ab35cc51a4c2a4633" +checksum = "f7c25da092f0a868cdf09e8674cd3b7ef3a7d92a24253e663a2fb85e2496de56" dependencies = [ "form_urlencoded", "idna", @@ -3555,11 +4182,23 @@ dependencies = [ "use-module", ] +[[package]] +name = "utf16_iter" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c8232dd3cdaed5356e0f716d285e4b40b932ac434100fe9b7e0e8e935b9e6246" + +[[package]] +name = "utf8_iter" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be" + [[package]] name = "utf8parse" -version = "0.2.1" +version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" [[package]] name = "vault" @@ -3643,7 +4282,7 @@ version = "0.2.92" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4be2531df63900aeb2bca0daaaddec08491ee64ceecbee5076636a3b026795a8" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "wasm-bindgen-macro", ] @@ -3668,7 +4307,7 @@ version = "0.4.42" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "76bc14366121efc8dbb487ab05bcc9d346b3b5ec0eaa76e46594cabbe51762c0" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "js-sys", "wasm-bindgen", "web-sys", @@ -3703,6 +4342,17 @@ version = "0.2.92" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "af190c94f2773fdb3729c55b007a722abb5384da03bc0986df4c289bf5567e96" +[[package]] +name = "wasm-demo" +version = "0.1.0" +dependencies = [ + "plotters", + "plotters-canvas", + "wasm-bindgen", + "web-sys", + "wee_alloc", +] + [[package]] name = "wasmparser" version = "0.208.1" @@ -3737,6 +4387,40 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "wee_alloc" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dbb3b5a6b2bb17cb6ad44a2e68a43e8d2722c997da10e928665c72ec6c0a0b8e" +dependencies = [ + "cfg-if 0.1.10", + "libc", + "memory_units", + "winapi", +] + +[[package]] +name = "weezl" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53a85b86a771b1c87058196170769dd264f66c0782acf1ae6cc51bfd64b39082" + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + [[package]] name = "winapi-util" version = "0.1.8" @@ -3746,6 +4430,21 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + +[[package]] +name = "windows-core" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33ab640c8d7e35bf8ba19b884ba838ceb4fba93a4e8c65a9059d08afcfc683d9" +dependencies = [ + "windows-targets 0.52.5", +] + [[package]] name = "windows-sys" version = "0.48.0" @@ -3887,9 +4586,9 @@ checksum = "bec47e5bfd1bff0eeaf6d8b485cc1074891a197ab4225d504cb7a1ab88b02bf0" [[package]] name = "winnow" -version = "0.6.9" +version = "0.6.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "86c949fede1d13936a99f14fafd3e76fd642b556dd2ce96287fbe2e0151bfac6" +checksum = "59b5e5f6c299a3c7890b876a2a587f3115162487e704907d9b6cd29473052ba1" dependencies = [ "memchr", ] @@ -3900,10 +4599,67 @@ version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a277a57398d4bfa075df44f501a17cfdf8542d224f0d36095a2adc7aee4ef0a5" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "windows-sys 0.48.0", ] +[[package]] +name = "wio" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d129932f4644ac2396cb456385cbf9e63b5b30c6e8dc4820bdca4eb082037a5" +dependencies = [ + "winapi", +] + +[[package]] +name = "write16" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d1890f4022759daae28ed4fe62859b1236caebfc61ede2f63ed4e695f3f6d936" + +[[package]] +name = "writeable" +version = "0.5.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e9df38ee2d2c3c5948ea468a8406ff0db0b29ae1ffde1bcf20ef305bcc95c51" + +[[package]] +name = "yeslogic-fontconfig-sys" +version = "5.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ffb6b23999a8b1a997bf47c7bb4d19ad4029c3327bb3386ebe0a5ff584b33c7a" +dependencies = [ + "cstr", + "dlib", + "once_cell", + "pkg-config", +] + +[[package]] +name = "yoke" +version = "0.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c5b1314b079b0930c31e3af543d8ee1757b1951ae1e1565ec704403a7240ca5" +dependencies = [ + "serde", + "stable_deref_trait", + "yoke-derive", + "zerofrom", +] + +[[package]] +name = "yoke-derive" +version = "0.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "28cc31741b18cb6f1d5ff12f5b7523e3d6eb0852bbbad19d73905511d9849b95" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "synstructure", +] + [[package]] name = "zerocopy" version = "0.7.34" @@ -3924,6 +4680,27 @@ dependencies = [ "syn", ] +[[package]] +name = "zerofrom" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "91ec111ce797d0e0784a1116d0ddcdbea84322cd79e5d5ad173daeba4f93ab55" +dependencies = [ + "zerofrom-derive", +] + +[[package]] +name = "zerofrom-derive" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ea7b4a3637ea8669cedf0f1fd5c286a17f3de97b8dd5a70a6c167a1730e63a5" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "synstructure", +] + [[package]] name = "zeroize" version = "1.8.1" @@ -3944,11 +4721,33 @@ dependencies = [ "syn", ] +[[package]] +name = "zerovec" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb2cc8827d6c0994478a15c53f374f46fbd41bea663d809b14744bc42e6b109c" +dependencies = [ + "yoke", + "zerofrom", + "zerovec-derive", +] + +[[package]] +name = "zerovec-derive" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "97cf56601ee5052b4417d90c8755c6683473c926039908196cf35d99f893ebe7" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "zip" -version = "2.1.1" +version = "2.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1dd56a4d5921bc2f99947ac5b3abe5f510b1be7376fdc5e9fce4a23c6a93e87c" +checksum = "775a2b471036342aa69bc5a602bc889cb0a06cda00477d0c69566757d5553d39" dependencies = [ "arbitrary", "crc32fast", diff --git a/Cargo.toml b/Cargo.toml index 345b8670a5..5f94cec44e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,6 +18,7 @@ members = [ "tools/mxpy-snippet-generator", "tools/payload-macro-generator", + "tools/plotter", "vm", diff --git a/tools/plotter/.gitignore b/tools/plotter/.gitignore new file mode 100644 index 0000000000..769dfbb4ba --- /dev/null +++ b/tools/plotter/.gitignore @@ -0,0 +1,3 @@ +/target +.*.sw* +.vscode/* diff --git a/tools/plotter/Cargo.toml b/tools/plotter/Cargo.toml new file mode 100644 index 0000000000..f936cadd16 --- /dev/null +++ b/tools/plotter/Cargo.toml @@ -0,0 +1,18 @@ +[package] +name = "wasm-demo" +version = "0.1.0" +authors = ["Hao Hou "] +edition = "2018" + +[lib] +crate-type=["cdylib"] + +[dependencies] +plotters = "^0.3.2" +wasm-bindgen = "0.2.78" +wee_alloc = "0.4.5" +web-sys = { version = "0.3.39", features = ["HtmlCanvasElement"] } +plotters-canvas = "^0.3.0" + +[profile.release] +lto = true diff --git a/tools/plotter/README.md b/tools/plotter/README.md new file mode 100644 index 0000000000..cfe2983da2 --- /dev/null +++ b/tools/plotter/README.md @@ -0,0 +1,54 @@ +# Example Project of Plotters + WASM + +This is a minimal project that uses Plotters in WASM application. + +- For more information about Plotters project, check our [core repository](https://github.com/plotters-rs/plotters). + +- This demo has been deployed at this [link](https://plotters-rs.github.io/wasm-demo/www/index.html). + +## Try this example locally + +To build the demo you need [wasm-pack](https://rustwasm.github.io/docs/book/game-of-life/setup.html). + +Then you can run it locally either using `npm` and `webpack-dev-server` or +just with static web server. + +The following script will install needed software and run the server via `npm`. +``` +./start-server.sh +``` + +For Windows users without Bash, `start-server.bat` can be used to +launch the server. + +``` +start-server.bat +``` + +## Developing with NPM +Please use [rust-wasm guide](https://rustwasm.github.io/docs/book/game-of-life/setup.html) for initial setup . +Then you can run the demo locally using `npm`: +```bash +wasm-pack build +cd www +npm install +npm start +``` + +This will start a dev server which will automatically reload your page +whenever you change anything in `www` directory. To update `rust` code +call `wasm-pack build` manually. + +## Developing without dependenices +If you don't want to use `npm` here's how you can run the example +using any web server. We are using rust [basic-http-server](https://github.com/brson/basic-http-server), but +any web server will do. + +```bash +# Install web server (instead you can use your local nginx for example) +cargo install basic-http-server +wasm-pack build --target web # Note `--target web` +basic-http-server +``` + +Then open http://127.0.0.1:4000/www diff --git a/tools/plotter/src/func_plot.rs b/tools/plotter/src/func_plot.rs new file mode 100644 index 0000000000..054452f08b --- /dev/null +++ b/tools/plotter/src/func_plot.rs @@ -0,0 +1,31 @@ +use crate::DrawResult; +use plotters::prelude::*; +use plotters_canvas::CanvasBackend; + +/// Draw power function f(x) = x^power. +pub fn draw(canvas_id: &str, power: i32) -> DrawResult Option<(f32, f32)>> { + let backend = CanvasBackend::new(canvas_id).expect("cannot find canvas"); + let root = backend.into_drawing_area(); + let font: FontDesc = ("sans-serif", 20.0).into(); + + root.fill(&WHITE)?; + + let mut chart = ChartBuilder::on(&root) + .margin(20u32) + .caption(format!("y=x^{}", power), font) + .x_label_area_size(30u32) + .y_label_area_size(30u32) + .build_cartesian_2d(-1f32..1f32, -1.2f32..1.2f32)?; + + chart.configure_mesh().x_labels(3).y_labels(3).draw()?; + + chart.draw_series(LineSeries::new( + (-50..=50) + .map(|x| x as f32 / 50.0) + .map(|x| (x, x.powf(power as f32))), + &RED, + ))?; + + root.present()?; + return Ok(chart.into_coord_trans()); +} diff --git a/tools/plotter/src/lib.rs b/tools/plotter/src/lib.rs new file mode 100644 index 0000000000..99b270cb73 --- /dev/null +++ b/tools/plotter/src/lib.rs @@ -0,0 +1,58 @@ +use wasm_bindgen::prelude::*; +use web_sys::HtmlCanvasElement; + +mod func_plot; +mod mandelbrot; +mod plot3d; + +#[global_allocator] +static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT; + +/// Type alias for the result of a drawing function. +pub type DrawResult = Result>; + +/// Type used on the JS side to convert screen coordinates to chart +/// coordinates. +#[wasm_bindgen] +pub struct Chart { + convert: Box Option<(f64, f64)>>, +} + +/// Result of screen to chart coordinates conversion. +#[wasm_bindgen] +pub struct Point { + pub x: f64, + pub y: f64, +} + +#[wasm_bindgen] +impl Chart { + /// Draw provided power function on the canvas element using it's id. + /// Return `Chart` struct suitable for coordinate conversion. + pub fn power(canvas_id: &str, power: i32) -> Result { + let map_coord = func_plot::draw(canvas_id, power).map_err(|err| err.to_string())?; + Ok(Chart { + convert: Box::new(move |coord| map_coord(coord).map(|(x, y)| (x.into(), y.into()))), + }) + } + + /// Draw Mandelbrot set on the provided canvas element. + /// Return `Chart` struct suitable for coordinate conversion. + pub fn mandelbrot(canvas: HtmlCanvasElement) -> Result { + let map_coord = mandelbrot::draw(canvas).map_err(|err| err.to_string())?; + Ok(Chart { + convert: Box::new(map_coord), + }) + } + + pub fn plot3d(canvas: HtmlCanvasElement, pitch: f64, yaw: f64) -> Result<(), JsValue> { + plot3d::draw(canvas, pitch, yaw).map_err(|err| err.to_string())?; + Ok(()) + } + + /// This function can be used to convert screen coordinates to + /// chart coordinates. + pub fn coord(&self, x: i32, y: i32) -> Option { + (self.convert)((x, y)).map(|(x, y)| Point { x, y }) + } +} diff --git a/tools/plotter/src/mandelbrot.rs b/tools/plotter/src/mandelbrot.rs new file mode 100644 index 0000000000..6c6bc9ae8a --- /dev/null +++ b/tools/plotter/src/mandelbrot.rs @@ -0,0 +1,67 @@ +use crate::DrawResult; +use plotters::prelude::*; +use plotters_canvas::CanvasBackend; +use std::ops::Range; +use web_sys::HtmlCanvasElement; + +/// Draw Mandelbrot set +pub fn draw(element: HtmlCanvasElement) -> DrawResult Option<(f64, f64)>> { + let backend = CanvasBackend::with_canvas_object(element).unwrap(); + + let root = backend.into_drawing_area(); + root.fill(&WHITE)?; + + let mut chart = ChartBuilder::on(&root) + .margin(20) + .x_label_area_size(10) + .y_label_area_size(10) + .build_cartesian_2d(-2.1..0.6, -1.2..1.2)?; + + chart + .configure_mesh() + .disable_x_mesh() + .disable_y_mesh() + .draw()?; + + let plotting_area = chart.plotting_area(); + + let range = plotting_area.get_pixel_range(); + let (pw, ph) = (range.0.end - range.0.start, range.1.end - range.1.start); + let (xr, yr) = (chart.x_range(), chart.y_range()); + + for (x, y, c) in mandelbrot_set(xr, yr, (pw as usize, ph as usize), 100) { + if c != 100 { + plotting_area.draw_pixel((x, y), &HSLColor(c as f64 / 100.0, 1.0, 0.5))?; + } else { + plotting_area.draw_pixel((x, y), &BLACK)?; + } + } + + root.present()?; + return Ok(Box::new(chart.into_coord_trans())); +} + +fn mandelbrot_set( + real: Range, + complex: Range, + samples: (usize, usize), + max_iter: usize, +) -> impl Iterator { + let step = ( + (real.end - real.start) / samples.0 as f64, + (complex.end - complex.start) / samples.1 as f64, + ); + return (0..(samples.0 * samples.1)).map(move |k| { + let c = ( + real.start + step.0 * (k % samples.0) as f64, + complex.start + step.1 * (k / samples.0) as f64, + ); + let mut z = (0.0, 0.0); + let mut cnt = 0; + while cnt < max_iter && z.0 * z.0 + z.1 * z.1 <= 1e10 { + z = (z.0 * z.0 - z.1 * z.1 + c.0, 2.0 * z.0 * z.1 + c.1); + cnt += 1; + } + return (c.0, c.1, cnt); + }); +} diff --git a/tools/plotter/src/plot3d.rs b/tools/plotter/src/plot3d.rs new file mode 100644 index 0000000000..06d0190d19 --- /dev/null +++ b/tools/plotter/src/plot3d.rs @@ -0,0 +1,42 @@ +use crate::DrawResult; +use plotters::prelude::*; +use plotters_canvas::CanvasBackend; +use web_sys::HtmlCanvasElement; + +pub fn draw(canvas: HtmlCanvasElement, pitch: f64, yaw: f64) -> DrawResult<()> { + let area = CanvasBackend::with_canvas_object(canvas) + .unwrap() + .into_drawing_area(); + area.fill(&WHITE)?; + + let x_axis = (-3.0..3.0).step(0.1); + let z_axis = (-3.0..3.0).step(0.1); + + let mut chart = + ChartBuilder::on(&area).build_cartesian_3d(x_axis.clone(), -3.0..3.0, z_axis.clone())?; + + chart.with_projection(|mut pb| { + pb.yaw = yaw; + pb.pitch = pitch; + pb.scale = 0.7; + pb.into_matrix() + }); + + chart.configure_axes().draw()?; + + chart.draw_series( + SurfaceSeries::xoz(x_axis.values(), z_axis.values(), |x:f64, z:f64| { + (x * x + z * z).cos() + }) + .style(&BLUE.mix(0.2)), + )?; + + chart.draw_series(LineSeries::new( + (-100..100) + .map(|y| y as f64 / 40.0) + .map(|y| ((y * 10.0).sin(), y, (y * 10.0).cos())), + &BLACK, + ))?; + + Ok(()) +} diff --git a/tools/plotter/start-server.bat b/tools/plotter/start-server.bat new file mode 100644 index 0000000000..aee88705f2 --- /dev/null +++ b/tools/plotter/start-server.bat @@ -0,0 +1,8 @@ +if not exist "www\pkg" mkdir www\pkg +rustup target add wasm32-unknown-unknown +wasm-pack build --release +if errorlevel 1 cargo install wasm-pack +wasm-pack build --release +cd www +call npm install +npm start diff --git a/tools/plotter/start-server.sh b/tools/plotter/start-server.sh new file mode 100755 index 0000000000..d8ba0e6562 --- /dev/null +++ b/tools/plotter/start-server.sh @@ -0,0 +1,23 @@ +#!/bin/bash +set -e + +CONFIG=release +mkdir -p www/pkg + +rustup target add wasm32-unknown-unknown + +if [ -z "$(cargo install --list | grep wasm-pack)" ] +then + cargo install wasm-pack +fi + +if [ "${CONFIG}" = "release" ] +then + wasm-pack build +else + wasm-pack build --release +fi + +cd www +npm install +npm start diff --git a/tools/plotter/www/.gitignore b/tools/plotter/www/.gitignore new file mode 100644 index 0000000000..16acd49d7f --- /dev/null +++ b/tools/plotter/www/.gitignore @@ -0,0 +1,3 @@ +node_modules +dist +package-lock.json diff --git a/tools/plotter/www/bootstrap.js b/tools/plotter/www/bootstrap.js new file mode 100644 index 0000000000..bea8b6b67a --- /dev/null +++ b/tools/plotter/www/bootstrap.js @@ -0,0 +1,21 @@ +init(); + +async function init() { + if (typeof process == "object") { + // We run in the npm/webpack environment. + const [{Chart}, {main, setup}] = await Promise.all([ + import("wasm-demo"), + import("./index.js"), + ]); + setup(Chart); + main(); + } else { + const [{Chart, default: init}, {main, setup}] = await Promise.all([ + import("../pkg/wasm_demo.js"), + import("./index.js"), + ]); + await init(); + setup(Chart); + main(); + } +} diff --git a/tools/plotter/www/index.html b/tools/plotter/www/index.html new file mode 100644 index 0000000000..5256468042 --- /dev/null +++ b/tools/plotter/www/index.html @@ -0,0 +1,42 @@ + + + + + + Plotters WebAssembly Demo + + + + + +
+

Plotters WebAssembly Demo

+
+ +
Loading WebAssembly...
+
+ + +
+
+ +
+
+
+ + + diff --git a/tools/plotter/www/index.js b/tools/plotter/www/index.js new file mode 100644 index 0000000000..6cd4bd36f5 --- /dev/null +++ b/tools/plotter/www/index.js @@ -0,0 +1,103 @@ +// If you only use `npm` you can simply +// import { Chart } from "wasm-demo" and remove `setup` call from `bootstrap.js`. +class Chart {} + +const canvas = document.getElementById("canvas"); +const coord = document.getElementById("coord"); +const plotType = document.getElementById("plot-type"); +const pitch = document.getElementById("pitch"); +const yaw = document.getElementById("yaw"); +const control = document.getElementById("3d-control"); +const status = document.getElementById("status"); + +let chart = null; + +/** Main entry point */ +export function main() { + let hash = location.hash.substr(1); + for(var i = 0; i < plotType.options.length; i++) { + if(hash == plotType.options[i].value) { + plotType.value = hash; + } + } + setupUI(); + setupCanvas(); +} + +/** This function is used in `bootstrap.js` to setup imports. */ +export function setup(WasmChart) { + Chart = WasmChart; +} + +/** Add event listeners. */ +function setupUI() { + status.innerText = "WebAssembly loaded!"; + plotType.addEventListener("change", updatePlot); + yaw.addEventListener("change", updatePlot); + pitch.addEventListener("change", updatePlot); + yaw.addEventListener("input", updatePlot); + pitch.addEventListener("input", updatePlot); + window.addEventListener("resize", setupCanvas); + window.addEventListener("mousemove", onMouseMove); +} + +/** Setup canvas to properly handle high DPI and redraw current plot. */ +function setupCanvas() { + const dpr = window.devicePixelRatio || 1.0; + const aspectRatio = canvas.width / canvas.height; + const size = canvas.parentNode.offsetWidth * 0.8; + canvas.style.width = size + "px"; + canvas.style.height = size / aspectRatio + "px"; + canvas.width = size; + canvas.height = size / aspectRatio; + updatePlot(); +} + +/** Update displayed coordinates. */ +function onMouseMove(event) { + if (chart) { + var text = "Mouse pointer is out of range"; + + if(event.target == canvas) { + let actualRect = canvas.getBoundingClientRect(); + let logicX = event.offsetX * canvas.width / actualRect.width; + let logicY = event.offsetY * canvas.height / actualRect.height; + const point = chart.coord(logicX, logicY); + text = (point) + ? `(${point.x.toFixed(3)}, ${point.y.toFixed(3)})` + : text; + } + coord.innerText = text; + } +} + +function updatePlot3d() { + let yaw_value = Number(yaw.value) / 100.0; + let pitch_value = Number(pitch.value) / 100.0; + Chart.plot3d(canvas, pitch_value, yaw_value); + coord.innerText = `Pitch:${pitch_value}, Yaw:${yaw_value}` +} + +/** Redraw currently selected plot. */ +function updatePlot() { + const selected = plotType.selectedOptions[0]; + status.innerText = `Rendering ${selected.innerText}...`; + chart = null; + const start = performance.now(); + switch(selected.value) { + case "mandelbrot": + control.classList.add("hide"); + chart = Chart.mandelbrot(canvas); + break; + case "3d-plot": + control.classList.remove("hide"); + updatePlot3d(); + break; + default: + control.classList.add("hide"); + chart = Chart.power("canvas", Number(selected.value)) + } + + const end = performance.now(); + status.innerText = `Rendered ${selected.innerText} in ${Math.ceil(end - start)}ms`; +} diff --git a/tools/plotter/www/package.json b/tools/plotter/www/package.json new file mode 100644 index 0000000000..32a99f8aff --- /dev/null +++ b/tools/plotter/www/package.json @@ -0,0 +1,36 @@ +{ + "name": "plotters-wasm-demo", + "version": "0.1.0", + "description": "Plotters WASM Demo", + "main": "index.js", + "scripts": { + "build": "webpack --config webpack.config.js", + "start": "webpack-dev-server" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/38/plotters.git" + }, + "keywords": [ + "webassembly", + "wasm", + "rust", + "webpack", + "visualization" + ], + "author": "Plotters Developers", + "license": "MIT", + "bugs": { + "url": "https://github.com/38/plotters/issues" + }, + "homepage": "https://github.com/38/plotters", + "dependencies": { + "wasm-demo": "file:../pkg" + }, + "devDependencies": { + "webpack": "^4.43.0", + "webpack-cli": "^3.3.11", + "webpack-dev-server": "^3.10.3", + "copy-webpack-plugin": "^5.0.0" + } +} diff --git a/tools/plotter/www/style.css b/tools/plotter/www/style.css new file mode 100644 index 0000000000..404d385be6 --- /dev/null +++ b/tools/plotter/www/style.css @@ -0,0 +1,55 @@ +html, body, main { + width: 100%; + margin: 0; + padding: 0; +} + +body { + margin: auto; + max-width: 800px; + display: flex; + flex-direction: column; +} + +@media (max-width: 800px) { + body { + padding: 10px; + box-sizing: border-box; + } +} + +main { + display: flex; + flex-direction: column; + align-items: center; +} + +#coord, #status { + color: grey; + font-size: 10px; + height: 15px +} + +#control { + margin-top: 1em; +} + +#control label { + font-weight: bold; + margin-right: 1em; +} + +#control select { + padding: 0.25em 0.5em; +} + +footer { + margin-top: 2em; + font-size: 12px; + text-align: center; +} + +.hide { + visibility: hidden; + height: 0px; +} diff --git a/tools/plotter/www/webpack.config.js b/tools/plotter/www/webpack.config.js new file mode 100644 index 0000000000..80ad8142d2 --- /dev/null +++ b/tools/plotter/www/webpack.config.js @@ -0,0 +1,14 @@ +const CopyWebpackPlugin = require("copy-webpack-plugin"); +const path = require('path'); + +module.exports = { + entry: "./bootstrap.js", + output: { + path: path.resolve(__dirname, "dist"), + filename: "bootstrap.js", + }, + mode: "development", + plugins: [ + new CopyWebpackPlugin(['index.html']) + ], +}; From 2b1a0f6fa98608f1127a54eda76a33166db4139c Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Mon, 17 Jun 2024 21:33:45 +0300 Subject: [PATCH 10/20] plotters - logarithm stub --- tools/plotter/src/lib.rs | 8 ++++++++ tools/plotter/src/logarithm.rs | 31 +++++++++++++++++++++++++++++++ tools/plotter/www/index.html | 1 + tools/plotter/www/index.js | 7 ++++++- 4 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 tools/plotter/src/logarithm.rs diff --git a/tools/plotter/src/lib.rs b/tools/plotter/src/lib.rs index 99b270cb73..37940ab944 100644 --- a/tools/plotter/src/lib.rs +++ b/tools/plotter/src/lib.rs @@ -4,6 +4,7 @@ use web_sys::HtmlCanvasElement; mod func_plot; mod mandelbrot; mod plot3d; +mod logarithm; #[global_allocator] static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT; @@ -36,6 +37,13 @@ impl Chart { }) } + pub fn logarithm(canvas_id: &str) -> Result { + let map_coord = logarithm::draw(canvas_id).map_err(|err| err.to_string())?; + Ok(Chart { + convert: Box::new(move |coord| map_coord(coord).map(|(x, y)| (x.into(), y.into()))), + }) + } + /// Draw Mandelbrot set on the provided canvas element. /// Return `Chart` struct suitable for coordinate conversion. pub fn mandelbrot(canvas: HtmlCanvasElement) -> Result { diff --git a/tools/plotter/src/logarithm.rs b/tools/plotter/src/logarithm.rs new file mode 100644 index 0000000000..797e67c1a8 --- /dev/null +++ b/tools/plotter/src/logarithm.rs @@ -0,0 +1,31 @@ +use crate::DrawResult; +use plotters::prelude::*; +use plotters_canvas::CanvasBackend; + +/// Draw power function f(x) = x^power. +pub fn draw(canvas_id: &str) -> DrawResult Option<(f32, f32)>> { + let backend = CanvasBackend::new(canvas_id).expect("cannot find canvas"); + let root = backend.into_drawing_area(); + let font: FontDesc = ("sans-serif", 20.0).into(); + + root.fill(&WHITE)?; + + let mut chart = ChartBuilder::on(&root) + .margin(20u32) + .caption(format!("y=ln(x)"), font) + .x_label_area_size(30u32) + .y_label_area_size(30u32) + .build_cartesian_2d(0f32..50f32, -10f32..10f32)?; + + chart.configure_mesh().x_labels(3).y_labels(3).draw()?; + + chart.draw_series(LineSeries::new( + (0..=500) + .map(|x| x as f32 / 10f32) + .map(|x| (x, x.ln())), + &RED, + ))?; + + root.present()?; + return Ok(chart.into_coord_trans()); +} diff --git a/tools/plotter/www/index.html b/tools/plotter/www/index.html index 5256468042..826d3ac9ba 100644 --- a/tools/plotter/www/index.html +++ b/tools/plotter/www/index.html @@ -23,6 +23,7 @@

Plotters WebAssembly Demo

+ diff --git a/tools/plotter/www/index.js b/tools/plotter/www/index.js index 6cd4bd36f5..e84a500080 100644 --- a/tools/plotter/www/index.js +++ b/tools/plotter/www/index.js @@ -89,13 +89,18 @@ function updatePlot() { control.classList.add("hide"); chart = Chart.mandelbrot(canvas); break; + case "logarithm": + control.classList.add("hide"); + chart = Chart.logarithm("canvas"); + break; case "3d-plot": control.classList.remove("hide"); updatePlot3d(); break; default: control.classList.add("hide"); - chart = Chart.power("canvas", Number(selected.value)) + chart = Chart.power("canvas", Number(selected.value)); + // chart = null; } const end = performance.now(); From b3204563d030c63925ea97daa3f64bb0d7798e94 Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Tue, 18 Jun 2024 09:10:03 +0300 Subject: [PATCH 11/20] plotters - cleanup --- tools/plotter/build-wasm.sh | 18 ++++++++++++++++++ tools/plotter/src/lib.rs | 10 ---------- tools/plotter/start-server.sh | 15 +-------------- tools/plotter/www/index.html | 1 - tools/plotter/www/index.js | 4 ---- 5 files changed, 19 insertions(+), 29 deletions(-) create mode 100755 tools/plotter/build-wasm.sh diff --git a/tools/plotter/build-wasm.sh b/tools/plotter/build-wasm.sh new file mode 100755 index 0000000000..fc7fe6fb1b --- /dev/null +++ b/tools/plotter/build-wasm.sh @@ -0,0 +1,18 @@ +#!/bin/bash +set -e + +CONFIG=release + +rustup target add wasm32-unknown-unknown + +if [ -z "$(cargo install --list | grep wasm-pack)" ] +then + cargo install wasm-pack +fi + +if [ "${CONFIG}" = "release" ] +then + wasm-pack build +else + wasm-pack build --release +fi diff --git a/tools/plotter/src/lib.rs b/tools/plotter/src/lib.rs index 37940ab944..c703856617 100644 --- a/tools/plotter/src/lib.rs +++ b/tools/plotter/src/lib.rs @@ -2,7 +2,6 @@ use wasm_bindgen::prelude::*; use web_sys::HtmlCanvasElement; mod func_plot; -mod mandelbrot; mod plot3d; mod logarithm; @@ -44,15 +43,6 @@ impl Chart { }) } - /// Draw Mandelbrot set on the provided canvas element. - /// Return `Chart` struct suitable for coordinate conversion. - pub fn mandelbrot(canvas: HtmlCanvasElement) -> Result { - let map_coord = mandelbrot::draw(canvas).map_err(|err| err.to_string())?; - Ok(Chart { - convert: Box::new(map_coord), - }) - } - pub fn plot3d(canvas: HtmlCanvasElement, pitch: f64, yaw: f64) -> Result<(), JsValue> { plot3d::draw(canvas, pitch, yaw).map_err(|err| err.to_string())?; Ok(()) diff --git a/tools/plotter/start-server.sh b/tools/plotter/start-server.sh index d8ba0e6562..09641b1a03 100755 --- a/tools/plotter/start-server.sh +++ b/tools/plotter/start-server.sh @@ -1,22 +1,9 @@ #!/bin/bash set -e -CONFIG=release mkdir -p www/pkg -rustup target add wasm32-unknown-unknown - -if [ -z "$(cargo install --list | grep wasm-pack)" ] -then - cargo install wasm-pack -fi - -if [ "${CONFIG}" = "release" ] -then - wasm-pack build -else - wasm-pack build --release -fi +./build-wasm.sh cd www npm install diff --git a/tools/plotter/www/index.html b/tools/plotter/www/index.html index 826d3ac9ba..5a68c73ed3 100644 --- a/tools/plotter/www/index.html +++ b/tools/plotter/www/index.html @@ -24,7 +24,6 @@

Plotters WebAssembly Demo

-
diff --git a/tools/plotter/www/index.js b/tools/plotter/www/index.js index e84a500080..e6cae453c5 100644 --- a/tools/plotter/www/index.js +++ b/tools/plotter/www/index.js @@ -85,10 +85,6 @@ function updatePlot() { chart = null; const start = performance.now(); switch(selected.value) { - case "mandelbrot": - control.classList.add("hide"); - chart = Chart.mandelbrot(canvas); - break; case "logarithm": control.classList.add("hide"); chart = Chart.logarithm("canvas"); From c6d1357eef82a12928c5ea7d85527c565cd3e0d4 Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Tue, 18 Jun 2024 14:10:20 +0300 Subject: [PATCH 12/20] plotters - basic logarithm --- tools/plotter/src/lib.rs | 4 +- tools/plotter/src/logarithm.rs | 19 ++++++---- tools/plotter/src/mandelbrot.rs | 67 --------------------------------- tools/plotter/www/index.html | 3 ++ tools/plotter/www/index.js | 18 +++++++-- 5 files changed, 31 insertions(+), 80 deletions(-) delete mode 100644 tools/plotter/src/mandelbrot.rs diff --git a/tools/plotter/src/lib.rs b/tools/plotter/src/lib.rs index c703856617..838fea4ab7 100644 --- a/tools/plotter/src/lib.rs +++ b/tools/plotter/src/lib.rs @@ -36,8 +36,8 @@ impl Chart { }) } - pub fn logarithm(canvas_id: &str) -> Result { - let map_coord = logarithm::draw(canvas_id).map_err(|err| err.to_string())?; + pub fn logarithm(canvas: HtmlCanvasElement, max_x: f32) -> Result { + let map_coord = logarithm::draw(canvas, max_x).map_err(|err| err.to_string())?; Ok(Chart { convert: Box::new(move |coord| map_coord(coord).map(|(x, y)| (x.into(), y.into()))), }) diff --git a/tools/plotter/src/logarithm.rs b/tools/plotter/src/logarithm.rs index 797e67c1a8..62dbfdad0a 100644 --- a/tools/plotter/src/logarithm.rs +++ b/tools/plotter/src/logarithm.rs @@ -1,27 +1,30 @@ use crate::DrawResult; use plotters::prelude::*; use plotters_canvas::CanvasBackend; +use web_sys::HtmlCanvasElement; -/// Draw power function f(x) = x^power. -pub fn draw(canvas_id: &str) -> DrawResult Option<(f32, f32)>> { - let backend = CanvasBackend::new(canvas_id).expect("cannot find canvas"); - let root = backend.into_drawing_area(); +pub fn draw(canvas: HtmlCanvasElement, max_x: f32) -> DrawResult Option<(f32, f32)>> { + let root = CanvasBackend::with_canvas_object(canvas) + .unwrap() + .into_drawing_area(); + let font: FontDesc = ("sans-serif", 20.0).into(); root.fill(&WHITE)?; let mut chart = ChartBuilder::on(&root) .margin(20u32) - .caption(format!("y=ln(x)"), font) + .caption(format!("y=ln(x), x=1..{max_x}"), font) .x_label_area_size(30u32) .y_label_area_size(30u32) - .build_cartesian_2d(0f32..50f32, -10f32..10f32)?; + .build_cartesian_2d(0f32..max_x, -10f32..10f32)?; chart.configure_mesh().x_labels(3).y_labels(3).draw()?; + const RANGE_MAX: i32 = 1000; chart.draw_series(LineSeries::new( - (0..=500) - .map(|x| x as f32 / 10f32) + (0..=RANGE_MAX) + .map(|x| x as f32 * max_x / RANGE_MAX as f32) .map(|x| (x, x.ln())), &RED, ))?; diff --git a/tools/plotter/src/mandelbrot.rs b/tools/plotter/src/mandelbrot.rs deleted file mode 100644 index 6c6bc9ae8a..0000000000 --- a/tools/plotter/src/mandelbrot.rs +++ /dev/null @@ -1,67 +0,0 @@ -use crate::DrawResult; -use plotters::prelude::*; -use plotters_canvas::CanvasBackend; -use std::ops::Range; -use web_sys::HtmlCanvasElement; - -/// Draw Mandelbrot set -pub fn draw(element: HtmlCanvasElement) -> DrawResult Option<(f64, f64)>> { - let backend = CanvasBackend::with_canvas_object(element).unwrap(); - - let root = backend.into_drawing_area(); - root.fill(&WHITE)?; - - let mut chart = ChartBuilder::on(&root) - .margin(20) - .x_label_area_size(10) - .y_label_area_size(10) - .build_cartesian_2d(-2.1..0.6, -1.2..1.2)?; - - chart - .configure_mesh() - .disable_x_mesh() - .disable_y_mesh() - .draw()?; - - let plotting_area = chart.plotting_area(); - - let range = plotting_area.get_pixel_range(); - let (pw, ph) = (range.0.end - range.0.start, range.1.end - range.1.start); - let (xr, yr) = (chart.x_range(), chart.y_range()); - - for (x, y, c) in mandelbrot_set(xr, yr, (pw as usize, ph as usize), 100) { - if c != 100 { - plotting_area.draw_pixel((x, y), &HSLColor(c as f64 / 100.0, 1.0, 0.5))?; - } else { - plotting_area.draw_pixel((x, y), &BLACK)?; - } - } - - root.present()?; - return Ok(Box::new(chart.into_coord_trans())); -} - -fn mandelbrot_set( - real: Range, - complex: Range, - samples: (usize, usize), - max_iter: usize, -) -> impl Iterator { - let step = ( - (real.end - real.start) / samples.0 as f64, - (complex.end - complex.start) / samples.1 as f64, - ); - return (0..(samples.0 * samples.1)).map(move |k| { - let c = ( - real.start + step.0 * (k % samples.0) as f64, - complex.start + step.1 * (k / samples.0) as f64, - ); - let mut z = (0.0, 0.0); - let mut cnt = 0; - while cnt < max_iter && z.0 * z.0 + z.1 * z.1 <= 1e10 { - z = (z.0 * z.0 - z.1 * z.1 + c.0, 2.0 * z.0 * z.1 + c.1); - cnt += 1; - } - return (c.0, c.1, cnt); - }); -} diff --git a/tools/plotter/www/index.html b/tools/plotter/www/index.html index 5a68c73ed3..c463175603 100644 --- a/tools/plotter/www/index.html +++ b/tools/plotter/www/index.html @@ -30,6 +30,9 @@

Plotters WebAssembly Demo


+
+ +