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

csg_collector.cpp « lib « carve « extern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3986a918a5e63dd013c364fceac070de15bff682 (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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
// 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:


#if defined(HAVE_CONFIG_H)
#  include <carve_config.h>
#endif

#include <carve/csg.hpp>
#include <iostream>
#include "csg_collector.hpp"
#include "intersect_debug.hpp"

#if defined(CARVE_DEBUG_WRITE_PLY_DATA)
void writePLY(const std::string &out_file, const carve::mesh::MeshSet<3> *poly, bool ascii);
#endif


namespace carve {
  namespace csg {
    namespace {

      class BaseCollector : public CSG::Collector {
        BaseCollector();
        BaseCollector(const BaseCollector &);
        BaseCollector &operator=(const BaseCollector &);

      protected:
        struct face_data_t {
          carve::mesh::MeshSet<3>::face_t *face;
          const carve::mesh::MeshSet<3>::face_t *orig_face;
          bool flipped;
          face_data_t(carve::mesh::MeshSet<3>::face_t *_face,
                      const carve::mesh::MeshSet<3>::face_t *_orig_face,
                      bool _flipped) : face(_face), orig_face(_orig_face), flipped(_flipped) {
          };
        };

        std::list<face_data_t> faces;

        const carve::mesh::MeshSet<3> *src_a;
        const carve::mesh::MeshSet<3> *src_b;
    
        BaseCollector(const carve::mesh::MeshSet<3> *_src_a,
                      const carve::mesh::MeshSet<3> *_src_b) : CSG::Collector(), src_a(_src_a), src_b(_src_b) {
        }

        virtual ~BaseCollector() {
        }

        void FWD(const carve::mesh::MeshSet<3>::face_t *orig_face,
                 const std::vector<carve::mesh::MeshSet<3>::vertex_t *> &vertices,
                 carve::geom3d::Vector /* normal */,
                 bool /* poly_a */,
                 FaceClass face_class,
                 CSG::Hooks &hooks) {
          std::vector<carve::mesh::MeshSet<3>::face_t *> new_faces;
          new_faces.reserve(1);
          new_faces.push_back(orig_face->create(vertices.begin(), vertices.end(), false));
          hooks.processOutputFace(new_faces, orig_face, false);
          for (size_t i = 0; i < new_faces.size(); ++i) {
            faces.push_back(face_data_t(new_faces[i], orig_face, false));
          }

#if defined(CARVE_DEBUG) && defined(DEBUG_PRINT_RESULT_FACES)
          std::cerr << "+" << ENUM(face_class) << " ";
          for (unsigned i = 0; i < vertices.size(); ++i) std::cerr << " " << vertices[i] << ":" << *vertices[i];
          std::cerr << std::endl;
#endif
        }

        void REV(const carve::mesh::MeshSet<3>::face_t *orig_face,
                 const std::vector<carve::mesh::MeshSet<3>::vertex_t *> &vertices,
                 carve::geom3d::Vector /* normal */,
                 bool /* poly_a */,
                 FaceClass face_class,
                 CSG::Hooks &hooks) {
          // normal = -normal;
          std::vector<carve::mesh::MeshSet<3>::face_t *> new_faces;
          new_faces.reserve(1);
          new_faces.push_back(orig_face->create(vertices.begin(), vertices.end(), true));
          hooks.processOutputFace(new_faces, orig_face, true);
          for (size_t i = 0; i < new_faces.size(); ++i) {
            faces.push_back(face_data_t(new_faces[i], orig_face, true));
          }

#if defined(CARVE_DEBUG) && defined(DEBUG_PRINT_RESULT_FACES)
          std::cerr << "-" << ENUM(face_class) << " ";
          for (unsigned i = 0; i < vertices.size(); ++i) std::cerr << " " << vertices[i] << ":" << *vertices[i];
          std::cerr << std::endl;
#endif
        }

        virtual void collect(const carve::mesh::MeshSet<3>::face_t *orig_face,
                             const std::vector<carve::mesh::MeshSet<3>::vertex_t *> &vertices,
                             carve::geom3d::Vector normal,
                             bool poly_a,
                             FaceClass face_class,
                             CSG::Hooks &hooks) =0;

        virtual void collect(FaceLoopGroup *grp, CSG::Hooks &hooks) {
          std::list<ClassificationInfo> &cinfo = (grp->classification);

          if (cinfo.size() == 0) {
            std::cerr << "WARNING! group " << grp << " has no classification info!" << std::endl;
            return;
          }
          
          FaceClass fc = FACE_UNCLASSIFIED;

          unsigned fc_closed_bits = 0;
          unsigned fc_open_bits = 0;
          unsigned fc_bits = 0;

          for (std::list<ClassificationInfo>::const_iterator i = grp->classification.begin(), e = grp->classification.end(); i != e; ++i) {

            if ((*i).intersected_mesh == NULL) {
              // classifier only returns global info
              fc_closed_bits = class_to_class_bit((*i).classification);
              break;
            }

            if ((*i).classification == FACE_UNCLASSIFIED) continue;
            if ((*i).intersectedMeshIsClosed()) {
              fc_closed_bits |= class_to_class_bit((*i).classification);
            } else {
              fc_open_bits |= class_to_class_bit((*i).classification);
            }
          }

          if (fc_closed_bits) {
            fc_bits = fc_closed_bits;
          } else {
            fc_bits = fc_open_bits;
          }

          fc = class_bit_to_class(fc_bits);

          // handle the complex cases where a group is classified differently with respect to two or more closed manifolds.
          if (fc == FACE_UNCLASSIFIED) {
            unsigned inout_bits = fc_bits & FACE_NOT_ON_BIT;
            unsigned on_bits = fc_bits & FACE_ON_BIT;

            // both in and out. indicates an invalid manifold embedding.
            if (inout_bits == (FACE_IN_BIT | FACE_OUT_BIT)) goto out;

            // on, both orientations. could be caused by two manifolds touching at a face.
            if (on_bits == (FACE_ON_ORIENT_IN_BIT | FACE_ON_ORIENT_OUT_BIT)) goto out;

            // in or out, but also on (with orientation). the on classification takes precedence.
            fc = class_bit_to_class(on_bits);
          }

        out:

          if (fc == FACE_UNCLASSIFIED) {
            std::cerr << "group " << grp << " is unclassified!" << std::endl;

#if defined(CARVE_DEBUG_WRITE_PLY_DATA)
            static int uc_count = 0;

            std::vector<carve::mesh::MeshSet<3>::face_t *> faces;

            for (FaceLoop *f = grp->face_loops.head; f; f = f->next) {
              carve::mesh::MeshSet<3>::face_t *temp = f->orig_face->create(f->vertices.begin(), f->vertices.end(), false);
              faces.push_back(temp);
            }

            carve::mesh::MeshSet<3> *p = new carve::mesh::MeshSet<3>(faces);

            std::ostringstream filename;
            filename << "classifier_fail_" << ++uc_count << ".ply";
            std::string out(filename.str().c_str());
            ::writePLY(out, p, false);

            delete p;
#endif

            return;
          }

          bool is_poly_a = grp->src == src_a;

          for (FaceLoop *f = grp->face_loops.head; f; f = f->next) {
            collect(f->orig_face, f->vertices, f->orig_face->plane.N, is_poly_a, fc, hooks);
          }
        }

        virtual carve::mesh::MeshSet<3> *done(CSG::Hooks &hooks) {
          std::vector<carve::mesh::MeshSet<3>::face_t *> f;
          f.reserve(faces.size());
          for (std::list<face_data_t>::iterator i = faces.begin(); i != faces.end(); ++i) {
            f.push_back((*i).face);
          }

          carve::mesh::MeshSet<3> *p = new carve::mesh::MeshSet<3>(f);

          if (hooks.hasHook(carve::csg::CSG::Hooks::RESULT_FACE_HOOK)) {
            for (std::list<face_data_t>::iterator i = faces.begin(); i != faces.end(); ++i) {
              hooks.resultFace((*i).face, (*i).orig_face, (*i).flipped);
            }
          }

          return p;
        }
      };



      class AllCollector : public BaseCollector {
      public:
        AllCollector(const carve::mesh::MeshSet<3> *_src_a,
                     const carve::mesh::MeshSet<3> *_src_b) : BaseCollector(_src_a, _src_b) {
        }
        virtual ~AllCollector() {
        }
        virtual void collect(FaceLoopGroup *grp, CSG::Hooks &hooks) {
          for (FaceLoop *f = grp->face_loops.head; f; f = f->next) {
            FWD(f->orig_face, f->vertices, f->orig_face->plane.N, f->orig_face->mesh->meshset == src_a, FACE_OUT, hooks);
          }
        }
        virtual void collect(const carve::mesh::MeshSet<3>::face_t *orig_face,
                             const std::vector<carve::mesh::MeshSet<3>::vertex_t *> &vertices,
                             carve::geom3d::Vector normal,
                             bool poly_a,
                             FaceClass face_class,
                             CSG::Hooks &hooks) {
          FWD(orig_face, vertices, normal, poly_a, face_class, hooks);
        }
      };



      class UnionCollector : public BaseCollector {
      public:
        UnionCollector(const carve::mesh::MeshSet<3> *_src_a,
                       const carve::mesh::MeshSet<3> *_src_b) : BaseCollector(_src_a, _src_b) {
        }
        virtual ~UnionCollector() {
        }
        virtual void collect(const carve::mesh::MeshSet<3>::face_t *orig_face,
                             const std::vector<carve::mesh::MeshSet<3>::vertex_t *> &vertices,
                             carve::geom3d::Vector normal,
                             bool poly_a,
                             FaceClass face_class,
                             CSG::Hooks &hooks) {
          if (face_class == FACE_OUT || (poly_a && face_class == FACE_ON_ORIENT_OUT)) {
            FWD(orig_face, vertices, normal, poly_a, face_class, hooks);
          }
        }
      };



      class IntersectionCollector : public BaseCollector {
      public:
        IntersectionCollector(const carve::mesh::MeshSet<3> *_src_a,
                              const carve::mesh::MeshSet<3> *_src_b) : BaseCollector(_src_a, _src_b) {
        }
        virtual ~IntersectionCollector() {
        }
        virtual void collect(const carve::mesh::MeshSet<3>::face_t *orig_face,
                             const std::vector<carve::mesh::MeshSet<3>::vertex_t *> &vertices,
                             carve::geom3d::Vector normal,
                             bool poly_a,
                             FaceClass face_class,
                             CSG::Hooks &hooks) {
          if (face_class == FACE_IN || (poly_a && face_class == FACE_ON_ORIENT_OUT)) {
            FWD(orig_face, vertices, normal, poly_a, face_class, hooks);
          }
        }
      };



      class SymmetricDifferenceCollector : public BaseCollector {
      public:
        SymmetricDifferenceCollector(const carve::mesh::MeshSet<3> *_src_a,
                                     const carve::mesh::MeshSet<3> *_src_b) : BaseCollector(_src_a, _src_b) {
        }
        virtual ~SymmetricDifferenceCollector() {
        }
        virtual void collect(const carve::mesh::MeshSet<3>::face_t *orig_face,
                             const std::vector<carve::mesh::MeshSet<3>::vertex_t *> &vertices,
                             carve::geom3d::Vector normal,
                             bool poly_a,
                             FaceClass face_class,
                             CSG::Hooks &hooks) {
          if (face_class == FACE_OUT) {
            FWD(orig_face, vertices, normal, poly_a, face_class, hooks);
          } else if (face_class == FACE_IN) {
            REV(orig_face, vertices, normal, poly_a, face_class, hooks);
          }
        }
      };



      class AMinusBCollector : public BaseCollector {
      public:
        AMinusBCollector(const carve::mesh::MeshSet<3> *_src_a,
                         const carve::mesh::MeshSet<3> *_src_b) : BaseCollector(_src_a, _src_b) {
        }
        virtual ~AMinusBCollector() {
        }
        virtual void collect(const carve::mesh::MeshSet<3>::face_t *orig_face,
                             const std::vector<carve::mesh::MeshSet<3>::vertex_t *> &vertices,
                             carve::geom3d::Vector normal,
                             bool poly_a,
                             FaceClass face_class,
                             CSG::Hooks &hooks) {
          if ((face_class == FACE_OUT || face_class == FACE_ON_ORIENT_IN) && poly_a) {
            FWD(orig_face, vertices, normal, poly_a, face_class, hooks);
          } else if (face_class == FACE_IN && !poly_a) {
            REV(orig_face, vertices, normal, poly_a, face_class, hooks);
          }
        }
      };



      class BMinusACollector : public BaseCollector {
      public:
        BMinusACollector(const carve::mesh::MeshSet<3> *_src_a,
                         const carve::mesh::MeshSet<3> *_src_b) : BaseCollector(_src_a, _src_b) {
        }
        virtual ~BMinusACollector() {
        }
        virtual void collect(const carve::mesh::MeshSet<3>::face_t *orig_face,
                             const std::vector<carve::mesh::MeshSet<3>::vertex_t *> &vertices,
                             carve::geom3d::Vector normal,
                             bool poly_a,
                             FaceClass face_class,
                             CSG::Hooks &hooks) {
          if ((face_class == FACE_OUT || face_class == FACE_ON_ORIENT_IN) && !poly_a) {
            FWD(orig_face, vertices, normal, poly_a, face_class, hooks);
          } else if (face_class == FACE_IN && poly_a) {
            REV(orig_face, vertices, normal, poly_a, face_class, hooks);
          }
        }
      };

    }

    CSG::Collector *makeCollector(CSG::OP op,
                                  const carve::mesh::MeshSet<3> *poly_a,
                                  const carve::mesh::MeshSet<3> *poly_b) {
      switch (op) {
      case CSG::UNION:                return new UnionCollector(poly_a, poly_b);
      case CSG::INTERSECTION:         return new IntersectionCollector(poly_a, poly_b);
      case CSG::A_MINUS_B:            return new AMinusBCollector(poly_a, poly_b);
      case CSG::B_MINUS_A:            return new BMinusACollector(poly_a, poly_b);
      case CSG::SYMMETRIC_DIFFERENCE: return new SymmetricDifferenceCollector(poly_a, poly_b);
      case CSG::ALL:                  return new AllCollector(poly_a, poly_b);
      }
      return NULL;
    }
  }
}