void Lab_Reflect(double a[][4], int n, double pt1[3], double
pt2[3],
double b[][4])
/*
function: to generate a
mirror image of a set of vertices
with respect to a mirroring line defined
by its
end points.
input: a[][4] - a set of vertices
a[i][0] -
the x-coordinate of the (i+1)th vertex
a[i][1] -
the y-coordinate of the (i+1)th vertex
a[i][2] -
the z-coordinate of the (i+1)th vertex
a[i][3] -
the h-coordinate of the (i+1)th vertex
n - no. of vertices specified in a[][]
pt1[3] - the first end
point of the mirroring line
pt1[0] - x coordinate
pt1[1] - y coordinate
pt1[2] - z coordinate
pt2[3] - the second end
point of the mirroring line
pt2[0] - x coordinate
pt2[1] - y coordinate
pt2[2] - z coordinate
output: b[][4] - the set of transformed
vertices
b[i][0] - the x-coord of the (i+1)th mirrored
vertex
b[i][1] - the y-coord of the (i+1)th mirrored
vertex
b[i][2] - the z-coord of the (i+1)th mirrored
vertex
b[i][3] - the h-coord of the (i+1)th mirrored
vertex
*/
{
/* determine angle of rotation the brings the
mirror line to
become horizontal */
double dx, dy;
dx = pt2[0] - pt1[0];
dy = pt2[1] - pt1[1];
double ang;
if (fabs(dx) < 0.0001) ang = 1.57079; //
mirror line vertical
else ang = atan2(dy, dx);
ang = -1.0*ang; // inverse the sign
/* store the sine and cosine of the angle, to
be used in the
rotation matrices */
double cosine = cos(ang);
double sine = sin(ang);
// setup the translation matrix
double Mtrans1[4][4];
matrix_identity(Mtrans1);
for (int i=0; i<3; i++)
Mtrans1[3][i] =
-1.0*pt1[i];
// setup the inverse translation matrix
double Mtrans2[4][4];
matrix_identity(Mtrans2);
for (i=0; i<3; i++)
Mtrans2[3][i] = pt1[i];
// setup the rotation matrix
double Mrot1[4][4];
matrix_identity(Mrot1);
Mrot1[0][0] = cosine;
Mrot1[1][1] = cosine;
Mrot1[0][1] = sine;
Mrot1[1][0] = -1.0*sine;
double Mrot2[4][4];
matrix_identity(Mrot2);
Mrot2[0][0] = cosine;
Mrot2[1][1] = cosine;
Mrot2[0][1] = -1.0*sine;
Mrot2[1][0] = sine;
// setup the reflection matrix
double Mref[4][4];
matrix_identity(Mref);
Mref[1][1]=-1.0;
// setup the concatenated matrix
double Mcon[4][4], temp1[4][4], temp2[4][4],
temp3[4][4];
matrix_matrix(Mtrans1, Mrot1, temp1);
matrix_matrix(temp1, Mref, temp2);
matrix_matrix(temp2, Mrot2, temp3);
matrix_matrix(temp3, Mtrans2, Mcon);
// Apply multiplication to all vertices
for (int k=0; k<n; k++) // loop for all
vertices
vector_matrix(a[k], Mcon, b[k]);
}