extrusion.hpp 11 KB
Newer Older
Pierre Kraemer's avatar
Pierre Kraemer committed
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
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
* Copyright (C) 2009, IGG Team, LSIIT, University of Strasbourg                *
*                                                                              *
* 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.           *
*                                                                              *
* Web site: https://iggservis.u-strasbg.fr/CGoGN/                              *
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#include <GL/gl.h>
#include "Geometry/transfo.h"

namespace CGoGN
{

namespace Algo
{

namespace Modelisation
{

template<typename PFP>
Polyhedron<PFP>* revolution_prim(typename PFP::MAP& the_map, typename PFP::TVEC3& position, const std::vector<typename PFP::VEC3>& profile, const typename PFP::VEC3& center,
				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);
		VEC3 P = circCenter + cosf(alpha)*V + sinf(alpha)*U;
		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>
Dart revolution(typename PFP::MAP& the_map, typename PFP::TVEC3& position, const std::vector<typename PFP::VEC3>& profile, const typename PFP::VEC3& center,
				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>
Dart extrusion_scale(typename PFP::MAP& the_map, typename PFP::TVEC3& position, const std::vector<typename PFP::VEC3>& profile, const typename PFP::VEC3& centerProfile, const typename PFP::VEC3& normalProfile, bool profile_closed,
			   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>
Dart extrusion(typename PFP::MAP& the_map, typename PFP::TVEC3& position, const std::vector<typename PFP::VEC3>& profile, const typename PFP::VEC3& centerProfile, const typename PFP::VEC3& normalProfile, bool profile_closed,
				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>
Polyhedron<PFP>* extrusion_prim(typename PFP::MAP& the_map, typename PFP::TVEC3& position, const std::vector<typename PFP::VEC3>& profile, const typename PFP::VEC3& centerProfile, const typename PFP::VEC3& normalProfile, bool profile_closed,
			   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>
Polyhedron<PFP>* extrusion_scale_prim(typename PFP::MAP& the_map, typename PFP::TVEC3& 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)
{
	// 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);
		}

	std::cout << "PATH: "<< i<< std::endl;
		// apply transfo on object to embed Polyhedron.
		for(typename std::vector<typename PFP::VEC3>::iterator ip=localObj.begin(); ip!=localObj.end(); ++ip)
		{
			if (i!=0) //exept for first point of path
			{
				(*ip) += (path[i]-path[i-1]);
			}

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

			unsigned int em = the_map.newCell(VERTEX_ORBIT);
//			positions[em] = (*ip);
			typename PFP::VEC3 P = (*ip); //positions.at(em);

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

			std::cout << "P: "<< P<< std::endl;

			// 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++];
			the_map.embedOrbit(VERTEX_ORBIT,d,em);

			// 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>
Dart extrudeFace(typename PFP::MAP& the_map, typename PFP::TVEC3& positions, Dart d, const typename PFP::VEC3& N)
{
	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);
		cc = the_map.alpha1(cc);
	}while (cc != c);

	// cut faces
	do
	{
		Dart d1 = the_map.phi1(cc);
		Dart d2 = the_map.phi_1(cc);
		the_map.splitFace(d1,d2);
		cc = the_map.alpha1(cc);
	}while (cc != c);

	//merge central faces by removing edges
	bool notFinished=true;
	do
	{
		Dart d1 = the_map.alpha1(cc);
		if (d1 == cc)			// last edge is pending edge inside of face
			notFinished = false;
		the_map.deleteFace(cc);
		cc = d1;
	} while (notFinished);

	the_map.closeHole(the_map.phi1(the_map.phi1(d)));

	// 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>
Dart extrudeFace(typename PFP::MAP& the_map, typename PFP::TVEC3& position, Dart d, float dist)
{
	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);
}

}//end namespace
}//end namespace
}//end namespace