OOFEM  2.4
OOFEM.org - Object Oriented Finite Element Solver
BoostInterface.h
Go to the documentation of this file.
1 /*
2  * BoostInterface.h
3  *
4  * Created on: Jun 5, 2013
5  *
6  * @author Erik Svenning
7  *
8  * Description: Interface to some useful Boost functions.
9  * Boost is subject to the Boost license:
10  *
11  * Boost Software License - Version 1.0 - August 17th, 2003
12  *
13  * Permission is hereby granted, free of charge, to any person or organization
14  * obtaining a copy of the software and accompanying documentation covered by
15  * this license (the "Software") to use, reproduce, display, distribute,
16  * execute, and transmit the Software, and to prepare derivative works of the
17  * Software, and to permit third-parties to whom the Software is furnished to
18  * do so, all subject to the following:
19  *
20  * The copyright notices in the Software and this entire statement, including
21  * the above license grant, this restriction and the following disclaimer,
22  * must be included in all copies of the Software, in whole or in part, and
23  * all derivative works of the Software, unless such copies or derivative
24  * works are solely in the form of machine-executable object code generated by
25  * a source language processor.
26  *
27  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29  * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
30  * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
31  * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
32  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
33  * DEALINGS IN THE SOFTWARE.
34  *
35  */
36 
37 #ifdef __BOOST_MODULE
38 
39 #ifndef BOOSTINTERFACE_H_
40  #define BOOSTINTERFACE_H_
41 
42 
43  #include <iostream>
44  #include <list>
45 
46  #include <boost/geometry.hpp>
47  #include <boost/geometry/geometries/segment.hpp>
48  #include <boost/geometry/geometries/linestring.hpp>
49  #include <boost/geometry/geometries/point_xy.hpp>
50  #include <boost/geometry/geometries/polygon.hpp>
51  #include <boost/geometry/multi/geometries/multi_point.hpp>
52  #include <boost/geometry/multi/geometries/multi_polygon.hpp>
53 
54  #include <boost/foreach.hpp>
55 
56  #include <boost/numeric/ublas/matrix.hpp>
57 
59 // Typedefs for boost types
60 
61 // 2D point
62 typedef boost :: geometry :: model :: d2 :: point_xy< double >bPoint2;
63 
64 // 2D segment
65 typedef boost :: geometry :: model :: segment< bPoint2 >bSeg2;
66 
67 // 2D line
68 typedef boost :: geometry :: model :: linestring< bPoint2 >bLine2;
69 
70 // Matrix
71 typedef boost :: numeric :: ublas :: matrix< double >bMatrix;
72 
74 // Distance
75 
76 // Distance between two points in 2D
77 inline double bDist(const bPoint2 &iP1, const bPoint2 &iP2) { return boost :: geometry :: distance(iP1, iP2); }
78 
79 // Distance between point and line in 2D
80 inline double bDist(const bPoint2 &iP, const bLine2 &iL) { return boost :: geometry :: distance(iP, iL); }
81 
82 // Distance between point and line segment in 2D
83 inline double bDist(const bPoint2 &iP, const bSeg2 &iL) { return boost :: geometry :: distance(iP, iL); }
84 
85 // Distane between two line segments in 2D
86 inline double bDist(const bSeg2 &iLS1, const bSeg2 &iLS2, bPoint2 *oIntersectionPoint = NULL);
87 
88 
89 
91 // Overlap checks
92 inline bool bOverlap(const bPoint2 &iLC1, const bPoint2 &iUC1, const bPoint2 &iLC2, const bPoint2 &iUC2);
93 
95 // Vector operations
96 
97 inline double bDot(const bPoint2 &iP1, const bPoint2 &iP2) { return boost :: geometry :: dot_product(iP1, iP2); }
98 
99 inline double bNorm(const bPoint2 &iP1) { return sqrt( iP1.x() * iP1.x() + iP1.y() * iP1.y() ); }
100 inline void bNormalized(bPoint2 &ioP);
102 // Matrix operations
103 inline bool bSolve2by2(const bMatrix &iA, const bPoint2 &ib, bPoint2 &ox);
104 
105 
107 // Basic operations
108 //inline int bSign(const double &iVal) { return boost::math::sign(iVal);}
109 
110 
111 
112 
113 
114 
115 
116 
117 
118 
119 
120 
121 
122 
123 
124 
125 
126 
127 
129 // Implementation
130 
131 inline double bDist(const bSeg2 &iLS1, const bSeg2 &iLS2, bPoint2 *oIntersectionPoint)
132 {
133  bPoint2 pSpE( iLS1.second.x() - iLS1.first.x(), iLS1.second.y() - iLS1.first.y() );
134  double l1 = bNorm(pSpE);
135  bPoint2 n1(pSpE.x() / l1, pSpE.y() / l1);
136 
137  bPoint2 qSqE( iLS2.second.x() - iLS2.first.x(), iLS2.second.y() - iLS2.first.y() );
138  double l2 = bNorm(qSqE);
139  bPoint2 n2(qSqE.x() / l2, qSqE.y() / l2);
140 
141 
142  bPoint2 psqs( iLS2.first.x() - iLS1.first.x(), iLS2.first.y() - iLS1.first.y() );
143  bPoint2 b( 2.0 * l1 * bDot(psqs, n1), -2.0 * l2 * bDot(psqs, n2) );
144 
145 
146  bMatrix A(2, 2);
147  A(0, 0) = 2.0 * l1 * l1;
148  A(0, 1) = -2.0 *l1 *l2 *bDot(n1, n2);
149  A(1, 0) = A(0, 1);
150  A(1, 1) = 2.0 * l2 * l2;
151 
152  bPoint2 x(0.0, 0.0);
153 
154  // If we do not succeed to invert A,
155  // the lines are probably parallel.
156  // This case needs to be treated separately
157  if ( bSolve2by2(A, b, x) ) {
158  // Clip to the range of the segments
159  if ( x.x() < 0.0 ) {
160  x.x(0.0);
161  }
162 
163  if ( x.x() > 1.0 ) {
164  x.x(1.0);
165  }
166 
167  if ( x.y() < 0.0 ) {
168  x.y(0.0);
169  }
170 
171  if ( x.y() > 1.0 ) {
172  x.y(1.0);
173  }
174 
175  bPoint2 p( ( 1.0 - x.x() ) * iLS1.first.x() + x.x() * iLS1.second.x(), ( 1.0 - x.x() ) * iLS1.first.y() + x.x() * iLS1.second.y() );
176  bPoint2 q( ( 1.0 - x.y() ) * iLS2.first.x() + x.y() * iLS2.second.x(), ( 1.0 - x.y() ) * iLS2.first.y() + x.y() * iLS2.second.y() );
177 
178  if ( oIntersectionPoint != NULL ) {
179  oIntersectionPoint->x( 0.5 * ( p.x() + q.x() ) );
180  oIntersectionPoint->y( 0.5 * ( p.y() + q.y() ) );
181  }
182 
183  return bDist(p, q);
184  } else {
185  // The lines are parallel (or we have screwed up the input).
186 
187  // TODO: Check with end points properly.
188  // For now, pick the start point on segment 1
189  // and compute the distance from that point to
190  // segment 2.
191 
192  bPoint2 p( iLS1.first.x(), iLS1.first.y() );
193  return bDist(p, iLS2);
194  }
195 }
196 
197 inline bool bOverlap(const bPoint2 &iLC1, const bPoint2 &iUC1, const bPoint2 &iLC2, const bPoint2 &iUC2)
198 {
199  if ( iUC1.x() > iLC2.x() && iUC1.y() > iLC2.y() ) {
200  if ( iLC1.x() < iUC2.x() && iLC1.y() < iUC2.y() ) {
201  return true;
202  }
203  }
204 
205  return false;
206 }
207 
208 inline void bNormalized(bPoint2 &ioP)
209 {
210  double l = bNorm(ioP);
211 
212  ioP.x(ioP.x() / l);
213  ioP.y(ioP.y() / l);
214 }
215 
216 inline bool bSolve2by2(const bMatrix &iA, const bPoint2 &ib, bPoint2 &ox)
217 {
218  double tol = 1.0e-12;
219  double det = iA(0, 0) * iA(1, 1) - iA(0, 1) * iA(1, 0);
220 
221  if ( fabs(det) > tol ) {
222  ox.x( ( iA(1, 1) * ib.x() - iA(0, 1) * ib.y() ) / det );
223  ox.y( ( -iA(1, 0) * ib.x() + iA(0, 0) * ib.y() ) / det );
224  return true;
225  } else {
226  return false;
227  //printf("Warning in bSolve2by2. det: %e\n", det);
228  }
229 }
230 
231 
232 
233 
234 #endif /* BOOSTINTERFACE_H_ */
235 
236 #endif

This page is part of the OOFEM documentation. Copyright (c) 2011 Borek Patzak
Project e-mail: info@oofem.org
Generated at Tue Jan 2 2018 20:07:27 for OOFEM by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2011