simple_attribs.cpp 7.62 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 58 59 60 61 62
	if (testPos.isValid())
		std::cout << "Attribute "<< name <<" valid"<< std::endl;
	else
		std::cout << "Attribute "<< name <<"invalid"<< std::endl;
}

/**
 * @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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
}

/**
* @brief dump any attribute
* @param attr attribute
*/
template<typename ATTRIB>
void dumpAttribute(const ATTRIB& attr)
{
	std::cout << "Attribute " <<attr.name() << " of orbit "<< orbitName(attr.getOrbit())<< " of type "<< attr.typeName()<< std::endl;

	// NEVER USE 0 AND ++ IN FOR LOOP ON ATTRIBUTE !
	// USE ONLY BEGIN / END / NEXT (for hole jumping over)
	for (unsigned int i=attr.begin(); i!=attr.end(); attr.next(i))
	{
		std::cout << attr.name() << "["<< i << "] = "<<attr[i]<< std::endl;
	}
}


int main()
{
	// declare a map to handle the mesh
	MAP myMap;

	// add position attribute on vertices and get handler on it
132
	VertexAttribute<VEC3, MAP> positionAtt = myMap.addAttribute<VEC3, VERTEX, MAP>("position");
Sylvain Thery's avatar
Sylvain Thery committed
133 134 135 136 137 138 139 140 141 142
	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.);


Sylvain Thery's avatar
Sylvain Thery committed
143 144
	// ATTRIBUTE DECLARATION

Sylvain Thery's avatar
Sylvain Thery committed
145
	// add an attribute of type float on orbit EDGE
146
	EdgeAttribute<float, MAP> lengthAtt = myMap.addAttribute<float, EDGE, MAP>("length");
Sylvain Thery's avatar
Sylvain Thery committed
147 148 149 150 151 152
	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
153
	FaceAttribute<std::string, MAP> nameAtt = myMap.addAttribute<std::string, FACE, MAP>("name");
Sylvain Thery's avatar
Sylvain Thery committed
154 155 156 157
	if (!nameAtt.isValid())
		std::cerr << "impossible to create the attribute"<< std::endl;

	// for complex type use following template (function nameOfType not applicable)
158
	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
159 160 161 162 163 164 165 166 167
	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
168 169 170

	// ATTRIBUTE ACCESS

Sylvain Thery's avatar
Sylvain Thery committed
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
	// [] 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
187 188 189
	// 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
190 191
	std::cout << positionAtt[f.dart]<< std::endl;

Sylvain Thery's avatar
Sylvain Thery committed
192 193 194 195
	// 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
196
	// possible to have any number of attribute a same ORBIT
197
	VertexAttribute<VEC3, MAP> position2Att = myMap.addAttribute<VEC3, VERTEX, MAP>("other_position");
Sylvain Thery's avatar
Sylvain Thery committed
198 199 200 201 202 203 204 205 206

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

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

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

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

Sylvain Thery's avatar
Sylvain Thery committed
210 211
	// remove the attribute
	myMap.removeAttribute(positionAtt);
Sylvain Thery's avatar
Sylvain Thery committed
212

Sylvain Thery's avatar
Sylvain Thery committed
213 214
	//check if there is a Vertex Attribute of VEC3 named position => no
	testVAbyNames(myMap,"position");
Sylvain Thery's avatar
Sylvain Thery committed
215 216 217

	return 0;
}