Blender V2.61 - r43446
|
00001 #if 0 00002 00003 /* 00004 * 00005 * ***** BEGIN GPL LICENSE BLOCK ***** 00006 * 00007 * This program is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU General Public License 00009 * as published by the Free Software Foundation; either version 2 00010 * of the License, or (at your option) any later version. 00011 * 00012 * This program is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 * GNU General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU General Public License 00018 * along with this program; if not, write to the Free Software Foundation, 00019 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00020 * 00021 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. 00022 * All rights reserved. 00023 * 00024 * The Original Code is: all of this file. 00025 * 00026 * Contributor(s): none yet. 00027 * 00028 * ***** END GPL LICENSE BLOCK ***** 00029 */ 00030 00035 #include "CSG_BooleanOps.h" 00036 00037 00038 00039 00040 00045 void 00046 CSG_DestroyMeshDescriptor( 00047 CSG_MeshDescriptor *mesh 00048 ){ 00049 // Call mesh descriptors destroy function.... 00050 mesh->m_destroy_func(mesh); 00051 } 00052 00053 // Destroy function for blender mesh internals. 00054 00055 static 00056 void 00057 CSG_DestroyBlenderMeshInternals( 00058 CSG_MeshDescriptor *mesh 00059 ) { 00060 // Free face and vertex iterators. 00061 FreeMeshDescriptors(&(mesh->m_face_iterator),&(mesh->m_vertex_iterator)); 00062 } 00063 00064 00065 static 00066 void 00067 CSG_DestroyCSGMeshInternals( 00068 CSG_MeshDescriptor *mesh 00069 ){ 00070 CSG_FreeVertexDescriptor(&(mesh->m_vertex_iterator)); 00071 CSG_FreeFaceDescriptor(&(mesh->m_face_iterator)); 00072 } 00073 00074 static 00075 int 00076 MakeCSGMeshFromBlenderBase( 00077 Base * base, 00078 CSG_MeshDescriptor * output 00079 ) { 00080 Mesh *me; 00081 if (output == NULL || base == NULL) return 0; 00082 00083 me = get_mesh(base->object); 00084 00085 output->m_descriptor.user_face_vertex_data_size = 0; 00086 output->m_descriptor.user_data_size = sizeof(FaceData); 00087 00088 output->base = base; 00089 00090 BuildMeshDescriptors( 00091 base->object, 00092 &(output->m_face_iterator), 00093 &(output->m_vertex_iterator) 00094 ); 00095 00096 output->m_destroy_func = CSG_DestroyBlenderMeshInternals; 00097 00098 return 1; 00099 } 00100 00101 int 00102 CSG_LoadBlenderMesh( 00103 Object * obj, 00104 CSG_MeshDescriptor *output 00105 ){ 00106 00107 Mesh *me; 00108 if (output == NULL || obj == NULL) return 0; 00109 00110 me = get_mesh(obj); 00111 00112 output->m_descriptor.user_face_vertex_data_size = 0; 00113 output->m_descriptor.user_data_size = sizeof(FaceData); 00114 00115 output->base = NULL; 00116 00117 BuildMeshDescriptors( 00118 obj, 00119 &(output->m_face_iterator), 00120 &(output->m_vertex_iterator) 00121 ); 00122 00123 output->m_destroy_func = CSG_DestroyBlenderMeshInternals; 00124 output->base = NULL; 00125 00126 return 1; 00127 } 00128 00129 00130 00131 00132 int 00133 CSG_AddMeshToBlender( 00134 CSG_MeshDescriptor *mesh 00135 ){ 00136 Mesh *me_new = NULL; 00137 Object *ob_new = NULL; 00138 float inv_mat[4][4]; 00139 00140 if (mesh == NULL) return 0; 00141 if (mesh->base == NULL) return 0; 00142 00143 invert_m4_m4(inv_mat,mesh->base->object->obmat); 00144 00145 // Create a new blender mesh object - using 'base' as 00146 // a template for the new object. 00147 ob_new= AddNewBlenderMesh(mesh->base); 00148 00149 me_new = ob_new->data; 00150 00151 // make sure the iterators are reset. 00152 mesh->m_face_iterator.Reset(mesh->m_face_iterator.it); 00153 mesh->m_vertex_iterator.Reset(mesh->m_vertex_iterator.it); 00154 00155 // iterate through results of operation and insert into new object 00156 // see subsurf.c 00157 00158 ConvertCSGDescriptorsToMeshObject( 00159 ob_new, 00160 &(mesh->m_descriptor), 00161 &(mesh->m_face_iterator), 00162 &(mesh->m_vertex_iterator), 00163 inv_mat 00164 ); 00165 00166 return 1; 00167 } 00168 00169 int 00170 CSG_PerformOp( 00171 CSG_MeshDescriptor *mesh1, 00172 CSG_MeshDescriptor *mesh2, 00173 int int_op_type, 00174 CSG_MeshDescriptor *output 00175 ){ 00176 00177 CSG_OperationType op_type; 00178 CSG_BooleanOperation * bool_op = CSG_NewBooleanFunction(); 00179 int success = 0; 00180 00181 if (bool_op == NULL) return 0; 00182 00183 if ((mesh1 == NULL) || (mesh2 == NULL) || (output == NULL)) { 00184 return 0; 00185 } 00186 if ((int_op_type < 1) || (int_op_type > 3)) return 0; 00187 00188 switch (int_op_type) { 00189 case 1 : op_type = e_csg_intersection; break; 00190 case 2 : op_type = e_csg_union; break; 00191 case 3 : op_type = e_csg_difference; break; 00192 case 4 : op_type = e_csg_classify; break; 00193 default : op_type = e_csg_intersection; 00194 } 00195 00196 output->m_descriptor = CSG_DescibeOperands(bool_op,mesh1->m_descriptor,mesh2->m_descriptor); 00197 output->base = mesh1->base; 00198 00199 if (output->m_descriptor.user_face_vertex_data_size) { 00200 // Then use the only interp function supported 00201 success = 00202 CSG_PerformBooleanOperation( 00203 bool_op, 00204 op_type, 00205 mesh1->m_face_iterator, 00206 mesh1->m_vertex_iterator, 00207 mesh2->m_face_iterator, 00208 mesh2->m_vertex_iterator, 00209 InterpFaceVertexData 00210 ); 00211 } else { 00212 success = 00213 CSG_PerformBooleanOperation( 00214 bool_op, 00215 op_type, 00216 mesh1->m_face_iterator, 00217 mesh1->m_vertex_iterator, 00218 mesh2->m_face_iterator, 00219 mesh2->m_vertex_iterator, 00220 InterpNoUserData 00221 ); 00222 } 00223 00224 if (!success) { 00225 CSG_FreeBooleanOperation(bool_op); 00226 bool_op = NULL; 00227 return 0; 00228 } 00229 00230 // get the ouput mesh descriptors. 00231 00232 CSG_OutputFaceDescriptor(bool_op,&(output->m_face_iterator)); 00233 CSG_OutputVertexDescriptor(bool_op,&(output->m_vertex_iterator)); 00234 output->m_destroy_func = CSG_DestroyCSGMeshInternals; 00235 00236 return 1; 00237 } 00238 00239 int 00240 NewBooleanMeshTest( 00241 struct Base * base, 00242 struct Base * base_select, 00243 int op_type 00244 ){ 00245 00246 CSG_MeshDescriptor m1,m2,output; 00247 CSG_MeshDescriptor output2,output3; 00248 00249 if (!MakeCSGMeshFromBlenderBase(base,&m1)) { 00250 return 0; 00251 } 00252 00253 if (!MakeCSGMeshFromBlenderBase(base_select,&m2)) { 00254 return 0; 00255 } 00256 00257 CSG_PerformOp(&m1,&m2,1,&output); 00258 CSG_PerformOp(&m1,&m2,2,&output2); 00259 CSG_PerformOp(&m1,&m2,3,&output3); 00260 00261 if (!CSG_AddMeshToBlender(&output)) { 00262 return 0; 00263 } 00264 if (!CSG_AddMeshToBlender(&output2)) { 00265 return 0; 00266 } 00267 if (!CSG_AddMeshToBlender(&output3)) { 00268 return 0; 00269 } 00270 00271 00272 CSG_DestroyMeshDescriptor(&m1); 00273 CSG_DestroyMeshDescriptor(&m2); 00274 CSG_DestroyMeshDescriptor(&output); 00275 CSG_DestroyMeshDescriptor(&output2); 00276 CSG_DestroyMeshDescriptor(&output3); 00277 00278 return 1; 00279 } 00280 00281 #endif 00282