1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
|
/**
** sipp - SImple Polygon Processor
**
** A general 3d graphic package
**
** Copyright Equivalent Software HB 1992
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 1, or any later version.
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
** You can receive a copy of the GNU General Public License from the
** Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
**/
/**
** geometric.c - Matrixes, transformations and coordinates.
**/
#include <stdio.h>
#include <math.h>
#include <sipp.h>
#include <smalloc.h>
#include <geometric.h>
/* =================================================================== */
/* */
Transf_mat ident_matrix = {{ /* Unit tranfs. matrix */
{ 1.0, 0.0, 0.0 },
{ 0.0, 1.0, 0.0 },
{ 0.0, 0.0, 1.0 },
{ 0.0, 0.0, 0.0 }
}};
/* =================================================================== */
/*
* Allocate a new matrix, and if INITMAT != NULL copy the contents
* of INITMAT to the new matrix, otherwise copy the identity matrix
* to the new matrix.
*/
Transf_mat *
transf_mat_create(initmat)
Transf_mat * initmat;
{
Transf_mat * mat;
mat = (Transf_mat *) smalloc(sizeof(Transf_mat));
if (initmat != NULL)
MatCopy(mat, initmat);
else
MatCopy(mat, &ident_matrix);
return mat;
}
void
transf_mat_destruct(mat)
Transf_mat * mat;
{
sfree(mat);
}
/* =================================================================== */
/* Transformation routines (see also geometric.h) */
/*
* Normalize a vector.
*/
void
vecnorm(vec)
Vector *vec;
{
double len;
len = VecLen(*vec);
if (len == 0.0) { /* As Mark says, we could really use error handling...*/
MakeVector(*vec, 0.0, 0.0, 0.0);
} else {
VecScalMul(*vec, 1.0 / len, *vec);
}
}
/*
* Set MAT to the transformation matrix that represents the
* concatenation between the previous transformation in MAT
* and a translation along the vector described by DX, DY and DZ.
*
* [a b c 0] [ 1 0 0 0] [ a b c 0]
* [d e f 0] [ 0 1 0 0] [ d e f 0]
* [g h i 0] * [ 0 0 1 0] = [ g h i 0]
* [j k l 1] [Tx Ty Tz 1] [j+Tx k+Ty l+Tz 1]
*/
void
mat_translate(mat, dx, dy, dz)
Transf_mat * mat;
double dx;
double dy;
double dz;
{
mat->mat[3][0] += dx;
mat->mat[3][1] += dy;
mat->mat[3][2] += dz;
}
/*
* Set MAT to the transformation matrix that represents the
* concatenation between the previous transformation in MAT
* and a rotation with the angle ANG around the X axis.
*
* [a b c 0] [1 0 0 0] [a b*Ca-c*Sa b*Sa+c*Ca 0]
* [d e f 0] [0 Ca Sa 0] [d e*Ca-f*Sa e*Sa+f*Ca 0]
* [g h i 0] * [0 -Sa Ca 0] = [g h*Ca-i*Sa h*Sa+i*Ca 0]
* [j k l 1] [0 0 0 1] [j k*Ca-l*Sa k*Se+l*Ca 1]
*/
void
mat_rotate_x(mat, ang)
Transf_mat * mat;
double ang;
{
double cosang;
double sinang;
double tmp;
int i;
cosang = cos(ang);
sinang = sin(ang);
if (fabs(cosang) < 1.0e-15) {
cosang = 0.0;
}
if (fabs(sinang) < 1.0e-15) {
sinang = 0.0;
}
for (i = 0; i < 4; ++i) {
tmp = mat->mat[i][1];
mat->mat[i][1] = mat->mat[i][1] * cosang
- mat->mat[i][2] * sinang;
mat->mat[i][2] = tmp * sinang + mat->mat[i][2] * cosang;
}
}
/*
* Set MAT to the transformation matrix that represents the
* concatenation between the previous transformation in MAT
* and a rotation with the angle ANG around the Y axis.
*
* [a b c 0] [Ca 0 -Sa 0] [a*Ca+c*Sa b -a*Sa+c*Ca 0]
* [d e f 0] * [ 0 1 0 0] = [d*Ca+f*Sa e -d*Sa+f*Ca 0]
* [g h i 0] [Sa 0 Ca 0] [g*Ca+i*Sa h -g*Sa+i*Ca 0]
* [j k l 1] [ 0 0 0 1] [j*Ca+l*Sa k -j*Sa+l*Ca 1]
*/
void
mat_rotate_y(mat, ang)
Transf_mat * mat;
double ang;
{
double cosang;
double sinang;
double tmp;
int i;
cosang = cos(ang);
sinang = sin(ang);
if (fabs(cosang) < 1.0e-15) {
cosang = 0.0;
}
if (fabs(sinang) < 1.0e-15) {
sinang = 0.0;
}
for (i = 0; i < 4; ++i) {
tmp = mat->mat[i][0];
mat->mat[i][0] = mat->mat[i][0] * cosang
+ mat->mat[i][2] * sinang;
mat->mat[i][2] = -tmp * sinang + mat->mat[i][2] * cosang;
}
}
/*
* Set MAT to the transformation matrix that represents the
* concatenation between the previous transformation in MAT
* and a rotation with the angle ANG around the Z axis.
*
* [a b c 0] [ Ca Sa 0 0] [a*Ca-b*Sa a*Sa+b*Ca c 0]
* [d e f 0] [-Sa Ca 0 0] [d*Ca-e*Sa d*Sa+e*Ca f 0]
* [g h i 0] * [ 0 0 1 0] = [g*Ca-h*Sa g*Sa+h*Ca i 0]
* [j k l 1] [ 0 0 0 1] [j*Ca-k*Sa j*Sa+k*Ca l 0]
*/
void
mat_rotate_z(mat, ang)
Transf_mat * mat;
double ang;
{
double cosang;
double sinang;
double tmp;
int i;
cosang = cos(ang);
sinang = sin(ang);
if (fabs(cosang) < 1.0e-15) {
cosang = 0.0;
}
if (fabs(sinang) < 1.0e-15) {
sinang = 0.0;
}
for (i = 0; i < 4; ++i) {
tmp = mat->mat[i][0];
mat->mat[i][0] = mat->mat[i][0] * cosang
- mat->mat[i][1] * sinang;
mat->mat[i][1] = tmp * sinang + mat->mat[i][1] * cosang;
}
}
/*
* Set MAT to the transformation matrix that represents the
* concatenation between the previous transformation in MAT
* and a rotation with the angle ANG around the line represented
* by the point POINT and the vector VECTOR.
*/
void
mat_rotate(mat, point, vector, ang)
Transf_mat * mat;
Vector * point;
Vector * vector;
double ang;
{
double ang2;
double ang3;
ang2 = atan2(vector->y, vector->x);
ang3 = atan2(hypot(vector->x, vector->y), vector->z);
mat_translate(mat, -point->x, -point->y, -point->z);
mat_rotate_z(mat, -ang2);
mat_rotate_y(mat, -ang3);
mat_rotate_z(mat, ang);
mat_rotate_y(mat, ang3);
mat_rotate_z(mat, ang2);
mat_translate(mat, point->x, point->y, point->z);
}
/*
* Set MAT to the transformation matrix that represents the
* concatenation between the previous transformation in MAT
* and a scaling with the scaling factors XSCALE, YSCALE and ZSCALE
*
* [a b c 0] [Sx 0 0 0] [a*Sx b*Sy c*Sz 0]
* [d e f 0] [ 0 Sy 0 0] [d*Sx e*Sy f*Sz 0]
* [g h i 0] * [ 0 0 Sz 0] = [g*Sx h*Sy i*Sz 0]
* [j k l 1] [ 0 0 0 1] [j*Sx k*Sy l*Sz 1]
*/
void
mat_scale(mat, xscale, yscale, zscale)
Transf_mat * mat;
double xscale;
double yscale;
double zscale;
{
int i;
for (i = 0; i < 4; ++i) {
mat->mat[i][0] *= xscale;
mat->mat[i][1] *= yscale;
mat->mat[i][2] *= zscale;
}
}
/*
* Set MAT to the transformation matrix that represents the
* concatenation between the previous transformation in MAT
* and a mirroring in the plane defined by the point POINT
* and the normal vector NORM.
*/
void
mat_mirror_plane(mat, point, norm)
Transf_mat * mat;
Vector * point;
Vector * norm;
{
Transf_mat tmp;
double factor;
/* The first thing we do is to make a transformation matrix */
/* for mirroring through a plane with the same normal vector */
/* as our, but through the origin instead. */
factor = 2.0 / (norm->x * norm->x + norm->y * norm->y
+ norm->z * norm->z);
/* The diagonal elements. */
tmp.mat[0][0] = 1 - factor * norm->x * norm->x;
tmp.mat[1][1] = 1 - factor * norm->y * norm->y;
tmp.mat[2][2] = 1 - factor * norm->z * norm->z;
/* The rest of the matrix */
tmp.mat[1][0] = tmp.mat[0][1] = -factor * norm->x * norm->y;
tmp.mat[2][0] = tmp.mat[0][2] = -factor * norm->x * norm->z;
tmp.mat[2][1] = tmp.mat[1][2] = -factor * norm->y * norm->z;
tmp.mat[3][0] = tmp.mat[3][1] = tmp.mat[3][2] = 0.0;
/* Do the actual transformation. This is done in 3 steps: */
/* 1) Translate the plane so that it goes through the origin. */
/* 2) Do the actual mirroring. */
/* 3) Translate it all back to the starting position. */
mat_translate(mat, -point->x, -point->y, -point->z);
mat_mul(mat, mat, &tmp);
mat_translate(mat, point->x, point->y, point->z);
}
/*
* Multiply the Matrix A with the Matrix B, and store the result
* into the Matrix RES. It is possible for RES to point to the
* same Matrix as either A or B since the result is stored into
* a temporary during computation.
*
* [a b c 0] [A B C 0] [aA+bD+cG aB+bE+cH aC+bF+cI 0]
* [d e f 0] [D E F 0] [dA+eD+fG dB+eE+fH dC+eF+fI 0]
* [g h i 0] [G H I 0] = [gA+hD+iG gB+hE+iH gC+hF+iI 0]
* [j k l 1] [J K L 1] [jA+kD+lG+J jB+kE+lH+K jC+kF+lI+L 1]
*/
void
mat_mul(res, a, b)
Transf_mat * res;
Transf_mat * a;
Transf_mat * b;
{
Transf_mat tmp;
int i;
for (i = 0; i < 4; ++i) {
tmp.mat[i][0] = a->mat[i][0] * b->mat[0][0]
+ a->mat[i][1] * b->mat[1][0]
+ a->mat[i][2] * b->mat[2][0];
tmp.mat[i][1] = a->mat[i][0] * b->mat[0][1]
+ a->mat[i][1] * b->mat[1][1]
+ a->mat[i][2] * b->mat[2][1];
tmp.mat[i][2] = a->mat[i][0] * b->mat[0][2]
+ a->mat[i][1] * b->mat[1][2]
+ a->mat[i][2] * b->mat[2][2];
}
tmp.mat[3][0] += b->mat[3][0];
tmp.mat[3][1] += b->mat[3][1];
tmp.mat[3][2] += b->mat[3][2];
MatCopy(res, &tmp);
}
/*
* Transform the Point3d VEC with the transformation matrix MAT, and
* put the result into the vector *RES.
*
* [a b c 0]
* [d e f 0]
* [x y z 1] [g h i 0] = [ax+dy+gz+j bx+ey+hz+k cx+fy+iz+l 1]
* [j k l 1]
*/
void
point_transform(res, vec, mat)
Vector * res;
Vector * vec;
Transf_mat * mat;
{
res->x = mat->mat[0][0] * vec->x + mat->mat[1][0] * vec->y
+ mat->mat[2][0] * vec->z + mat->mat[3][0];
res->y = mat->mat[0][1] * vec->x + mat->mat[1][1] * vec->y
+ mat->mat[2][1] * vec->z + mat->mat[3][1];
res->z = mat->mat[0][2] * vec->x + mat->mat[1][2] * vec->y
+ mat->mat[2][2] * vec->z + mat->mat[3][2];
}
|