clusterfit.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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 <cfloat>
  25. namespace squish {
  26. ClusterFit::ClusterFit( ColourSet const* colours, int flags )
  27. : ColourFit( colours, flags )
  28. {
  29. // set the iteration count
  30. m_iterationCount = ( m_flags & kColourIterativeClusterFit ) ? kMaxIterations : 1;
  31. // initialise the best error
  32. m_besterror = VEC4_CONST( FLT_MAX );
  33. // initialise the metric
  34. bool perceptual = ( ( m_flags & kColourMetricPerceptual ) != 0 );
  35. if( perceptual )
  36. m_metric = Vec4( 0.2126f, 0.7152f, 0.0722f, 0.0f );
  37. else
  38. m_metric = VEC4_CONST( 1.0f );
  39. // cache some values
  40. int const count = m_colours->GetCount();
  41. Vec3 const* values = m_colours->GetPoints();
  42. // get the covariance matrix
  43. Sym3x3 covariance = ComputeWeightedCovariance( count, values, m_colours->GetWeights() );
  44. // compute the principle component
  45. m_principle = ComputePrincipleComponent( covariance );
  46. }
  47. bool ClusterFit::ConstructOrdering( Vec3 const& axis, int iteration )
  48. {
  49. // cache some values
  50. int const count = m_colours->GetCount();
  51. Vec3 const* values = m_colours->GetPoints();
  52. // build the list of dot products
  53. float dps[16];
  54. u8* order = ( u8* )m_order + 16*iteration;
  55. for( int i = 0; i < count; ++i )
  56. {
  57. dps[i] = Dot( values[i], axis );
  58. order[i] = ( u8 )i;
  59. }
  60. // stable sort using them
  61. for( int i = 0; i < count; ++i )
  62. {
  63. for( int j = i; j > 0 && dps[j] < dps[j - 1]; --j )
  64. {
  65. std::swap( dps[j], dps[j - 1] );
  66. std::swap( order[j], order[j - 1] );
  67. }
  68. }
  69. // check this ordering is unique
  70. for( int it = 0; it < iteration; ++it )
  71. {
  72. u8 const* prev = ( u8* )m_order + 16*it;
  73. bool same = true;
  74. for( int i = 0; i < count; ++i )
  75. {
  76. if( order[i] != prev[i] )
  77. {
  78. same = false;
  79. break;
  80. }
  81. }
  82. if( same )
  83. return false;
  84. }
  85. // copy the ordering and weight all the points
  86. Vec3 const* unweighted = m_colours->GetPoints();
  87. float const* weights = m_colours->GetWeights();
  88. m_xsum_wsum = VEC4_CONST( 0.0f );
  89. for( int i = 0; i < count; ++i )
  90. {
  91. int j = order[i];
  92. Vec4 p( unweighted[j].X(), unweighted[j].Y(), unweighted[j].Z(), 1.0f );
  93. Vec4 w( weights[j] );
  94. Vec4 x = p*w;
  95. m_points_weights[i] = x;
  96. m_xsum_wsum += x;
  97. }
  98. return true;
  99. }
  100. void ClusterFit::Compress3( void* block )
  101. {
  102. // declare variables
  103. int const count = m_colours->GetCount();
  104. Vec4 const two = VEC4_CONST( 2.0 );
  105. Vec4 const one = VEC4_CONST( 1.0f );
  106. Vec4 const half_half2( 0.5f, 0.5f, 0.5f, 0.25f );
  107. Vec4 const zero = VEC4_CONST( 0.0f );
  108. Vec4 const half = VEC4_CONST( 0.5f );
  109. Vec4 const grid( 31.0f, 63.0f, 31.0f, 0.0f );
  110. Vec4 const gridrcp( 1.0f/31.0f, 1.0f/63.0f, 1.0f/31.0f, 0.0f );
  111. // prepare an ordering using the principle axis
  112. ConstructOrdering( m_principle, 0 );
  113. // check all possible clusters and iterate on the total order
  114. Vec4 beststart = VEC4_CONST( 0.0f );
  115. Vec4 bestend = VEC4_CONST( 0.0f );
  116. Vec4 besterror = m_besterror;
  117. u8 bestindices[16];
  118. int bestiteration = 0;
  119. int besti = 0, bestj = 0;
  120. // loop over iterations (we avoid the case that all points in first or last cluster)
  121. for( int iterationIndex = 0;; )
  122. {
  123. // first cluster [0,i) is at the start
  124. Vec4 part0 = VEC4_CONST( 0.0f );
  125. for( int i = 0; i < count; ++i )
  126. {
  127. // second cluster [i,j) is half along
  128. Vec4 part1 = ( i == 0 ) ? m_points_weights[0] : VEC4_CONST( 0.0f );
  129. int jmin = ( i == 0 ) ? 1 : i;
  130. for( int j = jmin;; )
  131. {
  132. // last cluster [j,count) is at the end
  133. Vec4 part2 = m_xsum_wsum - part1 - part0;
  134. // compute least squares terms directly
  135. Vec4 alphax_sum = MultiplyAdd( part1, half_half2, part0 );
  136. Vec4 alpha2_sum = alphax_sum.SplatW();
  137. Vec4 betax_sum = MultiplyAdd( part1, half_half2, part2 );
  138. Vec4 beta2_sum = betax_sum.SplatW();
  139. Vec4 alphabeta_sum = ( part1*half_half2 ).SplatW();
  140. // compute the least-squares optimal points
  141. Vec4 factor = Reciprocal( NegativeMultiplySubtract( alphabeta_sum, alphabeta_sum, alpha2_sum*beta2_sum ) );
  142. Vec4 a = NegativeMultiplySubtract( betax_sum, alphabeta_sum, alphax_sum*beta2_sum )*factor;
  143. Vec4 b = NegativeMultiplySubtract( alphax_sum, alphabeta_sum, betax_sum*alpha2_sum )*factor;
  144. // clamp to the grid
  145. a = Min( one, Max( zero, a ) );
  146. b = Min( one, Max( zero, b ) );
  147. a = Truncate( MultiplyAdd( grid, a, half ) )*gridrcp;
  148. b = Truncate( MultiplyAdd( grid, b, half ) )*gridrcp;
  149. // compute the error (we skip the constant xxsum)
  150. Vec4 e1 = MultiplyAdd( a*a, alpha2_sum, b*b*beta2_sum );
  151. Vec4 e2 = NegativeMultiplySubtract( a, alphax_sum, a*b*alphabeta_sum );
  152. Vec4 e3 = NegativeMultiplySubtract( b, betax_sum, e2 );
  153. Vec4 e4 = MultiplyAdd( two, e3, e1 );
  154. // apply the metric to the error term
  155. Vec4 e5 = e4*m_metric;
  156. Vec4 error = e5.SplatX() + e5.SplatY() + e5.SplatZ();
  157. // keep the solution if it wins
  158. if( CompareAnyLessThan( error, besterror ) )
  159. {
  160. beststart = a;
  161. bestend = b;
  162. besti = i;
  163. bestj = j;
  164. besterror = error;
  165. bestiteration = iterationIndex;
  166. }
  167. // advance
  168. if( j == count )
  169. break;
  170. part1 += m_points_weights[j];
  171. ++j;
  172. }
  173. // advance
  174. part0 += m_points_weights[i];
  175. }
  176. // stop if we didn't improve in this iteration
  177. if( bestiteration != iterationIndex )
  178. break;
  179. // advance if possible
  180. ++iterationIndex;
  181. if( iterationIndex == m_iterationCount )
  182. break;
  183. // stop if a new iteration is an ordering that has already been tried
  184. Vec3 axis = ( bestend - beststart ).GetVec3();
  185. if( !ConstructOrdering( axis, iterationIndex ) )
  186. break;
  187. }
  188. // save the block if necessary
  189. if( CompareAnyLessThan( besterror, m_besterror ) )
  190. {
  191. // remap the indices
  192. u8 const* order = ( u8* )m_order + 16*bestiteration;
  193. u8 unordered[16];
  194. for( int m = 0; m < besti; ++m )
  195. unordered[order[m]] = 0;
  196. for( int m = besti; m < bestj; ++m )
  197. unordered[order[m]] = 2;
  198. for( int m = bestj; m < count; ++m )
  199. unordered[order[m]] = 1;
  200. m_colours->RemapIndices( unordered, bestindices );
  201. // save the block
  202. WriteColourBlock3( beststart.GetVec3(), bestend.GetVec3(), bestindices, block );
  203. // save the error
  204. m_besterror = besterror;
  205. }
  206. }
  207. void ClusterFit::Compress4( void* block )
  208. {
  209. // declare variables
  210. int const count = m_colours->GetCount();
  211. Vec4 const two = VEC4_CONST( 2.0f );
  212. Vec4 const one = VEC4_CONST( 1.0f );
  213. Vec4 const onethird_onethird2( 1.0f/3.0f, 1.0f/3.0f, 1.0f/3.0f, 1.0f/9.0f );
  214. Vec4 const twothirds_twothirds2( 2.0f/3.0f, 2.0f/3.0f, 2.0f/3.0f, 4.0f/9.0f );
  215. Vec4 const twonineths = VEC4_CONST( 2.0f/9.0f );
  216. Vec4 const zero = VEC4_CONST( 0.0f );
  217. Vec4 const half = VEC4_CONST( 0.5f );
  218. Vec4 const grid( 31.0f, 63.0f, 31.0f, 0.0f );
  219. Vec4 const gridrcp( 1.0f/31.0f, 1.0f/63.0f, 1.0f/31.0f, 0.0f );
  220. // prepare an ordering using the principle axis
  221. ConstructOrdering( m_principle, 0 );
  222. // check all possible clusters and iterate on the total order
  223. Vec4 beststart = VEC4_CONST( 0.0f );
  224. Vec4 bestend = VEC4_CONST( 0.0f );
  225. Vec4 besterror = m_besterror;
  226. u8 bestindices[16];
  227. int bestiteration = 0;
  228. int besti = 0, bestj = 0, bestk = 0;
  229. // loop over iterations (we avoid the case that all points in first or last cluster)
  230. for( int iterationIndex = 0;; )
  231. {
  232. // first cluster [0,i) is at the start
  233. Vec4 part0 = VEC4_CONST( 0.0f );
  234. for( int i = 0; i < count; ++i )
  235. {
  236. // second cluster [i,j) is one third along
  237. Vec4 part1 = VEC4_CONST( 0.0f );
  238. for( int j = i;; )
  239. {
  240. // third cluster [j,k) is two thirds along
  241. Vec4 part2 = ( j == 0 ) ? m_points_weights[0] : VEC4_CONST( 0.0f );
  242. int kmin = ( j == 0 ) ? 1 : j;
  243. for( int k = kmin;; )
  244. {
  245. // last cluster [k,count) is at the end
  246. Vec4 part3 = m_xsum_wsum - part2 - part1 - part0;
  247. // compute least squares terms directly
  248. Vec4 const alphax_sum = MultiplyAdd( part2, onethird_onethird2, MultiplyAdd( part1, twothirds_twothirds2, part0 ) );
  249. Vec4 const alpha2_sum = alphax_sum.SplatW();
  250. Vec4 const betax_sum = MultiplyAdd( part1, onethird_onethird2, MultiplyAdd( part2, twothirds_twothirds2, part3 ) );
  251. Vec4 const beta2_sum = betax_sum.SplatW();
  252. Vec4 const alphabeta_sum = twonineths*( part1 + part2 ).SplatW();
  253. // compute the least-squares optimal points
  254. Vec4 factor = Reciprocal( NegativeMultiplySubtract( alphabeta_sum, alphabeta_sum, alpha2_sum*beta2_sum ) );
  255. Vec4 a = NegativeMultiplySubtract( betax_sum, alphabeta_sum, alphax_sum*beta2_sum )*factor;
  256. Vec4 b = NegativeMultiplySubtract( alphax_sum, alphabeta_sum, betax_sum*alpha2_sum )*factor;
  257. // clamp to the grid
  258. a = Min( one, Max( zero, a ) );
  259. b = Min( one, Max( zero, b ) );
  260. a = Truncate( MultiplyAdd( grid, a, half ) )*gridrcp;
  261. b = Truncate( MultiplyAdd( grid, b, half ) )*gridrcp;
  262. // compute the error (we skip the constant xxsum)
  263. Vec4 e1 = MultiplyAdd( a*a, alpha2_sum, b*b*beta2_sum );
  264. Vec4 e2 = NegativeMultiplySubtract( a, alphax_sum, a*b*alphabeta_sum );
  265. Vec4 e3 = NegativeMultiplySubtract( b, betax_sum, e2 );
  266. Vec4 e4 = MultiplyAdd( two, e3, e1 );
  267. // apply the metric to the error term
  268. Vec4 e5 = e4*m_metric;
  269. Vec4 error = e5.SplatX() + e5.SplatY() + e5.SplatZ();
  270. // keep the solution if it wins
  271. if( CompareAnyLessThan( error, besterror ) )
  272. {
  273. beststart = a;
  274. bestend = b;
  275. besterror = error;
  276. besti = i;
  277. bestj = j;
  278. bestk = k;
  279. bestiteration = iterationIndex;
  280. }
  281. // advance
  282. if( k == count )
  283. break;
  284. part2 += m_points_weights[k];
  285. ++k;
  286. }
  287. // advance
  288. if( j == count )
  289. break;
  290. part1 += m_points_weights[j];
  291. ++j;
  292. }
  293. // advance
  294. part0 += m_points_weights[i];
  295. }
  296. // stop if we didn't improve in this iteration
  297. if( bestiteration != iterationIndex )
  298. break;
  299. // advance if possible
  300. ++iterationIndex;
  301. if( iterationIndex == m_iterationCount )
  302. break;
  303. // stop if a new iteration is an ordering that has already been tried
  304. Vec3 axis = ( bestend - beststart ).GetVec3();
  305. if( !ConstructOrdering( axis, iterationIndex ) )
  306. break;
  307. }
  308. // save the block if necessary
  309. if( CompareAnyLessThan( besterror, m_besterror ) )
  310. {
  311. // remap the indices
  312. u8 const* order = ( u8* )m_order + 16*bestiteration;
  313. u8 unordered[16];
  314. for( int m = 0; m < besti; ++m )
  315. unordered[order[m]] = 0;
  316. for( int m = besti; m < bestj; ++m )
  317. unordered[order[m]] = 2;
  318. for( int m = bestj; m < bestk; ++m )
  319. unordered[order[m]] = 3;
  320. for( int m = bestk; m < count; ++m )
  321. unordered[order[m]] = 1;
  322. m_colours->RemapIndices( unordered, bestindices );
  323. // save the block
  324. WriteColourBlock4( beststart.GetVec3(), bestend.GetVec3(), bestindices, block );
  325. // save the error
  326. m_besterror = besterror;
  327. }
  328. }
  329. } // namespace squish