include headers in cmake source, added a script to check for consistency, reporting...
[blender.git] / intern / bsp / intern / CSG_BooleanOps.cpp
1 /**
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): none yet.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /**
29
30  * Implementation of external api for CSG part of BSP lib interface.
31  */
32
33 #include "../extern/CSG_BooleanOps.h"
34 #include "BSP_CSGMesh_CFIterator.h"
35 #include "MEM_RefCountPtr.h"
36
37 #include "../../boolop/extern/BOP_Interface.h"
38 #include <iostream>
39 using namespace std;
40
41 #include "BSP_MeshPrimitives.h"
42
43 struct BSP_MeshInfo {
44         BSP_CSGMesh *output_mesh;
45 };
46
47 using namespace std;
48         
49         CSG_BooleanOperation * 
50 CSG_NewBooleanFunction(
51         void
52 ){
53         BSP_MeshInfo * mesh_info = new BSP_MeshInfo;
54         CSG_BooleanOperation * output = new CSG_BooleanOperation;
55
56         if (mesh_info==NULL || output==NULL) return NULL;
57
58         mesh_info->output_mesh = NULL;
59         output->CSG_info = mesh_info;
60
61         return output;
62 }
63         
64 /**
65  * Compute the boolean operation, UNION, INTERSECION or DIFFERENCE
66  */
67         int
68 CSG_PerformBooleanOperation(
69         CSG_BooleanOperation                    *operation,
70         CSG_OperationType                               op_type,
71         CSG_FaceIteratorDescriptor              obAFaces,
72         CSG_VertexIteratorDescriptor    obAVertices,
73         CSG_FaceIteratorDescriptor              obBFaces,
74         CSG_VertexIteratorDescriptor    obBVertices
75 ){
76         if (operation == NULL) return 0;
77         BSP_MeshInfo * mesh_info = static_cast<BSP_MeshInfo *>(operation->CSG_info);
78         if (mesh_info == NULL) return 0;
79
80         obAFaces.Reset(obAFaces.it);
81         obBFaces.Reset(obBFaces.it);
82         obAVertices.Reset(obAVertices.it);
83         obBVertices.Reset(obBVertices.it);
84
85         BoolOpType boolType;
86         
87         switch( op_type ) {
88         case e_csg_union:
89           boolType = BOP_UNION;
90           break;
91         case e_csg_difference:
92           boolType = BOP_DIFFERENCE;
93           break;
94         default:
95           boolType = BOP_INTERSECTION;
96           break;
97         }
98
99         BoolOpState boolOpResult;
100         try {
101         boolOpResult = BOP_performBooleanOperation( boolType,
102                                      (BSP_CSGMesh**) &(mesh_info->output_mesh),
103                                          obAFaces, obAVertices, obBFaces, obBVertices);
104         }
105         catch(...) {
106                 return 0;
107         }
108
109         switch (boolOpResult) {
110         case BOP_OK: return 1;
111         case BOP_NO_SOLID: return -2;
112         case BOP_ERROR: return 0;
113         default: return 1;
114         }
115 }
116
117         int
118 CSG_OutputFaceDescriptor(
119         CSG_BooleanOperation * operation,
120         CSG_FaceIteratorDescriptor * output
121 ){
122         if (operation == NULL) return 0;
123         BSP_MeshInfo * mesh_info = static_cast<BSP_MeshInfo *>(operation->CSG_info);
124
125         if (mesh_info == NULL) return 0;
126         if (mesh_info->output_mesh == NULL) return 0;
127
128         BSP_CSGMesh_FaceIt_Construct(mesh_info->output_mesh,output);
129         return 1;
130 }
131
132
133         int
134 CSG_OutputVertexDescriptor(
135         CSG_BooleanOperation * operation,
136         CSG_VertexIteratorDescriptor *output
137 ){
138         if (operation == NULL) return 0;
139         BSP_MeshInfo * mesh_info = static_cast<BSP_MeshInfo *>(operation->CSG_info);
140
141         if (mesh_info == NULL) return 0;
142         if (mesh_info->output_mesh == NULL) return 0;
143
144         BSP_CSGMeshVertexIt_Construct(mesh_info->output_mesh,output);
145         return 1;
146 }
147
148         void
149 CSG_FreeVertexDescriptor(
150         CSG_VertexIteratorDescriptor * v_descriptor
151 ){      
152         BSP_CSGMesh_VertexIt_Destruct(v_descriptor);
153 }       
154
155
156         void
157 CSG_FreeFaceDescriptor(
158         CSG_FaceIteratorDescriptor * f_descriptor
159 ){
160         BSP_CSGMesh_FaceIt_Destruct(f_descriptor);
161 }
162
163
164         void
165 CSG_FreeBooleanOperation(
166         CSG_BooleanOperation *operation
167 ){
168         if (operation != NULL) {
169                 BSP_MeshInfo * mesh_info = static_cast<BSP_MeshInfo *>(operation->CSG_info);
170
171                 delete (mesh_info->output_mesh);
172                 delete(mesh_info);
173                 delete(operation);
174         }
175 }
176