extrusion.hpp 10.9 KB
Newer Older
Pierre Kraemer's avatar
Pierre Kraemer committed
1 2 3
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
4
* Copyright (C) 2009-2012, IGG Team, LSIIT, University of Strasbourg           *
Pierre Kraemer's avatar
Pierre Kraemer committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
*                                                                              *
* 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.           *
*                                                                              *
20
* Web site: http://cgogn.unistra.fr/                                           *
Pierre Kraemer's avatar
Pierre Kraemer committed
21 22 23 24
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

Thery Sylvain's avatar
Thery Sylvain committed
25
#include "Utils/gl_def.h"
Pierre Kraemer's avatar
Pierre Kraemer committed
26 27 28 29 30 31 32 33 34 35 36 37
#include "Geometry/transfo.h"

namespace CGoGN
{

namespace Algo
{

namespace Modelisation
{

template<typename PFP>
38
Polyhedron<PFP>* revolution_prim(typename PFP::MAP& the_map, VertexAttribute<typename PFP::VEC3>& position, const std::vector<typename PFP::VEC3>& profile, const typename PFP::VEC3& center,
Pierre Kraemer's avatar
Pierre Kraemer committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
				const typename PFP::VEC3& axis, bool profile_closed, int nbSides)
{
	typedef typename PFP::VEC3 VEC3 ;

	// find circle center
	float k = (axis * (center-profile[0])) / (axis*axis);
	VEC3 circCenter = center + k*axis;

	// compute vector base plane for the circle
	VEC3 U = profile[0] - circCenter;
	VEC3 V = axis^U;
	V.normalize();
	V *=  U.norm();

	// create the path:
	std::vector<typename PFP::VEC3> path;
	path.reserve(nbSides);
	for(int i=0; i< nbSides; ++i)
	{
		float alpha = float(2.0*M_PI/nbSides*i);
Sylvain Thery's avatar
MAJ MC  
Sylvain Thery committed
59
		VEC3 P = circCenter + cos(alpha)*V + sin(alpha)*U;
Pierre Kraemer's avatar
Pierre Kraemer committed
60 61 62 63 64 65 66
		path.push_back(P);
	}
	// do the extrusion with good parameters
	return extrusion_prim<PFP>(the_map, position, profile, path[0], U, profile_closed, path, true);
}

template<typename PFP>
67
Dart revolution(typename PFP::MAP& the_map, VertexAttribute<typename PFP::VEC3>& position, const std::vector<typename PFP::VEC3>& profile, const typename PFP::VEC3& center,
Pierre Kraemer's avatar
Pierre Kraemer committed
68 69 70 71 72 73 74 75 76 77
				const typename PFP::VEC3& axis, bool profile_closed, int nbSides)
{
	Polyhedron<PFP> *prim = revolution_prim<PFP>(the_map, position, profile, center, axis, profile_closed, nbSides);
	Dart d = prim->getDart();
	delete prim;
	return d;
}


template<typename PFP>
78
Dart extrusion_scale(typename PFP::MAP& the_map, VertexAttribute<typename PFP::VEC3>& position, const std::vector<typename PFP::VEC3>& profile, const typename PFP::VEC3& centerProfile, const typename PFP::VEC3& normalProfile, bool profile_closed,
Pierre Kraemer's avatar
Pierre Kraemer committed
79 80 81 82 83 84 85 86 87
			   const std::vector<typename PFP::VEC3>& path, bool path_closed, const std::vector<float>& scalePath)
{
	Polyhedron<PFP> *prim = extrusion_scale_prim<PFP>(the_map, position, profile, centerProfile, normalProfile, profile_closed, path, path_closed,scalePath);
	Dart d = prim->getDart();
	delete prim;
	return d;
}

template<typename PFP>
88
Dart extrusion(typename PFP::MAP& the_map, VertexAttribute<typename PFP::VEC3>& position, const std::vector<typename PFP::VEC3>& profile, const typename PFP::VEC3& centerProfile, const typename PFP::VEC3& normalProfile, bool profile_closed,
Pierre Kraemer's avatar
Pierre Kraemer committed
89 90 91 92 93 94 95 96 97 98
				const std::vector<typename PFP::VEC3>& path, bool path_closed)
{
	std::vector<float> scalePath;
	Polyhedron<PFP> *prim = extrusion_scale_prim<PFP>(the_map, position, profile, centerProfile, normalProfile, profile_closed, path, path_closed,scalePath);
	Dart d = prim->getDart();
	delete prim;
	return d;
}

template<typename PFP>
99
Polyhedron<PFP>* extrusion_prim(typename PFP::MAP& the_map, VertexAttribute<typename PFP::VEC3>& position, const std::vector<typename PFP::VEC3>& profile, const typename PFP::VEC3& centerProfile, const typename PFP::VEC3& normalProfile, bool profile_closed,
Pierre Kraemer's avatar
Pierre Kraemer committed
100 101 102 103 104 105 106
			   const std::vector<typename PFP::VEC3>& path, bool path_closed)
{
	std::vector<float> scalePath;
	return extrusion_scale_prim<PFP>(the_map, position, profile, centerProfile, normalProfile, profile_closed, path, path_closed,scalePath);
}

template<typename PFP>
107
Polyhedron<PFP>* extrusion_scale_prim(typename PFP::MAP& the_map, VertexAttribute<typename PFP::VEC3>& position, const std::vector<typename PFP::VEC3>& profile, const typename PFP::VEC3& centerProfile, const typename PFP::VEC3& normal, bool profile_closed, const std::vector<typename PFP::VEC3>& path, bool path_closed, const std::vector<float>& scalePath)
Pierre Kraemer's avatar
Pierre Kraemer committed
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
{
	// topological creation
	Polyhedron<PFP> *prim = new Polyhedron<PFP>(the_map, position);
	Dart grid;

	if (profile_closed)
	{
		if (path_closed)
			grid = prim->tore_topo(profile.size() ,path.size());
		else
			grid = prim->cylinder_topo(profile.size() ,path.size()-1, false, false);
	}
	else
	{
		if (path_closed)
		{
			grid = prim->grid_topo(profile.size()-1 ,path.size());
			// sewing boundaries correponding to path boundaries
			std::vector<Dart>& darts = prim->getVertexDarts();
			int index = profile.size()*path.size();
			for (unsigned int i=0;i<profile.size()-1;++i)
			{
				Dart d = the_map.phi_1(darts[index++]);
				the_map.sewFaces(d,darts[i]);
			}
			// remove (virtually) the last row of darts that is no more need due to sewing
			darts.resize(darts.size() - profile.size());

		}
		else
			grid = prim->grid_topo(profile.size()-1 ,path.size()-1);
	}

	glPushMatrix();
	// embedding
	std::vector<Dart>& vertD = prim->getVertexDarts();
	typename PFP::VEC3 normalObj(normal);
	normalObj.normalize();

	// put profile at the beginning of path
	std::vector<typename PFP::VEC3> localObj;
	localObj.reserve(profile.size());
	for(typename std::vector<typename PFP::VEC3>::const_iterator ip=profile.begin(); ip!=profile.end(); ++ip)
	{
		typename PFP::VEC3 P = *ip + path[0] - centerProfile;
		localObj.push_back(P);
	}

	int index=0;
	for(unsigned int i=0; i<path.size(); ++i)
	{
		typename PFP::VEC3 rot(0.0f,0.0f,0.0f);
		typename PFP::VEC3 VP;
		if (i==0) //begin
		{
			// vector on path
			VP = path[i+1] - path[i];
			// computing axis of rotation
			VP.normalize();
			rot = normalObj^VP;
			normalObj= VP;
		}
		else if (i==(path.size()-1)) //end
		{
			if (!path_closed)
			{
				// vector on path
				VP = path[i] - path[i-1];
				// computing axis of rotation
				VP.normalize();
				rot=normalObj^VP;
			}
			else
			{
				typename PFP::VEC3 V1 =  path[0] - path[i];
				typename PFP::VEC3 V2 =  path[i] - path[i-1];
				V1.normalize();
				V2.normalize();
				// vector on path
				VP = V1+V2;
				// computing axis of rotation
				VP.normalize();
				rot=normalObj^VP;
			}
		}
		else // middle nodes
		{
			typename PFP::VEC3 V1 =  path[i+1] - path[i];
			typename PFP::VEC3 V2 =  path[i] - path[i-1];

			V1.normalize();
			V2.normalize();
			// vector on path
			VP = V1+V2;
			// computing axis of rotation
			VP.normalize();
			rot = normalObj^VP;

			normalObj= V1;
		}

		// computing angle of rotation
		float pscal = normalObj*VP;
		float asinAlpha = rot.normalize();
		float alpha;
		if (pscal>=0)
			alpha = asin(asinAlpha);
		else
			alpha = float(M_PI) - asin(asinAlpha);
		// creation of transformation matrix
		Geom::Matrix44f transf;
		transf.identity();
		if (alpha>0.00001f)
		{
			Geom::translate(-path[i][0],-path[i][1],-path[i][2],transf);
			Geom::rotate(rot[0],rot[1],rot[2],alpha,transf);
			Geom::translate(path[i][0],path[i][1],path[i][2],transf);
		}

227
	CGoGNout << "PATH: "<< i<< CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
228
		// apply transfo on object to embed Polyhedron.
229
		for(typename std::vector<typename PFP::VEC3>::iterator ip = localObj.begin(); ip != localObj.end(); ++ip)
Pierre Kraemer's avatar
Pierre Kraemer committed
230 231 232 233 234 235 236 237
		{
			if (i!=0) //exept for first point of path
			{
				(*ip) += (path[i]-path[i-1]);
			}

			(*ip)= Geom::transform((*ip), transf);

238
			unsigned int em = the_map.template newCell<VERTEX>();
Pierre Kraemer's avatar
Pierre Kraemer committed
239 240 241 242 243 244
//			positions[em] = (*ip);
			typename PFP::VEC3 P = (*ip); //positions.at(em);

			if (!scalePath.empty())
				P = path[i] + (scalePath[i]*(P-path[i]));

245
			CGoGNout << "P: "<< P<< CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
246 247 248 249 250 251 252 253 254 255 256 257 258 259

			// compute the scale factor for angle deformation
			float coef = 1.0f/(float(sin(M_PI/2.0f - alpha))); // warning here is angle/2 but alpha is half of angle we want to use
			if (fabs(coef-1.0f)>0.00001f)
			{
				// projection of path point on plane define par P and the rot vector
				float k = (rot*(P-path[i])) / (rot*rot);
				typename PFP::VEC3 X = path[i] + k*rot;
				//and scale in the plane
				position[em] = X + coef*(P-X);
			}
			else position[em] = P;

			Dart d = vertD[index++];
260
			the_map.template embedOrbit<VERTEX>(d, em);
Pierre Kraemer's avatar
Pierre Kraemer committed
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275

			// rotate again to put profile in the good position along the path
//			pos4=Geom::Vec4f ((*ip)[0],(*ip)[1],(*ip)[2], 1.0f);
//			np4=Geom::Vec4f( (pos4 * tv1), (pos4  * tv2), (pos4 * tv3), (pos4 * tv4));
//			(*ip)[0] = np4[0]/np4[3];
//			(*ip)[1] = np4[1]/np4[3];
//			(*ip)[2] = np4[2]/np4[3];
			(*ip)= Geom::transform((*ip), transf);
		}
	}
	glPopMatrix();
	return prim;
}

template <typename PFP>
276
Dart extrudeFace(typename PFP::MAP& the_map, VertexAttribute<typename PFP::VEC3>& positions, Dart d, const typename PFP::VEC3& N)
Pierre Kraemer's avatar
Pierre Kraemer committed
277 278 279 280 281 282 283 284 285 286 287
{
	typedef typename PFP::MAP MAP;

	// triangule
	Dart c = Algo::Modelisation::trianguleFace<PFP>(the_map,d);

	Dart cc = c;
	// cut edges
	do
	{
		the_map.cutEdge(cc);
288
		cc = the_map.phi2_1(cc);
Pierre Kraemer's avatar
Pierre Kraemer committed
289 290 291 292 293 294 295 296
	}while (cc != c);

	// cut faces
	do
	{
		Dart d1 = the_map.phi1(cc);
		Dart d2 = the_map.phi_1(cc);
		the_map.splitFace(d1,d2);
297
		cc = the_map.phi2_1(cc);
Pierre Kraemer's avatar
Pierre Kraemer committed
298 299
	}while (cc != c);

300 301
	// delete the central vertex
	the_map.deleteVertex(c) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316

	// embedding of new vertices
	Dart dd = d;
	do
	{
		const typename PFP::VEC3& P = positions[dd];
		positions[the_map.phi_1(dd)] = P+N;
		dd = the_map.phi1(the_map.phi2(the_map.phi1(dd)));
	} while (dd != d);

	// return a dart of the extruded face 
	return the_map.phi2(the_map.phi1(the_map.phi1(d)));
}

template <typename PFP>
317
Dart extrudeFace(typename PFP::MAP& the_map, VertexAttribute<typename PFP::VEC3>& position, Dart d, float dist)
Pierre Kraemer's avatar
Pierre Kraemer committed
318 319 320 321 322 323 324 325 326 327
{
	typedef typename PFP::MAP MAP;

	//compute normal
	typename PFP::VEC3 normal = Algo::Geometry::faceNormal<PFP>(the_map, d, position);
	normal *= dist;
	
	return extrudeFace<PFP>(the_map, position, d, normal);
}

328 329 330 331 332
} // namespace Modelisation

} // namespace Algo

} // namespace CGoGN