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 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130
|
/*
Copyright (C) 2003 by Sean David Fleming
sean@ivec.org
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 2
of the License, or (at your option) 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 should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
The GNU GPL can also be found at http://www.gnu.org
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "gdis.h"
#include "coords.h"
#include "edit.h"
#include "matrix.h"
#include "quaternion.h"
#include "model.h"
#include "morph.h"
#include "opengl.h"
#include "render.h"
#include "select.h"
#include "space.h"
#include "spatial.h"
#include "interface.h"
/* main structure */
extern struct sysenv_pak sysenv;
/**********************************************************/
/* adjust a rotation matrix wrt curent camera position */
/**********************************************************/
void matrix_camera_transform(gdouble *rot, struct model_pak *model)
{
gdouble q[9], qi[9], mat[9];
g_assert(model != NULL);
/* build component matrices */
quat_matrix(qi, camera_q(model->camera));
quat_matrix(q, camera_q(model->camera));
matrix_invert(qi);
/* build transformation */
memcpy(mat, model->latmat, 9*sizeof(gdouble));
matmat(qi, mat);
matmat(rot, mat);
matmat(q, mat);
matmat(model->ilatmat, mat);
memcpy(rot, mat, 9*sizeof(gdouble));
}
/**********************************************************/
/* construct a rotation matrix wrt curent camera position */
/**********************************************************/
void matrix_relative_rotation(gdouble *m, gdouble a, gint type, struct model_pak *model)
{
g_assert(model != NULL);
/* get rotation and transform according to camera */
matrix_rotation(m, a, type);
matrix_camera_transform(m, model);
}
/*******************************/
/* multiply two (3x3) matrices */
/*******************************/
void matmat(gdouble *mat1, gdouble *mat2)
/* mat2 -> mat1*mat2 */
{
gint i;
gdouble tmp[9], *res, *ptr1, *ptr2;
/* init */
matrix_transpose(mat2);
res = tmp;
ptr1 = mat1;
/* mult - loop over rows of mat 1*/
for (i=3 ; i-- ; )
{
ptr2 = mat2;
*res = *(ptr1++) * (*(ptr2++));
*res += *(ptr1++) * (*(ptr2++));
*(res++) += *(ptr1--) * (*(ptr2++));
ptr1--;
*res = *(ptr1++) * (*(ptr2++));
*res += *(ptr1++) * (*(ptr2++));
*(res++) += *(ptr1--) * (*(ptr2++));
ptr1--;
*res = *(ptr1++) * (*(ptr2++));
*res += *(ptr1++) * (*(ptr2++));
*(res++) += *(ptr1++) * (*ptr2);
}
/* copy result to mat2 */
memcpy(mat2,tmp,9*sizeof(gdouble));
}
/*******************************/
/* multiply two (4x4) matrices */
/*******************************/
void mat4mat(gdouble *mat1, gdouble *mat2)
/* mat2 -> mat1*mat2 */
{
gint i;
gdouble tmp[16], *res, *ptr1, *ptr2;
/* init */
matrix_transpose_44(mat2);
res = tmp;
ptr1 = mat1;
/* mult - loop over rows of mat 1*/
for (i=4 ; i-- ; )
{
ptr2 = mat2;
*res = *(ptr1++) * (*(ptr2++));
*res += *(ptr1++) * (*(ptr2++));
*res += *(ptr1++) * (*(ptr2++));
*(res++) += *(ptr1--) * (*(ptr2++));
ptr1--;
ptr1--;
*res = *(ptr1++) * (*(ptr2++));
*res += *(ptr1++) * (*(ptr2++));
*res += *(ptr1++) * (*(ptr2++));
*(res++) += *(ptr1--) * (*(ptr2++));
ptr1--;
ptr1--;
*res = *(ptr1++) * (*(ptr2++));
*res += *(ptr1++) * (*(ptr2++));
*res += *(ptr1++) * (*(ptr2++));
*(res++) += *(ptr1--) * (*(ptr2++));
ptr1--;
ptr1--;
*res = *(ptr1++) * (*(ptr2++));
*res += *(ptr1++) * (*(ptr2++));
*res += *(ptr1++) * (*(ptr2++));
*(res++) += *(ptr1++) * (*ptr2);
}
/* copy result to mat2 */
memcpy(mat2,tmp,16*sizeof(gdouble));
return;
}
/****************************************/
/* transform a 3D vector by matrix mult */
/****************************************/
void vecmat(gdouble *mat, gdouble *vec)
{
gdouble x, y, z;
gdouble *mptr=mat, *vptr=vec;
/* init */
x = *vptr++;
y = *vptr++;
z = *vptr--;
/* mult */
*(--vptr) *= *mptr++;
*vptr += *mptr++ * y;
*vptr++ += *mptr++ * z;
*vptr = *mptr++ * x;
*vptr += *mptr++ * y;
*vptr++ += *mptr++ * z;
*vptr = *mptr++ * x;
*vptr += *mptr++ * y;
*vptr += *mptr * z;
}
/****************************************/
/* transform a 4D vector by matrix mult */
/****************************************/
void vec4mat(gdouble *mat, gdouble *vec)
{
gdouble x, y, z, t;
gdouble *mptr=mat, *vptr=vec;
/* init */
x = *vptr++;
y = *vptr++;
z = *vptr++;
t = *vptr--;
vptr--;
vptr--;
/* mult */
*vptr *= *mptr++;
*vptr += *mptr++ * y;
*vptr += *mptr++ * z;
*vptr++ += *mptr++ * t;
*vptr = *mptr++ * x;
*vptr += *mptr++ * y;
*vptr += *mptr++ * z;
*vptr++ += *mptr++ * t;
*vptr = *mptr++ * x;
*vptr += *mptr++ * y;
*vptr += *mptr++ * z;
*vptr++ += *mptr++ * t;
*vptr = *mptr++ * x;
*vptr += *mptr++ * y;
*vptr += *mptr++ * z;
*vptr += *mptr * t;
}
/************************************************************/
/* transform a 3D vector by matrix mult (transposed matrix) */
/************************************************************/
void vectmat(gdouble *mat, gdouble *vec)
{
gdouble x, y, z;
gdouble *mptr=mat, *vptr=vec;
/* init */
x = *vptr++;
y = *vptr++;
z = *vptr--;
/* mult */
*(--vptr) *= *mptr++;
*(++vptr) *= *mptr++;
*(++vptr) *= *mptr++;
vptr--;
*(--vptr) += *mptr++ * y;
*(++vptr) += *mptr++ * y;
*(++vptr) += *mptr++ * y;
vptr--;
*(--vptr) += *mptr++ * z;
*(++vptr) += *mptr++ * z;
*(++vptr) += *mptr * z;
}
/*****************/
/* transpose 4x4 */
/*****************/
void matrix_transpose_44(gdouble *mat)
{
gdouble tmp[16], *ptr=mat;
memcpy(tmp, mat, 16*sizeof(gdouble));
ptr++;
*ptr++ = tmp[4];
*ptr++ = tmp[8];
*ptr++ = tmp[12];
*ptr++ = tmp[1];
ptr++;
*ptr++ = tmp[9];
*ptr++ = tmp[13];
*ptr++ = tmp[2];
*ptr++ = tmp[6];
ptr++;
*ptr++ = tmp[14];
*ptr++ = tmp[3];
*ptr++ = tmp[7];
*ptr = tmp[11];
}
/*****************/
/* transpose 3x3 */
/*****************/
void matrix_transpose(gdouble *mat)
{
gdouble tmp[9], *ptr=mat;
memcpy(tmp, mat, 9*sizeof(gdouble));
ptr++;
*ptr++ = tmp[3];
*ptr++ = tmp[6];
*ptr++ = tmp[1];
ptr++;
*ptr++ = tmp[7];
*ptr++ = tmp[2];
*ptr = tmp[5];
}
void make_cofmat(gdouble *cof, gdouble *mat)
{
cof[0] = mat[4]*mat[8] - mat[5]*mat[7];
cof[1] = mat[5]*mat[6] - mat[3]*mat[8];
cof[2] = mat[3]*mat[7] - mat[4]*mat[6];
cof[3] = mat[2]*mat[7] - mat[1]*mat[8];
cof[4] = mat[0]*mat[8] - mat[2]*mat[6];
cof[5] = mat[1]*mat[6] - mat[0]*mat[7];
cof[6] = mat[1]*mat[5] - mat[2]*mat[4];
cof[7] = mat[2]*mat[3] - mat[0]*mat[5];
cof[8] = mat[0]*mat[4] - mat[1]*mat[3];
}
/********************************/
/* new matrix inversion routine */
/********************************/
#define DEBUG_INVMAT 0
gint matrix_invert(gdouble *mat)
{
gdouble d, id;
gdouble cof[9];
/* determinant */
d = matrix_determinant(mat);
#if DEBUG_INVMAT
P3MAT("input:", mat);
printf("determinant = %f\n", d);
#endif
/* FIXME - what is a good value to use here??? */
if (fabs(d) < 0.0001)
{
gui_text_show(ERROR, "Bad lattice matrix.");
return(1);
}
id = 1.0/d;
/* compute co-factor matrix */
make_cofmat(cof, mat);
matrix_transpose(cof);
VEC3MUL(&cof[0], id);
VEC3MUL(&cof[3], id);
VEC3MUL(&cof[6], id);
#if DEBUG_INVMAT
P3MAT("inverse matrix: ", cof);
#endif
matmat(cof, mat);
/* TODO - checks */
#if DEBUG_INVMAT
P3MAT("Should be I: ", mat);
#endif
memcpy(mat,cof,9*sizeof(gdouble));
return(0);
}
/****************************/
/* general vector magnitude */
/****************************/
gdouble magnitude(gdouble *vec, gint dim)
{
gint i;
gdouble sum=0.0;
for (i=0 ; i<dim ; i++)
sum += (*(vec+i)) * (*(vec+i));
return(sqrt(sum));
}
/********************************/
/* general vector normalization */
/********************************/
gint normalize(gdouble *vec, gint dim)
{
gint i;
gdouble len, *ptr=vec;
len = magnitude(vec, dim);
if (len < FRACTION_TOLERANCE)
return(1);
for (i=0 ; i<dim ; i++)
*ptr++ /= len;
return(0);
}
/*****************************/
/* vector intersection angle */
/*****************************/
gdouble via(gdouble *vec1, gdouble *vec2, gint dim)
{
gint i;
gdouble lenprod, dot=0.0;
gdouble *ptr1=vec1, *ptr2=vec2;
gdouble cosa=0.0;
for (i=0 ; i<dim ; i++)
dot += (*ptr1++) * (*ptr2++);
lenprod = magnitude(vec1, dim) * magnitude(vec2, dim);
/* get cos of the angle */
if (lenprod > FRACTION_TOLERANCE)
cosa = dot / lenprod;
/* enforce range */
cosa = CLAMP(cosa, -1.0, 1.0);
return(acos(cosa));
}
/*************************/
/* cross product for 3x3 */
/*************************/
void crossprod(gdouble *res, gdouble *vec1, gdouble *vec2)
{
*(res+0) = *(vec1+1) * *(vec2+2) - *(vec1+2) * *(vec2+1);
*(res+1) = *(vec1+2) * *(vec2) - *(vec1) * *(vec2+2);
*(res+2) = *(vec1) * *(vec2+1) - *(vec1+1) * *(vec2);
}
/***************************************************/
/* calculate normal of a plane defined by 3 points */
/***************************************************/
void calc_norm(gdouble *res, gdouble *a, gdouble *b, gdouble *c)
{
gdouble vec1[3], vec2[3];
/* a-b */
ARR3SET(vec1, b);
ARR3SUB(vec1, a);
/* a-c */
ARR3SET(vec2, c);
ARR3SUB(vec2, a);
/* normal */
crossprod(res, vec1, vec2);
}
/*********************************************/
/* calculate the distance between two points */
/*********************************************/
gdouble calc_sep(gdouble *a, gdouble *b)
{
gdouble x[3];
ARR3SET(x, a);
ARR3SUB(x, b);
return(VEC3MAG(x));
}
/*****************************/
/* project vector onto plane */
/*****************************/
void proj_vop(gdouble *res, gdouble *vec, gdouble *plane_norm)
{
gdouble len;
gdouble tmp[3];
/* project vector onto normal */
/* NB: assumes plane_norm is normalized */
ARR3SET(tmp, plane_norm);
ARR3MUL(tmp, vec);
len = tmp[0] + tmp[1] + tmp[2];
/* get vector to plane */
ARR3SET(tmp, plane_norm);
VEC3MUL(tmp, len);
/* subtract to get projected vector */
ARR3SET(res, vec);
ARR3SUB(res, tmp);
}
/***********************/
/* determinant for 3x3 */
/***********************/
gdouble matrix_determinant(gdouble *mat)
{
gdouble vec1[3], vec2[3], vec3[3], tmp[3];
/* make vectors from columns */
VEC3SET(vec1, mat[0], mat[3], mat[6]);
VEC3SET(vec2, mat[1], mat[4], mat[7]);
VEC3SET(vec3, mat[2], mat[5], mat[8]);
/* get cross product of 2 & 3 */
crossprod(tmp, vec2, vec3);
/* return dot prod with 1 */
ARR3MUL(vec1, tmp);
return(vec1[0]+vec1[1]+vec1[2]);
}
/**********************************************/
/* compute volume, assuming 3D lattice matrix */
/**********************************************/
gdouble calc_volume(gdouble *latmat)
{
gdouble vec[3], tmat[9];
/* NB: latmat has cell vectors in columns */
memcpy(tmat, latmat, 9*sizeof(gdouble));
matrix_transpose(tmat);
/* compute volume */
crossprod(vec, &tmat[0], &tmat[3]);
ARR3MUL(vec, &tmat[6]);
return(fabs(vec[0]+vec[1]+vec[2]));
}
/****************************************************/
/* compute surface area, assuming 2D lattice matrix */
/****************************************************/
gdouble calc_area(gdouble *latmat)
{
gdouble a, b, c, tmat[9];
/* NB: latmat has cell vectors in columns */
memcpy(tmat, latmat, 9*sizeof(gdouble));
matrix_transpose(tmat);
/* get lengths and angle between */
a = VEC3MAG(&tmat[0]);
b = VEC3MAG(&tmat[3]);
c = via(&tmat[0], &tmat[3], 3);
/* return volume */
return(a * b * sin(c));
}
/*****************************/
/* initialize a 3x3 identity */
/*****************************/
void matrix_identity(gdouble *mat)
{
VEC3SET(&mat[0], 1.0, 0.0, 0.0);
VEC3SET(&mat[3], 0.0, 1.0, 0.0);
VEC3SET(&mat[6], 0.0, 0.0, 1.0);
}
/************************************************************/
/* assign a transformation matrix that aligns a vector to z */
/************************************************************/
#define DEBUG_Z_ALIGNMENT 0
void matrix_z_alignment(gdouble *mat, gdouble *vec)
{
gdouble len, proj, iproj, n[3], mop[9];
/* default return matrix */
matrix_identity(mat);
ARR3SET(n, vec);
/* check */
len = VEC3MAG(n);
if (len < FRACTION_TOLERANCE)
{
printf("matrix_z_alignment(): bad orientation vector.\n");
return;
}
/* normalize */
VEC3MUL(n, 1.0/len);
proj = sqrt(n[0]*n[0] + n[1]*n[1]);
iproj = 1.0 / proj;
/* rot z -> yz plane */
if (proj > FRACTION_TOLERANCE)
{
VEC3SET(&mop[0], n[1]*iproj, -n[0]*iproj, 0.0);
VEC3SET(&mop[3], n[0]*iproj, n[1]*iproj, 0.0);
VEC3SET(&mop[6], 0.0, 0.0, 1.0);
matmat(mop, mat);
}
/* rot x -> colinear with z */
VEC3SET(&mop[0], 1.0, 0.0, 0.0);
VEC3SET(&mop[3], 0.0, n[2], -proj);
VEC3SET(&mop[6], 0.0, proj, n[2]);
matmat(mop, mat);
#if DEBUG_Z_ALIGNMENT
ARR3SET(n, vec);
vecmat(mat, n);
P3MAT("mat: ", mat);
P3VEC("v0: ", vec);
P3VEC("v1: ", n);
#endif
}
/********************************************************/
/* arbitrary alignment, maps v1 to be co-linear with v2 */
/********************************************************/
#define DEBUG_V_ALIGNMENT 0
void matrix_v_alignment(gdouble *m1, gdouble *v1, gdouble *v2)
{
gdouble m2[9];
/* map v1 to z, then to v2 via inverse of v2's z alignment */
matrix_z_alignment(m1, v1);
matrix_z_alignment(m2, v2);
matrix_invert(m2);
matmat(m2, m1);
}
/****************************************************/
/* construct rotation matrix about arbitrary vector */
/****************************************************/
void matrix_v_rotation(gdouble *mat, gdouble *v, gdouble angle)
{
gdouble m1[9], m2[9];
/* align with z */
matrix_z_alignment(m1, v);
memcpy(mat, m1, 9*sizeof(gdouble));
/* rotation operation (about z) */
matrix_rotation(m2, angle, YAW);
matmat(m2, mat);
/* inverse z alignment */
matrix_invert(m1);
matmat(m1, mat);
}
/****************************************************/
/* construct rotation matrix about arbitrary vector */
/****************************************************/
void matrix_v_reflection(gdouble *mat, gdouble *v)
{
gdouble m1[9], m2[9];
/* align with z */
matrix_z_alignment(m1, v);
memcpy(mat, m1, 9*sizeof(gdouble));
/* reflection about z */
matrix_identity(m2);
m2[8] = -1.0;
matmat(m2, mat);
/* inverse z alignment */
matrix_invert(m1);
matmat(m1, mat);
}
/*******************************/
/* construct a rotation matrix */
/*******************************/
void matrix_rotation(gdouble *rot, gdouble da, gint type)
{
gdouble cosa, sina;
/* precalc */
cosa = cos(da);
sina = sin(da);
/* dependant matrix elements */
switch(type)
{
case PITCH:
VEC3SET(&rot[0], 1.0, 0.0, 0.0);
VEC3SET(&rot[3], 0.0, cosa,-sina);
VEC3SET(&rot[6], 0.0, sina, cosa);
break;
case YAW:
VEC3SET(&rot[0], cosa, sina, 0.0);
VEC3SET(&rot[3], -sina, cosa, 0.0);
VEC3SET(&rot[6], 0.0, 0.0, 1.0);
break;
case ROLL:
VEC3SET(&rot[0], cosa, 0.0,-sina);
VEC3SET(&rot[3], 0.0, 1.0, 0.0);
VEC3SET(&rot[6], sina, 0.0, cosa);
break;
case INITIAL:
default:
VEC3SET(&rot[0], 1.0, 0.0, 0.0);
VEC3SET(&rot[3], 0.0, 1.0, 0.0);
VEC3SET(&rot[6], 0.0, 0.0, 1.0);
break;
}
}
/**************************************************************/
/* get reciprocal lattice vectors from direct lattice vectors */
/**************************************************************/
#define DEBUG_RLAT 0
void matrix_reciprocal_init(struct model_pak *data)
{
gdouble norm;
gdouble a[3], b[3], c[3];
gdouble axb[3], bxc[3], cxa[3];
#if DEBUG_RLAT
P3MAT("direct lattice matrix:",data->latmat);
#endif
/* extract direct lattice vectors */
a[0] = data->latmat[0];
a[1] = data->latmat[3];
a[2] = data->latmat[6];
b[0] = data->latmat[1];
b[1] = data->latmat[4];
b[2] = data->latmat[7];
c[0] = data->latmat[2];
c[1] = data->latmat[5];
c[2] = data->latmat[8];
/* calc intermediate products */
crossprod(bxc, b, c);
crossprod(cxa, c, a);
crossprod(axb, a, b);
norm = 1.0/(a[0]*bxc[0] + a[1]*bxc[1] + a[2]*bxc[2]);
/* create reciprocal lattice vectors */
ARR3SET(a, bxc);
ARR3SET(b, cxa);
ARR3SET(c, axb);
VEC3MUL(a, norm);
VEC3MUL(b, norm);
VEC3MUL(c, norm);
/* store */
data->rlatmat[0] = a[0];
data->rlatmat[3] = a[1];
data->rlatmat[6] = a[2];
data->rlatmat[1] = b[0];
data->rlatmat[4] = b[1];
data->rlatmat[7] = b[2];
data->rlatmat[2] = c[0];
data->rlatmat[5] = c[1];
data->rlatmat[8] = c[2];
#if DEBUG_RLAT
P3MAT("reciprocal lattice matrix:",data->rlatmat);
#endif
}
/******************************************/
/* construct the unit translation vectors */
/******************************************/
#define DEBUG_XLAT 0
void matrix_lattice_init(struct model_pak *data)
{
gdouble n[3], v1[3], v2[3], v3[3];
gdouble b1, b2, b3, c1, c2, c3;
/* use a supplied latmat, rather than generating from pbc's */
/* NB: should be in gdis style colum vector format (gulp/marvin are in rows) */
if (data->construct_pbc)
{
#if DEBUG_XLAT
printf("constructing pbc...\n");
#endif
/* get lattice vector lengths */
VEC3SET(v1, data->latmat[0], data->latmat[3], data->latmat[6]);
VEC3SET(v2, data->latmat[1], data->latmat[4], data->latmat[7]);
VEC3SET(v3, data->latmat[2], data->latmat[5], data->latmat[8]);
/* set lengths */
data->pbc[0] = VEC3MAG(v1);
data->pbc[1] = VEC3MAG(v2);
data->pbc[2] = VEC3MAG(v3);
/* get cell angles */
data->pbc[3] = via(v2,v3,3);
data->pbc[4] = via(v1,v3,3);
data->pbc[5] = via(v1,v2,3);
/* NEW - handle cell angle signs */
crossprod(n, v2 ,v3);
ARR3MUL(n, v3);
if (n[0]+n[1]+n[2] < 0.0)
data->pbc[3] *= -1.0;
crossprod(n, v1 ,v3);
ARR3MUL(n, v3);
if (n[0]+n[1]+n[2] < 0.0)
data->pbc[4] *= -1.0;
crossprod(n, v1 ,v2);
ARR3MUL(n, v3);
if (n[0]+n[1]+n[2] < 0.0)
data->pbc[5] *= -1.0;
}
else
{
#if DEBUG_XLAT
printf("constructing latmat...\n");
#endif
/* construct lattice matrix from the unit cell lengths & angles */
/* this basically works by using the cosine rule in conjunction */
/* with a few geometric constraints (eg normalized vectors) */
/* FIXME - correction needed for 1D case as well? */
if (data->periodic == 2)
{
/* lattice code requires non existent c parameter to be 1 */
data->pbc[2] = 1.0;
}
/* compute the translation vector for b */
b1 = cos(data->pbc[5]);
b2 = sin(data->pbc[5]);
b3 = 0.0; /* constrain a,b to remain on the x,y plane */
if (b2 == 0.0)
{
printf("matrix_lattice_init(): bad cell parameters.\n");
b2 = 1.0;
}
/* compute the translation vector for c */
c1 = cos(data->pbc[4]);
c2 = (2.0*cos(data->pbc[3]) + b1*b1 + b2*b2 - 2.0*b1*c1 - 1.0)/(2.0*b2);
c3 = sqrt(1.0 - c1*c1 - c2*c2);
/* assign in rows to make it easier to scale */
/* x & a are assumed co-linear */
VEC3SET(&data->latmat[0], data->pbc[0], 0.0, 0.0);
VEC3SET(&data->latmat[3], b1, b2, b3);
VEC3SET(&data->latmat[6], c1, c2, c3);
/* scale b & c vectors up */
VEC3MUL(&data->latmat[3], data->pbc[1]);
VEC3MUL(&data->latmat[6], data->pbc[2]);
/* get vectors in cols */
matrix_transpose(data->latmat);
}
/* update dependents */
make_axes(data);
make_cell(data);
memcpy(data->ilatmat, data->latmat, 9*sizeof(gdouble));
matrix_invert(data->ilatmat);
matrix_reciprocal_init(data);
switch(data->periodic)
{
case 3:
data->volume = calc_volume(data->latmat);
break;
case 2:
data->area = calc_area(data->latmat);
break;
}
/* NEW - store angles in degrees as well as radians */
ARR3SET(data->cell_angles, &data->pbc[3]);
VEC3MUL(data->cell_angles, R2D);
#if DEBUG_XLAT
printf("cell dimensions: [%6.2f %6.2f %6.2f] (%6.2f %6.2f %6.2f)\n",
data->pbc[0], data->pbc[1], data->pbc[2],
data->cell_angles[0], data->cell_angles[1], data->cell_angles[2]);
P3MAT("lattice matrix:",data->latmat);
#endif
return;
}
/****************************/
/* build new lattice matrix */
/****************************/
/* v - new orientation (may need projection if model is 2D) */
/* ix - which lattice vector to replace (0-2) */
#define DEBUG_NEW_LATTICE 0
void matrix_lattice_new(gdouble *latmat, struct model_pak *model)
{
gint i;
gint ia, ib, ic, ma, mb, mc;
gdouble tmat[9], mat1[9], mat2[9];
gdouble vec[3];
GSList *list;
struct core_pak *core;
struct shel_pak *shel;
struct model_pak *dest;
/* checks */
g_assert(latmat != NULL);
g_assert(model != NULL);
#if DEBUG_NEW_LATTICE
P3MAT("transformation matrix: :", latmat);
P3MAT("old latmat:", model->latmat);
#endif
/* get the transformation matrix */
memcpy(tmat, latmat, 9*sizeof(gdouble));
/* transpose the source lattice matrix */
memcpy(mat1, model->latmat, 9*sizeof(gdouble));
matrix_transpose(mat1);
/* get the new lattice matrix */
matmat(tmat, mat1);
matrix_transpose(mat1);
#if DEBUG_NEW_LATTICE
P3MAT("new latmat:", mat1);
#endif
memcpy(mat2, mat1, 9*sizeof(gdouble));
if (matrix_invert(mat2))
{
gui_text_show(ERROR, "Two or more lattice vectors are not independant.");
return;
}
/* transformation -> new model */
dest = model_new();
dest->periodic = model->periodic;
memcpy(dest->latmat, mat1, 9*sizeof(gdouble));
gulp_data_copy(model, dest);
gulp_extra_copy(model, dest);
/* setup repeats required to fill the new cell */
ma=mb=mc=1;
/* a direction repeat */
for (i=0 ; i<3 ; i++)
{
/* NB: add tolerance, since precision can be a problem */
ia = (gint) (FRACTION_TOLERANCE + fabs(tmat[i]));
if (ia > ma)
ma = ia;
}
/* b direction repeat */
for (i=3 ; i<6 ; i++)
{
/* NB: add tolerance, since precision can be a problem */
ib = (gint) (FRACTION_TOLERANCE + fabs(tmat[i]));
if (ib > mb)
mb = ib;
}
/* c direction repeat */
for (i=6 ; i<9 ; i++)
{
/* NB: add tolerance, since precision can be a problem */
ic = (gint) (FRACTION_TOLERANCE + fabs(tmat[i]));
if (ic > mc)
mc = ic;
}
/* TODO - check if these values are very large - warn user */
/* TDOO - implement a correct y/n/continue? popup */
#if DEBUG_NEW_LATTICE
printf("ma=%d, mb=%d, mc=%d\n", ma, mb, mc);
#endif
/* modify bulk energy */
dest->gulp.sbulkenergy *= ma*mb*mc;
/* full loop required to cover one cell in the transformed coordinates */
for (ic=0 ; ic<mc ; ic++)
{
for (ib=0 ; ib<mb ; ib++)
{
for (ia=0 ; ia<ma ; ia++)
{
/* current source cell translation */
VEC3SET(vec, ia, ib, ic);
/* create CARTESIAN core/shell images */
for (list = model->cores ; list ; list=g_slist_next(list))
{
core = dup_core(list->data);
core->primary = TRUE;
core->primary_core = NULL;
core->orig = TRUE;
ARR3ADD(core->x, vec);
vecmat(model->latmat, core->x);
dest->cores = g_slist_prepend(dest->cores, core);
if (core->shell)
{
shel = core->shell;
shel->primary = TRUE;
shel->primary_shell = NULL;
shel->orig = TRUE;
ARR3ADD(shel->x, vec);
vecmat(model->latmat, shel->x);
dest->shels = g_slist_prepend(dest->shels, shel);
}
}
}
}
}
/* init new model */
dest->fractional = FALSE;
dest->construct_pbc = TRUE;
model_prep(dest);
tree_model_add(dest);
redraw_canvas(SINGLE);
}
/************************/
/* distinguish identity */
/************************/
gint matrix_is_identity(gdouble *mat)
{
if (*(mat+0) != 1.0)
return(FALSE);
if (*(mat+4) != 1.0)
return(FALSE);
if (*(mat+8) != 1.0)
return(FALSE);
if (*(mat+1) != 0.0)
return(FALSE);
if (*(mat+2) != 0.0)
return(FALSE);
if (*(mat+3) != 0.0)
return(FALSE);
if (*(mat+5) != 0.0)
return(FALSE);
if (*(mat+6) != 0.0)
return(FALSE);
if (*(mat+7) != 0.0)
return(FALSE);
return(TRUE);
}
/*************************/
/* distinguish inversion */
/*************************/
gint matrix_is_inversion(gdouble *mat)
{
if (*(mat+0) != -1.0)
return(FALSE);
if (*(mat+4) != -1.0)
return(FALSE);
if (*(mat+8) != -1.0)
return(FALSE);
if (*(mat+1) != 0.0)
return(FALSE);
if (*(mat+2) != 0.0)
return(FALSE);
if (*(mat+3) != 0.0)
return(FALSE);
if (*(mat+5) != 0.0)
return(FALSE);
if (*(mat+6) != 0.0)
return(FALSE);
if (*(mat+7) != 0.0)
return(FALSE);
return(TRUE);
}
/**************************************/
/* get the order of a symmetry matrix */
/**************************************/
gint matrix_order(gdouble *mat)
{
gint i;
gdouble m1[9], m2[9];
ARR3SET(&m1[0], (mat+0));
ARR3SET(&m1[3], (mat+3));
ARR3SET(&m1[6], (mat+6));
ARR3SET(&m2[0], (mat+0));
ARR3SET(&m2[3], (mat+3));
ARR3SET(&m2[6], (mat+6));
/* keep applying until get the identity */
i=1;
while(i<17)
{
if (matrix_is_identity(m2))
return(i);
matmat(m1, m2);
i++;
}
/* not a symmetry operator */
return(0);
}
/************************************************/
/* return the order if a matrix is a z rotation */
/************************************************/
/* FIXME - only does 2 fold (for defect setup) */
gint matrix_is_z_rotation(gdouble *mat)
{
if (*(mat+0) != -1.0)
return(0);
if (*(mat+1) != 0.0)
return(0);
if (*(mat+2) != 0.0)
return(0);
if (*(mat+3) != 0.0)
return(0);
if (*(mat+4) != -1.0)
return(0);
if (*(mat+5) != 0.0)
return(0);
if (*(mat+6) != 0.0)
return(0);
if (*(mat+7) != 0.0)
return(0);
if (*(mat+8) != 1.0)
return(0);
return(2);
}
|