clusterfit.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /* -----------------------------------------------------------------------------
  2. Copyright (c) 2006 Simon Brown [email protected]
  3. Copyright (c) 2007 Ignacio Castano [email protected]
  4. Permission is hereby granted, free of charge, to any person obtaining
  5. a copy of this software and associated documentation files (the
  6. "Software"), to deal in the Software without restriction, including
  7. without limitation the rights to use, copy, modify, merge, publish,
  8. distribute, sublicense, and/or sell copies of the Software, and to
  9. permit persons to whom the Software is furnished to do so, subject to
  10. the following conditions:
  11. The above copyright notice and this permission notice shall be included
  12. in all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  14. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  15. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  16. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  17. CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  18. TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  19. SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. -------------------------------------------------------------------------- */
  21. #include "clusterfit.h"
  22. #include "colourset.h"
  23. #include "colourblock.h"
  24. #include "squishMath.h"
  25. #include <algorithm>
  26. namespace squish {
  27. ClusterFit::ClusterFit( ColourSet const* colours, int flags )
  28. : ColourFit( colours, flags )
  29. {
  30. // set the iteration count
  31. m_iterationCount = ( m_flags & kColourIterativeClusterFit ) ? kMaxIterations : 1;
  32. // initialise the best error
  33. m_besterror = VEC4_CONST( FLT_MAX );
  34. // initialise the metric
  35. bool perceptual = ( ( m_flags & kColourMetricPerceptual ) != 0 );
  36. if( perceptual )
  37. m_metric = Vec4( 0.2126f, 0.7152f, 0.0722f, 0.0f );
  38. else
  39. m_metric = VEC4_CONST( 1.0f );
  40. // cache some values
  41. int const count = m_colours->GetCount();
  42. Vec3 const* values = m_colours->GetPoints();
  43. // get the covariance matrix
  44. Sym3x3 covariance = ComputeWeightedCovariance( count, values, m_colours->GetWeights() );
  45. // compute the principle component
  46. m_principle = ComputePrincipleComponent( covariance );
  47. }
  48. bool ClusterFit::ConstructOrdering( Vec3 const& axis, int iteration )
  49. {
  50. // cache some values
  51. int const count = m_colours->GetCount();
  52. Vec3 const* values = m_colours->GetPoints();
  53. // build the list of dot products
  54. float dps[16];
  55. u8* order = ( u8* )m_order + 16*iteration;
  56. for( int i = 0; i < count; ++i )
  57. {
  58. dps[i] = Dot( values[i], axis );
  59. order[i] = ( u8 )i;
  60. }
  61. // stable sort using them
  62. for( int i = 0; i < count; ++i )
  63. {
  64. for( int j = i; j > 0 && dps[j] < dps[j - 1]; --j )
  65. {
  66. std::swap( dps[j], dps[j - 1] );
  67. std::swap( order[j], order[j - 1] );
  68. }
  69. }
  70. // check this ordering is unique
  71. for( int it = 0; it < iteration; ++it )
  72. {
  73. u8 const* prev = ( u8* )m_order + 16*it;
  74. bool same = true;
  75. for( int i = 0; i < count; ++i )
  76. {
  77. if( order[i] != prev[i] )
  78. {
  79. same = false;
  80. break;
  81. }
  82. }
  83. if( same )
  84. return false;
  85. }
  86. // copy the ordering and weight all the points
  87. Vec3 const* unweighted = m_colours->GetPoints();
  88. float const* weights = m_colours->GetWeights();
  89. m_xsum_wsum = VEC4_CONST( 0.0f );
  90. for( int i = 0; i < count; ++i )
  91. {
  92. int j = order[i];
  93. Vec4 p( unweighted[j].X(), unweighted[j].Y(), unweighted[j].Z(), 1.0f );
  94. Vec4 w( weights[j] );
  95. Vec4 x = p*w;
  96. m_points_weights[i] = x;
  97. m_xsum_wsum += x;
  98. }
  99. return true;
  100. }
  101. void ClusterFit::Compress3( void* block )
  102. {
  103. // declare variables
  104. int const count = m_colours->GetCount();
  105. Vec4 const two = VEC4_CONST( 2.0 );
  106. Vec4 const one = VEC4_CONST( 1.0f );
  107. Vec4 const half_half2( 0.5f, 0.5f, 0.5f, 0.25f );
  108. Vec4 const zero = VEC4_CONST( 0.0f );
  109. Vec4 const half = VEC4_CONST( 0.5f );
  110. Vec4 const grid( 31.0f, 63.0f, 31.0f, 0.0f );
  111. Vec4 const gridrcp( 1.0f/31.0f, 1.0f/63.0f, 1.0f/31.0f, 0.0f );
  112. // prepare an ordering using the principle axis
  113. ConstructOrdering( m_principle, 0 );
  114. // check all possible clusters and iterate on the total order
  115. Vec4 beststart = VEC4_CONST( 0.0f );
  116. Vec4 bestend = VEC4_CONST( 0.0f );
  117. Vec4 besterror = m_besterror;
  118. u8 bestindices[16];
  119. int bestiteration = 0;
  120. int besti = 0, bestj = 0;
  121. // loop over iterations (we avoid the case that all points in first or last cluster)
  122. for( int iterationIndex = 0;; )
  123. {
  124. // first cluster [0,i) is at the start
  125. Vec4 part0 = VEC4_CONST( 0.0f );
  126. for( int i = 0; i < count; ++i )
  127. {
  128. // second cluster [i,j) is half along
  129. Vec4 part1 = ( i == 0 ) ? m_points_weights[0] : VEC4_CONST( 0.0f );
  130. int jmin = ( i == 0 ) ? 1 : i;
  131. for( int j = jmin;; )
  132. {
  133. // last cluster [j,count) is at the end
  134. Vec4 part2 = m_xsum_wsum - part1 - part0;
  135. // compute least squares terms directly
  136. Vec4 alphax_sum = MultiplyAdd( part1, half_half2, part0 );
  137. Vec4 alpha2_sum = alphax_sum.SplatW();
  138. Vec4 betax_sum = MultiplyAdd( part1, half_half2, part2 );
  139. Vec4 beta2_sum = betax_sum.SplatW();
  140. Vec4 alphabeta_sum = ( part1*half_half2 ).SplatW();
  141. // compute the least-squares optimal points
  142. Vec4 factor = Reciprocal( NegativeMultiplySubtract( alphabeta_sum, alphabeta_sum, alpha2_sum*beta2_sum ) );
  143. Vec4 a = NegativeMultiplySubtract( betax_sum, alphabeta_sum, alphax_sum*beta2_sum )*factor;
  144. Vec4 b = NegativeMultiplySubtract( alphax_sum, alphabeta_sum, betax_sum*alpha2_sum )*factor;
  145. // clamp to the grid
  146. a = Min( one, Max( zero, a ) );
  147. b = Min( one, Max( zero, b ) );
  148. a = Truncate( MultiplyAdd( grid, a, half ) )*gridrcp;
  149. b = Truncate( MultiplyAdd( grid, b, half ) )*gridrcp;
  150. // compute the error (we skip the constant xxsum)
  151. Vec4 e1 = MultiplyAdd( a*a, alpha2_sum, b*b*beta2_sum );
  152. Vec4 e2 = NegativeMultiplySubtract( a, alphax_sum, a*b*alphabeta_sum );
  153. Vec4 e3 = NegativeMultiplySubtract( b, betax_sum, e2 );
  154. Vec4 e4 = MultiplyAdd( two, e3, e1 );
  155. // apply the metric to the error term
  156. Vec4 e5 = e4*m_metric;
  157. Vec4 error = e5.SplatX() + e5.SplatY() + e5.SplatZ();
  158. // keep the solution if it wins
  159. if( CompareAnyLessThan( error, besterror ) )
  160. {
  161. beststart = a;
  162. bestend = b;
  163. besti = i;
  164. bestj = j;
  165. besterror = error;
  166. bestiteration = iterationIndex;
  167. }
  168. // advance
  169. if( j == count )
  170. break;
  171. part1 += m_points_weights[j];
  172. ++j;
  173. }
  174. // advance
  175. part0 += m_points_weights[i];
  176. }
  177. // stop if we didn't improve in this iteration
  178. if( bestiteration != iterationIndex )
  179. break;
  180. // advance if possible
  181. ++iterationIndex;
  182. if( iterationIndex == m_iterationCount )
  183. break;
  184. // stop if a new iteration is an ordering that has already been tried
  185. Vec3 axis = ( bestend - beststart ).GetVec3();
  186. if( !ConstructOrdering( axis, iterationIndex ) )
  187. break;
  188. }
  189. // save the block if necessary
  190. if( CompareAnyLessThan( besterror, m_besterror ) )
  191. {
  192. // remap the indices
  193. u8 const* order = ( u8* )m_order + 16*bestiteration;
  194. u8 unordered[16];
  195. for( int m = 0; m < besti; ++m )
  196. unordered[order[m]] = 0;
  197. for( int m = besti; m < bestj; ++m )
  198. unordered[order[m]] = 2;
  199. for( int m = bestj; m < count; ++m )
  200. unordered[order[m]] = 1;
  201. m_colours->RemapIndices( unordered, bestindices );
  202. // save the block
  203. WriteColourBlock3( beststart.GetVec3(), bestend.GetVec3(), bestindices, block );
  204. // save the error
  205. m_besterror = besterror;
  206. }
  207. }
  208. void ClusterFit::Compress4( void* block )
  209. {
  210. // declare variables
  211. int const count = m_colours->GetCount();
  212. Vec4 const two = VEC4_CONST( 2.0f );
  213. Vec4 const one = VEC4_CONST( 1.0f );
  214. Vec4 const onethird_onethird2( 1.0f/3.0f, 1.0f/3.0f, 1.0f/3.0f, 1.0f/9.0f );
  215. Vec4 const twothirds_twothirds2( 2.0f/3.0f, 2.0f/3.0f, 2.0f/3.0f, 4.0f/9.0f );
  216. Vec4 const twonineths = VEC4_CONST( 2.0f/9.0f );
  217. Vec4 const zero = VEC4_CONST( 0.0f );
  218. Vec4 const half = VEC4_CONST( 0.5f );
  219. Vec4 const grid( 31.0f, 63.0f, 31.0f, 0.0f );
  220. Vec4 const gridrcp( 1.0f/31.0f, 1.0f/63.0f, 1.0f/31.0f, 0.0f );
  221. // prepare an ordering using the principle axis
  222. ConstructOrdering( m_principle, 0 );
  223. // check all possible clusters and iterate on the total order
  224. Vec4 beststart = VEC4_CONST( 0.0f );
  225. Vec4 bestend = VEC4_CONST( 0.0f );
  226. Vec4 besterror = m_besterror;
  227. u8 bestindices[16];
  228. int bestiteration = 0;
  229. int besti = 0, bestj = 0, bestk = 0;
  230. // loop over iterations (we avoid the case that all points in first or last cluster)
  231. for( int iterationIndex = 0;; )
  232. {
  233. // first cluster [0,i) is at the start
  234. Vec4 part0 = VEC4_CONST( 0.0f );
  235. for( int i = 0; i < count; ++i )
  236. {
  237. // second cluster [i,j) is one third along
  238. Vec4 part1 = VEC4_CONST( 0.0f );
  239. for( int j = i;; )
  240. {
  241. // third cluster [j,k) is two thirds along
  242. Vec4 part2 = ( j == 0 ) ? m_points_weights[0] : VEC4_CONST( 0.0f );
  243. int kmin = ( j == 0 ) ? 1 : j;
  244. for( int k = kmin;; )
  245. {
  246. // last cluster [k,count) is at the end
  247. Vec4 part3 = m_xsum_wsum - part2 - part1 - part0;
  248. // compute least squares terms directly
  249. Vec4 const alphax_sum = MultiplyAdd( part2, onethird_onethird2, MultiplyAdd( part1, twothirds_twothirds2, part0 ) );
  250. Vec4 const alpha2_sum = alphax_sum.SplatW();
  251. Vec4 const betax_sum = MultiplyAdd( part1, onethird_onethird2, MultiplyAdd( part2, twothirds_twothirds2, part3 ) );
  252. Vec4 const beta2_sum = betax_sum.SplatW();
  253. Vec4 const alphabeta_sum = twonineths*( part1 + part2 ).SplatW();
  254. // compute the least-squares optimal points
  255. Vec4 factor = Reciprocal( NegativeMultiplySubtract( alphabeta_sum, alphabeta_sum, alpha2_sum*beta2_sum ) );
  256. Vec4 a = NegativeMultiplySubtract( betax_sum, alphabeta_sum, alphax_sum*beta2_sum )*factor;
  257. Vec4 b = NegativeMultiplySubtract( alphax_sum, alphabeta_sum, betax_sum*alpha2_sum )*factor;
  258. // clamp to the grid
  259. a = Min( one, Max( zero, a ) );
  260. b = Min( one, Max( zero, b ) );
  261. a = Truncate( MultiplyAdd( grid, a, half ) )*gridrcp;
  262. b = Truncate( MultiplyAdd( grid, b, half ) )*gridrcp;
  263. // compute the error (we skip the constant xxsum)
  264. Vec4 e1 = MultiplyAdd( a*a, alpha2_sum, b*b*beta2_sum );
  265. Vec4 e2 = NegativeMultiplySubtract( a, alphax_sum, a*b*alphabeta_sum );
  266. Vec4 e3 = NegativeMultiplySubtract( b, betax_sum, e2 );
  267. Vec4 e4 = MultiplyAdd( two, e3, e1 );
  268. // apply the metric to the error term
  269. Vec4 e5 = e4*m_metric;
  270. Vec4 error = e5.SplatX() + e5.SplatY() + e5.SplatZ();
  271. // keep the solution if it wins
  272. if( CompareAnyLessThan( error, besterror ) )
  273. {
  274. beststart = a;
  275. bestend = b;
  276. besterror = error;
  277. besti = i;
  278. bestj = j;
  279. bestk = k;
  280. bestiteration = iterationIndex;
  281. }
  282. // advance
  283. if( k == count )
  284. break;
  285. part2 += m_points_weights[k];
  286. ++k;
  287. }
  288. // advance
  289. if( j == count )
  290. break;
  291. part1 += m_points_weights[j];
  292. ++j;
  293. }
  294. // advance
  295. part0 += m_points_weights[i];
  296. }
  297. // stop if we didn't improve in this iteration
  298. if( bestiteration != iterationIndex )
  299. break;
  300. // advance if possible
  301. ++iterationIndex;
  302. if( iterationIndex == m_iterationCount )
  303. break;
  304. // stop if a new iteration is an ordering that has already been tried
  305. Vec3 axis = ( bestend - beststart ).GetVec3();
  306. if( !ConstructOrdering( axis, iterationIndex ) )
  307. break;
  308. }
  309. // save the block if necessary
  310. if( CompareAnyLessThan( besterror, m_besterror ) )
  311. {
  312. // remap the indices
  313. u8 const* order = ( u8* )m_order + 16*bestiteration;
  314. u8 unordered[16];
  315. for( int m = 0; m < besti; ++m )
  316. unordered[order[m]] = 0;
  317. for( int m = besti; m < bestj; ++m )
  318. unordered[order[m]] = 2;
  319. for( int m = bestj; m < bestk; ++m )
  320. unordered[order[m]] = 3;
  321. for( int m = bestk; m < count; ++m )
  322. unordered[order[m]] = 1;
  323. m_colours->RemapIndices( unordered, bestindices );
  324. // save the block
  325. WriteColourBlock4( beststart.GetVec3(), bestend.GetVec3(), bestindices, block );
  326. // save the error
  327. m_besterror = besterror;
  328. }
  329. }
  330. } // namespace squish