17 double v1Mag = qSqrt (v1.x() * v1.x() + v1.y() * v1.y());
18 double v2Mag = qSqrt (v2.x() * v2.x() + v2.y() * v2.y());
21 if ((v1Mag > 0) || (v2Mag > 0)) {
23 double cosArg = (v1.x() * v2.x() + v1.y() * v2.y()) / (v1Mag * v2Mag);
24 cosArg = qMin (qMax (cosArg, -1.0), 1.0);
25 angle = qAcos (cosArg);
38 double dotX = xBasisX * x + xBasisY * y;
39 double dotY = yBasisX * x + yBasisY * y;
40 return qAtan2 (dotY, dotX);
46 double angleFrom = qAtan2 (vFrom.y(), vFrom.x());
47 double angleTo = qAtan2 (vTo.y() , vTo.x());
51 double angleSeparation = angleTo - angleFrom;
53 while (angleSeparation < -1.0 * M_PI) {
54 angleSeparation += 2.0 * M_PI;
56 while (angleSeparation > M_PI) {
57 angleSeparation -= 2.0 * M_PI;
60 return angleSeparation;
63double dot (
const QPointF &vec1,
66 return vec1.x() * vec2.x() +
95 double xT = (xTL + xTR) / 2.0;
96 double yT = (yTL + yTR) / 2.0;
97 double xR = (xTR + xBR) / 2.0;
98 double yR = (yTR + yBR) / 2.0;
129 double m00 = xT * xT;
130 double m01 = yT * yT;
131 double m02 = 2.0 * xT * yT;
132 double m10 = xR * xR;
133 double m11 = yR * yR;
134 double m12 = 2.0 * xR * yR;
141 if (qAbs (yTR - yTL) * qAbs (xTR - xBR) < qAbs (yTR - yBR) * qAbs (xTR - xTL)) {
143 double mT = (yTR - yTL) / (xTR - xTL);
150 double mR = (yTR - yBR) / (xTR - xBR);
157 QTransform denominator (m00, m01, m02,
160 QTransform numeratorA (-1.0, m01, m02,
163 QTransform numeratorB (m00, -1.0, m02,
166 QTransform numeratorC (m00, m01, -1.0,
169 double A = numeratorA.determinant () / denominator.determinant ();
170 double B = numeratorB.determinant () / denominator.determinant ();
171 double C = numeratorC.determinant () / denominator.determinant ();
176 double numerator = 4.0 * F * C * C - 4.0 * A * B * F;
177 double denominatorMinus = 2.0 * (A * B - C * C) * (A + B + qSqrt ((B - A) * (B - A) + 4 * C * C));
178 double denominatorPlus = 2.0 * (A * B - C * C) * (A + B - qSqrt ((B - A) * (B - A) + 4 * C * C));
179 aAligned = qSqrt (numerator / denominatorMinus);
180 bAligned = qSqrt (numerator / denominatorPlus);
182 if (qAbs (2.0 * C) > 10000.0 * qAbs (A - B)) {
183 angleRadians = M_PI / 2.0;
185 angleRadians = 0.5 * qAtan (2 * C / (A - B));
188 LOG4CPP_DEBUG_S ((*
mainCat)) <<
"ellipseFromParallelogram TL=(" << xTL <<
", " << yTL <<
") TR=(" << xTR <<
", " << yTR
189 <<
") BR=(" << xBR <<
", " << yBR <<
") angleDegrees=" << qRadiansToDegrees (angleRadians)
190 <<
" a=" << aAligned <<
" b=" << bAligned;
195 return qSqrt (vec.x() * vec.x() + vec.y() * vec.y());
202 return QPointF (vec.x() / vecMag,
211 switch (image.depth())
225 if (image1Bit.format () == QImage::Format_MonoLSB) {
226 bit = *(image1Bit.scanLine (y) + (x >> 3)) & (1 << (x & 7));
228 bit = *(image1Bit.scanLine (y) + (x >> 3)) & (1 << (7 - (x & 7)));
231 int tableIndex = ((bit == 0) ? 0 : 1);
232 return image1Bit.color(tableIndex);
237 int tableIndex = *(image8Bit.scanLine(y) + x);
238 return image8Bit.color(tableIndex);
247 const QRgb *p =
reinterpret_cast<const QRgb *
> (image32Bit.scanLine(y)) + x;
259 double *projectedDistanceOutsideLine,
260 double *distanceToLine)
263 if (qAbs (yStart - yStop) > qAbs (xStart - xStop)) {
266 double slope = (xStop - xStart) / (yStart - yStop);
267 double yintercept = yToProject - slope * xToProject;
270 s = (slope * xStart + yintercept - yStart) /
271 (yStop - yStart + slope * (xStart - xStop));
276 double slope = (yStop - yStart) / (xStart - xStop);
277 double xintercept = xToProject - slope * yToProject;
280 s = (slope * yStart + xintercept - xStart) /
281 (xStop - xStart + slope * (yStart - yStop));
285 *xProjection = (1.0 - s) * xStart + s * xStop;
286 *yProjection = (1.0 - s) * yStart + s * yStop;
290 *projectedDistanceOutsideLine = qSqrt ((*xProjection - xStart) * (*xProjection - xStart) +
291 (*yProjection - yStart) * (*yProjection - yStart));
292 *distanceToLine = qSqrt ((xToProject - xStart) * (xToProject - xStart) +
293 (yToProject - yStart) * (yToProject - yStart));
296 *xProjection = xStart;
297 *yProjection = yStart;
301 *projectedDistanceOutsideLine = qSqrt ((*xProjection - xStop) * (*xProjection - xStop) +
302 (*yProjection - yStop) * (*yProjection - yStop));
303 *distanceToLine = qSqrt ((xToProject - xStop) * (xToProject - xStop) +
304 (yToProject - yStop) * (yToProject - yStop));
307 *xProjection = xStop;
308 *yProjection = yStop;
312 *distanceToLine = qSqrt ((xToProject - *xProjection) * (xToProject - *xProjection) +
313 (yToProject - *yProjection) * (yToProject - *yProjection));
316 *projectedDistanceOutsideLine = 0.0;
323 switch (image.depth())
339 for (
int index = 0; index < image1Bit.colorCount(); index++) {
340 if (q == image1Bit.color(index))
342 if (image1Bit.format () == QImage::Format_MonoLSB)
344 *(image1Bit.scanLine (y) + (x >> 3)) &= ~(1 << (x & 7));
346 *(image1Bit.scanLine (y) + (x >> 3)) |= index << (x & 7);
350 *(image1Bit.scanLine (y) + (x >> 3)) &= ~(1 << (7 - (x & 7)));
352 *(image1Bit.scanLine (y) + (x >> 3)) |= index << (7 - (x & 7));
361 for (
int index = 0; index < image8Bit.colorCount(); index++) {
362 if (q == image8Bit.color(index))
364 *(image8Bit.scanLine(y) + x) =
static_cast<uchar
> (index);
372 int* p = (
int *)image32Bit.scanLine(y) + x;
log4cpp::Category * mainCat
#define LOG4CPP_DEBUG_S(logger)
QRgb pixelRGB8(const QImage &image8Bit, int x, int y)
Get pixel method for 8 bit depth.
double angleFromVectorToVector(const QPointF &vFrom, const QPointF &vTo)
Angle between two vectors. Direction is positive when rotation is about +z vector,...
QRgb pixelRGB32(const QImage &image32Bit, int x, int y)
Get pixel method for 32 bit depth.
double dot(const QPointF &vec1, const QPointF &vec2)
Vector dot product.
QRgb pixelRGB(const QImage &image, int x, int y)
Get pixel method for any bit depth.
void setPixelRGB8(QImage &image8Bit, int x, int y, QRgb q)
Set pixel method for 8 bit depth.
double magnitude(const QPointF &vec)
Norm of vector.
double angleFromBasisVectors(double xBasisX, double xBasisY, double yBasisX, double yBasisY, double x, double y)
Four quadrant angle to specified vector, given two orthogonal basis vectors corresonding to +x and +y...
double angleBetweenVectors(const QPointF &v1, const QPointF &v2)
Angle between two vectors. Direction is unimportant, so result is between 0 to pi radians.
void setPixelRGB(QImage &image, int x, int y, QRgb q)
Set pixel method for any bit depth.
void setPixelRGB1(QImage &image1Bit, int x, int y, QRgb q)
Set pixel method for one bit depth.
void projectPointOntoLine(double xToProject, double yToProject, double xStart, double yStart, double xStop, double yStop, double *xProjection, double *yProjection, double *projectedDistanceOutsideLine, double *distanceToLine)
Find the projection of a point onto a line segment such that the line through the point and its proje...
void ellipseFromParallelogram(double xTL, double yTL, double xTR, double yTR, double xBR, double yBR, double &angleRadians, double &aAligned, double &bAligned)
Calculate ellipse parameters that is incribed in a parallelogram centered at the origin,...
QPointF normalize(const QPointF &vec)
Return normalized vector.
QRgb pixelRGB1(const QImage &image1Bit, int x, int y)
Get pixel method for one bit depth.
void setPixelRGB32(QImage &image32Bit, int x, int y, QRgb q)
Set pixel method for 32 bit depth.