MRPT  1.9.9
KmUtils.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 // BEWARE: BETA VERSION
10 // --------------------
11 //
12 // Utilities for arbitrary dimensional points in space. All points are treated
13 // as simple value
14 // arrays. This is done for two reasons:
15 // - Using value arrays instead of a point class makes all point operations
16 // very explicit, which
17 // makes their usage easier to optimize.
18 // - A value array is about as universal a format as possible, which makes it
19 // easier for
20 // people to use the k-means code in any project.
21 // Also contains assertion code that can be disabled if desired.
22 //
23 // Author: David Arthur (darthur@gmail.com), 2009
24 
25 #pragma once
26 
27 #include <mrpt/config.h> // For HAVE_MALLOC_H
28 
29 // Includes
30 /* Jerome Monceaux : bilock@gmail.com
31  * Add a specific case for apple
32  */
33 #ifdef HAVE_MALLOC_H
34 #include <malloc.h>
35 #elif defined(HAVE_MALLOC_MALLOC_H)
36 #include <malloc/malloc.h>
37 #endif
38 
39 #include <memory.h>
40 #include <cstdlib>
41 
42 // The data-type used for a single coordinate for points
43 using Scalar = double;
44 
45 // Point utilities
46 // ===============
47 
48 // Point creation and deletion
49 inline Scalar* PointAllocate(int d)
50 {
51  return (Scalar*)malloc(d * sizeof(Scalar));
52 }
53 
54 inline void PointFree(Scalar* p) { free(p); }
55 inline void PointCopy(Scalar* p1, const Scalar* p2, int d)
56 {
57  memcpy(p1, p2, d * sizeof(Scalar));
58 }
59 
60 // Point vector tools
61 inline void PointAdd(Scalar* p1, const Scalar* p2, int d)
62 {
63  for (int i = 0; i < d; i++) p1[i] += p2[i];
64 }
65 
66 inline void PointScale(Scalar* p, Scalar scale, int d)
67 {
68  for (int i = 0; i < d; i++) p[i] *= scale;
69 }
70 
71 inline Scalar PointDistSq(const Scalar* p1, const Scalar* p2, int d)
72 {
73  Scalar result = 0;
74  for (int i = 0; i < d; i++) result += (p1[i] - p2[i]) * (p1[i] - p2[i]);
75  return result;
76 }
77 
78 // Assertions
79 // ==========
80 
81 // Comment out ENABLE_KMEANS_ASSERTS to turn off ASSERTS for added speed.
82 #define ENABLE_KMEANS_ASSERTS
83 #ifdef ENABLE_KMEANS_ASSERTS
85  const char* file, int line, const char* expression);
86 #define KM_ASSERT(expression) \
87  (void)((expression) != 0 ? 0 : __KMeansAssertionFailure(__FILE__, __LINE__, #expression))
88 #else
89 #define KM_ASSERT(expression)
90 #endif
91 
92 // Miscellaneous utilities
93 // =======================
94 
95 // Returns a random integer chosen uniformly from the range [0, n-1]. Note that
96 // RAND_MAX could be
97 // less than n. On Visual Studio, it is only 32767. For larger values of
98 // RAND_MAX, we need to be
99 // careful of overflow.
100 inline int GetRandom(int n)
101 {
102  int u = rand() * RAND_MAX + rand();
103  return ((u % n) + n) % n;
104 }
double Scalar
Definition: KmUtils.h:43
Scalar * PointAllocate(int d)
Definition: KmUtils.h:49
void PointCopy(Scalar *p1, const Scalar *p2, int d)
Definition: KmUtils.h:55
void PointAdd(Scalar *p1, const Scalar *p2, int d)
Definition: KmUtils.h:61
void PointScale(Scalar *p, Scalar scale, int d)
Definition: KmUtils.h:66
int __KMeansAssertionFailure(const char *file, int line, const char *expression)
Definition: KmUtils.cpp:17
int GetRandom(int n)
Definition: KmUtils.h:100
void PointFree(Scalar *p)
Definition: KmUtils.h:54
Scalar PointDistSq(const Scalar *p1, const Scalar *p2, int d)
Definition: KmUtils.h:71
void memcpy(void *dest, size_t destSize, const void *src, size_t copyCount) noexcept
An OS and compiler independent version of "memcpy".



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: c7a3bec24 Sun Mar 29 18:33:13 2020 +0200 at dom mar 29 18:50:38 CEST 2020