1/*
2 * Copyright 2018 Uber Technologies, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16/** @file vec3d.c
17 * @brief 3D floating point vector functions.
18 */
19
20#include "vec3d.h"
21#include <math.h>
22
23/**
24 * Square of a number
25 *
26 * @param x The input number.
27 * @return The square of the input number.
28 */
29double _square(double x) { return x * x; }
30
31/**
32 * Calculate the square of the distance between two 3D coordinates.
33 *
34 * @param v1 The first 3D coordinate.
35 * @param v2 The second 3D coordinate.
36 * @return The square of the distance between the given points.
37 */
38double _pointSquareDist(const Vec3d* v1, const Vec3d* v2) {
39 return _square(v1->x - v2->x) + _square(v1->y - v2->y) +
40 _square(v1->z - v2->z);
41}
42
43/**
44 * Calculate the 3D coordinate on unit sphere from the latitude and longitude.
45 *
46 * @param geo The latitude and longitude of the point.
47 * @param v The 3D coordinate of the point.
48 */
49void _geoToVec3d(const GeoCoord* geo, Vec3d* v) {
50 double r = cos(geo->lat);
51
52 v->z = sin(geo->lat);
53 v->x = cos(geo->lon) * r;
54 v->y = sin(geo->lon) * r;
55}
56