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
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 #include <stdlib.h>
00044 #include <stdio.h>
00045
00046 #include "OSGConfig.h"
00047 #include "OSGLog.h"
00048
00049 #include "OSGNormalQuantifier.h"
00050
00051 #define EPSILON 0.0000001
00052
00053 OSG_USING_NAMESPACE
00054
00055
00056
00057
00058 NormalQuantifier::NormalQuantifier(UInt32 numberSubdivisions)
00059 {
00060 build(numberSubdivisions);
00061 }
00062
00063
00064
00065 NormalQuantifier::~NormalQuantifier(void)
00066 {
00067 }
00068
00069
00070
00074 UInt32 NormalQuantifier::getIndex(Vec3f &normal,
00075 UInt32 numberSubdivisions) const
00076 {
00077 UInt32 index;
00078 UInt32 nS;
00079 UInt32 octant;
00080 Int32 xoctant;
00081 Int32 yoctant;
00082 Int32 zoctant;
00083
00084 Vec3f point1;
00085 Vec3f point2;
00086 Vec3f point3;
00087
00088 if(numberSubdivisions == 0)
00089 {
00090 nS = _numberSubdivisions;
00091 }
00092 else if(numberSubdivisions > _numberSubdivisions)
00093 {
00094 nS = _numberSubdivisions;
00095 }
00096 else
00097 {
00098 nS = numberSubdivisions;
00099 }
00100
00101 octant =
00102 (((normal.x() >= 0) ? 0 : 1) << 2) |
00103 (((normal.y() >= 0) ? 0 : 1) << 1) |
00104 ( (normal.z() >= 0) ? 0 : 1);
00105
00106 xoctant = (octant & 4) ? -1 : 1;
00107 yoctant = (octant & 2) ? -1 : 1;
00108 zoctant = (octant & 1) ? -1 : 1;
00109
00110 point1.setValues(0.f * xoctant, 0.f * yoctant, 1.f * zoctant);
00111 point2.setValues(1.f * xoctant, 0.f * yoctant, 0.f * zoctant);
00112 point3.setValues(0.f * xoctant, 1.f * yoctant, 0.f * zoctant);
00113
00114 index = getSubIndex(normal, point1, point2, point3, nS);
00115
00116 index = (octant << (2 * nS)) + index;
00117
00118 return index;
00119 }
00120
00121
00122
00126 void NormalQuantifier::build (UInt32 numberSubdivisions)
00127 {
00128 UInt32 index = 0;
00129 UInt32 nN = ((1 << (2 * numberSubdivisions)) * 8);
00130
00131 _numberSubdivisions = numberSubdivisions;
00132
00133 _normalTable.resize(nN);
00134
00135 if(_numberSubdivisions != 0)
00136 {
00137 for(UInt32 octant = 0; octant < 8; octant++)
00138 {
00139 Int32 xoctant = (octant & 4)>0?-1:1;
00140 Int32 yoctant = (octant & 2)>0?-1:1;
00141 Int32 zoctant = (octant & 1)>0?-1:1;
00142
00143 Vec3f point1(0.f * xoctant, 0.f * yoctant, 1.f * zoctant);
00144 Vec3f point2(1.f * xoctant, 0.f * yoctant, 0.f * zoctant);
00145 Vec3f point3(0.f * xoctant, 1.f * yoctant, 0.f * zoctant);
00146
00147 subdivide(point1, point2, point3, _numberSubdivisions+1, index);
00148 }
00149
00150 if(index != nN)
00151 {
00152 FFATAL(("NormalQuantifier::build() index missmatch!\n"));
00153 }
00154 else
00155 {
00156 FLOG(("NormalQuantifier init: %d subdivision, %d normal\n",
00157 _numberSubdivisions, _normalTable.size()));
00158 }
00159 }
00160 }
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170 bool NormalQuantifier::rayTriangle ( const Vec3f & dir,
00171 const Vec3f & vert0,
00172 const Vec3f & vert1,
00173 const Vec3f & vert2 ) const
00174 {
00175 Vec3f edge1, edge2, tvec, pvec, qvec;
00176 float det,inv_det;
00177 float u,v;
00178 Vec3f orig(0,0,0);
00179
00180
00181 edge1 = vert1 - vert0;
00182 edge2 = vert2 - vert0;
00183
00184
00185 pvec = dir.cross(edge2);
00186
00187
00188 det = edge1.dot(pvec);
00189
00190
00191 if (det > -EPSILON && det < EPSILON)
00192 return false;
00193
00194 inv_det = 1.0f / det;
00195
00196
00197 tvec = orig - vert0;
00198
00199
00200 u = tvec.dot(pvec) *inv_det;
00201
00202 if (u < 0.0 || u > 1.0)
00203 return false;
00204
00205
00206 qvec = tvec.cross(edge1);
00207
00208
00209 v = dir.dot(qvec) * inv_det;
00210
00211 if (v < 0.0 || u + v > 1.0)
00212 return false;
00213
00214
00215
00216
00217 return true;
00218 };
00219
00220
00224 void NormalQuantifier::subdivide(Vec3f point1,
00225 Vec3f point2,
00226 Vec3f point3,
00227 UInt32 number,
00228 UInt32 &index )
00229 {
00230 number--;
00231
00232 if(number == 0)
00233 {
00234 Vec3f newPoint = point1;
00235
00236 newPoint += point2;
00237 newPoint += point3;
00238 newPoint /= 3;
00239
00240 newPoint.normalize();
00241
00242 _normalTable[index] = newPoint;
00243 ++index;
00244 }
00245 else
00246 {
00247 Vec3f newPoint1 = point1;
00248
00249 newPoint1 += point2;
00250 newPoint1 /= 2;
00251 newPoint1.normalize();
00252
00253 Vec3f newPoint2 = point1;
00254
00255 newPoint2 += point3;
00256 newPoint2 /= 2;
00257 newPoint2.normalize();
00258
00259 Vec3f newPoint3 = point2;
00260
00261 newPoint3 += point3;
00262 newPoint3 /= 2;
00263 newPoint3.normalize();
00264
00265 subdivide(point1 , newPoint1, newPoint2, number, index);
00266 subdivide(newPoint1, point2 , newPoint3, number, index);
00267 subdivide(newPoint1, newPoint2, newPoint3, number, index);
00268 subdivide(newPoint2, newPoint3, point3 , number, index);
00269 }
00270 }
00271
00275 UInt32 NormalQuantifier::getSubIndex(Vec3f point,
00276 Vec3f point1,
00277 Vec3f point2,
00278 Vec3f point3,
00279 UInt32 number) const
00280 {
00281 int intersect = -1, index = 0;
00282 Vec3f newPoint1(point1);
00283 Vec3f newPoint2(point1);
00284 Vec3f newPoint3(point2);
00285
00286
00287
00288
00289
00290 newPoint1 += point2;
00291 newPoint1 /= 2;
00292 newPoint1.normalize();
00293
00294 newPoint2 += point3;
00295 newPoint2 /=2;
00296 newPoint2.normalize();
00297
00298 newPoint3 += point3;
00299 newPoint3 /= 2;
00300 newPoint3.normalize();
00301
00302 number--;
00303
00304 if (rayTriangle(point, point1, newPoint1, newPoint2))
00305 intersect = 0;
00306 else
00307 if (rayTriangle(point, newPoint1, point2, newPoint3))
00308 intersect = 1;
00309 else
00310 if ( rayTriangle(point, newPoint1, newPoint2, newPoint3))
00311 intersect = 2;
00312 else
00313 if ( rayTriangle(point, newPoint2, newPoint3, point3))
00314 intersect = 3;
00315
00316 if ( intersect >= 0 ) {
00317
00318 index = intersect;
00319
00320 if (number != 0) {
00321
00322 index = index << (number*2);
00323
00324 switch (intersect) {
00325 case 0:
00326 index += getSubIndex(point, point1, newPoint1, newPoint2, number);
00327 break;
00328 case 1:
00329 index += getSubIndex(point, newPoint1, point2, newPoint3, number);
00330 break;
00331 case 2:
00332 index += getSubIndex(point, newPoint1, newPoint2, newPoint3, number);
00333 break;
00334 case 3:
00335 index += getSubIndex(point, newPoint2, newPoint3, point3, number);
00336 break;
00337 }
00338 }
00339 }
00340 else {
00341 FFATAL (( "Intersect < 0 in NormalQuantifier::getSubIndex()\n"));
00342 }
00343
00344 return index;
00345 }
00346
00347
00348
00349
00350
00351
00352
00353
00354 #ifdef __sgi
00355 #pragma set woff 1174
00356 #endif
00357
00358 #ifdef OSG_LINUX_ICC
00359 #pragma warning( disable : 177 )
00360 #endif
00361
00362 namespace
00363 {
00364 static Char8 cvsid_cpp[] = "@(#)$Id: $";
00365 static Char8 cvsid_hpp[] = OSG_HEADER_CVSID;
00366 static Char8 cvsid_inl[] = OSG_INLINE_CVSID;
00367 }