Example #1
0
void RGraphicsView::setOffset(const RVector& offset, bool regen) {
    RVector o = offset;

    if (!o.isSane()) {
        o = RVector(0,0);
    }

    // 20111024: avoid overflows with weird behaviour when using track pad:
    if (RSettings::getLimitZoomAndScroll()) {
        if (offset.x < -1.0e8 || offset.x > 1.0e8) {
            o.x = 0.0;
        }
        if (offset.y < -1.0e8 || offset.y > 1.0e8) {
            o.y = 0.0;
        }
    }

    this->offset = o;

    // offset in pixels:
    //RVector ov(RMath::mround(offset.x*factor), RMath::mround(offset.y*factor));
    ////ov = RVector(RMath::mround(ov.x), RMath::mround(ov.y));
    ////this->offset = mapFromView(ov);
    //this->offset = RVector(ov.x/factor, ov.y/factor);

    if (regen) {
        regenerate();
        if (scene!=NULL) {
            // notify actions about zoom change:
            scene->getDocumentInterface().zoomChangeEvent(*this);
        }
    }
    //viewportChangeEvent();
}
Example #2
0
/**
 * \return Shape of segment at given position.
 */
QSharedPointer<RShape> RPolyline::getSegmentAt(int i) const {
    if (i<0 || i>=vertices.size() || i>=bulges.size()) {
        qWarning() << "RPolyline::getSegmentAt(" << i << "): i out of range";
        return QSharedPointer<RShape>();
    }

    RVector p1 = vertices.at(i);
    RVector p2 = vertices.at((i+1) % vertices.size());

    if (RPolyline::isStraight(bulges.at(i))) {
        return QSharedPointer<RShape>(new RLine(p1, p2));
    }

    else {
        double bulge = bulges.at(i);
        bool reversed = bulge<0.0;
        double alpha = atan(bulge)*4.0;

        if (fabs(alpha) > 2*M_PI-RS::AngleTolerance) {
            return QSharedPointer<RShape>();
        }

        double radius;
        RVector center;
        RVector middle;
        double dist;
        double angle;

        middle = (p1+p2)/2.0;
        dist = p1.getDistanceTo(p2)/2.0;
        angle = p1.getAngleTo(p2);

        // alpha can't be 0.0 at this point
        radius = fabs(dist / sin(alpha/2.0));

        double rootTerm = fabs(radius*radius - dist*dist);
        double h = sqrt(rootTerm);

        if (bulge>0.0) {
            angle+=M_PI/2.0;
        } else {
            angle-=M_PI/2.0;
        }

        if (fabs(alpha)>M_PI) {
            h*=-1.0;
        }

        center.setPolar(h, angle);
        center+=middle;

        double a1;
        double a2;

        a1 = center.getAngleTo(p1);
        a2 = center.getAngleTo(p2);

        return QSharedPointer<RShape>(new RArc(center, radius, a1, a2, reversed));
    }
}
Example #3
0
RCircle RCircle::createFrom3Points(const RVector& p1,
                                  const RVector& p2,
                                  const RVector& p3) {
    // intersection of two middle lines

    // middle points between first two points:
    RVector mp1 = RVector::getAverage(p1, p2);
    double a1 = p1.getAngleTo(p2) + M_PI / 2.0;
    // direction from middle point to center:
    RVector dir1 = RVector::createPolar(1.0, a1);

    // middle points between last two points:
    RVector mp2 = RVector::getAverage(p2, p3);
    double a2 = p2.getAngleTo(p3) + M_PI / 2.0;
    // direction from middle point to center:
    RVector dir2 = RVector::createPolar(1.0, a2);

    RLine midLine1(mp1, mp1 + dir1);
    RLine midLine2(mp2, mp2 + dir2);

    QList<RVector> ips = midLine1.getIntersectionPoints(midLine2, false);
    if (ips.length()!=1) {
        return RCircle();
    }

    RVector center = ips[0];
    double radius = center.getDistanceTo(p3);
//    double angle1 = center.getAngleTo(p1);
//    double angle2 = center.getAngleTo(p3);
//    bool reversed = RMath::isAngleBetween(center.getAngleTo(p2),
//                                            angle1, angle2, true);

    return RCircle(center, radius);
}
Example #4
0
bool RPolylineData::moveReferencePoint(const RVector& referencePoint,
        const RVector& targetPoint) {
    bool ret = false;

    QList<RVector>::iterator it;
    for (it=vertices.begin(); it!=vertices.end(); ++it) {
        if (referencePoint.equalsFuzzy(*it)) {
            (*it) = targetPoint;
            ret = true;
        }
    }

    for (int i=0; i<countSegments(); i++) {
        if (isArcSegmentAt(i)) {
            QSharedPointer<RArc> arc = getSegmentAt(i).dynamicCast<RArc>();
            if (!arc.isNull()) {
                if (referencePoint.equalsFuzzy(arc->getMiddlePoint())) {
                    RArc a = RArc::createFrom3Points(arc->getStartPoint(), targetPoint, arc->getEndPoint());
                    setBulgeAt(i, a.getBulge());
                    ret = true;
                }
            }
        }
    }

    return ret;
}
Example #5
0
void RDimDiametricData::updateTextData() const {
    initTextData();

    double dimgap = getDimgap();

    if (RMath::isNaN(defaultAngle)) {
        // updates default angle:
        getShapes();
    }

    // move text to the side if appropriate:
    if (!hasCustomTextPosition()) {
        //RBox bbox = textData.getBoundingBox();
        if (!RMath::isNaN(dimLineLength) && textData.getWidth()>dimLineLength) {
            RVector distH;
            distH.setPolar(textData.getWidth()/2.0
                           +dimLineLength/2.0+dimgap, defaultAngle);
            textPositionSide = textPositionCenter;
            textPositionSide+=distH;
        }
        else {
            textPositionSide = RVector::invalid;
        }
    }

    textData.rotate(defaultAngle, RVector(0,0));
    textData.move(getTextPosition());
}
Example #6
0
/**
 * \return Point on this shape that is closest to p. Based on getVectorTo.
 */
