1 | #include <PixelMap/Scan.hh> |
---|
2 | #include <iostream> |
---|
3 | |
---|
4 | namespace PixelInfo |
---|
5 | { |
---|
6 | |
---|
7 | Scan nullScan() |
---|
8 | { |
---|
9 | /** |
---|
10 | * A simple way of returning a scan with zero length. |
---|
11 | */ |
---|
12 | Scan null(-1,-1,0); |
---|
13 | return null; |
---|
14 | } |
---|
15 | //------------------------------------------------------ |
---|
16 | |
---|
17 | Scan unite(Scan &scan1, Scan &scan2) |
---|
18 | { |
---|
19 | /** |
---|
20 | * Return a scan that includes all pixels from both scans, but |
---|
21 | * only if they overlap. If they do not, return the null scan. |
---|
22 | * |
---|
23 | */ |
---|
24 | |
---|
25 | // TEST FOR FAILURES: |
---|
26 | bool fail = false; |
---|
27 | fail = fail || (scan1.getY()!=scan2.getY()); |
---|
28 | fail = fail || ( (scan1.getX() < scan2.getX()) && |
---|
29 | (scan2.getX() > scan1.getXmax()+1) ); |
---|
30 | fail = fail || ( (scan2.getX() < scan1.getX()) && |
---|
31 | (scan1.getX() > scan2.getXmax()+1) ); |
---|
32 | Scan joined; |
---|
33 | if(fail){ |
---|
34 | // std::cerr << "Joining scans failed! (" |
---|
35 | // << scan1 <<"),("<<scan2<<") don't overlap\n"; |
---|
36 | joined = nullScan(); |
---|
37 | } |
---|
38 | else{ |
---|
39 | long y = scan1.getY(); |
---|
40 | long x = scan1.getX(); |
---|
41 | if(scan2.getX()<x) x=scan2.getX(); |
---|
42 | long xmax=scan1.getXmax(); |
---|
43 | if(scan2.getXmax()>xmax) xmax=scan2.getXmax(); |
---|
44 | joined.define(y,x,xmax-x+1); |
---|
45 | } |
---|
46 | return joined; |
---|
47 | } |
---|
48 | |
---|
49 | //------------------------------------------------------ |
---|
50 | |
---|
51 | Scan intersect(Scan &scan1, Scan &scan2) |
---|
52 | { |
---|
53 | /** |
---|
54 | * Return a scan that includes all pixels that lie in both scans. |
---|
55 | * |
---|
56 | * If they do not overlap, return the null scan. |
---|
57 | * |
---|
58 | */ |
---|
59 | |
---|
60 | bool fail = false; |
---|
61 | fail = fail || (scan1.getY()!=scan2.getY()); |
---|
62 | fail = fail || ( (scan1.getX() < scan2.getX()) && |
---|
63 | (scan2.getX() > scan1.getXmax()+1) ); |
---|
64 | fail = fail || ( (scan2.getX() < scan1.getX()) && |
---|
65 | (scan1.getX() > scan2.getXmax()+1) ); |
---|
66 | |
---|
67 | Scan intersection; |
---|
68 | if(fail){ |
---|
69 | // std::cerr << "Intersecting scans failed! (" |
---|
70 | // << scan1 <<"),("<<scan2<<") don't overlap.\n"; |
---|
71 | intersection = nullScan(); |
---|
72 | } |
---|
73 | else{ |
---|
74 | long y = scan1.getY(); |
---|
75 | long x = scan1.getX(); |
---|
76 | if(scan2.getX()>x) x=scan2.getX(); |
---|
77 | long xmax=scan1.getXmax(); |
---|
78 | if(scan2.getXmax()<xmax) xmax=scan2.getXmax(); |
---|
79 | intersection.define(y,x,xmax-x+1); |
---|
80 | } |
---|
81 | return intersection; |
---|
82 | } |
---|
83 | //------------------------------------------------------ |
---|
84 | |
---|
85 | bool touching(Scan &scan1, Scan &scan2) |
---|
86 | { |
---|
87 | /** |
---|
88 | * Test whether two scans either overlap, or lie adjacent |
---|
89 | * (ie. there are no pixels lying between the two scans). |
---|
90 | * \return A bool value. |
---|
91 | */ |
---|
92 | if(scan1.getY()!=scan2.getY()) return false; |
---|
93 | else if(scan1.getX() <= scan2.getX()) |
---|
94 | return (scan2.getX() <= scan1.getXmax()+1); |
---|
95 | else |
---|
96 | return (scan1.getX() <= scan2.getXmax()+1); |
---|
97 | |
---|
98 | } |
---|
99 | //------------------------------------------------------ |
---|
100 | |
---|
101 | bool overlap(Scan &scan1, Scan &scan2) |
---|
102 | { |
---|
103 | /** |
---|
104 | * Test whether two scans overlap, ie. they have pixels in |
---|
105 | * common. |
---|
106 | * \return A bool value. |
---|
107 | */ |
---|
108 | if(scan1.getY()!=scan2.getY()) return false; |
---|
109 | else if(scan1.getX() <= scan2.getX()) |
---|
110 | return (scan2.getX() <= scan1.getXmax()); |
---|
111 | else |
---|
112 | return (scan1.getX() <= scan2.getXmax()); |
---|
113 | |
---|
114 | } |
---|
115 | //------------------------------------------------------ |
---|
116 | |
---|
117 | bool adjacent(Scan &scan1, Scan &scan2) |
---|
118 | { |
---|
119 | /** |
---|
120 | * Test whether two scans lie adjacent (ie. there are no pixels |
---|
121 | * lying between the two scans). If they overlap, return false. |
---|
122 | * \return A bool value. |
---|
123 | */ |
---|
124 | if(scan1.getY()!=scan2.getY()) return false; |
---|
125 | else if(scan1.getX() <= scan2.getX()) |
---|
126 | return (scan2.getX() == scan1.getXmax()+1); |
---|
127 | else |
---|
128 | return (scan1.getX() == scan2.getXmax()+1); |
---|
129 | |
---|
130 | } |
---|
131 | //------------------------------------------------------ |
---|
132 | |
---|
133 | std::ostream& operator<< ( std::ostream& theStream, Scan& scan) |
---|
134 | { |
---|
135 | /** |
---|
136 | * Output the three key parameters of the scan. |
---|
137 | */ |
---|
138 | theStream << scan.itsY; |
---|
139 | theStream << " " << scan.itsX; |
---|
140 | theStream << " " << scan.itsXLen; |
---|
141 | } |
---|
142 | //------------------------------------------------------ |
---|
143 | |
---|
144 | bool operator< (Scan lhs, Scan rhs) |
---|
145 | { |
---|
146 | /** |
---|
147 | * Test for less-than first on the y-values, and if they are |
---|
148 | * equal, test on the starting x-value, and then finally on the |
---|
149 | * length. |
---|
150 | * |
---|
151 | * This is necessary for sorting functions on lists of Scans (used |
---|
152 | * by the Object2D class). |
---|
153 | */ |
---|
154 | if(lhs.itsY != rhs.itsY) return (lhs.itsY < rhs.itsY); |
---|
155 | else if(lhs.itsX != rhs.itsX) return (lhs.itsX < rhs.itsX); |
---|
156 | else return (lhs.itsXLen < rhs.itsXLen); |
---|
157 | } |
---|
158 | //------------------------------------------------------ |
---|
159 | |
---|
160 | bool operator== (Scan lhs, Scan rhs) |
---|
161 | { |
---|
162 | /** |
---|
163 | * For two scans to be equal, all three parameters must be equal. |
---|
164 | */ |
---|
165 | return (lhs.itsY == rhs.itsY) && |
---|
166 | (lhs.itsX == rhs.itsX) && |
---|
167 | (lhs.itsXLen == rhs.itsXLen); |
---|
168 | } |
---|
169 | //------------------------------------------------------ |
---|
170 | |
---|
171 | bool Scan::isInScan(long x, long y) |
---|
172 | { |
---|
173 | return (y == this->itsY) && |
---|
174 | ( (x>= this->itsX) && (x < (this->itsXLen+this->itsX)) ); |
---|
175 | } |
---|
176 | //------------------------------------------------------ |
---|
177 | |
---|
178 | void mergeList(std::vector<Scan> scanlist) |
---|
179 | { |
---|
180 | std::vector<Scan>::iterator iter; |
---|
181 | int counter=0,compCounter; |
---|
182 | while(counter<(scanlist.size()-1)){ |
---|
183 | |
---|
184 | |
---|
185 | compCounter = counter+1; |
---|
186 | |
---|
187 | do{ |
---|
188 | |
---|
189 | if(touching(scanlist[counter],scanlist[compCounter])){ |
---|
190 | Scan temp = unite(scanlist[counter],scanlist[compCounter]); |
---|
191 | iter = scanlist.begin()+compCounter; |
---|
192 | scanlist.erase(iter); |
---|
193 | iter = scanlist.begin()+counter; |
---|
194 | scanlist.erase(iter); |
---|
195 | scanlist.push_back(temp); |
---|
196 | } |
---|
197 | else compCounter ++; |
---|
198 | }while(compCounter < scanlist.size()); |
---|
199 | |
---|
200 | counter++; |
---|
201 | } |
---|
202 | |
---|
203 | } |
---|
204 | |
---|
205 | |
---|
206 | } |
---|