Home » U++ Library support » U++ Core » PROPOSAL: linear Scaler helper for different min/max domains
PROPOSAL: linear Scaler helper for different min/max domains [message #29942] |
Wed, 01 December 2010 10:19  |
 |
kohait00
Messages: 939 Registered: July 2009 Location: Germany
|
Experienced Contributor |
|
|
hey all
often one ends up using the same pattern over and over again for traversing from i.e. pixel domain (Draw in Ctrl) to the representative Value domain, and back to notify on user action..its a nightmare 
what about this one? its a *linear* scaler..
template<class T = double>
class Scaler
{
public:
Scaler() : mn(0), mx(0) {}
Scaler(const T& mn, const T& mx) : mn(mn), mx(mx) {}
inline void Min(const T& t) { mn = t; }
inline T Min() const { return mn; }
inline void Max(const T& t) { mx = t; }
inline T Max() const { return mx; }
inline void MinMax(const T& _mn, const T& _mx) { mn = _mn; mx = _mx; }
//scales local dimension value t to foreign dimensions d
//returned in foreign dimension
inline T To(const Scaler& d, const T& t) { return (t-mn)*(d.mx-d.mn)/(mx-mn)+d.mn; }
//scales foreign dimension value t from foreign s to local dimension
//return in local dimension
inline T From(const Scaler& s, const T& t) { return (t-s.mn)*(mx-mn)/(s.mx-s.mn)+mn; }
inline T operator() (const Scaler& s, const T& t) { return From(s, t); }
protected:
T mn, mx;
};
using it like this (v for value domain, p for pixel domain)
here only for x axis i.e.
Scaler<double> vsx, psx;
//from draw domain to value domain
valx = vsx(psx, Point().x);
//from value domain to draw domain
Point().x = (int) psx(vsx, valx);
i think also of type conversions to be able to convert scaler to scaler with different types..
trying to avoid virtualisation here, but thinking of a logarithmic scaler as well, but then probably would need to virtualize an interface.
any hints / improvements welcome
|
|
|
|
|
Re: PROPOSAL: linear Scaler helper for different min/max domains [message #29960 is a reply to message #29942] |
Wed, 01 December 2010 16:08   |
|
Good idea Kohait, I spend a lot of time debugging broken scaling in my apps A reliable class to use repeatedly would probably save a lot of time, work and code. I would use this e.g. in PlotCtrl.
I would prefer little bit different interface, I somehow don't like the idea that I need two scalers to perform any conversion. I understand that you were aiming for a universal tool that can be used to convert even between multiple scales (lets think px,pt,cm,m,...), but in simple cases (e.g. I know I only need to convert px<->cm) it adds complexity... Maybe such simpler interface could be made by using class with two Scalers, that would only hide the detail of using two of them.
Best regards,
Honza
|
|
|
|
|
Re: PROPOSAL: linear Scaler helper for different min/max domains [message #29966 is a reply to message #29965] |
Wed, 01 December 2010 17:30  |
 |
kohait00
Messages: 939 Registered: July 2009 Location: Germany
|
Experienced Contributor |
|
|
well, i think it's not quite the same, it's a 2D transformation facility, with, translation, rotation, sheer, scale...etc..which is good to have btw..didn't know that
but maybe one could setup a transformation matrix (Xform2D) to transform from one domain (i.e. view) to host domain (value), in the matrix, min and max values would find their place...
i'll take a look..need some math
[Updated on: Wed, 01 December 2010 17:32] Report message to a moderator
|
|
|
Goto Forum:
Current Time: Mon Apr 28 15:26:06 CEST 2025
Total time taken to generate the page: 0.01286 seconds
|