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

Comparing ray/src/util/rmatrix.c (file contents):
Revision 2.9 by greg, Thu Aug 28 05:59:42 2014 UTC vs.
Revision 2.16 by greg, Wed Jul 22 04:29:56 2015 UTC

# Line 10 | Line 10 | static const char RCSid[] = "$Id$";
10   #include <string.h>
11   #include <fcntl.h>
12   #include "resolu.h"
13 + #include "rtprocess.h"
14   #include "rmatrix.h"
15  
16   static char     rmx_mismatch_warn[] = "WARNING: data type mismatch\n";
# Line 46 | Line 47 | rmx_free(RMATRIX *rm)
47   int
48   rmx_newtype(int dtyp1, int dtyp2)
49   {
50 <        if ((dtyp1==DTxyze) | (dtyp1==DTrgbe) && dtyp1 != dtyp2)
50 >        if ((dtyp1==DTxyze) | (dtyp1==DTrgbe) |
51 >                        (dtyp2==DTxyze) | (dtyp2==DTrgbe)
52 >                        && dtyp1 != dtyp2)
53                  return(0);
54          if (dtyp1 < dtyp2)
55                  return(dtyp1);
# Line 184 | Line 187 | rmx_load_rgbe(RMATRIX *rm, FILE *fp)
187  
188   /* Load matrix from supported file type */
189   RMATRIX *
190 < rmx_load(const char *fname)
190 > rmx_load(const char *inspec)
191   {
192          FILE            *fp = stdin;
193          RMATRIX         dinfo;
194          RMATRIX         *dnew;
195  
196 <        if (fname == NULL) {                    /* reading from stdin? */
197 <                fname = "<stdin>";
196 >        if (inspec == NULL) {                   /* reading from stdin? */
197 >                inspec = "<stdin>";
198   #ifdef _WIN32
199                  _setmode(fileno(stdin), _O_BINARY);
200   #endif
201 +        } else if (inspec[0] == '!') {
202 +                if ((fp = popen(inspec+1, "r")) == NULL)
203 +                        return(NULL);
204 + #ifdef _WIN32
205 +                _setmode(fileno(fp), _O_BINARY);
206 + #endif
207          } else {
208 <                const char      *sp = fname;    /* check suffix */
208 >                const char      *sp = inspec;   /* check suffix */
209                  while (*sp)
210                          ++sp;
211 <                while (sp > fname && sp[-1] != '.')
211 >                while (sp > inspec && sp[-1] != '.')
212                          --sp;
213                  if (!strcasecmp(sp, "XML")) {   /* assume it's a BSDF */
214 <                        CMATRIX *cm = cm_loadBTDF((char *)fname);
214 >                        CMATRIX *cm = cm_loadBTDF((char *)inspec);
215                          if (cm == NULL)
216                                  return(NULL);
217                          dnew = rmx_from_cmatrix(cm);
# Line 210 | Line 219 | rmx_load(const char *fname)
219                          return(dnew);
220                  }
221                                                  /* else open it ourselves */
222 <                if ((fp = fopen(fname, "rb")) == NULL)
222 >                if ((fp = fopen(inspec, "rb")) == NULL)
223                          return(NULL);
224          }
225   #ifdef getc_unlocked
# Line 246 | Line 255 | rmx_load(const char *fname)
255          case DTascii:
256                  if (!rmx_load_ascii(dnew, fp))
257                          goto loaderr;
258 +                dnew->dtype = DTascii;          /* should leave double? */
259                  break;
260          case DTfloat:
261                  if (!rmx_load_float(dnew, fp))
# Line 266 | Line 276 | rmx_load(const char *fname)
276          default:
277                  goto loaderr;
278          }
279 <        if (fp != stdin)
280 <                fclose(fp);
279 >        if (fp != stdin) {
280 >                if (inspec[0] == '!')
281 >                        pclose(fp);
282 >                else
283 >                        fclose(fp);
284 >        }
285 > #ifdef getc_unlocked
286 >        else
287 >                funlockfile(fp);
288 > #endif
289          return(dnew);
290   loaderr:                                        /* should report error? */
291 <        fclose(fp);
291 >        if (inspec[0] == '!')
292 >                pclose(fp);
293 >        else
294 >                fclose(fp);
295          rmx_free(dnew);
296          return(NULL);
297   }
# Line 356 | Line 377 | rmx_write_rgbe(const RMATRIX *rm, FILE *fp)
377   }
378  
379   /* Write matrix to file type indicated by dtype */
380 < long
380 > int
381   rmx_write(const RMATRIX *rm, int dtype, FILE *fp)
382   {
383          RMATRIX *mydm = NULL;
# Line 409 | Line 430 | rmx_write(const RMATRIX *rm, int dtype, FILE *fp)
430          }
431          ok &= (fflush(fp) == 0);
432          rmx_free(mydm);
433 <        return(ftell(fp) * ok);         /* return # bytes written */
433 >        return(ok);
434   }
435  
436   /* Allocate and assign square identity matrix with n components */
# Line 417 | Line 438 | RMATRIX *
438   rmx_identity(const int dim, const int n)
439   {
440          RMATRIX *rid = rmx_alloc(dim, dim, n);
441 <        int     i;
441 >        int     i, k;
442  
443          if (rid == NULL)
444                  return(NULL);
445 <        memset(rid->mtx, 0, sizeof(rid->mtx[0])*dim*dim);
445 >        memset(rid->mtx, 0, sizeof(rid->mtx[0])*n*dim*dim);
446          for (i = dim; i--; )
447 <                rmx_lval(rid,i,i,0) = 1;
448 <        for (i = n; --i; )
428 <                memcpy(rid->mtx+i*(dim*dim), rid->mtx,
429 <                                sizeof(rid->mtx[0])*dim*dim);
447 >            for (k = n; k--; )
448 >                rmx_lval(rid,i,i,k) = 1;
449          return(rid);
450   }
451  
# Line 495 | Line 514 | rmx_multiply(const RMATRIX *m1, const RMATRIX *m2)
514                  for (k = mres->ncomp; k--; ) {
515                      long double d = 0;
516                      for (h = m1->ncols; h--; )
517 <                        d += (long double)rmx_lval(m1,i,h,k) *
499 <                                (long double)rmx_lval(m2,h,j,k);
517 >                        d += rmx_lval(m1,i,h,k) * rmx_lval(m2,h,j,k);
518                      rmx_lval(mres,i,j,k) = (double)d;
519                  }
520          return(mres);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines