DistanceOctree.h 9.04 KB
Newer Older
Christian Godenschwager's avatar
Christian Godenschwager committed
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
//======================================================================================================================
//
//  This file is part of waLBerla. waLBerla 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.
//  
//  waLBerla 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 waLBerla (see COPYING.txt). If not, see <http://www.gnu.org/licenses/>.
//
//! \file DistanceOctree.h
//! \ingroup mesh
//! \author Christian Godenschwager <christian.godenschwager@fau.de>
//
//======================================================================================================================

#pragma once

#include "Node.h"
#include "LeafNode.h"
#include "BranchNode.h"

28
#include "mesh_common/DistanceComputations.h"
Christian Godenschwager's avatar
Christian Godenschwager committed
29
30
31
32
33
34
35

#include "core/DataTypes.h"
#include "core/Abort.h"

#include "core/math/GenericAABB.h"
#include "core/math/Vector3.h"

36
37
#include "core/typeToString.h"

Christian Godenschwager's avatar
Christian Godenschwager committed
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

#include <vector>
#include <queue>

namespace walberla {
namespace mesh {
namespace distance_octree {


template <typename MeshType>
class DistanceOctree
{
public:
   typedef typename MeshType::Point                    Point;
   typedef typename MeshType::Normal                   Normal;
   typedef typename MeshType::Scalar                   Scalar;  
   typedef typename MeshType::FaceHandle               FaceHandle; 
   typedef typename math::GenericAABB<Scalar> AABB;

   DistanceOctree( const shared_ptr< TriangleDistance<MeshType> > & triDist, uint_t maxDepth = 20u, uint_t minNumTriangles = 25u )
   {
      if( triDist->getMesh().faces_empty() )
         WALBERLA_ABORT( "You cannot build a distance octree on a mesh without triangles!");

      if( maxDepth == 0 || triDist->getMesh().n_faces() < minNumTriangles )
         rootNode_ = walberla::make_shared<const LeafNode<MeshType> >( triDist, std::vector<FaceHandle>( triDist->getMesh().faces_begin(), triDist->getMesh().faces_end() ) );
      else
         rootNode_ = walberla::make_shared<const BranchNode<MeshType> >( triDist, triDist->getMesh().faces_begin(), triDist->getMesh().faces_end(), maxDepth - 1, minNumTriangles );
   }

   Scalar sqSignedDistance( const Point & p ) const
   {
      WALBERLA_ASSERT_NOT_NULLPTR( rootNode_ );
      return rootNode_->sqSignedDistance( p );
   }

   Scalar sqSignedDistance( const Point & p, FaceHandle & closestTriangle ) const
   {
      return rootNode_->sqSignedDistance( p, closestTriangle );
   }

   Scalar sqSignedDistance( const Point & p, Point & closestPoint ) const
   {
      return rootNode_->sqSignedDistance( p, closestPoint );
   }

   Scalar sqSignedDistance( const Point & p, Point & closestPoint, Normal & normal ) const
   {
      return rootNode_->sqSignedDistance( p, closestPoint, normal );
   }


   Scalar sqDistance( const Point & p ) const
   {
      WALBERLA_ASSERT_NOT_NULLPTR( rootNode_ );
      return rootNode_->sqDistance( p );
   }

   Scalar sqDistance( const Point & p, FaceHandle & closestTriangle ) const
   {
      return rootNode_->sqDistance( p, closestTriangle );
   }

   Scalar sqDistance( const Point & p, Point & closestPoint ) const
   {
      return rootNode_->sqDistance( p, closestPoint );
   }

   Scalar sqDistance( const Point & p, Point & closestPoint, Normal & normal ) const
   {
      return rootNode_->sqDistance( p, closestPoint, normal );
   }


   uint_t numTriangles() const { return rootNode_->numTriangles(); }

   void numTrianglesToStream( std::ostream & os ) { rootNode_->numTrianglesToStream(os, 0); }

   uint_t height() const { return rootNode_->height(); }

   const AABB & getAABB() const { return rootNode_->getAABB(); }

   static inline Point             toPoint( const Vector3<Scalar> & p ) { return toOpenMesh( p ); }
   static inline Vector3<Scalar> fromPoint( const Point & p )           { return toWalberla( p ); }

   static inline Vector3<Scalar> fromNormal( const Normal & p ) { return toWalberla( p ); }

   static inline Scalar   toScalar( const real_t & x ) { return numeric_cast<Scalar>( x ); }
   static inline real_t fromScalar( const Scalar & x ) { return numeric_cast<real_t>( x ); }

