rtVTK  0.6.0
RayLogger.h
00001 
00002 /******************************************************************************
00003  * Copyright (c) 2012, Grove City College
00004  * All rights reserved.
00005  *
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions are
00008  * met:
00009  * 
00010  *  * Redistributions of source code must retain the above copyright
00011  *    notice, this list of conditions and the following disclaimer.
00012  *
00013  *  * Redistributions in binary form must reproduce the above copyright
00014  *    notice, this list of conditions and the following disclaimer in
00015  *    the documentation and/or other materials provided with the
00016  *    distribution.
00017  *
00018  *  * Neither the name of Grove City College nor the names of its
00019  *    contributors may be used to endorse or promote products derived
00020  *    from this software without specific prior written permission.
00021  *
00022  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00023  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00024  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00025  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00026  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00027  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00028  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00029  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00030  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00031  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00032  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00033  *
00034  */
00035 
00036 
00042 #ifndef RL_RayLogger_h
00043 #define RL_RayLogger_h
00044 
00045 #include <fstream>
00046 using std::fstream;
00047 
00048 #include <iosfwd>
00049 using std::istream;
00050 using std::ostream;
00051 
00052 #include <string>
00053 using std::string;
00054 
00055 #include <vector>
00056 using std::vector;
00057 
00058 #include <Core/ioCore/BufferedReader.t>
00059 #include <Core/ioCore/BufferedWriter.t>
00060 
00061 #include <Math/Math.h>
00062 
00063 #include <RL/RayRecord.h>
00064 #include <RL/TreeRecord.h>
00065 #include <RL/rl.h>
00066 
00067 
00068 namespace rl
00069 {
00070 
00072   // Using declarations
00073 
00074   using ioCore::BufferedReader;
00075   using ioCore::BufferedWriter;
00076 
00077   using MathF::Point;
00078   using MathF::Vector;
00079 
00080 
00082   // Type definitions
00083 
00084   typedef vector<RayRecord>::iterator  RRiter;
00085   typedef vector<TreeRecord>::iterator TRiter;
00086 
00087 
00089   // Enumerated types
00090 
00092   enum IOMode
00093     {
00094       InvalidMode = -1, 
00095       ReadMode = 0,     
00096       WriteMode,        
00097       ImmediateMode     
00098     };
00099 
00101   enum TraversalMode
00102     {
00103       BreadthFirst = 0, 
00104       DepthFirst        
00105     };
00106 
00107 
00109   // Class definitions
00110 
00118   class RayLogger
00119   {
00120   public:
00121 
00123     // Default constructor
00124 
00128     RayLogger() :
00129       iomode(InvalidMode),
00130       imode(Uninited),
00131       ctree(-1),
00132       bfTreeReader(ftrees),
00133       bfRecordReader(frecords),
00134       bfTreeWriter(ftrees),
00135       bfRecordWriter(frecords)
00136     {
00137       // no-op
00138     }
00139 
00140 
00142     // Setup/teardown
00154     rl::Error init(IOMode iomode, TraversalMode tmode, string fname);
00155 
00157     void close();
00159 
00160 
00162     // Breadth-first tree traversal
00172     rl::Error nextRay();
00174 
00175 
00177     // Depth-first tree traversal
00184 
00185     rl::Error descendTree();
00186 
00188     rl::Error ascendTree();
00190 
00191 
00193     // Forest traversal
00200 
00201     rl::Error nextTree();
00202 
00204     rl::Error prevTree();
00206 
00207  
00209     // Read-mode functions
00216 
00217     uint    numTrees();
00218 
00222     uint    rayStats(RayType type);
00223 
00225     uint    numRays();
00226 
00228     uint    maxDepth();
00229 
00233     rl::Error readTree(Tree& tree);
00234 
00236     rl::Error readRays();
00237 
00241     rl::Error populateTree(Tree& tree);
00242 
00246     rl::Error readRay(Ray& ray);
00247 
00249     rl::Error startRays();
00251 
00252 
00254     // Write-mode functions
00261 
00262     rl::Error beginForest();
00263 
00265     rl::Error endForest();
00266 
00268     rl::Error beginTree(ushort px, ushort py);
00269 
00271     rl::Error endTree();
00272 
00281     rl::Error addRay(const Point&,
00282                      const Vector&,
00283                      const float,
00284                      const RayType,
00285                      const void*  = NULL,
00286                      const size_t = 0);
00288 
00289 
00291     // Error functions
00299 
00300     rl::Error getError();
00301 
00303     string  getErrorString();
00305 
00307     IOMode getMode() const { return iomode; }
00308 
00309   private:
00311     // Modes
00312 
00314     IOMode iomode;
00315 
00317     TraversalMode tmode;
00318 
00323     enum iMode
00324       {
00325         Uninited, 
00326         Read,     
00327         Write     
00328       };
00329 
00331     iMode imode;
00332 
00334     // Ray logger state
00335 
00337     vector<TreeRecord> trees;
00338 
00340     uint ctree;
00341 
00343     uint ntrees;
00344 
00346     uint nrays[NRayTypes];
00347 
00348 
00350     // File I/O utility structures
00351 
00352     // file I/O
00357     struct TreeRecordIO
00358     {
00359       ushort   px;
00360       ushort   py;
00361       float    origin[3];
00362       uint64_t record_offset;
00363     };
00364 
00369     struct RayRecordIO
00370     {
00371       float    distance[3]; 
00372       char     type;        
00373       ushort   nchildren;   
00374       uint64_t data_offset; 
00375       uint64_t data_size;   
00376     };
00377 
00379     struct CookieIO
00380     {
00381       char     cookie[7]; 
00382       uint64_t dataAddr;  
00383       uint64_t recordAddr;
00384       uint64_t treeAddr;  
00385       uint     ntrees;    
00386       uint     nrays[NRayTypes];  
00387     };
00388 
00389 
00391     // Stream I/O utility functions
00392 
00394     friend ostream& operator<<(ostream&, const RayRecordIO&);
00395 
00397     friend istream& operator>>(istream&, RayRecordIO&);
00398 
00400     friend ostream& operator<<(ostream&, const TreeRecordIO&);
00401 
00403     friend istream& operator>>(istream&, TreeRecordIO&);
00404   
00405 
00407     // File I/O data members
00408 
00410     static const char cookie[7];
00411 
00413     fstream ftrees;
00414 
00416     fstream frecords;
00417 
00419     fstream fdata;
00420 
00422     uint64_t offsetTrees;
00423 
00425     uint64_t offsetRecords;
00426 
00428     uint64_t offsetData;
00429 
00431     BufferedReader<TreeRecordIO,   100, false> bfTreeReader;
00432 
00434     BufferedReader<RayRecordIO, 100, false> bfRecordReader;
00435 
00437     string fname;
00438 
00440     string treesname;
00441 
00443     string recordsname;
00444 
00446     string dataname;
00447 
00449     BufferedWriter<TreeRecordIO,   100, false> bfTreeWriter;
00450 
00452     BufferedWriter<RayRecordIO, 100, false> bfRecordWriter;
00453 
00454 
00456     // Error recording, reporting
00457 
00461     enum ErrorLevel
00462       {
00463         Warning,  
00464         Error     
00465       };
00466 
00468     rl::Error error;
00469 
00471     string emsg;
00472 
00474     rl::Error setError(rl::Error  error_,
00475                        string     emsg_,
00476                        ErrorLevel level);
00477 
00479     void clearError();
00480 
00481 
00483     // Traversal utility functions
00484 
00485     // NOTE(dje) - Make sure you check for tree->curr == -1 after calling this.
00487     void NextRayDF(TRiter tree);
00488 
00490     void ConvBFToDF(TRiter tree);
00491 
00493     void ConvDFToBF(TRiter tree);
00494 
00496     void ResetChildIndices(TRiter tree);
00497 
00499     void GetRayRecord(TRiter tree, uint parent);
00500 
00502     void GetMaxDepth(TRiter tree);
00503 
00504   };
00505 
00506 } // namespace rl
00507 
00508 #endif // RL_RayLogger_h
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends