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.8 by greg, Wed Aug 27 13:33:47 2014 UTC vs.
Revision 2.17 by greg, Wed Jul 22 04:47:51 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 108 | Line 111 | static int
111   rmx_load_ascii(RMATRIX *rm, FILE *fp)
112   {
113          int     i, j, k;
114 < #ifdef _WIN32
112 <        _setmode(fileno(fp), _O_TEXT);
113 < #endif
114 >
115          for (i = 0; i < rm->nrows; i++)
116              for (j = 0; j < rm->ncols; j++)
117                  for (k = 0; k < rm->ncomp; k++)
# Line 184 | Line 185 | rmx_load_rgbe(RMATRIX *rm, FILE *fp)
185  
186   /* Load matrix from supported file type */
187   RMATRIX *
188 < rmx_load(const char *fname)
188 > rmx_load(const char *inspec)
189   {
190          FILE            *fp = stdin;
191          RMATRIX         dinfo;
192          RMATRIX         *dnew;
193  
194 <        if (fname == NULL) {                    /* reading from stdin? */
195 <                fname = "<stdin>";
194 >        if (inspec == NULL) {                   /* reading from stdin? */
195 >                inspec = "<stdin>";
196   #ifdef _WIN32
197                  _setmode(fileno(stdin), _O_BINARY);
198   #endif
199 +        } else if (inspec[0] == '!') {
200 +                if ((fp = popen(inspec+1, "r")) == NULL)
201 +                        return(NULL);
202 + #ifdef _WIN32
203 +                _setmode(fileno(fp), _O_BINARY);
204 + #endif
205          } else {
206 <                const char      *sp = fname;    /* check suffix */
206 >                const char      *sp = inspec;   /* check suffix */
207                  while (*sp)
208                          ++sp;
209 <                while (sp > fname && sp[-1] != '.')
209 >                while (sp > inspec && sp[-1] != '.')
210                          --sp;
211                  if (!strcasecmp(sp, "XML")) {   /* assume it's a BSDF */
212 <                        CMATRIX *cm = cm_loadBTDF((char *)fname);
212 >                        CMATRIX *cm = cm_loadBTDF((char *)inspec);
213                          if (cm == NULL)
214                                  return(NULL);
215                          dnew = rmx_from_cmatrix(cm);
# Line 210 | Line 217 | rmx_load(const char *fname)
217                          return(dnew);
218                  }
219                                                  /* else open it ourselves */
220 <                if ((fp = fopen(fname, "rb")) == NULL)
220 >                if ((fp = fopen(inspec, "rb")) == NULL)
221                          return(NULL);
222          }
223   #ifdef getc_unlocked
# Line 244 | Line 251 | rmx_load(const char *fname)
251          dnew->info = dinfo.info;
252          switch (dinfo.dtype) {
253          case DTascii:
254 + #ifdef _WIN32
255 +                _setmode(fileno(fp), _O_TEXT);
256 + #endif
257                  if (!rmx_load_ascii(dnew, fp))
258                          goto loaderr;
259 +                dnew->dtype = DTascii;          /* should leave double? */
260                  break;
261          case DTfloat:
262                  if (!rmx_load_float(dnew, fp))
# Line 266 | Line 277 | rmx_load(const char *fname)
277          default:
278                  goto loaderr;
279          }
280 <        if (fp != stdin)
281 <                fclose(fp);
280 >        if (fp != stdin) {
281 >                if (inspec[0] == '!')
282 >                        pclose(fp);
283 >                else
284 >                        fclose(fp);
285 >        }
286 > #ifdef getc_unlocked
287 >        else
288 >                funlockfile(fp);
289 > #endif
290          return(dnew);
291   loaderr:                                        /* should report error? */
292 <        fclose(fp);
292 >        if (inspec[0] == '!')
293 >                pclose(fp);
294 >        else
295 >                fclose(fp);
296          rmx_free(dnew);
297          return(NULL);
298   }
# Line 279 | Line 301 | static int
301   rmx_write_ascii(const RMATRIX *rm, FILE *fp)
302   {
303          int     i, j, k;
304 < #ifdef _WIN32
283 <        _setmode(fileno(fp), _O_TEXT);
284 < #endif
304 >
305          for (i = 0; i < rm->nrows; i++) {
306              for (j = 0; j < rm->ncols; j++) {
307                  for (k = 0; k < rm->ncomp; k++)
# Line 356 | Line 376 | rmx_write_rgbe(const RMATRIX *rm, FILE *fp)
376   }
377  
378   /* Write matrix to file type indicated by dtype */
379 < long
379 > int
380   rmx_write(const RMATRIX *rm, int dtype, FILE *fp)
381   {
382          RMATRIX *mydm = NULL;
# Line 364 | Line 384 | rmx_write(const RMATRIX *rm, int dtype, FILE *fp)
384  
385          if ((rm == NULL) | (fp == NULL))
386                  return(0);
387 + #ifdef getc_unlocked
388 +        flockfile(fp);
389 + #endif
390                                                  /* complete header */
391          if (rm->info)
392                  fputs(rm->info, fp);
# Line 371 | Line 394 | rmx_write(const RMATRIX *rm, int dtype, FILE *fp)
394                  dtype = rm->dtype;
395          else if ((dtype == DTrgbe) & (rm->dtype == DTxyze))
396                  dtype = DTxyze;
397 <        else if ((dtype = DTxyze) & (rm->dtype == DTrgbe))
397 >        else if ((dtype == DTxyze) & (rm->dtype == DTrgbe))
398                  dtype = DTrgbe;
399          if ((dtype != DTrgbe) & (dtype != DTxyze)) {
400                  fprintf(fp, "NROWS=%d\n", rm->nrows);
# Line 408 | Line 431 | rmx_write(const RMATRIX *rm, int dtype, FILE *fp)
431                  return(0);
432          }
433          ok &= (fflush(fp) == 0);
434 + #ifdef getc_unlocked
435 +        funlockfile(fp);
436 + #endif
437          rmx_free(mydm);
438 <        return(ftell(fp) * ok);         /* return # bytes written */
438 >        return(ok);
439   }
440  
441   /* Allocate and assign square identity matrix with n components */
# Line 417 | Line 443 | RMATRIX *
443   rmx_identity(const int dim, const int n)
444   {
445          RMATRIX *rid = rmx_alloc(dim, dim, n);
446 <        int     i;
446 >        int     i, k;
447  
448          if (rid == NULL)
449                  return(NULL);
450 <        memset(rid->mtx, 0, sizeof(rid->mtx[0])*dim*dim);
450 >        memset(rid->mtx, 0, sizeof(rid->mtx[0])*n*dim*dim);
451          for (i = dim; i--; )
452 <                rmx_lval(rid,i,i,0) = 1;
453 <        for (i = n; --i; )
428 <                memcpy(rid->mtx+i*(dim*dim), rid->mtx,
429 <                                sizeof(rid->mtx[0])*dim*dim);
452 >            for (k = n; k--; )
453 >                rmx_lval(rid,i,i,k) = 1;
454          return(rid);
455   }
456  
# Line 495 | Line 519 | rmx_multiply(const RMATRIX *m1, const RMATRIX *m2)
519                  for (k = mres->ncomp; k--; ) {
520                      long double d = 0;
521                      for (h = m1->ncols; h--; )
522 <                        d += (long double)rmx_lval(m1,i,h,k) *
499 <                                (long double)rmx_lval(m2,h,j,k);
522 >                        d += rmx_lval(m1,i,h,k) * rmx_lval(m2,h,j,k);
523                      rmx_lval(mres,i,j,k) = (double)d;
524                  }
525          return(mres);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines