Chris_M_Thomasson

2d Projection

Jul 30th, 2017
380
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.36 KB | None | 0 0
  1. #if ! defined (CT_G2D_HPP)
  2. #define CT_G2D_HPP 1
  3.  
  4. #define CT_PI 3.1415926535897932384626433832795
  5. #define CT_PI2 6.283185307179586476925286766559
  6.  
  7. #include <complex>
  8.  
  9.  
  10. // graphics base
  11. #include "EasyBMP.h"
  12. #include "EasyBMP_Geometry.h"
  13.  
  14.  
  15. // common
  16. typedef double ct_float;
  17. typedef std::complex<ct_float> ct_complex;
  18.  
  19.  
  20. #define CT_RGBA(mp_r, mp_g, mp_b, mp_a) \
  21.     RGBApixel { ((unsigned char)(mp_b)), \
  22.         ((unsigned char)(mp_g)), \
  23.         ((unsigned char)(mp_r)), \
  24.         ((unsigned char)(mp_a)) }
  25.  
  26. #define CT_RGB(mp_r, mp_g, mp_b) \
  27.     CT_RGBA(mp_r, mp_g, mp_b, 0)
  28.  
  29.  
  30. #define CT_RGBAF(mp_r, mp_g, mp_b, mp_a) \
  31.     CT_RGBA(((mp_r) * 255), ((mp_g) * 255), ((mp_b) * 255), ((mp_a) * 255))
  32.  
  33. #define CT_RGBF(mp_r, mp_g, mp_b) \
  34.     CT_RGBAF(mp_r, mp_g, mp_b, 0)
  35.  
  36. namespace ct
  37. {
  38.     struct axes2d
  39.     {
  40.         ct_float xmin;
  41.         ct_float xmax;
  42.         ct_float ymin;
  43.         ct_float ymax;
  44.  
  45.         axes2d(ct_float xmin_, ct_float xmax_, ct_float ymin_, ct_float ymax_)
  46.         :   xmin(xmin_),
  47.             xmax(xmax_),
  48.             ymin(ymin_),
  49.             ymax(ymax_)
  50.         {
  51.  
  52.         }
  53.  
  54.         axes2d(ct_complex const& p0, ct_float r)
  55.         :   xmin(p0.real() - r),
  56.             xmax(p0.real() + r),
  57.             ymin(p0.imag() - r),
  58.             ymax(p0.imag() + r)
  59.         {
  60.  
  61.         }
  62.  
  63.  
  64.         ct_float width() const { return xmax - xmin; }
  65.         ct_float height() const { return ymax - ymin; }
  66.     };
  67.  
  68.  
  69.     struct plane2d
  70.     {
  71.         axes2d m_axes;
  72.         unsigned int m_width;
  73.         unsigned int m_height;
  74.         ct_float m_aratio;
  75.         ct_float m_xstep;
  76.         ct_float m_ystep;
  77.  
  78.         plane2d(axes2d const& axes, unsigned int width, unsigned int height, bool aratio = true)
  79.         :   m_axes(axes),
  80.             m_width(width),
  81.             m_height(height),
  82.             m_aratio((ct_float)height / width),
  83.             m_xstep((axes.width() / (width - 1))),
  84.             m_ystep((axes.height() / (height - 1)))
  85.         {
  86.             if (!aratio)
  87.             {
  88.                 m_xstep /= m_aratio;
  89.                 return;
  90.             }
  91.  
  92.             ct_float daspect = std::abs((ct_float)height / width);
  93.             ct_float waspect = std::abs(m_axes.height() / m_axes.width());
  94.  
  95.             if (daspect > waspect)
  96.             {
  97.                 ct_float excess = m_axes.height() * (daspect / waspect - 1);
  98.                 m_axes.ymax += excess / 2;
  99.                 m_axes.ymin -= excess / 2;
  100.             }
  101.  
  102.             else if (daspect < waspect)
  103.             {
  104.                 ct_float excess = m_axes.width() * (waspect / daspect - 1);
  105.                 m_axes.xmax += excess / 2;
  106.                 m_axes.xmin -= excess / 2;
  107.             }
  108.  
  109.             m_xstep = m_axes.width() / ((width > 1) ? (width - 1) : width);
  110.             m_ystep = m_axes.height() / ((height > 1) ? (height - 1) : height);
  111.         }
  112.  
  113.         ct_float project_to(ct_float p) const
  114.         {
  115.             return std::floor(p / m_ystep);
  116.         }
  117.  
  118.         ct_complex project_toxxx(ct_complex const& p) const
  119.         {
  120.             return ct_complex(
  121.                 std::floor((p.real() - m_axes.xmin) / m_xstep),
  122.                 std::floor((m_axes.ymax - p.imag()) / m_ystep)
  123.             );
  124.         }
  125.  
  126.         ct_complex project_to(ct_complex const& p) const
  127.         {
  128.             return ct_complex(
  129.                 std::floor((p.real() - m_axes.xmin) / m_xstep),
  130.                 std::floor((m_axes.ymax - p.imag()) / m_ystep)
  131.             );
  132.         }
  133.  
  134.         ct_complex project_to(unsigned int x, unsigned int y) const
  135.         {
  136.             return ct_complex(
  137.                 m_axes.xmin + x * m_xstep,
  138.                 m_axes.ymax - y * m_ystep
  139.             );
  140.         }
  141.     };
  142.  
  143.  
  144.     struct plot2d
  145.     {
  146.         unsigned int m_width;
  147.         unsigned int m_height;
  148.         ct::plane2d m_plane;
  149.         BMP m_bmp;
  150.  
  151.         plot2d(ct::axes2d const& axes, unsigned int width, unsigned int height, bool aratio)
  152.         :   m_width(width),
  153.             m_height(height),
  154.             m_plane(axes, width, height, aratio)
  155.         {
  156.             m_bmp.SetSize(m_width, m_height);
  157.             m_bmp.SetBitDepth(24);
  158.         }
  159.  
  160.  
  161.         void clear(RGBApixel const& color)
  162.         {
  163.             for (unsigned int x = 0; x < m_width; ++x)
  164.             {
  165.                 for (unsigned int y = 0; y < m_height; ++y)
  166.                 {
  167.                     set_pixel(x, y, color);
  168.                 }
  169.             }
  170.         }
  171.  
  172.  
  173.         void circle(ct_complex const& c, ct_float r, RGBApixel const& color)
  174.         {
  175.             ct_complex pc = m_plane.project_to(c);
  176.             ct_float pr = m_plane.project_to(r);
  177.  
  178.  
  179.  
  180.             if (pr > m_height) return;
  181.  
  182.  
  183.  
  184.             DrawArc(m_bmp, pc.real(), pc.imag(), pr, 0, 6.28, color);
  185.             //DrawArc(m_bmp, pc.real(), pc.imag(), pr + 1, 0, 6.28, color);
  186.         }
  187.  
  188.         void line(ct_complex const& p0, ct_complex const& p1, RGBApixel const& color)
  189.         {
  190.             ct_complex pp0 = m_plane.project_to(p0);
  191.             ct_complex pp1 = m_plane.project_to(p1);
  192.             DrawLine(m_bmp, pp0.real(), pp0.imag(), pp1.real(), pp1.imag(), color);
  193.             /*
  194.             {
  195.                 RGBApixel xc = color;
  196.  
  197.                 xc.Red /= 1.5;
  198.                 xc.Green /= 1.5;
  199.                 xc.Blue /= 1.5;
  200.  
  201.                 DrawLine(m_bmp, pp0.real() + 1, pp0.imag() + 1, pp1.real() + 1, pp1.imag() + 1, xc);
  202.             }
  203.  
  204.  
  205.             {
  206.                 RGBApixel xc = color;
  207.  
  208.                 xc.Red /= 2;
  209.                 xc.Green /= 2;
  210.                 xc.Blue /= 2;
  211.  
  212.                 DrawLine(m_bmp, pp0.real() + 2, pp0.imag() + 2, pp1.real() + 2, pp1.imag() + 2, xc);
  213.             }
  214.             */
  215.        
  216.            
  217.         }
  218.  
  219.  
  220.         void arc(ct_complex const& c, ct_float r, ct_float origina, ct_float desta, RGBApixel const& color)
  221.         {
  222.             ct_complex pc = m_plane.project_to(c);
  223.             ct_float pr = m_plane.project_to(r);
  224.             //DrawArc(m_bmp, pc.real(), pc.imag(), pr, -toa, froma, color);
  225.             DrawArc(m_bmp, pc.real(), pc.imag(), pr, (CT_PI2 - desta), (CT_PI2 - origina), color);
  226.         }
  227.  
  228.  
  229.  
  230.         void set_pixelf(ct_complex const& p, RGBApixel const& color)
  231.         {
  232.             ct_complex pp = m_plane.project_to(p);
  233.             if (pp.real() >= m_width || pp.real() < 0 ||
  234.                 pp.imag() >= m_height || pp.imag() < 0) return;
  235.             set_pixelx(pp.real(), pp.imag(), color);
  236.             //set_pixelx(pp.real() - 1, pp.imag() - 0, color);
  237.            // set_pixelx(pp.real() - 0, pp.imag() - 1, color);
  238.            // set_pixelx(pp.real() + 1, pp.imag() + 0, color);
  239.             //set_pixelx(pp.real() + 0, pp.imag() + 1, color);
  240.         }
  241.        
  242.  
  243.         bool get_pixelf(RGBApixel& color, ct_complex const& p)
  244.         {
  245.             ct_complex pp = m_plane.project_to(p);
  246.             if (pp.real() >= m_width || pp.real() < 0 ||
  247.                 pp.imag() >= m_height || pp.imag() < 0) return false;
  248.             color = m_bmp.GetPixel(pp.real(), pp.imag());
  249.             return true;
  250.         }
  251.  
  252.  
  253.         void set_pixelx(unsigned int x, unsigned int y, RGBApixel const& color)
  254.         {
  255.             if (x >= m_width || y >= m_height) return;
  256.             RGBApixel pcolor = m_bmp.GetPixel(x, y);
  257.             RGBApixel pcolorx = pcolor;
  258.             pcolor.Red = pcolor.Red + color.Red;
  259.             pcolor.Green = pcolor.Green + color.Green;
  260.             pcolor.Blue = pcolor.Blue + color.Blue;
  261.             if (pcolor.Red < pcolorx.Red) pcolor.Red = pcolorx.Red;
  262.             if (pcolor.Green < pcolorx.Green) pcolor.Green = pcolorx.Green;
  263.             if (pcolor.Blue < pcolorx.Red) pcolor.Blue = pcolorx.Blue;
  264.             m_bmp.SetPixel(x, y, pcolor);
  265.         }
  266.  
  267.  
  268.         void set_pixel(unsigned int x, unsigned int y, RGBApixel const& color)
  269.         {
  270.             if (x >= m_width || y >= m_height) return;
  271.             m_bmp.SetPixel(x, y, color);
  272.         }
  273.  
  274.  
  275.         bool get_pixel(RGBApixel& color, unsigned int x, unsigned int y)
  276.         {
  277.             if (x >= m_width || y >= m_height) return false;
  278.             color = m_bmp.GetPixel(x, y);
  279.             return true;
  280.         }
  281.  
  282.  
  283.         // RGBApixel color = plot.m_bmp.GetPixel(pp.real(), pp.imag());
  284.  
  285.         void save(char const* fname)
  286.         {
  287.             m_bmp.WriteToFile(fname);
  288.         }
  289.     };
  290. }
  291.  
  292.  
  293.  
  294. #endif
Advertisement
Add Comment
Please, Sign In to add comment