File: ccPointDescriptor.cpp

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 (785 lines) | stat: -rw-r--r-- 20,218 bytes parent folder | download | duplicates (2)
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
//##########################################################################
//#                                                                        #
//#                     CLOUDCOMPARE PLUGIN: qCANUPO                       #
//#                                                                        #
//#  This program is free software; you can redistribute it and/or modify  #
//#  it under the terms of the GNU General Public License as published by  #
//#  the Free Software Foundation; version 2 or later of the License.      #
//#                                                                        #
//#  This program is distributed in the hope that it will be useful,       #
//#  but WITHOUT ANY WARRANTY; without even the implied warranty of        #
//#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the          #
//#  GNU General Public License for more details.                          #
//#                                                                        #
//#      COPYRIGHT: UEB (UNIVERSITE EUROPEENNE DE BRETAGNE) / CNRS         #
//#                                                                        #
//##########################################################################

#include "ccPointDescriptor.h"

//qCC_db
#include <ccPointCloud.h>
#include <ccScalarField.h>

//CCLib
#include <Neighbourhood.h>
#include <Jacobi.h>

//Qt
#include <QMap>

//system
#include <fstream>

/**** SCALE PARAMETERS COMPUTERS ****/
/*									*/
/*  PUT THE CODE OF YOUR OWN BELOW  */
/*  THEN DECLARE IT IN THE "VAULT"  */
/*  (SEE ScaleParamsComputerVault)  */
/*									*/
/************************************/

//useful constant
static const double SQRT_3_DIV_2 = sqrt(3.0)/2;

//! Per-scale "dimensionality" parameters computer (i.e. same as the original CANUPO suite)
class DimensionalityScaleParamsComputer : public ScaleParamsComputer
{
public:

	//! Default constructor
	DimensionalityScaleParamsComputer() : m_firstScale(true) {}

	//inherited from ScaleParamsComputer
	virtual unsigned getID() const { return DESC_DIMENSIONALITY; }

	//inherited from ScaleParamsComputer
	virtual QString getName() const { return "Dimensionality"; }

	//inherited from ScaleParamsComputer
	virtual unsigned dimPerScale() const { return 2; }

	//inherited from ScaleParamsComputer
	virtual void reset()
	{
		//a = 1/3, b = 1/3
		//c = 1 - a - b = 1/3
		//x = b + c/2 = 1/2
		//y = c * SQRT(3)/2 = SQRT(3)/2 * 1/3

		m_defaultParams[0] = 0.5;
		m_defaultParams[1] = SQRT_3_DIV_2 / 3;

		m_firstScale = true;
	}

	//inherited from ScaleParamsComputer
	virtual bool computeScaleParams(CCLib::ReferenceCloud& neighbors, double radius, float params[], bool& invalidScale)
	{
		//PCA analysis
		if (neighbors.size() >= 3)
		{
			CCLib::Neighbourhood Z(&neighbors);

			CCLib::SquareMatrixd eigVectors;
			std::vector<double> eigValues;
			if (Jacobi<double>::ComputeEigenValuesAndVectors(Z.computeCovarianceMatrix(), eigVectors, eigValues, true))
			{
				Jacobi<double>::SortEigenValuesAndVectors(eigVectors, eigValues); //decreasing order of their associated eigenvalues

				double totalVariance = 0;
				CCVector3d sValues(0, 0, 0);
				{
					// contrarily to Brodu's version, here we get directly the eigenvalues!
					for (unsigned j = 0; j < 3; ++j)
					{
						sValues.u[j] = eigValues[j];
						totalVariance += sValues.u[j];
					}
				}
				assert(totalVariance != 0);
				sValues /= totalVariance;

				// Use barycentric coordinates : a for 1D, b for 2D and c for 3D
				// Formula on wikipedia page for barycentric coordinates
				// using directly the triangle in %variance space, they simplify a lot
				double a = std::min<double>(1.0, std::max<double>(0.0, sValues.x - sValues.y));
				double b = std::min<double>(1.0, std::max<double>(0.0, 2 * sValues.x + 4 * sValues.y - 2.0));
				double c = 1.0 - a - b;
				// see original Brodu's code for this transformation
				params[0] = static_cast<float>(b + c / 2);
				params[1] = static_cast<float>(c * SQRT_3_DIV_2);

				//save parameters for next scale
				m_defaultParams[0] = params[0];
				m_defaultParams[1] = params[1];
				m_firstScale = false;
			}
			else if (m_firstScale) //PCA failed at first scale?!
			{
				invalidScale = true;
				params[0] = m_defaultParams[0];
				params[1] = m_defaultParams[1];
			}
		}
		else if (m_firstScale) //less than 3 points at the biggest scale?!
		{
			invalidScale = true;
			params[0] = m_defaultParams[0];
			params[1] = m_defaultParams[1];
		}

		return true;
	}

protected:

