/usr/include/ipegeo.h is in libipe-dev 7.2.7-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 | // -*- C++ -*-
// --------------------------------------------------------------------
// Geometric primitives
// --------------------------------------------------------------------
/*
This file is part of the extensible drawing editor Ipe.
Copyright (C) 1993-2016 Otfried Cheong
Ipe 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 3 of the License, or
(at your option) any later version.
As a special exception, you have permission to link Ipe with the
CGAL library and distribute executables, as long as you follow the
requirements of the Gnu General Public License in regard to all of
the software in the executable aside from CGAL.
Ipe 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 Ipe; if not, you can find it at
"http://www.gnu.org/copyleft/gpl.html", or write to the Free
Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#ifndef IPEGEO_H
#define IPEGEO_H
#include "ipebase.h"
#include <cmath>
// --------------------------------------------------------------------
#define IpePi 3.1415926535897932385
#define IpeTwoPi 6.2831853071795862
#define IpeHalfPi 1.5707963267948966
namespace ipe {
/*! \ingroup geo
Maximum of two values.
*/
template<class T>
inline T max(const T &lhs, const T &rhs)
{
return (lhs > rhs) ? lhs : rhs;
}
/*! \ingroup geo
Minimum of two values.
*/
template<class T>
inline T min(const T &lhs, const T &rhs)
{
return (lhs < rhs) ? lhs : rhs;
}
/*! \ingroup geo
Absolute value.
*/
inline double abs(double val)
{
return (val > 0) ? val : -val;
}
// --------------------------------------------------------------------
class Angle {
public:
//! Construct uninitialized angle.
inline explicit Angle() { /* nothing */ }
//! Construct an angle (in radians).
inline Angle(double alpha) : iAlpha(alpha) { }
//! Construct an angle in degrees.
inline static Angle Degrees(double alpha) {
return Angle(alpha * IpePi / 180.0); }
//! Return value (in radians).
inline operator double() const { return iAlpha; }
double degrees() const;
Angle normalize(double lowlimit);
bool liesBetween(Angle small, Angle large) const;
private:
double iAlpha;
};
// --------------------------------------------------------------------
class Vector {
public:
//! Uninitialized vector.
Vector() { /* no initialization */ }
explicit Vector(Angle alpha);
//! Construct a vector.
explicit Vector(double x0, double y0) : x(x0), y(y0) { }
//! Return square of Euclidean length
inline double sqLen() const;
double len() const;
Angle angle() const;
Vector normalized() const;
Vector orthogonal() const;
double factorize(Vector &unit) const;
bool snap(const Vector &mouse, Vector &pos, double &bound) const;
inline bool operator==(const Vector &rhs) const;
inline bool operator!=(const Vector &rhs) const;
inline void operator+=(const Vector &rhs);
inline void operator-=(const Vector &rhs);
inline void operator*=(double rhs);
inline Vector operator+(const Vector &rhs) const;
inline Vector operator-(const Vector &rhs) const;
inline Vector operator*(double rhs) const;
inline Vector operator-() const;
static Vector ZERO;
public:
double x; //!< Coordinates are public.
double y; //!< Coordinates are public.
};
/*! \relates Vector */
Stream &operator<<(Stream &stream, const Vector &rhs);
// --------------------------------------------------------------------
class Rect {
public:
//! Create empty rectangle.
explicit Rect() : iMin(1,0), iMax(-1,0) { }
//! Create rectangle containing just the point \a c.
explicit Rect(const Vector &c) : iMin(c), iMax(c) { }
explicit Rect(const Vector &c1, const Vector &c2);
//! Make rectangle empty.
void clear() { iMin.x = 1.0; iMax.x = -1.0; iMin.y = iMax.y = 0; }
//! True if rectangle is empty.
int isEmpty() const { return iMin.x > iMax.x; }
//! Return left side.
inline double left() const { return iMin.x; }
//! Return right side.
inline double right() const { return iMax.x; }
//! Return bottom side.
inline double bottom() const { return iMin.y; }
//! Return top side.
inline double top() const { return iMax.y; }
//! Return top right corner.
inline Vector topRight() const { return iMax; }
//! Return bottom left corner.
inline Vector bottomLeft() const { return iMin; }
//! Return top left corner.
inline Vector topLeft() const { return Vector(iMin.x, iMax.y); }
//! Return bottom right corner.
inline Vector bottomRight() const { return Vector(iMax.x, iMin.y); }
//! Return center of rectangle.
inline Vector center() const { return (iMin + iMax) * 0.5; }
//! Return width.
double width() const { return iMax.x - iMin.x; }
//! Return height.
double height() const { return iMax.y - iMin.y; }
void addPoint(const Vector &rhs);
void addRect(const Rect &rhs);
void clipTo(const Rect &rhs);
bool contains(const Vector &rhs) const;
bool contains(const Rect &rhs) const;
bool certainClearance(const Vector &v, double bound) const;
bool intersects(const Rect &rhs) const;
private:
Vector iMin; //!< Lower-left corner.
Vector iMax; //!< Top-right corner.
};
/*! \relates Rect */
Stream &operator<<(Stream &stream, const Rect &rhs);
// --------------------------------------------------------------------
class Line {
public:
//! Create default line (x-axis).
explicit Line() : iP(0.0, 0.0), iDir(1.0, 0.0) { }
explicit Line(const Vector &p, const Vector &dir);
static Line through(const Vector &p, const Vector &q);
double side(const Vector &p) const;
inline Vector normal() const;
double distance(const Vector &v) const;
bool intersects(const Line &line, Vector &pt);
Vector project(const Vector &v) const;
inline Vector dir() const;
public:
//! Point on the line.
Vector iP;
private:
Vector iDir; // unit vector
};
// --------------------------------------------------------------------
class Segment {
public:
//! Create uninitialized segment
Segment() { /* nothing */ }
explicit Segment(const Vector &p, const Vector &q) : iP(p), iQ(q) { }
inline Line line() const;
double distance(const Vector &v, double bound) const;
double distance(const Vector &v) const;
bool project(const Vector &v, Vector &projection) const;
bool intersects(const Segment &seg, Vector &pt) const;
bool intersects(const Line &l, Vector &pt) const;
bool snap(const Vector &mouse, Vector &pos, double &bound) const;
public:
//! First endpoint.
Vector iP;
//! Second endpoint.
Vector iQ;
};
// --------------------------------------------------------------------
class Bezier {
public:
//! Default constructor, uninitialized curve.
inline Bezier() { /* nothing */ }
inline Bezier(const Vector &p0, const Vector &p1,
const Vector &p2, const Vector &p3);
Vector point(double t) const;
Vector tangent(double t) const;
double distance(const Vector &v, double bound);
bool straight(double precision) const;
void subdivide(Bezier &l, Bezier &r) const;
void approximate(double precision, std::vector<Vector> &result) const;
Rect bbox() const;
bool snap(const Vector &v, double &t, Vector &pos, double &bound) const;
static Bezier quadBezier(const Vector &p0, const Vector &p1,
const Vector &p2);
static void oldSpline(int n, const Vector *v,
std::vector<Bezier> &result);
static void spline(int n, const Vector *v,
std::vector<Bezier> &result);
static void closedSpline(int n, const Vector *v,
std::vector<Bezier> &result);
void intersect(const Line &l, std::vector<Vector> &result) const;
void intersect(const Segment &l, std::vector<Vector> &result) const;
void intersect(const Bezier &b, std::vector<Vector> &result) const;
public:
Vector iV[4];
};
// --------------------------------------------------------------------
class Linear {
public:
Linear();
explicit Linear(Angle angle);
inline explicit Linear(double m11, double m21, double m12, double m22);
explicit Linear(String str);
Linear inverse() const;
inline bool isIdentity() const;
inline Vector operator*(const Vector &rhs) const;
inline bool operator==(const Linear &rhs) const;
inline double determinant() const;
public:
double a[4];
};
/*! \relates Linear */
Stream &operator<<(Stream &stream, const Linear &rhs);
// --------------------------------------------------------------------
class Matrix {
public:
Matrix();
inline Matrix(const Linear &linear);
inline explicit Matrix(const Linear &linear, const Vector &t);
inline explicit Matrix(double m11, double m21, double m12, double m22,
double t1, double t2);
inline explicit Matrix(const Vector &v);
explicit Matrix(String str);
Matrix inverse() const;
inline Vector operator*(const Vector &rhs) const;
inline Bezier operator*(const Bezier &rhs) const;
inline Vector translation() const;
inline Linear linear() const;
inline double determinant() const;
inline bool isIdentity() const;
inline bool operator==(const Matrix &rhs) const;
public:
double a[6];
};
/*! \relates Matrix */
Stream &operator<<(Stream &stream, const Matrix &rhs);
// --------------------------------------------------------------------
class Arc {
public:
inline Arc();
inline Arc(const Matrix &m, Angle alpha, Angle beta);
inline Arc(const Matrix &m);
Arc(const Matrix &m0, const Vector &begp, const Vector &endp);
inline bool isEllipse() const;
double distance(const Vector &v, double bound) const;
double distance(const Vector &v, double bound,
Vector &pos, Angle &angle) const;
Rect bbox() const;
inline Vector beginp() const;
inline Vector endp() const;
void intersect(const Line &l, std::vector<Vector> &result) const;
void intersect(const Segment &s, std::vector<Vector> &result) const;
void intersect(const Arc &a, std::vector<Vector> &result) const;
void intersect(const Bezier &b, std::vector<Vector> &result) const;
private:
void subdivide(Arc &l, Arc &r) const;
bool straight(const double precision) const;
public:
Matrix iM;
Angle iAlpha;
Angle iBeta;
};
// --------------------------------------------------------------------
//! Return square of vector's length.
inline double Vector::sqLen() const
{
return (x * x + y * y);
}
//! Equality.
inline bool Vector::operator==(const Vector &rhs) const
{
return x == rhs.x && y == rhs.y;
}
//! Inequality.
inline bool Vector::operator!=(const Vector &rhs) const
{
return x != rhs.x || y != rhs.y;
}
//! Vector-addition.
inline void Vector::operator+=(const Vector &rhs)
{
x += rhs.x; y += rhs.y;
}
//! Vector-subtraction.
inline void Vector::operator-=(const Vector &rhs)
{
x -= rhs.x; y -= rhs.y;
}
//! Multiply vector by scalar.
inline void Vector::operator*=(double rhs)
{
x *= rhs; y *= rhs;
}
//! Vector-addition.
inline Vector Vector::operator+(const Vector &rhs) const
{
Vector result = *this; result += rhs; return result;
}
//! Vector-subtraction.
inline Vector Vector::operator-(const Vector &rhs) const
{
Vector result = *this; result -= rhs; return result;
}
//! Vector * scalar.
inline Vector Vector::operator*(double rhs) const
{
Vector result = *this; result *= rhs; return result;
}
//! Scalar * vector. \relates Vector
inline Vector operator*(double lhs, const Vector &rhs)
{
return Vector(lhs * rhs.x, lhs * rhs.y);
}
//! Dotproduct of two vectors. \relates Vector
inline double dot(const Vector &lhs, const Vector &rhs)
{
return lhs.x * rhs.x + lhs.y * rhs.y;
}
//! Return a normal vector pointing to the left of the directed line.
inline Vector Line::normal() const
{
return Vector(-iDir.y, iDir.x);
}
//! Return direction of line.
inline Vector Line::dir() const
{
return iDir;
}
//! Return directed line supporting the segment.
inline Line Segment::line() const
{
return Line(iP, (iQ - iP).normalized());
}
//! Unary minus for Vector.
inline Vector Vector::operator-() const
{
return -1 * *this;
}
//! Constructor with four control points.
inline Bezier::Bezier(const Vector &p0, const Vector &p1,
const Vector &p2, const Vector &p3)
{
iV[0] = p0; iV[1] = p1; iV[2] = p2; iV[3] = p3;
}
//! Transform Bezier spline. \relates Matrix
inline Bezier Matrix::operator*(const Bezier &rhs) const
{
return Bezier(*this * rhs.iV[0], *this * rhs.iV[1],
*this * rhs.iV[2], *this * rhs.iV[3]);
};
// --------------------------------------------------------------------
//! Construct unit circle.
inline Arc::Arc() : iAlpha(0.0), iBeta(IpeTwoPi)
{
// nothing
}
//! Construct with given parameters.
inline Arc::Arc(const Matrix &m, Angle alpha, Angle beta)
: iM(m), iAlpha(alpha), iBeta(beta)
{
// nothing
}
//! Construct an ellipse
inline Arc::Arc(const Matrix &m)
: iM(m), iAlpha(0.0), iBeta(IpeTwoPi)
{
// nothing
}
//! Is this an entire ellipse?
inline bool Arc::isEllipse() const
{
return iAlpha == 0.0 && iBeta == IpeTwoPi;
}
//! Return begin point of arc.
inline Vector Arc::beginp() const
{
return iM * Vector(Angle(iAlpha));
}
//! Return end point of arc.
inline Vector Arc::endp() const
{
return iM * Vector(Angle(iBeta));
}
// --------------------------------------------------------------------
//! Create identity matrix.
inline Linear::Linear()
{
a[0] = a[3] = 1.0;
a[1] = a[2] = 0.0;
}
//! Create linear matrix with given coefficients.
inline Linear::Linear(double m11, double m21, double m12, double m22)
{
a[0] = m11; a[1] = m21; a[2] = m12; a[3] = m22;
}
//! Linear matrix times vector. \relates Linear
inline Vector Linear::operator*(const Vector &rhs) const
{
return Vector(a[0] * rhs.x + a[2] * rhs.y,
a[1] * rhs.x + a[3] * rhs.y);
};
//! Is this the identity matrix?
inline bool Linear::isIdentity() const
{
return (a[0] == 1.0 && a[1] == 0.0 &&
a[2] == 0.0 && a[3] == 1.0);
}
//! Linear matrix multiplication. \relates Linear
inline Linear operator*(const Linear &lhs, const Linear &rhs)
{
Linear m;
m.a[0] = lhs.a[0] * rhs.a[0] + lhs.a[2] * rhs.a[1];
m.a[1] = lhs.a[1] * rhs.a[0] + lhs.a[3] * rhs.a[1];
m.a[2] = lhs.a[0] * rhs.a[2] + lhs.a[2] * rhs.a[3];
m.a[3] = lhs.a[1] * rhs.a[2] + lhs.a[3] * rhs.a[3];
return m;
}
//! Check for equality of two linear matrices.
inline bool Linear::operator==(const Linear &rhs) const
{
return (a[0] == rhs.a[0] && a[1] == rhs.a[1] &&
a[2] == rhs.a[2] && a[3] == rhs.a[3]);
}
//! Return determinant of a linear matrix.
inline double Linear::determinant() const
{
return (a[0] * a[3] - a[1] * a[2]);
}
// --------------------------------------------------------------------
//! Create matrix with given coefficients.
inline Matrix::Matrix(double m11, double m21, double m12, double m22,
double t1, double t2)
{
a[0] = m11; a[1] = m21; a[2] = m12; a[3] = m22;
a[4] = t1; a[5] = t2;
}
//! Create linear matrix.
inline Matrix::Matrix(const Linear &linear)
{
a[0] = linear.a[0]; a[1] = linear.a[1];
a[2] = linear.a[2]; a[3] = linear.a[3];
a[4] = a[5] = 0.0;
}
inline Matrix::Matrix(const Linear &linear, const Vector &t)
{
a[0] = linear.a[0]; a[1] = linear.a[1];
a[2] = linear.a[2]; a[3] = linear.a[3];
a[4] = t.x; a[5] = t.y;
}
//! Matrix times vector. \relates Matrix
inline Vector Matrix::operator*(const Vector &rhs) const
{
return Vector(a[0] * rhs.x + a[2] * rhs.y + a[4],
a[1] * rhs.x + a[3] * rhs.y + a[5]);
};
//! Is this the identity matrix?
inline bool Matrix::isIdentity() const
{
return (a[0] == 1.0 && a[1] == 0.0 &&
a[2] == 0.0 && a[3] == 1.0 &&
a[4] == 0.0 && a[5] == 0.0);
}
//! Create identity matrix.
inline Matrix::Matrix()
{
a[0] = a[3] = 1.0;
a[1] = a[2] = a[4] = a[5] = 0.0;
}
//! Matrix multiplication. \relates Matrix
inline Matrix operator*(const Matrix &lhs, const Matrix &rhs)
{
Matrix m;
m.a[0] = lhs.a[0] * rhs.a[0] + lhs.a[2] * rhs.a[1];
m.a[1] = lhs.a[1] * rhs.a[0] + lhs.a[3] * rhs.a[1];
m.a[2] = lhs.a[0] * rhs.a[2] + lhs.a[2] * rhs.a[3];
m.a[3] = lhs.a[1] * rhs.a[2] + lhs.a[3] * rhs.a[3];
m.a[4] = lhs.a[0] * rhs.a[4] + lhs.a[2] * rhs.a[5] + lhs.a[4];
m.a[5] = lhs.a[1] * rhs.a[4] + lhs.a[3] * rhs.a[5] + lhs.a[5];
return m;
}
//! Create translation matrix.
inline Matrix::Matrix(const Vector &v)
{
a[0] = a[3] = 1.0;
a[1] = a[2] = 0.0;
a[4] = v.x;
a[5] = v.y;
}
//! Return translation component.
inline Vector Matrix::translation() const
{
return Vector(a[4], a[5]);
}
//! Return determinant of the matrix.
inline double Matrix::determinant() const
{
return a[0]*a[3]-a[1]*a[2];
}
//! Check for equality of two matrices.
inline bool Matrix::operator==(const Matrix &rhs) const
{
return (a[0] == rhs.a[0] && a[1] == rhs.a[1] && a[2] == rhs.a[2] &&
a[3] == rhs.a[3] && a[4] == rhs.a[4] && a[5] == rhs.a[5]);
}
//! Return linear transformation component of this affine transformation.
inline Linear Matrix::linear() const
{
return Linear(a[0], a[1], a[2], a[3]);
}
//! Transform arc. \relates Matrix
// must be here because it uses Matrix multiplication above
inline Arc operator*(const Matrix &lhs, const Arc &rhs)
{
return Arc(lhs * rhs.iM, rhs.iAlpha, rhs.iBeta);
}
} // namespace
// --------------------------------------------------------------------
#endif
|