1/*
2 * Copyright 2006, Haiku.
3 * Distributed under the terms of the MIT License.
4 *
5 * Authors:
6 *		Stephan Aßmus <superstippi@gmx.de>
7 */
8
9#ifndef SUPPORT_H
10#define SUPPORT_H
11
12#include <Rect.h>
13
14class BPositionIO;
15class BString;
16
17// constrain
18inline void
19constrain(float& value, float min, float max)
20{
21	if (value < min)
22		value = min;
23	if (value > max)
24		value = max;
25}
26
27// constrain_int32_0_255_asm
28inline int32
29constrain_int32_0_255_asm(int32 value) {
30    asm("movl  $0,    %%ecx;\n"
31        "movl  $255,  %%edx;\n"
32        "cmpl  %%ecx, %%eax;\n"
33        "cmovl %%ecx, %%eax;\n"
34        "cmpl  %%edx, %%eax;\n"
35        "cmovg %%edx, %%eax"
36       : "=a" (value)
37       : "a" (value)
38       : "%ecx", "%edx" );
39    return value;
40}
41
42inline int32
43constrain_int32_0_255_c(int32 value) {
44    return max_c(0, min_c(255, value));
45}
46
47#define constrain_int32_0_255 constrain_int32_0_255_asm
48
49// rect_to_int
50inline void
51rect_to_int(BRect r,
52			int32& left, int32& top, int32& right, int32& bottom)
53{
54	left = (int32)floorf(r.left);
55	top = (int32)floorf(r.top);
56	right = (int32)ceilf(r.right);
57	bottom = (int32)ceilf(r.bottom);
58}
59
60// point_point_distance
61inline float
62point_point_distance(BPoint a, BPoint b)
63{
64	float xDiff = b.x - a.x;
65	float yDiff = b.y - a.y;
66	return sqrtf(xDiff * xDiff + yDiff * yDiff);
67}
68
69// point_line_distance
70double
71point_line_distance(double x1, double y1,
72					double x2, double y2,
73					double x,  double y);
74
75// point_line_distance
76double
77point_line_distance(BPoint point, BPoint a, BPoint b);
78
79// calc_angle
80double
81calc_angle(BPoint origin, BPoint from, BPoint to, bool degree = true);
82
83/*
84template <class T>
85T min4(const T a, const T b, const T c, const T d)
86{
87	T e = a < b ? a : b;
88	T f = c < d ? c : d;
89	return e < f ? e : f;
90}
91template <class T>
92T max4(const T a, const T b, const T c, const T d)
93{
94	T e = a > b ? a : b;
95	T f = c > d ? c : d;
96	return e > f ? e : f;
97}
98*/
99inline float
100min4(float a, float b, float c, float d)
101{
102	return min_c(a, min_c(b, min_c(c, d)));
103}
104
105inline float
106max4(float a, float b, float c, float d)
107{
108	return max_c(a, max_c(b, max_c(c, d)));
109}
110
111inline float
112min5(float v1, float v2, float v3, float v4, float v5)
113{
114	return min_c(min4(v1, v2, v3, v4), v5);
115}
116
117inline float
118max5(float v1, float v2, float v3, float v4, float v5)
119{
120	return max_c(max4(v1, v2, v3, v4), v5);
121}
122
123inline float
124roundf(float v)
125{
126	if (v >= 0.0)
127		return floorf(v + 0.5);
128	return ceilf(v - 0.5);
129}
130
131status_t write_string(BPositionIO* stream, BString& string);
132void append_float(BString& string, float n, int32 maxDigits = 4);
133
134//double gauss(double f);
135
136
137# endif // SUPPORT_H
138