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