opensurgsim
CommonTests.h
Go to the documentation of this file.
1 // Copyright 2013, SimQuest Solutions Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
17 
18 #ifndef SURGSIM_PHYSICS_UNITTESTS_COMMONTESTS_H
19 #define SURGSIM_PHYSICS_UNITTESTS_COMMONTESTS_H
20 
21 #include <gtest/gtest.h>
22 
23 #include <string>
24 #include <memory>
25 
26 #include "SurgSim/Math/Shapes.h"
27 #include "SurgSim/Physics/Constraint.h"
28 #include "SurgSim/Physics/ConstraintImplementation.h"
29 #include "SurgSim/Physics/ContactConstraintData.h"
30 #include "SurgSim/Physics/FixedConstraintFrictionlessContact.h"
31 #include "SurgSim/Physics/FixedRepresentation.h"
32 #include "SurgSim/Physics/MlcpPhysicsProblem.h"
33 #include "SurgSim/Physics/MlcpPhysicsSolution.h"
34 #include "SurgSim/Physics/PhysicsManagerState.h"
35 #include "SurgSim/Physics/RigidConstraintFrictionlessContact.h"
36 #include "SurgSim/Physics/RigidRepresentation.h"
37 
41 
42 namespace
43 {
44  const double epsilon = 1e-10;
45 };
46 
47 namespace SurgSim
48 {
49 namespace Physics
50 {
51 
52 class CommonTests : public ::testing::Test
53 {
54 public:
56  void SetUp()
57  {
58  // Set the time step
59  dt = 1e-3;
60 
61  // Create a fixed world to define constraint against it
62  m_fixedWorldRepresentation = std::make_shared<FixedRepresentation>("FixedPlane");
63  m_fixedWorldRepresentation->setIsGravityEnabled(false);
64  {
65  // Simply do 1 time step to make sure things are initialized (compliance matrix...)
66  m_fixedWorldRepresentation->beforeUpdate(dt);
68  m_fixedWorldRepresentation->afterUpdate(dt);
69  }
70 
71  // Create the physics manager state
72  m_physicsManagerState = std::make_shared<PhysicsManagerState>();
73 
74  // Create a Rigid Sphere
75  std::shared_ptr<RigidRepresentation> rigidSphereRepresentation;
76  rigidSphereRepresentation = std::make_shared<RigidRepresentation>("RigidSphere");
77  {
78  double radius = 1e-2;
79  std::shared_ptr<Shape> shape = std::make_shared<SphereShape>(radius);
80  rigidSphereRepresentation->setShape(shape);
81  rigidSphereRepresentation->setDensity(1000);
82  rigidSphereRepresentation->setIsGravityEnabled(false);
83  {
84  // Simply do 1 time step to make sure things are initialized (compliance matrix...)
85  rigidSphereRepresentation->beforeUpdate(dt);
86  rigidSphereRepresentation->update(dt);
87  rigidSphereRepresentation->afterUpdate(dt);
88  }
89  m_allRepresentations.push_back(rigidSphereRepresentation);
90  }
91 
92  // Create a Rigid Box
93  std::shared_ptr<RigidRepresentation> rigidBoxRepresentation = std::make_shared<RigidRepresentation>("RigidBox");
94  {
95  double size[3]={0.01, 0.02, 0.03};
96  rigidBoxRepresentation->setDensity(1000);
97  std::shared_ptr<Shape> shape = std::make_shared<BoxShape>(size[0], size[1], size[2]);
98  rigidBoxRepresentation->setShape(shape);
99  rigidBoxRepresentation->setIsGravityEnabled(false);
100  {
101  // Simply do 1 time step to make sure things are initialized (compliance matrix...)
102  rigidBoxRepresentation->beforeUpdate(dt);
103  rigidBoxRepresentation->update(dt);
104  rigidBoxRepresentation->afterUpdate(dt);
105  }
106  m_allRepresentations.push_back(rigidBoxRepresentation);
107  }
108  }
109 
110  void resetMlcpProblem(int nbDof, int nbConstraint)
111  {
113  {
114  m_physicsManagerState->getMlcpProblem().A.setZero(nbConstraint, nbConstraint);
115  m_physicsManagerState->getMlcpProblem().b.setZero(nbConstraint);
116  m_physicsManagerState->getMlcpProblem().CHt.setZero(nbDof, nbConstraint);
117  m_physicsManagerState->getMlcpProblem().H.resize(nbConstraint, nbDof);
118  m_physicsManagerState->getMlcpProblem().mu.setZero(nbConstraint);
119  m_physicsManagerState->getMlcpProblem().constraintTypes.clear();
120 
121  m_physicsManagerState->getMlcpSolution().x.setZero(nbConstraint);
122  m_physicsManagerState->getMlcpSolution().dofCorrection.setZero(nbDof);
123  }
124  }
125 
126 protected:
128  double dt = std::numeric_limits<double>::signaling_NaN();
129 
131  std::shared_ptr<Representation> m_fixedWorldRepresentation;
132 
134  std::vector<std::shared_ptr<Representation>> m_allRepresentations;
135 
137  std::vector<std::shared_ptr<Representation>> m_usedRepresentations;
138 
140  std::vector<std::shared_ptr<Constraint>> m_usedConstraints;
141 
143  std::shared_ptr<PhysicsManagerState> m_physicsManagerState;
144 };
145 
146 }; // namespace Physics
147 }; // namespace SurgSim
148 
149 #endif // SURGSIM_PHYSICS_UNITTESTS_COMMONTESTS_H
Wraps glewInit() to separate the glew opengl definitions from the osg opengl definitions only imgui n...
Definition: AddRandomSphereBehavior.cpp:36
std::vector< std::shared_ptr< Representation > > m_allRepresentations
Vector of all representations.
Definition: CommonTests.h:134
double dt
Time step.
Definition: CommonTests.h:128
Box shape: box centered on (0 0 0), aligned with the axis with different sizes along X...
Definition: BoxShape.h:33
Definition: CommonTests.h:52
std::shared_ptr< Representation > m_fixedWorldRepresentation
Fixed representation to define constraint in fixed space.
Definition: CommonTests.h:131
std::shared_ptr< PhysicsManagerState > m_physicsManagerState
The unique physics manager state.
Definition: CommonTests.h:143
Sphere shape: sphere centered on (0 0 0), defined with radius.
Definition: SphereShape.h:30
void SetUp()
Setup the test case by creating all object.
Definition: CommonTests.h:56
std::vector< std::shared_ptr< Representation > > m_usedRepresentations
Vector of representations useful for the current test.
Definition: CommonTests.h:137
std::vector< std::shared_ptr< Constraint > > m_usedConstraints
Vector of constraints useful for the current test.
Definition: CommonTests.h:140
Generic rigid shape class defining a shape.
Definition: Shape.h:65