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
23 namespace plask {
24
29 template < int dim >
30 struct PLASK_API TranslationContainer: public WithAligners<GeometryObjectContainer<dim>, align::AlignerD<dim>> {
31
32 friend struct Lattice;
33
35 typedef typename GeometryObjectContainer<dim>::DVec DVec;
36
38 typedef typename GeometryObjectContainer<dim>::Box Box;
39
41 typedef typename GeometryObjectContainer<dim>::ChildType ChildType;
42
44 typedef align::AlignerD<dim> ChildAligner;
45
47 typedef typename GeometryObjectContainer<dim>::TranslationT TranslationT;
48
49 using GeometryObjectContainer<dim>::children;
50 using GeometryObjectContainer<dim>::shared_from_this;
51
52 static const char* NAME;
53
54 TranslationContainer(): cache(nullptr) {}
55
56 TranslationContainer(const TranslationContainer& to_copy)
57 : WithAligners<GeometryObjectContainer<dim>, align::AlignerD<dim>>(to_copy) {}
58
59 ~TranslationContainer();
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:
165 void onChildChanged(const GeometryObject::Event& evt) override {
166 if (evt.isResize()) invalidateCache();
167 WithAligners<GeometryObjectContainer<dim>, align::AlignerD<dim>>::onChildChanged(evt);
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();
180 WithAligners<GeometryObjectContainer<dim>, align::AlignerD<dim>>::removeAtUnsafe(index);
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
189 protected:
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
209 private:
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
223 PLASK_API_EXTERN_TEMPLATE_STRUCT(TranslationContainer<2>)
224 PLASK_API_EXTERN_TEMPLATE_STRUCT(TranslationContainer<3>)
225
226} // namespace plask
227
228#endif // PLASK__GEOMETRY_CONTAINER_TRANS_H