RVector RShape::getClosestPointOnShape(const RVector& p, bool limited) const {
    RVector dv = getVectorTo(p, limited);
    if (!dv.isValid()) {
        return RVector::invalid;
    }
    return p - dv;
}
Example #7
0
QList<RVector> RShape::getIntersectionPointsLT(const RLine& line1,
        const RTriangle& triangle2, bool limited) {

    QList<RVector> res;

    RVector normal = triangle2.getNormal();

    if (normal.getMagnitude() < 1.0e-12) {
        return res;
    }

    if (line1.getLength() < 1.0e-12) {
        return res;
    }

    double t = RVector::getDotProduct(normal, triangle2.getCorner(2) - line1.getStartPoint())
            / RVector::getDotProduct(normal, (line1.getEndPoint() - line1.getStartPoint()));

    // check if intersection point is on the line:
    if (limited && (t < 0.0 || t > 1.0)) {
        return res;
    }

    // intersection point:
    RVector ip = line1.getStartPoint() + (line1.getEndPoint() - line1.getStartPoint()) * t;

    // check if intersection point is inside the triangle:
    if (!limited || triangle2.isPointInTriangle(ip)) {
        res.push_back(ip);
    }

    return res;
}
Example #8
0
/**
 * \return Shortest distance from this shape to the given point.
 *      Based on \ref getVectorTo.
 */
