Blame view

legacy/rtsImplicit3D.h 68.4 KB
f1402849   dmayerich   renewed commit
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
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
  #ifndef RTSIMPLICIT3D_H

  #define RTSIMPLICIT3D_H

  

  #define DIST_MAX	255

  

  #include "rtsLinearAlgebra.h"

  #include "rtsDTGrid3D.h"

  #include <fstream>

  #include <iostream>

  #include <math.h>

  #include <queue>

  #include <algorithm>

  using namespace std;

  

  typedef int indextype;

  

  ///This class represents a 3D implicit function as a grid.  It provides methods for accessing values, interpolation, and several utilities.

  

  template <class T> class rtsImplicit3D

  {

  private:

  	//pointer to store the data

  	T* m_data;

  	//resolution of the data (x, y, z) dimensional extents

  	vector3D<indextype> m_resolution;

  	T m_boundary;			//boundary condition

  	point3D<double> m_domain_min;	//min and max range values (used for parametric access)

  	point3D<double> m_domain_max;

  	vector3D<double> m_voxel_size;

  	

  	//bit-blit function copies 3D data quickly from source to dest

  	void blit3D(const T* source,

  				   indextype s_px, indextype s_py, indextype s_pz,

  				   indextype s_sx, indextype s_sy, indextype s_sz,

  				   T* dest,

  				   indextype d_px, indextype d_py, indextype d_pz,

  				   indextype d_sx, indextype d_sy, indextype d_sz,

  				   indextype blit_size_x, indextype blit_size_y, indextype blit_size_z);

  

  	void shallow_copy(const rtsImplicit3D<T> source, rtsImplicit3D<T> &dest);

  	inline point3D<double> getParameter(indextype i);

  

  	inline float isosurface_distance(point3D<double> p0, point3D<double> p1, T isovalue);

  	inline float manhattan_distance(rtsImplicit3D<float>* function, point3D<indextype> p, bool sdf = false);

  	void compute_distance_function_boundary(T isovalue, rtsImplicit3D<float>* &result, rtsImplicit3D<bool>* &mask, bool sdf = false);

  

  

  public:

  	//construct an implicit function with a size of 1

  	rtsImplicit3D();			///<Create an empty implicit function

  	//construct an implicit function of the specified resolution

  	rtsImplicit3D(indextype res_x, indextype res_y, indextype res_z);	///<Create an implicit function with the specified resolution

  	//construct an implicit function from sample data and a specified size

  	rtsImplicit3D(T* data, indextype res_x, indextype res_y, indextype res_z);	///<Create an implicit function from previous data at the specified resolution

  	//shallow-copy constructor, defines all shallow variables

  	rtsImplicit3D(vector3D<int> resolution, T boundary, point3D<double> min_domain, point3D<double> max_domain);

  	//full copy constructor, defines all variables

  	rtsImplicit3D(T* data, vector3D<int> resolution, T boundary, point3D<double> min_domain, point3D<double> max_domain);

  	rtsImplicit3D(const rtsImplicit3D<T> &original);	//copy constructor

  	~rtsImplicit3D();		//destructor

  

  	//overloaded operators

  	rtsImplicit3D<T>& operator=(const rtsImplicit3D<T>& original);		///<Overloaded operator creates a copy of an implicit function

  	rtsImplicit3D<T>& operator=(const T constant);						///<Overloaded operator sets all points in an implicit function to the given constant value

  	inline T& operator()(indextype x, indextype y, indextype z);		///<Allows access to the sample point indexed by x, y, and z using the parenthesis operator

  	inline T operator()(double i, double j, double k);					///<Allows access to the implicit function (based on the domain boundaries) at the position (i, j, k).  This class uses linear interpolation.

  	rtsImplicit3D<T>& operator*=(const T constant);						///<Multiplies the values at all sample points by a constant.

  	rtsImplicit3D<T>& operator+=(const T constant);						///<Adds a constant to the values at all sample points.

  	rtsImplicit3D<T>& operator-=(const T constant);						///<Subtracts a constant from the values at all sample points.

  	rtsImplicit3D<T>& operator/=(const T constant);						///<Divides all values by a constant.

  	const rtsImplicit3D<T> operator+(const T constant);					///<Adds a constant to an implicit function and returns a new function.

  	const rtsImplicit3D<T> operator-(const T constant);					///<Subtracts a constant from an implicit function and returns a new function.

  	const rtsImplicit3D<T> operator*(const T constant);					///<Multiplies an implicit function by a constant and returns a new function.

  	const rtsImplicit3D<T> operator/(const T constant);					///<Divides an implicit function by a constant and returns a new function.

  

  	//casting operator

  	//template <class U> friend class rtsImplicit3D<U>;

  	template <class U> operator rtsImplicit3D<U>();						///<Casts between data types.

  

  	//friend classes for overloading "backwards" operations (like 3*function)

  	friend rtsImplicit3D<T> operator*(const T lhs, rtsImplicit3D<T> rhs){return rhs*lhs;}	///<Allows associative multiplication.

  	friend rtsImplicit3D<T> operator+(const T lhs, rtsImplicit3D<T> rhs){return rhs+lhs;}	///<Allows associative addition.

  	friend rtsImplicit3D<T> operator-(const T lhs, rtsImplicit3D<T> rhs)					///<Allows associative subtraction.

  	{

  		rtsImplicit3D<T> result;

  		rhs.shallow_copy(rhs, result);	//make a copy of all of the shallow variables and allocate memory

  		indextype size = rhs.m_resolution.x * rhs.m_resolution.y * rhs.m_resolution.z;

  		//iterate and subtract

  		for(indextype i=0; i<size; i++)

  			result.m_data[i] = lhs - rhs.m_data[i];

  

  		return result;

  	}

  	//friend rtsImplicit3D<T> operator/(const T lhs, rtsImplicit3D<T> rhs);

  

  	//loading/saving data to disk

  	void LoadRAW(indextype header_size, indextype data_x, indextype data_y, indextype data_z, const char* filename);	///<Loads RAW data from a file with the specified header size and data size.

  	void SaveRAW(const char* filename);							///<Save the data as RAW data to disk.

  	void LoadVOL(const char* filename);							///<Load a VOL file from disk.

  	void SaveVOL(const char* filename);							///<Save a VOL file to disk.

  

  	//data access methods

  	inline T& xyz(indextype x, indextype y, indextype z);

  	inline T ijk(double i, double j, double k);

  	void Parameterize(double x_min, double x_max, double y_min, double y_max, double z_min, double z_max);

  	void setBoundary(T boundary){m_boundary = boundary;}

  	T getBoundary(){return m_boundary;}

  	T* GetBits();

  	indextype DimX(){return m_resolution.x;}

  	indextype DimY(){return m_resolution.y;}

  	indextype DimZ(){return m_resolution.z;}

  	inline point3D<double> getParameter(indextype x, indextype y, indextype z);

  	inline point3D<indextype> getNearestIndex(double i, double j, double k);

  	inline point3D<double> getFractionalIndex(double i, double j, double k);

  	inline point3D<indextype> getNearestIndex(indextype i);

  	point3D<double> getMinDomain(){return m_domain_min;}

  	point3D<double> getMaxDomain(){return m_domain_max;}

  	vector<point3D<indextype>> getEdgeNodes(T isovalue, bool protrusions = true); ///<Returns a vector nodes lying on the edge of an implicit surface.

  	rtsImplicit3D<T> Project2D();			//<Projects the data along the z-axis using a maximum-intensity projection.

  	unsigned int BackgroundComponents6(indextype x, indextype y, indextype z, T threshold, int n=18);	///<Returns the number of background components 6-connected to the specified node.

  	unsigned int Neighbors6(indextype x, indextype y, indextype z, T threshold); //<Returns the number of 6-connected neighbors above threshold.

  

  	//data input methods

  	void Insert(rtsDTGrid3D<T>* dt_grid, double factor, indextype x, indextype y, indextype z);

  	void Insert(rtsImplicit3D<T>* source, indextype x, indextype y, indextype z);

  

  	//data massaging

  	

  	void Scale(T min, T max);

  	void Crop(indextype x, indextype y, indextype z, indextype size_x, indextype size_y, indextype size_z);

  	void Binary(T threshold, T true_value);		///<Turns the image into a binary image based on a threshold value T.  All values below T are set to 0, all values above are set to true_value.

  	void Threshold(T min, T value);

  	void Threshold(T min, T max, T value);

  	void Threshold(T min, T max, T inside, T outside);

  	void Erode(T isovalue, T fill_value);			///<Erodes the image around the edges defined by an isovalue.

  	unsigned int Thin(T isovalue);		///<Finds the skeleton of the given isosurface using erosion.

  	bool TestTopology(T isovalue, unsigned int x, unsigned int y, unsigned int z);	///<Tests if the specified voxel is necessary to the topology of the specified isosurface.

  	int Neighbors26(indextype x, indextype y, indextype z, T isovalue);		///<Returns the number of neighbors above the given isovalue.

  	void FloodFill26(indextype x, indextype y, indextype z, T new_value);	///<Fills the connected component at (x, y, z) with the new value.

  	void FloodFill6(indextype x, indextype y, indextype z, T new_value);	///<Performs a 6-connected flood-fill operation starting at the specified node.

  	void MedianFilter(int dist_x, int dist_y, int dist_z, double factor = 0.5);		///<Performs a median filter on the data set.

  	void ClampMax(T max);		///<Clamps the function to the given maximum value.

  	void ClampMin(T min);

  	void ClampMin(T min, T value);	///<Sets all function values below min to value.

  

  	//create new data

  	rtsImplicit3D<T>* Resample(indextype newres_x, indextype newres_y, indextype newres_z);

  	rtsImplicit3D<float>* Isodistance_Manhattan(T isovalue, bool sdf = false);

  	rtsImplicit3D<vector3D<T>>* Gradient();

  	rtsImplicit3D<float>* EstimateAmbient(T threshold);

  	rtsImplicit3D<float>* EstimateAttenuatedAmbient(T surface, T transparent, float attenuation);

  

  	//implicit shapes

  	//(these functions create some basic implicit shapes just for fun)

  	void Sphere(double center_i, double center_j, double center_k, double radius, T in_value);

  

  	//output functions

  	void toConsole();

  

  };

  

  template <class T>

  void rtsImplicit3D<T>::blit3D(const T* source,

  				   indextype s_px, indextype s_py, indextype s_pz,

  				   indextype s_sx, indextype s_sy, indextype s_sz,

  				   T* dest,

  				   indextype d_px, indextype d_py, indextype d_pz,

  				   indextype d_sx, indextype d_sy, indextype d_sz,

  				   indextype blit_size_x, indextype blit_size_y, indextype blit_size_z)

  {

  	indextype ps, pd;		//stores the mapping for the source point to the dest point

  	//find the maximum points that can be blit to (in case source overlaps the edges of dest)

  	blit_size_x = min(blit_size_x, min(s_sx - s_px, d_sx - d_px));

  	blit_size_y = min(blit_size_y, min(s_sy - s_py, d_sy - d_py));

  	blit_size_z = min(blit_size_z, min(s_sz - s_pz, d_sz - d_pz));

  

  	indextype source_z_offset = s_sx * s_sy;

  	indextype dest_z_offset = d_sx * d_sy;

  

  	indextype z,y;

  	for(z=0; z<blit_size_z; z++)

  		for(y=0; y<blit_size_y; y++)

  		{

  			ps = (z + s_pz) * source_z_offset + (y + s_py) * s_sx + s_px;

  			pd = (z + d_pz) * dest_z_offset + (y + d_py) * d_sx + d_px;

  			memcpy((void*)(&dest[pd]), (void*)(&source[ps]), sizeof(T)*blit_size_x);

  		}

  }

  

  template <class T>

  void rtsImplicit3D<T>::shallow_copy(const rtsImplicit3D<T> source, rtsImplicit3D<T> &dest)

  {

  	dest = rtsImplicit3D<T>(source.m_resolution.x, source.m_resolution.y, source.m_resolution.z);

  	dest.m_boundary = source.m_boundary;

  	dest.m_domain_max = source.m_domain_max;

  	dest.m_domain_min = source.m_domain_max;

  	dest.m_voxel_size = source.m_voxel_size;

  }

  

  template <class T>

  rtsImplicit3D<T>::rtsImplicit3D(vector3D<int> resolution, T boundary, point3D<double> domain_min, point3D<double> domain_max)

  {

  	//This function creates an implicit function based on all of the shallow variables

  	m_resolution = resolution;

  	m_boundary = boundary;

  	m_domain_min = domain_min;

  	m_domain_max = domain_max;

  	m_voxel_size = domain_max - domain_min;

  	m_voxel_size.x /= m_resolution.x;

  	m_voxel_size.y /= m_resolution.y;

  	m_voxel_size.z /= m_resolution.z;

  

  	//allocate the data

  	m_data = new T[m_resolution.x * m_resolution.y * m_resolution.z];

  }

  

  template <class T>

  rtsImplicit3D<T>::rtsImplicit3D(T* data, vector3D<int> resolution, T boundary, point3D<double> domain_min, point3D<double> domain_max)

  {

  	//This function creates an implicit function based on ALL of the variables

  	m_resolution = resolution;

  	m_boundary = boundary;

  	m_domain_min = domain_min;

  	m_domain_max = domain_max;

  	m_voxel_size = domain_max - domain_min;

  	m_voxel_size.x /= m_resolution.x;

  	m_voxel_size.y /= m_resolution.y;

  	m_voxel_size.z /= m_resolution.z;

  

  	//allocate the data

  	indextype size = m_resolution.x * m_resolution.y * m_resolution.z;

  	m_data = new T[size];

  	memcpy(m_data, data, sizeof(T)*size);

  	//for(int i=0; i<size; i++)

  	//	m_data[i] = data[i];

  }

  

  

  

  template <class T>

  rtsImplicit3D<T>::rtsImplicit3D()

  {

  	m_resolution.x = 1;

  	m_resolution.y = 1;

  	m_resolution.z = 1;

  	m_data = new T[1];

  	//m_boundary = 0;				//initialize boundary condition

  	m_domain_min = point3D<double>(0.0, 0.0, 0.0);	//set range parameters

  	m_domain_max = point3D<double>(1.0, 1.0, 1.0);

  	m_voxel_size = vector3D<double>(1.0, 1.0, 1.0);

  }

  

  template <class T>

  rtsImplicit3D<T>::rtsImplicit3D(indextype res_x, indextype res_y, indextype res_z)

  {

  	m_resolution.x = res_x;					//set resolution vector

  	m_resolution.y = res_y;

  	m_resolution.z = res_z;

  	m_data = new T[res_x*res_y*res_z];		//allocate data

  	memset(&m_boundary, 0, sizeof(T));							//initialize boundary condition

  	m_domain_min = point3D<double>(0.0, 0.0, 0.0);	//set range parameters

  	m_domain_max = point3D<double>(1.0, 1.0, 1.0);

  

  	m_voxel_size = m_domain_max - m_domain_min;

  	m_voxel_size.x /= m_resolution.x;

  	m_voxel_size.y /= m_resolution.y;

  	m_voxel_size.z /= m_resolution.z;

  }

  

  template <class T>

  rtsImplicit3D<T>::rtsImplicit3D(T* data, indextype res_x, indextype res_y, indextype res_z)

  {

  	m_resolution.x = res_x;					//set resolution vector

  	m_resolution.y = res_y;

  	m_resolution.z = res_z;

  	m_data = new T[res_x*res_y*res_z];		//allocate data

  	//copy the sample data into the data array

  	indextype size = res_x*res_y*res_z;

  	for(indextype i=0; i<size; i++)

  		m_data[i] = data[i];

  	m_boundary = 0;							//initialize boundary condition

  	m_domain_min = point3D<double>(0.0, 0.0, 0.0);	//set range parameters

  	m_domain_max = point3D<double>(1.0, 1.0, 1.0);

  

  	m_voxel_size = domain_max - domain_min;

  	m_voxel_size.x /= m_resolution.x;

  	m_voxel_size.y /= m_resolution.y;

  	m_voxel_size.z /= m_resolution.z;

  }

  

  template <class T>

  rtsImplicit3D<T>::rtsImplicit3D(const rtsImplicit3D<T>& original)

  {

  	//copy the shallow variables

  	m_resolution = original.m_resolution;

  	m_boundary = original.m_boundary;

  	m_domain_min = original.m_domain_min;

  	m_domain_max = original.m_domain_max;

  	m_voxel_size = original.m_voxel_size;

  

  	//allocate space for the data

  	m_data = new T[m_resolution.x * m_resolution.y * m_resolution.z];

  	//copy the data

  	blit3D(original.m_data,

  		   0, 0, 0,

  		   m_resolution.x, m_resolution.y, m_resolution.z,

  		   m_data,

  		   0, 0, 0,

  		   m_resolution.x, m_resolution.y, m_resolution.z,

  		   m_resolution.x, m_resolution.y, m_resolution.z);

  }

  

  template <class T>

  rtsImplicit3D<T>::~rtsImplicit3D()

  {

  	delete m_data;

  }

  

  template <class T>

  typename rtsImplicit3D<T>& rtsImplicit3D<T>::operator=(const T rhs)

  {

  	indextype size = m_resolution.x*m_resolution.y*m_resolution.z;

  	for(int i=0; i<size; i++)

  		m_data[i] = rhs;

  

  	return *this;

  }

  

  template <class T>

  typename rtsImplicit3D<T>& rtsImplicit3D<T>::operator=(const rtsImplicit3D<T>& rhs)

  {

  	//check for self-assignment

  	if(this == &rhs)

  		return *this;

  

  	//deallocate memory

  	if(m_data != NULL)

  		delete m_data;

  

  	//copy the shallow variables

  	m_resolution = rhs.m_resolution;

  	m_boundary = rhs.m_boundary;

  	m_domain_min = rhs.m_domain_min;

  	m_domain_max = rhs.m_domain_max;

  	m_voxel_size = rhs.m_voxel_size;

  

  	//allocate and copy memory

  	m_data = new T[m_resolution.x * m_resolution.y * m_resolution.z];

  	//copy the data

  	blit3D(rhs.m_data,

  		   0,0,0,

  		   m_resolution.x, m_resolution.y, m_resolution.z,

  		   m_data, 

  		   0, 0, 0, 

  		   m_resolution.x, m_resolution.y, m_resolution.z,

  		   m_resolution.x, m_resolution.y, m_resolution.z);

  

  	//return the left hand side

  	return *this;

  }

  

  template <class T>

  inline T& rtsImplicit3D<T>::operator ()(indextype x, indextype y, indextype z)

  {

  	return xyz(x, y, z);

  }

  

  template <class T>

  inline T rtsImplicit3D<T>::operator()(double i, double j, double k)

  {

  	return ijk(i, j, k);

  }

  

  template <class T>

  rtsImplicit3D<T>& rtsImplicit3D<T>::operator *=(const T constant)

  {

  	indextype size = m_resolution.x * m_resolution.y * m_resolution.z;

  	for(indextype i = 0; i<size; i++)

  		m_data[i] *= constant;

  

  	return *this;

  }

  

  template <class T>

  rtsImplicit3D<T>& rtsImplicit3D<T>::operator +=(const T constant)

  {

  	indextype size = m_resolution.x * m_resolution.y * m_resolution.z;

  	for(indextype i = 0; i<size; i++)

  		m_data[i] += constant;

  

  	return *this;

  }

  template <class T>

  rtsImplicit3D<T>& rtsImplicit3D<T>::operator -=(const T constant)

  {

  	indextype size = m_resolution.x * m_resolution.y * m_resolution.z;

  	for(indextype i = 0; i<size; i++)

  		m_data[i] -= constant;

  

  	return *this;

  }

  template <class T>

  rtsImplicit3D<T>& rtsImplicit3D<T>::operator /=(const T constant)

  {

  	indextype size = m_resolution.x * m_resolution.y * m_resolution.z;

  	for(indextype i = 0; i<size; i++)

  		m_data[i] /= constant;

  

  	return *this;

  }

  template <class T>

  const rtsImplicit3D<T> rtsImplicit3D<T>::operator *(const T constant)

  {

  	rtsImplicit3D<T> result = (*this);

  	result *= constant;

  

  	return result;

  }

  

  template <class T>

  const rtsImplicit3D<T> rtsImplicit3D<T>::operator +(const T constant)

  {

  	rtsImplicit3D<T> result = (*this);

  	result += constant;

  

  	return result;

  }

  

  template <class T>

  const rtsImplicit3D<T> rtsImplicit3D<T>::operator -(const T constant)

  {

  	rtsImplicit3D<T> result = (*this);

  	result -= constant;

  

  	return result;

  }

  

  template <class T>

  const rtsImplicit3D<T> rtsImplicit3D<T>::operator /(const T constant)

  {

  	rtsImplicit3D<T> result = (*this);

  	result /= constant;

  

  	return result;

  }

  

  template <class T>

  template <class U>

  rtsImplicit3D<T>::operator rtsImplicit3D<U>()

  {

  	//cast one type to another

  	//create the data pointer from the current function

  	indextype size = m_resolution.x * m_resolution.y * m_resolution.z;

  	U* new_data = new U[size];

  	for(int i=0; i<size; i++)

  		new_data[i] = m_data[i];

  	rtsImplicit3D<U> cast_result(new_data, m_resolution, m_boundary, m_domain_min, m_domain_max);

  

  	return cast_result;

  }

  

  template <class T>

  inline T& rtsImplicit3D<T>::xyz(indextype x, indextype y, indextype z)

  {

  	if(x<0 || y<0 || z<0 || x>=m_resolution.x || y>=m_resolution.y || z>=m_resolution.z)

  		return m_boundary;

  	//return m_data[(z * m_resolution.x * m_resolution.y) + (y * m_resolution.x) + x];

  	return m_data[x + m_resolution.x * (y + z * m_resolution.y)];

  

  }

  

  template <class T>

  inline point3D<indextype> rtsImplicit3D<T>::getNearestIndex(indextype i)

  {

  	point3D<indextype> result;

  	result.z = i/(m_resolution.x*m_resolution.y);

  	indextype mod = i%(m_resolution.x*m_resolution.y);

  	result.y = mod/m_resolution.x;

  	result.x = mod%m_resolution.x;

  

  	return result;

  

  }

  

  template <class T>

  void rtsImplicit3D<T>::LoadRAW(indextype header_size, indextype size_x,

  							   indextype size_y, indextype size_z, const char *filename)

  {

  	//set the data size

  	m_resolution = vector3D<indextype>(size_x, size_y, size_z);

  	//delete any previous data

  	if(m_data != NULL)

  		delete m_data;

  

  	ifstream infile(filename, ios::in | ios::binary);

  

  	//load the header

  	unsigned char* header = new unsigned char[header_size];

  	infile.read((char*)header, header_size);

  

  	//load the actual data

  	indextype size = m_resolution.x * m_resolution.y * m_resolution.z;

  	m_data = new T[size];

  	infile.read((char*)m_data, size*sizeof(T));

  

  	//calculate min and maxes

  	infile.close();

  }

  

  template <class T>

  void rtsImplicit3D<T>::LoadVOL(const char *filename)

  {

  	ifstream infile(filename, ios::in | ios::binary);	//create the files stream

  	if(!infile)

  		return;

  

  	indextype size_x, size_y, size_z;				//create variables to store the size of the data set

  	//load the dimensions of the data set

  	infile.read((char*)&size_x, sizeof(int));			//load the file header

  	infile.read((char*)&size_y, sizeof(int));

  	infile.read((char*)&size_z, sizeof(int));

  

  	//close the file

  	infile.close();

  	//load the raw data

  	LoadRAW(12, size_x, size_y, size_z, filename);

  }

  

  template <class T>

  void rtsImplicit3D<T>::SaveVOL(const char *filename)

  {

  	ofstream outfile(filename, ios::out | ios::binary);	//create the binary file stream

  

  	//write the volume size to the file

  	vector3D<int> vol_size = m_resolution;

  	outfile.write((char*)&vol_size.x, sizeof(int));

  	outfile.write((char*)&vol_size.y, sizeof(int));

  	outfile.write((char*)&vol_size.z, sizeof(int));

  

  	outfile.write((char*)m_data, sizeof(char)*vol_size.x*vol_size.y*vol_size.z);

  }

  

  template <class T>

  void rtsImplicit3D<T>::SaveRAW(const char *filename)

  {

  	ofstream outfile(filename, ios::out | ios::binary);	//create the binary file stream

  

  	//write the volume data

  	outfile.write((char*)m_data, sizeof(T)*m_resolution.x*m_resolution.y*m_resolution.z);

  }

  

  template <class T>

  inline T rtsImplicit3D<T>::ijk(double i, double j, double k)

  {

  	/*This function determines the value at the specified parametric points

  	defined by the m_domain_min and m_domain_max parameter values.*/

  

  	//if the parameter is outside the range, return the boundary value

  	if(i<m_domain_min.x || j<m_domain_min.y || k<m_domain_min.z ||

  	   i>m_domain_max.x || j>m_domain_max.y || k>m_domain_max.z)

  	   return m_boundary;

  	

  	point3D<double> index = getFractionalIndex(i, j, k);

  

  	//cout<<index.x<<","<<index.y<<","<<index.z<<endl;

  

  	//interpolate the values

  	int f_x = (int)floor(index.x);				//calculate floor and ceiling values

  	int f_y = (int)floor(index.y);

  	int f_z = (int)floor(index.z);

  	int c_x = (int)ceil(index.x);

  	int c_y = (int)ceil(index.y);

  	int c_z = (int)ceil(index.z);

  

  	double x_d = index.x - f_x;			//find the point within the voxel

  	double y_d = index.y - f_y;

  	double z_d = index.z - f_z;

  

  	T i_1 = xyz(f_x, f_y, f_z)*(1.0 - z_d) + xyz(f_x, f_y, c_z)*(z_d);	//interpolate along z

  	T i_2 = xyz(f_x, c_y, f_z)*(1.0 - z_d) + xyz(f_x, c_y, c_z)*(z_d);

  	T j_1 = xyz(c_x, f_y, f_z)*(1.0 - z_d) + xyz(c_x, f_y, c_z)*(z_d);

  	T j_2 = xyz(c_x, c_y, f_z)*(1.0 - z_d) + xyz(c_x, c_y, c_z)*(z_d);

  

  	T w_1 = i_1*(1.0 - y_d) + i_2*(y_d);

  	T w_2 = j_1*(1.0 - y_d) + j_2*(y_d);

  

  	return w_1*(1.0 - x_d) + w_2*(x_d);

  }

  

  

  template <class T>

  void rtsImplicit3D<T>::Parameterize(double x_min, double x_max, double y_min, double y_max, double z_min, double z_max)

  {

  	m_domain_min = point3D<double>(x_min, y_min, z_min);

  	m_domain_max = point3D<double>(x_max, y_max, z_max);

  	m_voxel_size = m_domain_max - m_domain_min;

  	m_voxel_size.x /= m_resolution.x;

  	m_voxel_size.y /= m_resolution.y;

  	m_voxel_size.z /= m_resolution.z;

  }

  

  template <class T>

  inline point3D<double> rtsImplicit3D<T>::getParameter(indextype x, indextype y, indextype z)

  {

  	//get the value between 0 and 1

  	point3D<double> normalized((double)x / (double)(m_resolution.x) + (1.0/(m_resolution.x*2.0)),

  							   (double)y / (double)(m_resolution.y) + (1.0/(m_resolution.y*2.0)),

  							   (double)z/(double)(m_resolution.z) + (1.0/(m_resolution.z*2.0)));

  

  	point3D<double> result(normalized.x * (m_domain_max.x - m_domain_min.x) + m_domain_min.x,

  						   normalized.y * (m_domain_max.y - m_domain_min.y) + m_domain_min.y,

  						   normalized.z * (m_domain_max.z - m_domain_min.z) + m_domain_min.z);

  

  	return result;

  }

  

  template <class T>

  inline point3D<indextype> rtsImplicit3D<T>::getNearestIndex(double i, double j, double k)

  {

  	//this function returns the index of the voxel containing the specified parameter point

  	point3D<double> normalized((i - m_domain_min.x)/(m_domain_max.x-m_domain_min.x),

  							   (j - m_domain_min.y)/(m_domain_max.y-m_domain_min.y),

  							   (k - m_domain_min.z)/(m_domain_max.z-m_domain_min.z));

  	

  	point3D<indextype> result((normalized.x - (1.0/(m_resolution.x*2.0)))*(double)m_resolution.x+0.5,

  							  (normalized.y - (1.0/(m_resolution.y*2.0)))*(double)m_resolution.y+0.5,

  							  (normalized.z - (1.0/(m_resolution.z*2.0)))*(double)m_resolution.z+0.5);

  

  	return result;

  }

  

  template <class T>

  inline point3D<double> rtsImplicit3D<T>::getFractionalIndex(double i, double j, double k)

  {

  	//this function returns the index of the voxel containing the specified parameter point

  	point3D<double> normalized((i - m_domain_min.x)/(m_domain_max.x-m_domain_min.x),

  							   (j - m_domain_min.y)/(m_domain_max.y-m_domain_min.y),

  							   (k - m_domain_min.z)/(m_domain_max.z-m_domain_min.z));

  	

  	point3D<double> result((normalized.x - (1.0/(m_resolution.x*2.0)))*(double)m_resolution.x,

  							  (normalized.y - (1.0/(m_resolution.y*2.0)))*(double)m_resolution.y,

  							  (normalized.z - (1.0/(m_resolution.z*2.0)))*(double)m_resolution.z);

  	return result;

  }

  

  

  template <class T>

  T* rtsImplicit3D<T>::GetBits()

  {

  	/*Returns bit data in lexocographical order (possibly for 3D texture mapping)*/

  	return m_data;

  }

  

  template <class T>

  rtsImplicit3D<T>* rtsImplicit3D<T>::Resample(indextype newres_x, indextype newres_y, indextype newres_z)

  {

  	/*This function resamples the current function at the specified resolution.

  	No convolution is done for reducing he resolution.

  	*/

  

  	rtsImplicit3D<T>* result = new rtsImplicit3D<T>(vector3D<indextype>(newres_x, newres_y, newres_z),

  						    m_boundary, m_domain_min, m_domain_max);

  

  	//run through the entire resolution of the new function, sampling the current function

  	int x, y, z;

  	point3D<double> parametric;

  	for(x = 0; x<newres_x; x++)

  		for(y=0; y<newres_y; y++)

  			for(z=0; z<newres_z; z++)

  			{

  				//compute the parametric point for the sample point

  				parametric = result->getParameter(x, y, z);

  				(*result)(x, y, z) = ijk(parametric.x, parametric.y, parametric.z);

  			}

  

  	return result;

  }

  

  template <class T>

  void rtsImplicit3D<T>::Scale(T new_min, T new_max)

  {

  	/*This function scales all values of the implicit function to within a specified range

  	*/

  

  	//find the minimum and maximum values in this function

  	indextype data_size = m_resolution.x * m_resolution.y * m_resolution.z;

  	T min = m_data[0];

  	T max = m_data[0];

  	for(indextype i=0; i<data_size; i++)

  	{

  		if(m_data[i] < min)

  			min = m_data[i];

  		if(m_data[i] > max)

  			max = m_data[i];

  	}

  

  	//scale all values to the specified range

  	T current_range = max - min;

  	T new_range = new_max - new_min;

  	for(indextype i=0; i<data_size; i++)

  		m_data[i] = ((m_data[i] - min)/current_range)*(new_range) + new_min;

  }

  

  template <class T>

  void rtsImplicit3D<T>::Crop(indextype x, indextype y, indextype z, 

  							indextype size_x, indextype size_y, indextype size_z)

  {

  	/*This function crops the implicit function at the specified nodes

  	*/

  	//create a pointer for the new data

  	T* new_data = new T[size_x*size_y*size_z];

  

  	//blit from the old data to the new data

  	blit3D(m_data,

  			x, y, z,

  			m_resolution.x, m_resolution.y, m_resolution.z,

  			new_data,

  			0, 0, 0,

  			size_x, size_y, size_z,

  			size_x, size_y, size_z);

  

  	//change the shallow variables

  	vector3D<indextype> new_resolution = vector3D<indextype>(size_x, size_y, size_z);

  	vector3D<double> voxel_size = getParameter(0,0,0) - getParameter(1,1,1);

  	point3D<double> new_domain_min = getParameter(x, y, z) - 0.5*voxel_size;

  	point3D<double> new_domain_max = getParameter(size_x-1, size_y - 1, size_z-1) + 0.5*voxel_size;

  	//copy new shallow variables

  	m_resolution = new_resolution;

  	m_domain_min = new_domain_min;

  	m_domain_max = new_domain_max;

  

  	//copy data

  	delete m_data;

  	m_data = new_data;

  

  }

  

  template <class T>

  void rtsImplicit3D<T>::Threshold(T min, T value)

  {

  	/*This function sets all values between min and max to value.

  	*/

  	int x, y, z;

  	T test_value;

  	for(x=0; x<m_resolution.x; x++)

  		for(y=0; y<m_resolution.y; y++)

  			for(z=0; z<m_resolution.z; z++)

  			{

  				test_value = xyz(x, y, z);

  				if(test_value >= min)

  					xyz(x, y, z) = value;

  			}

  }

  

  template <class T>

  void rtsImplicit3D<T>::Threshold(T min, T max, T value)

  {

  	/*This function sets all values between min and max to value.

  	*/

  	int x, y, z;

  	T test_value;

  	for(x=0; x<m_resolution.x; x++)

  		for(y=0; y<m_resolution.y; y++)

  			for(z=0; z<m_resolution.z; z++)

  			{

  				test_value = xyz(x, y, z);

  				if(test_value >= min && test_value <= max)

  					xyz(x, y, z) = value;

  			}

  }

  

  template <class T>

  void rtsImplicit3D<T>::Threshold(T min, T max, T inside, T outside)

  {

  	/*This function sets all values between min and max to value.

  	*/

  	int x, y, z;

  	T test_value;

  	for(x=0; x<m_resolution.x; x++)

  		for(y=0; y<m_resolution.y; y++)

  			for(z=0; z<m_resolution.z; z++)

  			{

  				test_value = xyz(x, y, z);

  				if(test_value >= min && test_value <= max)

  					xyz(x, y, z) = inside;

  				else

  					xyz(x, y, z) = outside;

  			}

  }

  

  template <class T>

  void rtsImplicit3D<T>::Insert(rtsDTGrid3D<T>* dt_grid, double factor, indextype pos_x, indextype pos_y, indextype pos_z)

  {

  /*	This function copies a 3D DT-Grid into the implicit function at the specified position.

  */

  	rtsDTGrid3D<T>::iterator i;

  	indextype x, y, z;

  	for(i=dt_grid->begin(); i != dt_grid->end(); i.increment())	//for each node in the grid

  	{

  		x = pos_x + i.getX();

  		y = pos_y + i.getY();

  		z = pos_z + i.getZ();

  		if(x >= 0 || x < m_resolution.x ||

  			y >= 0 || y < m_resolution.y ||

  			y >= 0 || y < m_resolution.z)

  			xyz(x, y, z) = max(i.value* factor, (double)xyz(x, y, z));

  	}

  }

  

  template <class T>

  void rtsImplicit3D<T>::Insert(rtsImplicit3D<T>* source, indextype x, indextype y, indextype z)

  {

  	blit3D(source->m_data, 0, 0, 0, source->m_resolution.x, source->m_resolution.y, source->m_resolution.z,

  			m_data, x, y, z, m_resolution.x, m_resolution.y, m_resolution.z,

  			source->m_resolution.x, source->m_resolution.y, source->m_resolution.z);

  }

  

  

  template <class T>

  inline float rtsImplicit3D<T>::manhattan_distance(rtsImplicit3D<float>* function, point3D<indextype> point, bool sdf)

  {

  	/*This function updates the manhattan distance from a surface using the manhattan

  	distance of its neighboring points.

  	*/

  	indextype x, y, z;

  	x=point.x; y=point.y, z=point.z;

  	int sign = 1;

  	float result = DIST_MAX;

  	float near_value;				//the value of the neighbor being considered

  	float possible_value;			

  	if(x!=0)

  	{ 

  		near_value = (*function)(x-1, y, z);

  		if(!sdf)

  			result = min(result, near_value + (float)m_voxel_size.x);

  		else

  		{

  			if(near_value<0) sign = -1; else sign = 1;	//determine if the value is inside or outside

  			possible_value = sign*(fabs(near_value) + m_voxel_size.x);

  			if(fabs(possible_value) < fabs(result))

  				result = possible_value;

  		}

  

  	}

  	if(x!=function->DimX()-1)

  	{

  		near_value = (*function)(x+1, y, z);

  		if(!sdf)

  			result = min(result, near_value + (float)m_voxel_size.x);

  		else

  		{

  			if(near_value<0) sign = -1; else sign = 1;	//determine if the value is inside or outside

  			possible_value = sign*(fabs(near_value) + m_voxel_size.x);

  			if(fabs(possible_value) < fabs(result))

  				result = possible_value;

  		}

  	}

  	if(y!=0)

  	{

  		near_value = (*function)(x, y-1, z);

  		if(!sdf)

  			result = min(result, near_value + (float)m_voxel_size.y);

  		else

  		{

  			if(near_value<0) sign = -1; else sign = 1;	//determine if the value is inside or outside

  			possible_value = sign*(fabs(near_value) + m_voxel_size.y);

  			if(fabs(possible_value) < fabs(result))

  				result = possible_value;

  		}

  	}

  	if(y!=function->DimY()-1)

  	{

  		near_value = (*function)(x, y+1, z);

  		if(!sdf)

  			result = min(result, near_value + (float)m_voxel_size.y);

  		else

  		{

  			if(near_value<0) sign = -1; else sign = 1;	//determine if the value is inside or outside

  			possible_value = sign*(fabs(near_value) + m_voxel_size.y);

  			if(fabs(possible_value) < fabs(result))

  				result = possible_value;

  		}

  	}

  	if(z!=0)

  	{

  		near_value = (*function)(x, y, z-1);

  		if(!sdf)

  			result = min(result, near_value + (float)m_voxel_size.z);

  		else

  		{

  			if(near_value<0) sign = -1; else sign = 1;	//determine if the value is inside or outside

  			possible_value = sign*(fabs(near_value) + m_voxel_size.z);

  			if(fabs(possible_value) < fabs(result))

  				result = possible_value;

  		}

  	}

  	if(z!=function->DimZ()-1)

  	{

  		near_value = (*function)(x, y, z+1);

  		if(!sdf)

  			result = min(result, near_value + (float)m_voxel_size.z);

  		else

  		{

  			if(near_value<0) sign = -1; else sign = 1;	//determine if the value is inside or outside

  			possible_value = sign*(fabs(near_value) + m_voxel_size.z);

  			if(fabs(possible_value) < fabs(result))

  				result = possible_value;

  		}

  	}

  	return result;

  }

  

  template <class T>

  inline float rtsImplicit3D<T>::isosurface_distance(point3D<double> p0, point3D<double> p1, T isovalue)

  {

  	/*This function computes the distance from p0 to the surface, given two points p0 and p1

  	on either side of the surface.  isovalue specifies

  	the value at the surface.  Right now, this function returns a float.  I'll have to think

  	of something better to do in the future.

  	*/

  

  	//compute the normalized position of the surface between p0 and p1

  	float val0 = ijk(p0.x, p0.y, p0.z);

  	float val1 = ijk(p1.x, p1.y, p1.z);

  	float isovalue_norm_pos = (isovalue - val0) / (val1 - val0);

  	//compute the actual position of the surface

  	point3D<double> s_pos = p0 + isovalue_norm_pos * (p1 - p0);

  	//compute the distance from p0 to the surface

  	float result = (s_pos - p0).Length();

  	//cout<<"distance: "<<result<<endl;

  	return result;

  }

  

  template <class T>

  void rtsImplicit3D<T>::compute_distance_function_boundary(T isovalue, 

  														  rtsImplicit3D<float>* &result, 

  														  rtsImplicit3D<bool>* &mask, bool sdf)

  {

  	/*This function creates an initial signed distance function from a threshold image.

  	All voxels adjacent to the surface specified by the threshold are initialized with a

  	distance value.  Low values are inside, high values are outside.

  	*/

  	//current and neighboring voxel flags (false = inside, true = outside)

  	bool c, x_p, x_n, y_p, y_n, z_p, z_n;

  	float d_xp, d_xn, d_yp, d_yn, d_zp, d_zn;

  	float in_out = 1;

  

  	//boundary condition function and the mask

  	result = new rtsImplicit3D<float>(m_resolution.x, m_resolution.y, m_resolution.z);

  	//get the parameterization

  	result->Parameterize(m_domain_min.x, m_domain_max.x, m_domain_min.y, m_domain_max.y, m_domain_min.z, m_domain_max.z);

  	(*result) = DIST_MAX;

  	result->setBoundary(DIST_MAX);	

  	//create a mask

  	mask = new rtsImplicit3D<bool>(m_resolution.x, m_resolution.y, m_resolution.z);

  	(*mask) = false;

  

  	cout<<"done making boundary condition function"<<endl;

  	//for each voxel

  	int x, y, z;

  	for(x=0; x<m_resolution.x; x++)

  		for(y=0; y<m_resolution.y; y++)

  			for(z=0; z<m_resolution.z; z++)

  			{

  				//reset flags

  				c=x_p=x_n=y_p=y_n=z_p=z_n=true;

  				in_out = 1.0;

  				//look at the current voxel

  				if(xyz(x, y, z) < isovalue)

  					c=false;

  				else c=true;

  				//if the voxel is outside the domain, assume that it is equal to the current voxel

  				if(x-1 < 0) x_n = c;	//X

  				else if(xyz(x-1, y, z) < isovalue) x_n = false;

  				if(x+1 >= m_resolution.x) x_p = c;

  				else if(xyz(x+1, y, z) < isovalue) x_p = false;

  				if(y-1 < 0) y_n = c;	//Y

  				else if(xyz(x, y-1, z) < isovalue) y_n = false;

  				if(y+1 >= m_resolution.y) y_p = c;

  				else if(xyz(x, y+1, z) < isovalue) y_p = false;

  				if(z-1 < 0) z_n = c;	//Z

  				else if(xyz(x, y, z-1) < isovalue) z_n = false;

  				if(z+1 >= m_resolution.z) z_p = c;

  				else if(xyz(x, y, z+1) < isovalue) z_p = false;

  

  				//set the distance from the isosurface

  				if(c == false && sdf)

  					in_out = -1.0;

  				if(x_n != c)

  					(*result)(x, y, z) = min((*result)(x,y,z),

  										 isosurface_distance(getParameter(x, y, z), 

  													  getParameter(x-1, y, z),

  													  isovalue) * in_out);

  				if(x_p != c)

  					(*result)(x, y, z) = min((*result)(x,y,z),

  											isosurface_distance(getParameter(x, y, z), 

  													  getParameter(x+1, y, z),

  													  isovalue) * in_out);

  				if(y_n != c)

  					(*result)(x, y, z) = min((*result)(x,y,z),

  											isosurface_distance(getParameter(x, y, z), 

  													  getParameter(x, y-1, z),

  													  isovalue) * in_out);

  				if(y_p != c)

  					(*result)(x, y, z) = min((*result)(x,y,z),

  											isosurface_distance(getParameter(x, y, z), 

  													  getParameter(x, y+1, z),

  													  isovalue) * in_out);

  				if(z_n != c)

  					(*result)(x, y, z) = min((*result)(x,y,z),

  											isosurface_distance(getParameter(x, y, z-1), 

  													  getParameter(x, y, z),

  													  isovalue) * in_out);

  				if(z_p != c)

  					(*result)(x, y, z) = min((*result)(x,y,z),

  											isosurface_distance(getParameter(x, y, z), 

  													  getParameter(x, y, z+1),

  													  isovalue) * in_out);

  

  				//set the mask to 1 if the voxel is on an edge node

  				if(x_n != c || x_p != c || y_n != c || y_p != c || z_n != c || z_p != c)

  					(*mask)(x, y, z) = true;

  			}

  				

  

  	//if a line between the two voxels crosses the surface

  		//find the distance between the voxel center and the surface

  

  

  			cout<<"done computing boundary conditions"<<endl;

  }

  

  template <class T>

  rtsImplicit3D<float>* rtsImplicit3D<T>::EstimateAmbient(T threshold)

  {

  	rtsImplicit3D<float>* result = new rtsImplicit3D<float>(m_resolution.x, m_resolution.y, m_resolution.z);		//create a new implicit function

  	(*result) = 0.0f;

  	rtsImplicit3D<float>* temp = new rtsImplicit3D<float>(m_resolution.x, m_resolution.y, m_resolution.z);	//temp buffer for current lighting iteration

  	(*temp) = 0.0f;

  	temp->setBoundary(1.0);

  	

  	cout<<"first iteration..."<<endl;

  	int x,y,z;

  	float ambient;

  	

  	for(x=0; x<m_resolution.x; x++)

  		for(y=0; y<m_resolution.y; y++)

  			for(z=0; z<m_resolution.z; z++)

  			{

  				ambient = 0.0;

  				if(xyz(x-1, y, z) < threshold)

  					ambient += (*temp)(x-1, y, z);

  				if(xyz(x, y-1, z) < threshold)

  					ambient += (*temp)(x, y-1, z);

  				if(xyz(x, y-1, z) < threshold)

  					ambient += (*temp)(x, y, z-1);

  

  				(*temp)(x, y, z) += ambient/3.0;

  				(*result)(x, y, z) += ambient/3.0;

  				if(ambient > 3.0)

  					cout<<"error"<<endl;

  			}

  	(*temp) = 0.0f;

  	temp->setBoundary(1.0);

  	cout<<"done."<<endl;

  

  	cout<<"second iteration..."<<endl;

  	for(x=0; x<m_resolution.x; x++)

  		for(y=0; y<m_resolution.y; y++)

  			for(z=m_resolution.z-1; z>=0; z--)

  			{

  				ambient = 0.0;

  				if(xyz(x-1, y, z) < threshold)

  					ambient += (*temp)(x-1, y, z);

  				if(xyz(x, y-1, z) < threshold)

  					ambient += (*temp)(x, y-1, z);

  				if(xyz(x, y, z+1) < threshold)

  					ambient += (*temp)(x, y, z+1);

  

  				(*temp)(x, y, z) += ambient/3.0;

  				(*result)(x, y, z) += ambient/3.0;

  				if(ambient > 3.0)

  					cout<<"error"<<endl;

  			}

  	(*temp) = 0.0f;

  	temp->setBoundary(1.0);

  	cout<<"done."<<endl;

  

  	cout<<"third iteration..."<<endl;

  	for(x=0; x<m_resolution.x; x++)

  		for(y=m_resolution.y-1; y>=0; y--)

  			for(z=0; z<m_resolution.z; z++)

  			{

  				ambient = 0.0;

  				if(xyz(x-1, y, z) < threshold)

  					ambient += (*temp)(x-1, y, z);

  				if(xyz(x, y+1, z) < threshold)

  					ambient += (*temp)(x, y+1, z);

  				if(xyz(x, y, z-1) < threshold)

  					ambient += (*temp)(x, y, z-1);

  

  				(*temp)(x, y, z) += ambient/3.0;

  				(*result)(x, y, z) += ambient/3.0;

  				if(ambient > 3.0)

  					cout<<"error"<<endl;

  			}

  	(*temp) = 0.0f;

  	temp->setBoundary(1.0);

  	cout<<"done."<<endl;

  

  	cout<<"fourth iteration..."<<endl;

  	for(x=0; x<m_resolution.x; x++)

  		for(y=m_resolution.y-1; y>=0; y--)

  			for(z=m_resolution.z-1; z>=0; z--)

  			{

  				ambient = 0.0;

  				if(xyz(x-1, y, z) < threshold)

  					ambient += (*temp)(x-1, y, z);

  				if(xyz(x, y+1, z) < threshold)

  					ambient += (*temp)(x, y+1, z);

  				if(xyz(x, y, z+1) < threshold)

  					ambient += (*temp)(x, y, z+1);

  

  				(*temp)(x, y, z) += ambient/3.0;

  				(*result)(x, y, z) += ambient/3.0;

  				if(ambient > 3.0)

  					cout<<"error"<<endl;

  			}

  	(*temp) = 0.0f;

  	temp->setBoundary(1.0);

  	cout<<"done."<<endl;

  

  	cout<<"fifth iteration..."<<endl;

  	for(x=m_resolution.x-1; x>=0; x--)

  		for(y=0; y<m_resolution.y; y++)

  			for(z=0; z<m_resolution.z; z++)

  			{

  				ambient = 0.0;

  				if(xyz(x+1, y, z) < threshold)

  					ambient += (*temp)(x+1, y, z);

  				if(xyz(x, y-1, z) < threshold)

  					ambient += (*temp)(x, y-1, z);

  				if(xyz(x, y, z-1) < threshold)

  					ambient += (*temp)(x, y, z-1);

  

  				(*temp)(x, y, z) += ambient/3.0;

  				(*result)(x, y, z) += ambient/3.0;

  				if(ambient > 3.0)

  					cout<<"error"<<endl;

  			}

  	(*temp) = 0.0f;

  	temp->setBoundary(1.0);

  	cout<<"done."<<endl;

  

  	cout<<"sixth iteration..."<<endl;

  	for(x=m_resolution.x-1; x>=0; x--)

  		for(y=0; y<m_resolution.y; y++)

  			for(z=m_resolution.z-1; z>=0; z--)

  			{

  				ambient = 0.0;

  				if(xyz(x+1, y, z) < threshold)

  					ambient += (*temp)(x+1, y, z);

  				if(xyz(x, y-1, z) < threshold)

  					ambient += (*temp)(x, y-1, z);

  				if(xyz(x, y, z+1) < threshold)

  					ambient += (*temp)(x, y, z+1);

  

  				(*temp)(x, y, z) += ambient/3.0;

  				(*result)(x, y, z) += ambient/3.0;

  				if(ambient > 3.0)

  					cout<<"error"<<endl;

  			}

  	(*temp) = 0.0f;

  	temp->setBoundary(1.0);

  	cout<<"done."<<endl;

  

  	cout<<"seventh iteration..."<<endl;

  	for(x=m_resolution.x-1; x>=0; x--)

  		for(y=m_resolution.y-1; y>=0; y--)

  			for(z=0; z<m_resolution.z; z++)

  			{

  				ambient = 0.0;

  				if(xyz(x+1, y, z) < threshold)

  					ambient += (*temp)(x+1, y, z);

  				if(xyz(x, y+1, z) < threshold)

  					ambient += (*temp)(x, y+1, z);

  				if(xyz(x, y, z-1) < threshold)

  					ambient += (*temp)(x, y, z-1);

  

  				(*temp)(x, y, z) += ambient/3.0;

  				(*result)(x, y, z) += ambient/3.0;

  				if(ambient > 3.0)

  					cout<<"error"<<endl;

  			}

  	(*temp) = 0.0f;

  	temp->setBoundary(1.0);

  	cout<<"done."<<endl;

  

  	cout<<"eighth iteration..."<<endl;

  	for(x=m_resolution.x-1; x>=0; x--)

  		for(y=m_resolution.y-1; y>=0; y--)

  			for(z=m_resolution.z-1; z>=0; z--)

  			{

  				ambient = 0.0;

  				if(xyz(x+1, y, z) < threshold)

  					ambient += (*temp)(x+1, y, z);

  				if(xyz(x, y+1, z) < threshold)

  					ambient += (*temp)(x, y+1, z);

  				if(xyz(x, y, z+1) < threshold)

  					ambient += (*temp)(x, y, z+1);

  

  				(*temp)(x, y, z) += ambient/3.0;

  				(*result)(x, y, z) += ambient/3.0;

  				if(ambient > 3.0)

  					cout<<"error"<<endl;

  			}

  	(*temp) = 0.0f;

  	temp->setBoundary(1.0);

  	cout<<"done."<<endl;

  

  	(*result)/=8.0;

  	

  	return result;

  }

  

  

  template <class T>

  rtsImplicit3D<float>* rtsImplicit3D<T>::EstimateAttenuatedAmbient(T threshold, T transparent, float attenuation)

  {

  	rtsImplicit3D<float>* result = new rtsImplicit3D<float>(m_resolution.x, m_resolution.y, m_resolution.z);		//create a new implicit function

  	(*result) = 0.0f;

  	rtsImplicit3D<float>* temp = new rtsImplicit3D<float>(m_resolution.x, m_resolution.y, m_resolution.z);	//temp buffer for current lighting iteration

  	(*temp) = 0.0f;

  	temp->setBoundary(1.0);

  	

  	cout<<"first iteration..."<<endl;

  	int x,y,z;

  	float ambient;

  	

  	for(x=0; x<m_resolution.x; x++)

  		for(y=0; y<m_resolution.y; y++)

  			for(z=0; z<m_resolution.z; z++)

  			{

  				ambient = 0.0;

  				if(xyz(x-1, y, z) < threshold)

  					ambient += (*temp)(x-1, y, z)*(1.0 - (xyz(x-1, y, z)/255.0)*attenuation);

  				if(xyz(x, y-1, z) < threshold)

  					ambient += (*temp)(x, y-1, z)*(1.0 - (xyz(x, y-1, z)/255.0)*attenuation);

  				if(xyz(x, y-1, z) < threshold)

  					ambient += (*temp)(x, y, z-1)*(1.0 - (xyz(x, y, z-1)/255.0)*attenuation);

  

  				(*temp)(x, y, z) += ambient/3.0;

  				(*result)(x, y, z) += min(1.0, ambient/3.0);

  				//if(ambient > 3.0)

  				//	cout<<"error"<<endl;

  			}

  	(*temp) = 0.0f;

  	temp->setBoundary(1.0);

  	cout<<"done."<<endl;

  

  	cout<<"second iteration..."<<endl;

  	for(x=0; x<m_resolution.x; x++)

  		for(y=0; y<m_resolution.y; y++)

  			for(z=m_resolution.z-1; z>=0; z--)

  			{

  				ambient = 0.0;

  				if(xyz(x-1, y, z) < threshold)

  					ambient += (*temp)(x-1, y, z)*(1.0 - (xyz(x-1, y, z)/255.0)*attenuation);

  				if(xyz(x, y-1, z) < threshold)

  					ambient += (*temp)(x, y-1, z)*(1.0 - (xyz(x, y-1, z)/255.0)*attenuation);

  				if(xyz(x, y, z+1) < threshold)

  					ambient += (*temp)(x, y, z+1)*(1.0 - (xyz(x, y, z+1)/255.0)*attenuation);

  

  				(*temp)(x, y, z) += ambient/3.0;

  				(*result)(x, y, z) += ambient/3.0;

  				if(ambient > 3.0)

  					cout<<"error"<<endl;

  			}

  	(*temp) = 0.0f;

  	temp->setBoundary(1.0);

  	cout<<"done."<<endl;

  

  	cout<<"third iteration..."<<endl;

  	for(x=0; x<m_resolution.x; x++)

  		for(y=m_resolution.y-1; y>=0; y--)

  			for(z=0; z<m_resolution.z; z++)

  			{

  				ambient = 0.0;

  				if(xyz(x-1, y, z) < threshold)

  					ambient += (*temp)(x-1, y, z)*(1.0 - (xyz(x-1, y, z)/255.0)*attenuation);

  				if(xyz(x, y+1, z) < threshold)

  					ambient += (*temp)(x, y+1, z)*(1.0 - (xyz(x, y+1, z)/255.0)*attenuation);

  				if(xyz(x, y, z-1) < threshold)

  					ambient += (*temp)(x, y, z-1)*(1.0 - (xyz(x, y, z-1)/255.0)*attenuation);

  

  				(*temp)(x, y, z) += ambient/3.0;

  				(*result)(x, y, z) += ambient/3.0;

  				if(ambient > 3.0)

  					cout<<"error"<<endl;

  			}

  	(*temp) = 0.0f;

  	temp->setBoundary(1.0);

  	cout<<"done."<<endl;

  

  	cout<<"fourth iteration..."<<endl;

  	for(x=0; x<m_resolution.x; x++)

  		for(y=m_resolution.y-1; y>=0; y--)

  			for(z=m_resolution.z-1; z>=0; z--)

  			{

  				ambient = 0.0;

  				if(xyz(x-1, y, z) < threshold)

  					ambient += (*temp)(x-1, y, z)*(1.0 - (xyz(x-1, y, z)/255.0)*attenuation);

  				if(xyz(x, y+1, z) < threshold)

  					ambient += (*temp)(x, y+1, z)*(1.0 - (xyz(x, y+1, z)/255.0)*attenuation);

  				if(xyz(x, y, z+1) < threshold)

  					ambient += (*temp)(x, y, z+1)*(1.0 - (xyz(x, y, z+1)/255.0)*attenuation);

  

  				(*temp)(x, y, z) += ambient/3.0;

  				(*result)(x, y, z) += ambient/3.0;

  				if(ambient > 3.0)

  					cout<<"error"<<endl;

  			}

  	(*temp) = 0.0f;

  	temp->setBoundary(1.0);

  	cout<<"done."<<endl;

  

  	cout<<"fifth iteration..."<<endl;

  	for(x=m_resolution.x-1; x>=0; x--)

  		for(y=0; y<m_resolution.y; y++)

  			for(z=0; z<m_resolution.z; z++)

  			{

  				ambient = 0.0;

  				if(xyz(x+1, y, z) < threshold)

  					ambient += (*temp)(x+1, y, z)*(1.0 - (xyz(x+1, y, z)/255.0)*attenuation);

  				if(xyz(x, y-1, z) < threshold)

  					ambient += (*temp)(x, y-1, z)*(1.0 - (xyz(x, y-1, z)/255.0)*attenuation);

  				if(xyz(x, y, z-1) < threshold)

  					ambient += (*temp)(x, y, z-1)*(1.0 - (xyz(x, y, z-1)/255.0)*attenuation);

  

  				(*temp)(x, y, z) += ambient/3.0;

  				(*result)(x, y, z) += ambient/3.0;

  				if(ambient > 3.0)

  					cout<<"error"<<endl;

  			}

  	(*temp) = 0.0f;

  	temp->setBoundary(1.0);

  	cout<<"done."<<endl;

  

  	cout<<"sixth iteration..."<<endl;

  	for(x=m_resolution.x-1; x>=0; x--)

  		for(y=0; y<m_resolution.y; y++)

  			for(z=m_resolution.z-1; z>=0; z--)

  			{

  				ambient = 0.0;

  				if(xyz(x+1, y, z) < threshold)

  					ambient += (*temp)(x+1, y, z)*(1.0 - (xyz(x+1, y, z)/255.0)*attenuation);

  				if(xyz(x, y-1, z) < threshold)

  					ambient += (*temp)(x, y-1, z)*(1.0 - (xyz(x, y-1, z)/255.0)*attenuation);

  				if(xyz(x, y, z+1) < threshold)

  					ambient += (*temp)(x, y, z+1)*(1.0 - (xyz(x, y, z+1)/255.0)*attenuation);

  

  				(*temp)(x, y, z) += ambient/3.0;

  				(*result)(x, y, z) += ambient/3.0;

  				if(ambient > 3.0)

  					cout<<"error"<<endl;

  			}

  	(*temp) = 0.0f;

  	temp->setBoundary(1.0);

  	cout<<"done."<<endl;

  

  	cout<<"seventh iteration..."<<endl;

  	for(x=m_resolution.x-1; x>=0; x--)

  		for(y=m_resolution.y-1; y>=0; y--)

  			for(z=0; z<m_resolution.z; z++)

  			{

  				ambient = 0.0;

  				if(xyz(x+1, y, z) < threshold)

  					ambient += (*temp)(x+1, y, z)*(1.0 - (xyz(x+1, y, z)/255.0)*attenuation);

  				if(xyz(x, y+1, z) < threshold)

  					ambient += (*temp)(x, y+1, z)*(1.0 - (xyz(x, y+1, z)/255.0)*attenuation);

  				if(xyz(x, y, z-1) < threshold)

  					ambient += (*temp)(x, y, z-1)*(1.0 - (xyz(x, y, z-1)/255.0)*attenuation);

  

  				(*temp)(x, y, z) += ambient/3.0;

  				(*result)(x, y, z) += ambient/3.0;

  				if(ambient > 3.0)

  					cout<<"error"<<endl;

  			}

  	(*temp) = 0.0f;

  	temp->setBoundary(1.0);

  	cout<<"done."<<endl;

  

  	cout<<"eighth iteration..."<<endl;

  	for(x=m_resolution.x-1; x>=0; x--)

  		for(y=m_resolution.y-1; y>=0; y--)

  			for(z=m_resolution.z-1; z>=0; z--)

  			{

  				ambient = 0.0;

  				if(xyz(x+1, y, z) < threshold)

  					ambient += (*temp)(x+1, y, z)*(1.0 - (xyz(x+1, y, z)/255.0)*attenuation);

  				if(xyz(x, y+1, z) < threshold)

  					ambient += (*temp)(x, y+1, z)*(1.0 - (xyz(x, y+1, z)/255.0)*attenuation);

  				if(xyz(x, y, z+1) < threshold)

  					ambient += (*temp)(x, y, z+1)*(1.0 - (xyz(x, y, z+1)/255.0)*attenuation);

  

  				(*temp)(x, y, z) += ambient/3.0;

  				(*result)(x, y, z) += ambient/3.0;

  				if(ambient > 3.0)

  					cout<<"error"<<endl;

  			}

  	(*temp) = 0.0f;

  	temp->setBoundary(1.0);

  	cout<<"done."<<endl;

  

  	(*result)/=8.0;

  	

  	return result;

  }

  

  template <class T>

  rtsImplicit3D<float>* rtsImplicit3D<T>::Isodistance_Manhattan(T isovalue, bool sdf)

  {

  	rtsImplicit3D<float>* function;

  	rtsImplicit3D<bool>* mask;

  	compute_distance_function_boundary(isovalue, function, mask, sdf);

  

  	//compute the manhattan distance for the entire function

  	//use fast sweeping to compute the manhattan distance

  	//0:X  0:Y  0:Z

  	cout<<"first iteration..."<<endl;

  	int x,y,z;

  	for(x=0; x<m_resolution.x; x++)

  		for(y=0; y<m_resolution.y; y++)

  			for(z=0; z<m_resolution.z; z++)

  				//if the current point is not a boundary value

  				if(!(*mask)(x, y, z))

  					(*function)(x,y,z) = manhattan_distance(function, point3D<indextype>(x, y, z), sdf);

  	cout<<"done."<<endl;

  	cout<<"second iteration..."<<endl;

  	//0:X 0:Y Z:0

  	for(x=0; x<m_resolution.x; x++)

  		for(y=0; y<m_resolution.y; y++)

  			for(z=m_resolution.z-1; z>=0; z--)

  				//if the current point is not a boundary value

  				if(!(*mask)(x, y, z))

  					(*function)(x,y,z) = manhattan_distance(function, point3D<indextype>(x, y, z), sdf);

  	cout<<"done."<<endl;

  	cout<<"third iteration..."<<endl;

  	//0:X Y:0 0:Z

  	for(x=0; x<m_resolution.x; x++)

  		for(y=m_resolution.y-1; y>=0; y--)

  			for(z=0; z<m_resolution.z; z++)

  				//if the current point is not a boundary value

  				if(!(*mask)(x, y, z))

  					(*function)(x,y,z) = manhattan_distance(function, point3D<indextype>(x, y, z), sdf);

  	cout<<"done."<<endl;

  	cout<<"fourth iteration..."<<endl;

  	//0:X Y:0 Z:0

  	for(x=0; x<m_resolution.x; x++)

  		for(y=m_resolution.y-1; y>=0; y--)

  			for(z=m_resolution.z-1; z>=0; z--)

  				//if the current point is not a boundary value

  				if(!(*mask)(x, y, z))

  					(*function)(x,y,z) = manhattan_distance(function, point3D<indextype>(x, y, z), sdf);

  	cout<<"done."<<endl;

  	cout<<"fifth iteration..."<<endl;

  	//X:0 0:Y 0:Z

  	for(x=m_resolution.x-1; x>=0; x--)

  		for(y=0; y<m_resolution.y; y++)

  			for(z=0; z<m_resolution.z; z++)

  				//if the current point is not a boundary value

  				if(!(*mask)(x, y, z))

  					(*function)(x,y,z) = manhattan_distance(function, point3D<indextype>(x, y, z), sdf);

  	cout<<"done."<<endl;

  	cout<<"sixth iteration..."<<endl;

  	//X:0 0:Y Z:0

  	for(x=m_resolution.x-1; x>=0; x--)

  		for(y=0; y<m_resolution.y; y++)

  			for(z=m_resolution.z-1; z>=0; z--)

  				//if the current point is not a boundary value

  				if(!(*mask)(x, y, z))

  					(*function)(x,y,z) = manhattan_distance(function, point3D<indextype>(x, y, z), sdf);

  	cout<<"done."<<endl;

  	cout<<"seventh iteration..."<<endl;

  	//X:0 Y:0 0:Z

  	for(x=m_resolution.x-1; x>=0; x--)

  		for(y=m_resolution.y-1; y>=0; y--)

  			for(z=0; z<m_resolution.z; z++)

  				//if the current point is not a boundary value

  				if(!(*mask)(x, y, z))

  					(*function)(x,y,z) = manhattan_distance(function, point3D<indextype>(x, y, z), sdf);

  	cout<<"done."<<endl;

  	cout<<"eighth iteration..."<<endl;

  	//X:0 Y:0 Z:0

  	for(x=m_resolution.x-1; x>=0; x--)

  		for(y=m_resolution.y-1; y>=0; y--)

  			for(z=m_resolution.z-1; z>=0; z--)

  				//if the current point is not a boundary value

  				if(!(*mask)(x, y, z))

  					(*function)(x,y,z) = manhattan_distance(function, point3D<indextype>(x, y, z), sdf);

  	cout<<"done."<<endl;

  	

  

  	return function;

  }

  

  //computes the gradient along all three dimensions and returns a vector field

  template <class T>

  rtsImplicit3D<vector3D<T>>* rtsImplicit3D<T>::Gradient()

  {

  	int x, y, z;

  	rtsImplicit3D<vector3D<T>>* result = new rtsImplicit3D<vector3D<T>>(m_resolution.x, m_resolution.y, m_resolution.z);

  	for(x=0; x<m_resolution.x; x++)

  		for(y=0; y<m_resolution.y; y++)

  			for(z=0; z<m_resolution.z; z++)

  			{

  				result->xyz(x, y, z).x = xyz(x-1, y, z) - xyz(x, y, z);

  				result->xyz(x, y, z).y = xyz(x, y-1, z) - xyz(x, y, z);

  				result->xyz(x, y, z).z = xyz(x, y, z-1) - xyz(x, y, z);

  			}

  	return result;

  }

  

  

  template <class T>

  int rtsImplicit3D<T>::Neighbors26(indextype x, indextype y, indextype z, T isovalue)

  {

  	int neighbors = 0;

  	int u,v,w;

  

  	for(u=-1; u<=1; u++)

  		for(v=-1; v<=1; v++)

  			for(w=-1; w<=1; w++)

  				if(xyz(x+u, y+v, z+w) >= isovalue)

  					neighbors++;

  	if(xyz(x, y, z) > isovalue)

  		neighbors--;

  

  	return neighbors;

  }

  

  template <class T>

  unsigned int rtsImplicit3D<T>::Neighbors6(indextype x, indextype y, indextype z, T threshold)

  {

  

  	unsigned int neighbors = 0;

  	if(xyz(x+1, y, z) >= threshold)

  		neighbors++;

  	if(xyz(x-1, y, z) >= threshold)

  		neighbors++;

  	if(xyz(x, y+1, z) >= threshold)

  		neighbors++;

  	if(xyz(x, y-1, z) >= threshold)

  		neighbors++;

  	if(xyz(x, y, z+1) >= threshold)

  		neighbors++;

  	if(xyz(x, y, z-1) >= threshold)

  		neighbors++;

  

  	return neighbors;

  }

  

  template <class T>

  bool rtsImplicit3D<T>::TestTopology(T isovalue, unsigned int x, unsigned int y, unsigned int z)

  {

  	if(xyz(x,y,z) < isovalue)

  		return false;

  	//This function returns true if a voxel is necessary, otherwise it returns false

  	unsigned int neighbors = Neighbors(x, y, z, isovalue);

  

  	if(neighbors == 3)

  		return false;

  	if(neighbors == 0 || neighbors == 1 || neighbors == 4)

  		return true;

  	if(neighbors == 2)

  	{

  		if(xyz(x-1, y, z) >= isovalue && xyz(x+1, y, z) >= isovalue)

  			return true;

  		if(xyz(x, y-1, z) >= isovalue && xyz(x, y+1, z) >= isovalue)

  			return true;

  		return false;

  	}

  	

  }

  

  template <class T>

  void rtsImplicit3D<T>::FloodFill6(indextype x, indextype y, indextype z, T target_value)

  {

  	T old_value = xyz(x, y, z);					//find the old value (the value being flood-filled)

  	if(target_value == old_value)				//if the target value is the same as the old value, nothing to do

  		return;

  

  	queue<point3D<indextype>> Q;				//create a queue for neighboring points

  	point3D<indextype> current(x, y, z);		//start with the current point

  	xyz(current.x, current.y, current.z) = target_value;

  	point3D<indextype> next;

  	Q.push(current);

  	indextype u, v, w;

  

  	while(!Q.empty())							//continue until the queue is empty

  	{

  		current = Q.front();					//get the first element from the queue

  		Q.pop();							

  		

  		if(current.x != m_resolution.x - 1)

  			if(xyz(current.x + 1, current.y, current.z) == old_value)

  			{

  				xyz(current.x + 1, current.y, current.z) = target_value;

  				Q.push(point3D<indextype>(current.x + 1, current.y, current.z));

  			}

  		if(current.x != 0)

  			if(xyz(current.x - 1, current.y, current.z) == old_value)

  			{

  				xyz(current.x - 1, current.y, current.z) = target_value;

  				Q.push(point3D<indextype>(current.x - 1, current.y, current.z));

  			}

  		if(current.y != m_resolution.y - 1)

  			if(xyz(current.x, current.y +1, current.z) == old_value)

  			{

  				xyz(current.x, current.y+1, current.z) = target_value;

  				Q.push(point3D<indextype>(current.x, current.y+1, current.z));

  			}

  		if(current.y != 0)

  			if(xyz(current.x, current.y-1, current.z) == old_value)

  			{

  				xyz(current.x, current.y-1, current.z) = target_value;

  				Q.push(point3D<indextype>(current.x, current.y-1, current.z));

  			}

  		if(current.z != m_resolution.z - 1)

  			if(xyz(current.x, current.y, current.z+1) == old_value)

  			{

  				xyz(current.x, current.y, current.z+1) = target_value;

  				Q.push(point3D<indextype>(current.x, current.y, current.z+1));

  			}

  		if(current.z != 0)

  			if(xyz(current.x, current.y, current.z-1) == old_value)

  			{

  				xyz(current.x, current.y, current.z-1) = target_value;

  				Q.push(point3D<indextype>(current.x, current.y, current.z-1));

  			}

  

  	}

  

  }

  

  template <class T>

  void rtsImplicit3D<T>::FloodFill26(int x, int y, int z, T target_value)

  {

  	T old_value = xyz(x, y, z);

  	if(target_value == old_value)

  		return;

  

  	queue<point3D<indextype>> Q;

  	point3D<indextype> current(x, y, z);

  	point3D<indextype> next;

  	Q.push(current);

  	indextype u, v, w;

  	while(!Q.empty())

  	{

  		current = Q.front();

  		if(xyz(current.x, current.y, current.z) == old_value)

  			xyz(current.x, current.y, current.z) = target_value;

  		Q.pop();

  		for(u=-1; u<=1; u++)

  			for(v=-1; v<=1; v++)

  				for(w=-1; w<=1; w++)

  				{

  					next.x = current.x + u;

  					next.y = current.y + v;

  					next.z = current.z + w;

  

  					if(next.x >= 0 && next.x < m_resolution.x &&

  						next.y >= 0 && next.y < m_resolution.y &&

  						next.z >= 0 && next.z < m_resolution.z &&

  						xyz(next.x, next.y, next.z) == old_value)

  						{

  							xyz(next.x, next.y, next.z) = target_value;

  							Q.push(next);

  						}

  				}

  	}

  

  				

  }

  

  template <class T>

  void rtsImplicit3D<T>::Binary(T threshold, T true_value)

  {

  	/**

  	This function converts an implicit function into a binary or characteristic function describing the solid represented by the level

  	set at isovalue "threshold".  All values below threshold are set to zero while all values above threshold are set to the specified

  	"true_value".  In order to use this function, the data type T must be able to be set to 0.

  	**/

  	int max_index = m_resolution.x * m_resolution.y * m_resolution.z;	//find the size of the data array

  	int i;

  	for(i=0; i<max_index; i++)

  		if(m_data[i] >= threshold)

  			m_data[i] = true_value;

  		else

  			m_data[i] = 0;

  }

  

  template <class T>

  vector<point3D<indextype>> rtsImplicit3D<T>::getEdgeNodes(T isovalue, bool protrusions = true)

  {

  	vector<point3D<indextype>> result;

  	indextype x, y, z;

  	int neighbors;

  	for(x=0; x<m_resolution.x; x++)

  		for(y=0; y<m_resolution.y; y++)

  			for(z=0; z<m_resolution.z; z++)

  			{

  				if(xyz(x, y, z) >= isovalue)

  				{

  					neighbors = Neighbors26(x, y, z, isovalue);

  					if(protrusions == false && neighbors < 1)

  						continue;

  					if(neighbors < 26)

  						result.push_back(point3D<indextype>(x, y, z));

  				}

  			}

  

  	return result;

  

  }

  

  template <class T>

  unsigned int rtsImplicit3D<T>::BackgroundComponents6(indextype x, indextype y, indextype z, T threshold, int n = 18)

  {

  	/**

  	This function computes the number of 6-connected background components in the local region of (x, y, z).

  	This computation is performed by testing all 6 possible voxels that can connect to the specified node.  If

  	a background node is found, the entire background component associated with that node is filled and the counter

  	is incremented by 1.  The value n specifies the connectivity domain for the flood fill.

  	The definition of background components is that specified by He, Kischell, Rioult and Holmes.

  	**/

  

  	//see if there is at least one BG component

  	if(Neighbors6(x, y, z, threshold) == 6)

  		return 0;

  

  	

  	//retrieve the local region of the function

  	rtsImplicit3D<T> local(3, 3, 3);

  	point3D<indextype> corner(x-1, y-1, z-1);

  	indextype u, v, w;

  	for(u=0; u<3; u++)

  		for(v=0; v<3; v++)

  			for(w=0; w<3; w++)

  				local(u, v, w) = xyz(corner.x + u, corner.y + v, corner.z + w);

  

  	//threshold the background to find inside/outside points

  	local.Binary(threshold, 1);

  	//fill points that are not in the connectivity domain

  	if(n == 18)

  	{

  		local(0, 0, 0) = 1;

  		local(0, 0, 2) = 1;

  		local(0, 2, 0) = 1;

  		local(0, 2, 2) = 1;

  		local(2, 0, 0) = 1;

  		local(2, 0, 2) = 1;

  		local(2, 2, 0) = 1;

  		local(2, 2, 2) = 1;

  	}

  	//local.toConsole();

  

  	//search all 6 possible connected points.  If a background node is found, fill the component

  	unsigned int components = 0;

  	if(local(0, 1, 1) == 0)

  	{

  		components++;

  		local.FloodFill6(0, 1, 1, 1);

  	}

  	if(local(2, 1, 1) == 0)

  	{

  		components++;

  		local.FloodFill6(2, 1, 1, 1);

  	}

  	if(local(1, 0, 1) == 0)

  	{

  		components++;

  		local.FloodFill6(1, 0, 1, 1);

  	}

  	if(local(1, 2, 1) == 0)

  	{

  		components++;

  		local.FloodFill6(1, 2, 1, 1);

  	}

  	if(local(1, 1, 0) == 0)

  	{

  		components++;

  		local.FloodFill6(1, 1, 0, 1);

  	}

  	if(local(1, 1, 2) == 0)

  	{

  		components++;

  		local.FloodFill6(1, 1, 2, 1);

  	}

  

  	return components;

  }

  

  template <class T>

  rtsImplicit3D<T> rtsImplicit3D<T>::Project2D()

  {

  	/**

  	This function projects the entire 3D function onto a 2D function along the z-axis.

  	**/

  	rtsImplicit3D<T> result(m_resolution.x, m_resolution.y, 1);

  	result = 0;

  	

  	indextype x, y, z;

  	for(x = 0; x<m_resolution.x; x++)

  		for(y=0; y<m_resolution.y; y++)

  			for(z=0; z<m_resolution.z; z++)

  			{

  				if(result(x, y, 0) < xyz(x, y, z))

  					result(x, y, 0) = xyz(x, y, z);

  			}

  	return result;

  }

  

  template <class T>

  void rtsImplicit3D<T>::Erode(T isovalue, T fill_value)

  {

  	vector<point3D<indextype>> border_nodes;	//get the border nodes for the image

  	indextype x, y, z;

  	int condition;

  	for(x=0; x<m_resolution.x; x++)

  		for(y=0; y<m_resolution.y; y++)

  			for(z=0; z<m_resolution.z; z++)

  				if(xyz(x, y, z) >= isovalue && BackgroundComponents6(x, y, z, isovalue) == 1)

  				{

  					condition = 0;

  					//now find the border pairs.  A border point must meet two of the following conditions to be a border pair.

  					//south border: s(p) is background

  					if(xyz(x, y-1, z) < isovalue)

  						condition++;

  					//north border: n(p) is background, s(p) and s(s(p)) are foreground

  					if(xyz(x, y+1, z) < isovalue && xyz(x, y-1, z) >= isovalue && xyz(x, y-2, z) >= isovalue)

  						condition++;

  					//west border: w(p) is background

  					if(xyz(x-1, y, z) < isovalue)

  						condition++;

  					//east border: e(p) is background, w(p) and w(w(p)) are foreground

  					if(xyz(x+1, y, z) < isovalue && xyz(x-1, y, z) >= isovalue && xyz(x-2, y, z) >= isovalue)

  						condition++;

  					//up border: u(p) is background

  					if(xyz(x, y, z-1) < isovalue)

  						condition++;

  					//down border: d(p) is background, u(p) and u(u(p)) are foreground

  					if(xyz(x, y, z+1) < isovalue && xyz(x, y, z-1) >= isovalue && xyz(x, y, z-2) >= isovalue)

  						condition++;

  					

  					if(condition > 1)

  						border_nodes.push_back(point3D<indextype>(x, y, z));

  				}

  	cout<<"Number of border nodes: "<<border_nodes.size()<<endl;

  	

  	vector<point3D<indextype>>::iterator i;

  	for(i=border_nodes.begin(); i!= border_nodes.end(); i++)

  		xyz((*i).x, (*i).y, (*i).z) = fill_value;

  

  

  }

  

  template <class T>

  void rtsImplicit3D<T>::ClampMax(T max)

  {

  	int i;

  	int elements = m_resolution.x * m_resolution.y * m_resolution.z;

  	for(i=0; i<elements; i++)

  		if(m_data[i] > max)

  			m_data[i] = max;

  }

  

  template <class T>

  void rtsImplicit3D<T>::ClampMin(T min)

  {

  	int i;

  	int elements = m_resolution.x * m_resolution.y * m_resolution.z;

  	for(i=0; i<elements; i++)

  		if(m_data[i] < min)

  			m_data[i] = min;

  }

  

  template <class T>

  void rtsImplicit3D<T>::ClampMin(T min, T value)

  {

  	int i;

  	int elements = m_resolution.x * m_resolution.y * m_resolution.z;

  	for(i=0; i<elements; i++)

  		if(m_data[i] < min)

  			m_data[i] = value;

  }

  

  template <class T>

  void rtsImplicit3D<T>::MedianFilter(int dist_x, int dist_y, int dist_z, double factor = 0.5)

  {

  	rtsImplicit3D<T> result = (*this);

  	indextype x, y, z;

  	indextype min_x, min_y, min_z;

  	indextype max_x, max_y, max_z;

  	indextype u, v, w;

  	vector<T> region;

  	for(x=0; x<m_resolution.x; x++)

  		for(y=0; y<m_resolution.y; y++)

  			for(z=0; z<m_resolution.z; z++)

  			{

  				region.clear();

  				min_x = x - dist_x;

  				min_y = y - dist_y;

  				min_z = z - dist_z;

  				max_x = x + dist_x;

  				max_y = y + dist_y;

  				max_z = z + dist_z;

  

  				for(u=min_x; u<=max_x; u++)

  					for(v=min_y; v<=max_y; v++)

  						for(w=min_z; w<=max_z; w++)

  						{

  							region.push_back(xyz(u, v, w));

  						}

  				sort(region.begin(), region.end());

  				result(x, y, z) = region[(int)(region.size()*factor)];

  			}

  	(*this) = result;

  }

  

  template <class T>

  unsigned int rtsImplicit3D<T>::Thin(T isovalue)

  {

  	/**

  	This function computes the skeleton of an isosurface embedded in the implicit function and

  	described by the "isovalue" parameter.

  	**/

  

  	vector<point3D<indextype>> border_nodes;	//get the border nodes for the image

  	indextype x, y, z;

  	int condition;

  	for(x=0; x<m_resolution.x; x++)

  		for(y=0; y<m_resolution.y; y++)

  			for(z=0; z<m_resolution.z; z++)

  				//find the border nodes

  				if(xyz(x, y, z) >= isovalue && BackgroundComponents6(x, y, z, isovalue) == 1 && Neighbors26(x, y, z, isovalue) != 1)

  				{

  					condition = 0;

  					//now find the border pairs.  A border point must meet two of the following conditions to be a border pair.

  					//south border: s(p) is background

  					if(xyz(x, y-1, z) < isovalue)

  						condition++;

  					//north border: n(p) is background, s(p) and s(s(p)) are foreground

  					if(xyz(x, y+1, z) < isovalue && xyz(x, y-1, z) >= isovalue && xyz(x, y-2, z) >= isovalue)

  						condition++;

  					//west border: w(p) is background

  					if(xyz(x-1, y, z) < isovalue)

  						condition++;

  					//east border: e(p) is background, w(p) and w(w(p)) are foreground

  					if(xyz(x+1, y, z) < isovalue && xyz(x-1, y, z) >= isovalue && xyz(x-2, y, z) >= isovalue)

  						condition++;

  					//up border: u(p) is background

  					if(xyz(x, y, z-1) < isovalue)

  						condition++;

  					//down border: d(p) is background, u(p) and u(u(p)) are foreground

  					if(xyz(x, y, z+1) < isovalue && xyz(x, y, z-1) >= isovalue && xyz(x, y, z-2) >= isovalue)

  						condition++;

  					

  					if(condition > 1)

  						border_nodes.push_back(point3D<indextype>(x, y, z));

  				}

  	cout<<"Number of border nodes: "<<border_nodes.size()<<endl;

  

  	//determine if each edge node can be removed without changing the topology of the model

  	//declare some initial variables

  	rtsImplicit3D<T> local(3, 3, 3);	//store the region local to the current voxel

  	int nodes_before, nodes_after;		//number of neighboring nodes before and after the filling operation

  	point3D<indextype> fill_start;

  	vector<point3D<indextype>>::iterator i;

  

  	/*

  	Here we determine if a point can be removed by looking at the number of foreground connected

  	components in the local region.  If there is more than one connected component

  	*/

  	unsigned int removed = 0;

  	for(i=border_nodes.begin(); i<border_nodes.end(); i++)

  	{

  		//get the local region around the current point

  		for(x=-1; x<=1; x++)

  			for(y=-1; y<=1; y++)

  				for(z=-1; z<=1; z++)

  					local(x+1, y+1, z+1) = xyz((*i).x + x, (*i).y + y, (*i).z + z);

  

  		//deal with the degenerate case of all four sides being internal

  		//if(local(0, 1, 0) >= isovalue && local(1, 0, 0) >= isovalue && local(1, 2, 0) >= isovalue && local(2, 1, 0) >= isovalue)

  		//	continue;

  		local(1, 1, 1) = 0;				//remove the center voxel

  		local.Binary(isovalue, 1);

  		nodes_before = local.Neighbors26(1, 1, 1, 1);

  		//if(nodes_before == 1)			//prevent reducing ends

  		//	continue;

  

  		//find an interior voxel to fill

  		for(x=0; x<3; x++)

  			for(y=0; y<3; y++)

  				for(z=0; z<3; z++)

  					if(local(x, y, z) > 0)

  						fill_start = point3D<indextype>(x, y, z);

  

  		//fill the local region

  		local.FloodFill26(fill_start.x, fill_start.y, fill_start.z, 2);

  		//get the number of filled neighbors

  		nodes_after = local.Neighbors26(1, 1, 1, 2);

  		if(nodes_after == nodes_before)

  		{

  			xyz((*i).x, (*i).y, (*i).z) = 0;

  			removed++;

  			//cout<<"removed"<<endl;

  		}

  		//else

  		//{

  			/*for(x=-1; x<=1; x++)

  			for(y=-1; y<=1; y++)

  				for(z=-1; z<=1; z++)

  					local(x+1, y+1, z+1) = xyz((*i).x + x, (*i).y + y, (*i).z + z);

  			local.toConsole();

  			cout<<"not removed:  "<<nodes_before<<"  "<<nodes_after<<endl;

  			xyz((*i).x, (*i).y, (*i).z) = 100;

  			char c;

  			cin>>c;*/

  		//}

  	}

  	return removed;

  }

  

  /*Shape functions*/

  /*These functions create implicit shapes in the function.

  Generally, the shape is based on the parameterization.*/

  template <class T>

  void rtsImplicit3D<T>::Sphere(double center_i, double center_j, double center_k, double radius, T in_value)

  {

  	point3D<double> center(center_i, center_j, center_k);

  	//for each point in the function

  	indextype x, y, z;

  	double radius_squared = radius*radius;

  	vector3D<double> point_to_point;

  	point3D<double> result;

  	double distance_squared;

  

  	for(x=0; x<m_resolution.x; x++)

  		for(y=0; y<m_resolution.y; y++)

  			for(z=0; z<m_resolution.z; z++)

  			{

  				//get the parameterized value

  				result = getParameter(x, y, z);

  				//find the distance between the center of the sphere and the resulting point

  				//double distance = (result - center).Length();

  				point_to_point = result - center;

  				distance_squared = point_to_point*point_to_point;

  				//if the distance is less than the radius, the point is inside the sphere

  				if(distance_squared < radius_squared)

  					xyz(x, y, z) = in_value;

  			}

  			

  }

  

  

  template <class T>

  void rtsImplicit3D<T>::toConsole()

  {

  	cout<<endl;

  	int x, y, z;

  	for(z=0; z<m_resolution.z; z++)

  	{

  		for(y=0; y<m_resolution.y; y++)

  		{

  			for(x=0; x<m_resolution.x; x++)

  			{

  				cout.width(7);

  				cout.precision(3);

  				cout<<(double)xyz(x, y, z);

  			}

  			cout<<endl;

  		}

  		cout<<"-----------------------------"<<endl;

  	}

  

  }

  

  

  #endif