Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
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 Go to next message
kohait00 is currently offline  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 Smile

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 #29943 is a reply to message #29942] Wed, 01 December 2010 10:28 Go to previous messageGo to next message
kohait00 is currently offline  kohait00
Messages: 939
Registered: July 2009
Location: Germany
Experienced Contributor
i've got a test 'suite' for the Scaler, actualle a positioning control with veeery basic look. maybe this could be extended somehow to be helpful in Upp..

attached is the Test environment
Re: PROPOSAL: linear Scaler helper for different min/max domains [message #29959 is a reply to message #29943] Wed, 01 December 2010 15:54 Go to previous messageGo to next message
koldo is currently offline  koldo
Messages: 3357
Registered: August 2008
Senior Veteran
Hello Kohait

This could be other focus, perhaps more algebraic Smile. Just a draft:

class Function2D {
	virtual double GetX(double y) {};		// Get the first value
	virtual double GetY(double x) {};
	virtual Array <double> GetX(double y) {};	// Get all values
	virtual Array <double> GetY(double x) {};
};

class Line : public Function2D {
private:
	Point p1, p2;

public:
	Line(Point p1, Point p2) : p1(p1), p2(p2) {};
	
	virtual double GetX(double y);
	virtual double GetY(double x);
};

class Polynomial : public Function2D {
public:
	Polynomial(Point p1, ...);	// Polynomial defined by points
	Polynomial(double coeff, ...);	// Polynomial defined by coefficients
};

... other functions ...


Best regards
IƱaki
Re: PROPOSAL: linear Scaler helper for different min/max domains [message #29960 is a reply to message #29942] Wed, 01 December 2010 16:08 Go to previous messageGo to next message
dolik.rce is currently offline  dolik.rce
Messages: 1789
Registered: August 2008
Location: Czech Republic
Ultimate Contributor

Good idea Kohait, I spend a lot of time debugging broken scaling in my apps Smile 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 #29961 is a reply to message #29960] Wed, 01 December 2010 16:33 Go to previous messageGo to next message
kohait00 is currently offline  kohait00
Messages: 939
Registered: July 2009
Location: Germany
Experienced Contributor
combining 2 of them into a bi-domain is a good idea...let's think on how to expose the iface
Re: PROPOSAL: linear Scaler helper for different min/max domains [message #29965 is a reply to message #29961] Wed, 01 December 2010 17:19 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
What about Xform2D? I guess it is the complete solution to the problem of 2D transformations... (at least for doubles).

Well, perhaps I should move it from Painter to Core?

Mirek
Re: PROPOSAL: linear Scaler helper for different min/max domains [message #29966 is a reply to message #29965] Wed, 01 December 2010 17:30 Go to previous message
kohait00 is currently offline  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 Smile

[Updated on: Wed, 01 December 2010 17:32]

Report message to a moderator

Previous Topic: Slight changes in DHCtrl
Next Topic: HELPER: Value grouping to ValueArray
Goto Forum:
  


Current Time: Sun Apr 28 01:59:56 CEST 2024

Total time taken to generate the page: 0.90211 seconds