simple_attribs.cpp 9.01 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
/*******************************************************************************
* 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 "Topology/generic/parameters.h"
#include "Topology/map/embeddedMap2.h"
#include "Algo/Tiling/Surface/square.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 type of the map
	typedef EmbeddedMap2 MAP;
};

// some typedef shortcuts
typedef PFP::MAP MAP ;				// map type
typedef PFP::VEC3 VEC3 ;			// type of R³ vector 

/**
Sylvain Thery's avatar
Sylvain Thery committed
48 49 50
 * @brief test if map has a Vertex Attribute of VEC3 named name
 * @param map  the map
 * @param name name of attribute
Sylvain Thery's avatar
Sylvain Thery committed
51
 */
Sylvain Thery's avatar
Sylvain Thery committed
52
void testVAbyNames(MAP& map, const std::string& name)
Sylvain Thery's avatar
Sylvain Thery committed
53
{
54
	VertexAttribute<VEC3, MAP> testPos = map.getAttribute<VEC3, VERTEX, MAP>(name);
Sylvain Thery's avatar
Sylvain Thery committed
55 56 57
	if (testPos.isValid())
		std::cout << "Attribute "<< name <<" valid"<< std::endl;
	else
58
		std::cout << "Attribute "<< name <<" invalid"<< std::endl;
Sylvain Thery's avatar
Sylvain Thery committed
59 60 61 62
}

/**
 * @brief computeLengthEdges
Sylvain Thery's avatar
Sylvain Thery committed
63
 * Demonstrate usage of 2 attributes on 2 differents orbits.
Sylvain Thery's avatar
Sylvain Thery committed
64 65 66 67
 * @param map the map
 * @param pos attribute handler of position of vertices
 * @param len attribute handler of length of edges
 */
68
void computeLengthEdges(MAP& map,const VertexAttribute<VEC3, MAP>& pos, EdgeAttribute<float, MAP> len)
Sylvain Thery's avatar
Sylvain Thery committed
69 70 71 72 73 74 75 76 77 78 79
{
	// warning c++11 lambda syntax
	foreach_cell<EDGE>(map,[&](Edge e) // for all edge e of map do
	{
		VEC3 P1 = pos[e.dart]; // access with dart because of access to VertexAttribute with an edge
		VEC3 P2 = pos[map.phi1(e)]; // phi1 return a dart so no problem (and e can auto-cast in dart)
		VEC3 V = P2 - P1;
		len[e] = V.norm();
	});
}

Sylvain Thery's avatar
Sylvain Thery committed
80 81 82
/**
 * @brief computeNewPositions Demonstrate  the usage of AutoAttributes
 */
83
void computeNewPositions(MAP& map, VertexAttribute<VEC3, MAP>& pos)
Sylvain Thery's avatar
Sylvain Thery committed
84
{
Sylvain Thery's avatar
Sylvain Thery committed
85
	// here we need new and old positions simultaneously so create temporary attribute position
Sylvain Thery's avatar
Sylvain Thery committed
86

87
	VertexAutoAttribute<VEC3, MAP> pos2(map);
Sylvain Thery's avatar
Sylvain Thery committed
88 89 90 91 92 93 94 95 96 97 98 99 100 101

	foreach_cell<VERTEX>(map,[&](Vertex v)  // for all vertices
	{
		int nb=0;
		pos2[v] = VEC3(0,0,0);// init with 0,0,0,
		foreach_adjacent2<EDGE>(map,v,[&](Vertex x) // for all its neighbours (by edges)
		{
			pos2[v] += pos[x];
			nb++;
		});
		pos2[v] /= nb;
	});

	// swap attribute position with temporary (constant complexity !)
Sylvain Thery's avatar
Sylvain Thery committed
102
	// only possible with same type and same orbit attribute.
Sylvain Thery's avatar
Sylvain Thery committed
103
	map.swapAttributes(pos,pos2);
Sylvain Thery's avatar
Sylvain Thery committed
104 105

	// destruction of VertexAutoAttribute handller remove the attribute from the map.
Sylvain Thery's avatar
Sylvain Thery committed
106 107 108 109 110 111 112 113 114
}

