NTRT Simulator  Version: Master
 All Classes Namespaces Files Functions Variables Typedefs Friends Pages
tgBlockField.cpp
Go to the documentation of this file.
1 /*
2  * Copyright © 2014, United States Government, as represented by the
3  * Administrator of the National Aeronautics and Space Administration.
4  * All rights reserved.
5  *
6  * The NASA Tensegrity Robotics Toolkit (NTRT) v1 platform is licensed
7  * under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * http://www.apache.org/licenses/LICENSE-2.0.
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
15  * either express or implied. See the License for the specific language
16  * governing permissions and limitations under the License.
17  */
18 
25 // This module
26 #include "tgBlockField.h"
27 // This library
28 #include "core/tgBox.h"
29 #include "tgcreator/tgBuildSpec.h"
30 #include "tgcreator/tgBoxInfo.h"
31 #include "tgcreator/tgStructure.h"
33 #include "tgcreator/tgNode.h"
34 #include "tgcreator/tgUtil.h"
35 // The Bullet Physics library
36 #include "LinearMath/btVector3.h"
37 // The C++ Standard Library
38 #include <stdexcept>
39 #include <vector>
40 #include <numeric> // RAND_MAX
41 
42 tgBlockField::Config::Config(btVector3 origin,
43  btScalar friction,
44  btScalar restitution,
45  btVector3 minPos,
46  btVector3 maxPos,
47  size_t nBlocks,
48  double blockLength,
49  double blockWidth,
50  double blockHeight) :
51 m_origin(origin),
52 m_friction(friction),
53 m_restitution(restitution),
54 m_minPos(minPos),
55 m_maxPos(maxPos),
56 m_nBlocks(nBlocks),
57 m_length(blockLength),
58 m_width(blockWidth),
59 m_height(blockHeight)
60 {
61  assert(m_friction >= 0.0);
62  assert(m_restitution >= 0.0);
63  assert(m_nBlocks >= 0.0);
64  assert(m_length >= 0.0);
65  assert(m_width >= 0.0);
66  assert(m_height >= 0.0);
67 }
68 
70 tgModel(),
71 m_config()
72 {
73  // Seed the random number generator
75  tgUtil::seedRandom(1);
76 }
77 
79 tgModel(),
80 m_config(config)
81 {
82  // Seed the random number generator
84  tgUtil::seedRandom(1);
85 }
86 
88 
90  // Density and roll friction are set to zero (respectively)
91  const tgBox::Config boxConfig(m_config.m_width / 2.0, m_config.m_height / 2.0, 0.0, m_config.m_friction, 0.0, m_config.m_restitution);
92 
93  // Start creating the structure
94  tgStructure s;
95  addNodes(s);
96 
97  // Create the build spec that uses tags to turn the structure into a real model
98  tgBuildSpec spec;
99  spec.addBuilder("box", new tgBoxInfo(boxConfig));
100 
101  // Create your structureInfo
102  tgStructureInfo structureInfo(s, spec);
103 
104  // Use the structureInfo to build ourselves
105  structureInfo.buildInto(*this, world);
106 
107  // Actually setup the children
108  tgModel::setup(world);
109 }
110 
111 void tgBlockField::step(double dt) {
112  // Precondition
113  if (dt <= 0.0) {
114  throw std::invalid_argument("dt is not positive");
115  }
116  else {
117  tgModel::step(dt); // Step any children
118  }
119 }
120 
122  tgModel::onVisit(r);
123 }
124 
127 }
128 
129 // Nodes: center points of opposing faces of rectangles
130 void tgBlockField::addNodes(tgStructure& s) {
131 
132  btVector3 fieldSize = m_config.m_maxPos - m_config.m_minPos;
133 
134  for(size_t i = 0; i < 2 * m_config.m_nBlocks; i += 2) {
135  double xOffset = fieldSize.getX() * rand() / RAND_MAX;
136  double yOffset = fieldSize.getY() * rand() / RAND_MAX;
137  double zOffset = fieldSize.getZ() * rand() / RAND_MAX;
138 
139  btVector3 offset(xOffset, yOffset, zOffset);
140 
141  tgNode position = m_config.m_minPos + offset;
142  s.addNode(position);
143  position += btVector3(0.0, 0.0, m_config.m_length);
144  s.addNode(position);
145 
146  s.addPair(i, i+1, "box");
147  }
148 
149  s.move(m_config.m_origin); // Set center of field to desired origin position
150 }
151 
Create a box shape as an obstacle or add it to your tensegrity.
virtual void teardown()
Definition: tgModel.cpp:68
virtual void setup(tgWorld &world)
Definition: tgModel.cpp:57
virtual void step(double dt)
Definition: tgModel.cpp:84
virtual void onVisit(const tgModelVisitor &r) const
Definition: tgModel.cpp:107
Class that interfaces with Bullet to build the boxes.
void addPair(int fromNodeIdx, int toNodeIdx, std::string tags="")
Definition: tgStructure.cpp:80
virtual void step(double dt)
virtual ~tgBlockField()
virtual void setup(tgWorld &world)
Definition of class tgNode.
Contains the definition of class tgBlockField. A random field of blocks used to test tensegrities...
Definition: tgNode.h:45
Definition of class tgStructure.
Definition of class tgStructureInfo.
Rand seeding simular to the evolution and terrain classes.
Definition of class tgBuildSpec.
void buildInto(tgModel &model, tgWorld &world)
void addNode(double x, double y, double z, std::string tags="")
Definition: tgStructure.cpp:70
virtual void onVisit(tgModelVisitor &r)