	//! Default parameters (or last computed scale's ones!)
	float m_defaultParams[2];
	//! First scale flag
	bool m_firstScale;
};

#ifdef COMPILE_PRIVATE_CANUPO

//! Per-scale "dimensionality" + mean scalar field value parameters computer
class DimensionalityAndSFScaleParamsComputer : public ScaleParamsComputer
{
public:

	//! Default constructor
	DimensionalityAndSFScaleParamsComputer() : m_firstScale(true) {}

	//inherited from ScaleParamsComputer
	virtual unsigned getID() const { return DESC_DIMENSIONALITY_SF; }

	//inherited from ScaleParamsComputer
	virtual QString getName() const { return "Dimensionality + SF"; }

	//inherited from ScaleParamsComputer
	virtual unsigned dimPerScale() const { return 3; }

	//inherited from ScaleParamsComputer
	virtual bool needSF() const { return true; }

	//inherited from ScaleParamsComputer
	virtual void reset()
	{
		//a = 1/3, b = 1/3
		//c = 1 - a - b = 1/3
		//x = b + c/2 = 1/2
		//y = c * SQRT(3)/2 = SQRT(3)/2 * 1/3
		m_defaultParams[0] = 0.5;
		m_defaultParams[1] = SQRT_3_DIV_2 / 3;

		//mean SF value
		m_defaultParams[2] = 0.0;

		m_firstScale = true;
	}

	//inherited from ScaleParamsComputer
	virtual bool computeScaleParams(CCLib::ReferenceCloud& neighbors, double radius, float params[], bool& invalidScale)
	{
		//PCA analysis
		if (neighbors.size() >= 3)
		{
			//first compute the mean SF value
			unsigned validCount = 0;
			double meanVal = 0;
			for (unsigned i=0; i<neighbors.size(); ++i)
			{
				ScalarType val = neighbors.getPointScalarValue(i);
				if (CCLib::ScalarField::ValidValue(val))
				{
					meanVal += val;
					++validCount;
				}
			}

			if (validCount)
			{
				m_defaultParams[2] = static_cast<float>(meanVal / validCount);
			}
			else
			{
				//not enough (valid) SF values
				invalidScale = true;
				params[0] = m_defaultParams[0];
				params[1] = m_defaultParams[1];
				params[2] = m_defaultParams[2];
				return true;
			}

			CCLib::Neighbourhood Z(&neighbors);

			CCLib::SquareMatrixd eigVectors;
			std::vector<double> eigValues;
			if (Jacobi<double>::ComputeEigenValuesAndVectors(Z.computeCovarianceMatrix(), eigVectors, eigValues, true))
			{
				Jacobi<double>::SortEigenValuesAndVectors(eigVectors, eigValues); //decreasing order of their associated eigenvalues

				double totalVariance = 0;
				CCVector3d sValues(0, 0, 0);
				{
					// contrarily to Brodu's version, here we get directly the eigenvalues!
					for (unsigned j = 0; j < 3; ++j)
					{
						sValues.u[j] = eigValues(j);
						totalVariance += sValues.u[j];
					}
				}
				assert(totalVariance != 0);
				sValues /= totalVariance;

				// Use barycentric coordinates : a for 1D, b for 2D and c for 3D
				// Formula on wikipedia page for barycentric coordinates
				// using directly the triangle in %variance space, they simplify a lot
				double a = std::min<double>(1.0, std::max<double>(0.0, sValues.x - sValues.y));
				double b = std::min<double>(1.0, std::max<double>(0.0, 2*sValues.x + 4*sValues.y - 2.0));
				double c = 1.0 - a - b;
				// see original Brodu's code for this transformation
				params[0] = static_cast<float>(b + c/2);
				params[1] = static_cast<float>(c * SQRT_3_DIV_2);

				//save parameters for next scale
				m_defaultParams[0] = params[0];
				m_defaultParams[1] = params[1];
				m_defaultParams[2] = params[2];
				m_firstScale = false;
			}
			else if (m_firstScale) //PCA failed at first scale?!
			{
				invalidScale = true;
				params[0] = m_defaultParams[0];
				params[1] = m_defaultParams[1];
				params[2] = m_defaultParams[2];
			}
		}
		else if (m_firstScale) //less than 3 points at the biggest scale?!
		{
			invalidScale = true;
			params[0] = m_defaultParams[0];
			params[1] = m_defaultParams[1];
			params[2] = m_defaultParams[2];
		}

		return true;
	}

protected:

