Rework carve integration into boolean modifier
[blender.git] / extern / carve / carve-util.h
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) 2014 Blender Foundation.
19  * All rights reserved.
20  *
21  * Contributor(s): Blender Foundation,
22  *                 Sergey Sharybin
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 #ifndef __CARVE_UTIL_H__
28 #define __CARVE_UTIL_H__
29
30 #include <carve/csg.hpp>
31 #include <carve/geom3d.hpp>
32 #include <carve/interpolator.hpp>
33 #include <carve/mesh.hpp>
34
35 #include "carve-capi.h"
36
37 void carve_getRescaleMinMax(const carve::mesh::MeshSet<3> *left,
38                             const carve::mesh::MeshSet<3> *right,
39                             carve::geom3d::Vector *min,
40                             carve::geom3d::Vector *max);
41
42 void carve_unionIntersections(carve::csg::CSG *csg,
43                               carve::mesh::MeshSet<3> **left_r,
44                               carve::mesh::MeshSet<3> **right_r);
45
46 bool carve_checkPolyPlanarAndGetNormal(const std::vector<carve::geom3d::Vector> &vertices,
47                                        const int verts_per_poly,
48                                        const int *verts_of_poly,
49                                        carve::math::Matrix3 *axis_matrix_r);
50
51 int carve_triangulatePoly(struct ImportMeshData *import_data,
52                           CarveMeshImporter *mesh_importer,
53                           int poly_index,
54                           int start_loop_index,
55                           const std::vector<carve::geom3d::Vector> &vertices,
56                           const int verts_per_poly,
57                           const int *verts_of_poly,
58                           const carve::math::Matrix3 &axis_matrix,
59                           std::vector<int> *face_indices,
60                           std::vector<int> *orig_loop_index_map,
61                           std::vector<int> *orig_poly_index_map);
62
63 namespace carve {
64         namespace interpolate {
65
66                 template<typename attr_t>
67                 class VertexAttr : public Interpolator {
68                 public:
69                         typedef const meshset_t::vertex_t *key_t;
70
71                 protected:
72                         typedef std::unordered_map<key_t, attr_t> attrmap_t;
73
74                         attrmap_t attrs;
75
76                         virtual void resultFace(const carve::csg::CSG &csg,
77                                                 const meshset_t::face_t *new_face,
78                                                 const meshset_t::face_t *orig_face,
79                                                 bool flipped)
80                         {
81                                 typedef meshset_t::face_t::const_edge_iter_t const_edge_iter_t;
82                                 for (const_edge_iter_t new_edge_iter = new_face->begin();
83                                          new_edge_iter != new_face->end();
84                                          ++new_edge_iter)
85                                 {
86                                         typename attrmap_t::const_iterator found =
87                                                 attrs.find(new_edge_iter->vert);
88                                         if (found == attrs.end()) {
89                                                 for (const_edge_iter_t orig_edge_iter = orig_face->begin();
90                                                          orig_edge_iter != orig_face->end();
91                                                          ++orig_edge_iter)
92                                                 {
93                                                         if ((orig_edge_iter->vert->v - new_edge_iter->vert->v).length2() < 1e-5) {
94                                                                 attrs[new_edge_iter->vert] = attrs[orig_edge_iter->vert];
95                                                         }
96                                                 }
97                                         }
98                                 }
99                         }
100
101                 public:
102                         bool hasAttribute(const meshset_t::vertex_t *v) {
103                                 return attrs.find(v) != attrs.end();
104                         }
105
106                         const attr_t &getAttribute(const meshset_t::vertex_t *v, const attr_t &def = attr_t()) {
107                                 typename attrmap_t::const_iterator found = attrs.find(v);
108                                 if (found != attrs.end()) {
109                                         return found->second;
110                                 }
111                                 return def;
112                         }
113
114                         void setAttribute(const meshset_t::vertex_t *v, const attr_t &attr) {
115                                 attrs[v] = attr;
116                         }
117                 };
118
119         }  // namespace interpolate
120 }  // namespace carve
121
122 #endif  // __CARVE_UTIL_H__