Blender V2.61 - r43446
|
00001 /* 00002 * ***** BEGIN GPL LICENSE BLOCK ***** 00003 * 00004 * This program is free software; you can redistribute it and/or 00005 * modify it under the terms of the GNU General Public License 00006 * as published by the Free Software Foundation; either version 2 00007 * of the License, or (at your option) any later version. 00008 * 00009 * This program is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU General Public License 00015 * along with this program; if not, write to the Free Software Foundation, 00016 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00017 * 00018 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. 00019 * All rights reserved. 00020 * 00021 * The Original Code is: all of this file. 00022 * 00023 * Contributor(s): none yet. 00024 * 00025 * ***** END GPL LICENSE BLOCK ***** 00026 */ 00027 00033 // implementation of LOD_FaceNormalEditor.h 00034 00036 #include "LOD_FaceNormalEditor.h" 00037 00038 using namespace std; 00039 00040 LOD_FaceNormalEditor:: 00041 LOD_FaceNormalEditor( 00042 LOD_ManMesh2 & mesh 00043 ) : m_mesh(mesh) { 00044 }; 00045 00046 LOD_FaceNormalEditor * 00047 LOD_FaceNormalEditor:: 00048 New( 00049 LOD_ManMesh2 &mesh 00050 ){ 00051 // build a set of normals of the same size 00052 // as the number of polys in the mesh 00053 00054 MEM_SmartPtr<LOD_FaceNormalEditor> output(new LOD_FaceNormalEditor(mesh)); 00055 00056 int face_num = mesh.FaceSet().size(); 00057 00058 MEM_SmartPtr<vector<MT_Vector3> > normals(new vector<MT_Vector3>); 00059 MEM_SmartPtr<vector<MT_Vector3> > vertex_normals(new vector<MT_Vector3>); 00060 00061 if (output == NULL || 00062 normals == NULL 00063 ) { 00064 return NULL; 00065 } 00066 00067 normals->reserve(face_num); 00068 vertex_normals->reserve(mesh.VertexSet().size()); 00069 output->m_normals = normals.Release(); 00070 output->m_vertex_normals = vertex_normals.Release(); 00071 00072 return output.Release(); 00073 }; 00074 00075 00076 // Property editor interface 00078 00079 void 00080 LOD_FaceNormalEditor:: 00081 Remove( 00082 std::vector<LOD_FaceInd> &sorted_faces 00083 ){ 00084 00085 // assumes a collection of faces sorted in descending order . 00086 00087 vector<MT_Vector3> & normals = m_normals.Ref(); 00088 00089 vector<LOD_FaceInd>::const_iterator it_start = sorted_faces.begin(); 00090 vector<LOD_FaceInd>::const_iterator it_end = sorted_faces.end(); 00091 00092 for (; it_start != it_end; ++it_start) { 00093 00094 if (normals.size() > 0) { 00095 MT_Vector3 temp = normals[*it_start]; 00096 00097 normals[*it_start] = normals.back(); 00098 normals.back() = temp; 00099 00100 normals.pop_back(); 00101 } 00102 00103 // FIXME - through exception 00104 } 00105 } 00106 00107 00108 void 00109 LOD_FaceNormalEditor:: 00110 Add( 00111 ){ 00112 MT_Vector3 zero(0.0f,0.0f,0.0f); 00113 m_normals->push_back(zero); 00114 } 00115 00116 void 00117 LOD_FaceNormalEditor:: 00118 Update( 00119 std::vector<LOD_FaceInd> &sorted_faces 00120 ){ 00121 00122 vector<MT_Vector3> & normals = m_normals.Ref(); 00123 00124 vector<LOD_FaceInd>::const_iterator it_start = sorted_faces.begin(); 00125 vector<LOD_FaceInd>::const_iterator it_end = sorted_faces.end(); 00126 00127 const vector<LOD_TriFace> &faces = m_mesh.FaceSet(); 00128 00129 for (; it_start != it_end; ++it_start) { 00130 normals[*it_start] = ComputeNormal(faces[*it_start]); 00131 } 00132 }; 00133 00134 // vertex normals 00136 00137 00138 void 00139 LOD_FaceNormalEditor:: 00140 RemoveVertexNormals( 00141 vector<LOD_VertexInd> &sorted_verts 00142 ){ 00143 vector<MT_Vector3> & vertex_normals = m_vertex_normals.Ref(); 00144 00145 vector<LOD_VertexInd>::const_iterator it_start = sorted_verts.begin(); 00146 vector<LOD_VertexInd>::const_iterator it_end = sorted_verts.end(); 00147 00148 for (; it_start != it_end; ++it_start) { 00149 00150 if (vertex_normals.size() > 0) { 00151 MT_Vector3 temp = vertex_normals[*it_start]; 00152 00153 vertex_normals[*it_start] = vertex_normals.back(); 00154 vertex_normals.back() = temp; 00155 00156 vertex_normals.pop_back(); 00157 } 00158 00159 // FIXME - through exception 00160 } 00161 }; 00162 00163 void 00164 LOD_FaceNormalEditor:: 00165 UpdateVertexNormals( 00166 vector<LOD_VertexInd> &sorted_verts 00167 ){ 00168 vector<MT_Vector3> & vertex_normals = m_vertex_normals.Ref(); 00169 00170 vector<LOD_VertexInd>::const_iterator it_start = sorted_verts.begin(); 00171 vector<LOD_VertexInd>::const_iterator it_end = sorted_verts.end(); 00172 00173 for (; it_start != it_end; ++it_start) { 00174 vertex_normals[*it_start] = ComputeVertexNormal(*it_start); 00175 } 00176 } 00177 00178 00179 00180 // Editor specific methods 00182 00183 void 00184 LOD_FaceNormalEditor:: 00185 BuildNormals( 00186 ){ 00187 00188 const vector<LOD_TriFace> &faces = m_mesh.FaceSet(); 00189 vector<MT_Vector3> & normals = m_normals.Ref(); 00190 00191 int face_num = faces.size(); 00192 int cur_face = 0; 00193 00194 for (; cur_face < face_num; ++cur_face) { 00195 00196 MT_Vector3 new_normal = ComputeNormal(faces[cur_face]); 00197 normals.push_back(new_normal); 00198 } 00199 // now build the vertex normals 00200 00201 vector<MT_Vector3> & vertex_normals = m_vertex_normals.Ref(); 00202 const vector<LOD_Vertex> &verts = m_mesh.VertexSet(); 00203 00204 int vertex_num = verts.size(); 00205 int cur_vertex = 0; 00206 00207 for (; cur_vertex < vertex_num; ++cur_vertex) { 00208 MT_Vector3 new_normal = ComputeVertexNormal(cur_vertex); 00209 vertex_normals.push_back(new_normal); 00210 } 00211 } 00212 00213 const 00214 MT_Vector3 00215 LOD_FaceNormalEditor:: 00216 ComputeNormal( 00217 const LOD_TriFace &face 00218 ) const { 00219 00220 const vector<LOD_Vertex> &verts = m_mesh.VertexSet(); 00221 00222 MT_Vector3 vec1 = 00223 verts[face.m_verts[1]].pos - 00224 verts[face.m_verts[0]].pos; 00225 00226 MT_Vector3 vec2 = 00227 verts[face.m_verts[2]].pos - 00228 verts[face.m_verts[1]].pos; 00229 00230 vec1 = vec1.cross(vec2); 00231 00232 if (!vec1.fuzzyZero()) { 00233 vec1.normalize(); 00234 return (vec1); 00235 } else { 00236 return (MT_Vector3(1.0,0,0)); 00237 } 00238 } 00239 00240 const 00241 MT_Vector3 00242 LOD_FaceNormalEditor:: 00243 ComputeVertexNormal( 00244 const LOD_VertexInd v 00245 ) const { 00246 00247 // average the face normals surrounding this 00248 // vertex and normalize 00249 const vector<MT_Vector3> & face_normals = m_normals.Ref(); 00250 00251 vector<LOD_FaceInd> vertex_faces; 00252 vertex_faces.reserve(32); 00253 00254 m_mesh.VertexFaces(v,vertex_faces); 00255 00256 MT_Vector3 normal(0,0,0); 00257 00258 vector<LOD_FaceInd>::const_iterator face_it = vertex_faces.begin(); 00259 vector<LOD_FaceInd>::const_iterator face_end = vertex_faces.end(); 00260 00261 for (; face_it != face_end; ++face_it) { 00262 normal += face_normals[*face_it]; 00263 } 00264 00265 if (!normal.fuzzyZero()) { 00266 normal.normalize(); 00267 return (normal); 00268 } else { 00269 return (MT_Vector3(1.0,0,0)); 00270 } 00271 } 00272 00273 00274 00275 00276 00277 00278 00279 00280 00281 00282 00283 00284 00285 00286 00287 00288 00289 00290 00291