double RShape::getDistanceTo(const RVector& point, bool limited) const {
    RVector v = getVectorTo(point, limited);
    if (v.isValid()) {
        return v.getMagnitude();
    }
    return RNANDOUBLE;
}
Example #9
0
// previously: getEllipseAngle
double REllipse::getParamTo(const RVector& pos) const {
    RVector m = pos;
    m.rotate(-majorPoint.getAngle(), center);
    RVector v = m-center;
    v.scale(RVector(1.0, 1.0/ratio));
    return v.getAngle();
}
Example #10
0
RBox REllipse::getBoundingBox() const {
    double radius1 = getMajorRadius();
    double radius2 = getMinorRadius();
    double angle = getAngle();
    double a1 = ((!isReversed()) ? startParam : endParam);
    double a2 = ((!isReversed()) ? endParam : startParam);
    RVector startPoint = getStartPoint();
    RVector endPoint = getEndPoint();

    double minX = qMin(startPoint.x, endPoint.x);
    double minY = qMin(startPoint.y, endPoint.y);
    double maxX = qMax(startPoint.x, endPoint.x);
    double maxY = qMax(startPoint.y, endPoint.y);

    // kind of a brute force. TODO: exact calculation
    RVector vp;
    double a = a1;
    do {
        vp.set(center.x + radius1 * cos(a),
               center.y + radius2 * sin(a));
        vp.rotate(angle, center);

        minX = qMin(minX, vp.x);
        minY = qMin(minY, vp.y);
        maxX = qMax(maxX, vp.x);
        maxY = qMax(maxY, vp.y);

        a += 0.03;
    } while (RMath::isAngleBetween(a, a1, a2, false) && a<4*M_PI);

    return RBox(RVector(minX,minY), RVector(maxX,maxY));
}
Example #11
0
bool RDimensionData::moveReferencePoint(const RVector& referencePoint,
        const RVector& targetPoint) {

    if (referencePoint.equalsFuzzy(definitionPoint)) {
        definitionPoint = targetPoint;
        autoTextPos = true;
        update();
        return true;
    }
    if (textPositionSide.isValid()) {
        if (referencePoint.equalsFuzzy(textPositionSide)) {
            textPositionCenter = targetPoint;
            textPositionSide = RVector::invalid;
            autoTextPos = false;
            update();
            return true;
        }
    }
    if (referencePoint.equalsFuzzy(textPositionCenter)) {
        textPositionCenter = targetPoint;
        autoTextPos = false;
        update();
        return true;
    }

    return false;
}
Example #12
0
void RCPBody::applyImpulse(RVector const & impulse)
{
    if (mBody) {
        cpBodyApplyImpulseAtWorldPoint(mBody, cpv(impulse.x(), impulse.y()), cpBodyLocalToWorld(mBody, cpBodyGetCenterOfGravity(mBody)));
    }

}
BOOST_FIXTURE_TEST_CASE( ser_vs_unser , F) {
	assert(count > 0);

	p->save(filename);

	RegistryCache* up = new RegistryCache();
	up->from_file(filename);

	BOOST_CHECK_EQUAL(up->size(), count);

	Registry left = createReg(1);
	RVector* v = p->get(left.name);
	BOOST_CHECK_EQUAL(v->size(), 1);
	Registry right = v->front();
	BOOST_CHECK_EQUAL(left.name, right.name);
	BOOST_CHECK_EQUAL(left.host, right.host);
	BOOST_CHECK_EQUAL(left.port, right.port);

	left = createReg(count - 1);
	BOOST_CHECK_EQUAL(v->size(), 1);
	right = v->front();
	BOOST_CHECK_EQUAL(left.name, right.name);
	BOOST_CHECK_EQUAL(left.host, right.host);
	BOOST_CHECK_EQUAL(left.port, right.port);

	delete up;
	up = NULL;

	teardown();
}
Example #14
0
RArc RArc::createTangential(const RVector& startPoint, const RVector& pos,
                            double direction, double radius) {
    RArc arc;

    arc.radius = radius;

    // orthogonal to base entity:
    RVector ortho;
    ortho.setPolar(radius, direction + M_PI/2.0);

    // two possible center points for arc:
    RVector center1 = startPoint + ortho;
    RVector center2 = startPoint - ortho;
    if (center1.getDistanceTo(pos) < center2.getDistanceTo(pos)) {
        arc.center = center1;
    } else {
        arc.center = center2;
    }

    // angles:
    arc.startAngle = arc.center.getAngleTo(startPoint);
    arc.endAngle = arc.center.getAngleTo(pos);

    // handle arc direction:
    arc.reversed = false;
    double diff = RMath::getNormalizedAngle(arc.getDirection1() - direction);
    if (fabs(diff-M_PI) < 1.0e-1) {
        arc.reversed = true;
    }

    return arc;
}
Example #15
0
/**
 * \return List of RLines describing this spline.
 */
