~ubuntu-branches/ubuntu/hardy/libterralib/hardy

« back to all changes in this revision

Viewing changes to src/terralib/kernel/TeGeneralizedProxMatrix.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Daniel T Chen
  • Date: 2005-11-25 22:32:59 UTC
  • Revision ID: james.westby@ubuntu.com-20051125223259-3zubal8ux4ki4fjg
Tags: upstream-3.0.3b2
ImportĀ upstreamĀ versionĀ 3.0.3b2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/************************************************************************************
 
2
TerraLib - a library for developing GIS applications.
 
3
Copyright ļæ½ 2001, 2002, 2003 INPE and Tecgraf/PUC-Rio.
 
4
 
 
5
This code is part of the TerraLib library.
 
6
This library is free software; you can redistribute it and/or
 
7
modify it under the terms of the GNU Lesser General Public
 
8
License as published by the Free Software Foundation; either
 
9
version 2.1 of the License, or (at your option) any later version.
 
10
 
 
11
You should have received a copy of the GNU Lesser General Public
 
12
License along with this library.
 
13
 
 
14
The authors reassure the license terms regarding the warranties.
 
15
They specifically disclaim any warranties, including, but not limited to,
 
16
the implied warranties of merchantability and fitness for a particular purpose.
 
17
The library provided hereunder is on an "as is" basis, and the authors have no
 
18
obligation to provide maintenance, support, updates, enhancements, or modifications.
 
19
In no event shall INPE and Tecgraf / PUC-Rio be held liable to any party for direct,
 
20
indirect, special, incidental, or consequential damages arising out of the use
 
21
of this library and its documentation.
 
22
*************************************************************************************/
 
23
 
 
24
#include "TeSTElementSet.h"
 
25
#include "TeGeneralizedProxMatrix.h"
 
26
 
 
27
 
 
28
bool
 
29
TeGeneralizedProxMatrix::IsValid () const
 
30
{
 
31
        if ((imp_) && (sc_) && (ss_) && (sw_))
 
32
                return true;
 
33
 
 
34
        return false;
 
35
}
 
36
 
 
37
 
 
38
TeProxMatrixImplementation*
 
39
TeGeneralizedProxMatrix::getImplementation (const string type)
 
40
{
 
41
         if (imp_ == 0) 
 
42
                imp_ = TeProxMatrixAbstractFactory::MakeConcreteImplementation (type); 
 
43
    return imp_;
 
44
}
 
45
 
 
46
 
 
47
 
 
48
TeGeneralizedProxMatrix::TeGeneralizedProxMatrix(TeSTElementSet* objects, TeGeomRep geom_type, const string imp_type)
 
49
{
 
50
        imp_ = 0;
 
51
        imp_ = getImplementation(imp_type);
 
52
        total_slices_ = 1;
 
53
 
 
54
        if ((geom_type==TePOLYGONS) || (geom_type==TeCELLS))
 
55
                sc_ = new TeProxMatrixLocalAdjacencyStrategy (objects, geom_type);
 
56
        else {
 
57
                        imp_ = 0;
 
58
                        sc_ = 0;
 
59
                        ss_ = 0;
 
60
                        sw_ = 0;
 
61
                }
 
62
        ss_ = new TeProxMatrixNoSlicingStrategy;
 
63
        sw_ = new TeProxMatrixNoWeightsStrategy;
 
64
 
 
65
        if (IsValid()) {
 
66
                if (sc_->Construct (imp_)) {
 
67
                        ss_->Slice (imp_);
 
68
                        sw_->ComputeWeigths (imp_);
 
69
                } 
 
70
                else {
 
71
 
 
72
                        imp_ = 0;
 
73
                        sc_ = 0;
 
74
                        ss_ = 0;
 
75
                        sw_ = 0;
 
76
                }
 
77
        }
 
78
}
 
79
 
 
80
 
 
81
TeGeneralizedProxMatrix::TeGeneralizedProxMatrix (TeProxMatrixConstructionStrategy* sc, TeProxMatrixWeightsStrategy* sw, TeProxMatrixSlicingStrategy* ss, const string imp_type)
 
82
{
 
83
        imp_ = 0;
 
84
        imp_ = getImplementation(imp_type);
 
85
        total_slices_ = 1;
 
86
        sc_ = sc;
 
87
 
 
88
 
 
89
  if(ss)
 
90
                ss_ = ss;
 
91
        else
 
92
                ss_ = new TeProxMatrixNoSlicingStrategy();
 
93
 
 
94
        if(sw)
 
95
                sw_ = sw;
 
96
        else
 
97
                sw_ = new TeProxMatrixNoWeightsStrategy();
 
98
 
 
99
 
 
100
        if (IsValid()) {
 
101
                if (sc_->Construct (imp_)) {
 
102
                        ss_->Slice (imp_);
 
103
                        sw_->ComputeWeigths (imp_);
 
104
                } 
 
105
                else {
 
106
                        imp_ = 0;
 
107
                        sc_ = 0;
 
108
                        ss_ = 0;
 
109
                        sw_ = 0;
 
110
                }
 
111
        }
 
112
}
 
113
 
 
114
 
 
115
TeGeneralizedProxMatrix::TeGeneralizedProxMatrix(const TeGeneralizedProxMatrix& pm)
 
116
{
 
117
        if (pm.imp_ == 0)
 
118
                imp_ = 0;
 
119
        else 
 
120
                imp_ = pm.imp_->CreateCopy ();
 
121
 
 
122
        total_slices_ = pm.total_slices_;
 
123
        sc_ = pm.sc_;
 
124
        ss_ = pm.ss_;
 
125
        sw_ = pm.sw_;
 
126
 
 
127
        if (IsValid() == false)
 
128
        {
 
129
                imp_ = 0;
 
130
                sc_ = 0;
 
131
                ss_ = 0;
 
132
                sw_ = 0;
 
133
        }
 
134
}
 
135
 
 
136
 
 
137
TeGeneralizedProxMatrix& 
 
138
TeGeneralizedProxMatrix::operator=(const TeGeneralizedProxMatrix& pm) 
 
139
{
 
140
        if (*this == pm) return *this;
 
141
 
 
142
        if (imp_) delete imp_;
 
143
        imp_ = 0;
 
144
        if (pm.imp_)    imp_ = pm.imp_->CreateCopy ();
 
145
 
 
146
        total_slices_ = pm.total_slices_;
 
147
        sc_ = pm.sc_;
 
148
        ss_ = pm.ss_;
 
149
        sw_ = pm.sw_;
 
150
 
 
151
        if (IsValid() == false)
 
152
        {
 
153
                imp_ = 0;
 
154
                sc_ = 0;
 
155
                ss_ = 0;
 
156
                sw_ = 0;
 
157
        }
 
158
 
 
159
        return *this;
 
160
 
 
161
}
 
162
 
 
163
bool 
 
164
TeGeneralizedProxMatrix::operator==(const TeGeneralizedProxMatrix& pm) const
 
165
{
 
166
        if (IsValid() && pm.IsValid()) 
 
167
        {       
 
168
                if ((sc_->IsEqual (*(pm.sc_))) &&
 
169
                        (ss_->IsEqual (*(pm.ss_))) &&
 
170
                        (sw_->IsEqual (*(pm.sw_))) &&
 
171
                        (total_slices_ == pm.total_slices_) &&
 
172
 
 
173
                        (imp_->IsEqual(*(pm.imp_)))) return true;
 
174
        } else  
 
175
                if ((IsValid() == false) && (pm.IsValid() == false)) 
 
176
                        return true;
 
177
        
 
178
        return false;
 
179
 
 
180
}
 
181
 
 
182
TeNeighbours
 
183
TeGeneralizedProxMatrix:: getNeighbours (const string& object_id, int slice) 
 
184
{
 
185
        TeNeighbours neigh2;
 
186
        if (slice > total_slices_) return neigh2;
 
187
 
 
188
        if (IsValid()) 
 
189
        {
 
190
                TeNeighbours neigh1;
 
191
                imp_->getNeighbours (object_id, neigh1);
 
192
                for (int i=0; i < neigh1.size(); i++) 
 
193
                        if (neigh1.Attributes(i).Slice() == slice) 
 
194
                                neigh2.Insert (neigh1.ObjectId(i), neigh1.Attributes(i));
 
195
        }
 
196
 
 
197
        return neigh2;
 
198
}
 
