CS488 PA6
|
#include <cmath>
#include "../pa5/Matrix.hpp"
#include "../pa5/Vector.hpp"
Go to the source code of this file.
Functions | |
template<int N> | |
double | area (const Matrix< 2, 1 > &a, const Matrix< 2, 1 > &b, const Matrix< 2, 1 > &c) |
template<int N> | |
Vector< N > | barycentricCombination (const Matrix< N, 1 > &q, const Matrix< N, 1 > &r, double alpha) |
template<int R, int C> | |
Matrix< R, 2 > | getBounds (const Matrix< R, C > &p) |
template<int N> | |
bool | inside (const Matrix< 2, 1 > &p, const Matrix< 2, 1 > &r, const Matrix< 2, 1 > &s, const Matrix< 2, 1 > &t) |
template<int N> | |
bool | intersect (const Matrix< 2, 1 > &p, const Matrix< 2, 1 > &q, const Matrix< 2, 1 > &r, const Matrix< 2, 1 > &s, double *alpha, double *beta) |
template<int N> | |
Vector< 2 > | perp (const Matrix< 2, 1 > &a) |
template<int N> | |
double | toImplicit (const Matrix< 2, 1 > &p, const Matrix< 2, 1 > &q, Matrix< 2, 1 > *n) |
A template for a utility class that can perform various calculations required by 2-D and 3-D rasterizers.
Note: Since we don't need to transform 2D points, they are in Cartesian (rather than homogeneous) coordinates and stored in a Matrix<2,1> (i.e., a 2-element column vector which is the same as a Vector<2>). This also makes it a little easier to calculate the area and determine whether a point is inside of a triangle.
Note: area(), inside(), and toImplicit() are templated so that they are one-to-one with Matrix.
double area | ( | const Matrix< 2, 1 > & | a, |
const Matrix< 2, 1 > & | b, | ||
const Matrix< 2, 1 > & | c | ||
) |
Compute the area of a triangle from its three vertices.
The vertices can be (i.e., are) assumed to be ordered correctly (i.e., obey the right-hand rule).
Note: This is a function template to avoid problems of duplicate definitions when linking.
N | The dimensionality (always 2) |
a | The first vertex |
b | The second vertex |
c | The third vertex |
Vector< N > barycentricCombination | ( | const Matrix< N, 1 > & | q, |
const Matrix< N, 1 > & | r, | ||
double | alpha | ||
) |
Returns the point \( \alpha \mathbf{q} + (1-\alpha) \mathbf{r} \).
N | The dimensionality |
q | One point |
r | The other points |
alpha | The weight |
Matrix< R, 2 > getBounds | ( | const Matrix< R, C > & | p | ) |
Returns the bounds of the given line, triangle, or polygon.
This method returns a matrix with two columns. Column 0 contains the minimums value and column 1 contains the maximum values. (The rows correspond to the rows of the points.)
R | The number of rows (i.e., the dimensionality of each point) |
C | The number of columns (i.e., the number of points) |
p | The line, triangle, or polygon |
bool inside | ( | const Matrix< 2, 1 > & | p, |
const Matrix< 2, 1 > & | r, | ||
const Matrix< 2, 1 > & | s, | ||
const Matrix< 2, 1 > & | t | ||
) |
Determine whether the point p is inside the triangle formed by the vertices r, s, t.
Note: This is a function template to avoid problems of duplicate definitions when linking
N | The dimensionality (always 2) |
p | The test point |
r | The first vertex of the triangle |
s | The second vertex of the triangle |
t | The third vertex of the triangle |
bool intersect | ( | const Matrix< 2, 1 > & | p, |
const Matrix< 2, 1 > & | q, | ||
const Matrix< 2, 1 > & | r, | ||
const Matrix< 2, 1 > & | s, | ||
double * | alpha, | ||
double * | beta | ||
) |
Find the intersection of two lines (NOT line segments).
This method returns false if the lines are parallel and true if the lines intersect.
If the lines intersect it calculates the (convex combination) weights that define the intersection point. Letting alpha denote the weight for line 0 and beta denote the weight for line 1, the intersection point is given by:
\( [ \alpha p_0 + (1-\alpha) q_0 , \alpha p_1 + (1-\alpha) q_1 ] \)
and or:
\( [ \beta r_0 + (1-\beta) s_0 , \beta r_1 + (1-\beta) s_1 ] \)
If alpha is in [0, 1] then the intersection point is within the line segment from p to q. Similarly, if beta is in [0, 1] then the intersection point is within the line segment from r to s.
N | The dimensionality (always 2) |
p | One endpoint of the line from p to q |
q | The other endpoint of the line from p to q |
r | One endpoint of the line from r to s |
s | The other endpoint of the line from r to s |
alpha | One of the weights (outbound) |
beta | The other weight (outbound) |
Vector< 2 > perp | ( | const Matrix< 2, 1 > & | a | ) |
Find a perpendicular to the given 2-vector.
N | The dimensionality (always 2) |
a | The 2-vector (which is a Matrix<2, 1> to increase flexibility) |
double toImplicit | ( | const Matrix< 2, 1 > & | p, |
const Matrix< 2, 1 > & | q, | ||
Matrix< 2, 1 > * | n | ||
) |
Compute the implicit form of a line defined by two points.
The implicit form is the set of points \( \mathbf{r} \) that satisfy \( \mathbf{n} \cdot \mathbf{r} = \mathbf{b} \)
n is passed into this method empty and is filled. b is returned.
Note: This is a function template to avoid problems of duplicate definitions when linking
N | The dimensionality (always 2) |
p | One endpoint |
q | The other endpoint |
n | The vector parameter of the implict form (returned) |