BlinkenArea - GitList
Repositories
Blog
Wiki
libetherpix
Code
Commits
Branches
Tags
Search
Tree:
4ef7955
Branches
Tags
master
libetherpix
config_gen
src
layer.cpp
update copyright year
Stefan Schuermans
commited
4ef7955
at 2017-05-20 16:55:59
layer.cpp
Blame
History
Raw
/* * EtherPix config file generator * * Copyright 2010-2017 Stefan Schuermans <stefan schuermans info> * * 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, version 3 of the License. * * * 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 Lesser General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ #include <vector> #include "box.h" #include "layer.h" #include "line.h" #include "object.h" #include "point.h" Layer::~Layer() { std::vector<Object *>::const_iterator itObject; for (itObject = mObjects.begin(); itObject != mObjects.end(); ++itObject) delete *itObject; } void Layer::getBounds(Box &bounds) const { if (mObjects.empty()) return; (*mObjects.begin())->getBounds(bounds); Box bnds; std::vector<Object *>::const_iterator itObj; for (itObj = mObjects.begin() + 1; itObj != mObjects.end(); ++itObj) { (*itObj)->getBounds(bnds); bounds.include(bnds); } } bool Layer::isIntersecting(const Object *pObject) const { std::vector<Object *>::const_iterator itObject; for (itObject = mObjects.begin(); itObject != mObjects.end(); ++itObject) if ((*itObject)->isIntersecting(pObject)) return true; return false; } bool Layer::getIntersection(const Object *pObject, Point *pIntersect) const { std::vector<Object *>::const_iterator itObject; for (itObject = mObjects.begin(); itObject != mObjects.end(); ++itObject) if ((*itObject)->getIntersection(pObject, pIntersect)) return true; return false; } void Layer::getIntersections(const Object *pObject, std::vector<Intersection> &intersections) const { intersections.clear(); std::vector<Object *>::const_iterator itObject; for (itObject = mObjects.begin(); itObject != mObjects.end(); ++itObject) { Intersection intersect; if ((*itObject)->getIntersection(pObject, &intersect.pt)) { intersect.pObj = *itObject; intersections.push_back(intersect); } } } void Layer::add(Object *pObject) { mObjects.push_back(pObject); } void Layer::build(const Line *pLine) { // find intersecting objects std::vector<unsigned int> intersect; for (unsigned int i = 0; i < mObjects.size(); ++i) if (mObjects[i]->isIntersecting(pLine)) intersect.push_back(i); // new object if (intersect.size() == 0) { // create new object Object *pNewObject = new Object; pNewObject->add(pLine); mObjects.push_back(pNewObject); } // new part of one existing object else if (intersect.size() == 1) { // add to existing object mObjects[intersect[0]]->add(pLine); } // connecting multiple objects else { // merge existing objects Object *pNewObject = new Object; pNewObject->add(pLine); // merge with existing intersecting objects // (go in reverse direction to avoid problems with erasing from vector) for (int i = intersect.size() - 1; i >= 0; --i) { Object *pSubObject = mObjects[intersect[i]]; mObjects.erase(mObjects.begin() + intersect[i]); pNewObject->merge(pSubObject); delete pSubObject; } mObjects.push_back(pNewObject); } } void Layer::build(Object *pObject) { // ignore empty objects if (pObject->mLines.size() < 1) { delete pObject; return; } // find intersecting objects std::vector<unsigned int> intersect; for (unsigned int i = 0; i < mObjects.size(); ++i) if (mObjects[i]->isIntersecting(pObject)) intersect.push_back(i); // new object if (intersect.size() == 0) { // use object mObjects.push_back(pObject); } // new part of one existing object else if (intersect.size() == 1) { // add to existing object mObjects[intersect[0]]->merge(pObject); delete pObject; } // connecting multiple objects else { // merge objects with existing intersecting objects // (go in reverse direction to avoid problems with erasing from vector) for (int i = intersect.size() - 1; i >= 0; --i) { Object *pSubObject = mObjects[intersect[i]]; mObjects.erase(mObjects.begin() + intersect[i]); pObject->merge(pSubObject); delete pSubObject; } mObjects.push_back(pObject); } } void Layer::merge(Layer *pLayer) { std::vector<Object *>::const_iterator itObject; for (itObject = pLayer->mObjects.begin(); itObject != pLayer->mObjects.end(); ++itObject) add(*itObject); pLayer->mObjects.clear(); }