The SKIRT project
advanced radiative transfer for astrophysics
Public Member Functions | Private Member Functions | Private Attributes | List of all members
Canvas Class Reference

#include <Canvas.hpp>

Public Member Functions

 Canvas (size_t numPixels)
 
void drawLine (double x1, double y1, double x2, double y2)
 
void fillBox (double x1, double y1, double x2, double y2)
 
void fillConvexPolygon (const ConvexPolygon &poly)
 
bool saveToTiff (string filepath) const
 
void setColor (double r, double g, double b)
 
void setWidth (double w)
 

Private Member Functions

bool indicesInRange (double x1, double x2, size_t &i1, size_t &i2)
 

Private Attributes

double _b
 
double _g
 
size_t _n
 
Table< 3 > _pixels
 
double _r
 
double _w2
 

Detailed Description

The Canvas class represents a simple pixelated canvas with support for drawing RGB-colored lines and saving to a TIFF file for viewing. The canvas is always square with a logical coordinate range between 0 and 1 in each direction. The number of pixels must be specified at construction and is the same in each direction. Colors are specified as three RGB components ranging from 0 (no intensity) to 1 (maximum intensity). Line widths are specified in units where the canvas size equals 1. The canvas is initialized with a black background (i.e. all pixels values are set to zero), and with a default line color of white (i.e. the three components equal 1) and a default line width of 0.01 (i.e. 1/100 of the canvas size).

Constructor & Destructor Documentation

◆ Canvas()

Canvas::Canvas ( size_t  numPixels)

Constructs a canvas with the specified number of pixels in each direction, which must be at least 3 and at most 32000 (the number must fit in 15 bits). If the number of pixels is out of range, the behavior is undefined. The canvas is initialized with a black background, with a default line color of white, and a default line width of 0.01. These defaults can be changed with the respective setters.

Member Function Documentation

◆ drawLine()

void Canvas::drawLine ( double  x1,
double  y1,
double  x2,
double  y2 
)

Draws a line with the current color and width from the point (x1,y1) to the point (x2,y2). The coordinates are specified in the canvas coordinate system ranging from 0 and 1 in each direction. Segments of the line lying outside of the canvas are clipped away. More specifically, the function proceeds as follows. It first constructs a virtual rectangle by expanding the mathematical line between the given points with half of the current width in all directions. It then assigns the current color to all pixels for which the pixel center lies inside that rectangle (and leaves any other pixels alone).

◆ fillBox()

void Canvas::fillBox ( double  x1,
double  y1,
double  x2,
double  y2 
)

Fills a rectangle lined up with the coordinate axes with corner points (x1,y1) and (x2,y2) using the current color. The coordinates are specified in the canvas coordinate system ranging from 0 and 1 in each direction. Segments of the rectangle lying outside of the canvas are clipped away.

◆ fillConvexPolygon()

void Canvas::fillConvexPolygon ( const ConvexPolygon poly)

Fills the specified convex polygon using the current color. The coordinates are specified in the canvas coordinate system ranging from 0 and 1 in each direction. Segments of the polygon lying outside of the canvas are clipped away. If the points of the polygon violate the restrictions described for the ConvexPolygon class, the behavior of this function is undefined.

◆ indicesInRange()

bool Canvas::indicesInRange ( double  x1,
double  x2,
size_t &  i1,
size_t &  i2 
)
private

Determines the indices for the range of pixels that lie inside the specified coordinate range. If the range contains at least one pixel, the function places the indices in the output arguments and returns true. If not, the function returns false and the contents of the output arguments is undefined.

◆ saveToTiff()

bool Canvas::saveToTiff ( string  filepath) const

Saves the current contents of the canvas as a TIFF file with the specified file path. The file path should already include the appropriate filename extension. If the operation was successful, the function returns true; otherwise it returns false.

◆ setColor()

void Canvas::setColor ( double  r,
double  g,
double  b 
)

Sets the color of lines drawn by subsequent invocations of drawLine(). The color is specified as three RGB components ranging from 0 (no intensity) to 1 (maximum intensity). If a component value is out of range, the result is undefined.

◆ setWidth()

void Canvas::setWidth ( double  w)

Sets the width of lines drawn by subsequent invocations of drawLine(). The line width is specified in units where the canvas size equals 1, and should be greater than 0 and smaller than 1. If the value is out of range, the result is undefined.


The documentation for this class was generated from the following file: