Mercurial > hg > orthanc-stone
comparison Framework/Toolbox/Extent2D.cpp @ 111:7665ccbf33db wasm
rename Extent as Extent2D
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 14 Jun 2017 15:54:06 +0200 |
parents | Framework/Toolbox/Extent.cpp@53bd9277b025 |
children | 2eca030792aa |
comparison
equal
deleted
inserted
replaced
110:53025eecbc95 | 111:7665ccbf33db |
---|---|
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 Osimis, 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 | |
27 namespace OrthancStone | |
28 { | |
29 Extent2D::Extent2D(double x1, | |
30 double y1, | |
31 double x2, | |
32 double y2) : | |
33 empty_(false), | |
34 x1_(x1), | |
35 y1_(y1), | |
36 x2_(x2), | |
37 y2_(y2) | |
38 { | |
39 if (x1_ > x2_) | |
40 { | |
41 std::swap(x1_, x2_); | |
42 } | |
43 | |
44 if (y1_ > y2_) | |
45 { | |
46 std::swap(y1_, y2_); | |
47 } | |
48 } | |
49 | |
50 | |
51 void Extent2D::Reset() | |
52 { | |
53 empty_ = true; | |
54 x1_ = 0; | |
55 y1_ = 0; | |
56 x2_ = 0; | |
57 y2_ = 0; | |
58 } | |
59 | |
60 void Extent2D::AddPoint(double x, | |
61 double y) | |
62 { | |
63 if (empty_) | |
64 { | |
65 x1_ = x; | |
66 y1_ = y; | |
67 x2_ = x; | |
68 y2_ = y; | |
69 empty_ = false; | |
70 } | |
71 else | |
72 { | |
73 x1_ = std::min(x1_, x); | |
74 y1_ = std::min(y1_, y); | |
75 x2_ = std::max(x2_, x); | |
76 y2_ = std::max(y2_, y); | |
77 } | |
78 | |
79 assert(x1_ <= x2_ && | |
80 y1_ <= y2_); // This is the invariant of the structure | |
81 } | |
82 | |
83 | |
84 void Extent2D::Union(const Extent2D& other) | |
85 { | |
86 if (other.empty_) | |
87 { | |
88 return; | |
89 } | |
90 | |
91 if (empty_) | |
92 { | |
93 *this = other; | |
94 return; | |
95 } | |
96 | |
97 assert(!empty_); | |
98 | |
99 x1_ = std::min(x1_, other.x1_); | |
100 y1_ = std::min(y1_, other.y1_); | |
101 x2_ = std::max(x2_, other.x2_); | |
102 y2_ = std::max(y2_, other.y2_); | |
103 | |
104 assert(x1_ <= x2_ && | |
105 y1_ <= y2_); // This is the invariant of the structure | |
106 } | |
107 | |
108 | |
109 bool Extent2D::IsEmpty() const | |
110 { | |
111 if (empty_) | |
112 { | |
113 return true; | |
114 } | |
115 else | |
116 { | |
117 assert(x1_ <= x2_ && | |
118 y1_ <= y2_); | |
119 return (x2_ <= x1_ + 10 * std::numeric_limits<double>::epsilon() || | |
120 y2_ <= y1_ + 10 * std::numeric_limits<double>::epsilon()); | |
121 } | |
122 } | |
123 } |