	//! Default parameters (or last computed scale's ones!)
	float m_defaultParams[3];
	//! First scale flag
	bool m_firstScale;
};

//! Per scale curvature computer
class CurvatureScaleParamsComputer : public ScaleParamsComputer
{
public:

	//! Default constructor
	CurvatureScaleParamsComputer() : m_firstScale(true) {}

	//inherited from ScaleParamsComputer
	virtual unsigned getID() const { return DESC_CURVATURE; }

	//inherited from ScaleParamsComputer
	virtual QString getName() const { return "Gaussian curvature"; }

	//inherited from ScaleParamsComputer
	virtual unsigned dimPerScale() const { return 1; }

	//inherited from ScaleParamsComputer
	virtual void reset()
	{
		m_defaultParams[0] = 0;
		m_firstScale = true;
	}

	//inherited from ScaleParamsComputer
	virtual bool computeScaleParams(CCLib::ReferenceCloud& neighbors, double radius, float params[], bool& invalidScale)
	{
		//Curvature
		if (neighbors.size() >= 6)
		{
			CCLib::Neighbourhood Z(&neighbors);
			params[0] = Z.computeCurvature(*neighbors.getPoint(0), CCLib::Neighbourhood::GAUSSIAN_CURV);

			//save parameters for next scale
			m_defaultParams[0] = params[0];
			m_firstScale = false;
		}
		else if (m_firstScale) //less than 6 points at the biggest scale?!
		{
			invalidScale = true;
			params[0] = m_defaultParams[0];
		}

		return true;
	}

protected:

	//! Default parameters (or last computed scale's ones!)
	float m_defaultParams[1];
	//! First scale flag
	bool m_firstScale;
};

//! Custom parameters computer (TO BE REIMPLEMENTED)
/*class CustomScaleParamsComputer : public ScaleParamsComputer
{
public:

	//! Default constructor
	CustomScaleParamsComputer() : m_firstScale(true) {}

	//inherited from ScaleParamsComputer
	virtual unsigned getID() const { return DESC_CUSTOM; }

	//inherited from ScaleParamsComputer
	virtual QString getName() const { return "Custom"; }

	//inherited from ScaleParamsComputer
	virtual unsigned dimPerScale() const { return 1; }

	//inherited from ScaleParamsComputer
	virtual void reset()
	{
		//FIXME
		m_defaultParams[0] = 0;
		m_firstScale = true;
	}

	//inherited from ScaleParamsComputer
	virtual bool computeScaleParams(CCLib::ReferenceCloud& neighbors, double radius, float params[], bool& invalidScale)
	{
		//Curvature
		if (neighbors.size() >= ?)
		{
			//do something and set the right values for 'params'

			//save parameters for next scale
			m_defaultParams[0] = params[0];
			m_defaultParams[1] = params[1];
			...
			m_firstScale = false;
		}
		else if (m_firstScale) //less than 6 points at the biggest scale?!
		{
			invalidScale = true;
			params[0] = m_defaultParams[0];
			params[1] = m_defaultParams[1];
			...
		}

		return true;
	}

protected:

	//! Default parameters (or last computed scale's ones!)
	float m_defaultParams[?];
	//! First scale flag
	bool m_firstScale;
};
//*/

