Welcome to mirror list, hosted at ThFree Co, Russian Federation.

tree.hpp « carve « include « carve « extern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1a3f6b04ef7fa37d3a9b7d3e21f112a4ed622630 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324

// Begin License:
// Copyright (C) 2006-2014 Tobias Sargeant (tobias.sargeant@gmail.com).
// All rights reserved.
//
// This file is part of the Carve CSG Library (http://carve-csg.com/)
//
// This file may be used under the terms of either the GNU General
// Public License version 2 or 3 (at your option) as published by the
// Free Software Foundation and appearing in the files LICENSE.GPL2
// and LICENSE.GPL3 included in the packaging of this file.
//
// This file is provided "AS IS" with NO WARRANTY OF ANY KIND,
// INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE.
// End:


#pragma once

#include <carve/carve.hpp>

#include <carve/matrix.hpp>
#include <carve/timing.hpp>
#include <carve/rescale.hpp>

namespace carve {
  namespace csg {

    class CSG_TreeNode {
      CSG_TreeNode(const CSG_TreeNode &);
      CSG_TreeNode &operator=(const CSG_TreeNode &);

    protected:
  
    public:
      CSG_TreeNode() {
      }

      virtual ~CSG_TreeNode() {
      }

      virtual carve::mesh::MeshSet<3> *eval(bool &is_temp, CSG &csg) =0;

      virtual carve::mesh::MeshSet<3> *eval(CSG &csg) {
        bool temp;
        carve::mesh::MeshSet<3> *r = eval(temp, csg);
        if (!temp) r = r->clone();
        return r;
      }
    };



    class CSG_TransformNode : public CSG_TreeNode {
      carve::math::Matrix transform;
      CSG_TreeNode *child;

    public:
      CSG_TransformNode(const carve::math::Matrix &_transform, CSG_TreeNode *_child) : transform(_transform), child(_child) {
      }
      virtual ~CSG_TransformNode() {
        delete child;
      }

      virtual carve::mesh::MeshSet<3> *eval(bool &is_temp, CSG &csg) {
        carve::mesh::MeshSet<3> *result = child->eval(is_temp, csg);
        if (!is_temp) {
          result = result->clone();
          is_temp = true;
        }
        result->transform(carve::math::matrix_transformation(transform));
        return result;
      }
    };




    class CSG_InvertNode : public CSG_TreeNode {
      std::vector<bool> selected_meshes;
      CSG_TreeNode *child;

    public:
      CSG_InvertNode(CSG_TreeNode *_child) : selected_meshes(), child(_child) {
      }
      CSG_InvertNode(int g_id, CSG_TreeNode *_child) : selected_meshes(), child(_child) {
        selected_meshes.resize(g_id + 1, false);
        selected_meshes[g_id] = true;
      }
      virtual ~CSG_InvertNode() {
        delete child;
      }

      template<typename T>
      CSG_InvertNode(T start, T end, CSG_TreeNode *_child) : selected_meshes(), child(_child) {
        while (start != end) {
          int g_id = (int)(*start);
          if (selected_meshes.size() < g_id + 1) selected_meshes.resize(g_id + 1, false);
          selected_meshes[g_id] = true;
          ++start;
        }
      }

      virtual carve::mesh::MeshSet<3> *eval(bool &is_temp, CSG &csg) {
        bool c_temp;
        carve::mesh::MeshSet<3> *c = child->eval(c_temp, csg);
        if (!c_temp) c = c->clone();
        if (!selected_meshes.size()) {
          c->invert();
        } else {
          for (size_t i = 0; i < c->meshes.size() && i < selected_meshes.size(); ++i) {
            if (selected_meshes[i]) {
              c->meshes[i]->invert();
            }
          }
        }
        is_temp = true;
        return c;
      }
    };




    class CSG_SelectNode : public CSG_TreeNode {
      std::vector<bool> selected_meshes;
      CSG_TreeNode *child;

    public:
      CSG_SelectNode(int m_id, CSG_TreeNode *_child) : selected_meshes(), child(_child) {
        selected_meshes.resize(m_id + 1, false);
        selected_meshes[m_id] = true;
      }

      template<typename T>
      CSG_SelectNode(T start, T end, CSG_TreeNode *_child) : selected_meshes(), child(_child) {
        while (start != end) {
          int m_id = (int)(*start);
          if ((int)selected_meshes.size() < m_id + 1) selected_meshes.resize(m_id + 1, false);
          selected_meshes[m_id] = true;
          ++start;
        }
      }

      virtual ~CSG_SelectNode() {
        delete child;
      }

      virtual carve::mesh::MeshSet<3> *eval(bool &is_temp, CSG &csg) {
        bool c_temp;
        carve::mesh::MeshSet<3> *c = child->eval(c_temp, csg);
        if (!c_temp) c = c->clone();
        size_t i = 0;
        size_t j = 0;
        for (size_t i = 0; i < c->meshes.size(); ++i) {
          if (i >= selected_meshes.size() || !selected_meshes[i]) {
            delete c->meshes[i];
            c->meshes[i] = NULL;
          } else {
            c->meshes[j++] = c->meshes[i];
          }
        }
        c->meshes.erase(c->meshes.begin() + j, c->meshes.end());
        c->collectVertices();
        is_temp = true;
        return c;
      }
    };




