qreal BezierCurve::findDistance(BezierCurve curve, int i, QPointF P, QPointF& nearestPoint, qreal& t) //finds the distance between a cubic section and a point { //qDebug() << "---- INTER CUBIC SEGMENT"; int nSteps = 24; int k0 = 0; QPointF Q; Q = curve.getVertex(i-1); qreal distMin = eLength(Q-P); nearestPoint = Q; t = 0; for(int k=1; k<=nSteps; k++) { qreal s = (k+0.0)/nSteps; Q = curve.getPointOnCubic(i, s); qreal dist = eLength(Q-P); if (dist <= distMin) { distMin = dist; nearestPoint = Q; t = s; k0 = k; } } //QPointF Q1 = curve.getPointOnCubic(i, t); return distMin; }
void BezierCurve::normalise(QPointF& point) { qreal length = eLength(point); if (length > 1.0e-6) { point = point/length; } }
void Photon::move() { Float rnd; Float theta = symmetrizeTheta(Angle(s_i, Optics::director).theta); Float meanFreePath = (channel == Optics::OCHANNEL) ? oLength(theta) : eLength(theta); Float c1 = (s_i.z() >= 0) ? 1. : -expm1((pos.z()/s_i.z())/meanFreePath); rnd = random(); Float d = -log1p(-c1*rnd)*meanFreePath; pos += d*s_i; }
void BezierCurve::smoothCurve() { QPointF c1, c2, c2old, tangentVec, normalVec; int n = vertex.size(); c2old = QPointF(-100,-100); // bogus point for(int p=0; p<n-1; p++) { QPointF D = getVertex(p); QPointF Dprev = getVertex(p-1); QPointF Dnext = getVertex(p+1); qreal L1 = mLength(D-Dprev); qreal L2 = mLength(D-Dnext); tangentVec = 0.4*(Dnext - Dprev); normalVec = QPointF(-tangentVec.y(), tangentVec.x())/eLength(tangentVec); if ( ((D-Dprev).x()*(D-Dnext).x()+(D-Dprev).y()*(D-Dnext).y())/(1.0*L1*L2) < 0 ) { // smooth point c1 = D - tangentVec*(L1+0.0)/(L1+L2); c2 = D + tangentVec*(L2+0.0)/(L1+L2); } else { // sharp point c1 = 0.6*D + 0.4*Dprev; c2 = 0.6*D + 0.4*Dnext; } if (p==0) { c2old = 0.5*(vertex.at(0)+c1); } this->c1[p] = c2old; this->c2[p] = c1; //appendCubic(c2old, c1, D, pressureList->at(p)); c2old = c2; } if (n>2) { this->c1[n-1] = c2old; this->c2[n-1] = 0.5*(c2old+vertex.at(n-1)); } }
bool BezierCurve::findIntersection(BezierCurve curve1, int i1, BezierCurve curve2, int i2, QList<Intersection>& intersections) //finds the intersection between two cubic sections { bool result = false; //qDebug() << "---- INTER CUBIC CUBIC" << i1 << i2; QPointF P1, Q1, P2, Q2; QLineF L1, L2; QRectF R1; QRectF R2; P1 = curve1.getVertex(i1-1); Q1 = curve1.getVertex(i1); P2 = curve2.getVertex(i2-1); Q2 = curve2.getVertex(i2); L1 = QLineF(P1, Q1); L2 = QLineF(P2, Q2); //qDebug() << "-------------------- "; R1.setTopLeft(P1); R1.setBottomRight(Q1); R2.setTopLeft(P2); R2.setBottomRight(Q2); QPointF intersectionPoint = QPointF(50.0, 50.0); // bogus point QPointF* cubicIntersection = &intersectionPoint; if ( R1.intersects(R2) || L2.intersect(L1, cubicIntersection) == QLineF::BoundedIntersection ) { //if (L2.intersect(L1, intersection) == QLineF::BoundedIntersection) { //qDebug() << " FOUND rectangle intersection "; //if (intersectionPoint != curve1.getVertex(i1-1) && intersectionPoint != curve1.getVertex(i1)) { // qDebug() << " it's not one of the points "; // find the cubic intersection int nSteps = 24; P1 = curve1.getVertex(i1-1); for(int i=1; i<=nSteps; i++) { qreal s = (i+0.0)/nSteps; Q1 = curve1.getPointOnCubic(i1, s); P2 = curve2.getVertex(i2-1); for(int j=1; j<=nSteps; j++) { qreal t = (j+0.0)/nSteps; Q2 = curve2.getPointOnCubic(i2, t); L1 = QLineF(P1, Q1); L2 = QLineF(P2, Q2); if (L2.intersect(L1, cubicIntersection) == QLineF::BoundedIntersection) { QPointF intersectionPoint = *cubicIntersection; if (intersectionPoint != curve1.getVertex(i1-1) && intersectionPoint != curve1.getVertex(i1)) { qreal fraction1 = eLength(intersectionPoint-Q1)/(0.0+eLength(Q1-P1)); qreal fraction2 = eLength(intersectionPoint-Q2)/(0.0+eLength(Q2-P2)); qreal t1 = (i - fraction1)/nSteps; qreal t2 = (j - fraction2)/nSteps; Intersection intersection; intersection.point = intersectionPoint; intersection.t1 = t1; intersection.t2 = t2; intersections.append( intersection ); result = true; //qDebug() << "FOUND cubic interesection " << intersectionPoint << i << j; } } P2 = Q2; } P1 = Q1; } } else { //return false; // approximation to speed up the calculation } //qDebug() << "------"; return result; }