|
Blender
V2.59
|
00001 /* 00002 * $Id: LOD_MeshPrimitives.cpp 35147 2011-02-25 10:47:28Z jesterking $ 00003 * ***** BEGIN GPL LICENSE BLOCK ***** 00004 * 00005 * This program is free software; you can redistribute it and/or 00006 * modify it under the terms of the GNU General Public License 00007 * as published by the Free Software Foundation; either version 2 00008 * of the License, or (at your option) any later version. 00009 * 00010 * This program 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 00013 * GNU General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU General Public License 00016 * along with this program; if not, write to the Free Software Foundation, 00017 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00018 * 00019 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. 00020 * All rights reserved. 00021 * 00022 * The Original Code is: all of this file. 00023 * 00024 * Contributor(s): none yet. 00025 * 00026 * ***** END GPL LICENSE BLOCK ***** 00027 */ 00028 00034 #include "LOD_MeshPrimitives.h" 00035 00036 #include "MT_assert.h" 00037 #include "LOD_MeshException.h" 00038 #include <algorithm> 00039 00040 using namespace std; 00041 00042 // Vertex Methods 00044 00045 LOD_Vertex:: 00046 LOD_Vertex( 00047 ) : 00048 pos (MT_Vector3()), 00049 m_select_tag(false) 00050 { 00051 }; 00052 00053 bool 00054 LOD_Vertex:: 00055 RemoveEdge( 00056 LOD_EdgeInd e 00057 ){ 00058 00059 vector<LOD_EdgeInd>::iterator result = find(m_edges.begin(),m_edges.end(),e); 00060 if (result == m_edges.end()) { 00061 return false; 00062 } 00063 00064 std::swap(*result, m_edges.back()); 00065 m_edges.pop_back(); 00066 return true; 00067 }; 00068 00069 void 00070 LOD_Vertex:: 00071 AddEdge( 00072 LOD_EdgeInd e 00073 ){ 00074 m_edges.push_back(e); 00075 }; 00076 00077 void 00078 LOD_Vertex:: 00079 SwapEdge( 00080 LOD_EdgeInd e_old, 00081 LOD_EdgeInd e_new 00082 ){ 00083 00084 vector<LOD_EdgeInd>::iterator result = 00085 find(m_edges.begin(),m_edges.end(),e_old); 00086 if (result == m_edges.end()) { 00087 MT_assert(false); 00088 LOD_MeshException e(LOD_MeshException::e_search_error); 00089 throw(e); 00090 } 00091 00092 *result = e_new; 00093 }; 00094 00095 bool 00096 LOD_Vertex:: 00097 SelectTag( 00098 ) const { 00099 return m_select_tag; 00100 }; 00101 00102 void 00103 LOD_Vertex:: 00104 SetSelectTag( 00105 bool tag 00106 ){ 00107 m_select_tag = tag; 00108 }; 00109 00110 bool 00111 LOD_Vertex:: 00112 Degenerate( 00113 ){ 00114 return m_edges.empty(); 00115 } 00116 00117 void 00118 LOD_Vertex:: 00119 CopyPosition( 00120 float *float_ptr 00121 ){ 00122 pos.getValue(float_ptr); 00123 } 00124 00125 00126 00127 // Edge Methods 00129 00130 LOD_Edge:: 00131 LOD_Edge ( 00132 ) { 00133 m_verts[0] = m_verts[1] = LOD_VertexInd::Empty(); 00134 m_faces[0] = m_faces[1] = LOD_FaceInd::Empty(); 00135 } 00136 00137 bool 00138 LOD_Edge:: 00139 operator == ( 00140 LOD_Edge & rhs 00141 ) { 00142 // edges are the same if their vertex indices are the 00143 // same!!! Other properties are not checked 00144 00145 int matches = 0; 00146 00147 if (this->m_verts[0] == rhs.m_verts[0]) { 00148 ++matches; 00149 } 00150 if (this->m_verts[1] == rhs.m_verts[0]) { 00151 ++matches; 00152 } 00153 if (this->m_verts[0] == rhs.m_verts[1]) { 00154 ++matches; 00155 } 00156 if (this->m_verts[1] == rhs.m_verts[1]) { 00157 ++matches; 00158 } 00159 00160 if (matches >= 2) { 00161 return true; 00162 } 00163 return false; 00164 } 00165 00166 // Elementary helper methods 00168 00169 LOD_FaceInd 00170 LOD_Edge:: 00171 OpFace( 00172 LOD_FaceInd f 00173 ) const { 00174 if (f == m_faces[0]) { 00175 return m_faces[1]; 00176 } else 00177 if (f == m_faces[1]) { 00178 return m_faces[0]; 00179 } else { 00180 MT_assert(false); 00181 LOD_MeshException e(LOD_MeshException::e_search_error); 00182 throw(e); 00183 00184 return LOD_FaceInd::Empty(); 00185 } 00186 } 00187 00188 void 00189 LOD_Edge:: 00190 SwapFace( 00191 LOD_FaceInd old_f, 00192 LOD_FaceInd new_f 00193 ) { 00194 if (old_f == m_faces[0]) { 00195 m_faces[0] = new_f; 00196 } else 00197 if (old_f == m_faces[1]) { 00198 m_faces[1] = new_f; 00199 } else { 00200 LOD_MeshException e(LOD_MeshException::e_search_error); 00201 throw(e); 00202 } 00203 } 00204 00205 00206 // return the half edge face - the half edge is defined 00207 // by the {vertex,edge} tuple. 00208 00209 LOD_FaceInd 00210 LOD_Edge:: 00211 HalfEdgeFace( 00212 LOD_VertexInd vi 00213 ){ 00214 if (vi == m_verts[0]) return m_faces[0]; 00215 if (vi == m_verts[1]) return m_faces[1]; 00216 MT_assert(false); 00217 00218 LOD_MeshException e(LOD_MeshException::e_search_error); 00219 throw(e); 00220 00221 return LOD_FaceInd::Empty(); 00222 } 00223 00224 00225 LOD_VertexInd 00226 LOD_Edge:: 00227 OpVertex( 00228 LOD_VertexInd vi 00229 ) { 00230 if (vi == m_verts[0]) return m_verts[1]; 00231 if (vi == m_verts[1]) return m_verts[0]; 00232 MT_assert(false); 00233 00234 LOD_MeshException e(LOD_MeshException::e_search_error); 00235 throw(e); 00236 00237 return LOD_VertexInd::Empty(); 00238 } 00239 00240 // replace the vertex v_old with vertex v_new 00241 // error if v_old is not one of the original vertices 00242 00243 void 00244 LOD_Edge:: 00245 SwapVertex( 00246 LOD_VertexInd v_old, 00247 LOD_VertexInd v_new 00248 ) { 00249 if (v_old == m_verts[0]) { 00250 m_verts[0] = v_new; 00251 } else 00252 if (v_old == m_verts[1]) { 00253 m_verts[1] = v_new; 00254 } else { 00255 00256 MT_assert(false); 00257 00258 LOD_MeshException e(LOD_MeshException::e_search_error); 00259 throw(e); 00260 } 00261 if(m_verts[0] == m_verts[1]) { 00262 MT_assert(false); 00263 00264 LOD_MeshException e(LOD_MeshException::e_non_manifold); 00265 throw(e); 00266 } 00267 00268 } 00269 00270 bool 00271 LOD_Edge:: 00272 SelectTag( 00273 ) const { 00274 return bool(m_verts[1].Tag() & 0x1); 00275 }; 00276 00277 void 00278 LOD_Edge:: 00279 SetSelectTag( 00280 bool tag 00281 ) { 00282 m_verts[1].SetTag(int(tag)); 00283 }; 00284 00285 int 00286 LOD_Edge:: 00287 OpenTag( 00288 ) const { 00289 return m_faces[0].Tag(); 00290 } 00291 00292 void 00293 LOD_Edge:: 00294 SetOpenTag( 00295 int tag 00296 ) { 00297 m_faces[0].SetTag(tag); 00298 } 00299 00300 bool 00301 LOD_Edge:: 00302 Degenerate( 00303 ) const { 00304 return ( 00305 (m_faces[0].IsEmpty() && m_faces[1].IsEmpty()) || 00306 (m_verts[0] == m_verts[1]) 00307 ); 00308 }; 00309 00310 // TriFace Methods 00312 00313 LOD_TriFace:: 00314 LOD_TriFace( 00315 ) { 00316 m_verts[0] = m_verts[1] = m_verts[2] = LOD_VertexInd::Empty(); 00317 } 00318 00319 // Elementary helper methods 00321 00322 void 00323 LOD_TriFace:: 00324 SwapVertex( 00325 LOD_VertexInd old_v, 00326 LOD_VertexInd new_v 00327 ) { 00328 // could save branching here... 00329 00330 if (m_verts[0] == old_v) { 00331 m_verts[0] = new_v; 00332 } else 00333 if (m_verts[1] == old_v) { 00334 m_verts[1] = new_v; 00335 } else 00336 if (m_verts[2] == old_v) { 00337 m_verts[2] = new_v; 00338 } else { 00339 MT_assert(false); 00340 00341 LOD_MeshException excep(LOD_MeshException::e_search_error); 00342 throw(excep); 00343 } 00344 } 00345 00346 bool 00347 LOD_TriFace:: 00348 SelectTag( 00349 ) const { 00350 return bool(m_verts[1].Tag() & 0x1); 00351 }; 00352 00353 void 00354 LOD_TriFace:: 00355 SetSelectTag( 00356 bool tag 00357 ) { 00358 m_verts[1].SetTag(int(tag)); 00359 }; 00360 00361 int 00362 LOD_TriFace:: 00363 OpenTag( 00364 ) { 00365 return m_verts[2].Tag(); 00366 } 00367 00368 void 00369 LOD_TriFace:: 00370 SetOpenTag( 00371 int tag 00372 ) { 00373 m_verts[2].SetTag(tag); 00374 } 00375 00376 bool 00377 LOD_TriFace:: 00378 Degenerate( 00379 ) { 00380 00381 return ( 00382 (m_verts[0] == m_verts[1]) || 00383 (m_verts[1] == m_verts[2]) || 00384 (m_verts[2] == m_verts[0]) 00385 ); 00386 } 00387 00388 void 00389 LOD_TriFace:: 00390 CopyVerts( 00391 int * index_ptr 00392 ){ 00393 index_ptr[0] = m_verts[0]; 00394 index_ptr[1] = m_verts[1]; 00395 index_ptr[2] = m_verts[2]; 00396 }; 00397 00398 00399 00400 00401 00402 00403 00404 00405