ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/mat4.c
(Generate patch)

Comparing ray/src/common/mat4.c (file contents):
Revision 1.1 by greg, Thu Feb 2 10:34:35 1989 UTC vs.
Revision 1.2 by greg, Tue Oct 24 14:01:17 1989 UTC

# Line 79 | Line 79 | register double  m4[4][4];
79   #ifdef  INVMAT
80   /*
81   * invmat - computes the inverse of mat into inverse.  Returns 1
82 < * if there exists an inverse, 0 otherwise.  It uses Gause Elimination
83 < * method.
82 > * if there exists an inverse, 0 otherwise.  It uses Gaussian Elimination
83 > * method with partial pivoting.
84   */
85  
86   invmat(inverse,mat)
87   double mat[4][4],inverse[4][4];
88   {
89   #define SWAP(a,b,t) (t=a,a=b,b=t)
90 + #define ABS(x) (x>=0?x:-(x))
91  
92          register int i,j,k;
93          register double temp;
94  
94        setident4(inverse);
95          copymat4(m4tmp, mat);
96 +        setident(inverse);
97  
98          for(i = 0; i < 4; i++) {
99 <                if(m4tmp[i][i] == 0) {    /* Pivot is zero */
100 <                        /* Look for a raw with pivot != 0 and swap raws */
101 <                        for(j = i + 1; j < 4; j++)
102 <                                if(m4tmp[j][i] != 0) {
103 <                                        for( k = 0; k < 4; k++) {
104 <                                                SWAP(m4tmp[i][k],m4tmp[j][k],temp);
105 <                                                SWAP(inverse[i][k],inverse[j][k],temp);
106 <                                                }
107 <                                        break;
108 <                                        }
109 <                        if(j == 4)      /* No replacing raw -> no inverse */
110 <                                return(0);
111 <                        }
99 >                /* Look for row with largest pivot and swap rows */
100 >                temp = 0; j = -1;
101 >                for(k = i; k < 4; k++)
102 >                        if(ABS(m4tmp[k][i]) > temp) {
103 >                                temp = ABS(m4tmp[k][i]);
104 >                                j = k;
105 >                                }
106 >                if(j == -1)     /* No replacing row -> no inverse */
107 >                        return(0);
108 >                if (j != i)
109 >                        for(k = 0; k < 4; k++) {
110 >                                SWAP(m4tmp[i][k],m4tmp[j][k],temp);
111 >                                SWAP(inverse[i][k],inverse[j][k],temp);
112 >                                }
113  
114                  temp = m4tmp[i][i];
115                  for(k = 0; k < 4; k++) {
# Line 125 | Line 127 | double mat[4][4],inverse[4][4];
127                          }
128                  }
129          return(1);
130 +
131 + #undef ABS
132 + #undef SWAP
133   }
134   #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines