1 // =============================================================================
2 // PROJECT CHRONO - http://projectchrono.org
3 //
4 // Copyright (c) 2014 projectchrono.org
5 // All rights reserved.
6 //
7 // Use of this source code is governed by a BSD-style license that can be found
8 // in the LICENSE file at the top level of the distribution and at
9 // http://projectchrono.org/license-chrono.txt.
10 //
11 // =============================================================================
12 // Authors: Alessandro Tasora, Radu Serban
13 // =============================================================================
14 
15 #ifndef CHLINKMASK_H
16 #define CHLINKMASK_H
17 
18 #include <cmath>
19 #include <vector>
20 
21 #include "chrono/core/ChMath.h"
22 #include "chrono/solver/ChConstraintTwoBodies.h"
23 
24 namespace chrono {
25 
26 /// Mask structure for N scalar constraint equations between two bodies.
27 class ChApi ChLinkMask {
28   protected:
29     std::vector<ChConstraintTwoBodies*> constraints;  ///< array of pointers to 'n' scalar constraints
30 
31   public:
32     int nconstr;  ///< number of scalar constraint equations.
33 
34     /// Build a link mask with no constraints.
35     ChLinkMask();
36 
37     /// Build a link mask with a default array of mnconstr constraints of class ChConstraintTwoBodies().
38     ChLinkMask(int mnconstr);
39 
40     /// Copy constructor
41     ChLinkMask(const ChLinkMask& source);
42 
43     /// Destructor
44     virtual ~ChLinkMask();
45 
46     /// Assignment operator.
47     ChLinkMask& operator=(const ChLinkMask& other);
48 
49     /// Set references to variables of two connected bodies to all
50     /// constraints at once, therefore also sets all the constraints as active.
51     void SetTwoBodiesVariables(ChVariables* var1, ChVariables* var2);
52 
53     /// Obtain the reference to the i-th scalar constraint data in the collection link mask.
Constr_N(int i)54     ChConstraintTwoBodies& Constr_N(int i) {
55         assert((i >= 0) && (i < nconstr));
56         return *constraints[i];
57     }
58 
59     /// Utility: to change the size of the mask, reallocating constraints
60     /// (all of type ChConstraintTwo).
61     /// No action if newnconstr == nconstr
62     void ResetNconstr(int newnconstr);
63 
64     /// Add a ChConstraintTwoBodies to mask (NOTE: later, the constraint will
65     /// be automatically deleted when the mask will be deleted)
66     void AddConstraint(ChConstraintTwoBodies* aconstr);
67 
68     /// To compare two masks, return true if equal
69     bool IsEqual(ChLinkMask& mask2);
70 
71     // Get the number of removed degrees of freedom (n.of constraints)
72 
73     /// Count both bilaterals and unilaterals
74     int GetMaskDoc();
75     /// Count only bilaterals
76     int GetMaskDoc_c();
77     /// Count only unilaterals
78     int GetMaskDoc_d();
79 
80     /// Get the i-th active scalar constraint (not active constr. won't be considered)
81     ChConstraintTwoBodies* GetActiveConstrByNum(int mnum);
82 
83     /// Sets some active constraints as redundant.
84     int SetActiveRedundantByArray(int* mvector, int mcount);
85 
86     /// Set lock =ON for constraints which were disabled because redundant
87     int RestoreRedundant();
88 
89     /// If SetAllDisabled(true), all the constraints are temporarily turned
90     /// off (inactive) at once, because marked as 'disabled'. Return n.of changed
91     int SetAllDisabled(bool mdis);
92 
93     /// If SetAllBroken(true), all the constraints are temporarily turned
94     /// off (broken) at once, because marked as 'broken'. Return n.of changed.
95     int SetAllBroken(bool mdis);
96 
97     /// Method to allow serialization of transient data to archives.
98     virtual void ArchiveOUT(ChArchiveOut& marchive);
99 
100     /// Method to allow de-serialization of transient data from archives.
101     virtual void ArchiveIN(ChArchiveIn& marchive);
102 };
103 
104 CH_CLASS_VERSION(ChLinkMask, 0)
105 
106 // -----------------------------------------------------------------------------
107 
108 /// Specialized ChLinkMask class, for constraint equations of the ChLinkLock link.
109 class ChApi ChLinkMaskLF : public ChLinkMask {
110   public:
111     /// Create a ChLinkMaskLF which has 7 scalar constraints of
112     /// class ChConstraintTwoBodies(). This is useful in case it must
113     /// be used for the ChLinkLock link.
114     ChLinkMaskLF();
ChLinkMaskLF(const ChLinkMaskLF & other)115     ChLinkMaskLF(const ChLinkMaskLF& other) : ChLinkMask(other) {}
116 
117     /// Assignment operator.
118     ChLinkMaskLF& operator=(const ChLinkMaskLF& other);
119 
120     /// Set all mask data at once
121     void SetLockMask(bool x, bool y, bool z, bool e0, bool e1, bool e2, bool e3);
122 
123     /// Obtain the reference to specific scalar constraint data
124     /// in the collection of this link mask.
Constr_X()125     ChConstraintTwoBodies& Constr_X() { return *constraints[0]; }
Constr_Y()126     ChConstraintTwoBodies& Constr_Y() { return *constraints[1]; }
Constr_Z()127     ChConstraintTwoBodies& Constr_Z() { return *constraints[2]; }
Constr_E0()128     ChConstraintTwoBodies& Constr_E0() { return *constraints[3]; }
Constr_E1()129     ChConstraintTwoBodies& Constr_E1() { return *constraints[4]; }
Constr_E2()130     ChConstraintTwoBodies& Constr_E2() { return *constraints[5]; }
Constr_E3()131     ChConstraintTwoBodies& Constr_E3() { return *constraints[6]; }
132 
133     /// Method to allow serialization of transient data to archives.
134     virtual void ArchiveOUT(ChArchiveOut& marchive) override;
135 
136     /// Method to allow deserialization of transient data from archives.
137     virtual void ArchiveIN(ChArchiveIn& marchive) override;
138 };
139 
140 CH_CLASS_VERSION(ChLinkMaskLF, 0)
141 
142 }  // end namespace chrono
143 
144 #endif
145