QList<QSharedPointer<RShape> > RSpline::getExploded(int segments) const {
    if (!exploded.isEmpty() && segments==-1) {
        return exploded;
    }

    //qDebug() << "RSpline::getExploded: segments: " << segments;
    //RDebug::printBacktrace("getExploded:    ");

    //##boundingBox = RBox();

    updateInternal();

    exploded.clear();

    if (!isValid()) {
        //qWarning() << "RSpline::getExploded: invalid spline";
        return exploded;
    }

    if (segments==-1) {
        segments = 8;
    }

    double tMin = getTMin();
    double tMax = getTMax();

    double step = getTDelta() / (controlPoints.size() * segments);

    RVector p1;
    RVector prev = RVector::invalid;
    for (double t = tMin; t<tMax+(step/2.0); t+=step) {
        double tc = qMin(t, tMax);
        p1 = getPointAt(tc);

        if (RMath::isNaN(p1.x) || RMath::isNaN(p1.y)) {
            continue;
        }

        if (prev.isValid()) {
            RLine* line = new RLine(prev, p1);
            exploded.append(QSharedPointer<RShape>(line));
        }
        prev = p1;

        //##boundingBox.growToInclude(p1);
    }

    p1 = getEndPoint();
    if (!RMath::isNaN(p1.x) && !RMath::isNaN(p1.y)) {
        if (prev.isValid()) {
            RLine* line = new RLine(prev, p1);
            // prevent zero length line at the end:
            if (line->getLength()>1.0e-4) {
                exploded.append(QSharedPointer<RShape>(line));
            }
        }
    }

    return exploded;
}
Example #16
0
QList<RVector> RArc::getPointsWithDistanceToEnd(double distance, RS::From from) const {
    QList<RVector> ret;

    if (radius<RS::PointTolerance) {
        return ret;
    }

    double a1;
    double a2;
    RVector p;
    double aDist = distance / radius;

    if (isReversed()) {
        a1 = getStartAngle() - aDist;
        a2 = getEndAngle() + aDist;
    } else {
        a1 = getStartAngle() + aDist;
        a2 = getEndAngle() - aDist;
    }

    if (from==RS::FromStart || from==RS::FromAny) {
        p.setPolar(radius, a1);
        p += center;
        ret.append(p);
    }

    if (from==RS::FromEnd || from==RS::FromAny) {
        p.setPolar(radius, a2);
        p += center;
        ret.append(p);
    }

    return ret;
}
Example #17
0
bool RCircle::move(const RVector& offset) {
    if (!offset.isValid() || offset.getMagnitude() < RS::PointTolerance) {
        return false;
    }
    center += offset;
    return true;
}
Example #18
0
void RB2DBody::setPosition(RVector const & pos)
{
    mPosition.set(pos.x()/PTM_RATIO, pos.y()/PTM_RATIO, 0);
    if (mBody) {
        mBody->SetTransform(b2Vec2(mPosition.x()/PTM_RATIO, mPosition.y()/PTM_RATIO), mBody->GetAngle());
    }
}
Example #19
0
void REllipse::moveEndPoint(const RVector& pos, bool changeAngleOnly) {
    if (changeAngleOnly) {
        endParam = getParamTo(pos);
    }
    else {
        RVector sp = getStartPoint();
        double distOri = sp.getDistanceTo(getEndPoint());
        double angleOri = sp.getAngleTo(getEndPoint());
        if (distOri<RS::PointTolerance) {
            return;
        }

        double distNew = sp.getDistanceTo(pos);
        double angleNew = sp.getAngleTo(pos);
        double factor = distNew / distOri;
        if (factor<RS::PointTolerance) {
            return;
        }
        double angleDelta = angleNew - angleOri;

        center.scale(factor, sp);
        center.rotate(angleDelta, sp);
        majorPoint.scale(factor);
        majorPoint.rotate(angleDelta);
    }
}
Example #20
0
/**
 * Creates an arc from its startpoint, endpoint and bulge (= tan(angle/4)).
 */
