00001 #include <iostream>
00002 #include <math.h>
00003 #include "vuTFPreintegrated.h"
00004
00005 vuTFPreintegrated::vuTFPreintegrated()
00006 {
00007 m_Table = 0;
00008 m_Light = 0;
00009 init(4,256);
00010 };
00011
00012 vuTFPreintegrated::vuTFPreintegrated(dword ncomp, dword range)
00013 {
00014 m_Table = 0;
00015 m_Light = 0;
00016 init(ncomp,range);
00017 }
00018
00019 vuTFPreintegrated::vuTFPreintegrated(const vuTFIntensity& inst)
00020 {
00021 m_Table = 0;
00022 m_Light = 0;
00023 init(inst.getNComponents(), inst.getRange());
00024 operator=(inst);
00025 }
00026
00027 vuTFPreintegrated::~vuTFPreintegrated()
00028 {
00029 cleanup();
00030 }
00031
00032
00033 vuTFPreintegrated& vuTFPreintegrated::operator=(const vuTFIntensity& rhs)
00034 {
00035 vuTFIntensity::operator=(rhs);
00036 preintegrate();
00037
00038 return *this;
00039 }
00040
00041 void vuTFPreintegrated::preintegrate()
00042 {
00043 float *pitable = m_PITable;
00044 float *table = m_Table;
00045 float alpha = m_Table[m_NComp-1];
00046
00047 if(m_AlphaWeighted)
00048 {
00049 for(word c=0; c<m_NComp-1; c++, pitable++, table++)
00050 {
00051 *pitable = (*table)*alpha;
00052 }
00053 *pitable = alpha;
00054 pitable++;
00055 table++;
00056
00057 for(word i=1; i<m_Range; i++)
00058 {
00059 alpha = table[m_NComp-1];
00060 for(word c=0; c<m_NComp-1; c++, pitable++, table++)
00061 {
00062 *pitable = *(pitable-m_NComp) + ((*table) * alpha);
00063 }
00064 *pitable = *(pitable-m_NComp) + alpha;
00065 pitable++;
00066 table++;
00067 }
00068 } else {
00069 for(word c=0; c<m_NComp; c++, pitable++, table++)
00070 {
00071 *pitable = (*table);
00072 }
00073
00074 for(word i=1; i<m_Range; i++)
00075 {
00076 alpha = table[m_NComp-1];
00077 for(word c=0; c<m_NComp; c++, pitable++, table++)
00078 {
00079 *pitable = *(pitable-m_NComp) + (*table);
00080 }
00081 }
00082 }
00083
00084 for(word c=0; c<m_NComp; c++, pitable++)
00085 {
00086 *pitable = *(pitable-m_NComp);
00087 }
00088 }
00089
00090 bool vuTFPreintegrated::integrate(float back, float front, float d, vuColourN & col)
00091 {
00092 if(col.nComponents() != m_NComp) return false;
00093
00094 word a,b;
00095
00096 if(front == back)
00097 {
00098 a = (word)floor(front);
00099
00100
00101
00102 for(word c = 0; c<m_NComp; c++)
00103 col[c] = (m_Table[a*m_NComp + c]);
00104
00105 return true;
00106 }
00107
00108
00109
00110
00111
00112
00113
00114
00115 a = (word)floor(front);
00116 b = (word)floor(back);
00117 word aind = a*m_NComp;
00118 word aind2 = aind+m_NComp;
00119 word bind = b*m_NComp;
00120 word bind2 = bind+m_NComp;
00121
00122 float fpoint = front-a;
00123 float omfpoint = 1-fpoint;
00124 float bpoint = back-b;
00125 float ombpoint = 1-bpoint;
00126
00127 float ialpha = ((m_PITable[bind2 + m_NComp-1]*bpoint
00128 + m_PITable[bind + m_NComp-1]*ombpoint)
00129 - (m_PITable[aind2 + m_NComp-1]*fpoint
00130 + m_PITable[aind + m_NComp-1]*omfpoint)
00131 );
00132 if(fabs(ialpha)<0.0001)
00133 {
00134 for(word c = 0; c<m_NComp; c++)
00135 col[c] = 0;
00136 return true;
00137 }
00138
00139 float cscale, ascale=1/(back-front);
00140 if(m_AlphaWeighted)
00141 cscale = 1/(ialpha);
00142 else
00143 cscale = ascale;
00144
00145 for(word c = 0; c<m_NComp-1; c++)
00146 col[c] = ((m_PITable[bind2 + c]*bpoint
00147 + m_PITable[bind+ c]*ombpoint)
00148 - ( m_PITable[aind2 + c]*fpoint
00149 + m_PITable[aind + c]*omfpoint)
00150 )*cscale;
00151
00152 col[m_NComp-1] = ialpha*ascale;
00153 return true;
00154 }
00155
00156 bool vuTFPreintegrated::init(dword ncomp, dword range)
00157 {
00158 if(!vuTFIntensity::init(ncomp, range)) return false;
00159
00160 m_PITable = new float[m_TableLength+m_NComp];
00161 if(!m_PITable) return false;
00162
00163 for (dword i=0;i<m_TableLength+m_NComp;i++)
00164 m_PITable[i] = 0.0f;
00165 return true;
00166 }
00167
00168 void vuTFPreintegrated::cleanup()
00169 {
00170 vuTFIntensity::cleanup();
00171 if(m_PITable) delete [] m_PITable;
00172 m_PITable = 0;
00173 }
00174