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.21 by greg, Mon Jan 20 17:53:40 2020 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines