|
| 1 | +/******************************************************************************** |
| 2 | +* * |
| 3 | +* This file is part of IfcOpenShell. * |
| 4 | +* * |
| 5 | +* IfcOpenShell is free software: you can redistribute it and/or modify * |
| 6 | +* it under the terms of the Lesser GNU General Public License as published by * |
| 7 | +* the Free Software Foundation, either version 3.0 of the License, or * |
| 8 | +* (at your option) any later version. * |
| 9 | +* * |
| 10 | +* IfcOpenShell is distributed in the hope that it will be useful, * |
| 11 | +* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 12 | +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 13 | +* Lesser GNU General Public License for more details. * |
| 14 | +* * |
| 15 | +* You should have received a copy of the Lesser GNU General Public License * |
| 16 | +* along with this program. If not, see <http://www.gnu.org/licenses/>. * |
| 17 | +* * |
| 18 | +********************************************************************************/ |
| 19 | + |
| 20 | +#ifndef IFCGEOMTREE_H |
| 21 | +#define IFCGEOMTREE_H |
| 22 | + |
| 23 | +#include "../ifcparse/IfcFile.h" |
| 24 | +#include "../ifcgeom/IfcGeomIterator.h" |
| 25 | + |
| 26 | +#include <NCollection_UBTree.hxx> |
| 27 | +#include <BRepBndLib.hxx> |
| 28 | +#include <Bnd_Box.hxx> |
| 29 | +#include <BRepAlgoAPI_Common.hxx> |
| 30 | +#include <BRepAlgoAPI_Cut.hxx> |
| 31 | +#include <BRepClass3d_SolidClassifier.hxx> |
| 32 | + |
| 33 | +namespace IfcGeom { |
| 34 | + |
| 35 | + namespace impl { |
| 36 | + template <typename T> |
| 37 | + class tree { |
| 38 | + |
| 39 | + public: |
| 40 | + |
| 41 | + void add(const T& t, const Bnd_Box& b) { |
| 42 | + tree_.Add(t, b); |
| 43 | + } |
| 44 | + |
| 45 | + void add(const T& t, const TopoDS_Shape& s) { |
| 46 | + Bnd_Box b; |
| 47 | + BRepBndLib::AddClose(s, b); |
| 48 | + add(t, b); |
| 49 | + shapes_[t] = s; |
| 50 | + } |
| 51 | + |
| 52 | + std::vector<T> select_box(const T& t, bool completely_within = false, double extend=-1.e-5) const { |
| 53 | + typename map_t::const_iterator it = shapes_.find(t); |
| 54 | + if (it == shapes_.end()) { |
| 55 | + return std::vector<T>(); |
| 56 | + } |
| 57 | + |
| 58 | + Bnd_Box b; |
| 59 | + BRepBndLib::AddClose(it->second, b); |
| 60 | + |
| 61 | + // Gap is assumed to be positive throughout the codebase, |
| 62 | + // but at least for IsOut() in the selector a negative |
| 63 | + // Gap should work as well. |
| 64 | + b.SetGap(b.GetGap() + extend); |
| 65 | + |
| 66 | + return select_box(b, completely_within); |
| 67 | + } |
| 68 | + |
| 69 | + std::vector<T> select_box(const gp_Pnt& p) const { |
| 70 | + Bnd_Box b; |
| 71 | + b.Add(p); |
| 72 | + return select_box(b); |
| 73 | + } |
| 74 | + |
| 75 | + std::vector<T> select_box(const Bnd_Box& b, bool completely_within = false) const { |
| 76 | + selector s(b); |
| 77 | + tree_.Select(s); |
| 78 | + if (completely_within) { |
| 79 | + std::vector<T> ts = s.results(); |
| 80 | + std::vector<T> ts_filtered; |
| 81 | + ts_filtered.reserve(ts.size()); |
| 82 | + typename std::vector<T>::const_iterator it = ts.begin(); |
| 83 | + for (; it != ts.end(); ++it) { |
| 84 | + const TopoDS_Shape& shp = shapes_.find(*it)->second; |
| 85 | + Bnd_Box B; |
| 86 | + BRepBndLib::AddClose(shp, B); |
| 87 | + |
| 88 | + // BndBox::CornerMin() /-Max() introduced in OCCT 6.8 |
| 89 | + double x1, y1, z1, x2, y2, z2; |
| 90 | + b.Get(x1, y1, z1, x2, y2, z2); |
| 91 | + double gap = B.GetGap(); |
| 92 | + gp_Pnt p1(x1 - gap, y1 - gap, z1 - gap); |
| 93 | + gp_Pnt p2(x2 + gap, y2 + gap, z2 + gap); |
| 94 | + |
| 95 | + if (!b.IsOut(p1) && !b.IsOut(p2)) { |
| 96 | + ts_filtered.push_back(*it); |
| 97 | + } |
| 98 | + } |
| 99 | + return ts_filtered; |
| 100 | + } else { |
| 101 | + return s.results(); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + std::vector<T> select(const T& t, bool completely_within = false) const { |
| 106 | + std::vector<T> ts = select_box(t); |
| 107 | + if (ts.empty()) { |
| 108 | + return ts; |
| 109 | + } |
| 110 | + |
| 111 | + std::vector<T> ts_filtered; |
| 112 | + |
| 113 | + const TopoDS_Shape& A = shapes_.find(t)->second; |
| 114 | + if (IfcGeom::Kernel::count(A, TopAbs_SHELL) == 0) { |
| 115 | + return ts_filtered; |
| 116 | + } |
| 117 | + |
| 118 | + ts_filtered.reserve(ts.size()); |
| 119 | + |
| 120 | + typename std::vector<T>::const_iterator it = ts.begin(); |
| 121 | + for (it = ts.begin(); it != ts.end(); ++it) { |
| 122 | + const TopoDS_Shape& B = shapes_.find(*it)->second; |
| 123 | + if (IfcGeom::Kernel::count(B, TopAbs_SHELL) == 0) { |
| 124 | + continue; |
| 125 | + } |
| 126 | + |
| 127 | + if (completely_within) { |
| 128 | + BRepAlgoAPI_Cut cut(B, A); |
| 129 | + if (cut.IsDone()) { |
| 130 | + if (IfcGeom::Kernel::count(cut.Shape(), TopAbs_SHELL) == 0) { |
| 131 | + ts_filtered.push_back(*it); |
| 132 | + } |
| 133 | + } |
| 134 | + } else { |
| 135 | + BRepAlgoAPI_Common common(A, B); |
| 136 | + if (common.IsDone()) { |
| 137 | + if (IfcGeom::Kernel::count(common.Shape(), TopAbs_SHELL) > 0) { |
| 138 | + ts_filtered.push_back(*it); |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + return ts_filtered; |
| 145 | + } |
| 146 | + |
| 147 | + std::vector<T> select(const TopoDS_Shape& s) const { |
| 148 | + Bnd_Box bb; |
| 149 | + BRepBndLib::AddClose(s, bb); |
| 150 | + |
| 151 | + std::vector<T> ts; |
| 152 | + |
| 153 | + if (IfcGeom::Kernel::count(s, TopAbs_SHELL) == 0) { |
| 154 | + return ts; |
| 155 | + } |
| 156 | + |
| 157 | + ts = select_box(bb); |
| 158 | + |
| 159 | + if (ts.empty()) { |
| 160 | + return ts; |
| 161 | + } |
| 162 | + |
| 163 | + std::vector<T> ts_filtered; |
| 164 | + ts_filtered.reserve(ts.size()); |
| 165 | + |
| 166 | + typename std::vector<T>::const_iterator it = ts.begin(); |
| 167 | + for (it = ts.begin(); it != ts.end(); ++it) { |
| 168 | + const TopoDS_Shape& B = shapes_.find(*it)->second; |
| 169 | + |
| 170 | + if (IfcGeom::Kernel::count(B, TopAbs_SHELL) == 0) { |
| 171 | + continue; |
| 172 | + } |
| 173 | + |
| 174 | + BRepAlgoAPI_Common common(s, B); |
| 175 | + if (common.IsDone()) { |
| 176 | + if (IfcGeom::Kernel::count(common.Shape(), TopAbs_SHELL) > 0) { |
| 177 | + ts_filtered.push_back(*it); |
| 178 | + } |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + return ts_filtered; |
| 183 | + } |
| 184 | + |
| 185 | + std::vector<T> select(const gp_Pnt& p) const { |
| 186 | + std::vector<T> ts = select_box(p); |
| 187 | + if (ts.empty()) { |
| 188 | + return ts; |
| 189 | + } |
| 190 | + |
| 191 | + std::vector<T> ts_filtered; |
| 192 | + ts_filtered.reserve(ts.size()); |
| 193 | + |
| 194 | + typename std::vector<T>::const_iterator it = ts.begin(); |
| 195 | + for (it = ts.begin(); it != ts.end(); ++it) { |
| 196 | + const TopoDS_Shape& B = shapes_.find(*it)->second; |
| 197 | + TopExp_Explorer exp(B, TopAbs_SOLID); |
| 198 | + for (; exp.More(); exp.Next()) { |
| 199 | + BRepClass3d_SolidClassifier cls(exp.Current(), p, 1e-5); |
| 200 | + if (cls.State() != TopAbs_OUT) { |
| 201 | + ts_filtered.push_back(*it); |
| 202 | + break; |
| 203 | + } |
| 204 | + } |
| 205 | + } |
| 206 | + |
| 207 | + return ts_filtered; |
| 208 | + } |
| 209 | + |
| 210 | + protected: |
| 211 | + |
| 212 | + typedef NCollection_UBTree<T, Bnd_Box> tree_t; |
| 213 | + typedef std::map<T, TopoDS_Shape> map_t; |
| 214 | + tree_t tree_; |
| 215 | + map_t shapes_; |
| 216 | + |
| 217 | + class selector : public tree_t::Selector |
| 218 | + { |
| 219 | + public: |
| 220 | + selector(const Bnd_Box& b) |
| 221 | + : tree_t::Selector() |
| 222 | + , bounds_(b) |
| 223 | + {} |
| 224 | + |
| 225 | + Standard_Boolean Reject(const Bnd_Box& b) const { |
| 226 | + return bounds_.IsOut(b); |
| 227 | + } |
| 228 | + |
| 229 | + Standard_Boolean Accept(const T& o) { |
| 230 | + results_.push_back(o); |
| 231 | + return Standard_True; |
| 232 | + } |
| 233 | + |
| 234 | + const std::vector<T>& results() const { |
| 235 | + return results_; |
| 236 | + } |
| 237 | + |
| 238 | + private: |
| 239 | + std::vector<T> results_; |
| 240 | + const Bnd_Box& bounds_; |
| 241 | + }; |
| 242 | + |
| 243 | + }; |
| 244 | + } |
| 245 | + |
| 246 | + class tree : public impl::tree<IfcSchema::IfcProduct*> { |
| 247 | + public: |
| 248 | + |
| 249 | + tree() {}; |
| 250 | + |
| 251 | + tree(IfcParse::IfcFile& f) { |
| 252 | + add_file(f, IfcGeom::IteratorSettings()); |
| 253 | + } |
| 254 | + |
| 255 | + tree(IfcParse::IfcFile& f, const IfcGeom::IteratorSettings& settings) { |
| 256 | + add_file(f, settings); |
| 257 | + } |
| 258 | + |
| 259 | + void add_file(IfcParse::IfcFile& f, const IfcGeom::IteratorSettings& settings) { |
| 260 | + IfcGeom::IteratorSettings settings_ = settings; |
| 261 | + settings_.set(IfcGeom::IteratorSettings::DISABLE_TRIANGULATION, true); |
| 262 | + settings_.set(IfcGeom::IteratorSettings::USE_WORLD_COORDS, true); |
| 263 | + settings_.set(IfcGeom::IteratorSettings::SEW_SHELLS, true); |
| 264 | + |
| 265 | + IfcGeom::Iterator<double> it(settings_, &f); |
| 266 | + |
| 267 | + if (it.initialize()) { |
| 268 | + do { |
| 269 | + IfcGeom::BRepElement<double>* elem = (IfcGeom::BRepElement<double>*)it.get(); |
| 270 | + add((IfcSchema::IfcProduct*)f.entityById(elem->id()), elem->geometry().as_compound()); |
| 271 | + } while (it.next()); |
| 272 | + } |
| 273 | + } |
| 274 | + }; |
| 275 | + |
| 276 | +} |
| 277 | + |
| 278 | +#endif |
0 commit comments