-
Notifications
You must be signed in to change notification settings - Fork 65
/
OpenMapper.h
1795 lines (1559 loc) · 58 KB
/
OpenMapper.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2006-2011, SRI International (R)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#ifndef __OpenKarto_Mapper_h__
#define __OpenKarto_Mapper_h__
#ifdef USE_TBB
#include <tbb/mutex.h>
#include <tbb/parallel_for.h>
#include <tbb/blocked_range.h>
#include <tbb/blocked_range3d.h>
#endif
#include <OpenKarto/Event.h>
#include <OpenKarto/Pair.h>
#include <OpenKarto/Geometry.h>
#include <OpenKarto/StringHelper.h>
#include <OpenKarto/SensorData.h>
#include <OpenKarto/Grid.h>
#include <OpenKarto/GridIndexLookup.h>
#include <OpenKarto/Module.h>
#include <OpenKarto/OccupancyGrid.h>
#include <OpenKarto/TypeCasts.h>
namespace karto
{
///** \addtogroup OpenKarto */
//@{
/**
* Event arguments for the events in the Mapper
*/
class MapperEventArguments : public EventArguments
{
public:
/**
* Mapper event arguments with the provided message
* @param rMessage message
*/
MapperEventArguments(const String& rMessage)
: m_Message(rMessage)
{
}
/**
* Destructor
*/
virtual ~MapperEventArguments()
{
}
public:
/**
* Gets the event message
* @return event message
*/
const String& GetEventMessage() const
{
return m_Message;
}
private:
String m_Message;
};
#ifdef WIN32
EXPORT_KARTO_EVENT(KARTO_EXPORT, MapperEventArguments)
#endif
////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////
/**
* Base class to tag edge labels
*/
class EdgeLabel
{
public:
/**
* Default constructor
*/
EdgeLabel()
{
}
/**
* Destructor
*/
virtual ~EdgeLabel()
{
}
}; // EdgeLabel
////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////
// Contains the requisite information for the "spring"
// that links two scans together--the pose difference and the uncertainty
// (represented by a covariance matrix).
class LinkInfo : public EdgeLabel
{
public:
/**
* Link between the given poses
* @param rPose1 first pose
* @param rPose2 second pose
* @param rCovariance link uncertainty
*/
LinkInfo(const Pose2& rPose1, const Pose2& rPose2, const Matrix3& rCovariance)
{
Update(rPose1, rPose2, rCovariance);
}
/**
* Destructor
*/
virtual ~LinkInfo()
{
}
public:
/**
* Changes the link information to be the given parameters
* @param rPose1 new first pose
* @param rPose2 new second pose
* @param rCovariance new link uncertainty
*/
void Update(const Pose2& rPose1, const Pose2& rPose2, const Matrix3& rCovariance)
{
m_Pose1 = rPose1;
m_Pose2 = rPose2;
// transform second pose into the coordinate system of the first pose
Transform transform(rPose1, Pose2());
m_PoseDifference = transform.TransformPose(rPose2);
// transform covariance into reference of first pose
Matrix3 rotationMatrix;
rotationMatrix.FromAxisAngle(0, 0, 1, -rPose1.GetHeading());
m_Covariance = rotationMatrix * rCovariance * rotationMatrix.Transpose();
}
/**
* Gets the first pose
* @return first pose
*/
inline const Pose2& GetPose1()
{
return m_Pose1;
}
/**
* Gets the second pose
* @return second pose
*/
inline const Pose2& GetPose2()
{
return m_Pose2;
}
/**
* Gets the pose difference
* @return pose difference
*/
inline const Pose2& GetPoseDifference()
{
return m_PoseDifference;
}
/**
* Gets the link covariance
* @return link covariance
*/
inline const Matrix3& GetCovariance()
{
return m_Covariance;
}
private:
Pose2 m_Pose1;
Pose2 m_Pose2;
Pose2 m_PoseDifference;
Matrix3 m_Covariance;
}; // LinkInfo
////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////
template<typename T>
class Edge;
/**
* Represents a object in a graph
*/
template<typename T>
class Vertex
{
friend class Edge<T>;
public:
/**
* Constructs a vertex representing the given object
* @param pObject object
*/
Vertex(T pObject)
: m_pObject(pObject)
{
}
/**
* Destructor
*/
virtual ~Vertex()
{
}
/**
* Gets edges adjacent to this vertex
* @return adjacent edges
*/
inline const List<Edge<T>*>& GetEdges() const
{
return m_Edges;
}
/**
* Gets the object associated with this vertex
* @return the object
*/
inline T GetVertexObject() const
{
return m_pObject;
}
/**
* Gets a list of the vertices adjacent to this vertex
* @return adjacent vertices
*/
List<Vertex<T>*> GetAdjacentVertices() const
{
List<Vertex<T>*> vertices;
karto_const_forEach(typename List<Edge<T>*>, &m_Edges)
{
Edge<T>* pEdge = *iter;
// check both source and target because we have a undirected graph
if (pEdge->GetSource() != this)
{
vertices.Add(pEdge->GetSource());
}
if (pEdge->GetTarget() != this)
{
vertices.Add(pEdge->GetTarget());
}
}
return vertices;
}
private:
/**
* Adds the given edge to this vertex's edge list
* @param pEdge edge to add
*/
inline void AddEdge(Edge<T>* pEdge)
{
m_Edges.Add(pEdge);
}
T m_pObject;
List<Edge<T>*> m_Edges;
}; // Vertex<T>
////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////
/**
* Represents an edge in a graph
*/
template<typename T>
class Edge
{
public:
/**
* Constructs an edge from the source to target vertex
* @param pSource source vertex
* @param pTarget target vertex
*/
Edge(Vertex<T>* pSource, Vertex<T>* pTarget)
: m_pSource(pSource)
, m_pTarget(pTarget)
, m_pLabel(NULL)
{
m_pSource->AddEdge(this);
m_pTarget->AddEdge(this);
}
/**
* Destructor
*/
virtual ~Edge()
{
m_pSource = NULL;
m_pTarget = NULL;
if (m_pLabel != NULL)
{
delete m_pLabel;
m_pLabel = NULL;
}
}
public:
/**
* Gets the source vertex
* @return source vertex
*/
inline Vertex<T>* GetSource() const
{
return m_pSource;
}
/**
* Gets the target vertex
* @return target vertex
*/
inline Vertex<T>* GetTarget() const
{
return m_pTarget;
}
/**
* Gets the link info
* @return link info
*/
inline EdgeLabel* GetLabel()
{
return m_pLabel;
}
/**
* Sets the link payload
* @param pLabel edge label
*/
inline void SetLabel(EdgeLabel* pLabel)
{
m_pLabel = pLabel;
}
private:
Vertex<T>* m_pSource;
Vertex<T>* m_pTarget;
EdgeLabel* m_pLabel;
}; // class Edge<T>
////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////
/**
* Visitor class
*/
template<typename T>
class Visitor
{
public:
/**
* Applies the visitor to the vertex
* @param pVertex
* @return true if the visitor accepted the vertex, false otherwise
*/
virtual kt_bool Visit(Vertex<T>* pVertex) = 0;
}; // Visitor<T>
////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////
template<typename T>
class Graph;
/**
* Graph traversal algorithm
*/
template<typename T>
class GraphTraversal
{
public:
/**
* Traverser for the given graph
* @param pGraph graph
*/
GraphTraversal(Graph<T>* pGraph)
: m_pGraph(pGraph)
{
}
/**
* Destructor
*/
virtual ~GraphTraversal()
{
}
public:
/**
* Traverses the graph starting at the given vertex and applying the visitor to all visited nodes
* @param pStartVertex starting vertex
* @param pVisitor visitor
* @return list of vertices visited during traversal
*/
virtual List<T> Traverse(Vertex<T>* pStartVertex, Visitor<T>* pVisitor) = 0;
protected:
/**
* Graph being traversed
*/
Graph<T>* m_pGraph;
}; // GraphTraversal<T>
////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////
/**
* Graph
*/
template<typename T>
class Graph
{
public:
/**
* Type definition for list of vertices
*/
typedef List<Vertex<T>*> VertexList;
/**
* Type definition for list of edges
*/
typedef List<Edge<T>*> EdgeList;
public:
/**
* Default constructor
*/
Graph()
{
}
/**
* Destructor
*/
virtual ~Graph()
{
Clear();
}
public:
/**
* Adds and indexes the given vertex into the map
* @param pVertex vertex
*/
inline void AddVertex(Vertex<T>* pVertex)
{
m_Vertices.Add(pVertex);
}
/**
* Adds an edge to the graph
* @param pEdge edge
*/
inline void AddEdge(Edge<T>* pEdge)
{
m_Edges.Add(pEdge);
}
/**
* Deletes the graph data
*/
void Clear()
{
karto_const_forEach(typename VertexList, &m_Vertices)
{
// delete each vertex
delete *iter;
}
m_Vertices.Clear();
karto_const_forEach(typename EdgeList, &m_Edges)
{
// delete each edge
delete *iter;
}
m_Edges.Clear();
}
/**
* Gets the edges of this graph
* @return graph edges
*/
inline const EdgeList& GetEdges() const
{
return m_Edges;
}
/**
* Gets the vertices of this graph
* @return graph vertices
*/
inline const VertexList& GetVertices() const
{
return m_Vertices;
}
protected:
/**
* Map of names to vector of vertices
*/
VertexList m_Vertices;
/**
* Edges of this graph
*/
EdgeList m_Edges;
}; // Graph<T>
////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////
class OpenMapper;
class ScanMatcher;
/**
* Graph for graph SLAM algorithm
*/
class KARTO_EXPORT MapperGraph : public Graph<LocalizedObjectPtr>
{
public:
/**
* Graph for graph SLAM
* @param pOpenMapper mapper
* @param rangeThreshold range threshold
*/
MapperGraph(OpenMapper* pOpenMapper, kt_double rangeThreshold);
/**
* Destructor
*/
virtual ~MapperGraph();
public:
/**
* Adds a vertex representing the given object to the graph
* @param pObject object
*/
void AddVertex(LocalizedObject* pObject);
/**
* Creates an edge between the source object and the target object if it
* does not already exist; otherwise return the existing edge
* @param pSourceObject source object
* @param pTargetObject target object
* @param rIsNewEdge set to true if the edge is new
* @return edge between source and target vertices
*/
Edge<LocalizedObjectPtr>* AddEdge(LocalizedObject* pSourceObject, LocalizedObject* pTargetObject, kt_bool& rIsNewEdge);
/**
* Links object to last scan
* @param pObject object
*/
void AddEdges(LocalizedObject* pObject);
/**
* Links scan to last scan and nearby chains of scans
* @param pScan scan
* @param rCovariance match uncertainty
*/
void AddEdges(LocalizedLaserScan* pScan, const Matrix3& rCovariance);
/**
* Tries to close a loop using the given scan with the scans from the given sensor
* @param pScan scan
* @param rSensorName name of sensor
* @return true if a loop was closed
*/
kt_bool TryCloseLoop(LocalizedLaserScan* pScan, const Identifier& rSensorName);
/**
* Finds "nearby" (no further than given distance away) scans through graph links
* @param pScan scan
* @param maxDistance maximum distance
* @return "nearby" scans that have a path of links to given scan
*/
LocalizedLaserScanList FindNearLinkedScans(LocalizedLaserScan* pScan, kt_double maxDistance);
/**
* Finds scans that overlap the given scan (based on bounding boxes)
* @param pScan scan
* @return overlapping scans
*/
LocalizedLaserScanList FindOverlappingScans(LocalizedLaserScan* pScan);
/**
* Gets the graph's scan matcher
* @return scan matcher
*/
inline ScanMatcher* GetLoopScanMatcher() const
{
return m_pLoopScanMatcher;
}
/**
* Links the chain of scans to the given scan by finding the closest scan in the chain to the given scan
* @param rChain chain
* @param pScan scan
* @param rMean mean
* @param rCovariance match uncertainty
*/
void LinkChainToScan(const LocalizedLaserScanList& rChain, LocalizedLaserScan* pScan, const Pose2& rMean, const Matrix3& rCovariance);
private:
/**
* Gets the vertex associated with the given scan
* @param pScan scan
* @return vertex of scan
*/
inline Vertex<LocalizedObjectPtr>* GetVertex(LocalizedObject* pObject)
{
return m_Vertices[pObject->GetUniqueId()];
}
/**
* Finds the closest scan in the vector to the given pose
* @param rScans scan
* @param rPose pose
*/
LocalizedLaserScan* GetClosestScanToPose(const LocalizedLaserScanList& rScans, const Pose2& rPose) const;
/**
* Adds an edge between the two objects and labels the edge with the given mean and covariance
* @param pFromObject from object
* @param pToObject to object
* @param rMean mean
* @param rCovariance match uncertainty
*/
void LinkObjects(LocalizedObject* pFromObject, LocalizedObject* pToObject, const Pose2& rMean, const Matrix3& rCovariance);
/**
* Finds nearby chains of scans and link them to scan if response is high enough
* @param pScan scan
* @param rMeans means
* @param rCovariances match uncertainties
*/
void LinkNearChains(LocalizedLaserScan* pScan, Pose2List& rMeans, List<Matrix3>& rCovariances);
/**
* Finds chains of scans that are close to given scan
* @param pScan scan
* @return chains of scans
*/
List<LocalizedLaserScanList> FindNearChains(LocalizedLaserScan* pScan);
/**
* Compute mean of poses weighted by covariances
* @param rMeans list of poses
* @param rCovariances uncertainties
* @return weighted mean
*/
Pose2 ComputeWeightedMean(const Pose2List& rMeans, const List<Matrix3>& rCovariances) const;
/**
* Tries to find a chain of scan from the given sensor starting at the
* given scan index that could possibly close a loop with the given scan
* @param pScan scan
* @param rSensorName name of sensor
* @param rStartScanIndex start index
* @return chain that can possibly close a loop with given scan
*/
LocalizedLaserScanList FindPossibleLoopClosure(LocalizedLaserScan* pScan, const Identifier& rSensorName, kt_int32u& rStartScanIndex);
/**
* Optimizes scan poses
*/
void CorrectPoses();
private:
/**
* Mapper of this graph
*/
OpenMapper* m_pOpenMapper;
/**
* Scan matcher for loop closures
*/
ScanMatcher* m_pLoopScanMatcher;
/**
* Traversal algorithm to find near linked scans
*/
GraphTraversal<LocalizedObjectPtr>* m_pTraversal;
}; // MapperGraph
////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////
/**
* Graph optimization algorithm
*/
class ScanSolver : public Referenced
{
public:
/**
* Vector of id-pose pairs
*/
typedef List<Pair<kt_int32s, Pose2> > IdPoseVector;
/**
* Default constructor
*/
ScanSolver()
{
}
protected:
//@cond EXCLUDE
/**
* Destructor
*/
virtual ~ScanSolver()
{
}
//@endcond
public:
/**
* Solve!
*/
virtual void Compute() = 0;
/**
* Gets corrected poses after optimization
* @return optimized poses
*/
virtual const IdPoseVector& GetCorrections() const = 0;
/**
* Adds a node to the solver
*/
virtual void AddNode(Vertex<LocalizedObjectPtr>* /*pVertex*/)
{
}
/**
* Removes a node from the solver
*/
virtual void RemoveNode(kt_int32s /*id*/)
{
}
/**
* Adds a constraint to the solver
*/
virtual void AddConstraint(Edge<LocalizedObjectPtr>* /*pEdge*/)
{
}
/**
* Removes a constraint from the solver
*/
virtual void RemoveConstraint(kt_int32s /*sourceId*/, kt_int32s /*targetId*/)
{
}
/**
* Resets the solver
*/
virtual void Clear() {};
}; // ScanSolver
////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////
/**
* Correlation grid used for scan matching
*/
class CorrelationGrid : public Grid<kt_int8u>
{
protected:
//@cond EXCLUDE
/**
* Destructor
*/
virtual ~CorrelationGrid()
{
delete [] m_pKernel;
}
//@endcond
public:
/**
* Creates a correlation grid of given size and parameters
* @param width width
* @param height height
* @param resolution resolution
* @param smearDeviation amount to smear when adding scans to grid
* @return correlation grid
*/
static CorrelationGrid* CreateGrid(kt_int32s width, kt_int32s height, kt_double resolution, kt_double smearDeviation)
{
assert(resolution != 0.0);
// +1 in case of roundoff
kt_int32u borderSize = GetHalfKernelSize(smearDeviation, resolution) + 1;
CorrelationGrid* pGrid = new CorrelationGrid(width, height, borderSize, resolution, smearDeviation);
return pGrid;
}
/**
* Gets the index into the data pointer of the given grid coordinate
* @param rGrid grid coordinate
* @param boundaryCheck whether to check the boundary of the grid
* @return grid index
*/
virtual kt_int32s GridIndex(const Vector2i& rGrid, kt_bool boundaryCheck = true) const
{
kt_int32s x = rGrid.GetX() + m_Roi.GetX();
kt_int32s y = rGrid.GetY() + m_Roi.GetY();
return Grid<kt_int8u>::GridIndex(Vector2i(x, y), boundaryCheck);
}
/**
* Get the region of interest (ROI)
* @return region of interest
*/
inline const Rectangle2<kt_int32s>& GetROI() const
{
return m_Roi;
}
/**
* Sets the region of interest (ROI)
* @param roi location of the ROI
*/
inline void SetROI(const Rectangle2<kt_int32s>& roi)
{
m_Roi = roi;
}
/**
* Smears cell if the cell at the given point is marked as "occupied"
* @param rGridPoint grid coordinate
*/
inline void SmearPoint(const Vector2i& rGridPoint)
{
assert(m_pKernel != NULL);
int gridIndex = GridIndex(rGridPoint);
if (GetDataPointer()[gridIndex] != GridStates_Occupied)
{
return;
}
kt_int32s halfKernel = m_KernelSize / 2;
// apply kernel
for (kt_int32s j = -halfKernel; j <= halfKernel; j++)
{
kt_int8u* pGridAdr = GetDataPointer(Vector2i(rGridPoint.GetX(), rGridPoint.GetY() + j));
kt_int32s kernelConstant = (halfKernel) + m_KernelSize * (j + halfKernel);
// if a point is on the edge of the grid, there is no problem
// with running over the edge of allowable memory, because
// the grid has margins to compensate for the kernel size
SmearInternal(halfKernel, kernelConstant, pGridAdr);
}
}
protected:
/**
* Constructs a correlation grid of given size and parameters
* @param width width
* @param height height
* @param borderSize size of border
* @param resolution resolution
* @param smearDeviation amount to smear when adding scans to grid
*/
CorrelationGrid(kt_int32u width, kt_int32u height, kt_int32u borderSize, kt_double resolution, kt_double smearDeviation)
: Grid<kt_int8u>(width + borderSize * 2, height + borderSize * 2)
, m_SmearDeviation(smearDeviation)
, m_pKernel(NULL)
{
GetCoordinateConverter()->SetScale(1.0 / resolution);
// setup region of interest
m_Roi = Rectangle2<kt_int32s>(borderSize, borderSize, width, height);
// calculate kernel
CalculateKernel();
}
/**
* Sets up the kernel for grid smearing
*/
virtual void CalculateKernel()
{
kt_double resolution = GetResolution();
assert(resolution != 0.0);
assert(m_SmearDeviation != 0.0);
// min and max distance deviation for smearing;
// will smear for two standard deviations, so deviation must be at least 1/2 of the resolution
const kt_double MIN_SMEAR_DISTANCE_DEVIATION = 0.5 * resolution;
const kt_double MAX_SMEAR_DISTANCE_DEVIATION = 10 * resolution;
// check if given value too small or too big
if (!math::InRange(m_SmearDeviation, MIN_SMEAR_DISTANCE_DEVIATION, MAX_SMEAR_DISTANCE_DEVIATION))
{
StringBuilder error;
error << "Mapper Error: Smear deviation too small: Must be between " << MIN_SMEAR_DISTANCE_DEVIATION << " and " << MAX_SMEAR_DISTANCE_DEVIATION;
throw Exception(error.ToString());
}
// NOTE: Currently assumes a two-dimensional kernel
// +1 for center
m_KernelSize = 2 * GetHalfKernelSize(m_SmearDeviation, resolution) + 1;
// allocate kernel
m_pKernel = new kt_int8u[m_KernelSize * m_KernelSize];
if (m_pKernel == NULL)
{
throw Exception("Unable to allocate memory for kernel!");
}
// calculate kernel
kt_int32s halfKernel = m_KernelSize / 2;
for (kt_int32s i = -halfKernel; i <= halfKernel; i++)
{
for (kt_int32s j = -halfKernel; j <= halfKernel; j++)
{
#ifdef WIN32
kt_double distanceFromMean = _hypot(i * resolution, j * resolution);
#else
kt_double distanceFromMean = hypot(i * resolution, j * resolution);
#endif
kt_double z = exp(-0.5 * pow(distanceFromMean / m_SmearDeviation, 2));
kt_int32u kernelValue = static_cast<kt_int32u>(math::Round(z * GridStates_Occupied));
assert(math::IsUpTo(kernelValue, static_cast<kt_int32u>(255)));
int kernelArrayIndex = (i + halfKernel) + m_KernelSize * (j + halfKernel);
m_pKernel[kernelArrayIndex] = static_cast<kt_int8u>(kernelValue);
}
}
}
/**
* Computes the kernel half-size based on the smear distance and the grid resolution.
* Computes to two standard deviations to get 95% region and to reduce aliasing.
* @param smearDeviation amount to smear when adding scans to grid
* @param resolution resolution
* @return kernel half-size based on the parameters
*/
static kt_int32s GetHalfKernelSize(kt_double smearDeviation, kt_double resolution)
{