source: GTP/trunk/Lib/Vis/Preprocessing/src/QtInterface/ObjectPlacer.cpp @ 2693

Revision 2693, 4.7 KB checked in by mattausch, 16 years ago (diff)

started on dynamic object interaction

Line 
1#include "ObjectPlacer.h"
2
3namespace GtpVisibilityPreprocessor {
4
5// ---------------------------------------------------------------------------
6//  ObjectPlacer implementation
7// ---------------------------------------------------------------------------
8#if 0
9ObjectPlacer::ObjectPlacer()
10        :mCurrentLibaryObject(0), mPlacedNodes(new BranchGroup), mLibraryViz(new BranchGroup)
11{
12        mLibraryViz->addChild(new BranchGroup);
13
14        BranchGroup *bg = new BranchGroup;
15        bg->addChild(new Box);
16        SetLibrary(bg);
17}
18
19
20void ObjectPlacer::SetLibrary(BranchGroup *library)
21{
22        mLibrary = new BranchGroup;
23        if (!library)
24        {
25                mLibrary->addChild(new Box);
26        }
27        else
28        {
29                for (int i = 0; i < library->numChildren(); i ++)
30                {       
31                        Node *n = library->getChild(i);
32                        SharedGroup *s = new SharedGroup;
33                        s->addChild(n);
34                       
35                        Link *l = new Link;
36                        l->setSharedGroup(s);
37                        mLibrary->addChild(l);
38                }
39        }
40       
41        UpdateLibraryViz();
42}
43
44void ObjectPlacer::UpdateLibraryViz()
45{
46        Link *l = new Link;
47        l->setSharedGroup(((Link *)mLibrary->getChild(mCurrentLibaryObject))->getSharedGroup());
48
49        mLibraryViz->setChild(l, 0);
50}
51
52
53void ObjectPlacer::NextObject()
54{
55        if (mLibrary)
56                mCurrentLibaryObject = (mCurrentLibaryObject + 1) % (mLibrary->numChildren());
57        UpdateLibraryViz();
58}
59
60void ObjectPlacer::PreviousObject()
61{
62        if (mLibrary)
63                mCurrentLibaryObject = (mCurrentLibaryObject - 1) % (mLibrary->numChildren());
64        UpdateLibraryViz();
65}
66
67void ObjectPlacer::AddObject(const Point3f &p)
68{
69        //BranchGroup *o = new BranchGroup;
70        TransformGroup *tg = new TransformGroup;
71        Transform3D t;
72        t.setTranslation(p - Origin3f);
73        tg->setTransform(t);
74        //o->addChild(tg);
75
76        Link *l = new Link;
77        l->setSharedGroup(((Link *)(mLibrary->getChild(mCurrentLibaryObject)))->getSharedGroup());
78        tg->addChild(l);
79
80        mPlacedNodes->addChild(tg);
81}
82
83void ObjectPlacer::RemoveLastObject()
84{
85        if (mPlacedNodes->numChildren() > 0)
86                mPlacedNodes->removeChild(mPlacedNodes->numChildren()-1);
87}
88
89void ObjectPlacer::SaveObjects(const String &filename)
90{
91        WriteActionIV wa(filename, "");
92
93        wa.Apply(mPlacedNodes);
94}
95
96BranchGroup *ObjectPlacer::GetLibraryVisualization()
97{
98        return mLibraryViz;
99}
100
101BranchGroup *ObjectPlacer::GetPlacedNodeVisualization()
102{
103        return mPlacedNodes;
104}
105
106void ObjectPlacer::Clear()
107{
108        mPlacedNodes->removeAllChildren();     
109}
110
111
112void TestApp::Placer(const String &parm)
113{
114        if (parm.Empty())
115        {
116                OUT1("Object placement: place/load <libaryfile>/removelast/next/previous/save <objectfile>");
117                return;
118        }
119
120        ASM_RET(viewer->GetRoot(), "No scene");
121       
122        if (!placer)
123                placer = new ObjectPlacer;
124
125        //-- make sure the placer visualization (which shows the already placed objects) is in the viewer
126        SearchNodeByNameVisitor search("placervis");
127        search.Apply(viewer->GetRoot(), SearchNodeByNameVisitor::FIRST);
128        BranchGroup *placervis = search.GetNode();
129        if (!placervis)
130        {
131                placervis = placer->GetPlacedNodeVisualization();
132                viewer->GetRoot()->addChild(placervis);
133                placervis->setName("placervis");
134        }
135
136        if (parm == "place")
137        {
138                int x, y;
139                input_module->GetMousePos(x, y);
140
141                Point3f pos = viewer->UnProject(x, y);
142                if (viewer && viewer->GetRoot() && viewer->GetRoot()->getBoundingBox()->intersects(pos))
143                {
144                        placer->AddObject(pos);
145                        OUT1(x << " " << y << " " << pos);
146                }
147                else
148                {
149                        OUT1("Point out of bounds");
150                }
151        } else if (parm == "removelast") {
152                placer->RemoveLastObject();
153        } else if (parm == "next") {
154                placer->NextObject();
155        } else if (parm == "previous") {
156                placer->PreviousObject();
157        } else if (parm.Left(5) == "save ") {
158                String filename = parm.Right(parm.size()-5);
159                if (!filename.Empty())
160                        placer->SaveObjects(filename);
161        } else if (parm.Left(5) == "clear") {
162                placer->Clear();
163        } else if (parm.Left(5) == "load ") {
164                String fn = sysutils::FindFile(parm.Right(parm.size()-5), settings->get_ObjectPath());
165
166                ASM_ELSE(!fn.empty(), "Couldn't find file " << fn)
167                {
168                        OUT1("Loading library " << fn << "...");
169                        SceneBasePtr<BranchGroup> root = ReadSceneGraph(fn, 0, 0, 1);
170
171                        BranchGroup *library = new BranchGroup;
172                        ASM_ELSE((root != NULL), "No scene found in file...")
173                        {
174                                SearchNodeByNameVisitor search("start");
175                                search.Apply(root, SearchNodeByNameVisitor::FIRST);
176                                BranchGroup *local_root = search.GetNode();
177
178                                if (local_root == NULL)
179                                {
180                                        OUT1("Note: BranchGroup named \"start\" not found - using root node");
181                                        local_root = root;
182                                }
183
184                                placer->SetLibrary(local_root);
185                                BranchGroup *viz = placer->GetLibraryVisualization();
186                                mRenderArea->SetSceneRoot(viz);
187                                mRenderArea->SetCamera(new Camera);
188                                mRenderArea->GetCamera()->ViewAll(viz->getBoundingSphere());
189                        }
190                }
191        } else
192                OUT1("Unknown placer command");
193
194}
195
196#endif
197
198}
Note: See TracBrowser for help on using the repository browser.