1 /*!
2  *
3  * OVERVIEW:
4  *
5  * This app uses factory and keywordlist to instatiate objects within OSSIM.  Among these
6  * objects are projections, and image processing filters/mosaics and annotations.  Basically any
7  * object within OSSIM that must have a state saved or loaded uses a keywordlist to do so.
8  * The main keyword required by all objects is a TYPE_KW which has a value "type".  Most
9  * keywords that are used in OSSIM are located in base/common/ossimKeywordNames.h
10  *
11  * We will also demonstrate the use of object factory registry to instantiate objects from
12  * type name.
13  *
14  * A keyword list is basically a name value pair and you want to make sure that the name portion
15  * is unique for all objects being saved to the keywordlist.  for example I may want to save 2 objects
16  * to the keyword list and object 1 may have a prefix value of object1. and object 2 may have a
17  * prefix value of object2. and so forth.  See sample code for demonstration.
18  *
19  * Note: Since multiple objects may reside within a keywodlist a prefix value is used to prefix all
20  *       attributes of an object.
21  *
22  *
23  * PURPOSE:
24  *
25  * Learn how to instantiate different types of objects from a keywordlist
26  * through the  ossimObjectFactoryRegistry.
27  *
28  */
29 
30 #include <iostream>
31 
32 #include "base/common/ossimCommon.h"
33 #include "base/data_types/ossimFilename.h"
34 #include "base/data_types/ossimKeywordlist.h"
35 #include "base/factory/ossimObjectFactoryRegistry.h"
36 #include "init/ossimInit.h"
37 
38 using namespace std;
39 
40 ossimObject* createObject(const ossimString& objectType);
41 void demo1();
42 void demo2();
43 void demo3();
44 
main(int argc,char * argv[])45 int main(int argc, char* argv[])
46 {
47    ossimInit::instance()->initialize(argc, argv);
48 
49    cout << "_______________________________DEMO 1__________________________\n";
50    demo1();
51 
52    cout << "_______________________________DEMO 2__________________________\n";
53    demo2();
54 
55    cout << "_______________________________DEMO 3__________________________\n";
56    demo3();
57 
58    ossimInit::instance()->finalize();
59 
60    return 0;
61 }
62 
63 
createObject(const ossimString & objectType)64 ossimObject* createObject(const ossimString& objectType)
65 {
66    return ossimObjectFactoryRegistry::instance()->createObject(objectType);
67 }
68 
69 /*!
70  * Demo1 will just instantiate and object of the passed in type and then execute a
71  * save state and print that keywordlist out.
72  */
demo1()73 void demo1()
74 {
75    ossimObject* obj1 = createObject("ossimRgbToGreyFilter");
76    if(obj1)
77    {
78       ossimKeywordlist kwl;
79 
80       obj1->saveState(kwl);
81 
82       cout << "------------------ object 1 = " << obj1->getClassName() << " keyword list is -----------------" << endl;
83       cout << kwl << endl;
84 
85       kwl.clear();
86       cout << "------------------ object 1 = " << obj1->getClassName() << " keyword list with prefix  -----------------" << endl;
87       obj1->saveState(kwl, "object1.");
88       cout << kwl << endl;
89 
90    }
91    else
92    {
93       cout << "This shouldn't happen" << endl;
94    }
95 }
96 
97 /*!
98  * Save 2 objects to a keywordlist without conflict in values.  This will force us to
99  * use a prefix in order to save 2 objects to the same list
100  */
demo2()101 void demo2()
102 {
103    ossimObject* obj1 = createObject("ossimRgbToGreyFilter");
104    ossimObject* obj2 = createObject("ossimBrightnessContrastSource");
105    if(obj1&&obj2)
106    {
107       ossimKeywordlist kwl;
108 
109       obj1->saveState(kwl, "object1.");
110       obj2->saveState(kwl, "object2.");
111 
112       cout << "Objects " << obj1->getClassName() << " and "
113            << obj2->getClassName() << " saved to the same keywordlist."
114            << endl;
115 
116       cout << kwl << endl;
117    }
118    else
119    {
120       cout << "This shouldn't happen" << endl;
121    }
122 
123 }
124 
125 /*!
126  * Save object to keywordlist and then pass keyword list to factory and
127  * instantitate an object from keywordlist.  We will do it twice, the first time
128  * without a prefix and then the second time with a prefix.
129  *
130  * We will use 2 object types: the first one will be a filter and the second one will
131  * be a projection.
132  *
133  * Note:  you should get identical outputs for both outputs.
134  *
135  */
demo3()136 void demo3()
137 {
138    ossimObject* obj  = createObject("ossimRgbToGreyFilter");
139    ossimObject* proj = createObject("ossimUtmProjection");
140 
141    if(obj&&proj)
142    {
143       ossimKeywordlist kwl;
144       obj->saveState(kwl);
145       ossimObject* objNew = ossimObjectFactoryRegistry::instance()->createObject(kwl);
146 
147       if(objNew)
148       {
149          ossimKeywordlist kwl2;
150          objNew->saveState(kwl2);
151 
152          cout << "____________________ The Original object " << obj->getClassName() << " ___________________" << endl;
153          cout << kwl << endl;
154          cout << "____________________ The New object " << objNew->getClassName() << " ___________________" << endl;
155          cout << kwl2 << endl;
156 
157          delete objNew;
158       }
159       else
160       {
161          cout << "ERROR: This shouldn't happen" << endl;
162       }
163       kwl.clear();
164 
165       proj->saveState(kwl);
166       objNew = ossimObjectFactoryRegistry::instance()->createObject(kwl);
167 
168       if(objNew)
169       {
170          ossimKeywordlist kwl2;
171          objNew->saveState(kwl2);
172 
173          cout << "____________________ The Original object " << proj->getClassName() << " ___________________" << endl;
174          cout << kwl << endl;
175          cout << "____________________ The New object " << objNew->getClassName() << " ___________________" << endl;
176          cout << kwl2 << endl;
177 
178          delete objNew;
179       }
180       else
181       {
182          cout << "ERROR: This shouldn't happen" << endl;
183       }
184 
185       delete obj;
186       delete proj;
187    }
188    else
189    {
190       cout << "This shouldn't happen" << endl;
191    }
192 
193 }
194