corner.cpp

Go to the documentation of this file.
00001 /*
00002     mesh3d
00003     Copyright (C) 2010  Timo Suoranta
00004 
00005     This library is free software; you can redistribute it and/or
00006     modify it under the terms of the GNU Lesser General Public
00007     License as published by the Free Software Foundation; either
00008     version 2.1 of the License, or (at your option) any later version.
00009 
00010     This library is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013     Lesser General Public License for more details.
00014 
00015     You should have received a copy of the GNU Lesser General Public
00016     License along with this library; if not, write to the Free Software
00017     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00018 */
00019 
00020 #include "mesh3d/corner.h"
00021 #include "mesh3d/attribute_map.h"
00022 #include "mesh3d/point.h"
00023 #include "mesh3d/vec3.h"
00024 #include <cassert>
00025 
00033 namespace mesh3d {
00034 
00035 /*  Computer corner normal with max smoothing angle  */ 
00036 /*  Initialize corner normal to polygon normal,      */ 
00037 /*  loop through all polygons contributing to the    */ 
00038 /*  corner point, add all other polygon normals      */ 
00039 /*  that are not too different, take average         */ 
00040 void corner::compute_normal(
00041     std::tr1::shared_ptr<attribute_map<mesh3d::corner*,  vec3> > corner_normals,
00042     std::tr1::shared_ptr<attribute_map<mesh3d::polygon*, vec3> > polygon_normals,
00043     std::tr1::shared_ptr<attribute_map<mesh3d::point*,   vec3> > point_locations,
00044     float                                                        cos_max_smoothing_angle
00045 )
00046 {
00047     mesh3d::polygon *pol = polygon();
00048     mesh3d::point   *pnt = point  ();
00049 
00050     vec3 corner_normal;
00051  
00052     if(polygon_normals->has(pol) == false)
00053     {
00054         corner_normal = vec3(0, 0, 0);
00055     }
00056     else
00057     {
00058         vec3 polygon_normal = polygon_normals->value(pol);
00059         corner_normal       = polygon_normal;
00060 
00061         size_t point_corners = 0;
00062         size_t participants  = 0;
00063         for(
00064             point::corner_collection::iterator i = pnt->corners().begin();
00065             i != pnt->corners().end();
00066             ++i
00067         )
00068         {
00069             ++point_corners;
00070             mesh3d::corner  *point_corner = *i;
00071             mesh3d::polygon *neighbor_pol = point_corner->polygon();
00072             if( 
00073                 (pol != neighbor_pol                ) &&
00074                 (polygon_normals->has(neighbor_pol) ) &&
00075                 (neighbor_pol->corners().size() > 2)
00076             )
00077             {
00078                 vec3 neighbor_normal = polygon_normals->value(neighbor_pol);
00079                 float cos_angle = dot(
00080                     polygon_normal, 
00081                     neighbor_normal
00082                 );
00083                 if(cos_angle < cos_max_smoothing_angle)
00084                 {
00085                     corner_normal += neighbor_normal;
00086                     ++participants;
00087                 }
00088             }
00089         }
00090 
00091         corner_normal = normalize(corner_normal);
00092     }
00093     corner_normals->set_value(this, corner_normal);
00094 }
00095 
00096 }  /*  namespace mesh3d  */ 
00097 
Generated on Sun Apr 11 12:23:08 2010 for RenderStack by  doxygen 1.6.3