23 #ifndef COM_DAFER45_TBTK_CANVAS 24 #define COM_DAFER45_TBTK_CANVAS 29 #include <initializer_list> 31 #include <opencv2/core/core.hpp> 32 #include <opencv2/highgui/highgui.hpp> 36 template<
typename CoordinateType>
42 RGBA(
char r,
char g,
char b,
char a = 0);
53 Pixel(
unsigned int x,
unsigned int y);
60 Canvas(
unsigned int width,
unsigned int height);
66 void setOrigin(CoordinateType x, CoordinateType y);
70 std::initializer_list<std::initializer_list<CoordinateType>> basisVectors
74 void drawPixel(
const RGBA &rgba, CoordinateType x, CoordinateType y);
77 void drawCircle(
const RGBA &rgba, CoordinateType x, CoordinateType y, CoordinateType radius);
80 void save(std::string filename)
const;
86 CoordinateType origin[2];
89 CoordinateType basisVectors[2][2];
95 Pixel getPixel(CoordinateType x, CoordinateType y);
98 void calculateNorms();
101 template<
typename CoordinateType>
105 template<
typename CoordinateType>
117 if(x < (
unsigned int)data.cols && y < (
unsigned int)data.rows){
118 data.at<cv::Vec3b>(y, x)[0] = rgba.b;
119 data.at<cv::Vec3b>(y, x)[1] = rgba.g;
120 data.at<cv::Vec3b>(y, x)[2] = rgba.r;
130 Pixel p = getPixel(x, y);
132 && p.
x < (
unsigned int)data.cols
134 && p.y < (
unsigned int)data.rows
136 data.at<cv::Vec3b>(p.y, p.
x)[0] = rgba.b;
137 data.at<cv::Vec3b>(p.y, p.
x)[1] = rgba.g;
138 data.at<cv::Vec3b>(p.y, p.
x)[2] = rgba.r;
150 cv::circle(data, cv::Point(x, y), radius, cv::Scalar(rgba.r, rgba.g, rgba.b), CV_FILLED, 8, 0);
161 Pixel p = getPixel(x, y);
167 cv::circle(data, cv::Point(p.
x, p.y), (
int)radius, cv::Scalar(rgba.r, rgba.g, rgba.b));
171 template<
typename CoordinateType>
176 double projections[2];
177 for(
unsigned int n = 0; n < 2; n++){
179 = ((double)x - (
double)origin[0])*((
double)basisVectors[n][0])
180 + ((
double)y - (double)origin[1])*((double)basisVectors[n][1]);
181 projections[n] /= norms[n]*norms[n];
184 return Pixel(data.cols*projections[0], data.rows*projections[1]);
187 template<
typename CoordinateType>
189 std::initializer_list<std::initializer_list<CoordinateType>> basisVectors
192 basisVectors.size() == 2,
193 "Canvas::setBasisVectors()",
194 "'basisVectors' must contain two vectors.",
197 for(
unsigned int n = 0; n < 2; n++){
199 (basisVectors.begin() + n)->size() == 2,
200 "Canvas::setBasisVectors",
201 "Each basis vector must have two components, but basis" 202 <<
" vector " << n <<
" has " 203 << (basisVectors.begin() + n)->size()
209 for(
unsigned int n = 0; n < 2; n++)
210 for(
unsigned int c = 0; c < 2; c++)
211 this->basisVectors[n][c] = *((basisVectors.begin() + n)->begin() + c);
216 template<
typename CoordinateType>
218 imwrite(filename, data);
221 template<
typename CoordinateType>
229 template<
typename CoordinateType>
235 template<
typename CoordinateType>
237 for(
unsigned int n = 0; n < 2; n++){
239 for(
unsigned int c = 0; c < 2; c++)
240 norms[n] += basisVectors[n][c]*basisVectors[n][c];
241 norms[n] = sqrt(norms[n]);
void setOrigin(CoordinateType x, CoordinateType y)
Definition: Canvas.h:106
void drawPixel(const RGBA &rgba, CoordinateType x, CoordinateType y)
RGBA(char r, char g, char b, char a=0)
Definition: Canvas.h:222
~Canvas()
Definition: Canvas.h:102
Definition: ModelFactory.h:35
void save(std::string filename) const
Definition: Canvas.h:217
void setBasisVectors(std::initializer_list< std::initializer_list< CoordinateType >> basisVectors)
Definition: Canvas.h:188
void drawCircle(const RGBA &rgba, CoordinateType x, CoordinateType y, CoordinateType radius)
unsigned int x
Definition: Canvas.h:56
Pixel(unsigned int x, unsigned int y)
Definition: Canvas.h:230