simple_attribs.cpp 7.7 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
/*******************************************************************************
* 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::MAP::IMPL MAP_IMPL ;	// map implementation
typedef PFP::VEC3 VEC3 ;			// type of R³ vector 

/**
Sylvain Thery's avatar
Sylvain Thery committed
49 50 51
 * @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
52
 */
Sylvain Thery's avatar
Sylvain Thery committed
53
void testVAbyNames(MAP& map, const std::string& name)
Sylvain Thery's avatar
Sylvain Thery committed
54 55 56 57 58 59 60 61 62 63
{
	VertexAttribute<VEC3, MAP_IMPL> testPos = map.getAttribute<VEC3, VERTEX>(name);
	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
64
 * Demonstrate usage of 2 attributes on 2 differents orbits.
Sylvain Thery's avatar
Sylvain Thery committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
 * @param map the map
 * @param pos attribute handler of position of vertices
 * @param len attribute handler of length of edges
 */
void computeLengthEdges(MAP& map,const VertexAttribute<VEC3, MAP_IMPL>& pos, EdgeAttribute<float, MAP_IMPL> len)
{
	// 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
81 82 83
/**
 * @brief computeNewPositions Demonstrate  the usage of AutoAttributes
 */
Sylvain Thery's avatar
Sylvain Thery committed
84 85
void computeNewPositions(MAP& map, VertexAttribute<VEC3, MAP_IMPL>& pos)
{
Sylvain Thery's avatar
Sylvain Thery committed
86
	// here we need new and old positions simultaneously so create temporary attribute position
Sylvain Thery's avatar
Sylvain Thery committed
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102

	VertexAutoAttribute<VEC3, MAP_IMPL> pos2(map);

	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
103
	// only possible with same type and same orbit attribute.
Sylvain Thery's avatar
Sylvain Thery committed
104
	map.swapAttributes(pos,pos2);
Sylvain Thery's avatar
Sylvain Thery committed
105 106

	// destruction of VertexAutoAttribute handller remove the attribute from the map.
Sylvain Thery's avatar
Sylvain Thery committed
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
}

/**
* @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
	VertexAttribute<VEC3, MAP_IMPL> positionAtt = myMap.addAttribute<VEC3, VERTEX>("position");
	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
144 145
	// ATTRIBUTE DECLARATION

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

	// for complex type use following template (function nameOfType not applicable)
	EdgeAttribute< NoTypeNameAttribute< std::vector<int> >, MAP_IMPL> vectAtt = myMap.addAttribute< NoTypeNameAttribute< std::vector<int> >, EDGE>("vector_of_int");
	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
169 170 171

	// ATTRIBUTE ACCESS

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

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

	// 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
208 209
	//check if there is a Vertex Attribute of VEC3 named position => yes
	testVAbyNames(myMap,"position");
Sylvain Thery's avatar
Sylvain Thery committed
210

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

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


	return 0;
}