PLaSK library
Loading...
Searching...
No Matches
path.cpp
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#include "path.hpp"
15
16#include <plask/config.hpp>
17
18#include "object.hpp"
19
20namespace plask {
21
23 addHint(hint.first, hint.second);
24}
25
28 if (cont_set_it == hintFor.end()) return true; //no entry == no constraint == everything is included
29 return cont_set_it->second.find(const_pointer_cast<GeometryObject>(child_tran)) != cont_set_it->second.end();
30}
31
32void PathHints::addHint(weak_ptr<GeometryObject> container, weak_ptr<GeometryObject> child) {
33 hintFor[container].insert(child);
34}
35
42
46
48 if (subtree.object->isContainer()) {
49 for (auto& c: subtree.children)
51 }
52 for (auto& c: subtree.children)
54}
55
56std::set<shared_ptr<GeometryObject>> PathHints::getChildren(shared_ptr<const GeometryObject> container) {
57 std::set<shared_ptr<GeometryObject>> result;
58 auto e = hintFor.find(const_pointer_cast<GeometryObject>(container));
59 if (e == hintFor.end()) return result;
60 if (e->first.expired()) { // container was deleted, new container is under same address as was old one
61 hintFor.erase(e);
62 return result;
63 }
64 for (auto weak_child_iter = e->second.begin(); weak_child_iter != e->second.end(); ) {
66 if (!child) // child was deleted
67 e->second.erase(weak_child_iter++);
68 else {
69 result.insert(child);
71 }
72 }
73 if (e->second.empty()) hintFor.erase(e); // we remove all constraints
74 return result;
75}
76
77std::set<shared_ptr<GeometryObject>> PathHints::getChildren(shared_ptr<const GeometryObject> container) const {
78 std::set<shared_ptr<GeometryObject>> result;
79 auto e = hintFor.find(const_pointer_cast<GeometryObject>(container));
80 if (e == hintFor.end() || e->first.expired()) return result;
81 for (auto child_weak: e->second) {
83 if (child) result.insert(child);
84 }
85 return result;
86}
87
89 for(auto i = hintFor.begin(); i != hintFor.end(); )
90 if (i->first.expired())
91 hintFor.erase(i++);
92 else {
93 for (auto weak_child_iter = i->second.begin(); weak_child_iter != i->second.end(); ) {
94 if (weak_child_iter->expired())
95 i->second.erase(weak_child_iter);
96 else
98 }
99 if (i->second.empty())
100 hintFor.erase(i++);
101 else
102 ++i;
103 }
104}
105
106//----------------- Path ------------------------------------------
107
108bool Path::completeToFirst(const GeometryObject& newFirst, const PathHints* hints) {
109 GeometryObject::Subtree path = newFirst.getPathsTo(*objects.front(), hints);
110 if (path.empty()) return false;
112 return true;
113}
114
115bool Path::completeFromLast(const GeometryObject& newLast, const PathHints* hints) {
116 GeometryObject::Subtree path = objects.back()->getPathsTo(newLast, hints);
117 if (path.empty()) return false;
118 push_back(path.toLinearPath().objects);
119 return true;
120}
121
123 if (toAdd.empty()) return;
124 if (objects.empty()) {
125 objects = toAdd;
126 } else {
127 if (toAdd.back() == objects.front()) //last to add is already first on list?
128 objects.insert(objects.begin(), toAdd.begin(), toAdd.end()-1);
129 else
130 objects.insert(objects.begin(), toAdd.begin(), toAdd.end());
131 }
132}
133
135 if (toAdd.empty()) return;
136 if (objects.empty()) {
137 objects = toAdd;
138 } else {
139 if (toAdd.front() == objects.back()) //first to add is already as last on list?
140 objects.insert(objects.end(), toAdd.begin()+1, toAdd.end());
141 else
142 objects.insert(objects.end(), toAdd.begin(), toAdd.end());
143 }
144}
145
147 if (path.empty()) return *this;
148 if (objects.empty())
149 objects = path;
150 else {
151 if (completeToFirst(*path.back(), hints)) {
152 push_front(path);
153 } else
154 if (completeFromLast(*path.front(), hints)) {
155 push_back(path);
156 } else
157 throw Exception("cannot connect paths.");
158 }
159 return *this;
160}
161
163 return append(path.toLinearPath(), hints);
164}
165
166Path& Path::append(const Path& path, const PathHints* hints) {
167 return append(path.objects, hints);
168}
169
171 return append(std::vector< shared_ptr<const GeometryObject> > { hint.first, hint.second }, hints);
172}
173
175 return append( std::vector< shared_ptr<const GeometryObject> > { object.shared_from_this() }, hints);
176}
177
179 return append( std::vector< shared_ptr<const GeometryObject> > { object }, hints);
180}
181
187
188} //namespace plask