PLaSK library
Loading...
Searching...
No Matches
translation_container.hpp
Go to the documentation of this file.
1/*
2 * This file is part of PLaSK (https://plask.app) by Photonics Group at TUL
3 * Copyright (c) 2022 Lodz University of Technology
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 3.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 */
14#ifndef PLASK__GEOMETRY_CONTAINER_TRANS_H
15#define PLASK__GEOMETRY_CONTAINER_TRANS_H
16
17#include "container.hpp"
18#include "spatial_index.hpp"
19
20#include <boost/thread.hpp>
21#include <atomic>
22
23namespace plask {
24
29template < int dim >
30struct PLASK_API TranslationContainer: public WithAligners<GeometryObjectContainer<dim>, align::AlignerD<dim>> {
31
32 friend struct Lattice;
33
36
39
42
45
48
49 using GeometryObjectContainer<dim>::children;
51
52 static const char* NAME;
53
55
58
60
61 std::string getTypeName() const override { return NAME; }
62
70 PathHints::Hint addUnsafe(shared_ptr<ChildType> el, ChildAligner aligner);
71
79 PathHints::Hint addUnsafe(shared_ptr<ChildType> el, const DVec& translation = Primitive<dim>::ZERO_VEC);
80
88 PathHints::Hint add(shared_ptr<ChildType> el, ChildAligner aligner) {
89 if (el) this->ensureCanHaveAsChild(*el);
90 return addUnsafe(el, aligner);
91 }
92
100 PathHints::Hint add(shared_ptr<ChildType> el, const DVec& translation = Primitive<dim>::ZERO_VEC) {
101 if (el) this->ensureCanHaveAsChild(*el);
102 return addUnsafe(el, translation);
103 }
104
113 PathHints::Hint insertUnsafe(const std::size_t pos, shared_ptr<ChildType> el, ChildAligner aligner);
114
123 PathHints::Hint insertUnsafe(const std::size_t pos, shared_ptr<ChildType> el, const DVec& translation = Primitive<dim>::ZERO_VEC);
124
133 PathHints::Hint insert(const std::size_t pos, shared_ptr<ChildType> el, ChildAligner aligner) {
134 if (el) this->ensureCanHaveAsChild(*el);
135 return insertUnsafe(pos, el, aligner);
136 }
137
146 PathHints::Hint insert(const std::size_t pos, shared_ptr<ChildType> el, const DVec& translation = Primitive<dim>::ZERO_VEC) {
147 if (el) this->ensureCanHaveAsChild(*el);
148 return insertUnsafe(pos, el, translation);
149 }
150
151 //methods overwrite to use cache:
152 shared_ptr<Material> getMaterial(const DVec& p) const override {
153 return ensureHasCache()->getMaterial(p);
154 }
155
156 bool contains(const DVec& p) const override {
157 return ensureHasCache()->contains(p);
158 }
159
160 GeometryObject::Subtree getPathsAt(const DVec& point, bool all=false) const override {
161 return ensureHasCache()->getPathsAt(this->shared_from_this(), point, all);
162 }
163
164 //some methods must be overwrite to invalidate cache:
166 if (evt.isResize()) invalidateCache();
168 }
169
170 bool removeIfTUnsafe(const std::function<bool(const shared_ptr<TranslationT>& c)>& predicate) override {
171 if (WithAligners<GeometryObjectContainer<dim>, align::AlignerD<dim>>::removeIfTUnsafe(predicate)) {
172 invalidateCache();
173 return true;
174 } else
175 return false;
176 }
177
178 void removeAtUnsafe(std::size_t index) override {
179 invalidateCache();
181 }
182
183 //virtual void writeXMLChildAttr(XMLWriter::Element &dest_xml_child_tag, std::size_t child_index, const AxisNames &axes) const;
184
185 shared_ptr<GeometryObject> shallowCopy() const override;
186
187 shared_ptr<GeometryObject> deepCopy(std::map<const GeometryObject*, shared_ptr<GeometryObject>>& copied) const override;
188
189protected:
190 shared_ptr<GeometryObject> changedVersionForChildren(std::vector<std::pair<shared_ptr<ChildType>, Vec<3, double>>>& children_after_change, Vec<3, double>* recomended_translation) const override;
191
193 void invalidateCache();
194
199 SpatialIndexNode<dim>* ensureHasCache();
200
207 SpatialIndexNode<dim>* ensureHasCache() const;
208
209private:
210 shared_ptr<TranslationT> newTranslation(const shared_ptr<ChildType>& el, ChildAligner aligner);
211
216 std::atomic<SpatialIndexNode<dim>*> cache;
217
219 boost::mutex cache_mutex;
220
221};
222
225
226} // namespace plask
227
228#endif // PLASK__GEOMETRY_CONTAINER_TRANS_H