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