Manta Interactive Ray Tracer Development Mailing List

Text archives Help


[MANTA] r530 - in branches/AFR: . Engine/ImageTraversers Engine/ImageTraversers/AFR Model/Cameras


Chronological Thread 
  • From: abhinav@sci.utah.edu
  • To: manta@sci.utah.edu
  • Subject: [MANTA] r530 - in branches/AFR: . Engine/ImageTraversers Engine/ImageTraversers/AFR Model/Cameras
  • Date: Mon, 5 Sep 2005 10:30:09 -0600 (MDT)

Author: abhinav
Date: Mon Sep  5 10:30:07 2005
New Revision: 530

Added:
   branches/AFR/combinestreams.cpp
   branches/AFR/streamtest.cpp
Modified:
   branches/AFR/Engine/ImageTraversers/AFImageTraverser.cc
   branches/AFR/Engine/ImageTraversers/AFImageTraverser.h
   branches/AFR/Engine/ImageTraversers/AFR/kdtree.cc
   branches/AFR/Engine/ImageTraversers/AFR/sample.h
   branches/AFR/Engine/ImageTraversers/AFR/tiles.cc
   branches/AFR/Engine/ImageTraversers/AFR/tiles.h
   branches/AFR/Model/Cameras/PinholeCamera.cc
Log:
fixed streams
grouped 6 xhairs in one raypacket, gives slight performance boost
modified spatial coherence criterion a bit. gives some performance 
improvement but not too much. 

added code to test stream (streamtest.cpp) and combine more than one streams 
into one (combinestreams.cpp), these are not part of Manta and should be 
compiled separately. These are desighed for linux, for max you need to 
replace GL/glut.h with GLUT/glut.h
syntax:
streamtest filename
combinestreams filenameprefix number_of_files
latter will output to finalfilenameprefix.tfr


A    combinestreams.cpp
M    Model/Cameras/PinholeCamera.cc
M    Engine/ImageTraversers/AFR/kdtree.cc
M    Engine/ImageTraversers/AFR/tiles.cc
M    Engine/ImageTraversers/AFR/tiles.h
M    Engine/ImageTraversers/AFR/sample.h
M    Engine/ImageTraversers/AFImageTraverser.cc
M    Engine/ImageTraversers/AFImageTraverser.h
A    streamtest.cpp


Modified: branches/AFR/Engine/ImageTraversers/AFImageTraverser.cc
==============================================================================
--- branches/AFR/Engine/ImageTraversers/AFImageTraverser.cc     (original)
+++ branches/AFR/Engine/ImageTraversers/AFImageTraverser.cc     Mon Sep  5 
10:30:07 2005
@@ -322,12 +322,11 @@
                {
                  sprintf(fname,"%s%03d",streamfilename.c_str(),i);
                  streamfile[i] = fopen(fname,"wb");
-                 fprintf(streamfile[i], "Version 3.0\n");
+                 fprintf(streamfile[i], "Version 1.3\n");
                  fprintf(streamfile[i],"Created on: %s", ctime(&t));
                  // print the width and height
                  fprintf(streamfile[i],"width %d\n", xres);
                  fprintf(streamfile[i],"height %d\n", yres);
-                 fprintf(streamfile[i],"depth INF\n");
                  // print the simulated frame rate
                  fprintf(streamfile[i],"frame_rate %d\n",DESIRED_FRAME_RATE);
                  fprintf(streamfile[i],"END\n");
@@ -440,19 +439,26 @@
   temporalQ = new CQ<Sample>[num_clients];
   newSample = new Sample* [num_clients];
   newSampleSet = new SampleSet* [num_clients];
+  // find raypacket size and divide it by 5 to get xhairBlockSize
+  xhairBlockSize = RayPacket::MaxSize/5;
+  cout << "xhairBlockSize = " << xhairBlockSize << endl;
+  xhairBlockStart = new int [num_clients];
+  color = new Color* [num_clients];
   // Initialize 
   for(i = 0; i<num_clients; i++)
   {
     temporalQ[i].init(qSize+1);
-       // these are really overdimensioned I believe.
-       newSample[i] = new Sample [chunk_size];
-       newSampleSet[i] = new SampleSet [chunk_size];
+         // these are really overdimensioned I believe.
+         newSample[i] = new Sample [chunk_size];
+    color[i] = new Color [xhairBlockSize*5];
+         newSampleSet[i] = new SampleSet [chunk_size];
+    xhairBlockStart[i] = 0;
   }
   if(outputStreams)
   {
     firsttime = new int[num_clients];
-       for(i=0; i<num_clients; i++)
-         firsttime[i] = 1;
+         for(i=0; i<num_clients; i++)
+           firsttime[i] = 1;
     numsamples_pos = (fpos_t*)malloc(num_clients*sizeof(fpos_t));
     streamfile = (FILE**)malloc(num_clients*sizeof(FILE*));
     initStreams(xres, yres);   
@@ -460,29 +466,24 @@
 }
 
 void AFImageTraverser::renderCrossHair(const RenderContext& context, int 
myID, 
-                                       Image *image, int xres, int yres, 
Sample *s, int sscount)
+                                       Image *image, const int xres, const 
int yres, Sample *s, const int sscount, bool endChunk)
 {
-
-  float sx, sy;
-  int size = 5;
+ //cout << "now rendering xhair" << endl;
+  newSampleSet[myID][sscount].set(TEMPORAL_SAMPLE, s->viewCoord[0], 
s->viewCoord[1], s->t, 
+         s->worldCoord[0], s->worldCoord[1], s->worldCoord[2],
+         s->c[0], s->c[1], s->c[2]);
+  newSampleSet[myID][sscount].tileindex = 
kdtree[context.proc].getTileforXY(s->viewCoord[0], s->viewCoord[1]);
+  //cout << "checking ...sscount = " << sscount << ", blockstart = " << 
xhairBlockStart[myID] << endl;
+  
+  if(!endChunk && (sscount - xhairBlockStart[myID])<xhairBlockSize) return;
+  int size = (sscount - xhairBlockStart[myID])*5;  
+  float t = (float)chunkTimeStamp[context.proc];
   int flags = RayPacket::HaveImageCoordinates | RayPacket::ConstantEye;
-  sx = s->viewCoord[0];
-  sy = s->viewCoord[1];
-  //cout << "now rendering xhair" << endl;
   int depth = 0;
   RayPacketData raydata;
   RayPacket rays(raydata, size, depth, flags);
-  double cx, cy;
-  int isx, isy;
-  isx = (int)sx;
-  isy = (int)sy;
-  newSampleSet[myID][sscount].set(TEMPORAL_SAMPLE, sx, sy, s->t, 
-         s->worldCoord[0], s->worldCoord[1], s->worldCoord[2],
-         s->c[0], s->c[1], s->c[2]);
-  newSampleSet[myID][sscount].tileindex = 
kdtree[context.proc].getTileforXY(sx, sy);
-  float t = (float)chunkTimeStamp[context.proc];
-  Color color[5];
-
+  int i, j;
+  int start = xhairBlockStart[myID]+1;
   const static double offset[5][2] = {
     { 0.0, 0.0},
     {-1.0, 0.0},
@@ -490,29 +491,24 @@
     { 0.0,-1.0},
     { 0.0, 1.0}
   };
-  
-  for(int i=0;i<size;i++) {
-
-    double px, py;
-    
-    cx = sx+offset[i][0];
-    cy = sy+offset[i][1];
-
-    if(xres > yres) // let the smaller dimension be mapped to [-1,1]
-    {
-      px = (double)(-1.0 + 2.0*cx/(double)yres); 
-      py = (double)(-1.0 + 2.0*cy/(double)yres);
+  for(j=start; j<=sscount; j++) {
+    for(i=0; i<5; i++) {
+      double px, py;
+      if(xres > yres) // let the smaller dimension be mapped to [-1,1]
+      {
+        px = (double)(-1.0 + 
2.0*(newSampleSet[myID][j].sample[TEMPORAL_SAMPLE].viewCoord[0] + 
offset[i][0])/(double)yres); 
+        py = (double)(-1.0 + 
2.0*(newSampleSet[myID][j].sample[TEMPORAL_SAMPLE].viewCoord[1] + 
offset[i][1])/(double)yres);
+      }
+      else
+      {
+        px = (double)(-1.0 + 
2.0*(newSampleSet[myID][j].sample[TEMPORAL_SAMPLE].viewCoord[0] + 
offset[i][0])/(double)xres); 
+        py = (double)(-1.0 + 
2.0*(newSampleSet[myID][j].sample[TEMPORAL_SAMPLE].viewCoord[1] + 
offset[i][1])/(double)xres);
+      }  
+      
+      // Specify the pixel.
+      rays.setPixel((j-start)*5+i, 0, px, py, &color[myID][(j-start)*5+i]);
     }
-    else
-    {
-      px = (double)(-1.0 + 2.0*cx/(double)xres); 
-      py = (double)(-1.0 + 2.0*cy/(double)xres);
-    }  
-    
-    // Specify the pixel.
-    rays.setPixel(i, 0, px, py, &color[i]);
   }
-  
   
/////////////////////////////////////////////////////////////////////////////
   // Trace the rays.  The results will automatically go into the fragment  
   context.renderer->traceEyeRays(context, rays);
@@ -520,25 +516,29 @@
   
   
/////////////////////////////////////////////////////////////////////////////
   // okay now copy from fragment to temporalQ
-  for(int i=0;i<size;i++) {
-    RayPacket::Element& re = rays.get(i);
-
-    RGBColor tempcol = color[i].convertRGB();
-    Point p = re.hitPosition;
-    
-    newSampleSet[myID][sscount].set( i, sx, sy, t, p.x(), p.y(), 
p.z(),tempcol.r(), tempcol.g(), tempcol.b() ); 
-    
-    image->set( (int)(sx+offset[i][0]), (int)(sy+offset[i][1]), color[i] );
-  }
-  
-  //cout << "computing gradients" << endl;
-  newSampleSet[myID][sscount].computeGradients(t);
+  for(j=start; j<=sscount; j++) {
+    for(i=0; i<5; i++) {
+      RayPacket::Element& re = rays.get((j-start)*5+i);
+  
+      RGBColor tempcol = color[myID][(j-start)*5+i].convertRGB();
+      Point p = re.hitPosition;
+      float nsx, nsy;
+      nsx = newSampleSet[myID][j].sample[TEMPORAL_SAMPLE].viewCoord[0] + 
offset[i][0];
+      nsy = newSampleSet[myID][j].sample[TEMPORAL_SAMPLE].viewCoord[1] + 
offset[i][1];
   
-  // add the sample to the corresponding kdtree
-  kdtree[context.proc].updateStatsAddSampleSet(&newSampleSet[myID][sscount], 
(float)chunkTimeStamp[context.proc], samplingrate);
+      newSampleSet[myID][j].set( i, nsx, nsy, t, p.x(), p.y(), 
p.z(),tempcol.r(), tempcol.g(), tempcol.b() ); 
+      
+      image->set( (int)(nsx), (int)(nsy), color[myID][(j-start)*5+i] );
+    }
+    newSampleSet[myID][j].computeGradients(t);
+    // add the sample to the corresponding kdtree
+    kdtree[context.proc].updateStatsAddSampleSet(&newSampleSet[myID][j], 
(float)chunkTimeStamp[context.proc], samplingrate);
   
-  // samples_done[context.proc] += 5;
-  samples_done[context.proc].addFive();
+    // samples_done[context.proc] += 5;
+    samples_done[context.proc].addFive();
+  }
+  //cout << "done one xhairBlock" << endl;
+  xhairBlockStart[myID] = sscount;
 }
 
 
@@ -561,7 +561,7 @@
   {
     fsize = (chunk_size>(qSize-temporalQ[myID].getN()))? chunk_size : 
qSize-temporalQ[myID].getN();
     if((fsize + temporalQ[myID].getN()) > temporalQ[myID].getMaxSize())
-       fsize = temporalQ[myID].getMaxSize() - temporalQ[myID].getN() -1;
+           fsize = temporalQ[myID].getMaxSize() - temporalQ[myID].getN() -1;
     //cout << "fsize = " << fsize << endl;
   }
   Color color[RayPacket::MaxSize];
@@ -569,6 +569,15 @@
   //cout << "fsize = " << fsize << ", myID = " << myID << endl;
   
/////////////////////////////////////////////////////////////////////////////
   // Create ray packets.
+  
+  const static int offset[5][2] = {
+    { 0, 0},
+    {-3, 0},
+    { 3, 0},
+    { 0,-3},
+    { 0, 3}
+  };
+  
   for(f=0;f<fsize;f+=RayPacket::MaxSize) {
     int size = RayPacket::MaxSize;
     if(size >= fsize-f)
@@ -579,22 +588,46 @@
     RayPacketData raydata;
     RayPacket rays(raydata, size, depth, flags);
     
-    int cx, cy;
+    int cx, cy, ox, oy;
     //int tindex = kdtree[myID].getRandomSample(cx, cy, 
myRandomNumber[myID]);
     int tindex = kdtree[myID].getPseudoRandomTile();
     int coherenceCount=0;
     // Copy samples from the sampleQ into the fragment.
     for(i=0;i<size;i++) {
-      kdtree[myID].getRandomSamplefromTile(tindex, cx, cy, 
myRandomNumber[myID]);
-      if(cx<0) cx=0; if(cx>=xres) cx = xres-1;
-      if(cy<0) cy=0; if(cy>=yres) cy = yres-1;
+      if(spatialCoherence) {
+        if(coherenceCount==0)
+        {
+          kdtree[myID].getRandomSamplefromTile(tindex, cx, cy, 
myRandomNumber[myID]);
+          ox = cx; oy = cy;
+        }
+        else
+        {
+          cx += offset[coherenceCount-1][0];
+          cy += offset[coherenceCount-1][1];
+          if(cx<0 || cx>=xres || cy<0 || cy>=yres)
+            kdtree[myID].getRandomSamplefromTile(tindex, cx, cy, 
myRandomNumber[myID]);
+        }
+      }
+      else
+        kdtree[myID].getRandomSamplefromTile(tindex, cx, cy, 
myRandomNumber[myID]);
      
+      //if(cx<0) cx=0; if(cx>=xres) cx = xres-1;
+      //if(cy<0) cy=0; if(cy>=yres) cy = yres-1;
       // cout << "f+i = " << f+i << endl;
       double jitterx, jittery;
-      jitterx = myRandomNumber[myID].genfrand();
-      jittery = myRandomNumber[myID].genfrand();
+      jitterx = myRandomNumber[myID].genfrand()-0.5;
+      jittery = myRandomNumber[myID].genfrand()-0.5;
       newSample[myID][f+i].viewCoord[0] = (float)cx+jitterx;
       newSample[myID][f+i].viewCoord[1] = (float)cy+jittery;
+      
+      if(newSample[myID][f+i].viewCoord[0]<0) 
+        newSample[myID][f+i].viewCoord[0]=0; 
+      if(newSample[myID][f+i].viewCoord[0]>=xres) 
+        newSample[myID][f+i].viewCoord[0]= xres-1;
+      if(newSample[myID][f+i].viewCoord[1]<0) 
+        newSample[myID][f+i].viewCoord[1]=0; 
+      if(newSample[myID][f+i].viewCoord[1]>=yres) 
+        newSample[myID][f+i].viewCoord[1]= yres-1;
       // cout << "raytracing: " << cx << ", " << cy << endl;
       newSample[myID][f+i].tileindex = tindex;
       newSample[myID][f+i].t = t;
@@ -618,10 +651,10 @@
       // Specify the position and color pointer for the packet element.
       rays.setPixel(i, 0, px, py, &color[i]);
       coherenceCount++;
-      if(!spatialCoherence || 
coherenceCount>=kdtree[myID].getTileSize(tindex)/10)
+      if(!spatialCoherence || 
coherenceCount>=6)//kdtree[myID].getTileSize(tindex)/10)
       {
         tindex = kdtree[myID].getPseudoRandomTile();
-       coherenceCount=0;
+             coherenceCount=0;
       }
     }
     
@@ -722,7 +755,8 @@
 
     // initialization is done so we can reproject one chunk worth
     int i;
-       int sscount=0;
+         int sscount=0;
+    xhairBlockStart[myID] = 0;
     Sample *sp;
     //cout << "now making xhairs" << endl;
        
@@ -755,14 +789,19 @@
           sp->viewCoord[0] = rp.x()*xres;
           sp->viewCoord[1] = rp.y()*yres;
           sp->viewCoord[2] = rp.z();
+          if(sp->viewCoord[0]<xres && sp->viewCoord[0]>=0 
+               && sp->viewCoord[1]<yres && sp->viewCoord[1]>=0
+               && sp->viewCoord[2]<=1.0)
+          {
+            renderCrossHair(context, myID, image, xres, yres, sp, sscount, 
(i==(chunk_size-1)));
+            sscount++;
+          }
+        }
+        else
+        {
+          renderCrossHair(context, myID, image, xres, yres, sp, sscount, 
(i==(chunk_size-1)));
+          sscount++;
         }
-        if(sp->viewCoord[0]<xres && sp->viewCoord[0]>=0 
-             && sp->viewCoord[1]<yres && sp->viewCoord[1]>=0
-             && sp->viewCoord[2]<=1.0)
-           {
-          renderCrossHair(context, myID, image, xres, yres, sp, sscount);
-                 sscount++;
-           }
       }
       else break;
     }

Modified: branches/AFR/Engine/ImageTraversers/AFImageTraverser.h
==============================================================================
--- branches/AFR/Engine/ImageTraversers/AFImageTraverser.h      (original)
+++ branches/AFR/Engine/ImageTraversers/AFImageTraverser.h      Mon Sep  5 
10:30:07 2005
@@ -45,7 +45,7 @@
                        virtual void renderImage(const RenderContext& 
context, Image* image) { /*Undefined for AFRPipeline. */ };
                        
                        void renderChunk(const RenderContext& context, Image* 
image, int xres, int yres);
-                       void renderCrossHair(const RenderContext& context, 
int myID, Image *image, int xres, int yres, Sample *s, int sscount);
+                       void renderCrossHair(const RenderContext& context, 
int myID, Image *image, const int xres, const int yres, Sample *s, const int 
sscount, bool endChunk=false);
                        
                        // Master thread task.
                        void masterTask(const RenderContext& context, Image* 
image);
@@ -80,47 +80,50 @@
                        }
                        static ImageTraverser* create(const vector<string>& 
args);
                        
-               private:
-                       AFImageTraverser(const AFImageTraverser&);
-                       AFImageTraverser& operator=(const AFImageTraverser&);
-                       
-                       // Image kd-tree. per thread
-                       KDTree *kdtree;
-                       int qSize, maxQSize;
-                       // number of fragments a chunk is divided into. It is 
divided equally amongst all clients
-                       int numFragments;
-                       bool outputStreams;
-                       bool spatialCoherence;
-                       string streamfilename;
-                       FILE **streamfile;
-                       //SCIRun::Mutex streamlock;
-                       fpos_t *numsamples_pos;
-                       int *firsttime;
-                       // the reprojection queue per thread
-                       CQ<Sample> *temporalQ;
-                       Sample **newSample;
-                       SampleSet **newSampleSet;
-                       // Random number generator array??
-                       MT_RNG *myRandomNumber;
-                       // display stuff
-                       bool debug_window;
-                       XWindow statsWindow;
-            Display* dpy;
-            GLXContext gl_context;
-                       int displayClientId;
-                       int displayCount;
-                       // These contain stuff for font stuff.
-                       XFontStruct* fontInfo;
-                       GLuint fontbase;
-                       bool windowOpen;
-                       bool madeCurrent;
-                       TileDisplayMode tmode;
-                       float visScale;
-
-                       // ??????????????
-                       int num_clients;
-                       int chunk_size;
-                       int samplingrate;
+    private:
+      AFImageTraverser(const AFImageTraverser&);
+      AFImageTraverser& operator=(const AFImageTraverser&);
+      
+      // Image kd-tree. per thread
+      KDTree *kdtree;
+      int qSize, maxQSize;
+      // number of fragments a chunk is divided into. It is divided equally 
amongst all clients
+      int numFragments;
+      bool outputStreams;
+      bool spatialCoherence;
+      string streamfilename;
+      FILE **streamfile;
+      //SCIRun::Mutex streamlock;
+      fpos_t *numsamples_pos;
+      int *firsttime;
+      // the reprojection queue per thread
+      CQ<Sample> *temporalQ;
+      Sample **newSample;
+      SampleSet **newSampleSet;
+      int *xhairBlockStart;
+      int xhairBlockSize;
+      Color **color;
+      // Random number generator array??
+      MT_RNG *myRandomNumber;
+      // display stuff
+      bool debug_window;
+      XWindow statsWindow;
+          Display* dpy;
+          GLXContext gl_context;
+      int displayClientId;
+      int displayCount;
+      // These contain stuff for font stuff.
+      XFontStruct* fontInfo;
+      GLuint fontbase;
+      bool windowOpen;
+      bool madeCurrent;
+      TileDisplayMode tmode;
+      float visScale;
+      
+      // ??????????????
+      int num_clients;
+      int chunk_size;
+      int samplingrate;
       
       
/////////////////////////////////////////////////////////////////////////
       // Samples done.

Modified: branches/AFR/Engine/ImageTraversers/AFR/kdtree.cc
==============================================================================
--- branches/AFR/Engine/ImageTraversers/AFR/kdtree.cc   (original)
+++ branches/AFR/Engine/ImageTraversers/AFR/kdtree.cc   Mon Sep  5 10:30:07 
2005
@@ -234,7 +234,7 @@
        tile[index*2].recomputeStats(tile);
 #endif
 #endif
-       tile[index*2].updateCommon(currenttime, tile, numTiles, samplingrate);
+       tile[index*2].updateCommon(currenttime, tile, numTiles, samplingrate, 
B);
        // insert right child in the random list; we do it first because we 
do not want it to be valid yet
        insertRandom(index*2+1);
        // set the right child as valid tile
@@ -247,7 +247,7 @@
        tile[index*2+1].recomputeStats(tile);
 #endif
 #endif
-       tile[index*2+1].updateCommon(currenttime, tile, numTiles, 
samplingrate);
+       tile[index*2+1].updateCommon(currenttime, tile, numTiles, 
samplingrate, B);
        numTiles+=2; // update valid tile counter
        
        // connect the two children
@@ -311,7 +311,7 @@
        tile[index/2].recomputeStats(tile);
 #endif
 #endif
-       tile[index/2].updateCommon(currenttime, tile, numTiles, samplingrate);
+       tile[index/2].updateCommon(currenttime, tile, numTiles, samplingrate, 
B);
        tile[index].setTimeLastDerivativeSet(currenttime);
        // increment valid tile counter
        numTiles++;

Modified: branches/AFR/Engine/ImageTraversers/AFR/sample.h
==============================================================================
--- branches/AFR/Engine/ImageTraversers/AFR/sample.h    (original)
+++ branches/AFR/Engine/ImageTraversers/AFR/sample.h    Mon Sep  5 10:30:07 
2005
@@ -93,6 +93,13 @@
                                worldCoord[2] = wz;
                        }
                
+            bool isInfinite()
+            {
+              if(isinf(worldCoord[0]) || isinf(worldCoord[1]) || 
isinf(worldCoord[2]))
+                return true;
+              else
+                return false;
+            }
                        void operator=(const Sample &value)
                        {
                                int i;
@@ -133,9 +140,12 @@
                        
                        inline float getRGBDistance(FloatColor &fc)
                        {
-                               return sqrt((c[0]-fc[0])*(c[0]-fc[0])
+                static const float epsilon = 0.001;
+                               float sqrdist = ((c[0]-fc[0])*(c[0]-fc[0])
                                                                              
  + (c[1]-fc[1])*(c[1]-fc[1])
                                                                              
  + (c[2]-fc[2])*(c[2]-fc[2]));
+                if(sqrdist<= epsilon) return 0.0f;
+                else return sqrt(sqrdist);
                        }
                        inline float getRGBMeasure()
                        {
@@ -174,138 +184,138 @@
                                
         Sample sample[6];
         
-                               float tgrad, xgrad, ygrad;
-                               Timestamp timestamp;
-                               float viewX, viewY, viewZ; 
-                               int tileindex;                  
+        float tgrad, xgrad, ygrad;
+        Timestamp timestamp;
+        float viewX, viewY, viewZ; 
+        int tileindex;                 
         // Constructor.
-                               SampleSet() { reset(); }
-                               
-                               void reset()
-                               {
-                                       xgrad = ygrad = tgrad = 0.0;
-                                       viewX = viewY = viewZ = 0;
-                                       timestamp = 0.0;
-                                       
+        SampleSet() { reset(); }
+        
+        void reset()
+        {
+          xgrad = ygrad = tgrad = 0.0;
+          viewX = viewY = viewZ = 0;
+          timestamp = 0.0;
+          
           // Reset the samples.
           for (int i=0;i<6;++i) {
             sample[i].reset();
           }
-                               }
-                               
-                               void set(int sloc, const float x, const float 
y, const Timestamp tstamp, 
-                                                                        
const float wx, const float wy, const float wz,
-                                                                        
const float r, const float g, const float b)
-                               {
-                                       
sample[sloc].set(x,y,tstamp,wx,wy,wz,r,g,b); 
-                                       timestamp = tstamp;
-                               }
-                               
-                               void computeGradients(Timestamp currenttime) {
+        }
         
-                                       //        float li, ri, bi, ti, ci, 
tmpi;
-                                       
-                                       viewX = 
sample[CENTER_SAMPLE].viewCoord[0];
-                                       viewY = 
sample[CENTER_SAMPLE].viewCoord[1];
-                                       viewZ = 
sample[CENTER_SAMPLE].viewCoord[2];
-                                       timestamp = currenttime;
-          
-       //         li = left.intensity();
-       //         ri = right.intensity();
-       //         ti = top.intensity();
-       //         bi = bottom.intensity();
-       //         ci = center.intensity();
-       //         tmpi = temporal.intensity();
-                                       
-                                       
if(fabsf(sample[CENTER_SAMPLE].viewCoord[0]-sample[LEFT_SAMPLE].viewCoord[0])>0.001
 &&
+        void set(int sloc, const float x, const float y, const Timestamp 
tstamp, 
+                 const float wx, const float wy, const float wz,
+                 const float r, const float g, const float b)
+        {
+          sample[sloc].set(x,y,tstamp,wx,wy,wz,r,g,b); 
+          timestamp = tstamp;
+        }
+        
+        void computeGradients(Timestamp currenttime) {
+        
+          //        float li, ri, bi, ti, ci, tmpi;
+          
+          viewX = sample[CENTER_SAMPLE].viewCoord[0];
+          viewY = sample[CENTER_SAMPLE].viewCoord[1];
+          viewZ = sample[CENTER_SAMPLE].viewCoord[2];
+          timestamp = currenttime;
+          
+          //         li = left.intensity();
+          //         ri = right.intensity();
+          //         ti = top.intensity();
+          //         bi = bottom.intensity();
+          //         ci = center.intensity();
+          //         tmpi = temporal.intensity();
+          
+          
if(fabsf(sample[CENTER_SAMPLE].viewCoord[0]-sample[LEFT_SAMPLE].viewCoord[0])>0.001
 &&
              
fabsf(sample[CENTER_SAMPLE].viewCoord[0]-sample[RIGHT_SAMPLE].viewCoord[0])>0.001
 )
-                                       {
-                                               //xgrad = 
fabsf(ci-li)/fabsf(center.viewCoord[0]-left.viewCoord[0]) 
-                                               //       + 
fabsf(ci-ri)/fabsf(center.viewCoord[0]-right.viewCoord[0]);
-                                               xgrad = 
sample[CENTER_SAMPLE].getRGBDistance(sample[LEFT_SAMPLE].c) / 
-                      
fabsf(sample[CENTER_SAMPLE].viewCoord[0]-sample[LEFT_SAMPLE].viewCoord[0]) + 
-                    
sample[CENTER_SAMPLE].getRGBDistance(sample[RIGHT_SAMPLE].c) /
-                      
fabsf(sample[CENTER_SAMPLE].viewCoord[0]-sample[RIGHT_SAMPLE].viewCoord[0]);
-                                       }
-                                       else
-                                       {
-                                               //xgrad = (fabsf(ci-li) + 
fabsf(ci-ri))/2.0;
-                                               xgrad = 
(sample[CENTER_SAMPLE].getRGBDistance(sample[LEFT_SAMPLE].c) + 
sample[CENTER_SAMPLE].getRGBDistance(sample[RIGHT_SAMPLE].c))/2.0f;
-                                       }
-                                       
-                                       
if(fabsf(sample[CENTER_SAMPLE].viewCoord[1]-sample[TOP_SAMPLE].viewCoord[1])>0.001
 && 
+          {
+            //xgrad = 
fabsf(ci-li)/fabsf(center.viewCoord[0]-left.viewCoord[0]) 
+            //  + fabsf(ci-ri)/fabsf(center.viewCoord[0]-right.viewCoord[0]);
+            xgrad = 
sample[CENTER_SAMPLE].getRGBDistance(sample[LEFT_SAMPLE].c) / 
+            
fabsf(sample[CENTER_SAMPLE].viewCoord[0]-sample[LEFT_SAMPLE].viewCoord[0]) + 
+            sample[CENTER_SAMPLE].getRGBDistance(sample[RIGHT_SAMPLE].c) /
+            
fabsf(sample[CENTER_SAMPLE].viewCoord[0]-sample[RIGHT_SAMPLE].viewCoord[0]);
+          }
+          else
+          {
+            //xgrad = (fabsf(ci-li) + fabsf(ci-ri))/2.0;
+            xgrad = 
(sample[CENTER_SAMPLE].getRGBDistance(sample[LEFT_SAMPLE].c) + 
sample[CENTER_SAMPLE].getRGBDistance(sample[RIGHT_SAMPLE].c))/2.0f;
+          }
+          
+          
if(fabsf(sample[CENTER_SAMPLE].viewCoord[1]-sample[TOP_SAMPLE].viewCoord[1])>0.001
 && 
              
fabsf(sample[CENTER_SAMPLE].viewCoord[1]-sample[BOTTOM_SAMPLE].viewCoord[1])>0.001
 )
-                                       {
-                                               //ygrad = 
fabsf(ci-bi)/fabsf(center.viewCoord[1]-top.viewCoord[1]) 
-                                               //       + 
fabsf(ci-ti)/fabsf(center.viewCoord[1]-bottom.viewCoord[1]);
-                                               ygrad = 
sample[CENTER_SAMPLE].getRGBDistance(sample[BOTTOM_SAMPLE].c) /
-                      
fabsf(sample[CENTER_SAMPLE].viewCoord[1]-sample[TOP_SAMPLE].viewCoord[1]) + 
-                    
sample[CENTER_SAMPLE].getRGBDistance(sample[TOP_SAMPLE].c) /
-                      
fabsf(sample[CENTER_SAMPLE].viewCoord[1]-sample[BOTTOM_SAMPLE].viewCoord[1]);
-                                       }
-                                       else
-                                       {
-                                               //ygrad = (fabsf(ci-bi) + 
fabsf(ci-ti))/2.0;
-                                               ygrad = 
(sample[CENTER_SAMPLE].getRGBDistance(sample[BOTTOM_SAMPLE].c) + 
sample[CENTER_SAMPLE].getRGBDistance(sample[TOP_SAMPLE].c))/2.0f;
-                                       }
-                                       
-                                       
if(fabsf(sample[CENTER_SAMPLE].t-sample[TEMPORAL_SAMPLE].t)>0.001)
-                                       {
-                                               //tgrad = 
fabsf(ci-tmpi)/(center.t - temporal.t);
-                                               tgrad = 
sample[CENTER_SAMPLE].getRGBDistance(sample[TEMPORAL_SAMPLE].c)/(sample[CENTER_SAMPLE].t
 - sample[TEMPORAL_SAMPLE].t);
-                                       }
-                                       else
-                                       {
-                                               //tgrad = fabsf(ci-tmpi);
-                                               tgrad = 
sample[CENTER_SAMPLE].getRGBDistance(sample[TEMPORAL_SAMPLE].c);
-                                       }                       
-                                       
-                                       //<TODO> set occlusion status here
-                               }
-                               
-                               void writeToFile(FILE *fp)
-                               {
-                                       sample[CENTER_SAMPLE].writeToFile(fp);
-                                       sample[LEFT_SAMPLE].writeToFile(fp);
-                                       sample[RIGHT_SAMPLE].writeToFile(fp);
-                                       sample[TOP_SAMPLE].writeToFile(fp);
-                                       sample[BOTTOM_SAMPLE].writeToFile(fp);
-                               }
-                               
-                               void print()
-                               {
-                                /* cout << "sample set ---------------->" << 
endl;
-                                       cout << "center: "; center.print();
-                                       cout << "left: "; 
sample[LEFT_SAMPLE].print();
-                                       cout << "right: "; right.print();
-                                       cout << "top: "; top.print();
-                                       cout << "bottom: "; bottom.print();
-                                       cout << "temporal: "; 
temporal.print();
-                                       cout << 
"<---------------------------" << endl << endl;*/
-                               }
-                               
-                               float      getIntensity(CrosshairSampleLoc 
sloc) { return sample[sloc].intensity(); }
-                               float      getR(CrosshairSampleLoc sloc) { 
return sample[sloc].c[0]; }
-                               float      getG(CrosshairSampleLoc sloc) { 
return sample[sloc].c[1]; }
-                               float      getB(CrosshairSampleLoc sloc) { 
return sample[sloc].c[2]; }
-                               Timestamp  
getSampleTimeStamp(CrosshairSampleLoc sloc) { return sample[sloc].t; } 
-                               inline bool isOccluded() { 
-                                 const static float epsilon = 0.001;
-                                 
if(isinf(sample[TEMPORAL_SAMPLE].worldCoord[0]) 
-                                    || 
isinf(sample[TEMPORAL_SAMPLE].worldCoord[1])
-                                    || 
isinf(sample[TEMPORAL_SAMPLE].worldCoord[2])
-                                    || 
isinf(sample[CENTER_SAMPLE].worldCoord[0])
-                                    || 
isinf(sample[CENTER_SAMPLE].worldCoord[1])
-                                    || 
isinf(sample[CENTER_SAMPLE].worldCoord[2]))
-                                   return false;
-                       
-                                 
if(fabsf(sample[TEMPORAL_SAMPLE].worldCoord[0]-sample[CENTER_SAMPLE].worldCoord[0])>
 epsilon 
-                                    || 
fabsf(sample[TEMPORAL_SAMPLE].worldCoord[1]-sample[CENTER_SAMPLE].worldCoord[1])>
 epsilon 
-                                    || 
fabsf(sample[TEMPORAL_SAMPLE].worldCoord[2]-sample[CENTER_SAMPLE].worldCoord[2])>
 epsilon )
-                                    return true;
-                                  else
-                                    return false;
-                               }
+          {
+            //ygrad = 
fabsf(ci-bi)/fabsf(center.viewCoord[1]-top.viewCoord[1]) 
+            //  + 
fabsf(ci-ti)/fabsf(center.viewCoord[1]-bottom.viewCoord[1]);
+            ygrad = 
sample[CENTER_SAMPLE].getRGBDistance(sample[BOTTOM_SAMPLE].c) /
+            
fabsf(sample[CENTER_SAMPLE].viewCoord[1]-sample[TOP_SAMPLE].viewCoord[1]) + 
+            sample[CENTER_SAMPLE].getRGBDistance(sample[TOP_SAMPLE].c) /
+            
fabsf(sample[CENTER_SAMPLE].viewCoord[1]-sample[BOTTOM_SAMPLE].viewCoord[1]);
+          }
+          else
+          {
+            //ygrad = (fabsf(ci-bi) + fabsf(ci-ti))/2.0;
+            ygrad = 
(sample[CENTER_SAMPLE].getRGBDistance(sample[BOTTOM_SAMPLE].c) + 
sample[CENTER_SAMPLE].getRGBDistance(sample[TOP_SAMPLE].c))/2.0f;
+          }
+          
+          if(fabsf(sample[CENTER_SAMPLE].t-sample[TEMPORAL_SAMPLE].t)>0.001)
+          {
+            //tgrad = fabsf(ci-tmpi)/(center.t - temporal.t);
+            tgrad = 
sample[CENTER_SAMPLE].getRGBDistance(sample[TEMPORAL_SAMPLE].c)/(sample[CENTER_SAMPLE].t
 - sample[TEMPORAL_SAMPLE].t);
+          }
+          else
+          {
+            //tgrad = fabsf(ci-tmpi);
+            tgrad = 
sample[CENTER_SAMPLE].getRGBDistance(sample[TEMPORAL_SAMPLE].c);
+          }                    
+          
+          //<TODO> set occlusion status here
+        }
+        
+        void writeToFile(FILE *fp)
+        {
+          sample[CENTER_SAMPLE].writeToFile(fp);
+          sample[LEFT_SAMPLE].writeToFile(fp);
+          sample[RIGHT_SAMPLE].writeToFile(fp);
+          sample[TOP_SAMPLE].writeToFile(fp);
+          sample[BOTTOM_SAMPLE].writeToFile(fp);
+        }
+        
+        void print()
+        {
+          /* cout << "sample set ---------------->" << endl;
+          cout << "center: "; center.print();
+          cout << "left: "; sample[LEFT_SAMPLE].print();
+          cout << "right: "; right.print();
+          cout << "top: "; top.print();
+          cout << "bottom: "; bottom.print();
+          cout << "temporal: "; temporal.print();
+          cout << "<---------------------------" << endl << endl;*/
+        }
+        
+        float      getIntensity(CrosshairSampleLoc sloc) { return 
sample[sloc].intensity(); }
+        float      getR(CrosshairSampleLoc sloc) { return sample[sloc].c[0]; 
}
+        float      getG(CrosshairSampleLoc sloc) { return sample[sloc].c[1]; 
}
+        float      getB(CrosshairSampleLoc sloc) { return sample[sloc].c[2]; 
}
+        Timestamp  getSampleTimeStamp(CrosshairSampleLoc sloc) { return 
sample[sloc].t; } 
+        inline bool isOccluded() { 
+          const static float epsilon = 0.001;
+          if(isinf(sample[TEMPORAL_SAMPLE].worldCoord[0]) 
+          || isinf(sample[TEMPORAL_SAMPLE].worldCoord[1])
+          || isinf(sample[TEMPORAL_SAMPLE].worldCoord[2])
+          || isinf(sample[CENTER_SAMPLE].worldCoord[0])
+          || isinf(sample[CENTER_SAMPLE].worldCoord[1])
+          || isinf(sample[CENTER_SAMPLE].worldCoord[2]))
+            return false;
+          
+          
if(fabsf(sample[TEMPORAL_SAMPLE].worldCoord[0]-sample[CENTER_SAMPLE].worldCoord[0])>
 epsilon 
+          || 
fabsf(sample[TEMPORAL_SAMPLE].worldCoord[1]-sample[CENTER_SAMPLE].worldCoord[1])>
 epsilon 
+          || 
fabsf(sample[TEMPORAL_SAMPLE].worldCoord[2]-sample[CENTER_SAMPLE].worldCoord[2])>
 epsilon )
+            return true;
+          else
+            return false;
+        }
         
                };
        } // end namespace Afr

Modified: branches/AFR/Engine/ImageTraversers/AFR/tiles.cc
==============================================================================
--- branches/AFR/Engine/ImageTraversers/AFR/tiles.cc    (original)
+++ branches/AFR/Engine/ImageTraversers/AFR/tiles.cc    Mon Sep  5 10:30:07 
2005
@@ -46,9 +46,9 @@
 }
 
 void Tile::initialize(int lft, int btm, int rht, int tp, 
-                           int currlevel, int address, 
-                      int width, int height, 
-                      float A, float B)
+                         int currlevel, int address, 
+                      const int width, const int height, 
+                      const float A, const float B)
 {
        left = lft;
        right = rht;
@@ -69,7 +69,7 @@
        tGradient.reset();
        occlusion.reset();
        error = AMBIENT_ERROR;
-       sum_tstamps.reset();
+       sumTstamp.reset();
        color[0] = drand48();
        color[1] = drand48();
        color[2] = drand48();
@@ -97,7 +97,7 @@
                return false;
 }
 
-void Tile::setDerivative(Timestamp currenttime)
+void Tile::setDerivative(const Timestamp currenttime)
 {
        if(currenttime>time_last_updated && currenttime > 
time_last_derivative_set)
                derivative_gap_count++;
@@ -115,30 +115,47 @@
        }
 }
 
-float Tile::getUnderSampling(float currenttime, int samplingrate, Tile *tree)
+float Tile::getUnderSampling(const Timestamp currenttime, const int 
samplingrate, Tile *tree, const float B)
 {
-       float meanGlobalAge = 
0.5*(float)tree[1].num_pixels/(float)samplingrate;
-       float uerror = (currenttime - 
sum_tstamps.getMean())*0.33/meanGlobalAge - 1.0;
-       if(uerror>1.0) uerror = 1.0;
-       if(uerror<0.0) uerror = 0.0;
-       return uerror;
+  //float meanGlobalAge = 0.5*(float)tree[1].num_pixels/(float)samplingrate;
+  float a = -B;
+  //float meanGlobalAge = currenttime - 
(1.0f/(a*a*currenttime))*(expf(a*currenttime)*(a*currenttime-1.0f) + 1.0f);
+  float globalAge = (1.0f/(a*a))*(1.0 - 
expf(a*currenttime)*(B*currenttime+1.0f));
+  float sumWeights = (1.0f/B)*(1.0- - expf(a*currenttime));
+ /* float expectedSamples = 
ceilf(currenttime*samplingrate*((float)tree[1].num_pixels/(float)num_pixels));
+  float r = currenttime/expectedSamples;
+  float efac = expf(B*r);
+  float gpsum;
+  if(efac>0)
+    gpsum = (powf(efac,expectedSamples) - 1.0f)/(efac - 1.0f);
+  else
+    gpsum = (1.0f - powf(efac,expectedSamples))/(1.0f - efac);
+  
+  float globalAge = (1.0f/expf(B*currenttime))
+                    *(currenttime*gpsum 
+                      - r*(gpsum-1.0f 
+                           - expectedSamples*expf(
+                                 (expectedSamples+1.0f)*B*r))
+                                  /(1.0f - expf(B*r)));
+  float sumWeights = gpsum/expf(B*currenttime);*/
+  //cout << "meanGlobalAge = " << globalAge/sumWeights << endl;
+  float uerror = (currenttime - 
sumTstamp.getMean())*0.20/(globalAge/sumWeights) - 1.0;
+  if(uerror>1.0) uerror = 1.0f;
+  if(uerror<0.0) uerror = 0.0f;
+  return uerror;
 }
 
-void Tile::setError(Tile *tree, int nTiles, Timestamp currenttime, unsigned 
int samplingrate)
+void Tile::setError(Tile *tree, const int nTiles, const Timestamp 
currenttime, const unsigned int samplingrate, const float B)
 {
-       //error = (0.25*getUnderSampling(currenttime, samplingrate) 
-       //      + 2.0*(intensity.getVariance()+AMBIENT_ERROR) 
-       //      +0.25*getOcclusionMeasure(tree, nTiles))*num_pixels ;
-       //error =        
2.0*(intensity.getVariance()+AMBIENT_ERROR)*num_pixels; 
        error =  2.0*(r.getVariance() + g.getVariance() + b.getVariance() + 
AMBIENT_ERROR)*num_pixels; 
-       error += 0.25*getUnderSampling(currenttime, samplingrate, 
tree)*num_pixels; 
+       //error += 0.25*getUnderSampling(currenttime, samplingrate, tree, 
B)*num_pixels; 
        error += 0.25*getOcclusionMeasure(tree, nTiles)*num_pixels;
 }
 
-void Tile::setTotalError(Tile *tree, Timestamp currenttime, int nTiles, 
unsigned int samplingrate)
+void Tile::setTotalError(Tile *tree, const Timestamp currenttime, const int 
nTiles, const unsigned int samplingrate, const float B)
 {
        // update the mean & variance
-       setError(tree, nTiles, currenttime, samplingrate);
+       setError(tree, nTiles, currenttime, samplingrate, B);
        // well what about the derivative
        setDerivative(currenttime);
        total_error = KOEFF*(error) + (1.0-KOEFF)*derivative;
@@ -146,10 +163,10 @@
        time_last_updated = currenttime;
 }
 
-void Tile::updateCommon(Timestamp currenttime, Tile *tree, int nTiles, 
unsigned int samplingrate)
+void Tile::updateCommon(const Timestamp currenttime, Tile *tree, const int 
nTiles, const unsigned int samplingrate, const float B)
 {
        // set the total error
-       setTotalError(tree, currenttime, nTiles, samplingrate);
+       setTotalError(tree, currenttime, nTiles, samplingrate, B);
        if(num_pixels<=min_tile_area) min_size_violation = true;
        else min_size_violation = false;
        max_error = min_error = total_error; // check if we need variance here
@@ -175,12 +192,12 @@
     yGradient += tree[i].yGradient;
     tGradient += tree[i].tGradient;
     occlusion += tree[i].occlusion;
-    sum_tstamps += tree[i].sum_tstamps;
+    sumTstamp += tree[i].sumTstamp;
   } 
 }
 
-void Tile::updateStatsAddSampleSet(SampleSet *newsampleset, Timestamp 
currenttime, float A, float B, 
-                          Tile *tree, int nTiles, unsigned int samplingrate)
+void Tile::updateStatsAddSampleSet(SampleSet *newsampleset, const Timestamp 
currenttime, const float A, const float B, 
+                          Tile *tree, const int nTiles, const unsigned int 
samplingrate)
 {
        float wt_new = 1.0;
        float ratio = getWeight(A, B, (currenttime - time_last_updated));
@@ -215,18 +232,18 @@
        yGradient.addElement(newsampleset->ygrad, wt_new, ratio);
        tGradient.addElement(newsampleset->tgrad, wt_new, ratio);
        
-       sum_tstamps.addElement(stime, wt_new, ratio);
-       //sum_tstamps.addElement(4.0*stime, wt_new, 1.0);
-       //sum_tstamps.addElement(temptime, 
getWeight(A,B,currenttime-temptime), 1.0);
+       sumTstamp.addElement(stime, wt_new, ratio);
+       //sumTstamp.addElement(4.0*stime, wt_new, 1.0);
+       //sumTstamp.addElement(temptime, getWeight(A,B,currenttime-temptime), 
1.0);
        
        float occ = (newsampleset->isOccluded())? 1.0 : 0.0;
        occlusion.addElement(occ, wt_new, ratio);
 
-       updateCommon(currenttime, tree, nTiles, samplingrate);
+       updateCommon(currenttime, tree, nTiles, samplingrate, B);
 }
 
-void Tile::updateStatsAddSample(Sample *newsample, Timestamp currenttime, 
float A, float B, 
-                          Tile *tree, int nTiles, float tgrad, bool occ, 
unsigned int samplingrate)
+void Tile::updateStatsAddSample(Sample *newsample, Timestamp currenttime, 
const float A, const float B, 
+                          Tile *tree, const int nTiles, float tgrad, bool 
occ, const unsigned int samplingrate)
 {
        float wt_new = 1.0;
        float ratio = getWeight(A, B, (currenttime - time_last_updated));
@@ -234,14 +251,16 @@
        r.addElement(newsample->c[0], wt_new, ratio);
        g.addElement(newsample->c[1], wt_new, ratio);
        b.addElement(newsample->c[2], wt_new, ratio);
-       //sum_tstamps.addElement(newsample->t, wt_new, ratio);
-       sum_tstamps.uniformDecrease(ratio);
+    if(newsample->isInfinite())
+         sumTstamp.addElement(newsample->t, wt_new, ratio);
+       else
+      sumTstamp.uniformDecrease(ratio);
        xGradient.uniformDecrease(ratio);
        yGradient.uniformDecrease(ratio);
        tGradient.uniformDecrease(ratio);
        occlusion.uniformDecrease(ratio);
 
-       updateCommon(currenttime, tree, nTiles, samplingrate);
+       updateCommon(currenttime, tree, nTiles, samplingrate, B);
 }
 
 void Tile::updateMinMax(Tile *tree)
@@ -351,7 +370,7 @@
        }
 }
 
-void Tile::writeToFile(FILE *fp, Tile *tree, int nTiles, Timestamp 
currenttime, int samplingrate)
+void Tile::writeToFile(FILE *fp, Tile *tree, const int nTiles, const 
Timestamp currenttime, int samplingrate)
 {
        // we have to output in binart, so make  a struct and use fwrite
        TilePrintFormat tpf;
@@ -370,7 +389,7 @@
        fwrite(&tpf, sizeof(tpf), 1, fp);
 }
 
-float Tile::getOcclusionMeasure(Tile *tree, int nTiles)
+float Tile::getOcclusionMeasure(Tile *tree, const int nTiles)
 {
         // Note that it might be needed to blur the occlusion measure
        // we used to update up the tree and use higher levels, but now it 
wont work, we
@@ -387,7 +406,7 @@
        invexpInit();
 }
 
-float Manta::Afr::getWeight(float A, float B, float age)
+float Manta::Afr::getWeight(const float A, const float B, const float age)
 {
        //return (A*invexp(B*age));
        return (A*expf(-B*age));
@@ -406,8 +425,8 @@
 }
 
 
-void Tile::display(TileDisplayMode displaymode, int nTiles, Tile *tree, 
-                  int samplingrate, Timestamp currenttime, float A, float B, 
float visScale)
+void Tile::display(TileDisplayMode displaymode, const int nTiles, Tile 
*tree, 
+                  const int samplingrate, const Timestamp currenttime, const 
float A, const float B, float visScale)
 {
   float gvisScale;
   gvisScale = 0.33;
@@ -440,9 +459,13 @@
                case AVERAGE_AGE:
                        {
                                glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
-                               float dcol= getUnderSampling(currenttime, 
samplingrate, tree);
+                               float dcol= getUnderSampling(currenttime, 
samplingrate, tree, B);
                                dcol *= visScale;
                                glColor3f(0.6*dcol, 0.3*dcol, 0.9*dcol);
+                               //float a = -B;
+                               //float meanGlobalAge = currenttime - 
(1.0f/(a*a*currenttime))*(expf(a*currenttime)*(a*currenttime-1.0f) + 1.0f);
+                               //float meanGlobalAge = 
(1.0f/(a*a*currenttime))*(1.0 - expf(a*currenttime)*(B*currenttime+1.0f));
+                               //cout << "meanGlobalAge = " << meanGlobalAge 
<< endl;
                        }
                        break;
                case OCCLUSION:

Modified: branches/AFR/Engine/ImageTraversers/AFR/tiles.h
==============================================================================
--- branches/AFR/Engine/ImageTraversers/AFR/tiles.h     (original)
+++ branches/AFR/Engine/ImageTraversers/AFR/tiles.h     Mon Sep  5 10:30:07 
2005
@@ -57,7 +57,7 @@
                 */
 
                Stats2 xGradient, yGradient, tGradient, 
-                     sum_tstamps, occlusion;
+                     sumTstamp, occlusion;
                //Stats intensity;
                Stats r,g,b;
                float error, previous_error, total_error;
@@ -88,14 +88,14 @@
                  g = t.g;
                  b = t.b;
                        
-                 sum_tstamps = t.sum_tstamps;
+                 sumTstamp = t.sumTstamp;
                  occlusion = t.occlusion;
                }
                void print( int nTiles, Tile *tree, Timestamp currenttime);
                void initialize(int lft, int btm, int rht, int top, 
                                                int currlevel, int address, 
-                                               int width, int height,  
-                                               float A, float B);
+                                               const int width, const int 
height,      
+                                               const float A, const float B);
                inline void setNextTile(int index) { next = index; }
                inline void setPrevTile(int index) { prev = index; }
                inline void setNextRandomTile(int index) { nextrandom = 
index; }
@@ -138,12 +138,12 @@
                                return true;
                        return false;
                }
-               void updateCommon(Timestamp currenttime, Tile *tree, int 
nTiles, unsigned int samplingrate);
+               void updateCommon(const Timestamp currenttime, Tile *tree, 
const int nTiles, const unsigned int samplingrate, const float B);
                void updateStatsAddSampleSet(SampleSet *newsampleset, 
Timestamp currenttime, float A, float B, 
                                        Tile *tree, int nTiles, unsigned int 
samplingrate);
                void recomputeStats(Tile *tree);
-               void updateStatsAddSample(Sample *newsample, Timestamp 
currenttime, float A, float B, 
-                          Tile *tree, int nTiles, float tgrad, bool occ, 
unsigned int samplingrate);
+               void updateStatsAddSample(Sample *newsample, const Timestamp 
currenttime, const float A, const float B, 
+                          Tile *tree, const int nTiles, float tgrad, bool 
occ, const unsigned int samplingrate);
                void updateMinMax(Tile *tree);
                inline float xGrad() { return xGradient.getMean(); }
                inline float yGrad() { return yGradient.getMean(); }
@@ -167,14 +167,14 @@
                        y = ((float)bottom+(float)top)/2.0;
                }       
                void setDerivative(Timestamp currenttime);
-               void setError(Tile *tree, int nTiles, Timestamp currenttime, 
unsigned int samplingrate);
-               void setTotalError(Tile *tree, Timestamp currenttime, int 
nTiles, unsigned int samplingrate);
-               inline void setTimeLastDerivativeSet(Timestamp currenttime)
+               void setError(Tile *tree, const int nTiles, const Timestamp 
currenttime, const unsigned int samplingrate, const float B);
+               void setTotalError(Tile *tree, const Timestamp currenttime, 
const int nTiles, const unsigned int samplingrate, const float B);
+               inline void setTimeLastDerivativeSet(const Timestamp 
currenttime)
                {
                        time_last_derivative_set = currenttime;
                        derivative_gap_count = 0;
                }
