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

intersect_group.cpp « lib « carve « extern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ec28791150a65d5e224e3ff1c35d875cf3812eb2 (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
// 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 <carve/timing.hpp>

#include "csg_detail.hpp"
#include "intersect_common.hpp"

void carve::csg::CSG::makeEdgeMap(const carve::csg::FaceLoopList &loops,
                                  size_t edge_count,
                                  detail::LoopEdges &edge_map) {
#if defined(UNORDERED_COLLECTIONS_SUPPORT_RESIZE)
  edge_map.resize(edge_count);
#endif

  for (carve::csg::FaceLoop *i = loops.head; i; i = i->next) {
    edge_map.addFaceLoop(i);
    i->group = NULL;
  }
}

#include <carve/polyline.hpp>

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

void carve::csg::CSG::findSharedEdges(const detail::LoopEdges &edge_map_a,
                                      const detail::LoopEdges &edge_map_b,
                                      V2Set &shared_edges) {
  for (detail::LoopEdges::const_iterator
         i = edge_map_a.begin(), e = edge_map_a.end();
       i != e;
       ++i) {
    detail::LoopEdges::const_iterator j = edge_map_b.find((*i).first);
    if (j != edge_map_b.end()) {
      shared_edges.insert((*i).first);
    }
  }

#if defined(CARVE_DEBUG)
  detail::VVSMap edge_graph;

  for (V2Set::const_iterator i = shared_edges.begin(); i != shared_edges.end(); ++i) {
    edge_graph[(*i).first].insert((*i).second);
    edge_graph[(*i).second].insert((*i).first);
  }

  std::cerr << "*** testing consistency of edge graph" << std::endl;
  for (detail::VVSMap::const_iterator i = edge_graph.begin(); i != edge_graph.end(); ++i) {
    if ((*i).second.size() > 2) {
      std::cerr << "branch at: " << (*i).first << std::endl;
    }
    if ((*i).second.size() == 1) {
      std::cerr << "endpoint at: " << (*i).first << std::endl;
      std::cerr << "coordinate: " << (*i).first->v << std::endl;
    }
  }

  {
    carve::line::PolylineSet intersection_graph;
    intersection_graph.vertices.resize(edge_graph.size());
    std::map<const carve::mesh::MeshSet<3>::vertex_t *, size_t> vmap;

    size_t j = 0;
    for (detail::VVSMap::const_iterator i = edge_graph.begin(); i != edge_graph.end(); ++i) {
      intersection_graph.vertices[j].v = (*i).first->v;
      vmap[(*i).first] = j++;
    }

    while (edge_graph.size()) {
      detail::VVSMap::iterator prior_i = edge_graph.begin();
      carve::mesh::MeshSet<3>::vertex_t *prior = (*prior_i).first;
      std::vector<size_t> connected;
      connected.push_back(vmap[prior]);
      while (prior_i != edge_graph.end() && (*prior_i).second.size()) {
        carve::mesh::MeshSet<3>::vertex_t *next = *(*prior_i).second.begin();
        detail::VVSMap::iterator next_i = edge_graph.find(next);
        CARVE_ASSERT(next_i != edge_graph.end());
        connected.push_back(vmap[next]);
        (*prior_i).second.erase(next);
        (*next_i).second.erase(prior);
        if (!(*prior_i).second.size()) { edge_graph.erase(prior_i); prior_i = edge_graph.end(); }
        if (!(*next_i).second.size()) { edge_graph.erase(next_i); next_i = edge_graph.end(); }
        prior_i = next_i;
        prior = next;
      }
      bool closed = connected.front() == connected.back();
      for (size_t k = 0; k < connected.size(); ++k) {
        std::cerr << " " << connected[k];
      }
      std::cerr << std::endl;
      intersection_graph.addPolyline(closed, connected.begin(), connected.end());
    }

#if defined(CARVE_DEBUG_WRITE_PLY_DATA)
    std::string out("/tmp/intersection.ply");
    ::writePLY(out, &intersection_graph, true);
#endif
  }

  std::cerr << "*** edge graph consistency test done" << std::endl;
#endif
}



#if defined(CARVE_DEBUG_WRITE_PLY_DATA)
static carve::mesh::MeshSet<3> *groupToPolyhedron(const carve::csg::FaceLoopGroup &grp) {
  const carve::csg::FaceLoopList &fl = grp.face_loops;
  std::vector<carve::mesh::MeshSet<3>::face_t *> faces;
  faces.reserve(fl.size());
  for (carve::csg::FaceLoop *f = fl.head; f; f = f->next) {
    faces.push_back(f->orig_face->create(f->vertices.begin(), f->vertices.end(), false));
  }
  carve::mesh::MeshSet<3> *poly = new carve::mesh::MeshSet<3>(faces);

  poly->canonicalize();
  return poly;
}
#endif



void carve::csg::CSG::groupFaceLoops(carve::mesh::MeshSet<3> *src,
                                     carve::csg::FaceLoopList &face_loops,
                                     const carve::csg::detail::LoopEdges &loop_edges,
                                     const carve::csg::V2Set &no_cross,
                                     carve::csg::FLGroupList &out_loops) {
  // Find all the groups of face loops that are connected by edges
  // that are not part of no_cross.
  // this could potentially be done with a disjoint set data-structure.
#if defined(CARVE_DEBUG_WRITE_PLY_DATA)
  static int call_num = 0;
  call_num++;
#endif

  static carve::TimingName GROUP_FACE_LOOPS("groupFaceLoops()");

  carve::TimingBlock block(GROUP_FACE_LOOPS);

  int tag_num = 0;
  while (face_loops.size()) {
    out_loops.push_back(FaceLoopGroup(src));
    carve::csg::FaceLoopGroup &group = (out_loops.back());
    carve::csg::FaceLoopList &curr = (group.face_loops);
    carve::csg::V2Set &perim = (group.perimeter);

    carve::csg::FaceLoop *expand = face_loops.head;

    expand->group = &group;
    face_loops.remove(expand);
    curr.append(expand);

    while (expand) {
      std::vector<carve::mesh::MeshSet<3>::vertex_t *> &loop = (expand->vertices);
      carve::mesh::MeshSet<3>::vertex_t *v1, *v2;

      v1 = loop.back();
      for (size_t i = 0; i < loop.size(); ++i) {
        v2 = loop[i];

        carve::csg::V2Set::const_iterator nc = no_cross.find(std::make_pair(v1, v2));
        if (nc == no_cross.end()) {
          carve::csg::detail::LoopEdges::const_iterator j;

          j = loop_edges.find(std::make_pair(v1, v2));
          if (j != loop_edges.end()) {
            for (std::list<carve::csg::FaceLoop *>::const_iterator
                   k = (*j).second.begin(), ke = (*j).second.end();
                 k != ke; ++k) {
              if ((*k)->group != NULL ||
                  (*k)->orig_face->mesh != expand->orig_face->mesh) continue;
              face_loops.remove((*k));
              curr.append((*k));
              (*k)->group = &group;
            }
          }

          j = loop_edges.find(std::make_pair(v2, v1));
          if (j != loop_edges.end()) {
            for (std::list<carve::csg::FaceLoop *>::const_iterator
                   k = (*j).second.begin(), ke = (*j).second.end();
                 k != ke; ++k) {
              if ((*k)->group != NULL ||
                  (*k)->orig_face->mesh != expand->orig_face->mesh) continue;
              face_loops.remove((*k));
              curr.append((*k));
              (*k)->group = &group;
            }
          }
        } else {
          perim.insert(std::make_pair(v1, v2));
        }
        v1 = v2;
      }
      expand = expand->next;
    }
    tag_num++;

#if defined(CARVE_DEBUG_WRITE_PLY_DATA)
    {
      carve::mesh::MeshSet<3> *poly = groupToPolyhedron(group);
      char buf[128];
      sprintf(buf, "/tmp/group-%d-%p.ply", call_num, &curr);
      std::string out(buf);
      ::writePLY(out, poly, false);
      delete poly;
    }
#endif
  }
}