KratosMultiphysics
KRATOS Multiphysics (Kratos) is a framework for building parallel, multi-disciplinary simulation software, aiming at modularity, extensibility, and high performance. Kratos is written in C++, and counts with an extensive Python interface.
brep_trimming_utilities.h
Go to the documentation of this file.
1 // | / |
2 // ' / __| _` | __| _ \ __|
3 // . \ | ( | | ( |\__ `
4 // _|\_\_| \__,_|\__|\___/ ____/
5 // Multi-Physics
6 //
7 // License: BSD License
8 // Kratos default license: kratos/license.txt
9 
10 #if !defined(KRATOS_BREP_TRIMMING_UTILITIES_H_INCLUDED)
11 #define KRATOS_BREP_TRIMMING_UTILITIES_H_INCLUDED
12 
13 // Std includes
14 #include <list>
15 
16 // System includes
17 #include "includes/define.h"
18 
19 // External includes
20 #include "clipper/include/clipper2/clipper.h"
21 
22 // Project includes
23 #include "geometries/geometry.h"
25 
27 #include "includes/node.h"
28 
29 namespace Kratos
30 {
33 
34  //using namespace ClipperLib;
35 
36  class KRATOS_API(KRATOS_CORE) BrepTrimmingUtilities
37  {
38  public:
41 
42  typedef std::size_t IndexType;
43  typedef std::size_t SizeType;
44 
46  typedef std::vector<IntegrationPointType> IntegrationPointsArrayType;
47 
49 
50  //typedef CurveTessellation<PointerVector<Node>> CurveTesselationType;
51  typedef std::vector<std::pair<double, CoordinatesArrayType>> TessellationType;
52 
53  typedef signed long long cInt;
54 
55  //template<class TBrepLoopType, class TPointType>
56  static void CreateBrepSurfaceTrimmingIntegrationPoints(
57  IntegrationPointsArrayType& rIntegrationPoints,
60  const std::vector<double>& rSpansU,
61  const std::vector<double>& rSpansV,
62  IntegrationInfo& rIntegrationInfo);
63 
64  struct DPState {
65  bool visible;
66  double weight;
67  long bestvertex;
68  };
69 
70  struct Diagonal {
71  long index1;
72  long index2;
73  };
74 
75  //Triangulation
76  static void Triangulate_OPT(const Clipper2Lib::Path64& polygon, std::vector<Matrix>& triangles, const double factor)
77  {
78  if (polygon.size() == 4)
79  {
80  Matrix triangle(3, 2);
81  triangle(0, 0) = BrepTrimmingUtilities::IntPointToDoublePoint(polygon[0], factor)[0];
82  triangle(0, 1) = BrepTrimmingUtilities::IntPointToDoublePoint(polygon[0], factor)[1];
83  triangle(1, 0) = BrepTrimmingUtilities::IntPointToDoublePoint(polygon[1], factor)[0];
84  triangle(1, 1) = BrepTrimmingUtilities::IntPointToDoublePoint(polygon[1], factor)[1];
85  triangle(2, 0) = BrepTrimmingUtilities::IntPointToDoublePoint(polygon[2], factor)[0];
86  triangle(2, 1) = BrepTrimmingUtilities::IntPointToDoublePoint(polygon[2], factor)[1];
87  triangles.push_back(triangle);
88  return;
89  }
90 
91  array_1d<double, 2> p1, p2, p3, p4;
92  int bestvertex;
93  double weight = 0;
94  double d1, d2 = 0.0;
95  double minweight = std::numeric_limits<double>::max();
96  Diagonal diagonal, newdiagonal;
97 
98  std::list<Diagonal> diagonals;
99 
100  IndexType n = polygon.size();
101  std::vector< Clipper2Lib::Point64 > const& points = polygon;
102  matrix<DPState> dpstates(n, n);
103 
104  //init states and visibility
105  for (IndexType i = 0; i < (n - 1); i++) {
107  for (IndexType j = i + 1; j < n; j++) {
108  dpstates(j, i).visible = true;
109  dpstates(j, i).weight = 0;
110  dpstates(j, i).bestvertex = -1;
111  if (j != (i + 1)) {
113 
114  //visibility check
115  if (i == 0) p3 = BrepTrimmingUtilities::IntPointToDoublePoint(points[n - 1], factor);
116  else p3 = BrepTrimmingUtilities::IntPointToDoublePoint(points[i - 1], factor);
117  if (i == (n - 1)) p4 = BrepTrimmingUtilities::IntPointToDoublePoint(points[0], factor);
118  else p4 = BrepTrimmingUtilities::IntPointToDoublePoint(points[i + 1], factor);
119  if (!BrepTrimmingUtilities::InCone(p3, p1, p4, p2)) {
120  dpstates(j, i).visible = false;
121  continue;
122  }
123 
124  if (j == 0) p3 = BrepTrimmingUtilities::IntPointToDoublePoint(points[n - 1], factor);
125  else p3 = BrepTrimmingUtilities::IntPointToDoublePoint(points[j - 1], factor);
126  if (j == (n - 1)) p4 = BrepTrimmingUtilities::IntPointToDoublePoint(points[0], factor);
127  else p4 = BrepTrimmingUtilities::IntPointToDoublePoint(points[j + 1], factor);
128  if (!BrepTrimmingUtilities::InCone(p3, p2, p4, p1)) {
129  dpstates(j, i).visible = false;
130  continue;
131  }
132 
133  for (IndexType k = 0; k < n; k++) {
135  if (k == (n - 1)) p4 = BrepTrimmingUtilities::IntPointToDoublePoint(points[0], factor);
136  else p4 = BrepTrimmingUtilities::IntPointToDoublePoint(points[k + 1], factor);
137  if (BrepTrimmingUtilities::Intersects(p1, p2, p3, p4)) {
138  dpstates(j, i).visible = false;
139  break;
140  }
141  }
142  }
143  }
144  }
145 
146  dpstates(n - 1, 0).visible = true;
147  dpstates(n - 1, 0).weight = 0;
148  dpstates(n - 1, 0).bestvertex = -1;
149 
150  for (IndexType gap = 2; gap < n; gap++) {
151  for (IndexType i = 0; i < (n - gap); i++) {
152  IndexType j = i + gap;
153  if (!dpstates(j, i).visible) continue;
154  bestvertex = -1;
155  for (IndexType k = (i + 1); k < j; k++) {
156  if (!dpstates(k, i).visible) continue;
157  if (!dpstates(j, k).visible) continue;
158 
159  if (k <= (i + 1)) d1 = 0;
161  if (j <= (k + 1)) d2 = 0;
163 
164  weight = dpstates(k, i).weight + dpstates(j, k).weight + d1 + d2;
165 
166  if ((bestvertex == -1) || (weight < minweight)) {
167  bestvertex = k;
168  minweight = weight;
169  }
170  }
171  if (bestvertex == -1) {
172  return;
173  }
174 
175  dpstates(j, i).bestvertex = bestvertex;
176  dpstates(j, i).weight = minweight;
177  }
178  }
179 
180  newdiagonal.index1 = 0;
181  newdiagonal.index2 = n - 1;
182  diagonals.push_back(newdiagonal);
183 
184  while (!diagonals.empty()) {
185  diagonal = *(diagonals.begin());
186  diagonals.pop_front();
187 
188  bestvertex = dpstates(diagonal.index2, diagonal.index1).bestvertex;
189  if (bestvertex == -1) {
190  break;
191  }
192  Matrix triangle(3, 2);
193  triangle(0, 0) = BrepTrimmingUtilities::IntPointToDoublePoint(points[diagonal.index1], factor)[0];
194  triangle(0, 1) = BrepTrimmingUtilities::IntPointToDoublePoint(points[diagonal.index1], factor)[1];
195  triangle(1, 0) = BrepTrimmingUtilities::IntPointToDoublePoint(points[bestvertex], factor)[0];
196  triangle(1, 1) = BrepTrimmingUtilities::IntPointToDoublePoint(points[bestvertex], factor)[1];
197  triangle(2, 0) = BrepTrimmingUtilities::IntPointToDoublePoint(points[diagonal.index2], factor)[0];
198  triangle(2, 1) = BrepTrimmingUtilities::IntPointToDoublePoint(points[diagonal.index2], factor)[1];
199 
200  if (BrepTrimmingUtilities::GetAreaOfTriangle(triangle) > 1e-5)
201  triangles.push_back(triangle);
202  else
203  {
204  std::cout << "triangle with zero area" << BrepTrimmingUtilities::GetAreaOfTriangle(triangle) << std::endl;
205  }
206  if (bestvertex > (diagonal.index1 + 1)) {
207  newdiagonal.index1 = diagonal.index1;
208  newdiagonal.index2 = bestvertex;
209  diagonals.push_back(newdiagonal);
210  }
211  if (diagonal.index2 > (bestvertex + 1)) {
212  newdiagonal.index1 = bestvertex;
213  newdiagonal.index2 = diagonal.index2;
214  diagonals.push_back(newdiagonal);
215  }
216  }
217  }
218 
221  {
222  if (BrepTrimmingUtilities::IsConvex(p1, p2, p3)) {
223  if (!BrepTrimmingUtilities::IsConvex(p1, p2, p)) return false;
224  if (!BrepTrimmingUtilities::IsConvex(p2, p3, p)) return false;
225  return true;
226  }
227  else {
228  if (BrepTrimmingUtilities::IsConvex(p1, p2, p)) return true;
229  if (BrepTrimmingUtilities::IsConvex(p2, p3, p)) return true;
230  return false;
231  }
232  }
233 
234  static bool IsConvex(
235  const array_1d<double, 2>& p1,
236  const array_1d<double, 2>& p2,
237  const array_1d<double, 2>& p3)
238  {
239  double tmp;
240  tmp = (p3[1] - p1[1]) * (p2[0] - p1[0]) - (p3[0] - p1[0]) * (p2[1] - p1[1]);
241  if (tmp > 0) return true;
242  else return false;
243  }
244 
245  static double Distance(array_1d<double, 2> point_1, array_1d<double, 2> point_2)
246  {
247  return sqrt(point_1[0] * point_2[0] + point_1[1] * point_2[1]);
248  }
249 
250  static double GetAreaOfTriangle(const Matrix& triangle)
251  {
252  double area = (triangle(0, 0) * (triangle(1, 1) - triangle(2, 1))
253  + triangle(1, 0) * (triangle(2, 1) - triangle(0, 1))
254  + triangle(2, 0) * (triangle(0, 1) - triangle(1, 1))) / 2;
255 
256  return area;
257  }
258 
259  //checks if two lines intersect
262  {
263  if ((p11[0] == p21[0]) && (p11[1] == p21[1])) return false;
264  if ((p11[0] == p22[0]) && (p11[1] == p22[1])) return false;
265  if ((p12[0] == p21[0]) && (p12[1] == p21[1])) return false;
266  if ((p12[0] == p22[0]) && (p12[1] == p22[1])) return false;
267 
268  array_1d<double, 2> v1ort, v2ort, v;
269  double dot11, dot12, dot21, dot22;
270 
271  v1ort[0] = p12[1] - p11[1];
272  v1ort[1] = p11[0] - p12[0];
273 
274  v2ort[0] = p22[1] - p21[1];
275  v2ort[1] = p21[0] - p22[0];
276 
277  v[0] = p21[0] - p11[0];
278  v[1] = p21[1] - p11[1];
279  dot21 = v[0] * v1ort[0] + v[1] * v1ort[1];
280  v[0] = p22[0] - p11[0];
281  v[1] = p22[1] - p11[1];
282  dot22 = v[0] * v1ort[0] + v[1] * v1ort[1];
283 
284  v[0] = p11[0] - p21[0];
285  v[1] = p11[1] - p21[1];
286  dot11 = v[0] * v2ort[0] + v[1] * v2ort[1];
287  v[0] = p12[0] - p21[0];
288  v[1] = p12[1] - p21[1];
289  dot12 = v[0] * v2ort[0] + v[1] * v2ort[1];
290 
291  if (dot11 * dot12 > 0) return false;
292  if (dot21 * dot22 > 0) return false;
293 
294  return true;
295  }
296 
297  static Clipper2Lib::Point64 ToIntPoint(
298  const double x,
299  const double y,
300  const double factor)
301  {
302  Clipper2Lib::Point64 int_point;
303 
304  int_point.x = static_cast<cInt>(x / factor);
305  int_point.y = static_cast<cInt>(y / factor);
306 
307  return int_point;
308  }
309 
311  const Clipper2Lib::Point64& int_point,
312  const double factor)
313  {
314  array_1d<double, 2> point;
315 
316  point[0] = int_point.x * factor;
317  point[1] = int_point.y * factor;
318 
319  return point;
320  }
321 
323  };
325 } // namespace Kratos.
326 
327 #endif // KRATOS_BREP_TRIMMING_UTILITIES_H_INCLUDED defined
The BrepCurveOnSurface acts as topology for curves on surfaces.
Definition: brep_curve_on_surface.h:44
Definition: brep_trimming_utilities.h:37
signed long long cInt
Definition: brep_trimming_utilities.h:53
static bool IsConvex(const array_1d< double, 2 > &p1, const array_1d< double, 2 > &p2, const array_1d< double, 2 > &p3)
Definition: brep_trimming_utilities.h:234
static bool InCone(array_1d< double, 2 > &p1, array_1d< double, 2 > &p2, array_1d< double, 2 > &p3, array_1d< double, 2 > &p)
Definition: brep_trimming_utilities.h:219
static double Distance(array_1d< double, 2 > point_1, array_1d< double, 2 > point_2)
Definition: brep_trimming_utilities.h:245
array_1d< double, 3 > CoordinatesArrayType
Definition: brep_trimming_utilities.h:48
std::vector< std::pair< double, CoordinatesArrayType > > TessellationType
Definition: brep_trimming_utilities.h:51
std::vector< IntegrationPointType > IntegrationPointsArrayType
Definition: brep_trimming_utilities.h:46
std::size_t SizeType
Definition: brep_trimming_utilities.h:43
static bool Intersects(array_1d< double, 2 > &p11, array_1d< double, 2 > &p12, array_1d< double, 2 > &p21, array_1d< double, 2 > &p22)
Definition: brep_trimming_utilities.h:260
static Clipper2Lib::Point64 ToIntPoint(const double x, const double y, const double factor)
Definition: brep_trimming_utilities.h:297
std::size_t IndexType
Definition: brep_trimming_utilities.h:42
IntegrationPoint< 3 > IntegrationPointType
Definition: brep_trimming_utilities.h:45
static array_1d< double, 2 > IntPointToDoublePoint(const Clipper2Lib::Point64 &int_point, const double factor)
Definition: brep_trimming_utilities.h:310
static void Triangulate_OPT(const Clipper2Lib::Path64 &polygon, std::vector< Matrix > &triangles, const double factor)
Definition: brep_trimming_utilities.h:76
static double GetAreaOfTriangle(const Matrix &triangle)
Definition: brep_trimming_utilities.h:250
Integration information for the creation of integration points.
Definition: integration_info.h:35
Short class definition.
Definition: integration_point.h:52
Definition: amatrix_interface.h:41
PointerVector is a container like stl vector but using a vector to store pointers to its data.
Definition: pointer_vector.h:72
static double max(double a, double b)
Definition: GeometryFunctions.h:79
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
y
Other simbols definition.
Definition: generate_axisymmetric_navier_stokes_element.py:54
v
Definition: generate_convection_diffusion_explicit_element.py:114
tuple tmp
Definition: generate_total_lagrangian_mixed_volumetric_strain_element.py:98
int n
manufactured solution and derivatives (u=0 at z=0 dudz=0 at z=domain_height)
Definition: ode_solve.py:402
int k
Definition: quadrature.py:595
int j
Definition: quadrature.py:648
p
Definition: sensitivityMatrix.py:52
x
Definition: sensitivityMatrix.py:49
integer i
Definition: TensorModule.f:17
subroutine d1(DSTRAN, D, dtime, NDI, NSHR, NTENS)
Definition: TensorModule.f:594
e
Definition: run_cpp_mpi_tests.py:31
float factor
for node in (self.combined_model_part).Nodes: pold = node.GetSolutionStepValue(PRESSURE,...
Definition: ulf_PGLASS.py:254
Definition: brep_trimming_utilities.h:64
long bestvertex
Definition: brep_trimming_utilities.h:67
double weight
Definition: brep_trimming_utilities.h:66
bool visible
Definition: brep_trimming_utilities.h:65
Definition: brep_trimming_utilities.h:70
long index1
Definition: brep_trimming_utilities.h:71
long index2
Definition: brep_trimming_utilities.h:72