iPlug2 - C++ Audio Plug-in Framework
IGraphicsUtilities.h
1 /*
2  ==============================================================================
3 
4  This file is part of the iPlug 2 library. Copyright (C) the iPlug 2 developers.
5 
6  See LICENSE.txt for more info.
7 
8  ==============================================================================
9 */
10 
11 #pragma once
12 #include "IPlugConstants.h"
13 #include "IGraphicsConstants.h"
14 
15 #include <cmath>
16 
17 BEGIN_IPLUG_NAMESPACE
18 BEGIN_IGRAPHICS_NAMESPACE
19 
20 // these are macros to shorten the instantiation of IControls
21 // for a paramater ID MyParam, define constants named MyParam_X, MyParam_Y, MyParam_W, MyParam_H to specify the Control's IRect
22 // then when instantiating a Control you can just call MakeIRect(MyParam) to specify the IRect
23 #define MakeIRect(a) IRECT(a##_X, a##_Y, a##_X + a##_W, a##_Y + a##_H)
24 #define MakeIRectHOffset(a, xoffs) IRECT(a##_X + xoffs, a##_Y, a##_X + a##_W + xoffs, a##_Y + a##_H)
25 #define MakeIRectVOffset(a, yoffs) IRECT(a##_X, a##_Y + yoffs, a##_X + a##_W, a##_Y + a##_H + yoffs)
26 #define MakeIRectHVOffset(a, xoffs, yoffs) IRECT(a##_X + xoffs, a##_Y + yoffs, a##_X + a##_W + xoffs, a##_Y + a##_H + yoffs)
27 
28 static double GetTimestamp()
29 {
30  static auto start = std::chrono::steady_clock::now();
31  return std::chrono::duration<double>(std::chrono::steady_clock::now() - start).count();
32 }
33 
34 template <typename T>
35 inline T DegToRad(T degrees)
36 {
37  return static_cast<T>(iplug::PI) * (degrees / static_cast<T>(180.0));
38 }
39 
40 template <typename T>
41 inline T RadToDeg(T radians)
42 {
43  return radians / static_cast<T>(iplug::PI) * static_cast<T>(180.0);
44 }
45 
54 static inline void RadialPoints(float angleDegrees, float cx, float cy, float rMin, float rMax, int nPoints, float data[][2])
55 {
56  const float angleRadians = DegToRad(angleDegrees - 90.f);
57  const float sinV = std::sin(angleRadians);
58  const float cosV = std::cos(angleRadians);
59 
60  for(auto i = 0; i < nPoints; i++)
61  {
62  const float r = rMin+(rMax-rMin) * (float) i / float (nPoints-1);
63  data[i][0] = (cx + r * cosV);
64  data[i][1] = (cy + r * sinV);
65  }
66 }
67 
68 // Return the intersection of line(p0, p1) with line(p2, p3) as a fraction of the distance along (p2, p3).
69 static float GetLineCrossing(IVec2 p0, IVec2 p1, IVec2 p2, IVec2 p3)
70 {
71  auto b = p2 - p0;
72  auto d = p1 - p0;
73  auto e = p3 - p2;
74  float m = d.x * e.y - d.y * e.x;
75 
76  float epsilon = 1e-8f;
77  if (std::abs(m) < epsilon)
78  return NAN;
79  return -(d.x * b.y - d.y * b.x) / m;
80 }
81 
82 END_IGRAPHICS_NAMESPACE
83 END_IPLUG_NAMESPACE
84 
Encapsulate an xy point in one struct.
IPlug Constant definitions, Types, magic numbers.