/**
* @brief dump any attribute
* @param attr attribute
*/
template<typename ATTRIB>
void dumpAttribute(const ATTRIB& attr)
{
115
	CHECK_ATTRIBUTEHANDLER(ATTRIB);
116

117
	std::cout << "Attribute " << attr.name() << " of orbit " << orbitName(attr.getOrbit()) << " of type " << attr.typeName() << std::endl;
Sylvain Thery's avatar
Sylvain Thery committed
118 119 120

	// NEVER USE 0 AND ++ IN FOR LOOP ON ATTRIBUTE !
	// USE ONLY BEGIN / END / NEXT (for hole jumping over)
121
	for (unsigned int i = attr.begin(); i != attr.end(); attr.next(i))
Sylvain Thery's avatar
Sylvain Thery committed
122
	{
123
		std::cout << attr.name() << "[" << i << "] = " << attr[i] << std::endl;
Sylvain Thery's avatar
Sylvain Thery committed
124 125 126 127
	}
}


128 129 130 131
//function that apply on vertice with templated attribute type
template<typename T>
void VertexTyped(MAP& map, T& va)
{
132 133 134 135 136 137 138

//	foreach_cell<VERTEX>(map,[&](Vertex v)  // for all vertices
//	{
//		va[v] = 1.1 * va[v];
//	});

	// other syntax for traversal
139
	 for (Vertex v : allVerticesOf(map))
140 141 142 143
	 {
		 va[v] = 1.1 * va[v];
		 std::cout << "V:" << v << " -> "<<va[v]<< std::endl;
	 }
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
}

// version that take a VertexAttribute, check type at runtime and call instancied template version
void VertexGeneric(MAP& map, VertexAttributeGen& vg)
{
	auto va3 = dynamic_cast<VertexAttribute<VEC3, MAP>*>(&vg);
	if (va3 != NULL)
		return VertexTyped(map,*va3);

	auto vaf = dynamic_cast<VertexAttribute<float, MAP>*>(&vg);
	if (vaf != NULL)
		return VertexTyped(map,*vaf);
}