-               inline void setTimeLastUpdated(Timestamp currenttime)
+               inline void setTimeLastUpdated(const Timestamp currenttime)
                {
                        time_last_updated = currenttime;
                }
@@ -183,16 +183,16 @@
                        return (num_pixels<min_tile_area);
                }
                void writeToFile(FILE *fp, Tile *tree, int nTiles, Timestamp 
currenttime, int samplingrate);
-               float getUnderSampling(float currenttime, int samplingrate, 
Tile *tree);
-               float getOcclusionMeasure(Tile *tree, int nTiles);
+               float getUnderSampling(const Timestamp currenttime, const int 
samplingrate, Tile *tree, const float B);
+               float getOcclusionMeasure(Tile *tree, const int nTiles);
                bool checkBounds(int x, int y);
-    void display(TileDisplayMode displaymode, int nTiles, Tile *tree, 
-               int samplingrate, Timestamp currenttime, float A, float B, 
float visScale);
+    void display(TileDisplayMode displaymode, const int nTiles, Tile *tree, 
+               const int samplingrate, const Timestamp currenttime, const 
float A, const float B, float visScale);
                
 };
 
 void shuffleOrder(int *buf, int size); // shuffle elements of the buffer of 
size w*h
-float getWeight(float A, float B, float age);
+float getWeight(const float A, const float B, const float age);
 void invexp_init();
 
 } // end namespace Afr

Modified: branches/AFR/Model/Cameras/PinholeCamera.cc
==============================================================================
--- branches/AFR/Model/Cameras/PinholeCamera.cc (original)
+++ branches/AFR/Model/Cameras/PinholeCamera.cc Mon Sep  5 10:30:07 2005
@@ -239,10 +239,10 @@
   wR = nearZ*tan(hfov*0.5*M_PI/180.0);
   wT = nearZ*tan(vfov*0.5*M_PI/180.0);
   fprintf(fp,"origin %f %f %f\nU %f %f %f\nV %f %f %f\nN %f %f %f\nextents 
%f %f %f %f\nnear %f\nfar %f\n",
-              eye.x(), eye.y(), eye.z(),
-             u.x(), u.y(), u.z(),
-              v.x(), v.y(), v.z(),
-             direction.x(), direction.y(), direction.z(),
-             -wR, wR, -wT, wT,
-              nearZ, nearZ*1000.0);
+          eye.x(), eye.y(), eye.z(),
+          mv[0][0], mv[0][1], mv[0][2],
+          mv[1][0], mv[1][1], mv[1][2],
+          -mv[2][0], -mv[2][1], -mv[2][2],
+          -wR, wR, -wT, wT,
+          nearZ, nearZ*1000.0);
 }