#endif

/**** SCALE PARAMETERS COMPUTERS VAULT ****/
/*										  */
/*   DON'T FORGET TO ADD YOUR OWN HERE!   */
/*										  */
/******************************************/

struct ScaleParamsComputerVault
{
	//! Default constructor
	ScaleParamsComputerVault()
	{
		//DEFAULT DESCRIPTOR
		map.insert(DESC_DIMENSIONALITY, new DimensionalityScaleParamsComputer);

#ifdef COMPILE_PRIVATE_CANUPO
		map.insert(DESC_DIMENSIONALITY_SF, new DimensionalityAndSFScaleParamsComputer);
		map.insert(DESC_CURVATURE, new CurvatureScaleParamsComputer);
		//ADD YOUR OWN DESCRIPTOR COMPUTERS HERE!
		//map.insert(DESC_CUSTOM, new CustomScaleParamsComputer);
		//map.insert(DESC_XXX, new XXXScaleParamsComputer);
#endif
	}

	//! Destructor
	~ScaleParamsComputerVault()
	{
		//auto delete computers
		for (QMap<unsigned, ScaleParamsComputer*>::Iterator it = map.begin(); it != map.end(); ++it)
		{
			delete it.value();
			it.value() = 0;
		}
		map.clear();
	}

	QMap<unsigned, ScaleParamsComputer*> map;
};


/********** OTHER STUFF ***********/
/*								  */
/*   NO NEED TO LOOK AT THEM ;)   */
/*								  */
/**********************************/

// Unique static instance
static ScaleParamsComputerVault s_vault;

unsigned ScaleParamsComputer::AvailableCount()
{
	return static_cast<unsigned>(s_vault.map.size());
}

ScaleParamsComputer* ScaleParamsComputer::GetByID(unsigned descID)
{
	if (s_vault.map.contains(descID))
	{
		return s_vault.map[descID];
	}
	else
	{
		//couldn't find the corresponding descriptor!
		assert(false);
		return 0;
	}
}

ScaleParamsComputer* ScaleParamsComputer::GetByIndex(unsigned index)
{
	QMap<unsigned, ScaleParamsComputer*>::Iterator it = s_vault.map.begin();
	for (unsigned i=0; i<index; ++i)
	{
		++it;
		assert(it != s_vault.map.end());
	}
	return it.value();
}

QByteArray CorePointDescSet::toByteArray() const
{
	int scaleCount = static_cast<int>(m_scales.size());
	int descCount = static_cast<int>(size());

	if (scaleCount == 0 || descCount == 0)
		return QByteArray();

	int totalSize = 4 * sizeof(int) /*header*/ + (scaleCount + descCount * scaleCount * m_dimPerScale) * sizeof(float) /*data*/;

	QByteArray data(totalSize, Qt::Uninitialized);
	
	if (data.capacity() < totalSize) //not enough memory?
		return data;

	char* buffer = data.data();

	//header
	*reinterpret_cast<int*>(buffer) = scaleCount;
	buffer += sizeof(int);
	*reinterpret_cast<int*>(buffer) = descCount;
	buffer += sizeof(int);
	*reinterpret_cast<int*>(buffer) = static_cast<int>(m_descriptorID);
	buffer += sizeof(int);
	*reinterpret_cast<int*>(buffer) = static_cast<int>(m_dimPerScale);
	buffer += sizeof(int);

	//scales
	{
		for (int i=0; i<scaleCount; ++i)
		{
			*reinterpret_cast<float*>(buffer) = m_scales[i];
			buffer += sizeof(float);
		}
	}

	//descriptors
	{
		for (int j=0; j<descCount; ++j)
		{
			const CorePointDesc& desc = at(j);
			assert(desc.params.size() == scaleCount * m_dimPerScale);
			for (size_t i=0; i<desc.params.size(); ++i)
			{
				*reinterpret_cast<float*>(buffer) = desc.params[i];
				buffer += sizeof(float);
			}
		}
	}

	//first scales
	return data;
}

