00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef __SIMIAN_TF_RASTERIZE_DOT_H
00021 #define __SIMIAN_TF_RASTERIZE_DOT_H
00022
00023 #include <mathGutz.h>
00024 #include <iostream>
00025
00026 using namespace std;
00027
00028
00029
00030
00031 template<class T>
00032 class Triangle {
00033 public:
00034 typedef gutz::vec3<T> Vec3;
00035 enum N_TEX_COORDS {
00036 N_TEX = 8
00037 };
00038
00039 Triangle()
00040 {
00041 for(int i=0; i<3; ++i)
00042 pts[i] = 0;
00043 for(int i=0; i<N_TEX; ++i)
00044 for(int j=0; j<3; ++j)
00045 tex[i][j] = 0;
00046 }
00047
00048 Triangle(Vec3 *p1, Vec3 *p2, Vec3 *p3)
00049 {
00050 if((!pi) || (!p2) || (!p3))
00051 {
00052 std::cerr << "Triangle(), got null point!" << std::endl;
00053 }
00054 pts[0] = p1; pts[1] = p2; pts[2] = p3;
00055 for(int i=0; i<N_TEX; ++i)
00056 for(int j=0; j<3; ++j)
00057 tex[i][j] = 0;
00058 }
00059 Triangle(const Triangle &t)
00060 {
00061 for(int i=0; i<3; ++i) pts[i] = t.pts[i];
00062 for(int i=0; i<N_TEX; ++i)
00063 for(int j=0; j<3; ++j)
00064 tex[i][j] = t.tex[i][j];
00065 }
00066
00067 Triangle &operator=(const Triangle &t)
00068 {
00069 for(int i=0; i<3; ++i) pts[i] = t.pts[i];
00070 for(int i=0; i<N_TEX; ++i)
00071 for(int j=0; j<3; ++j)
00072 tex[i][j] = t.tex[i][j];
00073 return *this;
00074 }
00075 void setPoints(Vec3 *p1, Vec3 *p2, Vec3 *p3)
00076 {
00077 if((!p1) || (!p2) || (!p3))
00078 {
00079 std::cerr << "Triangle(), got null point!" << std::endl;
00080 }
00081 pts[0] = p1; pts[1] = p2; pts[2] = p3;
00082 }
00083 void setTexCoord(int tc, Vec3 *t1, Vec3 *t2, Vec3 *t3)
00084 {
00085 if((!t1) || (!t2) || (!t3))
00086 {
00087 std::cerr << "Triangle(), got null tc!" << std::endl;
00088 }
00089 tex[tc][0] = t1; tex[tc][1] = t2; tex[tc][2] = t3;
00090 }
00091
00092 Vec3 &getTex(int tcNum, int ptNum) { return *(tex[tcNum][ptNum]); }
00093 const Vec3 &getTex(int tcNum, int ptNum) const { return *(tex[tcNum][ptNum]); }
00094
00095
00096 Vec3 &operator[](int i) { return *(pts[i]); }
00097 const Vec3 &operator[](int i) const { return *(pts[i]); }
00098
00099
00100 Vec3 *pts[3];
00101
00102 Vec3 *tex[N_TEX][3];
00103 };
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127 template< class T, class TT = Triangle<T> >
00128 class TriRasterInfo {
00129 public:
00130 typedef gutz::vec4<T> Vec4;
00131 typedef gutz::vec3<T> Vec3;
00132 typedef gutz::vec2<T> Vec2;
00133 typedef gutz::ray3<T> Ray3;
00134 typedef TT Tri3;
00135
00136 TriRasterInfo(int sx, int sy) : yPos(0), maxY(0), _sx(sx), _sy(sy) {}
00137
00138 inline int getSizeX() const { return _sx; }
00139 inline int getSizeY() const { return _sy; }
00140
00141
00142
00143
00144
00145
00146 inline bool end() const { return yPos >= maxY; }
00147
00148 inline TriRasterInfo &operator++()
00149 {
00150 incX();
00151 if(xPos >= maxX)
00152 {
00153
00154 incY();
00155 }
00156 return *this;
00157 }
00158
00159
00160
00161 inline bool setTri(Tri3 const *t) { return setUp(t); }
00162 inline Tri3 const *getTri() const { return tri; }
00163
00164 inline T x() const { return xPos; }
00165 inline T y() const { return yPos; }
00166
00167 inline void reset() { setUp(this->tri); }
00168
00169
00170
00171 int getMinX() const { return minX; }
00172
00173 int getMinY() const { return minY; }
00174
00175
00176
00177 int getMaxX() const { return maxX; }
00178
00179 int getMaxY() const { return maxY; }
00180
00181
00182
00183 inline int getMaxX(T yp) const
00184 {
00185 if(yPos > yswapEnd)
00186 {
00187 const T t = intersectY(yPos, r[2]);
00188 return clamp(0, int(t)-1, _sx-1);
00189 }
00190 else
00191 {
00192 const T t = intersectY(yPos, r[ee]);
00193 return clamp(0, int(t)-1, _sx-1);
00194 }
00195 }
00196
00197
00198
00199 inline int getMinX(T yp) const
00200 {
00201 if(yPos > yswapStart)
00202 {
00203 const T t = intersectY(yPos, r[2]);
00204 return clamp(0, int(t), _sx-1);
00205 }
00206 else
00207 {
00208 T t = intersectY(yPos, r[se]);
00209 return clamp(0, int(t), _sx-1);
00210 }
00211 }
00212
00213
00214
00215 inline void incY()
00216 {
00217 ++yPos;
00218 minX = getMinX(yPos);
00219 xPos = T(minX);
00220 maxX = getMaxX(yPos);
00221 setBary(T(xPos), T(yPos));
00222 }
00223
00224
00225 inline void incX()
00226 {
00227 alpha += xa;
00228 beta += xb;
00229 gamma += xg;
00230 ++xPos;
00231 }
00232
00233 inline T getAlpha(const T x, const T y) const
00234 { return x * xa + y * ya + ac; }
00235
00236 inline T getBeta(const T x, const T y) const
00237 { return x * xb + y * yb + bc; }
00238
00239 inline T getGamma(const T x, const T y) const
00240 { return x * xg + y * yg + gc; }
00241
00242
00243
00244
00245
00246
00247 inline void setBary(const T x, const T y)
00248 {
00249 alpha = getAlpha(x,y);
00250 beta = getBeta(x,y);
00251 gamma = getGamma(x,y);
00252 }
00253
00254 inline bool validBary() const
00255 {
00256 return (alpha >= 0) && (beta >= 0) && (gamma >= 0);
00257 }
00258
00259
00260
00261
00262 template< class VT >
00263 inline Vec4 interp4(const VT &v0, const VT &v1, const VT &v3) const
00264 {
00265 return Vec4(v0[0] * alpha + v1[0] * beta + v2[0] * gamma,
00266 v0[1] * alpha + v2[1] * beta + v2[1] * gamma,
00267 v0[2] * alpha + v2[2] * beta + v2[3] * gamma,
00268 v0[3] * alpha + v2[3] * beta + v2[3] * gamma);
00269 }
00270
00271 template< class VT >
00272 inline Vec3 interp3(const VT &v0, const VT &v1, const VT &v2) const
00273 {
00274 return Vec3(v0[0] * alpha + v1[0] * beta + v2[0] * gamma,
00275 v0[1] * alpha + v2[1] * beta + v2[1] * gamma,
00276 v0[2] * alpha + v2[2] * beta + v2[3] * gamma);
00277 }
00278
00279 template< class VT >
00280 inline Vec2 interp2(const VT &v0, const VT &v1, const VT &v2) const
00281 {
00282 return Vec3(v0[0] * alpha + v1[0] * beta + v2[0] * gamma,
00283 v0[1] * alpha + v2[1] * beta + v2[1] * gamma);
00284 }
00285
00286 template< class TP >
00287 inline TP interp(const TP &v0, const TP &v1, const TP &v2) const
00288 {
00289 return v0 * alpha + v1 * beta + v2 * gamma;
00290 }
00291
00292
00293
00294 public:
00295
00296
00297
00298
00299 T alpha,beta,gamma;
00300 T yPos;
00301 T xPos;
00302
00303 int minY;
00304 int maxY;
00305 int minX;
00306 int maxX;
00307 Tri3 const *tri;
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332 inline bool setUp(Tri3 const *t3)
00333 {
00334
00335 this->tri = t3;
00336
00337 const Tri3 &t = (*t3);
00338
00339
00340 int sp = mini3(t[0].y, t[1].y, t[2].y);
00341 int ep = maxi3(t[0].y, t[1].y, t[2].y);
00342 int mp = 0;
00343 if(((0==sp) && (2==ep)) || ((2==sp) && (0==ep))) mp = 1;
00344 if(((1==sp) && (0==ep)) || ((0==sp) && (1==ep))) mp = 2;
00345
00346
00347 minY = clamp(0,int(min3(t[0].y, t[1].y, t[2].y)), _sy-1);
00348 maxY = clamp(0,int(max3(t[0].y, t[1].y, t[2].y))-1, _sy-1);
00349
00350 int tminX = clamp(0,int(min3(t[0].x, t[1].x, t[2].x)), _sx-1);
00351 int tmaxX = clamp(0,int(max3(t[0].x, t[1].x, t[2].x))-1, _sx-1);
00352
00353 yPos = T(minY);
00354
00355
00356
00357 r[0] = Ray3(t[sp], t[mp] - t[sp]);
00358 r[1] = Ray3(t[sp], t[ep] - t[sp]);
00359 r[2] = Ray3(t[mp], t[ep] - t[mp]);
00360
00361
00362 yswapStart = T( int(t[mp].y) );
00363 yswapEnd = T( maxY );
00364 se = 0;
00365 ee = 1;
00366 if(r[se].d.y == 0)
00367 {
00368 se = 2;
00369 }
00370 if(r[ee].d.y == 0)
00371 {
00372 ee = 2;
00373 }
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383 T typ = r[se].p.y + r[se].d.y/T(2.0);
00384 if(intersectY(typ,r[se]) > intersectY(typ,r[ee]))
00385 {
00386
00387 int tmp = se;
00388 se = ee;
00389 ee = tmp;
00390 yswapStart = T( maxY );
00391 yswapEnd = T( int(t[mp].y) );
00392 }
00393
00394
00395 minX = int(clamp(0,intersectY(yPos,r[se]),_sx-1));
00396 xPos = T(minX);
00397 maxX = int(clamp(0,intersectY(yPos,r[ee])-1,_sx-1));
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407 setupBary(t[0],t[1],t[2]);
00408
00409 setBary(T(xPos),T(yPos));
00410
00411 return !(minY < maxY) || !(tminX < tmaxX);
00412 }
00413
00414
00415 TriRasterInfo() : yPos(0), maxY(0), _sx(0), _sy(0) {}
00416
00417 inline T intersectY(const T yp, const Ray3 &r) const
00418 {
00419
00420
00421
00422
00423
00424
00425
00426
00427 return r.p.x + (r.p.y * r.d.x - yp*r.d.x)/(-r.d.y);
00428 }
00429
00430 inline
00431 T intersect2D(const Ray3 &ri, const Ray3 &ro) const
00432 {
00433 return gutz::intersect2D(ri.p.x, ri.p.y, ri.d.x, ri.d.y,
00434 ro.p.x, ro.p.y, ro.d.x, ro.d.y);
00435 }
00436
00437
00438 int _sx, _sy;
00439
00440
00441
00442 T xa, ya, ac, xb, yb, bc, xg, yg, gc;
00443 void setupBary(const Vec3 &p0, const Vec3 &p1, const Vec3 &p2)
00444 {
00445
00446 xa = p1.y - p2.y;
00447 ya = p2.x - p1.x;
00448 ac = p1.x * p2.y - p2.x * p1.y;
00449
00450 xb = p2.y - p0.y;
00451 yb = p0.x - p2.x;
00452 bc = p2.x * p0.y - p0.x * p2.y;
00453
00454 xg = p0.y - p1.y;
00455 yg = p1.x - p0.x;
00456 gc = p0.x * p1.y - p1.x * p0.y;
00457
00458
00459 T tmp;
00460 tmp = T(1.0)/(p0.x * xa + p0.y * ya + ac);
00461 xa *= tmp; ya *= tmp; ac *= tmp;
00462 tmp = T(1.0)/(p1.x * xb + p1.y * yb + bc);
00463 xb *= tmp; yb *= tmp; bc *= tmp;
00464 tmp = T(1.0)/(p2.x * xg + p2.y * yg + gc);
00465 xg *= tmp; yg *= tmp; gc *= tmp;
00466 }
00467
00468 int se, ee;
00469 T yswapStart, yswapEnd;
00470
00471 Ray3 r[3];
00472
00473 };
00474
00475 #if 0
00476 class TFRasterInfo : public TriRasterInfo<STF::tfSType, Triangle<STF::tfSType> >
00477 {
00478 public:
00479 typedef STF::tfSType SType;
00480 typedef Triangle<SType> TriType;
00481 typedef gutz::vec2<SType> Vec2;
00482 typedef gutz::mat3<SType> Mat3;
00483
00484 TFRasterInfo() : TriRasterInfo<SType,TriType>() {}
00485 TFRasterInfo(const TriType &t) : TriRasterInfo<SType,TriType>(t) {}
00486 TFRasterInfo(const TriType t) : TriRasterInfo<SType,TriType>(t) {}
00487
00488 ~TFRasterInfo();
00489
00490
00491
00492
00493 SType xIncS;
00494 SType yIncS;
00495 Vec2 xIncV;
00496 Vec2 yIncV;
00497 Mat3 xform;
00498
00499
00500 };
00501 #endif
00502
00503
00504 #endif
00505
00506
00507
00508
00509 #if 0
00510
00511
00512 #include <limits>
00513 #include "TFParams.h"
00514 #include <iostream>
00515
00516 namespace STFR {
00517
00518 typedef STF::tfSType ST;
00519 typedef STF::tfRangeType VT;
00520 typedef STF::tfDomainType PT;
00521 typedef STF::tfVec2 V2;
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554 template<
00555 class B1,
00556 class B2,
00557 class B3,
00558 class B4,
00559 class NT,
00560 class EL
00561 >
00562 inline
00563 void rasterize2D4(Nrro::NrroIter<NT> ni,
00564 const gutz::vec2i &posIdx,
00565 const STF::tfRangeIdx &valIdx,
00566 const EL *elt)
00567 {
00568 std::cerr << " hi type = " << typeid(NT).name() << std::endl;
00569
00570 vec2<NT> mmm;
00571
00572 if(numeric_limits<NT>::epsilon())
00573 {
00574 mmm = vec2<NT>(NT(0.0),NT(1.0));
00575 }
00576 else
00577 {
00578 mmm = vec2<NT>(numeric_limits<NT>::min(), numeric_limits<NT>::max());
00579 }
00580
00581
00582 const int i1 = valIdx[0];
00583 const int i2 = valIdx[1];
00584 const int i3 = valIdx[2];
00585 const int i4 = valIdx[3];
00586
00587
00588 const PT bmin = elt->getMinBox();
00589 const PT bmax = elt->getMaxBox();
00590
00591
00592 const int minX = clamp(0, int(bmin[posIdx.x] * ni->dim(1)), ni->dim(1));
00593 const int minY = clamp(0, int(bmin[posIdx.y] * ni->dim(2)), ni->dim(2));
00594 const int maxX = clamp(0, int(bmax[posIdx.x] * ni->dim(1)), ni->dim(1));
00595 const int maxY = clamp(0, int(bmax[posIdx.y] * ni->dim(2)), ni->dim(2));
00596
00597
00598 const ST px = minX/float(ni->dim(1));
00599 const ST py = minY/float(ni->dim(2));
00600 const ST dx = 1.0f/float(ni->dim(1));
00601 const ST dy = 1.0f/float(ni->dim(2));
00602
00603
00604 ST u;
00605 VT o;
00606 V2 pos(px,py);
00607
00608
00609 for(int y = minY; y<= maxY; ++y, pos.y+=dy)
00610 {
00611 for(int x = minX; x<= maxX; ++x, pos.x+=dx)
00612 {
00613
00614 o = elt->eval(pos, posIdx);
00615 #if 1
00616
00617
00618 u = g_affine(NT(mmm.x), ni(0,x,y), NT(mmm.y), ST(0.0), ST(1.0));
00619
00620 ni(0,x,y) = g_affine(ST(0.0), o[i1], ST(1.0), NT(mmm.x), NT(mmm.y));
00621
00622 u = g_affine(NT(mmm.x), ni(1,x,y), NT(mmm.y), ST(0.0), ST(1.0));
00623
00624 ni(1,x,y) = g_affine(ST(0.0), o[i2], ST(1.0), NT(mmm.x), NT(mmm.y));
00625
00626 u = g_affine(NT(mmm.x), ni(2,x,y), NT(mmm.y), ST(0.0), ST(1.0));
00627
00628 ni(2,x,y) = g_affine(ST(0.0), o[i3], ST(1.0), NT(mmm.x), NT(mmm.y));
00629
00630 u = g_affine(NT(mmm.x), ni(3,x,y), NT(mmm.y), ST(0.0), ST(1.0));
00631
00632 ni(3,x,y) = g_affine(ST(0.0), o[i4], ST(1.0), NT(mmm.x), NT(mmm.y));
00633
00634 #endif
00635 }
00636 pos.x = px;
00637 }
00638
00639 }
00640
00641
00642
00643
00644 #define dispatchRast2D4(B1,B2,B3,B4,NSP,PI,VI,EL) \
00645 \
00646 switch( NSP->getType() ) { \
00647 case Nrro::CHAR: \
00648 STFR::rasterize2D4< B1 , B2 , B3 , B4 >( NSP ->begin<char>(), PI , VI , EL ); \
00649 break; \
00650 case Nrro::UCHAR: \
00651 STFR::rasterize2D4< B1 , B2 , B3 , B4 >( NSP ->begin<unsigned char>(), PI , VI , EL ); \
00652 break; \
00653 case Nrro::SHORT: \
00654 STFR::rasterize2D4< B1 , B2 , B3 , B4 >( NSP ->begin<short>(), PI , VI , EL ); \
00655 break; \
00656 case Nrro::USHORT: \
00657 STFR::rasterize2D4< B1 , B2 , B3 , B4 >( NSP ->begin<unsigned short>(), PI , VI , EL ); \
00658 break; \
00659 case Nrro::INT: \
00660 STFR::rasterize2D4< B1 , B2 , B3 , B4 >( NSP ->begin<int>(), PI , VI , EL ); \
00661 break; \
00662 case Nrro::UINT: \
00663 STFR::rasterize2D4< B1 , B2 , B3 , B4 >( NSP ->begin<unsigned int>(), PI , VI , EL ); \
00664 break; \
00665 case Nrro::FLOAT: \
00666 STFR::rasterize2D4< B1 , B2 , B3 , B4 >( NSP ->begin<float>(), PI , VI , EL ); \
00667 break; \
00668 case Nrro::DOUBLE: \
00669 STFR::rasterize2D4< B1 , B2 , B3 , B4 >( NSP ->begin<double>(), PI , VI , EL ); \
00670 break; \
00671 }
00672
00673 }
00674
00675 #endif
00676
00677