accident when merge. revert to r37405
[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 /** \file bsp/intern/CSG_BooleanOps.cpp
29  *  \ingroup bsp
30  */
31
32
33 /**
34
35  * Implementation of external api for CSG part of BSP lib interface.
36  */
37
38 #include "../extern/CSG_BooleanOps.h"
39 #include "BSP_CSGMesh_CFIterator.h"
40 #include "MEM_RefCountPtr.h"
41
42 #include "../../boolop/extern/BOP_Interface.h"
43 #include <iostream>
44 using namespace std;
45
46 #include "BSP_MeshPrimitives.h"
47
48 struct BSP_MeshInfo {
49         BSP_CSGMesh *output_mesh;
50 };
51
52 using namespace std;
53         
54         CSG_BooleanOperation * 
55 CSG_NewBooleanFunction(
56         void
57 ){
58         BSP_MeshInfo * mesh_info = new BSP_MeshInfo;
59         CSG_BooleanOperation * output = new CSG_BooleanOperation;
60
61         if (mesh_info==NULL || output==NULL) return NULL;
62
63         mesh_info->output_mesh = NULL;
64         output->CSG_info = mesh_info;
65
66         return output;
67 }
68         
69 /**
70  * Compute the boolean operation, UNION, INTERSECION or DIFFERENCE
71  */
72         int
73 CSG_PerformBooleanOperation(
74         CSG_BooleanOperation                    *operation,
75         CSG_OperationType                               op_type,
76         CSG_FaceIteratorDescriptor              obAFaces,
77         CSG_VertexIteratorDescriptor    obAVertices,
78         CSG_FaceIteratorDescriptor              obBFaces,
79         CSG_VertexIteratorDescriptor    obBVertices
80 ){
81         if (operation == NULL) return 0;
82         BSP_MeshInfo * mesh_info = static_cast<BSP_MeshInfo *>(operation->CSG_info);
83         if (mesh_info == NULL) return 0;
84
85         obAFaces.Reset(obAFaces.it);
86         obBFaces.Reset(obBFaces.it);
87         obAVertices.Reset(obAVertices.it);
88         obBVertices.Reset(obBVertices.it);
89
90         BoolOpType boolType;
91         
92         switch( op_type ) {
93         case e_csg_union:
94           boolType = BOP_UNION;
95           break;
96         case e_csg_difference:
97           boolType = BOP_DIFFERENCE;
98           break;
99         default:
100           boolType = BOP_INTERSECTION;
101           break;
102         }
103
104         BoolOpState boolOpResult;
105         try {
106         boolOpResult = BOP_performBooleanOperation( boolType,
107                                      (BSP_CSGMesh**) &(mesh_info->output_mesh),
108                                          obAFaces, obAVertices, obBFaces, obBVertices);
109         }
110         catch(...) {
111                 return 0;
112         }
113
114         switch (boolOpResult) {
115         case BOP_OK: return 1;
116         case BOP_NO_SOLID: return -2;
117         case BOP_ERROR: return 0;
118         default: return 1;
119         }
120 }
121
122         int
123 CSG_OutputFaceDescriptor(
124         CSG_BooleanOperation * operation,
125         CSG_FaceIteratorDescriptor * output
126 ){
127         if (operation == NULL) return 0;
128         BSP_MeshInfo * mesh_info = static_cast<BSP_MeshInfo *>(operation->CSG_info);
129
130         if (mesh_info == NULL) return 0;
131         if (mesh_info->output_mesh == NULL) return 0;
132
133         BSP_CSGMesh_FaceIt_Construct(mesh_info->output_mesh,output);
134         return 1;
135 }
136
137
138         int
139 CSG_OutputVertexDescriptor(
140         CSG_BooleanOperation * operation,
141         CSG_VertexIteratorDescriptor *output
142 ){
143         if (operation == NULL) return 0;
144         BSP_MeshInfo * mesh_info = static_cast<BSP_MeshInfo *>(operation->CSG_info);
145
146         if (mesh_info == NULL) return 0;
147         if (mesh_info->output_mesh == NULL) return 0;
148
149         BSP_CSGMeshVertexIt_Construct(mesh_info->output_mesh,output);
150         return 1;
151 }
152
153         void
154 CSG_FreeVertexDescriptor(
155         CSG_VertexIteratorDescriptor * v_descriptor
156 ){      
157         BSP_CSGMesh_VertexIt_Destruct(v_descriptor);
158 }       
159
160
161         void
162 CSG_FreeFaceDescriptor(
163         CSG_FaceIteratorDescriptor * f_descriptor
164 ){
165         BSP_CSGMesh_FaceIt_Destruct(f_descriptor);
166 }
167
168
169         void
170 CSG_FreeBooleanOperation(
171         CSG_BooleanOperation *operation
172 ){
173         if (operation != NULL) {
174                 BSP_MeshInfo * mesh_info = static_cast<BSP_MeshInfo *>(operation->CSG_info);
175
176                 delete (mesh_info->output_mesh);
177                 delete(mesh_info);
178                 delete(operation);
179         }
180 }
181