File size: 5,493 Bytes
68ce2fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-    */
/* ex: set filetype=cpp softtabstop=4 shiftwidth=4 tabstop=4 cindent expandtab: */

/*

  Author(s):	Balazs Vagvolgyi, Simon DiMaio, Anton Deguet
  Created on:	2008-06-10

  (C) Copyright 2008 Johns Hopkins University (JHU), All Rights
  Reserved.

--- begin cisst license - do not edit ---

This software is provided "as is" under an open source license, with
no warranty.  The complete license can be found in license.txt and
http://www.cisst.org/cisst/license.txt.

--- end cisst license ---
*/


#include <cisst3DUserInterface/ui3VisibleList.h>
#include <cisst3DUserInterface/ui3SceneManager.h>

#include <vtkAssembly.h>
#include <vtkProperty.h>
#include <vtkMatrix4x4.h>


CMN_IMPLEMENT_SERVICES(ui3VisibleList);


ui3VisibleList::ui3VisibleList(const std::string & name):
    ui3VisibleObject(name + " (list)"),
    UpdateNeededMember(true)
{
}


bool ui3VisibleList::Add(ui3VisibleObject * objectPointer)
{
    // verify that the pointer is not already in the list
    if (std::find(this->Objects.begin(),
                  this->Objects.end(),
                  objectPointer) != this->Objects.end()) {
        CMN_LOG_CLASS_RUN_ERROR << "Add: object \"" << objectPointer->Name() << "\" already in list \""
                                << this->Name() << "\"" << std::endl;
        return false;
    }
    // otherwise, add it to the list
	this->Objects.push_back(objectPointer);
	objectPointer->ParentList = this;
	this->RecursiveUpdateNeeded();
	CMN_LOG_CLASS_RUN_VERBOSE << "Add: object \"" << objectPointer->Name() << "\" added to list \""
	                          << this->Name() << "\"" << std::endl;
    return true;
}


void ui3VisibleList::RecursiveUpdateNeeded(void)
{
    this->UpdateNeededMember = true;
    this->Assembly->Modified();
    if (this->ParentList) {
        this->ParentList->RecursiveUpdateNeeded();
    }
}


bool ui3VisibleList::UpdateVTKObjects(void)
{
    bool result = true;
    const ListType::iterator end = Objects.end();
    ListType::iterator iterator;

    for (iterator = Objects.begin();
         iterator != end;
         ++iterator) {

        result &= ((*iterator)->UpdateVTKObjects());
    }
    return result;
}


bool ui3VisibleList::Update(ui3SceneManager * sceneManager)
{
    // debug
    CMN_ASSERT(sceneManager);

    if (!this->UpdateNeededMember) {
        return false; // there was no update
    }

    // pseudo creation, just set the scene manager pointer
    this->SceneManager = sceneManager;

    // if an object has been added, we check all of them
    bool result = true;
    const ListType::iterator end = Objects.end();
    ListType::iterator iterator;
    vtkProp3D * objectVTKProp;
    VTKHandleType propHandle;

    ui3VisibleList * listPointer;
    ui3VisibleObject * objectPointer;

    for (iterator = Objects.begin();
         iterator != end;
         ++iterator) {

        // check if the object is a list or not
        objectPointer = (*iterator);
        listPointer = dynamic_cast<ui3VisibleList *>(objectPointer);

        if (listPointer) {
            // this is a list, i.e. a node

            // check if the corresponding VTK object has been added
            if (!listPointer->Created()) {
                CMN_LOG_CLASS_RUN_VERBOSE << "Update: list \"" << listPointer->Name()
                                          << "\" added in update for \""
                                          << this->Name() << "\"" << std::endl;
                // set vtk handle on visible object
                objectVTKProp = listPointer->GetVTKProp();
                CMN_ASSERT(objectVTKProp);

                // convert pointer to (void *)
                propHandle = reinterpret_cast<VTKHandleType>(objectVTKProp);
                listPointer->SetVTKHandle(propHandle);
 
                // add to assembly
                this->Assembly->AddPart(objectVTKProp);

                // mark as created so we don't create it again
                listPointer->SetCreated(true);
            }

            // pass the update to children
            listPointer->Update(sceneManager);


        } else {
            // check it this object has already been created
            if (!objectPointer->Created()) {
                CMN_LOG_CLASS_RUN_VERBOSE << "Update: calling Update for object \"" << objectPointer->Name()
                                          << "\" in update for \""
                                          << this->Name() << "\"" << std::endl;
                result &= objectPointer->Update(sceneManager);

                // add to assembly
                objectVTKProp = objectPointer->GetVTKProp();
                this->Assembly->AddPart(objectVTKProp);
            }
        }
        objectPointer->PropagateVisibility(objectPointer->Visible());
    }

    this->UpdateNeededMember = false;

    return true;
}


bool ui3VisibleList::CreateVTKObjects(void)
{
    return true;
}


void ui3VisibleList::PropagateVisibility(bool visible)
{
    const ListType::iterator end = Objects.end();
    ListType::iterator iterator;

    if (visible) {
        for (iterator = Objects.begin();
             iterator != end;
             ++iterator) {
            (*iterator)->PropagateVisibility(this->Visible());
        }
    } else {
        for (iterator = Objects.begin();
             iterator != end;
             ++iterator) {
            (*iterator)->PropagateVisibility(false);
        }
    }
}