OpenWalnut  1.3.1
WMath.cpp
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #include "WMath.h"
26 #include "WPlane.h"
27 #include "linearAlgebra/WLinearAlgebra.h"
28 #include "../WAssert.h"
29 #include "../WLimits.h"
30 
31 bool testIntersectTriangle( const WPosition& p1, const WPosition& p2, const WPosition& p3, const WPlane& p )
32 {
33  const WVector3d& normal = p.getNormal();
34  const WPosition& planePoint = p.getPosition();
35 
36  double r1 = dot( normal, p1 - planePoint );
37  double r2 = dot( normal, p2 - planePoint );
38  double r3 = dot( normal, p3 - planePoint );
39 
40  // TODO(math): use signum here!
41  if( std::abs( ( ( r1 > 0 ) - ( r1 < 0 ) ) + ( ( r2 > 0) - ( r2 < 0 ) ) + ( ( r3 > 0 ) - ( r3 < 0 ) ) ) == 3 )
42  {
43  return false;
44  }
45  return true;
46 }
47 
48 bool intersectPlaneSegment( const WPlane& p,
49  const WPosition& p1,
50  const WPosition& p2,
51  boost::shared_ptr< WPosition > pointOfIntersection )
52 {
53  const WVector3d& normal = normalize( p.getNormal() );
54  double const d = dot( normal, p.getPosition() );
55  WAssert( pointOfIntersection.get(), "Place to store a point of intersection is not ready!" );
56  *pointOfIntersection = p.getPosition(); // otherwise it would be undefined
57 
58  // at least one point is in plane (maybe the whole segment)
59  if( std::abs( dot( normal, p1 - p.getPosition() ) ) <= 2 * wlimits::DBL_EPS )
60  {
61  *pointOfIntersection = p1;
62  return true;
63  }
64  else if( std::abs( dot( normal, p2 - p.getPosition() ) ) <= 2 * wlimits::DBL_EPS )
65  {
66  *pointOfIntersection = p2;
67  return true;
68  }
69 
70  if( std::abs( dot( normal, p1 - p2 ) ) < wlimits::DBL_EPS ) // plane and line are parallel
71  {
72  return false;
73  }
74 
75  double const t = ( d - dot( normal, p2 ) ) / ( dot( normal, p1 - p2 ) );
76 
77  *pointOfIntersection = p2 + t * ( p1 - p2 );
78 
79  if( t >= -wlimits::DBL_EPS && t <= ( 1.0 + wlimits::DBL_EPS ) )
80  {
81  return true;
82  }
83  return false;
84 }
85 
86 bool intersectPlaneLineNearCP( const WPlane& p, const WLine& l, boost::shared_ptr< WPosition > cutPoint )
87 {
88  bool result = false;
89  double minDistance = wlimits::MAX_DOUBLE;
90  WAssert( cutPoint.get(), "Place to store a point of intersection is not ready!" );
91  *cutPoint = WPosition( 0, 0, 0 );
92  for( size_t i = 1; i < l.size(); ++i ) // test each segment
93  {
94  boost::shared_ptr< WPosition > cP( new WPosition( 0, 0, 0 ) );
95  if( intersectPlaneSegment( p, l[i-1], l[i], cP ) )
96  {
97  result = true;
98  double dist = length2( *cP - p.getPosition() );
99  if( dist < minDistance )
100  {
101  minDistance = dist;
102  *cutPoint = *cP;
103  }
104  }
105  }
106  return result;
107 }