| 1 | // Created by: Kirill GAVRILOV |
| 2 | // Copyright (c) 2012 OPEN CASCADE SAS |
| 3 | // |
| 4 | // The content of this file is subject to the Open CASCADE Technology Public |
| 5 | // License Version 6.5 (the "License"). You may not use the content of this file |
| 6 | // except in compliance with the License. Please obtain a copy of the License |
| 7 | // at http://www.opencascade.org and read it completely before using this file. |
| 8 | // |
| 9 | // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its |
| 10 | // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France. |
| 11 | // |
| 12 | // The Original Code and all software distributed under the License is |
| 13 | // distributed on an "AS IS" basis, without warranty of any kind, and the |
| 14 | // Initial Developer hereby disclaims all such warranties, including without |
| 15 | // limitation, any warranties of merchantability, fitness for a particular |
| 16 | // purpose or non-infringement. Please see the License for the specific terms |
| 17 | // and conditions governing the rights and limitations under the License. |
| 18 | |
| 19 | #ifndef _NCollection_Vec2_H__ |
| 20 | #define _NCollection_Vec2_H__ |
| 21 | |
| 22 | #include <cmath> // std::sqrt() |
| 23 | |
| 24 | //! Auxiliary macros to define couple of similar access components as vector methods. |
| 25 | //! @return 2 components by their names in specified order |
| 26 | #define NCOLLECTION_VEC_COMPONENTS_2D(theX, theY) \ |
| 27 | const NCollection_Vec2<Element_t> theX##theY() const { return NCollection_Vec2<Element_t>(theX(), theY()); } \ |
| 28 | const NCollection_Vec2<Element_t> theY##theX() const { return NCollection_Vec2<Element_t>(theY(), theX()); } |
| 29 | |
| 30 | //! Defines the 2D-vector template. |
| 31 | //! The main target for this class - to handle raw low-level arrays (from/to graphic driver etc.). |
| 32 | template<typename Element_t> |
| 33 | class NCollection_Vec2 |
| 34 | { |
| 35 | |
| 36 | public: |
| 37 | |
| 38 | //! Returns the number of components. |
| 39 | static int Length() |
| 40 | { |
| 41 | return 2; |
| 42 | } |
| 43 | |
| 44 | //! Empty constructor. Construct the zero vector. |
| 45 | NCollection_Vec2() |
| 46 | { |
| 47 | v[0] = v[1] = Element_t(0); |
| 48 | } |
| 49 | |
| 50 | //! Initialize ALL components of vector within specified value. |
| 51 | explicit NCollection_Vec2 (const Element_t theXY) |
| 52 | { |
| 53 | v[0] = v[1] = theXY; |
| 54 | } |
| 55 | |
| 56 | //! Per-component constructor. |
| 57 | explicit NCollection_Vec2 (const Element_t theX, |
| 58 | const Element_t theY) |
| 59 | { |
| 60 | v[0] = theX; |
| 61 | v[1] = theY; |
| 62 | } |
| 63 | |
| 64 | //! Copy constructor. |
| 65 | NCollection_Vec2 (const NCollection_Vec2& theVec2) |
| 66 | { |
| 67 | v[0] = theVec2[0]; |
| 68 | v[1] = theVec2[1]; |
| 69 | } |
| 70 | |
| 71 | //! Assignment operator. |
| 72 | const NCollection_Vec2& operator= (const NCollection_Vec2& theVec2) |
| 73 | { |
| 74 | v[0] = theVec2[0]; |
| 75 | v[1] = theVec2[1]; |
| 76 | return *this; |
| 77 | } |
| 78 | |
| 79 | //! Alias to 1st component as X coordinate in XY. |
| 80 | Element_t x() const { return v[0]; } |
| 81 | |
| 82 | //! Alias to 2nd component as Y coordinate in XY. |
| 83 | Element_t y() const { return v[1]; } |
| 84 | |
| 85 | //! @return 2 components by their names in specified order (in GLSL-style) |
| 86 | NCOLLECTION_VEC_COMPONENTS_2D(x, y); |
| 87 | |
| 88 | //! Alias to 1st component as X coordinate in XY. |
| 89 | Element_t& x() { return v[0]; } |
| 90 | |
| 91 | //! Alias to 2nd component as Y coordinate in XY. |
| 92 | Element_t& y() { return v[1]; } |
| 93 | |
| 94 | //! Raw access to the data (for OpenGL exchange). |
| 95 | const Element_t* GetData() const { return v; } |
| 96 | Element_t* ChangeData() { return v; } |
| 97 | operator const Element_t*() const { return v; } |
| 98 | operator Element_t*() { return v; } |
| 99 | |
| 100 | //! Compute per-component summary. |
| 101 | NCollection_Vec2& operator+= (const NCollection_Vec2& theAdd) |
| 102 | { |
| 103 | v[0] += theAdd.v[0]; |
| 104 | v[1] += theAdd.v[1]; |
| 105 | return *this; |
| 106 | } |
| 107 | |
| 108 | //! Compute per-component summary. |
| 109 | friend NCollection_Vec2 operator+ (const NCollection_Vec2& theLeft, |
| 110 | const NCollection_Vec2& theRight) |
| 111 | { |
| 112 | return NCollection_Vec2 (theLeft.v[0] + theRight.v[0], |
| 113 | theLeft.v[1] + theRight.v[1]); |
| 114 | } |
| 115 | |
| 116 | //! Compute per-component subtraction. |
| 117 | NCollection_Vec2& operator-= (const NCollection_Vec2& theDec) |
| 118 | { |
| 119 | v[0] -= theDec.v[0]; |
| 120 | v[1] -= theDec.v[1]; |
| 121 | return *this; |
| 122 | } |
| 123 | |
| 124 | //! Compute per-component subtraction. |
| 125 | friend NCollection_Vec2 operator- (const NCollection_Vec2& theLeft, |
| 126 | const NCollection_Vec2& theRight) |
| 127 | { |
| 128 | return NCollection_Vec2 (theLeft.v[0] - theRight.v[0], |
| 129 | theLeft.v[1] - theRight.v[1]); |
| 130 | } |
| 131 | |
| 132 | //! Unary -. |
| 133 | NCollection_Vec2 operator-() const |
| 134 | { |
| 135 | return NCollection_Vec2 (-x(), -y()); |
| 136 | } |
| 137 | |
| 138 | //! Compute per-component multiplication. |
| 139 | NCollection_Vec2& operator*= (const NCollection_Vec2& theRight) |
| 140 | { |
| 141 | v[0] *= theRight.v[0]; |
| 142 | v[1] *= theRight.v[1]; |
| 143 | return *this; |
| 144 | } |
| 145 | |
| 146 | //! Compute per-component multiplication. |
| 147 | friend NCollection_Vec2 operator* (const NCollection_Vec2& theLeft, |
| 148 | const NCollection_Vec2& theRight) |
| 149 | { |
| 150 | return NCollection_Vec2 (theLeft.v[0] * theRight.v[0], |
| 151 | theLeft.v[1] * theRight.v[1]); |
| 152 | } |
| 153 | |
| 154 | //! Compute per-component multiplication by scale factor. |
| 155 | void Multiply (const Element_t theFactor) |
| 156 | { |
| 157 | v[0] *= theFactor; |
| 158 | v[1] *= theFactor; |
| 159 | } |
| 160 | |
| 161 | //! Compute per-component multiplication by scale factor. |
| 162 | NCollection_Vec2 Multiplied (const Element_t theFactor) const |
| 163 | { |
| 164 | return NCollection_Vec2 (v[0] * theFactor, |
| 165 | v[1] * theFactor); |
| 166 | } |
| 167 | |
| 168 | //! Compute per-component multiplication by scale factor. |
| 169 | NCollection_Vec2& operator*= (const Element_t theFactor) |
| 170 | { |
| 171 | Multiply (theFactor); |
| 172 | return *this; |
| 173 | } |
| 174 | |
| 175 | //! Compute per-component division by scale factor. |
| 176 | NCollection_Vec2& operator/= (const Element_t theInvFactor) |
| 177 | { |
| 178 | v[0] /= theInvFactor; |
| 179 | v[1] /= theInvFactor; |
| 180 | return *this; |
| 181 | } |
| 182 | |
| 183 | //! Compute per-component multiplication by scale factor. |
| 184 | NCollection_Vec2 operator* (const Element_t theFactor) const |
| 185 | { |
| 186 | return Multiplied (theFactor); |
| 187 | } |
| 188 | |
| 189 | //! Compute per-component division by scale factor. |
| 190 | NCollection_Vec2 operator/ (const Element_t theInvFactor) const |
| 191 | { |
| 192 | return NCollection_Vec2(v[0] / theInvFactor, |
| 193 | v[1] / theInvFactor); |
| 194 | } |
| 195 | |
| 196 | //! Computes the dot product. |
| 197 | Element_t Dot (const NCollection_Vec2& theOther) const |
| 198 | { |
| 199 | return x() * theOther.x() + y() * theOther.y(); |
| 200 | } |
| 201 | |
| 202 | //! Computes the vector modulus (magnitude, length). |
| 203 | Element_t Modulus() const |
| 204 | { |
| 205 | return std::sqrt (x() * x() + y() * y()); |
| 206 | } |
| 207 | |
| 208 | //! Computes the square of vector modulus (magnitude, length). |
| 209 | //! This method may be used for performance tricks. |
| 210 | Element_t SquareModulus() const |
| 211 | { |
| 212 | return x() * x() + y() * y(); |
| 213 | } |
| 214 | |
| 215 | //! Constuct DX unit vector. |
| 216 | static NCollection_Vec2 DX() |
| 217 | { |
| 218 | return NCollection_Vec2 (Element_t(1), Element_t(0)); |
| 219 | } |
| 220 | |
| 221 | //! Constuct DY unit vector. |
| 222 | static NCollection_Vec2 DY() |
| 223 | { |
| 224 | return NCollection_Vec2 (Element_t(0), Element_t(1)); |
| 225 | } |
| 226 | |
| 227 | private: |
| 228 | |
| 229 | Element_t v[2]; |
| 230 | |
| 231 | }; |
| 232 | |
| 233 | #endif // _NCollection_Vec2_H__ |