1 /*---------------------------------------------------------------------------*\
5 * Copyright (C) 2006 by the OpenSG Forum *
7 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
9 \*---------------------------------------------------------------------------*/
10 /*---------------------------------------------------------------------------*\
13 * This library is free software; you can redistribute it and/or modify it *
14 * under the terms of the GNU Library General Public License as published *
15 * by the Free Software Foundation, version 2. *
17 * This library is distributed in the hope that it will be useful, but *
18 * WITHOUT ANY WARRANTY; without even the implied warranty of *
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
20 * Library General Public License for more details. *
22 * You should have received a copy of the GNU Library General Public *
23 * License along with this library; if not, write to the Free Software *
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
26 \*---------------------------------------------------------------------------*/
27 /*---------------------------------------------------------------------------*\
35 \*---------------------------------------------------------------------------*/
37 #include <UnitTest++.h>
39 // Unit tests for nodes, attachments various core types
42 #include "OSGNameAttachment.h"
43 #include "OSGAttachment.h"
44 #include "OSGDynamicAttachmentMixin.h"
46 #include "OSGBillboard.h"
48 #include "OSGSwitch.h"
49 #include "OSGTransform.h"
58 typedef OSG::Attachment Parent
;
60 // TODO rename it to VRMLGenericAtt ????
61 static const OSG::Char8
*getTypeName (void) { return "TestAtt"; }
62 static const OSG::Char8
*getParentTypeName (void)
66 static const OSG::Char8
*getGroupName (void) { return "TestGenAtt"; }
68 static OSG::InitContainerF
getInitMethod(void) { return NULL
; }
70 static OSG::FieldDescriptionBase
**getDesc (void) { return NULL
; }
73 typedef OSG::DynFieldAttachment
<TestAttDesc
> TestAtt
;
74 typedef TestAtt::ObjTransitPtr TestAttTransitPtr
;
75 typedef TestAtt::ObjRecPtr TestAttRecPtr
;
76 typedef TestAtt::ObjUnrecPtr TestAttUnrecPtr
;
77 typedef TestAtt::ObjWeakPtr TestAttWeakPtr
;
79 OSG_DYNFIELDATTACHMENT_INST(TestAttDesc
)
88 OSG::NodeUnrecPtr n
= OSG::Node::create();
95 OSG::NodeUnrecPtr root
= OSG::Node::create();
96 OSG::NodeUnrecPtr child_node
= OSG::Node::create();
97 root
->addChild(child_node
);
98 OSG::setName(root
, "root");
99 OSG::setName(child_node
, "child_node");
101 OSG::NodeUnrecPtr new_root
= OSG::cloneTree(root
);
103 CHECK(new_root
->getNChildren() == 1);
104 CHECK(new_root
!= root
);
105 CHECK(new_root
->getChild(0) != child_node
);
107 std::string new_name
= OSG::getName(new_root
);
108 CHECK(new_name
== "root");
111 // build a test tree (left is top)
113 // Group01 - Switch01 ---- Billboard01
117 // + Transform01 - Group04
119 // - Group02 ----- Billboard02
121 // ----- Switch02 ---- Group05
129 g01Node
= OSG::makeCoredNode
<OSG::Group
>(&g01Core
);
130 s01Node
= OSG::makeCoredNode
<OSG::Switch
>(&s01Core
);
131 t01Node
= OSG::makeCoredNode
<OSG::Transform
>(&t01Core
);
132 g02Node
= OSG::makeCoredNode
<OSG::Group
>(&g02Core
);
133 b01Node
= OSG::makeCoredNode
<OSG::Billboard
>(&b01Core
);
134 g03Node
= OSG::makeCoredNode
<OSG::Group
>(&g03Core
);
135 g04Node
= OSG::makeCoredNode
<OSG::Group
>(&g04Core
);
136 b02Node
= OSG::makeCoredNode
<OSG::Billboard
>(&b02Core
);
137 s02Node
= OSG::makeCoredNode
<OSG::Switch
>(&s02Core
);
138 g05Node
= OSG::makeCoredNode
<OSG::Group
>(&g05Core
);
139 g06Node
= OSG::makeCoredNode
<OSG::Group
>(&g06Core
);
141 g01Node
->addChild(s01Node
);
142 g01Node
->addChild(t01Node
);
143 g01Node
->addChild(g02Node
);
144 s01Node
->addChild(b01Node
);
145 s01Node
->addChild(g03Node
);
146 t01Node
->addChild(g04Node
);
147 g02Node
->addChild(b02Node
);
148 g02Node
->addChild(s02Node
);
149 s02Node
->addChild(g05Node
);
150 s02Node
->addChild(g06Node
);
179 OSG::commitChanges();
182 OSG::GroupRecPtr g01Core
;
183 OSG::SwitchRecPtr s01Core
;
184 OSG::TransformRecPtr t01Core
;
185 OSG::GroupRecPtr g02Core
;
186 OSG::BillboardRecPtr b01Core
;
187 OSG::GroupRecPtr g03Core
;
188 OSG::GroupRecPtr g04Core
;
189 OSG::BillboardRecPtr b02Core
;
190 OSG::SwitchRecPtr s02Core
;
191 OSG::GroupRecPtr g05Core
;
192 OSG::GroupRecPtr g06Core
;
194 OSG::NodeRecPtr g01Node
;
195 OSG::NodeRecPtr s01Node
;
196 OSG::NodeRecPtr t01Node
;
197 OSG::NodeRecPtr g02Node
;
198 OSG::NodeRecPtr b01Node
;
199 OSG::NodeRecPtr g03Node
;
200 OSG::NodeRecPtr g04Node
;
201 OSG::NodeRecPtr b02Node
;
202 OSG::NodeRecPtr s02Node
;
203 OSG::NodeRecPtr g05Node
;
204 OSG::NodeRecPtr g06Node
;
207 TEST_FIXTURE(CloneFixture
, CloneTree
)
209 // test cloneTree - all shared
210 OSG::NodeUnrecPtr clone01
= OSG::cloneTree(g01Node
);
212 CHECK(clone01
!= g01Node
);
213 CHECK(clone01
->getCore() == g01Core
);
214 CHECK(clone01
->getChild(0)->getCore() == s01Core
);
215 CHECK(clone01
->getChild(1)->getChild(0)->getCore() == g04Core
);
216 CHECK(clone01
->getChild(2)->getCore() == g02Core
);
218 // test cloneTree - clone Switch
219 OSG::NodeUnrecPtr clone02
= OSG::cloneTree(g01Node
, "Switch", "");
221 CHECK(clone02
->getCore() == g01Core
);
222 CHECK(clone02
->getChild(0)->getCore() != s01Core
);
223 CHECK(OSG::osgStringCmp(clone02
->getChild(0)->getCore()->getType().getCName(),
225 CHECK(clone02
->getChild(1)->getChild(0)->getCore() == g04Core
);
226 CHECK(clone02
->getChild(2)->getCore() == g02Core
);
228 // test cloneTree - clone Billboard, ignore Switch
229 OSG::NodeUnrecPtr clone03
= OSG::cloneTree(g01Node
, "Billboard", "Switch");
231 CHECK(clone03
->getCore() == g01Core
);
232 CHECK(clone03
->getChild(0)->getChild(0)->getCore() != b01Core
);
233 CHECK(clone03
->getChild(0)->getCore() == NULL
);
234 CHECK(clone03
->getChild(2)->getChild(0)->getCore() != b02Core
);
235 CHECK(clone03
->getChild(2)->getChild(1)->getCore() == NULL
);
238 TEST_FIXTURE(CloneFixture
, DeepCloneTree
)
240 // test deepCloneTree - all cloned
241 OSG::NodeUnrecPtr clone01
= OSG::deepCloneTree(g01Node
);
243 CHECK(clone01
!= g01Node
);
244 CHECK(clone01
->getCore() != g01Core
);
245 CHECK(OSG::osgStringCmp(clone01
->getCore()->getType().getCName(),
247 CHECK(clone01
->getChild(0) != s01Node
);
248 CHECK(clone01
->getChild(0)->getCore() != s01Core
);
249 CHECK(OSG::osgStringCmp(clone01
->getChild(0)->getCore()->getType().getCName(),
252 // test deepCloneTree - share Switch
253 OSG::NodeUnrecPtr clone02
= OSG::deepCloneTree(g01Node
, "Switch");
255 CHECK(clone02
->getCore() != g01Core
);
256 CHECK(clone02
->getChild(0) != s01Node
);
257 CHECK(clone02
->getChild(0)->getCore() == s01Core
);
259 // test deepCloneTree - share Billboard, ignore Switch
260 OSG::NodeUnrecPtr clone03
= OSG::deepCloneTree(g01Node
, "Billboard", "Switch");
262 CHECK(clone03
->getCore() != g01Core
);
263 CHECK(clone03
->getChild(0)->getCore() == NULL
);
264 CHECK(clone03
->getChild(0)->getChild(0)->getCore() == b01Core
);
265 CHECK(clone03
->getChild(2)->getChild(0)->getCore() == b02Core
);
266 CHECK(clone03
->getChild(2)->getChild(1)->getCore() == NULL
);
269 TEST(DynFieldAttachment
)
271 OSG::TestAttUnrecPtr pT
= OSG::TestAtt::create();
273 OSG::FieldDescriptionBase
*pDesc
= NULL
;
275 pDesc
= new OSG::SFInt32::Description(
276 OSG::SFInt32::getClassType(),
282 OSG::Field::SFDefaultFlags
,
283 static_cast<OSG::FieldIndexEditMethodSig
>(
284 &OSG::TestAtt::editDynamicField
),
285 static_cast<OSG::FieldIndexGetMethodSig
>(
286 &OSG::TestAtt::getDynamicField
));
290 OSG::UInt32 fIndex
= pT
->addField(*pDesc
);
294 OSG::GetFieldHandlePtr pFI
= pT
->getDynamicField(fIndex
);
298 OSG::GetFieldHandlePtr pFN
= pT
->getDynamicFieldByName("foo");