199
 
 
200
 
 
201
TeNeighbours 
 
202
TeGeneralizedProxMatrix::operator[](const string& object_id) 
 
203
{
 
204
        return getNeighbours(object_id);
 
205
}
 
206
 
 
207
 
 
208
TeNeighboursMap
 
209
TeGeneralizedProxMatrix::getMapNeighbours (const string& object_id, int slice) 
 
210
{
 
211
        TeNeighboursMap map;
 
212
        if (slice > total_slices_) return map;
 
213
 
 
214
        if (IsValid()) 
 
215
        {
 
216
                TeNeighbours neigh;
 
217
                imp_->getNeighbours (object_id, neigh);
 
218
 
 
219
                for (int i=0; i < neigh.size(); i++) 
 
220
                        if (neigh.Attributes(i).Slice() == slice) 
 
221
                                map[neigh.ObjectId(i)] = neigh.Attributes(i);
 
222
        }
 
223
 
 
224
        return map;
 
225
}
 
226
 
 
227
 
 
228
TeSTElementSet 
 
229
TeGeneralizedProxMatrix::getSTENeighbours(const string& object_id)
 
230
{
 
231
        TeSTElementSet selected_objects;
 
232
        if (imp_) 
 
233
        {
 
234
                TeNeighbours neigh;
 
235
                imp_->getNeighbours (object_id, neigh);
 
236
                
 
237
                for (int i = 0; i < neigh.size(); i++) 
 
238
                {
 
239
                        // Construct a sto instance with its attributes
 
240
                        TeSTInstance obj;
 
241
                        obj.objectId(neigh.ObjectId(i));  
 
242
                        
 
243
                        //load the attributes
 
244
                        TePropertyVector propVector;
 
245
                        sc_->objects()->getPropertyVector(object_id, propVector);
 
246
                        obj.properties(propVector);
 
247
                                
 
248
                        // insert object in the return vector
 
249
                        selected_objects.insertSTInstance(obj);
 
250
                }
 
251
        }
 
252
        return selected_objects;
 
253
}
 
254
 
 
255
 
 
256
bool 
 
257
TeGeneralizedProxMatrix::setCurrentConstructionStrategy (TeProxMatrixConstructionStrategy* sc)  
 
258
{       
 
259
        if (sc == 0) 
 
260
                return false; 
 
261
        sc_ = sc; 
 
262
        return true;
 
263
}
 
264
 
 
265
 
 
266
bool 
 
267
TeGeneralizedProxMatrix::setCurrentWeightsStrategy (TeProxMatrixWeightsStrategy* sw) 
 
268
{       
 
269
        if (sw == 0) 
 
270
                return false; 
 
271
        sw_ = sw; 
 
272
        return true;
 
273
}
 
274
 
 
275
bool 
 
276
TeGeneralizedProxMatrix::setCurrentSlicingStrategy (TeProxMatrixSlicingStrategy* ss) 
 
277
{
 
278
        if (ss == 0) 
 
279
                return false; 
 
280
        ss_ = ss; 
 
281
        return true;
 
282
}
 
283
 
 
284
bool
 
285
TeGeneralizedProxMatrix:: ConstructMatrix ()
 
286
{
 
287
 
 
288
        //      ClearImplementation();
 
289
        imp_ = 0;
 
290
        imp_ = getImplementation();
 
291
 
 
292
        if (sc_) 
 
293
        {
 
294
                if (sc_->Construct (imp_)) 
 
295
                {
 
296
                        if (ss_) ss_->Slice (imp_);
 
297
                        if (sw_) sw_->ComputeWeigths (imp_); 
 
298
                        return true;
 
299
                } 
 
300
        }
 
301
        return false;
 
302
}
 
303
 
 
304
 
 
305
bool
 
306
TeGeneralizedProxMatrix:: ClearImplementation ()
 
307
{
 
308
        TeProxMatrixImplementation* aux;
 
309
        if (imp_ == 0)  aux = getImplementation ();
 
310
        else    aux = getImplementation (imp_->Type().c_str());
 
311
 
 
312
        if (aux == 0) return false;
 
313
        delete imp_;
 
314
        imp_ = aux;
 
315
 
 
316
        return true;
 
317
}
 
318
 
 
319
bool
 
320
TeGeneralizedProxMatrix:: RecomputeWeights ()
 
321
{
 
322
        if (IsValid()){
 
323
                sw_->ComputeWeigths (imp_); return true;
 
324
        } 
 
325
        return false;
 
326
}
 
327
 
 
328
 
 
329
bool
 
330
TeGeneralizedProxMatrix:: RecomputeSlicing ()
 
331
{
 
332
        if (IsValid()){
 
333
                ss_->Slice (imp_); return true;
 
334
        } 
 
335
        return false;
 
336
}
 
337
 
 
338
 
 
339
bool 
 
340
TeGeneralizedProxMatrix::IsConnected (const string& object_id1, const string& object_id2) 
 
341
{
 
342
        if (imp_ == 0) 
 
343
                return false;  
 
344
        return imp_->isConnected (object_id1, object_id2);
 
345
}
 
346
 
 
347
bool 
 
348
TeGeneralizedProxMatrix::ConnectObjects (const string& object_id1, const string& object_id2, const TeProxMatrixAttributes& attr)
 
349
{
 
350
        if (!imp_) 
 
351
                getImplementation();  
 
352
        imp_->connectObjects (object_id1, object_id2, attr);
 
353
        return true;
 
354
}
 
355
 
 
356
bool 
 
357
TeGeneralizedProxMatrix::ConnectObjects (const string& object_id1, const string& object_id2)
 
358
{
 
359
        if (!imp_) 
 
360
                getImplementation();  
 
361
        TeProxMatrixAttributes attr;
 
362
        imp_->connectObjects (object_id1, object_id2, attr);
 
363
        return true;
 
364
}
 
365
 
 
366
bool 
 
367
TeGeneralizedProxMatrix::DisconnectObjects (const string& object_id1, const string& object_id2)
 
368
{
 
369
        if (imp_ == 0) 
 
370
                return false;  
 
371
        return imp_->disconnectObjects (object_id1, object_id2);
 
372
}
 
373
 
 
374
bool 
 
375
TeGeneralizedProxMatrix::RemoveObject (const string& object_id)
 
376
{
 
377
        if (imp_ == 0) 
 
378
                return false;  
 
379
        return imp_->removeObject (object_id);
 
380
}
 
381
 
 
382
bool 
 
383
TeGeneralizedProxMatrix::GetConnectionAttributes (const string& object_id1, string& object_id2, TeProxMatrixAttributes& attr)
 
384
{
 
385
        if (imp_ == 0) 
 
386
                return false;
 
387
        return imp_->getConnectionAttributes (object_id1, object_id2, attr); 
 
388
}
 
389
 
 
390
int  
 
391
TeGeneralizedProxMatrix::NumberOfObjects () 
 
392
{
 
393
        if (imp_ == 0) 
 
394
                return 0;  
 
395
        return imp_->NumberOfObjects ();
 
396
}
 
397
 
 
398
bool 
 
399
TeGeneralizedProxMatrix::SaveTextFile (string name) 
 
400
{       
 
401
        if (imp_ == 0) 
 
402
                return false;
 
403
        return imp_->SaveTextFile (name); 
 
404
}
 
405
 
 
406
 
 
407
bool 
 
408
TeGeneralizedProxMatrix::SaveGALFile (string name) 
 
409
{       
 
410
        if (imp_ == 0) 
 
411
                return false;
 
412
        return imp_->SaveGALFile (name); 
 
413
}
 
414
 
 
415
bool 
 
416
TeGeneralizedProxMatrix::SaveGWTFile (string name) 
 
417
{       
 
418
        if (imp_ == 0) 
 
419
                return false;
 
420
        return imp_->SaveGWTFile (name); 
 
421
}
 
422
 
 
423
TeGeneralizedProxMatrix::~TeGeneralizedProxMatrix()
 
424
{
 
425
    if (imp_ !=  0) 
 
426
                delete imp_; //It is not counted.
 
427
}
 
428
 
 
429
 
 
430
 
 
431
 
 
432
 
 
433