trav3_test.cpp 5.95 KB
Newer Older
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
/*******************************************************************************
* 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/embeddedMap3.h"
#include "Algo/Tiling/Volume/cubic.h"
#include "Utils/chrono.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_DOUBLE
{
	// definition of the type of the map
	typedef EmbeddedMap3 MAP;
};

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



template <typename PFP, typename V_ATT>
Sylvain Thery's avatar
Sylvain Thery committed
50
typename V_ATT::DATA_TYPE localvolumeCentroidELW(typename PFP::MAP& map, Vol d, const V_ATT& attributs)
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
{
	typedef typename V_ATT::DATA_TYPE EMB;
	EMB center(0.0);

	double count=0.0;

	for (Edge it : edgesIncidentToVolume3(map,d))
	{
		EMB e1 = attributs[it.dart];
		EMB e2 = attributs[map.phi1(it)];
		double l = (e2-e1).norm();
		center += (e1+e2)*l;
		count += 2.0*l ;
	}
	center /= double(count);
	return center ;
}


int main()
{
	// declare a map to handle the mesh
	MAP myMap;
	// add position attribute on vertices and get handler on it
	VertexAttribute<VEC3, MAP> position = myMap.addAttribute<VEC3, VERTEX, MAP>("position");


	// create a topo grid of 4x4x4 squares
	Algo::Volume::Tilings::Cubic::Grid<PFP> cubic(myMap, 20, 20, 20);
	cubic.embedIntoGrid(position, 1.0f, 1.0f, 1.0f);

	// easy way to find the central vertex of the grid	
	Vertex v;
	foreach_cell_until<VERTEX>(myMap, [&] (Vertex it)
	{
		if (position[it] == VEC3(0,0,0))
		{
			v = it;
			std::cout << "Trouve"<< std::endl;
			return false;
		}
		return true;
	});

	// must test of find ok (if not v.dart is NIL)
	if (! v.valid())
		std::cerr << "could not find a vertex with position (0,0,0)" << std::endl;

// WITH TRAVERSORS:
	
	// find incident faces to vertex
	Traversor3VF<MAP> trvf(myMap, v.dart);
	for (Dart e = trvf.begin(); e != trvf.end(); e = trvf.next())
	{
		std::cout << "Face of dart "<<e<< " incident to vertex of dart " << v.dart<< std::endl;
	}

	// find adjacent vertices thru a face
	Traversor3VVaF<MAP> trvvaf(myMap, v.dart);
	for (Dart e = trvvaf.begin(); e != trvvaf.end(); e = trvvaf.next())
	{
		std::cout << "vertex of dart "<<e<< " adjacent to vertex of dart " << v.dart<< " by a face" << std::endl;
	}

// WITH FOREACH FUNCTION (C++11 lambda expression)

	// find faces incident to vertex v
	foreach_incident3<FACE>(myMap, v, [&](Face f)
	{
		std::cout << "Face of dart " << f << " incident to vertex of dart " << v.dart << std::endl;
	});

	// find vertices adjacent to vertex v thru a face
	foreach_adjacent3<FACE>(myMap, v, [&](Vertex x)
	{
		std::cout << "vertex of dart " << x << " adjacent to vertex of dart " << v.dart << " by a face" << std::endl;
	});

// WITH FOR C++11 SYNTAX
	for(Face f : facesIncidentToVertex3(myMap,v))
		std::cout << "Face of dart " << f << " incident to vertex of dart " << v.dart << std::endl;

	for (Vertex x : verticesAdjacentByFace3(myMap,v))
		std::cout << "vertex of dart " << x << " adjacent to vertex of dart " << v.dart << " by a face" << std::endl;



	for(Vertex v1 : allVerticesOf(myMap))
	{
		for (Vertex v2 : verticesAdjacentByEdge3(myMap,v1))
		{
			for (Face f : facesIncidentToVertex3(myMap,v2))
				if ((f.dart.index)%1000 == 1 )
					std::cout << "juste for fun face " << f << std::endl;
		}
	}


	Utils::Chrono ch;

	VertexAttribute<VEC3,MAP> pos2 = myMap.getAttribute<VEC3, VERTEX, MAP>("pos2") ;
	if(!pos2.isValid())
		pos2 = myMap.addAttribute<VEC3, VERTEX, MAP>("pos2") ;

	ch.start();

	for (int i=0; i< 20; ++i)
	{
		VEC3 xx(0,0,0);
		int nb=0;
		for(Vol d : allVolumesOf(myMap))
		{
			VEC3 vCentroid = localvolumeCentroidELW<PFP>(myMap, d, position) ;
			xx += vCentroid;
			nb++;
		}
		xx /= nb;
		std::cout << xx << std::endl;
	}
	std::cout << "for "<< ch.elapsed()<< " ms "<< std::endl;

	ch.start();

	for (int i=0; i< 20; ++i)
	{
		VEC3 xx(0,0,0);
		int nb=0;
		foreach_cell<VOLUME>(myMap, [&] (Vol d)
		{
			VEC3 vCentroid = Algo::Surface::Geometry::volumeCentroidELW<PFP>(myMap, d, position) ;
			xx += vCentroid;
			nb++;
		});
		xx /= nb;
		std::cout << xx << std::endl;
	}
	std::cout << "Lambda "<< ch.elapsed()<< " ms "<< std::endl;




	return 0;
}