lullaby/tests/nine_patch_test.cc
// This test subdivides the nine patch and positions the slices such that the
// extra subdivisions are distributed in (and interpolated across) the middle
// horizontally stretched region of the nine patch.
This test subdivides the nine patch and positions the slices such that the
extra subdivisions are distributed in (and interpolated across) the middle
horizontally stretched region of the nine patch.
This test subdivides the nine patch and positions the slices such that the extra subdivisions are distributed in ( and interpolated across ) the middle horizontally stretched region of the nine patch .
TEST(NinePatch, CheckMiddlePatchSubdivision) {
  NinePatch nine_patch;
  nine_patch.size = mathfu::vec2(2, 2);
  nine_patch.original_size = mathfu::vec2(1, 1);
  nine_patch.left_slice = .2f;
  nine_patch.right_slice = .2f;
  nine_patch.bottom_slice = .2f;
  nine_patch.top_slice = .2f;
  nine_patch.subdivisions = mathfu::vec2i(3, 3);
  float middle_vertex_u[2];
  middle_vertex_u[0] = ComputeMiddlePatchU(nine_patch, 2.f / 3.f);
  middle_vertex_u[1] = ComputeMiddlePatchU(nine_patch, 4.f / 3.f);
  float middle_vertex_u1[2];
  middle_vertex_u1[0] = 1.f / 3.f;
  middle_vertex_u1[1] = 2.f / 3.f;
  EXPECT_THAT(nine_patch.GetVertexCount(), Eq((3 + 2 + 1) * (3 + 2 + 1)));
  EXPECT_THAT(nine_patch.GetIndexCount(), Eq(5 * 5 * 2 * 3));
  std::vector<lull::VertexPTT> nine_patch_vertices(nine_patch.GetVertexCount());
  std::vector<uint16_t> nine_patch_indices(nine_patch.GetIndexCount());
  MeshData mesh = BuildMeshFromNinePatchVerticesAndIndices(&nine_patch_vertices,
                                                           &nine_patch_indices);
  GenerateNinePatchMesh(nine_patch, &mesh);
  constexpr float expected_positions[] = {
      -1.f,    -1.f, 0, -.8f, -1.f, 0, -.33333f, -1.f, 0,
      .33333f, -1.f, 0, .8f,  -1.f, 0, 1.f,      -1.f, 0,
  };
  const float expected_uvs[] = {
      0, 1, .2f, 1, middle_vertex_u[0], 1, middle_vertex_u[1], 1, .8f, 1, 1, 1,
  };
  const float expected_uv1s[] = {
      0,   1, .1f, 1, middle_vertex_u1[0], 1, middle_vertex_u1[1], 1,
      .9f, 1, 1,   1,
  };
  constexpr int kVerticesInRow = 6;
  AssertCorrectTranslations(nine_patch, nine_patch_vertices, expected_positions,
                            expected_uvs, expected_uv1s, kVerticesInRow);
}
TEST ( NinePatch , CheckMiddlePatchSubdivision ) { NinePatch nine_patch ; nine_patch . size = mathfu :: vec2 ( 2 , 2 ) ; nine_patch . original_size = mathfu :: vec2 ( 1 , 1 ) ; nine_patch . left_slice = .2f ; nine_patch . right_slice = .2f ; nine_patch . bottom_slice = .2f ; nine_patch . top_slice = .2f ; nine_patch . subdivisions = mathfu :: vec2i ( 3 , 3 ) ; float middle_vertex_u [ 2 ] ; middle_vertex_u [ 0 ] = ComputeMiddlePatchU ( nine_patch , 2.f / 3.f ) ; middle_vertex_u [ 1 ] = ComputeMiddlePatchU ( nine_patch , 4.f / 3.f ) ; float middle_vertex_u1 [ 2 ] ; middle_vertex_u1 [ 0 ] = 1.f / 3.f ; middle_vertex_u1 [ 1 ] = 2.f / 3.f ; EXPECT_THAT ( nine_patch . GetVertexCount ( ) , Eq ( ( 3 + 2 + 1 ) * ( 3 + 2 + 1 ) ) ) ; EXPECT_THAT ( nine_patch . GetIndexCount ( ) , Eq ( 5 * 5 * 2 * 3 ) ) ; std :: vector < lull :: VertexPTT > nine_patch_vertices ( nine_patch . GetVertexCount ( ) ) ; std :: vector < uint16_t > nine_patch_indices ( nine_patch . GetIndexCount ( ) ) ; MeshData mesh = BuildMeshFromNinePatchVerticesAndIndices ( & nine_patch_vertices , & nine_patch_indices ) ; GenerateNinePatchMesh ( nine_patch , & mesh ) ; constexpr float expected_positions [ ] = { -1.f , -1.f , 0 , -.8f , -1.f , 0 , -.33333f , -1.f , 0 , .33333f , -1.f , 0 , .8f , -1.f , 0 , 1.f , -1.f , 0 , } ; const float expected_uvs [ ] = { 0 , 1 , .2f , 1 , middle_vertex_u [ 0 ] , 1 , middle_vertex_u [ 1 ] , 1 , .8f , 1 , 1 , 1 , } ; const float expected_uv1s [ ] = { 0 , 1 , .1f , 1 , middle_vertex_u1 [ 0 ] , 1 , middle_vertex_u1 [ 1 ] , 1 , .9f , 1 , 1 , 1 , } ; constexpr int kVerticesInRow = 6 ; AssertCorrectTranslations ( nine_patch , nine_patch_vertices , expected_positions , expected_uvs , expected_uv1s , kVerticesInRow ) ; }
This test subdivides the nine patch and positions the slices such that the
extra subdivisions are distributed in (and interpolated across) the middle
horizontally stretched region of the nine patch.
This test subdivides the nine patch and positions the slices such that the extra subdivisions are distributed in ( and interpolated across ) the middle horizontally stretched region of the nine patch .
// Ascii art of a row cutting through the middle of this nine-patch in 1-D: // -  | means vertex due to subdivision. // -  { or } means vertex due to 9-patch slices. // -  total width is 2, with 3 evenly spaced subdivisions and slices .2 from //    the sides. // |--{-----|--------|-----}--| // 0  .2    2/3     4/3   1.8 2  <-- x from 0, actual will be centered. // UVs in the interior are determined by the proportion of UV within // the middle patch. // UV1s are scaled evenly [0,1] in texture space. // 2 extra divisions for the slices plus an extra row and column of vertices // to make complete quads on the ends means 3 + 2 + 1 on each side. // five by five quads, 2 triangles per quad, 3 indices per triangle // Just check the top row.