Fast photons-in-range search

Let's look at photons-in-range search function.
float GetVal(vector3d P, vector3d N, float r, int side, PrimPtr obj)
{
 float Val = 0.0f, r2=r*r, dst;
 vector3d pp;

 HtPPtr p=Hits; //pointer to hit buffer
 while (p) {
   if (p->obj->viss > (obj? -1 : 0)) // object visible ?
     if (p->side == side) // point on right side of object ?
      {
        pp=P-p->pos;dst=pp.x*pp.x + pp.y*pp.y + pp.z*pp.z;
        if (dst<= r2) // point inside sampling-area?
          Val += (p->val);
      }
   p=p->next;
 }
 Val/=d;
 return Val;
}

How we can optimize?
Research!
dst=pp.x*pp.x + pp.y*pp.y + pp.z*pp.z;
If pp.x==pp.y==pp.z=n then dst=n*n + n*n + n*n=3*n*n
3*n*n=r*r
n*n=(r*r)/3
n=sqrt((r*r)/3)=r/sqrt(3) - minimal value, which can correspond to r*r
If pp.x!=pp.y!=pp.z then one of them bigger then another two.
Max value with bigger coordinate is r.
Conclusion: required coordinates in [r/sqrt(3)..r]

Look at the code.
float sqrt3=1/sqrt(3);
float GetVal(vector3d P, vector3d N, float r, int side, PrimPtr obj)
{
 float Val = 0.0f, r2=r*r, dst, r3=r*sqrt3;
 vector3d pp;
 int ok; //flag

 HtPPtr p=Hits; //pointer to hit buffer
 while (p) {
   if (p->obj->viss > (obj? -1 : 0)) // object visible ?
     if (p->side == side) // point on right side of object ?
      {
        pp=P-p->pos; pp.x=fabs(pp.x); pp.y=fabs(pp.y); pp.z=fabs(pp.z);
        if (pp.x > r || pp.y > r || pp.x > r) { p=p->next; continue; } // bigger - we dont need it
        if (pp.x > r3 || pp.y > r3 || pp.x > r3)
        {
          dst=pp.x*pp.x + pp.y*pp.y + pp.z*pp.z;
          ok=(dst <= r2);
        }
        else ok=1; // because we need coord<= r
        if (ok) // point inside sampling-area?
          Val += (p->val);
      }
   p=p->next;
 }
 Val/=d;
 return Val;
}

Works 2-3 times faster!
Fine ;)
---------------------------------------
Be optimized!
tigra
mail me
icq 429048706
Hosted by uCoz