Added: branches/AFR/combinestreams.cpp
==============================================================================
--- (empty file)
+++ branches/AFR/combinestreams.cpp     Mon Sep  5 10:30:07 2005
@@ -0,0 +1,793 @@
+/***************************************************************************
+                          parsestream.cpp  -  description
+                             -------------------
+    begin                : Mon Nov 22 2004
+    copyright            : (C) 2004 by Abhinav Dayal
+    email                : abhinav@cs.northwestern.edu
+ ***************************************************************************/
+
+// parse stream format 1.0
+#include <GL/glut.h>
+#include <stdlib.h>
+#include <iostream>
+#include <stdio.h>
+#include <time.h>
+#include <stdio.h>
+
+using namespace std;
+#define STREAM_VERSION "1.3"
+
+
+int chunk_count=0;
+typedef float Matrix_4x4[4][4];
+enum Mode {SAMPLES, TILES, CAMERA};
+class Camera
+{
+    public:
+        float nearplane, farplane, orig[3], U[3], V[3], N[3], wL, wR, wB, wT;
+        void read();
+        void getCamCoordinate(float c[3], float worldCoord[3]);
+        void getProjectionMatrix(Matrix_4x4 &m);
+       void writeToFile();
+};
+
+class Globals
+{
+    public:
+        int total_samples_to_parse, total_samples_parsed;
+        float total_time_to_parse;
+        int width, height;
+        int num_tiles;
+        Mode mode;
+        FILE **fp, *ofp;
+        fpos_t numsamplespos;
+       int numFiles, index;
+       int samplesThisChunk;
+       int firsttime;
+        Camera cam;
+        Matrix_4x4 cameramat;
+        Globals()
+        {
+            total_samples_to_parse=0;
+            total_samples_parsed = 0;
+            total_time_to_parse=0.0;
+            width = height = 0;
+            mode=CAMERA;
+            num_tiles = 0;
+            fp=NULL;
+        }
+};
+
+struct SamplePrintFormat
+{
+    float worldCoord[3], vel[3];
+    unsigned char rgb[3];
+    float timestamp;
+};
+
+struct TilePrintFormat
+{
+    int index;
+    float xgrad, ygrad, tgrad;
+    float oldest_sample_timestamp;
+};
+
+Globals globals;
+
+void eatWhitespace(FILE *fp)
+{
+   char ch=getc(fp);
+   while(ch==' ' || ch=='\t' || ch=='\n' || ch=='\f' || ch=='\r')
+      ch=getc(fp);
+   ungetc(ch,fp);
+}
+
+void Camera::writeToFile()
+{
+    fprintf(globals.ofp,"origin %f %f %f\nU %f %f %f\nV %f %f %f\nN %f %f 
%f\nextents %f %f %f %f\nnear %f\nfar %f\n",
+        orig[0], orig[1], orig[2],
+        U[0], U[1], U[2],
+        V[0], V[1], V[2],
+        N[0], N[1], N[2],
+        wL, wR, wB, wT,
+        nearplane, farplane);
+}
+
+void Camera::read()
+{
+    char token[255], ch;
+
+    if(fscanf(globals.fp[globals.index],"%s",token)!=1 && 
strcmp(token,"origin")!=0)
+    {
+        printf("origin expected\n");
+        exit(1);
+    }
+    
if(fscanf(globals.fp[globals.index],"%f%f%f",&orig[0],&orig[1],&orig[2])!=3)
+    {
+        printf("can't read camera origin\n");
+        exit(1);
+    }
+    eatWhitespace(globals.fp[globals.index]);
+    if((ch=fgetc(globals.fp[globals.index]))!='U')
+    {
+        printf("U vector expected\n");
+        exit(1);
+    }
+    if(fscanf(globals.fp[globals.index],"%f%f%f",&U[0],&U[1],&U[2])!=3)
+    {
+        printf("can't read U vector\n");
+        exit(1);
+    }
+    eatWhitespace(globals.fp[globals.index]);
+    if((ch=fgetc(globals.fp[globals.index]))!='V')
+    {
+        printf("V vector expected\n");
+        exit(1);
+    }
+    if(fscanf(globals.fp[globals.index],"%f%f%f",&V[0],&V[1],&V[2])!=3)
+    {
+        printf("can't read V vector\n");
+        exit(1);
+    }
+    eatWhitespace(globals.fp[globals.index]);
+    if((ch=fgetc(globals.fp[globals.index]))!='N')
+    {
+        printf("N vector expected\n");
+        exit(1);
+    }
+    if(fscanf(globals.fp[globals.index],"%f%f%f",&N[0],&N[1],&N[2])!=3)
+    {
+    }
+    eatWhitespace(globals.fp[globals.index]);
+    if(fscanf(globals.fp[globals.index],"%s",token)!=1 && 
strcmp(token,"extents")!=0)
+    {
+        printf("extents expected\n");
+        exit(1);
+    }
+    if(fscanf(globals.fp[globals.index],"%f%f%f%f",&wL,&wR,&wB,&wT)!=4)
+    {
+        printf("can't read camera image plane extents\n");
+        exit(1);
+    }
+    eatWhitespace(globals.fp[globals.index]);
+    if(fscanf(globals.fp[globals.index],"%s",token)!=1 && 
strcmp(token,"near")!=0)
+    {
+        printf("near expected\n");
+        exit(1);
+    }
+    if(fscanf(globals.fp[globals.index],"%f",&nearplane)!=1)
+    {
+        printf("can't read near plane distance\n");
+        exit(1);
+    }
+    eatWhitespace(globals.fp[globals.index]);
+    if(fscanf(globals.fp[globals.index],"%s",token)!=1 && 
strcmp(token,"far")!=0)
+    {
+        printf("far expected\n");
+        exit(1);
+    }
+    if(fscanf(globals.fp[globals.index],"%f",&farplane)!=1)
+    {
+        printf("can't read far plane distance\n");
+        exit(1);
+    }
+    
+    /*printf("read camera: O(%f, %f, %f); U(%f, %f, %f); V(%f, %f, %f); 
N(%f, %f, %f); extents((%f, %f, %f, %f); near=%f, far=%f\n",
+           orig[0], orig[1], orig[2], U[0], U[1], U[2], V[0], V[1], V[2], 
N[0], N[1], N[2],
+           wL, wR, wB, wT, nearplane, farplane);*/
+    getProjectionMatrix(globals.cameramat);
+}
+
+void Camera::getCamCoordinate(float c[3], float worldCoord[3])
+{
+  float wP[3];
+  wP[0] = worldCoord[0]-orig[0];
+  wP[1] = worldCoord[1]-orig[1];
+  wP[2] = worldCoord[2]-orig[2];
+  
+  c[0] = U[0]*wP[0] + U[1]*wP[1] + U[2]*wP[2];
+  c[1] = V[0]*wP[0] + V[1]*wP[1] + V[2]*wP[2];
+  c[2] = N[0]*wP[0] + N[1]*wP[1] + N[2]*wP[2];
+}
+
+
+// m3 = m1 x m2
+void multMatrix(Matrix_4x4 &m1, Matrix_4x4 &m2, Matrix_4x4 &m3)
+{
+    int i,j,k;
+    
+    for(i=0; i<4; i++)
+        for(j=0; j<4; j++)
+        {
+            m3[i][j] = 0;
+            for(k=0; k<4; k++)
+                m3[i][j] +=  m1[i][k]*m2[k][j];
+        }
+}
+
+void setIdentity(Matrix_4x4 &m)
+{
+    int i,j;
+    for(i=0; i<4; i++)
+        for(j=0; j<4; j++)
+            m[i][j] = (i==j);
+}
+
+void printMatrix(Matrix_4x4 &m)
+{
+    int i,j;
+    for(i=0; i<4; i++)
+    {
+        for(j=0; j<4; j++)
+            printf("%f  ",m[i][j]);
+        printf("\n");
+    }
+}
+
+void Camera::getProjectionMatrix(Matrix_4x4 &m)
+{
+    
+    // form the viewmatrix and projection matrices
+    // now view matrix is
+    // R =  [X | Y | Z | 1]
+    // T = translate by [-camX, -camY, -camZ]
+    // view matrix = RT
+    
+    // Note: after coding this we will have to write a test routine that 
reads the samples and 
+    // using the camera projects them on to the screen.
+    Matrix_4x4 S,T,R,view,projection,viewport,temp;
+    int i;
+    setIdentity(T);
+    T[0][3] = -orig[0];
+    T[1][3] = -orig[1];
+    T[2][3] = -orig[2];
+    
+    setIdentity(R);
+    for(i=0;i<3; i++)
+    {
+        R[0][i] = U[i];
+        R[1][i] = V[i];
+        R[2][i] = N[i];
+    }
+    multMatrix(R,T,view);
+
+    //printf("view matrix:\n");
+    //printMatrix(view);  
+    // now we set the projection matrix
+    // projection matrix is the perspective projection matrix
+    // [ from OpenGL standards ]
+    projection[0][0] = 2.0*nearplane/(wR-wL);
+    projection[0][1] = 0.0;
+    projection[0][2] = (wR+wL)/(wR-wL);
+    projection[0][3] = 0.0;
+    
+    projection[1][0] = 0.0;
+    projection[1][1] = 2.0*nearplane/(wT-wB);
+    projection[1][2] = (wT+wB)/(wT-wB);
+    projection[1][3] = 0.0;
+    
+    projection[2][0] = 0.0;
+    projection[2][1] = 0.0;
+    projection[2][2] = -(farplane+nearplane)/(farplane-nearplane);
+    projection[2][3] = -2.0*farplane*nearplane/(farplane-nearplane);
+    
+    projection[3][0] = 0.0;
+    projection[3][1] = 0.0;
+    projection[3][2] = -1.0;
+    projection[3][3] = 0.0;
+    
+    //printf("projection matrix:\n");
+    //printMatrix(projection);
+    
+    setIdentity(T);
+    T[0][3] = (float)globals.width/2.0;
+    T[1][3] = (float)globals.height/2.0;
+    setIdentity(S);
+    S[0][0] = -(float)globals.width/2.0;//(wR-wL);
+    S[1][1] = -(float)globals.height/2.0;//(wT-wB);
+    multMatrix(T,S,viewport);
+
+    //printf("viewport matrix:\n");
+    //printMatrix(viewport);
+    
+    multMatrix(viewport,projection,temp);
+    multMatrix(temp,view,m);
+    
+    //printf("camera matrix:\n");
+    //printMatrix(m);
+}
+
+
+void printSample(SamplePrintFormat &s)
+{
+    printf("Timestamp: %f; 3D coordinate (%f, %f, %f); ", s.timestamp, 
s.worldCoord[0], s.worldCoord[1], s.worldCoord[2]);
+    printf("Color (%d, %d, %d); ", s.rgb[0], s.rgb[1], s.rgb[2]);
+}
+
+void printTile(TilePrintFormat &t)
+{
+    printf("tile globals.index: %d; gradients (%f, %f, %f)\n", t.index, 
t.xgrad, t.ygrad, t.tgrad);
+}
+
+void displaySample(SamplePrintFormat &s)
+{
+    glBegin(GL_POINTS);
+    // define the sample color
+    glColor3f((float)s.rgb[0]/255.0, (float)s.rgb[1]/255.0, 
(float)s.rgb[2]/255.0);
+    float p[4];
+    int i;
+    for(i=0; i<4; i++)
+    {
+        p[i] = s.worldCoord[0]*globals.cameramat[i][0] + 
s.worldCoord[1]*globals.cameramat[i][1]
+               + s.worldCoord[2]*globals.cameramat[i][2] + 
globals.cameramat[i][3];
+    }
+    float znd = p[2]/p[3];
+    //if (znd >= 0.0f && znd <= 1.0f) {
+    //    printf("\n(%f, %f, %f) ---> (%f, %f, %f)\n", s.worldCoord[0], 
s.worldCoord[1], s.worldCoord[2], p[0]/p[3], p[1]/p[3], p[2]/p[3]);
+    //}
+    //else {
+    //    printf("#");
+    //}
+    glVertex2f(p[0]/p[3], p[1]/p[3]);
+    glEnd();
+}
+
+void displayTile(TilePrintFormat &t, int width, int height)
+{
+    // based om globals.index find the tile dimensions
+    // find the tile level, this would give us its dimension
+    int tilelevel = 0;  // tile lies at what tree level; 0 = root
+    int pos=0, count = t.index;
+    int beginindex=1;   // globals.index that starts at tilelevel
+    int xfac=1; // numtiles along x
+    int yfac=1; // numtiles along y
+    int xbegin=0, ybegin=0;
+    while(count!=1) // while not hit the root; note globals.index=1 for root
+    {
+        beginindex*=2;
+        count /= 2; // decrement count to go to previous level
+        tilelevel++;    // increment tilelevel
+        if(tilelevel%2==0) yfac*=2;
+        else xfac*=2;
+    }   
+    // find pos, i.e. the position of tile on this level, i.e. tilelevel
+    //cout << "tilelevel = " << tilelevel << ", xfac = " << xfac << ", yfac 
= " << yfac 
+    //    << "beginglobals.index = " << beginglobals.index << endl;
+
+    // this is kind of nasty -- the correct tile positions
+    // are swizzled (the bits are interleaved xyxyxyxy or yxyxyxyx
+    // depending on which tilelevel it is).
+    // the following loop unswizzles them.
+    pos = t.index - beginindex;
+    count = 0;
+    while (pos > 0) {
+        if (count%2 == tilelevel%2) {
+            ybegin += (pos&0x1)<<(count>>1);
+        }
+        else {
+            xbegin += (pos&0x1)<<(count>>1);
+        }
+        pos >>= 1;
+        count++;
+    }
+    // then scale the x,y location to the dimensions
+    // of the tiles at that tilelevel
+    int xdim = width/xfac;  // dimension of tile along x = 
screenwidth/numtiles_along_x
+    int ydim = height/yfac; // dimension of tile along y = 
screenheight/numtiles_along_y
+    xbegin *= xdim;
+    ybegin *= ydim;
+    //cout << "xdim = " << xdim << ", ydim = " << ydim << ", xbegin = " << 
xbegin << ", ybegin = " << ybegin << endl;
+    //cout << "dimensions: (" << xbegin << ", " << ybegin << ") -- (" << 
xbegin+xdim << ", " << ybegin+ydim << ")" << endl;
+    // display the tile
+
+#if 0
+    glBegin(GL_LINES);
+    glColor3f(1.0,1.0,0.0);
+    
+    glVertex2d(xbegin, ybegin);
+    glVertex2d(xbegin+xdim, ybegin);
+    
+    glVertex2d(xbegin+xdim, ybegin);
+    glVertex2d(xbegin+xdim, ybegin+ydim);
+    
+    glVertex2d(xbegin+xdim, ybegin+ydim);
+    glVertex2d(xbegin, ybegin+ydim);
+    
+    glVertex2d(xbegin, ybegin+ydim);
+    glVertex2d(xbegin, ybegin);
+    
+    glEnd();
+#else
+    glColor3f((float)rand()/(float)RAND_MAX+0.4f,
+              (float)rand()/(float)RAND_MAX+0.4f,
+              (float)rand()/(float)RAND_MAX+0.4f);
+
+    glBegin(GL_QUADS);
+    glVertex2d(xbegin, ybegin);
+    glVertex2d(xbegin+xdim, ybegin);
+    glVertex2d(xbegin+xdim, ybegin+ydim);
+    glVertex2d(xbegin, ybegin+ydim);
+    glEnd();
+#endif
+
+}
+
+void readMatrix(Matrix_4x4 &m)
+{
+    int i, j;
+    for(i=0; i<4; i++)
+        for(j=0; j<4; j++)
+        {
+            if(fscanf(globals.fp[globals.index],"%f",&m[i][j])!=1)
+            {
+                printf("matrix element %d,%d not found\n", i, j);
+                exit(1);
+            }
+        }
+    eatWhitespace(globals.fp[globals.index]);
+}
+
+void parseTiles()
+{
+    //glClear(GL_COLOR_BUFFER_BIT);
+    cout << "inside parseTiles" << endl;
+    TilePrintFormat t;
+    if(globals.mode!=TILES) return;
+    char token[255];
+    if(fscanf(globals.fp[globals.index],"%s",token)!=1)
+    {
+        printf("num_tiles token expected\n");
+        exit(1);
+    }
+    if(strcmp(token,"num_tiles")!=0)
+    {
+        printf("num_tiles token expected\n");
+        exit(1);
+    }
+    if(fscanf(globals.fp[globals.index],"%d",&globals.num_tiles)!=1)
+    {
+        printf("num_tiles expected\n");
+        exit(1);
+    }
+    if(globals.numFiles==1 || (globals.numFiles>1 && globals.index==1))
+        fprintf(globals.ofp,"num_tiles %d\n",globals.num_tiles);
+    cout << "now reading " << globals.num_tiles << " tiles " << endl;
+    eatWhitespace(globals.fp[globals.index]);
+    for(int i=0; i<globals.num_tiles; i++)
+    {
+        fread(&t, sizeof(TilePrintFormat), 1, globals.fp[globals.index]);
+        if(globals.numFiles==1 || (globals.numFiles>1 && globals.index==1))
+            fwrite(&t, sizeof(TilePrintFormat), 1, globals.ofp);
+ //       printTile(t);
+//     displayTile(t, globals.width, globals.height);
+    }
+    glFinish();
+    printf("done parsing tiles\n");
+    //getchar();
+}
+
+int parseSamples()
+{
+    char token[255];
+    SamplePrintFormat s;
+    if(globals.mode!=SAMPLES) return 0;
+    int samples_to_read;
+    if(fscanf(globals.fp[globals.index],"%s",token)!=1)
+    {
+        printf("num_samples expected\n");
+        exit(1);
+    }
+    if(strcmp(token,"num_samples")!=0)
+    {
+        printf("num_samples expected, not %s\n", token);
+        exit(1);
+    }
+    if(fscanf(globals.fp[globals.index],"%d",&samples_to_read)!=1)
+    {
+        printf("#samples expected\n");
+        exit(1);
+    }
+    printf("samples to read = %d\n", samples_to_read);
+    eatWhitespace(globals.fp[globals.index]);
+    for(int i=0; i<samples_to_read; i++)
+    {
+        if(fread(&s,sizeof(SamplePrintFormat), 1, 
globals.fp[globals.index])!=1)
+        {
+            printf("valid sample expected\n");
+            exit(1);
+        }
+        fwrite(&s, sizeof(SamplePrintFormat), 1, globals.ofp);
+        globals.total_samples_parsed++;
+//        printSample(s);
+        displaySample(s);
+    }
+   chunk_count++;
+   cout << "parsed chunk " << chunk_count << endl;
+   //getchar();
+   return samples_to_read;
+}
+
+// Glut idle callback
+void idle()
+{
+    char token[255];
+    for(globals.index=0; globals.index<globals.numFiles; globals.index++)
+    {
+        if(feof(globals.fp[globals.index]))
+        {
+            printf("total samples parsed = 
%d\n",globals.total_samples_parsed);
+            exit(0);
+        }
+    }
+    switch(globals.mode)
+    {
+        case SAMPLES:
+            {
+            for(globals.index=0; globals.index<globals.numFiles; 
globals.index++)
+           {
+                eatWhitespace(globals.fp[globals.index]);
+                if(fscanf(globals.fp[globals.index],"%s",token)!=1)
+                {
+                    printf("begin_tiles expected\n");
+                    exit(1);
+                }
+                if(strcmp(token,"begin_tiles")!=0)
+                {
+                    printf("begin_tiles expected, not %s\n", token);
+                    exit(1);
+                }
+                cout << "read begin_tiles" << endl;
+                eatWhitespace(globals.fp[globals.index]);
+           }
+                globals.mode=TILES;
+            }
+            break;
+        case TILES:
+            {
+            for(globals.index=0; globals.index<globals.numFiles; 
globals.index++)
+           {
+                eatWhitespace(globals.fp[globals.index]);
+                if(fscanf(globals.fp[globals.index],"%s",token)!=1)
+                {
+                    printf("begin_camera expected\n");
+                    exit(1);
+                }
+                if(strcmp(token,"begin_camera")!=0)
+                {
+                    printf("begin_camera expected, read %s\n", token);
+                    exit(1);
+                }
+                cout << "read begin_camera" << endl;
+           }
+                globals.mode=CAMERA;
+            }
+            break;
+        case CAMERA:
+            {
+            for(globals.index=0; globals.index<globals.numFiles; 
globals.index++)
+           {
+                eatWhitespace(globals.fp[globals.index]);
+                if(fscanf(globals.fp[globals.index],"%s",token)!=1)
+                {
+                    printf("unexpected token or end of file\n");
+                    exit(1);
+                }
+                if(strcmp(token, "begin_samples")!=0)
+                {
+                    printf("begin_samples expected, read %s\n", token);
+                    exit(1);
+                }
+                cout << "read begin_samples" << endl;
+                eatWhitespace(globals.fp[globals.index]);
+           }
+                globals.mode=SAMPLES;
+            }   
+            break;
+    };
+    glutPostRedisplay();
+}
+
+// Glut Display callback
+void display()
+{
+    idle();
+    for(globals.index=0; globals.index<globals.numFiles; globals.index++)
+    {
+    switch(globals.mode)
+    {
+        case SAMPLES:
+       {
+           if(globals.index==0)
+           {
+               fprintf(globals.ofp,"\n\nbegin_samples\n");
+               fprintf(globals.ofp,"num_samples ");
+               fgetpos(globals.ofp, &globals.numsamplespos);
+               fprintf(globals.ofp,"                                         
      \n");
+           }
+         
+            globals.samplesThisChunk += parseSamples();
+            break;
+       }
+        case TILES:
+       {
+           if(globals.numFiles==1 || (globals.numFiles>1 && 
globals.index==1))
+           {
+                   fpos_t tpos;
+                   fgetpos(globals.ofp, &tpos);
+                   fsetpos(globals.ofp,&globals.numsamplespos);
+                   fprintf(globals.ofp,"%d",globals.samplesThisChunk);
+                   globals.samplesThisChunk = 0;
+                   fsetpos(globals.ofp, &tpos);
+                   globals.samplesThisChunk=0;
+               fprintf(globals.ofp,"\n\nbegin_tiles\n");
+           }
+            parseTiles();
+            break;
+       }
+        case CAMERA:
+            globals.cam.read();
+           if(globals.numFiles==1 || (globals.numFiles>1 && 
globals.index==1))
+           {
+               fprintf(globals.ofp,"\n\nbegin_camera\n");
+               globals.cam.writeToFile();
+           }
+            break;
+    };
+    }
+}
+
+//----------------------------------------------------------------------------
+// OpenGL RESHAPE routine (NOTE: resets to original VIEWING position).
+//----------------------------------------------------------------------------
+void myReshape(int w, int h)
+{
+    glViewport(0, 0, w, h);  // LAST STAGE IN THE PIPE (AFTER PROJ AND 
MODELVIEW)
+    glMatrixMode(GL_PROJECTION);
+    glLoadIdentity();
+    gluOrtho2D(0, w, 0, h);
+    glMatrixMode(GL_MODELVIEW);
+    glLoadIdentity();
+}
+
+void init()
+{
+    char token[255], line[1024], ch;
+    do
+    {
+        ch = getc(globals.fp[globals.index]);
+        while(ch=='#' || ch=='%') //parse comment
+        {
+            fgets(line,1024,globals.fp[globals.index]);
+            printf("%s",line);
+           if(globals.numFiles==1 || (globals.numFiles>1 && 
globals.index==1))
+               fprintf(globals.ofp,"%c%s\n",ch,line);
+            ch = getc(globals.fp[globals.index]);
+        }
+        ungetc(ch, globals.fp[globals.index]);
+        
+        if(fscanf(globals.fp[globals.index],"%s",token)!=1)
+        {
+            printf("some token expected\n");
+            exit(1);
+        }   
+        
+       if(globals.numFiles==1 || (globals.numFiles>1 && globals.index==1))
+           fprintf(globals.ofp,"%s ",token);
+        if(strcmp(token,"Created")==0
+           || strcmp(token,"frame_rate")==0)
+        {
+            fgets(line,1024,globals.fp[globals.index]);
+            printf("%s: %s",token, line);
+           if(globals.numFiles==1 || (globals.numFiles>1 && 
globals.index==1))
+               fprintf(globals.ofp,"%s\n",line);
+        }
+        else if(strcmp(token,"Version")==0)
+        {
+            char version[10];
+            if (fscanf(globals.fp[globals.index],"%s",version)!=1)
+            {
+                printf("version expected\n");
+                exit(1);
+            }
+           if(globals.numFiles==1 || (globals.numFiles>1 && 
globals.index==1))
+               fprintf(globals.ofp,"%s\n",version);
+            if(strcmp(version,STREAM_VERSION)!=0)
+            {
+                printf("unsupported file version %s, required version %s\n", 
version, STREAM_VERSION);
+                exit(1);
+            }
+        }
+        else if(strcmp(token,"width")==0)
+        {
+            fscanf(globals.fp[globals.index],"%d",&globals.width);
+           if(globals.numFiles==1 || (globals.numFiles>1 && 
globals.index==1))
+               fprintf(globals.ofp,"%d\n",globals.width);
+        }
+        else if(strcmp(token,"height")==0)
+        {
+            fscanf(globals.fp[globals.index],"%d",&globals.height);
+           if(globals.numFiles==1 || (globals.numFiles>1 && 
globals.index==1))
+               fprintf(globals.ofp,"%d\n",globals.height);
+        }
+        eatWhitespace(globals.fp[globals.index]);
+    }
+    while(!feof(globals.fp[globals.index]) && strcmp(token,"END")!=0);
+    if(globals.numFiles==1 || (globals.numFiles>1 && globals.index==1))
+        fprintf(globals.ofp,"\n\n\nbegin_tiles\n");
+    if(feof(globals.fp[globals.index]))
+    {
+        printf("unexpected end of file\n");
+        exit(1);
+    }
+    if(globals.width==0 || globals.height==0)
+    {
+        printf("width/height not found\n");
+        exit(1);
+    }
+    eatWhitespace(globals.fp[globals.index]);
+    glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
+    if(fscanf(globals.fp[globals.index],"%s",token)!=1)
+    {
+        printf("begin_tiles expected\n");
+        exit(1);
+    }
+    if(strcmp(token,"begin_tiles")!=0)
+    {
+        printf("begin_tiles expected\n");
+        exit(1);
+    }
+    globals.mode=TILES;
+    parseTiles();
+}
+
+int main(int ac,char **av)
+{
+    if(ac!=3)
+    {
+        cout << "syntax: combinestream <filenameprefix> <number of files>" 
<< endl;
+        exit(1);
+    }
+  
+    globals.numFiles = atoi(av[2]);
+    cout << "number of files = " << globals.numFiles << endl;
+    glutInit(&ac, av);
+    glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE);
+    glutInitWindowPosition(100,100);    
+    globals.fp = (FILE**)malloc(globals.numFiles*sizeof(FILE*));
+    globals.firsttime = 1;
+    char fname[100];
+    sprintf(fname,"final%s.tfr",av[1]);
+    if((globals.ofp=fopen(fname, "wb"))==NULL)
+    {
+       cout << "cannot open to write " << fname << endl;
+               exit(1);
+    }
+    for(globals.index=0; globals.index<globals.numFiles; globals.index++)
+    {
+       sprintf(fname,"%s%03d",av[1],globals.index);
+       if((globals.fp[globals.index]=fopen(fname, "rb"))==NULL)
+       {
+               cout << "cannot read " << fname << endl;
+               exit(1);
+       }
+        init();
+    }
+
+    glutInitWindowSize(globals.width,globals.height);
+    char windowname[255];
+    sprintf(windowname,"Sample Stream %3.1f viewer", STREAM_VERSION);
+    glutCreateWindow(windowname);
+    glutDisplayFunc(display);
+    glutReshapeFunc(myReshape);
+    //glutIdleFunc(idle);
+
+    printf("press return key to continue\n");
+    getchar();
+
+    glClear(GL_COLOR_BUFFER_BIT);
+
+    glutMainLoop();
+
+    return 0;
+}
+

