~ubuntu-branches/ubuntu/trusty/scribus-ng/trusty

« back to all changes in this revision

Viewing changes to scribus/plugins/tools/2geomtools/lib2geom/point.cpp

  • Committer: Package Import Robot
  • Author(s): Oleksandr Moskalenko
  • Date: 2012-02-15 15:57:12 UTC
  • mfrom: (4.2.10 sid)
  • Revision ID: package-import@ubuntu.com-20120215155712-biimoc8o875jht80
Tags: 1.4.0.dfsg+r17300-1
* Prepare a dummy transitional package to converge on the 1.4.0 release.
* debian/NEWS: update the news.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#include "point.h"
2
 
#include <assert.h>
3
 
#include "coord.h"
4
 
#include "isnan.h" //temporary fix for isnan()
5
 
#include "matrix.h"
6
 
#include <limits>
7
 
 
8
 
 
9
 
namespace Geom {
10
 
 
11
 
/** Scales this vector to make it a unit vector (within rounding error).
12
 
 *
13
 
 *  The current version tries to handle infinite coordinates gracefully,
14
 
 *  but it's not clear that any callers need that.
15
 
 *
16
 
 *  \pre \f$this \neq (0, 0)\f$
17
 
 *  \pre Neither component is NaN.
18
 
 *  \post \f$-\epsilon<\left|this\right|-1<\epsilon\f$
19
 
 */
20
 
void Point::normalize() {
21
 
    double len = hypot(_pt[0], _pt[1]);
22
 
    if(len == 0) return;
23
 
    if(is_nan(len)) return;
24
 
        static double const inf = std::numeric_limits<double>::infinity();
25
 
    if(len != inf) {
26
 
        *this /= len;
27
 
    } else {
28
 
        unsigned n_inf_coords = 0;
29
 
        /* Delay updating pt in case neither coord is infinite. */
30
 
        Point tmp;
31
 
        for ( unsigned i = 0 ; i < 2 ; ++i ) {
32
 
            if ( _pt[i] == inf ) {
33
 
                ++n_inf_coords;
34
 
                tmp[i] = 1.0;
35
 
            } else if ( _pt[i] == -inf ) {
36
 
                ++n_inf_coords;
37
 
                tmp[i] = -1.0;
38
 
            } else {
39
 
                tmp[i] = 0.0;
40
 
            }
41
 
        }
42
 
        switch (n_inf_coords) {
43
 
        case 0: {
44
 
            /* Can happen if both coords are near +/-DBL_MAX. */
45
 
            *this /= 4.0;
46
 
            len = hypot(_pt[0], _pt[1]);
47
 
            assert(len != inf);
48
 
            *this /= len;
49
 
            break;
50
 
        }
51
 
        case 1: {
52
 
            *this = tmp;
53
 
            break;
54
 
        }
55
 
        case 2: {
56
 
            *this = tmp * sqrt(0.5);
57
 
            break;
58
 
        }
59
 
        }
60
 
    }
61
 
}
62
 
 
63
 
/** Compute the L1 norm, or manhattan distance, of \a p. */
64
 
Coord L1(Point const &p) {
65
 
    Coord d = 0;
66
 
    for ( int i = 0 ; i < 2 ; i++ ) {
67
 
        d += fabs(p[i]);
68
 
    }
69
 
    return d;
70
 
}
71
 
 
72
 
/** Compute the L infinity, or maximum, norm of \a p. */
73
 
Coord LInfty(Point const &p) {
74
 
    Coord const a(fabs(p[0]));
75
 
    Coord const b(fabs(p[1]));
76
 
    return ( a < b || is_nan(b)
77
 
             ? b
78
 
             : a );
79
 
}
80
 
 
81
 
/** Returns true iff p is a zero vector, i.e.\ Point(0, 0).
82
 
 *
83
 
 *  (NaN is considered non-zero.)
84
 
 */
85
 
bool
86
 
is_zero(Point const &p)
87
 
{
88
 
    return ( p[0] == 0 &&
89
 
             p[1] == 0   );
90
 
}
91
 
 
92
 
bool
93
 
is_unit_vector(Point const &p)
94
 
{
95
 
    return fabs(1.0 - L2(p)) <= 1e-4;
96
 
    /* The tolerance of 1e-4 is somewhat arbitrary.  Point::normalize is believed to return
97
 
       points well within this tolerance.  I'm not aware of any callers that want a small
98
 
       tolerance; most callers would be ok with a tolerance of 0.25. */
99
 
}
100
 
 
101
 
Coord atan2(Point const p) {
102
 
    return std::atan2(p[Y], p[X]);
103
 
}
104
 
 
105
 
/** compute the angle turning from a to b.  This should give \f$\pi/2\f$ for angle_between(a, rot90(a));
106
 
 * This works by projecting b onto the basis defined by a, rot90(a)
107
 
 */
108
 
Coord angle_between(Point const a, Point const b) {
109
 
    return std::atan2(cross(b,a), dot(b,a));
110
 
}
111
 
 
112
 
 
113
 
 
114
 
/** Returns a version of \a a scaled to be a unit vector (within rounding error).
115
 
 *
116
 
 *  The current version tries to handle infinite coordinates gracefully,
117
 
 *  but it's not clear that any callers need that.
118
 
 *
119
 
 *  \pre a != Point(0, 0).
120
 
 *  \pre Neither coordinate is NaN.
121
 
 *  \post L2(ret) very near 1.0.
122
 
 */
123
 
Point unit_vector(Point const &a)
124
 
{
125
 
    Point ret(a);
126
 
    ret.normalize();
127
 
    return ret;
128
 
}
129
 
 
130
 
Point abs(Point const &b)
131
 
{
132
 
    Point ret;
133
 
    for ( int i = 0 ; i < 2 ; i++ ) {
134
 
        ret[i] = fabs(b[i]);
135
 
    }
136
 
    return ret;
137
 
}
138
 
 
139
 
Point operator*(Point const &v, Matrix const &m) {
140
 
    Point ret;
141
 
    for(int i = 0; i < 2; i++) {
142
 
        ret[i] = v[X] * m[i] + v[Y] * m[i + 2] + m[i + 4];
143
 
    }
144
 
    return ret;
145
 
}
146
 
 
147
 
Point operator/(Point const &p, Matrix const &m) { return p * m.inverse(); }
148
 
 
149
 
Point &Point::operator*=(Matrix const &m)
150
 
{
151
 
    *this = *this * m;
152
 
    return *this;
153
 
}
154
 
 
155
 
}  //Namespace Geom
156
 
 
157
 
/*
158
 
  Local Variables:
159
 
  mode:c++
160
 
  c-file-style:"stroustrup"
161
 
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
162
 
  indent-tabs-mode:nil
163
 
  fill-column:99
164
 
  End:
165
 
*/
166
 
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :