File indexing completed on 2025-01-18 10:04:20
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015 #ifndef NCollection_Vec2_HeaderFile
0016 #define NCollection_Vec2_HeaderFile
0017
0018 #include <cmath> // std::sqrt()
0019
0020 #include <Standard_Dump.hxx>
0021
0022
0023
0024 #define NCOLLECTION_VEC_COMPONENTS_2D(theX, theY) \
0025 const NCollection_Vec2<Element_t> theX##theY() const { return NCollection_Vec2<Element_t>(theX(), theY()); } \
0026 const NCollection_Vec2<Element_t> theY##theX() const { return NCollection_Vec2<Element_t>(theY(), theX()); }
0027
0028
0029
0030 template<typename Element_t>
0031 class NCollection_Vec2
0032 {
0033
0034 public:
0035
0036
0037 static int Length()
0038 {
0039 return 2;
0040 }
0041
0042
0043 NCollection_Vec2()
0044 {
0045 v[0] = v[1] = Element_t(0);
0046 }
0047
0048
0049 explicit NCollection_Vec2 (const Element_t theXY)
0050 {
0051 v[0] = v[1] = theXY;
0052 }
0053
0054
0055 explicit NCollection_Vec2 (const Element_t theX,
0056 const Element_t theY)
0057 {
0058 v[0] = theX;
0059 v[1] = theY;
0060 }
0061
0062
0063
0064
0065
0066
0067 template <typename OtherElement_t>
0068 explicit NCollection_Vec2 (const NCollection_Vec2<OtherElement_t>& theOtherVec2)
0069 {
0070 v[0] = static_cast<Element_t> (theOtherVec2[0]);
0071 v[1] = static_cast<Element_t> (theOtherVec2[1]);
0072 }
0073
0074
0075 void SetValues (const Element_t theX,
0076 const Element_t theY)
0077 {
0078 v[0] = theX;
0079 v[1] = theY;
0080 }
0081
0082
0083 Element_t x() const { return v[0]; }
0084
0085
0086 Element_t y() const { return v[1]; }
0087
0088
0089 NCOLLECTION_VEC_COMPONENTS_2D(x, y)
0090
0091
0092 Element_t& x() { return v[0]; }
0093
0094
0095 Element_t& y() { return v[1]; }
0096
0097
0098 bool IsEqual (const NCollection_Vec2& theOther) const
0099 {
0100 return v[0] == theOther.v[0]
0101 && v[1] == theOther.v[1];
0102 }
0103
0104
0105 bool operator== (const NCollection_Vec2& theOther) const { return IsEqual (theOther); }
0106
0107
0108 bool operator!= (const NCollection_Vec2& theOther) const { return !IsEqual (theOther); }
0109
0110
0111 const Element_t* GetData() const { return v; }
0112 Element_t* ChangeData() { return v; }
0113 operator const Element_t*() const { return v; }
0114 operator Element_t*() { return v; }
0115
0116
0117 NCollection_Vec2& operator+= (const NCollection_Vec2& theAdd)
0118 {
0119 v[0] += theAdd.v[0];
0120 v[1] += theAdd.v[1];
0121 return *this;
0122 }
0123
0124
0125 friend NCollection_Vec2 operator+ (const NCollection_Vec2& theLeft,
0126 const NCollection_Vec2& theRight)
0127 {
0128 return NCollection_Vec2 (theLeft.v[0] + theRight.v[0],
0129 theLeft.v[1] + theRight.v[1]);
0130 }
0131
0132
0133 NCollection_Vec2& operator-= (const NCollection_Vec2& theDec)
0134 {
0135 v[0] -= theDec.v[0];
0136 v[1] -= theDec.v[1];
0137 return *this;
0138 }
0139
0140
0141 friend NCollection_Vec2 operator- (const NCollection_Vec2& theLeft,
0142 const NCollection_Vec2& theRight)
0143 {
0144 return NCollection_Vec2 (theLeft.v[0] - theRight.v[0],
0145 theLeft.v[1] - theRight.v[1]);
0146 }
0147
0148
0149 NCollection_Vec2 operator-() const
0150 {
0151 return NCollection_Vec2 (-x(), -y());
0152 }
0153
0154
0155 NCollection_Vec2& operator*= (const NCollection_Vec2& theRight)
0156 {
0157 v[0] *= theRight.v[0];
0158 v[1] *= theRight.v[1];
0159 return *this;
0160 }
0161
0162
0163 friend NCollection_Vec2 operator* (const NCollection_Vec2& theLeft,
0164 const NCollection_Vec2& theRight)
0165 {
0166 return NCollection_Vec2 (theLeft.v[0] * theRight.v[0],
0167 theLeft.v[1] * theRight.v[1]);
0168 }
0169
0170
0171 void Multiply (const Element_t theFactor)
0172 {
0173 v[0] *= theFactor;
0174 v[1] *= theFactor;
0175 }
0176
0177
0178 NCollection_Vec2 Multiplied (const Element_t theFactor) const
0179 {
0180 return NCollection_Vec2 (v[0] * theFactor,
0181 v[1] * theFactor);
0182 }
0183
0184
0185 NCollection_Vec2 cwiseMin (const NCollection_Vec2& theVec) const
0186 {
0187 return NCollection_Vec2 (v[0] < theVec.v[0] ? v[0] : theVec.v[0],
0188 v[1] < theVec.v[1] ? v[1] : theVec.v[1]);
0189 }
0190
0191
0192 NCollection_Vec2 cwiseMax (const NCollection_Vec2& theVec) const
0193 {
0194 return NCollection_Vec2 (v[0] > theVec.v[0] ? v[0] : theVec.v[0],
0195 v[1] > theVec.v[1] ? v[1] : theVec.v[1]);
0196 }
0197
0198
0199 NCollection_Vec2 cwiseAbs() const
0200 {
0201 return NCollection_Vec2 (std::abs (v[0]),
0202 std::abs (v[1]));
0203 }
0204
0205
0206 Element_t maxComp() const
0207 {
0208 return v[0] > v[1] ? v[0] : v[1];
0209 }
0210
0211
0212 Element_t minComp() const
0213 {
0214 return v[0] < v[1] ? v[0] : v[1];
0215 }
0216
0217
0218 NCollection_Vec2& operator*= (const Element_t theFactor)
0219 {
0220 Multiply (theFactor);
0221 return *this;
0222 }
0223
0224
0225 NCollection_Vec2& operator/= (const Element_t theInvFactor)
0226 {
0227 v[0] /= theInvFactor;
0228 v[1] /= theInvFactor;
0229 return *this;
0230 }
0231
0232
0233 NCollection_Vec2& operator/= (const NCollection_Vec2& theRight)
0234 {
0235 v[0] /= theRight.v[0];
0236 v[1] /= theRight.v[1];
0237 return *this;
0238 }
0239
0240
0241 NCollection_Vec2 operator* (const Element_t theFactor) const
0242 {
0243 return Multiplied (theFactor);
0244 }
0245
0246
0247 NCollection_Vec2 operator/ (const Element_t theInvFactor) const
0248 {
0249 return NCollection_Vec2(v[0] / theInvFactor,
0250 v[1] / theInvFactor);
0251 }
0252
0253
0254 friend NCollection_Vec2 operator/ (const NCollection_Vec2& theLeft,
0255 const NCollection_Vec2& theRight)
0256 {
0257 return NCollection_Vec2 (theLeft.v[0] / theRight.v[0],
0258 theLeft.v[1] / theRight.v[1]);
0259 }
0260
0261
0262 Element_t Dot (const NCollection_Vec2& theOther) const
0263 {
0264 return x() * theOther.x() + y() * theOther.y();
0265 }
0266
0267
0268 Element_t Modulus() const
0269 {
0270 return std::sqrt (x() * x() + y() * y());
0271 }
0272
0273
0274
0275 Element_t SquareModulus() const
0276 {
0277 return x() * x() + y() * y();
0278 }
0279
0280
0281 static NCollection_Vec2 DX()
0282 {
0283 return NCollection_Vec2 (Element_t(1), Element_t(0));
0284 }
0285
0286
0287 static NCollection_Vec2 DY()
0288 {
0289 return NCollection_Vec2 (Element_t(0), Element_t(1));
0290 }
0291
0292
0293 void DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth = -1) const
0294 {
0295 (void)theDepth;
0296 OCCT_DUMP_FIELD_VALUES_NUMERICAL (theOStream, "Vec2", 2, v[0], v[1])
0297 }
0298
0299 private:
0300
0301 Element_t v[2];
0302
0303 };
0304
0305 #endif