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

Comparing ray/src/rt/ambient.c (file contents):
Revision 2.101 by schorsch, Sun Mar 6 01:13:17 2016 UTC vs.
Revision 2.109 by greg, Tue Mar 10 15:57:52 2020 UTC

# Line 12 | Line 12 | static const char      RCSid[] = "$Id$";
12   #include  "platform.h"
13   #include  "ray.h"
14   #include  "otypes.h"
15 + #include  "otspecial.h"
16   #include  "resolu.h"
17   #include  "ambient.h"
18   #include  "random.h"
# Line 76 | Line 77 | static long  lastpos = -1;             /* last flush position */
77   #define  AMBFLUSH       (BUFSIZ/AMBVALSIZ)
78  
79   #define  newambval()    (AMBVAL *)malloc(sizeof(AMBVAL))
79 #define  freeav(av)     free((void *)av);
80  
81 + #define  tfunc(lwr, x, upr)     (((x)-(lwr))/((upr)-(lwr)))
82 +
83   static void initambfile(int creat);
84   static void avsave(AMBVAL *av);
85   static AMBVAL *avstore(AMBVAL  *aval);
# Line 94 | Line 96 | static int aposcmp(const void *avp1, const void *avp2)
96   static int avlmemi(AMBVAL *avaddr);
97   static void sortambvals(int always);
98  
99 + static int      plugaleak(RAY *r, AMBVAL *ap, FVECT anorm, double ang);
100 + static double   sumambient(COLOR acol, RAY *r, FVECT rn, int al,
101 +                                AMBTREE *at, FVECT c0, double s);
102 + static int      makeambient(COLOR acol, RAY *r, FVECT rn, int al);
103 + static int      extambient(COLOR cr, AMBVAL *ap, FVECT pv, FVECT nv,
104 +                                FVECT uvw[3]);
105 +
106   #ifdef  F_SETLKW
107   static void aflock(int  typ);
108   #endif
# Line 132 | Line 141 | setambacc(                             /* set ambient accuracy */
141          newa *= (newa > 0);
142          if (fabs(newa - olda) >= .05*(newa + olda)) {
143                  ambacc = newa;
144 <                if (nambvals > 0)
144 >                if (ambacc > FTINY && nambvals > 0)
145                          sortambvals(1);         /* rebuild tree */
146          }
147   }
# Line 255 | Line 264 | ambnotify(                     /* record new modifier */
264                  }
265   }
266  
258 /************ THE FOLLOWING ROUTINES DIFFER BETWEEN NEW & OLD ***************/
267  
260 #ifndef OLDAMB
261
262 #define tfunc(lwr, x, upr)      (((x)-(lwr))/((upr)-(lwr)))
263
264 static int      plugaleak(RAY *r, AMBVAL *ap, FVECT anorm, double ang);
265 static double   sumambient(COLOR acol, RAY *r, FVECT rn, int al,
266                                AMBTREE *at, FVECT c0, double s);
267 static int      makeambient(COLOR acol, RAY *r, FVECT rn, int al);
268 static int      extambient(COLOR cr, AMBVAL *ap, FVECT pv, FVECT nv,
269                                FVECT uvw[3]);
270
268   void
269   multambient(            /* compute ambient component & multiply by coef. */
270          COLOR  aval,
# Line 277 | Line 274 | multambient(           /* compute ambient component & multiply
274   {
275          static int  rdepth = 0;                 /* ambient recursion */
276          COLOR   acol, caustic;
277 <        int     ok;
277 >        int     i, ok;
278          double  d, l;
279  
280          /* PMAP: Factor in ambient from photon map, if enabled and ray is
# Line 303 | Line 300 | multambient(           /* compute ambient component & multiply
300                  goto dumbamb;
301  
302          if (ambacc <= FTINY) {                  /* no ambient storage */
303 +                FVECT   uvd[2];
304 +                float   dgrad[2], *dgp = NULL;
305 +
306 +                if (nrm != r->ron && DOT(nrm,r->ron) < 0.9999)
307 +                        dgp = dgrad;            /* compute rotational grad. */
308                  copycolor(acol, aval);
309                  rdepth++;
310                  ok = doambient(acol, r, r->rweight,
311 <                                NULL, NULL, NULL, NULL, NULL);
311 >                                uvd, NULL, NULL, dgp, NULL);
312                  rdepth--;
313                  if (!ok)
314                          goto dumbamb;
315 +                if ((ok > 0) & (dgp != NULL)) { /* apply texture */
316 +                        FVECT   v1;
317 +                        VCROSS(v1, r->ron, nrm);
318 +                        d = 1.0;
319 +                        for (i = 3; i--; )
320 +                                d += v1[i] * (dgp[0]*uvd[0][i] + dgp[1]*uvd[1][i]);
321 +                        if (d >= 0.05)
322 +                                scalecolor(acol, d);
323 +                }
324                  copycolor(aval, acol);
325  
326                  /* PMAP: add in caustic */
# Line 402 | Line 413 | plugaleak(RAY *r, AMBVAL *ap, FVECT anorm, double ang)
413          VSUM(rtst.rdir, vdif, anorm, t[1]);     /* further dist. > plane */
414          rtst.rmax = normalize(rtst.rdir);       /* short ray test */
415          while (localhit(&rtst, &thescene)) {    /* check for occluder */
416 <                if (rtst.ro->omod != OVOID &&
416 >                OBJREC  *m = findmaterial(rtst.ro);
417 >                if (m != NULL && !istransp(m->otype) && !isBSDFproxy(m) &&
418                                  (rtst.clipset == NULL ||
419                                          !inset(rtst.clipset, rtst.ro->omod)))
420                          return(1);              /* plug light leak */
# Line 641 | Line 653 | avinsert(                              /* insert ambient value in our tree */
653   }
654  
655  
644 #else /* ! NEWAMB */
645
646 static double   sumambient(COLOR acol, RAY *r, FVECT rn, int al,
647                                AMBTREE *at, FVECT c0, double s);
648 static double   makeambient(COLOR acol, RAY *r, FVECT rn, int al);
649 static void     extambient(COLOR cr, AMBVAL *ap, FVECT pv, FVECT nv);
650
651
652 void
653 multambient(            /* compute ambient component & multiply by coef. */
654        COLOR  aval,
655        RAY  *r,
656        FVECT  nrm
657 )
658 {
659        static int  rdepth = 0;                 /* ambient recursion */
660        COLOR   acol, caustic;
661        double  d, l;
662
663        /* PMAP: Factor in ambient from global photon map (if enabled) and return
664         * as all ambient components accounted for */
665        if (ambPmap(aval, r, rdepth))
666                return;
667
668        /* PMAP: Otherwise factor in ambient from caustic photon map
669         * (ambPmapCaustic() returns zero if caustic photons disabled) and
670         * continue with RADIANCE ambient calculation */
671        copycolor(caustic, aval);
672        ambPmapCaustic(caustic, r, rdepth);
673        
674        if (ambdiv <= 0)                        /* no ambient calculation */
675                goto dumbamb;
676                                                /* check number of bounces */
677        if (rdepth >= ambounce)
678                goto dumbamb;
679                                                /* check ambient list */
680        if (ambincl != -1 && r->ro != NULL &&
681                        ambincl != inset(ambset, r->ro->omod))
682                goto dumbamb;
683
684        if (ambacc <= FTINY) {                  /* no ambient storage */
685                copycolor(acol, aval);
686                rdepth++;
687                d = doambient(acol, r, r->rweight, NULL, NULL);
688                rdepth--;
689                if (d <= FTINY)
690                        goto dumbamb;
691                copycolor(aval, acol);          
692        
693           /* PMAP: add in caustic */
694                addcolor(aval, caustic);        
695                return;
696        }
697
698        if (tracktime)                          /* sort to minimize thrashing */
699                sortambvals(0);
700                                                /* interpolate ambient value */
701        setcolor(acol, 0.0, 0.0, 0.0);
702        d = sumambient(acol, r, nrm, rdepth,
703                        &atrunk, thescene.cuorg, thescene.cusize);
704                        
705        if (d > FTINY) {
706                d = 1.0/d;
707                scalecolor(acol, d);
708                multcolor(aval, acol);
709                
710                /* PMAP: add in caustic */
711                addcolor(aval, caustic);        
712                return;
713        }
714        
715        rdepth++;                               /* need to cache new value */
716        d = makeambient(acol, r, nrm, rdepth-1);
717        rdepth--;
718        
719        if (d > FTINY) {
720                multcolor(aval, acol);          /* got new value */
721
722                /* PMAP: add in caustic */
723                addcolor(aval, caustic);                        
724                return;
725        }
726        
727 dumbamb:                                        /* return global value */
728        if ((ambvwt <= 0) | (navsum == 0)) {
729                multcolor(aval, ambval);
730
731                /* PMAP: add in caustic */
732                addcolor(aval, caustic);        
733                return;
734        }
735        
736        l = bright(ambval);                     /* average in computations */
737        if (l > FTINY) {
738                d = (log(l)*(double)ambvwt + avsum) /
739                                (double)(ambvwt + navsum);
740                d = exp(d) / l;
741                scalecolor(aval, d);
742                multcolor(aval, ambval);        /* apply color of ambval */
743        } else {
744                d = exp( avsum / (double)navsum );
745                scalecolor(aval, d);            /* neutral color */
746        }
747 }
748
749
750 static double
751 sumambient(     /* get interpolated ambient value */
752        COLOR  acol,
753        RAY  *r,
754        FVECT  rn,
755        int  al,
756        AMBTREE  *at,
757        FVECT  c0,
758        double  s
759 )
760 {
761        double  d, e1, e2, wt, wsum;
762        COLOR  ct;
763        FVECT  ck0;
764        int  i;
765        int  j;
766        AMBVAL   *av;
767
768        wsum = 0.0;
769                                        /* do this node */
770        for (av = at->alist; av != NULL; av = av->next) {
771                double  rn_dot = -2.0;
772                if (tracktime)
773                        av->latick = ambclock;
774                /*
775                 *  Ambient level test.
776                 */
777                if (av->lvl > al ||     /* list sorted, so this works */
778                                (av->lvl == al) & (av->weight < 0.9*r->rweight))
779                        break;
780                /*
781                 *  Ambient radius test.
782                 */
783                VSUB(ck0, av->pos, r->rop);
784                e1 = DOT(ck0, ck0) / (av->rad * av->rad);
785                if (e1 > ambacc*ambacc*1.21)
786                        continue;
787                /*
788                 *  Direction test using closest normal.
789                 */
790                d = DOT(av->dir, r->ron);
791                if (rn != r->ron) {
792                        rn_dot = DOT(av->dir, rn);
793                        if (rn_dot > 1.0-FTINY)
794                                rn_dot = 1.0-FTINY;
795                        if (rn_dot >= d-FTINY) {
796                                d = rn_dot;
797                                rn_dot = -2.0;
798                        }
799                }
800                e2 = (1.0 - d) * r->rweight;
801                if (e2 < 0.0)
802                        e2 = 0.0;
803                else if (e1 + e2 > ambacc*ambacc*1.21)
804                        continue;
805                /*
806                 *  Ray behind test.
807                 */
808                d = 0.0;
809                for (j = 0; j < 3; j++)
810                        d += (r->rop[j] - av->pos[j]) *
811                                        (av->dir[j] + r->ron[j]);
812                if (d*0.5 < -minarad*ambacc-.001)
813                        continue;
814                /*
815                 *  Jittering final test reduces image artifacts.
816                 */
817                e1 = sqrt(e1);
818                e2 = sqrt(e2);
819                wt = e1 + e2;
820                if (wt > ambacc*(.9+.2*urand(9015+samplendx)))
821                        continue;
822                /*
823                 *  Recompute directional error using perturbed normal
824                 */
825                if (rn_dot > 0.0) {
826                        e2 = sqrt((1.0 - rn_dot)*r->rweight);
827                        wt = e1 + e2;
828                }
829                if (wt <= 1e-3)
830                        wt = 1e3;
831                else
832                        wt = 1.0 / wt;
833                wsum += wt;
834                extambient(ct, av, r->rop, rn);
835                scalecolor(ct, wt);
836                addcolor(acol, ct);
837        }
838        if (at->kid == NULL)
839                return(wsum);
840                                        /* do children */
841        s *= 0.5;
842        for (i = 0; i < 8; i++) {
843                for (j = 0; j < 3; j++) {
844                        ck0[j] = c0[j];
845                        if (1<<j & i)
846                                ck0[j] += s;
847                        if (r->rop[j] < ck0[j] - OCTSCALE*s)
848                                break;
849                        if (r->rop[j] > ck0[j] + (1.0+OCTSCALE)*s)
850                                break;
851                }
852                if (j == 3)
853                        wsum += sumambient(acol, r, rn, al,
854                                                at->kid+i, ck0, s);
855        }
856        return(wsum);
857 }
858
859
860 static double
861 makeambient(            /* make a new ambient value for storage */
862        COLOR  acol,
863        RAY  *r,
864        FVECT  rn,
865        int  al
866 )
867 {
868        AMBVAL  amb;
869        FVECT   gp, gd;
870        int     i;
871
872        amb.weight = 1.0;                       /* compute weight */
873        for (i = al; i-- > 0; )
874                amb.weight *= AVGREFL;
875        if (r->rweight < 0.1*amb.weight)        /* heuristic override */
876                amb.weight = 1.25*r->rweight;
877        setcolor(acol, AVGREFL, AVGREFL, AVGREFL);
878                                                /* compute ambient */
879        amb.rad = doambient(acol, r, amb.weight, gp, gd);
880        if (amb.rad <= FTINY) {
881                setcolor(acol, 0.0, 0.0, 0.0);
882                return(0.0);
883        }
884        scalecolor(acol, 1./AVGREFL);           /* undo assumed reflectance */
885                                                /* store value */
886        VCOPY(amb.pos, r->rop);
887        VCOPY(amb.dir, r->ron);
888        amb.lvl = al;
889        copycolor(amb.val, acol);
890        VCOPY(amb.gpos, gp);
891        VCOPY(amb.gdir, gd);
892                                                /* insert into tree */
893        avsave(&amb);                           /* and save to file */
894        if (rn != r->ron)
895                extambient(acol, &amb, r->rop, rn);     /* texture */
896        return(amb.rad);
897 }
898
899
656   static void
901 extambient(             /* extrapolate value at pv, nv */
902        COLOR  cr,
903        AMBVAL   *ap,
904        FVECT  pv,
905        FVECT  nv
906 )
907 {
908        FVECT  v1;
909        int  i;
910        double  d;
911
912        d = 1.0;                        /* zeroeth order */
913                                        /* gradient due to translation */
914        for (i = 0; i < 3; i++)
915                d += ap->gpos[i]*(pv[i]-ap->pos[i]);
916                                        /* gradient due to rotation */
917        VCROSS(v1, ap->dir, nv);
918        d += DOT(ap->gdir, v1);
919        if (d <= 0.0) {
920                setcolor(cr, 0.0, 0.0, 0.0);
921                return;
922        }
923        copycolor(cr, ap->val);
924        scalecolor(cr, d);
925 }
926
927
928 static void
929 avinsert(                               /* insert ambient value in our tree */
930        AMBVAL *av
931 )
932 {
933        AMBTREE  *at;
934        AMBVAL  *ap;
935        AMBVAL  avh;
936        FVECT  ck0;
937        double  s;
938        int  branch;
939        int  i;
940
941        if (av->rad <= FTINY)
942                error(CONSISTENCY, "zero ambient radius in avinsert");
943        at = &atrunk;
944        VCOPY(ck0, thescene.cuorg);
945        s = thescene.cusize;
946        while (s*(OCTSCALE/2) > av->rad*ambacc) {
947                if (at->kid == NULL)
948                        if ((at->kid = newambtree()) == NULL)
949                                error(SYSTEM, "out of memory in avinsert");
950                s *= 0.5;
951                branch = 0;
952                for (i = 0; i < 3; i++)
953                        if (av->pos[i] > ck0[i] + s) {
954                                ck0[i] += s;
955                                branch |= 1 << i;
956                        }
957                at = at->kid + branch;
958        }
959        avh.next = at->alist;           /* order by increasing level */
960        for (ap = &avh; ap->next != NULL; ap = ap->next)
961                if ( ap->next->lvl > av->lvl ||
962                                (ap->next->lvl == av->lvl) &
963                                (ap->next->weight <= av->weight) )
964                        break;
965        av->next = ap->next;
966        ap->next = (AMBVAL*)av;
967        at->alist = avh.next;
968 }
969
970 #endif  /* ! NEWAMB */
971
972 /************* FOLLOWING ROUTINES SAME FOR NEW & OLD METHODS ***************/
973
974 static void
657   initambfile(            /* initialize ambient file */
658          int  cre8
659   )
# Line 1072 | Line 754 | newambtree(void)                               /* allocate 8 ambient tree structs
754          }
755          atp = atfreelist;
756          atfreelist = atp->kid;
757 <        memset((char *)atp, '\0', 8*sizeof(AMBTREE));
757 >        memset(atp, 0, 8*sizeof(AMBTREE));
758          return(atp);
759   }
760  
# Line 1098 | Line 780 | unloadatree(                   /* unload an ambient value tree */
780                                          /* transfer values at this node */
781          for (av = at->alist; av != NULL; av = at->alist) {
782                  at->alist = av->next;
783 +                av->next = NULL;
784                  (*f)(av);
785          }
786          if (at->kid == NULL)
# Line 1175 | Line 858 | avlmemi(                               /* find list position from address */
858   {
859          AMBVAL  **avlpp;
860  
861 <        avlpp = (AMBVAL **)bsearch((char *)&avaddr, (char *)avlist2,
862 <                        nambvals, sizeof(AMBVAL *), &aposcmp);
861 >        avlpp = (AMBVAL **)bsearch(&avaddr, avlist2,
862 >                        nambvals, sizeof(AMBVAL *), aposcmp);
863          if (avlpp == NULL)
864                  error(CONSISTENCY, "address not found in avlmemi");
865          return(avlpp - avlist2);
# Line 1219 | Line 902 | sortambvals(                   /* resort ambient values */
902          }
903          if (avlist1 == NULL) {          /* no time tracking -- rebuild tree? */
904                  if (avlist2 != NULL)
905 <                        free((void *)avlist2);
905 >                        free(avlist2);
906                  if (always) {           /* rebuild without sorting */
907                          oldatrunk = atrunk;
908                          atrunk.alist = NULL;
# Line 1246 | Line 929 | sortambvals(                   /* resort ambient values */
929                  if (i_avlist < nambvals)
930                          error(CONSISTENCY, "missing ambient values in sortambvals");
931   #endif
932 <                qsort((char *)avlist1, nambvals, sizeof(struct avl), alatcmp);
933 <                qsort((char *)avlist2, nambvals, sizeof(AMBVAL *), aposcmp);
932 >                qsort(avlist1, nambvals, sizeof(struct avl), alatcmp);
933 >                qsort(avlist2, nambvals, sizeof(AMBVAL *), aposcmp);
934                  for (i = 0; i < nambvals; i++) {
935                          if (avlist1[i].p == NULL)
936                                  continue;
# Line 1263 | Line 946 | sortambvals(                   /* resort ambient values */
946                          avinsert(avlist2[j]);
947                          avlist1[j].p = NULL;
948                  }
949 <                free((void *)avlist1);
950 <                free((void *)avlist2);
949 >                free(avlist1);
950 >                free(avlist2);
951                                                  /* compute new sort interval */
952                  sortintvl = ambclock - lastsort;
953                  if (sortintvl >= MAX_SORT_INTVL/2)
# Line 1292 | Line 975 | aflock(                        /* lock/unlock ambient file */
975  
976          if (typ == fls.l_type)          /* already called? */
977                  return;
978 +
979          fls.l_type = typ;
980 <        if (fcntl(fileno(ambfp), F_SETLKW, &fls) < 0)
981 <                error(SYSTEM, "cannot (un)lock ambient file");
980 >        do
981 >                if (fcntl(fileno(ambfp), F_SETLKW, &fls) != -1)
982 >                        return;
983 >        while (errno == EINTR);
984 >        
985 >        error(SYSTEM, "cannot (un)lock ambient file");
986   }
987  
988  
# Line 1313 | Line 1001 | ambsync(void)                  /* synchronize ambient file */
1001          if ((flen = lseek(fileno(ambfp), (off_t)0, SEEK_END)) < 0)
1002                  goto seekerr;
1003          if ((n = flen - lastpos) > 0) {         /* file has grown */
1004 <                if (ambinp == NULL) {           /* use duplicate filedes */
1005 <                        ambinp = fdopen(dup(fileno(ambfp)), "r");
1004 >                if (ambinp == NULL) {           /* get new file pointer */
1005 >                        ambinp = fopen(ambfile, "rb");
1006                          if (ambinp == NULL)
1007 <                                error(SYSTEM, "fdopen failed in ambsync");
1007 >                                error(SYSTEM, "fopen failed in ambsync");
1008                  }
1009                  if (fseek(ambinp, lastpos, SEEK_SET) < 0)
1010                          goto seekerr;
# Line 1331 | Line 1019 | ambsync(void)                  /* synchronize ambient file */
1019                          avstore(&avs);
1020                          n -= AMBVALSIZ;
1021                  }
1022 <                lastpos = flen - n;
1023 <                /*** seek always as safety measure
1024 <                if (n) ***/                     /* alignment */
1337 <                        if (lseek(fileno(ambfp), (off_t)lastpos, SEEK_SET) < 0)
1338 <                                goto seekerr;
1022 >                lastpos = flen - n;             /* check alignment */
1023 >                if (n && lseek(fileno(ambfp), (off_t)lastpos, SEEK_SET) < 0)
1024 >                        goto seekerr;
1025          }
1026          n = fflush(ambfp);                      /* calls write() at last */
1027 <        if (n != EOF)
1342 <                lastpos += (long)nunflshed*AMBVALSIZ;
1343 <        else if ((lastpos = lseek(fileno(ambfp), (off_t)0, SEEK_CUR)) < 0)
1344 <                goto seekerr;
1345 <                
1027 >        lastpos += (long)nunflshed*AMBVALSIZ;
1028          aflock(F_UNLCK);                        /* release file */
1029          nunflshed = 0;
1030          return(n);
1031   seekerr:
1032          error(SYSTEM, "seek failed in ambsync");
1033 <        return -1; /* pro forma return */
1033 >        return(EOF);    /* pro forma return */
1034   }
1035  
1036   #else   /* ! F_SETLKW */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines