Blender V2.61 - r43446

LOD_MeshPrimitives.cpp

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