Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:04:20

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 { 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 //! Defines the 2D-vector template.
0029 //! The main target for this class - to handle raw low-level arrays (from/to graphic driver etc.).
0030 template<typename Element_t>
0031 class NCollection_Vec2
0032 {
0033 
0034 public:
0035 
0036   //! Returns the number of components.
0037   static int Length()
0038   {
0039     return 2;
0040   }
0041 
0042   //! Empty constructor. Construct the zero vector.
0043   NCollection_Vec2()
0044   {
0045     v[0] = v[1] = Element_t(0);
0046   }
0047 
0048   //! Initialize ALL components of vector within specified value.
0049   explicit NCollection_Vec2 (const Element_t theXY)
0050   {
0051     v[0] = v[1] = theXY;
0052   }
0053 
0054   //! Per-component constructor.
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   //! Conversion constructor (explicitly converts some 2-component vector with other element type
0063   //! to a new 2-component vector with the element type Element_t,
0064   //! whose elements are static_cast'ed corresponding elements of theOtherVec2 vector)
0065   //! @tparam OtherElement_t the element type of the other 2-component vector theOtherVec2
0066   //! @param theOtherVec2 the 2-component vector that needs to be converted
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   //! Assign new values to the vector.
0075   void SetValues (const Element_t theX,
0076                   const Element_t theY)
0077   {
0078     v[0] = theX;
0079     v[1] = theY;
0080   }
0081 
0082   //! Alias to 1st component as X coordinate in XY.
0083   Element_t x() const { return v[0]; }
0084 
0085   //! Alias to 2nd component as Y coordinate in XY.
0086   Element_t y() const { return v[1]; }
0087 
0088   //! @return 2 components by their names in specified order (in GLSL-style)
0089   NCOLLECTION_VEC_COMPONENTS_2D(x, y)
0090 
0091   //! Alias to 1st component as X coordinate in XY.
0092   Element_t& x() { return v[0]; }
0093 
0094   //! Alias to 2nd component as Y coordinate in XY.
0095   Element_t& y() { return v[1]; }
0096 
0097   //! Check this vector with another vector for equality (without tolerance!).
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   //! Check this vector with another vector for equality (without tolerance!).
0105   bool operator== (const NCollection_Vec2& theOther) const { return IsEqual (theOther); }
0106 
0107   //! Check this vector with another vector for non-equality (without tolerance!).
0108   bool operator!= (const NCollection_Vec2& theOther) const { return !IsEqual (theOther); }
0109 
0110   //! Raw access to the data (for OpenGL exchange).
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   //! Compute per-component summary.
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   //! Compute per-component summary.
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   //! Compute per-component subtraction.
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   //! Compute per-component subtraction.
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   //! Unary -.
0149   NCollection_Vec2 operator-() const
0150   {
0151     return NCollection_Vec2 (-x(), -y());
0152   }
0153 
0154   //! Compute per-component multiplication.
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   //! Compute per-component multiplication.
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   //! Compute per-component multiplication by scale factor.
0171   void Multiply (const Element_t theFactor)
0172   {
0173     v[0] *= theFactor;
0174     v[1] *= theFactor;
0175   }
0176 
0177   //! Compute per-component multiplication by scale factor.
0178   NCollection_Vec2 Multiplied (const Element_t theFactor) const
0179   {
0180     return NCollection_Vec2 (v[0] * theFactor,
0181                              v[1] * theFactor);
0182   }
0183 
0184   //! Compute component-wise minimum of two vectors.
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   //! Compute component-wise maximum of two vectors.
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   //! Compute component-wise modulus of the vector.
0199   NCollection_Vec2 cwiseAbs() const
0200   {
0201     return NCollection_Vec2 (std::abs (v[0]),
0202                              std::abs (v[1]));
0203   }
0204 
0205   //! Compute maximum component of the vector.
0206   Element_t maxComp() const
0207   {
0208     return v[0] > v[1] ? v[0] : v[1];
0209   }
0210 
0211   //! Compute minimum component of the vector.
0212   Element_t minComp() const
0213   {
0214     return v[0] < v[1] ? v[0] : v[1];
0215   }
0216 
0217   //! Compute per-component multiplication by scale factor.
0218   NCollection_Vec2& operator*= (const Element_t theFactor)
0219   {
0220     Multiply (theFactor);
0221     return *this;
0222   }
0223 
0224   //! Compute per-component division by scale factor.
0225   NCollection_Vec2& operator/= (const Element_t theInvFactor)
0226   {
0227     v[0] /= theInvFactor;
0228     v[1] /= theInvFactor;
0229     return *this;
0230   }
0231 
0232   //! Compute per-component division.
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   //! Compute per-component multiplication by scale factor.
0241   NCollection_Vec2 operator* (const Element_t theFactor) const
0242   {
0243     return Multiplied (theFactor);
0244   }
0245 
0246   //! Compute per-component division by scale factor.
0247   NCollection_Vec2 operator/ (const Element_t theInvFactor) const
0248   {
0249     return NCollection_Vec2(v[0] / theInvFactor,
0250             v[1] / theInvFactor);
0251   }
0252 
0253   //! Compute per-component division.
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   //! Computes the dot product.
0262   Element_t Dot (const NCollection_Vec2& theOther) const
0263   {
0264     return x() * theOther.x() + y() * theOther.y();
0265   }
0266 
0267   //! Computes the vector modulus (magnitude, length).
0268   Element_t Modulus() const
0269   {
0270     return std::sqrt (x() * x() + y() * y());
0271   }
0272 
0273   //! Computes the square of vector modulus (magnitude, length).
0274   //! This method may be used for performance tricks.
0275   Element_t SquareModulus() const
0276   {
0277     return x() * x() + y() * y();
0278   }
0279 
0280   //! Construct DX unit vector.
0281   static NCollection_Vec2 DX()
0282   {
0283     return NCollection_Vec2 (Element_t(1), Element_t(0));
0284   }
0285 
0286   //! Construct DY unit vector.
0287   static NCollection_Vec2 DY()
0288   {
0289     return NCollection_Vec2 (Element_t(0), Element_t(1));
0290   }
0291 
0292   //! Dumps the content of me into the stream
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 // _NCollection_Vec2_H__