   void writeVTKOutput( const std::string & filestem ) const;

protected:
   shared_ptr< const Node<MeshType> > rootNode_;
};



136
137
138
139
140
141
142
143
144
145
146
147
148
149
//**********************************************************************************************************************
/*! \brief Write the distance octree to a VTK file.
 * 
 * This method should only be called by the root process:
 * \code
     WALBERLA_ROOT_SECTION()
     {
        distanceOctree->writeVTKOutput("distanceOctree");
     }
 * \endcode
 *
 * \param filestem name of the VTK file without extension
 */
//**********************************************************************************************************************
Christian Godenschwager's avatar
Christian Godenschwager committed
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
template <typename MeshType>
void DistanceOctree<MeshType>::writeVTKOutput( const std::string & filestem ) const
{
   std::ostringstream oss;
   oss << filestem << ".vtk";

   std::ofstream outfile( oss.str().c_str() );

   outfile << "# vtk DataFile Version 3.0\n"
           << "DistanceOctree\n"
           << "ASCII\n\n"
           << "DATASET UNSTRUCTURED_GRID\n\n";

   uint_t numNodes = 0;

   std::queue<const Node<MeshType> *> nodeQueue;
   nodeQueue.push( rootNode_.get() );
   while( !nodeQueue.empty() )
   {
      const Node<MeshType> * frontNode = nodeQueue.front();
      nodeQueue.pop();

      if( frontNode->numTriangles() == 0 )
         continue;

      ++numNodes;

      for( uint_t i = 0; i < frontNode->numChildren(); ++i )
         nodeQueue.push( frontNode->getChild( i ) );
   }

   outfile << "POINTS " << ( 8 * numNodes ) << " " << typeToString<real_t>() << "\n\n";


   nodeQueue.push( rootNode_.get() );
   while( !nodeQueue.empty() )
   {
      const Node<MeshType> * frontNode = nodeQueue.front();
      nodeQueue.pop();

      if( frontNode->numTriangles() == 0 )
         continue;

      const auto aabb = frontNode->getAABB();

      for( uint_t z = 0; z != 2; ++z ) {
         for( uint_t y = 0; y != 2; ++y ) {
            for( uint_t x = 0; x != 2; ++x ) {
               outfile << ( ( x == 0 ) ? aabb.xMin() : aabb.xMax() ) << " "
                       << ( ( y == 0 ) ? aabb.yMin() : aabb.yMax() ) << " "
                       << ( ( z == 0 ) ? aabb.zMin() : aabb.zMax() ) << "\n";
            }
         }
      }

      for( uint_t i = 0; i < frontNode->numChildren(); ++i )
         nodeQueue.push( frontNode->getChild( i ) );
   }

   outfile << "\n\nCELLS " << numNodes << " " << ( 9 * numNodes ) << "\n\n";

   for( uint_t i = 0, c = 0; i != numNodes; ++i ) {

      outfile << "8";

      for( uint_t j = 0; j != 8; ++j, ++c )
         outfile << " " << c;

      outfile << std::endl;
   }

   outfile << "\n\nCELL_TYPES " << numNodes << "\n\n";

   for( uint_t i = 0; i != numNodes; ++i )
      outfile << "11\n";

   outfile << "\n\nCELL_DATA " << numNodes;

   outfile << "\n\nSCALARS numTriangles unsigned_int 1"
           << "\nLOOKUP_TABLE default\n";

   nodeQueue.push( rootNode_.get() );
   while( !nodeQueue.empty() )
   {
      const Node<MeshType> * frontNode = nodeQueue.front();
      nodeQueue.pop();

      if( frontNode->numTriangles() == 0 )
         continue;

      outfile  << uint32_c( frontNode->numTriangles() ) << "\n";

      for( uint_t i = 0; i < frontNode->numChildren(); ++i )
         nodeQueue.push( frontNode->getChild( i ) );
   }

   outfile << "\n\nSCALARS height unsigned_char 1"
           << "\nLOOKUP_TABLE default\n";
   
   nodeQueue.push( rootNode_.get() );
   while( !nodeQueue.empty() )
   {
      const Node<MeshType> * frontNode = nodeQueue.front();
      nodeQueue.pop();

      if( frontNode->numTriangles() == 0 )
         continue;
      
      WALBERLA_ASSERT_LESS_EQUAL( frontNode->height(), std::numeric_limits<uint8_t>::max() );

      outfile  << uint16_c( frontNode->height() ) << "\n";

      for( uint_t i = 0; i < frontNode->numChildren(); ++i )
         nodeQueue.push( frontNode->getChild( i ) );
   }

   outfile << "\n\nSCALARS depth unsigned_char 1"
           << "\nLOOKUP_TABLE default\n";

   std::queue<uint8_t> depthQueue;
   nodeQueue.push( rootNode_.get() );
   depthQueue.push( 0 );
   while( !nodeQueue.empty() )
   {
      const Node<MeshType> * frontNode = nodeQueue.front();
      uint8_t depth = depthQueue.front();
      nodeQueue.pop();
      depthQueue.pop();

      if( frontNode->numTriangles() == 0 )
         continue;

      outfile  << uint16_c( depth ) << "\n";

      for( uint_t i = 0; i < frontNode->numChildren(); ++i )
      {
         nodeQueue.push( frontNode->getChild( i ) );
         depthQueue.push( uint8_c( depth + 1 ) );
      }
   }

   outfile << std::endl;
   outfile.close();
}

} //namespace distance_octree

using distance_octree::DistanceOctree;

} // namespace mesh
} // namespace walberla