AddOperation.cpp
Go to the documentation of this file.
1/*
2 * This file is part of ArmarX.
3 *
4 * Copyright (C) 2012-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5 *
6 * ArmarX is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * ArmarX is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * @package MemoryX::gui-plugins::SceneEditor
19 * @date 2015
20 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
21 * GNU General Public License
22 */
23
24// std
25#include <stdexcept>
26#include <string>
27
28// scene3D
29#include <Inventor/SbRotation.h>
30#include <Inventor/SbVec3f.h>
31#include <Inventor/nodes/SoCone.h>
32#include <Inventor/nodes/SoMaterial.h>
33#include <Inventor/nodes/SoSeparator.h>
34
35// local
36#include "AddOperation.h"
37#include "RemoveOperation.h"
38
40 Operation(operation.getMemoryXController(), operation.getScene()),
41 objectName(operation.objectName),
42 objectCollection(operation.objectCollection),
43 objectPosition(operation.objectPosition),
44 objectRotation(operation.objectRotation)
45{
46 while (getObjectId() == operation.getObjectId())
47 {
48 createRandomId();
49 }
50}
51
53 const std::shared_ptr<memoryxcontroller::MemoryXController>& memoryXController,
54 const std::shared_ptr<scene3D::Scene>& scene,
55 const std::string& objectName,
56 const std::string& objectCollection,
57 const SbVec3f& objectPosition,
58 const SbRotation& objectRotation,
59 const std::string& objectId) :
60 Operation(memoryXController, scene, objectId),
61 objectName(objectName),
62 objectCollection(objectCollection),
63 objectPosition(objectPosition),
64 objectRotation(objectRotation)
65{
66}
67
69 const std::shared_ptr<memoryxcontroller::MemoryXController>& memoryXController,
70 const std::shared_ptr<scene3D::Scene>& scene,
71 const std::string& objectName,
72 const std::string& objectCollection,
73 const SbVec3f& objectPosition,
74 const SbRotation& objectRotation) :
75 Operation(memoryXController, scene),
76 objectName(objectName),
77 objectCollection(objectCollection),
78 objectPosition(objectPosition),
79 objectRotation(objectRotation)
80{
81 createRandomId();
82}
83
85 const std::shared_ptr<memoryxcontroller::MemoryXController>& memoryXController,
86 const std::shared_ptr<scene3D::Scene>& scene,
87 const std::string& objectName,
88 const std::string& objectCollection) :
89 Operation(memoryXController, scene),
90 objectName(objectName),
91 objectCollection(objectCollection),
92 objectPosition(0, 0, 0),
93 objectRotation(SbVec3f(1, 0, 0), 0)
94{
95 createRandomId();
96}
97
98void
99controller::AddOperation::createRandomId()
100{
101 static const char chars[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
102 const int length = 20;
103
104 std::shared_ptr<scene3D::Scene> scene = getScene();
105 char random[length + 1];
106
107 do
108 {
109 for (int i = 0; i < length; ++i)
110 {
111 random[i] = chars[rand() % (sizeof(chars) - 1)];
112 }
113
114 random[length] = 0;
115 } while (scene->getObjectManager()->getObjectById(random));
116
117 std::string id = random;
118 setObjectId(id);
119}
120
123{
124 std::shared_ptr<controller::Operation> inverseOperation(
126 getScene(),
127 objectName,
128 objectCollection,
129 objectPosition,
130 objectRotation,
131 getObjectId()));
132 return inverseOperation;
133}
134
135void
137{
138
140 ARMARX_CHECK_EXPRESSION(memoryXController);
141
142 ARMARX_CHECK_EXPRESSION(memoryXController->getPriorKnowlegdeController());
143 memoryx::ObjectClassPtr objectClass =
144 memoryXController->getPriorKnowlegdeController()->getObjectClassPtr(objectName,
145 objectCollection);
146 ARMARX_CHECK_EXPRESSION(objectClass);
147
148
149 std::string newObjectId =
150 memoryXController->getWorkingMemoryController()->addObjectInstance(objectName, objectClass);
151 setObjectId(newObjectId);
152 ARMARX_CHECK_EXPRESSION(getMemoryXController()->getWorkingMemoryController());
153
154 getMemoryXController()->getWorkingMemoryController()->rotateTranslateObject(
155 newObjectId, objectRotation, objectPosition);
156}
157
158void
160{
161 std::shared_ptr<scene3D::Scene> scene = getScene();
162 memoryxcontroller::PriorKnowledgeControllerPtr priorKnowledgeController =
163 getMemoryXController()->getPriorKnowlegdeController();
164 memoryx::ObjectClassPtr objectClass =
165 priorKnowledgeController->getObjectClassPtr(objectName, objectCollection);
166 SoNode* geometryNode = priorKnowledgeController->getCoinVisualisation(objectClass, false);
167 SoNode* collisionNode = priorKnowledgeController->getCoinVisualisation(objectClass, true);
168
169 if (!collisionNode)
170 {
171 ARMARX_WARNING_S << "object \"" << objectName << "\" has no collison model";
172 collisionNode = new SoSeparator;
173 SoMaterial* material = new SoMaterial;
174 material->ambientColor.setValue(1.0f, 0.0f, 0.0f);
175 material->diffuseColor.setValue(1.0f, 0.0f, 0.0f);
176 SoCone* cone = new SoCone;
177 cone->bottomRadius.setValue(0.03);
178 cone->height.setValue(0.3);
179 ((SoSeparator*)collisionNode)->addChild(material);
180 ((SoSeparator*)collisionNode)->addChild(cone);
181 }
182
183 if (!geometryNode)
184 {
185 ARMARX_WARNING_S << "object \"" << objectName << "\" has no visualisation model";
186 geometryNode = new SoSeparator;
187 SoMaterial* material = new SoMaterial;
188 material->ambientColor.setValue(1.0f, 0.0f, 0.0f);
189 material->diffuseColor.setValue(1.0f, 0.0f, 0.0f);
190 SoCone* cone = new SoCone;
191 cone->bottomRadius.setValue(0.03);
192 cone->height.setValue(0.3);
193 ((SoSeparator*)geometryNode)->addChild(material);
194 ((SoSeparator*)geometryNode)->addChild(cone);
195 }
196
197 SoSeparator* geometry = new SoSeparator();
198 geometry->addChild(geometryNode);
199 SoSeparator* collision = new SoSeparator();
200 collision->addChild(collisionNode);
201
203 new scene3D::SceneObject(getObjectId(), objectName, objectCollection, geometry, collision));
204 object->setRotation(objectRotation);
205 object->setTranslation(objectPosition);
206 scene->getObjectManager()->addObject(object);
207}
A Operation to create a new object in the scene and the WorkingMemory.
const OperationPtr createInverseOperation() const override
Returns a new operation which removes the object, which will be added in this operation.
void executeOnScene() override
Adds the object to the local Scene.
AddOperation(const AddOperation &operation)
Creates a new operation, which is a copy of the given operation, but has a new random ID.
void executeOnWorkingMemory() override
Adds the object to the WorkingMemory.
std::string getObjectId() const
Returns the ID of the object, this Operation is working on.
Definition Operation.cpp:54
const std::shared_ptr< memoryxcontroller::MemoryXController > getMemoryXController() const
Returns the the MemoryXController to execute the Operation at.
Definition Operation.cpp:48
const std::shared_ptr< scene3D::Scene > getScene() const
Returns the the Scene to execute the Operation at.
Definition Operation.cpp:42
Operation(const std::shared_ptr< memoryxcontroller::MemoryXController > &memoryXController, const std::shared_ptr< scene3D::Scene > &scene, const std::string &objectId)
A constructor.
Definition Operation.cpp:26
void setObjectId(const std::string &objectId)
Sets the ID, this operation is working on.
Definition Operation.cpp:60
A Operation to remove a object in the scene and the WorkingMemory.
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
#define ARMARX_WARNING_S
The logging level for unexpected behaviour, but not a serious problem.
Definition Logging.h:213
std::shared_ptr< Operation > OperationPtr
IceInternal::Handle< ObjectClass > ObjectClassPtr
Definition ObjectClass.h:35
std::shared_ptr< PriorKnowledgeController > PriorKnowledgeControllerPtr
std::shared_ptr< MemoryXController > MemoryXControllerPtr
boost::intrusive_ptr< SceneObject > SceneObjectPtr