Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-09 09:16:11

0001 // Created by: Kirill GAVRILOV
0002 // Copyright (c) 2013-2014 OPEN CASCADE SAS
0003 //
0004 // This file is part of Open CASCADE Technology software library.
0005 //
0006 // This library is free software; you can redistribute it and/or modify it under
0007 // the terms of the GNU Lesser General Public License version 2.1 as published
0008 // by the Free Software Foundation, with special exception defined in the file
0009 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
0010 // distribution for complete text of the license and disclaimer of any warranty.
0011 //
0012 // Alternatively, this file may be used under the terms of Open CASCADE
0013 // commercial license or contractual agreement.
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 //! Auxiliary macros to define couple of similar access components as vector methods.
0023 //! @return 2 components by their names in specified order
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 //! Defines the 2D-vector template.
0035 //! The main target for this class - to handle raw low-level arrays (from/to graphic driver etc.).
0036 template <typename Element_t>
0037 class NCollection_Vec2
0038 {
0039 
0040 public:
0041   //! Returns the number of components.
0042   static int Length() { return 2; }
0043 
0044   //! Empty constructor. Construct the zero vector.
0045   NCollection_Vec2() { v[0] = v[1] = Element_t(0); }
0046 
0047   //! Initialize ALL components of vector within specified value.
0048   explicit NCollection_Vec2(const Element_t theXY) { v[0] = v[1] = theXY; }
0049 
0050   //! Per-component constructor.
0051   explicit NCollection_Vec2(const Element_t theX, const Element_t theY)
0052   {
0053     v[0] = theX;
0054     v[1] = theY;
0055   }
0056 
0057   //! Conversion constructor (explicitly converts some 2-component vector with other element type
0058   //! to a new 2-component vector with the element type Element_t,
0059   //! whose elements are static_cast'ed corresponding elements of theOtherVec2 vector)
0060   //! @tparam OtherElement_t the element type of the other 2-component vector theOtherVec2
0061   //! @param theOtherVec2 the 2-component vector that needs to be converted
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   //! Assign new values to the vector.
0070   void SetValues(const Element_t theX, const Element_t theY)
0071   {
0072     v[0] = theX;
0073     v[1] = theY;
0074   }
0075 
0076   //! Alias to 1st component as X coordinate in XY.
0077   Element_t x() const { return v[0]; }
0078 
0079   //! Alias to 2nd component as Y coordinate in XY.
0080   Element_t y() const { return v[1]; }
0081 
0082   //! @return 2 components by their names in specified order (in GLSL-style)
0083   NCOLLECTION_VEC_COMPONENTS_2D(x, y)
0084 
0085   //! Alias to 1st component as X coordinate in XY.
0086   Element_t& x() { return v[0]; }
0087 
0088   //! Alias to 2nd component as Y coordinate in XY.
0089   Element_t& y() { return v[1]; }
0090 
0091   //! Check this vector with another vector for equality (without tolerance!).
0092   bool IsEqual(const NCollection_Vec2& theOther) const
0093   {
0094     return v[0] == theOther.v[0] && v[1] == theOther.v[1];
0095   }
0096 
0097   //! Check this vector with another vector for equality (without tolerance!).
0098   bool operator==(const NCollection_Vec2& theOther) const { return IsEqual(theOther); }
0099 
0100   //! Check this vector with another vector for non-equality (without tolerance!).
0101   bool operator!=(const NCollection_Vec2& theOther) const { return !IsEqual(theOther); }
0102 
0103   //! Raw access to the data (for OpenGL exchange).
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   //! Compute per-component summary.
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   //! Compute per-component summary.
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   //! Compute per-component subtraction.
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   //! Compute per-component subtraction.
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   //! Unary -.
0143   NCollection_Vec2 operator-() const { return NCollection_Vec2(-x(), -y()); }
0144 
0145   //! Compute per-component multiplication.
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   //! Compute per-component multiplication.
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   //! Compute per-component multiplication by scale factor.
0161   void Multiply(const Element_t theFactor)
0162   {
0163     v[0] *= theFactor;
0164     v[1] *= theFactor;
0165   }
0166 
0167   //! Compute per-component multiplication by scale factor.
0168   NCollection_Vec2 Multiplied(const Element_t theFactor) const
0169   {
0170     return NCollection_Vec2(v[0] * theFactor, v[1] * theFactor);
0171   }
0172 
0173   //! Compute component-wise minimum of two vectors.
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   //! Compute component-wise maximum of two vectors.
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   //! Compute component-wise modulus of the vector.
0188   NCollection_Vec2 cwiseAbs() const { return NCollection_Vec2(std::abs(v[0]), std::abs(v[1])); }
0189 
0190   //! Compute maximum component of the vector.
0191   Element_t maxComp() const { return v[0] > v[1] ? v[0] : v[1]; }
0192 
0193   //! Compute minimum component of the vector.
0194   Element_t minComp() const { return v[0] < v[1] ? v[0] : v[1]; }
0195 
0196   //! Compute per-component multiplication by scale factor.
0197   NCollection_Vec2& operator*=(const Element_t theFactor)
0198   {
0199     Multiply(theFactor);
0200     return *this;
0201   }
0202 
0203   //! Compute per-component division by scale factor.
0204   NCollection_Vec2& operator/=(const Element_t theInvFactor)
0205   {
0206     v[0] /= theInvFactor;
0207     v[1] /= theInvFactor;
0208     return *this;
0209   }
0210 
0211   //! Compute per-component division.
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   //! Compute per-component multiplication by scale factor.
0220   NCollection_Vec2 operator*(const Element_t theFactor) const { return Multiplied(theFactor); }
0221 
0222   //! Compute per-component division by scale factor.
0223   NCollection_Vec2 operator/(const Element_t theInvFactor) const
0224   {
0225     return NCollection_Vec2(v[0] / theInvFactor, v[1] / theInvFactor);
0226   }
0227 
0228   //! Compute per-component division.
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   //! Computes the dot product.
0236   Element_t Dot(const NCollection_Vec2& theOther) const
0237   {
0238     return x() * theOther.x() + y() * theOther.y();
0239   }
0240 
0241   //! Computes the vector modulus (magnitude, length).
0242   Element_t Modulus() const { return std::sqrt(x() * x() + y() * y()); }
0243 
0244   //! Computes the square of vector modulus (magnitude, length).
0245   //! This method may be used for performance tricks.
0246   Element_t SquareModulus() const { return x() * x() + y() * y(); }
0247 
0248   //! Construct DX unit vector.
0249   static NCollection_Vec2 DX() { return NCollection_Vec2(Element_t(1), Element_t(0)); }
0250 
0251   //! Construct DY unit vector.
0252   static NCollection_Vec2 DY() { return NCollection_Vec2(Element_t(0), Element_t(1)); }
0253 
0254   //! Dumps the content of me into the stream
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 // _NCollection_Vec2_H__