bool RPolyline::simplify(double angleTolerance) { bool ret = false; RPolyline newPolyline; //ret.appendVertex(getStartPoint()); RS::EntityType type = RS::EntityUnknown; double angle = RMAXDOUBLE; //double radius = 0; //RVector center; //RVector vertex; for (int i=0; i<countSegments(); i++) { QSharedPointer<RShape> seg = getSegmentAt(i); QSharedPointer<RLine> line = seg.dynamicCast<RLine>(); if (!line.isNull()) { double angleDiff = qAbs(RMath::getAngleDifference180(line->getAngle(), angle)); // qDebug() << "angle diff: " << angleDiff; // qDebug() << "tol: " << angleTolerance; if (type==RS::EntityLine && angleDiff<angleTolerance) { //vertex = line->getEndPoint(); ret = true; } else { //qDebug() << "start: " << line->getStartPoint(); newPolyline.appendVertex(line->getStartPoint()); angle = line->getAngle(); type = RS::EntityLine; } } QSharedPointer<RArc> arc = seg.dynamicCast<RArc>(); if (!arc.isNull()) { // TODO newPolyline.appendVertex(arc->getStartPoint(), arc->getBulge()); } } if (isClosed()) { newPolyline.setClosed(true); } else { newPolyline.appendVertex(getEndPoint()); } // qDebug() << "old polyline: " << *this; // qDebug() << "new polyline: " << newPolyline; //*this = newPolyline; //clear(); vertices = newPolyline.vertices; bulges = newPolyline.bulges; closed = newPolyline.closed; return ret; }
/** * Exports an ellipse with the current attributes. * \todo switch from line based interpolation to arcs. */ void RExporter::exportEllipse(const REllipse& ellipse, double offset) { if (ellipse.getMajorRadius()<RS::PointTolerance || ellipse.getMinorRadius()<RS::PointTolerance) { return; } RPolyline polyline; RVector cp = ellipse.getCenter(); double radius1 = ellipse.getMajorRadius(); double radius2 = ellipse.getMinorRadius(); double angle = ellipse.getAngle(); double a1 = ellipse.getStartParam(); double a2 = ellipse.getEndParam(); bool reversed = ellipse.isReversed(); double aStep; // Angle Step (rad) double a; // Current Angle (rad) aStep=0.05; RVector vp; RVector vc(cp.x, cp.y); vp.set(cp.x+cos(a1)*radius1, cp.y+sin(a1)*radius2); vp.rotate(angle, vc); polyline.appendVertex(vp); if (!reversed) { // Arc Counterclockwise: if (a1>a2-RS::AngleTolerance) { a2+=2*M_PI; } for(a=a1+aStep; a<=a2; a+=aStep) { vp.set(cp.x+cos(a)*radius1, cp.y+sin(a)*radius2); vp.rotate(angle, vc); polyline.appendVertex(vp); } } else { // Arc Clockwise: if (a1<a2+RS::AngleTolerance) { a2-=2*M_PI; } for(a=a1-aStep; a>=a2; a-=aStep) { vp.set(cp.x+cos(a)*radius1, cp.y+sin(a)*radius2); vp.rotate(angle, vc); polyline.appendVertex(vp); } } vp.set(cp.x+cos(a2)*radius1, cp.y+sin(a2)*radius2); vp.rotate(angle, vc); polyline.appendVertex(vp); exportPolyline(polyline, offset); }
void RSolidEntity::exportEntity(RExporter& e, bool preview, bool forceSelected) const { Q_UNUSED(preview); Q_UNUSED(forceSelected); // note that order of fourth and third vertex is swapped: RPolyline pl; pl.appendVertex(data.getVertexAt(0)); pl.appendVertex(data.getVertexAt(1)); if (data.countVertices()>3) { pl.appendVertex(data.getVertexAt(3)); } pl.appendVertex(data.getVertexAt(2)); pl.setClosed(true); e.exportPolyline(pl); }
RPolyline RSpline::toPolyline(int segments) const { RPolyline ret; QList<QSharedPointer<RShape> > lineSegments = getExploded(segments); for (int k=0; k<lineSegments.size(); k++) { QSharedPointer<RDirected> dir = lineSegments[k].dynamicCast<RDirected>(); if (dir.isNull()) { continue; } if (k==0) { ret.appendVertex(dir->getStartPoint()); } ret.appendVertex(dir->getEndPoint()); } return ret; }
RPolyline RSpline::toPolyline(int segments) const { RPolyline ret; QList<QSharedPointer<RShape> > lineSegments = getExplodedBezier(segments); for (int k=0; k<lineSegments.size(); k++) { QSharedPointer<RShape> shape = lineSegments[k]; if (shape.isNull() || !shape->isDirected()) { continue; } if (k==0) { ret.appendVertex(shape->getStartPoint()); } ret.appendVertex(shape->getEndPoint()); } if (isClosed()) { ret.setClosed(true); } return ret; }
RPolyline RArc::approximateWithLines(double segmentLength) { RPolyline polyline; // avoid a segment length of 0: if (segmentLength<1.0e-6) { segmentLength = 1.0e-6; } double a1 = getStartAngle(); double a2 = getEndAngle(); double aStep = segmentLength / radius; double a, cix, ciy; polyline.appendVertex(getStartPoint()); if (!reversed) { // Arc Counterclockwise: if (a1>a2-1.0e-10) { a2+=2*M_PI; } for (a=a1+aStep; a<=a2; a+=aStep) { cix = center.x + cos(a) * radius; ciy = center.y + sin(a) * radius; polyline.appendVertex(RVector(cix, ciy)); } } else { // Arc Clockwise: if (a1<a2+1.0e-10) { a2-=2*M_PI; } for (a=a1-aStep; a>=a2; a-=aStep) { cix = center.x + cos(a) * radius; ciy = center.y + sin(a) * radius; polyline.appendVertex(RVector(cix, ciy)); } } polyline.appendVertex(getEndPoint()); return polyline; }