multi_att.cpp 6.4 KB
Newer Older
Sylvain Thery's avatar
Sylvain Thery 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
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
* Copyright (C) 2009-2012, 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: http://cgogn.unistra.fr/                                           *
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#include <iostream>

#include "Topology/generic/parameters.h"
#include "Topology/map/embeddedMap2.h"

#include "Geometry/vector_gen.h"

#include "Algo/Import/import.h"

#include "Topology/generic/multiAttribs.h"
#include "Algo/Geometry/centroid.h"
#include "Algo/Geometry/normal.h"


#include "Algo/Modelisation/subdivision.h"

using namespace CGoGN ;

/**
 * Struct that contains some informations about the types of the manipulated objects
 * Mainly here to be used by the algorithms that are parameterized by it
 */
struct PFP: public PFP_STANDARD
{
	// definition of the map
	typedef EmbeddedMap2 MAP;
};

53 54 55 56
typedef PFP::MAP MAP;
typedef PFP::MAP::IMPL MAP_IMPL;
typedef PFP::VEC3 VEC3;
typedef PFP::VEC4 VEC4;
Pierre Kraemer's avatar
Pierre Kraemer committed
57

Sylvain Thery's avatar
Sylvain Thery committed
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
/**
 * Simple example of function that work with generic type of attribute
 */
template <typename PFP, typename V_ATT>
typename V_ATT::DATA_TYPE smooth(typename PFP::MAP& map, Dart d, const V_ATT& attributs)
{
	typename V_ATT::DATA_TYPE res(0);
	int count=0;

	if (attributs.getOrbit() == VERTEX)
	{
		Traversor2VVaE<typename PFP::MAP> tra(map,d);
		for (Dart e = tra.begin(); e != tra.end(); e = tra.next())
		{
			res += attributs[d];
			count ++;
		}
		res /= typename PFP::REAL(count);
		return res;
	}
	else if (attributs.getOrbit() == FACE)
		{
			Traversor2FFaV<typename PFP::MAP> tra(map,d);
			for (Dart e = tra.begin(); e != tra.end(); e = tra.next())
			{
				res += attributs[d];
				count ++;
			}
			res /= typename PFP::REAL(count);
			return res;
		}

	return res;
}

template <typename PFP, typename V_ATT>
void applySmooth(typename PFP::MAP& map, const V_ATT& att_in, V_ATT& att_out)
{

	if (att_in.getOrbit() == VERTEX)
	{
		TraversorV<typename PFP::MAP> tra(map);
		for (Dart d = tra.begin(); d != tra.end(); d = tra.next())
		{
			att_out[d] = smooth<PFP>(map,d,att_in);
		}
	}
	else if (att_in.getOrbit() == FACE)
	{
		TraversorF<typename PFP::MAP> tra(map);
		for (Dart d = tra.begin(); d != tra.end(); d = tra.next())
		{
			att_out[d] = smooth<PFP>(map,d,att_in);
		}
	}
	else
		CGoGNerr << "unsupported orbit"<< CGoGNendl;
}

int main(int argc, char **argv)
{
	if(argc != 3)
	{
		CGoGNout << "Usage : " << argv[0] << " filename nbSteps" << CGoGNendl;
		return 0;
	}

	std::string filename(argv[1]);

	unsigned int nbSteps;
	std::istringstream iss(argv[2]);
	iss >> nbSteps;

	// declaration of the map
Pierre Kraemer's avatar
Pierre Kraemer committed
132
	MAP myMap;
Sylvain Thery's avatar
Sylvain Thery committed
133 134 135 136 137 138

	std::vector<std::string> attrNames ;
	Algo::Surface::Import::importMesh<PFP>(myMap, argv[1], attrNames);


	// get a handler to the 3D vector attribute created by the import
Pierre Kraemer's avatar
Pierre Kraemer committed
139 140 141 142
	VertexAttribute<VEC3, MAP_IMPL> position = myMap.getAttribute<VEC3, VERTEX>(attrNames[0]);
	VertexAttribute<VEC3, MAP_IMPL> pos2 = myMap.addAttribute<VEC3, VERTEX>("pos2");
	VertexAttribute<VEC4, MAP_IMPL> vc = myMap.addAttribute<VEC4, VERTEX>("vertexColor");
	VertexAttribute<VEC4, MAP_IMPL> vc2 = myMap.addAttribute<VEC4, VERTEX>("vertexColor2");
Sylvain Thery's avatar
Sylvain Thery committed
143 144

	// classic usage with simple vertex attributes
Pierre Kraemer's avatar
Pierre Kraemer committed
145
	applySmooth<PFP>(myMap, position, pos2);
Sylvain Thery's avatar
Sylvain Thery committed
146 147

	// multi attributes usage
Pierre Kraemer's avatar
Pierre Kraemer committed
148 149 150
	Vertex2Attributes<VEC3, VEC4, MAP_IMPL> pv_in(position, vc);
	Vertex2Attributes<VEC3, VEC4, MAP_IMPL> pv_out(pos2, vc2);
	applySmooth<PFP>(myMap, pv_in, pv_out);
Sylvain Thery's avatar
Sylvain Thery committed
151 152

	// usage with with a face attribute
Pierre Kraemer's avatar
Pierre Kraemer committed
153 154 155
	FaceAttribute<VEC4, MAP_IMPL> fc = myMap.addAttribute<VEC4, FACE>("faceColor");
	FaceAttribute<VEC4, MAP_IMPL> fc2 = myMap.addAttribute<VEC4, FACE>("faceColor2");
	applySmooth<PFP>(myMap, fc, fc2);
Sylvain Thery's avatar
Sylvain Thery committed
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


////	for(unsigned int i = 0; i < nbSteps; ++i)
////		Algo::Surface::Modelisation::LoopSubdivision<PFP>(myMap, position);


////	PFP::VEC3 c = Algo::Surface::Geometry::faceCentroidGen<PFP, VertexAttribute<PFP::VEC3> > (myMap, myMap.begin(),position);

//	T2 cc = Algo::Surface::Geometry::faceCentroid<PFP,T1> (myMap, myMap.begin(), xx);

//	typedef Face2Attributes<PFP::VEC3,PFP::VEC3>  TF;
//	TF yy(fc,fco);
//	Algo::Surface::Geometry::computeCentroidFaces< PFP >(myMap,xx,yy);

////	Algo::Surface::Geometry::computeCentroidFacesGen< PFP,VertexAttribute<PFP::VEC3> , FaceAttribute<PFP::VEC3> >(myMap,position,fc);
//	Algo::Surface::Geometry::computeCentroidFaces<PFP>(myMap,position,fc);
//	FaceAttribute<PFP::VEC3> fc2 = myMap.addAttribute<PFP::VEC3, FACE>("faceCenter2");
//	Algo::Surface::Geometry::computeCentroidFaces<PFP>(myMap,position,fc2);


//	PFP::VEC3 cx = Algo::Surface::Geometry::faceCentroid<PFP>(myMap,myMap.begin(),position);

//	for (unsigned int i=fc.begin(); i!= fc.end(); fc.next(i))
//	{
//		if (fc[i] != fc2[i])
//			std::cout << "PROBLEM" << std::endl;
//	}


//	for(unsigned int i = 0; i < nbSteps; ++i)
//		Algo::Surface::Modelisation::LoopSubdivision<PFP>(myMap, xx) ;

////	Algo::Surface::Modelisation::LoopSubdivision<PFP>(myMap, position);

    return 0;
}