bool CorePointDescSet::fromByteArray(const QByteArray& data)
{
	if (data.size() < 2*sizeof(int))
		return false;

	const char* buffer = data.data();

	//header
	int scaleCount = *reinterpret_cast<const int*>(buffer);
	buffer += sizeof(int);
	int descCount = *reinterpret_cast<const int*>(buffer);
	buffer += sizeof(int);
	int descriptorID = *reinterpret_cast<const int*>(buffer);
	buffer += sizeof(int);
	if (descriptorID <= 0)
		return false;
	m_descriptorID = static_cast<unsigned>(descriptorID);
	int dimPerScale = *reinterpret_cast<const int*>(buffer);
	buffer += sizeof(int);
	if (dimPerScale <= 0)
		return false;
	m_dimPerScale = static_cast<unsigned>(dimPerScale);

	if (scaleCount == 0 || descCount == 0)
		return false;

	//check that the array is big enough!
	int totalSize = 4 * sizeof(int) /*header*/ + (scaleCount + descCount * scaleCount * m_dimPerScale) * sizeof(float) /*data*/;
	if (data.size() < totalSize)
		return false;

	//check that we have enough memory
	std::vector<float> scales;
	try
	{
		resize(descCount);
		scales.resize(scaleCount);
	}
	catch (const std::bad_alloc&)
	{
		//not enough memory
		return false;
	}

	//scales
	{
		for (int i=0; i<scaleCount; ++i)
		{
			scales[i] = *reinterpret_cast<const float*>(buffer);
			buffer += sizeof(float);
		}

		//check that we have even more memory ;)
		if (!setScales(scales))
			return false;
	}

	//descriptors
	{
		for (int j=0; j<descCount; ++j)
		{
			CorePointDesc& desc = at(j);
			assert(desc.params.size() == scaleCount * m_dimPerScale);
			for (size_t i=0; i<scaleCount*m_dimPerScale; ++i)
			{
				desc.params[i] = *reinterpret_cast<const float*>(buffer);
				buffer += sizeof(float);
			}
		}
	}

	return true;
}

bool CorePointDescSet::setScales(const std::vector<float>& scales)
{
	assert(size() != 0);

	//same size as previous scales? Easy...
	if (scales.size() == m_scales.size())
	{
		m_scales = scales;
		return true;
	}

	//otherwise we must resize the scales vector AND the 'params' structures!
	try
	{
		m_scales = scales;
		size_t scaleCount = m_scales.size();
		
		size_t paramPerDesc = scaleCount*m_dimPerScale;
		for (size_t i=0; i<size(); ++i)
			at(i).params.resize(paramPerDesc);
	}
	catch (const std::bad_alloc&)
	{
		//not enough memory!
		return false;
	}
	return true;
}


