NTRT Simulator  Version: Master
 All Classes Namespaces Files Functions Variables Typedefs Friends Pages
Hilbert2DModel.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 #include "tgcreator/tgNodes.h"
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 
51 class Hilbert2DModel : public tgSubject<Hilbert2DModel>, public tgModel
52 {
53 public:
54 
55  Hilbert2DModel(int n = 1, double xsize = 10, double ysize = 0) :
56  m_n(n),
57  m_xsize(xsize),
58  m_ysize(ysize)
59  {
60  if(m_ysize == 0) {
61  m_ysize = m_xsize;
62  }
63 
64  }
65 
66  virtual void setup(tgWorld& world)
67  {
68 
69  tgStructure tetra;
70 
71  hilbert(tetra, 0,0,m_xsize,0,0,m_ysize,m_n);
72 
73  makePairs(tetra);
74 
75  // Just get it out of the way of the other structure
76  tetra.move(btVector3(25.0, 0, 0));
77 
78  const double density = 0.9;
79  const double radius = 0.5 / m_n; // divide by the number of iterations to keep the radius in check
80  const tgRod::Config rodConfig(radius, density);
81 
82  tgBuildSpec spec;
83  spec.addBuilder("rod", new tgRodInfo(rodConfig));
84 
85  // Create your structureInfo
86  tgStructureInfo structureInfo(tetra, spec);
87 
88  // Use the structureInfo to build ourselves
89  structureInfo.buildInto(*this, world);
90  }
91 
92 
93  void hilbert(tgStructure& tetra, double x, double y, double xi, double xj, double yi, double yj, int n)
94  {
95  std::cout << x << " " << y << " " << xi << " " << xj << " " << yi << " " << yj << std::endl;
96  if(n <= 0) {
97  tgNode node(point(x + (xi + yi)/2, y + (xj + yj)/2));
98  tetra.addNode( node ); // what to do here?
99  } else {
100  hilbert(tetra, x, y, yi/2, yj/2, xi/2, xj/2, n-1);
101  hilbert(tetra, x+xi/2, y+xj/2 , xi/2, xj/2, yi/2, yj/2, n-1);
102  hilbert(tetra, x+xi/2+yi/2, y+xj/2+yj/2, xi/2, xj/2, yi/2, yj/2, n-1);
103  hilbert(tetra, x+xi/2+yi, y+xj/2+yj, -yi/2,-yj/2, -xi/2, -xj/2, n-1);
104  }
105  }
106 
107  void makePairs(tgStructure& tetra)
108  {
109  size_t n = tetra.getNodes().size();
110  std::cout << "Nodes size is " << n << std::endl;
111  for(int i = 1; i < n; i++) {
112  tetra.addPair(i-1, i, "rod");
113  }
114  }
115 
116  btVector3 point(double x, double y)
117  {
118  return btVector3(x, y, 0);
119  }
120 
121  void makePair(btVector3 p1, btVector3 p2)
122  {
123  m_pairs.addPair(tgPair(p1, p2, "rod"));
124  }
125 
126  tgPairs& getPairs()
127  {
128  return m_pairs;
129  }
130 
131 private:
132  int m_n;
133  double m_xsize;
134  double m_ysize;
135 
136  tgNodes m_nodes;
137  tgPairs m_pairs;
138 };
Definition of class tgRodInfo.
virtual void setup(tgWorld &world)
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 of class tgNodes.
Definition: tgPair.h:48
Definition: tgNode.h:45
Definition of class tgStructure.
Definition of class tgStructureInfo.
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