00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #ifndef _CANVAS_H_
00036 #define _CANVAS_H_
00037
00038 #include "RGB.h"
00039 #include "xmath.h"
00040 #include "Point.h"
00041
00042 #include <string>
00043 #include <cassert>
00044 #include <map>
00045
00046 using namespace std;
00047
00048 namespace columbia
00049 {
00050
00051 class Canvas
00052 {
00053 public:
00054 explicit Canvas(int resoH = 512, int resoV = 512) : hReso(resoH), vReso(resoV), filename("tmp.ppm"), data(0) { init(); }
00055 ~Canvas() { delete [] data; }
00056 void Clear(RGB const&);
00057
00058 RGB* operator[] (int r) { assert(r>=0 && r<vReso); return data + r*hReso; }
00059 RGB& Pixel(int row, int col);
00060
00061
00062
00063
00064
00065
00066 void ScanLineSegment(int x1, int y1, RGB const& color1, int x2, int y2, RGB const& color2, multimap<int, pair<int, RGB> >* output=0);
00067
00068 void WireTriangle(Point const& P1, RGB const& col1, Point const& P2, RGB const& col2, Point const& P3, RGB const& col3);
00069 void SolidTriangle(Point const& P1, RGB const& col1, Point const& P2, RGB const& col2, Point const& P3, RGB const& col3);
00070
00071 void WritePPM(const char* = 0) const;
00072
00073 int hReso, vReso;
00074 std::string filename;
00075
00076
00077
00078 void WireCircle(int center_x, int center_y, int radius, int reso, RGB const& col);
00079 void SolidCircle(int center_x, int center_y, int radius, RGB const& col);
00080 void SolidRectangle(int minx, int miny, int maxx, int maxy, RGB const& col);
00081
00082 private:
00083 RGB* data;
00084 bool swap_xy, flip_y;
00085
00086 void init();
00087
00088 void scanLineSegment(int x1, int y1, RGB const& col1, int x2, int y2, RGB const& col2, multimap<int, pair<int, RGB> >* output);
00089 void writePixel(int X, int Y, RGB const& col, multimap<int, pair<int, RGB> >* output);
00090
00091 double eye_at;
00092 Point perspectiveProjection(Point const&);
00093 };
00094
00095 inline void Canvas :: writePixel(int X, int Y, RGB const& col, multimap<int, pair<int, RGB> >* output)
00096 {
00097 if(swap_xy) swap(X,Y);
00098 if(flip_y) Y = -Y;
00099
00100 if(output) output->insert(make_pair(Y, make_pair(X, col)));
00101
00102 else if( (X >= -hReso/2 && X <= hReso/2) &&
00103 (Y >= -vReso/2 && Y <= vReso/2) )
00104 {
00105 Pixel(Y + vReso/2, X + hReso/2) = col;
00106 }
00107 }
00108
00109
00110 inline RGB& Canvas :: Pixel(int row, int col)
00111 {
00112 if(row >= vReso) throw "row number overflow\n";
00113 if(row < 0) throw "row number underflow\n";
00114 if(col >= hReso) throw "col number overrflow\n";
00115 if(col < 0) throw "col number underflow\n";
00116 return *(data + row * hReso + col);
00117 }
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127 inline Point Canvas :: perspectiveProjection(Point const& P)
00128 {
00129 double scale = eye_at / (eye_at - P.z);
00130 return Point(P.x * scale, P.y * scale, P.z);
00131 }
00132
00133
00134 }
00135
00136 #endif