0029151: GCC 7.1 warnings "this statement may fall through" [-Wimplicit-fallthrough=]
[occt.git] / src / NCollection / NCollection_Vec2.hxx
index 36814cb..bb74d07 100644 (file)
@@ -1,29 +1,27 @@
 // Created by: Kirill GAVRILOV
-// Copyright (c) 2012 OPEN CASCADE SAS
+// Copyright (c) 2013-2014 OPEN CASCADE SAS
 //
-// The content of this file is subject to the Open CASCADE Technology Public
-// License Version 6.5 (the "License"). You may not use the content of this file
-// except in compliance with the License. Please obtain a copy of the License
-// at http://www.opencascade.org and read it completely before using this file.
+// This file is part of Open CASCADE Technology software library.
 //
-// The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
-// main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
+// This library is free software; you can redistribute it and/or modify it under
+// the terms of the GNU Lesser General Public License version 2.1 as published
+// by the Free Software Foundation, with special exception defined in the file
+// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
+// distribution for complete text of the license and disclaimer of any warranty.
 //
-// The Original Code and all software distributed under the License is
-// distributed on an "AS IS" basis, without warranty of any kind, and the
-// Initial Developer hereby disclaims all such warranties, including without
-// limitation, any warranties of merchantability, fitness for a particular
-// purpose or non-infringement. Please see the License for the specific terms
-// and conditions governing the rights and limitations under the License.
+// Alternatively, this file may be used under the terms of Open CASCADE
+// commercial license or contractual agreement.
 
 #ifndef _NCollection_Vec2_H__
 #define _NCollection_Vec2_H__
 
+#include <cmath> // std::sqrt()
+
 //! Auxiliary macros to define couple of similar access components as vector methods.
 //! @return 2 components by their names in specified order
 #define NCOLLECTION_VEC_COMPONENTS_2D(theX, theY) \
-  const NCollection_Vec2<Element_t> theX##theY##() const { return NCollection_Vec2<Element_t>(theX##(), theY##()); } \
-  const NCollection_Vec2<Element_t> theY##theX##() const { return NCollection_Vec2<Element_t>(theY##(), theX##()); }
+  const NCollection_Vec2<Element_t> theX##theY() const { return NCollection_Vec2<Element_t>(theX(), theY()); } \
+  const NCollection_Vec2<Element_t> theY##theX() const { return NCollection_Vec2<Element_t>(theY(), theX()); }
 
 //! Defines the 2D-vector template.
 //! The main target for this class - to handle raw low-level arrays (from/to graphic driver etc.).
@@ -59,19 +57,12 @@ public:
     v[1] = theY;
   }
 
-  //! Copy constructor.
-  NCollection_Vec2 (const NCollection_Vec2& theVec2)
-  {
-    v[0] = theVec2[0];
-    v[1] = theVec2[1];
-  }
-
-  //! Assignment operator.
-  const NCollection_Vec2& operator= (const NCollection_Vec2& theVec2)
+  //! Assign new values to the vector.
+  void SetValues (const Element_t theX,
+                  const Element_t theY)
   {
-    v[0] = theVec2[0];
-    v[1] = theVec2[1];
-    return *this;
+    v[0] = theX;
+    v[1] = theY;
   }
 
   //! Alias to 1st component as X coordinate in XY.
@@ -81,7 +72,7 @@ public:
   Element_t y() const { return v[1]; }
 
   //! @return 2 components by their names in specified order (in GLSL-style)
-  NCOLLECTION_VEC_COMPONENTS_2D(x, y);
+  NCOLLECTION_VEC_COMPONENTS_2D(x, y)
 
   //! Alias to 1st component as X coordinate in XY.
   Element_t& x() { return v[0]; }
@@ -89,10 +80,26 @@ public:
   //! Alias to 2nd component as Y coordinate in XY.
   Element_t& y() { return v[1]; }
 
-  //! Raw access to the data (to simplify OpenGL exchange).
-  const Element_t* GetData() const { return v; }
-  operator const Element_t*() const { return v; }
-  operator Element_t*() { return v; }
+  //! Check this vector with another vector for equality (without tolerance!).
+  bool IsEqual (const NCollection_Vec2& theOther) const
+  {
+    return v[0] == theOther.v[0]
+        && v[1] == theOther.v[1];
+  }
+
+  //! Check this vector with another vector for equality (without tolerance!).
+  bool operator== (const NCollection_Vec2& theOther)       { return IsEqual (theOther); }
+  bool operator== (const NCollection_Vec2& theOther) const { return IsEqual (theOther); }
+
+  //! Check this vector with another vector for non-equality (without tolerance!).
+  bool operator!= (const NCollection_Vec2& theOther)       { return !IsEqual (theOther); }
+  bool operator!= (const NCollection_Vec2& theOther) const { return !IsEqual (theOther); }
+
+  //! Raw access to the data (for OpenGL exchange).
+  const Element_t* GetData()    const { return v; }
+        Element_t* ChangeData()       { return v; }
+  operator const   Element_t*() const { return v; }
+  operator         Element_t*()       { return v; }
 
   //! Compute per-component summary.
   NCollection_Vec2& operator+= (const NCollection_Vec2& theAdd)
@@ -162,6 +169,39 @@ public:
                              v[1] * theFactor);
   }
 
+  //! Compute component-wise minimum of two vectors.
+  NCollection_Vec2 cwiseMin (const NCollection_Vec2& theVec) const
+  {
+    return NCollection_Vec2 (v[0] < theVec.v[0] ? v[0] : theVec.v[0],
+                             v[1] < theVec.v[1] ? v[1] : theVec.v[1]);
+  }
+
+  //! Compute component-wise maximum of two vectors.
+  NCollection_Vec2 cwiseMax (const NCollection_Vec2& theVec) const
+  {
+    return NCollection_Vec2 (v[0] > theVec.v[0] ? v[0] : theVec.v[0],
+                             v[1] > theVec.v[1] ? v[1] : theVec.v[1]);
+  }
+
+  //! Compute component-wise modulus of the vector.
+  NCollection_Vec2 cwiseAbs() const
+  {
+    return NCollection_Vec2 (std::abs (v[0]),
+                             std::abs (v[1]));
+  }
+
+  //! Compute maximum component of the vector.
+  Element_t maxComp() const
+  {
+    return v[0] > v[1] ? v[0] : v[1];
+  }
+
+  //! Compute minimum component of the vector.
+  Element_t minComp() const
+  {
+    return v[0] < v[1] ? v[0] : v[1];
+  }
+
   //! Compute per-component multiplication by scale factor.
   NCollection_Vec2& operator*= (const Element_t theFactor)
   {