00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifndef __UT_PolySample__
00026 #define __UT_PolySample__
00027
00028 #include "UT_API.h"
00029 #include "UT_Vector2.h"
00030
00031 class UT_SampleQuad2D;
00032
00033 typedef int (UT_SampleQuad2D::*UT_QuadSampleFunc)(double x, double y,
00034 float &u, float &v);
00035
00036 class UT_API UT_SampleSimpleTri2D {
00037 public:
00038 float u0, u1;
00039 float v0, v1;
00040 float Nxi;
00041
00042 inline void init(float x0[2], float x1[2], float x2[2])
00043 {
00044 u0 = x1[0] - x0[0]; v0 = x1[1] - x0[1];
00045 u1 = x2[0] - x0[0]; v1 = x2[1] - x0[1];
00046 Nxi = 1.0F/(u0*v1 - u1*v0);
00047 }
00048 inline void setVertices(fpreal x0, fpreal y0, fpreal x1, fpreal y1,
00049 fpreal x2, fpreal y2)
00050 {
00051 u0 = x1 - x0; v0 = y1 - y0;
00052 u1 = x2 - x0; v1 = y2 - y0;
00053 Nxi = 1.0F / (u0*v1 - u1*v0);
00054 }
00055
00056 inline void initD0(float x0[2], float x2[2], fpreal dx, fpreal dy)
00057 {
00058 u0 = dx; v0 = dy;
00059 u1 = x2[0] - x0[0]; v1 = x2[1] - x0[1];
00060 Nxi = 1/(u0*v1 - u1*v0);
00061 }
00062
00063 inline int sample(fpreal sx, fpreal sy, fpreal x0, fpreal y0, float &u, float &v)
00064 {
00065 fpreal tu, tv;
00066 sx -= x0; sy -= y0;
00067 tu = (sx*v1 - sy*u1) * Nxi;
00068 if (tu >= 0 && tu <= 1) {
00069 tv = (sy*u0 - sx*v0) * Nxi;
00070 if (tv >= 0 && tu+tv <= 1) {
00071 u = tu;
00072 v = tv;
00073 return 1;
00074 }
00075 }
00076 return 0;
00077 }
00078 };
00079
00080 class UT_API UT_SampleTri2D {
00081 public:
00082 int isBackface() const { return myBackface; }
00083 void setVertices(fpreal x0, fpreal y0, fpreal x1, fpreal y1,
00084 fpreal x2, fpreal y2)
00085 {
00086 fpreal dx, dy, z;
00087 myX[0] = x0;
00088 myX[1] = y0;
00089 myTri.setVertices(x0, y0, x1, y1, x2, y2);
00090 dx = myTri.v1; dy = -myTri.u1;
00091 z = -(dx*x0 + dy*y0);
00092 myBackface = dx*x1 + dy*y1 + z > 0;
00093 }
00094 int sample(fpreal x, fpreal y, float &u, float &v)
00095 {
00096 if (myTri.sample(x, y, myX[0], myX[1], u, v))
00097 {
00098 return 1;
00099 }
00100 return 0;
00101 }
00102
00103 private:
00104 UT_SampleSimpleTri2D myTri;
00105 float myX[2];
00106 int myBackface;
00107 };
00108
00109 class UT_API UT_SampleQuad2D {
00110 public:
00111
00112
00113
00114 int setVertices(fpreal x0, fpreal y0, fpreal x1, fpreal y1,
00115 fpreal x2, fpreal y2, fpreal x3, fpreal y3)
00116 {
00117 myXY[0][0] = x0; myXY[0][1] = y0;
00118 myXY[1][0] = x1; myXY[1][1] = y1;
00119 myXY[2][0] = x2; myXY[2][1] = y2;
00120 myXY[3][0] = x3; myXY[3][1] = y3;
00121 return init();
00122 }
00123
00124 int init();
00125
00126 int sample(double x, double y, float &u, float &v)
00127 {
00128 return (this->*mySampler)(x, y, u, v);
00129 }
00130
00131
00132 int isBackface() { return myBackface; }
00133
00134 private:
00135
00136 int sampleTrap1(double x, double y, float &u, float &v);
00137 int sampleTrap2(double x, double y, float &u, float &v);
00138
00139
00140 int sampleQuad(double x, double y, float &u, float &v);
00141
00142 int sampleConcave(double x, double y, float &u, float &v);
00143
00144 int sampleBowtie(double x, double y, float &u, float &v);
00145 int sampleGeneral(double x, double y, float &u, float &v);
00146
00147
00148 float myXY[4][2];
00149
00150 UT_QuadSampleFunc mySampler;
00151 UT_SampleSimpleTri2D myFirst;
00152 UT_SampleSimpleTri2D mySecond;
00153 UT_Vector2 myAB, myBC, myCD, myAD, myAE;
00154 float myU, myV;
00155 int myBackface;
00156 };
00157
00158
00159
00160 class UT_API UT_BilinearSampleQuad2D {
00161 public:
00162
00163
00164 void setVertices(fpreal x0, fpreal y0, fpreal x1, fpreal y1,
00165 fpreal x2, fpreal y2, fpreal x3, fpreal y3);
00166
00167
00168
00169
00170
00171 int sample(fpreal x, fpreal y, float &u, float &v);
00172
00173
00174 private:
00175 int myBackface;
00176 int myFlipped;
00177 int myGeneral;
00178 float mySBx, mySDx;
00179
00180 float myAx, myAy, myBx, myBy, myCx, myCy, myDx, myDy;
00181 float myK, myIK, myL, myM;
00182 };
00183
00184
00185 #endif
00186