Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/opencascade/NCollection_Vec3.hxx was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

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_Vec3_HeaderFile
0016 #define NCollection_Vec3_HeaderFile
0017 
0018 #include <cstring>
0019 #include <cmath>
0020 #include <NCollection_Vec2.hxx>
0021 
0022 //! Auxiliary macros to define couple of similar access components as vector methods
0023 #define NCOLLECTION_VEC_COMPONENTS_3D(theX, theY, theZ) \
0024   const NCollection_Vec3<Element_t> theX##theY##theZ() const { return NCollection_Vec3<Element_t>(theX(), theY(), theZ()); } \
0025   const NCollection_Vec3<Element_t> theX##theZ##theY() const { return NCollection_Vec3<Element_t>(theX(), theZ(), theY()); } \
0026   const NCollection_Vec3<Element_t> theY##theX##theZ() const { return NCollection_Vec3<Element_t>(theY(), theX(), theZ()); } \
0027   const NCollection_Vec3<Element_t> theY##theZ##theX() const { return NCollection_Vec3<Element_t>(theY(), theZ(), theX()); } \
0028   const NCollection_Vec3<Element_t> theZ##theY##theX() const { return NCollection_Vec3<Element_t>(theZ(), theY(), theX()); } \
0029   const NCollection_Vec3<Element_t> theZ##theX##theY() const { return NCollection_Vec3<Element_t>(theZ(), theX(), theY()); }
0030 
0031 //! Generic 3-components vector.
0032 //! To be used as RGB color pixel or XYZ 3D-point.
0033 //! The main target for this class - to handle raw low-level arrays (from/to graphic driver etc.).
0034 template<typename Element_t>
0035 class NCollection_Vec3
0036 {
0037 
0038 public:
0039 
0040   //! Returns the number of components.
0041   static int Length()
0042   {
0043     return 3;
0044   }
0045 
0046   //! Empty constructor. Construct the zero vector.
0047   NCollection_Vec3()
0048   {
0049     std::memset (this, 0, sizeof(NCollection_Vec3));
0050   }
0051 
0052   //! Initialize ALL components of vector within specified value.
0053   explicit NCollection_Vec3 (Element_t theValue)
0054   {
0055     v[0] = v[1] = v[2] = theValue;
0056   }
0057 
0058   //! Per-component constructor.
0059   explicit NCollection_Vec3 (const Element_t theX,
0060                              const Element_t theY,
0061                              const Element_t theZ)
0062   {
0063     v[0] = theX;
0064     v[1] = theY;
0065     v[2] = theZ;
0066   }
0067 
0068   //! Constructor from 2-components vector + optional 3rd value.
0069   explicit NCollection_Vec3 (const NCollection_Vec2<Element_t>& theVec2, Element_t theZ = Element_t(0))
0070   {
0071     v[0] = theVec2[0];
0072     v[1] = theVec2[1];
0073     v[2] = theZ;
0074   }
0075 
0076   //! Conversion constructor (explicitly converts some 3-component vector with other element type
0077   //! to a new 3-component vector with the element type Element_t,
0078   //! whose elements are static_cast'ed corresponding elements of theOtherVec3 vector)
0079   //! @tparam OtherElement_t the element type of the other 3-component vector theOtherVec3
0080   //! @param theOtherVec3 the 3-component vector that needs to be converted
0081   template <typename OtherElement_t>
0082   explicit NCollection_Vec3 (const NCollection_Vec3<OtherElement_t>& theOtherVec3)
0083   {
0084     v[0] = static_cast<Element_t> (theOtherVec3[0]);
0085     v[1] = static_cast<Element_t> (theOtherVec3[1]);
0086     v[2] = static_cast<Element_t> (theOtherVec3[2]);
0087   }
0088 
0089   //! Assign new values to the vector.
0090   void SetValues (const Element_t theX,
0091                   const Element_t theY,
0092                   const Element_t theZ)
0093   {
0094     v[0] = theX;
0095     v[1] = theY;
0096     v[2] = theZ;
0097   }
0098 
0099   //! Assign new values to the vector.
0100   void SetValues (const NCollection_Vec2<Element_t>& theVec2, Element_t theZ)
0101   {
0102     v[0] = theVec2.x();
0103     v[1] = theVec2.y();
0104     v[2] = theZ;
0105   }
0106 
0107   //! Alias to 1st component as X coordinate in XYZ.
0108   Element_t x() const { return v[0]; }
0109 
0110   //! Alias to 1st component as RED channel in RGB.
0111   Element_t r() const { return v[0]; }
0112 
0113   //! Alias to 2nd component as Y coordinate in XYZ.
0114   Element_t y() const { return v[1]; }
0115 
0116   //! Alias to 2nd component as GREEN channel in RGB.
0117   Element_t g() const { return v[1]; }
0118 
0119   //! Alias to 3rd component as Z coordinate in XYZ.
0120   Element_t z() const { return v[2]; }
0121 
0122   //! Alias to 3rd component as BLUE channel in RGB.
0123   Element_t b() const { return v[2]; }
0124 
0125   //! @return 2 components by their names in specified order (in GLSL-style)
0126   NCOLLECTION_VEC_COMPONENTS_2D(x, y)
0127   NCOLLECTION_VEC_COMPONENTS_2D(x, z)
0128   NCOLLECTION_VEC_COMPONENTS_2D(y, z)
0129 
0130   //! @return 3 components by their names in specified order (in GLSL-style)
0131   NCOLLECTION_VEC_COMPONENTS_3D(x, y, z)
0132 
0133   //! Alias to 1st component as X coordinate in XYZ.
0134   Element_t& x() { return v[0]; }
0135 
0136   //! Alias to 1st component as RED channel in RGB.
0137   Element_t& r() { return v[0]; }
0138 
0139   //! Alias to 2nd component as Y coordinate in XYZ.
0140   Element_t& y() { return v[1]; }
0141 
0142   //! Alias to 2nd component as GREEN channel in RGB.
0143   Element_t& g() { return v[1]; }
0144 
0145   //! Alias to 3rd component as Z coordinate in XYZ.
0146   Element_t& z() { return v[2]; }
0147 
0148   //! Alias to 3rd component as BLUE channel in RGB.
0149   Element_t& b() { return v[2]; }
0150 
0151   //! Check this vector with another vector for equality (without tolerance!).
0152   bool IsEqual (const NCollection_Vec3& theOther) const
0153   {
0154     return v[0] == theOther.v[0]
0155         && v[1] == theOther.v[1]
0156         && v[2] == theOther.v[2];
0157   }
0158 
0159   //! Check this vector with another vector for equality (without tolerance!).
0160   bool operator== (const NCollection_Vec3& theOther) const { return IsEqual (theOther); }
0161 
0162   //! Check this vector with another vector for non-equality (without tolerance!).
0163   bool operator!= (const NCollection_Vec3& theOther) const { return !IsEqual (theOther); }
0164 
0165   //! Raw access to the data (for OpenGL exchange).
0166   const Element_t* GetData()    const { return v; }
0167         Element_t* ChangeData()       { return v; }
0168   operator const   Element_t*() const { return v; }
0169   operator         Element_t*()       { return v; }
0170 
0171   //! Compute per-component summary.
0172   NCollection_Vec3& operator+= (const NCollection_Vec3& theAdd)
0173   {
0174     v[0] += theAdd.v[0];
0175     v[1] += theAdd.v[1];
0176     v[2] += theAdd.v[2];
0177     return *this;
0178   }
0179 
0180   //! Compute per-component summary.
0181   friend NCollection_Vec3 operator+ (const NCollection_Vec3& theLeft,
0182                                      const NCollection_Vec3& theRight)
0183   {
0184     NCollection_Vec3 aSumm = NCollection_Vec3 (theLeft);
0185     return aSumm += theRight;
0186   }
0187 
0188   //! Unary -.
0189   NCollection_Vec3 operator-() const
0190   {
0191     return NCollection_Vec3 (-x(), -y(), -z());
0192   }
0193 
0194   //! Compute per-component subtraction.
0195   NCollection_Vec3& operator-= (const NCollection_Vec3& theDec)
0196   {
0197     v[0] -= theDec.v[0];
0198     v[1] -= theDec.v[1];
0199     v[2] -= theDec.v[2];
0200     return *this;
0201   }
0202 
0203   //! Compute per-component subtraction.
0204   friend NCollection_Vec3 operator- (const NCollection_Vec3& theLeft,
0205                                      const NCollection_Vec3& theRight)
0206   {
0207     NCollection_Vec3 aSumm = NCollection_Vec3 (theLeft);
0208     return aSumm -= theRight;
0209   }
0210 
0211   //! Compute per-component multiplication by scale factor.
0212   void Multiply (const Element_t theFactor)
0213   {
0214     v[0] *= theFactor;
0215     v[1] *= theFactor;
0216     v[2] *= theFactor;
0217   }
0218 
0219   //! Compute per-component multiplication.
0220   NCollection_Vec3& operator*= (const NCollection_Vec3& theRight)
0221   {
0222     v[0] *= theRight.v[0];
0223     v[1] *= theRight.v[1];
0224     v[2] *= theRight.v[2];
0225     return *this;
0226   }
0227 
0228   //! Compute per-component multiplication.
0229   friend NCollection_Vec3 operator* (const NCollection_Vec3& theLeft,
0230                                      const NCollection_Vec3& theRight)
0231   {
0232     NCollection_Vec3 aResult = NCollection_Vec3 (theLeft);
0233     return aResult *= theRight;
0234   }
0235 
0236   //! Compute per-component multiplication by scale factor.
0237   NCollection_Vec3& operator*= (const Element_t theFactor)
0238   {
0239     Multiply (theFactor);
0240     return *this;
0241   }
0242 
0243   //! Compute per-component multiplication by scale factor.
0244   NCollection_Vec3 operator* (const Element_t theFactor) const
0245   {
0246     return Multiplied (theFactor);
0247   }
0248 
0249   //! Compute per-component multiplication by scale factor.
0250   NCollection_Vec3 Multiplied (const Element_t theFactor) const
0251   {
0252     NCollection_Vec3 aCopyVec3 (*this);
0253     aCopyVec3 *= theFactor;
0254     return aCopyVec3;
0255   }
0256 
0257   //! Compute component-wise minimum of two vectors.
0258   NCollection_Vec3 cwiseMin (const NCollection_Vec3& theVec) const
0259   {
0260     return NCollection_Vec3 (v[0] < theVec.v[0] ? v[0] : theVec.v[0],
0261                              v[1] < theVec.v[1] ? v[1] : theVec.v[1],
0262                              v[2] < theVec.v[2] ? v[2] : theVec.v[2]);
0263   }
0264 
0265   //! Compute component-wise maximum of two vectors.
0266   NCollection_Vec3 cwiseMax (const NCollection_Vec3& theVec) const
0267   {
0268     return NCollection_Vec3 (v[0] > theVec.v[0] ? v[0] : theVec.v[0],
0269                              v[1] > theVec.v[1] ? v[1] : theVec.v[1],
0270                              v[2] > theVec.v[2] ? v[2] : theVec.v[2]);
0271   }
0272 
0273   //! Compute component-wise modulus of the vector.
0274   NCollection_Vec3 cwiseAbs() const
0275   {
0276     return NCollection_Vec3 (std::abs (v[0]),
0277                              std::abs (v[1]),
0278                              std::abs (v[2]));
0279   }
0280 
0281   //! Compute maximum component of the vector.
0282   Element_t maxComp() const
0283   {
0284     return v[0] > v[1] ? (v[0] > v[2] ? v[0] : v[2])
0285                        : (v[1] > v[2] ? v[1] : v[2]);
0286   }
0287 
0288   //! Compute minimum component of the vector.
0289   Element_t minComp() const
0290   {
0291     return v[0] < v[1] ? (v[0] < v[2] ? v[0] : v[2])
0292                        : (v[1] < v[2] ? v[1] : v[2]);
0293   }
0294 
0295   //! Compute per-component division by scale factor.
0296   NCollection_Vec3& operator/= (const Element_t theInvFactor)
0297   {
0298     v[0] /= theInvFactor;
0299     v[1] /= theInvFactor;
0300     v[2] /= theInvFactor;
0301     return *this;
0302   }
0303 
0304   //! Compute per-component division.
0305   NCollection_Vec3& operator/= (const NCollection_Vec3& theRight)
0306   {
0307     v[0] /= theRight.v[0];
0308     v[1] /= theRight.v[1];
0309     v[2] /= theRight.v[2];
0310     return *this;
0311   }
0312 
0313   //! Compute per-component division by scale factor.
0314   NCollection_Vec3 operator/ (const Element_t theInvFactor) const
0315   {
0316     NCollection_Vec3 aResult (*this);
0317     return aResult /= theInvFactor;
0318   }
0319 
0320   //! Compute per-component division.
0321   friend NCollection_Vec3 operator/ (const NCollection_Vec3& theLeft,
0322                                      const NCollection_Vec3& theRight)
0323   {
0324     NCollection_Vec3 aResult = NCollection_Vec3 (theLeft);
0325     return aResult /= theRight;
0326   }
0327 
0328   //! Computes the dot product.
0329   Element_t Dot (const NCollection_Vec3& theOther) const
0330   {
0331     return x() * theOther.x() + y() * theOther.y() + z() * theOther.z();
0332   }
0333 
0334   //! Computes the vector modulus (magnitude, length).
0335   Element_t Modulus() const
0336   {
0337     return std::sqrt (x() * x() + y() * y() + z() * z());
0338   }
0339 
0340   //! Computes the square of vector modulus (magnitude, length).
0341   //! This method may be used for performance tricks.
0342   Element_t SquareModulus() const
0343   {
0344     return x() * x() + y() * y() + z() * z();
0345   }
0346 
0347   //! Normalize the vector.
0348   void Normalize()
0349   {
0350     Element_t aModulus = Modulus();
0351     if (aModulus != Element_t(0)) // just avoid divide by zero
0352     {
0353       x() = x() / aModulus;
0354       y() = y() / aModulus;
0355       z() = z() / aModulus;
0356     }
0357   }
0358 
0359   //! Normalize the vector.
0360   NCollection_Vec3 Normalized() const
0361   {
0362     NCollection_Vec3 aCopy (*this);
0363     aCopy.Normalize();
0364     return aCopy;
0365   }
0366 
0367   //! Computes the cross product.
0368   static NCollection_Vec3 Cross (const NCollection_Vec3& theVec1,
0369                                  const NCollection_Vec3& theVec2)
0370   {
0371     return NCollection_Vec3(theVec1.y() * theVec2.z() - theVec1.z() * theVec2.y(),
0372             theVec1.z() * theVec2.x() - theVec1.x() * theVec2.z(),
0373             theVec1.x() * theVec2.y() - theVec1.y() * theVec2.x());
0374   }
0375 
0376   //! Compute linear interpolation between to vectors.
0377   //! @param theT - interpolation coefficient 0..1;
0378   //! @return interpolation result.
0379   static NCollection_Vec3 GetLERP (const NCollection_Vec3& theFrom,
0380                                    const NCollection_Vec3& theTo,
0381                                    const Element_t         theT)
0382   {
0383     return theFrom * (Element_t(1) - theT) + theTo * theT;
0384   }
0385 
0386   //! Construct DX unit vector.
0387   static NCollection_Vec3 DX()
0388   {
0389     return NCollection_Vec3 (Element_t(1), Element_t(0), Element_t(0));
0390   }
0391 
0392   //! Construct DY unit vector.
0393   static NCollection_Vec3 DY()
0394   {
0395     return NCollection_Vec3 (Element_t(0), Element_t(1), Element_t(0));
0396   }
0397 
0398   //! Construct DZ unit vector.
0399   static NCollection_Vec3 DZ()
0400   {
0401     return NCollection_Vec3 (Element_t(0), Element_t(0), Element_t(1));
0402   }
0403 
0404   //! Dumps the content of me into the stream
0405   void DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth = -1) const
0406   {
0407     (void)theDepth;
0408     OCCT_DUMP_FIELD_VALUES_NUMERICAL (theOStream, "Vec3", 3, v[0], v[1], v[2])
0409   }
0410 
0411 private:
0412 
0413   Element_t v[3]; //!< define the vector as array to avoid structure alignment issues
0414 
0415 };
0416 
0417 //! Optimized concretization for float type.
0418 template<> inline NCollection_Vec3<float>& NCollection_Vec3<float>::operator/= (const float theInvFactor)
0419 {
0420   Multiply (1.0f / theInvFactor);
0421   return *this;
0422 }
0423 
0424 //! Optimized concretization for double type.
0425 template<> inline NCollection_Vec3<double>& NCollection_Vec3<double>::operator/= (const double theInvFactor)
0426 {
0427   Multiply (1.0 / theInvFactor);
0428   return *this;
0429 }
0430 
0431 #endif // _NCollection_Vec3_H__