NTRT Simulator  Version: Master
 All Classes Namespaces Files Functions Variables Typedefs Friends Pages
tgBulletRenderer.cpp
Go to the documentation of this file.
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 
26 // This module
27 #include "tgBulletRenderer.h"
28 // This application
29 #include "abstractMarker.h"
30 #include "tgSpringCable.h"
32 #include "tgSpringCableAnchor.h"
33 #include "tgBulletUtil.h"
34 #include "tgSpringCableActuator.h"
36 #include "tgWorld.h"
38 
39 #include "tgCast.h"
40 
41 #include "LinearMath/btQuickprof.h"
42 
43 // OpenGL_FreeGlut (patched Bullet)
44 #include "tgGLDebugDrawer.h"
45 // The Bullet Physics library
46 #include "BulletSoftBody/btSoftRigidDynamicsWorld.h"
47 // The C++ Standard Library
48 #include <cassert>
49 
50 
51 tgBulletRenderer::tgBulletRenderer(const tgWorld& world) : m_world(world)
52 {
53 }
54 
55 void tgBulletRenderer::render(const tgRod& rod) const
56 {
57 #ifndef BT_NO_PROFILE
58  BT_PROFILE("tgBulletRenderer::renderRod");
59 #endif //BT_NO_PROFILE
60  // render the rod (change color, etc. if we want)
61 }
62 
64 {
65 #ifndef BT_NO_PROFILE
66  BT_PROFILE("tgBulletRenderer::renderString");
67 #endif //BT_NO_PROFILE
68  // Fetch the btDynamicsWorld
69  btDynamicsWorld& dynamicsWorld =
71 
72  btIDebugDraw* const pDrawer = dynamicsWorld.getDebugDrawer();
73 
74  const tgSpringCable* const pSpringCable = mSCA.getSpringCable();
75 
76  if(pDrawer && pSpringCable)
77  {
78  const std::vector<const tgSpringCableAnchor*>& anchors = pSpringCable->getAnchors();
79  std::size_t n = anchors.size() - 1;
80  for (std::size_t i = 0; i < n; i++)
81  {
82  const btVector3 lineFrom =
83  anchors[i]->getWorldPosition();
84  const btVector3 lineTo =
85  anchors[i+1]->getWorldPosition();
86  // Should this be normalized??
87  const double stretch =
88  mSCA.getCurrentLength() - mSCA.getRestLength();
89  const btVector3 color =
90  (stretch < 0.0) ?
91  btVector3(0.0, 0.0, 1.0) :
92  btVector3(0.5 + stretch / 3.0,
93  0.5 - stretch / 2.0,
94  0.0);
95  pDrawer->drawLine(lineFrom, lineTo, color);
96  }
97  }
98 }
99 
105 {
106 #ifndef BT_NO_PROFILE
107  BT_PROFILE("tgBulletRenderer::renderCompressionSpring");
108 #endif //BT_NO_PROFILE
109  // Fetch the btDynamicsWorld
110  btDynamicsWorld& dynamicsWorld = tgBulletUtil::worldToDynamicsWorld(m_world);
111 
112  btIDebugDraw* const pDrawer = dynamicsWorld.getDebugDrawer();
113 
114  const tgBulletCompressionSpring* const pCompressionSpring =
115  mCSA.getCompressionSpring();
116 
117  if(pDrawer && pCompressionSpring)
118  {
119  const std::vector<const tgSpringCableAnchor*>& anchors =
120  pCompressionSpring->getAnchors();
121  // This for loop is designed for the multiple-anchor SpringCable,
122  // but works fine here too where there are only two anchors.
123  std::size_t n = anchors.size() - 1;
124  for (std::size_t i = 0; i < n; i++)
125  {
126 
127  // This method assumes the spring is "attached" to the first
128  // anchor, although it doesn't really matter mathematically.
129  const btVector3 springStartLoc =
130  anchors[i]->getWorldPosition();
131 
132  // Have the spring state its end point location, instead of
133  // assuming it's at anchor 2.
134  const btVector3 springEndLoc =
135  pCompressionSpring->getSpringEndpoint();
136 
137  // The 'color' variable will be set according to if the
138  // free end of the spring is attached or not.
139  btVector3 color;
140 
141  // Different behavior depending on if the spring is attached
142  // at its free end.
143  // If it *is* attached, render like a SpringCable:
144 
145  if( pCompressionSpring->isFreeEndAttached() )
146  {
147  // The color of the spring is according to its current force.
148  // According to the Bullet Physics API for btIDebugDraw.drawLine,
149  // "For color arguments the X,Y,Z components refer to
150  // Red, Green and Blue each in the range [0..1]"
151  // Let's do red for tension and green for compression
152  // (note that if the free end is attached, there is always an
153  // applied force, no "blue" for no force.")
154  // Negative forces pull the anchors together, positive pushes
155  // them apart.
156  color =
157  ( pCompressionSpring->getSpringForce() < 0.0 ) ?
158  btVector3(1.0, 0.0, 0.0) :
159  btVector3(0.0, 1.0, 0.0);
160  }
161  else
162  {
163  // Like above, the color will depend on force.
164  // Here, though, there will never be any tension force,
165  // since the spring will provide zero force when anchor distance
166  // is greater than rest length.
167  // Less than or equal to zero: blue, no force
168  color =
169  ( pCompressionSpring->getSpringForce() <= 0.0 ) ?
170  btVector3(0.0, 0.0, 1.0) :
171  btVector3(0.0, 1.0, 0.0);
172  }
173  // Draw the string, now that color has been set.
174  pDrawer->drawLine(springStartLoc, springEndLoc, color);
175  }
176  }
177 }
178 
179 void tgBulletRenderer::render(const tgModel& model) const
180 {
181 #ifndef BT_NO_PROFILE
182  BT_PROFILE("tgBulletRenderer::renderModel");
183 #endif //BT_NO_PROFILE
184 
188  // Fetch the btDynamicsWorld
189  btDynamicsWorld& dynamicsWorld = tgBulletUtil::worldToDynamicsWorld(m_world);
190  btIDebugDraw* const idraw = dynamicsWorld.getDebugDrawer();
191  for(int j=0;j<model.getMarkers().size() ;j++)
192  {
193  abstractMarker mark = model.getMarkers()[j];
194  idraw->drawSphere(mark.getWorldPosition(),0.6,mark.getColor());
195  }
196 }
197 
Contains the definition of class tgCompressionSpringActuator. This class assumes a linear spring...
Definitions of class tgSpringCable.
Contains the definition of class tgWorldBulletPhysicsImpl.
virtual const double getSpringForce() const
static btDynamicsWorld & worldToDynamicsWorld(const tgWorld &world)
virtual const bool isFreeEndAttached() const
virtual void render(const tgSpringCableActuator &mSCA) const
Utility class for class casting and filtering collections by type.
Markers for specific places on a tensegrity.
Contains the definition of abstract base class tgSpringCableActuator. Assumes that the string is line...
tgBulletRenderer(const tgWorld &world)
virtual const btVector3 getSpringEndpoint() const
Contains the definition of class tgWorld $Id$.
Contains the definition of class tgBulletUtil.
Definitions of class tgBulletCompressionSpring.
virtual const std::vector< const tgSpringCableAnchor * > getAnchors() const =0
virtual const double getRestLength() const
Definitions of class tgSpringCableAnchor.
virtual const std::vector< const tgSpringCableAnchor * > getAnchors() const
virtual const double getCurrentLength() const
virtual const tgBulletCompressionSpring * getCompressionSpring() const
Contains the definition of concrete class tgBulletRenderer.
btVector3 getWorldPosition() const
Definition: tgRod.h:43
const tgSpringCable * getSpringCable() const