MRPT  1.9.9
KmUtils.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2018, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://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 #ifndef KM_UTILS_H__
26 #define KM_UTILS_H__
27 
28 #include <mrpt/config.h> // For HAVE_MALLOC_H
29 
30 // Includes
31 /* Jerome Monceaux : bilock@gmail.com
32  * Add a specific case for apple
33  */
34 #ifdef HAVE_MALLOC_H
35 #include <malloc.h>
36 #elif defined(HAVE_MALLOC_MALLOC_H)
37 #include <malloc/malloc.h>
38 #endif
39 
40 #include <memory.h>
41 #include <cstdlib>
42 
43 // The data-type used for a single coordinate for points
44 using Scalar = double;
45 
46 // Point utilities
47 // ===============
48 
49 // Point creation and deletion
50 inline Scalar* PointAllocate(int d)
51 {
52  return (Scalar*)malloc(d * sizeof(Scalar));
53 }
54 
55 inline void PointFree(Scalar* p) { free(p); }
56 inline void PointCopy(Scalar* p1, const Scalar* p2, int d)
57 {
58  memcpy(p1, p2, d * sizeof(Scalar));
59 }
60 
61 // Point vector tools
62 inline void PointAdd(Scalar* p1, const Scalar* p2, int d)
63 {
64  for (int i = 0; i < d; i++) p1[i] += p2[i];
65 }
66 
67 inline void PointScale(Scalar* p, Scalar scale, int d)
68 {
69  for (int i = 0; i < d; i++) p[i] *= scale;
70 }
71 
72 inline Scalar PointDistSq(const Scalar* p1, const Scalar* p2, int d)
73 {
74  Scalar result = 0;
75  for (int i = 0; i < d; i++) result += (p1[i] - p2[i]) * (p1[i] - p2[i]);
76  return result;
77 }
78 
79 // Assertions
80 // ==========
81 
82 // Comment out ENABLE_KMEANS_ASSERTS to turn off ASSERTS for added speed.
83 #define ENABLE_KMEANS_ASSERTS
84 #ifdef ENABLE_KMEANS_ASSERTS
86  const char* file, int line, const char* expression);
87 #define KM_ASSERT(expression) \
88  (void)((expression) != 0 ? 0 : __KMeansAssertionFailure(__FILE__, __LINE__, #expression))
89 #else
90 #define KM_ASSERT(expression)
91 #endif
92 
93 // Miscellaneous utilities
94 // =======================
95 
96 // Returns a random integer chosen uniformly from the range [0, n-1]. Note that
97 // RAND_MAX could be
98 // less than n. On Visual Studio, it is only 32767. For larger values of
99 // RAND_MAX, we need to be
100 // careful of overflow.
101 inline int GetRandom(int n)
102 {
103  int u = rand() * RAND_MAX + rand();
104  return ((u % n) + n) % n;
105 }
106 
107 #endif
double Scalar
Definition: KmUtils.h:44
GLenum GLenum GLenum GLenum GLenum scale
Definition: glext.h:6502
GLenum GLsizei n
Definition: glext.h:5074
Scalar * PointAllocate(int d)
Definition: KmUtils.h:50
void PointCopy(Scalar *p1, const Scalar *p2, int d)
Definition: KmUtils.h:56
void PointAdd(Scalar *p1, const Scalar *p2, int d)
Definition: KmUtils.h:62
void PointScale(Scalar *p, Scalar scale, int d)
Definition: KmUtils.h:67
int __KMeansAssertionFailure(const char *file, int line, const char *expression)
Definition: KmUtils.cpp:17
int GetRandom(int n)
Definition: KmUtils.h:101
void PointFree(Scalar *p)
Definition: KmUtils.h:55
GLfloat GLfloat p
Definition: glext.h:6305
Scalar PointDistSq(const Scalar *p1, const Scalar *p2, int d)
Definition: KmUtils.h:72
void memcpy(void *dest, size_t destSize, const void *src, size_t copyCount) noexcept
An OS and compiler independent version of "memcpy".
Definition: os.cpp:356



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 7d5e6d718 Fri Aug 24 01:51:28 2018 +0200 at lun nov 2 08:35:50 CET 2020