00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "TFElementSTD.h"
00021 #include <mathGutz.h>
00022 #include <iostream>
00023 #include "TFRasterize.h"
00024 #include <nrro/nrroDispatch.h>
00025
00026 using namespace STF;
00027 using namespace gutz;
00028 using namespace std;
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 TFElementBox::TFElementBox()
00040 : _kind(RADIAL)
00041 {
00042 initBox(PosType(.5), PosType(.5));
00043 _valVec.push_back(ValType(randval(), randval(), randval(), .5, 0.0));
00044 }
00045
00046 TFElementBox::TFElementBox(const PosType center, const PosType size)
00047 : _kind(RADIAL)
00048 {
00049 initBox(center,size);
00050 _valVec.push_back(ValType(randval(), randval(), randval(), .5, 0.0));
00051 }
00052
00053
00054
00055
00056 void TFElementBox::initBox(const PosType center, const PosType size)
00057 {
00058 PosType ll = center - size/2.0;
00059 PosType ur = center + size/2.0;
00060
00061 _posVec.push_back(ll);
00062 _posVec.push_back(ur);
00063
00064 }
00065
00066
00067
00068
00069 void TFElementBox::setCenter(PosType center)
00070 {
00071 PosType d = center - getCenter();
00072 _posVec[LL] += d;
00073 _posVec[UR] += d;
00074 }
00075
00076
00077
00078
00079 void TFElementBox::setSize(PosType size)
00080 {
00081 PosType ds = (size - getSize())/2.0;
00082 _posVec[LL] -= ds;
00083 _posVec[UR] += ds;
00084 }
00085
00086
00087
00088
00089 void TFElementBox::setMin(PosType ll)
00090 {
00091 _posVec[LL] = ll;
00092 updateBox();
00093 }
00094
00095
00096
00097
00098 void TFElementBox::setMax(PosType ur)
00099 {
00100 _posVec[UR] = ur;
00101 updateBox();
00102 }
00103
00104
00105
00106
00107 void TFElementBox::updateBox()
00108 {
00109 for(int i=0; i<_posVec[UR].size(); ++i)
00110 {
00111
00112 if(_posVec[UR][i] < _posVec[LL][i])
00113 {
00114 float tmp = _posVec[UR][i];
00115 _posVec[UR][i] = _posVec[LL][i];
00116 _posVec[LL][i] = tmp;
00117 }
00118
00119 }
00120 }
00121
00122
00123
00124
00125 TFElementBox::ValType
00126 TFElementBox::eval(const tfVec2 &tfpos,
00127 const gutz::vec2i &posIdx) const
00128 {
00129 #if 1
00130 const tfSType left = _posVec[LL][posIdx.x];
00131 const tfSType right = _posVec[UR][posIdx.x];
00132 const tfSType bottom = _posVec[LL][posIdx.y];
00133 const tfSType top = _posVec[UR][posIdx.y];
00134
00135 if( (left > tfpos.x)
00136 || (right < tfpos.x)
00137 || (bottom > tfpos.y)
00138 || (top < tfpos.y) )
00139 {
00140 return STF::tfRangeType_default;
00141 }
00142
00143 const tfSType width = right - left;
00144 const tfSType height = top - bottom;
00145 const tfSType hw = width * tfSType(.5);
00146 const tfSType hh = height * tfSType(.5);
00147
00148 tfSType ev = 0;
00149
00150 if(RADIAL == _kind)
00151 {
00152 const tfSType xp = (tfpos.x - (left + hw))/hw;
00153 const tfSType yp = (tfpos.y - (bottom + hh))/hh;
00154 const tfSType ep = sqrt( xp*xp + yp*yp );
00155 ev = 0;
00156 }
00157 if(SEPARABLE == _kind)
00158 {
00159 const tfSType wp = g_min(tfpos.x - left, right - tfpos.x);
00160 const tfSType hp = g_min(tfpos.y - bottom, top - tfpos.y);
00161 const tfSType ep = g_min(wp/hw, hp/hh);
00162 ev = 0;
00163 }
00164 #endif
00165
00166
00167
00168 ValType ret = _valVec[0];
00169 ret[ALPHA] = ev;
00170
00171 return ret;
00172 }
00173
00174
00175
00176
00177 void TFElementBox::rasterize2D(NrroSP n,
00178 const gutz::vec2i &posIdx,
00179 const int kind)
00180 {
00181 _posIdx = posIdx;
00182
00183 if(TF_RGBA == kind)
00184 {
00185 _valIdx = STF::tfRGBA_idx;
00186
00187 }
00188
00189 dispatchIter1(rasterize2D,n);
00190
00191
00192 }
00193
00194 typedef gutz::vec3<STF::tfSType> _V3;
00195 typedef Triangle<STF::tfSType> TFTriangle;
00196 typedef TriRasterInfo<STF::tfSType, TFTriangle> TFRasterInfo;
00197
00198 template<class T>
00199 void TFElementBox::rasterize2D(Nrro::NrroIter<T> ni) const
00200 {
00201 const tfSType left = _posVec[LL][_posIdx.x] * ni->dim(1);
00202 const tfSType right = _posVec[UR][_posIdx.x] * ni->dim(1);
00203 const tfSType bottom = _posVec[LL][_posIdx.y] * ni->dim(2);
00204 const tfSType top = _posVec[UR][_posIdx.y] * ni->dim(2);
00205
00206 cerr << " dims " << left << " " << bottom << " = " << right << " " << top << endl;
00207
00208 _V3 ll(left,bottom,0);
00209 _V3 tll(-1,-1,0);
00210 _V3 lr(right,bottom,0);
00211 _V3 tlr(1,-1,0);
00212 _V3 tl(left,top,0);
00213 _V3 ttl(-1,1,0);
00214 _V3 tr(right,top,0);
00215 _V3 ttr(1,1,0);
00216
00217 Triangle<SType> tris[2];
00218 tris[0].setPoints(&ll,&tl,&tr);
00219 tris[0].setTexCoord(0,&tll,&ttl,&ttr);
00220 tris[1].setPoints(&ll,&tr,&lr);
00221 tris[1].setTexCoord(0,&tll,&ttr,&tlr);
00222
00223 TFRasterInfo ri(ni->dim(1), ni->dim(2));
00224
00225 SType deleteme = 0;
00226 int test = 0;
00227
00228
00229 for(int i=0; i<2; ++i)
00230 {
00231
00232 if( ri.setTri( &(tris[i]) ) )
00233 {
00234 cerr << " triangle " << i << " has no renderable pixels " << endl;
00235 continue;
00236 }
00237
00238 cerr << " dims = " << ri.minX << " " << ri.minY << " , " << ri.maxX << " " << ri.maxY << endl;
00239
00240 while(!ri.end())
00241 {
00242
00243
00244 if( (ri.alpha + ri.beta + ri.gamma) > 1.01 )
00245 {
00246 cerr << " bad bary > 1 = " << ri.alpha + ri.beta + ri.gamma << endl;
00247 }
00248 if( (ri.alpha + ri.beta + ri.gamma) < -.01 )
00249 {
00250 cerr << " bad bary < 0 = " << ri.alpha + ri.beta + ri.gamma << endl;
00251 }
00252
00253 ni(0, int(ri.x()), int(ri.y())) = 1;
00254 ni(1, int(ri.x()), int(ri.y())) = 1;
00255 ni(2, int(ri.x()), int(ri.y())) = 1;
00256 gutz::vec2<SType> tc = ri.interp2(ri.getTri()->getTex(0,0),
00257 ri.getTri()->getTex(0,1),
00258 ri.getTri()->getTex(0,2));
00259 ni(3, int(ri.x()), int(ri.y())) = T( exp(-(ri.x()*ri.x() + ri.y()*ri.y())) * 255 );
00260
00261 ++ri;
00262 ++test;
00263 }
00264 cerr << " triangle " << i << " = " << test << " pixels " << endl;
00265 test = 0;
00266 }
00267
00268 cerr << " rasterized " << test << " pixels " << endl;
00269
00270 #if 0
00271 for(int i=0; i<2; ++i)
00272 {
00273 ri.setUp( &(tris[i]) );
00274 for(int y=ri.minY; y<=ri.maxY; ++y, ri.tIncY())
00275 {
00276 for(int x=ri.getMinX(); x<=ri.getMaxX(); ++x, ri.incX())
00277 {
00278 deleteme = ri.x();
00279 ++test;
00280 }
00281 }
00282 }
00283 #endif
00284 cerr << " rasterized " << test << " pixels " << endl;
00285
00286 }
00287
00288 #if 0
00289
00290
00291 template<class T>
00292 void #include "TFRasterize2D01.h"
00293
00294
00295 template<class T>
00296 void TFElement::rasterize2D(Nrro::NrroIter<T> ni,
00297 const gutz::vec2i &posIdx,
00298 const STF::tfRangeIdx &valIdx) const
00299 {
00300
00301 vec2<T> mmm;
00302
00303 if(numeric_limits<T>::epsilon())
00304 {
00305 mmm = vec2<T>(T(0.0),T(1.0));
00306 }
00307 else
00308 {
00309 mmm = vec2<T>(numeric_limits<T>::min(), numeric_limits<T>::max());
00310 }
00311
00312 const tfSType left = _posVec[LL][posIdx.x];
00313 const tfSType right = _posVec[UR][posIdx.x];
00314 const tfSType bottom = _posVec[LL][posIdx.y];
00315 const tfSType top = _posVec[UR][posIdx.y];
00316
00317 const tfSType width = right - left;
00318 const tfSType height = top - bottom;
00319 const tfSType hw = width * tfSType(.5);
00320 const tfSType hh = height * tfSType(.5);
00321
00322
00323 const int minX = clamp(0, int(bmin[posIdx.x] * ni->dim(1)), ni->dim(1));
00324 const int minY = clamp(0, int(bmin[posIdx.y] * ni->dim(2)), ni->dim(2));
00325 const int maxX = clamp(0, int(bmax[posIdx.x] * ni->dim(1)), ni->dim(1));
00326 const int maxY = clamp(0, int(bmax[posIdx.y] * ni->dim(2)), ni->dim(2));
00327
00328
00329 const STF::tfSType px = left;
00330 const STF::tfSType py = bottom;
00331 const STF::tfSType dx = 2.0f/float(ni->dim(1));
00332 const STF::tfSType dy = 2.0f/float(ni->dim(2));
00333
00334
00335 tfSType u;
00336 ValType o = _valVec[0];
00337 tfVec2 pos(-1,-1);
00338
00339
00340 for(int y = minY; y<= maxY; ++y, pos.y+=dy)
00341 {
00342 for(int x = minX; x<= maxX; ++x, pos.x+=dx)
00343 {
00344
00345 o[STF::ALPHA] = myEval(pos.x*pos.x + pos.y*pos.y) * _valVec[0][STF::ALPHA];
00346
00347 for(int i=0; i<4; ++i)
00348 {
00349
00350 u = g_affine(T(mmm.x), ni(i,x,y), T(mmm.y), TFT(0.0), TFT(1.0));
00351
00352
00353
00354 ni(i,x,y) = g_affine(TFT(0.0), o[valIdx[i]], TFT(1.0), T(mmm.x), T(mmm.y));
00355 }
00356 }
00357 pos.x = px;
00358 }
00359 }
00360
00361 #endif
00362
00363