Blender V2.61 - r43446

LOD_decimation.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 // implementation of external c api
00034 #include "../extern/LOD_decimation.h"
00035 #include "LOD_DecimationClass.h"
00036 
00037 using namespace std;
00038 
00039     int 
00040 LOD_LoadMesh(
00041     LOD_Decimation_InfoPtr info
00042 ) {
00043     if (info == NULL) return 0;
00044     if (
00045         info->vertex_buffer == NULL ||
00046         info->vertex_normal_buffer == NULL ||
00047         info->triangle_index_buffer == NULL
00048     ) {
00049         return 0;
00050     }
00051 
00052 
00053     // create the intern object to hold all 
00054     // the decimation classes
00055 
00056     MEM_SmartPtr<LOD_DecimationClass> intern(LOD_DecimationClass::New(info));
00057 
00058     if (intern == NULL) return 0;
00059 
00060     MEM_SmartPtr<vector<LOD_Vertex> > intern_vertex_buffer(new vector<LOD_Vertex>(info->vertex_num));
00061     if (intern_vertex_buffer == NULL) return 0;
00062 
00063     vector<LOD_Vertex>::iterator intern_vertex_it(intern_vertex_buffer->begin());
00064 
00065     // now load in the vertices to the mesh
00066 
00067     const int vertex_stride = 3;
00068 
00069     float * vertex_ptr = info->vertex_buffer;
00070     const float * vertex_end = vertex_ptr + info->vertex_num*vertex_stride;
00071     
00072     LOD_ManMesh2 &mesh = intern->Mesh();
00073 
00074     for (;vertex_ptr < vertex_end; vertex_ptr += vertex_stride,++intern_vertex_it) {
00075         intern_vertex_it->pos = MT_Vector3(vertex_ptr);
00076     }
00077     
00078     mesh.SetVertices(intern_vertex_buffer);
00079 
00080     // load in the triangles
00081     
00082     const int triangle_stride = 3;
00083 
00084     int * triangle_ptr = info->triangle_index_buffer;
00085     const int * triangle_end = triangle_ptr + info->face_num*triangle_stride;
00086 
00087     try {
00088 
00089         for (;triangle_ptr < triangle_end; triangle_ptr += triangle_stride) {
00090             mesh.AddTriangle(triangle_ptr);
00091         }
00092     }
00093 
00094     catch (...) {
00095         return 0;
00096     }
00097 
00098     // ok we have built the mesh 
00099 
00100     intern->m_e_decimation_state = LOD_DecimationClass::e_loaded;
00101 
00102     info->intern = (void *) (intern.Release()); 
00103 
00104     return 1;
00105 }
00106 
00107     int 
00108 LOD_PreprocessMesh(
00109     LOD_Decimation_InfoPtr info
00110 ) {
00111     if (info == NULL) return 0;
00112     if (info->intern == NULL) return 0;
00113 
00114     LOD_DecimationClass *intern = (LOD_DecimationClass *) info->intern;
00115     if (intern->m_e_decimation_state != LOD_DecimationClass::e_loaded) return 0;    
00116 
00117     // arm the various internal classes so that we are ready to step
00118     // through decimation
00119 
00120     intern->FaceEditor().BuildNormals();
00121     if (intern->Decimator().Arm() == false) return 0;
00122 
00123     // ok preprocessing done 
00124     intern->m_e_decimation_state = LOD_DecimationClass::e_preprocessed;
00125 
00126     return 1;
00127 }
00128 
00129     int 
00130 LOD_CollapseEdge(
00131     LOD_Decimation_InfoPtr info
00132 ){
00133     if (info == NULL) return 0;
00134     if (info->intern == NULL) return 0;
00135     LOD_DecimationClass *intern = (LOD_DecimationClass *) info->intern;
00136     if (intern->m_e_decimation_state != LOD_DecimationClass::e_preprocessed) return 0;  
00137 
00138     bool step_result = intern->Decimator().Step();
00139 
00140     return step_result == true ? 1 : 0;
00141 }   
00142 
00143     
00144     int
00145 LOD_FreeDecimationData(
00146     LOD_Decimation_InfoPtr info
00147 ){
00148     if (info == NULL) return 0;
00149     if (info->intern == NULL) return 0;
00150     LOD_DecimationClass *intern = (LOD_DecimationClass *) info->intern;
00151     delete(intern);
00152     info->intern = NULL;
00153     return 1;
00154 }
00155