Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
Add vec3 math operations
Browse files Browse the repository at this point in the history
  • Loading branch information
mpulkki-mapbox committed Apr 23, 2020
1 parent 1798bda commit 9f8c9c6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 28 deletions.
28 changes: 0 additions & 28 deletions src/mbgl/util/bounding_volumes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,6 @@ vec3 toVec3(const vec4& v) {
return vec3{v[0], v[1], v[2]};
}

vec3 vec3Sub(const vec3& a, const vec3& b) {
return vec3{a[0] - b[0], a[1] - b[1], a[2] - b[2]};
}

vec3 vec3Scale(const vec3& a, double s) {
return vec3{a[0] * s, a[1] * s, a[2] * s};
}

vec3 vec3Cross(const vec3& a, const vec3& b) {
return vec3{a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0]};
}

double vec3Dot(const vec3& a, const vec3& b) {
return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
}

double vec3LengthSq(const vec3& a) {
return vec3Dot(a, a);
}

double vec3Length(const vec3& a) {
return std::sqrt(vec3LengthSq(a));
}

vec3 vec3Normalize(const vec3& a) {
return vec3Scale(a, 1.0 / vec3Length(a));
}

double vec4Dot(const vec4& a, const vec4& b) {
return a[0] * b[0] + a[1] * b[1] + a[2] * b[2] + a[3] * b[3];
}
Expand Down
29 changes: 29 additions & 0 deletions src/mbgl/util/mat3.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#pragma once

#include <array>
#include <cmath>

namespace mbgl {

Expand All @@ -31,6 +32,34 @@ using vec3f = std::array<float, 3>;
using vec3i = std::array<int, 3>;
using mat3 = std::array<double, 9>;

inline vec3 vec3Cross(const vec3& a, const vec3& b) {
return vec3{a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0]};
}

inline double vec3Dot(const vec3& a, const vec3& b) {
return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
}

inline double vec3LengthSq(const vec3& a) {
return vec3Dot(a, a);
}

inline double vec3Length(const vec3& a) {
return std::sqrt(vec3LengthSq(a));
}

inline vec3 vec3Scale(const vec3& a, double s) {
return vec3{a[0] * s, a[1] * s, a[2] * s};
}

inline vec3 vec3Normalize(const vec3& a) {
return vec3Scale(a, 1.0 / vec3Length(a));
}

inline vec3 vec3Sub(const vec3& a, const vec3& b) {
return vec3{a[0] - b[0], a[1] - b[1], a[2] - b[2]};
}

namespace matrix {

void identity(mat3& out);
Expand Down

0 comments on commit 9f8c9c6

Please sign in to comment.