File: ccIsolines.h

package info (click to toggle)
cloudcompare 2.10.1-2
  • links: PTS
  • area: main
  • in suites: buster
  • size: 55,916 kB
  • sloc: cpp: 219,837; ansic: 29,944; makefile: 67; sh: 45
file content (1080 lines) | stat: -rw-r--r-- 27,497 bytes parent folder | download | duplicates (3)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
#ifndef ISOLINES_HEADER
#define ISOLINES_HEADER

/**
*  Transcription of FindIsolines.java for C++
*
*  Fast implementation of marching squares
*
*  AUTHOR:   Murphy Stein, Greg Borenstein
*            New York University
*  CREATED:  Jan-Sept 2012 
*  MODIFIED: Dec 2014 (DGM)
*
*  LICENSE:  BSD
*
*  Copyright (c) 2012 New York University.
*  All rights reserved.
*
*  Redistribution and use in source and binary forms are permitted
*  provided that the above copyright notice and this paragraph are
*  duplicated in all such forms and that any documentation,
*  advertising materials, and other materials related to such
*  distribution and use acknowledge that the software was developed
*  by New York Univserity.  The name of the
*  University may not be used to endorse or promote products derived
*  from this software without specific prior written permission.
*  THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
*  IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
*  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
* 
*/

/**
* This is a fast implementation of the marching squares algorithm for finding isolines (lines of equal color) in an image.
* 
*/

//qCC_db
#include <ccLog.h>

//system
#include <assert.h>
#include <cmath>
#include <vector>

