Blender  V2.59
BSP_MeshPrimitives.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: BSP_MeshPrimitives.cpp 35145 2011-02-25 10:44:20Z 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 "BSP_MeshPrimitives.h"
00035 
00036 #include "MT_assert.h"
00037 #include "BSP_CSGException.h"
00038 #include <algorithm>
00039 
00040 using namespace std;
00041 
00042 BSP_MVertex::
00043 BSP_MVertex(
00044 ) :
00045         m_pos (MT_Point3()),
00046         m_select_tag (false),
00047         m_open_tag (0)
00048 {
00049 };
00050 
00051 BSP_MVertex::
00052 BSP_MVertex(
00053         const MT_Point3 & pos
00054 ) :
00055         m_pos(pos),
00056         m_select_tag (false),
00057         m_open_tag (0)
00058 {
00059 };
00060 
00061 
00062         bool
00063 BSP_MVertex::
00064 RemoveEdge(
00065         BSP_EdgeInd e
00066 ){
00067         vector<BSP_EdgeInd>::iterator result = find(m_edges.begin(),m_edges.end(),e);
00068         if (result == m_edges.end()) {
00069                 return false;
00070         }
00071         BSP_EdgeInd last = m_edges.back();
00072         m_edges.pop_back();
00073         if (m_edges.empty()) return true;
00074 
00075         *result = last;
00076         return true;    
00077 }       
00078 
00079         void
00080 BSP_MVertex::
00081 AddEdge(
00082         BSP_EdgeInd e
00083 ){
00084         m_edges.push_back(e);
00085 }
00086 
00087         void
00088 BSP_MVertex::
00089 SwapEdge(
00090         BSP_EdgeInd e_old,
00091         BSP_EdgeInd e_new
00092 ){
00093         vector<BSP_EdgeInd>::iterator result = 
00094                 find(m_edges.begin(),m_edges.end(),e_old);
00095         if (result == m_edges.end()) {
00096                 BSP_CSGException e(e_mesh_error);
00097                 throw(e);
00098                 MT_assert(false);
00099         }
00100         
00101         *result = e_new;
00102 }
00103 
00104         bool
00105 BSP_MVertex::
00106 SelectTag(
00107 ) const{
00108         return m_select_tag;
00109 }
00110 
00111         void
00112 BSP_MVertex::
00113 SetSelectTag(
00114         bool tag        
00115 ){
00116         m_select_tag = tag;
00117 }
00118 
00119         int
00120 BSP_MVertex::
00121 OpenTag(
00122 ) const {
00123         return m_open_tag;
00124 }
00125 
00126         void
00127 BSP_MVertex::
00128 SetOpenTag(
00129         int tag
00130 ){
00131         m_open_tag = tag;
00132 }
00133 
00134 
00139 BSP_MEdge::
00140 BSP_MEdge(
00141 ){
00142         m_verts[0] = m_verts[1] = BSP_VertexInd::Empty();
00143 }
00144         
00145         bool 
00146 BSP_MEdge::
00147 operator == (
00148         BSP_MEdge & rhs
00149 ){
00150         // edges are the same if their vertex indices are the 
00151         // same!!! Other properties are not checked 
00152 
00153         int matches = 0;
00154 
00155         if (this->m_verts[0] == rhs.m_verts[0]) {
00156                 ++matches;
00157         }
00158         if (this->m_verts[1] == rhs.m_verts[0]) {
00159                 ++matches;
00160         }
00161         if (this->m_verts[0] == rhs.m_verts[1]) {
00162                 ++matches;
00163         }
00164         if (this->m_verts[1] == rhs.m_verts[1]) {
00165                 ++matches;
00166         }
00167         
00168         if (matches >= 2) {
00169                 return true;
00170         }
00171         return false;
00172 }
00173 
00174         void
00175 BSP_MEdge::
00176 SwapFace(
00177         BSP_FaceInd old_f,
00178         BSP_FaceInd new_f
00179 ){
00180         vector<BSP_FaceInd>::iterator result = 
00181                 find(m_faces.begin(),m_faces.end(),old_f);
00182         if (result == m_faces.end()) {
00183                 BSP_CSGException e(e_mesh_error);
00184                 throw(e);
00185                 MT_assert(false);
00186         }
00187         
00188         *result = new_f;
00189 }
00190 
00191     BSP_VertexInd
00192 BSP_MEdge::
00193 OpVertex(
00194         BSP_VertexInd vi
00195 ) const {
00196         if (vi == m_verts[0]) return m_verts[1];
00197         if (vi == m_verts[1]) return m_verts[0];
00198         MT_assert(false);
00199         BSP_CSGException e(e_mesh_error);
00200         throw(e);
00201 
00202         return BSP_VertexInd::Empty();
00203 }
00204 
00205         bool
00206 BSP_MEdge::
00207 SelectTag(
00208 ) const {
00209         return bool(m_verts[1].Tag() & 0x1);
00210 }
00211         void
00212 BSP_MEdge::
00213 SetSelectTag(
00214         bool tag        
00215 ){
00216         m_verts[1].SetTag(int(tag));
00217 }
00218 
00219         int
00220 BSP_MEdge::
00221 OpenTag(
00222 ) const {
00223         return m_verts[0].Tag();
00224 }
00225 
00226         void
00227 BSP_MEdge::
00228 SetOpenTag(
00229         int tag
00230 ) {
00231         // Note conversion from int to unsigned int!!!!!
00232         m_verts[0].SetTag(tag);
00233 }
00234         
00235 
00241 BSP_MFace::
00242 BSP_MFace(
00243 ):
00244         m_open_tag(-1),
00245         m_orig_face(0)
00246 {
00247         // nothing to do
00248 }
00249 
00250         void
00251 BSP_MFace::
00252 Invert(
00253 ){
00254 
00255         // TODO replace reverse as I think some compilers
00256         // do not support the STL routines employed.
00257 
00258         reverse(
00259                 m_verts.begin(),
00260                 m_verts.end()
00261         );
00262 
00263         // invert the normal
00264         m_plane.Invert();
00265 }
00266 
00267         bool
00268 BSP_MFace::
00269 SelectTag(
00270 ) const {
00271         return bool(m_verts[1].Tag() & 0x1);
00272 }       
00273 
00274         void
00275 BSP_MFace::
00276 SetSelectTag(
00277         bool tag        
00278 ){
00279         m_verts[1].SetTag(int(tag));
00280 };      
00281 
00282         int
00283 BSP_MFace::
00284 OpenTag(
00285 ) const {
00286         return m_open_tag;
00287 }
00288 
00289         void
00290 BSP_MFace::
00291 SetOpenTag(
00292         int tag
00293 ){
00294         // Note conversion from int to unsigned int!!!!!
00295         m_open_tag = tag;
00296 }
00297 
00298 
00299