summaryrefslogtreecommitdiff
path: root/src/io/octree.h
blob: f3d47b1abd6400ed0ece2cf2abe76b0e5cbe0f27 (plain)
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
28
29
30
31
32
33
34
35
36
37
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*  This file is part of the OpenLB library
 *
 *  Copyright (C) 2015 Thomas Henn
 *  E-mail contact: info@openlb.net
 *  The most recent release of OpenLB can be downloaded at
 *  <http://www.openlb.net/>
 *
 *  This program 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 2
 *  of the License, or (at your option) any later version.
 *
 *  This program 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 this program; if not, write to the Free
 *  Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 *  Boston, MA  02110-1301, USA.
 */

/** \file
 * Octree
 */

#ifndef OCTREE_H
#define OCTREE_H

#include <iostream>
#include <set>

#include "core/singleton.h"
#include "core/vector.h"


using namespace olb::util;
/// All OpenLB code is contained in this namespace.
namespace olb {

template<typename T>
class STLmesh;

template<typename T>
struct STLtriangle;

template <typename T>
class Octree {
public:
  /*
   * Constructs Octree containing triangles of an STLmesh.
   * \param center Centerpoint
   * \param rad Radius
   * \param mesh STLmesh
   * \param maxDepth Maximal depth of tree
   * \param overlap Triangles within rad+overlap are added to this Octree
   */
  Octree(Vector<T,3> center, T rad, STLmesh<T>* mesh, short maxDepth, T overlap = 0., Octree<T>* parent=nullptr);
  /// Destructor destructs
  ~Octree();
  /// Find the node containing the first param with remaining maxDepth
  Octree<T>* find(const Vector<T,3>&,const int& maxDepth = 0);
  /// Write Octree
  void write(const Vector<T,3>& pt, const std::string no);
  /// Write Octree
  void write(const int, const std::string);
  /// Write Octree
  void write(const std::string);
  /// Test intersection of ray with all triangles in Octree
  /// returns number of intersections
  int testIntersection(const Vector<T,3>& pt,const Vector<T,3>& dir, bool print = false);
  /// Test intersection of ray with all triangles in Octree
  /// q contains point of closest intersection to pt in direction direction
  bool closestIntersection(const Vector<T,3>& pt, const Vector<T,3>& direction, Vector<T,3>& q, T& a);
  /// Test intersection of ray with all triangles in Octree
  /// q contains point of closest intersection to pt in direction direction
  /// tri contains triangle with closest intersection
  bool closestIntersection(const Vector<T,3>& pt, const Vector<T,3>& direction, Vector<T,3>& q, T& a, STLtriangle<T>& tri, const T& rad = 0., bool print = false);
  /// Test intersection of sphere moving along ray with radius rad
  /// q contains point of closest intersection to pt in direction direction
  /// tri contains triangle with closest intersection
  bool closestIntersectionSphere(const Vector<T,3>& pt, const T& rad, const Vector<T,3>& direction, Vector<T,3>& q, T& a, STLtriangle<T>& tri);
  /// It's complicated. Computes intersections of a ray with triangles inside this Octree. Sets _inside depending on value of rayInside and changes rayInside depending on the number of intersections. Also takes into account if the intersections happen before or after the center.
  void checkRay(const Vector<T,3>& pt,const Vector<T,3>& dir, unsigned short& rayInside);
  /// Computes intersection of ray with Octree boundaries
  void intersectRayNode(const Vector<T,3>& pt, const Vector<T,3>& dir, Vector<T,3>& s);
  /// Computes all centerpoints of Octree
  void getCenterpoints(std::vector<std::vector<T> >& pts);
  /// Collectes all leafs
  void getLeafs(std::vector<Octree<T>* >& pts);
  /// Return status of _isLeaf;
  bool isLeaf();
  /// Sets Inside
  inline void setInside(bool ins)
  {
    _inside = ins;
  };
  /// Gets Inside
  inline bool getInside()
  {
    return _inside;
  };
  /// Gets _boundarNode
  inline bool getBoundaryNode()
  {
    return _boundaryNode;
  };
  /// Gets Maxdepth
  inline int getMaxdepth() const
  {
    return _maxDepth;
  };
  /// Gets numbers of triangles contained by this Octree
  inline const std::vector<unsigned int>& getTriangles() const
  {
    return _triangles;
  };
  /// Gets centerpoint
  inline const Vector<T,3>& getCenter() const
  {
    return _center;
  };
  /// Gets radius
  inline const T getRadius() const
  {
    return _radius;
  };
  /// Prints console output
  void print();
  /// Returns set of indices of all triangles in nodes containing a line.
  void trianglesOnLine(const Vector<T,3>& pt1, const Vector<T,3>& pt2, std::set<unsigned int>& tris);
  /// Returns reference to _mesh
  inline STLmesh<T>* getMesh()
  {
    return _mesh;
  }

protected:
  ///_vector _triangles contains number of triangles
  std::vector<unsigned int> _triangles;
  Vector<T,3> _center;
  T _radius;
  STLmesh<T>* _mesh;
  short _maxDepth;
  bool _isLeaf;
  bool _boundaryNode;
  bool _inside;
  Octree<T> *_parent;
  Octree<T> **_child;
  void findTriangles(T overlap = 0.);
  bool AABBTri(const STLtriangle<T>& tri, T overlap = 0.);
};


}
#endif