comparison OrthancStone/Sources/Toolbox/Extent2D.cpp @ 1512:244ad1e4e76a

reorganization of folders
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 07 Jul 2020 16:21:02 +0200
parents Framework/Toolbox/Extent2D.cpp@2d8ab34c8c91
children 4fb8fdf03314
comparison
equal deleted inserted replaced
1511:9dfeee74c1e6 1512:244ad1e4e76a
1 /**
2 * Stone of Orthanc
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium
6 *
7 * This program is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU Affero General Public License
9 * as published by the Free Software Foundation, either version 3 of
10 * the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Affero General Public License for more details.
16 *
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 **/
20
21
22 #include "Extent2D.h"
23
24 #include <algorithm>
25 #include <cassert>
26 #include <limits>
27
28 namespace OrthancStone
29 {
30 Extent2D::Extent2D(double x1,
31 double y1,
32 double x2,
33 double y2) :
34 empty_(false),
35 x1_(x1),
36 y1_(y1),
37 x2_(x2),
38 y2_(y2)
39 {
40 if (x1_ > x2_)
41 {
42 std::swap(x1_, x2_);
43 }
44
45 if (y1_ > y2_)
46 {
47 std::swap(y1_, y2_);
48 }
49 }
50
51
52 void Extent2D::Reset()
53 {
54 empty_ = true;
55 x1_ = 0;
56 y1_ = 0;
57 x2_ = 0;
58 y2_ = 0;
59 }
60
61 void Extent2D::AddPoint(double x,
62 double y)
63 {
64 if (empty_)
65 {
66 x1_ = x;
67 y1_ = y;
68 x2_ = x;
69 y2_ = y;
70 empty_ = false;
71 }
72 else
73 {
74 x1_ = std::min(x1_, x);
75 y1_ = std::min(y1_, y);
76 x2_ = std::max(x2_, x);
77 y2_ = std::max(y2_, y);
78 }
79
80 assert(x1_ <= x2_ &&
81 y1_ <= y2_); // This is the invariant of the structure
82 }
83
84
85 void Extent2D::Union(const Extent2D& other)
86 {
87 if (other.empty_)
88 {
89 return;
90 }
91
92 if (empty_)
93 {
94 *this = other;
95 return;
96 }
97
98 assert(!empty_);
99
100 x1_ = std::min(x1_, other.x1_);
101 y1_ = std::min(y1_, other.y1_);
102 x2_ = std::max(x2_, other.x2_);
103 y2_ = std::max(y2_, other.y2_);
104
105 assert(x1_ <= x2_ &&
106 y1_ <= y2_); // This is the invariant of the structure
107 }
108
109
110 bool Extent2D::IsEmpty() const
111 {
112 if (empty_)
113 {
114 return true;
115 }
116 else
117 {
118 assert(x1_ <= x2_ &&
119 y1_ <= y2_);
120 return (x2_ <= x1_ + 10 * std::numeric_limits<double>::epsilon() ||
121 y2_ <= y1_ + 10 * std::numeric_limits<double>::epsilon());
122 }
123 }
124
125
126 bool Extent2D::Contains(double x,
127 double y) const
128 {
129 if (empty_)
130 {
131 return false;
132 }
133 else
134 {
135 return (x >= x1_ && x <= x2_ &&
136 y >= y1_ && y <= y2_);
137 }
138 }
139 }