summaryrefslogtreecommitdiffstats
path: root/mddmgr/test/test_mddops.cc
blob: 5a81bc828ad57910588332162385c66361dad981 (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
/*
* 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>.
/
/**
 * SOURCE: test_mddops.cc
 *
 * MODULE: example for executing operations on MDDs
 *
 * PURPOSE:
 *   makes operations on MDDs or parts of them
 *
 * COMMENTS:
 *   later has to be moved to the executor
 *
*/

static const char rcsid[] = "@(#)blobif,test_mddops: $Id: test_mddops.cc,v 1.10 2002/09/03 14:46:50 coman Exp $";

#include <stdlib.h>
#include <iostream>
#include "o2lib_CC.hxx"
#include "o2template_CC.hxx"
#include "ulongtype.hh"
#include "mddmgr/persmddobj.hh"
#include "mddmgr/perstile.hh"
#include "adminif.hh"
#include "databaseif.hh"
#include "transactionif.hh"
#include "raslib/rminit.hh"
#include "typefactory.hh"


// global variable for AdminIf because of O2 d_Session::begin()
extern char* myExecArgv0 = "";

RMINITGLOBALS('C')

static char O2BenchDBName[] = "DemoBase";
static char O2BenchSchemaName[] = "RasDaSchema";

static void testOperations( DatabaseIf myDB );

BaseType* myType;
static long myCell = 0;

/*************************************************************
 * Function name.: int main( int argc, char** argv)
 *
 * Arguments.....: 
 *   argc: number of arguments given to program
 *   argv: array of char* with arguments
 * Return value..: exit status
 * Description...: none
 ************************************************************/

int
main( int argc, char** argv)
{
  // variables representing O2 database, ta and session
  DatabaseIf database;
  TransactionIf ta;

  // don't forget to initialize before using AdminIf!
  myExecArgv0 = argv[0];

  AdminIf* myAdmin = AdminIf::instance();

  // only possible after AdminIf::instance on Sun!
  myType = TypeFactory::mapType("ULong");

  // connect to the database
  cout << "Connecting to database " << O2BenchDBName 
       << "..." << endl;
  database.open( O2BenchDBName );

  testOperations( database );

  cout << "Ending O2 session..." << endl;
  database.close();
  delete myAdmin;
}

void
printAllTiles(const MDDObj* mdd)
{
  // contains all tiles of MDD
  vector<Tile*>* allTiles;
  // iterator
  vector<Tile*>::iterator tileIt;
  // domains of a  tile
  r_Minterval tileDom;

  // domain of MDD object
  r_Minterval dom;
  dom = mdd->getCurrentDomain();

  // get all tiles of result MDD
  allTiles = mdd->intersect(dom);

  // and iterate over them
  tileIt = allTiles->begin();
  while (tileIt !=  allTiles->end())
  {
    tileDom = (*tileIt)->getDomain();
    cout << "Domain of Tile: ";
    tileDom.print_status();
    cout << endl;

    cout << "Tile: " << endl;
    (*tileIt)->printStatus();
    cout << endl;    

    tileIt++;
  }
}

/*************************************************************
 * Function......: testOperations( DatabaseIf myDB )
 *
 * Arguments.....: 
 *   myDB: database to use (should be opened)
 * Return value..: none
 * Description...: constructs BLOBTiles and inserts them
 *                 in root collection.
 ************************************************************/

MDDObj*
execBinaryOp( Ops::OpType op, 
	      const MDDObj* op1, const r_Minterval& areaOp1,
	      const MDDObj* op2, const r_Minterval& areaOp2 )
{
  // contains all tiles of op1
  vector<Tile*>* allTilesOp1;
  // contains all tiles of op2 which intersect a given op1 Tile
  // in the relevant area.
  vector<Tile*>* intersectTilesOp2;
  // iterators for tiles of the MDDs
  vector<Tile*>::iterator tileOp1It;
  vector<Tile*>::iterator intersectTileOp2It;
  // domain of tile of Op1
  r_Minterval tileOp1Dom; 
  // domain of tile of Op2
  r_Minterval tileOp2Dom; 
  // intersection of domains in relevant area.
  r_Minterval intersectDom;
  // pointer to generated result tile
  PersTile* resTile;
  // MDDObj for result
  PersMDDObj* mddres;
  // translations between the two areas
  r_Point offset12(areaOp1.dimension());
  r_Point offset21(areaOp1.dimension());
  // dummy
  r_Minterval dummy;

  // calculate translations
  r_Point originOp1 = areaOp1.get_origin();
  r_Point originOp2 = areaOp2.get_origin();
  for(r_Dimension i = 0; i<areaOp1.dimension(); i++)
  {
    offset12[i] = originOp2[i] - originOp1[i];
    offset21[i] = originOp1[i] - originOp2[i];
  }

  // create MDDObj for result (type has later to be
  // retrieved from one of the operands)
  mddres = new PersMDDObj( areaOp1, "ULong" );

  // get all tiles in relevant area of MDD op1
  allTilesOp1 = op1->intersect(areaOp1);

  // and iterate over them
  tileOp1It = allTilesOp1->begin();
  while (tileOp1It !=  allTilesOp1->end())
  {
    // domain of the op1 tile
    tileOp1Dom = (*tileOp1It)->getDomain();

    // intersect the tile with MDD op2 (including translation)
    intersectTilesOp2 = 
      op2->intersect(tileOp1Dom.create_translation(offset12));

    // iterate over intersecting tiles
    intersectTileOp2It = intersectTilesOp2->begin();
    while (intersectTileOp2It !=  intersectTilesOp2->end())
    {
      tileOp2Dom = (*intersectTileOp2It)->getDomain();

      // the relevant domain is the intersection of the
      // domains of the two tiles with the relevant area.
      intersectDom = tileOp1Dom.create_intersection(
        tileOp2Dom.create_translation(offset21));
      intersectDom.intersection_with(areaOp1);

      // Creating tile for result. Type should later come from
      // operand.
      resTile = new PersTile(intersectDom, myType);

      // carry out operation on the relevant area of the tiles
      resTile->execBinaryOp(op, intersectDom, 
			    (*tileOp1It), intersectDom, 
			    (*intersectTileOp2It), 
			    intersectDom.create_translation(offset12));

      // insert Tile in result tile
      mddres->insertTile(resTile);

      intersectTileOp2It++;
    }
    tileOp1It++;
  }

  return mddres;
}

/*************************************************************
 * Function......: testConstructors( DatabaseIf myDB )
 *
 * Arguments.....: 
 *   myDB: database to use (should be opened)
 * Return value..: none
 * Description...: constructs BLOBTiles and inserts them
 *                 in root collection.
 ************************************************************/

// function for creating demo tiles

Tile* 
create2DTile( long xmin, long xmax, long ymin, long ymax, 
	      BaseType* type )
{
  // is copied anyway in constructor
  unsigned long cell = 0x10000L;

  r_Sinterval s1(xmin, xmax);
  r_Sinterval s2(ymin, ymax);
  r_Minterval dom(2);
  dom << s1 << s2;
  cout << "  Domain of Tile ";
  dom.print_status();
  cout << endl;
  return new PersTile( dom, type, (char*)&cell);
}

static void testOperations( DatabaseIf myDB )
{
  Tile* aTile;
  ULongType ulongtype;
  BaseType* type = &ulongtype;
  MDDObj* res;

  r_Sinterval limits1(1l,10l);
  r_Sinterval limits2(1l,10l);
  r_Minterval dom(2);
  dom << limits1 << limits2;

  r_Sinterval oplimits1(2l,9l);
  r_Sinterval oplimits2(2l,9l);
  r_Minterval opdom(2);
  opdom << oplimits1 << oplimits2;

  r_Sinterval limits21(11l,20l);
  r_Sinterval limits22(11l,20l);
  r_Minterval dom2(2);
  dom2 << limits21 << limits22;

  r_Sinterval oplimits21(12l,19l);
  r_Sinterval oplimits22(12l,19l);
  r_Minterval opdom2(2);
  opdom2 << oplimits21 << oplimits22;

  // create MDD Object for 1st operand
  cout << "MDD Op1" << endl;

  PersMDDObj* mddop1 = new PersMDDObj( dom, "ULong" );
  
  cout << "  Tile 1 [ 1:5, 1:10 ] " << endl;
  aTile = create2DTile(1, 5, 1, 10, type);
  mddop1->insertTile(aTile);  
  
  cout << "  Tile 2 [ 6:10, 1:5 ] " << endl;
  aTile = create2DTile(6, 10, 1, 5, type);
  mddop1->insertTile(aTile);  
  
  cout << "  Tile 3 [ 6:10, 6:10 ] " << endl;
  aTile = create2DTile(6, 10, 6, 10, type);
  mddop1->insertTile(aTile);  
  
  mddop1->printStatus();

  // create MDD Object for 2nd operand
  cout << "MDD Op2" << endl;

  PersMDDObj* mddop2 = new PersMDDObj( dom2, "ULong" );
  
  cout << "  Tile 1 [ 11:17, 11:15 ] " << endl;
  aTile = create2DTile(11, 17, 11, 15, type);
  mddop2->insertTile(aTile);  
  
  cout << "  Tile 2 [ 11:17, 16:20 ] " << endl;
  aTile = create2DTile(11, 17, 16, 20, type);
  mddop2->insertTile(aTile);  
  
  cout << "  Tile 3 [ 18:20, 11:20 ] " << endl;
  aTile = create2DTile(18, 20, 11, 20, type);
  mddop2->insertTile(aTile);  
  
  mddop2->printStatus();

  res = execBinaryOp(Ops::OP_PLUS, mddop1, opdom, mddop2, opdom2);

  // output result (cast should not be necessary!)
  ((PersMDDObj*)res)->printStatus();
  printAllTiles(res);

}