pcbzone.cpp
142 lines
| 5.5 KiB
| text/x-c
|
CppLexer
r14 | /*------------------------------------------------------------------------------ | |||
-- This file is a part of the Kicad Tools Software | ||||
-- Copyright (C) 2015, Plasma Physics Laboratory - CNRS | ||||
-- | ||||
-- 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; either version 2 of the License, or | ||||
-- (at your option) any later version. | ||||
-- | ||||
-- 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 General Public License | ||||
-- along with this program; if not, write to the Free Software | ||||
-- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||||
-------------------------------------------------------------------------------*/ | ||||
/*-- Author : Alexis Jeandet | ||||
-- Mail : alexis.jeandet@member.fsf.org | ||||
----------------------------------------------------------------------------*/ | ||||
#include "pcbzone.h" | ||||
#include <QPen> | ||||
#include <QPixmapCache> | ||||
r15 | #include "polypartition/src/polypartition.h" | |||
r16 | #include "polygonssplit.h" | |||
r15 | ||||
r14 | ||||
PCBZone::PCBZone(QIlib::QIcadPcbZone *zoneNode, PCBContext *context) | ||||
:QGraphicsItemGroup(),zoneNode(zoneNode),context(context) | ||||
{ | ||||
r15 | this->initQtClipping(); | |||
r16 | // this->initTriangPolypartition(); | |||
r14 | } | |||
r15 | void PCBZone::initTriangPolypartition() | |||
{ | ||||
TPPLPartition pp; | ||||
this->setFlags(ItemIsMovable|ItemIsSelectable|ItemIsFocusable); | ||||
TPPLPoly test; | ||||
std::list<TPPLPoly> triangles; | ||||
test.Init(this->zoneNode->filledPolygon.points.points.count()); | ||||
for(int i=0;i<this->zoneNode->filledPolygon.points.points.count();i++) | ||||
{ | ||||
test[i].x =this->zoneNode->filledPolygon.points.points.at(i)->pos().x(); | ||||
test[i].y =this->zoneNode->filledPolygon.points.points.at(i)->pos().y(); | ||||
} | ||||
r16 | pp.ConvexPartition_HM(&test,&triangles); | |||
r15 | for(std::list<TPPLPoly>::iterator iter = triangles.begin();iter != triangles.end(); iter++) | |||
{ | ||||
QPolygonF poly; | ||||
TPPLPoly t = *iter; | ||||
for(int i=0;i<t.GetNumPoints();i++) | ||||
{ | ||||
poly.append(QPointF(t[i].x,t[i].y)); | ||||
} | ||||
QGraphicsPolygonItem* triangleItem = new QGraphicsPolygonItem(); | ||||
QPen pen = triangleItem->pen(); | ||||
pen.setWidthF(0.05); | ||||
triangleItem->setPen(pen); | ||||
QBrush brush = triangleItem->brush(); | ||||
brush.setStyle(Qt::SolidPattern); | ||||
brush.setColor(context->layerColor(this->zoneNode->layer.value())); | ||||
triangleItem->setBrush(brush); | ||||
triangleItem->setZValue(-context->layer(this->zoneNode->layer.value())); | ||||
triangleItem->setPolygon(poly); | ||||
triangleItem->setCacheMode(QGraphicsItem::DeviceCoordinateCache); | ||||
this->addToGroup(triangleItem); | ||||
} | ||||
this->setCacheMode(QGraphicsItem::DeviceCoordinateCache); | ||||
setOpacity(0.6); | ||||
} | ||||
void PCBZone::initQtClipping() | ||||
r14 | { | |||
this->setFlags(ItemIsMovable|ItemIsSelectable|ItemIsFocusable); | ||||
QPolygonF poly; | ||||
for(int i=0;i<this->zoneNode->filledPolygon.points.points.count();i++) | ||||
{ | ||||
r15 | QPointF p; | |||
p.setX(this->zoneNode->filledPolygon.points.points.at(i)->pos().x()); | ||||
p.setY(this->zoneNode->filledPolygon.points.points.at(i)->pos().y()); | ||||
poly.append(p); | ||||
r14 | } | |||
r16 | QList<QPolygonF> clippedPolygons = PolygonsSplit::splitPolygons(poly,100,5); | |||
r15 | for(int i=0;i<clippedPolygons.count();i++) | |||
{ | ||||
QGraphicsPolygonItem* polygonItem = new QGraphicsPolygonItem(); | ||||
// QPen pen = polygonItem->pen(); | ||||
r16 | // pen.setWidthF(0.05); | |||
r14 | ||||
r15 | polygonItem->setPen(Qt::NoPen); | |||
// polygonItem->setPen(pen); | ||||
QBrush brush = polygonItem->brush(); | ||||
brush.setStyle(Qt::SolidPattern); | ||||
brush.setColor(context->layerColor(this->zoneNode->layer.value())); | ||||
polygonItem->setBrush(brush); | ||||
polygonItem->setZValue(-context->layer(this->zoneNode->layer.value())); | ||||
polygonItem->setPolygon(clippedPolygons.at(i)); | ||||
// polygonItem->setCacheMode(QGraphicsItem::DeviceCoordinateCache); | ||||
this->addToGroup(polygonItem); | ||||
} | ||||
r14 | ||||
this->setCacheMode(QGraphicsItem::DeviceCoordinateCache); | ||||
setOpacity(0.6); | ||||
} | ||||
r15 | ||||
QPolygonF intersect(QPointF topLeft,QPointF bottomRight,QPolygonF poly) | ||||
{ | ||||
QPolygonF rect; | ||||
rect.append(topLeft); | ||||
rect.append(QPointF(bottomRight.x(),topLeft.y())); | ||||
rect.append(bottomRight); | ||||
rect.append(QPointF(topLeft.x(),bottomRight.y())); | ||||
return poly.intersected(rect); | ||||
} | ||||
QList<QPolygonF> PCBZone::splitPolygons(QPolygonF polygon, int maxPoints) | ||||
{ | ||||
QList<QPolygonF> result; | ||||
if(polygon.size()>maxPoints) | ||||
{ | ||||
QRectF brect = polygon.boundingRect(); | ||||
QPointF center = brect.center(); | ||||
QPointF centerLeft = QPointF(brect.x(),brect.center().y()); | ||||
QPointF centerRight = QPointF(brect.topRight().x(),brect.center().y()); | ||||
QPointF topCenter = QPointF(center.x(),brect.topLeft().y()); | ||||
QPointF bottomCenter = QPointF(center.x(),brect.bottomLeft().y()); | ||||
result.append(splitPolygons(intersect(brect.topLeft(),center,polygon),maxPoints)); | ||||
result.append(splitPolygons(intersect(topCenter,centerRight,polygon),maxPoints)); | ||||
result.append(splitPolygons(intersect(center,brect.bottomRight(),polygon),maxPoints)); | ||||
result.append(splitPolygons(intersect(centerLeft,bottomCenter,polygon),maxPoints)); | ||||
} | ||||
else | ||||
result.append(polygon); | ||||
return result; | ||||
} | ||||