gucoordinates
camera_pivot.c
Go to the documentation of this file.
1/*
2 * camera_pivot.c
3 * gunavigation
4 *
5 * Created by Callum McColl on 19/06/2020.
6 * Copyright © 2020 Callum McColl. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer in the documentation and/or other materials
18 * provided with the distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgement:
22 *
23 * This product includes software developed by Callum McColl.
24 *
25 * 4. Neither the name of the author nor the names of contributors
26 * may be used to endorse or promote products derived from this
27 * software without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
33 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
34 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
35 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
36 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
37 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
38 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
39 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 *
41 * -----------------------------------------------------------------------
42 * This program is free software; you can redistribute it and/or
43 * modify it under the above terms or under the terms of the GNU
44 * General Public License as published by the Free Software Foundation;
45 * either version 2 of the License, or (at your option) any later version.
46 *
47 * This program is distributed in the hope that it will be useful,
48 * but WITHOUT ANY WARRANTY; without even the implied warranty of
49 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
50 * GNU General Public License for more details.
51 *
52 * You should have received a copy of the GNU General Public License
53 * along with this program; if not, see http://www.gnu.org/licenses/
54 * or write to the Free Software Foundation, Inc., 51 Franklin Street,
55 * Fifth Floor, Boston, MA 02110-1301, USA.
56 *
57 */
58
59#include "camera_pivot.h"
60
61#include <stdbool.h>
62#include <math.h>
63
64bool gu_camera_pivot_equals(const gu_camera_pivot lhs, const gu_camera_pivot rhs, const double tolerance)
65{
66 const bool headEqual = fabs(deg_d_to_d(lhs.pitch) - deg_d_to_d(rhs.pitch)) <= tolerance
67 && fabs(deg_d_to_d(lhs.yaw) - deg_d_to_d(rhs.yaw)) <= tolerance
68 && fabs(cm_d_to_d(lhs.height) - cm_d_to_d(rhs.height)) <= tolerance;
69 if (!headEqual || lhs.numCameras != rhs.numCameras)
70 {
71 return false;
72 }
73 for (int i = 0; i < lhs.numCameras; i++)
74 {
75 if (!gu_camera_equals(lhs.cameras[i], rhs.cameras[i], tolerance))
76 {
77 return false;
78 }
79 }
80 return true;
81}
82
83centimetres_d gu_camera_pivot_calculate_camera_height(const gu_camera_pivot camera_pivot, const int cameraOffset)
84{
85 const gu_camera camera = camera_pivot.cameras[cameraOffset];
86 const centimetres_d totalHeight = camera_pivot.height + camera.height;
87 const double cosPitch = cos(deg_d_to_rad_d(camera_pivot.pitch));
88 const double sinPitch = sin(deg_d_to_rad_d(camera_pivot.pitch));
89 return (totalHeight - camera.height * d_to_cm_d(1.0 - cosPitch)) - camera.centerOffset * d_to_cm_d(sinPitch);
90}
91
92
93
94bool gu_camera_pivot_object_on_ground(const gu_camera_pivot camera_pivot, const int cameraOffset, const gu_percent_coordinate coord)
95{
96 const gu_camera camera = camera_pivot.cameras[cameraOffset];
97 const degrees_d pitchToObject = camera_pivot.pitch + camera.vDirection - d_to_deg_d(pct_d_to_d(coord.y)) * (camera.vFov / 2.0);
98 return pitchToObject > 0.0 && pitchToObject < 90.0;
99}
100
101bool gu_camera_pivot_can_see_object(const gu_camera_pivot camera_pivot, const int cameraOffset, const gu_relative_coordinate coord)
102{
103 const gu_camera camera = camera_pivot.cameras[cameraOffset];
104 const degrees_d yaw = coord.direction - camera_pivot.yaw;
105 const radians_d yawRad = deg_d_to_rad_d(coord.direction);
106 const double frontDistance = cm_d_to_d(mm_u_to_cm_d(coord.distance)) * cos(rad_d_to_d(yawRad)) + cm_d_to_d(camera.centerOffset);
107 const centimetres_d actualCameraHeight = gu_camera_pivot_calculate_camera_height(camera_pivot, cameraOffset);
108 const degrees_d totalPitch = 90.0 - rad_d_to_deg_d(d_to_rad_d(atan2(frontDistance, cm_d_to_d(actualCameraHeight))));
109 const degrees_d pitch = totalPitch - camera.vDirection - camera_pivot.pitch;
110 const degrees_d yawBound = camera.hFov / 2.0;
111 const degrees_d pitchBound = camera.vFov / 2.0;
112 return yaw >= -yawBound && yaw <= yawBound && pitch >= -pitchBound && pitch <= pitchBound;
113}
bool gu_camera_equals(const gu_camera lhs, const gu_camera rhs, const double tolerance)
Definition: camera.c:64
bool gu_camera_pivot_equals(const gu_camera_pivot lhs, const gu_camera_pivot rhs, const double tolerance)
Definition: camera_pivot.c:64
centimetres_d gu_camera_pivot_calculate_camera_height(const gu_camera_pivot camera_pivot, const int cameraOffset)
Definition: camera_pivot.c:83
bool gu_camera_pivot_object_on_ground(const gu_camera_pivot camera_pivot, const int cameraOffset, const gu_percent_coordinate coord)
Definition: camera_pivot.c:94
bool gu_camera_pivot_can_see_object(const gu_camera_pivot camera_pivot, const int cameraOffset, const gu_relative_coordinate coord)
Definition: camera_pivot.c:101
A gu_camera_pivot represents the pivot point which a gu_camera is attached to.
Definition: camera_pivot.h:87
degrees_d pitch
The vertical orientation of the pivot point.
Definition: camera_pivot.h:92
degrees_d yaw
The horizontal orientation of the pivot point.
Definition: camera_pivot.h:97
centimetres_d height
The vertical distance from the ground to the pivot point.
Definition: camera_pivot.h:102
gu_camera cameras[2]
The gu_cameras attached to this pivot point.
Definition: camera_pivot.h:107
int numCameras
The number of elements in cameras.
Definition: camera_pivot.h:112
centimetres_d centerOffset
The distance the camera is from the center point.
Definition: camera.h:92
centimetres_d height
The height from the pivot of the camera to the middle of the camera.
Definition: camera.h:80
degrees_d hFov
The horizontal field of view.
Definition: camera.h:110
degrees_d vFov
The vertical field of view.
Definition: camera.h:105
degrees_d vDirection
The degree in which the camera is facing in the vertical direction.
Definition: camera.h:100
A gu_percent_coordinate represents the coordinate of a pixel within an image.
percent_d y
The y coordinate of the pixel within the image as a percentage.
A coordinate that is relative to some other coordinate.
degrees_d direction
The heading towards the coordinate.
millimetres_u distance
The distance to the coordinate.