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

Comparing ray/src/util/eplus_adduvf.c (file contents):
Revision 2.10 by greg, Thu Feb 13 17:33:37 2014 UTC vs.
Revision 2.19 by greg, Fri Dec 13 16:00:33 2019 UTC

# Line 8 | Line 8 | static const char RCSid[] = "$Id$";
8   */
9  
10   #include <stdlib.h>
11 + #include "platform.h"
12   #include "rtio.h"
13   #include "rtmath.h"
14   #include "random.h"
# Line 19 | Line 20 | static const char RCSid[] = "$Id$";
20   #define NSAMPLES        80000                   /* default number of samples */
21   #endif
22  
23 + #define SURF_EPS        0.0005                  /* surface testing epsilon */
24 +
25   char            *progname;                      /* global argv[0] */
26  
27   int             nsamps = NSAMPLES;              /* number of samples to use */
# Line 28 | Line 31 | char           temp_octree[128];               /* temporary octree */
31   const char      UVF_PNAME[] =
32                          "ZoneProperty:UserViewFactors:bySurfaceName";
33  
34 + const char      SUBSURF_PNAME[] =
35 +                        "FenestrationSurface:Detailed";
36 +
37   const char      ADD_HEADER[] =
38                          "\n!+++ User View Factors computed by Radiance +++!\n\n";
39  
40   #define NAME_FLD        1                       /* name field always first? */
41  
42 + #define SS_BASE_FLD     4                       /* subsurface base surface */
43 + #define SS_VERT_FLD     10                      /* subsurface vertex count */
44 +
45   typedef struct {
46          const char      *pname;                 /* object type name */
47          short           zone_fld;               /* zone field index */
# Line 51 | Line 60 | typedef struct s_zone {
60          const char      *zname;                 /* zone name */
61          struct s_zone   *next;                  /* next zone in list */
62          int             nsurf;                  /* surface count */
63 +        int             ntotal;                 /* surfaces+subsurfaces */
64          IDF_OBJECT      *pfirst;                /* first matching object */
65 <        IDF_OBJECT      *plast;                 /* last matching object */
65 >        IDF_OBJECT      *plast;                 /* last before subsurfaces */
66 >        float           *area_redu;             /* subsurface area per surf. */
67   } ZONE;                 /* a list of collected zone surfaces */
68  
69   ZONE            *zone_list = NULL;      /* our list of zones */
70  
71 + LUTAB           zonesurf_lut =          /* zone surface lookup table */
72 +                        LU_SINIT(NULL,NULL);
73 +
74   IDF_LOADED      *our_idf = NULL;        /* loaded/modified IDF */
75  
76   typedef struct {
77 +        FVECT           norm;           /* surface normal */
78 +        double          area;           /* surface area */
79 +        int             nv;             /* number of vertices */
80 +        FVECT           vl[3];          /* vertex list (extends struct) */
81 + } SURFACE;              /* a polygonal surface */
82 +
83 + typedef struct {
84          FVECT           sdir[3];        /* UVW unit sampling vectors */
85          double          poff;           /* W-offset for plane of polygon */
86          double          area_left;      /* area left to sample */
# Line 78 | Line 99 | new_zone(const char *zname, IDF_OBJECT *param)
99          znew->zname = zname;                    /* assumes static */
100          znew->next = zone_list;
101          znew->pfirst = znew->plast = param;
102 <        znew->nsurf = 1;
102 >        znew->ntotal = znew->nsurf = 1;
103 >        znew->area_redu = NULL;
104          return(zone_list = znew);
105   }
106  
# Line 86 | Line 108 | new_zone(const char *zname, IDF_OBJECT *param)
108   static ZONE *
109   add2zone(IDF_OBJECT *param, const char *zname)
110   {
111 <        ZONE    *zptr;
111 >        IDF_FIELD       *nfp = idf_getfield(param, NAME_FLD);
112 >        ZONE            *zptr;
113 >        LUENT           *lep;
114  
115 +        if (nfp == NULL) {
116 +                fputs(progname, stderr);
117 +                fputs(": surface missing name field!\n", stderr);
118 +                return(NULL);
119 +        }
120          for (zptr = zone_list; zptr != NULL; zptr = zptr->next)
121                  if (!strcmp(zptr->zname, zname))
122                          break;
123 <        if (zptr == NULL)
124 <                return(new_zone(zname, param));
125 <                                                /* keep surfaces together */
123 >        if (zptr == NULL) {
124 >                zptr = new_zone(zname, param);
125 >        } else {                                /* keep surfaces together */
126 >                if (!idf_movobject(our_idf, param, zptr->plast))
127 >                        return(NULL);
128 >                zptr->plast = param;
129 >                zptr->nsurf++;
130 >                zptr->ntotal++;
131 >        }
132 >                                                /* add to lookup table */
133 >        lep = lu_find(&zonesurf_lut, nfp->val);
134 >        if (lep == NULL)
135 >                return(NULL);
136 >        if (lep->data != NULL) {
137 >                fputs(progname, stderr);
138 >                fputs(": duplicate surface name '", stderr);
139 >                fputs(nfp->val, stderr);
140 >                fputs("'\n", stderr);
141 >                return(NULL);
142 >        }
143 >        lep->key = nfp->val;
144 >        lep->data = (char *)zptr;
145 >        return(zptr);
146 > }
147 >
148 > /* Add a subsurface by finding its base surface and the corresponding zone */
149 > static ZONE *
150 > add_subsurf(IDF_OBJECT *param)
151 > {
152 >        IDF_FIELD       *bfp = idf_getfield(param, SS_BASE_FLD);
153 >        ZONE            *zptr;
154 >        LUENT           *lep;
155 >
156 >        if (bfp == NULL) {
157 >                fputs(progname, stderr);
158 >                fputs(": missing base field name in subsurface!\n", stderr);
159 >                return(NULL);
160 >        }
161 >        lep = lu_find(&zonesurf_lut, bfp->val); /* find base zone */
162 >        if (lep == NULL || lep->data == NULL) {
163 >                fputs(progname, stderr);
164 >                fputs(": cannot find referenced base surface '", stderr);
165 >                fputs(bfp->val, stderr);
166 >                fputs("'\n", stderr);
167 >                return(NULL);
168 >        }
169 >        zptr = (ZONE *)lep->data;               /* add this subsurface */
170          if (!idf_movobject(our_idf, param, zptr->plast))
171                  return(NULL);
172 <        zptr->plast = param;
100 <        zptr->nsurf++;
172 >        zptr->ntotal++;
173          return(zptr);
174   }
175  
# Line 105 | Line 177 | add2zone(IDF_OBJECT *param, const char *zname)
177   static IDF_FIELD *
178   get_vlist(IDF_OBJECT *param, const char *zname)
179   {
180 <        int             i = 0;
181 <        IDF_FIELD       *fptr;
182 <                                                /* check for surface type */
183 <        while (strcmp(surf_type[i].pname, param->pname))
184 <                if (surf_type[++i].pname == NULL)
185 <                        return(NULL);
180 > #define itm_len         (sizeof(IDF_FIELD)+6)
181 >        static char             fld_buf[4*itm_len];
182 >        static char             *next_fbp = fld_buf;
183 >        int                     i;
184 >        IDF_FIELD               *res;
185 >                                                /* check if subsurface */
186 >        if (!strcmp(param->pname, SUBSURF_PNAME)) {
187 >                if (zname != NULL) {
188 >                        LUENT   *lep = lu_find(&zonesurf_lut,
189 >                                        idf_getfield(param,SS_BASE_FLD)->val);
190 >                        if (strcmp((*(ZONE *)lep->data).zname, zname))
191 >                                return(NULL);
192 >                }
193 >                res = idf_getfield(param, SS_VERT_FLD);
194 >        } else {
195 >                i = 0;                          /* check for surface type */
196 >                while (strcmp(surf_type[i].pname, param->pname))
197 >                        if (surf_type[++i].pname == NULL)
198 >                                return(NULL);
199  
200 <        if (zname != NULL) {                    /* matches specified zone? */
201 <                fptr = idf_getfield(param, surf_type[i].zone_fld);
202 <                if (fptr == NULL || strcmp(fptr->val, zname))
200 >                if (zname != NULL) {            /* matches specified zone? */
201 >                        IDF_FIELD       *fptr = idf_getfield(param, surf_type[i].zone_fld);
202 >                        if (fptr == NULL || strcmp(fptr->val, zname))
203 >                                return(NULL);
204 >                }
205 >                res = idf_getfield(param, surf_type[i].vert_fld);
206 >        }
207 >        if (!res->val[0]) {                     /* hack for missing #vert */
208 >                IDF_FIELD       *fptr;
209 >                if (next_fbp >= fld_buf+sizeof(fld_buf))
210 >                        next_fbp = fld_buf;
211 >                i = 0;                          /* count vertices */
212 >                for (fptr = res->next; fptr != NULL; fptr = fptr->next)
213 >                        ++i;
214 >                if (i % 3)
215                          return(NULL);
216 +                fptr = res->next;
217 +                res = (IDF_FIELD *)next_fbp; next_fbp += itm_len;
218 +                res->next = fptr;
219 +                res->rem = "";
220 +                sprintf(res->val, "%d", i/3);
221          }
222 <                                                /* return field for #verts */
223 <        return(idf_getfield(param, surf_type[i].vert_fld));
222 >        return(res);
223 > #undef itm_len
224   }
225  
226 + /* Get/allocate surface polygon */
227 + static SURFACE *
228 + get_surface(IDF_FIELD *fptr)
229 + {
230 +        SURFACE *surf;
231 +        int     nv;
232 +        FVECT   e1, e2, vc;
233 +        int     i, j;
234 +                                        /* get number of vertices */
235 +        if (fptr == NULL || (nv = atoi(fptr->val)) < 3) {
236 +                fputs(progname, stderr);
237 +                fputs(": bad vertex count for surface!\n", stderr);
238 +                return(NULL);
239 +        }
240 +        surf = (SURFACE *)malloc(sizeof(SURFACE)+sizeof(FVECT)*(nv-3));
241 +        if (surf == NULL)
242 +                return(NULL);
243 +        surf->nv = nv;
244 +        for (i = nv; i--; )             /* reverse vertex order/orientation */
245 +                for (j = 0; j < 3; j++) {
246 +                        fptr = fptr->next;
247 +                        if (fptr == NULL) {
248 +                                fputs(progname, stderr);
249 +                                fputs(": missing vertex in get_surface()\n", stderr);
250 +                                free(surf);
251 +                                return(NULL);
252 +                        }
253 +                        if ((surf->vl[i][j] = atof(fptr->val)) == 0 &&
254 +                                                        !isflt(fptr->val)) {
255 +                                fputs(progname, stderr);
256 +                                fputs(": bad vertex in get_surface()\n", stderr);
257 +                                free(surf);
258 +                                return(NULL);
259 +                        }
260 +                }
261 +                                        /* compute area and normal */
262 +        surf->norm[0] = surf->norm[1] = surf->norm[2] = 0;
263 +        VSUB(e1, surf->vl[1], surf->vl[0]);
264 +        for (i = 2; i < nv; i++) {
265 +                VSUB(e2, surf->vl[i], surf->vl[0]);
266 +                fcross(vc, e1, e2);
267 +                surf->norm[0] += vc[0];
268 +                surf->norm[1] += vc[1];
269 +                surf->norm[2] += vc[2];
270 +                VCOPY(e1, e2);
271 +        }
272 +        surf->area = .5 * normalize(surf->norm);
273 +        if (surf->area == 0) {
274 +                fputs(progname, stderr);
275 +                fputs(": degenerate polygon in get_surface()\n", stderr);
276 +                free(surf);
277 +                return(NULL);
278 +        }
279 +        return(surf);
280 + }
281 +
282   /* Convert surface to Radiance with modifier based on unique name */
283   static int
284   rad_surface(IDF_OBJECT *param, FILE *ofp)
285   {
286          const char      *sname = idf_getfield(param,NAME_FLD)->val;
287          IDF_FIELD       *fptr = get_vlist(param, NULL);
288 <        int             nvert, i;
288 >        const char      **fargs;
289 >        int             nvert, i, j;
290  
291          if (fptr == NULL || (nvert = atoi(fptr->val)) < 3) {
292                  fprintf(stderr, "%s: bad surface '%s'\n", progname, sname);
293                  return(0);
294          }
295 +        fargs = (const char **)malloc(sizeof(const char *)*3*nvert);
296 +        if (fargs == NULL)
297 +                return(0);
298          fprintf(ofp, "\nvoid glow '%s'\n0\n0\n4 1 1 1 0\n", sname);
299          fprintf(ofp, "\n'%s' polygon 's_%s'\n0\n0\n%d\n", sname, sname, 3*nvert);
300 <        while (nvert--) {
301 <                for (i = 3; i--; ) {
300 >        for (j = nvert; j--; ) {                /* get arguments in reverse */
301 >                for (i = 0; i < 3; i++) {
302                          fptr = fptr->next;
303                          if (fptr == NULL || !isflt(fptr->val)) {
304                                  fprintf(stderr,
# Line 144 | Line 306 | rad_surface(IDF_OBJECT *param, FILE *ofp)
306                                                  progname, param->pname, sname);
307                                  return(0);
308                          }
309 +                        fargs[3*j + i] = fptr->val;
310 +                }
311 +        }
312 +        for (j = 0; j < nvert; j++) {           /* output reversed verts */
313 +                for (i = 0; i < 3; i++) {
314                          fputc('\t', ofp);
315 <                        fputs(fptr->val, ofp);
315 >                        fputs(fargs[3*j + i], ofp);
316                  }
317                  fputc('\n', ofp);
318          }
319 +        free(fargs);
320          return(!ferror(ofp));
321   }
322  
323 + /* Convert subsurface to Radiance with modifier based on unique name */
324 + static double
325 + rad_subsurface(IDF_OBJECT *param, FILE *ofp)
326 + {
327 +        const char      *sname = idf_getfield(param,NAME_FLD)->val;
328 +        SURFACE         *surf = get_surface(get_vlist(param, NULL));
329 +        double          area;
330 +        int             i;
331 +
332 +        if (surf == NULL) {
333 +                fprintf(stderr, "%s: bad subsurface '%s'\n", progname, sname);
334 +                return(-1.);
335 +        }
336 +        fprintf(ofp, "\nvoid glow '%s'\n0\n0\n4 1 1 1 0\n", sname);
337 +        fprintf(ofp, "\n'%s' polygon 'ss_%s'\n0\n0\n%d\n",
338 +                                                sname, sname, 3*surf->nv);
339 +        for (i = 0; i < surf->nv; i++) {        /* offset vertices */
340 +                FVECT   vert;
341 +                VSUM(vert, surf->vl[i], surf->norm, 2.*SURF_EPS);
342 +                fprintf(ofp, "\t%.12f %.12f %.12f\n", vert[0], vert[1], vert[2]);
343 +        }
344 +        area = surf->area;
345 +        free(surf);
346 +        if (ferror(ofp))
347 +                return(-1.);
348 +        return(area);
349 + }
350 +
351   /* Start rcontrib process */
352   static int
353   start_rcontrib(SUBPROC *pd, ZONE *zp)
354   {
355 < #define BASE_AC         5
355 > #define BASE_AC         6
356          static char     *base_av[BASE_AC] = {
357 <                                "rcontrib", "-fff", "-h", "-x", "1"
357 >                                "rcontrib", "-V+", "-fff", "-h", "-x", "1"
358                          };
359          char            cbuf[300];
360          char            **av;
361          FILE            *ofp;
362          IDF_OBJECT      *pptr;
363 <        int             i, n;
363 >        IDF_FIELD       *fptr;
364 >        int             i, j, n;
365                                                  /* start oconv command */
366 <        sprintf(cbuf, "oconv - > '%s'", temp_octree);
366 >        sprintf(cbuf, "oconv - > \"%s\"", temp_octree);
367          if ((ofp = popen(cbuf, "w")) == NULL) {
368                  fputs(progname, stderr);
369                  fputs(": cannot open oconv process\n", stderr);
370                  return(0);
371          }
372                                                  /* allocate argument list */
373 <        av = (char **)malloc(sizeof(char *)*(BASE_AC+4+2*zp->nsurf));
373 >        av = (char **)malloc(sizeof(char *)*(BASE_AC+4+2*(zp->ntotal)));
374          if (av == NULL)
375                  return(0);
376          for (i = 0; i < BASE_AC; i++)
377                  av[i] = base_av[i];
378          sprintf(cbuf, "%d", nsamps);
379          av[i++] = "-c";
380 <        av[i++] = cbuf;                         /* add modifier arguments */
381 <        for (n = zp->nsurf, pptr = zp->pfirst; n--; pptr = pptr->dnext) {
382 <                IDF_FIELD       *fptr = idf_getfield(pptr,NAME_FLD);
380 >        av[i++] = cbuf;                         /* add modifiers & surfaces */
381 >        for (n = 0, pptr = zp->pfirst; n < zp->nsurf; n++, pptr = pptr->dnext) {
382 >                fptr = idf_getfield(pptr,NAME_FLD);
383                  if (fptr == NULL || !fptr->val[0]) {
384                          fputs(progname, stderr);
385                          fputs(": missing name for surface object\n", stderr);
# Line 193 | Line 390 | start_rcontrib(SUBPROC *pd, ZONE *zp)
390                  av[i++] = "-m";
391                  av[i++] = fptr->val;
392          }
393 +                                                /* now subsurfaces */
394 +        if (zp->ntotal > zp->nsurf) {
395 +                if (zp->area_redu != NULL)
396 +                        memset(zp->area_redu, 0, sizeof(float)*zp->ntotal);
397 +                else if ((zp->area_redu = (float *)calloc(zp->ntotal,
398 +                                                sizeof(float))) == NULL)
399 +                        return(0);
400 +        }
401 +        for ( ; n < zp->ntotal; n++, pptr = pptr->dnext) {
402 +                double          ssarea;
403 +                const char      *bname;
404 +                IDF_OBJECT      *pptr1;
405 +                fptr = idf_getfield(pptr,NAME_FLD);
406 +                if (fptr == NULL || !fptr->val[0]) {
407 +                        fputs(progname, stderr);
408 +                        fputs(": missing name for subsurface object\n", stderr);
409 +                        return(0);
410 +                }
411 +                                                /* add subsurface to octree */
412 +                if ((ssarea = rad_subsurface(pptr, ofp)) < 0)
413 +                        return(0);
414 +                                                /* mark area for subtraction */
415 +                bname = idf_getfield(pptr,SS_BASE_FLD)->val;
416 +                for (j = 0, pptr1 = zp->pfirst;
417 +                                j < zp->nsurf; j++, pptr1 = pptr1->dnext)
418 +                        if (!strcmp(idf_getfield(pptr1,NAME_FLD)->val, bname)) {
419 +                                zp->area_redu[j] += ssarea;
420 +                                break;
421 +                        }
422 +                av[i++] = "-m";
423 +                av[i++] = fptr->val;
424 +        }
425          if (pclose(ofp) != 0) {                 /* finish oconv */
426                  fputs(progname, stderr);
427                  fputs(": error running oconv process\n", stderr);
# Line 212 | Line 441 | start_rcontrib(SUBPROC *pd, ZONE *zp)
441  
442   /* Initialize polygon sampling */
443   static Vert2_list *
444 < init_poly(POLYSAMP *ps, IDF_FIELD *f0, int nv)
444 > init_poly(POLYSAMP *ps, IDF_FIELD *f0)
445   {
446 <        IDF_FIELD       *fptr = f0;
447 <        int             i, j;
448 <        FVECT           *vl3, e1, e2, vc;
449 <        Vert2_list      *vl2 = polyAlloc(nv);
450 <
446 >        SURFACE         *surf;
447 >        Vert2_list      *vl2;
448 >        int             i;
449 >                                        /* get 3-D polygon vertices */
450 >        if ((surf = get_surface(f0)) == NULL)
451 >                return(NULL);
452 >        vl2 = polyAlloc(surf->nv);
453          if (vl2 == NULL)
454                  return(NULL);
224        vl2->p = ps;
225                                        /* get 3-D vertices */
226        vl3 = (FVECT *)malloc(sizeof(FVECT)*nv);
227        if (vl3 == NULL)
228                return(NULL);
229        for (i = nv; i--; )             /* reverse vertex ordering */
230                for (j = 0; j < 3; j++) {
231                        if (fptr == NULL) {
232                                fputs(progname, stderr);
233                                fputs(": missing vertex in init_poly()\n", stderr);
234                                return(NULL);
235                        }
236                        vl3[i][j] = atof(fptr->val);
237                        fptr = fptr->next;
238                }
239                                        /* compute area and normal */
240        ps->sdir[2][0] = ps->sdir[2][1] = ps->sdir[2][2] = 0;
241        VSUB(e1, vl3[1], vl3[0]);
242        for (i = 2; i < nv; i++) {
243                VSUB(e2, vl3[i], vl3[0]);
244                fcross(vc, e1, e2);
245                ps->sdir[2][0] += vc[0];
246                ps->sdir[2][1] += vc[1];
247                ps->sdir[2][2] += vc[2];
248                VCOPY(e1, e2);
249        }
250        ps->area_left = .5 * normalize(ps->sdir[2]);
251        if (ps->area_left == .0) {
252                fputs(progname, stderr);
253                fputs(": degenerate polygon in init_poly()\n", stderr);
254                return(0);
255        }
455                                          /* create X & Y axes */
456 <        VCOPY(ps->sdir[0], e1);
457 <        normalize(ps->sdir[0]);
456 >        VCOPY(ps->sdir[2], surf->norm);
457 >        VSUB(ps->sdir[0], surf->vl[1], surf->vl[0]);
458 >        if (normalize(ps->sdir[0]) == 0)
459 >                return(NULL);
460          fcross(ps->sdir[1], ps->sdir[2], ps->sdir[0]);
461                                          /* compute plane offset */
462 <        ps->poff = DOT(vl3[0], ps->sdir[2]);
462 >        ps->poff = DOT(surf->vl[0], ps->sdir[2]);
463                                          /* assign 2-D vertices */
464 <        for (i = 0; i < nv; i++) {
465 <                vl2->v[i].mX = DOT(vl3[i], ps->sdir[0]);
466 <                vl2->v[i].mY = DOT(vl3[i], ps->sdir[1]);
464 >        for (i = 0; i < surf->nv; i++) {
465 >                vl2->v[i].mX = DOT(surf->vl[i], ps->sdir[0]);
466 >                vl2->v[i].mY = DOT(surf->vl[i], ps->sdir[1]);
467          }
468 <        free(vl3);                      /* it's ready! */
468 >        ps->area_left = surf->area;
469 >        free(surf);                     /* it's ready! */
470 >        vl2->p = (void *)ps;
471          return(vl2);
472   }
473  
# Line 282 | Line 485 | sample_triangle(const Vert2_list *vl2, int a, int b, i
485          for (i = 3; i--; ) {
486                  orig[i] = vl2->v[a].mX*ps->sdir[0][i] +
487                                  vl2->v[a].mY*ps->sdir[1][i] +
488 <                                (ps->poff+.001)*ps->sdir[2][i];
488 >                                (ps->poff+SURF_EPS)*ps->sdir[2][i];
489                  ab[i] = (vl2->v[b].mX - vl2->v[a].mX)*ps->sdir[0][i] +
490                                  (vl2->v[b].mY - vl2->v[a].mY)*ps->sdir[1][i];
491                  ac[i] = (vl2->v[c].mX - vl2->v[a].mX)*ps->sdir[0][i] +
# Line 336 | Line 539 | sample_triangle(const Vert2_list *vl2, int a, int b, i
539   }
540  
541   /* Sample the given surface */
542 < static int
542 > static double
543   sample_surface(IDF_OBJECT *param, int wd)
544   {
342        IDF_FIELD       *fptr = get_vlist(param, NULL);
545          POLYSAMP        psamp;
546 +        double          area;
547          int             nv;
548          Vert2_list      *vlist2;
549                                          /* set up our polygon sampler */
550 <        if (fptr == NULL || (nv = atoi(fptr->val)) < 3 ||
348 <                        (vlist2 = init_poly(&psamp, fptr->next, nv)) == NULL) {
550 >        if ((vlist2 = init_poly(&psamp, get_vlist(param, NULL))) == NULL) {
551                  fprintf(stderr, "%s: bad polygon %s '%s'\n",
552                                  progname, param->pname,
553                                  idf_getfield(param,NAME_FLD)->val);
554 <                return(0);
554 >                return(-1.);
555          }
556          psamp.samp_left = nsamps;       /* assign samples & destination */
557          psamp.wd = wd;
558 +                                        /* hack for subsurface sampling */
559 +        psamp.poff += 2.*SURF_EPS * !strcmp(param->pname, SUBSURF_PNAME);
560 +
561 +        area = psamp.area_left;         /* remember starting surface area */
562                                          /* sample each subtriangle */
563          if (!polyTriangulate(vlist2, &sample_triangle))
564 <                return(0);
564 >                return(-1.);
565          polyFree(vlist2);               /* clean up and return */
566 <        return(1);
566 >        return(area);
567   }
568  
569   /* Compute User View Factors using open rcontrib process */
# Line 370 | Line 576 | compute_uvfs(SUBPROC *pd, ZONE *zp)
576          int             n, m;
577                                                  /* create output object */
578          pout = idf_newobject(our_idf, UVF_PNAME,
579 <                        "    ! computed by Radiance\n        ", zp->plast);
579 >                        "    ! computed by Radiance\n        ", our_idf->plast);
580          if (pout == NULL) {
581                  fputs(progname, stderr);
582                  fputs(": cannot create new IDF object\n", stderr);
# Line 383 | Line 589 | compute_uvfs(SUBPROC *pd, ZONE *zp)
589                  return(0);
590          }
591                                                  /* allocate read buffer */
592 <        uvfa = (float *)malloc(sizeof(float)*3*zp->nsurf);
592 >        uvfa = (float *)malloc(sizeof(float)*3*zp->ntotal);
593          if (uvfa == NULL)
594                  return(0);
595                                                  /* UVFs from each surface */
596 <        for (n = zp->nsurf, pptr = zp->pfirst; n--; pptr = pptr->dnext) {
596 >        for (n = 0, pptr = zp->pfirst; n < zp->ntotal; n++, pptr = pptr->dnext) {
597                  double  vfsum = 0;
598 +                double  adj_factor;
599                                                  /* send samples to rcontrib */
600 <                if (!sample_surface(pptr, pd->w))
600 >                if ((adj_factor = sample_surface(pptr, pd->w)) < 0)
601                          return(0);
602 +                if (zp->area_redu == NULL)
603 +                        adj_factor = 1.;
604 +                else                            /* comp. for subsurface area */
605 +                        adj_factor /= adj_factor - zp->area_redu[n];
606                                                  /* read results */
607 <                if (readbuf(pd->r, (char *)uvfa, sizeof(float)*3*zp->nsurf) !=
608 <                                sizeof(float)*3*zp->nsurf) {
607 >                if (readbuf(pd->r, (char *)uvfa, sizeof(float)*3*zp->ntotal) !=
608 >                                sizeof(float)*3*zp->ntotal) {
609                          fputs(progname, stderr);
610                          fputs(": read error from rcontrib process\n", stderr);
611                          return(0);
612                  }
613                                                  /* append UVF fields */
614                  for (m = 0, pptr1 = zp->pfirst;
615 <                                m < zp->nsurf; m++, pptr1 = pptr1->dnext) {
616 <                        vfsum += uvfa[3*m + 1];
617 <                        if (pptr1 == pptr) {
618 <                                if (uvfa[3*m + 1] > .001)
619 <                                        fprintf(stderr,
615 >                                m < zp->ntotal; m++, pptr1 = pptr1->dnext) {
616 >                        const double    uvf = uvfa[3*m + 1] * adj_factor;
617 >                        vfsum += uvf;
618 >                        if (pptr1 == pptr && uvf > .001)
619 >                                fprintf(stderr,
620                  "%s: warning - non-zero self-VF (%.1f%%) for surface '%s'\n",
621 <                                                progname, 100.*uvfa[3*m + 1],
621 >                                                progname, 100.*uvf,
622                                                  idf_getfield(pptr,NAME_FLD)->val);
623 <                                continue;       /* don't record self-factor */
413 <                        }
414 <                        sprintf(uvfbuf, "%.4f", uvfa[3*m + 1]);
623 >                        sprintf(uvfbuf, "%.4f", uvf);
624                          if (!idf_addfield(pout,
625                                          idf_getfield(pptr,NAME_FLD)->val, NULL) ||
626                                  !idf_addfield(pout,
627                                          idf_getfield(pptr1,NAME_FLD)->val, NULL) ||
628                                  !idf_addfield(pout, uvfbuf,
629 <                                                (n || m < zp->nsurf-2) ?
630 <                                                        "\n        " : "\n\n")) {
629 >                                                (n+m < 2*zp->ntotal-2) ?
630 >                                                "\n        " : "\n\n")) {
631                                  fputs(progname, stderr);
632                                  fputs(": error adding UVF fields\n", stderr);
633                                  return(0);
# Line 508 | Line 717 | main(int argc, char *argv[])
717                  do {
718                          pnext = pptr->pnext;
719                          idf_delobject(our_idf, pptr);
720 <                } while (pnext != NULL);
720 >                } while ((pptr = pnext) != NULL);
721          }
722                                                  /* add to header */
723          if (our_idf->hrem == NULL ||
# Line 529 | Line 738 | main(int argc, char *argv[])
738                          if (add2zone(pptr, fptr->val) == NULL)
739                                  return(1);
740                  }
741 +                                                /* add subsurfaces */
742 +        for (pptr = idf_getobject(our_idf, SUBSURF_PNAME);
743 +                        pptr != NULL; pptr = pptr->pnext)
744 +                if (add_subsurf(pptr) == NULL)
745 +                        return(1);
746                                                  /* run rcontrib on each zone */
747          if (!compute_zones())
748                  return(1);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines