OpenWalnut  1.4.0
WPlane.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 <set>
26 
27 #include <boost/shared_ptr.hpp>
28 
29 #include "../../common/math/WLinearAlgebraFunctions.h"
30 #include "../../common/math/linearAlgebra/WVectorFixed.h"
31 #include "../../common/WAssert.h"
32 #include "../../common/WLimits.h"
33 
34 #include "WPlane.h"
35 
36 WPlane::WPlane( const WVector3d& normal, const WPosition& pos )
37  : m_normal( normal ),
38  m_pos( pos )
39 {
40  setNormal( normal );
41 }
42 
43 WPlane::WPlane( const WVector3d& normal, const WPosition& pos, const WVector3d& first, const WVector3d& second )
44  : m_normal( normal ),
45  m_pos( pos )
46 {
47  setPlaneVectors( first, second );
48  m_first = normalize( m_first );
49  m_second = normalize( m_second );
50 }
51 
53 {
54 }
55 
56 bool WPlane::isInPlane( WPosition /* point */ ) const
57 {
58  // TODO(math): implement this: pos + first*m + second*n == point ??
59  return false;
60 }
61 
63 {
64  // TODO(math): check if pos is in plane first!
65  m_pos = newPos;
66 }
67 
68 
69 boost::shared_ptr< std::set< WPosition > > WPlane::samplePoints( double stepWidth, size_t numX, size_t numY ) const
70 {
71  // idea: start from m_pos in m_first direction until boundary reached, increment in m_second direction from m_pos and start again
72  boost::shared_ptr< std::set< WPosition > > result( new std::set< WPosition >() );
73 
74  // the plane has two directions m_first and m_second
75  const WVector3d ycrement = stepWidth * m_second;
76  const WVector3d xcrement = stepWidth * m_first;
77  result->insert( m_pos );
78  for( size_t i = 0; i < numY; ++i )
79  {
80  for( size_t j = 0; j < numX; ++j )
81  {
82  // NOTE: on some machines with older compilers, the size_t is not mapped to uint32_t or uint64_t. This
83  // breaks our WMatrixFixed type promotion. So we use doubles directly.
84  double id = i;
85  double jd = j;
86  result->insert( m_pos - id * ycrement - jd * xcrement );
87  result->insert( m_pos + id * ycrement - jd * xcrement );
88  result->insert( m_pos - id * ycrement + jd * xcrement );
89  result->insert( m_pos + id * ycrement + jd * xcrement );
90  }
91  }
92  return result;
93 }
94 
95 // boost::shared_ptr< std::set< WPosition > > WPlane::samplePoints( const WGridRegular3D& grid, double stepWidth )
96 // {
97 // // idea: start from m_pos in m_first direction until boundary reached, increment in m_second direction from m_pos and start again
98 // boost::shared_ptr< std::set< WPosition > > result( new std::set< WPosition >() );
99 //
100 // // the plane has two directions m_first and m_second
101 // const WVector3d ycrement = stepWidth * m_second;
102 // const WVector3d xcrement = stepWidth * m_first;
103 // WPosition y_offset_up = m_pos;
104 // WPosition y_offset_down = m_pos;
105 // WPosition x_offset_right = m_pos;
106 // WPosition x_offset_left = m_pos;
107 // // TODO(math): assert( grid.encloses( m_pos ) );
108 // while( grid.encloses( y_offset_up ) || grid.encloses( y_offset_down ) )
109 // {
110 // if( grid.encloses( y_offset_up ) ) // walk up
111 // {
112 // x_offset_right = y_offset_up;
113 // x_offset_left = y_offset_up;
114 // while( grid.encloses( x_offset_right ) || grid.encloses( x_offset_left ) )
115 // {
116 // if( grid.encloses( x_offset_right ) )
117 // {
118 // result->insert( x_offset_right );
119 // x_offset_right += xcrement;
120 // }
121 // if( grid.encloses( x_offset_left ) )
122 // {
123 // result->insert( x_offset_left );
124 // x_offset_left -= xcrement;
125 // }
126 // }
127 // y_offset_up += ycrement;
128 // }
129 // if( grid.encloses( y_offset_down ) ) // walk down
130 // {
131 // x_offset_right = y_offset_down;
132 // x_offset_left = y_offset_down;
133 // while( grid.encloses( x_offset_right ) || grid.encloses( x_offset_left ) )
134 // {
135 // if( grid.encloses( x_offset_right ) )
136 // {
137 // result->insert( x_offset_right );
138 // x_offset_right += xcrement;
139 // }
140 // if( grid.encloses( x_offset_left ) )
141 // {
142 // result->insert( x_offset_left );
143 // x_offset_left -= xcrement;
144 // }
145 // }
146 // y_offset_down -= ycrement;
147 // }
148 // }
149 //
150 // return result;
151 // }
152 
153 WPosition WPlane::getPointInPlane( double x, double y ) const
154 {
155  WVector3d sd= m_pos +
156  x * m_first
157  +
158  y * m_second;
159  return sd;
160 }
161 
162 void WPlane::setPlaneVectors( const WVector3d& first, const WVector3d& second )
163 {
164  std::stringstream msg;
165  msg << "The give two vectors are not perpendicular to plane. First: " << first << " second: " << second << " for plane normal: " << m_normal;
166  WAssert( std::abs( dot( first, m_normal ) ) < wlimits::FLT_EPS && std::abs( dot( second, m_normal ) ) < wlimits::FLT_EPS, msg.str() );
167 
168  std::stringstream msg2;
169  msg2 << "The given two vectors are not linear independent!: " << first << " and " << second;
170  WAssert( linearIndependent( first, second ), msg2.str() );
171 
172  m_first = first;
173  m_second = second;
174 }
bool isInPlane(WPosition point) const
Determines whether a given point is in this plane or not.
Definition: WPlane.cpp:56
WPosition m_pos
Position of the plane specifying the center.
Definition: WPlane.h:176
WPlane(const WVector3d &normal, const WPosition &pos)
Constructs a plane with its normal and containing the point.
Definition: WPlane.cpp:36
void setPlaneVectors(const WVector3d &first, const WVector3d &second)
Resets the vector spanning the plane.
Definition: WPlane.cpp:162
void setNormal(const WVector3d &normal)
Resets the normal of this plane.
Definition: WPlane.h:137
WVector3d m_first
First vector in the plane.
Definition: WPlane.h:177
This only is a 3d double vector.
void resetPosition(WPosition newPos)
Reset the position of the plane, normal remains the same.
Definition: WPlane.cpp:62
virtual ~WPlane()
Destructor.
Definition: WPlane.cpp:52
const float FLT_EPS
Smallest float such: 1.0 + FLT_EPS == 1.0 is still true.
Definition: WLimits.cpp:37
WPosition getPointInPlane(double x, double y) const
Computes with relative coordinates a point in this plane.
Definition: WPlane.cpp:153
WVector3d m_second
Second vector in the plane.
Definition: WPlane.h:178
WVector3d m_normal
Direction of the plane.
Definition: WPlane.h:175
boost::shared_ptr< std::set< WPosition > > samplePoints(const WGridRegular3D &grid, double stepWidth)
Computes sample points on that plane.