summaryrefslogtreecommitdiffstats
path: root/tilemgr/tiler.cc
blob: 9924cd40fef6fc4b0ca73ac646d076f1589c0a87 (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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/*
* This file is part of rasdaman community.
*
* Rasdaman community is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Rasdaman community is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with rasdaman community.  If not, see <http://www.gnu.org/licenses/>.
*
* Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009 Peter Baumann /
rasdaman GmbH.
*
* For more information please see <http://www.rasdaman.org>
* or contact Peter Baumann via <baumann@rasdaman.com>.
*/

#include "tiler.hh"
#include "tilemgr/tile.hh"

r_Tiler::r_Tiler(std::vector<r_Minterval>& sourceDomain2s, const std::vector<r_Minterval>& targetDomain2s)
	:	sourceDomains(sourceDomain2s),
		targetDomains(targetDomain2s)
	{
	}

void
r_Tiler::mergeDomains()
	{
	std::vector<r_Minterval>::iterator splitedIt;
	std::vector<r_Minterval> temp;
	r_Minterval tempDom;
	bool merged = false;
	while (!splitedDomains.empty())
		{
		merged = false;
		tempDom = *(splitedDomains.begin());
		splitedDomains.erase(splitedDomains.begin());
		for (splitedIt = splitedDomains.begin(); splitedDomains.end() != splitedIt; splitedIt++)
			{
			if (tempDom.is_mergeable(*splitedIt))
				{
				//cout << "is mergeable " << tempDom << " " << *splitedIt << endl;
				tempDom.closure_with(*splitedIt);
				//cout << "closure " << tempDom << endl;
				splitedDomains.erase(splitedIt);
				merged = true;
				break;
				}
			else	{
				//cout << "is not mergeable " << tempDom << " " << *splitedIt << endl;
				}
			}
		if (merged)
			splitedDomains.push_back(tempDom);
		else
			temp.push_back(tempDom);
		}
	splitedDomains = temp;
	}

void
r_Tiler::split()
	{
	std::vector<r_Minterval>::iterator sourceIt;
	std::vector<r_Minterval>::iterator retvalIt;
	std::vector<r_Minterval> splits;
	std::vector<RangePair> points;

	for (sourceIt = sourceDomains.begin(); sourceIt != sourceDomains.end(); sourceIt++)
		{
		//cout << "starting with source domain " << *sourceIt << endl;
		points = computeSplitDimensions(*sourceIt);
		splits = splitMinterval(*sourceIt, points);
		for (retvalIt = splits.begin(); retvalIt != splits.end(); retvalIt++)
			{
			splitedDomains.push_back(*retvalIt);
			}
		//it is either splitted and in retval or not splitted and then there is an error, because not splitted were inserted
		}
	}

std::vector<RangePair>
r_Tiler::computeSplitDimensions(const r_Minterval& sourceDomain) const
	{
	r_Dimension dim = 0;
	r_Range slow = 0;
	r_Range shigh = 0;
	r_Range ilow = 0;
	r_Range ihigh = 0;

	std::vector<r_Minterval> intersects; 
	std::vector<r_Minterval>::iterator intersectIt;
	std::vector<r_Minterval>::const_iterator targetIt;

	std::vector<RangePair> points;
	std::vector<RangePair>::iterator pointIt;
	RangePair pair;

	//cout << "\tfinding the intersections of current source with targets" << endl;
	for (targetIt = targetDomains.begin(); targetIt != targetDomains.end(); targetIt++)
		{
		if ((*targetIt).intersects_with(sourceDomain))
			{
			//cout << "\t\tsource intersected target " << *targetIt << endl;
			intersects.push_back((*targetIt).create_intersection(sourceDomain));
			}
		else	{
			//cout << "\t\tsource did not intersect target " << *targetIt << endl;
			}
		}

	//cout << "\titerating through intersecting domains to determine the spliting dimensions" << endl;
	for (intersectIt = intersects.begin(); intersectIt != intersects.end(); intersectIt++)	
		{
		//cout << "\t\tstarting to compute split dimensions with " << *intersectIt << endl;
		for (dim = 0; dim < (*intersectIt).dimension(); dim++)
			{
			ilow = (*intersectIt)[dim].low();
			ihigh = (*intersectIt)[dim].high();
			slow = (sourceDomain)[dim].low();
			shigh = (sourceDomain)[dim].high();
			//cout << "\t\t\tdimension " << dim << endl;
			//cout << "\t\t\tsource low  " << (sourceDomain)[dim].low() << "=" << slow << ", intersect low  " << (*intersectIt)[dim].low() << "=" << ilow << endl;
			//cout << "\t\t\tsource high " << (sourceDomain)[dim].high() << "=" << shigh << ", intersect high " << (*intersectIt)[dim].high() << "=" << ihigh << endl;
			if ((slow <= ilow) & (shigh >= ilow))
				{
				pair.first = dim;
				pair.second = ilow - 1;
				//cout << "\t\t\tadding " << pair.second << endl;
				points.push_back(pair);
				}
			if ((slow <= ihigh) & (shigh >= ihigh))
				{
				pair.first = dim;
				pair.second = ihigh;
				//cout << "\t\t\tadding " << pair.second << endl;
				points.push_back(pair);
				}
			}
		//cout << "\t\tfound all split dimensions for the current source with intersect " << *intersectIt << endl;
		}
	//cout << "\t\t\tfound all split dimensions for source " << sourceDomain << endl;
	//for (pointIt = points.begin(); pointIt != points.end(); pointIt++)
		//cout << "\t\t\tsplit in dimension " << (*pointIt).first << " at coordinate " << (*pointIt).second << endl;
	//cout << "try " << endl;
	std::vector<RangePair> temp;
	std::vector<RangePair>::iterator tempIt;
	RangePair tempP;
	bool tempadd = true;
	temp.swap(points);
	//for (pointIt = points.begin(); pointIt != points.end(); pointIt++)
		//cout << "\t\t\tsplit in dimension " << (*pointIt).first << " at coordinate " << (*pointIt).second << endl;
	//cout << "try end" << endl;
	while (!temp.empty())
		{
		tempadd = true;
		tempP = *(temp.begin());
		temp.erase(temp.begin());
		for (tempIt = temp.begin(); tempIt != temp.end(); tempIt++)
			{
			if ((tempP.first == (*tempIt).first) & (tempP.second == (*tempIt).second))
				{
				//cout << "matched" << endl;
				tempadd = false;
				break;
				}
			}
		if (tempadd)
			points.push_back(tempP);
		}
	//cout << "try end 2" << endl;
	//for (pointIt = points.begin(); pointIt != points.end(); pointIt++)
		//cout << "\t\t\tsplit in dimension " << (*pointIt).first << " at coordinate " << (*pointIt).second << endl;
	//cout << "try end 3" << endl;
	return points;
	}

std::vector<r_Minterval>
r_Tiler::splitMinterval(const r_Minterval& sourceTile, std::vector<RangePair>& points)
	{
	std::vector<r_Minterval> splits; 
	std::vector<r_Minterval> split2s; 
	std::vector<r_Minterval>::iterator splitIt;	
	std::vector<r_Minterval>::iterator split2It;
	
	r_Minterval splitInterval1;
	r_Minterval splitInterval2;
	
	std::vector<RangePair>::iterator pointIt;

	bool addDomain = false;
	
	r_Dimension dim = 0;
	r_Range low = 0;
	r_Range high = 0;
	r_Range splitCoordinate = 0;

	//initialize the split std::vector.  the result will contain a list of splitted domains.
	splits.push_back(sourceTile);

	//cout << "\t\tstarting to actually split" << endl;
	for (pointIt = points.begin(); pointIt != points.end(); pointIt++)
		{
		split2s = std::vector<r_Minterval>();
		//cout << "\t\t\tstarting to split at dimension " << (*pointIt).first << " coordinate " << (*pointIt).second << endl;
		//cout << "\t\t\titerating through all the tiles to be split, stemming from current source tile" << endl;
		while (!splits.empty())
			{
			splitIt = splits.begin();
			//cout << "\t\t\t\tsplitting " << (*splitIt) << endl;
			addDomain = false;
			splitInterval1 = r_Minterval(((*splitIt)).dimension());
			splitInterval2 = r_Minterval(((*splitIt)).dimension());
			//cout << "\t\t\t\ttrying split" << endl;
			for (dim = 0; dim < ((*splitIt)).dimension(); dim++)
				{
				if ((dim == (*pointIt).first))
					{
					high = (*splitIt)[dim].high();
					low = (*splitIt)[dim].low();
					splitCoordinate = (*pointIt).second;
					if ((splitCoordinate < high) & (splitCoordinate > low))
						{
						//cout << "\t\t\t\t\tsplit middle adding [" << low << ":" << splitCoordinate << "]" << endl;
						splitInterval1 << r_Sinterval(low, splitCoordinate);
						//cout << "\t\t\t\t\tsplit middle adding [" << splitCoordinate+1 << ":" << high << "]" << endl;
						splitInterval2 << r_Sinterval(splitCoordinate + 1, high);
						addDomain = true;
						}
					else	{
						if (splitCoordinate == high)
							{
							addDomain = true;
							if (low == high)
								{
								//cout << "\t\t\t\t\tsplit high adding [" << low << ":" << low << "]" << endl;
								splitInterval1 << r_Sinterval(low, high);
								}
							else	{
								//cout << "\t\t\t\t\tsplit high adding [" << low << ":" << high-1<<"]" << endl;
								splitInterval1 << r_Sinterval(low, splitCoordinate - 1);
								}
								
							splitInterval2 << r_Sinterval(splitCoordinate, splitCoordinate);
							}
						else	{
							if ((*pointIt).second == ((*splitIt))[dim].low())
								{
								addDomain = true;
								if (low == high)
									{
									//cout<<"\t\t\t\t\tsplit low adding ["<<low<<":"<<high<< "]"<<endl;
									splitInterval1 << r_Sinterval(low, high);
									}
								else	{
									//cout<<"\t\t\t\t\tsplit low adding ["<<low+1<<":"<< high<< "]"<<endl;
									splitInterval1 << r_Sinterval(low + 1, high);
									}
								splitInterval2<< r_Sinterval((*pointIt).second,(*pointIt).second);
								}
							else	{
								//cout << "\t\t\t\t\tno match, break" << endl;
								break;
								}
							}
						}
					}
				else	{
					//cout << "\t\t\t\t\tdimension unmatched, adding " << ((*splitIt))[dim] << endl;
					splitInterval1 << ((*splitIt))[dim];
					splitInterval2 << ((*splitIt))[dim];
					}
				}
			if (addDomain)
				{
				//cout << "\t\t\t\tadding split domain 1 " << splitInterval1 << endl;
				split2s.push_back(splitInterval1);
				//cout << "\t\t\t\tadding split domain 2 " << splitInterval2 << endl;
				split2s.push_back(splitInterval2);
				}
			else	{
				//cout << "\t\t\t\tnot adding split domains " << splitInterval1 << " " << splitInterval1 << endl;
				//cout << "\t\t\t\tadding because of no splits " << (*splitIt) << endl;
				split2s.push_back(*splitIt);
				}
			splits.erase(splitIt);
			}
		splits = split2s;
		}
	//cout << "splitMinterval computed" << endl;
	//for (splitIt = splits.begin(); splitIt != splits.end(); splitIt++)
	//	{
		//cout << (*splitIt) << endl;
	//	}
	return splits;
	}

void
r_Tiler::removeCoveredDomains()
	{
	r_Minterval temp;
	std::vector<r_Minterval> retval;
	bool kill = false;
	std::vector<r_Minterval>::iterator targetIt;
	retval.swap(splitedDomains);
	while (!retval.empty())
		{
		temp = *(retval.begin());
		retval.erase(retval.begin());
		kill = false;
		for (targetIt = retval.begin(); targetIt != retval.end(); targetIt++)
			{
			if ((*targetIt).intersects_with(temp))
				{
				kill = true;
				break;
				}
			}
		if (!kill)
			splitedDomains.push_back(temp);
		}
	}

void
r_Tiler::removeDoubleDomains()
	{
	r_Minterval temp;
	std::vector<r_Minterval> retval;
	bool kill = false;
	std::vector<r_Minterval>::iterator targetIt;
	while (!splitedDomains.empty())
		{
		temp = *(splitedDomains.begin());
		splitedDomains.erase(splitedDomains.begin());
		kill = false;
		for (targetIt = targetDomains.begin(); targetIt != targetDomains.end(); targetIt++)
			{
			if ((*targetIt).intersects_with(temp))
				{
				kill = true;
				break;
				}
			}
		if (!kill)
			retval.push_back(temp);
		}
	retval.swap(splitedDomains);
	}

std::vector<r_Minterval>
r_Tiler::getTiledDomains() const
	{
	return splitedDomains;
	}

std::vector<Tile*>
r_Tiler::generateTiles(const std::vector<Tile*>& sourceTiles) const
	{
	std::vector<r_Minterval>::const_iterator splitedDomIt;
	std::vector<Tile*>::const_iterator sourceTileIt;
	std::vector<Tile*> retval;
	r_Minterval dummy;
	Tile* p = 0;
	const BaseType* basetype = (*sourceTiles.begin())->getType();
	r_Data_Format dataformat = (*sourceTiles.begin())->getDataFormat();
	
	for (splitedDomIt = splitedDomains.begin(); splitedDomIt != splitedDomains.end(); splitedDomIt++)
		{
		dummy = *splitedDomIt;
		p = new Tile(dummy, basetype, dataformat);
		//std::cout << "new tile " << dummy << " " << basetype->getName() << " " << dataformat << " size " << p->getSize() << " other size " << p->getDBTile()->getSize() << std::endl;
		for (sourceTileIt = sourceTiles.begin(); sourceTileIt != sourceTiles.end(); sourceTileIt++)
			{
			//std::cout << " the other tile domain " << (*sourceTileIt)->getDomain() << " type " << (*sourceTileIt)->getType()->getName() << std::endl;
			if (dummy.intersects_with((*sourceTileIt)->getDomain()))
				{
				const r_Minterval& updateDomain = dummy.create_intersection((*sourceTileIt)->getDomain());
				//std::cout << "  they intersect.  on " << updateDomain << std::endl;
				//UnaryOp* tempOp = Ops::getUnaryOp(Ops::OP_IDENTITY, p->getType(), (*sourceTileIt)->getType(), 0, 0);
				//causes fmr/abr/umr
				p->copyTile(updateDomain, *sourceTileIt, updateDomain);
				//p->execUnaryOp(tempOp, dummy.create_intersection((*sourceTileIt)->getDomain()), *sourceTileIt, dummy.create_intersection((*sourceTileIt)->getDomain()));
				//delete tempOp;
				}
			}
		retval.push_back(p);
		}
	return retval;
	}