File: mlssurface.h

package info (click to toggle)
meshlab 1.3.2%2Bdfsg1-4
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 21,096 kB
  • ctags: 33,630
  • sloc: cpp: 224,813; ansic: 8,170; xml: 119; makefile: 80
file content (203 lines) | stat: -rw-r--r-- 7,253 bytes parent folder | download | duplicates (5)
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
/****************************************************************************
* MeshLab                                                           o o     *
* A versatile mesh processing toolbox                             o     o   *
*                                                                _   O  _   *
* Copyright(C) 2005                                                \/)\/    *
* Visual Computing Lab                                            /\/|      *
* ISTI - Italian National Research Council                           |      *
*                                                                    \      *
* All rights reserved.                                                      *
*                                                                           *
* 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; either version 2 of the License, or         *
* (at your option) any later version.                                       *
*                                                                           *
* This program is distributed in the hope that it will be useful,           *
* but WITHOUT ANY WARRANTY; without even the implied warranty of            *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt)          *
* for more details.                                                         *
*                                                                           *
****************************************************************************/

#ifndef MLSSURFACE_H
#define MLSSURFACE_H

#include "balltree.h"
#include <vcg/space/box3.h>
#include <vcg/math/matrix33.h>
#include <iostream>

namespace GaelMls {

enum {
	MLS_OK,
	MLS_TOO_FAR,
	MLS_TOO_MANY_ITERS,
	MLS_NOT_SUPPORTED,

	MLS_DERIVATIVE_ACCURATE,
	MLS_DERIVATIVE_APPROX,
	MLS_DERIVATIVE_FINITEDIFF
};

template<typename MeshType>
class MlsSurface
{
	public:
		typedef typename MeshType::ScalarType Scalar;
		typedef vcg::Point3<Scalar> VectorType;
		typedef vcg::Matrix33<Scalar> MatrixType;
		typedef typename MeshType::VertContainer PointsType;

		MlsSurface(const MeshType& mesh)
			: mMesh(mesh), mPoints(mesh.vert)
		{
			mCachedQueryPointIsOK = false;

			mAABB = mesh.bbox;

			// compute radii using a basic meshless density estimator
			if (!mPoints.RadiusEnabled)
			{
				const_cast<PointsType&>(mPoints).EnableRadius();
				computeVertexRaddi();
			}

			mFilterScale = 4.0;
			mMaxNofProjectionIterations = 20;
			mProjectionAccuracy = (Scalar)1e-4;
			mBallTree = 0;
			mGradientHint = MLS_DERIVATIVE_ACCURATE;
			mHessianHint = MLS_DERIVATIVE_ACCURATE;

			mDomainMinNofNeighbors = 4;
			mDomainRadiusScale = 2.;
			mDomainNormalScale = 1.;
		}

		/** \returns the value of the reconstructed scalar field at point \a x */
		virtual Scalar potential(const VectorType& x, int* errorMask = 0) const = 0;

		/** \returns the gradient of the reconstructed scalar field at point \a x
			*
			* The method used to compute the gradient can be controlled with setGradientHint().
			*/
		virtual VectorType gradient(const VectorType& x, int* errorMask = 0) const = 0;

		/** \returns the hessian matrix of the reconstructed scalar field at point \a x
			*
			* The method used to compute the hessian matrix can be controlled with setHessianHint().
			*/
		virtual MatrixType hessian(const VectorType& x, int* errorMask = 0) const
		{ if (errorMask) *errorMask = MLS_NOT_SUPPORTED; return MatrixType(); }

		/** \returns the projection of point x onto the MLS surface, and optionnaly returns the normal in \a pNormal */
		virtual VectorType project(const VectorType& x, VectorType* pNormal = 0, int* errorMask = 0) const = 0;

		/** \returns whether \a x is inside the restricted surface definition domain */
		virtual bool isInDomain(const VectorType& x) const;

