File indexing completed on 2026-09-09 09:16:11
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 \
0026 { \
0027 return NCollection_Vec2<Element_t>(theX(), theY()); \
0028 } \
0029 const NCollection_Vec2<Element_t> theY##theX() const \
0030 { \
0031 return NCollection_Vec2<Element_t>(theY(), theX()); \
0032 }
0033
0034
0035
0036 template <typename Element_t>
0037 class NCollection_Vec2
0038 {
0039
0040 public:
0041
0042 static int Length() { return 2; }
0043
0044
0045 NCollection_Vec2() { v[0] = v[1] = Element_t(0); }
0046
0047
0048 explicit NCollection_Vec2(const Element_t theXY) { v[0] = v[1] = theXY; }
0049
0050
0051 explicit NCollection_Vec2(const Element_t theX, const Element_t theY)
0052 {
0053 v[0] = theX;
0054 v[1] = theY;
0055 }
0056
0057
0058
0059
0060
0061
0062 template <typename OtherElement_t>
0063 explicit NCollection_Vec2(const NCollection_Vec2<OtherElement_t>& theOtherVec2)
0064 {
0065 v[0] = static_cast<Element_t>(theOtherVec2[0]);
0066 v[1] = static_cast<Element_t>(theOtherVec2[1]);
0067 }
0068
0069
0070 void SetValues(const Element_t theX, const Element_t theY)
0071 {
0072 v[0] = theX;
0073 v[1] = theY;
0074 }
0075
0076
0077 Element_t x() const { return v[0]; }
0078
0079
0080 Element_t y() const { return v[1]; }
0081
0082
0083 NCOLLECTION_VEC_COMPONENTS_2D(x, y)
0084
0085
0086 Element_t& x() { return v[0]; }
0087
0088
0089 Element_t& y() { return v[1]; }
0090
0091
0092 bool IsEqual(const NCollection_Vec2& theOther) const
0093 {
0094 return v[0] == theOther.v[0] && v[1] == theOther.v[1];
0095 }
0096
0097
0098 bool operator==(const NCollection_Vec2& theOther) const { return IsEqual(theOther); }
0099
0100
0101 bool operator!=(const NCollection_Vec2& theOther) const { return !IsEqual(theOther); }
0102
0103
0104 const Element_t* GetData() const { return v; }
0105
0106 Element_t* ChangeData() { return v; }
0107
0108 operator const Element_t*() const { return v; }
0109
0110 operator Element_t*() { return v; }
0111
0112
0113 NCollection_Vec2& operator+=(const NCollection_Vec2& theAdd)
0114 {
0115 v[0] += theAdd.v[0];
0116 v[1] += theAdd.v[1];
0117 return *this;
0118 }
0119
0120
0121 friend NCollection_Vec2 operator+(const NCollection_Vec2& theLeft,
0122 const NCollection_Vec2& theRight)
0123 {
0124 return NCollection_Vec2(theLeft.v[0] + theRight.v[0], theLeft.v[1] + theRight.v[1]);
0125 }
0126
0127
0128 NCollection_Vec2& operator-=(const NCollection_Vec2& theDec)
0129 {
0130 v[0] -= theDec.v[0];
0131 v[1] -= theDec.v[1];
0132 return *this;
0133 }
0134
0135
0136 friend NCollection_Vec2 operator-(const NCollection_Vec2& theLeft,
0137 const NCollection_Vec2& theRight)
0138 {
0139 return NCollection_Vec2(theLeft.v[0] - theRight.v[0], theLeft.v[1] - theRight.v[1]);
0140 }
0141
0142
0143 NCollection_Vec2 operator-() const { return NCollection_Vec2(-x(), -y()); }
0144
0145
0146 NCollection_Vec2& operator*=(const NCollection_Vec2& theRight)
0147 {
0148 v[0] *= theRight.v[0];
0149 v[1] *= theRight.v[1];
0150 return *this;
0151 }
0152
0153
0154 friend NCollection_Vec2 operator*(const NCollection_Vec2& theLeft,
0155 const NCollection_Vec2& theRight)
0156 {
0157 return NCollection_Vec2(theLeft.v[0] * theRight.v[0], theLeft.v[1] * theRight.v[1]);
0158 }
0159
0160
0161 void Multiply(const Element_t theFactor)
0162 {
0163 v[0] *= theFactor;
0164 v[1] *= theFactor;
0165 }
0166
0167
0168 NCollection_Vec2 Multiplied(const Element_t theFactor) const
0169 {
0170 return NCollection_Vec2(v[0] * theFactor, v[1] * theFactor);
0171 }
0172
0173
0174 NCollection_Vec2 cwiseMin(const NCollection_Vec2& theVec) const
0175 {
0176 return NCollection_Vec2(v[0] < theVec.v[0] ? v[0] : theVec.v[0],
0177 v[1] < theVec.v[1] ? v[1] : theVec.v[1]);
0178 }
0179
0180
0181 NCollection_Vec2 cwiseMax(const NCollection_Vec2& theVec) const
0182 {
0183 return NCollection_Vec2(v[0] > theVec.v[0] ? v[0] : theVec.v[0],
0184 v[1] > theVec.v[1] ? v[1] : theVec.v[1]);
0185 }
0186
0187
0188 NCollection_Vec2 cwiseAbs() const { return NCollection_Vec2(std::abs(v[0]), std::abs(v[1])); }
0189
0190
0191 Element_t maxComp() const { return v[0] > v[1] ? v[0] : v[1]; }
0192
0193
0194 Element_t minComp() const { return v[0] < v[1] ? v[0] : v[1]; }
0195
0196
0197 NCollection_Vec2& operator*=(const Element_t theFactor)
0198 {
0199 Multiply(theFactor);
0200 return *this;
0201 }
0202
0203
0204 NCollection_Vec2& operator/=(const Element_t theInvFactor)
0205 {
0206 v[0] /= theInvFactor;
0207 v[1] /= theInvFactor;
0208 return *this;
0209 }
0210
0211
0212 NCollection_Vec2& operator/=(const NCollection_Vec2& theRight)
0213 {
0214 v[0] /= theRight.v[0];
0215 v[1] /= theRight.v[1];
0216 return *this;
0217 }
0218
0219
0220 NCollection_Vec2 operator*(const Element_t theFactor) const { return Multiplied(theFactor); }
0221
0222
0223 NCollection_Vec2 operator/(const Element_t theInvFactor) const
0224 {
0225 return NCollection_Vec2(v[0] / theInvFactor, v[1] / theInvFactor);
0226 }
0227
0228
0229 friend NCollection_Vec2 operator/(const NCollection_Vec2& theLeft,
0230 const NCollection_Vec2& theRight)
0231 {
0232 return NCollection_Vec2(theLeft.v[0] / theRight.v[0], theLeft.v[1] / theRight.v[1]);
0233 }
0234
0235
0236 Element_t Dot(const NCollection_Vec2& theOther) const
0237 {
0238 return x() * theOther.x() + y() * theOther.y();
0239 }
0240
0241
0242 Element_t Modulus() const { return std::sqrt(x() * x() + y() * y()); }
0243
0244
0245
0246 Element_t SquareModulus() const { return x() * x() + y() * y(); }
0247
0248
0249 static NCollection_Vec2 DX() { return NCollection_Vec2(Element_t(1), Element_t(0)); }
0250
0251
0252 static NCollection_Vec2 DY() { return NCollection_Vec2(Element_t(0), Element_t(1)); }
0253
0254
0255 void DumpJson(Standard_OStream& theOStream, Standard_Integer theDepth = -1) const
0256 {
0257 (void)theDepth;
0258 OCCT_DUMP_FIELD_VALUES_NUMERICAL(theOStream, "Vec2", 2, v[0], v[1])
0259 }
0260
0261 private:
0262 Element_t v[2];
0263 };
0264
0265 #endif