aboutsummaryrefslogtreecommitdiff
path: root/ink_stroke_modeler/internal/utils.h
blob: 6bf2a2768dd3fb25b2420fde622c488a791ef573 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
 * Copyright 2022 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef INK_STROKE_MODELER_INTERNAL_UTILS_H_
#define INK_STROKE_MODELER_INTERNAL_UTILS_H_

#include <cmath>

#include "ink_stroke_modeler/types.h"

namespace ink {
namespace stroke_model {

// General utility functions for use within the stroke model.

// Clamps the given value to the range [0, 1].
inline float Clamp01(float value) {
  if (value < 0.f) return 0.f;
  if (value > 1.f) return 1.f;
  return value;
}

// Returns the ratio of the difference from `start` to `value` and the
// difference from `start` to `end`, clamped to the range [0, 1]. If
// `start` == `end`, returns 1 if `value` > `start`, 0 otherwise.
inline float Normalize01(float start, float end, float value) {
  if (start == end) {
    return value > start ? 1 : 0;
  }
  return Clamp01((value - start) / (end - start));
}

// Linearly interpolates between `start` and `end`, clamping the interpolation
// value to the range [0, 1].
template <typename ValueType>
inline ValueType Interp(ValueType start, ValueType end, float interp_amount) {
  return start + (end - start) * Clamp01(interp_amount);
}

// Linearly interpolates from `start` to `end`, traveling around the shorter
// path (e.g. interpolating from π/4 to 7π/4 is equivalent to interpolating from
// π/4 to 0, then 2π to 7π/4). The returned angle will be normalized to the
// interval [0, 2π). All angles are measured in radians.
inline float InterpAngle(float start, float end, float interp_amount) {
  auto normalize_angle = [](float angle) {
    while (angle < 0) angle += 2 * M_PI;
    while (angle > 2 * M_PI) angle -= 2 * M_PI;
    return angle;
  };

  start = normalize_angle(start);
  end = normalize_angle(end);
  float delta = end - start;
  if (delta < -M_PI) {
    end += 2 * M_PI;
  } else if (delta > M_PI) {
    end -= 2 * M_PI;
  }
  return normalize_angle(Interp(start, end, interp_amount));
}

// Returns the distance between two points.
inline float Distance(Vec2 start, Vec2 end) {
  return (end - start).Magnitude();
}

// Returns the point on the line segment from `segment_start` to `segment_end`
// that is closest to `point`, represented as the ratio of the length along the
// segment.
inline float NearestPointOnSegment(Vec2 segment_start, Vec2 segment_end,
                                   Vec2 point) {
  if (segment_start == segment_end) return 0;

  auto dot_product = [](Vec2 lhs, Vec2 rhs) {
    return lhs.x * rhs.x + lhs.y * rhs.y;
  };
  Vec2 segment_vector = segment_end - segment_start;
  Vec2 projection_vector = point - segment_start;
  return Clamp01(dot_product(projection_vector, segment_vector) /
                 dot_product(segment_vector, segment_vector));
}

}  // namespace stroke_model
}  // namespace ink

#endif  // INK_STROKE_MODELER_INTERNAL_UTILS_H_