File size: 6,962 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
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
/* -*- 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-2012 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/ui3VisibleObject.h>
#include <cisst3DUserInterface/ui3VisibleList.h>

#include <cisstOSAbstraction/osaSleep.h>

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


ui3VisibleObject::ui3VisibleObject(const std::string & name):
    Assembly(0),
    Matrix(0),
    SceneManager(0),
    VTKHandle(0),
    ParentList(0),
    IsSceneList(false),
    CreatedMember(false),
    VisibleMember(true),
    NameMember(name)
{
    this->Assembly = vtkAssembly::New();
    CMN_ASSERT(this->Assembly);

    this->Matrix = vtkMatrix4x4::New();
    CMN_ASSERT(this->Matrix);
    this->Matrix->Identity();

    this->Assembly->SetUserMatrix(this->Matrix);
}


bool ui3VisibleObject::Update(ui3SceneManager * sceneManager)
{
    vtkProp3D * objectVTKProp;
    VTKHandleType propHandle;
    bool result = true;

    // check it this object has already been created
    if (!this->Created()) {
        CMN_LOG_CLASS_RUN_VERBOSE << "Update: object \"" << this->Name()
                                  << "\" created" << std::endl;
        // this is an object, i.e. a leaf
        result = this->CreateVTKObjects();

        // set vtk handle on visible object
        objectVTKProp = this->GetVTKProp();
        CMN_ASSERT(objectVTKProp);

        // convert pointer to (void *)
        propHandle = reinterpret_cast<VTKHandleType>(objectVTKProp);
        this->SetVTKHandle(propHandle);

        // set the scene manager to get the Lock/Unlock working
        this->SceneManager = sceneManager;

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


vtkProp3D * ui3VisibleObject::GetVTKProp(void)
{
    return this->Assembly;
}


void ui3VisibleObject::Show(void)
{
    CMN_LOG_CLASS_RUN_DEBUG << "Show: called for object \"" << this->Name() << "\"" << std::endl;
    this->SetVisible(true);
    this->PropagateVisibility(true);
}


void ui3VisibleObject::Hide(void)
{
    CMN_LOG_CLASS_RUN_DEBUG << "Hide: called for object \"" << this->Name() << "\"" << std::endl;
    this->SetVisible(false);
    this->PropagateVisibility(false);
}


void ui3VisibleObject::SetMatrixElement(unsigned int i, unsigned int j, double value)
{
    this->Matrix->SetElement(i, j, value);
}


bool ui3VisibleObject::IsAddedToScene(void) const
{
	if (this->IsSceneList) {
		return true;
	} else {
		if (this->ParentList) {
			return this->ParentList->IsAddedToScene();
		}
	}
	return false;
}


void ui3VisibleObject::PropagateVisibility(bool visible)
{
    if (visible) {
        unsigned int index;
        for (index = 0; index < Parts.size(); index++) {
            if (this->Visible()) {
                Parts[index]->VisibilityOn();
            } else {
                Parts[index]->VisibilityOff();
            }
        }
    } else {
        unsigned int index;
        for (index = 0; index < Parts.size(); index++) {
            Parts[index]->VisibilityOff();
        }
    }
}


void ui3VisibleObject::SetPosition(const vctDouble3 & position, bool useLock)
{
    CMN_LOG_CLASS_RUN_DEBUG << "SetPosition: called for object \"" << this->Name() << "\"" << std::endl;
    if (this->Created()) {
        if (useLock) {
            this->Lock();
        }
        this->Matrix->SetElement(0, 3, position.X());
        this->Matrix->SetElement(1, 3, position.Y());
        this->Matrix->SetElement(2, 3, position.Z());
        if (useLock) {
            this->Unlock();
        }
    } else {
        CMN_LOG_CLASS_RUN_VERBOSE << "SetPosition: called on object \"" << this->Name() << "\" not yet created" << std::endl;
    }
}


void ui3VisibleObject::SetScale(const double & scale, bool useLock)
{
    CMN_LOG_CLASS_RUN_DEBUG << "SetScale: called for object \"" << this->Name() << "\"" << std::endl;
    if (this->Created()) {
        if (useLock) {
            this->Lock();
        }
        this->Assembly->SetScale(scale);
        if (useLock) {
            this->Unlock();
        }
    } else {
        CMN_LOG_CLASS_RUN_VERBOSE << "SetScale: called on object \"" << this->Name() << "\" not yet created" << std::endl;
    }
}


vctDoubleFrm3 ui3VisibleObject::GetTransformation(void) const
{
    vctDoubleFrm3 result;
    unsigned int i, j;
    for (i = 0; i < 3; i++) {
        result.Translation().Element(i) = this->Matrix->GetElement(i, 3);
        for (j = 0; j < 3; j++) {
            result.Rotation().Element(i, j) = this->Matrix->GetElement(i, j);
        }
    }
    return result;
}


vctDoubleFrm3 ui3VisibleObject::GetAbsoluteTransformation(void) const
{
    vctDoubleFrm3 result;
    if (!this->ParentList) {
        return result;
    }
    vctDoubleFrm3 parent, relative;
    relative = this->GetTransformation();
    parent = this->ParentList->GetAbsoluteTransformation();
    parent.ApplyTo(relative, result);
    return result;
}


void ui3VisibleObject::SetVTKMatrix(vtkMatrix4x4* matrix)
{
    if (this->Created()) {
        this->Matrix->DeepCopy(matrix);
    } else {
        CMN_LOG_CLASS_RUN_VERBOSE << "SetVTKMatrix: called on object \"" << this->Name() << "\" not yet created" << std::endl;
    }
}


void ui3VisibleObject::Lock(void)
{
    if (this->Created()) {
        this->SceneManager->Lock(this->VTKHandle);
    } else {
        CMN_LOG_CLASS_RUN_DEBUG << "Lock: attempt to lock with object \""
                                << this->Name() << "\" not yet added to the scene" << std::endl;
    }
}


void ui3VisibleObject::Unlock(void)
{
    if (this->Created()) {
        this->SceneManager->Unlock(this->VTKHandle);
    } else {
        CMN_LOG_CLASS_RUN_DEBUG << "Unlock: attempt to unlock with object \""
                                << this->Name() << "\" not yet added to the scene" << std::endl;
    }
}


void ui3VisibleObject::AddPart(vtkProp3D * part)
{
    CMN_LOG_CLASS_RUN_DEBUG << "AddPart: part added to object \"" << this->Name() << "\"" << std::endl;
    this->Assembly->AddPart(part);
    this->Parts.push_back(part);
}


void ui3VisibleObject::WaitForCreation(void) const
{
	if (!this->IsAddedToScene()) {
		CMN_LOG_CLASS_RUN_ERROR << "WaitForCreation: object \"" << this->Name()
		                        << "\" not added to scene (could be added to a list not yet added to the main scene)"
		                        << std::endl;
		return;
	}
	while (!this->Created()) {
		osaSleep(10.0 * cmn_ms); // 10 milliseconds
	}
}