00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef __VCG_TRI_UPDATE_NORMALS
00025 #define __VCG_TRI_UPDATE_NORMALS
00026
00027 #include <vcg/space/triangle3.h>
00028 #include <vcg/math/matrix33.h>
00029 #include <vcg/simplex/face/component.h>
00030
00031 namespace vcg {
00032 namespace tri {
00033
00035
00037
00039
00043 template <class ComputeMeshType>
00044 class UpdateNormals
00045 {
00046 public:
00047 typedef ComputeMeshType MeshType;
00048 typedef typename MeshType::VertexType VertexType;
00049 typedef typename MeshType::CoordType CoordType;
00050 typedef typename VertexType::NormalType NormalType;
00051 typedef typename VertexType::ScalarType ScalarType;
00052 typedef typename MeshType::VertexPointer VertexPointer;
00053 typedef typename MeshType::VertexIterator VertexIterator;
00054 typedef typename MeshType::FaceType FaceType;
00055 typedef typename MeshType::FacePointer FacePointer;
00056 typedef typename MeshType::FaceIterator FaceIterator;
00057
00059
00060 static void PerFace(ComputeMeshType &m)
00061 {
00062 if( !m.HasPerFaceNormal()) return;
00063 FaceIterator f;
00064 for(f=m.face.begin();f!=m.face.end();++f)
00065 if( !(*f).IsD() ) face::ComputeNormal(*f);
00066 }
00067
00069
00072 static void PerVertexFromCurrentFaceNormal(ComputeMeshType &m)
00073 {
00074 if( !m.HasPerVertexNormal()) return;
00075
00076 VertexIterator vi;
00077 for(vi=m.vert.begin();vi!=m.vert.end();++vi)
00078 if( !(*vi).IsD() && (*vi).IsRW() )
00079 (*vi).N()=CoordType(0,0,0);
00080
00081 FaceIterator fi;
00082 for(fi=m.face.begin();fi!=m.face.end();++fi)
00083 if( !(*fi).IsD())
00084 {
00085 for(int j=0; j<3; ++j)
00086 if( !(*fi).V(j)->IsD())
00087 (*fi).V(j)->N() += (*fi).cN();
00088 }
00089 }
00091
00094 static void PerFaceFromCurrentVertexNormal(ComputeMeshType &m)
00095 {
00096 for (FaceIterator fi=m.face.begin(); fi!=m.face.end(); ++fi)
00097 if( !(*fi).IsD())
00098 {
00099 NormalType n;
00100 n.SetZero();
00101 for(int j=0; j<3; ++j)
00102 n += fi->V(j)->cN();
00103 n.Normalize();
00104 fi->N() = n;
00105 }
00106 }
00107
00108
00110
00119 static void PerVertexAngleWeighted(ComputeMeshType &m)
00120 {
00121 assert(HasPerVertexNormal(m));
00122 VertexIterator vi;
00123 for(vi=m.vert.begin();vi!=m.vert.end();++vi)
00124 if( !(*vi).IsD() && (*vi).IsRW() )
00125 (*vi).N() = NormalType((ScalarType)0,(ScalarType)0,(ScalarType)0);
00126
00127 FaceIterator f;
00128 for(f=m.face.begin();f!=m.face.end();++f)
00129 if( !(*f).IsD() && (*f).IsR() )
00130 {
00131 typename FaceType::NormalType t = vcg::NormalizedNormal(*f);
00132 NormalType e0 = ((*f).V1(0)->cP()-(*f).V0(0)->cP()).Normalize();
00133 NormalType e1 = ((*f).V1(1)->cP()-(*f).V0(1)->cP()).Normalize();
00134 NormalType e2 = ((*f).V1(2)->cP()-(*f).V0(2)->cP()).Normalize();
00135
00136 (*f).V(0)->N() += t*AngleN(e0,-e2);
00137 (*f).V(1)->N() += t*AngleN(-e0,e1);
00138 (*f).V(2)->N() += t*AngleN(-e1,e2);
00139 }
00140 }
00141
00143
00150 static void PerVertexWeighted(ComputeMeshType &m)
00151 {
00152 assert(HasPerVertexNormal(m));
00153 VertexIterator vi;
00154 for(vi=m.vert.begin();vi!=m.vert.end();++vi)
00155 if( !(*vi).IsD() && (*vi).IsRW() )
00156 (*vi).N() = NormalType((ScalarType)0,(ScalarType)0,(ScalarType)0);
00157
00158 FaceIterator f;
00159
00160 for(f=m.face.begin();f!=m.face.end();++f)
00161 if( !(*f).IsD() && (*f).IsR() )
00162 {
00163 typename FaceType::NormalType t = vcg::Normal(*f);
00164 ScalarType e0 = SquaredDistance((*f).V0(0)->cP(),(*f).V1(0)->cP());
00165 ScalarType e1 = SquaredDistance((*f).V0(1)->cP(),(*f).V1(1)->cP());
00166 ScalarType e2 = SquaredDistance((*f).V0(2)->cP(),(*f).V1(2)->cP());
00167
00168 (*f).V(0)->N() += t/(e0*e2);
00169 (*f).V(1)->N() += t/(e0*e1);
00170 (*f).V(2)->N() += t/(e1*e2);
00171 }
00172 }
00173
00175
00179 static void PerVertex(ComputeMeshType &m)
00180 {
00181 assert(HasPerVertexNormal(m));
00182
00183 VertexIterator vi;
00184 for(vi=m.vert.begin();vi!=m.vert.end();++vi)
00185 if( !(*vi).IsD() && (*vi).IsRW() )
00186 (*vi).N() = NormalType((ScalarType)0,(ScalarType)0,(ScalarType)0);
00187
00188 FaceIterator f;
00189
00190 for(f=m.face.begin();f!=m.face.end();++f)
00191 if( !(*f).IsD() && (*f).IsR() )
00192 {
00193
00194 typename FaceType::NormalType t = vcg::Normal(*f);
00195
00196 for(int j=0; j<3; ++j)
00197 if( !(*f).V(j)->IsD() && (*f).V(j)->IsRW() )
00198 (*f).V(j)->N() += t;
00199 }
00200 }
00201
00202
00204
00208 static void PerVertexPerFace(ComputeMeshType &m)
00209 {
00210 if( !m.HasPerVertexNormal() || !m.HasPerFaceNormal()) return;
00211
00212 PerFace(m);
00213 VertexIterator vi;
00214 for(vi=m.vert.begin();vi!=m.vert.end();++vi)
00215 if( !(*vi).IsD() && (*vi).IsRW() )
00216 (*vi).N() = NormalType((ScalarType)0,(ScalarType)0,(ScalarType)0);
00217
00218 FaceIterator f;
00219
00220 for(f=m.face.begin();f!=m.face.end();++f)
00221 if( !(*f).IsD() && (*f).IsR() )
00222 {
00223 for(int j=0; j<3; ++j)
00224 if( !(*f).V(j)->IsD() && (*f).V(j)->IsRW() )
00225 (*f).V(j)->N() += (*f).cN();
00226 }
00227 }
00228
00230
00234 static void PerVertexNormalizedPerFace(ComputeMeshType &m)
00235 {
00236 PerVertexPerFace(m);
00237 NormalizeVertex(m);
00238 }
00239
00241 static void NormalizeVertex(ComputeMeshType &m)
00242 {
00243 VertexIterator vi;
00244 for(vi=m.vert.begin();vi!=m.vert.end();++vi)
00245 if( !(*vi).IsD() && (*vi).IsRW() )
00246 (*vi).N().Normalize();
00247 }
00248
00250 static void NormalizeFace(ComputeMeshType &m)
00251 {
00252 FaceIterator fi;
00253 for(fi=m.face.begin();fi!=m.face.end();++fi)
00254 if( !(*fi).IsD() ) (*fi).N().Normalize();
00255 }
00256
00257 static void AreaNormalizeFace(ComputeMeshType &m)
00258 {
00259 FaceIterator fi;
00260 for(fi=m.face.begin();fi!=m.face.end();++fi)
00261 if( !(*fi).IsD() )
00262 {
00263 (*fi).N().Normalize();
00264 (*fi).N() = (*fi).N() * DoubleArea(*fi);
00265 }
00266 }
00267
00268 static void PerVertexNormalizedPerFaceNormalized(ComputeMeshType &m)
00269 {
00270 PerVertexNormalizedPerFace(m);
00271 NormalizeFace(m);
00272 }
00273
00274 static void PerFaceRW(ComputeMeshType &m, bool normalize=false)
00275 {
00276 if( !m.HasPerFaceNormal()) return;
00277
00278 FaceIterator f;
00279 bool cn = true;
00280
00281 if(normalize)
00282 {
00283 for(f=m.m.face.begin();f!=m.m.face.end();++f)
00284 if( !(*f).IsD() && (*f).IsRW() )
00285 {
00286 for(int j=0; j<3; ++j)
00287 if( !(*f).V(j)->IsR()) cn = false;
00288 if( cn ) face::ComputeNormalizedNormal(*f);
00289 cn = true;
00290 }
00291 }
00292 else
00293 {
00294 for(f=m.m.face.begin();f!=m.m.face.end();++f)
00295 if( !(*f).IsD() && (*f).IsRW() )
00296 {
00297 for(int j=0; j<3; ++j)
00298 if( !(*f).V(j)->IsR()) cn = false;
00299
00300 if( cn )
00301 (*f).ComputeNormal();
00302 cn = true;
00303 }
00304 }
00305 }
00306
00307
00308 static void PerFaceNormalized(ComputeMeshType &m)
00309 {
00310 if( !m.HasPerFaceNormal()) return;
00311 FaceIterator f;
00312 for(f=m.face.begin();f!=m.face.end();++f)
00313 if( !(*f).IsD() ) face::ComputeNormalizedNormal(*f);
00314 }
00315
00316 static void PerBitQuadFaceNormalized(ComputeMeshType &m)
00317 {
00318 if( !m.HasPerFaceNormal()) return;
00319 PerFace(m);
00320
00321 FaceIterator f;
00322 for(f=m.face.begin();f!=m.face.end();++f) {
00323 if( !(*f).IsD() ) {
00324 for (int k=0; k<3; k++) if (f->IsF(k))
00325 if (&*f < f->FFp(k)) {
00326 f->N() = f->FFp(k)->N() = (f->FFp(k)->N() + f->N()).Normalize();
00327 }
00328 }
00329 }
00330 }
00331
00332
00334 static void PerVertexNormalized(ComputeMeshType &m)
00335 {
00336 if( !m.HasPerVertexNormal()) return;
00337 PerVertex(m);
00338 for(VertexIterator vi=m.vert.begin();vi!=m.vert.end();++vi)
00339 if( !(*vi).IsD() && (*vi).IsRW() )
00340 (*vi).N().Normalize();
00341 }
00342
00344 static void PerVertexMatrix(ComputeMeshType &m, const Matrix44<ScalarType> &mat, bool remove_scaling= true){
00345 float scale;
00346
00347 Matrix33<ScalarType> mat33(mat,3);
00348
00349 if( !m.HasPerVertexNormal()) return;
00350
00351 if(remove_scaling){
00352 scale = pow(mat33.Determinant(),(ScalarType)(1.0/3.0));
00353 mat33[0][0]/=scale;
00354 mat33[1][1]/=scale;
00355 mat33[2][2]/=scale;
00356 }
00357
00358 for(VertexIterator vi=m.vert.begin();vi!=m.vert.end();++vi)
00359 if( !(*vi).IsD() && (*vi).IsRW() )
00360 (*vi).N() = mat33*(*vi).N();
00361 }
00362
00364 static void PerFaceMatrix(ComputeMeshType &m, const Matrix44<ScalarType> &mat, bool remove_scaling= true){
00365 float scale;
00366
00367 Matrix33<ScalarType> mat33(mat,3);
00368
00369 if( !m.HasPerFaceNormal()) return;
00370
00371 if(remove_scaling){
00372 scale = pow(mat33.Determinant(),ScalarType(1.0/3.0));
00373 mat33[0][0]/=scale;
00374 mat33[1][1]/=scale;
00375 mat33[2][2]/=scale;
00376 }
00377
00378 for(FaceIterator fi=m.face.begin();fi!=m.face.end();++fi)
00379 if( !(*fi).IsD() && (*fi).IsRW() )
00380 (*fi).N() = mat33* (*fi).N();
00381 }
00382
00383 };
00384
00385 }
00386 }
00387
00388
00389 #endif