Main Page | Class Hierarchy | Class List | File List | Class Members

intervalmap.h

00001 /* ************************************************************************* 00002 intervalmap.h - description 00003 ------------------- 00004 begin : Fri Oct 18 2002 00005 copyright : (C) 2002 by Micha Riser 00006 email : mriser@gmx.net 00007 00008 $Id: intervalmap_8h-source.html,v 1.1 2005/02/04 21:02:27 micha Exp $ 00009 00010 ************************************************************************* */ 00011 00012 /* ************************************************************************* 00013 * * 00014 * This program is free software; you can redistribute it and/or modify * 00015 * it under the terms of the GNU General Public License as published by * 00016 * the Free Software Foundation; either version 2 of the License, or * 00017 * (at your option) any later version. * 00018 * * 00019 ************************************************************************* */ 00020 00021 #ifndef INTERVALMAP_H 00022 #define INTERVALMAP_H 00023 00024 #include<vector> 00025 00026 #include "types.h" 00027 #include "function.h" 00028 #include "vector.h" 00029 00034 template <class Interpol, class Eval> class IntervalMap { 00035 00036 public: // Constructor and destructor 00039 IntervalMap(): data() {} 00040 00043 IntervalMap(const IntervalMap& im): data() { 00044 for(unsigned int i=0; i<im.data.size(); ++i) { 00045 data.push_back(Entry(im.data[i].value, im.data[i].obj->copy() )); 00046 } 00047 } 00048 00049 virtual ~IntervalMap() { 00050 00051 for(unsigned int i=0; i<data.size(); ++i) { 00052 delete(data[i].obj); 00053 } 00054 00055 } 00056 00057 public: // Public Methods 00058 00061 void insert(FLT value, const Interpol* obj) { 00062 Entry newentry = Entry(value,obj); 00063 data.push_back(newentry); 00064 int i = data.size()-1; 00065 while(i>0 && data[i].value<data[i-1].value) { 00066 // swap i and i-1 00067 // i is newentry 00068 data[i]=data[i-1]; 00069 data[i-1]=newentry; 00070 i--; 00071 } 00072 } 00073 00074 Eval lookup(FLT value, const Vector3& v) const { 00075 00076 // do binary search 00077 int i = 0; 00078 int j = data.size()-1; 00079 00080 while(i<j-1) { 00081 int k = (i+j)/2; 00082 if ( data[k].value>value) { 00083 j = k; 00084 } else { 00085 i = k; 00086 } 00087 } 00088 00089 // do interpolation 00090 00091 // exceptional cases 00092 if (i==j) return data[i].obj->evaluateAt(v); 00093 if (value<data[i].value) return data[i].obj->evaluateAt(v); 00094 if (value>data[j].value) return data[j].obj->evaluateAt(v); 00095 00096 // normal case 00097 return linearInterpolate(i,j,value,v); 00098 00099 } 00100 00101 private: // Inner classes 00102 class Entry { 00103 public: // Public attributes 00104 FLT value; // DBL? 00105 const Interpol* obj; 00106 public: // Constructor and destructor 00107 Entry(FLT v, const Interpol* o): value(v), obj(o) {}; 00108 }; 00109 00110 private: // Private attributes 00111 std::vector<Entry> data; 00112 00113 private: // Private methods 00114 Eval linearInterpolate(int a, int b, FLT v, const Vector3& point) const { 00115 Eval tmp1 = data[a].obj->evaluateAt(point); 00116 Eval tmp2 = data[b].obj->evaluateAt(point); 00117 FLT va = data[a].value; 00118 FLT vb = data[b].value; 00119 tmp1.scale( (vb-v)/(vb-va) ); 00120 tmp2.scale( (v-va)/(vb-va) ); 00121 tmp1.add(tmp2); 00122 return tmp1; 00123 } 00124 00125 }; 00126 00127 #endif

Generated on Thu Jan 27 12:16:05 2005 for raytracer.kdevelop by doxygen 1.3.8