[hvgl] Implement transformations

I'm sure it has bugs but seems to work for non-rotation test
I've performed at least.
This commit is contained in:
Behdad Esfahbod committed 2025-03-02 16:57:54 -07:00
1 parent b4f26357d9
commit f5461c3b79
3 files changed
+171 -31

No files matched your search

+125 -5
View File
@@ -179,6 +179,127 @@ PartShape::get_path_at (const struct hvgl &hvgl,
}
}
void
PartComposite::apply_transforms (hb_array_t<hb_transform_t> transforms,
hb_array_t<const float> coords) const
{
const auto &allTranslations = StructAtOffset<AllTranslations> (this, allTranslationsOff4 * 4);
const auto &allRotations = StructAtOffset<AllRotations> (this, allRotationsOff4 * 4);
const auto &masterTranslationDelta = allTranslations.masterTranslationDelta;
const auto &extremumTranslationDelta = StructAfter<decltype (allTranslations.extremumTranslationDeltaX)> (masterTranslationDelta, sparseMasterTranslationCount);
const auto &extremumTranslationIndex = StructAfter<decltype (allTranslations.extremumTranslationIndexX)> (extremumTranslationDelta, sparseExtremumTranslationCount);
const auto &masterTranslationIndex = StructAfter<decltype (allTranslations.masterTranslationIndexX)> (extremumTranslationIndex, sparseExtremumTranslationCount);
const auto &masterRotationDelta = allRotations.masterRotationDelta;
const auto &extremumRotationDelta = StructAfter<decltype (allRotations.extremumRotationDeltaX)> (masterRotationDelta, sparseMasterRotationCount);
const auto &extremumRotationIndex = StructAfter<decltype (allRotations.extremumRotationIndexX)> (extremumRotationDelta, sparseExtremumRotationCount);
const auto &masterRotationIndex = StructAfter<decltype (allRotations.masterRotationIndexX)> (extremumRotationIndex, sparseExtremumRotationCount);
auto master_translation_deltas = masterTranslationDelta.as_array (sparseMasterTranslationCount);
auto master_translation_indices = masterTranslationIndex.as_array (sparseMasterTranslationCount);
auto extremum_translation_deltas = extremumTranslationDelta.as_array (sparseExtremumTranslationCount);
auto extremum_translation_indices = extremumTranslationIndex.as_array (sparseExtremumTranslationCount);
auto master_rotation_deltas = masterRotationDelta.as_array (sparseMasterRotationCount);
auto master_rotation_indices = masterRotationIndex.as_array (sparseMasterRotationCount);
auto extremum_rotation_deltas = extremumRotationDelta.as_array (sparseExtremumRotationCount);
auto extremum_rotation_indices = extremumRotationIndex.as_array (sparseExtremumRotationCount);
for (unsigned row = 0; row < transforms.length; row++)
{
hb_transform_t transform;
auto master_rotation_delta = Null(HBFLOAT32LE);
if (row == master_rotation_indices[0])
{
master_rotation_delta = master_rotation_deltas[0];
master_rotation_deltas++;
master_rotation_indices++;
}
auto master_translation_delta = Null(TranslationDelta);
if (row == master_translation_indices[0])
{
master_translation_delta = master_translation_deltas[0];
master_translation_deltas++;
master_translation_indices++;
}
transform.rotate (master_rotation_delta);
transform.translate (master_translation_delta.x, master_translation_delta.y);
unsigned translation_index_end = 0;
while (translation_index_end < extremum_translation_indices.length &&
row == extremum_translation_indices[translation_index_end].row)
translation_index_end++;
unsigned rotation_index_end = 0;
while (rotation_index_end < extremum_rotation_indices.length &&
row == extremum_rotation_indices[rotation_index_end].row)
rotation_index_end++;
unsigned translation_index = 0;
unsigned rotation_index = 0;
while (translation_index < translation_index_end ||
rotation_index < rotation_index_end)
{
auto extremum_translation_delta = Null(TranslationDelta);
auto extremum_rotation_delta = Null(HBFLOAT32LE);
unsigned column;
if (translation_index < translation_index_end &&
(rotation_index == rotation_index_end ||
extremum_translation_indices[translation_index].column < extremum_rotation_indices[rotation_index].column))
column = extremum_translation_indices[translation_index].column;
else
column = extremum_rotation_indices[rotation_index].column;
if (translation_index < translation_index_end &&
extremum_translation_indices[translation_index].column == column)
{
extremum_translation_delta = extremum_translation_deltas[translation_index];
translation_index++;
}
if (rotation_index < rotation_index_end &&
extremum_rotation_indices[rotation_index].column == column)
{
extremum_rotation_delta = extremum_rotation_deltas[rotation_index];
rotation_index++;
}
bool pos = column & 1;
unsigned axis_idx = column / 2;
float coord = coords[axis_idx];
if (!coord || (pos != (coord > 0)))
continue;
float scalar = fabsf (coord);
hb_transform_t extremum_transform;
float eigen_x, eigen_y;
if (transform.get_eigen_vector (eigen_x, eigen_y))
{
// Scale rotation around eigen vector
extremum_transform.translate (-eigen_x, -eigen_y);
extremum_transform.rotate (extremum_rotation_delta * scalar);
extremum_transform.translate (eigen_x, eigen_y);
}
else
{
// No rotation, just scale the translate
extremum_transform.translate (extremum_translation_delta.x * scalar,
extremum_translation_delta.y * scalar);
}
transform.transform (extremum_transform);
}
extremum_translation_deltas += translation_index_end;;
extremum_translation_indices += translation_index_end;
extremum_rotation_deltas += rotation_index_end;
extremum_rotation_indices += rotation_index_end;
transforms[row].transform (transform);
}
}
void
PartComposite::get_path_at (const struct hvgl &hvgl,
@@ -210,14 +331,13 @@ PartComposite::get_path_at (const struct hvgl &hvgl,
auto transforms_head = transforms[0];
auto transforms_tail = transforms.sub_array (1);
#if 0
const auto &allTranslations = StructAtOffset<AllTranslations> (this, allTranslationsOff4 * 4);
const auto &allRotations = StructAtOffset<AllRotations> (this, allRotationsOff4 * 4);
#endif
apply_transforms (transforms_tail, coords);
for (const auto &subPart : subParts.as_array (subPartCount))
{
transforms_tail[subPart.treeTransformIndex].transform (transforms_head);
hb_transform_t total_transform (transforms_head);
total_transform.transform (transforms_tail[subPart.treeTransformIndex]);
transforms_tail[subPart.treeTransformIndex] = total_transform;
hvgl.get_part_path_at (subPart.partIndex,
draw_session, coords_tail.sub_array (subPart.treeAxisIndex),
transforms_tail.sub_array (subPart.treeTransformIndex),
+33 -26
View File
@@ -280,33 +280,35 @@ struct MatrixIndex
struct AllTranslations
{
friend struct PartComposite;
public:
bool sanitize (hb_sanitize_context_t *c,
unsigned sparseMasterTranslationCount,
unsigned sparseExtremumTranslationCount) const
unsigned sparse_master_translation_count,
unsigned sparse_extremum_translation_count) const
{
TRACE_SANITIZE (this);
if (unlikely (!masterTranslationDelta.sanitize (c, sparseMasterTranslationCount))) return_trace (false);
if (unlikely (!masterTranslationDelta.sanitize (c, sparse_master_translation_count))) return_trace (false);
const auto &extremumTranslationDelta = StructAfter<decltype (extremumTranslationDeltaX)> (masterTranslationDelta, sparseMasterTranslationCount);
if (unlikely (!extremumTranslationDelta.sanitize (c, sparseExtremumTranslationCount))) return_trace (false);
const auto &extremumTranslationDelta = StructAfter<decltype (extremumTranslationDeltaX)> (masterTranslationDelta, sparse_master_translation_count);
if (unlikely (!extremumTranslationDelta.sanitize (c, sparse_extremum_translation_count))) return_trace (false);
const auto &extremumTranslationIndex = StructAfter<decltype (extremumTranslationIndexX)> (extremumTranslationDelta, sparseExtremumTranslationCount);
if (unlikely (!extremumTranslationIndex.sanitize (c, sparseExtremumTranslationCount))) return_trace (false);
const auto &extremumTranslationIndex = StructAfter<decltype (extremumTranslationIndexX)> (extremumTranslationDelta, sparse_extremum_translation_count);
if (unlikely (!extremumTranslationIndex.sanitize (c, sparse_extremum_translation_count))) return_trace (false);
const auto &masterTranslationIndex = StructAfter<decltype (masterTranslationIndexX)> (extremumTranslationIndex, sparseExtremumTranslationCount);
if (unlikely (!masterTranslationIndex.sanitize (c, sparseMasterTranslationCount))) return_trace (false);
const auto &masterTranslationIndex = StructAfter<decltype (masterTranslationIndexX)> (extremumTranslationIndex, sparse_extremum_translation_count);
if (unlikely (!masterTranslationIndex.sanitize (c, sparse_master_translation_count))) return_trace (false);
return_trace (true);
}
protected:
UnsizedArrayOf<TranslationDelta> masterTranslationDelta; // length: sparseMasterTranslationCount; Master translation deltas
UnsizedArrayOf<TranslationDelta> extremumTranslationDeltaX; // length: sparseExtremumTranslationCount; Extremum translation deltas
UnsizedArrayOf<MatrixIndex> extremumTranslationIndexX; // length: sparseExtremumTranslationCount; Extremum translation indices
UnsizedArrayOf<HBUINT16LE> masterTranslationIndexX; // length: sparseMasterTranslationCount; Master translation indices
UnsizedArrayOf<TranslationDelta> masterTranslationDelta; // length: sparse_master_translation_count; Master translation deltas
UnsizedArrayOf<TranslationDelta> extremumTranslationDeltaX; // length: sparse_extremum_translation_count; Extremum translation deltas
UnsizedArrayOf<MatrixIndex> extremumTranslationIndexX; // length: sparse_extremum_translation_count; Extremum translation indices
UnsizedArrayOf<HBUINT16LE> masterTranslationIndexX; // length: sparse_master_translation_count; Master translation indices
Align<4> padding; // Pad to float32le alignment
public:
@@ -315,33 +317,35 @@ struct AllTranslations
struct AllRotations
{
friend struct PartComposite;
public:
bool sanitize (hb_sanitize_context_t *c,
unsigned sparseMasterRotationCount,
unsigned sparseExtremumRotationCount) const
unsigned sparse_master_rotation_count,
unsigned sparse_extremum_rotation_count) const
{
TRACE_SANITIZE (this);
if (unlikely (!masterRotationDelta.sanitize (c, sparseMasterRotationCount))) return_trace (false);
if (unlikely (!masterRotationDelta.sanitize (c, sparse_master_rotation_count))) return_trace (false);
const auto &extremumRotationDelta = StructAfter<decltype (extremumRotationDeltaX)> (masterRotationDelta, sparseMasterRotationCount);
if (unlikely (!extremumRotationDelta.sanitize (c, sparseExtremumRotationCount))) return_trace (false);
const auto &extremumRotationDelta = StructAfter<decltype (extremumRotationDeltaX)> (masterRotationDelta, sparse_master_rotation_count);
if (unlikely (!extremumRotationDelta.sanitize (c, sparse_extremum_rotation_count))) return_trace (false);
const auto &extremumRotationIndex = StructAfter<decltype (extremumRotationIndexX)> (extremumRotationDelta, sparseExtremumRotationCount);
if (unlikely (!extremumRotationIndex.sanitize (c, sparseExtremumRotationCount))) return_trace (false);
const auto &extremumRotationIndex = StructAfter<decltype (extremumRotationIndexX)> (extremumRotationDelta, sparse_extremum_rotation_count);
if (unlikely (!extremumRotationIndex.sanitize (c, sparse_extremum_rotation_count))) return_trace (false);
const auto &masterRotationIndex = StructAfter<decltype (masterRotationIndexX)> (extremumRotationIndex, sparseExtremumRotationCount);
if (unlikely (!masterRotationIndex.sanitize (c, sparseMasterRotationCount))) return_trace (false);
const auto &masterRotationIndex = StructAfter<decltype (masterRotationIndexX)> (extremumRotationIndex, sparse_extremum_rotation_count);
if (unlikely (!masterRotationIndex.sanitize (c, sparse_master_rotation_count))) return_trace (false);
return_trace (true);
}
protected:
UnsizedArrayOf<HBFLOAT32LE> masterRotationDelta; // length: sparseMasterRotationCount; Master rotation deltas
UnsizedArrayOf<HBFLOAT32LE> extremumRotationDeltaX; // length: sparseExtremumRotationCount; Extremum rotation deltas
UnsizedArrayOf<MatrixIndex> extremumRotationIndexX; // length: sparseExtremumRotationCount; Extremum rotation indices
UnsizedArrayOf<HBUINT16LE> masterRotationIndexX; // length: sparseMasterRotationCount; Master rotation indices
UnsizedArrayOf<HBFLOAT32LE> masterRotationDelta; // length: sparse_master_rotation_count; Master rotation deltas
UnsizedArrayOf<HBFLOAT32LE> extremumRotationDeltaX; // length: sparse_extremum_rotation_count; Extremum rotation deltas
UnsizedArrayOf<MatrixIndex> extremumRotationIndexX; // length: sparse_extremum_rotation_count; Extremum rotation indices
UnsizedArrayOf<HBUINT16LE> masterRotationIndexX; // length: sparse_master_rotation_count; Master rotation indices
Align<4> padding; // Pad to float32le alignment
public:
@@ -354,6 +358,9 @@ struct PartComposite
{
public:
HB_INTERNAL void apply_transforms (hb_array_t<hb_transform_t> transforms,
hb_array_t<const float> coords) const;
unsigned get_total_num_axes () const
{ return totalNumAxes; }
+13
View File
@@ -225,6 +225,19 @@ struct hb_transform_t
transform (other);
}
bool get_eigen_vector (float &eigen_x, float &eigen_y) const
{
// Find the eigenvector of the transform, or determine that there is none.
float a = xx - yy;
float b = 2 * xy;
float d = sqrtf (a * a + b * b);
if (d == 0.f)
return false;
eigen_x = b / d;
eigen_y = -a / d;
return true;
}
float xx = 1.f;
float yx = 0.f;
float xy = 0.f;