Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

robot/AggregateControlInterface.cpp

Go to the documentation of this file.
00001 /****************************************************************************
00002   Copyright (C)2003 David Jung <opensim@pobox.com>
00003 
00004   This program/file is free software; you can redistribute it and/or modify
00005   it under the terms of the GNU General Public License as published by
00006   the Free Software Foundation; either version 2 of the License, or
00007   (at your option) any later version.
00008   
00009   This program is distributed in the hope that it will be useful,
00010   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012   GNU General Public License for more details. (http://www.gnu.org)
00013   
00014   You should have received a copy of the GNU General Public License
00015   along with this program; if not, write to the Free Software
00016   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017   
00018   $Id: AggregateControlInterface.cpp 1039 2004-02-11 20:50:52Z jungd $
00019   $Revision: 1.1 $
00020   $Date: 2004-02-11 15:50:52 -0500 (Wed, 11 Feb 2004) $
00021   $Author: jungd $
00022  
00023 ****************************************************************************/
00024 
00025 #include <robot/AggregateControlInterface>
00026 
00027 using robot::AggregateControlInterface;
00028 
00029 
00030 
00031 AggregateControlInterface::AggregateControlInterface(const String& name, const String& type,
00032                                                      array<ref<ControlInterface> > interfaces)
00033  : ControlInterface(name,type), interfaces(interfaces)
00034 {
00035   if (interfaces.size() < 1)
00036     throw std::invalid_argument(Exception("must supply at least one ControlInterface"));
00037   
00038   inputOffsets.resize(interfaces.size()+1);
00039   outputOffsets.resize(interfaces.size()+1);
00040 }
00041                             
00042                             
00043 AggregateControlInterface::AggregateControlInterface(const String& name, const String& type,
00044                                                      ref<ControlInterface> interface1, 
00045                                                      ref<ControlInterface> interface2)
00046   : ControlInterface(name,type)
00047 {
00048   Assert(interface1);
00049   Assert(interface2);
00050   
00051   interfaces.resize(2);
00052   interfaces[0] = interface1;
00053   interfaces[1] = interface2;
00054   
00055   inputOffsets.resize(3);
00056   outputOffsets.resize(3);
00057 }
00058 
00059 
00060 AggregateControlInterface::AggregateControlInterface(const AggregateControlInterface& aci)
00061   : ControlInterface(aci), interfaces(aci.interfaces)
00062 {
00063   inputOffsets.resize(interfaces.size()+1);
00064   outputOffsets.resize(interfaces.size()+1);
00065 }
00066 
00067 
00068 
00069 inline void AggregateControlInterface::inputIndex(Int aindex, Int& interfaceIndex, Int& index) const
00070 {
00071   interfaceIndex=1;
00072   while (inputOffsets[interfaceIndex] < aindex) interfaceIndex++;
00073   interfaceIndex--;
00074   index = aindex - inputOffsets[interfaceIndex];
00075 }
00076 
00077 
00078 inline void AggregateControlInterface::outputIndex(Int aindex, Int& interfaceIndex, Int& index) const
00079 {
00080   interfaceIndex=1;
00081   while (outputOffsets[interfaceIndex] < aindex) interfaceIndex++;
00082   interfaceIndex--;
00083   index = aindex - outputOffsets[interfaceIndex];
00084 }
00085 
00086 
00087 
00088 String AggregateControlInterface::inputName(Int i) const
00089 {
00090   recomputeInputSize();
00091   Assert( i < numInputs);
00092   Int interfaceIndex, index;
00093   inputIndex(i, interfaceIndex, index);
00094   return interfaces[interfaceIndex]->inputName(index);
00095 }
00096 
00097 
00098 Real AggregateControlInterface::getInput(Int i) const
00099 {
00100   recomputeInputSize();
00101   Assert( i < numInputs);
00102   Int interfaceIndex, index;
00103   inputIndex(i, interfaceIndex, index);
00104   return interfaces[interfaceIndex]->getInput(index);
00105 }
00106 
00107 
00108 const base::Vector& AggregateControlInterface::getInputs() const 
00109 {
00110   recomputeInputSize();
00111   lastInputs.resize(numInputs);
00112   
00113   Int interfaceIndex=0;
00114   Int interfaceOffset=0;
00115   ref<ControlInterface> interface(interfaces[0]);
00116   for(Int i=0; i<numInputs; i++) {
00117     if (i == inputOffsets[interfaceIndex+1]) {
00118       interfaceIndex++;
00119       interface = interfaces[interfaceIndex];
00120       interfaceOffset = inputOffsets[interfaceIndex];
00121     }
00122     lastInputs[i] = interface->getInput( i - interfaceOffset);
00123   }
00124   return lastInputs;
00125 }
00126 
00127 
00128 String AggregateControlInterface::outputName(Int i) const
00129 {
00130   recomputeOutputSize();
00131   Assert( i < numOutputs);
00132   Int interfaceIndex, index;
00133   outputIndex(i, interfaceIndex, index);
00134   return interfaces[interfaceIndex]->outputName(index);
00135 }
00136 
00137 
00138 void AggregateControlInterface::setOutput(Int i, Real value)
00139 {
00140   recomputeOutputSize();
00141   Assert( i < numOutputs);
00142   Int interfaceIndex, index;
00143   outputIndex(i, interfaceIndex, index);
00144   interfaces[interfaceIndex]->setOutput(index, value );
00145 }
00146 
00147 
00148 void AggregateControlInterface::setOutputs(const Vector& values) 
00149 {
00150   recomputeOutputSize();
00151   Assert(values.size() == numOutputs);
00152   
00153   Int interfaceIndex=0;
00154   Int interfaceOffset=0;
00155   ref<ControlInterface> interface(interfaces[0]);
00156   for(Int i=0; i<numOutputs; i++) {
00157     if (i == outputOffsets[interfaceIndex+1]) {
00158       interfaceIndex++;
00159       interface = interfaces[interfaceIndex];
00160       interfaceOffset = outputOffsets[interfaceIndex];
00161     }
00162     interface->setOutput( i - interfaceOffset, values[i]);
00163   }
00164 }
00165 
00166 
00167 void AggregateControlInterface::recomputeInputSize() const
00168 {
00169   numInputs=0;
00170   for(Int i=0; i<interfaces.size(); i++) {
00171     inputOffsets[i] = numInputs;
00172     numInputs += interfaces[i]->inputSize();
00173   }
00174   inputOffsets[interfaces.size()] = numInputs;
00175 }
00176 
00177 
00178 void AggregateControlInterface::recomputeOutputSize() const
00179 {
00180   numOutputs=0;
00181   for(Int i=0; i<interfaces.size(); i++) {
00182     outputOffsets[i] = numOutputs;
00183     numOutputs += interfaces[i]->outputSize();
00184   }
00185   outputOffsets[interfaces.size()] = numOutputs;
00186 }
00187 
00188 

Generated on Thu Jul 29 15:56:31 2004 for OpenSim by doxygen 1.3.6