NTRT Simulator  Version: Master
 All Classes Namespaces Files Functions Variables Typedefs Friends Pages
HilbertTGStructure.h
1 /*
2  * Copyright © 2012, 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 
19 // Adapted from http://www.fundza.com/algorithmic/space_filling/hilbert/basics/index.html
20 /*
21 procedure hilbert(x, y, xi, xj, yi, yj, n)
22 // x and y are the coordinates of the bottom left corner
23 // xi & xj are the i & j components of the unit x vector of the frame
24 // similarly yi and yj
25 if (n <= 0) then
26  LineTo(x + (xi + yi)/2, y + (xj + yj)/2);
27 else
28  {
29  hilbert(x, y, yi/2, yj/2, xi/2, xj/2, n-1);
30  hilbert(x+xi/2, y+xj/2 , xi/2, xj/2, yi/2, yj/2, n-1);
31  hilbert(x+xi/2+yi/2, y+xj/2+yj/2, xi/2, xj/2, yi/2, yj/2, n-1);
32  hilbert(x+xi/2+yi, y+xj/2+yj, -yi/2,-yj/2, -xi/2, -xj/2, n-1);
33  }
34 end procedure;
35 */
36 
37 #include "core/tgModel.h"
38 
39 #include "core/tgSubject.h"
40 
41 #include "tgcreator/tgPairs.h"
42 #include "tgcreator/tgUtil.h"
43 #include "core/tgRod.h"
44 #include "tgcreator/tgRodInfo.h"
45 
46 #include "tgcreator/tgBuildSpec.h"
47 #include "tgcreator/tgStructure.h"
49 
50 // The C++ Standard Library
51 #include <iostream>
52 
53 class SingleRibModel : public tgSubject<SingleRibModel>, public tgModel
54 {
55 public:
56 
57  SingleRibModel(int n = 1, double xsize = 10, double ysize = 0) :
58  m_n(n),
59  m_xsize(xsize),
60  m_ysize(ysize)
61  {
62  if(m_ysize == 0) {
63  m_ysize = m_xsize;
64  }
65 
66  }
67 
68  virtual void setup(tgWorld& world)
69  {
70 
71  tgStructure tetra;
72 
73  hilbert(tetra, 0,0,m_xsize,0,0,m_ysize,m_n);
74 
75  makePairs(tetra);
76 
77  // Just get it out of the way of the other structure
78  tetra.move(btVector3(25.0, 0, 0));
79 
80  const double density = 0.9;
81  const double radius = 0.5 / m_n; // divide by the number of iterations to keep the radius in check
82  const tgRod::Config rodConfig(radius, density);
83 
84  tgBuildSpec spec;
85  spec.addBuilder("rod", new tgRodInfo(rodConfig));
86 
87  // Create your structureInfo
88  tgStructureInfo structureInfo(&tetra, &spec);
89 
90  // Use the structureInfo to build ourselves
91  structureInfo.buildInto(this, world);
92  }
93 
94 
95  void hilbert(tgStructure& tetra, double x, double y, double xi, double xj, double yi, double yj, int n)
96  {
97  std::cout << x << " " << y << " " << xi << " " << xj << " " << yi << " " << yj << std::endl;
98  if(n <= 0) {
99  tgNode node(point(x + (xi + yi)/2, y + (xj + yj)/2));
100  tetra.addNode( node ); // what to do here?
101  } else {
102  hilbert(tetra, x, y, yi/2, yj/2, xi/2, xj/2, n-1);
103  hilbert(tetra, x+xi/2, y+xj/2 , xi/2, xj/2, yi/2, yj/2, n-1);
104  hilbert(tetra, x+xi/2+yi/2, y+xj/2+yj/2, xi/2, xj/2, yi/2, yj/2, n-1);
105  hilbert(tetra, x+xi/2+yi, y+xj/2+yj, -yi/2,-yj/2, -xi/2, -xj/2, n-1);
106  }
107  }
108 
109  void makePairs(tgStructure& tetra)
110  {
111  size_t n = tetra.getNodes().size();
112  std::cout << "Nodes size is " << n << std::endl;
113  for(int i = 1; i < n; i++) {
114  tetra.addPair(i-1, i, "rod");
115  }
116  }
117 
118  btVector3 point(double x, double y)
119  {
120  return btVector3(x, y, 0);
121  }
122 
123 
124 private:
125  int m_n;
126  double m_xsize;
127  double m_ysize;
128 
129 };
Definition of class tgRodInfo.
Definition of tgSubject class.
const tgNodes & getNodes() const
Definition: tgStructure.h:138
Definition of class tgPairs.
Contains the definition of class tgModel.
void addPair(int fromNodeIdx, int toNodeIdx, std::string tags="")
Definition: tgStructure.cpp:80
Definition: tgNode.h:45
Definition of class tgStructure.
Definition of class tgStructureInfo.
virtual void setup(tgWorld &world)
Rand seeding simular to the evolution and terrain classes.
Contains the definition of class tgRod.
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