annotate Framework/Radiography/RadiographyMaskLayer.cpp @ 475:3c28542229a3 am-touch-events

added a mask layer in the RadiographyWidget (to be cleaned)
author am@osimis.io
date Tue, 12 Feb 2019 12:22:13 +0100
parents
children a95090305dd4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
475
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
1 /**
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
2 * Stone of Orthanc
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
4 * Department, University Hospital of Liege, Belgium
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
5 * Copyright (C) 2017-2018 Osimis S.A., Belgium
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
6 *
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
7 * This program is free software: you can redistribute it and/or
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
8 * modify it under the terms of the GNU Affero General Public License
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
9 * as published by the Free Software Foundation, either version 3 of
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
10 * the License, or (at your option) any later version.
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
11 *
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
12 * This program is distributed in the hope that it will be useful, but
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
15 * Affero General Public License for more details.
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
16 *
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
17 * You should have received a copy of the GNU Affero General Public License
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
19 **/
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
20
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
21
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
22 #include "RadiographyMaskLayer.h"
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
23 #include "RadiographyDicomLayer.h"
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
24
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
25 #include "RadiographyScene.h"
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
26 #include "Core/Images/Image.h"
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
27 #include "Core/Images/ImageProcessing.h"
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
28 #include <Core/OrthancException.h>
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
29
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
30 namespace OrthancStone
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
31 {
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
32
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
33 void ComputeMaskExtent(unsigned int& left, unsigned int& right, unsigned int& top, unsigned int& bottom, const std::vector<MaskPoint>& corners)
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
34 {
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
35 left = std::numeric_limits<unsigned int>::max();
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
36 right = std::numeric_limits<unsigned int>::min();
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
37 top = std::numeric_limits<unsigned int>::max();
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
38 bottom = std::numeric_limits<unsigned int>::min();
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
39
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
40 for (size_t i = 0; i < corners.size(); i++)
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
41 {
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
42 const MaskPoint& p = corners[i];
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
43 left = std::min(p.x, left);
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
44 right = std::max(p.x, right);
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
45 bottom = std::max(p.y, bottom);
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
46 top = std::min(p.y, top);
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
47 }
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
48 }
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
49
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
50 void RadiographyMaskLayer::SetCorners(const std::vector<MaskPoint>& corners)
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
51 {
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
52 corners_ = corners;
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
53 invalidated_ = true;
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
54 }
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
55
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
56 void RadiographyMaskLayer::Render(Orthanc::ImageAccessor& buffer,
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
57 const AffineTransform2D& viewTransform,
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
58 ImageInterpolation interpolation) const
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
59 {
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
60 if (dicomLayer_.GetWidth() == 0) // nothing to do if the DICOM layer is not displayed (or not loaded)
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
61 return;
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
62
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
63 if (invalidated_)
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
64 {
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
65 mask_.reset(new Orthanc::Image(Orthanc::PixelFormat_Grayscale8, dicomLayer_.GetWidth(), dicomLayer_.GetHeight(), false));
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
66
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
67 DrawMask();
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
68
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
69 // for (unsigned int i = 0; i < 100; i++)
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
70 // {
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
71 // for (unsigned int j = 0; j < 50; j++)
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
72 // {
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
73 // if ((i + j) % 2 == 1)
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
74 // {
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
75 // Orthanc::ImageAccessor region;
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
76 // mask_->GetRegion(region, i* 20, j * 20, 20, 20);
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
77 // Orthanc::ImageProcessing::Set(region, 255);
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
78 // }
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
79 // }
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
80 // }
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
81 invalidated_ = false;
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
82 }
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
83
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
84 {// rendering
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
85 if (buffer.GetFormat() != Orthanc::PixelFormat_Float32)
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
86 {
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
87 throw Orthanc::OrthancException(Orthanc::ErrorCode_IncompatibleImageFormat);
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
88 }
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
89
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
90 unsigned int cropX, cropY, cropWidth, cropHeight;
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
91 dicomLayer_.GetCrop(cropX, cropY, cropWidth, cropHeight);
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
92
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
93 const AffineTransform2D t = AffineTransform2D::Combine(
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
94 viewTransform, dicomLayer_.GetTransform(),
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
95 AffineTransform2D::CreateOffset(cropX, cropY));
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
96
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
97 Orthanc::ImageAccessor cropped;
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
98 mask_->GetRegion(cropped, cropX, cropY, cropWidth, cropHeight);
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
99
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
100 Orthanc::Image tmp(Orthanc::PixelFormat_Grayscale8, buffer.GetWidth(), buffer.GetHeight(), false);
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
101
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
102 t.Apply(tmp, cropped, interpolation, true /* clear */);
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
103
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
104 // Blit
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
105 const unsigned int width = buffer.GetWidth();
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
106 const unsigned int height = buffer.GetHeight();
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
107
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
108 for (unsigned int y = 0; y < height; y++)
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
109 {
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
110 float *q = reinterpret_cast<float*>(buffer.GetRow(y));
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
111 const uint8_t *p = reinterpret_cast<uint8_t*>(tmp.GetRow(y));
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
112
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
113 for (unsigned int x = 0; x < width; x++, p++, q++)
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
114 {
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
115 if (*p == 0)
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
116 *q = foreground_;
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
117 // else keep the underlying pixel value
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
118 }
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
119 }
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
120
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
121 }
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
122 }
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
123
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
124 // from https://www.geeksforgeeks.org/how-to-check-if-a-given-point-lies-inside-a-polygon/
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
125 // Given three colinear points p, q, r, the function checks if
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
126 // point q lies on line segment 'pr'
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
127 bool onSegment(const MaskPoint& p, const MaskPoint& q, const MaskPoint& r)
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
128 {
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
129 if (q.x <= std::max(p.x, r.x) && q.x >= std::min(p.x, r.x) &&
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
130 q.y <= std::max(p.y, r.y) && q.y >= std::min(p.y, r.y))
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
131 return true;
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
132 return false;
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
133 }
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
134
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
135 // To find orientation of ordered triplet (p, q, r).
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
136 // The function returns following values
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
137 // 0 --> p, q and r are colinear
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
138 // 1 --> Clockwise
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
139 // 2 --> Counterclockwise
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
140 int orientation(const MaskPoint& p, const MaskPoint& q, const MaskPoint& r)
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
141 {
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
142 int val = (q.y - p.y) * (r.x - q.x) -
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
143 (q.x - p.x) * (r.y - q.y);
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
144
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
145 if (val == 0) return 0; // colinear
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
146 return (val > 0)? 1: 2; // clock or counterclock wise
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
147 }
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
148
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
149 // The function that returns true if line segment 'p1q1'
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
150 // and 'p2q2' intersect.
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
151 bool doIntersect(const MaskPoint& p1, const MaskPoint& q1, const MaskPoint& p2, const MaskPoint& q2)
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
152 {
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
153 // Find the four orientations needed for general and
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
154 // special cases
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
155 int o1 = orientation(p1, q1, p2);
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
156 int o2 = orientation(p1, q1, q2);
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
157 int o3 = orientation(p2, q2, p1);
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
158 int o4 = orientation(p2, q2, q1);
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
159
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
160 // General case
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
161 if (o1 != o2 && o3 != o4)
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
162 return true;
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
163
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
164 // Special Cases
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
165 // p1, q1 and p2 are colinear and p2 lies on segment p1q1
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
166 if (o1 == 0 && onSegment(p1, p2, q1)) return true;
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
167
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
168 // p1, q1 and p2 are colinear and q2 lies on segment p1q1
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
169 if (o2 == 0 && onSegment(p1, q2, q1)) return true;
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
170
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
171 // p2, q2 and p1 are colinear and p1 lies on segment p2q2
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
172 if (o3 == 0 && onSegment(p2, p1, q2)) return true;
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
173
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
174 // p2, q2 and q1 are colinear and q1 lies on segment p2q2
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
175 if (o4 == 0 && onSegment(p2, q1, q2)) return true;
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
176
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
177 return false; // Doesn't fall in any of the above cases
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
178 }
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
179
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
180 // Define Infinite (Using INT_MAX caused overflow problems)
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
181 #define MASK_INF 1000000
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
182
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
183 // Returns true if the point p lies inside the polygon[] with n vertices
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
184 bool isInside(const std::vector<MaskPoint>& polygon, const MaskPoint& p)
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
185 {
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
186 // There must be at least 3 vertices in polygon[]
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
187 if (polygon.size() < 3) return false;
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
188
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
189 // Create a point for line segment from p to infinite
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
190 MaskPoint extreme = {MASK_INF, p.y};
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
191
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
192 // Count intersections of the above line with sides of polygon
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
193 int count = 0, i = 0;
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
194 do
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
195 {
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
196 int next = (i+1) % polygon.size();
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
197
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
198 // Check if the line segment from 'p' to 'extreme' intersects
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
199 // with the line segment from 'polygon[i]' to 'polygon[next]'
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
200 if (doIntersect(polygon[i], polygon[next], p, extreme))
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
201 {
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
202 // If the point 'p' is colinear with line segment 'i-next',
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
203 // then check if it lies on segment. If it lies, return true,
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
204 // otherwise false
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
205 if (orientation(polygon[i], p, polygon[next]) == 0)
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
206 return onSegment(polygon[i], p, polygon[next]);
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
207
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
208 count++;
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
209 }
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
210 i = next;
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
211 } while (i != 0);
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
212
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
213 // Return true if count is odd, false otherwise
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
214 return count&1; // Same as (count%2 == 1)
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
215 }
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
216
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
217
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
218 void RadiographyMaskLayer::DrawMask() const
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
219 {
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
220 unsigned int left;
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
221 unsigned int right;
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
222 unsigned int top;
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
223 unsigned int bottom;
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
224
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
225 ComputeMaskExtent(left, right, top, bottom, corners_);
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
226
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
227 Orthanc::ImageProcessing::Set(*mask_, 0);
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
228
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
229 MaskPoint p(left, top);
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
230 for (p.y = top; p.y <= bottom; p.y++)
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
231 {
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
232 unsigned char* q = reinterpret_cast<unsigned char*>(mask_->GetRow(p.y));
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
233 for (p.x = left; p.x <= right; p.x++, q++)
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
234 {
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
235 if (isInside(corners_, p))
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
236 {
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
237 *q = 255;
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
238 }
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
239 }
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
240 }
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
241
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
242 // Orthanc::ImageAccessor region;
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
243 // mask_->GetRegion(region, 100, 100, 1000, 1000);
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
244 // Orthanc::ImageProcessing::Set(region, 255);
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
245 }
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
246
3c28542229a3 added a mask layer in the RadiographyWidget (to be cleaned)
am@osimis.io
parents:
diff changeset
247 }