Changeset 247 for cpp/common/Convert.h


Ignore:
Timestamp:
11/07/14 17:51:01 (9 years ago)
Author:
Maciej Komosinski
Message:

Sources support both 32-bit and 64-bit, and more compilers

File:
1 edited

Legend:

Unmodified
Added
Removed
  • cpp/common/Convert.h

    r197 r247  
    1010#include "nonstd_stl.h"
    1111#include "2d.h"
     12#include <stdint.h>
     13
    1214
    1315typedef XY<double> Pt2D;
     
    1719{
    1820public:
    19   static int toInt(string s);
    20   static float toFloat(string s);
    21   static string toLowerCase(string s);
    22   static string toUpperCase(string s);
    23   static char toLowerCase(char c);
    24   static char toUpperCase(char c);
    25   template<class T> static string _toString(const T& value);
    26   static string toString(unsigned int v);
    27   static string toString(int v);
    28   static string toString(short v);
    29   static string toString(float v);
    30   static string toString(double v);// ze niby w badzie ma nie byc? ale w frams na pewno ma byc bo tu same double
    31   static string zeroPad(string s,int l) {while((int)s.length()<l) s=string("0")+s; return s;}
     21        static int toInt(string s);
     22        static float toFloat(string s);
     23        static string toLowerCase(string s);
     24        static string toUpperCase(string s);
     25        static char toLowerCase(char c);
     26        static char toUpperCase(char c);
     27        template<class T> static string _toString(const T& value);
     28        static string toString(unsigned int v);
     29        static string toString(int v);
     30        static string toString(short v);
     31        static string toString(float v);
     32        static string toString(double v);
     33        static string zeroPad(string s, int l) { while ((int)s.length() < l) s = string("0") + s; return s; }
     34        static uint32_t hexToInt(const string& col);
    3235
    33   static double toRadians(double kat) {return kat*M_PI/180;}
    34   static double toDegrees(double kat) {return kat/M_PI*180;}
    35   static double atan_2(double y,double x) {if (x==0 && y==0) return 0; else return atan2(y,x);} //needed by borland 5/6 only?
     36        static double toRadians(double angle) { return angle*M_PI / 180; }
     37        static double toDegrees(double angle) { return angle / M_PI * 180; }
     38        static double atan_2(double y, double x) { if (x == 0 && y == 0) return 0; else return atan2(y, x); } //needed by borland 5/6 only?
    3639
    37         static double odleglosc_sq(double x1,double y1,double x2,double y2) //odleglosc do kwadratu, wystarczy do porownywania
    38         {double dx=x2-x1, dy=y2-y1; return dx*dx+dy*dy;}
    39         static double odleglosc_sq(const Pt2D& p1,const Pt2D& p2) //odleglosc do kwadratu
    40         {return odleglosc_sq(p1.x,p1.y,p2.x,p2.y);}
     40        static double odleglosc_sq(double x1, double y1, double x2, double y2) //odleglosc do kwadratu, wystarczy do porownywania
     41        {
     42                double dx = x2 - x1, dy = y2 - y1; return dx*dx + dy*dy;
     43        }
     44        static double odleglosc_sq(const Pt2D& p1, const Pt2D& p2) //odleglosc do kwadratu
     45        {
     46                return odleglosc_sq(p1.x, p1.y, p2.x, p2.y);
     47        }
    4148
    42         static double odleglosc(double x1,double y1,double x2,double y2)        {return sqrt(odleglosc_sq(x1,y1,x2,y2));}
    43         static double odleglosc(const Pt2D& p1,const Pt2D& p2)
    44         {return sqrt(odleglosc_sq(p1,p2));}
    45        
    46   //static float odleglosc(int x1,int y1,int x2,int y2) {float dx=x1-x2; float dy=y1-y2; return sqrt(dx*dx+dy*dy);}
    47   //static float odleglosc(float x1,float y1,float x2,float y2) {return sqrt(odleglosc_sq(x1,y1,x2,y2));}
    48   //static float odleglosc_sq(float x1,float y1,float x2,float y2) {float dx=x1-x2; float dy=y1-y2; return dx*dx+dy*dy;}
     49        static double odleglosc(double x1, double y1, double x2, double y2)     { return sqrt(odleglosc_sq(x1, y1, x2, y2)); }
     50        static double odleglosc(const Pt2D& p1, const Pt2D& p2)
     51        {
     52                return sqrt(odleglosc_sq(p1, p2));
     53        }
    4954
    50   static struct tm localtime(const time_t &timep);//jak ::localtime ale zwraca strukture zamiast wskaznika, ref w parametrze dla wygodnego wywolywania
    51   static string asctime(const struct tm &tm);//jak ::asctime ale thread safe i bez glupiego \n na koncu, ref w parametrze dla wygodnego wywolywania
     55        //static float odleglosc(int x1,int y1,int x2,int y2) {float dx=x1-x2; float dy=y1-y2; return sqrt(dx*dx+dy*dy);}
     56        //static float odleglosc(float x1,float y1,float x2,float y2) {return sqrt(odleglosc_sq(x1,y1,x2,y2));}
     57        //static float odleglosc_sq(float x1,float y1,float x2,float y2) {float dx=x1-x2; float dy=y1-y2; return dx*dx+dy*dy;}
     58
     59        static struct tm localtime(const time_t &timep);//jak ::localtime ale zwraca strukture zamiast wskaznika, ref w parametrze dla wygodnego wywolywania
     60        static string asctime(const struct tm &tm);//jak ::asctime ale thread safe i bez glupiego \n na koncu, ref w parametrze dla wygodnego wywolywania
     61
     62        static std::wstring strTOwstr(const char *s)
     63        {
     64                string str(s);
     65                return std::wstring(str.begin(), str.end());
     66        }
     67
     68        static string wstrTOstr(const wchar_t *s)
     69        {
     70                wstring str(s);
     71                return string(str.begin(), str.end());
     72        }
     73
     74        static string wstrTOutf8(const wchar_t *s)
     75        {
     76                if (s == NULL) return "";
     77                string res;
     78                wchar_t *wcp = (wchar_t*)s;
     79                while (*wcp != 0)
     80                {
     81                        int c = *wcp;
     82                        if (c < 0x80) res += c;
     83                        else if (c < 0x800) { res += 192 + c / 64; res += 128 + c % 64; }
     84                        else if (c - 0xd800u < 0x800) res += "<ERR-CHAR>";
     85                        else if (c < 0x10000) { res += 224 + c / 4096; res += 128 + c / 64 % 64; res += 128 + c % 64; }
     86                        else if (c < 0x110000) { res += 240 + c / 262144; res += 128 + c / 4096 % 64; res += 128 + c / 64 % 64; res += 128 + c % 64; }
     87                        else res += "<ERR-CHAR>";
     88                        wcp++;
     89                }
     90                return res;
     91        }
     92        //#endif
    5293};
    5394
    5495
    5596
    56 struct Kat //znormalizowany k¹t w radianach [0,2pi) i stopniach [0,360) z obliczonymi sinus i cosinus oraz intem [0,359]
     97struct Angle //normalized angle in radians [0,2pi) and degrees [0,360) with pre-computed sine and cosine and degree as integer [0,359]
    5798{
    5899private:
    59         double kat; //w radianach, read-only
     100        double angle; //in radians, read-only
    60101public:
    61         double kat_stopnie; //read-only
    62         int kat_stopnie_int; //read-only
     102        double angle_deg; //read-only
     103        int angle_deg_int; //read-only
    63104
    64         Kat() {set(0);}
    65         Kat(double k) {set(k);}
    66         Kat(Kat &kt) {set(kt.get());}
    67         Kat(double dy,double dx) {set(dy,dx);}
    68         void set(double k) {k=fmod(k,M_PI*2); if (k<0) k+=M_PI*2; kat=k; sinus=sin(k); cosinus=cos(k); kat_stopnie=Convert::toDegrees(kat); kat_stopnie_int=roundToInt(kat_stopnie); kat_stopnie_int%=360; }
    69         void set(double dy,double dx) {set(Convert::atan_2(dy,dx));}
    70         void add(double dk) {set(kat+dk);}
    71         void add(Kat &kt) {set(kat+kt.get());}
    72         double get() {return kat;}
    73         double sinus,cosinus;
     105        Angle() { set(0); }
     106        Angle(double k) { set(k); }
     107        Angle(Angle &kt) { set(kt.get()); }
     108        Angle(double dy, double dx) { set(dy, dx); }
     109        void set(double k) { k = fmod(k, M_PI * 2); if (k < 0) k += M_PI * 2; angle = k; sine = sin(k); cosine = cos(k); angle_deg = Convert::toDegrees(angle); angle_deg_int = roundToInt(angle_deg); angle_deg_int %= 360; }
     110        void set(double dy, double dx) { set(Convert::atan_2(dy, dx)); }
     111        void add(double dk) { set(angle + dk); }
     112        void add(Angle &kt) { set(angle + kt.get()); }
     113        double get() { return angle; }
     114        double sine, cosine;
    74115};
    75116
Note: See TracChangeset for help on using the changeset viewer.