src/basics.h

changeset 55
cb81ecb5fb23
parent 53
3af627f7a40f
child 70
f21b800b02a4
equal deleted inserted replaced
54:a4055f67b9c7 55:cb81ecb5fb23
106 return static_cast<std::make_signed_t<T>>(x); 106 return static_cast<std::make_signed_t<T>>(x);
107 } 107 }
108 108
109 109
110 /** 110 /**
111 * @brief casts double and long double - and only those types - to float 111 * @brief casts floating point values to float, converting non-floating point values causes an error
112 * @param[in] x double or long double to cast 112 * @param[in] x floating point value to cast
113 * @returns float 113 * @returns float
114 */ 114 */
115 template<typename T> 115 template<typename T>
116 auto doubleToFloat(T x) 116 auto toFloat(T x) -> std::enable_if_t<std::is_floating_point_v<T>, float>
117 -> std::enable_if_t<
118 std::is_same_v<std::decay_t<T>, double> ||
119 std::is_same_v<std::decay_t<T>, long double
120 >, float>
121 { 117 {
122 return static_cast<float>(x); 118 return static_cast<float>(x);
123 } 119 }
124 120
125 /** 121 /**
126 * @brief Casts float - and only float - to double 122 * @brief casts floating point values to double, converting non-floating point values causes an error
127 * @param[in] x float to cast 123 * @param[in] x floating point value to cast
128 * @returns double 124 * @returns double
129 */ 125 */
130 template<typename T> 126 template<typename T>
131 auto floatToDouble(T x) 127 auto toDouble(T x) -> std::enable_if_t<std::is_floating_point_v<T>, double>
132 -> std::enable_if_t<std::is_same_v<std::decay_t<T>, float>, double>
133 { 128 {
134 return static_cast<double>(x); 129 return static_cast<double>(x);
135 } 130 }
131
132 template<int N, typename T, glm::qualifier Q>
133 inline QPoint toQPoint(const glm::vec<N, T, Q>& vec)
134 {
135 return {static_cast<int>(vec.x), static_cast<int>(vec.y)};
136 }
137
138 template<int N, typename T, glm::qualifier Q>
139 inline QPointF toQPointF(const glm::vec<N, T, Q>& vec)
140 {
141 return {toDouble(vec.x), toDouble(vec.y)};
142 }
143
144 inline glm::vec2 toVec2(const QPoint& point)
145 {
146 return {point.x(), point.y()};
147 }
148
149 inline glm::vec2 toVec2(const QPointF& point)
150 {
151 return {point.x(), point.y()};
152 }
153
136 Q_DECLARE_METATYPE(glm::vec3) 154 Q_DECLARE_METATYPE(glm::vec3)
137 Q_DECLARE_METATYPE(glm::mat4) 155 Q_DECLARE_METATYPE(glm::mat4)

mercurial