node_spline.cc
Go to the documentation of this file.
00001 /******************************************************************************
00002 Copyright (c) 2018, Alexander W. Winkler. All rights reserved.
00003 
00004 Redistribution and use in source and binary forms, with or without
00005 modification, are permitted provided that the following conditions are met:
00006 
00007 * Redistributions of source code must retain the above copyright notice, this
00008   list of conditions and the following disclaimer.
00009 
00010 * Redistributions in binary form must reproduce the above copyright notice,
00011   this list of conditions and the following disclaimer in the documentation
00012   and/or other materials provided with the distribution.
00013 
00014 * Neither the name of the copyright holder nor the names of its
00015   contributors may be used to endorse or promote products derived from
00016   this software without specific prior written permission.
00017 
00018 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00019 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00020 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00021 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00022 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00023 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00024 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00025 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00026 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00027 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00028 ******************************************************************************/
00029 
00030 #include <towr/variables/node_spline.h>
00031 
00032 #include <towr/variables/nodes.h>
00033 
00034 namespace towr {
00035 
00036 NodeSpline::NodeSpline(NodeSubjectPtr const node_variables,
00037                        const VecTimes& polynomial_durations)
00038     :   Spline(polynomial_durations, node_variables->GetDim()),
00039         NodesObserver(node_variables)
00040 {
00041   UpdateNodes();
00042   jac_wrt_nodes_structure_ = Jacobian(node_variables->GetDim(), node_variables->GetRows());
00043 }
00044 
00045 
00046 void
00047 NodeSpline::UpdateNodes ()
00048 {
00049   for (int i=0; i<cubic_polys_.size(); ++i) {
00050     auto nodes = node_values_->GetBoundaryNodes(i);
00051     cubic_polys_.at(i).SetNodes(nodes.front(), nodes.back());
00052   }
00053 
00054   UpdatePolynomialCoeff();
00055 }
00056 
00057 int
00058 NodeSpline::GetNodeVariablesCount() const
00059 {
00060   return node_values_->GetRows();
00061 }
00062 
00063 NodeSpline::Jacobian
00064 NodeSpline::GetJacobianWrtNodes (double t_global, Dx dxdt) const
00065 {
00066   int id; double t_local;
00067   std::tie(id, t_local) = GetLocalTime(t_global, GetPolyDurations());
00068 
00069   return GetJacobianWrtNodes(id, t_local, dxdt);
00070 }
00071 
00072 NodeSpline::Jacobian
00073 NodeSpline::GetJacobianWrtNodes (int id, double t_local, Dx dxdt) const
00074 {
00075   Jacobian jac = jac_wrt_nodes_structure_;
00076   FillJacobianWrtNodes(id, t_local, dxdt, jac, false);
00077 
00078   // needed to avoid Eigen::assert failure "wrong storage order" triggered
00079   // in dynamic_constraint.cc
00080   jac.makeCompressed();
00081 
00082   return jac;
00083 }
00084 
00085 void
00086 NodeSpline::FillJacobianWrtNodes (int poly_id, double t_local, Dx dxdt,
00087                                   Jacobian& jac, bool fill_with_zeros) const
00088 {
00089   for (int idx=0; idx<jac.cols(); ++idx) {
00090     for (auto info : node_values_->GetNodeInfoAtOptIndex(idx)) {
00091       for (auto side : {Nodes::Side::Start, Nodes::Side::End}) { // every jacobian is affected by two nodes
00092 
00093         int node = node_values_->GetNodeId(poly_id, side);
00094 
00095         if (node == info.node_id_) {
00096           double val = 0.0;
00097 
00098           if (side == Nodes::Side::Start)
00099             val = cubic_polys_.at(poly_id).GetDerivativeWrtStartNode(dxdt, info.node_deriv_, t_local);
00100           else if (side == Nodes::Side::End)
00101             val = cubic_polys_.at(poly_id).GetDerivativeWrtEndNode(dxdt, info.node_deriv_, t_local);
00102           else
00103             assert(false); // this shouldn't happen
00104 
00105           // if only want structure
00106           if (fill_with_zeros)
00107             val = 0.0;
00108 
00109           jac.coeffRef(info.node_dim_, idx) += val;
00110         }
00111       }
00112     }
00113   }
00114 }
00115 
00116 } /* namespace towr */


towr_core
Author(s): Alexander W. Winkler
autogenerated on Mon Apr 9 2018 03:12:44