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 
98 void
99 controller::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(
125  new controller::RemoveOperation(getMemoryXController(),
126  getScene(),
127  objectName,
128  objectCollection,
129  objectPosition,
130  objectRotation,
131  getObjectId()));
132  return inverseOperation;
133 }
134 
135 void
137 {
138 
139  memoryxcontroller::MemoryXControllerPtr memoryXController = getMemoryXController();
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 
158 void
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 }
RemoveOperation.h
controller::AddOperation::createInverseOperation
const OperationPtr createInverseOperation() const override
Returns a new operation which removes the object, which will be added in this operation.
Definition: AddOperation.cpp:122
controller::Operation::getObjectId
std::string getObjectId() const
Returns the ID of the object, this Operation is working on.
Definition: Operation.cpp:54
controller::AddOperation::executeOnWorkingMemory
void executeOnWorkingMemory() override
Adds the object to the WorkingMemory.
Definition: AddOperation.cpp:136
AddOperation.h
scene3D::SceneObject
Definition: SceneObject.h:43
IceInternal::Handle< ObjectClass >
controller::AddOperation::AddOperation
AddOperation(const AddOperation &operation)
Creates a new operation, which is a copy of the given operation, but has a new random ID.
Definition: AddOperation.cpp:39
ARMARX_WARNING_S
#define ARMARX_WARNING_S
Definition: Logging.h:213
memoryxcontroller::PriorKnowledgeControllerPtr
std::shared_ptr< PriorKnowledgeController > PriorKnowledgeControllerPtr
Definition: PriorKnowledgeController.h:144
controller::AddOperation::executeOnScene
void executeOnScene() override
Adds the object to the local Scene.
Definition: AddOperation.cpp:159
ARMARX_CHECK_EXPRESSION
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
Definition: ExpressionException.h:73
controller::OperationPtr
std::shared_ptr< Operation > OperationPtr
Definition: ClassDefinitions.h:54
scene3D::SceneObjectPtr
boost::intrusive_ptr< SceneObject > SceneObjectPtr
Definition: PointerDefinitions.h:40
controller::RemoveOperation
A Operation to remove a object in the scene and the WorkingMemory.
Definition: RemoveOperation.h:47
controller::AddOperation
A Operation to create a new object in the scene and the WorkingMemory.
Definition: AddOperation.h:46
controller::Operation
An abstract class, which offers methods to run operations on the WorkingMemory and the Scene.
Definition: Operation.h:44
memoryxcontroller::MemoryXControllerPtr
std::shared_ptr< MemoryXController > MemoryXControllerPtr
Definition: MemoryXController.h:149