Warning, /include/Geant4/tools/glutess/normal is written in an unsupported language. File is not indexed.
0001 // see license file for original license.
0002
0003 #ifndef tools_glutess_normal
0004 #define tools_glutess_normal
0005
0006 #include "_tess"
0007
0008 /* __gl_projectPolygon( tess ) determines the polygon normal
0009 * and project vertices onto the plane of the polygon.
0010 */
0011 //void __gl_projectPolygon( GLUtesselator *tess );
0012
0013 ////////////////////////////////////////////////////////
0014 /// inlined C code : ///////////////////////////////////
0015 ////////////////////////////////////////////////////////
0016 #include <cmath>
0017
0018 #define Dot(u,v) (u[0]*v[0] + u[1]*v[1] + u[2]*v[2])
0019
0020 inline/*static*/ int static_LongAxis( GLUdouble v[3] )
0021 {
0022 int i = 0;
0023
0024 if( GLU_ABS(v[1]) > GLU_ABS(v[0]) ) { i = 1; }
0025 if( GLU_ABS(v[2]) > GLU_ABS(v[i]) ) { i = 2; }
0026 return i;
0027 }
0028
0029 inline/*static*/ void static_ComputeNormal( GLUtesselator *tess, GLUdouble norm[3] )
0030 {
0031 GLUvertex *v, *v1, *v2;
0032 GLUdouble c, tLen2, maxLen2;
0033 GLUdouble maxVal[3], minVal[3], d1[3], d2[3], tNorm[3];
0034 GLUvertex *maxVert[3], *minVert[3];
0035 GLUvertex *vHead = &tess->mesh->vHead;
0036 int i;
0037
0038 maxVal[0] = maxVal[1] = maxVal[2] = -2 * GLU_TESS_MAX_COORD;
0039 minVal[0] = minVal[1] = minVal[2] = 2 * GLU_TESS_MAX_COORD;
0040
0041 minVert[0] = 0;minVert[1] = 0;minVert[2] = 0; //G.Barrand : to quiet Coverity.
0042 maxVert[0] = 0;maxVert[1] = 0;maxVert[2] = 0; //G.Barrand : to quiet Coverity.
0043
0044 for( v = vHead->next; v != vHead; v = v->next ) {
0045 for( i = 0; i < 3; ++i ) {
0046 c = v->coords[i];
0047 if( c < minVal[i] ) { minVal[i] = c; minVert[i] = v; }
0048 if( c > maxVal[i] ) { maxVal[i] = c; maxVert[i] = v; }
0049 }
0050 }
0051
0052 /* Find two vertices separated by at least 1/sqrt(3) of the maximum
0053 * distance between any two vertices
0054 */
0055 i = 0;
0056 if( maxVal[1] - minVal[1] > maxVal[0] - minVal[0] ) { i = 1; }
0057 if( maxVal[2] - minVal[2] > maxVal[i] - minVal[i] ) { i = 2; }
0058 if( minVal[i] >= maxVal[i] ) {
0059 /* All vertices are the same -- normal doesn't matter */
0060 norm[0] = 0; norm[1] = 0; norm[2] = 1;
0061 return;
0062 }
0063
0064 /* Look for a third vertex which forms the triangle with maximum area
0065 * (Length of normal == twice the triangle area)
0066 */
0067 maxLen2 = 0;
0068 v1 = minVert[i];
0069 v2 = maxVert[i];
0070 if( !v1 || !v2 ) {norm[0] = 0; norm[1] = 0; norm[2] = 1;return;} //G.Barrand.
0071 d1[0] = v1->coords[0] - v2->coords[0];
0072 d1[1] = v1->coords[1] - v2->coords[1];
0073 d1[2] = v1->coords[2] - v2->coords[2];
0074 for( v = vHead->next; v != vHead; v = v->next ) {
0075 d2[0] = v->coords[0] - v2->coords[0];
0076 d2[1] = v->coords[1] - v2->coords[1];
0077 d2[2] = v->coords[2] - v2->coords[2];
0078 tNorm[0] = d1[1]*d2[2] - d1[2]*d2[1];
0079 tNorm[1] = d1[2]*d2[0] - d1[0]*d2[2];
0080 tNorm[2] = d1[0]*d2[1] - d1[1]*d2[0];
0081 tLen2 = tNorm[0]*tNorm[0] + tNorm[1]*tNorm[1] + tNorm[2]*tNorm[2];
0082 if( tLen2 > maxLen2 ) {
0083 maxLen2 = tLen2;
0084 norm[0] = tNorm[0];
0085 norm[1] = tNorm[1];
0086 norm[2] = tNorm[2];
0087 }
0088 }
0089
0090 if( maxLen2 <= 0 ) {
0091 /* All points lie on a single line -- any decent normal will do */
0092 norm[0] = norm[1] = norm[2] = 0;
0093 norm[static_LongAxis(d1)] = 1;
0094 }
0095 }
0096
0097
0098 inline/*static*/ void static_CheckOrientation( GLUtesselator *tess )
0099 {
0100 GLUdouble area;
0101 GLUface *f, *fHead = &tess->mesh->fHead;
0102 GLUvertex *v, *vHead = &tess->mesh->vHead;
0103 GLUhalfEdge *e;
0104
0105 /* When we compute the normal automatically, we choose the orientation
0106 * so that the sum of the signed areas of all contours is non-negative.
0107 */
0108 area = 0;
0109 for( f = fHead->next; f != fHead; f = f->next ) {
0110 e = f->anEdge;
0111 if( e->winding <= 0 ) continue;
0112 do {
0113 area += (e->Org->s - e->Dst->s) * (e->Org->t + e->Dst->t);
0114 e = e->Lnext;
0115 } while( e != f->anEdge );
0116 }
0117 if( area < 0 ) {
0118 /* Reverse the orientation by flipping all the t-coordinates */
0119 for( v = vHead->next; v != vHead; v = v->next ) {
0120 v->t = - v->t;
0121 }
0122 tess->tUnit[0] = - tess->tUnit[0];
0123 tess->tUnit[1] = - tess->tUnit[1];
0124 tess->tUnit[2] = - tess->tUnit[2];
0125 }
0126 }
0127
0128 #if defined(SLANTED_SWEEP)
0129 /* The "feature merging" is not intended to be complete. There are
0130 * special cases where edges are nearly parallel to the sweep line
0131 * which are not implemented. The algorithm should still behave
0132 * robustly (ie. produce a reasonable tesselation) in the presence
0133 * of such edges, however it may miss features which could have been
0134 * merged. We could minimize this effect by choosing the sweep line
0135 * direction to be something unusual (ie. not parallel to one of the
0136 * coordinate axes).
0137 */
0138 #define S_UNIT_X 0.50941539564955385 /* Pre-normalized */
0139 #define S_UNIT_Y 0.86052074622010633
0140 #else
0141 #define S_UNIT_X 1.0
0142 #define S_UNIT_Y 0.0
0143 #endif
0144
0145 /* Determine the polygon normal and project vertices onto the plane
0146 * of the polygon.
0147 */
0148 inline void __gl_projectPolygon( GLUtesselator *tess )
0149 {
0150 GLUvertex *v, *vHead = &tess->mesh->vHead;
0151 GLUdouble norm[3];
0152 GLUdouble *sUnit, *tUnit;
0153 int i, computedNormal = TOOLS_GLU_FALSE;
0154
0155 norm[0] = tess->normal[0];
0156 norm[1] = tess->normal[1];
0157 norm[2] = tess->normal[2];
0158 if( norm[0] == 0 && norm[1] == 0 && norm[2] == 0 ) {
0159 static_ComputeNormal( tess, norm );
0160 computedNormal = TOOLS_GLU_TRUE;
0161 }
0162 sUnit = tess->sUnit;
0163 tUnit = tess->tUnit;
0164 i = static_LongAxis( norm );
0165
0166 #if defined(FOR_TRITE_TEST_PROGRAM) || defined(TRUE_PROJECT)
0167 /* Choose the initial sUnit vector to be approximately perpendicular
0168 * to the normal.
0169 */
0170 Normalize( norm );
0171
0172 sUnit[i] = 0;
0173 sUnit[(i+1)%3] = S_UNIT_X;
0174 sUnit[(i+2)%3] = S_UNIT_Y;
0175
0176 /* Now make it exactly perpendicular */
0177 w = Dot( sUnit, norm );
0178 sUnit[0] -= w * norm[0];
0179 sUnit[1] -= w * norm[1];
0180 sUnit[2] -= w * norm[2];
0181 Normalize( sUnit );
0182
0183 /* Choose tUnit so that (sUnit,tUnit,norm) form a right-handed frame */
0184 tUnit[0] = norm[1]*sUnit[2] - norm[2]*sUnit[1];
0185 tUnit[1] = norm[2]*sUnit[0] - norm[0]*sUnit[2];
0186 tUnit[2] = norm[0]*sUnit[1] - norm[1]*sUnit[0];
0187 Normalize( tUnit );
0188 #else
0189 /* Project perpendicular to a coordinate axis -- better numerically */
0190 sUnit[i] = 0;
0191 sUnit[(i+1)%3] = S_UNIT_X;
0192 sUnit[(i+2)%3] = S_UNIT_Y;
0193
0194 tUnit[i] = 0;
0195 tUnit[(i+1)%3] = (norm[i] > 0) ? -S_UNIT_Y : S_UNIT_Y;
0196 tUnit[(i+2)%3] = (norm[i] > 0) ? S_UNIT_X : -S_UNIT_X;
0197 #endif
0198
0199 /* Project the vertices onto the sweep plane */
0200 for( v = vHead->next; v != vHead; v = v->next ) {
0201 v->s = Dot( v->coords, sUnit );
0202 v->t = Dot( v->coords, tUnit );
0203 }
0204 if( computedNormal ) {
0205 static_CheckOrientation( tess );
0206 }
0207 }
0208
0209 #endif