template< typename T > class Isolines
{
protected:
	std::vector<double> m_minx;
	std::vector<double> m_miny;
	std::vector<double> m_maxx;
	std::vector<double> m_maxy;

	std::vector<int>    m_cd;
	std::vector<double> m_contourX;
	std::vector<double> m_contourY;
	std::vector<int>    m_contourLength;
	std::vector<int>    m_contourOrigin;
	std::vector<int>    m_contourIndexes;
	std::vector<bool>   m_contourClosed;

	int m_w;
	int m_h;
	int m_numContours;

	T m_threshold;

public:

	//! Default constructor
	Isolines(int w, int h)
		: m_w(w)
		, m_h(h)
		, m_numContours(0)
		, m_threshold(0)
	{
		//DGM: as this is done in the constructor,
		//we don't catch the exception (so that the
		//caller can properly handle the error!)
		//try
		{
			m_cd.resize(w*h,0);
		}
		//catch (const std::bad_alloc&)
		//{
		//	//not enough memory!
		//}
	}

	//! Sets isoline value to trace
	inline void setThreshold(T t) { m_threshold = t; }

	//! Find isolines
	int find(const T* in)
	{
		//createOnePixelBorder(in, m_threshold + 1); //DGM: modifies 'in', the user will have to do it himself :(
		preCodeImage(in);
		return findIsolines(in);
	}

	//! Returns the number of found contours
	inline int getNumContours() const { return m_numContours; }

	//! Returns the length of a given contour
	inline int getContourLength(int contour) const { return m_contourLength[contour]; }

	//! Returns whether a given contour is closed or not
	inline bool isContourClosed(int contour) const { return m_contourClosed[contour]; }

	//! Returns the given point (x,y) of a given contour
	void getContourPoint(int contour, size_t index, double& x, double& y) const
	{
		assert(static_cast<int>(index) < getContourLength(contour));
		x = getContourX(contour, index);
		y = getContourY(contour, index);
	}

	//! Measures the area delineated by a given contour
	inline double measureArea(int contour) const { return measureArea(contour, 0, getContourLength(contour)); }

	//! Measures the perimeter of a given contour
	inline double measurePerimeter(int contour) const { return measurePerimeter(contour, 0, getContourLength(contour)); }

	//! Creates a single pixel, 0-valued border around the grid
	void createOnePixelBorder(T* in, T borderval) const
	{
		//rows
		{
			int shift = (m_h-1) * m_w;
			for (int i=0; i<m_w; i++)
			{
				in[i] = in[i+shift] = borderval;
			}
		}
		//columns
		{
			for (int j=0; j<m_h; j++)
			{
				in[j*m_w] = in[(j+1)*m_w-1] = borderval;
			}
		}
	}

protected:

	//! Computes a code for each group of 4x4 cells
	/** The code depends only on whether each of four corners is
		above or below threshold.
	**/
	void preCodeImage(const T* in)
	{
		for (int x = 0; x < m_w-1; x++)
		{
			for (int y = 0; y < m_h-1; y++)
			{
				int b0(in[ixy(x + 0, y + 1)] < m_threshold ? 1 : 0); //bottom-left is below threshold
				int b1(in[ixy(x + 1, y + 1)] < m_threshold ? 2 : 0); //bottom-right is below threshold
				int b2(in[ixy(x + 1, y + 0)] < m_threshold ? 4 : 0); //top-right is below threshold
				int b3(in[ixy(x + 0, y + 0)] < m_threshold ? 8 : 0); //top-left is below threshold
				m_cd[ixy(x, y)] = b0 | b1 | b2 | b3;
			}
		} 
	}

	//! 2x2 cell configuration codes
	enum ConfigurationCodes
	{
		CASE0   =  0, 
		CASE1   =  1,
		CASE2   =  2,
		CASE3   =  3,
		CASE4   =  4,
		CASE5   =  5,
		CASE6   =  6,
		CASE7   =  7,
		CASE8   =  8,
		CASE9   =  9,
		CASE10  = 10,
		CASE11  = 11,
		CASE12  = 12,
		CASE13  = 13,
		CASE14  = 14,
		CASE15  = 15,
		VISITED = 16
	};

	//! Entry/exit edges
	enum Edges
	{
		NONE   = -1,
		TOP    =  0,
		RIGHT  =  1,
		BOTTOM =  2,
		LEFT   =  3
	};

	void endContour(bool closed, bool alternatePath)
	{
		if (alternatePath)
		{
			//we have to merge this path with the previous one!
			//try //will be taken care of by 'findIsolines'
			//{
				size_t length = m_contourLength.back();
				size_t firstIndex = m_contourOrigin.back();
				m_contourLength.pop_back();
				m_contourOrigin.pop_back();
				m_contourClosed.pop_back();

				//backup the alternate part of the contour
				std::vector<double> subContourX(length), subContourY(length);
				std::vector<int> subContourIndexes(length);
				{
					for (size_t i=0; i<length; ++i)
					{
						subContourX[i] = m_contourX[firstIndex+i];
						subContourY[i] = m_contourY[firstIndex+i];
						subContourIndexes[i] = m_contourIndexes[firstIndex+i];
					}
				}

				assert(!m_contourLength.empty() && !m_contourOrigin.empty());
				size_t length0 = m_contourLength.back();
				size_t firstIndex0 = m_contourOrigin.back();

				//shift the first part values
				{
					for (int i=static_cast<int>(length0); i>=0; --i) //we start by end so as to not overwrite values!
					{
						m_contourX[firstIndex0+length+i] = m_contourX[firstIndex0+i];
						m_contourX[firstIndex0+length+i] = m_contourY[firstIndex0+i];
						m_contourIndexes[firstIndex0+length+i] = m_contourIndexes[firstIndex0+i];
					}
				}

				//now copy the second part values
				{
					for (size_t i=0; i<length; ++i)
					{
						m_contourX[firstIndex0+i] = subContourX[i];
						m_contourY[firstIndex0+i] = subContourY[i];
						m_contourIndexes[firstIndex0+i] = subContourIndexes[i];
					}
				}

				//even though we are merging contours, if we are here it
				//means that the contour was not a proper loop!
				closed = false;
				assert(!m_contourClosed.empty());
				m_contourClosed.back() = false;
			//}
			//catch (const std::bad_alloc&)
			//{
			//	return false;
			//}
		}

		//simply update the closed state (just to be sure)
		assert(!m_contourClosed.empty());
		assert(!closed || m_contourLength.back() > 2);
		m_contourClosed.back() = closed;

		//return true;
	}

	//! Searches image for contours from topleft to bottomright corners
	int findIsolines(const T* in)
	{
		//traversal case
		static const int TRAVERSAL[4]           = { /*TOP=*/BOTTOM,  /*RIGHT=*/LEFT,   /*BOTTOM=*/TOP,   /*LEFT=*/RIGHT   };
		//disambiguation cases (CASES 5 and 10)
		static const int DISAMBIGUATION_UP  [4] = { /*TOP=*/LEFT,    /*RIGHT=*/BOTTOM, /*BOTTOM=*/RIGHT, /*LEFT=*/TOP     };
		static const int DISAMBIGUATION_DOWN[4] = { /*TOP=*/RIGHT,   /*RIGHT=*/TOP,    /*BOTTOM=*/LEFT,  /*LEFT=*/BOTTOM  };

		m_contourX.clear();
		m_contourY.clear();
		m_contourLength.clear();
		m_contourOrigin.clear();
		m_contourIndexes.clear();
		m_contourClosed.clear();

		try
		{
			int toEdge = NONE;
			int cellIndex = -1;
			int x = 0, y = 0;

			//mechanism for merging two parts of a non-closed contour
			int altToEdge = NONE;
			int altStartIndex = 0;
			bool alternatePath = false;

			const int maxCellIndex = m_w * (m_h-1); //DGM: last line is only 0!
			while (cellIndex < maxCellIndex)
			{
				//entry edge
				int fromEdge = (toEdge == NONE ? NONE : TRAVERSAL[toEdge]);
				//exit edge
				toEdge = NONE;
				//alternative exit edge (when starting a new contour)
				if (fromEdge == NONE)
					altToEdge = NONE;

				int currentCellIndex = -1;

				//last isoline is 'finished'
				if (fromEdge == NONE)
				{
					//do we have an alternate path?
					if (altToEdge != NONE && !alternatePath)
					{
						//we know that we are coming from the TOP (case 2 or 13)
						fromEdge = TOP;
						currentCellIndex = altStartIndex + m_w; //same coumn, next row
						alternatePath = true;
						//we start a new (temporary) contour
						m_contourLength.push_back(0);
						m_contourClosed.push_back(false);
						m_contourOrigin.push_back(static_cast<int>(m_contourX.size())+1);
					}
					else
					{
						// we have to look for a new starting point
						alternatePath = false;
						altToEdge = NONE;
						//skip empty cells
						while (	++cellIndex < maxCellIndex
							&&	(m_cd[cellIndex] == CASE0 || m_cd[cellIndex] == CASE15) )
						{
						}
						if (cellIndex == maxCellIndex)
							break;
						currentCellIndex = cellIndex;
					}
					x = currentCellIndex % m_w;
					y = currentCellIndex / m_w;
				}

				//we have reached a border (bottom or right)
				if (x < 0 || x >= m_w-1 || y < 0 || y >= m_h-1)
				{
					//if an isoline was underway, it won't be closed!
					if (fromEdge != NONE)
					{
						endContour(false,alternatePath);
					}
					//toEdge = NONE;
					continue;
				}
				else if (fromEdge != NONE)
				{
					//isoline underway: we have to re-evaluate the
					//current position in the grid
					currentCellIndex = ixy(x,y);
				}

				assert(currentCellIndex >= 0);
				int& currentCode = m_cd[currentCellIndex];
				switch (currentCode)
				{
				case CASE0:									// CASE 0
					//we have reached a border!
					//if an isoline was underway, it won't be closed!
					if (fromEdge != NONE)
					{
						endContour(false,alternatePath);
					}
					//toEdge = NONE;
					continue;

				case CASE1:									// CASE 1
				case CASE14:								// CASE 14
					toEdge = (fromEdge == NONE || fromEdge == LEFT ? BOTTOM : LEFT);
					currentCode |= VISITED;
					break;

				case CASE2:									// CASE 2
				case CASE13:								// CASE 13
					//if it's a new contour, we have 2 options (RIGHT and BOTTOM)
					if (fromEdge == NONE)
					{
						if (x >= m_w-2) //can't go on the right!
						{
							if (y >= m_h-2) //can't go lower
							{
								//toEdge = NONE;
								continue;
							}
							else
							{
								toEdge = BOTTOM;
							}
						}
						else
						{
							//go right by default
							toEdge = RIGHT;
							if (y < m_h-2)
							{
								//if we can go lower, rembemr this as an alternate rout
								altToEdge = BOTTOM;
								altStartIndex = currentCellIndex;
							}
						}
					}
					else
					{
						toEdge = (fromEdge == BOTTOM ? RIGHT : BOTTOM);
					}
					currentCode |= VISITED;
					break;

				case CASE3:									// CASE 3
				case CASE12:								// CASE 12
					toEdge = (fromEdge == NONE || fromEdge == LEFT ? RIGHT : LEFT);
					currentCode |= VISITED;
					break;

				case CASE11:								// CASE 11
					//assert(fromEdge != NONE);
				case CASE4:									// CASE 4
					toEdge = (fromEdge == NONE || fromEdge == TOP ? RIGHT : TOP);
					currentCode |= VISITED;
					break;

				case CASE5:									// CASE 5, saddle
				case CASE10:								// CASE 10, saddle
					{
						if (fromEdge != NONE)
						{
							//check if we are not looping as the saddle points can't be flagged as VISITED!
							assert(!m_contourOrigin.empty() && !m_contourIndexes.empty());
							if (m_contourIndexes[m_contourOrigin.back()] == currentCellIndex)
							{
								//isoline loop is closed!
								assert(!alternatePath);
								endContour(true,false);
								//no need to look at the alternate path!
								altToEdge = NONE;
								//toEdge = NONE;
								continue;
							}
						}

						double avg = (	in[ixy(x + 0, y + 0)]
									+	in[ixy(x + 1, y + 0)]
									+	in[ixy(x + 0, y + 1)]
									+	in[ixy(x + 1, y + 1)] ) / 4.0;

						//see http://en.wikipedia.org/wiki/Marching_squares for the disambiguation cases
						bool down = (	(currentCode == CASE5  && avg <  m_threshold)
									||	(currentCode == CASE10 && avg >= m_threshold) );
						if (down)
						{
							if (fromEdge == NONE)
							{
								//if we start here, we know that some routes have already been taken (the ones coming from UP or LEFT)
								//we can ignore the cell
								continue;
							}
							else
							{
								toEdge = DISAMBIGUATION_DOWN[fromEdge];
							}
						}
						else
						{
							if (fromEdge == NONE)
							{
								//if we start here, we know that some routes have already been taken (the ones coming from UP or LEFT)
								//it can only be on the right
								if (x < m_w-2)
								{
									if (m_cd[currentCellIndex+1] < VISITED)
										toEdge = RIGHT;
									else
										//we can ignore the cell
										continue;
								}
								else
								{
									//we can ignore the cell
									continue;
								}
							}
							else
							{
								toEdge = DISAMBIGUATION_UP[fromEdge];
							}
						}
					}
					break;

				case CASE6:									// CASE 6
					//assert(fromEdge != NONE);
				case CASE9:									// CASE 9
					toEdge = (fromEdge == NONE || fromEdge == TOP ? BOTTOM : TOP);
					currentCode |= VISITED;
					break;

				case CASE7:									// CASE 7
					//assert(fromEdge != NONE);
				case CASE8:									// CASE 8
					toEdge = (fromEdge == NONE || fromEdge == LEFT ? TOP : LEFT);
					currentCode |= VISITED;
					break;

				case CASE15:								// CASE 15
					//assert(fromEdge == NONE); //apart at the very beginning!
					//toEdge = NONE;
					continue;

				default:
					assert(currentCode > 0 && ((currentCode & VISITED) == VISITED));
					if (fromEdge != NONE)
					{
						//check that we are indeed coming back to the start
						assert(!m_contourOrigin.empty() && !m_contourIndexes.empty());
						if (m_contourIndexes[m_contourOrigin.back()] == currentCellIndex)
						{
							//isoline loop is closed!
							assert(!alternatePath);
							endContour(true,false);
							//no need to look at the alternate path!
							altToEdge = NONE;
						}
						else
						{
							//have we reached a kind of tri-point? (DGM: not sure it's possible)
							endContour(false,alternatePath);
						}
					}
					//toEdge = NONE;
					continue;
				}

				assert(toEdge != NONE);
				
				if (fromEdge == NONE)
				{
					// starting a new contour
					m_contourLength.push_back(0);
					m_contourClosed.push_back(false);
					m_contourOrigin.push_back(static_cast<int>(m_contourX.size()));
					//ccLog::Print(QString("New contour: #%1 - origin = %2 - (x=%3, y=%4)").arg(m_contourLength.size()).arg(m_contourOrigin.back()).arg(x).arg(y));
				}

				double x2 = 0.0, y2 = 0.0;
				switch (toEdge)
				{
				case TOP: 
					x2 = x + LERP(in[ixy(x + 0, y + 0)], in[ixy(x + 1, y + 0)]);
					y2 = y;
					y--;
					break;
				case RIGHT:
					x2 = x + 1;
					y2 = y + LERP(in[ixy(x + 1, y + 0)], in[ixy(x + 1, y + 1)]);
					x++;
					break;
				case BOTTOM:
					x2 = x + LERP(in[ixy(x + 0, y + 1)], in[ixy(x + 1, y + 1)]);
					y2 = y + 1;
					y++;
					break;
				case LEFT:
					x2 = x;
					y2 = y + LERP(in[ixy(x + 0, y + 0)], in[ixy(x + 0, y + 1)]);
					x--;
					break;
				default:
					assert(false);
					continue;
				}

				//if (m_contourLength.back() > 1)
				//{
				//	size_t vertCount = m_contourX.size();
				//	const double& x0 = m_contourX[vertCount - 2];
				//	const double& y0 = m_contourY[vertCount - 2];
				//	double& x1 = m_contourX.back();
				//	double& y1 = m_contourY.back();
				//	double ux = x1 - x0;
				//	double uy = y1 - y0;
				//	double vx = x2 - x0;
				//	double vy = y2 - y0;
				//	//test colinearity so as to merge both segments if possible
				//	double dotprod = (ux*vx + uy*vy) / sqrt((vx*vx + vy*vy) * (ux*ux + uy*uy));
				//	if (fabsl(dotprod - 1.0) < 1.0e-6)
				//	{
				//		//merge: we replace the last vertex by this one
				//		x1 = x2;
				//		y1 = y2;
				//		m_contourIndexes.back() = currentCellIndex;
				//	}
				//	else
				//	{
				//		//new vertex
				//		m_contourX.push_back(x2);
				//		m_contourY.push_back(y2);
				//		m_contourIndexes.push_back(currentCellIndex);
				//		m_contourLength.back()++;
				//	}
				//}
				//else
				{
					//new vertex
					m_contourX.push_back(x2);
					m_contourY.push_back(y2);
					m_contourIndexes.push_back(currentCellIndex);
					m_contourLength.back()++;
				}
			}
		}
		catch (const std::bad_alloc&)
		{
			//not enough memory
			m_contourX.clear();
			m_contourY.clear();
			m_contourLength.clear();
			m_contourIndexes.clear();
			return -1;
		}

		m_numContours = static_cast<int>(m_contourLength.size());

		computeBoundingBoxes();

		return m_numContours;
	}

	//! LERP between two values
	inline double LERP(T A, T B) const
	{
		T AB = A-B;
		return AB == 0 ? 0 : static_cast<double>(A - m_threshold) / AB;
	}

	inline int getLastIndex() const
	{
		int nc = getNumContours();
		return nc > 0 ? m_contourOrigin[nc - 1] + m_contourLength[nc - 1] : 0;
	}

	inline void setContourX(int contour, int v, double x)
	{
		int o = m_contourOrigin[contour];
		m_contourX[wrap(o + v, o, o + m_contourLength[contour])] = x;
	}

	inline void setContourY(int contour, int v, double y)
	{
		int o = m_contourOrigin[contour];
		m_contourY[wrap(o + v, o, o + m_contourLength[contour])] = y;
	}

	inline int getValidIndex(int contour, int v) const
	{
		int o = m_contourOrigin[contour];
		return wrap(o + v, o, o + m_contourLength[contour]);
	}

	double measureArea(int contour, int first, int last) const
	{
		double area = 0;
		if (getValidIndex(contour, first) == getValidIndex(contour, last))
			last = last - 1;
		
		double w = 0, h = 0;
		for (int i = first; i < last ; i++)
		{
			w = getContourX(contour, i + 1) - getContourX(contour, i);
			h = (getContourY(contour, i + 1) + getContourY(contour, i)) / 2.0;
			area += w * h;
		}
		w = getContourX(contour, first) - getContourX(contour, last);
		h = (getContourY(contour, first) + getContourY(contour, last)) / 2.0;
		area += w * h;
		
		return area;
	}

	double measureMeanX(int contour) const
	{
		double mean = 0.0;
		int l = getContourLength(contour);
		for (int i = 0; i < l; i++)
			mean += getContourX(contour, i);
		return (l == 0 ? 0 : mean / l);
	}

	double measureMeanY(int contour) const
	{
		double mean = 0.0;
		int l = getContourLength(contour);
		for (int i = 0; i < l; i++)
			mean += getContourY(contour, i);
		return (l == 0 ? 0 : mean / l);
	}

	double measurePerimeter(int contour, int first, int last)  const
	{
		if (getValidIndex(contour, first) == getValidIndex(contour, last))
			last = last - 1;
		
		double perim = 0;
		for (int i = first; i < last ; i++)
			perim += measureLength(contour, i);
		perim += measureDistance(contour,first,last);
		
		return perim;
	}

	double measureNormalX(int contour, int i) const
	{
		double ret = getContourY(contour, i) - getContourY(contour, i + 1);
		ret = ret / measureLength(contour, i);
		return ret;
	}

	double measureNormalY(int contour, int i) const
	{
		double ret = getContourX(contour, i + 1) - getContourX(contour, i);
		ret = ret / measureLength(contour, i);
		return ret;
	}

	double measureNormalY(int contour, int first, int last) const
	{
		double ret = 0;
		for (int i = first; i < last ; i++)
			ret += measureNormalY(contour, i);
		return ret;
	}

	double measureNormalX(int contour, int first, int last) const
	{
		double ret = 0;
		for (int i = first; i < last ; i++)
			ret += measureNormalX(contour, i);
		return ret;
	}

	double measureAngleChange(int contour, int first, int last) const
	{
		double sum = 0;
		for (int i = first; i <= last; i++)
			sum += measureAngle(contour, i);
		return sum;
	}

	static int wrap(int i, int lo, int hi)
	{
		int l = hi - lo;
		int d = i - lo;
		int w = 0;
		if (d < 0)
			w = hi - ((-d) % l);
		else
			w = lo + (d % l);
		
		if (w == hi)
			w = lo;
		
		if (w < lo)
		{
			assert(false);
			printf("went below lo\n");
		}
		else if (w >= hi)
		{
			assert(false);
			printf("went above hi\n");
		}
		
		return w;
	}

	inline int ixy(int x, int y) const { return x + y * m_w; }

	inline double measureDistance(int contour, int first, int second) const
	{
		double dx = getContourX(contour, first) - getContourX(contour, second);
		double dy = getContourY(contour, first) - getContourY(contour, second);
		return std::sqrt(dx * dx + dy * dy);
	}

	// return length from i to i + 1
	double measureLength(int contour, int i) const
	{
		int lo = m_contourOrigin[contour];
		int n  = m_contourLength[contour];
		int hi = lo + n;

		int v1 = wrap(lo + i+0, lo, hi);
		int v2 = wrap(lo + i+1, lo, hi);

		double aftx = m_contourX[v2] - m_contourX[v1];
		double afty = m_contourY[v2] - m_contourY[v1];

		return std::sqrt(aftx * aftx + afty * afty);
	}

	// return the relative angle change in radians
	// about the point i (assuming ccw is positive)
	double measureAngle(int contour, int i) const
	{
		double befx = getContourX(contour, i + 0) - getContourX(contour, i - 1);
		double befy = getContourY(contour, i + 0) - getContourY(contour, i - 1);
		double aftx = getContourX(contour, i + 1) - getContourX(contour, i + 0);
		double afty = getContourY(contour, i + 1) - getContourY(contour, i + 0);

		double befl = std::sqrt(befx * befx + befy * befy); 
		befx /= befl;
		befy /= befl;
		double aftl = std::sqrt(aftx * aftx + afty * afty);
		aftx /= aftl;
		afty /= aftl;		

		double dot = befx * aftx + befy * afty;
		if (dot > 1.0)
			dot = 1.0;
		else if (dot < 0)
			dot = 0;
		double rads = std::acos(dot);
		assert(rads == rads); //otherwise it means that rads is NaN!!!

		if (aftx * befy - afty * befx < 0)
			rads = -rads;

		return rads;
	}

public:

	inline double getContourX(int contour, int v) const
	{
		int o = m_contourOrigin[contour];
		return m_contourX[wrap(o + v, o, o + m_contourLength[contour])];
	}

	inline double getContourY(int contour, int v) const
	{
		int o = m_contourOrigin[contour];
		return m_contourY[wrap(o + v, o, o + m_contourLength[contour])];
	}

	inline double measureCurvature(int contour, int i) const
	{
		return measureAngle(contour, i) / measureLength(contour, i);
	}

	void findAreas(int window, std::vector<double>& tips)
	{
		tips.resize(m_w * m_h);

		for (int k = 0; k < m_numContours; k++)
		{
			int l = getContourLength(k);
			for (int i = 0; i < l; i++)
			{
				int lo = i - window;
				int hi = i + window;
				tips[getValidIndex(k, i)] = measureArea(k, lo, hi);
			}
		}
	}

	void findRoundedCorners(int window, std::vector<double>& tips)
	{
		tips.resize(m_w * m_h);

		for (int k = 0; k < m_numContours; k++)
		{
			int l = getContourLength(k);
			for (int i = 0; i < l; i++)
			{
				int lo = i - window;
				int hi = i + window;
				tips[getValidIndex(k, i)] = measureArea(k, lo, hi) / measurePerimeter(k, lo, hi);
			}
		}
	}

	int getMaxContour() const
	{
		int maxlength = 0;
		int idx = 0;
		for (int k = 0; k < m_numContours; k++)
		{
			int l = getContourLength(k);
			if (l > maxlength)
			{
				maxlength = l;
				idx = k;
			}
		}
		return idx;
	}

	// POLYGON HIT TESTING ROUTINES

	bool computeBoundingBoxes()
	{
		int numContours = getNumContours();
		if (numContours == 0)
		{
			m_minx.clear();
			m_miny.clear();
			m_maxx.clear();
			m_maxy.clear();
			return true;
		}

		try
		{
			m_minx.resize(numContours);
			m_miny.resize(numContours);
			m_maxx.resize(numContours);
			m_maxy.resize(numContours);
		}
		catch (const std::bad_alloc&)
		{
			//not enough memory!
			return false;
		}

		for (int k = 0; k < numContours; k++)
		{
			int o = m_contourOrigin[k];
			m_minx[k] = m_contourX[o];
			m_miny[k] = m_contourY[o];
			m_maxx[k] = m_contourX[o];
			m_maxy[k] = m_contourY[o];
			for (int i = 1; i < getContourLength(k); i++)
			{
				int j = o + i;
				if (m_contourX[j] < m_minx[k]) 
					m_minx[k] = m_contourX[j];
				else if (m_contourX[j] > m_maxx[k]) 
					m_maxx[k] = m_contourX[j];
				if (m_contourY[j] < m_miny[k]) 
					m_miny[k] = m_contourY[j];
				else if (m_contourY[j] > m_maxy[k]) 
					m_maxy[k] = m_contourY[j];
			}
		}

		return true;
	}

	inline double getBBMinX(int contour) const { return m_minx[contour]; }
	inline double getBBMaxX(int contour) const { return m_maxx[contour]; }
	inline double getBBMinY(int contour) const { return m_miny[contour]; }
	inline double getBBMaxY(int contour) const { return m_maxy[contour]; }

	bool contains(int k, double x, double y) const
	{
		bool inside = false;
		int l = getContourLength(k);
		for (int i = 0, j = -1; i < l; j = i++)
		{
			double yi = getContourY(k, i);
			double yj = getContourY(k, j);
			if ((yj > y) != (yi > y))
			{
				double xi = getContourX(k, i);
				double xj = getContourX(k, j);
				if (yi == yj)
				{
					if (x < xi)
						inside = !inside;
				}
				else if (x < xi + (y - yi) * (xi - xj) / (yi - yj))
				{
					inside = !inside;
				}
			}
		}
		return inside;
	}

	bool containsContour(int k1, int k2)
	{
		if (!bbIntersect(k1, k2))
			return false;

		double minx = getBBMinX(k2);
		double maxx = getBBMaxX(k2);
		double miny = getBBMinY(k2);
		double maxy = getBBMaxY(k2);

		return (	contains(k1, minx, miny)
			&&	contains(k1, maxx, miny)
			&&	contains(k1, maxx, maxy)
			&&	contains(k1, minx, maxy) );
	}

	inline bool containsBoundingBox(int k, double minx, double miny, double maxx, double maxy) const
	{
		return (	contains(k, minx, miny)
				&&	contains(k, maxx, miny)
				&&	contains(k, maxx, maxy)
				&&	contains(k, minx, maxy) );
	}

	bool contains(const std::vector<double>& polyx, const std::vector<double>& polyy, double x, double y) const
	{
		bool inside = false;
		size_t l = polyx.size();
		if (l < 1)
			return false;
		for (size_t i = 0, j = l - 1; i < l; j = i++)
		{
			double yi = polyy[i];
			double yj = polyy[j];
			if ((yj > y) != (yi > y))
			{
				double xi = polyx[i];
				double xj = polyx[j];
				if (yi == yj)
				{
					if (x < xi)
						inside = !inside;
				}
				else if (x < xi + (y - yi) * (xi - xj) / (yi - yj))
				{
					inside = !inside;
				}
			}
		}
		return inside;
	}

	// intersects contour k1 with contour k2
	bool bbIntersect(int k1, int k2) const
	{
		double minx1 = getBBMinX(k1);
		double maxx1 = getBBMaxX(k1);
		double miny1 = getBBMinY(k1);
		double maxy1 = getBBMaxY(k1);
		double minx2 = getBBMinX(k2);
		double maxx2 = getBBMaxX(k2);
		double miny2 = getBBMinY(k2);
		double maxy2 = getBBMaxY(k2);

		double lt = minx1 > minx2 ? minx1 : minx2;
		double rt = maxx1 < maxx2 ? maxx1 : maxx2;
		double tp = miny1 > miny2 ? miny1 : miny2;
		double bt = maxy1 < maxy2 ? maxy1 : maxy2;

		return (lt < rt && tp < bt);
	}

	bool bbContainsBB(int k1, int k2) const
	{
		double minx1 = getBBMinX(k1);
		double maxx1 = getBBMaxX(k1);
		double miny1 = getBBMinY(k1);
		double maxy1 = getBBMaxY(k1);
		double minx2 = getBBMinX(k2);
		double maxx2 = getBBMaxX(k2);
		double miny2 = getBBMinY(k2);
		double maxy2 = getBBMaxY(k2);

		return (minx1 <= minx2 && maxx1 >= maxx2 && miny1 <= miny2 && maxy1 >= maxy2);
	}

	inline double bbArea(int k) const
	{
		double w = getBBMaxX(k) - getBBMinX(k);
		double h = getBBMaxY(k) - getBBMinY(k);
		return w * h;
	}

	inline double getBBCenterX(int k) const { return (getBBMinX(k) + getBBMaxX(k)) / 2.0; }
	inline double getBBCenterY(int k) const { return (getBBMinY(k) + getBBMaxY(k)) / 2.0; }
};

#endif //ISOLINES_HEADER