src/utilities/geometry/Vector3d.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /*********************************************************************************************************************** | ||
| 2 | * OpenStudio(R), Copyright (c) Alliance for Energy Innovation, LLC. | ||
| 3 | * See also https://openstudio.net/license | ||
| 4 | ***********************************************************************************************************************/ | ||
| 5 | |||
| 6 | #include "Vector3d.hpp" | ||
| 7 | |||
| 8 | namespace openstudio { | ||
| 9 | |||
| 10 | /// default constructor creates vector with 0, 0, 0 | ||
| 11 |
1/1✓ Branch 1 taken 2495146 times.
|
2495146 | Vector3d::Vector3d() : m_storage(3, 0.0) {} |
| 12 | |||
| 13 | /// constructor with x, y, z | ||
| 14 | 23665421 | Vector3d::Vector3d(double x, double y, double z) : m_storage(3) { | |
| 15 |
1/1✓ Branch 1 taken 23665421 times.
|
23665421 | m_storage[0] = x; |
| 16 |
1/1✓ Branch 1 taken 23665421 times.
|
23665421 | m_storage[1] = y; |
| 17 |
1/1✓ Branch 1 taken 23665421 times.
|
23665421 | m_storage[2] = z; |
| 18 | 23665421 | } | |
| 19 | |||
| 20 | /// get x | ||
| 21 | 36300087 | double Vector3d::x() const { | |
| 22 | 36300087 | return m_storage[0]; | |
| 23 | } | ||
| 24 | |||
| 25 | /// get y | ||
| 26 | 36297152 | double Vector3d::y() const { | |
| 27 | 36297152 | return m_storage[1]; | |
| 28 | } | ||
| 29 | |||
| 30 | /// get z | ||
| 31 | 36264403 | double Vector3d::z() const { | |
| 32 | 36264403 | return m_storage[2]; | |
| 33 | } | ||
| 34 | |||
| 35 | /// addition | ||
| 36 | ✗ | Vector3d Vector3d::operator+(const Vector3d& other) const { | |
| 37 | ✗ | double newX = x() + other.x(); | |
| 38 | ✗ | double newY = y() + other.y(); | |
| 39 | ✗ | double newZ = z() + other.z(); | |
| 40 | ✗ | return {newX, newY, newZ}; | |
| 41 | } | ||
| 42 | |||
| 43 | /// addition | ||
| 44 | 6284658 | Vector3d& Vector3d::operator+=(const Vector3d& other) { | |
| 45 | 6284658 | m_storage[0] += other.x(); | |
| 46 | 6284658 | m_storage[1] += other.y(); | |
| 47 | 6284658 | m_storage[2] += other.z(); | |
| 48 | 6284658 | return *this; | |
| 49 | } | ||
| 50 | |||
| 51 | /// subtraction | ||
| 52 | 166412 | Vector3d Vector3d::operator-(const Vector3d& other) const { | |
| 53 | 166412 | double newX = x() - other.x(); | |
| 54 | 166412 | double newY = y() - other.y(); | |
| 55 | 166412 | double newZ = z() - other.z(); | |
| 56 | 166412 | return {newX, newY, newZ}; | |
| 57 | } | ||
| 58 | |||
| 59 | /// subtraction | ||
| 60 | ✗ | Vector3d& Vector3d::operator-=(const Vector3d& other) { | |
| 61 | ✗ | m_storage[0] -= other.x(); | |
| 62 | ✗ | m_storage[1] -= other.y(); | |
| 63 | ✗ | m_storage[2] -= other.z(); | |
| 64 | ✗ | return *this; | |
| 65 | } | ||
| 66 | |||
| 67 | /// check equality | ||
| 68 | 22 | bool Vector3d::operator==(const Vector3d& other) const { | |
| 69 | 22 | return (m_storage == other.m_storage); | |
| 70 | } | ||
| 71 | |||
| 72 | /// check inequality | ||
| 73 | 2 | bool Vector3d::operator!=(const Vector3d& other) const { | |
| 74 | 2 | return (m_storage != other.m_storage); | |
| 75 | } | ||
| 76 | |||
| 77 | /// ostream operator | ||
| 78 | 1 | std::ostream& operator<<(std::ostream& os, const Vector3d& vec) { | |
| 79 | 1 | os << "[" << vec.x() << ", " << vec.y() << ", " << vec.z() << "]"; | |
| 80 | 1 | return os; | |
| 81 | } | ||
| 82 | |||
| 83 | /// ostream operator | ||
| 84 | ✗ | std::ostream& operator<<(std::ostream& os, const std::vector<Vector3d>& vecVector) { | |
| 85 | ✗ | os << "["; | |
| 86 | ✗ | for (unsigned i = 0; i < vecVector.size(); ++i) { | |
| 87 | ✗ | os << vecVector[i]; | |
| 88 | ✗ | if (i < vecVector.size() - 1) { | |
| 89 | ✗ | os << ", "; | |
| 90 | } | ||
| 91 | } | ||
| 92 | ✗ | os << "]"; | |
| 93 | ✗ | return os; | |
| 94 | } | ||
| 95 | |||
| 96 | /// negation | ||
| 97 | 10 | Vector3d operator-(const Vector3d& vec) { | |
| 98 | 10 | return {-vec.x(), -vec.y(), -vec.z()}; | |
| 99 | } | ||
| 100 | |||
| 101 | /// multiplication by a scalar | ||
| 102 | 257011 | Vector3d operator*(double mult, const Vector3d& vec) { | |
| 103 | 257011 | return {mult * vec.x(), mult * vec.y(), mult * vec.z()}; | |
| 104 | } | ||
| 105 | |||
| 106 | /// normalize to one | ||
| 107 | 2417306 | bool Vector3d::normalize() { | |
| 108 | 2417306 | return setLength(1.0); | |
| 109 | } | ||
| 110 | |||
| 111 | /// get a vector which is the reverse of this | ||
| 112 | 12972 | Vector3d Vector3d::reverseVector() const { | |
| 113 | 12972 | return {-m_storage[0], -m_storage[1], -m_storage[2]}; | |
| 114 | } | ||
| 115 | |||
| 116 | /// get length | ||
| 117 | 4838582 | double Vector3d::length() const { | |
| 118 | 4838582 | return norm_2(m_storage); | |
| 119 | } | ||
| 120 | |||
| 121 | /// set length | ||
| 122 | 2430134 | bool Vector3d::setLength(double newLength) { | |
| 123 | 2430134 | bool result = false; | |
| 124 | 2430134 | double currentLength = length(); | |
| 125 |
2/2✓ Branch 0 taken 2430116 times.
✓ Branch 1 taken 18 times.
|
2430134 | if (currentLength > 0) { |
| 126 | 2430116 | double mult = newLength / currentLength; | |
| 127 | 2430116 | m_storage[0] *= mult; | |
| 128 | 2430116 | m_storage[1] *= mult; | |
| 129 | 2430116 | m_storage[2] *= mult; | |
| 130 | 2430116 | result = true; | |
| 131 | } | ||
| 132 | 2430134 | return result; | |
| 133 | } | ||
| 134 | |||
| 135 | /// orthogonal right | ||
| 136 | 22122 | Vector3d Vector3d::orthogonalRight() const { | |
| 137 | 22122 | return {m_storage[1], -m_storage[0], m_storage[2]}; | |
| 138 | } | ||
| 139 | |||
| 140 | /// orthogonal left | ||
| 141 | 31548 | Vector3d Vector3d::orthogonalLeft() const { | |
| 142 | 31548 | return {-m_storage[1], m_storage[0], m_storage[2]}; | |
| 143 | } | ||
| 144 | |||
| 145 | /// dot product with another Vector3d | ||
| 146 | 1445174 | double Vector3d::dot(const Vector3d& other) const { | |
| 147 | 1445174 | return inner_prod(m_storage, other.m_storage); | |
| 148 | } | ||
| 149 | |||
| 150 | /// cross product with another Vector3d | ||
| 151 | 6391080 | Vector3d Vector3d::cross(const Vector3d& other) const { | |
| 152 | 6391080 | double newX = (y() * other.z() - z() * other.y()); | |
| 153 | 6391080 | double newY = (z() * other.x() - x() * other.z()); | |
| 154 | 6391080 | double newZ = (x() * other.y() - y() * other.x()); | |
| 155 | 6391080 | return {newX, newY, newZ}; | |
| 156 | } | ||
| 157 | |||
| 158 | /// get the Vector directly | ||
| 159 | 12308 | Vector Vector3d::vector() const { | |
| 160 | 12308 | return m_storage; | |
| 161 | } | ||
| 162 | |||
| 163 | } // namespace openstudio | ||
| 164 |