		/** \returns the mean curvature from the gradient vector and Hessian matrix.
			*/
		Scalar meanCurvature(const VectorType& gradient, const MatrixType& hessian) const;

		/** set the scale of the spatial filter */
		void setFilterScale(Scalar v);
		/** set the maximum number of iterations during the projection */
		void setMaxProjectionIters(int n);
		/** set the threshold factor to detect convergence of the iterations */
		void setProjectionAccuracy(Scalar v);

		/** set a hint on how to compute the gradient
			*
			* Possible values are MLS_DERIVATIVE_ACCURATE, MLS_DERIVATIVE_APPROX, MLS_DERIVATIVE_FINITEDIFF
			*/
		void setGradientHint(int h);

		/** set a hint on how to compute the hessian matrix
			*
			* Possible values are MLS_DERIVATIVE_ACCURATE, MLS_DERIVATIVE_APPROX, MLS_DERIVATIVE_FINITEDIFF
			*/
		void setHessianHint(int h);

		inline const MeshType& mesh() const { return mMesh; }
		/** a shortcut for mesh().vert */
		inline const PointsType& points() const { return mPoints; }

		inline ConstDataWrapper<VectorType> positions() const
		{
			return ConstDataWrapper<VectorType>(&mPoints[0].cP(), mPoints.size(),
																					size_t(mPoints[1].cP().V()) - size_t(mPoints[0].cP().V()));
		}
		inline ConstDataWrapper<VectorType> normals() const
		{
			return ConstDataWrapper<VectorType>(&mPoints[0].cN(), mPoints.size(),
																					size_t(mPoints[1].cN().V()) - size_t(mPoints[0].cN().V()));
		}
		inline ConstDataWrapper<Scalar> radii() const
		{
			return ConstDataWrapper<Scalar>(&mPoints[0].cR(), mPoints.size(),
																			size_t(&mPoints[1].cR()) - size_t(&mPoints[0].cR()));
		}
		const vcg::Box3<Scalar>& boundingBox() const { return mAABB; }

		static const Scalar InvalidValue() { return Scalar(12345679810.11121314151617); }

		void computeVertexRaddi(const int nbNeighbors = 16);
	protected:
		void computeNeighborhood(const VectorType& x, bool computeDerivatives) const;
		void requestSecondDerivatives() const;

		struct PointToPointSqDist
		{
			inline bool operator()(const VectorType &a, const VectorType &b, Scalar& refD2, VectorType &q) const
			{
// 				std::cout << a.X() << a.Y() << a.Z() << "  -  " << b.X() << b.Y() << b.Z() <<
// 					" => " <<  vcg::Distance(a, b) << " < " << refD2 << "\n";
				Scalar d2 = vcg::SquaredDistance(a, b);
				if (d2>refD2)
					return false;

				refD2 = d2;
				q = a;
				return true;
			}
		};

		class DummyObjectMarker {};

	protected:
		const MeshType& mMesh;
		const PointsType& mPoints;
		vcg::Box3<Scalar> mAABB;
		int mGradientHint;
		int mHessianHint;

		BallTree<Scalar>* mBallTree;

		int mMaxNofProjectionIterations;
		Scalar mFilterScale;
		Scalar mAveragePointSpacing;
		Scalar mProjectionAccuracy;

		int mDomainMinNofNeighbors;
		float mDomainRadiusScale;
		float mDomainNormalScale;

		// cached values:
		mutable bool mCachedQueryPointIsOK;
		mutable VectorType mCachedQueryPoint;
		mutable Neighborhood<Scalar> mNeighborhood;
		mutable std::vector<Scalar> mCachedWeights;
		mutable std::vector<Scalar> mCachedWeightDerivatives;
		mutable std::vector<VectorType> mCachedWeightGradients;
		mutable std::vector<Scalar> mCachedWeightSecondDerivatives;
};

} // namespace

#include "mlssurface.tpp"

#endif // MLSSURFACE_H