    class CSG_PolyNode : public CSG_TreeNode {
      carve::mesh::MeshSet<3> *poly;
      bool del;

    public:
      CSG_PolyNode(carve::mesh::MeshSet<3> *_poly, bool _del) : poly(_poly), del(_del)  {
      }
      virtual ~CSG_PolyNode() {
        static carve::TimingName FUNC_NAME("delete polyhedron");
        carve::TimingBlock block(FUNC_NAME);
    
        if (del) {
          delete poly;
        }
      }

      virtual carve::mesh::MeshSet<3> *eval(bool &is_temp, CSG &csg) {
        is_temp = false;
        return poly;
      }
    };



    class CSG_OPNode : public CSG_TreeNode {
      CSG_TreeNode *left, *right;
      CSG::OP op;
      bool rescale;
      CSG::CLASSIFY_TYPE classify_type;

    public:
      CSG_OPNode(CSG_TreeNode *_left,
                 CSG_TreeNode *_right,
                 CSG::OP _op,
                 bool _rescale,
                 CSG::CLASSIFY_TYPE _classify_type = CSG::CLASSIFY_NORMAL) : left(_left), right(_right), op(_op), rescale(_rescale), classify_type(_classify_type) {
      }

      virtual ~CSG_OPNode() {
        delete left;
        delete right;
      }

      void minmax(double &min_x, double &min_y, double &min_z,
                  double &max_x, double &max_y, double &max_z,
                  const std::vector<carve::geom3d::Vector> &points) {
        for (unsigned i = 1; i < points.size(); ++i) {
          min_x = std::min(min_x, points[i].x);
          max_x = std::max(max_x, points[i].x);
          min_y = std::min(min_y, points[i].y);
          max_y = std::max(max_y, points[i].y);
          min_z = std::min(min_z, points[i].z);
          max_z = std::max(max_z, points[i].z);
        }
      }

      virtual carve::mesh::MeshSet<3> *evalScaled(bool &is_temp, CSG &csg) {
        carve::mesh::MeshSet<3> *l, *r;
        bool l_temp, r_temp;

        l = left->eval(l_temp, csg);
        r = right->eval(r_temp, csg);

        if (!l_temp) { l = l->clone(); }
        if (!r_temp) { r = r->clone(); }

        carve::geom3d::Vector min, max;
        carve::geom3d::Vector min_l, max_l;
        carve::geom3d::Vector min_r, max_r;

        carve::geom::bounds<3>(l->vertex_storage.begin(),
                               l->vertex_storage.end(),
                               carve::mesh::Face<3>::vector_mapping(),
                               min_l,
                               max_l);
        carve::geom::bounds<3>(r->vertex_storage.begin(),
                               r->vertex_storage.end(),
                               carve::mesh::Face<3>::vector_mapping(),
                               min_r,
                               max_r);

        carve::geom::assign_op(min, min_l, min_r, carve::util::min_functor());
        carve::geom::assign_op(max, max_l, max_r, carve::util::max_functor());

        carve::rescale::rescale scaler(min.x, min.y, min.z, max.x, max.y, max.z);

        carve::rescale::fwd fwd_r(scaler);
        carve::rescale::rev rev_r(scaler);

        l->transform(fwd_r);
        r->transform(fwd_r);

        carve::mesh::MeshSet<3> *result = NULL;
        {
          static carve::TimingName FUNC_NAME("csg.compute()");
          carve::TimingBlock block(FUNC_NAME);
          result = csg.compute(l, r, op, NULL, classify_type);
        }

        {
          static carve::TimingName FUNC_NAME("delete polyhedron");
          carve::TimingBlock block(FUNC_NAME);
      
          delete l;
          delete r;
        }

        result->transform(rev_r);

        is_temp = true;
        return result;
      }
  
      virtual carve::mesh::MeshSet<3> *evalUnscaled(bool &is_temp, CSG &csg) {
        carve::mesh::MeshSet<3> *l, *r;
        bool l_temp, r_temp;

        l = left->eval(l_temp, csg);
        r = right->eval(r_temp, csg);

        carve::mesh::MeshSet<3> *result = NULL;
        {
          static carve::TimingName FUNC_NAME("csg.compute()");
          carve::TimingBlock block(FUNC_NAME);
          result = csg.compute(l, r, op, NULL, classify_type);
        }

        {
          static carve::TimingName FUNC_NAME("delete polyhedron");
          carve::TimingBlock block(FUNC_NAME);
      
          if (l_temp) delete l;
          if (r_temp) delete r;
        }

        is_temp = true;
        return result;
      }
  

      virtual carve::mesh::MeshSet<3> *eval(bool &is_temp, CSG &csg) {
        if (rescale) {
          return evalScaled(is_temp, csg);
        } else {
          return evalUnscaled(is_temp, csg);
        }
      }
    };

  }
}