bool CorePointDescSet::loadFromMSC(QString filename, QString& error, ccPointCloud* corePoints/*=0*/)
{
	error.clear();

	std::ifstream mscfile(qPrintable(filename), std::ifstream::binary); //DGM: warning, toStdString doesn't preserve "local" characters

	if (!mscfile.is_open())
	{
		error = "Failed to open input file";
		return false;
	}

	// read the file header
	int ncorepoints;
	mscfile.read((char*)&ncorepoints,sizeof(ncorepoints));
	int nscales_msc;
	mscfile.read((char*)&nscales_msc, sizeof(int));

	//default for CANUPO MSC files!
	m_descriptorID = DESC_DIMENSIONALITY;
	m_dimPerScale = 2;

	// allocate memory
	{
		try
		{
			resize(ncorepoints);
		}
		catch (const std::bad_alloc&)
		{
			//not enough memory
			error = "Not enough memory";
			return false;
		}

		if (corePoints)
		{
			//not enough memory to load core points!
			if (!corePoints->reserve(ncorepoints))
				corePoints = 0;
		}
	}

	//read scales
	{
		std::vector<float> scales;
		try
		{
			scales.resize(nscales_msc);
		}
		catch (const std::bad_alloc&)
		{
			//not enough memory
			error = "Not enough memory";
			return false;
		}

		for (int si=0; si<nscales_msc; ++si)
			mscfile.read((char*)&scales[si], sizeof(float));
		
		if (!setScales(scales)) //automatically resize the descriptors 'params' structure
		{
			error = "Not enough memory";
			return false;
		}
	}

	// now load the points and multiscale information from the msc file.
	// Put the points in the cloud, keep the multiscale information in a separate vector matched by point index
	int ptnparams;
	mscfile.read((char*)&ptnparams, sizeof(int));

	std::vector<CCLib::ScalarField*> paramsSf(3,0);
	if (corePoints)
	{
		//above 3, ptnparams contains additional scalars
		for (int i=3; i<ptnparams; ++i)
		{
			int sfIdx = corePoints->addScalarField(qPrintable(QString("scalar #%1").arg(i-2)));
			paramsSf.push_back(sfIdx >= 0 ? corePoints->getScalarField(sfIdx) : 0);

			if (sfIdx < 0)
			{
				//just a warning in fact
				error = "Not enough memory to import additional scalars! (they are not used for classification anyway)";
			}
		}
	}

	//vector<float> avg_ndist_max_scale(ncorepoints);
	for (int pt=0; pt<ncorepoints; ++pt)
	{
		float x,y,z;
		mscfile.read((char*)&x, sizeof(float));
		mscfile.read((char*)&y, sizeof(float));
		mscfile.read((char*)&z, sizeof(float));
		if (corePoints)
			corePoints->addPoint(CCVector3(x,y,z));
		if (ptnparams>=4)
		{
			float dummy;
			mscfile.read((char*)&dummy, sizeof(float)); //already ignored in Brodu's code...

			for (int i=4; i<ptnparams; ++i)
			{
				float param;
				mscfile.read((char*)&param, sizeof(float));
			
				if (static_cast<int>(paramsSf.size()) > i)
					paramsSf[i]->addElement(static_cast<ScalarType>(param));
			}
		}

		for (int s=0; s<nscales_msc; ++s)
		{
			float a,b;
			mscfile.read((char*)(&a), sizeof(float));
			mscfile.read((char*)(&b), sizeof(float));
			
			// see original Brodu's code for this transformation
			float c = 1.0f - a - b;
			float x = b + c/2;
			float y = c * sqrt(3.0f)/2;
			at(pt).params[s*2  ] = x;
			at(pt).params[s*2+1] = y;
		}
		// we don't care for number of neighbors at max and min scales
		{
			int dummyInt;
			for (int i=0; i<nscales_msc; ++i)
				mscfile.read((char*)&dummyInt, sizeof(int));
		}
	}

	mscfile.close();

	//take care of scalar fields
	if (corePoints)
	{
		bool first = true;
		for (size_t i=3; i<paramsSf.size(); ++i)
		{
			if (paramsSf[i])
			{
				paramsSf[i]->computeMinAndMax();
				if (first)
				{
					corePoints->setCurrentDisplayedScalarField(corePoints->getScalarFieldIndexByName(paramsSf[i]->getName()));
					corePoints->showSF(true);
					first = true;
				}
			}
		}
	}

	return true;
}