HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_PolySample.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: UT_PolySample.h ( UT Library, C++)
7  *
8  * COMMENTS: This code comes from RAY_Quad2d.h, and provies
9  * methods to sample two dimensional polygons. Ie, given
10  * a point, find the "u v" coordinates of that point in
11  * the polygon.
12  * It currently only handles quads and tris. Tris give
13  * barycentric coordinates, quads give bilinear interpolated
14  * coordinates.
15  */
16 
17 #ifndef __UT_PolySample__
18 #define __UT_PolySample__
19 
20 #include "UT_API.h"
21 #include "UT_Vector2.h"
22 
23 class UT_SampleQuad2D;
24 
25 typedef int (UT_SampleQuad2D::*UT_QuadSampleFunc)(double x, double y,
26  float &u, float &v);
27 
29 public:
30  float u0, u1;
31  float v0, v1;
32  float Nxi;
33 
34  inline void init(float x0[2], float x1[2], float x2[2])
35  {
36  u0 = x1[0] - x0[0]; v0 = x1[1] - x0[1];
37  u1 = x2[0] - x0[0]; v1 = x2[1] - x0[1];
38  Nxi = 1.0F/(u0*v1 - u1*v0);
39  }
40  inline void setVertices(fpreal x0, fpreal y0, fpreal x1, fpreal y1,
41  fpreal x2, fpreal y2)
42  {
43  u0 = x1 - x0; v0 = y1 - y0;
44  u1 = x2 - x0; v1 = y2 - y0;
45  Nxi = 1.0F / (u0*v1 - u1*v0);
46  }
47 
48  inline void initD0(float x0[2], float x2[2], fpreal dx, fpreal dy)
49  {
50  u0 = dx; v0 = dy;
51  u1 = x2[0] - x0[0]; v1 = x2[1] - x0[1];
52  Nxi = 1/(u0*v1 - u1*v0);
53  }
54 
55  inline int sample(fpreal sx, fpreal sy, fpreal x0, fpreal y0, float &u, float &v, fpreal tol=0.0)
56  {
57  sx -= x0; sy -= y0;
58  fpreal tu = (sx*v1 - sy*u1) * Nxi;
59  if (tu >= -tol && tu <= 1+tol) {
60  fpreal tv = (sy*u0 - sx*v0) * Nxi;
61  if (tv >= -tol && tu+tv <= 1+tol) {
62  u = tu;
63  v = tv;
64  return 1;
65  }
66  }
67  return 0;
68  }
69 };
70 
72 public:
73  int isBackface() const { return myBackface; }
75  fpreal x2, fpreal y2)
76  {
77  fpreal dx, dy, z;
78  myX[0] = x0;
79  myX[1] = y0;
80  myTri.setVertices(x0, y0, x1, y1, x2, y2);
81  dx = myTri.v1; dy = -myTri.u1;
82  z = -(dx*x0 + dy*y0);
83  myBackface = dx*x1 + dy*y1 + z > 0;
84  }
85  int sample(fpreal x, fpreal y, float &u, float &v, fpreal tol=0.0)
86  {
87  if (myTri.sample(x, y, myX[0], myX[1], u, v, tol))
88  {
89  return 1;
90  }
91  return 0;
92  }
93 
94 private:
96  float myX[2];
97  int myBackface;
98 };
99 
101 public:
102  // Static init can be called once before sampling. Please set up the point
103  // positions before this is done...
104  // If the polygon is large (in screen space), please set accurate to 1
106  fpreal x2, fpreal y2, fpreal x3, fpreal y3)
107  {
108  myXY[0][0] = x0; myXY[0][1] = y0;
109  myXY[1][0] = x1; myXY[1][1] = y1;
110  myXY[2][0] = x2; myXY[2][1] = y2;
111  myXY[3][0] = x3; myXY[3][1] = y3;
112  return init();
113  }
114 
115  int init();
116 
117  int sample(double x, double y, float &u, float &v)
118  {
119  return (this->*mySampler)(x, y, u, v);
120  }
121 
122  // Call the sampler after the init. See above for calling syntax
123  int isBackface() { return myBackface; }
124 
125 private:
126  // Trapezoidal sampling
127  int sampleTrap1(double x, double y, float &u, float &v);
128  int sampleTrap2(double x, double y, float &u, float &v);
129 
130  // Convex quadrilateral
131  int sampleQuad(double x, double y, float &u, float &v);
132  // Concave quadrilateral
133  int sampleConcave(double x, double y, float &u, float &v);
134  // Bowtie quad
135  int sampleBowtie(double x, double y, float &u, float &v);
136  int sampleGeneral(double x, double y, float &u, float &v);
137 
138  // Otherwise, only the following is needed
139  float myXY[4][2];
140 
141  UT_QuadSampleFunc mySampler;
142  UT_SampleSimpleTri2D myFirst;
143  UT_SampleSimpleTri2D mySecond;
144  UT_Vector2 myAB, myBC, myCD, myAD, myAE;
145  float myU, myV;
146  int myBackface;
147 };
148 
149 // This class actually performs bilinear sampling, which UT_SampleQuad2D
150 // does not seem to do.
152 public:
153  // Static init can be called once before sampling. Please set up the point
154  // positions before this is done...
155  void setVertices(fpreal x0, fpreal y0, fpreal x1, fpreal y1,
156  fpreal x2, fpreal y2, fpreal x3, fpreal y3);
157 
158  // NOTE: The u & v here are returned using the Mantra style winding
159  // of the polygon. This is opposite of Houdini's winding, so if
160  // this is to be used for evaluateInteriorPoint, one should
161  // reverse the u & v values (or swap x1 & x2)
162  int sample(fpreal x, fpreal y, float &u, float &v);
163 
164 
165 private:
166  int myBackface;
167  int myFlipped;
168  int myGeneral;
169  float mySBx, mySDx;
170 
171  float myAx, myAy, myBx, myBy, myCx, myCy, myDx, myDy;
172  float myK, myIK, myL, myM;
173 };
174 
175 
176 #endif
177 
typedef int(APIENTRYP RE_PFNGLXSWAPINTERVALSGIPROC)(int)
int sample(fpreal sx, fpreal sy, fpreal x0, fpreal y0, float &u, float &v, fpreal tol=0.0)
Definition: UT_PolySample.h:55
void initD0(float x0[2], float x2[2], fpreal dx, fpreal dy)
Definition: UT_PolySample.h:48
const GLdouble * v
Definition: glcorearb.h:837
int sample(double x, double y, float &u, float &v)
void setVertices(fpreal x0, fpreal y0, fpreal x1, fpreal y1, fpreal x2, fpreal y2)
Definition: UT_PolySample.h:40
GLdouble GLdouble GLdouble z
Definition: glcorearb.h:848
#define UT_API
Definition: UT_API.h:14
GLint y
Definition: glcorearb.h:103
int setVertices(fpreal x0, fpreal y0, fpreal x1, fpreal y1, fpreal x2, fpreal y2, fpreal x3, fpreal y3)
void init(float x0[2], float x1[2], float x2[2])
Definition: UT_PolySample.h:34
GLdouble u1
Definition: glad.h:2676
GLdouble GLdouble x2
Definition: glad.h:2349
int(UT_SampleQuad2D::* UT_QuadSampleFunc)(double x, double y, float &u, float &v)
Definition: UT_PolySample.h:25
void setVertices(fpreal x0, fpreal y0, fpreal x1, fpreal y1, fpreal x2, fpreal y2)
Definition: UT_PolySample.h:74
int isBackface() const
Definition: UT_PolySample.h:73
GLdouble y1
Definition: glad.h:2349
GLint GLenum GLint x
Definition: glcorearb.h:409
GLfloat v0
Definition: glcorearb.h:816
fpreal64 fpreal
Definition: SYS_Types.h:277
GLfloat GLfloat v1
Definition: glcorearb.h:817
GLdouble GLdouble GLdouble y2
Definition: glad.h:2349
int sample(fpreal x, fpreal y, float &u, float &v, fpreal tol=0.0)
Definition: UT_PolySample.h:85