summaryrefslogtreecommitdiffstats
path: root/src/3rdparty/2geom/tests/circle-test.cpp
blob: 5ff649344f2bac8f842a64de5753ad3e314c70cb (plain)
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
/** @file
 * @brief Unit tests for Circle and related functions.
 * Uses the Google Testing Framework
 *//*
 * Authors:
 *   Krzysztof Kosiński <tweenk.pl@gmail.com>
 * 
 * Copyright 2015 Authors
 *
 * This library is free software; you can redistribute it and/or
 * modify it either under the terms of the GNU Lesser General Public
 * License version 2.1 as published by the Free Software Foundation
 * (the "LGPL") or, at your option, under the terms of the Mozilla
 * Public License Version 1.1 (the "MPL"). If you do not alter this
 * notice, a recipient may use your version of this file under either
 * the MPL or the LGPL.
 *
 * You should have received a copy of the LGPL along with this library
 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 * You should have received a copy of the MPL along with this library
 * in the file COPYING-MPL-1.1
 *
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
 * the specific language governing rights and limitations.
 */

#include "testing.h"
#include <2geom/circle.h>
#include <2geom/line.h>

using namespace Geom;

TEST(CircleTest, Equality) {
    Circle a(4, 5, 6);
    Circle b(Point(4, 5), 6);
    Circle c(4.00000001, 5, 6);

    EXPECT_EQ(a, b);
    EXPECT_NE(a, c);
    EXPECT_NE(b, c);
}

TEST(CircleTest, Nearness) {
    Circle a(4, 5, 6);
    Circle b(4.000007, 5, 6);
    Circle c(4, 5, 6.000007);
    Circle d(4.000007, 5, 6.000007);
    Circle e(4, 5, 7);

    EXPECT_TRUE(are_near(a, b, 1e-5));
    EXPECT_TRUE(are_near(a, c, 1e-5));
    EXPECT_TRUE(are_near(c, d, 1e-5));
    EXPECT_FALSE(are_near(a, d, 1e-5));
    EXPECT_FALSE(are_near(a, e, 1e-2));
    EXPECT_FALSE(are_near(b, e, 1e-2));
    EXPECT_FALSE(are_near(c, e, 1e-2));
}

TEST(CircleTest, UnitCircleTransform) {
    Circle c(17, 23, 22);

    Point q = c.pointAt(M_PI/2);
    Point p = Point(0, 1) * c.unitCircleTransform();
    Point r = q * c.inverseUnitCircleTransform();

    EXPECT_FLOAT_EQ(p[X], q[X]);
    EXPECT_FLOAT_EQ(p[Y], q[Y]);
    EXPECT_FLOAT_EQ(r[X], 0);
    EXPECT_FLOAT_EQ(r[Y], 1);
}

TEST(CircleTest, Coefficients) {
    Circle circ(5, 12, 87), circ2;

    Coord a, b, c, d;
    circ.coefficients(a, b, c, d);
    circ2.setCoefficients(a, b, c, d);

    EXPECT_TRUE(are_near(circ, circ2, 1e-15));

    for (unsigned i = 0; i < 100; ++i) {
        Coord t = -5 + 0.111 * i;
        Point p = circ.pointAt(t);
        Coord eqres = a * p[X]*p[X] + a*p[Y]*p[Y] + b*p[X] + c*p[Y] + d;
        EXPECT_NEAR(eqres, 0, 1e-11);
    }
}

TEST(CircleTest, CircleIntersection) {
    Circle a(5, 5, 5), b(15, 5, 5), c(10, 10, 6), d(-5, 5, 2);
    std::vector<ShapeIntersection> r1, r2, r3;

    r1 = a.intersect(b);
    ASSERT_EQ(r1.size(), 1u);
    EXPECT_EQ(r1[0].point(), Point(10,5));
    EXPECT_intersections_valid(a, b, r1, 1e-15);

    r2 = a.intersect(c);
    EXPECT_EQ(r2.size(), 2u);
    EXPECT_intersections_valid(a, c, r2, 1e-15);

    r3 = b.intersect(c);
    EXPECT_EQ(r3.size(), 2u);
    EXPECT_intersections_valid(b, c, r3, 4e-15);

    EXPECT_TRUE(a.intersect(d).empty());
    EXPECT_TRUE(b.intersect(d).empty());
    EXPECT_TRUE(c.intersect(d).empty());
}

TEST(CircleTest, LineIntersection) {
    Circle c(5, 5, 10);
    Line l1(Point(-5, -20), Point(-5, 20));
    Line l2(Point(0, 0), Point(10, 2.3));
    Line l3(Point(20, -20), Point(0, -20));

    EXPECT_TRUE(c.intersects(l1));
    EXPECT_TRUE(c.intersects(l2));
    EXPECT_FALSE(c.intersects(l3));

    std::vector<ShapeIntersection> r1, r2, r3;

    r1 = c.intersect(l1);
    ASSERT_EQ(r1.size(), 1u);
    EXPECT_EQ(r1[0].point(), Point(-5, 5));
    EXPECT_intersections_valid(c, l1, r1, 1e-15);

    r2 = c.intersect(l2);
    EXPECT_EQ(r2.size(), 2u);
    EXPECT_intersections_valid(c, l2, r2, 1e-14);

    r3 = c.intersect(l3);
    EXPECT_TRUE(r3.empty());
}