RArc RArc::createFrom2PBulge(const RVector& startPoint,
        const RVector& endPoint, double bulge) {

    RArc arc;

    arc.reversed = (bulge < 0.0);
    double alpha = atan(bulge) * 4.0;

    RVector middle = (startPoint + endPoint) / 2.0;
    double dist = startPoint.getDistanceTo(endPoint) / 2.0;

    // alpha can't be 0.0 at this point
    arc.radius = fabs(dist / sin(alpha / 2.0));

    double wu = fabs(RMath::pow(arc.radius, 2.0) - RMath::pow(dist, 2.0));
    double h = sqrt(wu);
    double angle = startPoint.getAngleTo(endPoint);

    if (bulge > 0.0) {
        angle += M_PI / 2.0;
    } else {
        angle -= M_PI / 2.0;
    }

    if (fabs(alpha) > M_PI) {
        h *= -1.0;
    }

    arc.center.setPolar(h, angle);
    arc.center += middle;
    arc.startAngle = arc.center.getAngleTo(startPoint);
    arc.endAngle = arc.center.getAngleTo(endPoint);

    return arc;
}
Example #21
0
bool REllipse::contains(const RVector& p) const {
    RVector pt = p;
    pt.move(-center);
    pt.rotate(-getAngle());
    double rx = getMajorRadius();
    double ry = getMinorRadius();
    return (pt.x*pt.x) / (rx*rx) + (pt.y*pt.y) / (ry*ry) <= 1.0;
}
Example #22
0
void HarmonicFunction::setCoefficients( const RVector & coeff ){

    nHarmonic_ = coeff.size() / 2;
    if ( ( (double)coeff.size() / 2.0 - nHarmonic_ ) > TOLERANCE ){
        throwError( 1, WHERE_AM_I + " coefficients size is uneven" + str( coeff.size() ) );
    }
    coeff_ = coeff;
}
Example #23
0
RVector DC1dModelling::pot1d(const RVector & R, const RVector & rho, const RVector & thk) {
    RVector z0(R.size());
    double rabs;
    for (size_t i = 0; i < R.size(); i++) {
        rabs = std::fabs(R[i]);
        z0[i] = sum(myw_ * kern1d(myx_ / rabs, rho, thk) * 2.0) / rabs;
    }
    return z0;
}
Example #24
0
void RSpriteItem::onSingleTap(int x, int y)
{
    RVector worldPoint = RTransUtils::worldSpacePoint(mProjection, mCamera, mTransformation, RVector(x, y));
    mIsPressed = false;

    if (worldPoint.x() > mX && worldPoint.x() < mX + mWidth &&
            worldPoint.y() > mY && worldPoint.y() < mY + mHeight) {
        onClick(x, y);
    }
}
Example #25
0
/**
 * \return The shortest distance from this entity to the given point.
 */
double REntityData::getDistanceTo(const RVector& point, bool limited, double range, bool draft) const {
    Q_UNUSED(range);
    Q_UNUSED(draft);

    RVector v = getVectorTo(point, limited);
    if (v.isValid()) {
        return v.getMagnitude();
    }
    return RNANDOUBLE;
}
Example #26
0
RVector RCircle::getVectorTo(const RVector& point, bool /*limited*/) const {
    RVector v = point - center;

    // point is at the center of the circle, infinite solutions:
    if (v.getMagnitude()<RS::PointTolerance) {
        return RVector::invalid;
    }

    return RVector::createPolar(v.getMagnitude() - radius, v.getAngle());
}
Example #27
0
RVector RArc::getVectorTo(const RVector& point, bool limited) const {
    double angle = center.getAngleTo(point);
    if (limited
            && !RMath::isAngleBetween(angle, startAngle, endAngle, reversed)) {
        return RVector::invalid;
    }

    RVector v = point - center;
    return RVector::createPolar(v.getMagnitude() - radius, v.getAngle());
}
BOOST_FIXTURE_TEST_CASE( unserialize , F) {
	setup();

	const string content =
			"[{\"name\":\"servier0\",\"host\":\"localhost\",\"port\":0,\"weight\":3600 },{\"name\":\"servier1\",\"host\":\"localhost\",\"port\":1,\"weight\":3600 }]";
	// vector<Registry>
	RVector v = Registry::unserialize(content);
	BOOST_CHECK_EQUAL(v.size(), 2);

}
Example #29
0
void GaussLegendre(double x1, double x2, uint n, RVector & x, RVector & w){
//  taken from matlab-code (Thomas Guenther)
//  function [x, w] = gauleg(x1, x2, n)
//  Given the lower and upper limits of integration x1 and x2 and given n,
//  this routine returns arrays x(1..n) and w(1..n) of length n,
//  containing the abscissas and weights of the Gauss-Legendre
//  n-point quadrature formula.
//  For a description of the following routines see
//  Numerical Recipes, Press et al.

  if (x.size() != n) x.resize(n);
  if (w.size() != n) w.resize(n);

  double epsilon = 3.0e-6;

  double m = (n + 1.0) / 2.0 ;
  double xm = 0.5 * (x2 + x1);
  double xl = 0.5 * (x2 - x1);

  double z = 0.0, z1 = 0.0, p1 = 0.0, p2 = 0.0, p3 = 0.0, pp = 0.0;

  for (int i = 1; i <= m; i ++){
    z = std::cos(PI * (i - 0.25) / (n + 0.5));

   // Starting with the above approximation to the ith root, we enter
   // the main loop of refinements by Newton's method
   z1 = z + 2.0 * epsilon;

   while (std::fabs(z - z1) > epsilon){
     p1 = 1.0;
     p2 = 0.0;
     for (size_t j = 1; j <= n; j ++){
       p3 = p2;
       p2 = p1;
       p1 = ((2.0 * j - 1.0) * z * p2 - (j - 1.0) * p3) / (double)j;
     }

     // p1 is now the desired Legendre polynomial. We next compute pp,
     // its derivative, by a standard relation involving also p2, the
     // polynomial of one lower order
     pp = (double)n * (z * p1 - p2) / (z * z - 1.0);
     z1 = z;
     z = z1 - p1 / pp; // Newtons method
   }
   // Scale the root to the desired interval, and put in its
   // symmetric counterpart
   x[ i - 1 ] = xm - xl * z;
   x[ n - i ] = xm + xl * z;
   //   x[ n +1 - i ] = xm + xl * z;
   //Compute the weight and ist symmetric counterpart
   w[ i - 1 ] = 2.0 * xl / ((1.0 -z * z) *pp *pp);
   w[ n - i ] = w[ i - 1 ];
   //   w[ n + 1 - i ] = w[ i - 1 ];
  }
}
Example #30
0
 RVector response( const RVector & model ) {
     //! extract slowness from model and call old function
     RVector slowness( model, 0, model.size() - shots_.size() );
     RVector offsets( model, model.size() - shots_.size(), model.size() ); 
     RVector resp = TravelTimeDijkstraModelling::response( slowness ); //! normal response
     RVector shotpos = dataContainer_->get( "s" );
     for ( size_t i = 0; i < resp.size() ; i++ ){
         resp[ i ] += offsets[ shotMap_[ shotpos[ i ] ] ];
     }
     return resp;
 }