Sylvain Thery's avatar
Sylvain Thery committed
160 161 162 163 164 165
int main()
{
	// declare a map to handle the mesh
	MAP myMap;

	// add position attribute on vertices and get handler on it
166
	VertexAttribute<VEC3, MAP> positionAtt = myMap.addAttribute<VEC3, VERTEX, MAP>("position");
Sylvain Thery's avatar
Sylvain Thery committed
167 168 169 170 171 172 173 174 175 176
	if (!positionAtt.isValid())
		std::cerr << "impossible to create an attribute with name position (already used ?)"<< std::endl;


	// create a topo grid of 2x2 squares
	Algo::Surface::Tilings::Square::Grid<PFP> grid(myMap, 2, 2, true);
	// and embed it using position attribute
	grid.embedIntoGrid(positionAtt, 1.,1.,0.);


177 178
	VertexGeneric(myMap,positionAtt);

Sylvain Thery's avatar
Sylvain Thery committed
179 180
	// ATTRIBUTE DECLARATION

Sylvain Thery's avatar
Sylvain Thery committed
181
	// add an attribute of type float on orbit EDGE
182
	EdgeAttribute<float, MAP> lengthAtt = myMap.addAttribute<float, EDGE, MAP>("length");
Sylvain Thery's avatar
Sylvain Thery committed
183 184 185 186 187 188
	if (!lengthAtt.isValid())
		std::cerr << "impossible to create the attribute"<< std::endl;

	computeLengthEdges(myMap,positionAtt,lengthAtt);

	// add an attribute of type std::string on orbit FACE
189
	FaceAttribute<std::string, MAP> nameAtt = myMap.addAttribute<std::string, FACE, MAP>("name");
Sylvain Thery's avatar
Sylvain Thery committed
190 191 192 193
	if (!nameAtt.isValid())
		std::cerr << "impossible to create the attribute"<< std::endl;

	// for complex type use following template (function nameOfType not applicable)
194
	EdgeAttribute< NoTypeNameAttribute< std::vector<int> >, MAP> vectAtt = myMap.addAttribute< NoTypeNameAttribute< std::vector<int> >, EDGE, MAP>("vector_of_int");
Sylvain Thery's avatar
Sylvain Thery committed
195 196 197 198 199 200 201 202 203
	if (!vectAtt.isValid())
		std::cerr << "impossible to create the attribute"<< std::endl;

	Dart d = myMap.begin();
	// define a vertex from a dart
	Vertex v(d);
	// define a face from a dart
	Face f(d);

Sylvain Thery's avatar
Sylvain Thery committed
204 205 206

	// ATTRIBUTE ACCESS

Sylvain Thery's avatar
Sylvain Thery committed
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
	// [] operator can take a dart, a cell (only same off attribute), or an unsigned inf
	// access to any attributes with darts
	std::cout << positionAtt[d]<< std::endl;
	nameAtt[d] = "Hello";
	lengthAtt[myMap.phi1(d)] = 54.0f;

	std::vector<int> vi = {3,5,7,9,11};
	vectAtt[d]= vi;
	vectAtt[d].push_back(11);

	// access to VertexAttribute with a Vertex
	std::cout << positionAtt[v]<< std::endl;

	// access to FaceAttribute with a Face
	std::cout << nameAtt[f]<< std::endl;

Sylvain Thery's avatar
Sylvain Thery committed
223 224 225
	// following line does not compile because of wrong cell type
	//	std::cout << positionAtt[f]<< std::endl;
	//  possible to bypass using dart access
Sylvain Thery's avatar
Sylvain Thery committed
226 227
	std::cout << positionAtt[f.dart]<< std::endl;

Sylvain Thery's avatar
Sylvain Thery committed
228 229 230 231
	// access with unsigned int is dangerous, index must be obtain with begin/end/next (see dumpAttribute)

	// COPY, REMOVE, SWAP

Sylvain Thery's avatar
Sylvain Thery committed
232
	// possible to have any number of attribute a same ORBIT
233
	VertexAttribute<VEC3, MAP> position2Att = myMap.addAttribute<VEC3, VERTEX, MAP>("other_position");
Sylvain Thery's avatar
Sylvain Thery committed
234 235 236 237 238 239 240 241 242

	// copy of attribute of same type (linear complexity)
	myMap.copyAttribute(position2Att,positionAtt);

	positionAtt[v] += VEC3(0,0,1);

	computeNewPositions(myMap,positionAtt);
	dumpAttribute(positionAtt);

243

Sylvain Thery's avatar
Sylvain Thery committed
244 245
	//check if there is a Vertex Attribute of VEC3 named position => yes
	testVAbyNames(myMap,"position");
Sylvain Thery's avatar
Sylvain Thery committed
246

Sylvain Thery's avatar
Sylvain Thery committed
247 248
	// remove the attribute
	myMap.removeAttribute(positionAtt);
Sylvain Thery's avatar
Sylvain Thery committed
249

Sylvain Thery's avatar
Sylvain Thery committed
250
	//check if there is a Vertex Attribute of VEC3 named position => no
251
	std::cout << "after removing position"<< std::endl;
Sylvain Thery's avatar
Sylvain Thery committed
252
	testVAbyNames(myMap,"position");
Sylvain Thery's avatar
Sylvain Thery committed
253

254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274

	// its also possible to add an attribute only with name of type in a string (if type has been registred)
	if (myMap.addAttribute<VERTEX>("double", "pipo_double"))
	{
		VertexAttribute<double, MAP> position_double = myMap.getAttribute<double, VERTEX, MAP>("pipo_double");
		position_double[v] = 3.1415926;
	}

	if (!myMap.addAttribute<VERTEX>("double", "pipo_double"))
	{
		std::cout << "problem"<< std::endl;
	}

	if (!myMap.addAttribute<VERTEX>("triple", "pipo_triple"))
	{
		std::cout << "problem"<< std::endl;
	}




Sylvain Thery's avatar
Sylvain Thery committed
275 276
	return 0;
}