Cheetah Software  1.0
DrawList.cpp
Go to the documentation of this file.
1 
8 #include "DrawList.h"
9 
11  printf("[DrawList] Load object files...\n");
12  std::vector<std::string> names = {
13  "c3_body.obj", "mini_abad.obj",
14  "c3_upper_link.obj", "c3_lower_link.obj",
15  "mini_body.obj", "mini_abad.obj",
16  "mini_upper_link.obj", "mini_lower_link.obj",
17  "sphere.obj", "cube.obj"};
18  for (const auto& name : names) {
19  std::string filename = _baseFileName + name;
20  _vertexData.emplace_back();
21  _normalData.emplace_back();
22  _colorData.emplace_back();
23  load_obj_file(filename, _vertexData.back(), _normalData.back());
24  if (name == "sphere.obj") {
25  setSolidColor(_colorData.back(), _vertexData.back().size(),
26  debugRedColor[0], debugRedColor[1], debugRedColor[2]);
27  } else if (name == "cube.obj") {
28  setSolidColor(_colorData.back(), _vertexData.back().size(),
29  disgustingGreen[0], disgustingGreen[1], disgustingGreen[2]);
30  } else {
31  setSolidColor(_colorData.back(), _vertexData.back().size(),
33  defaultRobotColor[2]);
34  }
35 
36  _nUnique++;
37  }
38  _sphereLoadIndex = 8;
39  _cubeLoadIndex = 9;
42 }
48 size_t DrawList::addCheetah3(Vec4<float> color, bool useOld) {
49  size_t i0 = _cheetah3LoadIndex;
50  size_t j0 = _nTotal;
51 
52  // set model offsets:
53  QMatrix4x4 bodyOffset, abadOffset, lowerOffset, eye;
54  eye.setToIdentity();
55  QMatrix4x4 upperOffsets[2];
56 
57  // body
58  bodyOffset.setToIdentity();
59  bodyOffset.rotate(90, 1, 0, 0);
60  bodyOffset.rotate(90, 0, 0, 1);
61 
62  // abad
63  abadOffset.setToIdentity();
64 
65  // upper link
66  upperOffsets[0].setToIdentity();
67  upperOffsets[0].rotate(-180, 0, 1, 0);
68  upperOffsets[0].translate(0, -.045f, 0);
69  upperOffsets[0].rotate(-180, 0, 0, 1);
70 
71  upperOffsets[1].setToIdentity();
72  upperOffsets[1].rotate(-180, 0, 1, 0);
73  upperOffsets[1].translate(0, .045f, 0);
74 
75  lowerOffset.setToIdentity();
76  lowerOffset.rotate(180, 0, 1, 0);
77  lowerOffset.translate(0, 0, 0);
78 
79  SolidColor bodyColor, abadColor, link1Color, link2Color;
80  bodyColor.rgba = useOld ? Vec4<float>(.2, .2, .2, .6) : color;
81  bodyColor.useSolidColor = true;
82 
83  abadColor.rgba = useOld ? Vec4<float>(.3, .2, .2, .6) : color;
84  abadColor.useSolidColor = true;
85 
86  link1Color.rgba = useOld ? Vec4<float>(.2, .3, .2, .6) : color;
87  link1Color.useSolidColor = true;
88 
89  link2Color.rgba = useOld ? Vec4<float>(.2, .2, .3, .6) : color;
90  link2Color.useSolidColor = true;
91 
92  // add bodies
93  _objectMap.push_back(i0 + 0);
94  _modelOffsets.push_back(bodyOffset);
95  _kinematicXform.push_back(eye);
96  _instanceColor.push_back(bodyColor);
97  _nTotal++;
98 
99  for (int i = 0; i < 4; i++) {
100  _objectMap.push_back(i0 + 1);
101  _modelOffsets.push_back(abadOffset);
102  _kinematicXform.push_back(eye);
103  _instanceColor.push_back(abadColor);
104 
105  _objectMap.push_back(i0 + 2);
106  _modelOffsets.push_back(upperOffsets[i % 2]);
107  _kinematicXform.push_back(eye);
108  _instanceColor.push_back(link1Color);
109 
110  _objectMap.push_back(i0 + 3);
111  _modelOffsets.push_back(lowerOffset);
112  _kinematicXform.push_back(eye);
113  _instanceColor.push_back(link2Color);
114  _nTotal += 3;
115  }
116 
117  printf("size of kinematicXform: %lu, j0: %lu\n", _kinematicXform.size(), j0);
118 
119  buildDrawList();
120  return j0;
121 }
122 
129 size_t DrawList::addMiniCheetah(Vec4<float> color, bool useOld) {
130  size_t i0 = _miniCheetahLoadIndex; // todo don't hard code this
131  size_t j0 = _nTotal;
132 
133  // set model offsets:
134  QMatrix4x4 bodyOffset, upper, lower, eye;
135  QMatrix4x4 abadOffsets[4];
136  eye.setToIdentity();
137 
138  // body
139  bodyOffset.setToIdentity();
140 
141  // abads (todo, check these)
142  abadOffsets[0].setToIdentity(); // n
143  abadOffsets[0].rotate(-90, 0, 0, 1);
144  abadOffsets[0].translate(0, -.0565f, 0);
145  abadOffsets[0].rotate(180, 0, 1, 0);
146 
147  abadOffsets[1].setToIdentity(); // p
148  abadOffsets[1].rotate(-90, 0, 0, 1);
149  abadOffsets[1].translate(0, -.0565f, 0);
150  abadOffsets[1].rotate(0, 0, 1, 0);
151 
152  abadOffsets[2].setToIdentity(); // n
153  abadOffsets[2].rotate(90, 0, 0, 1);
154  abadOffsets[2].translate(0, -.0565f, 0);
155  abadOffsets[2].rotate(0, 0, 1, 0);
156 
157  abadOffsets[3].setToIdentity(); // p
158  abadOffsets[3].rotate(90, 0, 0, 1);
159  abadOffsets[3].translate(0, -.0565f, 0);
160  abadOffsets[3].rotate(180, 0, 1, 0);
161 
162  // upper
163  upper.setToIdentity();
164  upper.rotate(-90, 0, 1, 0);
165 
166  // lower
167  lower.setToIdentity();
168  lower.rotate(180, 0, 1, 0);
169 
170  SolidColor bodyColor, abadColor, link1Color, link2Color;
171  bodyColor.rgba = useOld ? Vec4<float>(.2, .2, .2, .6) : color;
172  bodyColor.useSolidColor = true;
173 
174  abadColor.rgba = useOld ? Vec4<float>(.3, .2, .2, .6) : color;
175  abadColor.useSolidColor = true;
176 
177  link1Color.rgba = useOld ? Vec4<float>(.2, .3, .2, .6) : color;
178  link1Color.useSolidColor = true;
179 
180  link2Color.rgba = useOld ? Vec4<float>(.2, .2, .3, .6) : color;
181  link2Color.useSolidColor = true;
182 
183  // add objects
184  _objectMap.push_back(i0 + 0);
185  _modelOffsets.push_back(bodyOffset);
186  _kinematicXform.push_back(eye);
187  _instanceColor.push_back(bodyColor);
188  _nTotal++;
189 
190  for (int i = 0; i < 4; i++) {
191  _objectMap.push_back(i0 + 1);
192  _modelOffsets.push_back(abadOffsets[i]);
193  _kinematicXform.push_back(eye);
194  _instanceColor.push_back(abadColor);
195 
196  _objectMap.push_back(i0 + 2);
197  _modelOffsets.push_back(upper);
198  _kinematicXform.push_back(eye);
199  _instanceColor.push_back(link1Color);
200 
201  _objectMap.push_back(i0 + 3);
202  _modelOffsets.push_back(lower);
203  _kinematicXform.push_back(eye);
204  _instanceColor.push_back(link2Color);
205  _nTotal += 3;
206  }
207  return j0;
208 }
209 
216  size_t j0 = _nTotal;
217  size_t i0 = _nUnique;
218 
219  SolidColor checkerColor;
220  checkerColor.useSolidColor = false;
221 
222  _nUnique++;
223  // add the object
224  _vertexData.emplace_back();
225  _normalData.emplace_back();
226  _colorData.emplace_back();
227  checkerBoard.computeVertices(_vertexData.back(), _normalData.back(),
228  _colorData.back());
229  QMatrix4x4 eye, offset;
230  eye.setToIdentity();
231  offset.setToIdentity();
232  offset.translate(-checkerBoard.getSize()[0] / 2,
233  -checkerBoard.getSize()[1] / 2);
234  _modelOffsets.push_back(offset);
235  _kinematicXform.push_back(eye);
236  _instanceColor.push_back(checkerColor);
237 
238  _nTotal++;
239  // add the instance
240  _objectMap.push_back(i0);
241  return j0;
242 }
243 
247 size_t DrawList::addDebugSphere(float radius) {
248  assert(false);
249  size_t j0 = _nTotal;
250 
251  QMatrix4x4 offset;
252  offset.setToIdentity();
253  _kinematicXform.push_back(offset);
254  offset.scale(radius);
255  _modelOffsets.push_back(offset);
256 
257  _nTotal++;
258  _objectMap.push_back(_sphereLoadIndex);
259  return j0;
260 }
261 
267  _glVertexData.clear();
268  _glColorData.clear();
269  _glNormalData.clear();
270  _glArrayOffsets.clear();
271  _glArraySizes.clear();
272 
273  for (size_t i = 0; i < _nUnique; i++) {
274  _glArrayOffsets.push_back(_glVertexData.size());
275  _glArraySizes.push_back(_vertexData.at(i).size());
276  // add the data for the objects
277  _glVertexData.insert(_glVertexData.end(), _vertexData.at(i).begin(),
278  _vertexData.at(i).end());
279  _glColorData.insert(_glColorData.end(), _colorData.at(i).begin(),
280  _colorData.at(i).end());
281  _glNormalData.insert(_glNormalData.end(), _normalData.at(i).begin(),
282  _normalData.at(i).end());
283  }
284 
285  _reloadNeeded = true;
286 }
287 
288 void DrawList::addBox(double depth, double width, double height,
289  const Vec3<double>& pos, const Mat3<double>& ori,
290  bool transparent) {
291  if (transparent) {
292  BoxInfo tmp;
293  tmp.depth = depth;
294  tmp.width = width;
295  tmp.height = height;
296 
297  tmp.frame[3] = 0.;
298  tmp.frame[7] = 0.;
299  tmp.frame[11] = 0.;
300  tmp.frame[15] = 1.;
301 
302  for (size_t i(0); i < 3; ++i) {
303  for (size_t j(0); j < 3; ++j) {
304  tmp.frame[4 * i + j] = ori(j, i);
305  }
306  }
307  for (size_t i(0); i < 3; ++i) tmp.frame[12 + i] = pos[i];
308 
309  _box_list.push_back(tmp);
310  } else {
311  QMatrix4x4 offset;
312 
313  // scale box
314  offset.setToIdentity();
315  offset.scale(depth, width, height);
316  _modelOffsets.push_back(offset);
317 
318  // move box
319  offset.setToIdentity();
320  offset.translate(pos[0], pos[1], pos[2]);
321  Quat<double> q = ori::rotationMatrixToQuaternion(ori.transpose());
322  QQuaternion qq(q[0], q[1], q[2], q[3]);
323  offset.rotate(qq);
324 
325  _kinematicXform.push_back(offset);
326 
327  SolidColor boxColor;
328  boxColor.rgba = Vec4<float>(disgustingGreen[0], disgustingGreen[1],
329  disgustingGreen[2], 1.f);
330  boxColor.useSolidColor = true;
331  _instanceColor.push_back(boxColor);
332 
333  _nTotal++;
334  _objectMap.push_back(_cubeLoadIndex);
335  }
336 }
337 
338 void DrawList::addMesh(double grid_size, const Vec3<double>& left_corner,
339  const DMat<double>& height_map, bool transparent) {
340  (void)transparent;
341 
342  _grid_size = grid_size;
343  _height_map_left_corner = left_corner;
344  _height_map = height_map;
345  _height_map_min = 1.e5;
346  _height_map_max = -1.e5;
347 
348  for (int i(0); i < height_map.rows(); ++i) {
349  for (int j(0); j < height_map.cols(); ++j) {
350  if (height_map(i, j) > _height_map_max) {
351  _height_map_max = height_map(i, j);
352  }
353  if (height_map(i, j) < _height_map_min) {
354  _height_map_min = height_map(i, j);
355  }
356  }
357  }
358 }
static constexpr float disgustingGreen[]
Definition: Colors.h:12
void addMesh(double grid_size, const Vec3< double > &left_corner, const DMat< double > &height_map, bool transparent)
Definition: DrawList.cpp:338
size_t addCheetah3(Vec4< float > color, bool useOld)
Definition: DrawList.cpp:48
std::vector< size_t > _glArrayOffsets
Definition: DrawList.h:292
std::vector< std::vector< float > > _normalData
Definition: DrawList.h:284
double height
Definition: DrawList.h:33
double depth
Definition: DrawList.h:33
std::vector< std::vector< float > > _colorData
Definition: DrawList.h:285
size_t _nUnique
Definition: DrawList.h:282
typename Eigen::Matrix< T, 3, 3 > Mat3
Definition: cppTypes.h:54
typename Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > DMat
Definition: cppTypes.h:106
bool _reloadNeeded
Definition: DrawList.h:301
typename Eigen::Matrix< T, 4, 1 > Quat
Definition: cppTypes.h:58
std::vector< size_t > _glArraySizes
Definition: DrawList.h:293
static void setSolidColor(std::vector< float > &data, size_t size, float r, float g, float b)
Definition: DrawList.h:247
size_t addMiniCheetah(Vec4< float > color, bool useOld)
Definition: DrawList.cpp:129
void computeVertices(std::vector< float > &vertices, std::vector< float > &normals, std::vector< float > &colors)
typename Eigen::Matrix< T, 3, 1 > Vec3
Definition: cppTypes.h:26
const float * getSize()
Definition: Checkerboard.h:34
size_t _miniCheetahLoadIndex
Definition: DrawList.h:317
static constexpr float defaultRobotColor[]
Definition: Colors.h:15
size_t addDebugSphere(float radius)
Definition: DrawList.cpp:247
void load_obj_file(std::string fileName, std::vector< float > &positions, std::vector< float > &normals)
Definition: obj_loader.cpp:19
double _height_map_max
Definition: DrawList.h:313
void addBox(double depth, double width, double height, const Vec3< double > &pos, const Mat3< double > &ori, bool transparent)
Definition: DrawList.cpp:288
vectorAligned< SolidColor > _instanceColor
Definition: DrawList.h:278
static constexpr float debugRedColor[]
Definition: Colors.h:16
Data structure to store robot model to be drawn.
void loadFiles()
Definition: DrawList.cpp:10
std::vector< BoxInfo > _box_list
Definition: DrawList.h:308
std::vector< QMatrix4x4 > _kinematicXform
Definition: DrawList.h:279
Vec3< double > _height_map_left_corner
Definition: DrawList.h:311
std::vector< float > _glColorData
Definition: DrawList.h:297
size_t _cubeLoadIndex
Definition: DrawList.h:318
Quat< typename T::Scalar > rotationMatrixToQuaternion(const Eigen::MatrixBase< T > &r1)
typename Eigen::Matrix< T, 4, 1 > Vec4
Definition: cppTypes.h:30
DMat< double > _height_map
Definition: DrawList.h:312
void buildDrawList()
Definition: DrawList.cpp:266
float frame[16]
Definition: DrawList.h:34
double _height_map_min
Definition: DrawList.h:313
std::vector< float > _glNormalData
Definition: DrawList.h:296
std::vector< size_t > _objectMap
Definition: DrawList.h:290
std::string _baseFileName
Definition: DrawList.h:288
size_t _cheetah3LoadIndex
Definition: DrawList.h:317
double width
Definition: DrawList.h:33
double _grid_size
Definition: DrawList.h:310
EIGEN_MAKE_ALIGNED_OPERATOR_NEW Vec4< float > rgba
Definition: DrawList.h:45
std::vector< float > _glVertexData
Definition: DrawList.h:295
size_t _nTotal
Definition: DrawList.h:282
bool useSolidColor
Definition: DrawList.h:46
size_t addCheckerboard(Checkerboard &checkerBoard)
Definition: DrawList.cpp:215
size_t _sphereLoadIndex
Definition: DrawList.h:318
std::vector< QMatrix4x4 > _modelOffsets
Definition: DrawList.h:299
MX f(const MX &x, const MX &u)
std::vector< std::vector< float > > _vertexData
Definition: DrawList.h:283