Ejemplo n.º 1
0
        public void QueryMap(ref Point pos, float maxDist, int nphotons, out Photon[] result) {
            var np = new NearestPhotons();
            np.dist = new float[nphotons + 1];
            np.index = Enumerable.Repeat(new Photon() {Plane = 255}, nphotons + 1).ToArray();
            np.pos = pos;
            np.max = nphotons;
            np.gotHeap = 0;
            np.found = 0;

            np.dist[0] = maxDist * maxDist;
            this.LocatePhotons(ref np, 1);
            result = np.found > 0 ? np.index.Skip(1).ToArray() : new Photon[0];
        }
Ejemplo n.º 2
0
        private void LocatePhotons(ref NearestPhotons np, int p)
        {
            Vector hh = (np.pos - bbox.Min) / cellSize;
            int ix = Math.Abs((int)(hh.x));
            int iy = Math.Abs((int)(hh.y));
            int iz = Math.Abs((int)(hh.z));
            var hash = Hash(ix, iy, iz, photonsCount);
            var photon = photons[hash];
            np.found = 1;

  

            if (IsNeighbour(ref np.pos, ref photon.Position, np.dist[0]))
            {
                photon.SetPower(photon.Power*counts[hash]); 
            }

            np.index[0] = photon;
        }
Ejemplo n.º 3
0
        public Vector IrradianceEstimate(ref Point pos, ref Normal n, float max_dist, int nphotons) {
            var normal = n;
            var irrad = new RgbSpectrum(0.0f);
            var np = new NearestPhotons();
            np.dist = new float[nphotons + 1];
            np.index = new Photon[nphotons + 1];
            np.pos = pos;
            np.max = nphotons;
            np.gotHeap = 0;
            np.found = 0;

            np.dist[0] = max_dist * max_dist;

            this.LocatePhotons(ref np, 1);

            if (np.found < 1)
                return new Vector(0);

            for (int index = 0; index < np.index.Length; index++) {
                Vector pdir = 
                    PhotonDirection(ref np.index[index]);
                var dot = Vector.Dot(ref pdir, ref normal);

                 if (dot > 0.0f) 
                {
                    irrad = irrad + np.index[index].Power; //*BRDF
                }
            }

            var tmp = (1.0f / MathLab.M_PI) / np.dist[0];

            irrad = irrad * tmp;

            return irrad;

        }
Ejemplo n.º 4
0
        private void LocatePhotonsTemp(ref NearestPhotons np, int index) {
            Photon p = this.photons[index];
            float dist1;

            if (index < this.Half) {
                dist1 = np.pos[p.Plane] - p.Position[p.Plane];

                if (dist1 > 0.0d) {
                    this.LocatePhotons(ref np, 2 * index + 1);

                    if ((dist1 * dist1) < np.dist[0])
                        this.LocatePhotons(ref np, 2 * index);
                }
                else {
                    this.LocatePhotons(ref np, 2 * index);
                    if ((dist1 * dist1) < np.dist[0])
                        this.LocatePhotons(ref np, 2 * index + 1);
                }
            }

            dist1 = (p.Position - (Vector)np.pos).Length;
            float dist2 = dist1 * dist1;

            if (dist2 < np.dist[0]) {
                //Photon found
                if (np.found < np.max) {
                    np.found++;
                    np.dist[np.found] = dist2;
                    np.index[np.found] = p;
                }
                else {
                    int j, parent;

                    if (np.gotHeap == 0) {
                        double dst2;
                        Photon phot;
                        int hFound = np.found >> 1;
                        for (int k = hFound; k >= 1; k--) {
                            parent = k;
                            phot = np.index[k];
                            dst2 = np.dist[k];

                            while (parent <= hFound) {
                                j = parent + parent;
                                if (j < np.found && np.dist[j] < np.dist[j + 1]) {
                                    j++;
                                }
                                if (dst2 >= np.dist[j])
                                    break;
                                np.dist[parent] = np.dist[j];
                                np.index[parent] = phot;
                                parent = j;
                            }
                        }
                        np.gotHeap = 1;
                    }

                    parent = 1;
                    j = 2;
                    while (j <= np.found) {
                        if (j < np.found && np.dist[j] < np.dist[j + 1])
                            j++;
                        if (dist2 > np.dist[j])
                            break;
                        np.dist[parent] = np.dist[j];
                        np.index[parent] = np.index[j];
                        parent = j;
                        j += j;
                    }
                    np.index[parent] = p;
                    np.dist[parent] = dist2;

                    np.dist[0] = np.dist[1];
                }
            }
        }
Ejemplo n.º 5
0
        private void LocatePhotons(ref NearestPhotons np, int index) {
            float dist1;

            if (index < this.Half) {
                dist1 = np.pos[this.photons[index].Plane] - this.photons[index].Position[this.photons[index].Plane];

                if (dist1 > 0.0d) {
                    this.LocatePhotons(ref np, 2 * index + 1);

                    if ((dist1 * dist1) < np.dist[0])
                        this.LocatePhotons(ref np, 2 * index);
                }
                else {
                    this.LocatePhotons(ref np, 2 * index);
                    if ((dist1 * dist1) < np.dist[0])
                        this.LocatePhotons(ref np, 2 * index + 1);
                }
            }

            dist1 = Geometry.EuclidianDistance(ref this.photons[index].Position, ref np.pos);
            /*
                (new Vector( this.photons[index].Position.x - np.pos.x, 
                                 this.photons[index].Position.y - np.pos.y,
                                 this.photons[index].Position.z - np.pos.z)).Length;
            */
            float dist2 = dist1 * dist1;

            if (dist2 < np.dist[0]) {
                //Photon found
                if (np.found < np.max) {
                    np.found++;
                    np.dist[np.found] = dist2;
                    np.index[np.found] = this.photons[index];
                }
                else {
                    int j, parent;

                    if (np.gotHeap == 0) {
                        double dst2;
                        Photon phot;
                        int hFound = np.found >> 1;
                        for (int k = hFound; k >= 1; k--) {
                            parent = k;
                            phot = np.index[k];
                            dst2 = np.dist[k];

                            while (parent <= hFound) {
                                j = parent + parent;
                                if (j < np.found && np.dist[j] < np.dist[j + 1]) {
                                    j++;
                                }
                                if (dst2 >= np.dist[j])
                                    break;
                                np.dist[parent] = np.dist[j];
                                np.index[parent] = phot;
                                parent = j;
                            }
                        }
                        np.gotHeap = 1;
                    }

                    parent = 1;
                    j = 2;
                    while (j <= np.found) {
                        if (j < np.found && np.dist[j] < np.dist[j + 1])
                            j++;
                        if (dist2 > np.dist[j])
                            break;
                        np.dist[parent] = np.dist[j];
                        np.index[parent] = np.index[j];
                        parent = j;
                        j += j;
                    }
                    np.index[parent] = this.photons[index];
                    np.dist[parent] = dist2;

                    np.dist[0] = np.dist[1];
                }
            }
        }