Goosefoot Mesher - CGAL
self_intersect.h
1 
20 /*
21  * This file is adapted from the very permissive CGAL-4.3 examples licence
22  * The licence is reproduced as follows:
23  * Copyright (c) 1996,1997,1998,1999,2000,2001,2002,2003,2004,2005,2006,2007
24  * Utrecht University (The Netherlands),
25  * ETH Zurich (Switzerland),
26  * INRIA Sophia-Antipolis (France),
27  * Max-Planck-Institute Saarbruecken (Germany),
28  * and Tel-Aviv University (Israel). All rights reserved.
29  *
30  * Permission is hereby granted, free of charge, to any person obtaining a
31  * copy of this software and associated documentation files (the
32  * "Software"), to deal in the Software without restriction, including
33  * without limitation the rights to use, copy, modify, merge, publish,
34  * distribute, sublicense, and/or sell copies of the Software, and to
35  * permit persons to whom the Software is furnished to do so, subject to
36  * the following conditions:
37  *
38  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
39  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
40  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
41  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
42  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
43  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
44  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
45  *
46  * */
47 
48 // compute self-intersection of a CGAL triangle polyhedron mesh
49 // original code from Lutz Kettner
50 #ifndef _SELF_INTERSECT_H_
51 #define _SELF_INTERSECT_H_
52 
53 #include "mesher_cgal.h"
54 
55 #include <CGAL/box_intersection_d.h>
56 #include <CGAL/intersections.h>
57 
58 template <class Polyhedron, class Kernel, class OutputIterator>
60 {
61  typedef typename Kernel::Segment_3 Segment;
62  typedef typename Kernel::Triangle_3 Triangle;
63  typedef typename Polyhedron::Facet_const_handle Facet_const_handle;
64  typedef typename Polyhedron::Facet_const_iterator Facet_const_iterator;
65  typedef typename Polyhedron::Halfedge_const_handle Halfedge_const_handle;
66  typedef typename CGAL::Box_intersection_d::Box_with_handle_d<double, 3, Facet_const_handle> Box;
67  mutable OutputIterator m_iterator;
68 
69 public:
70 
71  Intersect_facets(OutputIterator it)
72  : m_iterator(it)
73  {
74  }
75 
76  void operator()(const Box* b,
77  const Box* c) const
78  {
79  Halfedge_const_handle h = b->handle()->halfedge();
80 
81  // check for shared egde --> no intersection
82  if(h->opposite()->facet() == c->handle() ||
83  h->next()->opposite()->facet() == c->handle() ||
84  h->next()->next()->opposite()->facet() == c->handle())
85  return;
86 
87  // check for shared vertex --> maybe intersection, maybe not
88  Halfedge_const_handle g = c->handle()->halfedge();
89  Halfedge_const_handle v;
90 
91  if(h->vertex() == g->vertex())
92  v = g;
93  if(h->vertex() == g->next()->vertex())
94  v = g->next();
95  if(h->vertex() == g->next()->next()->vertex())
96  v = g->next()->next();
97 
98  if(v == Halfedge_const_handle())
99  {
100  h = h->next();
101  if(h->vertex() == g->vertex())
102  v = g;
103  if(h->vertex() == g->next()->vertex())
104  v = g->next();
105  if(h->vertex() == g->next()->next()->vertex())
106  v = g->next()->next();
107  if(v == Halfedge_const_handle())
108  {
109  h = h->next();
110  if(h->vertex() == g->vertex())
111  v = g;
112  if(h->vertex() == g->next()->vertex())
113  v = g->next();
114  if(h->vertex() == g->next()->next()->vertex())
115  v = g->next()->next();
116  }
117  }
118 
119  if(v != Halfedge_const_handle())
120  {
121  // found shared vertex:
122  CGAL_assertion(h->vertex() == v->vertex());
123  // geometric check if the opposite segments intersect the triangles
124  Triangle t1( h->vertex()->point(),
125  h->next()->vertex()->point(),
126  h->next()->next()->vertex()->point());
127  Triangle t2( v->vertex()->point(),
128  v->next()->vertex()->point(),
129  v->next()->next()->vertex()->point());
130  Segment s1( h->next()->vertex()->point(),
131  h->next()->next()->vertex()->point());
132  Segment s2( v->next()->vertex()->point(),
133  v->next()->next()->vertex()->point());
134 
135  if(CGAL::do_intersect(t1,s2))
136  {
137  *m_iterator++ = t1;
138  *m_iterator++ = t2;
139  }
140  else
141  if(CGAL::do_intersect(t2,s1))
142  {
143  *m_iterator++ = t1;
144  *m_iterator++ = t2;
145  }
146  return;
147  }
148 
149  // check for geometric intersection
150  Triangle t1( h->vertex()->point(),
151  h->next()->vertex()->point(),
152  h->next()->next()->vertex()->point());
153  Triangle t2( g->vertex()->point(),
154  g->next()->vertex()->point(),
155  g->next()->next()->vertex()->point());
156  if(CGAL::do_intersect(t1, t2))
157  {
158  *m_iterator++ = t1;
159  *m_iterator++ = t2;
160  }
161  } // end operator ()
162 }; // end struct Intersect_facets
163 
164 template <class Polyhedron, class Kernel, class OutputIterator>
165 void self_intersect(const Polyhedron& polyhedron,
166  OutputIterator out)
167 {
168  typedef typename Polyhedron::Facet_const_iterator Facet_const_iterator;
169  typedef typename Polyhedron::Facet_const_handle Facet_const_handle;
170  typedef typename CGAL::Box_intersection_d::Box_with_handle_d<double, 3, Facet_const_handle> Box;
171 
172  // make one box per facet
173  std::vector<Box> boxes;
174  boxes.reserve(polyhedron.size_of_facets());
175 
176  Facet_const_iterator f;
177  for(f = polyhedron.facets_begin();
178  f != polyhedron.facets_end();
179  f++)
180  boxes.push_back(Box( f->halfedge()->vertex()->point().bbox() +
181  f->halfedge()->next()->vertex()->point().bbox() +
182  f->halfedge()->next()->next()->vertex()->point().bbox(),
183  f));
184 
185  // generate box pointers
186  std::vector<const Box*> box_ptr;
187  box_ptr.reserve(polyhedron.size_of_facets());
188  typename std::vector<Box>::iterator b;
189  for(b = boxes.begin();
190  b != boxes.end();
191  b++)
192  box_ptr.push_back(&*b);
193 
194  // compute self-intersections filtered out by boxes
196  std::ptrdiff_t cutoff = 2000;
197  CGAL::box_self_intersection_d(box_ptr.begin(), box_ptr.end(),intersect_facets,cutoff);
198 
199 } // end self_intersect
200 
201 #endif // _SELF_INTERSECT_H_
Definition: self_intersect.h:59