2020-09-13 17:27:05 +00:00
|
|
|
/*
|
2022-08-28 02:57:47 +00:00
|
|
|
** fixedhorizon.h
|
2020-09-13 17:27:05 +00:00
|
|
|
**
|
2022-08-28 02:57:47 +00:00
|
|
|
** type safe representations of high precision horizon values.
|
2020-09-13 17:27:05 +00:00
|
|
|
**
|
|
|
|
**---------------------------------------------------------------------------
|
2022-08-28 02:57:47 +00:00
|
|
|
** Copyright 2020-2022 Christoph Oelckers
|
2020-09-13 17:27:05 +00:00
|
|
|
** All rights reserved.
|
|
|
|
**
|
|
|
|
** Redistribution and use in source and binary forms, with or without
|
|
|
|
** modification, are permitted provided that the following conditions
|
|
|
|
** are met:
|
|
|
|
**
|
|
|
|
** 1. Redistributions of source code must retain the above copyright
|
|
|
|
** notice, this list of conditions and the following disclaimer.
|
|
|
|
** 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
** notice, this list of conditions and the following disclaimer in the
|
|
|
|
** documentation and/or other materials provided with the distribution.
|
|
|
|
** 3. The name of the author may not be used to endorse or promote products
|
|
|
|
** derived from this software without specific prior written permission.
|
|
|
|
**
|
|
|
|
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
**---------------------------------------------------------------------------
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
|
2020-07-22 19:16:29 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <math.h>
|
2021-10-30 08:35:00 +00:00
|
|
|
#include "basics.h"
|
2020-07-22 19:16:29 +00:00
|
|
|
#include "m_fixed.h"
|
2022-08-26 16:28:22 +00:00
|
|
|
#include "vectors.h"
|
2020-07-22 19:16:29 +00:00
|
|
|
#include "xs_Float.h" // needed for reliably overflowing float->int conversions.
|
2020-10-10 13:10:53 +00:00
|
|
|
#include "serializer.h"
|
2020-11-22 20:54:39 +00:00
|
|
|
#include "math/cmath.h"
|
2020-07-22 19:16:29 +00:00
|
|
|
|
2020-10-10 13:10:53 +00:00
|
|
|
class FSerializer;
|
|
|
|
|
2020-10-06 20:34:20 +00:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
//
|
2021-04-10 00:06:30 +00:00
|
|
|
// Functions for use with fixedhoriz and friendly functions.
|
2020-10-06 20:34:20 +00:00
|
|
|
//
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2021-04-10 00:06:30 +00:00
|
|
|
inline double HorizToPitch(double horiz) { return atan2(horiz, 128) * (180. / pi::pi()); }
|
|
|
|
inline double HorizToPitch(fixed_t q16horiz) { return atan2(q16horiz, IntToFixed(128)) * (180. / pi::pi()); }
|
2021-12-06 06:31:00 +00:00
|
|
|
inline fixed_t PitchToHoriz(double pitch) { return xs_CRoundToInt(clamp<double>(IntToFixed(128) * tan(pitch * (pi::pi() / 180.)), INT32_MIN, INT32_MAX)); }
|
|
|
|
inline int32_t PitchToBAM(double pitch) { return xs_CRoundToInt(clamp<double>(pitch * (0x80000000u / 90.), INT32_MIN, INT32_MAX)); }
|
|
|
|
inline constexpr double BAMToPitch(int32_t bam) { return bam * (90. / 0x80000000u); }
|
2020-10-06 20:34:20 +00:00
|
|
|
|
|
|
|
|
2020-11-22 03:40:32 +00:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2020-07-23 14:45:58 +00:00
|
|
|
class fixedhoriz
|
|
|
|
{
|
2020-10-06 20:34:20 +00:00
|
|
|
fixed_t value;
|
2021-12-30 09:30:21 +00:00
|
|
|
|
2020-10-06 20:34:20 +00:00
|
|
|
constexpr fixedhoriz(fixed_t v) : value(v) {}
|
2021-12-30 09:30:21 +00:00
|
|
|
|
2020-10-06 20:34:20 +00:00
|
|
|
friend constexpr fixedhoriz q16horiz(fixed_t v);
|
2020-07-23 14:45:58 +00:00
|
|
|
friend constexpr fixedhoriz buildhoriz(int v);
|
2021-11-01 19:25:38 +00:00
|
|
|
friend fixedhoriz buildfhoriz(double v);
|
2020-10-06 20:34:20 +00:00
|
|
|
friend fixedhoriz pitchhoriz(double v);
|
|
|
|
friend fixedhoriz bamhoriz(int32_t v);
|
2020-10-10 13:10:53 +00:00
|
|
|
|
|
|
|
friend FSerializer &Serialize(FSerializer &arc, const char *key, fixedhoriz &obj, fixedhoriz *defval);
|
2021-12-30 09:30:21 +00:00
|
|
|
|
2020-07-23 14:45:58 +00:00
|
|
|
public:
|
|
|
|
fixedhoriz() = default;
|
|
|
|
fixedhoriz(const fixedhoriz &other) = default;
|
2022-10-12 22:00:35 +00:00
|
|
|
fixedhoriz& operator=(const fixedhoriz&) = default;
|
2020-07-23 14:45:58 +00:00
|
|
|
|
|
|
|
// This class intentionally makes no allowances for implicit type conversions because those would render it ineffective.
|
2020-10-06 20:34:20 +00:00
|
|
|
constexpr short asbuild() const { return FixedToInt(value); }
|
2020-11-22 13:17:07 +00:00
|
|
|
constexpr double asbuildf() const { return FixedToFloat(value); }
|
2020-07-23 14:45:58 +00:00
|
|
|
constexpr fixed_t asq16() const { return value; }
|
2020-10-06 20:34:20 +00:00
|
|
|
double aspitch() const { return HorizToPitch(value); }
|
|
|
|
int32_t asbam() const { return PitchToBAM(aspitch()); }
|
2021-12-30 09:30:21 +00:00
|
|
|
|
2020-07-23 14:45:58 +00:00
|
|
|
bool operator< (fixedhoriz other) const
|
|
|
|
{
|
|
|
|
return value < other.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator> (fixedhoriz other) const
|
|
|
|
{
|
|
|
|
return value > other.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator<= (fixedhoriz other) const
|
|
|
|
{
|
|
|
|
return value <= other.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator>= (fixedhoriz other) const
|
|
|
|
{
|
|
|
|
return value >= other.value;
|
|
|
|
}
|
|
|
|
constexpr bool operator== (fixedhoriz other) const
|
|
|
|
{
|
|
|
|
return value == other.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr bool operator!= (fixedhoriz other) const
|
|
|
|
{
|
|
|
|
return value != other.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr fixedhoriz &operator+= (fixedhoriz other)
|
|
|
|
{
|
|
|
|
value += other.value;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr fixedhoriz &operator-= (fixedhoriz other)
|
|
|
|
{
|
|
|
|
value -= other.value;
|
|
|
|
return *this;
|
|
|
|
}
|
2021-12-30 09:30:21 +00:00
|
|
|
|
2020-07-23 14:45:58 +00:00
|
|
|
constexpr fixedhoriz operator- () const
|
|
|
|
{
|
|
|
|
return fixedhoriz(-value);
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr fixedhoriz operator+ (fixedhoriz other) const
|
|
|
|
{
|
|
|
|
return fixedhoriz(value + other.value);
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr fixedhoriz operator- (fixedhoriz other) const
|
|
|
|
{
|
|
|
|
return fixedhoriz(value - other.value);
|
|
|
|
}
|
|
|
|
|
2020-11-22 20:54:39 +00:00
|
|
|
constexpr fixedhoriz &operator<<= (const uint8_t shift)
|
2020-11-22 10:46:17 +00:00
|
|
|
{
|
|
|
|
value <<= shift;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-11-22 20:54:39 +00:00
|
|
|
constexpr fixedhoriz &operator>>= (const uint8_t shift)
|
2020-11-22 10:46:17 +00:00
|
|
|
{
|
|
|
|
value >>= shift;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-11-22 20:54:39 +00:00
|
|
|
constexpr fixedhoriz operator<< (const uint8_t shift) const
|
2020-11-22 10:46:17 +00:00
|
|
|
{
|
|
|
|
return fixedhoriz(value << shift);
|
|
|
|
}
|
|
|
|
|
2020-11-22 20:54:39 +00:00
|
|
|
constexpr fixedhoriz operator>> (const uint8_t shift) const
|
2020-11-22 10:46:17 +00:00
|
|
|
{
|
|
|
|
return fixedhoriz(value >> shift);
|
|
|
|
}
|
|
|
|
|
2022-09-07 04:20:51 +00:00
|
|
|
template<class T>
|
|
|
|
constexpr fixedhoriz &operator*= (const T other)
|
|
|
|
{
|
|
|
|
value = value * other;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
constexpr fixedhoriz operator* (const T other) const
|
|
|
|
{
|
|
|
|
return value * other;
|
|
|
|
}
|
2020-07-22 19:16:29 +00:00
|
|
|
};
|
|
|
|
|
2020-10-06 20:34:20 +00:00
|
|
|
inline constexpr fixedhoriz q16horiz(fixed_t v) { return fixedhoriz(v); }
|
2020-09-01 13:00:35 +00:00
|
|
|
inline constexpr fixedhoriz buildhoriz(int v) { return fixedhoriz(IntToFixed(v)); }
|
2021-11-01 19:25:38 +00:00
|
|
|
inline fixedhoriz buildfhoriz(double v) { return fixedhoriz(FloatToFixed(v)); }
|
2020-10-06 20:34:20 +00:00
|
|
|
inline fixedhoriz pitchhoriz(double v) { return fixedhoriz(PitchToHoriz(v)); }
|
|
|
|
inline fixedhoriz bamhoriz(int32_t v) { return pitchhoriz(BAMToPitch(v)); }
|
2020-07-23 14:45:58 +00:00
|
|
|
|
2020-11-22 03:40:32 +00:00
|
|
|
inline FSerializer &Serialize(FSerializer &arc, const char *key, fixedhoriz &obj, fixedhoriz *defval)
|
2020-10-11 04:34:18 +00:00
|
|
|
{
|
|
|
|
return Serialize(arc, key, obj.value, defval ? &defval->value : nullptr);
|
|
|
|
}
|