Blender V2.61 - r43446

BSP_CSGMesh_CFIterator.h

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 #ifndef BSP_CSGMesh_CFIterator_h
00034 
00035 #define BSP_CSGMesh_CFIterator_h
00036 
00037 #include "BSP_CSGMesh.h"
00038 #include "../extern/CSG_BooleanOps.h"
00045 struct BSP_CSGMesh_VertexIt {
00046     BSP_CSGMesh *mesh;
00047     BSP_MVertex * pos;
00048 };
00049 
00050 
00051 inline
00052     void
00053 BSP_CSGMesh_VertexIt_Destruct(
00054     CSG_VertexIteratorDescriptor * iterator
00055 ) {
00056     delete ((BSP_CSGMesh_VertexIt *)(iterator->it));
00057     iterator->it = NULL;
00058     iterator->Done = NULL;
00059     iterator->Fill = NULL;
00060     iterator->Reset = NULL;
00061     iterator->Step = NULL;
00062     iterator->num_elements = 0;
00063 };
00064 
00065 
00066 inline
00067     int
00068 BSP_CSGMesh_VertexIt_Done(
00069     CSG_IteratorPtr it
00070 ) {
00071     // assume CSG_IteratorPtr is of the correct type.
00072     BSP_CSGMesh_VertexIt * vertex_it = (BSP_CSGMesh_VertexIt *)it;
00073 
00074     /* dereferencing iterator::end() is illegal, so we dereference 1 before it */
00075     /* also check that vector is not empty */
00076     if (vertex_it->mesh->VertexSet().size() && 
00077             vertex_it->pos <= &(*(vertex_it->mesh->VertexSet().end() -1) )) return 0;
00078     return 1;
00079 };
00080 
00081 inline
00082     void
00083 BSP_CSGMesh_VertexIt_Fill(
00084     CSG_IteratorPtr it,
00085     CSG_IVertex *vert
00086 ) {
00087     // assume CSG_IteratorPtr is of the correct type.
00088     BSP_CSGMesh_VertexIt * vertex_it = (BSP_CSGMesh_VertexIt *)it;
00089             
00090     vertex_it->pos->m_pos.getValue(vert->position);
00091 };
00092 
00093 inline
00094     void
00095 BSP_CSGMesh_VertexIt_Step(
00096     CSG_IteratorPtr it
00097 ) {
00098     // assume CSG_IteratorPtr is of the correct type.
00099     BSP_CSGMesh_VertexIt * vertex_it = (BSP_CSGMesh_VertexIt *)it;
00100 
00101     ++(vertex_it->pos);
00102 };
00103 
00104 inline
00105     void
00106 BSP_CSGMesh_VertexIt_Reset(
00107     CSG_IteratorPtr it
00108 ) {
00109     // assume CSG_IteratorPtr is of the correct type.
00110     BSP_CSGMesh_VertexIt * vertex_it = (BSP_CSGMesh_VertexIt *)it;
00111     vertex_it->pos = &vertex_it->mesh->VertexSet()[0];
00112 };  
00113 
00114 inline
00115     void
00116 BSP_CSGMeshVertexIt_Construct(
00117     BSP_CSGMesh *mesh,
00118     CSG_VertexIteratorDescriptor *output
00119 ){
00120     // user should have insured mesh is not equal to NULL.
00121     
00122     output->Done = BSP_CSGMesh_VertexIt_Done;
00123     output->Fill = BSP_CSGMesh_VertexIt_Fill;
00124     output->Step = BSP_CSGMesh_VertexIt_Step;
00125     output->Reset = BSP_CSGMesh_VertexIt_Reset;
00126     output->num_elements = mesh->VertexSet().size();
00127     
00128     BSP_CSGMesh_VertexIt * v_it = new BSP_CSGMesh_VertexIt;
00129     v_it->mesh = mesh;
00130     if( output->num_elements > 0 )
00131         v_it->pos = &mesh->VertexSet()[0];
00132     output->it = v_it;
00133 };          
00134 
00135 
00140 struct BSP_CSGMesh_FaceIt {
00141     BSP_CSGMesh *mesh;
00142     BSP_MFace *pos;
00143     int face_triangle;
00144 };
00145 
00146 
00147 inline
00148     void
00149 BSP_CSGMesh_FaceIt_Destruct(
00150     CSG_FaceIteratorDescriptor *iterator
00151 ) {
00152     delete ((BSP_CSGMesh_FaceIt *)(iterator->it));
00153     iterator->it = NULL;
00154     iterator->Done = NULL;
00155     iterator->Fill = NULL;
00156     iterator->Reset = NULL;
00157     iterator->Step = NULL;
00158     iterator->num_elements = 0;
00159 };
00160 
00161 
00162 inline
00163     int
00164 BSP_CSGMesh_FaceIt_Done(
00165     CSG_IteratorPtr it
00166 ) {
00167     // assume CSG_IteratorPtr is of the correct type.
00168     BSP_CSGMesh_FaceIt * face_it = (BSP_CSGMesh_FaceIt *)it;
00169 
00170     /* dereferencing iterator::end() is illegal, so we dereference 1 before it */
00171     /* also check that vector is not empty */
00172     if (face_it->mesh->FaceSet().size() && 
00173             face_it->pos <= &(*(face_it->mesh->FaceSet().end() -1))) {
00174         if (face_it->face_triangle + 3 <= (int)face_it->pos->m_verts.size()) {
00175             return 0;
00176         }
00177     }
00178     return 1;
00179 };
00180 
00181 inline
00182     void
00183 BSP_CSGMesh_FaceIt_Fill(
00184     CSG_IteratorPtr it,
00185     CSG_IFace *face
00186 ){
00187     // assume CSG_IteratorPtr is of the correct type.
00188     BSP_CSGMesh_FaceIt * face_it = (BSP_CSGMesh_FaceIt *)it;        
00189     // essentially iterating through a triangle fan here.
00190 
00191     if (face_it->pos->m_verts.size()>3) {
00192         // QUAD
00193         face->vertex_index[0] = int(face_it->pos->m_verts[0]);
00194         face->vertex_index[1] = int(face_it->pos->m_verts[1]);
00195         face->vertex_index[2] = int(face_it->pos->m_verts[2]);
00196         face->vertex_index[3] = int(face_it->pos->m_verts[3]);
00197 
00198         face->orig_face = face_it->pos->m_orig_face;
00199 
00200         face->vertex_number = 4;
00201     }
00202     else {
00203         // TRIANGLE
00204         face->vertex_index[0] = int(face_it->pos->m_verts[0]);
00205         face->vertex_index[1] = int(face_it->pos->m_verts[1]);
00206         face->vertex_index[2] = int(face_it->pos->m_verts[2]);
00207 
00208         face->orig_face = face_it->pos->m_orig_face;
00209 
00210         face->vertex_number = 3;
00211     }
00212 };
00213 
00214 inline
00215     void
00216 BSP_CSGMesh_FaceIt_Step(
00217     CSG_IteratorPtr it
00218 ) {
00219     // assume CSG_IteratorPtr is of the correct type.
00220     BSP_CSGMesh_FaceIt * face_it = (BSP_CSGMesh_FaceIt *)it;        
00221 
00222     /* dereferencing iterator::end() is illegal, so we dereference 1 before it */
00223     /* also check that vector is not empty */
00224     if (face_it->mesh->FaceSet().size() && 
00225             face_it->pos <= &(*(face_it->mesh->FaceSet().end() -1))) {
00226 
00227         //if (face_it->face_triangle + 3 < face_it->pos->m_verts.size()) {
00228         //  (face_it->face_triangle)++;
00229         //} else {
00230             face_it->face_triangle = 0;
00231             (face_it->pos) ++;
00232         //}
00233     }
00234 };
00235 
00236 inline
00237     void
00238 BSP_CSGMesh_FaceIt_Reset(
00239     CSG_IteratorPtr it
00240 ) {
00241     // assume CSG_IteratorPtr is of the correct type.
00242     BSP_CSGMesh_FaceIt * f_it = (BSP_CSGMesh_FaceIt *)it;       
00243     f_it->pos = &f_it->mesh->FaceSet()[0];
00244     f_it->face_triangle = 0;
00245 };
00246 
00247 inline
00248     void
00249 BSP_CSGMesh_FaceIt_Construct(
00250     BSP_CSGMesh * mesh,
00251     CSG_FaceIteratorDescriptor *output
00252 ) {
00253 
00254     output->Done = BSP_CSGMesh_FaceIt_Done;
00255     output->Fill = BSP_CSGMesh_FaceIt_Fill;
00256     output->Step = BSP_CSGMesh_FaceIt_Step;
00257     output->Reset = BSP_CSGMesh_FaceIt_Reset;
00258 
00259     output->num_elements = mesh->FaceSet().size();
00260     
00261     BSP_CSGMesh_FaceIt * f_it = new BSP_CSGMesh_FaceIt;
00262     f_it->mesh = mesh;
00263     if( output->num_elements > 0 )
00264         f_it->pos = &mesh->FaceSet()[0];
00265     f_it->face_triangle = 0;
00266 
00267     output->it = f_it;
00268 };
00269 
00270 
00271 #endif
00272