File: ImplicitSurfaceContainer.cpp

package info (click to toggle)
sofa-framework 1.0~beta4-11
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 88,820 kB
  • ctags: 27,300
  • sloc: cpp: 151,126; ansic: 2,387; xml: 581; sh: 417; makefile: 68
file content (278 lines) | stat: -rw-r--r-- 6,578 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
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
/******************************************************************************
*       SOFA, Simulation Open-Framework Architecture, version 1.0 beta 4      *
*                (c) 2006-2009 MGH, INRIA, USTL, UJF, CNRS                    *
*                                                                             *
* This library is free software; you can redistribute it and/or modify it     *
* under the terms of the GNU Lesser General Public License as published by    *
* the Free Software Foundation; either version 2.1 of the License, or (at     *
* your option) any later version.                                             *
*                                                                             *
* This library is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
* for more details.                                                           *
*                                                                             *
* You should have received a copy of the GNU Lesser General Public License    *
* along with this library; if not, write to the Free Software Foundation,     *
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.          *
*******************************************************************************
*                               SOFA :: Modules                               *
*                                                                             *
* Authors: The SOFA Team and external contributors (see Authors.txt)          *
*                                                                             *
* Contact information: contact@sofa-framework.org                             *
******************************************************************************/

#include <sofa/core/ObjectFactory.h>
#include "ImplicitSurfaceContainer.h"
namespace sofa
{
 
namespace component
{

namespace container
{


SOFA_DECL_CLASS(SphereSurface)

// Register in the Factory
int SphereSurfaceClass = core::RegisterObject("")
.add< SphereSurface >()
;

defaulttype::Vec3d ImplicitSurface::getGradient(defaulttype::Vec3d& Pos, int i)
{
	
	double epsilon=0.0001;
	double v = getValue(Pos, i);
	defaulttype::Vec3d Result;
	Pos[0] += epsilon;
	Result[0] = (getValue(Pos, i)-v)/epsilon;
	Pos[0] -= epsilon;
	Pos[1] += epsilon;
	Result[1] = (getValue(Pos, i)-v)/epsilon;	
	Pos[1] -= epsilon;
	Pos[2] += epsilon;
	Result[2] = (getValue(Pos, i)-v)/epsilon;
	Pos[2] -= epsilon;
	
	return Result;

}
bool ImplicitSurface::computeSegIntersection(defaulttype::Vec3d& posInside, defaulttype::Vec3d& posOutside, defaulttype::Vec3d& intersecPos, int i)
{
	
	
	double tolerance = 0.00001; // tolerance sur la précision m

	float a = (float)getValue(posInside, i);
	float b = (float)getValue(posOutside, i);
	
	if (a*b>0)
	{
		std::cerr<<"WARNING : les deux points sont du même côté de la surface \n"<<std::endl;
		return false;
	}

	if(b<0)
	{
		std::cerr<<"WARNING : posOutside is inside"<<std::endl;
		return false;
	}



	Vec3d Seg = posInside-posOutside;
	if (Seg.norm() < tolerance) // TODO : macro on the global precision
	{
		intersecPos = posOutside;
		return true;
	}

	// we start on posInside and search for the first point outside with a step given by scale //
	int count=0;
	Vec3d step = Seg;
	double val = b;
	intersecPos = posOutside; 
	
	double step_incr=0.1;
	
	while(step.norm()> tolerance && count < 1000)
	{
		step *= step_incr;

		while (val >= 0 && count < (1/step_incr + 1))
		{
			count++;
			intersecPos += step;
			val = getValue(intersecPos, i);
		}

		// we restart with more precision
		intersecPos -=step;
		
		val = getValue(intersecPos, i);
		if (val < 0)
			std::cerr<<"WARNING : val is negative\n"<<std::endl;
		
	}

	if (count>998)
	{
		std::cerr<<"ERROR in computeSegIntersection: Seg : "<<Seg<<std::endl;


	}



	return true;

	
	


}

void ImplicitSurface::projectPointonSurface(defaulttype::Vec3d& point, int i)
{
	
	defaulttype::Vec3d grad;
	double value= getValue(point, i);
	int it;
	for (it=0; it<10; it++)
	{
		if (value*value < 0.0000000001)
			break;
			
		grad = getGradient(point, i);
		point -= grad * (value / dot(grad,grad) ); 
		value = getValue(point, i);

		
	}
	
	if (it==10)
	{
		std::cout<<"No Convergence in projecting the contact point"<<std::endl;
		std::cout<<"-  grad :"	<< grad <<std::endl;
	}
}



bool ImplicitSurface::projectPointonSurface2(defaulttype::Vec3d& point, int i, defaulttype::Vec3d& dir)
{


	defaulttype::Vec3d posInside, posOutside;
	if (dir.norm()< 0.001){
		printf("Warning : grad is null \n");
		return false;
	}

	dir.normalize();
	double value= getValue(point, i);

	double step=0.1;

	int count = 0;
	if(value>0)
	{
		posInside = point;
		while(value>0 && count < 30)
		{
			count++;
			posInside -= dir * step;
			value = getValue(posInside, i);
		}
		posOutside = point;
	}
	else
	{
		posOutside = point;
		while(value<0 && count < 30)
		{
			count++;
			posOutside += dir * step;
			value = getValue(posOutside, i);
		}
		posInside = point;
		
	}
	if (count == 30)
	{
		printf("\n WARNING : no projection found in  ImplSurf::projectPointonSurface(Vec3d& point, Vec3d& dir)");
		return false;
	}
	return computeSegIntersection(posInside, posOutside, point, i);


}


bool ImplicitSurface::projectPointOutOfSurface(defaulttype::Vec3d& point, int i, defaulttype::Vec3d& dir, double &dist_out)
{


	if (projectPointonSurface2(point, i, dir))
	{
		defaulttype::Vec3d grad = getGradient(point, i);
		grad.normalize();
		point += grad*dist_out;
		return true;
	}
	printf(" pb in projectPointOutOfSurface \n");
	return false;


}



double SphereSurface::getValue(defaulttype::Vec3d& Pos)
{
	//std::cout<<"getValue is called for pos"<<Pos<<"radius="<<_radius <<std::endl;
	
		double result = (Pos[0] - _Center[0])*(Pos[0] - _Center[0]) +
				(Pos[1] - _Center[1])*(Pos[1] - _Center[1]) +
				(Pos[2] - _Center[2])*(Pos[2] - _Center[2]) -
				_radius * _radius ;
	if(_inside)
		result = -result;
				
	return result;	
}

/*
defaulttype::Vec3d SphereSurface::getGradient(defaulttype::Vec3d &Pos)
{
	defaulttype::Vec3d g;
	if (_inside)
	{
		g[0] = -2* (Pos[0] - _Center[0]);
		g[1] = -2* (Pos[1] - _Center[1]);	
		g[2] = -2* (Pos[2] - _Center[2]);
	}
	else
	{
		g[0] = 2* (Pos[0] - _Center[0]);
		g[1] = 2* (Pos[1] - _Center[1]);	
		g[2] = 2* (Pos[2] - _Center[2]);	
	}
	
	return g;
}
	
*/




} // namespace container

} // namespace component

} // namespace sofa