lookuptable.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. ** Command & Conquer Generals(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /***********************************************************************************************
  19. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : WWPhys *
  23. * *
  24. * $Archive:: /Commando/Code/wwmath/lookuptable.h $*
  25. * *
  26. * Original Author:: Greg Hjelstrom *
  27. * *
  28. * $Author:: Greg_h $*
  29. * *
  30. * $Modtime:: 4/18/00 1:20p $*
  31. * *
  32. * $Revision:: 6 $*
  33. * *
  34. *---------------------------------------------------------------------------------------------*
  35. * Functions: *
  36. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  37. #ifndef LOOKUPTABLE_H
  38. #define LOOKUPTABLE_H
  39. #include "always.h"
  40. #include "simplevec.h"
  41. #include "wwstring.h"
  42. #include "refcount.h"
  43. #include "multilist.h"
  44. #include "wwmath.h"
  45. class Vector2;
  46. class Curve1DClass;
  47. class ChunkSaveClass;
  48. class ChunkLoadClass;
  49. /**
  50. ** LookupTableClass
  51. ** This class contains the tabulated values for a function.
  52. */
  53. class LookupTableClass : public RefCountClass, public MultiListObjectClass
  54. {
  55. public:
  56. LookupTableClass(int sample_count = 256);
  57. virtual ~LookupTableClass(void);
  58. void Init(const char * name,Curve1DClass * curve);
  59. float Get_Value(float input);
  60. float Get_Value_Quick(float input);
  61. const char * Get_Name(void) { return Name; }
  62. protected:
  63. StringClass Name; // name of this table, if it came from a file, this is also the filename
  64. float MinInputValue;
  65. float MaxInputValue;
  66. float OOMaxMinusMin;
  67. SimpleVecClass<float> OutputSamples;
  68. };
  69. inline float LookupTableClass::Get_Value(float input)
  70. {
  71. if (input <= MinInputValue) {
  72. return OutputSamples[0];
  73. }
  74. if (input >= MaxInputValue) {
  75. return OutputSamples[OutputSamples.Length()-1];
  76. }
  77. float normalized_input = (float)(OutputSamples.Length()-1) * (input - MinInputValue) * OOMaxMinusMin;
  78. float input0 = WWMath::Floor(normalized_input);
  79. int index0 = WWMath::Float_To_Long(input0);
  80. int index1 = index0+1;
  81. float lerp = normalized_input - input0;
  82. return OutputSamples[index0] + lerp * (OutputSamples[index1] - OutputSamples[index0]);
  83. }
  84. inline float LookupTableClass::Get_Value_Quick(float input)
  85. {
  86. if (input <= MinInputValue) {
  87. return OutputSamples[0];
  88. }
  89. if (input >= MaxInputValue) {
  90. return OutputSamples[OutputSamples.Length()-1];
  91. }
  92. int index = (OutputSamples.Length()-1) * WWMath::Float_To_Long((input - MinInputValue) * OOMaxMinusMin);
  93. return OutputSamples[index];
  94. }
  95. /**
  96. ** LookupTableMgrClass
  97. ** This class tracks all of the LookupTableClass's that have been loaded or installed.
  98. ** LookupTables can be created using the "SimpleGraph" tool. It basically allows you
  99. ** to edit a curve which will be used to generate the table. These curves are stored
  100. ** in .TBL files.
  101. **
  102. ** NOTE: Use the SimpleGraph program to create lookup tables! Then just ask for them
  103. ** by filename and it will load the table for you (unless it is already loaded).
  104. **
  105. ** NOTE: I add a table called "DefaultTable" so that you can revert to that if
  106. ** your table isn't found.
  107. */
  108. class LookupTableMgrClass
  109. {
  110. public:
  111. LookupTableMgrClass(void);
  112. ~LookupTableMgrClass(void);
  113. // init and shutdown are automatically called from WWMath::Init, WWMath::Shutdown...
  114. static void Init(void);
  115. static void Shutdown(void);
  116. static bool Add_Table(LookupTableClass * table);
  117. static bool Remove_Table(LookupTableClass * table);
  118. static LookupTableClass * Get_Table(const char * name,bool try_to_load = true);
  119. static void Save_Table_Desc( ChunkSaveClass & csave,
  120. Curve1DClass * curve,
  121. const Vector2 & min,
  122. const Vector2 & max );
  123. static void Load_Table_Desc( ChunkLoadClass & cload,
  124. Curve1DClass ** curve_ptr,
  125. Vector2 * set_min = NULL,
  126. Vector2 * set_max = NULL );
  127. static void Reset(void);
  128. protected:
  129. static RefMultiListClass<LookupTableClass> Tables;
  130. };
  131. #endif // LOOKUPTABLE_H