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"; |
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); |
62 |
|
{ |
63 |
|
if (!info || !*info) |
64 |
|
return(0); |
65 |
< |
if (!rm->info) |
65 |
> |
if (!rm->info) { |
66 |
|
rm->info = (char *)malloc(strlen(info)+1); |
67 |
< |
else |
67 |
> |
if (rm->info) rm->info[0] = '\0'; |
68 |
> |
} else |
69 |
|
rm->info = (char *)realloc(rm->info, |
70 |
|
strlen(rm->info)+strlen(info)+1); |
71 |
|
if (!rm->info) |
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); |
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 |
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)) |
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 |
|
} |
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; |
392 |
|
dtype = rm->dtype; |
393 |
|
else if ((dtype == DTrgbe) & (rm->dtype == DTxyze)) |
394 |
|
dtype = DTxyze; |
395 |
< |
else if ((dtype = DTxyze) & (rm->dtype == DTrgbe)) |
395 |
> |
else if ((dtype == DTxyze) & (rm->dtype == DTrgbe)) |
396 |
|
dtype = DTrgbe; |
397 |
|
if ((dtype != DTrgbe) & (dtype != DTxyze)) { |
398 |
|
fprintf(fp, "NROWS=%d\n", rm->nrows); |
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 */ |
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; ) |
424 |
< |
memcpy(rid->mtx+i*(dim*dim), rid->mtx, |
425 |
< |
sizeof(rid->mtx[0])*dim*dim); |
447 |
> |
for (k = n; k--; ) |
448 |
> |
rmx_lval(rid,i,i,k) = 1; |
449 |
|
return(rid); |
450 |
|
} |
451 |
|
|
511 |
|
rmx_addinfo(mres, rmx_mismatch_warn); |
512 |
|
for (i = mres->nrows; i--; ) |
513 |
|
for (j = mres->ncols; j--; ) |
514 |
< |
for (h = m1->ncols; h--; ) { |
514 |
> |
for (k = mres->ncomp; k--; ) { |
515 |
|
long double d = 0; |
516 |
< |
for (k = mres->ncomp; k--; ) |
516 |
> |
for (h = m1->ncols; h--; ) |
517 |
|
d += (long double)rmx_lval(m1,i,h,k) * |
518 |
|
(long double)rmx_lval(m2,h,j,k); |
519 |
|
rmx_lval(mres,i,j,k) = (double)d; |