forked from monasse/CELIA3D
-
Notifications
You must be signed in to change notification settings - Fork 0
/
intersections.hpp
executable file
·279 lines (237 loc) · 8.9 KB
/
intersections.hpp
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
//Copyright 2017 Laurent Monasse
/*
This file is part of CELIA3D.
CELIA3D is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
CELIA3D is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with CELIA3D. If not, see <http://www.gnu.org/licenses/>.
*/
/*! \file
\authors Maria Adela Puscas and Laurent Monasse
\brief Inclusion of files and definition of types from the \b CGAL library.
\warning <b> Specific coupling file ! </b>
*/
#ifndef INTERSECTIONS
#define INTERSECTIONS
#include <iostream>
#include <stdio.h>
#include <vector>
#include <math.h>
#include <cassert>
#include <CGAL/Exact_predicates_exact_constructions_kernel.h>
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/intersections.h>
#include <CGAL/Bbox_3.h>
#include <CGAL/Bbox_2.h>
#include <CGAL/Timer.h>
#include <CGAL/Triangulation_3.h>
#include <CGAL/Triangulation_2.h>
#include <CGAL/Polyhedron_3.h>
#include <CGAL/Tetrahedron_3.h>
#include <CGAL/convex_hull_3.h>
#include <CGAL/squared_distance_3.h>
#include <CGAL/box_intersection_d.h>
#include <CGAL/centroid.h>
#include <CGAL/number_utils.h>
typedef CGAL::Exact_predicates_exact_constructions_kernel Kernel;
typedef CGAL::Exact_predicates_inexact_constructions_kernel IK;
typedef CGAL::Cartesian_converter<Kernel,IK> Exact_to_Inexact;
typedef CGAL::Cartesian_converter<IK,Kernel> Inexact_to_Exact;
typedef Kernel::Point_3 Point_3;
typedef IK::Point_3 InexactPoint_3;
typedef Kernel::Vector_3 Vector_3;
typedef Kernel::Line_3 Line_3;
typedef Kernel::Plane_3 Plane_3;
typedef CGAL::Triangle_3<Kernel> Triangle_3;
typedef CGAL::Triangle_3<IK> InexactTriangle_3;
typedef CGAL::Plane_3<Kernel> Plane_3;
typedef std::vector<Triangle_3> Triangles;
typedef CGAL::Tetrahedron_3<Kernel> Tetrahedron;
typedef CGAL::Tetrahedron_3<IK> InexactTetrahedron;
typedef std::vector<Point_3> Points;
typedef Kernel::Segment_3 Segment_3;
typedef CGAL::Bbox_3 Bbox;
typedef CGAL::Polyhedron_3<IK> InexactPolyhedron_3;
typedef CGAL::Polyhedron_3<Kernel> Polyhedron_3;
typedef CGAL::Triangulation_3<Kernel> ExactTriangulation;
typedef CGAL::Triangulation_3<IK> InexactTriangulation;
typedef CGAL::Aff_transformation_3<Kernel> Aff_transformation_3;
typedef ExactTriangulation::Finite_facets_iterator ExactFinite_faces_iterator;
typedef ExactTriangulation::Finite_cells_iterator ExactFinite_cells_iterator;
typedef InexactTriangulation::Finite_facets_iterator InexactFinite_faces_iterator;
typedef InexactTriangulation::Finite_cells_iterator InexactFinite_cells_iterator;
typedef Polyhedron_3::Facet Facet;
typedef Polyhedron_3::Facet_iterator Facet_iterator;
typedef Polyhedron_3::Vertex_iterator Vertex_iterator;
typedef Polyhedron_3::Plane_iterator Plane_iterator;
typedef Polyhedron_3::Halfedge_around_vertex_circulator Halfedge_around_vertex_circulator;
typedef InexactPolyhedron_3::Facet InexactFacet;
typedef InexactPolyhedron_3::Facet_iterator InexactFacet_iterator;
typedef InexactPolyhedron_3::Vertex_iterator InexactVertex_iterator;
typedef InexactPolyhedron_3::Plane_iterator InexactPlane_iterator;
typedef InexactPolyhedron_3::Halfedge_around_vertex_circulator InexactHalfedge_around_vertex_circulator;
typedef Triangles::iterator Triangle3_iterator;
typedef CGAL::Triangulation_2<Kernel> Triangulation_2;
typedef Kernel::Point_2 Point_2;
typedef CGAL::Triangle_2<Kernel> Triangle_2;
typedef std::vector<Triangle_2> Triangles_2;
typedef std::vector<Point_2> Points_2;
typedef Kernel::Segment_2 Segment_2;
typedef CGAL::Bbox_2 Bbox_2;
typedef Triangles_2::iterator Triangle2_iterator;
typedef Triangles::iterator Triangle3_iterator;
/*! \brief Triangulation of the faces of a fluid cubic cell.
\details Bbox is a 3D bounding box. This allows to use the member functions of class <b> CGAL::Bbox_3 </b>.
\param cel Box_3d (fluid cubic cell)
\param trianglesB list of the triangle splitting of the faces of Box
\warning Specific coupling procedure !
\return void
*/
void triang_cellule(const Bbox& cel, Triangles& trianglesB){
trianglesB.clear();
Point_3 s1B(cel.xmin(), cel.ymin(), cel.zmin());
Point_3 r1B(cel.xmax(), cel.ymin(), cel.zmin());
Point_3 t1B(cel.xmax(), cel.ymax(), cel.zmin());
Point_3 v1B(cel.xmin(), cel.ymax(), cel.zmin());
Point_3 s2B(cel.xmin(), cel.ymin(), cel.zmax());
Point_3 r2B(cel.xmax(), cel.ymin(), cel.zmax());
Point_3 t2B(cel.xmax(), cel.ymax(), cel.zmax());
Point_3 v2B(cel.xmin(), cel.ymax(), cel.zmax());
//face1
Triangle_3 Tri1B(s1B,r1B,v1B);
Triangle_3 Tri2B(t1B,r1B,v1B);
trianglesB.push_back(Tri1B);
trianglesB.push_back(Tri2B);
//face2
Triangle_3 Tri5B(s2B,r2B,v2B);
Triangle_3 Tri6B(t2B,r2B,v2B);
trianglesB.push_back(Tri5B);
trianglesB.push_back(Tri6B);
//face3
Triangle_3 Tri9B(s2B,s1B,v2B);
Triangle_3 Tri10B(v1B,s1B,v2B);
trianglesB.push_back(Tri9B);
trianglesB.push_back(Tri10B);
//face4
Triangle_3 Tri13B(r2B,r1B,t2B);
Triangle_3 Tri14B(t1B,r1B,t2B);
trianglesB.push_back(Tri13B);
trianglesB.push_back(Tri14B);
//face5
Triangle_3 Tri17B(v2B,v1B,t2B);
Triangle_3 Tri18B(t1B,v1B,t2B);
trianglesB.push_back(Tri17B);
trianglesB.push_back(Tri18B);
//face6
Triangle_3 Tri21B(s2B,s1B,r2B);
Triangle_3 Tri22B(r1B,s1B,r2B);
trianglesB.push_back(Tri21B);
trianglesB.push_back(Tri22B);
}
/*!
\brief Edge/triangle intersection
\return std:vector<Point_3>
*/
std::vector<Point_3> intersection_bis(const Segment_3& seg, const Triangle_3& t)
{
std::vector<Point_3> result;
Point_3 P;
Segment_3 s;
const CGAL::Object& intersec = CGAL::intersection(seg,t);
if(CGAL::assign(P,intersec)){
result.push_back(P);
}
else if(CGAL::assign(s,intersec)){
result.push_back(s.operator[](0));
result.push_back(s.operator[](1));
}
return result;
}
/*!
\brief New version of the triangle/triangle intersection
\details Goal: accelerate the computation, since we do not need the precise intersection type, onl the list of extremal points of the intersection.\n
Reduces to an edge/triangle intersection in CGAL
*/
std::vector<Point_3> intersection_bis(const Triangle_3& t1, const Triangle_3& t2)
{
std::vector<Point_3> result;
//Intersections between the segments of t1 and triangle t2
for(int k=0;k<3;k++){
int kp = (k+1)%3;
Point_3 P;
Segment_3 seg;
Segment_3 arete(t1.operator[](k),t1.operator[](kp));
const CGAL::Object& intersec = CGAL::intersection(arete,t2);
if(CGAL::assign(P,intersec)){
result.push_back(P);
}
else if(CGAL::assign(seg,intersec)){
result.push_back(seg.operator[](0));
result.push_back(seg.operator[](1));
}
}
//Intersections between the segments of t2 and triangle t1
for(int k=0;k<3;k++){
int kp = (k+1)%3;
Point_3 P;
Segment_3 seg;
Segment_3 arete(t2.operator[](k),t2.operator[](kp));
const CGAL::Object& intersec = CGAL::intersection(arete,t1);
if(CGAL::assign(P,intersec)){
result.push_back(P);
}
else if(CGAL::assign(seg,intersec)){
result.push_back(seg.operator[](0));
result.push_back(seg.operator[](1));
}
}
return result;
}
/* ! \brief Determine whether a point is inside a tetrahedron
\details Simple application of CGAL function <b> Tetrahedron_3::has_on_bounded_side <\b>
*/
bool inside_tetra(const Tetrahedron &tetra, const Point_3& P){
return tetra.has_on_bounded_side(P);
}
/*! \brief Test whether points are coplanar
*/
bool coplanar(std::vector<Point_3>::iterator begin, std::vector<Point_3>::iterator end)
{
bool test=true;
bool test_confondus=true;
Point_3 P1,P2;
P1 = *begin;
std::vector<Point_3>::iterator it;
for(it=begin;it!=end && test_confondus;it++){
P2 = *it;
test_confondus = (P1==P2);
}
if(test_confondus){
return true;
} else {
bool test_collinear=true;
Point_3 P3;
std::vector<Point_3>::iterator iter;
for(iter=it;iter!=end && test_collinear;iter++){
P3 = *iter;
test_collinear = CGAL::collinear(P1,P2,P3);
}
if(test_collinear){
return true;
} else {
Point_3 P4;
for(std::vector<Point_3>::iterator iterat=iter;iterat!=end && test;iterat++){
P4 = *iterat;
test = CGAL::coplanar(P1,P2,P3,P4);
}
}
}
return test;
}
#endif