2 |
|
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
4 |
|
/* |
5 |
< |
* Routines for handling BSDF data |
5 |
> |
* bsdf.c |
6 |
> |
* |
7 |
> |
* Definitions for bidirectional scattering distribution functions. |
8 |
> |
* |
9 |
> |
* Created by Greg Ward on 1/10/11. |
10 |
> |
* |
11 |
|
*/ |
12 |
|
|
13 |
< |
#include "standard.h" |
14 |
< |
#include "bsdf.h" |
15 |
< |
#include "paths.h" |
16 |
< |
#include "ezxml.h" |
13 |
> |
#define _USE_MATH_DEFINES |
14 |
> |
#include <stdio.h> |
15 |
> |
#include <stdlib.h> |
16 |
> |
#include <string.h> |
17 |
> |
#include <math.h> |
18 |
|
#include <ctype.h> |
19 |
+ |
#include "ezxml.h" |
20 |
+ |
#include "hilbert.h" |
21 |
+ |
#include "bsdf.h" |
22 |
+ |
#include "bsdf_m.h" |
23 |
+ |
#include "bsdf_t.h" |
24 |
|
|
25 |
< |
#define MAXLATS 46 /* maximum number of latitudes */ |
25 |
> |
/* English ASCII strings corresponding to ennumerated errors */ |
26 |
> |
const char *SDerrorEnglish[] = { |
27 |
> |
"No error", |
28 |
> |
"Memory error", |
29 |
> |
"File input/output error", |
30 |
> |
"File format error", |
31 |
> |
"Illegal argument", |
32 |
> |
"Invalid data", |
33 |
> |
"Unsupported feature", |
34 |
> |
"Internal program error", |
35 |
> |
"Unknown error" |
36 |
> |
}; |
37 |
|
|
38 |
< |
/* BSDF angle specification */ |
39 |
< |
typedef struct { |
18 |
< |
char name[64]; /* basis name */ |
19 |
< |
int nangles; /* total number of directions */ |
20 |
< |
struct { |
21 |
< |
float tmin; /* starting theta */ |
22 |
< |
short nphis; /* number of phis (0 term) */ |
23 |
< |
} lat[MAXLATS+1]; /* latitudes */ |
24 |
< |
} ANGLE_BASIS; |
38 |
> |
/* Pointer to error list in preferred language */ |
39 |
> |
const char **SDerrorList = SDerrorEnglish; |
40 |
|
|
41 |
< |
#define MAXABASES 7 /* limit on defined bases */ |
41 |
> |
/* Additional information on last error (ASCII English) */ |
42 |
> |
char SDerrorDetail[256]; |
43 |
|
|
44 |
< |
static ANGLE_BASIS abase_list[MAXABASES] = { |
45 |
< |
{ |
30 |
< |
"LBNL/Klems Full", 145, |
31 |
< |
{ {-5., 1}, |
32 |
< |
{5., 8}, |
33 |
< |
{15., 16}, |
34 |
< |
{25., 20}, |
35 |
< |
{35., 24}, |
36 |
< |
{45., 24}, |
37 |
< |
{55., 24}, |
38 |
< |
{65., 16}, |
39 |
< |
{75., 12}, |
40 |
< |
{90., 0} } |
41 |
< |
}, { |
42 |
< |
"LBNL/Klems Half", 73, |
43 |
< |
{ {-6.5, 1}, |
44 |
< |
{6.5, 8}, |
45 |
< |
{19.5, 12}, |
46 |
< |
{32.5, 16}, |
47 |
< |
{46.5, 20}, |
48 |
< |
{61.5, 12}, |
49 |
< |
{76.5, 4}, |
50 |
< |
{90., 0} } |
51 |
< |
}, { |
52 |
< |
"LBNL/Klems Quarter", 41, |
53 |
< |
{ {-9., 1}, |
54 |
< |
{9., 8}, |
55 |
< |
{27., 12}, |
56 |
< |
{46., 12}, |
57 |
< |
{66., 8}, |
58 |
< |
{90., 0} } |
59 |
< |
} |
60 |
< |
}; |
44 |
> |
/* Empty distribution for getCDist() calls that fail for some reason */ |
45 |
> |
const SDCDst SDemptyCD; |
46 |
|
|
47 |
< |
static int nabases = 3; /* current number of defined bases */ |
47 |
> |
/* Cache of loaded BSDFs */ |
48 |
> |
struct SDCache_s *SDcacheList = NULL; |
49 |
|
|
50 |
< |
#define FEQ(a,b) ((a)-(b) <= 1e-7 && (b)-(a) <= 1e-7) |
50 |
> |
/* Retain BSDFs in cache list */ |
51 |
> |
int SDretainSet = SDretainNone; |
52 |
|
|
53 |
< |
// returns the name of the given tag |
54 |
< |
#ifdef ezxml_name |
55 |
< |
#undef ezxml_name |
69 |
< |
static char * |
70 |
< |
ezxml_name(ezxml_t xml) |
53 |
> |
/* Report any error to the indicated stream */ |
54 |
> |
SDError |
55 |
> |
SDreportError(SDError ec, FILE *fp) |
56 |
|
{ |
57 |
< |
if (xml == NULL) |
58 |
< |
return(NULL); |
59 |
< |
return(xml->name); |
57 |
> |
if (!ec) |
58 |
> |
return SDEnone; |
59 |
> |
if ((ec < SDEnone) | (ec > SDEunknown)) { |
60 |
> |
SDerrorDetail[0] = '\0'; |
61 |
> |
ec = SDEunknown; |
62 |
> |
} |
63 |
> |
if (fp == NULL) |
64 |
> |
return ec; |
65 |
> |
fputs(SDerrorList[ec], fp); |
66 |
> |
if (SDerrorDetail[0]) { |
67 |
> |
fputs(": ", fp); |
68 |
> |
fputs(SDerrorDetail, fp); |
69 |
> |
} |
70 |
> |
fputc('\n', fp); |
71 |
> |
if (fp != stderr) |
72 |
> |
fflush(fp); |
73 |
> |
return ec; |
74 |
|
} |
76 |
– |
#endif |
75 |
|
|
76 |
< |
// returns the given tag's character content or empty string if none |
77 |
< |
#ifdef ezxml_txt |
78 |
< |
#undef ezxml_txt |
79 |
< |
static char * |
82 |
< |
ezxml_txt(ezxml_t xml) |
76 |
> |
static double |
77 |
> |
to_meters( /* return factor to convert given unit to meters */ |
78 |
> |
const char *unit |
79 |
> |
) |
80 |
|
{ |
81 |
< |
if (xml == NULL) |
82 |
< |
return(""); |
83 |
< |
return(xml->txt); |
81 |
> |
if (unit == NULL) return(1.); /* safe assumption? */ |
82 |
> |
if (!strcasecmp(unit, "Meter")) return(1.); |
83 |
> |
if (!strcasecmp(unit, "Foot")) return(.3048); |
84 |
> |
if (!strcasecmp(unit, "Inch")) return(.0254); |
85 |
> |
if (!strcasecmp(unit, "Centimeter")) return(.01); |
86 |
> |
if (!strcasecmp(unit, "Millimeter")) return(.001); |
87 |
> |
sprintf(SDerrorDetail, "Unknown dimensional unit '%s'", unit); |
88 |
> |
return(-1.); |
89 |
|
} |
88 |
– |
#endif |
90 |
|
|
91 |
< |
|
92 |
< |
static int |
93 |
< |
ab_getvec( /* get vector for this angle basis index */ |
93 |
< |
FVECT v, |
94 |
< |
int ndx, |
95 |
< |
void *p |
96 |
< |
) |
91 |
> |
/* Load geometric dimensions and description (if any) */ |
92 |
> |
static SDError |
93 |
> |
SDloadGeometry(SDData *sd, ezxml_t wtl) |
94 |
|
{ |
95 |
< |
ANGLE_BASIS *ab = (ANGLE_BASIS *)p; |
96 |
< |
int li; |
97 |
< |
double pol, azi, d; |
98 |
< |
|
99 |
< |
if ((ndx < 0) | (ndx >= ab->nangles)) |
100 |
< |
return(0); |
101 |
< |
for (li = 0; ndx >= ab->lat[li].nphis; li++) |
102 |
< |
ndx -= ab->lat[li].nphis; |
103 |
< |
pol = PI/180.*0.5*(ab->lat[li].tmin + ab->lat[li+1].tmin); |
104 |
< |
azi = 2.*PI*ndx/ab->lat[li].nphis; |
105 |
< |
v[2] = d = cos(pol); |
106 |
< |
d = sqrt(1. - d*d); /* sin(pol) */ |
107 |
< |
v[0] = cos(azi)*d; |
108 |
< |
v[1] = sin(azi)*d; |
109 |
< |
return(1); |
95 |
> |
ezxml_t node, matl, geom; |
96 |
> |
double cfact; |
97 |
> |
const char *fmt = NULL, *mgfstr; |
98 |
> |
|
99 |
> |
SDerrorDetail[0] = '\0'; |
100 |
> |
sd->matn[0] = '\0'; sd->makr[0] = '\0'; |
101 |
> |
sd->dim[0] = sd->dim[1] = sd->dim[2] = 0; |
102 |
> |
matl = ezxml_child(wtl, "Material"); |
103 |
> |
if (matl != NULL) { /* get material info. */ |
104 |
> |
if ((node = ezxml_child(matl, "Name")) != NULL) { |
105 |
> |
strncpy(sd->matn, ezxml_txt(node), SDnameLn); |
106 |
> |
if (sd->matn[SDnameLn-1]) |
107 |
> |
strcpy(sd->matn+(SDnameLn-4), "..."); |
108 |
> |
} |
109 |
> |
if ((node = ezxml_child(matl, "Manufacturer")) != NULL) { |
110 |
> |
strncpy(sd->makr, ezxml_txt(node), SDnameLn); |
111 |
> |
if (sd->makr[SDnameLn-1]) |
112 |
> |
strcpy(sd->makr+(SDnameLn-4), "..."); |
113 |
> |
} |
114 |
> |
if ((node = ezxml_child(matl, "Width")) != NULL) |
115 |
> |
sd->dim[0] = atof(ezxml_txt(node)) * |
116 |
> |
to_meters(ezxml_attr(node, "unit")); |
117 |
> |
if ((node = ezxml_child(matl, "Height")) != NULL) |
118 |
> |
sd->dim[1] = atof(ezxml_txt(node)) * |
119 |
> |
to_meters(ezxml_attr(node, "unit")); |
120 |
> |
if ((node = ezxml_child(matl, "Thickness")) != NULL) |
121 |
> |
sd->dim[2] = atof(ezxml_txt(node)) * |
122 |
> |
to_meters(ezxml_attr(node, "unit")); |
123 |
> |
if ((sd->dim[0] < 0) | (sd->dim[1] < 0) | (sd->dim[2] < 0)) { |
124 |
> |
if (!SDerrorDetail[0]) |
125 |
> |
sprintf(SDerrorDetail, "Negative dimension in \"%s\"", |
126 |
> |
sd->name); |
127 |
> |
return SDEdata; |
128 |
> |
} |
129 |
> |
} |
130 |
> |
sd->mgf = NULL; |
131 |
> |
geom = ezxml_child(wtl, "Geometry"); |
132 |
> |
if (geom == NULL) /* no actual geometry? */ |
133 |
> |
return SDEnone; |
134 |
> |
fmt = ezxml_attr(geom, "format"); |
135 |
> |
if (fmt != NULL && strcasecmp(fmt, "MGF")) { |
136 |
> |
sprintf(SDerrorDetail, |
137 |
> |
"Unrecognized geometry format '%s' in \"%s\"", |
138 |
> |
fmt, sd->name); |
139 |
> |
return SDEsupport; |
140 |
> |
} |
141 |
> |
if ((node = ezxml_child(geom, "MGFblock")) == NULL || |
142 |
> |
(mgfstr = ezxml_txt(node)) == NULL) |
143 |
> |
return SDEnone; |
144 |
> |
while (isspace(*mgfstr)) |
145 |
> |
++mgfstr; |
146 |
> |
if (!*mgfstr) |
147 |
> |
return SDEnone; |
148 |
> |
cfact = to_meters(ezxml_attr(node, "unit")); |
149 |
> |
if (cfact <= 0) |
150 |
> |
return SDEformat; |
151 |
> |
sd->mgf = (char *)malloc(strlen(mgfstr)+32); |
152 |
> |
if (sd->mgf == NULL) { |
153 |
> |
strcpy(SDerrorDetail, "Out of memory in SDloadGeometry"); |
154 |
> |
return SDEmemory; |
155 |
> |
} |
156 |
> |
if (cfact < 0.99 || cfact > 1.01) |
157 |
> |
sprintf(sd->mgf, "xf -s %.5f\n%s\nxf\n", cfact, mgfstr); |
158 |
> |
else |
159 |
> |
strcpy(sd->mgf, mgfstr); |
160 |
> |
return SDEnone; |
161 |
|
} |
162 |
|
|
163 |
< |
|
164 |
< |
static int |
165 |
< |
ab_getndx( /* get index corresponding to the given vector */ |
118 |
< |
FVECT v, |
119 |
< |
void *p |
120 |
< |
) |
163 |
> |
/* Load a BSDF struct from the given file (free first and keep name) */ |
164 |
> |
SDError |
165 |
> |
SDloadFile(SDData *sd, const char *fname) |
166 |
|
{ |
167 |
< |
ANGLE_BASIS *ab = (ANGLE_BASIS *)p; |
168 |
< |
int li, ndx; |
124 |
< |
double pol, azi, d; |
167 |
> |
SDError lastErr; |
168 |
> |
ezxml_t fl, wtl; |
169 |
|
|
170 |
< |
if ((v[2] < -1.0) | (v[2] > 1.0)) |
171 |
< |
return(-1); |
172 |
< |
pol = 180.0/PI*acos(v[2]); |
173 |
< |
azi = 180.0/PI*atan2(v[1], v[0]); |
174 |
< |
if (azi < 0.0) azi += 360.0; |
175 |
< |
for (li = 1; ab->lat[li].tmin <= pol; li++) |
176 |
< |
if (!ab->lat[li].nphis) |
177 |
< |
return(-1); |
178 |
< |
--li; |
179 |
< |
ndx = (int)((1./360.)*azi*ab->lat[li].nphis + 0.5); |
180 |
< |
if (ndx >= ab->lat[li].nphis) ndx = 0; |
181 |
< |
while (li--) |
182 |
< |
ndx += ab->lat[li].nphis; |
183 |
< |
return(ndx); |
170 |
> |
if ((sd == NULL) | (fname == NULL || !*fname)) |
171 |
> |
return SDEargument; |
172 |
> |
/* free old data, keeping name */ |
173 |
> |
SDfreeBSDF(sd); |
174 |
> |
/* parse XML file */ |
175 |
> |
fl = ezxml_parse_file(fname); |
176 |
> |
if (fl == NULL) { |
177 |
> |
sprintf(SDerrorDetail, "Cannot open BSDF \"%s\"", fname); |
178 |
> |
return SDEfile; |
179 |
> |
} |
180 |
> |
if (ezxml_error(fl)[0]) { |
181 |
> |
sprintf(SDerrorDetail, "BSDF \"%s\" %s", fname, ezxml_error(fl)); |
182 |
> |
ezxml_free(fl); |
183 |
> |
return SDEformat; |
184 |
> |
} |
185 |
> |
if (strcmp(ezxml_name(fl), "WindowElement")) { |
186 |
> |
sprintf(SDerrorDetail, |
187 |
> |
"BSDF \"%s\": top level node not 'WindowElement'", |
188 |
> |
sd->name); |
189 |
> |
ezxml_free(fl); |
190 |
> |
return SDEformat; |
191 |
> |
} |
192 |
> |
wtl = ezxml_child(fl, "FileType"); |
193 |
> |
if (wtl != NULL && strcmp(ezxml_txt(wtl), "BSDF")) { |
194 |
> |
sprintf(SDerrorDetail, |
195 |
> |
"XML \"%s\": wrong FileType (must be 'BSDF')", |
196 |
> |
sd->name); |
197 |
> |
ezxml_free(fl); |
198 |
> |
return SDEformat; |
199 |
> |
} |
200 |
> |
wtl = ezxml_child(ezxml_child(fl, "Optical"), "Layer"); |
201 |
> |
if (wtl == NULL) { |
202 |
> |
sprintf(SDerrorDetail, "BSDF \"%s\": no optical layers'", |
203 |
> |
sd->name); |
204 |
> |
ezxml_free(fl); |
205 |
> |
return SDEformat; |
206 |
> |
} |
207 |
> |
/* load geometry if present */ |
208 |
> |
lastErr = SDloadGeometry(sd, wtl); |
209 |
> |
if (lastErr) { |
210 |
> |
ezxml_free(fl); |
211 |
> |
return lastErr; |
212 |
> |
} |
213 |
> |
/* try loading variable resolution data */ |
214 |
> |
lastErr = SDloadTre(sd, wtl); |
215 |
> |
/* check our result */ |
216 |
> |
if (lastErr == SDEsupport) /* try matrix BSDF if not tree data */ |
217 |
> |
lastErr = SDloadMtx(sd, wtl); |
218 |
> |
|
219 |
> |
/* done with XML file */ |
220 |
> |
ezxml_free(fl); |
221 |
> |
|
222 |
> |
if (lastErr) { /* was there a load error? */ |
223 |
> |
SDfreeBSDF(sd); |
224 |
> |
return lastErr; |
225 |
> |
} |
226 |
> |
/* remove any insignificant components */ |
227 |
> |
if (sd->rf != NULL && sd->rf->maxHemi <= .001) { |
228 |
> |
SDfreeSpectralDF(sd->rf); sd->rf = NULL; |
229 |
> |
} |
230 |
> |
if (sd->rb != NULL && sd->rb->maxHemi <= .001) { |
231 |
> |
SDfreeSpectralDF(sd->rb); sd->rb = NULL; |
232 |
> |
} |
233 |
> |
if (sd->tf != NULL && sd->tf->maxHemi <= .001) { |
234 |
> |
SDfreeSpectralDF(sd->tf); sd->tf = NULL; |
235 |
> |
} |
236 |
> |
if (sd->tb != NULL && sd->tb->maxHemi <= .001) { |
237 |
> |
SDfreeSpectralDF(sd->tb); sd->tb = NULL; |
238 |
> |
} |
239 |
> |
/* return success */ |
240 |
> |
return SDEnone; |
241 |
|
} |
242 |
|
|
243 |
< |
|
244 |
< |
static double |
245 |
< |
ab_getohm( /* get solid angle for this angle basis index */ |
145 |
< |
int ndx, |
146 |
< |
void *p |
147 |
< |
) |
243 |
> |
/* Allocate new spectral distribution function */ |
244 |
> |
SDSpectralDF * |
245 |
> |
SDnewSpectralDF(int nc) |
246 |
|
{ |
247 |
< |
ANGLE_BASIS *ab = (ANGLE_BASIS *)p; |
150 |
< |
int li; |
151 |
< |
double theta, theta1; |
247 |
> |
SDSpectralDF *df; |
248 |
|
|
249 |
< |
if ((ndx < 0) | (ndx >= ab->nangles)) |
250 |
< |
return(0); |
251 |
< |
for (li = 0; ndx >= ab->lat[li].nphis; li++) |
156 |
< |
ndx -= ab->lat[li].nphis; |
157 |
< |
theta1 = PI/180. * ab->lat[li+1].tmin; |
158 |
< |
if (ab->lat[li].nphis == 1) { /* special case */ |
159 |
< |
if (ab->lat[li].tmin > FTINY) |
160 |
< |
error(USER, "unsupported BSDF coordinate system"); |
161 |
< |
return(2.*PI*(1. - cos(theta1))); |
249 |
> |
if (nc <= 0) { |
250 |
> |
strcpy(SDerrorDetail, "Zero component spectral DF request"); |
251 |
> |
return NULL; |
252 |
|
} |
253 |
< |
theta = PI/180. * ab->lat[li].tmin; |
254 |
< |
return(2.*PI*(cos(theta) - cos(theta1))/(double)ab->lat[li].nphis); |
253 |
> |
df = (SDSpectralDF *)malloc(sizeof(SDSpectralDF) + |
254 |
> |
(nc-1)*sizeof(SDComponent)); |
255 |
> |
if (df == NULL) { |
256 |
> |
sprintf(SDerrorDetail, |
257 |
> |
"Cannot allocate %d component spectral DF", nc); |
258 |
> |
return NULL; |
259 |
> |
} |
260 |
> |
df->minProjSA = .0; |
261 |
> |
df->maxHemi = .0; |
262 |
> |
df->ncomp = nc; |
263 |
> |
memset(df->comp, 0, nc*sizeof(SDComponent)); |
264 |
> |
return df; |
265 |
|
} |
266 |
|
|
267 |
< |
|
268 |
< |
static int |
269 |
< |
ab_getvecR( /* get reverse vector for this angle basis index */ |
170 |
< |
FVECT v, |
171 |
< |
int ndx, |
172 |
< |
void *p |
173 |
< |
) |
267 |
> |
/* Add component(s) to spectral distribution function */ |
268 |
> |
SDSpectralDF * |
269 |
> |
SDaddComponent(SDSpectralDF *odf, int nadd) |
270 |
|
{ |
271 |
< |
if (!ab_getvec(v, ndx, p)) |
176 |
< |
return(0); |
271 |
> |
SDSpectralDF *df; |
272 |
|
|
273 |
< |
v[0] = -v[0]; |
274 |
< |
v[1] = -v[1]; |
275 |
< |
v[2] = -v[2]; |
276 |
< |
|
277 |
< |
return(1); |
273 |
> |
if (odf == NULL) |
274 |
> |
return SDnewSpectralDF(nadd); |
275 |
> |
if (nadd <= 0) |
276 |
> |
return odf; |
277 |
> |
df = (SDSpectralDF *)realloc(odf, sizeof(SDSpectralDF) + |
278 |
> |
(odf->ncomp+nadd-1)*sizeof(SDComponent)); |
279 |
> |
if (df == NULL) { |
280 |
> |
sprintf(SDerrorDetail, |
281 |
> |
"Cannot add %d component(s) to spectral DF", nadd); |
282 |
> |
SDfreeSpectralDF(odf); |
283 |
> |
return NULL; |
284 |
> |
} |
285 |
> |
memset(df->comp+df->ncomp, 0, nadd*sizeof(SDComponent)); |
286 |
> |
df->ncomp += nadd; |
287 |
> |
return df; |
288 |
|
} |
289 |
|
|
290 |
< |
|
291 |
< |
static int |
292 |
< |
ab_getndxR( /* get index corresponding to the reverse vector */ |
188 |
< |
FVECT v, |
189 |
< |
void *p |
190 |
< |
) |
290 |
> |
/* Free cached cumulative distributions for BSDF component */ |
291 |
> |
void |
292 |
> |
SDfreeCumulativeCache(SDSpectralDF *df) |
293 |
|
{ |
294 |
< |
FVECT v2; |
295 |
< |
|
194 |
< |
v2[0] = -v[0]; |
195 |
< |
v2[1] = -v[1]; |
196 |
< |
v2[2] = -v[2]; |
294 |
> |
int n; |
295 |
> |
SDCDst *cdp; |
296 |
|
|
297 |
< |
return ab_getndx(v2, p); |
297 |
> |
if (df == NULL) |
298 |
> |
return; |
299 |
> |
for (n = df->ncomp; n-- > 0; ) |
300 |
> |
while ((cdp = df->comp[n].cdList) != NULL) { |
301 |
> |
df->comp[n].cdList = cdp->next; |
302 |
> |
free(cdp); |
303 |
> |
} |
304 |
|
} |
305 |
|
|
306 |
< |
|
307 |
< |
static void |
308 |
< |
load_angle_basis( /* load custom BSDF angle basis */ |
204 |
< |
ezxml_t wab |
205 |
< |
) |
306 |
> |
/* Free a spectral distribution function */ |
307 |
> |
void |
308 |
> |
SDfreeSpectralDF(SDSpectralDF *df) |
309 |
|
{ |
310 |
< |
char *abname = ezxml_txt(ezxml_child(wab, "AngleBasisName")); |
311 |
< |
ezxml_t wbb; |
312 |
< |
int i; |
210 |
< |
|
211 |
< |
if (!abname || !*abname) |
310 |
> |
int n; |
311 |
> |
|
312 |
> |
if (df == NULL) |
313 |
|
return; |
314 |
< |
for (i = nabases; i--; ) |
315 |
< |
if (!strcmp(abname, abase_list[i].name)) |
316 |
< |
return; /* assume it's the same */ |
317 |
< |
if (nabases >= MAXABASES) |
318 |
< |
error(INTERNAL, "too many angle bases"); |
218 |
< |
strcpy(abase_list[nabases].name, abname); |
219 |
< |
abase_list[nabases].nangles = 0; |
220 |
< |
for (i = 0, wbb = ezxml_child(wab, "AngleBasisBlock"); |
221 |
< |
wbb != NULL; i++, wbb = wbb->next) { |
222 |
< |
if (i >= MAXLATS) |
223 |
< |
error(INTERNAL, "too many latitudes in custom basis"); |
224 |
< |
abase_list[nabases].lat[i+1].tmin = atof(ezxml_txt( |
225 |
< |
ezxml_child(ezxml_child(wbb, |
226 |
< |
"ThetaBounds"), "UpperTheta"))); |
227 |
< |
if (!i) |
228 |
< |
abase_list[nabases].lat[i].tmin = |
229 |
< |
-abase_list[nabases].lat[i+1].tmin; |
230 |
< |
else if (!FEQ(atof(ezxml_txt(ezxml_child(ezxml_child(wbb, |
231 |
< |
"ThetaBounds"), "LowerTheta"))), |
232 |
< |
abase_list[nabases].lat[i].tmin)) |
233 |
< |
error(WARNING, "theta values disagree in custom basis"); |
234 |
< |
abase_list[nabases].nangles += |
235 |
< |
abase_list[nabases].lat[i].nphis = |
236 |
< |
atoi(ezxml_txt(ezxml_child(wbb, "nPhis"))); |
237 |
< |
} |
238 |
< |
abase_list[nabases++].lat[i].nphis = 0; |
314 |
> |
SDfreeCumulativeCache(df); |
315 |
> |
for (n = df->ncomp; n-- > 0; ) |
316 |
> |
if (df->comp[n].dist != NULL) |
317 |
> |
(*df->comp[n].func->freeSC)(df->comp[n].dist); |
318 |
> |
free(df); |
319 |
|
} |
320 |
|
|
321 |
< |
|
322 |
< |
static void |
323 |
< |
load_bsdf_data( /* load BSDF distribution for this wavelength */ |
244 |
< |
struct BSDF_data *dp, |
245 |
< |
ezxml_t wdb |
246 |
< |
) |
321 |
> |
/* Shorten file path to useable BSDF name, removing suffix */ |
322 |
> |
void |
323 |
> |
SDclipName(char *res, const char *fname) |
324 |
|
{ |
325 |
< |
char *cbasis = ezxml_txt(ezxml_child(wdb,"ColumnAngleBasis")); |
249 |
< |
char *rbasis = ezxml_txt(ezxml_child(wdb,"RowAngleBasis")); |
250 |
< |
char *sdata; |
251 |
< |
int i; |
325 |
> |
const char *cp, *dot = NULL; |
326 |
|
|
327 |
< |
if ((!cbasis || !*cbasis) | (!rbasis || !*rbasis)) { |
328 |
< |
error(WARNING, "missing column/row basis for BSDF"); |
327 |
> |
for (cp = fname; *cp; cp++) |
328 |
> |
if (*cp == '.') |
329 |
> |
dot = cp; |
330 |
> |
if ((dot == NULL) | (dot < fname+2)) |
331 |
> |
dot = cp; |
332 |
> |
if (dot - fname >= SDnameLn) |
333 |
> |
fname = dot - SDnameLn + 1; |
334 |
> |
while (fname < dot) |
335 |
> |
*res++ = *fname++; |
336 |
> |
*res = '\0'; |
337 |
> |
} |
338 |
> |
|
339 |
> |
/* Initialize an unused BSDF struct (simply clears to zeroes) */ |
340 |
> |
void |
341 |
> |
SDclearBSDF(SDData *sd, const char *fname) |
342 |
> |
{ |
343 |
> |
if (sd == NULL) |
344 |
|
return; |
345 |
< |
} |
346 |
< |
for (i = nabases; i--; ) |
258 |
< |
if (!strcmp(cbasis, abase_list[i].name)) { |
259 |
< |
dp->ninc = abase_list[i].nangles; |
260 |
< |
dp->ib_priv = (void *)&abase_list[i]; |
261 |
< |
dp->ib_vec = ab_getvecR; |
262 |
< |
dp->ib_ndx = ab_getndxR; |
263 |
< |
dp->ib_ohm = ab_getohm; |
264 |
< |
break; |
265 |
< |
} |
266 |
< |
if (i < 0) { |
267 |
< |
sprintf(errmsg, "undefined ColumnAngleBasis '%s'", cbasis); |
268 |
< |
error(WARNING, errmsg); |
345 |
> |
memset(sd, 0, sizeof(SDData)); |
346 |
> |
if (fname == NULL) |
347 |
|
return; |
348 |
< |
} |
349 |
< |
for (i = nabases; i--; ) |
350 |
< |
if (!strcmp(rbasis, abase_list[i].name)) { |
351 |
< |
dp->nout = abase_list[i].nangles; |
352 |
< |
dp->ob_priv = (void *)&abase_list[i]; |
353 |
< |
dp->ob_vec = ab_getvec; |
354 |
< |
dp->ob_ndx = ab_getndx; |
355 |
< |
dp->ob_ohm = ab_getohm; |
278 |
< |
break; |
279 |
< |
} |
280 |
< |
if (i < 0) { |
281 |
< |
sprintf(errmsg, "undefined RowAngleBasis '%s'", cbasis); |
282 |
< |
error(WARNING, errmsg); |
348 |
> |
SDclipName(sd->name, fname); |
349 |
> |
} |
350 |
> |
|
351 |
> |
/* Free data associated with BSDF struct */ |
352 |
> |
void |
353 |
> |
SDfreeBSDF(SDData *sd) |
354 |
> |
{ |
355 |
> |
if (sd == NULL) |
356 |
|
return; |
357 |
+ |
if (sd->mgf != NULL) { |
358 |
+ |
free(sd->mgf); |
359 |
+ |
sd->mgf = NULL; |
360 |
|
} |
361 |
< |
/* read BSDF data */ |
362 |
< |
sdata = ezxml_txt(ezxml_child(wdb,"ScatteringData")); |
363 |
< |
if (!sdata || !*sdata) { |
288 |
< |
error(WARNING, "missing BSDF ScatteringData"); |
289 |
< |
return; |
361 |
> |
if (sd->rf != NULL) { |
362 |
> |
SDfreeSpectralDF(sd->rf); |
363 |
> |
sd->rf = NULL; |
364 |
|
} |
365 |
< |
dp->bsdf = (float *)malloc(sizeof(float)*dp->ninc*dp->nout); |
366 |
< |
if (dp->bsdf == NULL) |
367 |
< |
error(SYSTEM, "out of memory in load_bsdf_data"); |
294 |
< |
for (i = 0; i < dp->ninc*dp->nout; i++) { |
295 |
< |
char *sdnext = fskip(sdata); |
296 |
< |
if (sdnext == NULL) { |
297 |
< |
error(WARNING, "bad/missing BSDF ScatteringData"); |
298 |
< |
free(dp->bsdf); dp->bsdf = NULL; |
299 |
< |
return; |
300 |
< |
} |
301 |
< |
while (*sdnext && isspace(*sdnext)) |
302 |
< |
sdnext++; |
303 |
< |
if (*sdnext == ',') sdnext++; |
304 |
< |
dp->bsdf[i] = atof(sdata); |
305 |
< |
sdata = sdnext; |
365 |
> |
if (sd->rb != NULL) { |
366 |
> |
SDfreeSpectralDF(sd->rb); |
367 |
> |
sd->rb = NULL; |
368 |
|
} |
369 |
< |
while (isspace(*sdata)) |
370 |
< |
sdata++; |
371 |
< |
if (*sdata) { |
310 |
< |
sprintf(errmsg, "%d extra characters after BSDF ScatteringData", |
311 |
< |
(int)strlen(sdata)); |
312 |
< |
error(WARNING, errmsg); |
369 |
> |
if (sd->tf != NULL) { |
370 |
> |
SDfreeSpectralDF(sd->tf); |
371 |
> |
sd->tf = NULL; |
372 |
|
} |
373 |
+ |
if (sd->tb != NULL) { |
374 |
+ |
SDfreeSpectralDF(sd->tb); |
375 |
+ |
sd->tb = NULL; |
376 |
+ |
} |
377 |
+ |
sd->rLambFront.cieY = .0; |
378 |
+ |
sd->rLambFront.spec.flags = 0; |
379 |
+ |
sd->rLambBack.cieY = .0; |
380 |
+ |
sd->rLambBack.spec.flags = 0; |
381 |
+ |
sd->tLamb.cieY = .0; |
382 |
+ |
sd->tLamb.spec.flags = 0; |
383 |
|
} |
384 |
|
|
385 |
< |
|
386 |
< |
static int |
387 |
< |
check_bsdf_data( /* check that BSDF data is sane */ |
319 |
< |
struct BSDF_data *dp |
320 |
< |
) |
385 |
> |
/* Find writeable BSDF by name, or allocate new cache entry if absent */ |
386 |
> |
SDData * |
387 |
> |
SDgetCache(const char *bname) |
388 |
|
{ |
389 |
< |
double *omega_iarr, *omega_oarr; |
390 |
< |
double dom, contrib, hemi_total; |
324 |
< |
int nneg; |
325 |
< |
FVECT v; |
326 |
< |
int i, o; |
389 |
> |
struct SDCache_s *sdl; |
390 |
> |
char sdnam[SDnameLn]; |
391 |
|
|
392 |
< |
if (dp == NULL || dp->bsdf == NULL) |
393 |
< |
return(0); |
330 |
< |
omega_iarr = (double *)calloc(dp->ninc, sizeof(double)); |
331 |
< |
omega_oarr = (double *)calloc(dp->nout, sizeof(double)); |
332 |
< |
if ((omega_iarr == NULL) | (omega_oarr == NULL)) |
333 |
< |
error(SYSTEM, "out of memory in check_bsdf_data"); |
334 |
< |
/* incoming projected solid angles */ |
335 |
< |
hemi_total = .0; |
336 |
< |
for (i = dp->ninc; i--; ) { |
337 |
< |
dom = getBSDF_incohm(dp,i); |
338 |
< |
if (dom <= .0) { |
339 |
< |
error(WARNING, "zero/negative incoming solid angle"); |
340 |
< |
continue; |
341 |
< |
} |
342 |
< |
if (!getBSDF_incvec(v,dp,i) || v[2] > FTINY) { |
343 |
< |
error(WARNING, "illegal incoming BSDF direction"); |
344 |
< |
free(omega_iarr); free(omega_oarr); |
345 |
< |
return(0); |
346 |
< |
} |
347 |
< |
hemi_total += omega_iarr[i] = dom * -v[2]; |
348 |
< |
} |
349 |
< |
if ((hemi_total > 1.02*PI) | (hemi_total < 0.98*PI)) { |
350 |
< |
sprintf(errmsg, "incoming BSDF hemisphere off by %.1f%%", |
351 |
< |
100.*(hemi_total/PI - 1.)); |
352 |
< |
error(WARNING, errmsg); |
353 |
< |
} |
354 |
< |
dom = PI / hemi_total; /* fix normalization */ |
355 |
< |
for (i = dp->ninc; i--; ) |
356 |
< |
omega_iarr[i] *= dom; |
357 |
< |
/* outgoing projected solid angles */ |
358 |
< |
hemi_total = .0; |
359 |
< |
for (o = dp->nout; o--; ) { |
360 |
< |
dom = getBSDF_outohm(dp,o); |
361 |
< |
if (dom <= .0) { |
362 |
< |
error(WARNING, "zero/negative outgoing solid angle"); |
363 |
< |
continue; |
364 |
< |
} |
365 |
< |
if (!getBSDF_outvec(v,dp,o) || v[2] < -FTINY) { |
366 |
< |
error(WARNING, "illegal outgoing BSDF direction"); |
367 |
< |
free(omega_iarr); free(omega_oarr); |
368 |
< |
return(0); |
369 |
< |
} |
370 |
< |
hemi_total += omega_oarr[o] = dom * v[2]; |
371 |
< |
} |
372 |
< |
if ((hemi_total > 1.02*PI) | (hemi_total < 0.98*PI)) { |
373 |
< |
sprintf(errmsg, "outgoing BSDF hemisphere off by %.1f%%", |
374 |
< |
100.*(hemi_total/PI - 1.)); |
375 |
< |
error(WARNING, errmsg); |
376 |
< |
} |
377 |
< |
dom = PI / hemi_total; /* fix normalization */ |
378 |
< |
for (o = dp->nout; o--; ) |
379 |
< |
omega_oarr[o] *= dom; |
380 |
< |
nneg = 0; /* check outgoing totals */ |
381 |
< |
for (i = 0; i < dp->ninc; i++) { |
382 |
< |
hemi_total = .0; |
383 |
< |
for (o = dp->nout; o--; ) { |
384 |
< |
double f = BSDF_value(dp,i,o); |
385 |
< |
if (f >= .0) |
386 |
< |
hemi_total += f*omega_oarr[o]; |
387 |
< |
else { |
388 |
< |
nneg += (f < -FTINY); |
389 |
< |
BSDF_value(dp,i,o) = .0f; |
390 |
< |
} |
391 |
< |
} |
392 |
< |
if (hemi_total > 1.02) { |
393 |
< |
sprintf(errmsg, |
394 |
< |
"incoming BSDF direction %d passes %.1f%% of light", |
395 |
< |
i, 100.*hemi_total); |
396 |
< |
error(WARNING, errmsg); |
397 |
< |
} |
398 |
< |
} |
399 |
< |
if (nneg) { |
400 |
< |
sprintf(errmsg, "%d negative BSDF values (ignored)", nneg); |
401 |
< |
error(WARNING, errmsg); |
402 |
< |
} |
403 |
< |
/* reverse roles and check again */ |
404 |
< |
for (o = 0; o < dp->nout; o++) { |
405 |
< |
hemi_total = .0; |
406 |
< |
for (i = dp->ninc; i--; ) |
407 |
< |
hemi_total += BSDF_value(dp,i,o) * omega_iarr[i]; |
392 |
> |
if (bname == NULL) |
393 |
> |
return NULL; |
394 |
|
|
395 |
< |
if (hemi_total > 1.02) { |
396 |
< |
sprintf(errmsg, |
397 |
< |
"outgoing BSDF direction %d collects %.1f%% of light", |
398 |
< |
o, 100.*hemi_total); |
399 |
< |
error(WARNING, errmsg); |
395 |
> |
SDclipName(sdnam, bname); |
396 |
> |
for (sdl = SDcacheList; sdl != NULL; sdl = sdl->next) |
397 |
> |
if (!strcmp(sdl->bsdf.name, sdnam)) { |
398 |
> |
sdl->refcnt++; |
399 |
> |
return &sdl->bsdf; |
400 |
|
} |
401 |
< |
} |
402 |
< |
free(omega_iarr); free(omega_oarr); |
403 |
< |
return(1); |
401 |
> |
|
402 |
> |
sdl = (struct SDCache_s *)calloc(1, sizeof(struct SDCache_s)); |
403 |
> |
if (sdl == NULL) |
404 |
> |
return NULL; |
405 |
> |
|
406 |
> |
strcpy(sdl->bsdf.name, sdnam); |
407 |
> |
sdl->next = SDcacheList; |
408 |
> |
SDcacheList = sdl; |
409 |
> |
|
410 |
> |
sdl->refcnt = 1; |
411 |
> |
return &sdl->bsdf; |
412 |
|
} |
413 |
|
|
414 |
< |
struct BSDF_data * |
415 |
< |
load_BSDF( /* load BSDF data from file */ |
416 |
< |
char *fname |
417 |
< |
) |
414 |
> |
/* Get loaded BSDF from cache (or load and cache it on first call) */ |
415 |
> |
/* Report any problem to stderr and return NULL on failure */ |
416 |
> |
const SDData * |
417 |
> |
SDcacheFile(const char *fname) |
418 |
|
{ |
419 |
< |
char *path; |
420 |
< |
ezxml_t fl, wtl, wld, wdb; |
427 |
< |
struct BSDF_data *dp; |
419 |
> |
SDData *sd; |
420 |
> |
SDError ec; |
421 |
|
|
422 |
< |
path = getpath(fname, getrlibpath(), R_OK); |
423 |
< |
if (path == NULL) { |
424 |
< |
sprintf(errmsg, "cannot find BSDF file \"%s\"", fname); |
425 |
< |
error(WARNING, errmsg); |
426 |
< |
return(NULL); |
422 |
> |
if (fname == NULL || !*fname) |
423 |
> |
return NULL; |
424 |
> |
SDerrorDetail[0] = '\0'; |
425 |
> |
if ((sd = SDgetCache(fname)) == NULL) { |
426 |
> |
SDreportError(SDEmemory, stderr); |
427 |
> |
return NULL; |
428 |
|
} |
429 |
< |
fl = ezxml_parse_file(path); |
430 |
< |
if (fl == NULL) { |
431 |
< |
sprintf(errmsg, "cannot open BSDF \"%s\"", path); |
432 |
< |
error(WARNING, errmsg); |
439 |
< |
return(NULL); |
429 |
> |
if (!SDisLoaded(sd) && (ec = SDloadFile(sd, fname))) { |
430 |
> |
SDreportError(ec, stderr); |
431 |
> |
SDfreeCache(sd); |
432 |
> |
return NULL; |
433 |
|
} |
434 |
< |
if (ezxml_error(fl)[0]) { |
435 |
< |
sprintf(errmsg, "BSDF \"%s\" %s", path, ezxml_error(fl)); |
436 |
< |
error(WARNING, errmsg); |
437 |
< |
ezxml_free(fl); |
438 |
< |
return(NULL); |
434 |
> |
return sd; |
435 |
> |
} |
436 |
> |
|
437 |
> |
/* Free a BSDF from our cache (clear all if NULL) */ |
438 |
> |
void |
439 |
> |
SDfreeCache(const SDData *sd) |
440 |
> |
{ |
441 |
> |
struct SDCache_s *sdl, *sdLast = NULL; |
442 |
> |
|
443 |
> |
if (sd == NULL) { /* free entire list */ |
444 |
> |
while ((sdl = SDcacheList) != NULL) { |
445 |
> |
SDcacheList = sdl->next; |
446 |
> |
SDfreeBSDF(&sdl->bsdf); |
447 |
> |
free(sdl); |
448 |
> |
} |
449 |
> |
return; |
450 |
|
} |
451 |
< |
if (strcmp(ezxml_name(fl), "WindowElement")) { |
452 |
< |
sprintf(errmsg, |
453 |
< |
"BSDF \"%s\": top level node not 'WindowElement'", |
454 |
< |
path); |
455 |
< |
error(WARNING, errmsg); |
456 |
< |
ezxml_free(fl); |
457 |
< |
return(NULL); |
451 |
> |
for (sdl = SDcacheList; sdl != NULL; sdl = (sdLast=sdl)->next) |
452 |
> |
if (&sdl->bsdf == sd) |
453 |
> |
break; |
454 |
> |
if (sdl == NULL || (sdl->refcnt -= (sdl->refcnt > 0))) |
455 |
> |
return; /* missing or still in use */ |
456 |
> |
/* keep unreferenced data? */ |
457 |
> |
if (SDisLoaded(sd) && SDretainSet) { |
458 |
> |
if (SDretainSet == SDretainAll) |
459 |
> |
return; /* keep everything */ |
460 |
> |
/* else free cumulative data */ |
461 |
> |
SDfreeCumulativeCache(sd->rf); |
462 |
> |
SDfreeCumulativeCache(sd->rb); |
463 |
> |
SDfreeCumulativeCache(sd->tf); |
464 |
> |
SDfreeCumulativeCache(sd->tb); |
465 |
> |
return; |
466 |
|
} |
467 |
< |
wtl = ezxml_child(ezxml_child(fl, "Optical"), "Layer"); |
468 |
< |
load_angle_basis(ezxml_child(ezxml_child(wtl, |
469 |
< |
"DataDefinition"), "AngleBasis")); |
470 |
< |
dp = (struct BSDF_data *)calloc(1, sizeof(struct BSDF_data)); |
471 |
< |
for (wld = ezxml_child(wtl, "WavelengthData"); |
472 |
< |
wld != NULL; wld = wld->next) { |
473 |
< |
if (strcmp(ezxml_txt(ezxml_child(wld,"Wavelength")), "Visible")) |
474 |
< |
continue; |
475 |
< |
wdb = ezxml_child(wld, "WavelengthDataBlock"); |
476 |
< |
if (wdb == NULL) continue; |
477 |
< |
if (strcmp(ezxml_txt(ezxml_child(wdb,"WavelengthDataDirection")), |
478 |
< |
"Transmission Front")) |
479 |
< |
continue; |
480 |
< |
load_bsdf_data(dp, wdb); /* load front BTDF */ |
481 |
< |
break; /* ignore the rest */ |
467 |
> |
/* remove from list and free */ |
468 |
> |
if (sdLast == NULL) |
469 |
> |
SDcacheList = sdl->next; |
470 |
> |
else |
471 |
> |
sdLast->next = sdl->next; |
472 |
> |
SDfreeBSDF(&sdl->bsdf); |
473 |
> |
free(sdl); |
474 |
> |
} |
475 |
> |
|
476 |
> |
/* Sample an individual BSDF component */ |
477 |
> |
SDError |
478 |
> |
SDsampComponent(SDValue *sv, FVECT ioVec, double randX, SDComponent *sdc) |
479 |
> |
{ |
480 |
> |
float coef[SDmaxCh]; |
481 |
> |
SDError ec; |
482 |
> |
FVECT inVec; |
483 |
> |
const SDCDst *cd; |
484 |
> |
double d; |
485 |
> |
int n; |
486 |
> |
/* check arguments */ |
487 |
> |
if ((sv == NULL) | (ioVec == NULL) | (sdc == NULL)) |
488 |
> |
return SDEargument; |
489 |
> |
/* get cumulative distribution */ |
490 |
> |
VCOPY(inVec, ioVec); |
491 |
> |
cd = (*sdc->func->getCDist)(inVec, sdc); |
492 |
> |
if (cd == NULL) |
493 |
> |
return SDEmemory; |
494 |
> |
if (cd->cTotal <= 1e-6) { /* anything to sample? */ |
495 |
> |
sv->spec = c_dfcolor; |
496 |
> |
sv->cieY = .0; |
497 |
> |
memset(ioVec, 0, 3*sizeof(double)); |
498 |
> |
return SDEnone; |
499 |
|
} |
500 |
< |
ezxml_free(fl); /* done with XML file */ |
501 |
< |
if (!check_bsdf_data(dp)) { |
502 |
< |
sprintf(errmsg, "bad/missing BTDF data in \"%s\"", path); |
503 |
< |
error(WARNING, errmsg); |
504 |
< |
free_BSDF(dp); |
505 |
< |
dp = NULL; |
500 |
> |
sv->cieY = cd->cTotal; |
501 |
> |
/* compute sample direction */ |
502 |
> |
ec = (*sdc->func->sampCDist)(ioVec, randX, cd); |
503 |
> |
if (ec) |
504 |
> |
return ec; |
505 |
> |
/* get BSDF color */ |
506 |
> |
n = (*sdc->func->getBSDFs)(coef, ioVec, inVec, sdc); |
507 |
> |
if (n <= 0) { |
508 |
> |
strcpy(SDerrorDetail, "BSDF sample value error"); |
509 |
> |
return SDEinternal; |
510 |
|
} |
511 |
< |
return(dp); |
511 |
> |
sv->spec = sdc->cspec[0]; |
512 |
> |
d = coef[0]; |
513 |
> |
while (--n) { |
514 |
> |
c_cmix(&sv->spec, d, &sv->spec, coef[n], &sdc->cspec[n]); |
515 |
> |
d += coef[n]; |
516 |
> |
} |
517 |
> |
/* make sure everything is set */ |
518 |
> |
c_ccvt(&sv->spec, C_CSXY+C_CSSPEC); |
519 |
> |
return SDEnone; |
520 |
|
} |
521 |
|
|
522 |
+ |
#define MS_MAXDIM 15 |
523 |
|
|
524 |
+ |
/* Convert 1-dimensional random variable to N-dimensional */ |
525 |
|
void |
526 |
< |
free_BSDF( /* free BSDF data structure */ |
484 |
< |
struct BSDF_data *b |
485 |
< |
) |
526 |
> |
SDmultiSamp(double t[], int n, double randX) |
527 |
|
{ |
528 |
< |
if (b == NULL) |
528 |
> |
unsigned nBits; |
529 |
> |
double scale; |
530 |
> |
bitmask_t ndx, coord[MS_MAXDIM]; |
531 |
> |
|
532 |
> |
if (n <= 0) /* check corner cases */ |
533 |
|
return; |
534 |
< |
if (b->bsdf != NULL) |
535 |
< |
free(b->bsdf); |
536 |
< |
free(b); |
534 |
> |
if (randX < 0) randX = 0; |
535 |
> |
else if (randX >= 1.) randX = 0.999999999999999; |
536 |
> |
if (n == 1) { |
537 |
> |
t[0] = randX; |
538 |
> |
return; |
539 |
> |
} |
540 |
> |
while (n > MS_MAXDIM) /* punt for higher dimensions */ |
541 |
> |
t[--n] = rand()*(1./(RAND_MAX+.5)); |
542 |
> |
nBits = (8*sizeof(bitmask_t) - 1) / n; |
543 |
> |
ndx = randX * (double)((bitmask_t)1 << (nBits*n)); |
544 |
> |
/* get coordinate on Hilbert curve */ |
545 |
> |
hilbert_i2c(n, nBits, ndx, coord); |
546 |
> |
/* convert back to [0,1) range */ |
547 |
> |
scale = 1. / (double)((bitmask_t)1 << nBits); |
548 |
> |
while (n--) |
549 |
> |
t[n] = scale * ((double)coord[n] + rand()*(1./(RAND_MAX+.5))); |
550 |
|
} |
551 |
|
|
552 |
+ |
#undef MS_MAXDIM |
553 |
|
|
554 |
< |
int |
555 |
< |
r_BSDF_incvec( /* compute random input vector at given location */ |
556 |
< |
FVECT v, |
498 |
< |
struct BSDF_data *b, |
499 |
< |
int i, |
500 |
< |
double rv, |
501 |
< |
MAT4 xm |
502 |
< |
) |
554 |
> |
/* Generate diffuse hemispherical sample */ |
555 |
> |
static void |
556 |
> |
SDdiffuseSamp(FVECT outVec, int outFront, double randX) |
557 |
|
{ |
558 |
< |
FVECT pert; |
559 |
< |
double rad; |
560 |
< |
int j; |
561 |
< |
|
562 |
< |
if (!getBSDF_incvec(v, b, i)) |
563 |
< |
return(0); |
564 |
< |
rad = sqrt(getBSDF_incohm(b, i) / PI); |
565 |
< |
multisamp(pert, 3, rv); |
512 |
< |
for (j = 0; j < 3; j++) |
513 |
< |
v[j] += rad*(2.*pert[j] - 1.); |
514 |
< |
if (xm != NULL) |
515 |
< |
multv3(v, v, xm); |
516 |
< |
return(normalize(v) != 0.0); |
558 |
> |
/* convert to position on hemisphere */ |
559 |
> |
SDmultiSamp(outVec, 2, randX); |
560 |
> |
SDsquare2disk(outVec, outVec[0], outVec[1]); |
561 |
> |
outVec[2] = 1. - outVec[0]*outVec[0] - outVec[1]*outVec[1]; |
562 |
> |
if (outVec[2] > 0) /* a bit of paranoia */ |
563 |
> |
outVec[2] = sqrt(outVec[2]); |
564 |
> |
if (!outFront) /* going out back? */ |
565 |
> |
outVec[2] = -outVec[2]; |
566 |
|
} |
567 |
|
|
568 |
+ |
/* Query projected solid angle coverage for non-diffuse BSDF direction */ |
569 |
+ |
SDError |
570 |
+ |
SDsizeBSDF(double *projSA, const FVECT v1, const RREAL *v2, |
571 |
+ |
int qflags, const SDData *sd) |
572 |
+ |
{ |
573 |
+ |
SDSpectralDF *rdf, *tdf; |
574 |
+ |
SDError ec; |
575 |
+ |
int i; |
576 |
+ |
/* check arguments */ |
577 |
+ |
if ((projSA == NULL) | (v1 == NULL) | (sd == NULL)) |
578 |
+ |
return SDEargument; |
579 |
+ |
/* initialize extrema */ |
580 |
+ |
switch (qflags) { |
581 |
+ |
case SDqueryMax: |
582 |
+ |
projSA[0] = .0; |
583 |
+ |
break; |
584 |
+ |
case SDqueryMin+SDqueryMax: |
585 |
+ |
projSA[1] = .0; |
586 |
+ |
/* fall through */ |
587 |
+ |
case SDqueryMin: |
588 |
+ |
projSA[0] = 10.; |
589 |
+ |
break; |
590 |
+ |
case 0: |
591 |
+ |
return SDEargument; |
592 |
+ |
} |
593 |
+ |
if (v1[2] > 0) { /* front surface query? */ |
594 |
+ |
rdf = sd->rf; |
595 |
+ |
tdf = (sd->tf != NULL) ? sd->tf : sd->tb; |
596 |
+ |
} else { |
597 |
+ |
rdf = sd->rb; |
598 |
+ |
tdf = (sd->tb != NULL) ? sd->tb : sd->tf; |
599 |
+ |
} |
600 |
+ |
if (v2 != NULL) /* bidirectional? */ |
601 |
+ |
if (v1[2] > 0 ^ v2[2] > 0) |
602 |
+ |
rdf = NULL; |
603 |
+ |
else |
604 |
+ |
tdf = NULL; |
605 |
+ |
ec = SDEdata; /* run through components */ |
606 |
+ |
for (i = (rdf==NULL) ? 0 : rdf->ncomp; i--; ) { |
607 |
+ |
ec = (*rdf->comp[i].func->queryProjSA)(projSA, v1, v2, |
608 |
+ |
qflags, &rdf->comp[i]); |
609 |
+ |
if (ec) |
610 |
+ |
return ec; |
611 |
+ |
} |
612 |
+ |
for (i = (tdf==NULL) ? 0 : tdf->ncomp; i--; ) { |
613 |
+ |
ec = (*tdf->comp[i].func->queryProjSA)(projSA, v1, v2, |
614 |
+ |
qflags, &tdf->comp[i]); |
615 |
+ |
if (ec) |
616 |
+ |
return ec; |
617 |
+ |
} |
618 |
+ |
if (ec) { /* all diffuse? */ |
619 |
+ |
projSA[0] = M_PI; |
620 |
+ |
if (qflags == SDqueryMin+SDqueryMax) |
621 |
+ |
projSA[1] = M_PI; |
622 |
+ |
} |
623 |
+ |
return SDEnone; |
624 |
+ |
} |
625 |
|
|
626 |
< |
int |
627 |
< |
r_BSDF_outvec( /* compute random output vector at given location */ |
628 |
< |
FVECT v, |
523 |
< |
struct BSDF_data *b, |
524 |
< |
int o, |
525 |
< |
double rv, |
526 |
< |
MAT4 xm |
527 |
< |
) |
626 |
> |
/* Return BSDF for the given incident and scattered ray vectors */ |
627 |
> |
SDError |
628 |
> |
SDevalBSDF(SDValue *sv, const FVECT outVec, const FVECT inVec, const SDData *sd) |
629 |
|
{ |
630 |
< |
FVECT pert; |
631 |
< |
double rad; |
632 |
< |
int j; |
633 |
< |
|
634 |
< |
if (!getBSDF_outvec(v, b, o)) |
635 |
< |
return(0); |
636 |
< |
rad = sqrt(getBSDF_outohm(b, o) / PI); |
637 |
< |
multisamp(pert, 3, rv); |
638 |
< |
for (j = 0; j < 3; j++) |
639 |
< |
v[j] += rad*(2.*pert[j] - 1.); |
640 |
< |
if (xm != NULL) |
641 |
< |
multv3(v, v, xm); |
642 |
< |
return(normalize(v) != 0.0); |
630 |
> |
int inFront, outFront; |
631 |
> |
SDSpectralDF *sdf; |
632 |
> |
float coef[SDmaxCh]; |
633 |
> |
int nch, i; |
634 |
> |
/* check arguments */ |
635 |
> |
if ((sv == NULL) | (outVec == NULL) | (inVec == NULL) | (sd == NULL)) |
636 |
> |
return SDEargument; |
637 |
> |
/* whose side are we on? */ |
638 |
> |
inFront = (inVec[2] > 0); |
639 |
> |
outFront = (outVec[2] > 0); |
640 |
> |
/* start with diffuse portion */ |
641 |
> |
if (inFront & outFront) { |
642 |
> |
*sv = sd->rLambFront; |
643 |
> |
sdf = sd->rf; |
644 |
> |
} else if (!(inFront | outFront)) { |
645 |
> |
*sv = sd->rLambBack; |
646 |
> |
sdf = sd->rb; |
647 |
> |
} else if (inFront) { |
648 |
> |
*sv = sd->tLamb; |
649 |
> |
sdf = (sd->tf != NULL) ? sd->tf : sd->tb; |
650 |
> |
} else /* inBack */ { |
651 |
> |
*sv = sd->tLamb; |
652 |
> |
sdf = (sd->tb != NULL) ? sd->tb : sd->tf; |
653 |
> |
} |
654 |
> |
sv->cieY *= 1./M_PI; |
655 |
> |
/* add non-diffuse components */ |
656 |
> |
i = (sdf != NULL) ? sdf->ncomp : 0; |
657 |
> |
while (i-- > 0) { |
658 |
> |
nch = (*sdf->comp[i].func->getBSDFs)(coef, outVec, inVec, |
659 |
> |
&sdf->comp[i]); |
660 |
> |
while (nch-- > 0) { |
661 |
> |
c_cmix(&sv->spec, sv->cieY, &sv->spec, |
662 |
> |
coef[nch], &sdf->comp[i].cspec[nch]); |
663 |
> |
sv->cieY += coef[nch]; |
664 |
> |
} |
665 |
> |
} |
666 |
> |
/* make sure everything is set */ |
667 |
> |
c_ccvt(&sv->spec, C_CSXY+C_CSSPEC); |
668 |
> |
return SDEnone; |
669 |
|
} |
670 |
|
|
671 |
< |
|
672 |
< |
static int |
673 |
< |
addrot( /* compute rotation (x,y,z) => (xp,yp,zp) */ |
547 |
< |
char *xfarg[], |
548 |
< |
FVECT xp, |
549 |
< |
FVECT yp, |
550 |
< |
FVECT zp |
551 |
< |
) |
671 |
> |
/* Compute directional hemispherical scattering at this incident angle */ |
672 |
> |
double |
673 |
> |
SDdirectHemi(const FVECT inVec, int sflags, const SDData *sd) |
674 |
|
{ |
675 |
< |
static char bufs[3][16]; |
676 |
< |
int bn = 0; |
677 |
< |
char **xfp = xfarg; |
678 |
< |
double theta; |
675 |
> |
double hsum; |
676 |
> |
SDSpectralDF *rdf, *tdf; |
677 |
> |
const SDCDst *cd; |
678 |
> |
int i; |
679 |
> |
/* check arguments */ |
680 |
> |
if ((inVec == NULL) | (sd == NULL)) |
681 |
> |
return .0; |
682 |
> |
/* gather diffuse components */ |
683 |
> |
if (inVec[2] > 0) { |
684 |
> |
hsum = sd->rLambFront.cieY; |
685 |
> |
rdf = sd->rf; |
686 |
> |
tdf = (sd->tf != NULL) ? sd->tf : sd->tb; |
687 |
> |
} else /* !inFront */ { |
688 |
> |
hsum = sd->rLambBack.cieY; |
689 |
> |
rdf = sd->rb; |
690 |
> |
tdf = (sd->tb != NULL) ? sd->tb : sd->tf; |
691 |
> |
} |
692 |
> |
if ((sflags & SDsampDf+SDsampR) != SDsampDf+SDsampR) |
693 |
> |
hsum = .0; |
694 |
> |
if ((sflags & SDsampDf+SDsampT) == SDsampDf+SDsampT) |
695 |
> |
hsum += sd->tLamb.cieY; |
696 |
> |
/* gather non-diffuse components */ |
697 |
> |
i = (((sflags & SDsampSp+SDsampR) == SDsampSp+SDsampR) & |
698 |
> |
(rdf != NULL)) ? rdf->ncomp : 0; |
699 |
> |
while (i-- > 0) { /* non-diffuse reflection */ |
700 |
> |
cd = (*rdf->comp[i].func->getCDist)(inVec, &rdf->comp[i]); |
701 |
> |
if (cd != NULL) |
702 |
> |
hsum += cd->cTotal; |
703 |
> |
} |
704 |
> |
i = (((sflags & SDsampSp+SDsampT) == SDsampSp+SDsampT) & |
705 |
> |
(tdf != NULL)) ? tdf->ncomp : 0; |
706 |
> |
while (i-- > 0) { /* non-diffuse transmission */ |
707 |
> |
cd = (*tdf->comp[i].func->getCDist)(inVec, &tdf->comp[i]); |
708 |
> |
if (cd != NULL) |
709 |
> |
hsum += cd->cTotal; |
710 |
> |
} |
711 |
> |
return hsum; |
712 |
> |
} |
713 |
|
|
714 |
< |
if (yp[2]*yp[2] + zp[2]*zp[2] < 2.*FTINY*FTINY) { |
715 |
< |
/* Special case for X' along Z-axis */ |
716 |
< |
theta = -atan2(yp[0], yp[1]); |
717 |
< |
*xfp++ = "-ry"; |
718 |
< |
*xfp++ = xp[2] < 0.0 ? "90" : "-90"; |
719 |
< |
*xfp++ = "-rz"; |
720 |
< |
sprintf(bufs[bn], "%f", theta*(180./PI)); |
721 |
< |
*xfp++ = bufs[bn++]; |
722 |
< |
return(xfp - xfarg); |
714 |
> |
/* Sample BSDF direction based on the given random variable */ |
715 |
> |
SDError |
716 |
> |
SDsampBSDF(SDValue *sv, FVECT ioVec, double randX, int sflags, const SDData *sd) |
717 |
> |
{ |
718 |
> |
SDError ec; |
719 |
> |
FVECT inVec; |
720 |
> |
int inFront; |
721 |
> |
SDSpectralDF *rdf, *tdf; |
722 |
> |
double rdiff; |
723 |
> |
float coef[SDmaxCh]; |
724 |
> |
int i, j, n, nr; |
725 |
> |
SDComponent *sdc; |
726 |
> |
const SDCDst **cdarr = NULL; |
727 |
> |
/* check arguments */ |
728 |
> |
if ((sv == NULL) | (ioVec == NULL) | (sd == NULL) | |
729 |
> |
(randX < 0) | (randX >= 1.)) |
730 |
> |
return SDEargument; |
731 |
> |
/* whose side are we on? */ |
732 |
> |
VCOPY(inVec, ioVec); |
733 |
> |
inFront = (inVec[2] > 0); |
734 |
> |
/* remember diffuse portions */ |
735 |
> |
if (inFront) { |
736 |
> |
*sv = sd->rLambFront; |
737 |
> |
rdf = sd->rf; |
738 |
> |
tdf = (sd->tf != NULL) ? sd->tf : sd->tb; |
739 |
> |
} else /* !inFront */ { |
740 |
> |
*sv = sd->rLambBack; |
741 |
> |
rdf = sd->rb; |
742 |
> |
tdf = (sd->tb != NULL) ? sd->tb : sd->tf; |
743 |
|
} |
744 |
< |
theta = atan2(yp[2], zp[2]); |
745 |
< |
if (!FEQ(theta,0.0)) { |
746 |
< |
*xfp++ = "-rx"; |
747 |
< |
sprintf(bufs[bn], "%f", theta*(180./PI)); |
748 |
< |
*xfp++ = bufs[bn++]; |
744 |
> |
if ((sflags & SDsampDf+SDsampR) != SDsampDf+SDsampR) |
745 |
> |
sv->cieY = .0; |
746 |
> |
rdiff = sv->cieY; |
747 |
> |
if ((sflags & SDsampDf+SDsampT) == SDsampDf+SDsampT) |
748 |
> |
sv->cieY += sd->tLamb.cieY; |
749 |
> |
/* gather non-diffuse components */ |
750 |
> |
i = nr = (((sflags & SDsampSp+SDsampR) == SDsampSp+SDsampR) & |
751 |
> |
(rdf != NULL)) ? rdf->ncomp : 0; |
752 |
> |
j = (((sflags & SDsampSp+SDsampT) == SDsampSp+SDsampT) & |
753 |
> |
(tdf != NULL)) ? tdf->ncomp : 0; |
754 |
> |
n = i + j; |
755 |
> |
if (n > 0 && (cdarr = (const SDCDst **)malloc(n*sizeof(SDCDst *))) == NULL) |
756 |
> |
return SDEmemory; |
757 |
> |
while (j-- > 0) { /* non-diffuse transmission */ |
758 |
> |
cdarr[i+j] = (*tdf->comp[j].func->getCDist)(inVec, &tdf->comp[j]); |
759 |
> |
if (cdarr[i+j] == NULL) { |
760 |
> |
free(cdarr); |
761 |
> |
return SDEmemory; |
762 |
> |
} |
763 |
> |
sv->cieY += cdarr[i+j]->cTotal; |
764 |
|
} |
765 |
< |
theta = asin(-xp[2]); |
766 |
< |
if (!FEQ(theta,0.0)) { |
767 |
< |
*xfp++ = "-ry"; |
768 |
< |
sprintf(bufs[bn], " %f", theta*(180./PI)); |
769 |
< |
*xfp++ = bufs[bn++]; |
765 |
> |
while (i-- > 0) { /* non-diffuse reflection */ |
766 |
> |
cdarr[i] = (*rdf->comp[i].func->getCDist)(inVec, &rdf->comp[i]); |
767 |
> |
if (cdarr[i] == NULL) { |
768 |
> |
free(cdarr); |
769 |
> |
return SDEmemory; |
770 |
> |
} |
771 |
> |
sv->cieY += cdarr[i]->cTotal; |
772 |
|
} |
773 |
< |
theta = atan2(xp[1], xp[0]); |
774 |
< |
if (!FEQ(theta,0.0)) { |
775 |
< |
*xfp++ = "-rz"; |
776 |
< |
sprintf(bufs[bn], "%f", theta*(180./PI)); |
584 |
< |
*xfp++ = bufs[bn++]; |
773 |
> |
if (sv->cieY <= 1e-6) { /* anything to sample? */ |
774 |
> |
sv->cieY = .0; |
775 |
> |
memset(ioVec, 0, 3*sizeof(double)); |
776 |
> |
return SDEnone; |
777 |
|
} |
778 |
< |
*xfp = NULL; |
779 |
< |
return(xfp - xfarg); |
778 |
> |
/* scale random variable */ |
779 |
> |
randX *= sv->cieY; |
780 |
> |
/* diffuse reflection? */ |
781 |
> |
if (randX < rdiff) { |
782 |
> |
SDdiffuseSamp(ioVec, inFront, randX/rdiff); |
783 |
> |
goto done; |
784 |
> |
} |
785 |
> |
randX -= rdiff; |
786 |
> |
/* diffuse transmission? */ |
787 |
> |
if ((sflags & SDsampDf+SDsampT) == SDsampDf+SDsampT) { |
788 |
> |
if (randX < sd->tLamb.cieY) { |
789 |
> |
sv->spec = sd->tLamb.spec; |
790 |
> |
SDdiffuseSamp(ioVec, !inFront, randX/sd->tLamb.cieY); |
791 |
> |
goto done; |
792 |
> |
} |
793 |
> |
randX -= sd->tLamb.cieY; |
794 |
> |
} |
795 |
> |
/* else one of cumulative dist. */ |
796 |
> |
for (i = 0; i < n && randX < cdarr[i]->cTotal; i++) |
797 |
> |
randX -= cdarr[i]->cTotal; |
798 |
> |
if (i >= n) |
799 |
> |
return SDEinternal; |
800 |
> |
/* compute sample direction */ |
801 |
> |
sdc = (i < nr) ? &rdf->comp[i] : &tdf->comp[i-nr]; |
802 |
> |
ec = (*sdc->func->sampCDist)(ioVec, randX/cdarr[i]->cTotal, cdarr[i]); |
803 |
> |
if (ec) |
804 |
> |
return ec; |
805 |
> |
/* compute color */ |
806 |
> |
j = (*sdc->func->getBSDFs)(coef, ioVec, inVec, sdc); |
807 |
> |
if (j <= 0) { |
808 |
> |
sprintf(SDerrorDetail, "BSDF \"%s\" sampling value error", |
809 |
> |
sd->name); |
810 |
> |
return SDEinternal; |
811 |
> |
} |
812 |
> |
sv->spec = sdc->cspec[0]; |
813 |
> |
rdiff = coef[0]; |
814 |
> |
while (--j) { |
815 |
> |
c_cmix(&sv->spec, rdiff, &sv->spec, coef[j], &sdc->cspec[j]); |
816 |
> |
rdiff += coef[j]; |
817 |
> |
} |
818 |
> |
done: |
819 |
> |
if (cdarr != NULL) |
820 |
> |
free(cdarr); |
821 |
> |
/* make sure everything is set */ |
822 |
> |
c_ccvt(&sv->spec, C_CSXY+C_CSSPEC); |
823 |
> |
return SDEnone; |
824 |
|
} |
825 |
|
|
826 |
+ |
/* Compute World->BSDF transform from surface normal and up (Y) vector */ |
827 |
+ |
SDError |
828 |
+ |
SDcompXform(RREAL vMtx[3][3], const FVECT sNrm, const FVECT uVec) |
829 |
+ |
{ |
830 |
+ |
if ((vMtx == NULL) | (sNrm == NULL) | (uVec == NULL)) |
831 |
+ |
return SDEargument; |
832 |
+ |
VCOPY(vMtx[2], sNrm); |
833 |
+ |
if (normalize(vMtx[2]) == 0) |
834 |
+ |
return SDEargument; |
835 |
+ |
fcross(vMtx[0], uVec, vMtx[2]); |
836 |
+ |
if (normalize(vMtx[0]) == 0) |
837 |
+ |
return SDEargument; |
838 |
+ |
fcross(vMtx[1], vMtx[2], vMtx[0]); |
839 |
+ |
return SDEnone; |
840 |
+ |
} |
841 |
|
|
842 |
< |
int |
843 |
< |
getBSDF_xfm( /* compute BSDF orient. -> world orient. transform */ |
844 |
< |
MAT4 xm, |
594 |
< |
FVECT nrm, |
595 |
< |
UpDir ud |
596 |
< |
) |
842 |
> |
/* Compute inverse transform */ |
843 |
> |
SDError |
844 |
> |
SDinvXform(RREAL iMtx[3][3], RREAL vMtx[3][3]) |
845 |
|
{ |
846 |
< |
char *xfargs[7]; |
847 |
< |
XF myxf; |
600 |
< |
FVECT updir, xdest, ydest; |
846 |
> |
RREAL mTmp[3][3]; |
847 |
> |
double d; |
848 |
|
|
849 |
< |
updir[0] = updir[1] = updir[2] = 0.; |
850 |
< |
switch (ud) { |
851 |
< |
case UDzneg: |
852 |
< |
updir[2] = -1.; |
853 |
< |
break; |
854 |
< |
case UDyneg: |
855 |
< |
updir[1] = -1.; |
856 |
< |
break; |
857 |
< |
case UDxneg: |
858 |
< |
updir[0] = -1.; |
612 |
< |
break; |
613 |
< |
case UDxpos: |
614 |
< |
updir[0] = 1.; |
615 |
< |
break; |
616 |
< |
case UDypos: |
617 |
< |
updir[1] = 1.; |
618 |
< |
break; |
619 |
< |
case UDzpos: |
620 |
< |
updir[2] = 1.; |
621 |
< |
break; |
622 |
< |
case UDunknown: |
623 |
< |
return(0); |
849 |
> |
if ((iMtx == NULL) | (vMtx == NULL)) |
850 |
> |
return SDEargument; |
851 |
> |
/* compute determinant */ |
852 |
> |
mTmp[0][0] = vMtx[2][2]*vMtx[1][1] - vMtx[2][1]*vMtx[1][2]; |
853 |
> |
mTmp[0][1] = vMtx[2][1]*vMtx[0][2] - vMtx[2][2]*vMtx[0][1]; |
854 |
> |
mTmp[0][2] = vMtx[1][2]*vMtx[0][1] - vMtx[1][1]*vMtx[0][2]; |
855 |
> |
d = vMtx[0][0]*mTmp[0][0] + vMtx[1][0]*mTmp[0][1] + vMtx[2][0]*mTmp[0][2]; |
856 |
> |
if (d == 0) { |
857 |
> |
strcpy(SDerrorDetail, "Zero determinant in matrix inversion"); |
858 |
> |
return SDEargument; |
859 |
|
} |
860 |
< |
fcross(xdest, updir, nrm); |
861 |
< |
if (normalize(xdest) == 0.0) |
862 |
< |
return(0); |
863 |
< |
fcross(ydest, nrm, xdest); |
864 |
< |
xf(&myxf, addrot(xfargs, xdest, ydest, nrm), xfargs); |
865 |
< |
copymat4(xm, myxf.xfm); |
866 |
< |
return(1); |
860 |
> |
d = 1./d; /* invert matrix */ |
861 |
> |
mTmp[0][0] *= d; mTmp[0][1] *= d; mTmp[0][2] *= d; |
862 |
> |
mTmp[1][0] = d*(vMtx[2][0]*vMtx[1][2] - vMtx[2][2]*vMtx[1][0]); |
863 |
> |
mTmp[1][1] = d*(vMtx[2][2]*vMtx[0][0] - vMtx[2][0]*vMtx[0][2]); |
864 |
> |
mTmp[1][2] = d*(vMtx[1][0]*vMtx[0][2] - vMtx[1][2]*vMtx[0][0]); |
865 |
> |
mTmp[2][0] = d*(vMtx[2][1]*vMtx[1][0] - vMtx[2][0]*vMtx[1][1]); |
866 |
> |
mTmp[2][1] = d*(vMtx[2][0]*vMtx[0][1] - vMtx[2][1]*vMtx[0][0]); |
867 |
> |
mTmp[2][2] = d*(vMtx[1][1]*vMtx[0][0] - vMtx[1][0]*vMtx[0][1]); |
868 |
> |
memcpy(iMtx, mTmp, sizeof(mTmp)); |
869 |
> |
return SDEnone; |
870 |
> |
} |
871 |
> |
|
872 |
> |
/* Transform and normalize direction (column) vector */ |
873 |
> |
SDError |
874 |
> |
SDmapDir(FVECT resVec, RREAL vMtx[3][3], const FVECT inpVec) |
875 |
> |
{ |
876 |
> |
FVECT vTmp; |
877 |
> |
|
878 |
> |
if ((resVec == NULL) | (inpVec == NULL)) |
879 |
> |
return SDEargument; |
880 |
> |
if (vMtx == NULL) { /* assume they just want to normalize */ |
881 |
> |
if (resVec != inpVec) |
882 |
> |
VCOPY(resVec, inpVec); |
883 |
> |
return (normalize(resVec) > 0) ? SDEnone : SDEargument; |
884 |
> |
} |
885 |
> |
vTmp[0] = DOT(vMtx[0], inpVec); |
886 |
> |
vTmp[1] = DOT(vMtx[1], inpVec); |
887 |
> |
vTmp[2] = DOT(vMtx[2], inpVec); |
888 |
> |
if (normalize(vTmp) == 0) |
889 |
> |
return SDEargument; |
890 |
> |
VCOPY(resVec, vTmp); |
891 |
> |
return SDEnone; |
892 |
|
} |