NTRT Simulator  v1.1
 All Classes Namespaces Files Functions Variables Typedefs Friends Pages
tgConnectorInfo.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 
27 #include "tgConnectorInfo.h"
28 
29 #include "tgPair.h"
30 #include "tgPairs.h"
31 #include "tgRigidInfo.h"
32 
33 #include "core/tgTagSearch.h"
34 
35 #include "LinearMath/btVector3.h"
36 #include "BulletDynamics/Dynamics/btRigidBody.h"
37 
38 
39 tgConnectorInfo* tgConnectorInfo::createConnectorInfo(const tgPair& pair, const tgTagSearch& tagSearch)
40 {
41  // Our subclasses may not be able to create connectorInfos based on nodes. Also, the
42  // tags may not match our search. Make sure both work, or return 0.
43  tgConnectorInfo* connectorInfo = 0;
44  if(tagSearch.matches(pair.getTags())) {
45  connectorInfo = createConnectorInfo(pair);
46  }
47  return connectorInfo;
48 }
49 
50 std::vector<tgConnectorInfo*> tgConnectorInfo::createConnectorInfos(const tgPairs& pairs, const tgTagSearch& tagSearch)
51 {
52  std::vector<tgConnectorInfo*> result;
53  for(int i = 0; i < pairs.size(); i++) {
54  tgConnectorInfo* r = createConnectorInfo(pairs[i], tagSearch);
55  if(r != 0) {
56  result.push_back(r);
57  }
58  }
59  return result;
60 }
61 
62 void tgConnectorInfo::chooseRigids(std::set<tgRigidInfo*> rigids)
63 {
64 
65  // @todo: find and set pointers to appropriate rigids from the set provided.
66  // @todo: should we throw an exception if no appropriate rigid is found?
67  if(getFromRigidInfo() == 0) { // if it hasn't already been set
68  tgRigidInfo* fromRigidInfo = chooseRigid(rigids, getFrom());
69  //std::cout << " chosen fromRigidInfo is " << fromRigidInfo << std::endl;
70  setFromRigidInfo(fromRigidInfo);
71  //std::cout << " getFromRigidInfo is " << getFromRigidInfo() << std::endl;
72  }
73 
74  if(getToRigidInfo() == 0) { // if it hasn't already been set
75  tgRigidInfo* toRigidInfo = chooseRigid(rigids, getTo());
76  //std::cout << " chosen toRigidInfo is " << toRigidInfo << std::endl;
77  setToRigidInfo(toRigidInfo);
78  //std::cout << " getToRigidInfo is " << getToRigidInfo() << std::endl;
79  }
80 }
81 
82 tgRigidInfo* tgConnectorInfo::chooseRigid(std::set<tgRigidInfo*> rigids, const btVector3& v) {
83 
84  std::set<tgRigidInfo*> candidateRigids = findRigidsContaining(rigids, v);
85 
86  tgRigidInfo* chosenRigid;
87  if (candidateRigids.size() == 1) {
88  // Choose the first element since there's only one
89  chosenRigid = *(candidateRigids.begin());
90  } else {
91  // find the best candidate (if more than one rigid, use the rigid whose center of mass is closest to v. This seems like a reasonable approach...)
92  chosenRigid = findClosestCenterOfMass(candidateRigids, v);
93  }
94 
95  return chosenRigid;
96 };
97 
98 btRigidBody* tgConnectorInfo::getToRigidBody() {
99  return getToRigidInfo()->getRigidInfoGroup()->getRigidBody();
100  //return m_toRigidBody;
101 };
102 
103 btRigidBody* tgConnectorInfo::getFromRigidBody() {
104  //return m_fromRigidBody;
105  return getFromRigidInfo()->getRigidInfoGroup()->getRigidBody();
106 };
107 
108 
109 // Protected:
110 
111 
112 tgRigidInfo* tgConnectorInfo::findClosestCenterOfMass(std::set<tgRigidInfo*> rigids, const btVector3& v) {
113  if (rigids.size() == 0) {
114  return NULL;
115  }
116  std::set<tgRigidInfo*>::iterator it;
117  it = rigids.begin();
118  tgRigidInfo* closest = *it; // First member
119  it++;
120  for(; it != rigids.end(); ++it) {
121  btVector3 com = (*it)->getCenterOfMass();
122  if(com.distance(v) < closest->getCenterOfMass().distance(v)) {
123  closest = *it;
124  }
125  }
126  return closest;
127 }
128 
129 
130 std::set<tgRigidInfo*> tgConnectorInfo::findRigidsContaining(std::set<tgRigidInfo*> rigids, const btVector3& toFind) {
131  std::set<tgRigidInfo*> found;
132  std::set<tgRigidInfo*>::iterator it;
133  for(it=rigids.begin(); it != rigids.end(); ++it) {
134  if ((*it)->containsNode(toFind)) {
135  found.insert(*it);
136  }
137  }
138  return found;
139 };
140 
141 // @todo: Remove this? Is it used by anything? It's protected...
142 bool tgConnectorInfo::rigidFoundIn(std::set<tgRigidInfo*> rigids, tgRigidInfo* rigid) {
143  //return (std::find(rigids.begin(), rigids.end(), rigid) != rigids.end()); // Doesn't work on some compilers (RDA 2014-Jan-28)
144  std::set<tgRigidInfo*>::iterator it;
145  for(it = rigids.begin(); it != rigids.end(); ++it) {
146  if(*it == rigid)
147  return true;
148  }
149  return false;
150 };
Contains the definition of class tgTagSearch.
Definition of abstract class tgRigidInfo.
virtual tgRigidInfo * getRigidInfoGroup()
Definition: tgRigidInfo.h:149
Definition of class tgPair.
Definition of class tgPairs.
Definition of class tgConnectorInfo.
virtual btRigidBody * getRigidBody()
const bool matches(const tgTags &tags) const
Definition: tgTagSearch.h:62
virtual btVector3 getCenterOfMass() const =0
Definition: tgPair.h:48