Added: branches/AFR/streamtest.cpp
==============================================================================
--- (empty file)
+++ branches/AFR/streamtest.cpp Mon Sep  5 10:30:07 2005
@@ -0,0 +1,701 @@
+/***************************************************************************
+                          parsestream.cpp  -  description
+                             -------------------
+    begin                : Mon Nov 22 2004
+    copyright            : (C) 2004 by Abhinav Dayal
+    email                : abhinav@cs.northwestern.edu
+ ***************************************************************************/
+
+// parse stream format 1.0
+#include <GL/glut.h>
+#include <stdlib.h>
+#include <iostream>
+#include <stdio.h>
+#include <time.h>
+#include <stdio.h>
+
+using namespace std;
+#define STREAM_VERSION "1.3"
+
+
+int chunk_count=0;
+typedef float Matrix_4x4[4][4];
+enum Mode {SAMPLES, TILES, CAMERA};
+class Camera
+{
+    public:
+        float nearplane, farplane, orig[3], U[3], V[3], N[3], wL, wR, wB, wT;
+        void read_from_file(FILE *fp);
+        void getCamCoordinate(float c[3], float worldCoord[3]);
+        void getProjectionMatrix(Matrix_4x4 &m);
+};
+
+class Globals
+{
+    public:
+        int total_samples_to_parse, total_samples_parsed;
+        float total_time_to_parse;
+        int width, height;
+        int num_tiles;
+        Mode mode;
+        FILE *fp;
+        Camera cam;
+        Matrix_4x4 cameramat;
+        Globals()
+        {
+            total_samples_to_parse=0;
+            total_samples_parsed = 0;
+            total_time_to_parse=0.0;
+            width = height = 0;
+            mode=CAMERA;
+            num_tiles = 0;
+            fp=NULL;
+        }
+};
+
+struct SamplePrintFormat
+{
+    float worldCoord[3], vel[3];
+    unsigned char rgb[3];
+    float timestamp;
+};
+
+struct TilePrintFormat
+{
+    int index;
+    float xgrad, ygrad, tgrad;
+    float oldest_sample_timestamp;
+};
+
+Globals globals;
+
+void eatWhitespace(FILE *fp)
+{
+   char ch=getc(fp);
+   while(ch==' ' || ch=='\t' || ch=='\n' || ch=='\f' || ch=='\r')
+      ch=getc(fp);
+   ungetc(ch,fp);
+}
+void Camera::read_from_file(FILE *fp)
+{
+    char token[255], ch;
+
+    if(fscanf(fp,"%s",token)!=1 && strcmp(token,"origin")!=0)
+    {
+        printf("origin expected\n");
+        exit(1);
+    }
+    if(fscanf(fp,"%f%f%f",&orig[0],&orig[1],&orig[2])!=3)
+    {
+        printf("can't read camera origin\n");
+        exit(1);
+    }
+    eatWhitespace(fp);
+    if((ch=fgetc(fp))!='U')
+    {
+        printf("U vector expected\n");
+        exit(1);
+    }
+    if(fscanf(fp,"%f%f%f",&U[0],&U[1],&U[2])!=3)
+    {
+        printf("can't read U vector\n");
+        exit(1);
+    }
+    eatWhitespace(fp);
+    if((ch=fgetc(fp))!='V')
+    {
+        printf("V vector expected\n");
+        exit(1);
+    }
+    if(fscanf(fp,"%f%f%f",&V[0],&V[1],&V[2])!=3)
+    {
+        printf("can't read V vector\n");
+        exit(1);
+    }
+    eatWhitespace(fp);
+    if((ch=fgetc(fp))!='N')
+    {
+        printf("N vector expected\n");
+        exit(1);
+    }
+    if(fscanf(fp,"%f%f%f",&N[0],&N[1],&N[2])!=3)
+    {
+    }
+    eatWhitespace(fp);
+    if(fscanf(fp,"%s",token)!=1 && strcmp(token,"extents")!=0)
+    {
+        printf("extents expected\n");
+        exit(1);
+    }
+    if(fscanf(fp,"%f%f%f%f",&wL,&wR,&wB,&wT)!=4)
+    {
+        printf("can't read camera image plane extents\n");
+        exit(1);
+    }
+    eatWhitespace(fp);
+    if(fscanf(fp,"%s",token)!=1 && strcmp(token,"near")!=0)
+    {
+        printf("near expected\n");
+        exit(1);
+    }
+    if(fscanf(fp,"%f",&nearplane)!=1)
+    {
+        printf("can't read near plane distance\n");
+        exit(1);
+    }
+    eatWhitespace(fp);
+    if(fscanf(fp,"%s",token)!=1 && strcmp(token,"far")!=0)
+    {
+        printf("far expected\n");
+        exit(1);
+    }
+    if(fscanf(fp,"%f",&farplane)!=1)
+    {
+        printf("can't read far plane distance\n");
+        exit(1);
+    }
+    
+    /*printf("read camera: O(%f, %f, %f); U(%f, %f, %f); V(%f, %f, %f); 
N(%f, %f, %f); extents((%f, %f, %f, %f); near=%f, far=%f\n",
+           orig[0], orig[1], orig[2], U[0], U[1], U[2], V[0], V[1], V[2], 
N[0], N[1], N[2],
+           wL, wR, wB, wT, nearplane, farplane);*/
+    getProjectionMatrix(globals.cameramat);
+}
+
+void Camera::getCamCoordinate(float c[3], float worldCoord[3])
+{
+  float wP[3];
+  wP[0] = worldCoord[0]-orig[0];
+  wP[1] = worldCoord[1]-orig[1];
+  wP[2] = worldCoord[2]-orig[2];
+  
+  c[0] = U[0]*wP[0] + U[1]*wP[1] + U[2]*wP[2];
+  c[1] = V[0]*wP[0] + V[1]*wP[1] + V[2]*wP[2];
+  c[2] = N[0]*wP[0] + N[1]*wP[1] + N[2]*wP[2];
+}
+
+
+// m3 = m1 x m2
+void multMatrix(Matrix_4x4 &m1, Matrix_4x4 &m2, Matrix_4x4 &m3)
+{
+    int i,j,k;
+    
+    for(i=0; i<4; i++)
+        for(j=0; j<4; j++)
+        {
+            m3[i][j] = 0;
+            for(k=0; k<4; k++)
+                m3[i][j] +=  m1[i][k]*m2[k][j];
+        }
+}
+
+void setIdentity(Matrix_4x4 &m)
+{
+    int i,j;
+    for(i=0; i<4; i++)
+        for(j=0; j<4; j++)
+            m[i][j] = (i==j);
+}
+
+void printMatrix(Matrix_4x4 &m)
+{
+    int i,j;
+    for(i=0; i<4; i++)
+    {
+        for(j=0; j<4; j++)
+            printf("%f  ",m[i][j]);
+        printf("\n");
+    }
+}
+
+void Camera::getProjectionMatrix(Matrix_4x4 &m)
+{
+    
+    // form the viewmatrix and projection matrices
+    // now view matrix is
+    // R =  [X | Y | Z | 1]
+    // T = translate by [-camX, -camY, -camZ]
+    // view matrix = RT
+    
+    // Note: after coding this we will have to write a test routine that 
reads the samples and 
+    // using the camera projects them on to the screen.
+    Matrix_4x4 S,T,R,view,projection,viewport,temp;
+    int i;
+    setIdentity(T);
+    T[0][3] = -orig[0];
+    T[1][3] = -orig[1];
+    T[2][3] = -orig[2];
+    
+    setIdentity(R);
+    for(i=0;i<3; i++)
+    {
+        R[0][i] = U[i];
+        R[1][i] = V[i];
+        R[2][i] = N[i];
+    }
+    multMatrix(R,T,view);
+
+    //printf("view matrix:\n");
+    //printMatrix(view);  
+    // now we set the projection matrix
+    // projection matrix is the perspective projection matrix
+    // [ from OpenGL standards ]
+    projection[0][0] = 2.0*nearplane/(wR-wL);
+    projection[0][1] = 0.0;
+    projection[0][2] = (wR+wL)/(wR-wL);
+    projection[0][3] = 0.0;
+    
+    projection[1][0] = 0.0;
+    projection[1][1] = 2.0*nearplane/(wT-wB);
+    projection[1][2] = (wT+wB)/(wT-wB);
+    projection[1][3] = 0.0;
+    
+    projection[2][0] = 0.0;
+    projection[2][1] = 0.0;
+    projection[2][2] = -(farplane+nearplane)/(farplane-nearplane);
+    projection[2][3] = -2.0*farplane*nearplane/(farplane-nearplane);
+    
+    projection[3][0] = 0.0;
+    projection[3][1] = 0.0;
+    projection[3][2] = -1.0;
+    projection[3][3] = 0.0;
+    
+    //printf("projection matrix:\n");
+    //printMatrix(projection);
+    
+    setIdentity(T);
+    T[0][3] = (float)globals.width/2.0;
+    T[1][3] = (float)globals.height/2.0;
+    setIdentity(S);
+    S[0][0] = -(float)globals.width/2.0;//(wR-wL);
+    S[1][1] = -(float)globals.height/2.0;//(wT-wB);
+    multMatrix(T,S,viewport);
+
+    //printf("viewport matrix:\n");
+    //printMatrix(viewport);
+    
+    multMatrix(viewport,projection,temp);
+    multMatrix(temp,view,m);
+    
+    //printf("camera matrix:\n");
+    //printMatrix(m);
+}
+
+
+void printSample(SamplePrintFormat &s)
+{
+    printf("Timestamp: %f; 3D coordinate (%f, %f, %f); ", s.timestamp, 
s.worldCoord[0], s.worldCoord[1], s.worldCoord[2]);
+    printf("Color (%d, %d, %d); ", s.rgb[0], s.rgb[1], s.rgb[2]);
+}
+
+void printTile(TilePrintFormat &t)
+{
+    printf("tile index: %d; gradients (%f, %f, %f)\n", t.index, t.xgrad, 
t.ygrad, t.tgrad);
+}
+
+void displaySample(SamplePrintFormat &s)
+{
+    glBegin(GL_POINTS);
+    // define the sample color
+    glColor3f((float)s.rgb[0]/255.0, (float)s.rgb[1]/255.0, 
(float)s.rgb[2]/255.0);
+    float p[4];
+    int i;
+    for(i=0; i<4; i++)
+    {
+        p[i] = s.worldCoord[0]*globals.cameramat[i][0] + 
s.worldCoord[1]*globals.cameramat[i][1]
+               + s.worldCoord[2]*globals.cameramat[i][2] + 
globals.cameramat[i][3];
+    }
+    float znd = p[2]/p[3];
+    //if (znd >= 0.0f && znd <= 1.0f) {
+    //    printf("\n(%f, %f, %f) ---> (%f, %f, %f)\n", s.worldCoord[0], 
s.worldCoord[1], s.worldCoord[2], p[0]/p[3], p[1]/p[3], p[2]/p[3]);
+    //}
+    //else {
+    //    printf("#");
+    //}
+    glVertex2f(p[0]/p[3], p[1]/p[3]);
+    glEnd();
+}
+
+void displayTile(TilePrintFormat &t, int width, int height)
+{
+    // based om index find the tile dimensions
+    // find the tile level, this would give us its dimension
+    int tilelevel = 0;  // tile lies at what tree level; 0 = root
+    int pos=0, count = t.index;
+    int beginindex=1;   // index that starts at tilelevel
+    int xfac=1; // numtiles along x
+    int yfac=1; // numtiles along y
+    int xbegin=0, ybegin=0;
+    while(count!=1) // while not hit the root; note index=1 for root
+    {
+        beginindex*=2;
+        count /= 2; // decrement count to go to previous level
+        tilelevel++;    // increment tilelevel
+        if(tilelevel%2==0) yfac*=2;
+        else xfac*=2;
+    }   
+    // find pos, i.e. the position of tile on this level, i.e. tilelevel
+    //cout << "tilelevel = " << tilelevel << ", xfac = " << xfac << ", yfac 
= " << yfac 
+    //    << "beginindex = " << beginindex << endl;
+
+    // this is kind of nasty -- the correct tile positions
+    // are swizzled (the bits are interleaved xyxyxyxy or yxyxyxyx
+    // depending on which tilelevel it is).
+    // the following loop unswizzles them.
+    pos = t.index - beginindex;
+    count = 0;
+    while (pos > 0) {
+        if (count%2 == tilelevel%2) {
+            ybegin += (pos&0x1)<<(count>>1);
+        }
+        else {
+            xbegin += (pos&0x1)<<(count>>1);
+        }
+        pos >>= 1;
+        count++;
+    }
+    // then scale the x,y location to the dimensions
+    // of the tiles at that tilelevel
+    int xdim = width/xfac;  // dimension of tile along x = 
screenwidth/numtiles_along_x
+    int ydim = height/yfac; // dimension of tile along y = 
screenheight/numtiles_along_y
+    xbegin *= xdim;
+    ybegin *= ydim;
+    //cout << "xdim = " << xdim << ", ydim = " << ydim << ", xbegin = " << 
xbegin << ", ybegin = " << ybegin << endl;
+    //cout << "dimensions: (" << xbegin << ", " << ybegin << ") -- (" << 
xbegin+xdim << ", " << ybegin+ydim << ")" << endl;
+    // display the tile
+
+#if 0
+    glBegin(GL_LINES);
+    glColor3f(1.0,1.0,0.0);
+    
+    glVertex2d(xbegin, ybegin);
+    glVertex2d(xbegin+xdim, ybegin);
+    
+    glVertex2d(xbegin+xdim, ybegin);
+    glVertex2d(xbegin+xdim, ybegin+ydim);
+    
+    glVertex2d(xbegin+xdim, ybegin+ydim);
+    glVertex2d(xbegin, ybegin+ydim);
+    
+    glVertex2d(xbegin, ybegin+ydim);
+    glVertex2d(xbegin, ybegin);
+    
+    glEnd();
+#else
+    glColor3f((float)rand()/(float)RAND_MAX+0.4f,
+              (float)rand()/(float)RAND_MAX+0.4f,
+              (float)rand()/(float)RAND_MAX+0.4f);
+
+    glBegin(GL_QUADS);
+    glVertex2d(xbegin, ybegin);
+    glVertex2d(xbegin+xdim, ybegin);
+    glVertex2d(xbegin+xdim, ybegin+ydim);
+    glVertex2d(xbegin, ybegin+ydim);
+    glEnd();
+#endif
+
+}
+
+void readMatrix(Matrix_4x4 &m)
+{
+    int i, j;
+    for(i=0; i<4; i++)
+        for(j=0; j<4; j++)
+        {
+            if(fscanf(globals.fp,"%f",&m[i][j])!=1)
+            {
+                printf("matrix element %d,%d not found\n", i, j);
+                exit(1);
+            }
+        }
+    eatWhitespace(globals.fp);
+}
+
+void parseTiles()
+{
+    //glClear(GL_COLOR_BUFFER_BIT);
+    cout << "inside parseTiles" << endl;
+    TilePrintFormat t;
+    if(globals.mode!=TILES) return;
+    char token[255];
+    if(fscanf(globals.fp,"%s",token)!=1)
+    {
+        printf("num_tiles token expected\n");
+        exit(1);
+    }
+    if(strcmp(token,"num_tiles")!=0)
+    {
+        printf("num_tiles token expected\n");
+        exit(1);
+    }
+    if(fscanf(globals.fp,"%d",&globals.num_tiles)!=1)
+    {
+        printf("num_tiles expected\n");
+        exit(1);
+    }
+    cout << "now reading " << globals.num_tiles << " tiles " << endl;
+    eatWhitespace(globals.fp);
+    for(int i=0; i<globals.num_tiles; i++)
+    {
+        fread(&t, sizeof(TilePrintFormat), 1, globals.fp);
+ //       printTile(t);
+//     displayTile(t, globals.width, globals.height);
+    }
+    glFinish();
+    printf("done parsing tiles\n");
+    //getchar();
+}
+
+void parseSamples()
+{
+    char token[255];
+    SamplePrintFormat s;
+    if(globals.mode!=SAMPLES) return;
+    int samples_to_read;
+    if(fscanf(globals.fp,"%s",token)!=1)
+    {
+        printf("num_samples expected\n");
+        exit(1);
+    }
+    if(strcmp(token,"num_samples")!=0)
+    {
+        printf("num_samples expected, not %s\n", token);
+        exit(1);
+    }
+    if(fscanf(globals.fp,"%d",&samples_to_read)!=1)
+    {
+        printf("#samples expected\n");
+        exit(1);
+    }
+    printf("samples to read = %d\n", samples_to_read);
+    eatWhitespace(globals.fp);
+    for(int i=0; i<samples_to_read; i++)
+    {
+        if(fread(&s,sizeof(SamplePrintFormat), 1, globals.fp)!=1)
+        {
+            printf("valid sample expected\n");
+            exit(1);
+        }
+        globals.total_samples_parsed++;
+//        printSample(s);
+        displaySample(s);
+    }
+   chunk_count++;
+   cout << "parsed chunk " << chunk_count << endl;
+   //getchar();
+}
+
+// Glut idle callback
+void idle()
+{
+    char token[255];
+    if(feof(globals.fp))
+    {
+        printf("total samples parsed = %d\n",globals.total_samples_parsed);
+        exit(0);
+    }
+    switch(globals.mode)
+    {
+        case SAMPLES:
+            {
+                eatWhitespace(globals.fp);
+                if(fscanf(globals.fp,"%s",token)!=1)
+                {
+                    printf("begin_tiles expected\n");
+                    exit(1);
+                }
+                if(strcmp(token,"begin_tiles")!=0)
+                {
+                    printf("begin_tiles expected, not %s\n", token);
+                    exit(1);
+                }
+                cout << "read begin_tiles" << endl;
+                globals.mode=TILES;
+                eatWhitespace(globals.fp);
+            }
+            break;
+        case TILES:
+            {
+                eatWhitespace(globals.fp);
+                if(fscanf(globals.fp,"%s",token)!=1)
+                {
+                    printf("begin_camera expected\n");
+                    exit(1);
+                }
+                if(strcmp(token,"begin_camera")!=0)
+                {
+                    printf("begin_camera expected, read %s\n", token);
+                    exit(1);
+                }
+                cout << "read begin_camera" << endl;
+                globals.mode=CAMERA;
+            }
+            break;
+        case CAMERA:
+            {
+                eatWhitespace(globals.fp);
+                if(fscanf(globals.fp,"%s",token)!=1)
+                {
+                    printf("unexpected token or end of file\n");
+                    exit(1);
+                }
+                if(strcmp(token, "begin_samples")!=0)
+                {
+                    printf("begin_samples expected, read %s\n", token);
+                    exit(1);
+                }
+                cout << "read begin_samples" << endl;
+                eatWhitespace(globals.fp);
+                globals.mode=SAMPLES;
+            }   
+            break;
+    };
+    
+    glutPostRedisplay();
+}
+
+// Glut Display callback
+void display()
+{
+    idle();
+    switch(globals.mode)
+    {
+        case SAMPLES:
+            parseSamples();
+            break;
+        case TILES:
+            parseTiles();
+            break;
+        case CAMERA:
+            globals.cam.read_from_file(globals.fp);
+            break;
+    };
+}
+
+//----------------------------------------------------------------------------
+// OpenGL RESHAPE routine (NOTE: resets to original VIEWING position).
+//----------------------------------------------------------------------------
+void myReshape(int w, int h)
+{
+    glViewport(0, 0, w, h);  // LAST STAGE IN THE PIPE (AFTER PROJ AND 
MODELVIEW)
+    glMatrixMode(GL_PROJECTION);
+    glLoadIdentity();
+    gluOrtho2D(0, w, 0, h);
+    glMatrixMode(GL_MODELVIEW);
+    glLoadIdentity();
+}
+
+void init()
+{
+    char token[255], line[1024], ch;
+    do
+    {
+        ch = getc(globals.fp);
+        while(ch=='#' || ch=='%') //parse comment
+        {
+            fgets(line,1024,globals.fp);
+            printf("%s",line);
+            ch = getc(globals.fp);
+        }
+        ungetc(ch, globals.fp);
+        
+        if(fscanf(globals.fp,"%s",token)!=1)
+        {
+            printf("some token expected\n");
+            exit(1);
+        }   
+        
+        if(strcmp(token,"Created")==0
+           || strcmp(token,"frame_rate")==0)
+        {
+            fgets(line,1024,globals.fp);
+            printf("%s: %s",token, line);
+        }
+        else if(strcmp(token,"Version")==0)
+        {
+            char version[10];
+            if (fscanf(globals.fp,"%s",version)!=1)
+            {
+                printf("version expected\n");
+                exit(1);
+            }
+            if(strcmp(version,STREAM_VERSION)!=0)
+            {
+                printf("unsupported file version %s, required version %s\n", 
version, STREAM_VERSION);
+                exit(1);
+            }
+        }
+        else if(strcmp(token,"width")==0)
+        {
+            fscanf(globals.fp,"%d",&globals.width);
+        }
+        else if(strcmp(token,"height")==0)
+        {
+            fscanf(globals.fp,"%d",&globals.height);
+        }
+        eatWhitespace(globals.fp);
+    }
+    while(!feof(globals.fp) && strcmp(token,"END")!=0);
+    if(feof(globals.fp))
+    {
+        printf("unexpected end of file\n");
+        exit(1);
+    }
+    if(globals.width==0 || globals.height==0)
+    {
+        printf("width/height not found\n");
+        exit(1);
+    }
+    eatWhitespace(globals.fp);
+    glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
+    if(fscanf(globals.fp,"%s",token)!=1)
+    {
+        printf("begin_tiles expected\n");
+        exit(1);
+    }
+    if(strcmp(token,"begin_tiles")!=0)
+    {
+        printf("begin_tiles expected\n");
+        exit(1);
+    }
+    globals.mode=TILES;
+    //globals.cam.read_from_file(globals.fp);
+    parseTiles();
+}
+
+int main(int ac,char **av)
+{
+    if(ac!=2)
+    {
+        cout << "syntax: parsestream <filename> " << endl;
+        exit(1);
+    }
+  
+    glutInit(&ac, av);
+    glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE);
+    glutInitWindowPosition(100,100);    
+    
+    if((globals.fp=fopen(av[1], "rb"))==NULL)
+    {
+        cout << "cannot read " << av[1] << endl;
+        exit(1);
+    }
+
+    init();
+    glutInitWindowSize(globals.width,globals.height);
+    char windowname[255];
+    sprintf(windowname,"Sample Stream %3.1f viewer", STREAM_VERSION);
+    glutCreateWindow(windowname);
+    glutDisplayFunc(display);
+    glutReshapeFunc(myReshape);
+    //glutIdleFunc(idle);
+
+    printf("press return key to continue\n");
+    //getchar();
+
+    glClear(GL_COLOR_BUFFER_BIT);
+
+    glutMainLoop();
+
+    return 0;
+}
+




  • [MANTA] r530 - in branches/AFR: . Engine/ImageTraversers Engine/ImageTraversers/AFR Model/Cameras, abhinav, 09/05/2005

Archive powered by MHonArc 2.6.16.

Top of page