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/SbVec3f.h>
30 #include <Inventor/SbRotation.h>
31 #include <Inventor/nodes/SoSeparator.h>
32 #include <Inventor/nodes/SoCone.h>
33 #include <Inventor/nodes/SoMaterial.h>
34 
35 // local
36 #include "AddOperation.h"
37 #include "RemoveOperation.h"
38 
39 
41  Operation(operation.getMemoryXController(), operation.getScene()),
42  objectName(operation.objectName),
43  objectCollection(operation.objectCollection),
44  objectPosition(operation.objectPosition),
45  objectRotation(operation.objectRotation)
46 {
47  while (getObjectId() == operation.getObjectId())
48  {
49  createRandomId();
50  }
51 }
52 
53 controller::AddOperation::AddOperation(const std::shared_ptr<memoryxcontroller::MemoryXController>& memoryXController, const std::shared_ptr<scene3D::Scene>& scene, const std::string& objectName, const std::string& objectCollection, const SbVec3f& objectPosition, const SbRotation& objectRotation, const std::string& objectId) :
54  Operation(memoryXController, scene, objectId),
55  objectName(objectName),
56  objectCollection(objectCollection),
57  objectPosition(objectPosition),
58  objectRotation(objectRotation)
59 {
60 
61 }
62 
63 controller::AddOperation::AddOperation(const std::shared_ptr<memoryxcontroller::MemoryXController>& memoryXController, const std::shared_ptr<scene3D::Scene>& scene, const std::string& objectName, const std::string& objectCollection, const SbVec3f& objectPosition, const SbRotation& objectRotation) :
64  Operation(memoryXController, scene),
65  objectName(objectName),
66  objectCollection(objectCollection),
67  objectPosition(objectPosition),
68  objectRotation(objectRotation)
69 {
70  createRandomId();
71 }
72 
73 controller::AddOperation::AddOperation(const std::shared_ptr<memoryxcontroller::MemoryXController>& memoryXController, const std::shared_ptr<scene3D::Scene>& scene, const std::string& objectName, const std::string& objectCollection) :
74  Operation(memoryXController, scene),
75  objectName(objectName),
76  objectCollection(objectCollection),
77  objectPosition(0, 0, 0),
78  objectRotation(SbVec3f(1, 0, 0), 0)
79 {
80  createRandomId();
81 }
82 
83 void controller::AddOperation::createRandomId()
84 {
85  static const char chars[] =
86  "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
87  const int length = 20;
88 
89  std::shared_ptr<scene3D::Scene> scene = getScene();
90  char random[length + 1];
91 
92  do
93  {
94  for (int i = 0; i < length; ++i)
95  {
96  random[i] = chars[rand() % (sizeof(chars) - 1)];
97  }
98 
99  random[length] = 0;
100  }
101  while (scene->getObjectManager()->getObjectById(random));
102 
103  std::string id = random;
104  setObjectId(id);
105 }
106 
108 {
109  std::shared_ptr<controller::Operation> inverseOperation(new controller::RemoveOperation(getMemoryXController(), getScene(), objectName, objectCollection, objectPosition, objectRotation, getObjectId()));
110  return inverseOperation;
111 }
112 
114 {
115 
116  memoryxcontroller::MemoryXControllerPtr memoryXController = getMemoryXController();
117  ARMARX_CHECK_EXPRESSION(memoryXController);
118 
119  ARMARX_CHECK_EXPRESSION(memoryXController->getPriorKnowlegdeController());
120  memoryx::ObjectClassPtr objectClass = memoryXController->getPriorKnowlegdeController()->getObjectClassPtr(objectName, objectCollection);
121  ARMARX_CHECK_EXPRESSION(objectClass);
122 
123 
124  std::string newObjectId = memoryXController->getWorkingMemoryController()->addObjectInstance(objectName, objectClass);
125  setObjectId(newObjectId);
126  ARMARX_CHECK_EXPRESSION(getMemoryXController()->getWorkingMemoryController());
127 
128  getMemoryXController()->getWorkingMemoryController()->rotateTranslateObject(newObjectId, objectRotation, objectPosition);
129 }
130 
132 {
133  std::shared_ptr<scene3D::Scene> scene = getScene();
134  memoryxcontroller::PriorKnowledgeControllerPtr priorKnowledgeController = getMemoryXController()->getPriorKnowlegdeController();
135  memoryx::ObjectClassPtr objectClass = priorKnowledgeController->getObjectClassPtr(objectName, objectCollection);
136  SoNode* geometryNode = priorKnowledgeController->getCoinVisualisation(objectClass, false);
137  SoNode* collisionNode = priorKnowledgeController->getCoinVisualisation(objectClass, true);
138 
139  if (!collisionNode)
140  {
141  ARMARX_WARNING_S << "object \"" << objectName << "\" has no collison model";
142  collisionNode = new SoSeparator;
143  SoMaterial* material = new SoMaterial;
144  material->ambientColor.setValue(1.0f, 0.0f, 0.0f);
145  material->diffuseColor.setValue(1.0f, 0.0f, 0.0f);
146  SoCone* cone = new SoCone;
147  cone->bottomRadius.setValue(0.03);
148  cone->height.setValue(0.3);
149  ((SoSeparator*)collisionNode)->addChild(material);
150  ((SoSeparator*)collisionNode)->addChild(cone);
151  }
152 
153  if (!geometryNode)
154  {
155  ARMARX_WARNING_S << "object \"" << objectName << "\" has no visualisation model";
156  geometryNode = new SoSeparator;
157  SoMaterial* material = new SoMaterial;
158  material->ambientColor.setValue(1.0f, 0.0f, 0.0f);
159  material->diffuseColor.setValue(1.0f, 0.0f, 0.0f);
160  SoCone* cone = new SoCone;
161  cone->bottomRadius.setValue(0.03);
162  cone->height.setValue(0.3);
163  ((SoSeparator*)geometryNode)->addChild(material);
164  ((SoSeparator*)geometryNode)->addChild(cone);
165  }
166 
167  SoSeparator* geometry = new SoSeparator();
168  geometry->addChild(geometryNode);
169  SoSeparator* collision = new SoSeparator();
170  collision->addChild(collisionNode);
171 
172  scene3D::SceneObjectPtr object(new scene3D::SceneObject(getObjectId(), objectName, objectCollection, geometry, collision));
173  object->setRotation(objectRotation);
174  object->setTranslation(objectPosition);
175  scene->getObjectManager()->addObject(object);
176 
177 }
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:107
controller::Operation::getObjectId
std::string getObjectId() const
Returns the ID of the object, this Operation is working on.
Definition: Operation.cpp:52
controller::AddOperation::executeOnWorkingMemory
void executeOnWorkingMemory() override
Adds the object to the WorkingMemory.
Definition: AddOperation.cpp:113
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:40
ARMARX_WARNING_S
#define ARMARX_WARNING_S
Definition: Logging.h:206
memoryxcontroller::PriorKnowledgeControllerPtr
std::shared_ptr< PriorKnowledgeController > PriorKnowledgeControllerPtr
Definition: PriorKnowledgeController.h:140
controller::AddOperation::executeOnScene
void executeOnScene() override
Adds the object to the local Scene.
Definition: AddOperation.cpp:131
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:146