1 #ifndef VORONOTALT_BASIC_TYPES_AND_FUNCTIONS_H_ 2 #define VORONOTALT_BASIC_TYPES_AND_FUNCTIONS_H_ 7 #define VORONOTALT_UI32 10 #define FLOATCONST( v ) v ## f 11 #define FLOATEPSILON 0.000001f 12 #define PIVALUE 3.14159265358979323846f 14 #define FLOATCONST( v ) v 15 #define FLOATEPSILON 0.0000000001 16 #define PIVALUE 3.14159265358979323846 22 #ifdef VORONOTALT_FP32 28 #ifdef VORONOTALT_UI32 29 typedef unsigned int UnsignedInt;
31 typedef std::size_t UnsignedInt;
40 SimplePoint() noexcept : x(FLOATCONST(0.0)), y(FLOATCONST(0.0)), z(FLOATCONST(0.0))
44 SimplePoint(
const Float x,
const Float y,
const Float z) noexcept : x(x), y(y), z(z)
62 SimpleSphere(
const Float x,
const Float y,
const Float z,
const Float r) noexcept : p(x, y, z), r(r)
74 SimpleQuaternion(
const Float a,
const Float b,
const Float c,
const Float d) noexcept : a(a), b(b), c(c), d(d)
88 ValuedID() noexcept : value(FLOATCONST(0.0)), index(0)
92 ValuedID(
const Float value,
const UnsignedInt index) noexcept : value(value), index(index)
96 bool operator<(
const ValuedID& cid)
const noexcept
98 return (value<cid.value || (value==cid.value && index<cid.index));
102 inline bool equal(
const Float a,
const Float b,
const Float e) noexcept
104 return (((a-b)<=e) && ((b-a)<=e));
107 inline bool equal(
const Float a,
const Float b) noexcept
109 return equal(a, b, FLOATEPSILON);
112 inline bool less(
const Float a,
const Float b) noexcept
114 return ((a+FLOATEPSILON)<b);
117 inline bool greater(
const Float a,
const Float b) noexcept
119 return ((a-FLOATEPSILON)>b);
122 inline bool less_or_equal(
const Float a,
const Float b) noexcept
124 return (less(a, b) || equal(a, b));
127 inline bool greater_or_equal(
const Float a,
const Float b) noexcept
129 return (greater(a, b) || equal(a, b));
132 inline void set_close_to_equal(Float& a,
const Float b) noexcept
142 const Float dx=(a.x-b.x);
143 const Float dy=(a.y-b.y);
144 const Float dz=(a.z-b.z);
145 return (dx*dx+dy*dy+dz*dz);
150 return sqrt(squared_distance_from_point_to_point(a, b));
155 return (equal(a.x, b.x) && equal(a.y, b.y) && equal(a.z, b.z));
158 inline Float squared_point_module(
const SimplePoint& a) noexcept
160 return (a.x*a.x+a.y*a.y+a.z*a.z);
163 inline Float point_module(
const SimplePoint& a) noexcept
165 return sqrt(squared_point_module(a));
170 return (a.x*b.x+a.y*b.y+a.z*b.z);
175 return SimplePoint(a.y*b.z-a.z*b.y, a.z*b.x-a.x*b.z, a.x*b.y-a.y*b.x);
185 return ((equal(squared_point_module(a), FLOATCONST(1.0))) ? a : point_and_number_product(a, FLOATCONST(1.0)/point_module(a)));
200 return dot_product(unit_point(plane_normal), sub_of_points(x, plane_point));
205 const Float sd=signed_distance_from_point_to_plane(plane_point, plane_normal, x);
206 if(greater(sd, FLOATCONST(0.0)))
210 else if(less(sd, FLOATCONST(0.0)))
219 const Float da=signed_distance_from_point_to_plane(plane_point, plane_normal, a);
220 const Float db=signed_distance_from_point_to_plane(plane_point, plane_normal, b);
227 const Float t=da/(da-db);
228 return sum_of_points(a, point_and_number_product(sub_of_points(b, a), t));
234 return (point_module(cross_product(sub_of_points(b, a), sub_of_points(c, a)))/FLOATCONST(2.0));
239 Float cos_val=dot_product(unit_point(sub_of_points(a, o)), unit_point(sub_of_points(b, o)));
240 if(cos_val<FLOATCONST(-1.0))
242 cos_val=FLOATCONST(-1.0);
244 else if(cos_val>FLOATCONST(1.0))
246 cos_val=FLOATCONST(1.0);
248 return std::acos(cos_val);
253 const Float angle=min_angle(o, a, b);
254 const SimplePoint n=cross_product(unit_point(sub_of_points(a, o)), unit_point(sub_of_points(b, o)));
255 if(dot_product(sub_of_points(c, o), n)>=0)
261 return (PIVALUE*FLOATCONST(2.0)-angle);
268 if(!equal(b.x, FLOATCONST(0.0)) && (!equal(b.y, FLOATCONST(0.0)) || !equal(b.z, FLOATCONST(0.0))))
270 b.x=FLOATCONST(0.0)-b.x;
271 return unit_point(cross_product(a, b));
273 else if(!equal(b.y, FLOATCONST(0.0)) && (!equal(b.x, FLOATCONST(0.0)) || !equal(b.z, FLOATCONST(0.0))))
275 b.y=FLOATCONST(0.0)-b.y;
276 return unit_point(cross_product(a, b));
278 else if(!equal(b.x, FLOATCONST(0.0)))
280 return SimplePoint(FLOATCONST(0.0), FLOATCONST(1.0), FLOATCONST(0.0));
284 return SimplePoint(FLOATCONST(1.0), FLOATCONST(0.0), FLOATCONST(0.0));
290 return less(squared_distance_from_point_to_point(a.p, b.p), (a.r+b.r)*(a.r+b.r));
295 return (equal(a.r, b.r) && equal(a.p.x, b.p.x) && equal(a.p.y, b.p.y) && equal(a.p.z, b.p.z));
300 return (greater_or_equal(a.r, b.r) && less_or_equal(squared_distance_from_point_to_point(a.p, b.p), (a.r-b.r)*(a.r-b.r)));
303 inline Float distance_to_center_of_intersection_circle_of_two_spheres(
const SimpleSphere& a,
const SimpleSphere& b) noexcept
305 const Float cm=distance_from_point_to_point(a.p, b.p);
306 const Float cos_g=(a.r*a.r+cm*cm-b.r*b.r)/(2*a.r*cm);
313 const Float cm=point_module(cv);
314 const Float cos_g=(a.r*a.r+cm*cm-b.r*b.r)/(2*a.r*cm);
315 return sum_of_points(a.p, point_and_number_product(cv, a.r*cos_g/cm));
321 const Float cm=point_module(cv);
322 const Float cos_g=(a.r*a.r+cm*cm-b.r*b.r)/(2*a.r*cm);
323 const Float sin_g=std::sqrt(1-cos_g*cos_g);
324 return SimpleSphere(sum_of_points(a.p, point_and_number_product(cv, a.r*cos_g/cm)), a.r*sin_g);
329 const SimplePoint v=unit_point(sub_of_points(b, a));
330 const Float l=dot_product(v, sub_of_points(o, a));
331 if(l>FLOATCONST(0.0) && (l*l)<=squared_distance_from_point_to_point(a, b))
333 result=sum_of_points(a, point_and_number_product(v, l));
341 const Float dist_in_to_out=distance_from_point_to_point(p_in, p_out);
342 if(dist_in_to_out>FLOATCONST(0.0))
344 const SimplePoint v=point_and_number_product(sub_of_points(p_in, p_out), FLOATCONST(1.0)/dist_in_to_out);
345 const SimplePoint u=sub_of_points(circle.p, p_out);
346 const SimplePoint s=sum_of_points(p_out, point_and_number_product(v, dot_product(v, u)));
347 const Float ll=(circle.r*circle.r)-squared_distance_from_point_to_point(circle.p, s);
348 if(ll>=FLOATCONST(0.0))
350 result=sum_of_points(s, point_and_number_product(v, FLOATCONST(0.0)-std::sqrt(ll)));
359 const SimplePoint oa=unit_point(sub_of_points(a, o));
360 const SimplePoint d1=sub_of_points(b1, sum_of_points(o, point_and_number_product(oa, dot_product(oa, sub_of_points(b1, o)))));
361 const SimplePoint d2=sub_of_points(b2, sum_of_points(o, point_and_number_product(oa, dot_product(oa, sub_of_points(b2, o)))));
362 const Float cos_val=dot_product(unit_point(d1), unit_point(d2));
363 return std::acos(std::max(FLOATCONST(-1.0), std::min(cos_val, FLOATCONST(1.0))));
369 q1.a*q2.a - q1.b*q2.b - q1.c*q2.c - q1.d*q2.d,
370 q1.a*q2.b + q1.b*q2.a + q1.c*q2.d - q1.d*q2.c,
371 q1.a*q2.c - q1.b*q2.d + q1.c*q2.a + q1.d*q2.b,
372 q1.a*q2.d + q1.b*q2.c - q1.c*q2.b + q1.d*q2.a);
377 return SimpleQuaternion(q.a, FLOATCONST(0.0)-q.b, FLOATCONST(0.0)-q.c, FLOATCONST(0.0)-q.d);
382 if(squared_point_module(axis)>0)
384 const Float radians_angle_half=(angle*FLOATCONST(0.5));
387 const SimpleQuaternion q3=quaternion_product(quaternion_product(q1, q2), inverted_quaternion(q1));
Definition: basic_types_and_functions.h:34
Definition: basic_types_and_functions.h:83
Definition: basic_types_and_functions.h:67
Definition: basic_types_and_functions.h:19
Definition: basic_types_and_functions.h:49