1 /*
2  * Copyright (c) 2011-2021, The DART development contributors
3  * All rights reserved.
4  *
5  * The list of contributors can be found at:
6  *   https://github.com/dartsim/dart/blob/master/LICENSE
7  *
8  * This file is provided under the following "BSD-style" License:
9  *   Redistribution and use in source and binary forms, with or
10  *   without modification, are permitted provided that the following
11  *   conditions are met:
12  *   * Redistributions of source code must retain the above copyright
13  *     notice, this list of conditions and the following disclaimer.
14  *   * Redistributions in binary form must reproduce the above
15  *     copyright notice, this list of conditions and the following
16  *     disclaimer in the documentation and/or other materials provided
17  *     with the distribution.
18  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19  *   CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20  *   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21  *   MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  *   DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23  *   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26  *   USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  *   AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  *   LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  *   ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  *   POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include "dart/dynamics/PlanarJoint.hpp"
34 
35 namespace dart {
36 namespace dynamics {
37 namespace detail {
38 
39 //==============================================================================
PlanarJointUniqueProperties(PlaneType _planeType)40 PlanarJointUniqueProperties::PlanarJointUniqueProperties(PlaneType _planeType)
41 {
42   switch (_planeType)
43   {
44     case PlaneType::ARBITRARY:
45     case PlaneType::XY:
46       setXYPlane();
47       mPlaneType
48           = _planeType; // In case the PlaneType was supposed to be arbitrary
49       break;
50     case PlaneType::YZ:
51       setYZPlane();
52       break;
53     case PlaneType::ZX:
54       setZXPlane();
55       break;
56   }
57 }
58 
59 //==============================================================================
PlanarJointUniqueProperties(const Eigen::Vector3d & _transAxis1,const Eigen::Vector3d & _transAxis2)60 PlanarJointUniqueProperties::PlanarJointUniqueProperties(
61     const Eigen::Vector3d& _transAxis1, const Eigen::Vector3d& _transAxis2)
62 {
63   setArbitraryPlane(_transAxis1, _transAxis2);
64 }
65 
66 //==============================================================================
PlanarJointUniqueProperties(const PlanarJointUniqueProperties & other)67 PlanarJointUniqueProperties::PlanarJointUniqueProperties(
68     const PlanarJointUniqueProperties& other)
69 {
70   switch (other.mPlaneType)
71   {
72     case PlaneType::ARBITRARY:
73       setArbitraryPlane(other.mTransAxis1, other.mTransAxis2);
74       break;
75     case PlaneType::XY:
76       setXYPlane();
77       break;
78     case PlaneType::YZ:
79       setYZPlane();
80       break;
81     case PlaneType::ZX:
82       setZXPlane();
83       break;
84   }
85 }
86 
87 //==============================================================================
operator =(const PlanarJointUniqueProperties & other)88 PlanarJointUniqueProperties& PlanarJointUniqueProperties::operator=(
89     const PlanarJointUniqueProperties& other)
90 {
91   if (this != &other)
92   {
93     switch (other.mPlaneType)
94     {
95       case PlaneType::ARBITRARY:
96         setArbitraryPlane(other.mTransAxis1, other.mTransAxis2);
97         break;
98       case PlaneType::XY:
99         setXYPlane();
100         break;
101       case PlaneType::YZ:
102         setYZPlane();
103         break;
104       case PlaneType::ZX:
105         setZXPlane();
106         break;
107     }
108   }
109 
110   return *this;
111 }
112 
113 //==============================================================================
setXYPlane()114 void PlanarJointUniqueProperties::setXYPlane()
115 {
116   mPlaneType = PlaneType::XY;
117   mRotAxis = Eigen::Vector3d::UnitZ();
118   mTransAxis1 = Eigen::Vector3d::UnitX();
119   mTransAxis2 = Eigen::Vector3d::UnitY();
120 }
121 
122 //==============================================================================
setYZPlane()123 void PlanarJointUniqueProperties::setYZPlane()
124 {
125   mPlaneType = PlaneType::YZ;
126   mRotAxis = Eigen::Vector3d::UnitX();
127   mTransAxis1 = Eigen::Vector3d::UnitY();
128   mTransAxis2 = Eigen::Vector3d::UnitZ();
129 }
130 
131 //==============================================================================
setZXPlane()132 void PlanarJointUniqueProperties::setZXPlane()
133 {
134   mPlaneType = PlaneType::ZX;
135   mRotAxis = Eigen::Vector3d::UnitY();
136   mTransAxis1 = Eigen::Vector3d::UnitZ();
137   mTransAxis2 = Eigen::Vector3d::UnitX();
138 }
139 
140 //==============================================================================
setArbitraryPlane(const Eigen::Vector3d & _transAxis1,const Eigen::Vector3d & _transAxis2)141 void PlanarJointUniqueProperties::setArbitraryPlane(
142     const Eigen::Vector3d& _transAxis1, const Eigen::Vector3d& _transAxis2)
143 {
144   // Set plane type as arbitrary plane
145   mPlaneType = PlaneType::ARBITRARY;
146 
147   // First translational axis
148   mTransAxis1 = _transAxis1.normalized();
149 
150   // Second translational axis
151   mTransAxis2 = _transAxis2.normalized();
152 
153   // Orthogonalize translational axes
154   double dotProduct = mTransAxis1.dot(mTransAxis2);
155   assert(std::abs(dotProduct) < 1.0 - 1e-6);
156   if (std::abs(dotProduct) > 1e-6)
157     mTransAxis2 = (mTransAxis2 - dotProduct * mTransAxis1).normalized();
158 
159   // Rotational axis
160   mRotAxis = (mTransAxis1.cross(mTransAxis2)).normalized();
161 }
162 
163 //==============================================================================
PlanarJointProperties(const GenericJoint<math::R3Space>::Properties & genericJointProperties,const PlanarJointUniqueProperties & planarProperties)164 PlanarJointProperties::PlanarJointProperties(
165     const GenericJoint<math::R3Space>::Properties& genericJointProperties,
166     const PlanarJointUniqueProperties& planarProperties)
167   : GenericJoint<math::R3Space>::Properties(genericJointProperties),
168     PlanarJointUniqueProperties(planarProperties)
169 {
170   // Do nothing
171 }
172 
173 } // namespace detail
174 } // namespace dynamics
175 } // namespace dart
176