Main MRPT website > C++ reference for MRPT 1.5.7
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-2017, 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 as simple value
13 // arrays. This is done for two reasons:
14 // - Using value arrays instead of a point class makes all point operations very explicit, which
15 // makes their usage easier to optimize.
16 // - A value array is about as universal a format as possible, which makes it easier for
17 // people to use the k-means code in any project.
18 // Also contains assertion code that can be disabled if desired.
19 //
20 // Author: David Arthur (darthur@gmail.com), 2009
21 
22 #ifndef KM_UTILS_H__
23 #define KM_UTILS_H__
24 
25 #include <mrpt/config.h> // For HAVE_MALLOC_H
26 
27 // Includes
28 /* Jerome Monceaux : bilock@gmail.com
29  * Add a specific case for apple
30  */
31 #ifdef HAVE_MALLOC_H
32 # include <malloc.h>
33 #elif defined(HAVE_MALLOC_MALLOC_H)
34 # include <malloc/malloc.h>
35 #endif
36 
37 #include <memory.h>
38 #include <cstdlib>
39 
40 // The data-type used for a single coordinate for points
41 typedef double Scalar;
42 
43 // Point utilities
44 // ===============
45 
46 // Point creation and deletion
47 inline Scalar *PointAllocate(int d) {
48  return (Scalar*)malloc(d * sizeof(Scalar));
49 }
50 
51 inline void PointFree(Scalar *p) {
52  free(p);
53 }
54 
55 inline void PointCopy(Scalar *p1, const Scalar *p2, int d) {
56  memcpy(p1, p2, d * sizeof(Scalar));
57 }
58 
59 // Point vector tools
60 inline void PointAdd(Scalar *p1, const Scalar *p2, int d) {
61  for (int i = 0; i < d; i++)
62  p1[i] += p2[i];
63 }
64 
65 inline void PointScale(Scalar *p, Scalar scale, int d) {
66  for (int i = 0; i < d; i++)
67  p[i] *= scale;
68 }
69 
70 inline Scalar PointDistSq(const Scalar *p1, const Scalar *p2, int d) {
71  Scalar result = 0;
72  for (int i = 0; i < d; i++)
73  result += (p1[i] - p2[i]) * (p1[i] - p2[i]);
74  return result;
75 }
76 
77 // Assertions
78 // ==========
79 
80 // Comment out ENABLE_KMEANS_ASSERTS to turn off ASSERTS for added speed.
81 #define ENABLE_KMEANS_ASSERTS
82 #ifdef ENABLE_KMEANS_ASSERTS
83 int __KMeansAssertionFailure(const char *file, int line, const char *expression);
84 #define KM_ASSERT(expression) \
85  (void)((expression) != 0? 0 : __KMeansAssertionFailure(__FILE__, __LINE__, #expression))
86 #else
87 #define KM_ASSERT(expression)
88 #endif
89 
90 // Miscellaneous utilities
91 // =======================
92 
93 // Returns a random integer chosen uniformly from the range [0, n-1]. Note that RAND_MAX could be
94 // less than n. On Visual Studio, it is only 32767. For larger values of RAND_MAX, we need to be
95 // careful of overflow.
96 inline int GetRandom(int n) {
97  int u = rand() * RAND_MAX + rand();
98  return ((u % n) + n) % n;
99 }
100 
101 #endif
void BASE_IMPEXP memcpy(void *dest, size_t destSize, const void *src, size_t copyCount) MRPT_NO_THROWS
An OS and compiler independent version of "memcpy".
Definition: os.cpp:358
GLenum GLenum GLenum GLenum GLenum scale
Definition: glext.h:5717
GLenum GLsizei n
Definition: glext.h:4618
Scalar * PointAllocate(int d)
Definition: KmUtils.h:47
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:60
void PointScale(Scalar *p, Scalar scale, int d)
Definition: KmUtils.h:65
int __KMeansAssertionFailure(const char *file, int line, const char *expression)
Definition: KmUtils.cpp:17
int GetRandom(int n)
Definition: KmUtils.h:96
void PointFree(Scalar *p)
Definition: KmUtils.h:51
double Scalar
Definition: KmUtils.h:41
GLfloat GLfloat p
Definition: glext.h:5587
Scalar PointDistSq(const Scalar *p1, const Scalar *p2, int d)
Definition: KmUtils.h:70



Page generated by Doxygen 1.8.14 for MRPT 1.5.7 Git: 5902e14cc Wed Apr 24 15:04:01 2019 +0200 at lun oct 28 01:39:17 CET 2019