gwnavruntime/basesystem/intcoordsystem.h Source File

intcoordsystem.h
Go to the documentation of this file.
1 /*
2 * Copyright 2016 Autodesk, Inc. All rights reserved.
3 * Use of this software is subject to the terms of the Autodesk license agreement and any attachments or Appendices thereto provided at the time of installation or download,
4 * or which otherwise accompanies this software in either electronic or hard copy form, or which is signed by you and accepted by Autodesk.
5 */
6 
7 
8 #pragma once
9 
10 
14 
15 
16 namespace Kaim
17 {
18 
19 #define KY_IntToPixelBitShift 7
20 #define KY_PixelSizeInIntCoord 128
21 #define KY_HalfPixelSizeInIntCoord 64
22 #define KY_PixelSizeInIntCoord_Float 128.0f
23 
24 class IntCoordSystem
25 {
26 public:
27  // [-320;-193] is snapped to -2
28  // [-192; -65] is snapped to -1
29  // [ -64; 64] is snapped to 0
30  // [ 65; 192] is snapped to 1
31  // [ 193; 320] is snapped to 2
32  // [ 321; 448] is snapped to 3
33  // ...
34  static inline KyInt32 NearestPixelCoord(KyInt32 x) { return IntToPixelCoord(x + Kaim::Isel(x, KY_HalfPixelSizeInIntCoord-1, KY_HalfPixelSizeInIntCoord)); }
35  static inline KyInt32 NearestPixelCoord(KyInt64 x) { return IntToPixelCoord(x + Kaim::Lsel(x, KY_HalfPixelSizeInIntCoord-1, KY_HalfPixelSizeInIntCoord)); }
36 
37  static inline KyInt32 IntPixelSize() { return KY_PixelSizeInIntCoord; }
38 
39  static inline KyInt32 IntToPixelCoord(KyInt32 intCoord) { return intCoord >> KY_IntToPixelBitShift; }
40  static inline KyInt32 IntToPixelCoord(KyInt64 intCoord) { return (KyInt32)(intCoord >> KY_IntToPixelBitShift); }
41  static inline KyInt32 PixelCoordToInt(KyInt32 pixelCoord) { return pixelCoord << KY_IntToPixelBitShift; }
42 
43  // one unit of Int in meter
44  static inline KyFloat32 IntegerPrecision(KyFloat32 rasterPrecision) { return rasterPrecision / KY_PixelSizeInIntCoord_Float; }
45  static inline KyFloat32 InvIntegerPrecision(KyFloat32 rasterPrecision) { return KY_PixelSizeInIntCoord_Float / rasterPrecision; }
46 
47  //PERF WARNING: Int->Float cast = LHS on PPC
48  static inline KyFloat32 IntToNavigation_Dist(KyInt32 intDist, KyFloat32 rasterPrecision)
49  {
50  return IntToNavigation(intDist, IntegerPrecision(rasterPrecision));
51  }
52 
53  //PERF WARNING: Int->Float cast = LHS on PPC
54  static inline KyInt32 NavigationToInt_Dist(KyFloat32 dist, KyFloat32 rasterPrecision)
55  {
56  return NavigationToInt(dist, InvIntegerPrecision(rasterPrecision));
57  }
58 
59  static inline void IntToNavigation_Pos(const Vec2i& intPos, Vec2f& pos, KyFloat32 rasterPrecision)
60  {
61  KyFloat32 integerPrecision = IntegerPrecision(rasterPrecision);
62  pos.x = IntToNavigation(intPos.x, integerPrecision);
63  pos.y = IntToNavigation(intPos.y, integerPrecision);
64  }
65 
66  //PERF WARNING: Int->Float cast = LHS on PPC
67  static inline void IntToNavigation_Pos(const Vec3i& intPos, Vec3f& pos, KyFloat32 rasterPrecision)
68  {
69  KyFloat32 integerPrecision = IntegerPrecision(rasterPrecision);
70  pos.x = IntToNavigation(intPos.x, integerPrecision);
71  pos.y = IntToNavigation(intPos.y, integerPrecision);
72  pos.z = IntToNavigation(intPos.z, integerPrecision);
73  }
74 
75  //PERF WARNING: Int->Float cast = LHS on PPC
76  static inline void PixelToNavigation_Pos(const Vec3i& intPos, Vec3f& pos, KyFloat32 integerPrecision)
77  {
78  pos.x = (KyFloat32)(PixelCoordToInt(intPos.x) * integerPrecision);
79  pos.y = (KyFloat32)(PixelCoordToInt(intPos.y) * integerPrecision);
80  pos.z = (KyFloat32)(PixelCoordToInt(intPos.z) * integerPrecision);
81  }
82 
83  //PERF WARNING: Int->Float cast = LHS on PPC
84  static inline void NavigationToInt_Pos(const Vec2f& pos, Vec2i& intPos, KyFloat32 rasterPrecision)
85  {
86  KyFloat32 invIntegerPrecision = InvIntegerPrecision(rasterPrecision);
87  intPos.x = NavigationToInt(pos.x, invIntegerPrecision);
88  intPos.y = NavigationToInt(pos.y, invIntegerPrecision);
89  }
90 
91  //PERF WARNING: Int->Float cast = LHS on PPC
92  static inline void NavigationToInt_Pos(const Vec3f& pos, Vec3i& intPos, KyFloat32 rasterPrecision)
93  {
94  KyFloat32 invIntegerPrecision = InvIntegerPrecision(rasterPrecision);
95  intPos.x = NavigationToInt(pos.x, invIntegerPrecision);
96  intPos.y = NavigationToInt(pos.y, invIntegerPrecision);
97  intPos.z = NavigationToInt(pos.z, invIntegerPrecision);
98  }
99 
100 private:
101  //PERF WARNING: Int->Float cast = LHS on PPC
102  static inline KyFloat32 IntToNavigation(KyInt32 intDist, KyFloat32 integerPrecision)
103  {
104  return ((KyFloat32)intDist) * integerPrecision;
105  }
106  //PERF WARNING: Int->Float cast = LHS on PPC
107  static inline KyInt32 NavigationToInt(KyFloat32 dist, KyFloat32 invIntegerPrecision)
108  {
109  return NearestInt(dist * invIntegerPrecision);
110  }
111 };
112 
113 }
114 
115 
KyInt32 Isel(KyInt32 a, KyInt32 x, KyInt32 y)
If a is greater than 0, returns x.
Definition: fastmath.h:26
std::int64_t KyInt64
int64_t
Definition: types.h:25
The Autodesk Navigation namespace.
Definition: gamekitcrowddispersion.cpp:17
std::int32_t KyInt32
int32_t
Definition: types.h:24
float KyFloat32
float
Definition: types.h:32
KyInt64 Lsel(KyInt64 a, KyInt64 x, KyInt64 y)
If a is greater than 0, returns x.
Definition: fastmath.h:35