1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
|
/****************************************************************************
* VCGLib o o *
* Visual and Computer Graphics Library o o *
* _ O _ *
* Copyright(C) 2004 \/)\/ *
* Visual Computing Lab /\/| *
* ISTI - Italian National Research Council | *
* \ *
* All rights reserved. *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt) *
* for more details. *
* *
****************************************************************************/
#ifndef __VCG_TRI_UPDATE_NORMALS
#define __VCG_TRI_UPDATE_NORMALS
#include <vcg/space/triangle3.h>
#include <vcg/math/matrix33.h>
#include <vcg/simplex/face/component.h>
#include <vcg/complex/algorithms/update/normal.h>
#include <vcg/complex/algorithms/update/flag.h>
namespace vcg {
namespace tri {
/// \ingroup trimesh
/// \headerfile normal.h vcg/complex/algorithms/update/normal.h
/// \brief Management, updating and computation of per-vertex and per-face normals.
/**
This class is used to compute or update the normals that can be stored in the vertex or face component of a mesh.
*/
template <class ComputeMeshType>
class UpdateNormals
{
public:
typedef ComputeMeshType MeshType;
typedef typename MeshType::VertexType VertexType;
typedef typename MeshType::CoordType CoordType;
typedef typename VertexType::NormalType NormalType;
typedef typename VertexType::ScalarType ScalarType;
typedef typename MeshType::VertexPointer VertexPointer;
typedef typename MeshType::VertexIterator VertexIterator;
typedef typename MeshType::FaceType FaceType;
typedef typename MeshType::FacePointer FacePointer;
typedef typename MeshType::FaceIterator FaceIterator;
/**
Set to zero all the normals. Usued by all the face averaging algorithms.
by default it does not clear the normals of unreferenced vertices because they could be still useful
*/
static void PerVertexClear(ComputeMeshType &m, bool ClearAllVertNormal=false)
{
assert(HasPerVertexNormal(m));
if(ClearAllVertNormal)
UpdateFlags<ComputeMeshType>::VertexClearV(m);
else
{
UpdateFlags<ComputeMeshType>::VertexSetV(m);
for(FaceIterator f=m.face.begin();f!=m.face.end();++f)
if( !(*f).IsD() )
for(int i=0;i<3;++i) (*f).V(i)->ClearV();
}
VertexIterator vi;
for(vi=m.vert.begin();vi!=m.vert.end();++vi)
if( !(*vi).IsD() && (*vi).IsRW() && (!(*vi).IsV()) )
(*vi).N() = NormalType((ScalarType)0,(ScalarType)0,(ScalarType)0);
}
/// \brief Calculates the face normal (if stored in the current face type)
static void PerFace(ComputeMeshType &m)
{
if( !HasPerFaceNormal(m)) return;
FaceIterator f;
for(f=m.face.begin();f!=m.face.end();++f)
if( !(*f).IsD() ) face::ComputeNormal(*f);
}
/// \brief Calculates the vertex normal. Exploiting or current face normals.
/**
The normal of a vertex v is the weigthed average of the normals of the faces incident on v.
*/
static void PerVertexFromCurrentFaceNormal(ComputeMeshType &m)
{
if( !HasPerVertexNormal(m)) return;
VertexIterator vi;
for(vi=m.vert.begin();vi!=m.vert.end();++vi)
if( !(*vi).IsD() && (*vi).IsRW() )
(*vi).N()=CoordType(0,0,0);
FaceIterator fi;
for(fi=m.face.begin();fi!=m.face.end();++fi)
if( !(*fi).IsD())
{
for(int j=0; j<3; ++j)
if( !(*fi).V(j)->IsD())
(*fi).V(j)->N() += (*fi).cN();
}
}
/// \brief Calculates the vertex normal. Exploiting or current face normals.
/**
The normal of a face f is the average of the normals of the vertices of f.
*/
static void PerFaceFromCurrentVertexNormal(ComputeMeshType &m)
{
for (FaceIterator fi=m.face.begin(); fi!=m.face.end(); ++fi)
if( !(*fi).IsD())
{
NormalType n;
n.SetZero();
for(int j=0; j<3; ++j)
n += fi->V(j)->cN();
n.Normalize();
fi->N() = n;
}
}
/// \brief Calculates the vertex normal. Without exploiting or touching face normals.
/**
The normal of a vertex v computed as a weighted sum f the incident face normals.
The weight is simlply the angle of the involved wedge. Described in:
G. Thurmer, C. A. Wuthrich
"Computing vertex normals from polygonal facets"
Journal of Graphics Tools, 1998
*/
static void PerVertexAngleWeighted(ComputeMeshType &m)
{
assert(HasPerVertexNormal(m));
PerVertexClear(m);
FaceIterator f;
for(f=m.face.begin();f!=m.face.end();++f)
if( !(*f).IsD() && (*f).IsR() )
{
typename FaceType::NormalType t = vcg::NormalizedNormal(*f);
NormalType e0 = ((*f).V1(0)->cP()-(*f).V0(0)->cP()).Normalize();
NormalType e1 = ((*f).V1(1)->cP()-(*f).V0(1)->cP()).Normalize();
NormalType e2 = ((*f).V1(2)->cP()-(*f).V0(2)->cP()).Normalize();
(*f).V(0)->N() += t*AngleN(e0,-e2);
(*f).V(1)->N() += t*AngleN(-e0,e1);
(*f).V(2)->N() += t*AngleN(-e1,e2);
}
}
/// \brief Calculates the vertex normal. Without exploiting or touching face normals.
/**
The normal of a vertex v is computed according to the formula described by Nelson Max in
Max, N., "Weights for Computing Vertex Normals from Facet Normals", Journal of Graphics Tools, 4(2) (1999)
The weight for each wedge is the cross product of the two edge over the product of the square of the two edge lengths.
According to the original paper it is perfect only for spherical surface, but it should perform well...
*/
static void PerVertexNelsonMaxWeighted(ComputeMeshType &m)
{
assert(HasPerVertexNormal(m));
PerVertexClear(m);
FaceIterator f;
for(f=m.face.begin();f!=m.face.end();++f)
if( !(*f).IsD() && (*f).IsR() )
{
typename FaceType::NormalType t = vcg::Normal(*f);
ScalarType e0 = SquaredDistance((*f).V0(0)->cP(),(*f).V1(0)->cP());
ScalarType e1 = SquaredDistance((*f).V0(1)->cP(),(*f).V1(1)->cP());
ScalarType e2 = SquaredDistance((*f).V0(2)->cP(),(*f).V1(2)->cP());
(*f).V(0)->N() += t/(e0*e2);
(*f).V(1)->N() += t/(e0*e1);
(*f).V(2)->N() += t/(e1*e2);
}
}
/// \brief Calculates the vertex normal. Without exploiting or touching face normals.
/**
The normal of a vertex v is the classical area weigthed average of the normals of the faces incident on v.
*/
static void PerVertex(ComputeMeshType &m)
{
assert(HasPerVertexNormal(m));
PerVertexClear(m);
FaceIterator f;
for(f=m.face.begin();f!=m.face.end();++f)
if( !(*f).IsD() && (*f).IsR() )
{
//typename FaceType::NormalType t = (*f).Normal();
typename FaceType::NormalType t = vcg::Normal(*f);
for(int j=0; j<3; ++j)
if( !(*f).V(j)->IsD() && (*f).V(j)->IsRW() )
(*f).V(j)->N() += t;
}
}
/// \brief Calculates both vertex and face normals.
/**
The normal of a vertex v is the weigthed average of the normals of the faces incident on v.
*/
static void PerVertexPerFace(ComputeMeshType &m)
{
if( !HasPerVertexNormal(m) || !HasPerFaceNormal(m)) return;
PerFace(m);
PerVertexClear(m);
FaceIterator f;
for(f=m.face.begin();f!=m.face.end();++f)
if( !(*f).IsD() && (*f).IsR() )
{
for(int j=0; j<3; ++j)
if( !(*f).V(j)->IsD() && (*f).V(j)->IsRW() )
(*f).V(j)->N() += (*f).cN();
}
}
/// \brief Calculates both vertex and face normals.
/**
The normal of a vertex v is the weigthed average of the normals of the faces incident on v.
*/
static void PerVertexNormalizedPerFace(ComputeMeshType &m)
{
PerVertexPerFace(m);
NormalizeVertex(m);
}
/// \brief Normalize the lenght of the face normals.
static void NormalizeVertex(ComputeMeshType &m)
{
VertexIterator vi;
for(vi=m.vert.begin();vi!=m.vert.end();++vi)
if( !(*vi).IsD() && (*vi).IsRW() )
(*vi).N().Normalize();
}
/// \brief Normalize the lenght of the face normals.
static void NormalizeFace(ComputeMeshType &m)
{
FaceIterator fi;
for(fi=m.face.begin();fi!=m.face.end();++fi)
if( !(*fi).IsD() ) (*fi).N().Normalize();
}
static void AreaNormalizeFace(ComputeMeshType &m)
{
FaceIterator fi;
for(fi=m.face.begin();fi!=m.face.end();++fi)
if( !(*fi).IsD() )
{
(*fi).N().Normalize();
(*fi).N() = (*fi).N() * DoubleArea(*fi);
}
}
static void PerVertexNormalizedPerFaceNormalized(ComputeMeshType &m)
{
PerVertexNormalizedPerFace(m);
NormalizeFace(m);
}
static void PerFaceRW(ComputeMeshType &m, bool normalize=false)
{
if( !HasPerFaceNormal(m)) return;
FaceIterator f;
bool cn = true;
if(normalize)
{
for(f=m.m.face.begin();f!=m.m.face.end();++f)
if( !(*f).IsD() && (*f).IsRW() )
{
for(int j=0; j<3; ++j)
if( !(*f).V(j)->IsR()) cn = false;
if( cn ) face::ComputeNormalizedNormal(*f);
cn = true;
}
}
else
{
for(f=m.m.face.begin();f!=m.m.face.end();++f)
if( !(*f).IsD() && (*f).IsRW() )
{
for(int j=0; j<3; ++j)
if( !(*f).V(j)->IsR()) cn = false;
if( cn )
(*f).ComputeNormal();
cn = true;
}
}
}
static void PerFaceNormalized(ComputeMeshType &m)
{
if( !HasPerFaceNormal(m)) return;
FaceIterator f;
for(f=m.face.begin();f!=m.face.end();++f)
if( !(*f).IsD() ) face::ComputeNormalizedNormal(*f);
}
static void PerBitQuadFaceNormalized(ComputeMeshType &m)
{
if( !HasPerFaceNormal(m)) return;
PerFace(m);
FaceIterator f;
for(f=m.face.begin();f!=m.face.end();++f) {
if( !(*f).IsD() ) {
for (int k=0; k<3; k++) if (f->IsF(k))
if (&*f < f->FFp(k)) {
f->N() = f->FFp(k)->N() = (f->FFp(k)->N() + f->N()).Normalize();
}
}
}
}
/// \brief Calculates the vertex normal.
static void PerVertexNormalized(ComputeMeshType &m)
{
if( !HasPerVertexNormal(m)) return;
PerVertex(m);
for(VertexIterator vi=m.vert.begin();vi!=m.vert.end();++vi)
if( !(*vi).IsD() && (*vi).IsRW() )
(*vi).N().Normalize();
}
/// \brief Multiply the vertex normals by the matrix passed. By default, the scale component is removed.
static void PerVertexMatrix(ComputeMeshType &m, const Matrix44<ScalarType> &mat, bool remove_scaling= true){
float scale;
Matrix33<ScalarType> mat33(mat,3);
if( !HasPerVertexNormal(m)) return;
if(remove_scaling){
scale = pow(mat33.Determinant(),(ScalarType)(1.0/3.0));
mat33[0][0]/=scale;
mat33[1][1]/=scale;
mat33[2][2]/=scale;
}
for(VertexIterator vi=m.vert.begin();vi!=m.vert.end();++vi)
if( !(*vi).IsD() && (*vi).IsRW() )
(*vi).N() = mat33*(*vi).N();
}
/// \brief Multiply the face normals by the matrix passed. By default, the scale component is removed.
static void PerFaceMatrix(ComputeMeshType &m, const Matrix44<ScalarType> &mat, bool remove_scaling= true){
float scale;
Matrix33<ScalarType> mat33(mat,3);
if( !HasPerFaceNormal(m)) return;
if(remove_scaling){
scale = pow(mat33.Determinant(),ScalarType(1.0/3.0));
mat33[0][0]/=scale;
mat33[1][1]/=scale;
mat33[2][2]/=scale;
}
for(FaceIterator fi=m.face.begin();fi!=m.face.end();++fi)
if( !(*fi).IsD() && (*fi).IsRW() )
(*fi).N() = mat33* (*fi).N();
}
}; // end class
} // End namespace
} // End namespace
#endif
|